1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2018 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
5 // This file is part of GCC.
7 // GCC is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3, or (at your option) any later
12 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 #include "go-system.h"
23 // This has to be included outside of extern "C", so we have to
24 // include it here before tree.h includes it later.
28 #include "fold-const.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
32 #include "tree-iterator.h"
37 #include "gimple-expr.h"
39 #include "langhooks.h"
51 // A class wrapping a tree.
72 // In gcc, types, expressions, and statements are all trees.
73 class Btype
: public Gcc_tree
81 class Bexpression
: public Gcc_tree
89 class Bstatement
: public Gcc_tree
97 class Bfunction
: public Gcc_tree
105 class Bblock
: public Gcc_tree
113 class Blabel
: public Gcc_tree
121 // Bvariable is a bit more complicated, because of zero-sized types.
122 // The GNU linker does not permit dynamic variables with zero size.
123 // When we see such a variable, we generate a version of the type with
124 // non-zero size. However, when referring to the global variable, we
125 // want an expression of zero size; otherwise, if, say, the global
126 // variable is passed to a function, we will be passing a
127 // non-zero-sized value to a zero-sized value, which can lead to a
134 : t_(t
), orig_type_(NULL
)
137 Bvariable(tree t
, tree orig_type
)
138 : t_(t
), orig_type_(orig_type
)
141 // Get the tree for use as an expression.
143 get_tree(Location
) const;
145 // Get the actual decl;
155 // Get the tree of a variable for use as an expression. If this is a
156 // zero-sized global, create an expression that refers to the decl but
159 Bvariable::get_tree(Location location
) const
161 if (this->orig_type_
== NULL
162 || this->t_
== error_mark_node
163 || TREE_TYPE(this->t_
) == this->orig_type_
)
165 // Return *(orig_type*)&decl. */
166 tree t
= build_fold_addr_expr_loc(location
.gcc_location(), this->t_
);
167 t
= fold_build1_loc(location
.gcc_location(), NOP_EXPR
,
168 build_pointer_type(this->orig_type_
), t
);
169 return build_fold_indirect_ref_loc(location
.gcc_location(), t
);
172 // This file implements the interface between the Go frontend proper
173 // and the gcc IR. This implements specific instantiations of
174 // abstract classes defined by the Go frontend proper. The Go
175 // frontend proper class methods of these classes to generate the
176 // backend representation.
178 class Gcc_backend
: public Backend
187 { return this->make_type(error_mark_node
); }
191 { return this->make_type(void_type_node
); }
195 { return this->make_type(boolean_type_node
); }
198 integer_type(bool, int);
207 pointer_type(Btype
*);
210 function_type(const Btyped_identifier
&,
211 const std::vector
<Btyped_identifier
>&,
212 const std::vector
<Btyped_identifier
>&,
217 struct_type(const std::vector
<Btyped_identifier
>&);
220 array_type(Btype
*, Bexpression
*);
223 placeholder_pointer_type(const std::string
&, Location
, bool);
226 set_placeholder_pointer_type(Btype
*, Btype
*);
229 set_placeholder_function_type(Btype
*, Btype
*);
232 placeholder_struct_type(const std::string
&, Location
);
235 set_placeholder_struct_type(Btype
* placeholder
,
236 const std::vector
<Btyped_identifier
>&);
239 placeholder_array_type(const std::string
&, Location
);
242 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
245 named_type(const std::string
&, Btype
*, Location
);
248 circular_pointer_type(Btype
*, bool);
251 is_circular_pointer_type(Btype
*);
257 type_alignment(Btype
*);
260 type_field_alignment(Btype
*);
263 type_field_offset(Btype
*, size_t index
);
268 zero_expression(Btype
*);
272 { return this->make_expression(error_mark_node
); }
275 nil_pointer_expression()
276 { return this->make_expression(null_pointer_node
); }
279 var_expression(Bvariable
* var
, Location
);
282 indirect_expression(Btype
*, Bexpression
* expr
, bool known_valid
, Location
);
285 named_constant_expression(Btype
* btype
, const std::string
& name
,
286 Bexpression
* val
, Location
);
289 integer_constant_expression(Btype
* btype
, mpz_t val
);
292 float_constant_expression(Btype
* btype
, mpfr_t val
);
295 complex_constant_expression(Btype
* btype
, mpc_t val
);
298 string_constant_expression(const std::string
& val
);
301 boolean_constant_expression(bool val
);
304 real_part_expression(Bexpression
* bcomplex
, Location
);
307 imag_part_expression(Bexpression
* bcomplex
, Location
);
310 complex_expression(Bexpression
* breal
, Bexpression
* bimag
, Location
);
313 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
316 function_code_expression(Bfunction
*, Location
);
319 address_expression(Bexpression
*, Location
);
322 struct_field_expression(Bexpression
*, size_t, Location
);
325 compound_expression(Bstatement
*, Bexpression
*, Location
);
328 conditional_expression(Bfunction
*, Btype
*, Bexpression
*, Bexpression
*,
329 Bexpression
*, Location
);
332 unary_expression(Operator
, Bexpression
*, Location
);
335 binary_expression(Operator
, Bexpression
*, Bexpression
*, Location
);
338 constructor_expression(Btype
*, const std::vector
<Bexpression
*>&, Location
);
341 array_constructor_expression(Btype
*, const std::vector
<unsigned long>&,
342 const std::vector
<Bexpression
*>&, Location
);
345 pointer_offset_expression(Bexpression
* base
, Bexpression
* offset
, Location
);
348 array_index_expression(Bexpression
* array
, Bexpression
* index
, Location
);
351 call_expression(Bfunction
* caller
, Bexpression
* fn
,
352 const std::vector
<Bexpression
*>& args
,
353 Bexpression
* static_chain
, Location
);
356 stack_allocation_expression(int64_t size
, Location
);
362 { return this->make_statement(error_mark_node
); }
365 expression_statement(Bfunction
*, Bexpression
*);
368 init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
);
371 assignment_statement(Bfunction
*, Bexpression
* lhs
, Bexpression
* rhs
,
375 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
379 if_statement(Bfunction
*, Bexpression
* condition
, Bblock
* then_block
,
380 Bblock
* else_block
, Location
);
383 switch_statement(Bfunction
* function
, Bexpression
* value
,
384 const std::vector
<std::vector
<Bexpression
*> >& cases
,
385 const std::vector
<Bstatement
*>& statements
,
389 compound_statement(Bstatement
*, Bstatement
*);
392 statement_list(const std::vector
<Bstatement
*>&);
395 exception_handler_statement(Bstatement
* bstat
, Bstatement
* except_stmt
,
396 Bstatement
* finally_stmt
, Location
);
401 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
405 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
408 block_statement(Bblock
*);
414 { return new Bvariable(error_mark_node
); }
417 global_variable(const std::string
& var_name
,
418 const std::string
& asm_name
,
422 bool in_unique_section
,
426 global_variable_set_init(Bvariable
*, Bexpression
*);
429 local_variable(Bfunction
*, const std::string
&, Btype
*, Bvariable
*, bool,
433 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
437 static_chain_variable(Bfunction
*, const std::string
&, Btype
*, Location
);
440 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
441 Location
, Bstatement
**);
444 implicit_variable(const std::string
&, const std::string
&, Btype
*,
445 bool, bool, bool, int64_t);
448 implicit_variable_set_init(Bvariable
*, const std::string
&, Btype
*,
449 bool, bool, bool, Bexpression
*);
452 implicit_variable_reference(const std::string
&, const std::string
&, Btype
*);
455 immutable_struct(const std::string
&, const std::string
&,
456 bool, bool, Btype
*, Location
);
459 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
460 Location
, Bexpression
*);
463 immutable_struct_reference(const std::string
&, const std::string
&,
469 label(Bfunction
*, const std::string
& name
, Location
);
472 label_definition_statement(Blabel
*);
475 goto_statement(Blabel
*, Location
);
478 label_address(Blabel
*, Location
);
484 { return this->make_function(error_mark_node
); }
487 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
488 bool is_visible
, bool is_declaration
, bool is_inlinable
,
489 bool disable_split_stack
, bool does_not_return
,
490 bool in_unique_section
, Location
);
493 function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
494 Bexpression
* defer
, Location
);
497 function_set_parameters(Bfunction
* function
, const std::vector
<Bvariable
*>&);
500 function_set_body(Bfunction
* function
, Bstatement
* code_stmt
);
503 lookup_builtin(const std::string
&);
506 write_global_definitions(const std::vector
<Btype
*>&,
507 const std::vector
<Bexpression
*>&,
508 const std::vector
<Bfunction
*>&,
509 const std::vector
<Bvariable
*>&);
512 write_export_data(const char* bytes
, unsigned int size
);
516 // Make a Bexpression from a tree.
518 make_expression(tree t
)
519 { return new Bexpression(t
); }
521 // Make a Bstatement from a tree.
523 make_statement(tree t
)
524 { return new Bstatement(t
); }
526 // Make a Btype from a tree.
529 { return new Btype(t
); }
532 make_function(tree t
)
533 { return new Bfunction(t
); }
536 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
539 fill_in_array(Btype
*, Btype
*, Bexpression
*);
542 non_zero_size_type(tree
);
545 convert_tree(tree
, tree
, Location
);
549 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
550 tree fntype
, bool const_p
, bool noreturn_p
);
552 // A mapping of the GCC built-ins exposed to GCCGo.
553 std::map
<std::string
, Bfunction
*> builtin_functions_
;
556 // A helper function to create a GCC identifier from a C++ string.
559 get_identifier_from_string(const std::string
& str
)
561 return get_identifier_with_length(str
.data(), str
.length());
564 // Define the built-in functions that are exposed to GCCGo.
566 Gcc_backend::Gcc_backend()
568 /* We need to define the fetch_and_add functions, since we use them
570 tree t
= this->integer_type(true, BITS_PER_UNIT
)->get_tree();
571 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
572 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
573 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
576 t
= this->integer_type(true, BITS_PER_UNIT
* 2)->get_tree();
577 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
578 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
579 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
582 t
= this->integer_type(true, BITS_PER_UNIT
* 4)->get_tree();
583 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
584 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
585 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
588 t
= this->integer_type(true, BITS_PER_UNIT
* 8)->get_tree();
589 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
590 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8",
591 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
594 // We use __builtin_expect for magic import functions.
595 this->define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
596 build_function_type_list(long_integer_type_node
,
597 long_integer_type_node
,
598 long_integer_type_node
,
602 // We use __builtin_memcmp for struct comparisons.
603 this->define_builtin(BUILT_IN_MEMCMP
, "__builtin_memcmp", "memcmp",
604 build_function_type_list(integer_type_node
,
611 // Used by runtime/internal/sys.
612 this->define_builtin(BUILT_IN_CTZ
, "__builtin_ctz", "ctz",
613 build_function_type_list(integer_type_node
,
617 this->define_builtin(BUILT_IN_CTZLL
, "__builtin_ctzll", "ctzll",
618 build_function_type_list(integer_type_node
,
619 long_long_unsigned_type_node
,
622 this->define_builtin(BUILT_IN_BSWAP32
, "__builtin_bswap32", "bswap32",
623 build_function_type_list(uint32_type_node
,
627 this->define_builtin(BUILT_IN_BSWAP64
, "__builtin_bswap64", "bswap64",
628 build_function_type_list(uint64_type_node
,
633 // We provide some functions for the math library.
634 tree math_function_type
= build_function_type_list(double_type_node
,
637 tree math_function_type_long
=
638 build_function_type_list(long_double_type_node
, long_double_type_node
,
640 tree math_function_type_two
= build_function_type_list(double_type_node
,
644 tree math_function_type_long_two
=
645 build_function_type_list(long_double_type_node
, long_double_type_node
,
646 long_double_type_node
, NULL_TREE
);
647 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
648 math_function_type
, true, false);
649 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
650 math_function_type_long
, true, false);
651 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
652 math_function_type
, true, false);
653 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
654 math_function_type_long
, true, false);
655 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
656 math_function_type
, true, false);
657 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
658 math_function_type_long
, true, false);
659 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
660 math_function_type_two
, true, false);
661 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
662 math_function_type_long_two
, true, false);
663 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
664 math_function_type
, true, false);
665 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
666 math_function_type_long
, true, false);
667 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
668 math_function_type
, true, false);
669 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
670 math_function_type_long
, true, false);
671 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
672 math_function_type
, true, false);
673 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
674 math_function_type_long
, true, false);
675 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
676 math_function_type
, true, false);
677 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
678 math_function_type_long
, true, false);
679 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
680 math_function_type
, true, false);
681 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
682 math_function_type_long
, true, false);
683 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
684 math_function_type
, true, false);
685 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
686 math_function_type_long
, true, false);
687 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
688 math_function_type_two
, true, false);
689 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
690 math_function_type_long_two
, true, false);
691 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
692 build_function_type_list(double_type_node
,
697 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
698 build_function_type_list(long_double_type_node
,
699 long_double_type_node
,
703 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
704 math_function_type
, true, false);
705 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
706 math_function_type_long
, true, false);
707 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
708 math_function_type
, true, false);
709 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
710 math_function_type_long
, true, false);
711 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
712 math_function_type
, true, false);
713 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
714 math_function_type_long
, true, false);
715 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
716 math_function_type
, true, false);
717 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
718 math_function_type_long
, true, false);
719 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
720 math_function_type
, true, false);
721 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
722 math_function_type_long
, true, false);
723 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
724 math_function_type
, true, false);
725 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
726 math_function_type_long
, true, false);
727 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
728 math_function_type
, true, false);
729 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
730 math_function_type_long
, true, false);
731 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
732 math_function_type
, true, false);
733 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
734 math_function_type_long
, true, false);
736 // We use __builtin_return_address in the thunk we build for
737 // functions which call recover, and for runtime.getcallerpc.
738 t
= build_function_type_list(ptr_type_node
, unsigned_type_node
, NULL_TREE
);
739 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
740 NULL
, t
, false, false);
742 // The runtime calls __builtin_frame_address for runtime.getcallersp.
743 this->define_builtin(BUILT_IN_FRAME_ADDRESS
, "__builtin_frame_address",
744 NULL
, t
, false, false);
746 // The runtime calls __builtin_extract_return_addr when recording
747 // the address to which a function returns.
748 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR
,
749 "__builtin_extract_return_addr", NULL
,
750 build_function_type_list(ptr_type_node
,
755 // The compiler uses __builtin_trap for some exception handling
757 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
758 build_function_type(void_type_node
, void_list_node
),
761 // The runtime uses __builtin_prefetch.
762 this->define_builtin(BUILT_IN_PREFETCH
, "__builtin_prefetch", NULL
,
763 build_varargs_function_type_list(void_type_node
,
768 // The compiler uses __builtin_unreachable for cases that can not
770 this->define_builtin(BUILT_IN_UNREACHABLE
, "__builtin_unreachable", NULL
,
771 build_function_type(void_type_node
, void_list_node
),
775 // Get an unnamed integer type.
778 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
783 if (bits
== INT_TYPE_SIZE
)
784 type
= unsigned_type_node
;
785 else if (bits
== CHAR_TYPE_SIZE
)
786 type
= unsigned_char_type_node
;
787 else if (bits
== SHORT_TYPE_SIZE
)
788 type
= short_unsigned_type_node
;
789 else if (bits
== LONG_TYPE_SIZE
)
790 type
= long_unsigned_type_node
;
791 else if (bits
== LONG_LONG_TYPE_SIZE
)
792 type
= long_long_unsigned_type_node
;
794 type
= make_unsigned_type(bits
);
798 if (bits
== INT_TYPE_SIZE
)
799 type
= integer_type_node
;
800 else if (bits
== CHAR_TYPE_SIZE
)
801 type
= signed_char_type_node
;
802 else if (bits
== SHORT_TYPE_SIZE
)
803 type
= short_integer_type_node
;
804 else if (bits
== LONG_TYPE_SIZE
)
805 type
= long_integer_type_node
;
806 else if (bits
== LONG_LONG_TYPE_SIZE
)
807 type
= long_long_integer_type_node
;
809 type
= make_signed_type(bits
);
811 return this->make_type(type
);
814 // Get an unnamed float type.
817 Gcc_backend::float_type(int bits
)
820 if (bits
== FLOAT_TYPE_SIZE
)
821 type
= float_type_node
;
822 else if (bits
== DOUBLE_TYPE_SIZE
)
823 type
= double_type_node
;
824 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
825 type
= long_double_type_node
;
828 type
= make_node(REAL_TYPE
);
829 TYPE_PRECISION(type
) = bits
;
832 return this->make_type(type
);
835 // Get an unnamed complex type.
838 Gcc_backend::complex_type(int bits
)
841 if (bits
== FLOAT_TYPE_SIZE
* 2)
842 type
= complex_float_type_node
;
843 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
844 type
= complex_double_type_node
;
845 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
846 type
= complex_long_double_type_node
;
849 type
= make_node(REAL_TYPE
);
850 TYPE_PRECISION(type
) = bits
/ 2;
852 type
= build_complex_type(type
);
854 return this->make_type(type
);
857 // Get a pointer type.
860 Gcc_backend::pointer_type(Btype
* to_type
)
862 tree to_type_tree
= to_type
->get_tree();
863 if (to_type_tree
== error_mark_node
)
864 return this->error_type();
865 tree type
= build_pointer_type(to_type_tree
);
866 return this->make_type(type
);
869 // Make a function type.
872 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
873 const std::vector
<Btyped_identifier
>& parameters
,
874 const std::vector
<Btyped_identifier
>& results
,
875 Btype
* result_struct
,
878 tree args
= NULL_TREE
;
880 if (receiver
.btype
!= NULL
)
882 tree t
= receiver
.btype
->get_tree();
883 if (t
== error_mark_node
)
884 return this->error_type();
885 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
886 pp
= &TREE_CHAIN(*pp
);
889 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
890 p
!= parameters
.end();
893 tree t
= p
->btype
->get_tree();
894 if (t
== error_mark_node
)
895 return this->error_type();
896 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
897 pp
= &TREE_CHAIN(*pp
);
900 // Varargs is handled entirely at the Go level. When converted to
901 // GENERIC functions are not varargs.
902 *pp
= void_list_node
;
906 result
= void_type_node
;
907 else if (results
.size() == 1)
908 result
= results
.front().btype
->get_tree();
911 gcc_assert(result_struct
!= NULL
);
912 result
= result_struct
->get_tree();
914 if (result
== error_mark_node
)
915 return this->error_type();
917 // The libffi library can not represent a zero-sized object. To
918 // avoid causing confusion on 32-bit SPARC, we treat a function that
919 // returns a zero-sized value as returning void. That should do no
920 // harm since there is no actual value to be returned. See
921 // https://gcc.gnu.org/PR72814 for details.
922 if (result
!= void_type_node
&& int_size_in_bytes(result
) == 0)
923 result
= void_type_node
;
925 tree fntype
= build_function_type(result
, args
);
926 if (fntype
== error_mark_node
)
927 return this->error_type();
929 return this->make_type(build_pointer_type(fntype
));
932 // Make a struct type.
935 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
937 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
940 // Fill in the fields of a struct type.
943 Gcc_backend::fill_in_struct(Btype
* fill
,
944 const std::vector
<Btyped_identifier
>& fields
)
946 tree fill_tree
= fill
->get_tree();
947 tree field_trees
= NULL_TREE
;
948 tree
* pp
= &field_trees
;
949 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
953 tree name_tree
= get_identifier_from_string(p
->name
);
954 tree type_tree
= p
->btype
->get_tree();
955 if (type_tree
== error_mark_node
)
956 return this->error_type();
957 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
959 DECL_CONTEXT(field
) = fill_tree
;
961 pp
= &DECL_CHAIN(field
);
963 TYPE_FIELDS(fill_tree
) = field_trees
;
964 layout_type(fill_tree
);
966 // Because Go permits converting between named struct types and
967 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
968 // because we don't try to maintain TYPE_CANONICAL for struct types,
969 // we need to tell the middle-end to use structural equality.
970 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
975 // Make an array type.
978 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
980 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
981 element_btype
, length
);
984 // Fill in an array type.
987 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
990 tree element_type_tree
= element_type
->get_tree();
991 tree length_tree
= length
->get_tree();
992 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
993 return this->error_type();
995 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
997 length_tree
= fold_convert(sizetype
, length_tree
);
999 // build_index_type takes the maximum index, which is one less than
1001 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
1005 tree fill_tree
= fill
->get_tree();
1006 TREE_TYPE(fill_tree
) = element_type_tree
;
1007 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
1008 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
1009 layout_type(fill_tree
);
1011 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
1012 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1013 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
1014 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
1015 TYPE_CANONICAL(fill_tree
) =
1016 build_array_type(TYPE_CANONICAL(element_type_tree
),
1017 TYPE_CANONICAL(index_type_tree
));
1022 // Create a placeholder for a pointer type.
1025 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
1026 Location location
, bool)
1028 tree ret
= build_distinct_type_copy(ptr_type_node
);
1031 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1032 get_identifier_from_string(name
),
1034 TYPE_NAME(ret
) = decl
;
1036 return this->make_type(ret
);
1039 // Set the real target type for a placeholder pointer type.
1042 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
1045 tree pt
= placeholder
->get_tree();
1046 if (pt
== error_mark_node
)
1048 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
1049 tree tt
= to_type
->get_tree();
1050 if (tt
== error_mark_node
)
1052 placeholder
->set_tree(error_mark_node
);
1055 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
1056 TREE_TYPE(pt
) = TREE_TYPE(tt
);
1057 if (TYPE_NAME(pt
) != NULL_TREE
)
1059 // Build the data structure gcc wants to see for a typedef.
1060 tree copy
= build_variant_type_copy(pt
);
1061 TYPE_NAME(copy
) = NULL_TREE
;
1062 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
1067 // Set the real values for a placeholder function type.
1070 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
1072 return this->set_placeholder_pointer_type(placeholder
, ft
);
1075 // Create a placeholder for a struct type.
1078 Gcc_backend::placeholder_struct_type(const std::string
& name
,
1081 tree ret
= make_node(RECORD_TYPE
);
1084 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1085 get_identifier_from_string(name
),
1087 TYPE_NAME(ret
) = decl
;
1089 return this->make_type(ret
);
1092 // Fill in the fields of a placeholder struct type.
1095 Gcc_backend::set_placeholder_struct_type(
1097 const std::vector
<Btyped_identifier
>& fields
)
1099 tree t
= placeholder
->get_tree();
1100 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
1101 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
1103 if (TYPE_NAME(t
) != NULL_TREE
)
1105 // Build the data structure gcc wants to see for a typedef.
1106 tree copy
= build_distinct_type_copy(t
);
1107 TYPE_NAME(copy
) = NULL_TREE
;
1108 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1111 return r
->get_tree() != error_mark_node
;
1114 // Create a placeholder for an array type.
1117 Gcc_backend::placeholder_array_type(const std::string
& name
,
1120 tree ret
= make_node(ARRAY_TYPE
);
1121 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1122 get_identifier_from_string(name
),
1124 TYPE_NAME(ret
) = decl
;
1125 return this->make_type(ret
);
1128 // Fill in the fields of a placeholder array type.
1131 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1132 Btype
* element_btype
,
1133 Bexpression
* length
)
1135 tree t
= placeholder
->get_tree();
1136 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1137 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1139 // Build the data structure gcc wants to see for a typedef.
1140 tree copy
= build_distinct_type_copy(t
);
1141 TYPE_NAME(copy
) = NULL_TREE
;
1142 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1144 return r
->get_tree() != error_mark_node
;
1147 // Return a named version of a type.
1150 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1153 tree type
= btype
->get_tree();
1154 if (type
== error_mark_node
)
1155 return this->error_type();
1157 // The middle-end expects a basic type to have a name. In Go every
1158 // basic type will have a name. The first time we see a basic type,
1159 // give it whatever Go name we have at this point.
1160 if (TYPE_NAME(type
) == NULL_TREE
1161 && location
.gcc_location() == BUILTINS_LOCATION
1162 && (TREE_CODE(type
) == INTEGER_TYPE
1163 || TREE_CODE(type
) == REAL_TYPE
1164 || TREE_CODE(type
) == COMPLEX_TYPE
1165 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1167 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1168 get_identifier_from_string(name
),
1170 TYPE_NAME(type
) = decl
;
1171 return this->make_type(type
);
1174 tree copy
= build_variant_type_copy(type
);
1175 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1176 get_identifier_from_string(name
),
1178 DECL_ORIGINAL_TYPE(decl
) = type
;
1179 TYPE_NAME(copy
) = decl
;
1180 return this->make_type(copy
);
1183 // Return a pointer type used as a marker for a circular type.
1186 Gcc_backend::circular_pointer_type(Btype
*, bool)
1188 return this->make_type(ptr_type_node
);
1191 // Return whether we might be looking at a circular type.
1194 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1196 return btype
->get_tree() == ptr_type_node
;
1199 // Return the size of a type.
1202 Gcc_backend::type_size(Btype
* btype
)
1204 tree t
= btype
->get_tree();
1205 if (t
== error_mark_node
)
1207 if (t
== void_type_node
)
1209 t
= TYPE_SIZE_UNIT(t
);
1210 gcc_assert(tree_fits_uhwi_p (t
));
1211 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1212 int64_t ret
= static_cast<int64_t>(val_wide
);
1213 if (ret
< 0 || static_cast<unsigned HOST_WIDE_INT
>(ret
) != val_wide
)
1218 // Return the alignment of a type.
1221 Gcc_backend::type_alignment(Btype
* btype
)
1223 tree t
= btype
->get_tree();
1224 if (t
== error_mark_node
)
1226 return TYPE_ALIGN_UNIT(t
);
1229 // Return the alignment of a struct field of type BTYPE.
1232 Gcc_backend::type_field_alignment(Btype
* btype
)
1234 tree t
= btype
->get_tree();
1235 if (t
== error_mark_node
)
1237 return go_field_alignment(t
);
1240 // Return the offset of a field in a struct.
1243 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1245 tree struct_tree
= btype
->get_tree();
1246 if (struct_tree
== error_mark_node
)
1248 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1249 tree field
= TYPE_FIELDS(struct_tree
);
1250 for (; index
> 0; --index
)
1252 field
= DECL_CHAIN(field
);
1253 gcc_assert(field
!= NULL_TREE
);
1255 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1256 int64_t ret
= static_cast<int64_t>(offset_wide
);
1257 gcc_assert(ret
== offset_wide
);
1261 // Return the zero value for a type.
1264 Gcc_backend::zero_expression(Btype
* btype
)
1266 tree t
= btype
->get_tree();
1268 if (t
== error_mark_node
)
1269 ret
= error_mark_node
;
1271 ret
= build_zero_cst(t
);
1272 return this->make_expression(ret
);
1275 // An expression that references a variable.
1278 Gcc_backend::var_expression(Bvariable
* var
, Location location
)
1280 tree ret
= var
->get_tree(location
);
1281 if (ret
== error_mark_node
)
1282 return this->error_expression();
1283 return this->make_expression(ret
);
1286 // An expression that indirectly references an expression.
1289 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1290 bool known_valid
, Location location
)
1292 tree expr_tree
= expr
->get_tree();
1293 tree type_tree
= btype
->get_tree();
1294 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1295 return this->error_expression();
1297 // If the type of EXPR is a recursive pointer type, then we
1298 // need to insert a cast before indirecting.
1299 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1300 if (VOID_TYPE_P(target_type_tree
))
1301 expr_tree
= fold_convert_loc(location
.gcc_location(),
1302 build_pointer_type(type_tree
), expr_tree
);
1304 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1307 TREE_THIS_NOTRAP(ret
) = 1;
1308 return this->make_expression(ret
);
1311 // Return an expression that declares a constant named NAME with the
1312 // constant value VAL in BTYPE.
1315 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1316 Bexpression
* val
, Location location
)
1318 tree type_tree
= btype
->get_tree();
1319 tree const_val
= val
->get_tree();
1320 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1321 return this->error_expression();
1323 tree name_tree
= get_identifier_from_string(name
);
1324 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1326 DECL_INITIAL(decl
) = const_val
;
1327 TREE_CONSTANT(decl
) = 1;
1328 TREE_READONLY(decl
) = 1;
1330 go_preserve_from_gc(decl
);
1331 return this->make_expression(decl
);
1334 // Return a typed value as a constant integer.
1337 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1339 tree t
= btype
->get_tree();
1340 if (t
== error_mark_node
)
1341 return this->error_expression();
1343 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1344 return this->make_expression(ret
);
1347 // Return a typed value as a constant floating-point number.
1350 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1352 tree t
= btype
->get_tree();
1354 if (t
== error_mark_node
)
1355 return this->error_expression();
1358 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1360 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1361 ret
= build_real(t
, r2
);
1362 return this->make_expression(ret
);
1365 // Return a typed real and imaginary value as a constant complex number.
1368 Gcc_backend::complex_constant_expression(Btype
* btype
, mpc_t val
)
1370 tree t
= btype
->get_tree();
1372 if (t
== error_mark_node
)
1373 return this->error_expression();
1376 real_from_mpfr(&r1
, mpc_realref(val
), TREE_TYPE(t
), GMP_RNDN
);
1378 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1381 real_from_mpfr(&r3
, mpc_imagref(val
), TREE_TYPE(t
), GMP_RNDN
);
1383 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1385 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1386 build_real(TREE_TYPE(t
), r4
));
1387 return this->make_expression(ret
);
1390 // Make a constant string expression.
1393 Gcc_backend::string_constant_expression(const std::string
& val
)
1395 tree index_type
= build_index_type(size_int(val
.length()));
1396 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1398 tree string_type
= build_array_type(const_char_type
, index_type
);
1399 TYPE_STRING_FLAG(string_type
) = 1;
1400 tree string_val
= build_string(val
.length(), val
.data());
1401 TREE_TYPE(string_val
) = string_type
;
1403 return this->make_expression(string_val
);
1406 // Make a constant boolean expression.
1409 Gcc_backend::boolean_constant_expression(bool val
)
1411 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1412 return this->make_expression(bool_cst
);
1415 // Return the real part of a complex expression.
1418 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1420 tree complex_tree
= bcomplex
->get_tree();
1421 if (complex_tree
== error_mark_node
)
1422 return this->error_expression();
1423 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1424 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1425 TREE_TYPE(TREE_TYPE(complex_tree
)),
1427 return this->make_expression(ret
);
1430 // Return the imaginary part of a complex expression.
1433 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1435 tree complex_tree
= bcomplex
->get_tree();
1436 if (complex_tree
== error_mark_node
)
1437 return this->error_expression();
1438 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1439 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1440 TREE_TYPE(TREE_TYPE(complex_tree
)),
1442 return this->make_expression(ret
);
1445 // Make a complex expression given its real and imaginary parts.
1448 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1451 tree real_tree
= breal
->get_tree();
1452 tree imag_tree
= bimag
->get_tree();
1453 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1454 return this->error_expression();
1455 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1456 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1457 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1458 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1459 build_complex_type(TREE_TYPE(real_tree
)),
1460 real_tree
, imag_tree
);
1461 return this->make_expression(ret
);
1464 // An expression that converts an expression to a different type.
1467 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1470 tree type_tree
= type
->get_tree();
1471 tree expr_tree
= expr
->get_tree();
1472 if (type_tree
== error_mark_node
1473 || expr_tree
== error_mark_node
1474 || TREE_TYPE(expr_tree
) == error_mark_node
)
1475 return this->error_expression();
1478 if (this->type_size(type
) == 0
1479 || TREE_TYPE(expr_tree
) == void_type_node
)
1481 // Do not convert zero-sized types.
1484 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1485 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1486 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1487 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1488 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1489 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1490 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1491 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1492 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1493 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1494 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1495 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1496 type_tree
, expr_tree
);
1498 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1500 return this->make_expression(ret
);
1503 // Get the address of a function.
1506 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1508 tree func
= bfunc
->get_tree();
1509 if (func
== error_mark_node
)
1510 return this->error_expression();
1512 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1513 return this->make_expression(ret
);
1516 // Get the address of an expression.
1519 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1521 tree expr
= bexpr
->get_tree();
1522 if (expr
== error_mark_node
)
1523 return this->error_expression();
1525 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1526 return this->make_expression(ret
);
1529 // Return an expression for the field at INDEX in BSTRUCT.
1532 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1535 tree struct_tree
= bstruct
->get_tree();
1536 if (struct_tree
== error_mark_node
1537 || TREE_TYPE(struct_tree
) == error_mark_node
)
1538 return this->error_expression();
1539 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1540 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1541 if (field
== NULL_TREE
)
1543 // This can happen for a type which refers to itself indirectly
1544 // and then turns out to be erroneous.
1545 return this->error_expression();
1547 for (unsigned int i
= index
; i
> 0; --i
)
1549 field
= DECL_CHAIN(field
);
1550 gcc_assert(field
!= NULL_TREE
);
1552 if (TREE_TYPE(field
) == error_mark_node
)
1553 return this->error_expression();
1554 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1555 TREE_TYPE(field
), struct_tree
, field
,
1557 if (TREE_CONSTANT(struct_tree
))
1558 TREE_CONSTANT(ret
) = 1;
1559 return this->make_expression(ret
);
1562 // Return an expression that executes BSTAT before BEXPR.
1565 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1568 tree stat
= bstat
->get_tree();
1569 tree expr
= bexpr
->get_tree();
1570 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1571 return this->error_expression();
1572 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1573 TREE_TYPE(expr
), stat
, expr
);
1574 return this->make_expression(ret
);
1577 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1578 // ELSE_EXPR otherwise.
1581 Gcc_backend::conditional_expression(Bfunction
*, Btype
* btype
,
1582 Bexpression
* condition
,
1583 Bexpression
* then_expr
,
1584 Bexpression
* else_expr
, Location location
)
1586 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1587 tree cond_tree
= condition
->get_tree();
1588 tree then_tree
= then_expr
->get_tree();
1589 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1590 if (type_tree
== error_mark_node
1591 || cond_tree
== error_mark_node
1592 || then_tree
== error_mark_node
1593 || else_tree
== error_mark_node
)
1594 return this->error_expression();
1595 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1596 cond_tree
, then_tree
, else_tree
);
1597 return this->make_expression(ret
);
1600 // Return an expression for the unary operation OP EXPR.
1603 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1605 tree expr_tree
= expr
->get_tree();
1606 if (expr_tree
== error_mark_node
1607 || TREE_TYPE(expr_tree
) == error_mark_node
)
1608 return this->error_expression();
1610 tree type_tree
= TREE_TYPE(expr_tree
);
1611 enum tree_code code
;
1614 case OPERATOR_MINUS
:
1616 tree computed_type
= excess_precision_type(type_tree
);
1617 if (computed_type
!= NULL_TREE
)
1619 expr_tree
= convert(computed_type
, expr_tree
);
1620 type_tree
= computed_type
;
1626 code
= TRUTH_NOT_EXPR
;
1629 code
= BIT_NOT_EXPR
;
1636 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1638 return this->make_expression(ret
);
1641 // Convert a gofrontend operator to an equivalent tree_code.
1643 static enum tree_code
1644 operator_to_tree_code(Operator op
, tree type
)
1646 enum tree_code code
;
1652 case OPERATOR_NOTEQ
:
1668 code
= TRUTH_ORIF_EXPR
;
1670 case OPERATOR_ANDAND
:
1671 code
= TRUTH_ANDIF_EXPR
;
1676 case OPERATOR_MINUS
:
1680 code
= BIT_IOR_EXPR
;
1683 code
= BIT_XOR_EXPR
;
1689 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1692 code
= TRUNC_DIV_EXPR
;
1695 code
= TRUNC_MOD_EXPR
;
1697 case OPERATOR_LSHIFT
:
1700 case OPERATOR_RSHIFT
:
1704 code
= BIT_AND_EXPR
;
1706 case OPERATOR_BITCLEAR
:
1707 code
= BIT_AND_EXPR
;
1716 // Return an expression for the binary operation LEFT OP RIGHT.
1719 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1720 Bexpression
* right
, Location location
)
1722 tree left_tree
= left
->get_tree();
1723 tree right_tree
= right
->get_tree();
1724 if (left_tree
== error_mark_node
1725 || right_tree
== error_mark_node
)
1726 return this->error_expression();
1727 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1729 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1730 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1731 tree computed_type
= excess_precision_type(type_tree
);
1732 if (computed_type
!= NULL_TREE
)
1734 left_tree
= convert(computed_type
, left_tree
);
1735 right_tree
= convert(computed_type
, right_tree
);
1736 type_tree
= computed_type
;
1739 // For comparison operators, the resulting type should be boolean.
1743 case OPERATOR_NOTEQ
:
1748 type_tree
= boolean_type_node
;
1754 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1755 left_tree
, right_tree
);
1756 return this->make_expression(ret
);
1759 // Return an expression that constructs BTYPE with VALS.
1762 Gcc_backend::constructor_expression(Btype
* btype
,
1763 const std::vector
<Bexpression
*>& vals
,
1766 tree type_tree
= btype
->get_tree();
1767 if (type_tree
== error_mark_node
)
1768 return this->error_expression();
1770 vec
<constructor_elt
, va_gc
> *init
;
1771 vec_alloc(init
, vals
.size());
1773 tree sink
= NULL_TREE
;
1774 bool is_constant
= true;
1775 tree field
= TYPE_FIELDS(type_tree
);
1776 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1778 ++p
, field
= DECL_CHAIN(field
))
1780 gcc_assert(field
!= NULL_TREE
);
1781 tree val
= (*p
)->get_tree();
1782 if (TREE_TYPE(field
) == error_mark_node
1783 || val
== error_mark_node
1784 || TREE_TYPE(val
) == error_mark_node
)
1785 return this->error_expression();
1787 if (int_size_in_bytes(TREE_TYPE(field
)) == 0)
1789 // GIMPLE cannot represent indices of zero-sized types so
1790 // trying to construct a map with zero-sized keys might lead
1791 // to errors. Instead, we evaluate each expression that
1792 // would have been added as a map element for its
1793 // side-effects and construct an empty map.
1794 append_to_statement_list(val
, &sink
);
1798 constructor_elt empty
= {NULL
, NULL
};
1799 constructor_elt
* elt
= init
->quick_push(empty
);
1801 elt
->value
= this->convert_tree(TREE_TYPE(field
), val
, location
);
1802 if (!TREE_CONSTANT(elt
->value
))
1803 is_constant
= false;
1805 gcc_assert(field
== NULL_TREE
);
1806 tree ret
= build_constructor(type_tree
, init
);
1808 TREE_CONSTANT(ret
) = 1;
1809 if (sink
!= NULL_TREE
)
1810 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1811 type_tree
, sink
, ret
);
1812 return this->make_expression(ret
);
1816 Gcc_backend::array_constructor_expression(
1817 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1818 const std::vector
<Bexpression
*>& vals
, Location location
)
1820 tree type_tree
= array_btype
->get_tree();
1821 if (type_tree
== error_mark_node
)
1822 return this->error_expression();
1824 gcc_assert(indexes
.size() == vals
.size());
1826 tree element_type
= TREE_TYPE(type_tree
);
1827 HOST_WIDE_INT element_size
= int_size_in_bytes(element_type
);
1828 vec
<constructor_elt
, va_gc
> *init
;
1829 vec_alloc(init
, element_size
== 0 ? 0 : vals
.size());
1831 tree sink
= NULL_TREE
;
1832 bool is_constant
= true;
1833 for (size_t i
= 0; i
< vals
.size(); ++i
)
1835 tree index
= size_int(indexes
[i
]);
1836 tree val
= (vals
[i
])->get_tree();
1838 if (index
== error_mark_node
1839 || val
== error_mark_node
)
1840 return this->error_expression();
1842 if (element_size
== 0)
1844 // GIMPLE cannot represent arrays of zero-sized types so trying
1845 // to construct an array of zero-sized values might lead to errors.
1846 // Instead, we evaluate each expression that would have been added as
1847 // an array value for its side-effects and construct an empty array.
1848 append_to_statement_list(val
, &sink
);
1852 if (!TREE_CONSTANT(val
))
1853 is_constant
= false;
1855 constructor_elt empty
= {NULL
, NULL
};
1856 constructor_elt
* elt
= init
->quick_push(empty
);
1861 tree ret
= build_constructor(type_tree
, init
);
1863 TREE_CONSTANT(ret
) = 1;
1864 if (sink
!= NULL_TREE
)
1865 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1866 type_tree
, sink
, ret
);
1867 return this->make_expression(ret
);
1870 // Return an expression for the address of BASE[INDEX].
1873 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
1876 tree base_tree
= base
->get_tree();
1877 tree index_tree
= index
->get_tree();
1878 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
1879 if (base_tree
== error_mark_node
1880 || TREE_TYPE(base_tree
) == error_mark_node
1881 || index_tree
== error_mark_node
1882 || element_type_tree
== error_mark_node
)
1883 return this->error_expression();
1885 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
1886 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
1887 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
1888 index_tree
, element_size
);
1889 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
1890 TREE_TYPE(base_tree
), base_tree
, offset
);
1891 return this->make_expression(ptr
);
1894 // Return an expression representing ARRAY[INDEX]
1897 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
1900 tree array_tree
= array
->get_tree();
1901 tree index_tree
= index
->get_tree();
1902 if (array_tree
== error_mark_node
1903 || TREE_TYPE(array_tree
) == error_mark_node
1904 || index_tree
== error_mark_node
)
1905 return this->error_expression();
1907 // A function call that returns a zero sized object will have been
1908 // changed to return void. If we see void here, assume we are
1909 // dealing with a zero sized type and just evaluate the operands.
1911 if (TREE_TYPE(array_tree
) != void_type_node
)
1912 ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
1913 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
1914 index_tree
, NULL_TREE
, NULL_TREE
);
1916 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1917 void_type_node
, array_tree
, index_tree
);
1919 return this->make_expression(ret
);
1922 // Create an expression for a call to FN_EXPR with FN_ARGS.
1924 Gcc_backend::call_expression(Bfunction
*, // containing fcn for call
1925 Bexpression
* fn_expr
,
1926 const std::vector
<Bexpression
*>& fn_args
,
1927 Bexpression
* chain_expr
,
1930 tree fn
= fn_expr
->get_tree();
1931 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
1932 return this->error_expression();
1934 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
1935 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
1937 size_t nargs
= fn_args
.size();
1938 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
1939 for (size_t i
= 0; i
< nargs
; ++i
)
1941 args
[i
] = fn_args
.at(i
)->get_tree();
1942 if (args
[i
] == error_mark_node
)
1943 return this->error_expression();
1947 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
1948 fndecl
= TREE_OPERAND(fndecl
, 0);
1950 // This is to support builtin math functions when using 80387 math.
1951 tree excess_type
= NULL_TREE
;
1953 && TREE_CODE(fndecl
) == FUNCTION_DECL
1954 && DECL_IS_BUILTIN(fndecl
)
1955 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
1957 && ((SCALAR_FLOAT_TYPE_P(rettype
)
1958 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
1959 || (COMPLEX_FLOAT_TYPE_P(rettype
)
1960 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
1962 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
1963 if (excess_type
!= NULL_TREE
)
1965 tree excess_fndecl
= mathfn_built_in(excess_type
,
1966 DECL_FUNCTION_CODE(fndecl
));
1967 if (excess_fndecl
== NULL_TREE
)
1968 excess_type
= NULL_TREE
;
1971 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
1973 for (size_t i
= 0; i
< nargs
; ++i
)
1975 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
1976 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
1977 args
[i
] = ::convert(excess_type
, args
[i
]);
1984 build_call_array_loc(location
.gcc_location(),
1985 excess_type
!= NULL_TREE
? excess_type
: rettype
,
1989 CALL_EXPR_STATIC_CHAIN (ret
) = chain_expr
->get_tree();
1991 if (excess_type
!= NULL_TREE
)
1993 // Calling convert here can undo our excess precision change.
1994 // That may or may not be a bug in convert_to_real.
1995 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
1999 return this->make_expression(ret
);
2002 // Return an expression that allocates SIZE bytes on the stack.
2005 Gcc_backend::stack_allocation_expression(int64_t size
, Location location
)
2007 tree alloca
= builtin_decl_explicit(BUILT_IN_ALLOCA
);
2008 tree size_tree
= build_int_cst(integer_type_node
, size
);
2009 tree ret
= build_call_expr_loc(location
.gcc_location(), alloca
, 1, size_tree
);
2010 tree memset
= builtin_decl_explicit(BUILT_IN_MEMSET
);
2011 ret
= build_call_expr_loc(location
.gcc_location(), memset
, 3,
2012 ret
, integer_zero_node
, size_tree
);
2013 return this->make_expression(ret
);
2016 // An expression as a statement.
2019 Gcc_backend::expression_statement(Bfunction
*, Bexpression
* expr
)
2021 return this->make_statement(expr
->get_tree());
2024 // Variable initialization.
2027 Gcc_backend::init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
)
2029 tree var_tree
= var
->get_decl();
2030 tree init_tree
= init
->get_tree();
2031 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
2032 return this->error_statement();
2033 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
2035 // To avoid problems with GNU ld, we don't make zero-sized
2036 // externally visible variables. That might lead us to doing an
2037 // initialization of a zero-sized expression to a non-zero sized
2038 // variable, or vice-versa. Avoid crashes by omitting the
2039 // initializer. Such initializations don't mean anything anyhow.
2040 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
2041 && init_tree
!= NULL_TREE
2042 && TREE_TYPE(init_tree
) != void_type_node
2043 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
2045 DECL_INITIAL(var_tree
) = init_tree
;
2046 init_tree
= NULL_TREE
;
2049 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
2050 void_type_node
, var_tree
);
2051 if (init_tree
!= NULL_TREE
)
2052 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
2053 void_type_node
, init_tree
, ret
);
2055 return this->make_statement(ret
);
2061 Gcc_backend::assignment_statement(Bfunction
* bfn
, Bexpression
* lhs
,
2062 Bexpression
* rhs
, Location location
)
2064 tree lhs_tree
= lhs
->get_tree();
2065 tree rhs_tree
= rhs
->get_tree();
2066 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
2067 return this->error_statement();
2069 // To avoid problems with GNU ld, we don't make zero-sized
2070 // externally visible variables. That might lead us to doing an
2071 // assignment of a zero-sized expression to a non-zero sized
2072 // expression; avoid crashes here by avoiding assignments of
2073 // zero-sized expressions. Such assignments don't really mean
2075 if (TREE_TYPE(lhs_tree
) == void_type_node
2076 || int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
2077 || TREE_TYPE(rhs_tree
) == void_type_node
2078 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
2079 return this->compound_statement(this->expression_statement(bfn
, lhs
),
2080 this->expression_statement(bfn
, rhs
));
2082 rhs_tree
= this->convert_tree(TREE_TYPE(lhs_tree
), rhs_tree
, location
);
2084 return this->make_statement(fold_build2_loc(location
.gcc_location(),
2087 lhs_tree
, rhs_tree
));
2093 Gcc_backend::return_statement(Bfunction
* bfunction
,
2094 const std::vector
<Bexpression
*>& vals
,
2097 tree fntree
= bfunction
->get_tree();
2098 if (fntree
== error_mark_node
)
2099 return this->error_statement();
2100 tree result
= DECL_RESULT(fntree
);
2101 if (result
== error_mark_node
)
2102 return this->error_statement();
2104 // If the result size is zero bytes, we have set the function type
2105 // to have a result type of void, so don't return anything.
2106 // See the function_type method.
2107 tree res_type
= TREE_TYPE(result
);
2108 if (res_type
== void_type_node
|| int_size_in_bytes(res_type
) == 0)
2110 tree stmt_list
= NULL_TREE
;
2111 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2115 tree val
= (*p
)->get_tree();
2116 if (val
== error_mark_node
)
2117 return this->error_statement();
2118 append_to_statement_list(val
, &stmt_list
);
2120 tree ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2121 void_type_node
, NULL_TREE
);
2122 append_to_statement_list(ret
, &stmt_list
);
2123 return this->make_statement(stmt_list
);
2128 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
2130 else if (vals
.size() == 1)
2132 tree val
= vals
.front()->get_tree();
2133 if (val
== error_mark_node
)
2134 return this->error_statement();
2135 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2136 void_type_node
, result
,
2137 vals
.front()->get_tree());
2138 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2139 void_type_node
, set
);
2143 // To return multiple values, copy the values into a temporary
2144 // variable of the right structure type, and then assign the
2145 // temporary variable to the DECL_RESULT in the return
2147 tree stmt_list
= NULL_TREE
;
2148 tree rettype
= TREE_TYPE(result
);
2150 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2151 push_struct_function(fntree
);
2153 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2154 tree rettmp
= create_tmp_var(rettype
, "RESULT");
2157 tree field
= TYPE_FIELDS(rettype
);
2158 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2160 p
++, field
= DECL_CHAIN(field
))
2162 gcc_assert(field
!= NULL_TREE
);
2163 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
2164 TREE_TYPE(field
), rettmp
, field
,
2166 tree val
= (*p
)->get_tree();
2167 if (val
== error_mark_node
)
2168 return this->error_statement();
2169 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2171 ref
, (*p
)->get_tree());
2172 append_to_statement_list(set
, &stmt_list
);
2174 gcc_assert(field
== NULL_TREE
);
2175 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2178 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2179 void_type_node
, set
);
2180 append_to_statement_list(ret_expr
, &stmt_list
);
2183 return this->make_statement(ret
);
2186 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2187 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2188 // NULL, it will always be executed. This is used for handling defers in Go
2189 // functions. In C++, the resulting code is of this form:
2190 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2193 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2194 Bstatement
* except_stmt
,
2195 Bstatement
* finally_stmt
,
2198 tree stat_tree
= bstat
->get_tree();
2199 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2200 tree finally_tree
= finally_stmt
== NULL
2202 : finally_stmt
->get_tree();
2204 if (stat_tree
== error_mark_node
2205 || except_tree
== error_mark_node
2206 || finally_tree
== error_mark_node
)
2207 return this->error_statement();
2209 if (except_tree
!= NULL_TREE
)
2210 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2211 void_type_node
, stat_tree
,
2212 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2213 void_type_node
, NULL
, except_tree
));
2214 if (finally_tree
!= NULL_TREE
)
2215 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2216 void_type_node
, stat_tree
, finally_tree
);
2217 return this->make_statement(stat_tree
);
2223 Gcc_backend::if_statement(Bfunction
*, Bexpression
* condition
,
2224 Bblock
* then_block
, Bblock
* else_block
,
2227 tree cond_tree
= condition
->get_tree();
2228 tree then_tree
= then_block
->get_tree();
2229 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2230 if (cond_tree
== error_mark_node
2231 || then_tree
== error_mark_node
2232 || else_tree
== error_mark_node
)
2233 return this->error_statement();
2234 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2235 cond_tree
, then_tree
, else_tree
);
2236 return this->make_statement(ret
);
2242 Gcc_backend::switch_statement(
2243 Bfunction
* function
,
2245 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2246 const std::vector
<Bstatement
*>& statements
,
2247 Location switch_location
)
2249 gcc_assert(cases
.size() == statements
.size());
2251 tree decl
= function
->get_tree();
2252 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2253 push_struct_function(decl
);
2255 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2257 tree stmt_list
= NULL_TREE
;
2258 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2259 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2260 ps
!= statements
.end();
2265 source_location loc
= (*ps
!= NULL
2266 ? EXPR_LOCATION((*ps
)->get_tree())
2267 : UNKNOWN_LOCATION
);
2268 tree label
= create_artificial_label(loc
);
2269 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2270 append_to_statement_list(c
, &stmt_list
);
2274 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2278 tree t
= (*pcv
)->get_tree();
2279 if (t
== error_mark_node
)
2280 return this->error_statement();
2281 source_location loc
= EXPR_LOCATION(t
);
2282 tree label
= create_artificial_label(loc
);
2283 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2284 append_to_statement_list(c
, &stmt_list
);
2290 tree t
= (*ps
)->get_tree();
2291 if (t
== error_mark_node
)
2292 return this->error_statement();
2293 append_to_statement_list(t
, &stmt_list
);
2298 tree tv
= value
->get_tree();
2299 if (tv
== error_mark_node
)
2300 return this->error_statement();
2301 tree t
= build2_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2302 NULL_TREE
, tv
, stmt_list
);
2303 return this->make_statement(t
);
2306 // Pair of statements.
2309 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2311 tree stmt_list
= NULL_TREE
;
2312 tree t
= s1
->get_tree();
2313 if (t
== error_mark_node
)
2314 return this->error_statement();
2315 append_to_statement_list(t
, &stmt_list
);
2317 if (t
== error_mark_node
)
2318 return this->error_statement();
2319 append_to_statement_list(t
, &stmt_list
);
2321 // If neither statement has any side effects, stmt_list can be NULL
2323 if (stmt_list
== NULL_TREE
)
2324 stmt_list
= integer_zero_node
;
2326 return this->make_statement(stmt_list
);
2329 // List of statements.
2332 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2334 tree stmt_list
= NULL_TREE
;
2335 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2336 p
!= statements
.end();
2339 tree t
= (*p
)->get_tree();
2340 if (t
== error_mark_node
)
2341 return this->error_statement();
2342 append_to_statement_list(t
, &stmt_list
);
2344 return this->make_statement(stmt_list
);
2347 // Make a block. For some reason gcc uses a dual structure for
2348 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2349 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2353 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2354 const std::vector
<Bvariable
*>& vars
,
2355 Location start_location
,
2358 tree block_tree
= make_node(BLOCK
);
2359 if (enclosing
== NULL
)
2361 tree fndecl
= function
->get_tree();
2362 gcc_assert(fndecl
!= NULL_TREE
);
2364 // We may have already created a block for local variables when
2365 // we take the address of a parameter.
2366 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2368 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2369 DECL_INITIAL(fndecl
) = block_tree
;
2373 tree superblock_tree
= DECL_INITIAL(fndecl
);
2374 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2376 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2378 pp
= &BLOCK_CHAIN(*pp
))
2385 tree superbind_tree
= enclosing
->get_tree();
2386 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2387 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2389 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2391 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2393 pp
= &BLOCK_CHAIN(*pp
))
2398 tree
* pp
= &BLOCK_VARS(block_tree
);
2399 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2403 *pp
= (*pv
)->get_decl();
2404 if (*pp
!= error_mark_node
)
2405 pp
= &DECL_CHAIN(*pp
);
2409 TREE_USED(block_tree
) = 1;
2411 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2412 void_type_node
, BLOCK_VARS(block_tree
),
2413 NULL_TREE
, block_tree
);
2414 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2415 return new Bblock(bind_tree
);
2418 // Add statements to a block.
2421 Gcc_backend::block_add_statements(Bblock
* bblock
,
2422 const std::vector
<Bstatement
*>& statements
)
2424 tree stmt_list
= NULL_TREE
;
2425 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2426 p
!= statements
.end();
2429 tree s
= (*p
)->get_tree();
2430 if (s
!= error_mark_node
)
2431 append_to_statement_list(s
, &stmt_list
);
2434 tree bind_tree
= bblock
->get_tree();
2435 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2436 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2439 // Return a block as a statement.
2442 Gcc_backend::block_statement(Bblock
* bblock
)
2444 tree bind_tree
= bblock
->get_tree();
2445 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2446 return this->make_statement(bind_tree
);
2449 // This is not static because we declare it with GTY(()) in go-c.h.
2450 tree go_non_zero_struct
;
2452 // Return a type corresponding to TYPE with non-zero size.
2455 Gcc_backend::non_zero_size_type(tree type
)
2457 if (int_size_in_bytes(type
) != 0)
2460 switch (TREE_CODE(type
))
2463 if (TYPE_FIELDS(type
) != NULL_TREE
)
2465 tree ns
= make_node(RECORD_TYPE
);
2466 tree field_trees
= NULL_TREE
;
2467 tree
*pp
= &field_trees
;
2468 for (tree field
= TYPE_FIELDS(type
);
2470 field
= DECL_CHAIN(field
))
2472 tree ft
= TREE_TYPE(field
);
2473 if (field
== TYPE_FIELDS(type
))
2474 ft
= non_zero_size_type(ft
);
2475 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2476 DECL_NAME(field
), ft
);
2477 DECL_CONTEXT(f
) = ns
;
2479 pp
= &DECL_CHAIN(f
);
2481 TYPE_FIELDS(ns
) = field_trees
;
2486 if (go_non_zero_struct
== NULL_TREE
)
2488 type
= make_node(RECORD_TYPE
);
2489 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2490 get_identifier("dummy"),
2492 DECL_CONTEXT(field
) = type
;
2493 TYPE_FIELDS(type
) = field
;
2495 go_non_zero_struct
= type
;
2497 return go_non_zero_struct
;
2501 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2502 return build_array_type_nelts(element_type
, 1);
2512 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type
2513 // can be created multiple times and thus have multiple tree
2514 // representations. Make sure this does not confuse the middle-end.
2517 Gcc_backend::convert_tree(tree type_tree
, tree expr_tree
, Location location
)
2519 if (type_tree
== TREE_TYPE(expr_tree
))
2522 if (type_tree
== error_mark_node
2523 || expr_tree
== error_mark_node
2524 || TREE_TYPE(expr_tree
) == error_mark_node
)
2525 return error_mark_node
;
2527 gcc_assert(TREE_CODE(type_tree
) == TREE_CODE(TREE_TYPE(expr_tree
)));
2528 if (POINTER_TYPE_P(type_tree
)
2529 || INTEGRAL_TYPE_P(type_tree
)
2530 || SCALAR_FLOAT_TYPE_P(type_tree
)
2531 || COMPLEX_FLOAT_TYPE_P(type_tree
))
2532 return fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
2533 else if (TREE_CODE(type_tree
) == RECORD_TYPE
2534 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
2536 gcc_assert(int_size_in_bytes(type_tree
)
2537 == int_size_in_bytes(TREE_TYPE(expr_tree
)));
2538 if (TYPE_MAIN_VARIANT(type_tree
)
2539 == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
2540 return fold_build1_loc(location
.gcc_location(), NOP_EXPR
,
2541 type_tree
, expr_tree
);
2542 return fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
2543 type_tree
, expr_tree
);
2549 // Make a global variable.
2552 Gcc_backend::global_variable(const std::string
& var_name
,
2553 const std::string
& asm_name
,
2557 bool in_unique_section
,
2560 tree type_tree
= btype
->get_tree();
2561 if (type_tree
== error_mark_node
)
2562 return this->error_variable();
2564 // The GNU linker does not like dynamic variables with zero size.
2565 tree orig_type_tree
= type_tree
;
2566 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2567 type_tree
= this->non_zero_size_type(type_tree
);
2569 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2570 get_identifier_from_string(var_name
),
2573 DECL_EXTERNAL(decl
) = 1;
2575 TREE_STATIC(decl
) = 1;
2578 TREE_PUBLIC(decl
) = 1;
2579 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2583 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2586 TREE_USED(decl
) = 1;
2588 if (in_unique_section
)
2589 resolve_unique_section (decl
, 0, 1);
2591 go_preserve_from_gc(decl
);
2593 return new Bvariable(decl
, orig_type_tree
);
2596 // Set the initial value of a global variable.
2599 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2601 tree expr_tree
= expr
->get_tree();
2602 if (expr_tree
== error_mark_node
)
2604 gcc_assert(TREE_CONSTANT(expr_tree
));
2605 tree var_decl
= var
->get_decl();
2606 if (var_decl
== error_mark_node
)
2608 DECL_INITIAL(var_decl
) = expr_tree
;
2610 // If this variable goes in a unique section, it may need to go into
2611 // a different one now that DECL_INITIAL is set.
2612 if (symtab_node::get(var_decl
)
2613 && symtab_node::get(var_decl
)->implicit_section
)
2615 set_decl_section_name (var_decl
, NULL
);
2616 resolve_unique_section (var_decl
,
2617 compute_reloc_for_constant (expr_tree
),
2622 // Make a local variable.
2625 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2626 Btype
* btype
, Bvariable
* decl_var
,
2627 bool is_address_taken
, Location location
)
2629 tree type_tree
= btype
->get_tree();
2630 if (type_tree
== error_mark_node
)
2631 return this->error_variable();
2632 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2633 get_identifier_from_string(name
),
2635 DECL_CONTEXT(decl
) = function
->get_tree();
2636 TREE_USED(decl
) = 1;
2637 if (is_address_taken
)
2638 TREE_ADDRESSABLE(decl
) = 1;
2639 if (decl_var
!= NULL
)
2641 DECL_HAS_VALUE_EXPR_P(decl
) = 1;
2642 SET_DECL_VALUE_EXPR(decl
, decl_var
->get_decl());
2644 go_preserve_from_gc(decl
);
2645 return new Bvariable(decl
);
2648 // Make a function parameter variable.
2651 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2652 Btype
* btype
, bool is_address_taken
,
2655 tree type_tree
= btype
->get_tree();
2656 if (type_tree
== error_mark_node
)
2657 return this->error_variable();
2658 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2659 get_identifier_from_string(name
),
2661 DECL_CONTEXT(decl
) = function
->get_tree();
2662 DECL_ARG_TYPE(decl
) = type_tree
;
2663 TREE_USED(decl
) = 1;
2664 if (is_address_taken
)
2665 TREE_ADDRESSABLE(decl
) = 1;
2666 go_preserve_from_gc(decl
);
2667 return new Bvariable(decl
);
2670 // Make a static chain variable.
2673 Gcc_backend::static_chain_variable(Bfunction
* function
, const std::string
& name
,
2674 Btype
* btype
, Location location
)
2676 tree type_tree
= btype
->get_tree();
2677 if (type_tree
== error_mark_node
)
2678 return this->error_variable();
2679 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2680 get_identifier_from_string(name
), type_tree
);
2681 tree fndecl
= function
->get_tree();
2682 DECL_CONTEXT(decl
) = fndecl
;
2683 DECL_ARG_TYPE(decl
) = type_tree
;
2684 TREE_USED(decl
) = 1;
2685 DECL_ARTIFICIAL(decl
) = 1;
2686 DECL_IGNORED_P(decl
) = 1;
2687 TREE_READONLY(decl
) = 1;
2689 struct function
*f
= DECL_STRUCT_FUNCTION(fndecl
);
2692 push_struct_function(fndecl
);
2694 f
= DECL_STRUCT_FUNCTION(fndecl
);
2696 gcc_assert(f
->static_chain_decl
== NULL
);
2697 f
->static_chain_decl
= decl
;
2698 DECL_STATIC_CHAIN(fndecl
) = 1;
2700 go_preserve_from_gc(decl
);
2701 return new Bvariable(decl
);
2704 // Make a temporary variable.
2707 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2708 Btype
* btype
, Bexpression
* binit
,
2709 bool is_address_taken
,
2711 Bstatement
** pstatement
)
2713 gcc_assert(function
!= NULL
);
2714 tree decl
= function
->get_tree();
2715 tree type_tree
= btype
->get_tree();
2716 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2717 if (type_tree
== error_mark_node
2718 || init_tree
== error_mark_node
2719 || decl
== error_mark_node
)
2721 *pstatement
= this->error_statement();
2722 return this->error_variable();
2726 // We can only use create_tmp_var if the type is not addressable.
2727 if (!TREE_ADDRESSABLE(type_tree
))
2729 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2730 push_struct_function(decl
);
2732 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2734 var
= create_tmp_var(type_tree
, "GOTMP");
2739 gcc_assert(bblock
!= NULL
);
2740 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2741 create_tmp_var_name("GOTMP"),
2743 DECL_ARTIFICIAL(var
) = 1;
2744 DECL_IGNORED_P(var
) = 1;
2746 DECL_CONTEXT(var
) = decl
;
2748 // We have to add this variable to the BLOCK and the BIND_EXPR.
2749 tree bind_tree
= bblock
->get_tree();
2750 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2751 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2752 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2753 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2754 BLOCK_VARS(block_tree
) = var
;
2755 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2758 if (this->type_size(btype
) != 0
2759 && init_tree
!= NULL_TREE
2760 && TREE_TYPE(init_tree
) != void_type_node
)
2761 DECL_INITIAL(var
) = this->convert_tree(type_tree
, init_tree
, location
);
2763 if (is_address_taken
)
2764 TREE_ADDRESSABLE(var
) = 1;
2766 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2768 void_type_node
, var
));
2770 // For a zero sized type, don't initialize VAR with BINIT, but still
2771 // evaluate BINIT for its side effects.
2772 if (init_tree
!= NULL_TREE
2773 && (this->type_size(btype
) == 0
2774 || TREE_TYPE(init_tree
) == void_type_node
))
2776 this->compound_statement(this->expression_statement(function
, binit
),
2779 return new Bvariable(var
);
2782 // Create an implicit variable that is compiler-defined. This is used when
2783 // generating GC root variables and storing the values of a slice initializer.
2786 Gcc_backend::implicit_variable(const std::string
& name
,
2787 const std::string
& asm_name
,
2788 Btype
* type
, bool is_hidden
, bool is_constant
,
2789 bool is_common
, int64_t alignment
)
2791 tree type_tree
= type
->get_tree();
2792 if (type_tree
== error_mark_node
)
2793 return this->error_variable();
2795 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2796 get_identifier_from_string(name
), type_tree
);
2797 DECL_EXTERNAL(decl
) = 0;
2798 TREE_PUBLIC(decl
) = !is_hidden
;
2799 TREE_STATIC(decl
) = 1;
2800 TREE_USED(decl
) = 1;
2801 DECL_ARTIFICIAL(decl
) = 1;
2804 DECL_COMMON(decl
) = 1;
2806 // When the initializer for one implicit_variable refers to another,
2807 // it needs to know the visibility of the referenced struct so that
2808 // compute_reloc_for_constant will return the right value. On many
2809 // systems calling make_decl_one_only will mark the decl as weak,
2810 // which will change the return value of compute_reloc_for_constant.
2811 // We can't reliably call make_decl_one_only yet, because we don't
2812 // yet know the initializer. This issue doesn't arise in C because
2813 // Go initializers, unlike C initializers, can be indirectly
2814 // recursive. To ensure that compute_reloc_for_constant computes
2815 // the right value if some other initializer refers to this one, we
2816 // mark this symbol as weak here. We undo that below in
2817 // immutable_struct_set_init before calling mark_decl_one_only.
2818 DECL_WEAK(decl
) = 1;
2822 TREE_READONLY(decl
) = 1;
2823 TREE_CONSTANT(decl
) = 1;
2827 SET_DECL_ALIGN(decl
, alignment
* BITS_PER_UNIT
);
2828 DECL_USER_ALIGN(decl
) = 1;
2830 if (! asm_name
.empty())
2831 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2833 go_preserve_from_gc(decl
);
2834 return new Bvariable(decl
);
2837 // Set the initalizer for a variable created by implicit_variable.
2838 // This is where we finish compiling the variable.
2841 Gcc_backend::implicit_variable_set_init(Bvariable
* var
, const std::string
&,
2842 Btype
*, bool, bool, bool is_common
,
2845 tree decl
= var
->get_decl();
2848 init_tree
= NULL_TREE
;
2850 init_tree
= init
->get_tree();
2851 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2854 DECL_INITIAL(decl
) = init_tree
;
2856 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2857 // See the comment where DECL_WEAK is set in implicit_variable.
2860 DECL_WEAK(decl
) = 0;
2861 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2864 resolve_unique_section(decl
, 2, 1);
2866 rest_of_decl_compilation(decl
, 1, 0);
2869 // Return a reference to an implicit variable defined in another package.
2872 Gcc_backend::implicit_variable_reference(const std::string
& name
,
2873 const std::string
& asm_name
,
2876 tree type_tree
= btype
->get_tree();
2877 if (type_tree
== error_mark_node
)
2878 return this->error_variable();
2880 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2881 get_identifier_from_string(name
), type_tree
);
2882 DECL_EXTERNAL(decl
) = 1;
2883 TREE_PUBLIC(decl
) = 1;
2884 TREE_STATIC(decl
) = 0;
2885 DECL_ARTIFICIAL(decl
) = 1;
2886 if (! asm_name
.empty())
2887 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2888 go_preserve_from_gc(decl
);
2889 return new Bvariable(decl
);
2892 // Create a named immutable initialized data structure.
2895 Gcc_backend::immutable_struct(const std::string
& name
,
2896 const std::string
& asm_name
,
2898 bool is_common
, Btype
* btype
, Location location
)
2900 tree type_tree
= btype
->get_tree();
2901 if (type_tree
== error_mark_node
)
2902 return this->error_variable();
2903 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2904 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2905 get_identifier_from_string(name
),
2906 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2907 TREE_STATIC(decl
) = 1;
2908 TREE_USED(decl
) = 1;
2909 TREE_READONLY(decl
) = 1;
2910 TREE_CONSTANT(decl
) = 1;
2911 DECL_ARTIFICIAL(decl
) = 1;
2913 TREE_PUBLIC(decl
) = 1;
2914 if (! asm_name
.empty())
2915 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2917 // When the initializer for one immutable_struct refers to another,
2918 // it needs to know the visibility of the referenced struct so that
2919 // compute_reloc_for_constant will return the right value. On many
2920 // systems calling make_decl_one_only will mark the decl as weak,
2921 // which will change the return value of compute_reloc_for_constant.
2922 // We can't reliably call make_decl_one_only yet, because we don't
2923 // yet know the initializer. This issue doesn't arise in C because
2924 // Go initializers, unlike C initializers, can be indirectly
2925 // recursive. To ensure that compute_reloc_for_constant computes
2926 // the right value if some other initializer refers to this one, we
2927 // mark this symbol as weak here. We undo that below in
2928 // immutable_struct_set_init before calling mark_decl_one_only.
2930 DECL_WEAK(decl
) = 1;
2932 // We don't call rest_of_decl_compilation until we have the
2935 go_preserve_from_gc(decl
);
2936 return new Bvariable(decl
);
2939 // Set the initializer for a variable created by immutable_struct.
2940 // This is where we finish compiling the variable.
2943 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
2944 bool, bool is_common
, Btype
*, Location
,
2945 Bexpression
* initializer
)
2947 tree decl
= var
->get_decl();
2948 tree init_tree
= initializer
->get_tree();
2949 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2952 DECL_INITIAL(decl
) = init_tree
;
2954 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2955 // See the comment where DECL_WEAK is set in immutable_struct.
2958 DECL_WEAK(decl
) = 0;
2959 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2962 // These variables are often unneeded in the final program, so put
2963 // them in their own section so that linker GC can discard them.
2964 resolve_unique_section(decl
,
2965 compute_reloc_for_constant (init_tree
),
2968 rest_of_decl_compilation(decl
, 1, 0);
2971 // Return a reference to an immutable initialized data structure
2972 // defined in another package.
2975 Gcc_backend::immutable_struct_reference(const std::string
& name
,
2976 const std::string
& asm_name
,
2980 tree type_tree
= btype
->get_tree();
2981 if (type_tree
== error_mark_node
)
2982 return this->error_variable();
2983 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2984 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2985 get_identifier_from_string(name
),
2986 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2987 TREE_READONLY(decl
) = 1;
2988 TREE_CONSTANT(decl
) = 1;
2989 DECL_ARTIFICIAL(decl
) = 1;
2990 TREE_PUBLIC(decl
) = 1;
2991 DECL_EXTERNAL(decl
) = 1;
2992 if (! asm_name
.empty())
2993 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2994 go_preserve_from_gc(decl
);
2995 return new Bvariable(decl
);
3001 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
3007 tree func_tree
= function
->get_tree();
3008 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
3009 push_struct_function(func_tree
);
3011 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
3013 decl
= create_artificial_label(location
.gcc_location());
3019 tree id
= get_identifier_from_string(name
);
3020 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
3022 DECL_CONTEXT(decl
) = function
->get_tree();
3024 return new Blabel(decl
);
3027 // Make a statement which defines a label.
3030 Gcc_backend::label_definition_statement(Blabel
* label
)
3032 tree lab
= label
->get_tree();
3033 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
3034 void_type_node
, lab
);
3035 return this->make_statement(ret
);
3038 // Make a goto statement.
3041 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
3043 tree lab
= label
->get_tree();
3044 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
3046 return this->make_statement(ret
);
3049 // Get the address of a label.
3052 Gcc_backend::label_address(Blabel
* label
, Location location
)
3054 tree lab
= label
->get_tree();
3056 TREE_ADDRESSABLE(lab
) = 1;
3057 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
3058 build_fold_addr_expr_loc(location
.gcc_location(),
3060 return this->make_expression(ret
);
3063 // Declare or define a new function.
3066 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
3067 const std::string
& asm_name
, bool is_visible
,
3068 bool is_declaration
, bool is_inlinable
,
3069 bool disable_split_stack
, bool does_not_return
,
3070 bool in_unique_section
, Location location
)
3072 tree functype
= fntype
->get_tree();
3073 if (functype
!= error_mark_node
)
3075 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
3076 functype
= TREE_TYPE(functype
);
3078 tree id
= get_identifier_from_string(name
);
3079 if (functype
== error_mark_node
|| id
== error_mark_node
)
3080 return this->error_function();
3082 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
3083 if (! asm_name
.empty())
3084 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3086 TREE_PUBLIC(decl
) = 1;
3088 DECL_EXTERNAL(decl
) = 1;
3091 tree restype
= TREE_TYPE(functype
);
3093 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
3094 DECL_ARTIFICIAL(resdecl
) = 1;
3095 DECL_IGNORED_P(resdecl
) = 1;
3096 DECL_CONTEXT(resdecl
) = decl
;
3097 DECL_RESULT(decl
) = resdecl
;
3100 DECL_UNINLINABLE(decl
) = 1;
3101 if (disable_split_stack
)
3103 tree attr
= get_identifier ("no_split_stack");
3104 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
3106 if (does_not_return
)
3107 TREE_THIS_VOLATILE(decl
) = 1;
3108 if (in_unique_section
)
3109 resolve_unique_section(decl
, 0, 1);
3111 go_preserve_from_gc(decl
);
3112 return new Bfunction(decl
);
3115 // Create a statement that runs all deferred calls for FUNCTION. This should
3116 // be a statement that looks like this in C++:
3118 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3121 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
3122 Bexpression
* defer
, Location location
)
3124 tree undefer_tree
= undefer
->get_tree();
3125 tree defer_tree
= defer
->get_tree();
3126 tree fntree
= function
->get_tree();
3128 if (undefer_tree
== error_mark_node
3129 || defer_tree
== error_mark_node
3130 || fntree
== error_mark_node
)
3131 return this->error_statement();
3133 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
3134 push_struct_function(fntree
);
3136 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
3138 tree stmt_list
= NULL
;
3139 Blabel
* blabel
= this->label(function
, "", location
);
3140 Bstatement
* label_def
= this->label_definition_statement(blabel
);
3141 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
3143 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
3144 tree jump
= jump_stmt
->get_tree();
3145 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
3146 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
3148 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
3149 append_to_statement_list(try_catch
, &stmt_list
);
3152 return this->make_statement(stmt_list
);
3155 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3156 // This will only be called for a function definition.
3159 Gcc_backend::function_set_parameters(Bfunction
* function
,
3160 const std::vector
<Bvariable
*>& param_vars
)
3162 tree func_tree
= function
->get_tree();
3163 if (func_tree
== error_mark_node
)
3166 tree params
= NULL_TREE
;
3168 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
3169 pv
!= param_vars
.end();
3172 *pp
= (*pv
)->get_decl();
3173 gcc_assert(*pp
!= error_mark_node
);
3174 pp
= &DECL_CHAIN(*pp
);
3177 DECL_ARGUMENTS(func_tree
) = params
;
3181 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3184 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
3186 tree func_tree
= function
->get_tree();
3187 tree code
= code_stmt
->get_tree();
3189 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
3191 DECL_SAVED_TREE(func_tree
) = code
;
3195 // Look up a named built-in function in the current backend implementation.
3196 // Returns NULL if no built-in function by that name exists.
3199 Gcc_backend::lookup_builtin(const std::string
& name
)
3201 if (this->builtin_functions_
.count(name
) != 0)
3202 return this->builtin_functions_
[name
];
3206 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3207 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3208 // emit early debugging information.
3211 Gcc_backend::write_global_definitions(
3212 const std::vector
<Btype
*>& type_decls
,
3213 const std::vector
<Bexpression
*>& constant_decls
,
3214 const std::vector
<Bfunction
*>& function_decls
,
3215 const std::vector
<Bvariable
*>& variable_decls
)
3217 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
3218 + function_decls
.size() + variable_decls
.size();
3220 tree
* defs
= new tree
[count_definitions
];
3222 // Convert all non-erroneous declarations into Gimple form.
3224 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
3225 p
!= variable_decls
.end();
3228 tree v
= (*p
)->get_decl();
3229 if (v
!= error_mark_node
)
3232 go_preserve_from_gc(defs
[i
]);
3237 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
3238 p
!= type_decls
.end();
3241 tree type_tree
= (*p
)->get_tree();
3242 if (type_tree
!= error_mark_node
3243 && IS_TYPE_OR_DECL_P(type_tree
))
3245 defs
[i
] = TYPE_NAME(type_tree
);
3246 gcc_assert(defs
[i
] != NULL
);
3247 go_preserve_from_gc(defs
[i
]);
3251 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
3252 p
!= constant_decls
.end();
3255 if ((*p
)->get_tree() != error_mark_node
)
3257 defs
[i
] = (*p
)->get_tree();
3258 go_preserve_from_gc(defs
[i
]);
3262 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
3263 p
!= function_decls
.end();
3266 tree decl
= (*p
)->get_tree();
3267 if (decl
!= error_mark_node
)
3269 go_preserve_from_gc(decl
);
3270 gimplify_function_tree(decl
);
3271 cgraph_node::finalize_function(decl
, true);
3278 // Pass everything back to the middle-end.
3280 wrapup_global_declarations(defs
, i
);
3286 Gcc_backend::write_export_data(const char* bytes
, unsigned int size
)
3288 go_write_export_data(bytes
, size
);
3292 // Define a builtin function. BCODE is the builtin function code
3293 // defined by builtins.def. NAME is the name of the builtin function.
3294 // LIBNAME is the name of the corresponding library function, and is
3295 // NULL if there isn't one. FNTYPE is the type of the function.
3296 // CONST_P is true if the function has the const attribute.
3297 // NORETURN_P is true if the function has the noreturn attribute.
3300 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
3301 const char* libname
, tree fntype
, bool const_p
,
3304 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
3305 libname
, NULL_TREE
);
3307 TREE_READONLY(decl
) = 1;
3309 TREE_THIS_VOLATILE(decl
) = 1;
3310 set_builtin_decl(bcode
, decl
, true);
3311 this->builtin_functions_
[name
] = this->make_function(decl
);
3312 if (libname
!= NULL
)
3314 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
3317 TREE_READONLY(decl
) = 1;
3319 TREE_THIS_VOLATILE(decl
) = 1;
3320 this->builtin_functions_
[libname
] = this->make_function(decl
);
3324 // Return the backend generator.
3329 return new Gcc_backend();