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
);
359 { return this->make_statement(error_mark_node
); }
362 expression_statement(Bfunction
*, Bexpression
*);
365 init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
);
368 assignment_statement(Bfunction
*, Bexpression
* lhs
, Bexpression
* rhs
,
372 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
376 if_statement(Bfunction
*, Bexpression
* condition
, Bblock
* then_block
,
377 Bblock
* else_block
, Location
);
380 switch_statement(Bfunction
* function
, Bexpression
* value
,
381 const std::vector
<std::vector
<Bexpression
*> >& cases
,
382 const std::vector
<Bstatement
*>& statements
,
386 compound_statement(Bstatement
*, Bstatement
*);
389 statement_list(const std::vector
<Bstatement
*>&);
392 exception_handler_statement(Bstatement
* bstat
, Bstatement
* except_stmt
,
393 Bstatement
* finally_stmt
, Location
);
398 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
402 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
405 block_statement(Bblock
*);
411 { return new Bvariable(error_mark_node
); }
414 global_variable(const std::string
& var_name
,
415 const std::string
& asm_name
,
419 bool in_unique_section
,
423 global_variable_set_init(Bvariable
*, Bexpression
*);
426 local_variable(Bfunction
*, const std::string
&, Btype
*, Bvariable
*, bool,
430 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
434 static_chain_variable(Bfunction
*, const std::string
&, Btype
*, Location
);
437 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
438 Location
, Bstatement
**);
441 implicit_variable(const std::string
&, const std::string
&, Btype
*,
442 bool, bool, bool, int64_t);
445 implicit_variable_set_init(Bvariable
*, const std::string
&, Btype
*,
446 bool, bool, bool, Bexpression
*);
449 implicit_variable_reference(const std::string
&, const std::string
&, Btype
*);
452 immutable_struct(const std::string
&, const std::string
&,
453 bool, bool, Btype
*, Location
);
456 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
457 Location
, Bexpression
*);
460 immutable_struct_reference(const std::string
&, const std::string
&,
466 label(Bfunction
*, const std::string
& name
, Location
);
469 label_definition_statement(Blabel
*);
472 goto_statement(Blabel
*, Location
);
475 label_address(Blabel
*, Location
);
481 { return this->make_function(error_mark_node
); }
484 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
485 unsigned int flags
, Location
);
488 function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
489 Bexpression
* defer
, Location
);
492 function_set_parameters(Bfunction
* function
, const std::vector
<Bvariable
*>&);
495 function_set_body(Bfunction
* function
, Bstatement
* code_stmt
);
498 lookup_builtin(const std::string
&);
501 write_global_definitions(const std::vector
<Btype
*>&,
502 const std::vector
<Bexpression
*>&,
503 const std::vector
<Bfunction
*>&,
504 const std::vector
<Bvariable
*>&);
507 write_export_data(const char* bytes
, unsigned int size
);
511 // Make a Bexpression from a tree.
513 make_expression(tree t
)
514 { return new Bexpression(t
); }
516 // Make a Bstatement from a tree.
518 make_statement(tree t
)
519 { return new Bstatement(t
); }
521 // Make a Btype from a tree.
524 { return new Btype(t
); }
527 make_function(tree t
)
528 { return new Bfunction(t
); }
531 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
534 fill_in_array(Btype
*, Btype
*, Bexpression
*);
537 non_zero_size_type(tree
);
540 convert_tree(tree
, tree
, Location
);
544 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
545 tree fntype
, bool const_p
, bool noreturn_p
);
547 // A mapping of the GCC built-ins exposed to GCCGo.
548 std::map
<std::string
, Bfunction
*> builtin_functions_
;
551 // A helper function to create a GCC identifier from a C++ string.
554 get_identifier_from_string(const std::string
& str
)
556 return get_identifier_with_length(str
.data(), str
.length());
559 // Define the built-in functions that are exposed to GCCGo.
561 Gcc_backend::Gcc_backend()
563 /* We need to define the fetch_and_add functions, since we use them
565 tree t
= this->integer_type(true, BITS_PER_UNIT
)->get_tree();
566 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
567 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
568 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
571 t
= this->integer_type(true, BITS_PER_UNIT
* 2)->get_tree();
572 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
573 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
574 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
577 t
= this->integer_type(true, BITS_PER_UNIT
* 4)->get_tree();
578 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
579 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
580 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
583 t
= this->integer_type(true, BITS_PER_UNIT
* 8)->get_tree();
584 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
585 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8",
586 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
589 // We use __builtin_expect for magic import functions.
590 this->define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
591 build_function_type_list(long_integer_type_node
,
592 long_integer_type_node
,
593 long_integer_type_node
,
597 // We use __builtin_memcmp for struct comparisons.
598 this->define_builtin(BUILT_IN_MEMCMP
, "__builtin_memcmp", "memcmp",
599 build_function_type_list(integer_type_node
,
606 // Used by runtime/internal/sys.
607 this->define_builtin(BUILT_IN_CTZ
, "__builtin_ctz", "ctz",
608 build_function_type_list(integer_type_node
,
612 this->define_builtin(BUILT_IN_CTZLL
, "__builtin_ctzll", "ctzll",
613 build_function_type_list(integer_type_node
,
614 long_long_unsigned_type_node
,
617 this->define_builtin(BUILT_IN_BSWAP32
, "__builtin_bswap32", "bswap32",
618 build_function_type_list(uint32_type_node
,
622 this->define_builtin(BUILT_IN_BSWAP64
, "__builtin_bswap64", "bswap64",
623 build_function_type_list(uint64_type_node
,
628 // We provide some functions for the math library.
629 tree math_function_type
= build_function_type_list(double_type_node
,
632 tree math_function_type_long
=
633 build_function_type_list(long_double_type_node
, long_double_type_node
,
635 tree math_function_type_two
= build_function_type_list(double_type_node
,
639 tree math_function_type_long_two
=
640 build_function_type_list(long_double_type_node
, long_double_type_node
,
641 long_double_type_node
, NULL_TREE
);
642 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
643 math_function_type
, true, false);
644 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
645 math_function_type_long
, true, false);
646 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
647 math_function_type
, true, false);
648 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
649 math_function_type_long
, true, false);
650 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
651 math_function_type
, true, false);
652 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
653 math_function_type_long
, true, false);
654 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
655 math_function_type_two
, true, false);
656 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
657 math_function_type_long_two
, true, false);
658 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
659 math_function_type
, true, false);
660 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
661 math_function_type_long
, true, false);
662 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
663 math_function_type
, true, false);
664 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
665 math_function_type_long
, true, false);
666 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
667 math_function_type
, true, false);
668 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
669 math_function_type_long
, true, false);
670 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
671 math_function_type
, true, false);
672 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
673 math_function_type_long
, true, false);
674 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
675 math_function_type
, true, false);
676 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
677 math_function_type_long
, true, false);
678 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
679 math_function_type
, true, false);
680 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
681 math_function_type_long
, true, false);
682 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
683 math_function_type_two
, true, false);
684 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
685 math_function_type_long_two
, true, false);
686 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
687 build_function_type_list(double_type_node
,
692 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
693 build_function_type_list(long_double_type_node
,
694 long_double_type_node
,
698 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
699 math_function_type
, true, false);
700 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
701 math_function_type_long
, true, false);
702 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
703 math_function_type
, true, false);
704 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
705 math_function_type_long
, true, false);
706 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
707 math_function_type
, true, false);
708 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
709 math_function_type_long
, true, false);
710 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
711 math_function_type
, true, false);
712 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
713 math_function_type_long
, true, false);
714 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
715 math_function_type
, true, false);
716 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
717 math_function_type_long
, true, false);
718 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
719 math_function_type
, true, false);
720 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
721 math_function_type_long
, true, false);
722 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
723 math_function_type
, true, false);
724 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
725 math_function_type_long
, true, false);
726 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
727 math_function_type
, true, false);
728 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
729 math_function_type_long
, true, false);
731 // We use __builtin_return_address in the thunk we build for
732 // functions which call recover, and for runtime.getcallerpc.
733 t
= build_function_type_list(ptr_type_node
, unsigned_type_node
, NULL_TREE
);
734 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
735 NULL
, t
, false, false);
737 // The runtime calls __builtin_frame_address for runtime.getcallersp.
738 this->define_builtin(BUILT_IN_FRAME_ADDRESS
, "__builtin_frame_address",
739 NULL
, t
, false, false);
741 // The runtime calls __builtin_extract_return_addr when recording
742 // the address to which a function returns.
743 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR
,
744 "__builtin_extract_return_addr", NULL
,
745 build_function_type_list(ptr_type_node
,
750 // The compiler uses __builtin_trap for some exception handling
752 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
753 build_function_type(void_type_node
, void_list_node
),
756 // The runtime uses __builtin_prefetch.
757 this->define_builtin(BUILT_IN_PREFETCH
, "__builtin_prefetch", NULL
,
758 build_varargs_function_type_list(void_type_node
,
763 // The compiler uses __builtin_unreachable for cases that can not
765 this->define_builtin(BUILT_IN_UNREACHABLE
, "__builtin_unreachable", NULL
,
766 build_function_type(void_type_node
, void_list_node
),
770 // Get an unnamed integer type.
773 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
778 if (bits
== INT_TYPE_SIZE
)
779 type
= unsigned_type_node
;
780 else if (bits
== CHAR_TYPE_SIZE
)
781 type
= unsigned_char_type_node
;
782 else if (bits
== SHORT_TYPE_SIZE
)
783 type
= short_unsigned_type_node
;
784 else if (bits
== LONG_TYPE_SIZE
)
785 type
= long_unsigned_type_node
;
786 else if (bits
== LONG_LONG_TYPE_SIZE
)
787 type
= long_long_unsigned_type_node
;
789 type
= make_unsigned_type(bits
);
793 if (bits
== INT_TYPE_SIZE
)
794 type
= integer_type_node
;
795 else if (bits
== CHAR_TYPE_SIZE
)
796 type
= signed_char_type_node
;
797 else if (bits
== SHORT_TYPE_SIZE
)
798 type
= short_integer_type_node
;
799 else if (bits
== LONG_TYPE_SIZE
)
800 type
= long_integer_type_node
;
801 else if (bits
== LONG_LONG_TYPE_SIZE
)
802 type
= long_long_integer_type_node
;
804 type
= make_signed_type(bits
);
806 return this->make_type(type
);
809 // Get an unnamed float type.
812 Gcc_backend::float_type(int bits
)
815 if (bits
== FLOAT_TYPE_SIZE
)
816 type
= float_type_node
;
817 else if (bits
== DOUBLE_TYPE_SIZE
)
818 type
= double_type_node
;
819 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
820 type
= long_double_type_node
;
823 type
= make_node(REAL_TYPE
);
824 TYPE_PRECISION(type
) = bits
;
827 return this->make_type(type
);
830 // Get an unnamed complex type.
833 Gcc_backend::complex_type(int bits
)
836 if (bits
== FLOAT_TYPE_SIZE
* 2)
837 type
= complex_float_type_node
;
838 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
839 type
= complex_double_type_node
;
840 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
841 type
= complex_long_double_type_node
;
844 type
= make_node(REAL_TYPE
);
845 TYPE_PRECISION(type
) = bits
/ 2;
847 type
= build_complex_type(type
);
849 return this->make_type(type
);
852 // Get a pointer type.
855 Gcc_backend::pointer_type(Btype
* to_type
)
857 tree to_type_tree
= to_type
->get_tree();
858 if (to_type_tree
== error_mark_node
)
859 return this->error_type();
860 tree type
= build_pointer_type(to_type_tree
);
861 return this->make_type(type
);
864 // Make a function type.
867 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
868 const std::vector
<Btyped_identifier
>& parameters
,
869 const std::vector
<Btyped_identifier
>& results
,
870 Btype
* result_struct
,
873 tree args
= NULL_TREE
;
875 if (receiver
.btype
!= NULL
)
877 tree t
= receiver
.btype
->get_tree();
878 if (t
== error_mark_node
)
879 return this->error_type();
880 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
881 pp
= &TREE_CHAIN(*pp
);
884 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
885 p
!= parameters
.end();
888 tree t
= p
->btype
->get_tree();
889 if (t
== error_mark_node
)
890 return this->error_type();
891 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
892 pp
= &TREE_CHAIN(*pp
);
895 // Varargs is handled entirely at the Go level. When converted to
896 // GENERIC functions are not varargs.
897 *pp
= void_list_node
;
901 result
= void_type_node
;
902 else if (results
.size() == 1)
903 result
= results
.front().btype
->get_tree();
906 gcc_assert(result_struct
!= NULL
);
907 result
= result_struct
->get_tree();
909 if (result
== error_mark_node
)
910 return this->error_type();
912 // The libffi library can not represent a zero-sized object. To
913 // avoid causing confusion on 32-bit SPARC, we treat a function that
914 // returns a zero-sized value as returning void. That should do no
915 // harm since there is no actual value to be returned. See
916 // https://gcc.gnu.org/PR72814 for details.
917 if (result
!= void_type_node
&& int_size_in_bytes(result
) == 0)
918 result
= void_type_node
;
920 tree fntype
= build_function_type(result
, args
);
921 if (fntype
== error_mark_node
)
922 return this->error_type();
924 return this->make_type(build_pointer_type(fntype
));
927 // Make a struct type.
930 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
932 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
935 // Fill in the fields of a struct type.
938 Gcc_backend::fill_in_struct(Btype
* fill
,
939 const std::vector
<Btyped_identifier
>& fields
)
941 tree fill_tree
= fill
->get_tree();
942 tree field_trees
= NULL_TREE
;
943 tree
* pp
= &field_trees
;
944 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
948 tree name_tree
= get_identifier_from_string(p
->name
);
949 tree type_tree
= p
->btype
->get_tree();
950 if (type_tree
== error_mark_node
)
951 return this->error_type();
952 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
954 DECL_CONTEXT(field
) = fill_tree
;
956 pp
= &DECL_CHAIN(field
);
958 TYPE_FIELDS(fill_tree
) = field_trees
;
959 layout_type(fill_tree
);
961 // Because Go permits converting between named struct types and
962 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
963 // because we don't try to maintain TYPE_CANONICAL for struct types,
964 // we need to tell the middle-end to use structural equality.
965 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
970 // Make an array type.
973 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
975 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
976 element_btype
, length
);
979 // Fill in an array type.
982 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
985 tree element_type_tree
= element_type
->get_tree();
986 tree length_tree
= length
->get_tree();
987 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
988 return this->error_type();
990 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
992 length_tree
= fold_convert(sizetype
, length_tree
);
994 // build_index_type takes the maximum index, which is one less than
996 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
1000 tree fill_tree
= fill
->get_tree();
1001 TREE_TYPE(fill_tree
) = element_type_tree
;
1002 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
1003 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
1004 layout_type(fill_tree
);
1006 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
1007 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1008 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
1009 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
1010 TYPE_CANONICAL(fill_tree
) =
1011 build_array_type(TYPE_CANONICAL(element_type_tree
),
1012 TYPE_CANONICAL(index_type_tree
));
1017 // Create a placeholder for a pointer type.
1020 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
1021 Location location
, bool)
1023 tree ret
= build_distinct_type_copy(ptr_type_node
);
1026 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1027 get_identifier_from_string(name
),
1029 TYPE_NAME(ret
) = decl
;
1031 return this->make_type(ret
);
1034 // Set the real target type for a placeholder pointer type.
1037 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
1040 tree pt
= placeholder
->get_tree();
1041 if (pt
== error_mark_node
)
1043 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
1044 tree tt
= to_type
->get_tree();
1045 if (tt
== error_mark_node
)
1047 placeholder
->set_tree(error_mark_node
);
1050 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
1051 TREE_TYPE(pt
) = TREE_TYPE(tt
);
1052 if (TYPE_NAME(pt
) != NULL_TREE
)
1054 // Build the data structure gcc wants to see for a typedef.
1055 tree copy
= build_variant_type_copy(pt
);
1056 TYPE_NAME(copy
) = NULL_TREE
;
1057 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
1062 // Set the real values for a placeholder function type.
1065 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
1067 return this->set_placeholder_pointer_type(placeholder
, ft
);
1070 // Create a placeholder for a struct type.
1073 Gcc_backend::placeholder_struct_type(const std::string
& name
,
1076 tree ret
= make_node(RECORD_TYPE
);
1079 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1080 get_identifier_from_string(name
),
1082 TYPE_NAME(ret
) = decl
;
1084 return this->make_type(ret
);
1087 // Fill in the fields of a placeholder struct type.
1090 Gcc_backend::set_placeholder_struct_type(
1092 const std::vector
<Btyped_identifier
>& fields
)
1094 tree t
= placeholder
->get_tree();
1095 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
1096 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
1098 if (TYPE_NAME(t
) != NULL_TREE
)
1100 // Build the data structure gcc wants to see for a typedef.
1101 tree copy
= build_variant_type_copy(t
);
1102 TYPE_NAME(copy
) = NULL_TREE
;
1103 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1106 return r
->get_tree() != error_mark_node
;
1109 // Create a placeholder for an array type.
1112 Gcc_backend::placeholder_array_type(const std::string
& name
,
1115 tree ret
= make_node(ARRAY_TYPE
);
1116 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1117 get_identifier_from_string(name
),
1119 TYPE_NAME(ret
) = decl
;
1120 return this->make_type(ret
);
1123 // Fill in the fields of a placeholder array type.
1126 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1127 Btype
* element_btype
,
1128 Bexpression
* length
)
1130 tree t
= placeholder
->get_tree();
1131 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1132 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1134 // Build the data structure gcc wants to see for a typedef.
1135 tree copy
= build_distinct_type_copy(t
);
1136 TYPE_NAME(copy
) = NULL_TREE
;
1137 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1139 return r
->get_tree() != error_mark_node
;
1142 // Return a named version of a type.
1145 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1148 tree type
= btype
->get_tree();
1149 if (type
== error_mark_node
)
1150 return this->error_type();
1152 // The middle-end expects a basic type to have a name. In Go every
1153 // basic type will have a name. The first time we see a basic type,
1154 // give it whatever Go name we have at this point.
1155 if (TYPE_NAME(type
) == NULL_TREE
1156 && location
.gcc_location() == BUILTINS_LOCATION
1157 && (TREE_CODE(type
) == INTEGER_TYPE
1158 || TREE_CODE(type
) == REAL_TYPE
1159 || TREE_CODE(type
) == COMPLEX_TYPE
1160 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1162 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1163 get_identifier_from_string(name
),
1165 TYPE_NAME(type
) = decl
;
1166 return this->make_type(type
);
1169 tree copy
= build_variant_type_copy(type
);
1170 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1171 get_identifier_from_string(name
),
1173 DECL_ORIGINAL_TYPE(decl
) = type
;
1174 TYPE_NAME(copy
) = decl
;
1175 return this->make_type(copy
);
1178 // Return a pointer type used as a marker for a circular type.
1181 Gcc_backend::circular_pointer_type(Btype
*, bool)
1183 return this->make_type(ptr_type_node
);
1186 // Return whether we might be looking at a circular type.
1189 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1191 return btype
->get_tree() == ptr_type_node
;
1194 // Return the size of a type.
1197 Gcc_backend::type_size(Btype
* btype
)
1199 tree t
= btype
->get_tree();
1200 if (t
== error_mark_node
)
1202 if (t
== void_type_node
)
1204 t
= TYPE_SIZE_UNIT(t
);
1205 gcc_assert(tree_fits_uhwi_p (t
));
1206 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1207 int64_t ret
= static_cast<int64_t>(val_wide
);
1208 if (ret
< 0 || static_cast<unsigned HOST_WIDE_INT
>(ret
) != val_wide
)
1213 // Return the alignment of a type.
1216 Gcc_backend::type_alignment(Btype
* btype
)
1218 tree t
= btype
->get_tree();
1219 if (t
== error_mark_node
)
1221 return TYPE_ALIGN_UNIT(t
);
1224 // Return the alignment of a struct field of type BTYPE.
1227 Gcc_backend::type_field_alignment(Btype
* btype
)
1229 tree t
= btype
->get_tree();
1230 if (t
== error_mark_node
)
1232 return go_field_alignment(t
);
1235 // Return the offset of a field in a struct.
1238 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1240 tree struct_tree
= btype
->get_tree();
1241 if (struct_tree
== error_mark_node
)
1243 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1244 tree field
= TYPE_FIELDS(struct_tree
);
1245 for (; index
> 0; --index
)
1247 field
= DECL_CHAIN(field
);
1248 gcc_assert(field
!= NULL_TREE
);
1250 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1251 int64_t ret
= static_cast<int64_t>(offset_wide
);
1252 gcc_assert(ret
== offset_wide
);
1256 // Return the zero value for a type.
1259 Gcc_backend::zero_expression(Btype
* btype
)
1261 tree t
= btype
->get_tree();
1263 if (t
== error_mark_node
)
1264 ret
= error_mark_node
;
1266 ret
= build_zero_cst(t
);
1267 return this->make_expression(ret
);
1270 // An expression that references a variable.
1273 Gcc_backend::var_expression(Bvariable
* var
, Location location
)
1275 tree ret
= var
->get_tree(location
);
1276 if (ret
== error_mark_node
)
1277 return this->error_expression();
1278 return this->make_expression(ret
);
1281 // An expression that indirectly references an expression.
1284 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1285 bool known_valid
, Location location
)
1287 tree expr_tree
= expr
->get_tree();
1288 tree type_tree
= btype
->get_tree();
1289 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1290 return this->error_expression();
1292 // If the type of EXPR is a recursive pointer type, then we
1293 // need to insert a cast before indirecting.
1294 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1295 if (VOID_TYPE_P(target_type_tree
))
1296 expr_tree
= fold_convert_loc(location
.gcc_location(),
1297 build_pointer_type(type_tree
), expr_tree
);
1299 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1302 TREE_THIS_NOTRAP(ret
) = 1;
1303 return this->make_expression(ret
);
1306 // Return an expression that declares a constant named NAME with the
1307 // constant value VAL in BTYPE.
1310 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1311 Bexpression
* val
, Location location
)
1313 tree type_tree
= btype
->get_tree();
1314 tree const_val
= val
->get_tree();
1315 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1316 return this->error_expression();
1318 tree name_tree
= get_identifier_from_string(name
);
1319 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1321 DECL_INITIAL(decl
) = const_val
;
1322 TREE_CONSTANT(decl
) = 1;
1323 TREE_READONLY(decl
) = 1;
1325 go_preserve_from_gc(decl
);
1326 return this->make_expression(decl
);
1329 // Return a typed value as a constant integer.
1332 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1334 tree t
= btype
->get_tree();
1335 if (t
== error_mark_node
)
1336 return this->error_expression();
1338 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1339 return this->make_expression(ret
);
1342 // Return a typed value as a constant floating-point number.
1345 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1347 tree t
= btype
->get_tree();
1349 if (t
== error_mark_node
)
1350 return this->error_expression();
1353 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1355 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1356 ret
= build_real(t
, r2
);
1357 return this->make_expression(ret
);
1360 // Return a typed real and imaginary value as a constant complex number.
1363 Gcc_backend::complex_constant_expression(Btype
* btype
, mpc_t val
)
1365 tree t
= btype
->get_tree();
1367 if (t
== error_mark_node
)
1368 return this->error_expression();
1371 real_from_mpfr(&r1
, mpc_realref(val
), TREE_TYPE(t
), GMP_RNDN
);
1373 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1376 real_from_mpfr(&r3
, mpc_imagref(val
), TREE_TYPE(t
), GMP_RNDN
);
1378 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1380 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1381 build_real(TREE_TYPE(t
), r4
));
1382 return this->make_expression(ret
);
1385 // Make a constant string expression.
1388 Gcc_backend::string_constant_expression(const std::string
& val
)
1390 tree index_type
= build_index_type(size_int(val
.length()));
1391 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1393 tree string_type
= build_array_type(const_char_type
, index_type
);
1394 TYPE_STRING_FLAG(string_type
) = 1;
1395 tree string_val
= build_string(val
.length(), val
.data());
1396 TREE_TYPE(string_val
) = string_type
;
1398 return this->make_expression(string_val
);
1401 // Make a constant boolean expression.
1404 Gcc_backend::boolean_constant_expression(bool val
)
1406 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1407 return this->make_expression(bool_cst
);
1410 // Return the real part of a complex expression.
1413 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1415 tree complex_tree
= bcomplex
->get_tree();
1416 if (complex_tree
== error_mark_node
)
1417 return this->error_expression();
1418 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1419 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1420 TREE_TYPE(TREE_TYPE(complex_tree
)),
1422 return this->make_expression(ret
);
1425 // Return the imaginary part of a complex expression.
1428 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1430 tree complex_tree
= bcomplex
->get_tree();
1431 if (complex_tree
== error_mark_node
)
1432 return this->error_expression();
1433 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1434 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1435 TREE_TYPE(TREE_TYPE(complex_tree
)),
1437 return this->make_expression(ret
);
1440 // Make a complex expression given its real and imaginary parts.
1443 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1446 tree real_tree
= breal
->get_tree();
1447 tree imag_tree
= bimag
->get_tree();
1448 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1449 return this->error_expression();
1450 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1451 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1452 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1453 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1454 build_complex_type(TREE_TYPE(real_tree
)),
1455 real_tree
, imag_tree
);
1456 return this->make_expression(ret
);
1459 // An expression that converts an expression to a different type.
1462 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1465 tree type_tree
= type
->get_tree();
1466 tree expr_tree
= expr
->get_tree();
1467 if (type_tree
== error_mark_node
1468 || expr_tree
== error_mark_node
1469 || TREE_TYPE(expr_tree
) == error_mark_node
)
1470 return this->error_expression();
1473 if (this->type_size(type
) == 0
1474 || TREE_TYPE(expr_tree
) == void_type_node
)
1476 // Do not convert zero-sized types.
1479 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1480 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1481 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1482 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1483 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1484 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1485 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1486 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1487 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1488 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1489 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1490 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1491 type_tree
, expr_tree
);
1493 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1495 return this->make_expression(ret
);
1498 // Get the address of a function.
1501 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1503 tree func
= bfunc
->get_tree();
1504 if (func
== error_mark_node
)
1505 return this->error_expression();
1507 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1508 return this->make_expression(ret
);
1511 // Get the address of an expression.
1514 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1516 tree expr
= bexpr
->get_tree();
1517 if (expr
== error_mark_node
)
1518 return this->error_expression();
1520 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1521 return this->make_expression(ret
);
1524 // Return an expression for the field at INDEX in BSTRUCT.
1527 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1530 tree struct_tree
= bstruct
->get_tree();
1531 if (struct_tree
== error_mark_node
1532 || TREE_TYPE(struct_tree
) == error_mark_node
)
1533 return this->error_expression();
1534 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1535 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1536 if (field
== NULL_TREE
)
1538 // This can happen for a type which refers to itself indirectly
1539 // and then turns out to be erroneous.
1540 return this->error_expression();
1542 for (unsigned int i
= index
; i
> 0; --i
)
1544 field
= DECL_CHAIN(field
);
1545 gcc_assert(field
!= NULL_TREE
);
1547 if (TREE_TYPE(field
) == error_mark_node
)
1548 return this->error_expression();
1549 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1550 TREE_TYPE(field
), struct_tree
, field
,
1552 if (TREE_CONSTANT(struct_tree
))
1553 TREE_CONSTANT(ret
) = 1;
1554 return this->make_expression(ret
);
1557 // Return an expression that executes BSTAT before BEXPR.
1560 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1563 tree stat
= bstat
->get_tree();
1564 tree expr
= bexpr
->get_tree();
1565 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1566 return this->error_expression();
1567 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1568 TREE_TYPE(expr
), stat
, expr
);
1569 return this->make_expression(ret
);
1572 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1573 // ELSE_EXPR otherwise.
1576 Gcc_backend::conditional_expression(Bfunction
*, Btype
* btype
,
1577 Bexpression
* condition
,
1578 Bexpression
* then_expr
,
1579 Bexpression
* else_expr
, Location location
)
1581 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1582 tree cond_tree
= condition
->get_tree();
1583 tree then_tree
= then_expr
->get_tree();
1584 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1585 if (type_tree
== error_mark_node
1586 || cond_tree
== error_mark_node
1587 || then_tree
== error_mark_node
1588 || else_tree
== error_mark_node
)
1589 return this->error_expression();
1590 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1591 cond_tree
, then_tree
, else_tree
);
1592 return this->make_expression(ret
);
1595 // Return an expression for the unary operation OP EXPR.
1598 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1600 tree expr_tree
= expr
->get_tree();
1601 if (expr_tree
== error_mark_node
1602 || TREE_TYPE(expr_tree
) == error_mark_node
)
1603 return this->error_expression();
1605 tree type_tree
= TREE_TYPE(expr_tree
);
1606 enum tree_code code
;
1609 case OPERATOR_MINUS
:
1611 tree computed_type
= excess_precision_type(type_tree
);
1612 if (computed_type
!= NULL_TREE
)
1614 expr_tree
= convert(computed_type
, expr_tree
);
1615 type_tree
= computed_type
;
1621 code
= TRUTH_NOT_EXPR
;
1624 code
= BIT_NOT_EXPR
;
1631 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1633 return this->make_expression(ret
);
1636 // Convert a gofrontend operator to an equivalent tree_code.
1638 static enum tree_code
1639 operator_to_tree_code(Operator op
, tree type
)
1641 enum tree_code code
;
1647 case OPERATOR_NOTEQ
:
1663 code
= TRUTH_ORIF_EXPR
;
1665 case OPERATOR_ANDAND
:
1666 code
= TRUTH_ANDIF_EXPR
;
1671 case OPERATOR_MINUS
:
1675 code
= BIT_IOR_EXPR
;
1678 code
= BIT_XOR_EXPR
;
1684 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1687 code
= TRUNC_DIV_EXPR
;
1690 code
= TRUNC_MOD_EXPR
;
1692 case OPERATOR_LSHIFT
:
1695 case OPERATOR_RSHIFT
:
1699 code
= BIT_AND_EXPR
;
1701 case OPERATOR_BITCLEAR
:
1702 code
= BIT_AND_EXPR
;
1711 // Return an expression for the binary operation LEFT OP RIGHT.
1714 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1715 Bexpression
* right
, Location location
)
1717 tree left_tree
= left
->get_tree();
1718 tree right_tree
= right
->get_tree();
1719 if (left_tree
== error_mark_node
1720 || right_tree
== error_mark_node
)
1721 return this->error_expression();
1722 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1724 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1725 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1726 tree computed_type
= excess_precision_type(type_tree
);
1727 if (computed_type
!= NULL_TREE
)
1729 left_tree
= convert(computed_type
, left_tree
);
1730 right_tree
= convert(computed_type
, right_tree
);
1731 type_tree
= computed_type
;
1734 // For comparison operators, the resulting type should be boolean.
1738 case OPERATOR_NOTEQ
:
1743 type_tree
= boolean_type_node
;
1749 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1750 left_tree
, right_tree
);
1751 return this->make_expression(ret
);
1754 // Return an expression that constructs BTYPE with VALS.
1757 Gcc_backend::constructor_expression(Btype
* btype
,
1758 const std::vector
<Bexpression
*>& vals
,
1761 tree type_tree
= btype
->get_tree();
1762 if (type_tree
== error_mark_node
)
1763 return this->error_expression();
1765 vec
<constructor_elt
, va_gc
> *init
;
1766 vec_alloc(init
, vals
.size());
1768 tree sink
= NULL_TREE
;
1769 bool is_constant
= true;
1770 tree field
= TYPE_FIELDS(type_tree
);
1771 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1773 ++p
, field
= DECL_CHAIN(field
))
1775 gcc_assert(field
!= NULL_TREE
);
1776 tree val
= (*p
)->get_tree();
1777 if (TREE_TYPE(field
) == error_mark_node
1778 || val
== error_mark_node
1779 || TREE_TYPE(val
) == error_mark_node
)
1780 return this->error_expression();
1782 if (int_size_in_bytes(TREE_TYPE(field
)) == 0)
1784 // GIMPLE cannot represent indices of zero-sized types so
1785 // trying to construct a map with zero-sized keys might lead
1786 // to errors. Instead, we evaluate each expression that
1787 // would have been added as a map element for its
1788 // side-effects and construct an empty map.
1789 append_to_statement_list(val
, &sink
);
1793 constructor_elt empty
= {NULL
, NULL
};
1794 constructor_elt
* elt
= init
->quick_push(empty
);
1796 elt
->value
= this->convert_tree(TREE_TYPE(field
), val
, location
);
1797 if (!TREE_CONSTANT(elt
->value
))
1798 is_constant
= false;
1800 gcc_assert(field
== NULL_TREE
);
1801 tree ret
= build_constructor(type_tree
, init
);
1803 TREE_CONSTANT(ret
) = 1;
1804 if (sink
!= NULL_TREE
)
1805 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1806 type_tree
, sink
, ret
);
1807 return this->make_expression(ret
);
1811 Gcc_backend::array_constructor_expression(
1812 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1813 const std::vector
<Bexpression
*>& vals
, Location location
)
1815 tree type_tree
= array_btype
->get_tree();
1816 if (type_tree
== error_mark_node
)
1817 return this->error_expression();
1819 gcc_assert(indexes
.size() == vals
.size());
1821 tree element_type
= TREE_TYPE(type_tree
);
1822 HOST_WIDE_INT element_size
= int_size_in_bytes(element_type
);
1823 vec
<constructor_elt
, va_gc
> *init
;
1824 vec_alloc(init
, element_size
== 0 ? 0 : vals
.size());
1826 tree sink
= NULL_TREE
;
1827 bool is_constant
= true;
1828 for (size_t i
= 0; i
< vals
.size(); ++i
)
1830 tree index
= size_int(indexes
[i
]);
1831 tree val
= (vals
[i
])->get_tree();
1833 if (index
== error_mark_node
1834 || val
== error_mark_node
)
1835 return this->error_expression();
1837 if (element_size
== 0)
1839 // GIMPLE cannot represent arrays of zero-sized types so trying
1840 // to construct an array of zero-sized values might lead to errors.
1841 // Instead, we evaluate each expression that would have been added as
1842 // an array value for its side-effects and construct an empty array.
1843 append_to_statement_list(val
, &sink
);
1847 if (!TREE_CONSTANT(val
))
1848 is_constant
= false;
1850 constructor_elt empty
= {NULL
, NULL
};
1851 constructor_elt
* elt
= init
->quick_push(empty
);
1856 tree ret
= build_constructor(type_tree
, init
);
1858 TREE_CONSTANT(ret
) = 1;
1859 if (sink
!= NULL_TREE
)
1860 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1861 type_tree
, sink
, ret
);
1862 return this->make_expression(ret
);
1865 // Return an expression for the address of BASE[INDEX].
1868 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
1871 tree base_tree
= base
->get_tree();
1872 tree index_tree
= index
->get_tree();
1873 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
1874 if (base_tree
== error_mark_node
1875 || TREE_TYPE(base_tree
) == error_mark_node
1876 || index_tree
== error_mark_node
1877 || element_type_tree
== error_mark_node
)
1878 return this->error_expression();
1880 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
1881 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
1882 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
1883 index_tree
, element_size
);
1884 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
1885 TREE_TYPE(base_tree
), base_tree
, offset
);
1886 return this->make_expression(ptr
);
1889 // Return an expression representing ARRAY[INDEX]
1892 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
1895 tree array_tree
= array
->get_tree();
1896 tree index_tree
= index
->get_tree();
1897 if (array_tree
== error_mark_node
1898 || TREE_TYPE(array_tree
) == error_mark_node
1899 || index_tree
== error_mark_node
)
1900 return this->error_expression();
1902 // A function call that returns a zero sized object will have been
1903 // changed to return void. If we see void here, assume we are
1904 // dealing with a zero sized type and just evaluate the operands.
1906 if (TREE_TYPE(array_tree
) != void_type_node
)
1907 ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
1908 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
1909 index_tree
, NULL_TREE
, NULL_TREE
);
1911 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1912 void_type_node
, array_tree
, index_tree
);
1914 return this->make_expression(ret
);
1917 // Create an expression for a call to FN_EXPR with FN_ARGS.
1919 Gcc_backend::call_expression(Bfunction
*, // containing fcn for call
1920 Bexpression
* fn_expr
,
1921 const std::vector
<Bexpression
*>& fn_args
,
1922 Bexpression
* chain_expr
,
1925 tree fn
= fn_expr
->get_tree();
1926 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
1927 return this->error_expression();
1929 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
1930 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
1932 size_t nargs
= fn_args
.size();
1933 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
1934 for (size_t i
= 0; i
< nargs
; ++i
)
1936 args
[i
] = fn_args
.at(i
)->get_tree();
1937 if (args
[i
] == error_mark_node
)
1938 return this->error_expression();
1942 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
1943 fndecl
= TREE_OPERAND(fndecl
, 0);
1945 // This is to support builtin math functions when using 80387 math.
1946 tree excess_type
= NULL_TREE
;
1948 && TREE_CODE(fndecl
) == FUNCTION_DECL
1949 && fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
1950 && DECL_IS_BUILTIN (fndecl
)
1952 && ((SCALAR_FLOAT_TYPE_P(rettype
)
1953 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
1954 || (COMPLEX_FLOAT_TYPE_P(rettype
)
1955 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
1957 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
1958 if (excess_type
!= NULL_TREE
)
1960 tree excess_fndecl
= mathfn_built_in(excess_type
,
1961 DECL_FUNCTION_CODE(fndecl
));
1962 if (excess_fndecl
== NULL_TREE
)
1963 excess_type
= NULL_TREE
;
1966 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
1968 for (size_t i
= 0; i
< nargs
; ++i
)
1970 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
1971 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
1972 args
[i
] = ::convert(excess_type
, args
[i
]);
1979 build_call_array_loc(location
.gcc_location(),
1980 excess_type
!= NULL_TREE
? excess_type
: rettype
,
1984 CALL_EXPR_STATIC_CHAIN (ret
) = chain_expr
->get_tree();
1986 if (excess_type
!= NULL_TREE
)
1988 // Calling convert here can undo our excess precision change.
1989 // That may or may not be a bug in convert_to_real.
1990 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
1994 return this->make_expression(ret
);
1997 // An expression as a statement.
2000 Gcc_backend::expression_statement(Bfunction
*, Bexpression
* expr
)
2002 return this->make_statement(expr
->get_tree());
2005 // Variable initialization.
2008 Gcc_backend::init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
)
2010 tree var_tree
= var
->get_decl();
2011 tree init_tree
= init
->get_tree();
2012 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
2013 return this->error_statement();
2014 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
2016 // To avoid problems with GNU ld, we don't make zero-sized
2017 // externally visible variables. That might lead us to doing an
2018 // initialization of a zero-sized expression to a non-zero sized
2019 // variable, or vice-versa. Avoid crashes by omitting the
2020 // initializer. Such initializations don't mean anything anyhow.
2021 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
2022 && init_tree
!= NULL_TREE
2023 && TREE_TYPE(init_tree
) != void_type_node
2024 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
2026 DECL_INITIAL(var_tree
) = init_tree
;
2027 init_tree
= NULL_TREE
;
2030 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
2031 void_type_node
, var_tree
);
2032 if (init_tree
!= NULL_TREE
)
2033 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
2034 void_type_node
, init_tree
, ret
);
2036 return this->make_statement(ret
);
2042 Gcc_backend::assignment_statement(Bfunction
* bfn
, Bexpression
* lhs
,
2043 Bexpression
* rhs
, Location location
)
2045 tree lhs_tree
= lhs
->get_tree();
2046 tree rhs_tree
= rhs
->get_tree();
2047 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
2048 return this->error_statement();
2050 // To avoid problems with GNU ld, we don't make zero-sized
2051 // externally visible variables. That might lead us to doing an
2052 // assignment of a zero-sized expression to a non-zero sized
2053 // expression; avoid crashes here by avoiding assignments of
2054 // zero-sized expressions. Such assignments don't really mean
2056 if (TREE_TYPE(lhs_tree
) == void_type_node
2057 || int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
2058 || TREE_TYPE(rhs_tree
) == void_type_node
2059 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
2060 return this->compound_statement(this->expression_statement(bfn
, lhs
),
2061 this->expression_statement(bfn
, rhs
));
2063 rhs_tree
= this->convert_tree(TREE_TYPE(lhs_tree
), rhs_tree
, location
);
2065 return this->make_statement(fold_build2_loc(location
.gcc_location(),
2068 lhs_tree
, rhs_tree
));
2074 Gcc_backend::return_statement(Bfunction
* bfunction
,
2075 const std::vector
<Bexpression
*>& vals
,
2078 tree fntree
= bfunction
->get_tree();
2079 if (fntree
== error_mark_node
)
2080 return this->error_statement();
2081 tree result
= DECL_RESULT(fntree
);
2082 if (result
== error_mark_node
)
2083 return this->error_statement();
2085 // If the result size is zero bytes, we have set the function type
2086 // to have a result type of void, so don't return anything.
2087 // See the function_type method.
2088 tree res_type
= TREE_TYPE(result
);
2089 if (res_type
== void_type_node
|| int_size_in_bytes(res_type
) == 0)
2091 tree stmt_list
= NULL_TREE
;
2092 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2096 tree val
= (*p
)->get_tree();
2097 if (val
== error_mark_node
)
2098 return this->error_statement();
2099 append_to_statement_list(val
, &stmt_list
);
2101 tree ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2102 void_type_node
, NULL_TREE
);
2103 append_to_statement_list(ret
, &stmt_list
);
2104 return this->make_statement(stmt_list
);
2109 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
2111 else if (vals
.size() == 1)
2113 tree val
= vals
.front()->get_tree();
2114 if (val
== error_mark_node
)
2115 return this->error_statement();
2116 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2117 void_type_node
, result
,
2118 vals
.front()->get_tree());
2119 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2120 void_type_node
, set
);
2124 // To return multiple values, copy the values into a temporary
2125 // variable of the right structure type, and then assign the
2126 // temporary variable to the DECL_RESULT in the return
2128 tree stmt_list
= NULL_TREE
;
2129 tree rettype
= TREE_TYPE(result
);
2131 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2132 push_struct_function(fntree
);
2134 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2135 tree rettmp
= create_tmp_var(rettype
, "RESULT");
2138 tree field
= TYPE_FIELDS(rettype
);
2139 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2141 p
++, field
= DECL_CHAIN(field
))
2143 gcc_assert(field
!= NULL_TREE
);
2144 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
2145 TREE_TYPE(field
), rettmp
, field
,
2147 tree val
= (*p
)->get_tree();
2148 if (val
== error_mark_node
)
2149 return this->error_statement();
2150 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2152 ref
, (*p
)->get_tree());
2153 append_to_statement_list(set
, &stmt_list
);
2155 gcc_assert(field
== NULL_TREE
);
2156 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2159 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2160 void_type_node
, set
);
2161 append_to_statement_list(ret_expr
, &stmt_list
);
2164 return this->make_statement(ret
);
2167 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2168 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2169 // NULL, it will always be executed. This is used for handling defers in Go
2170 // functions. In C++, the resulting code is of this form:
2171 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2174 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2175 Bstatement
* except_stmt
,
2176 Bstatement
* finally_stmt
,
2179 tree stat_tree
= bstat
->get_tree();
2180 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2181 tree finally_tree
= finally_stmt
== NULL
2183 : finally_stmt
->get_tree();
2185 if (stat_tree
== error_mark_node
2186 || except_tree
== error_mark_node
2187 || finally_tree
== error_mark_node
)
2188 return this->error_statement();
2190 if (except_tree
!= NULL_TREE
)
2191 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2192 void_type_node
, stat_tree
,
2193 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2194 void_type_node
, NULL
, except_tree
));
2195 if (finally_tree
!= NULL_TREE
)
2196 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2197 void_type_node
, stat_tree
, finally_tree
);
2198 return this->make_statement(stat_tree
);
2204 Gcc_backend::if_statement(Bfunction
*, Bexpression
* condition
,
2205 Bblock
* then_block
, Bblock
* else_block
,
2208 tree cond_tree
= condition
->get_tree();
2209 tree then_tree
= then_block
->get_tree();
2210 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2211 if (cond_tree
== error_mark_node
2212 || then_tree
== error_mark_node
2213 || else_tree
== error_mark_node
)
2214 return this->error_statement();
2215 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2216 cond_tree
, then_tree
, else_tree
);
2217 return this->make_statement(ret
);
2223 Gcc_backend::switch_statement(
2224 Bfunction
* function
,
2226 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2227 const std::vector
<Bstatement
*>& statements
,
2228 Location switch_location
)
2230 gcc_assert(cases
.size() == statements
.size());
2232 tree decl
= function
->get_tree();
2233 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2234 push_struct_function(decl
);
2236 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2238 tree stmt_list
= NULL_TREE
;
2239 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2240 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2241 ps
!= statements
.end();
2246 location_t loc
= (*ps
!= NULL
2247 ? EXPR_LOCATION((*ps
)->get_tree())
2248 : UNKNOWN_LOCATION
);
2249 tree label
= create_artificial_label(loc
);
2250 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2251 append_to_statement_list(c
, &stmt_list
);
2255 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2259 tree t
= (*pcv
)->get_tree();
2260 if (t
== error_mark_node
)
2261 return this->error_statement();
2262 location_t loc
= EXPR_LOCATION(t
);
2263 tree label
= create_artificial_label(loc
);
2264 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2265 append_to_statement_list(c
, &stmt_list
);
2271 tree t
= (*ps
)->get_tree();
2272 if (t
== error_mark_node
)
2273 return this->error_statement();
2274 append_to_statement_list(t
, &stmt_list
);
2279 tree tv
= value
->get_tree();
2280 if (tv
== error_mark_node
)
2281 return this->error_statement();
2282 tree t
= build2_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2283 NULL_TREE
, tv
, stmt_list
);
2284 return this->make_statement(t
);
2287 // Pair of statements.
2290 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2292 tree stmt_list
= NULL_TREE
;
2293 tree t
= s1
->get_tree();
2294 if (t
== error_mark_node
)
2295 return this->error_statement();
2296 append_to_statement_list(t
, &stmt_list
);
2298 if (t
== error_mark_node
)
2299 return this->error_statement();
2300 append_to_statement_list(t
, &stmt_list
);
2302 // If neither statement has any side effects, stmt_list can be NULL
2304 if (stmt_list
== NULL_TREE
)
2305 stmt_list
= integer_zero_node
;
2307 return this->make_statement(stmt_list
);
2310 // List of statements.
2313 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2315 tree stmt_list
= NULL_TREE
;
2316 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2317 p
!= statements
.end();
2320 tree t
= (*p
)->get_tree();
2321 if (t
== error_mark_node
)
2322 return this->error_statement();
2323 append_to_statement_list(t
, &stmt_list
);
2325 return this->make_statement(stmt_list
);
2328 // Make a block. For some reason gcc uses a dual structure for
2329 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2330 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2334 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2335 const std::vector
<Bvariable
*>& vars
,
2336 Location start_location
,
2339 tree block_tree
= make_node(BLOCK
);
2340 if (enclosing
== NULL
)
2342 tree fndecl
= function
->get_tree();
2343 gcc_assert(fndecl
!= NULL_TREE
);
2345 // We may have already created a block for local variables when
2346 // we take the address of a parameter.
2347 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2349 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2350 DECL_INITIAL(fndecl
) = block_tree
;
2354 tree superblock_tree
= DECL_INITIAL(fndecl
);
2355 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2357 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2359 pp
= &BLOCK_CHAIN(*pp
))
2366 tree superbind_tree
= enclosing
->get_tree();
2367 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2368 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2370 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2372 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2374 pp
= &BLOCK_CHAIN(*pp
))
2379 tree
* pp
= &BLOCK_VARS(block_tree
);
2380 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2384 *pp
= (*pv
)->get_decl();
2385 if (*pp
!= error_mark_node
)
2386 pp
= &DECL_CHAIN(*pp
);
2390 TREE_USED(block_tree
) = 1;
2392 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2393 void_type_node
, BLOCK_VARS(block_tree
),
2394 NULL_TREE
, block_tree
);
2395 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2396 return new Bblock(bind_tree
);
2399 // Add statements to a block.
2402 Gcc_backend::block_add_statements(Bblock
* bblock
,
2403 const std::vector
<Bstatement
*>& statements
)
2405 tree stmt_list
= NULL_TREE
;
2406 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2407 p
!= statements
.end();
2410 tree s
= (*p
)->get_tree();
2411 if (s
!= error_mark_node
)
2412 append_to_statement_list(s
, &stmt_list
);
2415 tree bind_tree
= bblock
->get_tree();
2416 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2417 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2420 // Return a block as a statement.
2423 Gcc_backend::block_statement(Bblock
* bblock
)
2425 tree bind_tree
= bblock
->get_tree();
2426 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2427 return this->make_statement(bind_tree
);
2430 // This is not static because we declare it with GTY(()) in go-c.h.
2431 tree go_non_zero_struct
;
2433 // Return a type corresponding to TYPE with non-zero size.
2436 Gcc_backend::non_zero_size_type(tree type
)
2438 if (int_size_in_bytes(type
) != 0)
2441 switch (TREE_CODE(type
))
2444 if (TYPE_FIELDS(type
) != NULL_TREE
)
2446 tree ns
= make_node(RECORD_TYPE
);
2447 tree field_trees
= NULL_TREE
;
2448 tree
*pp
= &field_trees
;
2449 for (tree field
= TYPE_FIELDS(type
);
2451 field
= DECL_CHAIN(field
))
2453 tree ft
= TREE_TYPE(field
);
2454 if (field
== TYPE_FIELDS(type
))
2455 ft
= non_zero_size_type(ft
);
2456 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2457 DECL_NAME(field
), ft
);
2458 DECL_CONTEXT(f
) = ns
;
2460 pp
= &DECL_CHAIN(f
);
2462 TYPE_FIELDS(ns
) = field_trees
;
2467 if (go_non_zero_struct
== NULL_TREE
)
2469 type
= make_node(RECORD_TYPE
);
2470 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2471 get_identifier("dummy"),
2473 DECL_CONTEXT(field
) = type
;
2474 TYPE_FIELDS(type
) = field
;
2476 go_non_zero_struct
= type
;
2478 return go_non_zero_struct
;
2482 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2483 return build_array_type_nelts(element_type
, 1);
2493 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type
2494 // can be created multiple times and thus have multiple tree
2495 // representations. Make sure this does not confuse the middle-end.
2498 Gcc_backend::convert_tree(tree type_tree
, tree expr_tree
, Location location
)
2500 if (type_tree
== TREE_TYPE(expr_tree
))
2503 if (type_tree
== error_mark_node
2504 || expr_tree
== error_mark_node
2505 || TREE_TYPE(expr_tree
) == error_mark_node
)
2506 return error_mark_node
;
2508 gcc_assert(TREE_CODE(type_tree
) == TREE_CODE(TREE_TYPE(expr_tree
)));
2509 if (POINTER_TYPE_P(type_tree
)
2510 || INTEGRAL_TYPE_P(type_tree
)
2511 || SCALAR_FLOAT_TYPE_P(type_tree
)
2512 || COMPLEX_FLOAT_TYPE_P(type_tree
))
2513 return fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
2514 else if (TREE_CODE(type_tree
) == RECORD_TYPE
2515 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
2517 gcc_assert(int_size_in_bytes(type_tree
)
2518 == int_size_in_bytes(TREE_TYPE(expr_tree
)));
2519 if (TYPE_MAIN_VARIANT(type_tree
)
2520 == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
2521 return fold_build1_loc(location
.gcc_location(), NOP_EXPR
,
2522 type_tree
, expr_tree
);
2523 return fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
2524 type_tree
, expr_tree
);
2530 // Make a global variable.
2533 Gcc_backend::global_variable(const std::string
& var_name
,
2534 const std::string
& asm_name
,
2538 bool in_unique_section
,
2541 tree type_tree
= btype
->get_tree();
2542 if (type_tree
== error_mark_node
)
2543 return this->error_variable();
2545 // The GNU linker does not like dynamic variables with zero size.
2546 tree orig_type_tree
= type_tree
;
2547 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2548 type_tree
= this->non_zero_size_type(type_tree
);
2550 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2551 get_identifier_from_string(var_name
),
2554 DECL_EXTERNAL(decl
) = 1;
2556 TREE_STATIC(decl
) = 1;
2559 TREE_PUBLIC(decl
) = 1;
2560 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2564 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2567 TREE_USED(decl
) = 1;
2569 if (in_unique_section
)
2570 resolve_unique_section (decl
, 0, 1);
2572 go_preserve_from_gc(decl
);
2574 return new Bvariable(decl
, orig_type_tree
);
2577 // Set the initial value of a global variable.
2580 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2582 tree expr_tree
= expr
->get_tree();
2583 if (expr_tree
== error_mark_node
)
2585 gcc_assert(TREE_CONSTANT(expr_tree
));
2586 tree var_decl
= var
->get_decl();
2587 if (var_decl
== error_mark_node
)
2589 DECL_INITIAL(var_decl
) = expr_tree
;
2591 // If this variable goes in a unique section, it may need to go into
2592 // a different one now that DECL_INITIAL is set.
2593 if (symtab_node::get(var_decl
)
2594 && symtab_node::get(var_decl
)->implicit_section
)
2596 set_decl_section_name (var_decl
, NULL
);
2597 resolve_unique_section (var_decl
,
2598 compute_reloc_for_constant (expr_tree
),
2603 // Make a local variable.
2606 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2607 Btype
* btype
, Bvariable
* decl_var
,
2608 bool is_address_taken
, Location location
)
2610 tree type_tree
= btype
->get_tree();
2611 if (type_tree
== error_mark_node
)
2612 return this->error_variable();
2613 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2614 get_identifier_from_string(name
),
2616 DECL_CONTEXT(decl
) = function
->get_tree();
2617 TREE_USED(decl
) = 1;
2618 if (is_address_taken
)
2619 TREE_ADDRESSABLE(decl
) = 1;
2620 if (decl_var
!= NULL
)
2622 DECL_HAS_VALUE_EXPR_P(decl
) = 1;
2623 SET_DECL_VALUE_EXPR(decl
, decl_var
->get_decl());
2625 go_preserve_from_gc(decl
);
2626 return new Bvariable(decl
);
2629 // Make a function parameter variable.
2632 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2633 Btype
* btype
, bool is_address_taken
,
2636 tree type_tree
= btype
->get_tree();
2637 if (type_tree
== error_mark_node
)
2638 return this->error_variable();
2639 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2640 get_identifier_from_string(name
),
2642 DECL_CONTEXT(decl
) = function
->get_tree();
2643 DECL_ARG_TYPE(decl
) = type_tree
;
2644 TREE_USED(decl
) = 1;
2645 if (is_address_taken
)
2646 TREE_ADDRESSABLE(decl
) = 1;
2647 go_preserve_from_gc(decl
);
2648 return new Bvariable(decl
);
2651 // Make a static chain variable.
2654 Gcc_backend::static_chain_variable(Bfunction
* function
, const std::string
& name
,
2655 Btype
* btype
, Location location
)
2657 tree type_tree
= btype
->get_tree();
2658 if (type_tree
== error_mark_node
)
2659 return this->error_variable();
2660 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2661 get_identifier_from_string(name
), type_tree
);
2662 tree fndecl
= function
->get_tree();
2663 DECL_CONTEXT(decl
) = fndecl
;
2664 DECL_ARG_TYPE(decl
) = type_tree
;
2665 TREE_USED(decl
) = 1;
2666 DECL_ARTIFICIAL(decl
) = 1;
2667 DECL_IGNORED_P(decl
) = 1;
2668 TREE_READONLY(decl
) = 1;
2670 struct function
*f
= DECL_STRUCT_FUNCTION(fndecl
);
2673 push_struct_function(fndecl
);
2675 f
= DECL_STRUCT_FUNCTION(fndecl
);
2677 gcc_assert(f
->static_chain_decl
== NULL
);
2678 f
->static_chain_decl
= decl
;
2679 DECL_STATIC_CHAIN(fndecl
) = 1;
2681 go_preserve_from_gc(decl
);
2682 return new Bvariable(decl
);
2685 // Make a temporary variable.
2688 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2689 Btype
* btype
, Bexpression
* binit
,
2690 bool is_address_taken
,
2692 Bstatement
** pstatement
)
2694 gcc_assert(function
!= NULL
);
2695 tree decl
= function
->get_tree();
2696 tree type_tree
= btype
->get_tree();
2697 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2698 if (type_tree
== error_mark_node
2699 || init_tree
== error_mark_node
2700 || decl
== error_mark_node
)
2702 *pstatement
= this->error_statement();
2703 return this->error_variable();
2707 // We can only use create_tmp_var if the type is not addressable.
2708 if (!TREE_ADDRESSABLE(type_tree
))
2710 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2711 push_struct_function(decl
);
2713 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2715 var
= create_tmp_var(type_tree
, "GOTMP");
2720 gcc_assert(bblock
!= NULL
);
2721 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2722 create_tmp_var_name("GOTMP"),
2724 DECL_ARTIFICIAL(var
) = 1;
2725 DECL_IGNORED_P(var
) = 1;
2727 DECL_CONTEXT(var
) = decl
;
2729 // We have to add this variable to the BLOCK and the BIND_EXPR.
2730 tree bind_tree
= bblock
->get_tree();
2731 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2732 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2733 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2734 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2735 BLOCK_VARS(block_tree
) = var
;
2736 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2739 if (this->type_size(btype
) != 0
2740 && init_tree
!= NULL_TREE
2741 && TREE_TYPE(init_tree
) != void_type_node
)
2742 DECL_INITIAL(var
) = this->convert_tree(type_tree
, init_tree
, location
);
2744 if (is_address_taken
)
2745 TREE_ADDRESSABLE(var
) = 1;
2747 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2749 void_type_node
, var
));
2751 // For a zero sized type, don't initialize VAR with BINIT, but still
2752 // evaluate BINIT for its side effects.
2753 if (init_tree
!= NULL_TREE
2754 && (this->type_size(btype
) == 0
2755 || TREE_TYPE(init_tree
) == void_type_node
))
2757 this->compound_statement(this->expression_statement(function
, binit
),
2760 return new Bvariable(var
);
2763 // Create an implicit variable that is compiler-defined. This is used when
2764 // generating GC root variables and storing the values of a slice initializer.
2767 Gcc_backend::implicit_variable(const std::string
& name
,
2768 const std::string
& asm_name
,
2769 Btype
* type
, bool is_hidden
, bool is_constant
,
2770 bool is_common
, int64_t alignment
)
2772 tree type_tree
= type
->get_tree();
2773 if (type_tree
== error_mark_node
)
2774 return this->error_variable();
2776 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2777 get_identifier_from_string(name
), type_tree
);
2778 DECL_EXTERNAL(decl
) = 0;
2779 TREE_PUBLIC(decl
) = !is_hidden
;
2780 TREE_STATIC(decl
) = 1;
2781 TREE_USED(decl
) = 1;
2782 DECL_ARTIFICIAL(decl
) = 1;
2785 DECL_COMMON(decl
) = 1;
2787 // When the initializer for one implicit_variable refers to another,
2788 // it needs to know the visibility of the referenced struct so that
2789 // compute_reloc_for_constant will return the right value. On many
2790 // systems calling make_decl_one_only will mark the decl as weak,
2791 // which will change the return value of compute_reloc_for_constant.
2792 // We can't reliably call make_decl_one_only yet, because we don't
2793 // yet know the initializer. This issue doesn't arise in C because
2794 // Go initializers, unlike C initializers, can be indirectly
2795 // recursive. To ensure that compute_reloc_for_constant computes
2796 // the right value if some other initializer refers to this one, we
2797 // mark this symbol as weak here. We undo that below in
2798 // immutable_struct_set_init before calling mark_decl_one_only.
2799 DECL_WEAK(decl
) = 1;
2803 TREE_READONLY(decl
) = 1;
2804 TREE_CONSTANT(decl
) = 1;
2808 SET_DECL_ALIGN(decl
, alignment
* BITS_PER_UNIT
);
2809 DECL_USER_ALIGN(decl
) = 1;
2811 if (! asm_name
.empty())
2812 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2814 go_preserve_from_gc(decl
);
2815 return new Bvariable(decl
);
2818 // Set the initalizer for a variable created by implicit_variable.
2819 // This is where we finish compiling the variable.
2822 Gcc_backend::implicit_variable_set_init(Bvariable
* var
, const std::string
&,
2823 Btype
*, bool, bool, bool is_common
,
2826 tree decl
= var
->get_decl();
2829 init_tree
= NULL_TREE
;
2831 init_tree
= init
->get_tree();
2832 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2835 DECL_INITIAL(decl
) = init_tree
;
2837 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2838 // See the comment where DECL_WEAK is set in implicit_variable.
2841 DECL_WEAK(decl
) = 0;
2842 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2845 resolve_unique_section(decl
, 2, 1);
2847 rest_of_decl_compilation(decl
, 1, 0);
2850 // Return a reference to an implicit variable defined in another package.
2853 Gcc_backend::implicit_variable_reference(const std::string
& name
,
2854 const std::string
& asm_name
,
2857 tree type_tree
= btype
->get_tree();
2858 if (type_tree
== error_mark_node
)
2859 return this->error_variable();
2861 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2862 get_identifier_from_string(name
), type_tree
);
2863 DECL_EXTERNAL(decl
) = 1;
2864 TREE_PUBLIC(decl
) = 1;
2865 TREE_STATIC(decl
) = 0;
2866 DECL_ARTIFICIAL(decl
) = 1;
2867 if (! asm_name
.empty())
2868 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2869 go_preserve_from_gc(decl
);
2870 return new Bvariable(decl
);
2873 // Create a named immutable initialized data structure.
2876 Gcc_backend::immutable_struct(const std::string
& name
,
2877 const std::string
& asm_name
,
2879 bool is_common
, Btype
* btype
, Location location
)
2881 tree type_tree
= btype
->get_tree();
2882 if (type_tree
== error_mark_node
)
2883 return this->error_variable();
2884 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2885 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2886 get_identifier_from_string(name
),
2887 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2888 TREE_STATIC(decl
) = 1;
2889 TREE_USED(decl
) = 1;
2890 TREE_READONLY(decl
) = 1;
2891 TREE_CONSTANT(decl
) = 1;
2892 DECL_ARTIFICIAL(decl
) = 1;
2894 TREE_PUBLIC(decl
) = 1;
2895 if (! asm_name
.empty())
2896 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2898 // When the initializer for one immutable_struct refers to another,
2899 // it needs to know the visibility of the referenced struct so that
2900 // compute_reloc_for_constant will return the right value. On many
2901 // systems calling make_decl_one_only will mark the decl as weak,
2902 // which will change the return value of compute_reloc_for_constant.
2903 // We can't reliably call make_decl_one_only yet, because we don't
2904 // yet know the initializer. This issue doesn't arise in C because
2905 // Go initializers, unlike C initializers, can be indirectly
2906 // recursive. To ensure that compute_reloc_for_constant computes
2907 // the right value if some other initializer refers to this one, we
2908 // mark this symbol as weak here. We undo that below in
2909 // immutable_struct_set_init before calling mark_decl_one_only.
2911 DECL_WEAK(decl
) = 1;
2913 // We don't call rest_of_decl_compilation until we have the
2916 go_preserve_from_gc(decl
);
2917 return new Bvariable(decl
);
2920 // Set the initializer for a variable created by immutable_struct.
2921 // This is where we finish compiling the variable.
2924 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
2925 bool, bool is_common
, Btype
*, Location
,
2926 Bexpression
* initializer
)
2928 tree decl
= var
->get_decl();
2929 tree init_tree
= initializer
->get_tree();
2930 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2933 DECL_INITIAL(decl
) = init_tree
;
2935 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2936 // See the comment where DECL_WEAK is set in immutable_struct.
2939 DECL_WEAK(decl
) = 0;
2940 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2943 // These variables are often unneeded in the final program, so put
2944 // them in their own section so that linker GC can discard them.
2945 resolve_unique_section(decl
,
2946 compute_reloc_for_constant (init_tree
),
2949 rest_of_decl_compilation(decl
, 1, 0);
2952 // Return a reference to an immutable initialized data structure
2953 // defined in another package.
2956 Gcc_backend::immutable_struct_reference(const std::string
& name
,
2957 const std::string
& asm_name
,
2961 tree type_tree
= btype
->get_tree();
2962 if (type_tree
== error_mark_node
)
2963 return this->error_variable();
2964 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2965 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2966 get_identifier_from_string(name
),
2967 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2968 TREE_READONLY(decl
) = 1;
2969 TREE_CONSTANT(decl
) = 1;
2970 DECL_ARTIFICIAL(decl
) = 1;
2971 TREE_PUBLIC(decl
) = 1;
2972 DECL_EXTERNAL(decl
) = 1;
2973 if (! asm_name
.empty())
2974 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2975 go_preserve_from_gc(decl
);
2976 return new Bvariable(decl
);
2982 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
2988 tree func_tree
= function
->get_tree();
2989 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
2990 push_struct_function(func_tree
);
2992 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
2994 decl
= create_artificial_label(location
.gcc_location());
3000 tree id
= get_identifier_from_string(name
);
3001 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
3003 DECL_CONTEXT(decl
) = function
->get_tree();
3005 return new Blabel(decl
);
3008 // Make a statement which defines a label.
3011 Gcc_backend::label_definition_statement(Blabel
* label
)
3013 tree lab
= label
->get_tree();
3014 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
3015 void_type_node
, lab
);
3016 return this->make_statement(ret
);
3019 // Make a goto statement.
3022 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
3024 tree lab
= label
->get_tree();
3025 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
3027 return this->make_statement(ret
);
3030 // Get the address of a label.
3033 Gcc_backend::label_address(Blabel
* label
, Location location
)
3035 tree lab
= label
->get_tree();
3037 TREE_ADDRESSABLE(lab
) = 1;
3038 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
3039 build_fold_addr_expr_loc(location
.gcc_location(),
3041 return this->make_expression(ret
);
3044 // Declare or define a new function.
3047 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
3048 const std::string
& asm_name
, unsigned int flags
,
3051 tree functype
= fntype
->get_tree();
3052 if (functype
!= error_mark_node
)
3054 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
3055 functype
= TREE_TYPE(functype
);
3057 tree id
= get_identifier_from_string(name
);
3058 if (functype
== error_mark_node
|| id
== error_mark_node
)
3059 return this->error_function();
3061 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
3062 if (! asm_name
.empty())
3063 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3064 if ((flags
& function_is_visible
) != 0)
3065 TREE_PUBLIC(decl
) = 1;
3066 if ((flags
& function_is_declaration
) != 0)
3067 DECL_EXTERNAL(decl
) = 1;
3070 tree restype
= TREE_TYPE(functype
);
3072 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
3073 DECL_ARTIFICIAL(resdecl
) = 1;
3074 DECL_IGNORED_P(resdecl
) = 1;
3075 DECL_CONTEXT(resdecl
) = decl
;
3076 DECL_RESULT(decl
) = resdecl
;
3078 if ((flags
& function_is_inlinable
) == 0)
3079 DECL_UNINLINABLE(decl
) = 1;
3080 if ((flags
& function_no_split_stack
) != 0)
3082 tree attr
= get_identifier ("no_split_stack");
3083 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
3085 if ((flags
& function_does_not_return
) != 0)
3086 TREE_THIS_VOLATILE(decl
) = 1;
3087 if ((flags
& function_in_unique_section
) != 0)
3088 resolve_unique_section(decl
, 0, 1);
3089 if ((flags
& function_only_inline
) != 0)
3091 DECL_EXTERNAL(decl
) = 1;
3092 DECL_DECLARED_INLINE_P(decl
) = 1;
3095 go_preserve_from_gc(decl
);
3096 return new Bfunction(decl
);
3099 // Create a statement that runs all deferred calls for FUNCTION. This should
3100 // be a statement that looks like this in C++:
3102 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3105 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
3106 Bexpression
* defer
, Location location
)
3108 tree undefer_tree
= undefer
->get_tree();
3109 tree defer_tree
= defer
->get_tree();
3110 tree fntree
= function
->get_tree();
3112 if (undefer_tree
== error_mark_node
3113 || defer_tree
== error_mark_node
3114 || fntree
== error_mark_node
)
3115 return this->error_statement();
3117 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
3118 push_struct_function(fntree
);
3120 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
3122 tree stmt_list
= NULL
;
3123 Blabel
* blabel
= this->label(function
, "", location
);
3124 Bstatement
* label_def
= this->label_definition_statement(blabel
);
3125 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
3127 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
3128 tree jump
= jump_stmt
->get_tree();
3129 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
3130 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
3132 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
3133 append_to_statement_list(try_catch
, &stmt_list
);
3136 return this->make_statement(stmt_list
);
3139 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3140 // This will only be called for a function definition.
3143 Gcc_backend::function_set_parameters(Bfunction
* function
,
3144 const std::vector
<Bvariable
*>& param_vars
)
3146 tree func_tree
= function
->get_tree();
3147 if (func_tree
== error_mark_node
)
3150 tree params
= NULL_TREE
;
3152 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
3153 pv
!= param_vars
.end();
3156 *pp
= (*pv
)->get_decl();
3157 gcc_assert(*pp
!= error_mark_node
);
3158 pp
= &DECL_CHAIN(*pp
);
3161 DECL_ARGUMENTS(func_tree
) = params
;
3165 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3168 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
3170 tree func_tree
= function
->get_tree();
3171 tree code
= code_stmt
->get_tree();
3173 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
3175 DECL_SAVED_TREE(func_tree
) = code
;
3179 // Look up a named built-in function in the current backend implementation.
3180 // Returns NULL if no built-in function by that name exists.
3183 Gcc_backend::lookup_builtin(const std::string
& name
)
3185 if (this->builtin_functions_
.count(name
) != 0)
3186 return this->builtin_functions_
[name
];
3190 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3191 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3192 // emit early debugging information.
3195 Gcc_backend::write_global_definitions(
3196 const std::vector
<Btype
*>& type_decls
,
3197 const std::vector
<Bexpression
*>& constant_decls
,
3198 const std::vector
<Bfunction
*>& function_decls
,
3199 const std::vector
<Bvariable
*>& variable_decls
)
3201 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
3202 + function_decls
.size() + variable_decls
.size();
3204 tree
* defs
= new tree
[count_definitions
];
3206 // Convert all non-erroneous declarations into Gimple form.
3208 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
3209 p
!= variable_decls
.end();
3212 tree v
= (*p
)->get_decl();
3213 if (v
!= error_mark_node
)
3216 go_preserve_from_gc(defs
[i
]);
3221 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
3222 p
!= type_decls
.end();
3225 tree type_tree
= (*p
)->get_tree();
3226 if (type_tree
!= error_mark_node
3227 && IS_TYPE_OR_DECL_P(type_tree
))
3229 defs
[i
] = TYPE_NAME(type_tree
);
3230 gcc_assert(defs
[i
] != NULL
);
3231 go_preserve_from_gc(defs
[i
]);
3235 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
3236 p
!= constant_decls
.end();
3239 if ((*p
)->get_tree() != error_mark_node
)
3241 defs
[i
] = (*p
)->get_tree();
3242 go_preserve_from_gc(defs
[i
]);
3246 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
3247 p
!= function_decls
.end();
3250 tree decl
= (*p
)->get_tree();
3251 if (decl
!= error_mark_node
)
3253 go_preserve_from_gc(decl
);
3254 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
3255 allocate_struct_function(decl
, false);
3256 cgraph_node::finalize_function(decl
, true);
3263 // Pass everything back to the middle-end.
3265 wrapup_global_declarations(defs
, i
);
3271 Gcc_backend::write_export_data(const char* bytes
, unsigned int size
)
3273 go_write_export_data(bytes
, size
);
3277 // Define a builtin function. BCODE is the builtin function code
3278 // defined by builtins.def. NAME is the name of the builtin function.
3279 // LIBNAME is the name of the corresponding library function, and is
3280 // NULL if there isn't one. FNTYPE is the type of the function.
3281 // CONST_P is true if the function has the const attribute.
3282 // NORETURN_P is true if the function has the noreturn attribute.
3285 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
3286 const char* libname
, tree fntype
, bool const_p
,
3289 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
3290 libname
, NULL_TREE
);
3292 TREE_READONLY(decl
) = 1;
3294 TREE_THIS_VOLATILE(decl
) = 1;
3295 set_builtin_decl(bcode
, decl
, true);
3296 this->builtin_functions_
[name
] = this->make_function(decl
);
3297 if (libname
!= NULL
)
3299 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
3302 TREE_READONLY(decl
) = 1;
3304 TREE_THIS_VOLATILE(decl
) = 1;
3305 this->builtin_functions_
[libname
] = this->make_function(decl
);
3309 // Return the backend generator.
3314 return new Gcc_backend();