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"
48 // A class wrapping a tree.
69 // In gcc, types, expressions, and statements are all trees.
70 class Btype
: public Gcc_tree
78 class Bexpression
: public Gcc_tree
86 class Bstatement
: public Gcc_tree
94 class Bfunction
: public Gcc_tree
102 class Bblock
: public Gcc_tree
110 class Bvariable
: public Gcc_tree
118 class Blabel
: public Gcc_tree
126 // This file implements the interface between the Go frontend proper
127 // and the gcc IR. This implements specific instantiations of
128 // abstract classes defined by the Go frontend proper. The Go
129 // frontend proper class methods of these classes to generate the
130 // backend representation.
132 class Gcc_backend
: public Backend
141 { return this->make_type(error_mark_node
); }
145 { return this->make_type(void_type_node
); }
149 { return this->make_type(boolean_type_node
); }
152 integer_type(bool, int);
161 pointer_type(Btype
*);
164 function_type(const Btyped_identifier
&,
165 const std::vector
<Btyped_identifier
>&,
166 const std::vector
<Btyped_identifier
>&,
171 struct_type(const std::vector
<Btyped_identifier
>&);
174 array_type(Btype
*, Bexpression
*);
177 placeholder_pointer_type(const std::string
&, Location
, bool);
180 set_placeholder_pointer_type(Btype
*, Btype
*);
183 set_placeholder_function_type(Btype
*, Btype
*);
186 placeholder_struct_type(const std::string
&, Location
);
189 set_placeholder_struct_type(Btype
* placeholder
,
190 const std::vector
<Btyped_identifier
>&);
193 placeholder_array_type(const std::string
&, Location
);
196 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
199 named_type(const std::string
&, Btype
*, Location
);
202 circular_pointer_type(Btype
*, bool);
205 is_circular_pointer_type(Btype
*);
211 type_alignment(Btype
*);
214 type_field_alignment(Btype
*);
217 type_field_offset(Btype
*, size_t index
);
222 zero_expression(Btype
*);
226 { return this->make_expression(error_mark_node
); }
229 var_expression(Bvariable
* var
, Location
);
232 indirect_expression(Btype
*, Bexpression
* expr
, bool known_valid
, Location
);
235 named_constant_expression(Btype
* btype
, const std::string
& name
,
236 Bexpression
* val
, Location
);
239 integer_constant_expression(Btype
* btype
, mpz_t val
);
242 float_constant_expression(Btype
* btype
, mpfr_t val
);
245 complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
);
248 string_constant_expression(const std::string
& val
);
251 real_part_expression(Bexpression
* bcomplex
, Location
);
254 imag_part_expression(Bexpression
* bcomplex
, Location
);
257 complex_expression(Bexpression
* breal
, Bexpression
* bimag
, Location
);
260 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
263 function_code_expression(Bfunction
*, Location
);
266 address_expression(Bexpression
*, Location
);
269 struct_field_expression(Bexpression
*, size_t, Location
);
272 compound_expression(Bstatement
*, Bexpression
*, Location
);
275 conditional_expression(Btype
*, Bexpression
*, Bexpression
*, Bexpression
*,
279 unary_expression(Operator
, Bexpression
*, Location
);
282 binary_expression(Operator
, Bexpression
*, Bexpression
*, Location
);
285 constructor_expression(Btype
*, const std::vector
<Bexpression
*>&, Location
);
288 array_constructor_expression(Btype
*, const std::vector
<unsigned long>&,
289 const std::vector
<Bexpression
*>&, Location
);
292 pointer_offset_expression(Bexpression
* base
, Bexpression
* offset
, Location
);
295 array_index_expression(Bexpression
* array
, Bexpression
* index
, Location
);
298 call_expression(Bexpression
* fn
, const std::vector
<Bexpression
*>& args
,
305 { return this->make_statement(error_mark_node
); }
308 expression_statement(Bexpression
*);
311 init_statement(Bvariable
* var
, Bexpression
* init
);
314 assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
, Location
);
317 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
321 if_statement(Bexpression
* condition
, Bblock
* then_block
, Bblock
* else_block
,
325 switch_statement(Bfunction
* function
, Bexpression
* value
,
326 const std::vector
<std::vector
<Bexpression
*> >& cases
,
327 const std::vector
<Bstatement
*>& statements
,
331 compound_statement(Bstatement
*, Bstatement
*);
334 statement_list(const std::vector
<Bstatement
*>&);
337 exception_handler_statement(Bstatement
* bstat
, Bstatement
* except_stmt
,
338 Bstatement
* finally_stmt
, Location
);
343 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
347 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
350 block_statement(Bblock
*);
356 { return new Bvariable(error_mark_node
); }
359 global_variable(const std::string
& package_name
,
360 const std::string
& pkgpath
,
361 const std::string
& name
,
365 bool in_unique_section
,
369 global_variable_set_init(Bvariable
*, Bexpression
*);
372 local_variable(Bfunction
*, const std::string
&, Btype
*, bool,
376 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
380 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
381 Location
, Bstatement
**);
384 implicit_variable(const std::string
&, Btype
*, Bexpression
*, bool);
387 immutable_struct(const std::string
&, bool, bool, Btype
*, Location
);
390 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
391 Location
, Bexpression
*);
394 immutable_struct_reference(const std::string
&, Btype
*, Location
);
399 label(Bfunction
*, const std::string
& name
, Location
);
402 label_definition_statement(Blabel
*);
405 goto_statement(Blabel
*, Location
);
408 label_address(Blabel
*, Location
);
414 { return this->make_function(error_mark_node
); }
417 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
418 bool is_visible
, bool is_declaration
, bool is_inlinable
,
419 bool disable_split_stack
, bool in_unique_section
, Location
);
422 function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
423 Bexpression
* defer
, Location
);
426 function_set_parameters(Bfunction
* function
, const std::vector
<Bvariable
*>&);
429 function_set_body(Bfunction
* function
, Bstatement
* code_stmt
);
432 lookup_builtin(const std::string
&);
435 write_global_definitions(const std::vector
<Btype
*>&,
436 const std::vector
<Bexpression
*>&,
437 const std::vector
<Bfunction
*>&,
438 const std::vector
<Bvariable
*>&);
441 // Make a Bexpression from a tree.
443 make_expression(tree t
)
444 { return new Bexpression(t
); }
446 // Make a Bstatement from a tree.
448 make_statement(tree t
)
449 { return new Bstatement(t
); }
451 // Make a Btype from a tree.
454 { return new Btype(t
); }
457 make_function(tree t
)
458 { return new Bfunction(t
); }
461 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
464 fill_in_array(Btype
*, Btype
*, Bexpression
*);
467 non_zero_size_type(tree
);
471 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
472 tree fntype
, bool const_p
);
474 // A mapping of the GCC built-ins exposed to GCCGo.
475 std::map
<std::string
, Bfunction
*> builtin_functions_
;
478 // A helper function.
481 get_identifier_from_string(const std::string
& str
)
483 return get_identifier_with_length(str
.data(), str
.length());
486 // Define the built-in functions that are exposed to GCCGo.
488 Gcc_backend::Gcc_backend()
490 /* We need to define the fetch_and_add functions, since we use them
492 tree t
= this->integer_type(BITS_PER_UNIT
, 1)->get_tree();
493 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
494 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
495 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
498 t
= this->integer_type(BITS_PER_UNIT
* 2, 1)->get_tree();
499 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
500 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
501 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
504 t
= this->integer_type(BITS_PER_UNIT
* 4, 1)->get_tree();
505 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
506 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
507 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
510 t
= this->integer_type(BITS_PER_UNIT
* 8, 1)->get_tree();
511 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
512 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8",
513 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
516 // We use __builtin_expect for magic import functions.
517 this->define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
518 build_function_type_list(long_integer_type_node
,
519 long_integer_type_node
,
520 long_integer_type_node
,
524 // We use __builtin_memcmp for struct comparisons.
525 this->define_builtin(BUILT_IN_MEMCMP
, "__builtin_memcmp", "memcmp",
526 build_function_type_list(integer_type_node
,
533 // We provide some functions for the math library.
534 tree math_function_type
= build_function_type_list(double_type_node
,
537 tree math_function_type_long
=
538 build_function_type_list(long_double_type_node
, long_double_type_node
,
539 long_double_type_node
, NULL_TREE
);
540 tree math_function_type_two
= build_function_type_list(double_type_node
,
544 tree math_function_type_long_two
=
545 build_function_type_list(long_double_type_node
, long_double_type_node
,
546 long_double_type_node
, NULL_TREE
);
547 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
548 math_function_type
, true);
549 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
550 math_function_type_long
, true);
551 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
552 math_function_type
, true);
553 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
554 math_function_type_long
, true);
555 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
556 math_function_type
, true);
557 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
558 math_function_type_long
, true);
559 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
560 math_function_type_two
, true);
561 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
562 math_function_type_long_two
, true);
563 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
564 math_function_type
, true);
565 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
566 math_function_type_long
, true);
567 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
568 math_function_type
, true);
569 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
570 math_function_type_long
, true);
571 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
572 math_function_type
, true);
573 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
574 math_function_type_long
, true);
575 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
576 math_function_type
, true);
577 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
578 math_function_type_long
, true);
579 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
580 math_function_type
, true);
581 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
582 math_function_type_long
, true);
583 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
584 math_function_type
, true);
585 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
586 math_function_type_long
, true);
587 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
588 math_function_type_two
, true);
589 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
590 math_function_type_long_two
, true);
591 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
592 build_function_type_list(double_type_node
,
597 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
598 build_function_type_list(long_double_type_node
,
599 long_double_type_node
,
603 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
604 math_function_type
, true);
605 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
606 math_function_type_long
, true);
607 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
608 math_function_type
, true);
609 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
610 math_function_type_long
, true);
611 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
612 math_function_type
, true);
613 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
614 math_function_type_long
, true);
615 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
616 math_function_type
, true);
617 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
618 math_function_type_long
, true);
619 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
620 math_function_type
, true);
621 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
622 math_function_type_long
, true);
623 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
624 math_function_type
, true);
625 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
626 math_function_type_long
, true);
627 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
628 math_function_type
, true);
629 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
630 math_function_type_long
, true);
631 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
632 math_function_type
, true);
633 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
634 math_function_type_long
, true);
636 // We use __builtin_return_address in the thunk we build for
637 // functions which call recover.
638 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
640 build_function_type_list(ptr_type_node
,
645 // The compiler uses __builtin_trap for some exception handling
647 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
648 build_function_type(void_type_node
, void_list_node
),
652 // Get an unnamed integer type.
655 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
660 if (bits
== INT_TYPE_SIZE
)
661 type
= unsigned_type_node
;
662 else if (bits
== CHAR_TYPE_SIZE
)
663 type
= unsigned_char_type_node
;
664 else if (bits
== SHORT_TYPE_SIZE
)
665 type
= short_unsigned_type_node
;
666 else if (bits
== LONG_TYPE_SIZE
)
667 type
= long_unsigned_type_node
;
668 else if (bits
== LONG_LONG_TYPE_SIZE
)
669 type
= long_long_unsigned_type_node
;
671 type
= make_unsigned_type(bits
);
675 if (bits
== INT_TYPE_SIZE
)
676 type
= integer_type_node
;
677 else if (bits
== CHAR_TYPE_SIZE
)
678 type
= signed_char_type_node
;
679 else if (bits
== SHORT_TYPE_SIZE
)
680 type
= short_integer_type_node
;
681 else if (bits
== LONG_TYPE_SIZE
)
682 type
= long_integer_type_node
;
683 else if (bits
== LONG_LONG_TYPE_SIZE
)
684 type
= long_long_integer_type_node
;
686 type
= make_signed_type(bits
);
688 return this->make_type(type
);
691 // Get an unnamed float type.
694 Gcc_backend::float_type(int bits
)
697 if (bits
== FLOAT_TYPE_SIZE
)
698 type
= float_type_node
;
699 else if (bits
== DOUBLE_TYPE_SIZE
)
700 type
= double_type_node
;
701 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
702 type
= long_double_type_node
;
705 type
= make_node(REAL_TYPE
);
706 TYPE_PRECISION(type
) = bits
;
709 return this->make_type(type
);
712 // Get an unnamed complex type.
715 Gcc_backend::complex_type(int bits
)
718 if (bits
== FLOAT_TYPE_SIZE
* 2)
719 type
= complex_float_type_node
;
720 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
721 type
= complex_double_type_node
;
722 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
723 type
= complex_long_double_type_node
;
726 type
= make_node(REAL_TYPE
);
727 TYPE_PRECISION(type
) = bits
/ 2;
729 type
= build_complex_type(type
);
731 return this->make_type(type
);
734 // Get a pointer type.
737 Gcc_backend::pointer_type(Btype
* to_type
)
739 tree to_type_tree
= to_type
->get_tree();
740 if (to_type_tree
== error_mark_node
)
741 return this->error_type();
742 tree type
= build_pointer_type(to_type_tree
);
743 return this->make_type(type
);
746 // Make a function type.
749 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
750 const std::vector
<Btyped_identifier
>& parameters
,
751 const std::vector
<Btyped_identifier
>& results
,
752 Btype
* result_struct
,
755 tree args
= NULL_TREE
;
757 if (receiver
.btype
!= NULL
)
759 tree t
= receiver
.btype
->get_tree();
760 if (t
== error_mark_node
)
761 return this->error_type();
762 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
763 pp
= &TREE_CHAIN(*pp
);
766 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
767 p
!= parameters
.end();
770 tree t
= p
->btype
->get_tree();
771 if (t
== error_mark_node
)
772 return this->error_type();
773 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
774 pp
= &TREE_CHAIN(*pp
);
777 // Varargs is handled entirely at the Go level. When converted to
778 // GENERIC functions are not varargs.
779 *pp
= void_list_node
;
783 result
= void_type_node
;
784 else if (results
.size() == 1)
785 result
= results
.front().btype
->get_tree();
788 gcc_assert(result_struct
!= NULL
);
789 result
= result_struct
->get_tree();
791 if (result
== error_mark_node
)
792 return this->error_type();
794 tree fntype
= build_function_type(result
, args
);
795 if (fntype
== error_mark_node
)
796 return this->error_type();
798 return this->make_type(build_pointer_type(fntype
));
801 // Make a struct type.
804 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
806 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
809 // Fill in the fields of a struct type.
812 Gcc_backend::fill_in_struct(Btype
* fill
,
813 const std::vector
<Btyped_identifier
>& fields
)
815 tree fill_tree
= fill
->get_tree();
816 tree field_trees
= NULL_TREE
;
817 tree
* pp
= &field_trees
;
818 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
822 tree name_tree
= get_identifier_from_string(p
->name
);
823 tree type_tree
= p
->btype
->get_tree();
824 if (type_tree
== error_mark_node
)
825 return this->error_type();
826 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
828 DECL_CONTEXT(field
) = fill_tree
;
830 pp
= &DECL_CHAIN(field
);
832 TYPE_FIELDS(fill_tree
) = field_trees
;
833 layout_type(fill_tree
);
837 // Make an array type.
840 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
842 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
843 element_btype
, length
);
846 // Fill in an array type.
849 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
852 tree element_type_tree
= element_type
->get_tree();
853 tree length_tree
= length
->get_tree();
854 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
855 return this->error_type();
857 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
859 length_tree
= fold_convert(sizetype
, length_tree
);
861 // build_index_type takes the maximum index, which is one less than
863 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
867 tree fill_tree
= fill
->get_tree();
868 TREE_TYPE(fill_tree
) = element_type_tree
;
869 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
870 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
871 layout_type(fill_tree
);
873 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
874 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
875 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
876 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
877 TYPE_CANONICAL(fill_tree
) =
878 build_array_type(TYPE_CANONICAL(element_type_tree
),
879 TYPE_CANONICAL(index_type_tree
));
884 // Create a placeholder for a pointer type.
887 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
888 Location location
, bool)
890 tree ret
= build_distinct_type_copy(ptr_type_node
);
893 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
894 get_identifier_from_string(name
),
896 TYPE_NAME(ret
) = decl
;
898 return this->make_type(ret
);
901 // Set the real target type for a placeholder pointer type.
904 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
907 tree pt
= placeholder
->get_tree();
908 if (pt
== error_mark_node
)
910 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
911 tree tt
= to_type
->get_tree();
912 if (tt
== error_mark_node
)
914 placeholder
->set_tree(error_mark_node
);
917 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
918 TREE_TYPE(pt
) = TREE_TYPE(tt
);
919 if (TYPE_NAME(pt
) != NULL_TREE
)
921 // Build the data structure gcc wants to see for a typedef.
922 tree copy
= build_variant_type_copy(pt
);
923 TYPE_NAME(copy
) = NULL_TREE
;
924 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
929 // Set the real values for a placeholder function type.
932 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
934 return this->set_placeholder_pointer_type(placeholder
, ft
);
937 // Create a placeholder for a struct type.
940 Gcc_backend::placeholder_struct_type(const std::string
& name
,
943 tree ret
= make_node(RECORD_TYPE
);
946 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
947 get_identifier_from_string(name
),
949 TYPE_NAME(ret
) = decl
;
951 return this->make_type(ret
);
954 // Fill in the fields of a placeholder struct type.
957 Gcc_backend::set_placeholder_struct_type(
959 const std::vector
<Btyped_identifier
>& fields
)
961 tree t
= placeholder
->get_tree();
962 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
963 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
965 if (TYPE_NAME(t
) != NULL_TREE
)
967 // Build the data structure gcc wants to see for a typedef.
968 tree copy
= build_distinct_type_copy(t
);
969 TYPE_NAME(copy
) = NULL_TREE
;
970 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
973 return r
->get_tree() != error_mark_node
;
976 // Create a placeholder for an array type.
979 Gcc_backend::placeholder_array_type(const std::string
& name
,
982 tree ret
= make_node(ARRAY_TYPE
);
983 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
984 get_identifier_from_string(name
),
986 TYPE_NAME(ret
) = decl
;
987 return this->make_type(ret
);
990 // Fill in the fields of a placeholder array type.
993 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
994 Btype
* element_btype
,
997 tree t
= placeholder
->get_tree();
998 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
999 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1001 // Build the data structure gcc wants to see for a typedef.
1002 tree copy
= build_distinct_type_copy(t
);
1003 TYPE_NAME(copy
) = NULL_TREE
;
1004 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1006 return r
->get_tree() != error_mark_node
;
1009 // Return a named version of a type.
1012 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1015 tree type
= btype
->get_tree();
1016 if (type
== error_mark_node
)
1017 return this->error_type();
1019 // The middle-end expects a basic type to have a name. In Go every
1020 // basic type will have a name. The first time we see a basic type,
1021 // give it whatever Go name we have at this point.
1022 if (TYPE_NAME(type
) == NULL_TREE
1023 && location
.gcc_location() == BUILTINS_LOCATION
1024 && (TREE_CODE(type
) == INTEGER_TYPE
1025 || TREE_CODE(type
) == REAL_TYPE
1026 || TREE_CODE(type
) == COMPLEX_TYPE
1027 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1029 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1030 get_identifier_from_string(name
),
1032 TYPE_NAME(type
) = decl
;
1033 return this->make_type(type
);
1036 tree copy
= build_variant_type_copy(type
);
1037 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1038 get_identifier_from_string(name
),
1040 DECL_ORIGINAL_TYPE(decl
) = type
;
1041 TYPE_NAME(copy
) = decl
;
1042 return this->make_type(copy
);
1045 // Return a pointer type used as a marker for a circular type.
1048 Gcc_backend::circular_pointer_type(Btype
*, bool)
1050 return this->make_type(ptr_type_node
);
1053 // Return whether we might be looking at a circular type.
1056 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1058 return btype
->get_tree() == ptr_type_node
;
1061 // Return the size of a type.
1064 Gcc_backend::type_size(Btype
* btype
)
1066 tree t
= btype
->get_tree();
1067 if (t
== error_mark_node
)
1069 t
= TYPE_SIZE_UNIT(t
);
1070 gcc_assert(tree_fits_uhwi_p (t
));
1071 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1072 size_t ret
= static_cast<size_t>(val_wide
);
1073 gcc_assert(ret
== val_wide
);
1077 // Return the alignment of a type.
1080 Gcc_backend::type_alignment(Btype
* btype
)
1082 tree t
= btype
->get_tree();
1083 if (t
== error_mark_node
)
1085 return TYPE_ALIGN_UNIT(t
);
1088 // Return the alignment of a struct field of type BTYPE.
1091 Gcc_backend::type_field_alignment(Btype
* btype
)
1093 tree t
= btype
->get_tree();
1094 if (t
== error_mark_node
)
1096 return go_field_alignment(t
);
1099 // Return the offset of a field in a struct.
1102 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1104 tree struct_tree
= btype
->get_tree();
1105 if (struct_tree
== error_mark_node
)
1107 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1108 tree field
= TYPE_FIELDS(struct_tree
);
1109 for (; index
> 0; --index
)
1111 field
= DECL_CHAIN(field
);
1112 gcc_assert(field
!= NULL_TREE
);
1114 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1115 gcc_assert(offset_wide
>= 0);
1116 size_t ret
= static_cast<size_t>(offset_wide
);
1117 gcc_assert(ret
== static_cast<unsigned HOST_WIDE_INT
>(offset_wide
));
1121 // Return the zero value for a type.
1124 Gcc_backend::zero_expression(Btype
* btype
)
1126 tree t
= btype
->get_tree();
1128 if (t
== error_mark_node
)
1129 ret
= error_mark_node
;
1131 ret
= build_zero_cst(t
);
1132 return tree_to_expr(ret
);
1135 // An expression that references a variable.
1138 Gcc_backend::var_expression(Bvariable
* var
, Location
)
1140 tree ret
= var
->get_tree();
1141 if (ret
== error_mark_node
)
1142 return this->error_expression();
1143 return tree_to_expr(ret
);
1146 // An expression that indirectly references an expression.
1149 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1150 bool known_valid
, Location location
)
1152 tree expr_tree
= expr
->get_tree();
1153 tree type_tree
= btype
->get_tree();
1154 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1155 return this->error_expression();
1157 // If the type of EXPR is a recursive pointer type, then we
1158 // need to insert a cast before indirecting.
1159 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1160 if (VOID_TYPE_P(target_type_tree
))
1161 expr_tree
= fold_convert_loc(location
.gcc_location(),
1162 build_pointer_type(type_tree
), expr_tree
);
1164 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1167 TREE_THIS_NOTRAP(ret
) = 1;
1168 return this->make_expression(ret
);
1171 // Return an expression that declares a constant named NAME with the
1172 // constant value VAL in BTYPE.
1175 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1176 Bexpression
* val
, Location location
)
1178 tree type_tree
= btype
->get_tree();
1179 tree const_val
= val
->get_tree();
1180 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1181 return this->error_expression();
1183 tree name_tree
= get_identifier_from_string(name
);
1184 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1186 DECL_INITIAL(decl
) = const_val
;
1187 TREE_CONSTANT(decl
) = 1;
1188 TREE_READONLY(decl
) = 1;
1190 go_preserve_from_gc(decl
);
1191 return this->make_expression(decl
);
1194 // Return a typed value as a constant integer.
1197 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1199 tree t
= btype
->get_tree();
1200 if (t
== error_mark_node
)
1201 return this->error_expression();
1203 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1204 return tree_to_expr(ret
);
1207 // Return a typed value as a constant floating-point number.
1210 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1212 tree t
= btype
->get_tree();
1214 if (t
== error_mark_node
)
1215 return this->error_expression();
1218 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1220 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1221 ret
= build_real(t
, r2
);
1222 return tree_to_expr(ret
);
1225 // Return a typed real and imaginary value as a constant complex number.
1228 Gcc_backend::complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
)
1230 tree t
= btype
->get_tree();
1232 if (t
== error_mark_node
)
1233 return this->error_expression();
1236 real_from_mpfr(&r1
, real
, TREE_TYPE(t
), GMP_RNDN
);
1238 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1241 real_from_mpfr(&r3
, imag
, TREE_TYPE(t
), GMP_RNDN
);
1243 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1245 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1246 build_real(TREE_TYPE(t
), r4
));
1247 return tree_to_expr(ret
);
1250 // Make a constant string expression.
1253 Gcc_backend::string_constant_expression(const std::string
& val
)
1255 tree index_type
= build_index_type(size_int(val
.length()));
1256 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1258 tree string_type
= build_array_type(const_char_type
, index_type
);
1259 string_type
= build_variant_type_copy(string_type
);
1260 TYPE_STRING_FLAG(string_type
) = 1;
1261 tree string_val
= build_string(val
.length(), val
.data());
1262 TREE_TYPE(string_val
) = string_type
;
1264 return this->make_expression(string_val
);
1267 // Return the real part of a complex expression.
1270 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1272 tree complex_tree
= bcomplex
->get_tree();
1273 if (complex_tree
== error_mark_node
)
1274 return this->error_expression();
1275 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1276 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1277 TREE_TYPE(TREE_TYPE(complex_tree
)),
1279 return this->make_expression(ret
);
1282 // Return the imaginary part of a complex expression.
1285 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1287 tree complex_tree
= bcomplex
->get_tree();
1288 if (complex_tree
== error_mark_node
)
1289 return this->error_expression();
1290 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1291 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1292 TREE_TYPE(TREE_TYPE(complex_tree
)),
1294 return this->make_expression(ret
);
1297 // Make a complex expression given its real and imaginary parts.
1300 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1303 tree real_tree
= breal
->get_tree();
1304 tree imag_tree
= bimag
->get_tree();
1305 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1306 return this->error_expression();
1307 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1308 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1309 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1310 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1311 build_complex_type(TREE_TYPE(real_tree
)),
1312 real_tree
, imag_tree
);
1313 return this->make_expression(ret
);
1316 // An expression that converts an expression to a different type.
1319 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1322 tree type_tree
= type
->get_tree();
1323 tree expr_tree
= expr
->get_tree();
1324 if (type_tree
== error_mark_node
1325 || expr_tree
== error_mark_node
1326 || TREE_TYPE(expr_tree
) == error_mark_node
)
1327 return this->error_expression();
1330 if (this->type_size(type
) == 0)
1332 // Do not convert zero-sized types.
1335 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1336 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1337 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1338 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1339 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1340 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1341 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1342 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1343 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1344 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1345 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1346 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1347 type_tree
, expr_tree
);
1349 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1351 return this->make_expression(ret
);
1354 // Get the address of a function.
1357 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1359 tree func
= bfunc
->get_tree();
1360 if (func
== error_mark_node
)
1361 return this->error_expression();
1363 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1364 return this->make_expression(ret
);
1367 // Get the address of an expression.
1370 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1372 tree expr
= bexpr
->get_tree();
1373 if (expr
== error_mark_node
)
1374 return this->error_expression();
1376 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1377 return this->make_expression(ret
);
1380 // Return an expression for the field at INDEX in BSTRUCT.
1383 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1386 tree struct_tree
= bstruct
->get_tree();
1387 if (struct_tree
== error_mark_node
1388 || TREE_TYPE(struct_tree
) == error_mark_node
)
1389 return this->error_expression();
1390 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1391 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1392 if (field
== NULL_TREE
)
1394 // This can happen for a type which refers to itself indirectly
1395 // and then turns out to be erroneous.
1396 return this->error_expression();
1398 for (unsigned int i
= index
; i
> 0; --i
)
1400 field
= DECL_CHAIN(field
);
1401 gcc_assert(field
!= NULL_TREE
);
1403 if (TREE_TYPE(field
) == error_mark_node
)
1404 return this->error_expression();
1405 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1406 TREE_TYPE(field
), struct_tree
, field
,
1408 if (TREE_CONSTANT(struct_tree
))
1409 TREE_CONSTANT(ret
) = 1;
1410 return tree_to_expr(ret
);
1413 // Return an expression that executes BSTAT before BEXPR.
1416 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1419 tree stat
= bstat
->get_tree();
1420 tree expr
= bexpr
->get_tree();
1421 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1422 return this->error_expression();
1423 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1424 TREE_TYPE(expr
), stat
, expr
);
1425 return this->make_expression(ret
);
1428 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1429 // ELSE_EXPR otherwise.
1432 Gcc_backend::conditional_expression(Btype
* btype
, Bexpression
* condition
,
1433 Bexpression
* then_expr
,
1434 Bexpression
* else_expr
, Location location
)
1436 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1437 tree cond_tree
= condition
->get_tree();
1438 tree then_tree
= then_expr
->get_tree();
1439 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1440 if (type_tree
== error_mark_node
1441 || cond_tree
== error_mark_node
1442 || then_tree
== error_mark_node
1443 || else_tree
== error_mark_node
)
1444 return this->error_expression();
1445 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1446 cond_tree
, then_tree
, else_tree
);
1447 return this->make_expression(ret
);
1450 // Return an expression for the unary operation OP EXPR.
1453 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1455 tree expr_tree
= expr
->get_tree();
1456 if (expr_tree
== error_mark_node
1457 || TREE_TYPE(expr_tree
) == error_mark_node
)
1458 return this->error_expression();
1460 tree type_tree
= TREE_TYPE(expr_tree
);
1461 enum tree_code code
;
1464 case OPERATOR_MINUS
:
1466 tree computed_type
= excess_precision_type(type_tree
);
1467 if (computed_type
!= NULL_TREE
)
1469 expr_tree
= convert(computed_type
, expr_tree
);
1470 type_tree
= computed_type
;
1476 code
= TRUTH_NOT_EXPR
;
1479 code
= BIT_NOT_EXPR
;
1486 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1488 return this->make_expression(ret
);
1491 // Convert a gofrontend operator to an equivalent tree_code.
1493 static enum tree_code
1494 operator_to_tree_code(Operator op
, tree type
)
1496 enum tree_code code
;
1502 case OPERATOR_NOTEQ
:
1518 code
= TRUTH_ORIF_EXPR
;
1520 case OPERATOR_ANDAND
:
1521 code
= TRUTH_ANDIF_EXPR
;
1526 case OPERATOR_MINUS
:
1530 code
= BIT_IOR_EXPR
;
1533 code
= BIT_XOR_EXPR
;
1539 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1542 code
= TRUNC_DIV_EXPR
;
1545 code
= TRUNC_MOD_EXPR
;
1547 case OPERATOR_LSHIFT
:
1550 case OPERATOR_RSHIFT
:
1554 code
= BIT_AND_EXPR
;
1556 case OPERATOR_BITCLEAR
:
1557 code
= BIT_AND_EXPR
;
1566 // Return an expression for the binary operation LEFT OP RIGHT.
1569 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1570 Bexpression
* right
, Location location
)
1572 tree left_tree
= left
->get_tree();
1573 tree right_tree
= right
->get_tree();
1574 if (left_tree
== error_mark_node
1575 || right_tree
== error_mark_node
)
1576 return this->error_expression();
1577 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1579 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1580 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1581 tree computed_type
= excess_precision_type(type_tree
);
1582 if (computed_type
!= NULL_TREE
)
1584 left_tree
= convert(computed_type
, left_tree
);
1585 right_tree
= convert(computed_type
, right_tree
);
1586 type_tree
= computed_type
;
1589 // For comparison operators, the resulting type should be boolean.
1593 case OPERATOR_NOTEQ
:
1598 type_tree
= boolean_type_node
;
1604 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1605 left_tree
, right_tree
);
1606 return this->make_expression(ret
);
1609 // Return an expression that constructs BTYPE with VALS.
1612 Gcc_backend::constructor_expression(Btype
* btype
,
1613 const std::vector
<Bexpression
*>& vals
,
1616 tree type_tree
= btype
->get_tree();
1617 if (type_tree
== error_mark_node
)
1618 return this->error_expression();
1620 vec
<constructor_elt
, va_gc
> *init
;
1621 vec_alloc(init
, vals
.size());
1623 bool is_constant
= true;
1624 tree field
= TYPE_FIELDS(type_tree
);
1625 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1627 ++p
, field
= DECL_CHAIN(field
))
1629 gcc_assert(field
!= NULL_TREE
);
1630 tree val
= (*p
)->get_tree();
1631 if (TREE_TYPE(field
) == error_mark_node
1632 || val
== error_mark_node
1633 || TREE_TYPE(val
) == error_mark_node
)
1634 return this->error_expression();
1636 constructor_elt empty
= {NULL
, NULL
};
1637 constructor_elt
* elt
= init
->quick_push(empty
);
1639 elt
->value
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(field
),
1641 if (!TREE_CONSTANT(elt
->value
))
1642 is_constant
= false;
1644 gcc_assert(field
== NULL_TREE
);
1645 tree ret
= build_constructor(type_tree
, init
);
1647 TREE_CONSTANT(ret
) = 1;
1649 return this->make_expression(ret
);
1653 Gcc_backend::array_constructor_expression(
1654 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1655 const std::vector
<Bexpression
*>& vals
, Location
)
1657 tree type_tree
= array_btype
->get_tree();
1658 if (type_tree
== error_mark_node
)
1659 return this->error_expression();
1661 gcc_assert(indexes
.size() == vals
.size());
1662 vec
<constructor_elt
, va_gc
> *init
;
1663 vec_alloc(init
, vals
.size());
1665 bool is_constant
= true;
1666 for (size_t i
= 0; i
< vals
.size(); ++i
)
1668 tree index
= size_int(indexes
[i
]);
1669 tree val
= (vals
[i
])->get_tree();
1671 if (index
== error_mark_node
1672 || val
== error_mark_node
)
1673 return this->error_expression();
1675 if (!TREE_CONSTANT(val
))
1676 is_constant
= false;
1678 constructor_elt empty
= {NULL
, NULL
};
1679 constructor_elt
* elt
= init
->quick_push(empty
);
1684 tree ret
= build_constructor(type_tree
, init
);
1686 TREE_CONSTANT(ret
) = 1;
1687 return this->make_expression(ret
);
1690 // Return an expression for the address of BASE[INDEX].
1693 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
1696 tree base_tree
= base
->get_tree();
1697 tree index_tree
= index
->get_tree();
1698 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
1699 if (base_tree
== error_mark_node
1700 || TREE_TYPE(base_tree
) == error_mark_node
1701 || index_tree
== error_mark_node
1702 || element_type_tree
== error_mark_node
)
1703 return this->error_expression();
1705 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
1706 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
1707 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
1708 index_tree
, element_size
);
1709 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
1710 TREE_TYPE(base_tree
), base_tree
, offset
);
1711 return this->make_expression(ptr
);
1714 // Return an expression representing ARRAY[INDEX]
1717 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
1720 tree array_tree
= array
->get_tree();
1721 tree index_tree
= index
->get_tree();
1722 if (array_tree
== error_mark_node
1723 || TREE_TYPE(array_tree
) == error_mark_node
1724 || index_tree
== error_mark_node
)
1725 return this->error_expression();
1727 tree ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
1728 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
1729 index_tree
, NULL_TREE
, NULL_TREE
);
1730 return this->make_expression(ret
);
1733 // Create an expression for a call to FN_EXPR with FN_ARGS.
1735 Gcc_backend::call_expression(Bexpression
* fn_expr
,
1736 const std::vector
<Bexpression
*>& fn_args
,
1739 tree fn
= fn_expr
->get_tree();
1740 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
1741 return this->error_expression();
1743 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
1744 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
1746 size_t nargs
= fn_args
.size();
1747 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
1748 for (size_t i
= 0; i
< nargs
; ++i
)
1750 args
[i
] = fn_args
.at(i
)->get_tree();
1751 if (args
[i
] == error_mark_node
)
1752 return this->error_expression();
1756 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
1757 fndecl
= TREE_OPERAND(fndecl
, 0);
1759 // This is to support builtin math functions when using 80387 math.
1760 tree excess_type
= NULL_TREE
;
1762 && TREE_CODE(fndecl
) == FUNCTION_DECL
1763 && DECL_IS_BUILTIN(fndecl
)
1764 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
1766 && ((SCALAR_FLOAT_TYPE_P(rettype
)
1767 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
1768 || (COMPLEX_FLOAT_TYPE_P(rettype
)
1769 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
1771 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
1772 if (excess_type
!= NULL_TREE
)
1774 tree excess_fndecl
= mathfn_built_in(excess_type
,
1775 DECL_FUNCTION_CODE(fndecl
));
1776 if (excess_fndecl
== NULL_TREE
)
1777 excess_type
= NULL_TREE
;
1780 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
1782 for (size_t i
= 0; i
< nargs
; ++i
)
1784 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
1785 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
1786 args
[i
] = ::convert(excess_type
, args
[i
]);
1793 build_call_array_loc(location
.gcc_location(),
1794 excess_type
!= NULL_TREE
? excess_type
: rettype
,
1797 if (excess_type
!= NULL_TREE
)
1799 // Calling convert here can undo our excess precision change.
1800 // That may or may not be a bug in convert_to_real.
1801 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
1805 return this->make_expression(ret
);
1808 // An expression as a statement.
1811 Gcc_backend::expression_statement(Bexpression
* expr
)
1813 return this->make_statement(expr
->get_tree());
1816 // Variable initialization.
1819 Gcc_backend::init_statement(Bvariable
* var
, Bexpression
* init
)
1821 tree var_tree
= var
->get_tree();
1822 tree init_tree
= init
->get_tree();
1823 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1824 return this->error_statement();
1825 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
1827 // To avoid problems with GNU ld, we don't make zero-sized
1828 // externally visible variables. That might lead us to doing an
1829 // initialization of a zero-sized expression to a non-zero sized
1830 // variable, or vice-versa. Avoid crashes by omitting the
1831 // initializer. Such initializations don't mean anything anyhow.
1832 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
1833 && init_tree
!= NULL_TREE
1834 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
1836 DECL_INITIAL(var_tree
) = init_tree
;
1837 init_tree
= NULL_TREE
;
1840 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
1841 void_type_node
, var_tree
);
1842 if (init_tree
!= NULL_TREE
)
1843 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
1844 void_type_node
, init_tree
, ret
);
1846 return this->make_statement(ret
);
1852 Gcc_backend::assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
,
1855 tree lhs_tree
= lhs
->get_tree();
1856 tree rhs_tree
= rhs
->get_tree();
1857 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
1858 return this->error_statement();
1860 // To avoid problems with GNU ld, we don't make zero-sized
1861 // externally visible variables. That might lead us to doing an
1862 // assignment of a zero-sized expression to a non-zero sized
1863 // expression; avoid crashes here by avoiding assignments of
1864 // zero-sized expressions. Such assignments don't really mean
1866 if (int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
1867 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
1868 return this->compound_statement(this->expression_statement(lhs
),
1869 this->expression_statement(rhs
));
1871 // Sometimes the same unnamed Go type can be created multiple times
1872 // and thus have multiple tree representations. Make sure this does
1873 // not confuse the middle-end.
1874 if (TREE_TYPE(lhs_tree
) != TREE_TYPE(rhs_tree
))
1876 tree lhs_type_tree
= TREE_TYPE(lhs_tree
);
1877 gcc_assert(TREE_CODE(lhs_type_tree
) == TREE_CODE(TREE_TYPE(rhs_tree
)));
1878 if (POINTER_TYPE_P(lhs_type_tree
)
1879 || INTEGRAL_TYPE_P(lhs_type_tree
)
1880 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
1881 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
1882 rhs_tree
= fold_convert_loc(location
.gcc_location(), lhs_type_tree
,
1884 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
1885 || TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
)
1887 gcc_assert(int_size_in_bytes(lhs_type_tree
)
1888 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
1889 rhs_tree
= fold_build1_loc(location
.gcc_location(),
1891 lhs_type_tree
, rhs_tree
);
1895 return this->make_statement(fold_build2_loc(location
.gcc_location(),
1898 lhs_tree
, rhs_tree
));
1904 Gcc_backend::return_statement(Bfunction
* bfunction
,
1905 const std::vector
<Bexpression
*>& vals
,
1908 tree fntree
= bfunction
->get_tree();
1909 if (fntree
== error_mark_node
)
1910 return this->error_statement();
1911 tree result
= DECL_RESULT(fntree
);
1912 if (result
== error_mark_node
)
1913 return this->error_statement();
1917 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
1919 else if (vals
.size() == 1)
1921 tree val
= vals
.front()->get_tree();
1922 if (val
== error_mark_node
)
1923 return this->error_statement();
1924 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1925 void_type_node
, result
,
1926 vals
.front()->get_tree());
1927 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1928 void_type_node
, set
);
1932 // To return multiple values, copy the values into a temporary
1933 // variable of the right structure type, and then assign the
1934 // temporary variable to the DECL_RESULT in the return
1936 tree stmt_list
= NULL_TREE
;
1937 tree rettype
= TREE_TYPE(result
);
1939 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
1940 push_struct_function(fntree
);
1942 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
1943 tree rettmp
= create_tmp_var(rettype
, "RESULT");
1946 tree field
= TYPE_FIELDS(rettype
);
1947 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1949 p
++, field
= DECL_CHAIN(field
))
1951 gcc_assert(field
!= NULL_TREE
);
1952 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1953 TREE_TYPE(field
), rettmp
, field
,
1955 tree val
= (*p
)->get_tree();
1956 if (val
== error_mark_node
)
1957 return this->error_statement();
1958 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1960 ref
, (*p
)->get_tree());
1961 append_to_statement_list(set
, &stmt_list
);
1963 gcc_assert(field
== NULL_TREE
);
1964 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1967 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1968 void_type_node
, set
);
1969 append_to_statement_list(ret_expr
, &stmt_list
);
1972 return this->make_statement(ret
);
1975 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
1976 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
1977 // NULL, it will always be executed. This is used for handling defers in Go
1978 // functions. In C++, the resulting code is of this form:
1979 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
1982 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
1983 Bstatement
* except_stmt
,
1984 Bstatement
* finally_stmt
,
1987 tree stat_tree
= bstat
->get_tree();
1988 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
1989 tree finally_tree
= finally_stmt
== NULL
1991 : finally_stmt
->get_tree();
1993 if (stat_tree
== error_mark_node
1994 || except_tree
== error_mark_node
1995 || finally_tree
== error_mark_node
)
1996 return this->error_statement();
1998 if (except_tree
!= NULL_TREE
)
1999 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2000 void_type_node
, stat_tree
,
2001 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2002 void_type_node
, NULL
, except_tree
));
2003 if (finally_tree
!= NULL_TREE
)
2004 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2005 void_type_node
, stat_tree
, finally_tree
);
2006 return this->make_statement(stat_tree
);
2012 Gcc_backend::if_statement(Bexpression
* condition
, Bblock
* then_block
,
2013 Bblock
* else_block
, Location location
)
2015 tree cond_tree
= condition
->get_tree();
2016 tree then_tree
= then_block
->get_tree();
2017 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2018 if (cond_tree
== error_mark_node
2019 || then_tree
== error_mark_node
2020 || else_tree
== error_mark_node
)
2021 return this->error_statement();
2022 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2023 cond_tree
, then_tree
, else_tree
);
2024 return this->make_statement(ret
);
2030 Gcc_backend::switch_statement(
2031 Bfunction
* function
,
2033 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2034 const std::vector
<Bstatement
*>& statements
,
2035 Location switch_location
)
2037 gcc_assert(cases
.size() == statements
.size());
2039 tree decl
= function
->get_tree();
2040 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2041 push_struct_function(decl
);
2043 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2045 tree stmt_list
= NULL_TREE
;
2046 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2047 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2048 ps
!= statements
.end();
2053 source_location loc
= (*ps
!= NULL
2054 ? EXPR_LOCATION((*ps
)->get_tree())
2055 : UNKNOWN_LOCATION
);
2056 tree label
= create_artificial_label(loc
);
2057 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2058 append_to_statement_list(c
, &stmt_list
);
2062 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2066 tree t
= (*pcv
)->get_tree();
2067 if (t
== error_mark_node
)
2068 return this->error_statement();
2069 source_location loc
= EXPR_LOCATION(t
);
2070 tree label
= create_artificial_label(loc
);
2071 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2072 append_to_statement_list(c
, &stmt_list
);
2078 tree t
= (*ps
)->get_tree();
2079 if (t
== error_mark_node
)
2080 return this->error_statement();
2081 append_to_statement_list(t
, &stmt_list
);
2086 tree tv
= value
->get_tree();
2087 if (tv
== error_mark_node
)
2088 return this->error_statement();
2089 tree t
= build3_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2090 NULL_TREE
, tv
, stmt_list
, NULL_TREE
);
2091 return this->make_statement(t
);
2094 // Pair of statements.
2097 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2099 tree stmt_list
= NULL_TREE
;
2100 tree t
= s1
->get_tree();
2101 if (t
== error_mark_node
)
2102 return this->error_statement();
2103 append_to_statement_list(t
, &stmt_list
);
2105 if (t
== error_mark_node
)
2106 return this->error_statement();
2107 append_to_statement_list(t
, &stmt_list
);
2108 return this->make_statement(stmt_list
);
2111 // List of statements.
2114 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2116 tree stmt_list
= NULL_TREE
;
2117 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2118 p
!= statements
.end();
2121 tree t
= (*p
)->get_tree();
2122 if (t
== error_mark_node
)
2123 return this->error_statement();
2124 append_to_statement_list(t
, &stmt_list
);
2126 return this->make_statement(stmt_list
);
2129 // Make a block. For some reason gcc uses a dual structure for
2130 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2131 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2135 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2136 const std::vector
<Bvariable
*>& vars
,
2137 Location start_location
,
2140 tree block_tree
= make_node(BLOCK
);
2141 if (enclosing
== NULL
)
2143 tree fndecl
= function
->get_tree();
2144 gcc_assert(fndecl
!= NULL_TREE
);
2146 // We may have already created a block for local variables when
2147 // we take the address of a parameter.
2148 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2150 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2151 DECL_INITIAL(fndecl
) = block_tree
;
2155 tree superblock_tree
= DECL_INITIAL(fndecl
);
2156 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2158 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2160 pp
= &BLOCK_CHAIN(*pp
))
2167 tree superbind_tree
= enclosing
->get_tree();
2168 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2169 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2171 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2173 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2175 pp
= &BLOCK_CHAIN(*pp
))
2180 tree
* pp
= &BLOCK_VARS(block_tree
);
2181 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2185 *pp
= (*pv
)->get_tree();
2186 if (*pp
!= error_mark_node
)
2187 pp
= &DECL_CHAIN(*pp
);
2191 TREE_USED(block_tree
) = 1;
2193 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2194 void_type_node
, BLOCK_VARS(block_tree
),
2195 NULL_TREE
, block_tree
);
2196 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2197 return new Bblock(bind_tree
);
2200 // Add statements to a block.
2203 Gcc_backend::block_add_statements(Bblock
* bblock
,
2204 const std::vector
<Bstatement
*>& statements
)
2206 tree stmt_list
= NULL_TREE
;
2207 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2208 p
!= statements
.end();
2211 tree s
= (*p
)->get_tree();
2212 if (s
!= error_mark_node
)
2213 append_to_statement_list(s
, &stmt_list
);
2216 tree bind_tree
= bblock
->get_tree();
2217 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2218 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2221 // Return a block as a statement.
2224 Gcc_backend::block_statement(Bblock
* bblock
)
2226 tree bind_tree
= bblock
->get_tree();
2227 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2228 return this->make_statement(bind_tree
);
2231 // This is not static because we declare it with GTY(()) in go-c.h.
2232 tree go_non_zero_struct
;
2234 // Return a type corresponding to TYPE with non-zero size.
2237 Gcc_backend::non_zero_size_type(tree type
)
2239 if (int_size_in_bytes(type
) != 0)
2242 switch (TREE_CODE(type
))
2245 if (TYPE_FIELDS(type
) != NULL_TREE
)
2247 tree ns
= make_node(RECORD_TYPE
);
2248 tree field_trees
= NULL_TREE
;
2249 tree
*pp
= &field_trees
;
2250 for (tree field
= TYPE_FIELDS(type
);
2252 field
= DECL_CHAIN(field
))
2254 tree ft
= TREE_TYPE(field
);
2255 if (field
== TYPE_FIELDS(type
))
2256 ft
= non_zero_size_type(ft
);
2257 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2258 DECL_NAME(field
), ft
);
2259 DECL_CONTEXT(f
) = ns
;
2261 pp
= &DECL_CHAIN(f
);
2263 TYPE_FIELDS(ns
) = field_trees
;
2268 if (go_non_zero_struct
== NULL_TREE
)
2270 type
= make_node(RECORD_TYPE
);
2271 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2272 get_identifier("dummy"),
2274 DECL_CONTEXT(field
) = type
;
2275 TYPE_FIELDS(type
) = field
;
2277 go_non_zero_struct
= type
;
2279 return go_non_zero_struct
;
2283 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2284 return build_array_type_nelts(element_type
, 1);
2294 // Make a global variable.
2297 Gcc_backend::global_variable(const std::string
& package_name
,
2298 const std::string
& pkgpath
,
2299 const std::string
& name
,
2303 bool in_unique_section
,
2306 tree type_tree
= btype
->get_tree();
2307 if (type_tree
== error_mark_node
)
2308 return this->error_variable();
2310 // The GNU linker does not like dynamic variables with zero size.
2311 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2312 type_tree
= this->non_zero_size_type(type_tree
);
2314 std::string
var_name(package_name
);
2315 var_name
.push_back('.');
2316 var_name
.append(name
);
2317 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2318 get_identifier_from_string(var_name
),
2321 DECL_EXTERNAL(decl
) = 1;
2323 TREE_STATIC(decl
) = 1;
2326 TREE_PUBLIC(decl
) = 1;
2328 std::string
asm_name(pkgpath
);
2329 asm_name
.push_back('.');
2330 asm_name
.append(name
);
2331 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2333 TREE_USED(decl
) = 1;
2335 if (in_unique_section
)
2336 resolve_unique_section (decl
, 0, 1);
2338 go_preserve_from_gc(decl
);
2340 return new Bvariable(decl
);
2343 // Set the initial value of a global variable.
2346 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2348 tree expr_tree
= expr
->get_tree();
2349 if (expr_tree
== error_mark_node
)
2351 gcc_assert(TREE_CONSTANT(expr_tree
));
2352 tree var_decl
= var
->get_tree();
2353 if (var_decl
== error_mark_node
)
2355 DECL_INITIAL(var_decl
) = expr_tree
;
2357 // If this variable goes in a unique section, it may need to go into
2358 // a different one now that DECL_INITIAL is set.
2359 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl
))
2361 DECL_SECTION_NAME (var_decl
) = NULL_TREE
;
2362 resolve_unique_section (var_decl
,
2363 compute_reloc_for_constant (expr_tree
),
2368 // Make a local variable.
2371 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2372 Btype
* btype
, bool is_address_taken
,
2375 tree type_tree
= btype
->get_tree();
2376 if (type_tree
== error_mark_node
)
2377 return this->error_variable();
2378 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2379 get_identifier_from_string(name
),
2381 DECL_CONTEXT(decl
) = function
->get_tree();
2382 TREE_USED(decl
) = 1;
2383 if (is_address_taken
)
2384 TREE_ADDRESSABLE(decl
) = 1;
2385 go_preserve_from_gc(decl
);
2386 return new Bvariable(decl
);
2389 // Make a function parameter variable.
2392 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2393 Btype
* btype
, bool is_address_taken
,
2396 tree type_tree
= btype
->get_tree();
2397 if (type_tree
== error_mark_node
)
2398 return this->error_variable();
2399 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2400 get_identifier_from_string(name
),
2402 DECL_CONTEXT(decl
) = function
->get_tree();
2403 DECL_ARG_TYPE(decl
) = type_tree
;
2404 TREE_USED(decl
) = 1;
2405 if (is_address_taken
)
2406 TREE_ADDRESSABLE(decl
) = 1;
2407 go_preserve_from_gc(decl
);
2408 return new Bvariable(decl
);
2411 // Make a temporary variable.
2414 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2415 Btype
* btype
, Bexpression
* binit
,
2416 bool is_address_taken
,
2418 Bstatement
** pstatement
)
2420 gcc_assert(function
!= NULL
);
2421 tree decl
= function
->get_tree();
2422 tree type_tree
= btype
->get_tree();
2423 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2424 if (type_tree
== error_mark_node
2425 || init_tree
== error_mark_node
2426 || decl
== error_mark_node
)
2428 *pstatement
= this->error_statement();
2429 return this->error_variable();
2433 // We can only use create_tmp_var if the type is not addressable.
2434 if (!TREE_ADDRESSABLE(type_tree
))
2436 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2437 push_struct_function(decl
);
2439 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2441 var
= create_tmp_var(type_tree
, "GOTMP");
2446 gcc_assert(bblock
!= NULL
);
2447 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2448 create_tmp_var_name("GOTMP"),
2450 DECL_ARTIFICIAL(var
) = 1;
2451 DECL_IGNORED_P(var
) = 1;
2453 DECL_CONTEXT(var
) = decl
;
2455 // We have to add this variable to the BLOCK and the BIND_EXPR.
2456 tree bind_tree
= bblock
->get_tree();
2457 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2458 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2459 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2460 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2461 BLOCK_VARS(block_tree
) = var
;
2462 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2465 if (init_tree
!= NULL_TREE
)
2466 DECL_INITIAL(var
) = fold_convert_loc(location
.gcc_location(), type_tree
,
2469 if (is_address_taken
)
2470 TREE_ADDRESSABLE(var
) = 1;
2472 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2474 void_type_node
, var
));
2475 return new Bvariable(var
);
2478 // Create an implicit variable that is compiler-defined. This is used when
2479 // generating GC root variables and storing the values of a slice initializer.
2482 Gcc_backend::implicit_variable(const std::string
& name
, Btype
* type
,
2483 Bexpression
* init
, bool is_constant
)
2485 tree type_tree
= type
->get_tree();
2486 tree init_tree
= init
->get_tree();
2487 if (type_tree
== error_mark_node
|| init_tree
== error_mark_node
)
2488 return this->error_variable();
2490 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2491 get_identifier_from_string(name
), type_tree
);
2492 DECL_EXTERNAL(decl
) = 0;
2493 TREE_PUBLIC(decl
) = 0;
2494 TREE_STATIC(decl
) = 1;
2495 DECL_ARTIFICIAL(decl
) = 1;
2498 TREE_READONLY(decl
) = 1;
2499 TREE_CONSTANT(decl
) = 1;
2501 DECL_INITIAL(decl
) = init_tree
;
2502 rest_of_decl_compilation(decl
, 1, 0);
2504 return new Bvariable(decl
);
2507 // Create a named immutable initialized data structure.
2510 Gcc_backend::immutable_struct(const std::string
& name
, bool is_hidden
,
2511 bool is_common
, Btype
* btype
, Location location
)
2513 tree type_tree
= btype
->get_tree();
2514 if (type_tree
== error_mark_node
)
2515 return this->error_variable();
2516 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2517 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2518 get_identifier_from_string(name
),
2519 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2520 TREE_STATIC(decl
) = 1;
2521 TREE_USED(decl
) = 1;
2522 TREE_READONLY(decl
) = 1;
2523 TREE_CONSTANT(decl
) = 1;
2524 DECL_ARTIFICIAL(decl
) = 1;
2526 TREE_PUBLIC(decl
) = 1;
2528 // When the initializer for one immutable_struct refers to another,
2529 // it needs to know the visibility of the referenced struct so that
2530 // compute_reloc_for_constant will return the right value. On many
2531 // systems calling make_decl_one_only will mark the decl as weak,
2532 // which will change the return value of compute_reloc_for_constant.
2533 // We can't reliably call make_decl_one_only yet, because we don't
2534 // yet know the initializer. This issue doesn't arise in C because
2535 // Go initializers, unlike C initializers, can be indirectly
2536 // recursive. To ensure that compute_reloc_for_constant computes
2537 // the right value if some other initializer refers to this one, we
2538 // mark this symbol as weak here. We undo that below in
2539 // immutable_struct_set_init before calling mark_decl_one_only.
2541 DECL_WEAK(decl
) = 1;
2543 // We don't call rest_of_decl_compilation until we have the
2546 go_preserve_from_gc(decl
);
2547 return new Bvariable(decl
);
2550 // Set the initializer for a variable created by immutable_struct.
2551 // This is where we finish compiling the variable.
2554 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
2555 bool, bool is_common
, Btype
*, Location
,
2556 Bexpression
* initializer
)
2558 tree decl
= var
->get_tree();
2559 tree init_tree
= initializer
->get_tree();
2560 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2563 DECL_INITIAL(decl
) = init_tree
;
2565 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2566 // See the comment where DECL_WEAK is set in immutable_struct.
2569 DECL_WEAK(decl
) = 0;
2570 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2573 // These variables are often unneeded in the final program, so put
2574 // them in their own section so that linker GC can discard them.
2575 resolve_unique_section(decl
,
2576 compute_reloc_for_constant (init_tree
),
2579 rest_of_decl_compilation(decl
, 1, 0);
2582 // Return a reference to an immutable initialized data structure
2583 // defined in another package.
2586 Gcc_backend::immutable_struct_reference(const std::string
& name
, Btype
* btype
,
2589 tree type_tree
= btype
->get_tree();
2590 if (type_tree
== error_mark_node
)
2591 return this->error_variable();
2592 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2593 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2594 get_identifier_from_string(name
),
2595 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2596 TREE_READONLY(decl
) = 1;
2597 TREE_CONSTANT(decl
) = 1;
2598 DECL_ARTIFICIAL(decl
) = 1;
2599 TREE_PUBLIC(decl
) = 1;
2600 DECL_EXTERNAL(decl
) = 1;
2601 go_preserve_from_gc(decl
);
2602 return new Bvariable(decl
);
2608 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
2614 tree func_tree
= function
->get_tree();
2615 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
2616 push_struct_function(func_tree
);
2618 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
2620 decl
= create_artificial_label(location
.gcc_location());
2626 tree id
= get_identifier_from_string(name
);
2627 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
2629 DECL_CONTEXT(decl
) = function
->get_tree();
2631 return new Blabel(decl
);
2634 // Make a statement which defines a label.
2637 Gcc_backend::label_definition_statement(Blabel
* label
)
2639 tree lab
= label
->get_tree();
2640 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
2641 void_type_node
, lab
);
2642 return this->make_statement(ret
);
2645 // Make a goto statement.
2648 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
2650 tree lab
= label
->get_tree();
2651 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
2653 return this->make_statement(ret
);
2656 // Get the address of a label.
2659 Gcc_backend::label_address(Blabel
* label
, Location location
)
2661 tree lab
= label
->get_tree();
2663 TREE_ADDRESSABLE(lab
) = 1;
2664 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
2665 build_fold_addr_expr_loc(location
.gcc_location(),
2667 return this->make_expression(ret
);
2670 // Declare or define a new function.
2673 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
2674 const std::string
& asm_name
, bool is_visible
,
2675 bool is_declaration
, bool is_inlinable
,
2676 bool disable_split_stack
, bool in_unique_section
,
2679 tree functype
= fntype
->get_tree();
2680 if (functype
!= error_mark_node
)
2682 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
2683 functype
= TREE_TYPE(functype
);
2685 tree id
= get_identifier_from_string(name
);
2686 if (functype
== error_mark_node
|| id
== error_mark_node
)
2687 return this->error_function();
2689 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
2690 if (!asm_name
.empty())
2691 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2693 TREE_PUBLIC(decl
) = 1;
2695 DECL_EXTERNAL(decl
) = 1;
2698 tree restype
= TREE_TYPE(functype
);
2700 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
2701 DECL_ARTIFICIAL(resdecl
) = 1;
2702 DECL_IGNORED_P(resdecl
) = 1;
2703 DECL_CONTEXT(resdecl
) = decl
;
2704 DECL_RESULT(decl
) = resdecl
;
2707 DECL_UNINLINABLE(decl
) = 1;
2708 if (disable_split_stack
)
2710 tree attr
= get_identifier("__no_split_stack__");
2711 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
2713 if (in_unique_section
)
2714 resolve_unique_section(decl
, 0, 1);
2716 go_preserve_from_gc(decl
);
2717 return new Bfunction(decl
);
2720 // Create a statement that runs all deferred calls for FUNCTION. This should
2721 // be a statement that looks like this in C++:
2723 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2726 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
2727 Bexpression
* defer
, Location location
)
2729 tree undefer_tree
= undefer
->get_tree();
2730 tree defer_tree
= defer
->get_tree();
2731 tree fntree
= function
->get_tree();
2733 if (undefer_tree
== error_mark_node
2734 || defer_tree
== error_mark_node
2735 || fntree
== error_mark_node
)
2736 return this->error_statement();
2738 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2739 push_struct_function(fntree
);
2741 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2743 tree stmt_list
= NULL
;
2744 Blabel
* blabel
= this->label(function
, "", location
);
2745 Bstatement
* label_def
= this->label_definition_statement(blabel
);
2746 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
2748 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
2749 tree jump
= jump_stmt
->get_tree();
2750 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
2751 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
2753 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
2754 append_to_statement_list(try_catch
, &stmt_list
);
2757 return this->make_statement(stmt_list
);
2760 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2761 // This will only be called for a function definition.
2764 Gcc_backend::function_set_parameters(Bfunction
* function
,
2765 const std::vector
<Bvariable
*>& param_vars
)
2767 tree func_tree
= function
->get_tree();
2768 if (func_tree
== error_mark_node
)
2771 tree params
= NULL_TREE
;
2773 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
2774 pv
!= param_vars
.end();
2777 *pp
= (*pv
)->get_tree();
2778 gcc_assert(*pp
!= error_mark_node
);
2779 pp
= &DECL_CHAIN(*pp
);
2782 DECL_ARGUMENTS(func_tree
) = params
;
2786 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2789 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
2791 tree func_tree
= function
->get_tree();
2792 tree code
= code_stmt
->get_tree();
2794 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
2796 DECL_SAVED_TREE(func_tree
) = code
;
2800 // Look up a named built-in function in the current backend implementation.
2801 // Returns NULL if no built-in function by that name exists.
2804 Gcc_backend::lookup_builtin(const std::string
& name
)
2806 if (this->builtin_functions_
.count(name
) != 0)
2807 return this->builtin_functions_
[name
];
2811 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
2812 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
2815 Gcc_backend::write_global_definitions(
2816 const std::vector
<Btype
*>& type_decls
,
2817 const std::vector
<Bexpression
*>& constant_decls
,
2818 const std::vector
<Bfunction
*>& function_decls
,
2819 const std::vector
<Bvariable
*>& variable_decls
)
2821 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
2822 + function_decls
.size() + variable_decls
.size();
2824 tree
* defs
= new tree
[count_definitions
];
2826 // Convert all non-erroneous declarations into Gimple form.
2828 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
2829 p
!= variable_decls
.end();
2832 if ((*p
)->get_tree() != error_mark_node
)
2834 defs
[i
] = (*p
)->get_tree();
2835 go_preserve_from_gc(defs
[i
]);
2840 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
2841 p
!= type_decls
.end();
2844 tree type_tree
= (*p
)->get_tree();
2845 if (type_tree
!= error_mark_node
2846 && IS_TYPE_OR_DECL_P(type_tree
))
2848 defs
[i
] = TYPE_NAME(type_tree
);
2849 gcc_assert(defs
[i
] != NULL
);
2850 go_preserve_from_gc(defs
[i
]);
2854 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
2855 p
!= constant_decls
.end();
2858 if ((*p
)->get_tree() != error_mark_node
)
2860 defs
[i
] = (*p
)->get_tree();
2861 go_preserve_from_gc(defs
[i
]);
2865 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
2866 p
!= function_decls
.end();
2869 tree decl
= (*p
)->get_tree();
2870 if (decl
!= error_mark_node
)
2872 go_preserve_from_gc(decl
);
2873 gimplify_function_tree(decl
);
2874 cgraph_finalize_function(decl
, true);
2881 // Pass everything back to the middle-end.
2883 wrapup_global_declarations(defs
, i
);
2885 finalize_compilation_unit();
2887 check_global_declarations(defs
, i
);
2888 emit_debug_global_declarations(defs
, i
);
2893 // Define a builtin function. BCODE is the builtin function code
2894 // defined by builtins.def. NAME is the name of the builtin function.
2895 // LIBNAME is the name of the corresponding library function, and is
2896 // NULL if there isn't one. FNTYPE is the type of the function.
2897 // CONST_P is true if the function has the const attribute.
2900 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
2901 const char* libname
, tree fntype
, bool const_p
)
2903 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
2904 libname
, NULL_TREE
);
2906 TREE_READONLY(decl
) = 1;
2907 set_builtin_decl(bcode
, decl
, true);
2908 this->builtin_functions_
[name
] = this->make_function(decl
);
2909 if (libname
!= NULL
)
2911 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
2914 TREE_READONLY(decl
) = 1;
2915 this->builtin_functions_
[libname
] = this->make_function(decl
);
2919 // Return the backend generator.
2924 return new Gcc_backend();
2927 // FIXME: Temporary functions while converting to the new backend
2931 tree_to_type(tree t
)
2933 return new Btype(t
);
2937 tree_to_expr(tree t
)
2939 return new Bexpression(t
);
2943 tree_to_stat(tree t
)
2945 return new Bstatement(t
);
2949 tree_to_function(tree t
)
2951 return new Bfunction(t
);
2955 tree_to_block(tree t
)
2957 gcc_assert(TREE_CODE(t
) == BIND_EXPR
);
2958 return new Bblock(t
);
2962 type_to_tree(Btype
* bt
)
2964 return bt
->get_tree();
2968 expr_to_tree(Bexpression
* be
)
2970 return be
->get_tree();
2974 stat_to_tree(Bstatement
* bs
)
2976 return bs
->get_tree();
2980 block_to_tree(Bblock
* bb
)
2982 return bb
->get_tree();
2986 var_to_tree(Bvariable
* bv
)
2988 return bv
->get_tree();
2992 function_to_tree(Bfunction
* bf
)
2994 return bf
->get_tree();