1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2019 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
,
420 bool in_unique_section
,
424 global_variable_set_init(Bvariable
*, Bexpression
*);
427 local_variable(Bfunction
*, const std::string
&, Btype
*, Bvariable
*, bool,
431 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
435 static_chain_variable(Bfunction
*, const std::string
&, Btype
*, Location
);
438 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
439 Location
, Bstatement
**);
442 implicit_variable(const std::string
&, const std::string
&, Btype
*,
443 bool, bool, bool, int64_t);
446 implicit_variable_set_init(Bvariable
*, const std::string
&, Btype
*,
447 bool, bool, bool, Bexpression
*);
450 implicit_variable_reference(const std::string
&, const std::string
&, Btype
*);
453 immutable_struct(const std::string
&, const std::string
&,
454 bool, bool, Btype
*, Location
);
457 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
458 Location
, Bexpression
*);
461 immutable_struct_reference(const std::string
&, const std::string
&,
467 label(Bfunction
*, const std::string
& name
, Location
);
470 label_definition_statement(Blabel
*);
473 goto_statement(Blabel
*, Location
);
476 label_address(Blabel
*, Location
);
482 { return this->make_function(error_mark_node
); }
485 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
486 unsigned int flags
, Location
);
489 function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
490 Bexpression
* defer
, Location
);
493 function_set_parameters(Bfunction
* function
, const std::vector
<Bvariable
*>&);
496 function_set_body(Bfunction
* function
, Bstatement
* code_stmt
);
499 lookup_builtin(const std::string
&);
502 write_global_definitions(const std::vector
<Btype
*>&,
503 const std::vector
<Bexpression
*>&,
504 const std::vector
<Bfunction
*>&,
505 const std::vector
<Bvariable
*>&);
508 write_export_data(const char* bytes
, unsigned int size
);
512 // Make a Bexpression from a tree.
514 make_expression(tree t
)
515 { return new Bexpression(t
); }
517 // Make a Bstatement from a tree.
519 make_statement(tree t
)
520 { return new Bstatement(t
); }
522 // Make a Btype from a tree.
525 { return new Btype(t
); }
528 make_function(tree t
)
529 { return new Bfunction(t
); }
532 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
535 fill_in_array(Btype
*, Btype
*, Bexpression
*);
538 non_zero_size_type(tree
);
541 convert_tree(tree
, tree
, Location
);
545 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
546 tree fntype
, bool const_p
, bool noreturn_p
);
548 // A mapping of the GCC built-ins exposed to GCCGo.
549 std::map
<std::string
, Bfunction
*> builtin_functions_
;
552 // A helper function to create a GCC identifier from a C++ string.
555 get_identifier_from_string(const std::string
& str
)
557 return get_identifier_with_length(str
.data(), str
.length());
560 // Define the built-in functions that are exposed to GCCGo.
562 Gcc_backend::Gcc_backend()
564 /* We need to define the fetch_and_add functions, since we use them
566 tree t
= this->integer_type(true, BITS_PER_UNIT
)->get_tree();
567 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
568 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
569 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
572 t
= this->integer_type(true, BITS_PER_UNIT
* 2)->get_tree();
573 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
574 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
575 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
578 t
= this->integer_type(true, BITS_PER_UNIT
* 4)->get_tree();
579 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
580 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
581 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
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
),
590 // We use __builtin_expect for magic import functions.
591 this->define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
592 build_function_type_list(long_integer_type_node
,
593 long_integer_type_node
,
594 long_integer_type_node
,
598 // We use __builtin_memcmp for struct comparisons.
599 this->define_builtin(BUILT_IN_MEMCMP
, "__builtin_memcmp", "memcmp",
600 build_function_type_list(integer_type_node
,
607 // Used by runtime/internal/sys.
608 this->define_builtin(BUILT_IN_CTZ
, "__builtin_ctz", "ctz",
609 build_function_type_list(integer_type_node
,
613 this->define_builtin(BUILT_IN_CTZLL
, "__builtin_ctzll", "ctzll",
614 build_function_type_list(integer_type_node
,
615 long_long_unsigned_type_node
,
618 this->define_builtin(BUILT_IN_BSWAP32
, "__builtin_bswap32", "bswap32",
619 build_function_type_list(uint32_type_node
,
623 this->define_builtin(BUILT_IN_BSWAP64
, "__builtin_bswap64", "bswap64",
624 build_function_type_list(uint64_type_node
,
629 // We provide some functions for the math library.
630 tree math_function_type
= build_function_type_list(double_type_node
,
633 tree math_function_type_long
=
634 build_function_type_list(long_double_type_node
, long_double_type_node
,
636 tree math_function_type_two
= build_function_type_list(double_type_node
,
640 tree math_function_type_long_two
=
641 build_function_type_list(long_double_type_node
, long_double_type_node
,
642 long_double_type_node
, NULL_TREE
);
643 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
644 math_function_type
, true, false);
645 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
646 math_function_type_long
, true, false);
647 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
648 math_function_type
, true, false);
649 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
650 math_function_type_long
, true, false);
651 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
652 math_function_type
, true, false);
653 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
654 math_function_type_long
, true, false);
655 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
656 math_function_type_two
, true, false);
657 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
658 math_function_type_long_two
, true, false);
659 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
660 math_function_type
, true, false);
661 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
662 math_function_type_long
, true, false);
663 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
664 math_function_type
, true, false);
665 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
666 math_function_type_long
, true, false);
667 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
668 math_function_type
, true, false);
669 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
670 math_function_type_long
, true, false);
671 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
672 math_function_type
, true, false);
673 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
674 math_function_type_long
, true, false);
675 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
676 math_function_type
, true, false);
677 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
678 math_function_type_long
, true, false);
679 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
680 math_function_type
, true, false);
681 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
682 math_function_type_long
, true, false);
683 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
684 math_function_type_two
, true, false);
685 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
686 math_function_type_long_two
, true, false);
687 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
688 build_function_type_list(double_type_node
,
693 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
694 build_function_type_list(long_double_type_node
,
695 long_double_type_node
,
699 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
700 math_function_type
, true, false);
701 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
702 math_function_type_long
, true, false);
703 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
704 math_function_type
, true, false);
705 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
706 math_function_type_long
, true, false);
707 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
708 math_function_type
, true, false);
709 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
710 math_function_type_long
, true, false);
711 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
712 math_function_type
, true, false);
713 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
714 math_function_type_long
, true, false);
715 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
716 math_function_type
, true, false);
717 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
718 math_function_type_long
, true, false);
719 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
720 math_function_type
, true, false);
721 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
722 math_function_type_long
, true, false);
723 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
724 math_function_type
, true, false);
725 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
726 math_function_type_long
, true, false);
727 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
728 math_function_type
, true, false);
729 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
730 math_function_type_long
, true, false);
732 // We use __builtin_return_address in the thunk we build for
733 // functions which call recover, and for runtime.getcallerpc.
734 t
= build_function_type_list(ptr_type_node
, unsigned_type_node
, NULL_TREE
);
735 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
736 NULL
, t
, false, false);
738 // The runtime calls __builtin_dwarf_cfa for runtime.getcallersp.
739 t
= build_function_type_list(ptr_type_node
, NULL_TREE
);
740 this->define_builtin(BUILT_IN_DWARF_CFA
, "__builtin_dwarf_cfa",
741 NULL
, t
, false, false);
743 // The runtime calls __builtin_extract_return_addr when recording
744 // the address to which a function returns.
745 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR
,
746 "__builtin_extract_return_addr", NULL
,
747 build_function_type_list(ptr_type_node
,
752 // The compiler uses __builtin_trap for some exception handling
754 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
755 build_function_type(void_type_node
, void_list_node
),
758 // The runtime uses __builtin_prefetch.
759 this->define_builtin(BUILT_IN_PREFETCH
, "__builtin_prefetch", NULL
,
760 build_varargs_function_type_list(void_type_node
,
765 // The compiler uses __builtin_unreachable for cases that cannot
767 this->define_builtin(BUILT_IN_UNREACHABLE
, "__builtin_unreachable", NULL
,
768 build_function_type(void_type_node
, void_list_node
),
772 // Get an unnamed integer type.
775 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
780 if (bits
== INT_TYPE_SIZE
)
781 type
= unsigned_type_node
;
782 else if (bits
== CHAR_TYPE_SIZE
)
783 type
= unsigned_char_type_node
;
784 else if (bits
== SHORT_TYPE_SIZE
)
785 type
= short_unsigned_type_node
;
786 else if (bits
== LONG_TYPE_SIZE
)
787 type
= long_unsigned_type_node
;
788 else if (bits
== LONG_LONG_TYPE_SIZE
)
789 type
= long_long_unsigned_type_node
;
791 type
= make_unsigned_type(bits
);
795 if (bits
== INT_TYPE_SIZE
)
796 type
= integer_type_node
;
797 else if (bits
== CHAR_TYPE_SIZE
)
798 type
= signed_char_type_node
;
799 else if (bits
== SHORT_TYPE_SIZE
)
800 type
= short_integer_type_node
;
801 else if (bits
== LONG_TYPE_SIZE
)
802 type
= long_integer_type_node
;
803 else if (bits
== LONG_LONG_TYPE_SIZE
)
804 type
= long_long_integer_type_node
;
806 type
= make_signed_type(bits
);
808 return this->make_type(type
);
811 // Get an unnamed float type.
814 Gcc_backend::float_type(int bits
)
817 if (bits
== FLOAT_TYPE_SIZE
)
818 type
= float_type_node
;
819 else if (bits
== DOUBLE_TYPE_SIZE
)
820 type
= double_type_node
;
821 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
822 type
= long_double_type_node
;
825 type
= make_node(REAL_TYPE
);
826 TYPE_PRECISION(type
) = bits
;
829 return this->make_type(type
);
832 // Get an unnamed complex type.
835 Gcc_backend::complex_type(int bits
)
838 if (bits
== FLOAT_TYPE_SIZE
* 2)
839 type
= complex_float_type_node
;
840 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
841 type
= complex_double_type_node
;
842 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
843 type
= complex_long_double_type_node
;
846 type
= make_node(REAL_TYPE
);
847 TYPE_PRECISION(type
) = bits
/ 2;
849 type
= build_complex_type(type
);
851 return this->make_type(type
);
854 // Get a pointer type.
857 Gcc_backend::pointer_type(Btype
* to_type
)
859 tree to_type_tree
= to_type
->get_tree();
860 if (to_type_tree
== error_mark_node
)
861 return this->error_type();
862 tree type
= build_pointer_type(to_type_tree
);
863 return this->make_type(type
);
866 // Make a function type.
869 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
870 const std::vector
<Btyped_identifier
>& parameters
,
871 const std::vector
<Btyped_identifier
>& results
,
872 Btype
* result_struct
,
875 tree args
= NULL_TREE
;
877 if (receiver
.btype
!= NULL
)
879 tree t
= receiver
.btype
->get_tree();
880 if (t
== error_mark_node
)
881 return this->error_type();
882 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
883 pp
= &TREE_CHAIN(*pp
);
886 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
887 p
!= parameters
.end();
890 tree t
= p
->btype
->get_tree();
891 if (t
== error_mark_node
)
892 return this->error_type();
893 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
894 pp
= &TREE_CHAIN(*pp
);
897 // Varargs is handled entirely at the Go level. When converted to
898 // GENERIC functions are not varargs.
899 *pp
= void_list_node
;
903 result
= void_type_node
;
904 else if (results
.size() == 1)
905 result
= results
.front().btype
->get_tree();
908 gcc_assert(result_struct
!= NULL
);
909 result
= result_struct
->get_tree();
911 if (result
== error_mark_node
)
912 return this->error_type();
914 // The libffi library cannot represent a zero-sized object. To
915 // avoid causing confusion on 32-bit SPARC, we treat a function that
916 // returns a zero-sized value as returning void. That should do no
917 // harm since there is no actual value to be returned. See
918 // https://gcc.gnu.org/PR72814 for details.
919 if (result
!= void_type_node
&& int_size_in_bytes(result
) == 0)
920 result
= void_type_node
;
922 tree fntype
= build_function_type(result
, args
);
923 if (fntype
== error_mark_node
)
924 return this->error_type();
926 return this->make_type(build_pointer_type(fntype
));
929 // Make a struct type.
932 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
934 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
937 // Fill in the fields of a struct type.
940 Gcc_backend::fill_in_struct(Btype
* fill
,
941 const std::vector
<Btyped_identifier
>& fields
)
943 tree fill_tree
= fill
->get_tree();
944 tree field_trees
= NULL_TREE
;
945 tree
* pp
= &field_trees
;
946 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
950 tree name_tree
= get_identifier_from_string(p
->name
);
951 tree type_tree
= p
->btype
->get_tree();
952 if (type_tree
== error_mark_node
)
953 return this->error_type();
954 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
956 DECL_CONTEXT(field
) = fill_tree
;
958 pp
= &DECL_CHAIN(field
);
960 TYPE_FIELDS(fill_tree
) = field_trees
;
961 layout_type(fill_tree
);
963 // Because Go permits converting between named struct types and
964 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
965 // because we don't try to maintain TYPE_CANONICAL for struct types,
966 // we need to tell the middle-end to use structural equality.
967 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
972 // Make an array type.
975 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
977 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
978 element_btype
, length
);
981 // Fill in an array type.
984 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
987 tree element_type_tree
= element_type
->get_tree();
988 tree length_tree
= length
->get_tree();
989 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
990 return this->error_type();
992 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
994 length_tree
= fold_convert(sizetype
, length_tree
);
996 // build_index_type takes the maximum index, which is one less than
998 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
1002 tree fill_tree
= fill
->get_tree();
1003 TREE_TYPE(fill_tree
) = element_type_tree
;
1004 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
1005 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
1006 layout_type(fill_tree
);
1008 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
1009 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1010 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
1011 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
1012 TYPE_CANONICAL(fill_tree
) =
1013 build_array_type(TYPE_CANONICAL(element_type_tree
),
1014 TYPE_CANONICAL(index_type_tree
));
1019 // Create a placeholder for a pointer type.
1022 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
1023 Location location
, bool)
1025 tree ret
= build_distinct_type_copy(ptr_type_node
);
1028 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1029 get_identifier_from_string(name
),
1031 TYPE_NAME(ret
) = decl
;
1033 return this->make_type(ret
);
1036 // Set the real target type for a placeholder pointer type.
1039 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
1042 tree pt
= placeholder
->get_tree();
1043 if (pt
== error_mark_node
)
1045 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
1046 tree tt
= to_type
->get_tree();
1047 if (tt
== error_mark_node
)
1049 placeholder
->set_tree(error_mark_node
);
1052 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
1053 TREE_TYPE(pt
) = TREE_TYPE(tt
);
1054 TYPE_CANONICAL(pt
) = TYPE_CANONICAL(tt
);
1055 if (TYPE_NAME(pt
) != NULL_TREE
)
1057 // Build the data structure gcc wants to see for a typedef.
1058 tree copy
= build_variant_type_copy(pt
);
1059 TYPE_NAME(copy
) = NULL_TREE
;
1060 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
1065 // Set the real values for a placeholder function type.
1068 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
1070 return this->set_placeholder_pointer_type(placeholder
, ft
);
1073 // Create a placeholder for a struct type.
1076 Gcc_backend::placeholder_struct_type(const std::string
& name
,
1079 tree ret
= make_node(RECORD_TYPE
);
1082 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1083 get_identifier_from_string(name
),
1085 TYPE_NAME(ret
) = decl
;
1087 // The struct type that eventually replaces this placeholder will require
1088 // structural equality. The placeholder must too, so that the requirement
1089 // for structural equality propagates to references that are constructed
1090 // before the replacement occurs.
1091 SET_TYPE_STRUCTURAL_EQUALITY(ret
);
1093 return this->make_type(ret
);
1096 // Fill in the fields of a placeholder struct type.
1099 Gcc_backend::set_placeholder_struct_type(
1101 const std::vector
<Btyped_identifier
>& fields
)
1103 tree t
= placeholder
->get_tree();
1104 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
1105 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
1107 if (TYPE_NAME(t
) != NULL_TREE
)
1109 // Build the data structure gcc wants to see for a typedef.
1110 tree copy
= build_distinct_type_copy(t
);
1111 TYPE_NAME(copy
) = NULL_TREE
;
1112 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1113 TYPE_SIZE(copy
) = NULL_TREE
;
1114 Btype
* bc
= this->make_type(copy
);
1115 this->fill_in_struct(bc
, fields
);
1119 return r
->get_tree() != error_mark_node
;
1122 // Create a placeholder for an array type.
1125 Gcc_backend::placeholder_array_type(const std::string
& name
,
1128 tree ret
= make_node(ARRAY_TYPE
);
1129 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1130 get_identifier_from_string(name
),
1132 TYPE_NAME(ret
) = decl
;
1133 return this->make_type(ret
);
1136 // Fill in the fields of a placeholder array type.
1139 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1140 Btype
* element_btype
,
1141 Bexpression
* length
)
1143 tree t
= placeholder
->get_tree();
1144 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1145 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1147 // Build the data structure gcc wants to see for a typedef.
1148 tree copy
= build_distinct_type_copy(t
);
1149 TYPE_NAME(copy
) = NULL_TREE
;
1150 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1152 return r
->get_tree() != error_mark_node
;
1155 // Return a named version of a type.
1158 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1161 tree type
= btype
->get_tree();
1162 if (type
== error_mark_node
)
1163 return this->error_type();
1165 // The middle-end expects a basic type to have a name. In Go every
1166 // basic type will have a name. The first time we see a basic type,
1167 // give it whatever Go name we have at this point.
1168 if (TYPE_NAME(type
) == NULL_TREE
1169 && location
.gcc_location() == BUILTINS_LOCATION
1170 && (TREE_CODE(type
) == INTEGER_TYPE
1171 || TREE_CODE(type
) == REAL_TYPE
1172 || TREE_CODE(type
) == COMPLEX_TYPE
1173 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1175 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1176 get_identifier_from_string(name
),
1178 TYPE_NAME(type
) = decl
;
1179 return this->make_type(type
);
1182 tree copy
= build_variant_type_copy(type
);
1183 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1184 get_identifier_from_string(name
),
1186 DECL_ORIGINAL_TYPE(decl
) = type
;
1187 TYPE_NAME(copy
) = decl
;
1188 return this->make_type(copy
);
1191 // Return a pointer type used as a marker for a circular type.
1194 Gcc_backend::circular_pointer_type(Btype
*, bool)
1196 return this->make_type(ptr_type_node
);
1199 // Return whether we might be looking at a circular type.
1202 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1204 return btype
->get_tree() == ptr_type_node
;
1207 // Return the size of a type.
1210 Gcc_backend::type_size(Btype
* btype
)
1212 tree t
= btype
->get_tree();
1213 if (t
== error_mark_node
)
1215 if (t
== void_type_node
)
1217 t
= TYPE_SIZE_UNIT(t
);
1218 gcc_assert(tree_fits_uhwi_p (t
));
1219 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1220 int64_t ret
= static_cast<int64_t>(val_wide
);
1221 if (ret
< 0 || static_cast<unsigned HOST_WIDE_INT
>(ret
) != val_wide
)
1226 // Return the alignment of a type.
1229 Gcc_backend::type_alignment(Btype
* btype
)
1231 tree t
= btype
->get_tree();
1232 if (t
== error_mark_node
)
1234 return TYPE_ALIGN_UNIT(t
);
1237 // Return the alignment of a struct field of type BTYPE.
1240 Gcc_backend::type_field_alignment(Btype
* btype
)
1242 tree t
= btype
->get_tree();
1243 if (t
== error_mark_node
)
1245 return go_field_alignment(t
);
1248 // Return the offset of a field in a struct.
1251 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1253 tree struct_tree
= btype
->get_tree();
1254 if (struct_tree
== error_mark_node
)
1256 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1257 tree field
= TYPE_FIELDS(struct_tree
);
1258 for (; index
> 0; --index
)
1260 field
= DECL_CHAIN(field
);
1261 gcc_assert(field
!= NULL_TREE
);
1263 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1264 int64_t ret
= static_cast<int64_t>(offset_wide
);
1265 gcc_assert(ret
== offset_wide
);
1269 // Return the zero value for a type.
1272 Gcc_backend::zero_expression(Btype
* btype
)
1274 tree t
= btype
->get_tree();
1276 if (t
== error_mark_node
)
1277 ret
= error_mark_node
;
1279 ret
= build_zero_cst(t
);
1280 return this->make_expression(ret
);
1283 // An expression that references a variable.
1286 Gcc_backend::var_expression(Bvariable
* var
, Location location
)
1288 tree ret
= var
->get_tree(location
);
1289 if (ret
== error_mark_node
)
1290 return this->error_expression();
1291 return this->make_expression(ret
);
1294 // An expression that indirectly references an expression.
1297 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1298 bool known_valid
, Location location
)
1300 tree expr_tree
= expr
->get_tree();
1301 tree type_tree
= btype
->get_tree();
1302 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1303 return this->error_expression();
1305 // If the type of EXPR is a recursive pointer type, then we
1306 // need to insert a cast before indirecting.
1307 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1308 if (VOID_TYPE_P(target_type_tree
))
1309 expr_tree
= fold_convert_loc(location
.gcc_location(),
1310 build_pointer_type(type_tree
), expr_tree
);
1312 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1315 TREE_THIS_NOTRAP(ret
) = 1;
1316 return this->make_expression(ret
);
1319 // Return an expression that declares a constant named NAME with the
1320 // constant value VAL in BTYPE.
1323 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1324 Bexpression
* val
, Location location
)
1326 tree type_tree
= btype
->get_tree();
1327 tree const_val
= val
->get_tree();
1328 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1329 return this->error_expression();
1331 tree name_tree
= get_identifier_from_string(name
);
1332 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1334 DECL_INITIAL(decl
) = const_val
;
1335 TREE_CONSTANT(decl
) = 1;
1336 TREE_READONLY(decl
) = 1;
1338 go_preserve_from_gc(decl
);
1339 return this->make_expression(decl
);
1342 // Return a typed value as a constant integer.
1345 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1347 tree t
= btype
->get_tree();
1348 if (t
== error_mark_node
)
1349 return this->error_expression();
1351 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1352 return this->make_expression(ret
);
1355 // Return a typed value as a constant floating-point number.
1358 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1360 tree t
= btype
->get_tree();
1362 if (t
== error_mark_node
)
1363 return this->error_expression();
1366 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1368 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1369 ret
= build_real(t
, r2
);
1370 return this->make_expression(ret
);
1373 // Return a typed real and imaginary value as a constant complex number.
1376 Gcc_backend::complex_constant_expression(Btype
* btype
, mpc_t val
)
1378 tree t
= btype
->get_tree();
1380 if (t
== error_mark_node
)
1381 return this->error_expression();
1384 real_from_mpfr(&r1
, mpc_realref(val
), TREE_TYPE(t
), GMP_RNDN
);
1386 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1389 real_from_mpfr(&r3
, mpc_imagref(val
), TREE_TYPE(t
), GMP_RNDN
);
1391 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1393 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1394 build_real(TREE_TYPE(t
), r4
));
1395 return this->make_expression(ret
);
1398 // Make a constant string expression.
1401 Gcc_backend::string_constant_expression(const std::string
& val
)
1403 tree index_type
= build_index_type(size_int(val
.length()));
1404 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1406 tree string_type
= build_array_type(const_char_type
, index_type
);
1407 TYPE_STRING_FLAG(string_type
) = 1;
1408 tree string_val
= build_string(val
.length(), val
.data());
1409 TREE_TYPE(string_val
) = string_type
;
1411 return this->make_expression(string_val
);
1414 // Make a constant boolean expression.
1417 Gcc_backend::boolean_constant_expression(bool val
)
1419 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1420 return this->make_expression(bool_cst
);
1423 // Return the real part of a complex expression.
1426 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1428 tree complex_tree
= bcomplex
->get_tree();
1429 if (complex_tree
== error_mark_node
)
1430 return this->error_expression();
1431 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1432 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1433 TREE_TYPE(TREE_TYPE(complex_tree
)),
1435 return this->make_expression(ret
);
1438 // Return the imaginary part of a complex expression.
1441 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1443 tree complex_tree
= bcomplex
->get_tree();
1444 if (complex_tree
== error_mark_node
)
1445 return this->error_expression();
1446 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1447 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1448 TREE_TYPE(TREE_TYPE(complex_tree
)),
1450 return this->make_expression(ret
);
1453 // Make a complex expression given its real and imaginary parts.
1456 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1459 tree real_tree
= breal
->get_tree();
1460 tree imag_tree
= bimag
->get_tree();
1461 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1462 return this->error_expression();
1463 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1464 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1465 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1466 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1467 build_complex_type(TREE_TYPE(real_tree
)),
1468 real_tree
, imag_tree
);
1469 return this->make_expression(ret
);
1472 // An expression that converts an expression to a different type.
1475 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1478 tree type_tree
= type
->get_tree();
1479 tree expr_tree
= expr
->get_tree();
1480 if (type_tree
== error_mark_node
1481 || expr_tree
== error_mark_node
1482 || TREE_TYPE(expr_tree
) == error_mark_node
)
1483 return this->error_expression();
1486 if (this->type_size(type
) == 0
1487 || TREE_TYPE(expr_tree
) == void_type_node
)
1489 // Do not convert zero-sized types.
1492 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1493 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1494 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1495 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1496 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1497 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1498 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1499 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1500 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1501 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1502 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1503 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1504 type_tree
, expr_tree
);
1506 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1508 return this->make_expression(ret
);
1511 // Get the address of a function.
1514 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1516 tree func
= bfunc
->get_tree();
1517 if (func
== error_mark_node
)
1518 return this->error_expression();
1520 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1521 return this->make_expression(ret
);
1524 // Get the address of an expression.
1527 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1529 tree expr
= bexpr
->get_tree();
1530 if (expr
== error_mark_node
)
1531 return this->error_expression();
1533 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1534 return this->make_expression(ret
);
1537 // Return an expression for the field at INDEX in BSTRUCT.
1540 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1543 tree struct_tree
= bstruct
->get_tree();
1544 if (struct_tree
== error_mark_node
1545 || TREE_TYPE(struct_tree
) == error_mark_node
)
1546 return this->error_expression();
1547 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1548 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1549 if (field
== NULL_TREE
)
1551 // This can happen for a type which refers to itself indirectly
1552 // and then turns out to be erroneous.
1553 return this->error_expression();
1555 for (unsigned int i
= index
; i
> 0; --i
)
1557 field
= DECL_CHAIN(field
);
1558 gcc_assert(field
!= NULL_TREE
);
1560 if (TREE_TYPE(field
) == error_mark_node
)
1561 return this->error_expression();
1562 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1563 TREE_TYPE(field
), struct_tree
, field
,
1565 if (TREE_CONSTANT(struct_tree
))
1566 TREE_CONSTANT(ret
) = 1;
1567 return this->make_expression(ret
);
1570 // Return an expression that executes BSTAT before BEXPR.
1573 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1576 tree stat
= bstat
->get_tree();
1577 tree expr
= bexpr
->get_tree();
1578 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1579 return this->error_expression();
1580 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1581 TREE_TYPE(expr
), stat
, expr
);
1582 return this->make_expression(ret
);
1585 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1586 // ELSE_EXPR otherwise.
1589 Gcc_backend::conditional_expression(Bfunction
*, Btype
* btype
,
1590 Bexpression
* condition
,
1591 Bexpression
* then_expr
,
1592 Bexpression
* else_expr
, Location location
)
1594 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1595 tree cond_tree
= condition
->get_tree();
1596 tree then_tree
= then_expr
->get_tree();
1597 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1598 if (type_tree
== error_mark_node
1599 || cond_tree
== error_mark_node
1600 || then_tree
== error_mark_node
1601 || else_tree
== error_mark_node
)
1602 return this->error_expression();
1603 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1604 cond_tree
, then_tree
, else_tree
);
1605 return this->make_expression(ret
);
1608 // Return an expression for the unary operation OP EXPR.
1611 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1613 tree expr_tree
= expr
->get_tree();
1614 if (expr_tree
== error_mark_node
1615 || TREE_TYPE(expr_tree
) == error_mark_node
)
1616 return this->error_expression();
1618 tree type_tree
= TREE_TYPE(expr_tree
);
1619 enum tree_code code
;
1622 case OPERATOR_MINUS
:
1624 tree computed_type
= excess_precision_type(type_tree
);
1625 if (computed_type
!= NULL_TREE
)
1627 expr_tree
= convert(computed_type
, expr_tree
);
1628 type_tree
= computed_type
;
1634 code
= TRUTH_NOT_EXPR
;
1637 code
= BIT_NOT_EXPR
;
1644 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1646 return this->make_expression(ret
);
1649 // Convert a gofrontend operator to an equivalent tree_code.
1651 static enum tree_code
1652 operator_to_tree_code(Operator op
, tree type
)
1654 enum tree_code code
;
1660 case OPERATOR_NOTEQ
:
1676 code
= TRUTH_ORIF_EXPR
;
1678 case OPERATOR_ANDAND
:
1679 code
= TRUTH_ANDIF_EXPR
;
1684 case OPERATOR_MINUS
:
1688 code
= BIT_IOR_EXPR
;
1691 code
= BIT_XOR_EXPR
;
1697 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1700 code
= TRUNC_DIV_EXPR
;
1703 code
= TRUNC_MOD_EXPR
;
1705 case OPERATOR_LSHIFT
:
1708 case OPERATOR_RSHIFT
:
1712 code
= BIT_AND_EXPR
;
1714 case OPERATOR_BITCLEAR
:
1715 code
= BIT_AND_EXPR
;
1724 // Return an expression for the binary operation LEFT OP RIGHT.
1727 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1728 Bexpression
* right
, Location location
)
1730 tree left_tree
= left
->get_tree();
1731 tree right_tree
= right
->get_tree();
1732 if (left_tree
== error_mark_node
1733 || right_tree
== error_mark_node
)
1734 return this->error_expression();
1735 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1737 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1738 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1739 tree computed_type
= excess_precision_type(type_tree
);
1740 if (computed_type
!= NULL_TREE
)
1742 left_tree
= convert(computed_type
, left_tree
);
1743 right_tree
= convert(computed_type
, right_tree
);
1744 type_tree
= computed_type
;
1747 // For comparison operators, the resulting type should be boolean.
1751 case OPERATOR_NOTEQ
:
1756 type_tree
= boolean_type_node
;
1762 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1763 left_tree
, right_tree
);
1764 return this->make_expression(ret
);
1767 // Return an expression that constructs BTYPE with VALS.
1770 Gcc_backend::constructor_expression(Btype
* btype
,
1771 const std::vector
<Bexpression
*>& vals
,
1774 tree type_tree
= btype
->get_tree();
1775 if (type_tree
== error_mark_node
)
1776 return this->error_expression();
1778 vec
<constructor_elt
, va_gc
> *init
;
1779 vec_alloc(init
, vals
.size());
1781 tree sink
= NULL_TREE
;
1782 bool is_constant
= true;
1783 tree field
= TYPE_FIELDS(type_tree
);
1784 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1786 ++p
, field
= DECL_CHAIN(field
))
1788 gcc_assert(field
!= NULL_TREE
);
1789 tree val
= (*p
)->get_tree();
1790 if (TREE_TYPE(field
) == error_mark_node
1791 || val
== error_mark_node
1792 || TREE_TYPE(val
) == error_mark_node
)
1793 return this->error_expression();
1795 if (int_size_in_bytes(TREE_TYPE(field
)) == 0)
1797 // GIMPLE cannot represent indices of zero-sized types so
1798 // trying to construct a map with zero-sized keys might lead
1799 // to errors. Instead, we evaluate each expression that
1800 // would have been added as a map element for its
1801 // side-effects and construct an empty map.
1802 append_to_statement_list(val
, &sink
);
1806 constructor_elt empty
= {NULL
, NULL
};
1807 constructor_elt
* elt
= init
->quick_push(empty
);
1809 elt
->value
= this->convert_tree(TREE_TYPE(field
), val
, location
);
1810 if (!TREE_CONSTANT(elt
->value
))
1811 is_constant
= false;
1813 gcc_assert(field
== NULL_TREE
);
1814 tree ret
= build_constructor(type_tree
, init
);
1816 TREE_CONSTANT(ret
) = 1;
1817 if (sink
!= NULL_TREE
)
1818 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1819 type_tree
, sink
, ret
);
1820 return this->make_expression(ret
);
1824 Gcc_backend::array_constructor_expression(
1825 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1826 const std::vector
<Bexpression
*>& vals
, Location location
)
1828 tree type_tree
= array_btype
->get_tree();
1829 if (type_tree
== error_mark_node
)
1830 return this->error_expression();
1832 gcc_assert(indexes
.size() == vals
.size());
1834 tree element_type
= TREE_TYPE(type_tree
);
1835 HOST_WIDE_INT element_size
= int_size_in_bytes(element_type
);
1836 vec
<constructor_elt
, va_gc
> *init
;
1837 vec_alloc(init
, element_size
== 0 ? 0 : vals
.size());
1839 tree sink
= NULL_TREE
;
1840 bool is_constant
= true;
1841 for (size_t i
= 0; i
< vals
.size(); ++i
)
1843 tree index
= size_int(indexes
[i
]);
1844 tree val
= (vals
[i
])->get_tree();
1846 if (index
== error_mark_node
1847 || val
== error_mark_node
)
1848 return this->error_expression();
1850 if (element_size
== 0)
1852 // GIMPLE cannot represent arrays of zero-sized types so trying
1853 // to construct an array of zero-sized values might lead to errors.
1854 // Instead, we evaluate each expression that would have been added as
1855 // an array value for its side-effects and construct an empty array.
1856 append_to_statement_list(val
, &sink
);
1860 if (!TREE_CONSTANT(val
))
1861 is_constant
= false;
1863 constructor_elt empty
= {NULL
, NULL
};
1864 constructor_elt
* elt
= init
->quick_push(empty
);
1869 tree ret
= build_constructor(type_tree
, init
);
1871 TREE_CONSTANT(ret
) = 1;
1872 if (sink
!= NULL_TREE
)
1873 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1874 type_tree
, sink
, ret
);
1875 return this->make_expression(ret
);
1878 // Return an expression for the address of BASE[INDEX].
1881 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
1884 tree base_tree
= base
->get_tree();
1885 tree index_tree
= index
->get_tree();
1886 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
1887 if (base_tree
== error_mark_node
1888 || TREE_TYPE(base_tree
) == error_mark_node
1889 || index_tree
== error_mark_node
1890 || element_type_tree
== error_mark_node
)
1891 return this->error_expression();
1893 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
1894 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
1895 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
1896 index_tree
, element_size
);
1897 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
1898 TREE_TYPE(base_tree
), base_tree
, offset
);
1899 return this->make_expression(ptr
);
1902 // Return an expression representing ARRAY[INDEX]
1905 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
1908 tree array_tree
= array
->get_tree();
1909 tree index_tree
= index
->get_tree();
1910 if (array_tree
== error_mark_node
1911 || TREE_TYPE(array_tree
) == error_mark_node
1912 || index_tree
== error_mark_node
)
1913 return this->error_expression();
1915 // A function call that returns a zero sized object will have been
1916 // changed to return void. If we see void here, assume we are
1917 // dealing with a zero sized type and just evaluate the operands.
1919 if (TREE_TYPE(array_tree
) != void_type_node
)
1920 ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
1921 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
1922 index_tree
, NULL_TREE
, NULL_TREE
);
1924 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1925 void_type_node
, array_tree
, index_tree
);
1927 return this->make_expression(ret
);
1930 // Create an expression for a call to FN_EXPR with FN_ARGS.
1932 Gcc_backend::call_expression(Bfunction
*, // containing fcn for call
1933 Bexpression
* fn_expr
,
1934 const std::vector
<Bexpression
*>& fn_args
,
1935 Bexpression
* chain_expr
,
1938 tree fn
= fn_expr
->get_tree();
1939 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
1940 return this->error_expression();
1942 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
1943 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
1945 size_t nargs
= fn_args
.size();
1946 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
1947 for (size_t i
= 0; i
< nargs
; ++i
)
1949 args
[i
] = fn_args
.at(i
)->get_tree();
1950 if (args
[i
] == error_mark_node
)
1951 return this->error_expression();
1955 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
1956 fndecl
= TREE_OPERAND(fndecl
, 0);
1958 // This is to support builtin math functions when using 80387 math.
1959 tree excess_type
= NULL_TREE
;
1961 && TREE_CODE(fndecl
) == FUNCTION_DECL
1962 && fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
1963 && DECL_IS_BUILTIN (fndecl
)
1965 && ((SCALAR_FLOAT_TYPE_P(rettype
)
1966 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
1967 || (COMPLEX_FLOAT_TYPE_P(rettype
)
1968 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
1970 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
1971 if (excess_type
!= NULL_TREE
)
1973 tree excess_fndecl
= mathfn_built_in(excess_type
,
1974 DECL_FUNCTION_CODE(fndecl
));
1975 if (excess_fndecl
== NULL_TREE
)
1976 excess_type
= NULL_TREE
;
1979 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
1981 for (size_t i
= 0; i
< nargs
; ++i
)
1983 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
1984 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
1985 args
[i
] = ::convert(excess_type
, args
[i
]);
1992 build_call_array_loc(location
.gcc_location(),
1993 excess_type
!= NULL_TREE
? excess_type
: rettype
,
1997 CALL_EXPR_STATIC_CHAIN (ret
) = chain_expr
->get_tree();
1999 if (excess_type
!= NULL_TREE
)
2001 // Calling convert here can undo our excess precision change.
2002 // That may or may not be a bug in convert_to_real.
2003 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
2007 return this->make_expression(ret
);
2010 // An expression as a statement.
2013 Gcc_backend::expression_statement(Bfunction
*, Bexpression
* expr
)
2015 return this->make_statement(expr
->get_tree());
2018 // Variable initialization.
2021 Gcc_backend::init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
)
2023 tree var_tree
= var
->get_decl();
2024 tree init_tree
= init
->get_tree();
2025 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
2026 return this->error_statement();
2027 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
2029 // To avoid problems with GNU ld, we don't make zero-sized
2030 // externally visible variables. That might lead us to doing an
2031 // initialization of a zero-sized expression to a non-zero sized
2032 // variable, or vice-versa. Avoid crashes by omitting the
2033 // initializer. Such initializations don't mean anything anyhow.
2034 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
2035 && init_tree
!= NULL_TREE
2036 && TREE_TYPE(init_tree
) != void_type_node
2037 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
2039 DECL_INITIAL(var_tree
) = init_tree
;
2040 init_tree
= NULL_TREE
;
2043 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
2044 void_type_node
, var_tree
);
2045 if (init_tree
!= NULL_TREE
)
2046 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
2047 void_type_node
, init_tree
, ret
);
2049 return this->make_statement(ret
);
2055 Gcc_backend::assignment_statement(Bfunction
* bfn
, Bexpression
* lhs
,
2056 Bexpression
* rhs
, Location location
)
2058 tree lhs_tree
= lhs
->get_tree();
2059 tree rhs_tree
= rhs
->get_tree();
2060 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
2061 return this->error_statement();
2063 // To avoid problems with GNU ld, we don't make zero-sized
2064 // externally visible variables. That might lead us to doing an
2065 // assignment of a zero-sized expression to a non-zero sized
2066 // expression; avoid crashes here by avoiding assignments of
2067 // zero-sized expressions. Such assignments don't really mean
2069 if (TREE_TYPE(lhs_tree
) == void_type_node
2070 || int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
2071 || TREE_TYPE(rhs_tree
) == void_type_node
2072 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
2073 return this->compound_statement(this->expression_statement(bfn
, lhs
),
2074 this->expression_statement(bfn
, rhs
));
2076 rhs_tree
= this->convert_tree(TREE_TYPE(lhs_tree
), rhs_tree
, location
);
2078 return this->make_statement(fold_build2_loc(location
.gcc_location(),
2081 lhs_tree
, rhs_tree
));
2087 Gcc_backend::return_statement(Bfunction
* bfunction
,
2088 const std::vector
<Bexpression
*>& vals
,
2091 tree fntree
= bfunction
->get_tree();
2092 if (fntree
== error_mark_node
)
2093 return this->error_statement();
2094 tree result
= DECL_RESULT(fntree
);
2095 if (result
== error_mark_node
)
2096 return this->error_statement();
2098 // If the result size is zero bytes, we have set the function type
2099 // to have a result type of void, so don't return anything.
2100 // See the function_type method.
2101 tree res_type
= TREE_TYPE(result
);
2102 if (res_type
== void_type_node
|| int_size_in_bytes(res_type
) == 0)
2104 tree stmt_list
= NULL_TREE
;
2105 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2109 tree val
= (*p
)->get_tree();
2110 if (val
== error_mark_node
)
2111 return this->error_statement();
2112 append_to_statement_list(val
, &stmt_list
);
2114 tree ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2115 void_type_node
, NULL_TREE
);
2116 append_to_statement_list(ret
, &stmt_list
);
2117 return this->make_statement(stmt_list
);
2122 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
2124 else if (vals
.size() == 1)
2126 tree val
= vals
.front()->get_tree();
2127 if (val
== error_mark_node
)
2128 return this->error_statement();
2129 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2130 void_type_node
, result
,
2131 vals
.front()->get_tree());
2132 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2133 void_type_node
, set
);
2137 // To return multiple values, copy the values into a temporary
2138 // variable of the right structure type, and then assign the
2139 // temporary variable to the DECL_RESULT in the return
2141 tree stmt_list
= NULL_TREE
;
2142 tree rettype
= TREE_TYPE(result
);
2144 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2145 push_struct_function(fntree
);
2147 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2148 tree rettmp
= create_tmp_var(rettype
, "RESULT");
2151 tree field
= TYPE_FIELDS(rettype
);
2152 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2154 p
++, field
= DECL_CHAIN(field
))
2156 gcc_assert(field
!= NULL_TREE
);
2157 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
2158 TREE_TYPE(field
), rettmp
, field
,
2160 tree val
= (*p
)->get_tree();
2161 if (val
== error_mark_node
)
2162 return this->error_statement();
2163 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2165 ref
, (*p
)->get_tree());
2166 append_to_statement_list(set
, &stmt_list
);
2168 gcc_assert(field
== NULL_TREE
);
2169 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2172 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2173 void_type_node
, set
);
2174 append_to_statement_list(ret_expr
, &stmt_list
);
2177 return this->make_statement(ret
);
2180 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2181 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2182 // NULL, it will always be executed. This is used for handling defers in Go
2183 // functions. In C++, the resulting code is of this form:
2184 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2187 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2188 Bstatement
* except_stmt
,
2189 Bstatement
* finally_stmt
,
2192 tree stat_tree
= bstat
->get_tree();
2193 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2194 tree finally_tree
= finally_stmt
== NULL
2196 : finally_stmt
->get_tree();
2198 if (stat_tree
== error_mark_node
2199 || except_tree
== error_mark_node
2200 || finally_tree
== error_mark_node
)
2201 return this->error_statement();
2203 if (except_tree
!= NULL_TREE
)
2204 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2205 void_type_node
, stat_tree
,
2206 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2207 void_type_node
, NULL
, except_tree
));
2208 if (finally_tree
!= NULL_TREE
)
2209 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2210 void_type_node
, stat_tree
, finally_tree
);
2211 return this->make_statement(stat_tree
);
2217 Gcc_backend::if_statement(Bfunction
*, Bexpression
* condition
,
2218 Bblock
* then_block
, Bblock
* else_block
,
2221 tree cond_tree
= condition
->get_tree();
2222 tree then_tree
= then_block
->get_tree();
2223 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2224 if (cond_tree
== error_mark_node
2225 || then_tree
== error_mark_node
2226 || else_tree
== error_mark_node
)
2227 return this->error_statement();
2228 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2229 cond_tree
, then_tree
, else_tree
);
2230 return this->make_statement(ret
);
2236 Gcc_backend::switch_statement(
2237 Bfunction
* function
,
2239 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2240 const std::vector
<Bstatement
*>& statements
,
2241 Location switch_location
)
2243 gcc_assert(cases
.size() == statements
.size());
2245 tree decl
= function
->get_tree();
2246 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2247 push_struct_function(decl
);
2249 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2251 tree stmt_list
= NULL_TREE
;
2252 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2253 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2254 ps
!= statements
.end();
2259 location_t loc
= (*ps
!= NULL
2260 ? EXPR_LOCATION((*ps
)->get_tree())
2261 : UNKNOWN_LOCATION
);
2262 tree label
= create_artificial_label(loc
);
2263 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2264 append_to_statement_list(c
, &stmt_list
);
2268 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2272 tree t
= (*pcv
)->get_tree();
2273 if (t
== error_mark_node
)
2274 return this->error_statement();
2275 location_t loc
= EXPR_LOCATION(t
);
2276 tree label
= create_artificial_label(loc
);
2277 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2278 append_to_statement_list(c
, &stmt_list
);
2284 tree t
= (*ps
)->get_tree();
2285 if (t
== error_mark_node
)
2286 return this->error_statement();
2287 append_to_statement_list(t
, &stmt_list
);
2292 tree tv
= value
->get_tree();
2293 if (tv
== error_mark_node
)
2294 return this->error_statement();
2295 tree t
= build2_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2296 NULL_TREE
, tv
, stmt_list
);
2297 return this->make_statement(t
);
2300 // Pair of statements.
2303 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2305 tree stmt_list
= NULL_TREE
;
2306 tree t
= s1
->get_tree();
2307 if (t
== error_mark_node
)
2308 return this->error_statement();
2309 append_to_statement_list(t
, &stmt_list
);
2311 if (t
== error_mark_node
)
2312 return this->error_statement();
2313 append_to_statement_list(t
, &stmt_list
);
2315 // If neither statement has any side effects, stmt_list can be NULL
2317 if (stmt_list
== NULL_TREE
)
2318 stmt_list
= integer_zero_node
;
2320 return this->make_statement(stmt_list
);
2323 // List of statements.
2326 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2328 tree stmt_list
= NULL_TREE
;
2329 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2330 p
!= statements
.end();
2333 tree t
= (*p
)->get_tree();
2334 if (t
== error_mark_node
)
2335 return this->error_statement();
2336 append_to_statement_list(t
, &stmt_list
);
2338 return this->make_statement(stmt_list
);
2341 // Make a block. For some reason gcc uses a dual structure for
2342 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2343 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2347 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2348 const std::vector
<Bvariable
*>& vars
,
2349 Location start_location
,
2352 tree block_tree
= make_node(BLOCK
);
2353 if (enclosing
== NULL
)
2355 tree fndecl
= function
->get_tree();
2356 gcc_assert(fndecl
!= NULL_TREE
);
2358 // We may have already created a block for local variables when
2359 // we take the address of a parameter.
2360 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2362 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2363 DECL_INITIAL(fndecl
) = block_tree
;
2367 tree superblock_tree
= DECL_INITIAL(fndecl
);
2368 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2370 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2372 pp
= &BLOCK_CHAIN(*pp
))
2379 tree superbind_tree
= enclosing
->get_tree();
2380 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2381 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2383 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2385 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2387 pp
= &BLOCK_CHAIN(*pp
))
2392 tree
* pp
= &BLOCK_VARS(block_tree
);
2393 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2397 *pp
= (*pv
)->get_decl();
2398 if (*pp
!= error_mark_node
)
2399 pp
= &DECL_CHAIN(*pp
);
2403 TREE_USED(block_tree
) = 1;
2405 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2406 void_type_node
, BLOCK_VARS(block_tree
),
2407 NULL_TREE
, block_tree
);
2408 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2409 return new Bblock(bind_tree
);
2412 // Add statements to a block.
2415 Gcc_backend::block_add_statements(Bblock
* bblock
,
2416 const std::vector
<Bstatement
*>& statements
)
2418 tree stmt_list
= NULL_TREE
;
2419 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2420 p
!= statements
.end();
2423 tree s
= (*p
)->get_tree();
2424 if (s
!= error_mark_node
)
2425 append_to_statement_list(s
, &stmt_list
);
2428 tree bind_tree
= bblock
->get_tree();
2429 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2430 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2433 // Return a block as a statement.
2436 Gcc_backend::block_statement(Bblock
* bblock
)
2438 tree bind_tree
= bblock
->get_tree();
2439 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2440 return this->make_statement(bind_tree
);
2443 // This is not static because we declare it with GTY(()) in go-c.h.
2444 tree go_non_zero_struct
;
2446 // Return a type corresponding to TYPE with non-zero size.
2449 Gcc_backend::non_zero_size_type(tree type
)
2451 if (int_size_in_bytes(type
) != 0)
2454 switch (TREE_CODE(type
))
2457 if (TYPE_FIELDS(type
) != NULL_TREE
)
2459 tree ns
= make_node(RECORD_TYPE
);
2460 tree field_trees
= NULL_TREE
;
2461 tree
*pp
= &field_trees
;
2462 for (tree field
= TYPE_FIELDS(type
);
2464 field
= DECL_CHAIN(field
))
2466 tree ft
= TREE_TYPE(field
);
2467 if (field
== TYPE_FIELDS(type
))
2468 ft
= non_zero_size_type(ft
);
2469 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2470 DECL_NAME(field
), ft
);
2471 DECL_CONTEXT(f
) = ns
;
2473 pp
= &DECL_CHAIN(f
);
2475 TYPE_FIELDS(ns
) = field_trees
;
2480 if (go_non_zero_struct
== NULL_TREE
)
2482 type
= make_node(RECORD_TYPE
);
2483 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2484 get_identifier("dummy"),
2486 DECL_CONTEXT(field
) = type
;
2487 TYPE_FIELDS(type
) = field
;
2489 go_non_zero_struct
= type
;
2491 return go_non_zero_struct
;
2495 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2496 return build_array_type_nelts(element_type
, 1);
2506 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type
2507 // can be created multiple times and thus have multiple tree
2508 // representations. Make sure this does not confuse the middle-end.
2511 Gcc_backend::convert_tree(tree type_tree
, tree expr_tree
, Location location
)
2513 if (type_tree
== TREE_TYPE(expr_tree
))
2516 if (type_tree
== error_mark_node
2517 || expr_tree
== error_mark_node
2518 || TREE_TYPE(expr_tree
) == error_mark_node
)
2519 return error_mark_node
;
2521 gcc_assert(TREE_CODE(type_tree
) == TREE_CODE(TREE_TYPE(expr_tree
)));
2522 if (POINTER_TYPE_P(type_tree
)
2523 || INTEGRAL_TYPE_P(type_tree
)
2524 || SCALAR_FLOAT_TYPE_P(type_tree
)
2525 || COMPLEX_FLOAT_TYPE_P(type_tree
))
2526 return fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
2527 else if (TREE_CODE(type_tree
) == RECORD_TYPE
2528 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
2530 gcc_assert(int_size_in_bytes(type_tree
)
2531 == int_size_in_bytes(TREE_TYPE(expr_tree
)));
2532 if (TYPE_MAIN_VARIANT(type_tree
)
2533 == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
2534 return fold_build1_loc(location
.gcc_location(), NOP_EXPR
,
2535 type_tree
, expr_tree
);
2536 return fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
2537 type_tree
, expr_tree
);
2543 // Make a global variable.
2546 Gcc_backend::global_variable(const std::string
& var_name
,
2547 const std::string
& asm_name
,
2551 bool in_unique_section
,
2554 tree type_tree
= btype
->get_tree();
2555 if (type_tree
== error_mark_node
)
2556 return this->error_variable();
2558 // The GNU linker does not like dynamic variables with zero size.
2559 tree orig_type_tree
= type_tree
;
2560 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2561 type_tree
= this->non_zero_size_type(type_tree
);
2563 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2564 get_identifier_from_string(var_name
),
2567 DECL_EXTERNAL(decl
) = 1;
2569 TREE_STATIC(decl
) = 1;
2572 TREE_PUBLIC(decl
) = 1;
2573 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2577 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2580 TREE_USED(decl
) = 1;
2582 if (in_unique_section
)
2583 resolve_unique_section (decl
, 0, 1);
2585 go_preserve_from_gc(decl
);
2587 return new Bvariable(decl
, orig_type_tree
);
2590 // Set the initial value of a global variable.
2593 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2595 tree expr_tree
= expr
->get_tree();
2596 if (expr_tree
== error_mark_node
)
2598 gcc_assert(TREE_CONSTANT(expr_tree
));
2599 tree var_decl
= var
->get_decl();
2600 if (var_decl
== error_mark_node
)
2602 DECL_INITIAL(var_decl
) = expr_tree
;
2604 // If this variable goes in a unique section, it may need to go into
2605 // a different one now that DECL_INITIAL is set.
2606 if (symtab_node::get(var_decl
)
2607 && symtab_node::get(var_decl
)->implicit_section
)
2609 set_decl_section_name (var_decl
, NULL
);
2610 resolve_unique_section (var_decl
,
2611 compute_reloc_for_constant (expr_tree
),
2616 // Make a local variable.
2619 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2620 Btype
* btype
, Bvariable
* decl_var
,
2621 bool is_address_taken
, Location location
)
2623 tree type_tree
= btype
->get_tree();
2624 if (type_tree
== error_mark_node
)
2625 return this->error_variable();
2626 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2627 get_identifier_from_string(name
),
2629 DECL_CONTEXT(decl
) = function
->get_tree();
2630 TREE_USED(decl
) = 1;
2631 if (is_address_taken
)
2632 TREE_ADDRESSABLE(decl
) = 1;
2633 if (decl_var
!= NULL
)
2635 DECL_HAS_VALUE_EXPR_P(decl
) = 1;
2636 SET_DECL_VALUE_EXPR(decl
, decl_var
->get_decl());
2638 go_preserve_from_gc(decl
);
2639 return new Bvariable(decl
);
2642 // Make a function parameter variable.
2645 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2646 Btype
* btype
, bool is_address_taken
,
2649 tree type_tree
= btype
->get_tree();
2650 if (type_tree
== error_mark_node
)
2651 return this->error_variable();
2652 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2653 get_identifier_from_string(name
),
2655 DECL_CONTEXT(decl
) = function
->get_tree();
2656 DECL_ARG_TYPE(decl
) = type_tree
;
2657 TREE_USED(decl
) = 1;
2658 if (is_address_taken
)
2659 TREE_ADDRESSABLE(decl
) = 1;
2660 go_preserve_from_gc(decl
);
2661 return new Bvariable(decl
);
2664 // Make a static chain variable.
2667 Gcc_backend::static_chain_variable(Bfunction
* function
, const std::string
& name
,
2668 Btype
* btype
, Location location
)
2670 tree type_tree
= btype
->get_tree();
2671 if (type_tree
== error_mark_node
)
2672 return this->error_variable();
2673 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2674 get_identifier_from_string(name
), type_tree
);
2675 tree fndecl
= function
->get_tree();
2676 DECL_CONTEXT(decl
) = fndecl
;
2677 DECL_ARG_TYPE(decl
) = type_tree
;
2678 TREE_USED(decl
) = 1;
2679 DECL_ARTIFICIAL(decl
) = 1;
2680 DECL_IGNORED_P(decl
) = 1;
2681 TREE_READONLY(decl
) = 1;
2683 struct function
*f
= DECL_STRUCT_FUNCTION(fndecl
);
2686 push_struct_function(fndecl
);
2688 f
= DECL_STRUCT_FUNCTION(fndecl
);
2690 gcc_assert(f
->static_chain_decl
== NULL
);
2691 f
->static_chain_decl
= decl
;
2692 DECL_STATIC_CHAIN(fndecl
) = 1;
2694 go_preserve_from_gc(decl
);
2695 return new Bvariable(decl
);
2698 // Make a temporary variable.
2701 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2702 Btype
* btype
, Bexpression
* binit
,
2703 bool is_address_taken
,
2705 Bstatement
** pstatement
)
2707 gcc_assert(function
!= NULL
);
2708 tree decl
= function
->get_tree();
2709 tree type_tree
= btype
->get_tree();
2710 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2711 if (type_tree
== error_mark_node
2712 || init_tree
== error_mark_node
2713 || decl
== error_mark_node
)
2715 *pstatement
= this->error_statement();
2716 return this->error_variable();
2720 // We can only use create_tmp_var if the type is not addressable.
2721 if (!TREE_ADDRESSABLE(type_tree
))
2723 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2724 push_struct_function(decl
);
2726 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2728 var
= create_tmp_var(type_tree
, "GOTMP");
2733 gcc_assert(bblock
!= NULL
);
2734 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2735 create_tmp_var_name("GOTMP"),
2737 DECL_ARTIFICIAL(var
) = 1;
2738 DECL_IGNORED_P(var
) = 1;
2740 DECL_CONTEXT(var
) = decl
;
2742 // We have to add this variable to the BLOCK and the BIND_EXPR.
2743 tree bind_tree
= bblock
->get_tree();
2744 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2745 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2746 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2747 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2748 BLOCK_VARS(block_tree
) = var
;
2749 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2752 if (this->type_size(btype
) != 0
2753 && init_tree
!= NULL_TREE
2754 && TREE_TYPE(init_tree
) != void_type_node
)
2755 DECL_INITIAL(var
) = this->convert_tree(type_tree
, init_tree
, location
);
2757 if (is_address_taken
)
2758 TREE_ADDRESSABLE(var
) = 1;
2760 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2762 void_type_node
, var
));
2764 // For a zero sized type, don't initialize VAR with BINIT, but still
2765 // evaluate BINIT for its side effects.
2766 if (init_tree
!= NULL_TREE
2767 && (this->type_size(btype
) == 0
2768 || TREE_TYPE(init_tree
) == void_type_node
))
2770 this->compound_statement(this->expression_statement(function
, binit
),
2773 return new Bvariable(var
);
2776 // Create an implicit variable that is compiler-defined. This is used when
2777 // generating GC root variables and storing the values of a slice initializer.
2780 Gcc_backend::implicit_variable(const std::string
& name
,
2781 const std::string
& asm_name
,
2782 Btype
* type
, bool is_hidden
, bool is_constant
,
2783 bool is_common
, int64_t alignment
)
2785 tree type_tree
= type
->get_tree();
2786 if (type_tree
== error_mark_node
)
2787 return this->error_variable();
2789 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2790 get_identifier_from_string(name
), type_tree
);
2791 DECL_EXTERNAL(decl
) = 0;
2792 TREE_PUBLIC(decl
) = !is_hidden
;
2793 TREE_STATIC(decl
) = 1;
2794 TREE_USED(decl
) = 1;
2795 DECL_ARTIFICIAL(decl
) = 1;
2798 DECL_COMMON(decl
) = 1;
2800 // When the initializer for one implicit_variable refers to another,
2801 // it needs to know the visibility of the referenced struct so that
2802 // compute_reloc_for_constant will return the right value. On many
2803 // systems calling make_decl_one_only will mark the decl as weak,
2804 // which will change the return value of compute_reloc_for_constant.
2805 // We can't reliably call make_decl_one_only yet, because we don't
2806 // yet know the initializer. This issue doesn't arise in C because
2807 // Go initializers, unlike C initializers, can be indirectly
2808 // recursive. To ensure that compute_reloc_for_constant computes
2809 // the right value if some other initializer refers to this one, we
2810 // mark this symbol as weak here. We undo that below in
2811 // immutable_struct_set_init before calling mark_decl_one_only.
2812 DECL_WEAK(decl
) = 1;
2816 TREE_READONLY(decl
) = 1;
2817 TREE_CONSTANT(decl
) = 1;
2821 SET_DECL_ALIGN(decl
, alignment
* BITS_PER_UNIT
);
2822 DECL_USER_ALIGN(decl
) = 1;
2824 if (! asm_name
.empty())
2825 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2827 go_preserve_from_gc(decl
);
2828 return new Bvariable(decl
);
2831 // Set the initalizer for a variable created by implicit_variable.
2832 // This is where we finish compiling the variable.
2835 Gcc_backend::implicit_variable_set_init(Bvariable
* var
, const std::string
&,
2836 Btype
*, bool, bool, bool is_common
,
2839 tree decl
= var
->get_decl();
2842 init_tree
= NULL_TREE
;
2844 init_tree
= init
->get_tree();
2845 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2848 DECL_INITIAL(decl
) = init_tree
;
2850 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2851 // See the comment where DECL_WEAK is set in implicit_variable.
2854 DECL_WEAK(decl
) = 0;
2855 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2858 resolve_unique_section(decl
, 2, 1);
2860 rest_of_decl_compilation(decl
, 1, 0);
2863 // Return a reference to an implicit variable defined in another package.
2866 Gcc_backend::implicit_variable_reference(const std::string
& name
,
2867 const std::string
& asm_name
,
2870 tree type_tree
= btype
->get_tree();
2871 if (type_tree
== error_mark_node
)
2872 return this->error_variable();
2874 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2875 get_identifier_from_string(name
), type_tree
);
2876 DECL_EXTERNAL(decl
) = 1;
2877 TREE_PUBLIC(decl
) = 1;
2878 TREE_STATIC(decl
) = 0;
2879 DECL_ARTIFICIAL(decl
) = 1;
2880 if (! asm_name
.empty())
2881 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2882 go_preserve_from_gc(decl
);
2883 return new Bvariable(decl
);
2886 // Create a named immutable initialized data structure.
2889 Gcc_backend::immutable_struct(const std::string
& name
,
2890 const std::string
& asm_name
,
2892 bool is_common
, Btype
* btype
, Location location
)
2894 tree type_tree
= btype
->get_tree();
2895 if (type_tree
== error_mark_node
)
2896 return this->error_variable();
2897 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2898 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2899 get_identifier_from_string(name
),
2900 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2901 TREE_STATIC(decl
) = 1;
2902 TREE_USED(decl
) = 1;
2903 TREE_READONLY(decl
) = 1;
2904 TREE_CONSTANT(decl
) = 1;
2905 DECL_ARTIFICIAL(decl
) = 1;
2907 TREE_PUBLIC(decl
) = 1;
2908 if (! asm_name
.empty())
2909 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2911 // When the initializer for one immutable_struct refers to another,
2912 // it needs to know the visibility of the referenced struct so that
2913 // compute_reloc_for_constant will return the right value. On many
2914 // systems calling make_decl_one_only will mark the decl as weak,
2915 // which will change the return value of compute_reloc_for_constant.
2916 // We can't reliably call make_decl_one_only yet, because we don't
2917 // yet know the initializer. This issue doesn't arise in C because
2918 // Go initializers, unlike C initializers, can be indirectly
2919 // recursive. To ensure that compute_reloc_for_constant computes
2920 // the right value if some other initializer refers to this one, we
2921 // mark this symbol as weak here. We undo that below in
2922 // immutable_struct_set_init before calling mark_decl_one_only.
2924 DECL_WEAK(decl
) = 1;
2926 // We don't call rest_of_decl_compilation until we have the
2929 go_preserve_from_gc(decl
);
2930 return new Bvariable(decl
);
2933 // Set the initializer for a variable created by immutable_struct.
2934 // This is where we finish compiling the variable.
2937 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
2938 bool, bool is_common
, Btype
*, Location
,
2939 Bexpression
* initializer
)
2941 tree decl
= var
->get_decl();
2942 tree init_tree
= initializer
->get_tree();
2943 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2946 DECL_INITIAL(decl
) = init_tree
;
2948 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2949 // See the comment where DECL_WEAK is set in immutable_struct.
2952 DECL_WEAK(decl
) = 0;
2953 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2956 // These variables are often unneeded in the final program, so put
2957 // them in their own section so that linker GC can discard them.
2958 resolve_unique_section(decl
,
2959 compute_reloc_for_constant (init_tree
),
2962 rest_of_decl_compilation(decl
, 1, 0);
2965 // Return a reference to an immutable initialized data structure
2966 // defined in another package.
2969 Gcc_backend::immutable_struct_reference(const std::string
& name
,
2970 const std::string
& asm_name
,
2974 tree type_tree
= btype
->get_tree();
2975 if (type_tree
== error_mark_node
)
2976 return this->error_variable();
2977 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2978 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2979 get_identifier_from_string(name
),
2980 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2981 TREE_READONLY(decl
) = 1;
2982 TREE_CONSTANT(decl
) = 1;
2983 DECL_ARTIFICIAL(decl
) = 1;
2984 TREE_PUBLIC(decl
) = 1;
2985 DECL_EXTERNAL(decl
) = 1;
2986 if (! asm_name
.empty())
2987 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2988 go_preserve_from_gc(decl
);
2989 return new Bvariable(decl
);
2995 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
3001 tree func_tree
= function
->get_tree();
3002 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
3003 push_struct_function(func_tree
);
3005 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
3007 decl
= create_artificial_label(location
.gcc_location());
3013 tree id
= get_identifier_from_string(name
);
3014 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
3016 DECL_CONTEXT(decl
) = function
->get_tree();
3018 return new Blabel(decl
);
3021 // Make a statement which defines a label.
3024 Gcc_backend::label_definition_statement(Blabel
* label
)
3026 tree lab
= label
->get_tree();
3027 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
3028 void_type_node
, lab
);
3029 return this->make_statement(ret
);
3032 // Make a goto statement.
3035 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
3037 tree lab
= label
->get_tree();
3038 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
3040 return this->make_statement(ret
);
3043 // Get the address of a label.
3046 Gcc_backend::label_address(Blabel
* label
, Location location
)
3048 tree lab
= label
->get_tree();
3050 TREE_ADDRESSABLE(lab
) = 1;
3051 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
3052 build_fold_addr_expr_loc(location
.gcc_location(),
3054 return this->make_expression(ret
);
3057 // Declare or define a new function.
3060 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
3061 const std::string
& asm_name
, unsigned int flags
,
3064 tree functype
= fntype
->get_tree();
3065 if (functype
!= error_mark_node
)
3067 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
3068 functype
= TREE_TYPE(functype
);
3070 tree id
= get_identifier_from_string(name
);
3071 if (functype
== error_mark_node
|| id
== error_mark_node
)
3072 return this->error_function();
3074 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
3075 if (! asm_name
.empty())
3076 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3077 if ((flags
& function_is_visible
) != 0)
3078 TREE_PUBLIC(decl
) = 1;
3079 if ((flags
& function_is_declaration
) != 0)
3080 DECL_EXTERNAL(decl
) = 1;
3083 tree restype
= TREE_TYPE(functype
);
3085 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
3086 DECL_ARTIFICIAL(resdecl
) = 1;
3087 DECL_IGNORED_P(resdecl
) = 1;
3088 DECL_CONTEXT(resdecl
) = decl
;
3089 DECL_RESULT(decl
) = resdecl
;
3091 if ((flags
& function_is_inlinable
) == 0)
3092 DECL_UNINLINABLE(decl
) = 1;
3093 if ((flags
& function_no_split_stack
) != 0)
3095 tree attr
= get_identifier ("no_split_stack");
3096 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
3098 if ((flags
& function_does_not_return
) != 0)
3099 TREE_THIS_VOLATILE(decl
) = 1;
3100 if ((flags
& function_in_unique_section
) != 0)
3101 resolve_unique_section(decl
, 0, 1);
3102 if ((flags
& function_only_inline
) != 0)
3104 TREE_PUBLIC (decl
) = 1;
3105 DECL_EXTERNAL(decl
) = 1;
3106 DECL_DECLARED_INLINE_P(decl
) = 1;
3109 // Optimize thunk functions for size. A thunk created for a defer
3110 // statement that may call recover looks like:
3111 // if runtime.setdeferretaddr(L1) {
3116 // The idea is that L1 should be the address to which realfn
3117 // returns. This only works if this little function is not over
3118 // optimized. At some point GCC started duplicating the epilogue in
3119 // the basic-block reordering pass, breaking this assumption.
3120 // Optimizing the function for size avoids duplicating the epilogue.
3121 // This optimization shouldn't matter for any thunk since all thunks
3123 size_t pos
= name
.find("..thunk");
3124 if (pos
!= std::string::npos
)
3126 for (pos
+= 7; pos
< name
.length(); ++pos
)
3128 if (name
[pos
] < '0' || name
[pos
] > '9')
3131 if (pos
== name
.length())
3133 struct cl_optimization cur_opts
;
3134 cl_optimization_save(&cur_opts
, &global_options
);
3135 global_options
.x_optimize_size
= 1;
3136 global_options
.x_optimize_fast
= 0;
3137 global_options
.x_optimize_debug
= 0;
3138 DECL_FUNCTION_SPECIFIC_OPTIMIZATION(decl
) =
3139 build_optimization_node(&global_options
);
3140 cl_optimization_restore(&global_options
, &cur_opts
);
3144 go_preserve_from_gc(decl
);
3145 return new Bfunction(decl
);
3148 // Create a statement that runs all deferred calls for FUNCTION. This should
3149 // be a statement that looks like this in C++:
3151 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3154 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
3155 Bexpression
* defer
, Location location
)
3157 tree undefer_tree
= undefer
->get_tree();
3158 tree defer_tree
= defer
->get_tree();
3159 tree fntree
= function
->get_tree();
3161 if (undefer_tree
== error_mark_node
3162 || defer_tree
== error_mark_node
3163 || fntree
== error_mark_node
)
3164 return this->error_statement();
3166 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
3167 push_struct_function(fntree
);
3169 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
3171 tree stmt_list
= NULL
;
3172 Blabel
* blabel
= this->label(function
, "", location
);
3173 Bstatement
* label_def
= this->label_definition_statement(blabel
);
3174 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
3176 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
3177 tree jump
= jump_stmt
->get_tree();
3178 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
3179 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
3181 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
3182 append_to_statement_list(try_catch
, &stmt_list
);
3185 return this->make_statement(stmt_list
);
3188 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3189 // This will only be called for a function definition.
3192 Gcc_backend::function_set_parameters(Bfunction
* function
,
3193 const std::vector
<Bvariable
*>& param_vars
)
3195 tree func_tree
= function
->get_tree();
3196 if (func_tree
== error_mark_node
)
3199 tree params
= NULL_TREE
;
3201 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
3202 pv
!= param_vars
.end();
3205 *pp
= (*pv
)->get_decl();
3206 gcc_assert(*pp
!= error_mark_node
);
3207 pp
= &DECL_CHAIN(*pp
);
3210 DECL_ARGUMENTS(func_tree
) = params
;
3214 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3217 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
3219 tree func_tree
= function
->get_tree();
3220 tree code
= code_stmt
->get_tree();
3222 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
3224 DECL_SAVED_TREE(func_tree
) = code
;
3228 // Look up a named built-in function in the current backend implementation.
3229 // Returns NULL if no built-in function by that name exists.
3232 Gcc_backend::lookup_builtin(const std::string
& name
)
3234 if (this->builtin_functions_
.count(name
) != 0)
3235 return this->builtin_functions_
[name
];
3239 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3240 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3241 // emit early debugging information.
3244 Gcc_backend::write_global_definitions(
3245 const std::vector
<Btype
*>& type_decls
,
3246 const std::vector
<Bexpression
*>& constant_decls
,
3247 const std::vector
<Bfunction
*>& function_decls
,
3248 const std::vector
<Bvariable
*>& variable_decls
)
3250 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
3251 + function_decls
.size() + variable_decls
.size();
3253 tree
* defs
= new tree
[count_definitions
];
3255 // Convert all non-erroneous declarations into Gimple form.
3257 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
3258 p
!= variable_decls
.end();
3261 tree v
= (*p
)->get_decl();
3262 if (v
!= error_mark_node
)
3265 go_preserve_from_gc(defs
[i
]);
3270 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
3271 p
!= type_decls
.end();
3274 tree type_tree
= (*p
)->get_tree();
3275 if (type_tree
!= error_mark_node
3276 && IS_TYPE_OR_DECL_P(type_tree
))
3278 defs
[i
] = TYPE_NAME(type_tree
);
3279 gcc_assert(defs
[i
] != NULL
);
3280 go_preserve_from_gc(defs
[i
]);
3284 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
3285 p
!= constant_decls
.end();
3288 if ((*p
)->get_tree() != error_mark_node
)
3290 defs
[i
] = (*p
)->get_tree();
3291 go_preserve_from_gc(defs
[i
]);
3295 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
3296 p
!= function_decls
.end();
3299 tree decl
= (*p
)->get_tree();
3300 if (decl
!= error_mark_node
)
3302 go_preserve_from_gc(decl
);
3303 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
3304 allocate_struct_function(decl
, false);
3305 cgraph_node::finalize_function(decl
, true);
3312 // Pass everything back to the middle-end.
3314 wrapup_global_declarations(defs
, i
);
3320 Gcc_backend::write_export_data(const char* bytes
, unsigned int size
)
3322 go_write_export_data(bytes
, size
);
3326 // Define a builtin function. BCODE is the builtin function code
3327 // defined by builtins.def. NAME is the name of the builtin function.
3328 // LIBNAME is the name of the corresponding library function, and is
3329 // NULL if there isn't one. FNTYPE is the type of the function.
3330 // CONST_P is true if the function has the const attribute.
3331 // NORETURN_P is true if the function has the noreturn attribute.
3334 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
3335 const char* libname
, tree fntype
, bool const_p
,
3338 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
3339 libname
, NULL_TREE
);
3341 TREE_READONLY(decl
) = 1;
3343 TREE_THIS_VOLATILE(decl
) = 1;
3344 set_builtin_decl(bcode
, decl
, true);
3345 this->builtin_functions_
[name
] = this->make_function(decl
);
3346 if (libname
!= NULL
)
3348 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
3351 TREE_READONLY(decl
) = 1;
3353 TREE_THIS_VOLATILE(decl
) = 1;
3354 this->builtin_functions_
[libname
] = this->make_function(decl
);
3358 // Return the backend generator.
3363 return new Gcc_backend();