1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2014 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 "stringpool.h"
29 #include "stor-layout.h"
31 #include "tree-iterator.h"
34 #include "basic-block.h"
35 #include "gimple-expr.h"
37 #include "langhooks.h"
49 // A class wrapping a tree.
70 // In gcc, types, expressions, and statements are all trees.
71 class Btype
: public Gcc_tree
79 class Bexpression
: public Gcc_tree
87 class Bstatement
: public Gcc_tree
95 class Bfunction
: public Gcc_tree
103 class Bblock
: public Gcc_tree
111 class Bvariable
: public Gcc_tree
119 class Blabel
: public Gcc_tree
127 // This file implements the interface between the Go frontend proper
128 // and the gcc IR. This implements specific instantiations of
129 // abstract classes defined by the Go frontend proper. The Go
130 // frontend proper class methods of these classes to generate the
131 // backend representation.
133 class Gcc_backend
: public Backend
142 { return this->make_type(error_mark_node
); }
146 { return this->make_type(void_type_node
); }
150 { return this->make_type(boolean_type_node
); }
153 integer_type(bool, int);
162 pointer_type(Btype
*);
165 function_type(const Btyped_identifier
&,
166 const std::vector
<Btyped_identifier
>&,
167 const std::vector
<Btyped_identifier
>&,
172 struct_type(const std::vector
<Btyped_identifier
>&);
175 array_type(Btype
*, Bexpression
*);
178 placeholder_pointer_type(const std::string
&, Location
, bool);
181 set_placeholder_pointer_type(Btype
*, Btype
*);
184 set_placeholder_function_type(Btype
*, Btype
*);
187 placeholder_struct_type(const std::string
&, Location
);
190 set_placeholder_struct_type(Btype
* placeholder
,
191 const std::vector
<Btyped_identifier
>&);
194 placeholder_array_type(const std::string
&, Location
);
197 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
200 named_type(const std::string
&, Btype
*, Location
);
203 circular_pointer_type(Btype
*, bool);
206 is_circular_pointer_type(Btype
*);
212 type_alignment(Btype
*);
215 type_field_alignment(Btype
*);
218 type_field_offset(Btype
*, size_t index
);
223 zero_expression(Btype
*);
227 { return this->make_expression(error_mark_node
); }
230 nil_pointer_expression()
231 { return this->make_expression(null_pointer_node
); }
234 var_expression(Bvariable
* var
, Location
);
237 indirect_expression(Btype
*, Bexpression
* expr
, bool known_valid
, Location
);
240 named_constant_expression(Btype
* btype
, const std::string
& name
,
241 Bexpression
* val
, Location
);
244 integer_constant_expression(Btype
* btype
, mpz_t val
);
247 float_constant_expression(Btype
* btype
, mpfr_t val
);
250 complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
);
253 string_constant_expression(const std::string
& val
);
256 boolean_constant_expression(bool val
);
259 real_part_expression(Bexpression
* bcomplex
, Location
);
262 imag_part_expression(Bexpression
* bcomplex
, Location
);
265 complex_expression(Bexpression
* breal
, Bexpression
* bimag
, Location
);
268 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
271 function_code_expression(Bfunction
*, Location
);
274 address_expression(Bexpression
*, Location
);
277 struct_field_expression(Bexpression
*, size_t, Location
);
280 compound_expression(Bstatement
*, Bexpression
*, Location
);
283 conditional_expression(Btype
*, Bexpression
*, Bexpression
*, Bexpression
*,
287 unary_expression(Operator
, Bexpression
*, Location
);
290 binary_expression(Operator
, Bexpression
*, Bexpression
*, Location
);
293 constructor_expression(Btype
*, const std::vector
<Bexpression
*>&, Location
);
296 array_constructor_expression(Btype
*, const std::vector
<unsigned long>&,
297 const std::vector
<Bexpression
*>&, Location
);
300 pointer_offset_expression(Bexpression
* base
, Bexpression
* offset
, Location
);
303 array_index_expression(Bexpression
* array
, Bexpression
* index
, Location
);
306 call_expression(Bexpression
* fn
, const std::vector
<Bexpression
*>& args
,
313 { return this->make_statement(error_mark_node
); }
316 expression_statement(Bexpression
*);
319 init_statement(Bvariable
* var
, Bexpression
* init
);
322 assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
, Location
);
325 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
329 if_statement(Bexpression
* condition
, Bblock
* then_block
, Bblock
* else_block
,
333 switch_statement(Bfunction
* function
, Bexpression
* value
,
334 const std::vector
<std::vector
<Bexpression
*> >& cases
,
335 const std::vector
<Bstatement
*>& statements
,
339 compound_statement(Bstatement
*, Bstatement
*);
342 statement_list(const std::vector
<Bstatement
*>&);
345 exception_handler_statement(Bstatement
* bstat
, Bstatement
* except_stmt
,
346 Bstatement
* finally_stmt
, Location
);
351 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
355 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
358 block_statement(Bblock
*);
364 { return new Bvariable(error_mark_node
); }
367 global_variable(const std::string
& package_name
,
368 const std::string
& pkgpath
,
369 const std::string
& name
,
373 bool in_unique_section
,
377 global_variable_set_init(Bvariable
*, Bexpression
*);
380 local_variable(Bfunction
*, const std::string
&, Btype
*, bool,
384 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
388 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
389 Location
, Bstatement
**);
392 implicit_variable(const std::string
&, Btype
*, Bexpression
*, bool, bool,
396 immutable_struct(const std::string
&, bool, bool, Btype
*, Location
);
399 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
400 Location
, Bexpression
*);
403 immutable_struct_reference(const std::string
&, Btype
*, Location
);
408 label(Bfunction
*, const std::string
& name
, Location
);
411 label_definition_statement(Blabel
*);
414 goto_statement(Blabel
*, Location
);
417 label_address(Blabel
*, Location
);
423 { return this->make_function(error_mark_node
); }
426 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
427 bool is_visible
, bool is_declaration
, bool is_inlinable
,
428 bool disable_split_stack
, bool in_unique_section
, Location
);
431 function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
432 Bexpression
* defer
, Location
);
435 function_set_parameters(Bfunction
* function
, const std::vector
<Bvariable
*>&);
438 function_set_body(Bfunction
* function
, Bstatement
* code_stmt
);
441 lookup_builtin(const std::string
&);
444 write_global_definitions(const std::vector
<Btype
*>&,
445 const std::vector
<Bexpression
*>&,
446 const std::vector
<Bfunction
*>&,
447 const std::vector
<Bvariable
*>&);
450 // Make a Bexpression from a tree.
452 make_expression(tree t
)
453 { return new Bexpression(t
); }
455 // Make a Bstatement from a tree.
457 make_statement(tree t
)
458 { return new Bstatement(t
); }
460 // Make a Btype from a tree.
463 { return new Btype(t
); }
466 make_function(tree t
)
467 { return new Bfunction(t
); }
470 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
473 fill_in_array(Btype
*, Btype
*, Bexpression
*);
476 non_zero_size_type(tree
);
480 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
481 tree fntype
, bool const_p
);
483 // A mapping of the GCC built-ins exposed to GCCGo.
484 std::map
<std::string
, Bfunction
*> builtin_functions_
;
487 // A helper function.
490 get_identifier_from_string(const std::string
& str
)
492 return get_identifier_with_length(str
.data(), str
.length());
495 // Define the built-in functions that are exposed to GCCGo.
497 Gcc_backend::Gcc_backend()
499 /* We need to define the fetch_and_add functions, since we use them
501 tree t
= this->integer_type(BITS_PER_UNIT
, 1)->get_tree();
502 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
503 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
504 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
507 t
= this->integer_type(BITS_PER_UNIT
* 2, 1)->get_tree();
508 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
509 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
510 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
513 t
= this->integer_type(BITS_PER_UNIT
* 4, 1)->get_tree();
514 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
515 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
516 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
519 t
= this->integer_type(BITS_PER_UNIT
* 8, 1)->get_tree();
520 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
521 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8",
522 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
525 // We use __builtin_expect for magic import functions.
526 this->define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
527 build_function_type_list(long_integer_type_node
,
528 long_integer_type_node
,
529 long_integer_type_node
,
533 // We use __builtin_memcmp for struct comparisons.
534 this->define_builtin(BUILT_IN_MEMCMP
, "__builtin_memcmp", "memcmp",
535 build_function_type_list(integer_type_node
,
542 // We provide some functions for the math library.
543 tree math_function_type
= build_function_type_list(double_type_node
,
546 tree math_function_type_long
=
547 build_function_type_list(long_double_type_node
, long_double_type_node
,
548 long_double_type_node
, NULL_TREE
);
549 tree math_function_type_two
= build_function_type_list(double_type_node
,
553 tree math_function_type_long_two
=
554 build_function_type_list(long_double_type_node
, long_double_type_node
,
555 long_double_type_node
, NULL_TREE
);
556 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
557 math_function_type
, true);
558 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
559 math_function_type_long
, true);
560 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
561 math_function_type
, true);
562 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
563 math_function_type_long
, true);
564 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
565 math_function_type
, true);
566 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
567 math_function_type_long
, true);
568 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
569 math_function_type_two
, true);
570 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
571 math_function_type_long_two
, true);
572 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
573 math_function_type
, true);
574 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
575 math_function_type_long
, true);
576 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
577 math_function_type
, true);
578 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
579 math_function_type_long
, true);
580 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
581 math_function_type
, true);
582 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
583 math_function_type_long
, true);
584 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
585 math_function_type
, true);
586 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
587 math_function_type_long
, true);
588 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
589 math_function_type
, true);
590 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
591 math_function_type_long
, true);
592 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
593 math_function_type
, true);
594 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
595 math_function_type_long
, true);
596 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
597 math_function_type_two
, true);
598 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
599 math_function_type_long_two
, true);
600 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
601 build_function_type_list(double_type_node
,
606 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
607 build_function_type_list(long_double_type_node
,
608 long_double_type_node
,
612 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
613 math_function_type
, true);
614 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
615 math_function_type_long
, true);
616 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
617 math_function_type
, true);
618 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
619 math_function_type_long
, true);
620 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
621 math_function_type
, true);
622 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
623 math_function_type_long
, true);
624 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
625 math_function_type
, true);
626 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
627 math_function_type_long
, true);
628 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
629 math_function_type
, true);
630 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
631 math_function_type_long
, true);
632 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
633 math_function_type
, true);
634 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
635 math_function_type_long
, true);
636 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
637 math_function_type
, true);
638 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
639 math_function_type_long
, true);
640 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
641 math_function_type
, true);
642 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
643 math_function_type_long
, true);
645 // We use __builtin_return_address in the thunk we build for
646 // functions which call recover.
647 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
649 build_function_type_list(ptr_type_node
,
654 // The compiler uses __builtin_trap for some exception handling
656 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
657 build_function_type(void_type_node
, void_list_node
),
661 // Get an unnamed integer type.
664 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
669 if (bits
== INT_TYPE_SIZE
)
670 type
= unsigned_type_node
;
671 else if (bits
== CHAR_TYPE_SIZE
)
672 type
= unsigned_char_type_node
;
673 else if (bits
== SHORT_TYPE_SIZE
)
674 type
= short_unsigned_type_node
;
675 else if (bits
== LONG_TYPE_SIZE
)
676 type
= long_unsigned_type_node
;
677 else if (bits
== LONG_LONG_TYPE_SIZE
)
678 type
= long_long_unsigned_type_node
;
680 type
= make_unsigned_type(bits
);
684 if (bits
== INT_TYPE_SIZE
)
685 type
= integer_type_node
;
686 else if (bits
== CHAR_TYPE_SIZE
)
687 type
= signed_char_type_node
;
688 else if (bits
== SHORT_TYPE_SIZE
)
689 type
= short_integer_type_node
;
690 else if (bits
== LONG_TYPE_SIZE
)
691 type
= long_integer_type_node
;
692 else if (bits
== LONG_LONG_TYPE_SIZE
)
693 type
= long_long_integer_type_node
;
695 type
= make_signed_type(bits
);
697 return this->make_type(type
);
700 // Get an unnamed float type.
703 Gcc_backend::float_type(int bits
)
706 if (bits
== FLOAT_TYPE_SIZE
)
707 type
= float_type_node
;
708 else if (bits
== DOUBLE_TYPE_SIZE
)
709 type
= double_type_node
;
710 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
711 type
= long_double_type_node
;
714 type
= make_node(REAL_TYPE
);
715 TYPE_PRECISION(type
) = bits
;
718 return this->make_type(type
);
721 // Get an unnamed complex type.
724 Gcc_backend::complex_type(int bits
)
727 if (bits
== FLOAT_TYPE_SIZE
* 2)
728 type
= complex_float_type_node
;
729 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
730 type
= complex_double_type_node
;
731 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
732 type
= complex_long_double_type_node
;
735 type
= make_node(REAL_TYPE
);
736 TYPE_PRECISION(type
) = bits
/ 2;
738 type
= build_complex_type(type
);
740 return this->make_type(type
);
743 // Get a pointer type.
746 Gcc_backend::pointer_type(Btype
* to_type
)
748 tree to_type_tree
= to_type
->get_tree();
749 if (to_type_tree
== error_mark_node
)
750 return this->error_type();
751 tree type
= build_pointer_type(to_type_tree
);
752 return this->make_type(type
);
755 // Make a function type.
758 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
759 const std::vector
<Btyped_identifier
>& parameters
,
760 const std::vector
<Btyped_identifier
>& results
,
761 Btype
* result_struct
,
764 tree args
= NULL_TREE
;
766 if (receiver
.btype
!= NULL
)
768 tree t
= receiver
.btype
->get_tree();
769 if (t
== error_mark_node
)
770 return this->error_type();
771 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
772 pp
= &TREE_CHAIN(*pp
);
775 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
776 p
!= parameters
.end();
779 tree t
= p
->btype
->get_tree();
780 if (t
== error_mark_node
)
781 return this->error_type();
782 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
783 pp
= &TREE_CHAIN(*pp
);
786 // Varargs is handled entirely at the Go level. When converted to
787 // GENERIC functions are not varargs.
788 *pp
= void_list_node
;
792 result
= void_type_node
;
793 else if (results
.size() == 1)
794 result
= results
.front().btype
->get_tree();
797 gcc_assert(result_struct
!= NULL
);
798 result
= result_struct
->get_tree();
800 if (result
== error_mark_node
)
801 return this->error_type();
803 tree fntype
= build_function_type(result
, args
);
804 if (fntype
== error_mark_node
)
805 return this->error_type();
807 return this->make_type(build_pointer_type(fntype
));
810 // Make a struct type.
813 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
815 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
818 // Fill in the fields of a struct type.
821 Gcc_backend::fill_in_struct(Btype
* fill
,
822 const std::vector
<Btyped_identifier
>& fields
)
824 tree fill_tree
= fill
->get_tree();
825 tree field_trees
= NULL_TREE
;
826 tree
* pp
= &field_trees
;
827 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
831 tree name_tree
= get_identifier_from_string(p
->name
);
832 tree type_tree
= p
->btype
->get_tree();
833 if (type_tree
== error_mark_node
)
834 return this->error_type();
835 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
837 DECL_CONTEXT(field
) = fill_tree
;
839 pp
= &DECL_CHAIN(field
);
841 TYPE_FIELDS(fill_tree
) = field_trees
;
842 layout_type(fill_tree
);
846 // Make an array type.
849 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
851 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
852 element_btype
, length
);
855 // Fill in an array type.
858 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
861 tree element_type_tree
= element_type
->get_tree();
862 tree length_tree
= length
->get_tree();
863 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
864 return this->error_type();
866 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
868 length_tree
= fold_convert(sizetype
, length_tree
);
870 // build_index_type takes the maximum index, which is one less than
872 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
876 tree fill_tree
= fill
->get_tree();
877 TREE_TYPE(fill_tree
) = element_type_tree
;
878 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
879 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
880 layout_type(fill_tree
);
882 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
883 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
884 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
885 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
886 TYPE_CANONICAL(fill_tree
) =
887 build_array_type(TYPE_CANONICAL(element_type_tree
),
888 TYPE_CANONICAL(index_type_tree
));
893 // Create a placeholder for a pointer type.
896 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
897 Location location
, bool)
899 tree ret
= build_distinct_type_copy(ptr_type_node
);
902 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
903 get_identifier_from_string(name
),
905 TYPE_NAME(ret
) = decl
;
907 return this->make_type(ret
);
910 // Set the real target type for a placeholder pointer type.
913 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
916 tree pt
= placeholder
->get_tree();
917 if (pt
== error_mark_node
)
919 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
920 tree tt
= to_type
->get_tree();
921 if (tt
== error_mark_node
)
923 placeholder
->set_tree(error_mark_node
);
926 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
927 TREE_TYPE(pt
) = TREE_TYPE(tt
);
928 if (TYPE_NAME(pt
) != NULL_TREE
)
930 // Build the data structure gcc wants to see for a typedef.
931 tree copy
= build_variant_type_copy(pt
);
932 TYPE_NAME(copy
) = NULL_TREE
;
933 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
938 // Set the real values for a placeholder function type.
941 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
943 return this->set_placeholder_pointer_type(placeholder
, ft
);
946 // Create a placeholder for a struct type.
949 Gcc_backend::placeholder_struct_type(const std::string
& name
,
952 tree ret
= make_node(RECORD_TYPE
);
955 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
956 get_identifier_from_string(name
),
958 TYPE_NAME(ret
) = decl
;
960 return this->make_type(ret
);
963 // Fill in the fields of a placeholder struct type.
966 Gcc_backend::set_placeholder_struct_type(
968 const std::vector
<Btyped_identifier
>& fields
)
970 tree t
= placeholder
->get_tree();
971 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
972 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
974 if (TYPE_NAME(t
) != NULL_TREE
)
976 // Build the data structure gcc wants to see for a typedef.
977 tree copy
= build_distinct_type_copy(t
);
978 TYPE_NAME(copy
) = NULL_TREE
;
979 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
982 return r
->get_tree() != error_mark_node
;
985 // Create a placeholder for an array type.
988 Gcc_backend::placeholder_array_type(const std::string
& name
,
991 tree ret
= make_node(ARRAY_TYPE
);
992 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
993 get_identifier_from_string(name
),
995 TYPE_NAME(ret
) = decl
;
996 return this->make_type(ret
);
999 // Fill in the fields of a placeholder array type.
1002 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1003 Btype
* element_btype
,
1004 Bexpression
* length
)
1006 tree t
= placeholder
->get_tree();
1007 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1008 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1010 // Build the data structure gcc wants to see for a typedef.
1011 tree copy
= build_distinct_type_copy(t
);
1012 TYPE_NAME(copy
) = NULL_TREE
;
1013 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1015 return r
->get_tree() != error_mark_node
;
1018 // Return a named version of a type.
1021 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1024 tree type
= btype
->get_tree();
1025 if (type
== error_mark_node
)
1026 return this->error_type();
1028 // The middle-end expects a basic type to have a name. In Go every
1029 // basic type will have a name. The first time we see a basic type,
1030 // give it whatever Go name we have at this point.
1031 if (TYPE_NAME(type
) == NULL_TREE
1032 && location
.gcc_location() == BUILTINS_LOCATION
1033 && (TREE_CODE(type
) == INTEGER_TYPE
1034 || TREE_CODE(type
) == REAL_TYPE
1035 || TREE_CODE(type
) == COMPLEX_TYPE
1036 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1038 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1039 get_identifier_from_string(name
),
1041 TYPE_NAME(type
) = decl
;
1042 return this->make_type(type
);
1045 tree copy
= build_variant_type_copy(type
);
1046 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1047 get_identifier_from_string(name
),
1049 DECL_ORIGINAL_TYPE(decl
) = type
;
1050 TYPE_NAME(copy
) = decl
;
1051 return this->make_type(copy
);
1054 // Return a pointer type used as a marker for a circular type.
1057 Gcc_backend::circular_pointer_type(Btype
*, bool)
1059 return this->make_type(ptr_type_node
);
1062 // Return whether we might be looking at a circular type.
1065 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1067 return btype
->get_tree() == ptr_type_node
;
1070 // Return the size of a type.
1073 Gcc_backend::type_size(Btype
* btype
)
1075 tree t
= btype
->get_tree();
1076 if (t
== error_mark_node
)
1078 t
= TYPE_SIZE_UNIT(t
);
1079 gcc_assert(tree_fits_uhwi_p (t
));
1080 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1081 size_t ret
= static_cast<size_t>(val_wide
);
1082 gcc_assert(ret
== val_wide
);
1086 // Return the alignment of a type.
1089 Gcc_backend::type_alignment(Btype
* btype
)
1091 tree t
= btype
->get_tree();
1092 if (t
== error_mark_node
)
1094 return TYPE_ALIGN_UNIT(t
);
1097 // Return the alignment of a struct field of type BTYPE.
1100 Gcc_backend::type_field_alignment(Btype
* btype
)
1102 tree t
= btype
->get_tree();
1103 if (t
== error_mark_node
)
1105 return go_field_alignment(t
);
1108 // Return the offset of a field in a struct.
1111 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1113 tree struct_tree
= btype
->get_tree();
1114 if (struct_tree
== error_mark_node
)
1116 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1117 tree field
= TYPE_FIELDS(struct_tree
);
1118 for (; index
> 0; --index
)
1120 field
= DECL_CHAIN(field
);
1121 gcc_assert(field
!= NULL_TREE
);
1123 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1124 gcc_assert(offset_wide
>= 0);
1125 size_t ret
= static_cast<size_t>(offset_wide
);
1126 gcc_assert(ret
== static_cast<unsigned HOST_WIDE_INT
>(offset_wide
));
1130 // Return the zero value for a type.
1133 Gcc_backend::zero_expression(Btype
* btype
)
1135 tree t
= btype
->get_tree();
1137 if (t
== error_mark_node
)
1138 ret
= error_mark_node
;
1140 ret
= build_zero_cst(t
);
1141 return this->make_expression(ret
);
1144 // An expression that references a variable.
1147 Gcc_backend::var_expression(Bvariable
* var
, Location
)
1149 tree ret
= var
->get_tree();
1150 if (ret
== error_mark_node
)
1151 return this->error_expression();
1152 return this->make_expression(ret
);
1155 // An expression that indirectly references an expression.
1158 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1159 bool known_valid
, Location location
)
1161 tree expr_tree
= expr
->get_tree();
1162 tree type_tree
= btype
->get_tree();
1163 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1164 return this->error_expression();
1166 // If the type of EXPR is a recursive pointer type, then we
1167 // need to insert a cast before indirecting.
1168 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1169 if (VOID_TYPE_P(target_type_tree
))
1170 expr_tree
= fold_convert_loc(location
.gcc_location(),
1171 build_pointer_type(type_tree
), expr_tree
);
1173 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1176 TREE_THIS_NOTRAP(ret
) = 1;
1177 return this->make_expression(ret
);
1180 // Return an expression that declares a constant named NAME with the
1181 // constant value VAL in BTYPE.
1184 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1185 Bexpression
* val
, Location location
)
1187 tree type_tree
= btype
->get_tree();
1188 tree const_val
= val
->get_tree();
1189 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1190 return this->error_expression();
1192 tree name_tree
= get_identifier_from_string(name
);
1193 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1195 DECL_INITIAL(decl
) = const_val
;
1196 TREE_CONSTANT(decl
) = 1;
1197 TREE_READONLY(decl
) = 1;
1199 go_preserve_from_gc(decl
);
1200 return this->make_expression(decl
);
1203 // Return a typed value as a constant integer.
1206 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1208 tree t
= btype
->get_tree();
1209 if (t
== error_mark_node
)
1210 return this->error_expression();
1212 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1213 return this->make_expression(ret
);
1216 // Return a typed value as a constant floating-point number.
1219 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1221 tree t
= btype
->get_tree();
1223 if (t
== error_mark_node
)
1224 return this->error_expression();
1227 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1229 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1230 ret
= build_real(t
, r2
);
1231 return this->make_expression(ret
);
1234 // Return a typed real and imaginary value as a constant complex number.
1237 Gcc_backend::complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
)
1239 tree t
= btype
->get_tree();
1241 if (t
== error_mark_node
)
1242 return this->error_expression();
1245 real_from_mpfr(&r1
, real
, TREE_TYPE(t
), GMP_RNDN
);
1247 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1250 real_from_mpfr(&r3
, imag
, TREE_TYPE(t
), GMP_RNDN
);
1252 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1254 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1255 build_real(TREE_TYPE(t
), r4
));
1256 return this->make_expression(ret
);
1259 // Make a constant string expression.
1262 Gcc_backend::string_constant_expression(const std::string
& val
)
1264 tree index_type
= build_index_type(size_int(val
.length()));
1265 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1267 tree string_type
= build_array_type(const_char_type
, index_type
);
1268 string_type
= build_variant_type_copy(string_type
);
1269 TYPE_STRING_FLAG(string_type
) = 1;
1270 tree string_val
= build_string(val
.length(), val
.data());
1271 TREE_TYPE(string_val
) = string_type
;
1273 return this->make_expression(string_val
);
1276 // Make a constant boolean expression.
1279 Gcc_backend::boolean_constant_expression(bool val
)
1281 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1282 return this->make_expression(bool_cst
);
1285 // Return the real part of a complex expression.
1288 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1290 tree complex_tree
= bcomplex
->get_tree();
1291 if (complex_tree
== error_mark_node
)
1292 return this->error_expression();
1293 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1294 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1295 TREE_TYPE(TREE_TYPE(complex_tree
)),
1297 return this->make_expression(ret
);
1300 // Return the imaginary part of a complex expression.
1303 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1305 tree complex_tree
= bcomplex
->get_tree();
1306 if (complex_tree
== error_mark_node
)
1307 return this->error_expression();
1308 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1309 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1310 TREE_TYPE(TREE_TYPE(complex_tree
)),
1312 return this->make_expression(ret
);
1315 // Make a complex expression given its real and imaginary parts.
1318 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1321 tree real_tree
= breal
->get_tree();
1322 tree imag_tree
= bimag
->get_tree();
1323 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1324 return this->error_expression();
1325 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1326 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1327 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1328 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1329 build_complex_type(TREE_TYPE(real_tree
)),
1330 real_tree
, imag_tree
);
1331 return this->make_expression(ret
);
1334 // An expression that converts an expression to a different type.
1337 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1340 tree type_tree
= type
->get_tree();
1341 tree expr_tree
= expr
->get_tree();
1342 if (type_tree
== error_mark_node
1343 || expr_tree
== error_mark_node
1344 || TREE_TYPE(expr_tree
) == error_mark_node
)
1345 return this->error_expression();
1348 if (this->type_size(type
) == 0)
1350 // Do not convert zero-sized types.
1353 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1354 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1355 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1356 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1357 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1358 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1359 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1360 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1361 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1362 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1363 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1364 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1365 type_tree
, expr_tree
);
1367 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1369 return this->make_expression(ret
);
1372 // Get the address of a function.
1375 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1377 tree func
= bfunc
->get_tree();
1378 if (func
== error_mark_node
)
1379 return this->error_expression();
1381 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1382 return this->make_expression(ret
);
1385 // Get the address of an expression.
1388 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1390 tree expr
= bexpr
->get_tree();
1391 if (expr
== error_mark_node
)
1392 return this->error_expression();
1394 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1395 return this->make_expression(ret
);
1398 // Return an expression for the field at INDEX in BSTRUCT.
1401 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1404 tree struct_tree
= bstruct
->get_tree();
1405 if (struct_tree
== error_mark_node
1406 || TREE_TYPE(struct_tree
) == error_mark_node
)
1407 return this->error_expression();
1408 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1409 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1410 if (field
== NULL_TREE
)
1412 // This can happen for a type which refers to itself indirectly
1413 // and then turns out to be erroneous.
1414 return this->error_expression();
1416 for (unsigned int i
= index
; i
> 0; --i
)
1418 field
= DECL_CHAIN(field
);
1419 gcc_assert(field
!= NULL_TREE
);
1421 if (TREE_TYPE(field
) == error_mark_node
)
1422 return this->error_expression();
1423 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1424 TREE_TYPE(field
), struct_tree
, field
,
1426 if (TREE_CONSTANT(struct_tree
))
1427 TREE_CONSTANT(ret
) = 1;
1428 return this->make_expression(ret
);
1431 // Return an expression that executes BSTAT before BEXPR.
1434 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1437 tree stat
= bstat
->get_tree();
1438 tree expr
= bexpr
->get_tree();
1439 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1440 return this->error_expression();
1441 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1442 TREE_TYPE(expr
), stat
, expr
);
1443 return this->make_expression(ret
);
1446 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1447 // ELSE_EXPR otherwise.
1450 Gcc_backend::conditional_expression(Btype
* btype
, Bexpression
* condition
,
1451 Bexpression
* then_expr
,
1452 Bexpression
* else_expr
, Location location
)
1454 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1455 tree cond_tree
= condition
->get_tree();
1456 tree then_tree
= then_expr
->get_tree();
1457 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1458 if (type_tree
== error_mark_node
1459 || cond_tree
== error_mark_node
1460 || then_tree
== error_mark_node
1461 || else_tree
== error_mark_node
)
1462 return this->error_expression();
1463 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1464 cond_tree
, then_tree
, else_tree
);
1465 return this->make_expression(ret
);
1468 // Return an expression for the unary operation OP EXPR.
1471 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1473 tree expr_tree
= expr
->get_tree();
1474 if (expr_tree
== error_mark_node
1475 || TREE_TYPE(expr_tree
) == error_mark_node
)
1476 return this->error_expression();
1478 tree type_tree
= TREE_TYPE(expr_tree
);
1479 enum tree_code code
;
1482 case OPERATOR_MINUS
:
1484 tree computed_type
= excess_precision_type(type_tree
);
1485 if (computed_type
!= NULL_TREE
)
1487 expr_tree
= convert(computed_type
, expr_tree
);
1488 type_tree
= computed_type
;
1494 code
= TRUTH_NOT_EXPR
;
1497 code
= BIT_NOT_EXPR
;
1504 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1506 return this->make_expression(ret
);
1509 // Convert a gofrontend operator to an equivalent tree_code.
1511 static enum tree_code
1512 operator_to_tree_code(Operator op
, tree type
)
1514 enum tree_code code
;
1520 case OPERATOR_NOTEQ
:
1536 code
= TRUTH_ORIF_EXPR
;
1538 case OPERATOR_ANDAND
:
1539 code
= TRUTH_ANDIF_EXPR
;
1544 case OPERATOR_MINUS
:
1548 code
= BIT_IOR_EXPR
;
1551 code
= BIT_XOR_EXPR
;
1557 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1560 code
= TRUNC_DIV_EXPR
;
1563 code
= TRUNC_MOD_EXPR
;
1565 case OPERATOR_LSHIFT
:
1568 case OPERATOR_RSHIFT
:
1572 code
= BIT_AND_EXPR
;
1574 case OPERATOR_BITCLEAR
:
1575 code
= BIT_AND_EXPR
;
1584 // Return an expression for the binary operation LEFT OP RIGHT.
1587 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1588 Bexpression
* right
, Location location
)
1590 tree left_tree
= left
->get_tree();
1591 tree right_tree
= right
->get_tree();
1592 if (left_tree
== error_mark_node
1593 || right_tree
== error_mark_node
)
1594 return this->error_expression();
1595 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1597 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1598 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1599 tree computed_type
= excess_precision_type(type_tree
);
1600 if (computed_type
!= NULL_TREE
)
1602 left_tree
= convert(computed_type
, left_tree
);
1603 right_tree
= convert(computed_type
, right_tree
);
1604 type_tree
= computed_type
;
1607 // For comparison operators, the resulting type should be boolean.
1611 case OPERATOR_NOTEQ
:
1616 type_tree
= boolean_type_node
;
1622 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1623 left_tree
, right_tree
);
1624 return this->make_expression(ret
);
1627 // Return an expression that constructs BTYPE with VALS.
1630 Gcc_backend::constructor_expression(Btype
* btype
,
1631 const std::vector
<Bexpression
*>& vals
,
1634 tree type_tree
= btype
->get_tree();
1635 if (type_tree
== error_mark_node
)
1636 return this->error_expression();
1638 vec
<constructor_elt
, va_gc
> *init
;
1639 vec_alloc(init
, vals
.size());
1641 bool is_constant
= true;
1642 tree field
= TYPE_FIELDS(type_tree
);
1643 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1645 ++p
, field
= DECL_CHAIN(field
))
1647 gcc_assert(field
!= NULL_TREE
);
1648 tree val
= (*p
)->get_tree();
1649 if (TREE_TYPE(field
) == error_mark_node
1650 || val
== error_mark_node
1651 || TREE_TYPE(val
) == error_mark_node
)
1652 return this->error_expression();
1654 constructor_elt empty
= {NULL
, NULL
};
1655 constructor_elt
* elt
= init
->quick_push(empty
);
1657 elt
->value
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(field
),
1659 if (!TREE_CONSTANT(elt
->value
))
1660 is_constant
= false;
1662 gcc_assert(field
== NULL_TREE
);
1663 tree ret
= build_constructor(type_tree
, init
);
1665 TREE_CONSTANT(ret
) = 1;
1667 return this->make_expression(ret
);
1671 Gcc_backend::array_constructor_expression(
1672 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1673 const std::vector
<Bexpression
*>& vals
, Location
)
1675 tree type_tree
= array_btype
->get_tree();
1676 if (type_tree
== error_mark_node
)
1677 return this->error_expression();
1679 gcc_assert(indexes
.size() == vals
.size());
1680 vec
<constructor_elt
, va_gc
> *init
;
1681 vec_alloc(init
, vals
.size());
1683 bool is_constant
= true;
1684 for (size_t i
= 0; i
< vals
.size(); ++i
)
1686 tree index
= size_int(indexes
[i
]);
1687 tree val
= (vals
[i
])->get_tree();
1689 if (index
== error_mark_node
1690 || val
== error_mark_node
)
1691 return this->error_expression();
1693 if (!TREE_CONSTANT(val
))
1694 is_constant
= false;
1696 constructor_elt empty
= {NULL
, NULL
};
1697 constructor_elt
* elt
= init
->quick_push(empty
);
1702 tree ret
= build_constructor(type_tree
, init
);
1704 TREE_CONSTANT(ret
) = 1;
1705 return this->make_expression(ret
);
1708 // Return an expression for the address of BASE[INDEX].
1711 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
1714 tree base_tree
= base
->get_tree();
1715 tree index_tree
= index
->get_tree();
1716 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
1717 if (base_tree
== error_mark_node
1718 || TREE_TYPE(base_tree
) == error_mark_node
1719 || index_tree
== error_mark_node
1720 || element_type_tree
== error_mark_node
)
1721 return this->error_expression();
1723 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
1724 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
1725 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
1726 index_tree
, element_size
);
1727 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
1728 TREE_TYPE(base_tree
), base_tree
, offset
);
1729 return this->make_expression(ptr
);
1732 // Return an expression representing ARRAY[INDEX]
1735 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
1738 tree array_tree
= array
->get_tree();
1739 tree index_tree
= index
->get_tree();
1740 if (array_tree
== error_mark_node
1741 || TREE_TYPE(array_tree
) == error_mark_node
1742 || index_tree
== error_mark_node
)
1743 return this->error_expression();
1745 tree ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
1746 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
1747 index_tree
, NULL_TREE
, NULL_TREE
);
1748 return this->make_expression(ret
);
1751 // Create an expression for a call to FN_EXPR with FN_ARGS.
1753 Gcc_backend::call_expression(Bexpression
* fn_expr
,
1754 const std::vector
<Bexpression
*>& fn_args
,
1757 tree fn
= fn_expr
->get_tree();
1758 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
1759 return this->error_expression();
1761 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
1762 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
1764 size_t nargs
= fn_args
.size();
1765 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
1766 for (size_t i
= 0; i
< nargs
; ++i
)
1768 args
[i
] = fn_args
.at(i
)->get_tree();
1769 if (args
[i
] == error_mark_node
)
1770 return this->error_expression();
1774 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
1775 fndecl
= TREE_OPERAND(fndecl
, 0);
1777 // This is to support builtin math functions when using 80387 math.
1778 tree excess_type
= NULL_TREE
;
1780 && TREE_CODE(fndecl
) == FUNCTION_DECL
1781 && DECL_IS_BUILTIN(fndecl
)
1782 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
1784 && ((SCALAR_FLOAT_TYPE_P(rettype
)
1785 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
1786 || (COMPLEX_FLOAT_TYPE_P(rettype
)
1787 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
1789 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
1790 if (excess_type
!= NULL_TREE
)
1792 tree excess_fndecl
= mathfn_built_in(excess_type
,
1793 DECL_FUNCTION_CODE(fndecl
));
1794 if (excess_fndecl
== NULL_TREE
)
1795 excess_type
= NULL_TREE
;
1798 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
1800 for (size_t i
= 0; i
< nargs
; ++i
)
1802 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
1803 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
1804 args
[i
] = ::convert(excess_type
, args
[i
]);
1811 build_call_array_loc(location
.gcc_location(),
1812 excess_type
!= NULL_TREE
? excess_type
: rettype
,
1815 if (excess_type
!= NULL_TREE
)
1817 // Calling convert here can undo our excess precision change.
1818 // That may or may not be a bug in convert_to_real.
1819 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
1823 return this->make_expression(ret
);
1826 // An expression as a statement.
1829 Gcc_backend::expression_statement(Bexpression
* expr
)
1831 return this->make_statement(expr
->get_tree());
1834 // Variable initialization.
1837 Gcc_backend::init_statement(Bvariable
* var
, Bexpression
* init
)
1839 tree var_tree
= var
->get_tree();
1840 tree init_tree
= init
->get_tree();
1841 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1842 return this->error_statement();
1843 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
1845 // To avoid problems with GNU ld, we don't make zero-sized
1846 // externally visible variables. That might lead us to doing an
1847 // initialization of a zero-sized expression to a non-zero sized
1848 // variable, or vice-versa. Avoid crashes by omitting the
1849 // initializer. Such initializations don't mean anything anyhow.
1850 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
1851 && init_tree
!= NULL_TREE
1852 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
1854 DECL_INITIAL(var_tree
) = init_tree
;
1855 init_tree
= NULL_TREE
;
1858 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
1859 void_type_node
, var_tree
);
1860 if (init_tree
!= NULL_TREE
)
1861 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
1862 void_type_node
, init_tree
, ret
);
1864 return this->make_statement(ret
);
1870 Gcc_backend::assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
,
1873 tree lhs_tree
= lhs
->get_tree();
1874 tree rhs_tree
= rhs
->get_tree();
1875 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
1876 return this->error_statement();
1878 // To avoid problems with GNU ld, we don't make zero-sized
1879 // externally visible variables. That might lead us to doing an
1880 // assignment of a zero-sized expression to a non-zero sized
1881 // expression; avoid crashes here by avoiding assignments of
1882 // zero-sized expressions. Such assignments don't really mean
1884 if (int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
1885 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
1886 return this->compound_statement(this->expression_statement(lhs
),
1887 this->expression_statement(rhs
));
1889 // Sometimes the same unnamed Go type can be created multiple times
1890 // and thus have multiple tree representations. Make sure this does
1891 // not confuse the middle-end.
1892 if (TREE_TYPE(lhs_tree
) != TREE_TYPE(rhs_tree
))
1894 tree lhs_type_tree
= TREE_TYPE(lhs_tree
);
1895 gcc_assert(TREE_CODE(lhs_type_tree
) == TREE_CODE(TREE_TYPE(rhs_tree
)));
1896 if (POINTER_TYPE_P(lhs_type_tree
)
1897 || INTEGRAL_TYPE_P(lhs_type_tree
)
1898 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
1899 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
1900 rhs_tree
= fold_convert_loc(location
.gcc_location(), lhs_type_tree
,
1902 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
1903 || TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
)
1905 gcc_assert(int_size_in_bytes(lhs_type_tree
)
1906 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
1907 rhs_tree
= fold_build1_loc(location
.gcc_location(),
1909 lhs_type_tree
, rhs_tree
);
1913 return this->make_statement(fold_build2_loc(location
.gcc_location(),
1916 lhs_tree
, rhs_tree
));
1922 Gcc_backend::return_statement(Bfunction
* bfunction
,
1923 const std::vector
<Bexpression
*>& vals
,
1926 tree fntree
= bfunction
->get_tree();
1927 if (fntree
== error_mark_node
)
1928 return this->error_statement();
1929 tree result
= DECL_RESULT(fntree
);
1930 if (result
== error_mark_node
)
1931 return this->error_statement();
1935 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
1937 else if (vals
.size() == 1)
1939 tree val
= vals
.front()->get_tree();
1940 if (val
== error_mark_node
)
1941 return this->error_statement();
1942 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1943 void_type_node
, result
,
1944 vals
.front()->get_tree());
1945 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1946 void_type_node
, set
);
1950 // To return multiple values, copy the values into a temporary
1951 // variable of the right structure type, and then assign the
1952 // temporary variable to the DECL_RESULT in the return
1954 tree stmt_list
= NULL_TREE
;
1955 tree rettype
= TREE_TYPE(result
);
1957 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
1958 push_struct_function(fntree
);
1960 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
1961 tree rettmp
= create_tmp_var(rettype
, "RESULT");
1964 tree field
= TYPE_FIELDS(rettype
);
1965 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1967 p
++, field
= DECL_CHAIN(field
))
1969 gcc_assert(field
!= NULL_TREE
);
1970 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1971 TREE_TYPE(field
), rettmp
, field
,
1973 tree val
= (*p
)->get_tree();
1974 if (val
== error_mark_node
)
1975 return this->error_statement();
1976 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1978 ref
, (*p
)->get_tree());
1979 append_to_statement_list(set
, &stmt_list
);
1981 gcc_assert(field
== NULL_TREE
);
1982 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1985 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1986 void_type_node
, set
);
1987 append_to_statement_list(ret_expr
, &stmt_list
);
1990 return this->make_statement(ret
);
1993 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
1994 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
1995 // NULL, it will always be executed. This is used for handling defers in Go
1996 // functions. In C++, the resulting code is of this form:
1997 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2000 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2001 Bstatement
* except_stmt
,
2002 Bstatement
* finally_stmt
,
2005 tree stat_tree
= bstat
->get_tree();
2006 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2007 tree finally_tree
= finally_stmt
== NULL
2009 : finally_stmt
->get_tree();
2011 if (stat_tree
== error_mark_node
2012 || except_tree
== error_mark_node
2013 || finally_tree
== error_mark_node
)
2014 return this->error_statement();
2016 if (except_tree
!= NULL_TREE
)
2017 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2018 void_type_node
, stat_tree
,
2019 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2020 void_type_node
, NULL
, except_tree
));
2021 if (finally_tree
!= NULL_TREE
)
2022 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2023 void_type_node
, stat_tree
, finally_tree
);
2024 return this->make_statement(stat_tree
);
2030 Gcc_backend::if_statement(Bexpression
* condition
, Bblock
* then_block
,
2031 Bblock
* else_block
, Location location
)
2033 tree cond_tree
= condition
->get_tree();
2034 tree then_tree
= then_block
->get_tree();
2035 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2036 if (cond_tree
== error_mark_node
2037 || then_tree
== error_mark_node
2038 || else_tree
== error_mark_node
)
2039 return this->error_statement();
2040 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2041 cond_tree
, then_tree
, else_tree
);
2042 return this->make_statement(ret
);
2048 Gcc_backend::switch_statement(
2049 Bfunction
* function
,
2051 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2052 const std::vector
<Bstatement
*>& statements
,
2053 Location switch_location
)
2055 gcc_assert(cases
.size() == statements
.size());
2057 tree decl
= function
->get_tree();
2058 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2059 push_struct_function(decl
);
2061 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2063 tree stmt_list
= NULL_TREE
;
2064 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2065 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2066 ps
!= statements
.end();
2071 source_location loc
= (*ps
!= NULL
2072 ? EXPR_LOCATION((*ps
)->get_tree())
2073 : UNKNOWN_LOCATION
);
2074 tree label
= create_artificial_label(loc
);
2075 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2076 append_to_statement_list(c
, &stmt_list
);
2080 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2084 tree t
= (*pcv
)->get_tree();
2085 if (t
== error_mark_node
)
2086 return this->error_statement();
2087 source_location loc
= EXPR_LOCATION(t
);
2088 tree label
= create_artificial_label(loc
);
2089 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2090 append_to_statement_list(c
, &stmt_list
);
2096 tree t
= (*ps
)->get_tree();
2097 if (t
== error_mark_node
)
2098 return this->error_statement();
2099 append_to_statement_list(t
, &stmt_list
);
2104 tree tv
= value
->get_tree();
2105 if (tv
== error_mark_node
)
2106 return this->error_statement();
2107 tree t
= build3_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2108 NULL_TREE
, tv
, stmt_list
, NULL_TREE
);
2109 return this->make_statement(t
);
2112 // Pair of statements.
2115 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2117 tree stmt_list
= NULL_TREE
;
2118 tree t
= s1
->get_tree();
2119 if (t
== error_mark_node
)
2120 return this->error_statement();
2121 append_to_statement_list(t
, &stmt_list
);
2123 if (t
== error_mark_node
)
2124 return this->error_statement();
2125 append_to_statement_list(t
, &stmt_list
);
2126 return this->make_statement(stmt_list
);
2129 // List of statements.
2132 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2134 tree stmt_list
= NULL_TREE
;
2135 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2136 p
!= statements
.end();
2139 tree t
= (*p
)->get_tree();
2140 if (t
== error_mark_node
)
2141 return this->error_statement();
2142 append_to_statement_list(t
, &stmt_list
);
2144 return this->make_statement(stmt_list
);
2147 // Make a block. For some reason gcc uses a dual structure for
2148 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2149 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2153 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2154 const std::vector
<Bvariable
*>& vars
,
2155 Location start_location
,
2158 tree block_tree
= make_node(BLOCK
);
2159 if (enclosing
== NULL
)
2161 tree fndecl
= function
->get_tree();
2162 gcc_assert(fndecl
!= NULL_TREE
);
2164 // We may have already created a block for local variables when
2165 // we take the address of a parameter.
2166 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2168 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2169 DECL_INITIAL(fndecl
) = block_tree
;
2173 tree superblock_tree
= DECL_INITIAL(fndecl
);
2174 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2176 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2178 pp
= &BLOCK_CHAIN(*pp
))
2185 tree superbind_tree
= enclosing
->get_tree();
2186 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2187 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2189 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2191 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2193 pp
= &BLOCK_CHAIN(*pp
))
2198 tree
* pp
= &BLOCK_VARS(block_tree
);
2199 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2203 *pp
= (*pv
)->get_tree();
2204 if (*pp
!= error_mark_node
)
2205 pp
= &DECL_CHAIN(*pp
);
2209 TREE_USED(block_tree
) = 1;
2211 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2212 void_type_node
, BLOCK_VARS(block_tree
),
2213 NULL_TREE
, block_tree
);
2214 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2215 return new Bblock(bind_tree
);
2218 // Add statements to a block.
2221 Gcc_backend::block_add_statements(Bblock
* bblock
,
2222 const std::vector
<Bstatement
*>& statements
)
2224 tree stmt_list
= NULL_TREE
;
2225 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2226 p
!= statements
.end();
2229 tree s
= (*p
)->get_tree();
2230 if (s
!= error_mark_node
)
2231 append_to_statement_list(s
, &stmt_list
);
2234 tree bind_tree
= bblock
->get_tree();
2235 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2236 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2239 // Return a block as a statement.
2242 Gcc_backend::block_statement(Bblock
* bblock
)
2244 tree bind_tree
= bblock
->get_tree();
2245 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2246 return this->make_statement(bind_tree
);
2249 // This is not static because we declare it with GTY(()) in go-c.h.
2250 tree go_non_zero_struct
;
2252 // Return a type corresponding to TYPE with non-zero size.
2255 Gcc_backend::non_zero_size_type(tree type
)
2257 if (int_size_in_bytes(type
) != 0)
2260 switch (TREE_CODE(type
))
2263 if (TYPE_FIELDS(type
) != NULL_TREE
)
2265 tree ns
= make_node(RECORD_TYPE
);
2266 tree field_trees
= NULL_TREE
;
2267 tree
*pp
= &field_trees
;
2268 for (tree field
= TYPE_FIELDS(type
);
2270 field
= DECL_CHAIN(field
))
2272 tree ft
= TREE_TYPE(field
);
2273 if (field
== TYPE_FIELDS(type
))
2274 ft
= non_zero_size_type(ft
);
2275 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2276 DECL_NAME(field
), ft
);
2277 DECL_CONTEXT(f
) = ns
;
2279 pp
= &DECL_CHAIN(f
);
2281 TYPE_FIELDS(ns
) = field_trees
;
2286 if (go_non_zero_struct
== NULL_TREE
)
2288 type
= make_node(RECORD_TYPE
);
2289 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2290 get_identifier("dummy"),
2292 DECL_CONTEXT(field
) = type
;
2293 TYPE_FIELDS(type
) = field
;
2295 go_non_zero_struct
= type
;
2297 return go_non_zero_struct
;
2301 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2302 return build_array_type_nelts(element_type
, 1);
2312 // Make a global variable.
2315 Gcc_backend::global_variable(const std::string
& package_name
,
2316 const std::string
& pkgpath
,
2317 const std::string
& name
,
2321 bool in_unique_section
,
2324 tree type_tree
= btype
->get_tree();
2325 if (type_tree
== error_mark_node
)
2326 return this->error_variable();
2328 // The GNU linker does not like dynamic variables with zero size.
2329 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2330 type_tree
= this->non_zero_size_type(type_tree
);
2332 std::string
var_name(package_name
);
2333 var_name
.push_back('.');
2334 var_name
.append(name
);
2335 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2336 get_identifier_from_string(var_name
),
2339 DECL_EXTERNAL(decl
) = 1;
2341 TREE_STATIC(decl
) = 1;
2344 TREE_PUBLIC(decl
) = 1;
2346 std::string
asm_name(pkgpath
);
2347 asm_name
.push_back('.');
2348 asm_name
.append(name
);
2349 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2351 TREE_USED(decl
) = 1;
2353 if (in_unique_section
)
2354 resolve_unique_section (decl
, 0, 1);
2356 go_preserve_from_gc(decl
);
2358 return new Bvariable(decl
);
2361 // Set the initial value of a global variable.
2364 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2366 tree expr_tree
= expr
->get_tree();
2367 if (expr_tree
== error_mark_node
)
2369 gcc_assert(TREE_CONSTANT(expr_tree
));
2370 tree var_decl
= var
->get_tree();
2371 if (var_decl
== error_mark_node
)
2373 DECL_INITIAL(var_decl
) = expr_tree
;
2375 // If this variable goes in a unique section, it may need to go into
2376 // a different one now that DECL_INITIAL is set.
2377 if (symtab_node::get(var_decl
)
2378 && symtab_node::get(var_decl
)->implicit_section
)
2380 set_decl_section_name (var_decl
, NULL
);
2381 resolve_unique_section (var_decl
,
2382 compute_reloc_for_constant (expr_tree
),
2387 // Make a local variable.
2390 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2391 Btype
* btype
, bool is_address_taken
,
2394 tree type_tree
= btype
->get_tree();
2395 if (type_tree
== error_mark_node
)
2396 return this->error_variable();
2397 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2398 get_identifier_from_string(name
),
2400 DECL_CONTEXT(decl
) = function
->get_tree();
2401 TREE_USED(decl
) = 1;
2402 if (is_address_taken
)
2403 TREE_ADDRESSABLE(decl
) = 1;
2404 go_preserve_from_gc(decl
);
2405 return new Bvariable(decl
);
2408 // Make a function parameter variable.
2411 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2412 Btype
* btype
, bool is_address_taken
,
2415 tree type_tree
= btype
->get_tree();
2416 if (type_tree
== error_mark_node
)
2417 return this->error_variable();
2418 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2419 get_identifier_from_string(name
),
2421 DECL_CONTEXT(decl
) = function
->get_tree();
2422 DECL_ARG_TYPE(decl
) = type_tree
;
2423 TREE_USED(decl
) = 1;
2424 if (is_address_taken
)
2425 TREE_ADDRESSABLE(decl
) = 1;
2426 go_preserve_from_gc(decl
);
2427 return new Bvariable(decl
);
2430 // Make a temporary variable.
2433 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2434 Btype
* btype
, Bexpression
* binit
,
2435 bool is_address_taken
,
2437 Bstatement
** pstatement
)
2439 gcc_assert(function
!= NULL
);
2440 tree decl
= function
->get_tree();
2441 tree type_tree
= btype
->get_tree();
2442 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2443 if (type_tree
== error_mark_node
2444 || init_tree
== error_mark_node
2445 || decl
== error_mark_node
)
2447 *pstatement
= this->error_statement();
2448 return this->error_variable();
2452 // We can only use create_tmp_var if the type is not addressable.
2453 if (!TREE_ADDRESSABLE(type_tree
))
2455 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2456 push_struct_function(decl
);
2458 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2460 var
= create_tmp_var(type_tree
, "GOTMP");
2465 gcc_assert(bblock
!= NULL
);
2466 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2467 create_tmp_var_name("GOTMP"),
2469 DECL_ARTIFICIAL(var
) = 1;
2470 DECL_IGNORED_P(var
) = 1;
2472 DECL_CONTEXT(var
) = decl
;
2474 // We have to add this variable to the BLOCK and the BIND_EXPR.
2475 tree bind_tree
= bblock
->get_tree();
2476 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2477 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2478 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2479 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2480 BLOCK_VARS(block_tree
) = var
;
2481 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2484 if (init_tree
!= NULL_TREE
)
2485 DECL_INITIAL(var
) = fold_convert_loc(location
.gcc_location(), type_tree
,
2488 if (is_address_taken
)
2489 TREE_ADDRESSABLE(var
) = 1;
2491 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2493 void_type_node
, var
));
2494 return new Bvariable(var
);
2497 // Create an implicit variable that is compiler-defined. This is used when
2498 // generating GC root variables and storing the values of a slice initializer.
2501 Gcc_backend::implicit_variable(const std::string
& name
, Btype
* type
,
2502 Bexpression
* init
, bool is_constant
,
2503 bool is_common
, size_t alignment
)
2505 tree type_tree
= type
->get_tree();
2508 init_tree
= NULL_TREE
;
2510 init_tree
= init
->get_tree();
2511 if (type_tree
== error_mark_node
|| init_tree
== error_mark_node
)
2512 return this->error_variable();
2514 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2515 get_identifier_from_string(name
), type_tree
);
2516 DECL_EXTERNAL(decl
) = 0;
2517 TREE_PUBLIC(decl
) = 0;
2518 TREE_STATIC(decl
) = 1;
2519 DECL_ARTIFICIAL(decl
) = 1;
2522 DECL_COMMON(decl
) = 1;
2523 TREE_PUBLIC(decl
) = 1;
2524 gcc_assert(init_tree
== NULL_TREE
);
2526 else if (is_constant
)
2528 TREE_READONLY(decl
) = 1;
2529 TREE_CONSTANT(decl
) = 1;
2531 DECL_INITIAL(decl
) = init_tree
;
2535 DECL_ALIGN(decl
) = alignment
* BITS_PER_UNIT
;
2536 DECL_USER_ALIGN(decl
) = 1;
2539 rest_of_decl_compilation(decl
, 1, 0);
2541 return new Bvariable(decl
);
2544 // Create a named immutable initialized data structure.
2547 Gcc_backend::immutable_struct(const std::string
& name
, bool is_hidden
,
2548 bool is_common
, Btype
* btype
, Location location
)
2550 tree type_tree
= btype
->get_tree();
2551 if (type_tree
== error_mark_node
)
2552 return this->error_variable();
2553 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2554 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2555 get_identifier_from_string(name
),
2556 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2557 TREE_STATIC(decl
) = 1;
2558 TREE_USED(decl
) = 1;
2559 TREE_READONLY(decl
) = 1;
2560 TREE_CONSTANT(decl
) = 1;
2561 DECL_ARTIFICIAL(decl
) = 1;
2563 TREE_PUBLIC(decl
) = 1;
2565 // When the initializer for one immutable_struct refers to another,
2566 // it needs to know the visibility of the referenced struct so that
2567 // compute_reloc_for_constant will return the right value. On many
2568 // systems calling make_decl_one_only will mark the decl as weak,
2569 // which will change the return value of compute_reloc_for_constant.
2570 // We can't reliably call make_decl_one_only yet, because we don't
2571 // yet know the initializer. This issue doesn't arise in C because
2572 // Go initializers, unlike C initializers, can be indirectly
2573 // recursive. To ensure that compute_reloc_for_constant computes
2574 // the right value if some other initializer refers to this one, we
2575 // mark this symbol as weak here. We undo that below in
2576 // immutable_struct_set_init before calling mark_decl_one_only.
2578 DECL_WEAK(decl
) = 1;
2580 // We don't call rest_of_decl_compilation until we have the
2583 go_preserve_from_gc(decl
);
2584 return new Bvariable(decl
);
2587 // Set the initializer for a variable created by immutable_struct.
2588 // This is where we finish compiling the variable.
2591 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
2592 bool, bool is_common
, Btype
*, Location
,
2593 Bexpression
* initializer
)
2595 tree decl
= var
->get_tree();
2596 tree init_tree
= initializer
->get_tree();
2597 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2600 DECL_INITIAL(decl
) = init_tree
;
2602 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2603 // See the comment where DECL_WEAK is set in immutable_struct.
2606 DECL_WEAK(decl
) = 0;
2607 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2610 // These variables are often unneeded in the final program, so put
2611 // them in their own section so that linker GC can discard them.
2612 resolve_unique_section(decl
,
2613 compute_reloc_for_constant (init_tree
),
2616 rest_of_decl_compilation(decl
, 1, 0);
2619 // Return a reference to an immutable initialized data structure
2620 // defined in another package.
2623 Gcc_backend::immutable_struct_reference(const std::string
& name
, Btype
* btype
,
2626 tree type_tree
= btype
->get_tree();
2627 if (type_tree
== error_mark_node
)
2628 return this->error_variable();
2629 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2630 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2631 get_identifier_from_string(name
),
2632 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2633 TREE_READONLY(decl
) = 1;
2634 TREE_CONSTANT(decl
) = 1;
2635 DECL_ARTIFICIAL(decl
) = 1;
2636 TREE_PUBLIC(decl
) = 1;
2637 DECL_EXTERNAL(decl
) = 1;
2638 go_preserve_from_gc(decl
);
2639 return new Bvariable(decl
);
2645 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
2651 tree func_tree
= function
->get_tree();
2652 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
2653 push_struct_function(func_tree
);
2655 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
2657 decl
= create_artificial_label(location
.gcc_location());
2663 tree id
= get_identifier_from_string(name
);
2664 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
2666 DECL_CONTEXT(decl
) = function
->get_tree();
2668 return new Blabel(decl
);
2671 // Make a statement which defines a label.
2674 Gcc_backend::label_definition_statement(Blabel
* label
)
2676 tree lab
= label
->get_tree();
2677 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
2678 void_type_node
, lab
);
2679 return this->make_statement(ret
);
2682 // Make a goto statement.
2685 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
2687 tree lab
= label
->get_tree();
2688 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
2690 return this->make_statement(ret
);
2693 // Get the address of a label.
2696 Gcc_backend::label_address(Blabel
* label
, Location location
)
2698 tree lab
= label
->get_tree();
2700 TREE_ADDRESSABLE(lab
) = 1;
2701 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
2702 build_fold_addr_expr_loc(location
.gcc_location(),
2704 return this->make_expression(ret
);
2707 // Declare or define a new function.
2710 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
2711 const std::string
& asm_name
, bool is_visible
,
2712 bool is_declaration
, bool is_inlinable
,
2713 bool disable_split_stack
, bool in_unique_section
,
2716 tree functype
= fntype
->get_tree();
2717 if (functype
!= error_mark_node
)
2719 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
2720 functype
= TREE_TYPE(functype
);
2722 tree id
= get_identifier_from_string(name
);
2723 if (functype
== error_mark_node
|| id
== error_mark_node
)
2724 return this->error_function();
2726 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
2727 if (!asm_name
.empty())
2728 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2730 TREE_PUBLIC(decl
) = 1;
2732 DECL_EXTERNAL(decl
) = 1;
2735 tree restype
= TREE_TYPE(functype
);
2737 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
2738 DECL_ARTIFICIAL(resdecl
) = 1;
2739 DECL_IGNORED_P(resdecl
) = 1;
2740 DECL_CONTEXT(resdecl
) = decl
;
2741 DECL_RESULT(decl
) = resdecl
;
2744 DECL_UNINLINABLE(decl
) = 1;
2745 if (disable_split_stack
)
2747 tree attr
= get_identifier("__no_split_stack__");
2748 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
2750 if (in_unique_section
)
2751 resolve_unique_section(decl
, 0, 1);
2753 go_preserve_from_gc(decl
);
2754 return new Bfunction(decl
);
2757 // Create a statement that runs all deferred calls for FUNCTION. This should
2758 // be a statement that looks like this in C++:
2760 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2763 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
2764 Bexpression
* defer
, Location location
)
2766 tree undefer_tree
= undefer
->get_tree();
2767 tree defer_tree
= defer
->get_tree();
2768 tree fntree
= function
->get_tree();
2770 if (undefer_tree
== error_mark_node
2771 || defer_tree
== error_mark_node
2772 || fntree
== error_mark_node
)
2773 return this->error_statement();
2775 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2776 push_struct_function(fntree
);
2778 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2780 tree stmt_list
= NULL
;
2781 Blabel
* blabel
= this->label(function
, "", location
);
2782 Bstatement
* label_def
= this->label_definition_statement(blabel
);
2783 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
2785 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
2786 tree jump
= jump_stmt
->get_tree();
2787 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
2788 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
2790 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
2791 append_to_statement_list(try_catch
, &stmt_list
);
2794 return this->make_statement(stmt_list
);
2797 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2798 // This will only be called for a function definition.
2801 Gcc_backend::function_set_parameters(Bfunction
* function
,
2802 const std::vector
<Bvariable
*>& param_vars
)
2804 tree func_tree
= function
->get_tree();
2805 if (func_tree
== error_mark_node
)
2808 tree params
= NULL_TREE
;
2810 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
2811 pv
!= param_vars
.end();
2814 *pp
= (*pv
)->get_tree();
2815 gcc_assert(*pp
!= error_mark_node
);
2816 pp
= &DECL_CHAIN(*pp
);
2819 DECL_ARGUMENTS(func_tree
) = params
;
2823 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2826 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
2828 tree func_tree
= function
->get_tree();
2829 tree code
= code_stmt
->get_tree();
2831 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
2833 DECL_SAVED_TREE(func_tree
) = code
;
2837 // Look up a named built-in function in the current backend implementation.
2838 // Returns NULL if no built-in function by that name exists.
2841 Gcc_backend::lookup_builtin(const std::string
& name
)
2843 if (this->builtin_functions_
.count(name
) != 0)
2844 return this->builtin_functions_
[name
];
2848 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
2849 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
2852 Gcc_backend::write_global_definitions(
2853 const std::vector
<Btype
*>& type_decls
,
2854 const std::vector
<Bexpression
*>& constant_decls
,
2855 const std::vector
<Bfunction
*>& function_decls
,
2856 const std::vector
<Bvariable
*>& variable_decls
)
2858 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
2859 + function_decls
.size() + variable_decls
.size();
2861 tree
* defs
= new tree
[count_definitions
];
2863 // Convert all non-erroneous declarations into Gimple form.
2865 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
2866 p
!= variable_decls
.end();
2869 if ((*p
)->get_tree() != error_mark_node
)
2871 defs
[i
] = (*p
)->get_tree();
2872 go_preserve_from_gc(defs
[i
]);
2877 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
2878 p
!= type_decls
.end();
2881 tree type_tree
= (*p
)->get_tree();
2882 if (type_tree
!= error_mark_node
2883 && IS_TYPE_OR_DECL_P(type_tree
))
2885 defs
[i
] = TYPE_NAME(type_tree
);
2886 gcc_assert(defs
[i
] != NULL
);
2887 go_preserve_from_gc(defs
[i
]);
2891 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
2892 p
!= constant_decls
.end();
2895 if ((*p
)->get_tree() != error_mark_node
)
2897 defs
[i
] = (*p
)->get_tree();
2898 go_preserve_from_gc(defs
[i
]);
2902 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
2903 p
!= function_decls
.end();
2906 tree decl
= (*p
)->get_tree();
2907 if (decl
!= error_mark_node
)
2909 go_preserve_from_gc(decl
);
2910 gimplify_function_tree(decl
);
2911 cgraph_finalize_function(decl
, true);
2918 // Pass everything back to the middle-end.
2920 wrapup_global_declarations(defs
, i
);
2922 finalize_compilation_unit();
2924 check_global_declarations(defs
, i
);
2925 emit_debug_global_declarations(defs
, i
);
2930 // Define a builtin function. BCODE is the builtin function code
2931 // defined by builtins.def. NAME is the name of the builtin function.
2932 // LIBNAME is the name of the corresponding library function, and is
2933 // NULL if there isn't one. FNTYPE is the type of the function.
2934 // CONST_P is true if the function has the const attribute.
2937 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
2938 const char* libname
, tree fntype
, bool const_p
)
2940 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
2941 libname
, NULL_TREE
);
2943 TREE_READONLY(decl
) = 1;
2944 set_builtin_decl(bcode
, decl
, true);
2945 this->builtin_functions_
[name
] = this->make_function(decl
);
2946 if (libname
!= NULL
)
2948 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
2951 TREE_READONLY(decl
) = 1;
2952 this->builtin_functions_
[libname
] = this->make_function(decl
);
2956 // Return the backend generator.
2961 return new Gcc_backend();