1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2015 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.
32 #include "fold-const.h"
33 #include "stringpool.h"
34 #include "stor-layout.h"
36 #include "tree-iterator.h"
39 #include "hard-reg-set.h"
43 #include "gimple-expr.h"
45 #include "langhooks.h"
56 // A class wrapping a tree.
77 // In gcc, types, expressions, and statements are all trees.
78 class Btype
: public Gcc_tree
86 class Bexpression
: public Gcc_tree
94 class Bstatement
: public Gcc_tree
102 class Bfunction
: public Gcc_tree
110 class Bblock
: public Gcc_tree
118 class Bvariable
: public Gcc_tree
126 class Blabel
: public Gcc_tree
134 // This file implements the interface between the Go frontend proper
135 // and the gcc IR. This implements specific instantiations of
136 // abstract classes defined by the Go frontend proper. The Go
137 // frontend proper class methods of these classes to generate the
138 // backend representation.
140 class Gcc_backend
: public Backend
149 { return this->make_type(error_mark_node
); }
153 { return this->make_type(void_type_node
); }
157 { return this->make_type(boolean_type_node
); }
160 integer_type(bool, int);
169 pointer_type(Btype
*);
172 function_type(const Btyped_identifier
&,
173 const std::vector
<Btyped_identifier
>&,
174 const std::vector
<Btyped_identifier
>&,
179 struct_type(const std::vector
<Btyped_identifier
>&);
182 array_type(Btype
*, Bexpression
*);
185 placeholder_pointer_type(const std::string
&, Location
, bool);
188 set_placeholder_pointer_type(Btype
*, Btype
*);
191 set_placeholder_function_type(Btype
*, Btype
*);
194 placeholder_struct_type(const std::string
&, Location
);
197 set_placeholder_struct_type(Btype
* placeholder
,
198 const std::vector
<Btyped_identifier
>&);
201 placeholder_array_type(const std::string
&, Location
);
204 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
207 named_type(const std::string
&, Btype
*, Location
);
210 circular_pointer_type(Btype
*, bool);
213 is_circular_pointer_type(Btype
*);
219 type_alignment(Btype
*);
222 type_field_alignment(Btype
*);
225 type_field_offset(Btype
*, size_t index
);
230 zero_expression(Btype
*);
234 { return this->make_expression(error_mark_node
); }
237 nil_pointer_expression()
238 { return this->make_expression(null_pointer_node
); }
241 var_expression(Bvariable
* var
, Location
);
244 indirect_expression(Btype
*, Bexpression
* expr
, bool known_valid
, Location
);
247 named_constant_expression(Btype
* btype
, const std::string
& name
,
248 Bexpression
* val
, Location
);
251 integer_constant_expression(Btype
* btype
, mpz_t val
);
254 float_constant_expression(Btype
* btype
, mpfr_t val
);
257 complex_constant_expression(Btype
* btype
, mpc_t val
);
260 string_constant_expression(const std::string
& val
);
263 boolean_constant_expression(bool val
);
266 real_part_expression(Bexpression
* bcomplex
, Location
);
269 imag_part_expression(Bexpression
* bcomplex
, Location
);
272 complex_expression(Bexpression
* breal
, Bexpression
* bimag
, Location
);
275 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
278 function_code_expression(Bfunction
*, Location
);
281 address_expression(Bexpression
*, Location
);
284 struct_field_expression(Bexpression
*, size_t, Location
);
287 compound_expression(Bstatement
*, Bexpression
*, Location
);
290 conditional_expression(Btype
*, Bexpression
*, Bexpression
*, Bexpression
*,
294 unary_expression(Operator
, Bexpression
*, Location
);
297 binary_expression(Operator
, Bexpression
*, Bexpression
*, Location
);
300 constructor_expression(Btype
*, const std::vector
<Bexpression
*>&, Location
);
303 array_constructor_expression(Btype
*, const std::vector
<unsigned long>&,
304 const std::vector
<Bexpression
*>&, Location
);
307 pointer_offset_expression(Bexpression
* base
, Bexpression
* offset
, Location
);
310 array_index_expression(Bexpression
* array
, Bexpression
* index
, Location
);
313 call_expression(Bexpression
* fn
, const std::vector
<Bexpression
*>& args
,
314 Bexpression
* static_chain
, Location
);
317 stack_allocation_expression(int64_t size
, Location
);
323 { return this->make_statement(error_mark_node
); }
326 expression_statement(Bexpression
*);
329 init_statement(Bvariable
* var
, Bexpression
* init
);
332 assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
, Location
);
335 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
339 if_statement(Bexpression
* condition
, Bblock
* then_block
, Bblock
* else_block
,
343 switch_statement(Bfunction
* function
, Bexpression
* value
,
344 const std::vector
<std::vector
<Bexpression
*> >& cases
,
345 const std::vector
<Bstatement
*>& statements
,
349 compound_statement(Bstatement
*, Bstatement
*);
352 statement_list(const std::vector
<Bstatement
*>&);
355 exception_handler_statement(Bstatement
* bstat
, Bstatement
* except_stmt
,
356 Bstatement
* finally_stmt
, Location
);
361 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
365 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
368 block_statement(Bblock
*);
374 { return new Bvariable(error_mark_node
); }
377 global_variable(const std::string
& package_name
,
378 const std::string
& pkgpath
,
379 const std::string
& name
,
383 bool in_unique_section
,
387 global_variable_set_init(Bvariable
*, Bexpression
*);
390 local_variable(Bfunction
*, const std::string
&, Btype
*, bool,
394 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
398 static_chain_variable(Bfunction
*, const std::string
&, Btype
*, Location
);
401 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
402 Location
, Bstatement
**);
405 implicit_variable(const std::string
&, Btype
*, bool, bool, bool,
409 implicit_variable_set_init(Bvariable
*, const std::string
&, Btype
*,
410 bool, bool, bool, Bexpression
*);
413 implicit_variable_reference(const std::string
&, Btype
*);
416 immutable_struct(const std::string
&, bool, bool, Btype
*, Location
);
419 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
420 Location
, Bexpression
*);
423 immutable_struct_reference(const std::string
&, Btype
*, Location
);
428 label(Bfunction
*, const std::string
& name
, Location
);
431 label_definition_statement(Blabel
*);
434 goto_statement(Blabel
*, Location
);
437 label_address(Blabel
*, Location
);
443 { return this->make_function(error_mark_node
); }
446 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
447 bool is_visible
, bool is_declaration
, bool is_inlinable
,
448 bool disable_split_stack
, bool in_unique_section
, Location
);
451 function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
452 Bexpression
* defer
, Location
);
455 function_set_parameters(Bfunction
* function
, const std::vector
<Bvariable
*>&);
458 function_set_body(Bfunction
* function
, Bstatement
* code_stmt
);
461 lookup_builtin(const std::string
&);
464 write_global_definitions(const std::vector
<Btype
*>&,
465 const std::vector
<Bexpression
*>&,
466 const std::vector
<Bfunction
*>&,
467 const std::vector
<Bvariable
*>&);
470 // Make a Bexpression from a tree.
472 make_expression(tree t
)
473 { return new Bexpression(t
); }
475 // Make a Bstatement from a tree.
477 make_statement(tree t
)
478 { return new Bstatement(t
); }
480 // Make a Btype from a tree.
483 { return new Btype(t
); }
486 make_function(tree t
)
487 { return new Bfunction(t
); }
490 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
493 fill_in_array(Btype
*, Btype
*, Bexpression
*);
496 non_zero_size_type(tree
);
500 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
501 tree fntype
, bool const_p
);
503 // A mapping of the GCC built-ins exposed to GCCGo.
504 std::map
<std::string
, Bfunction
*> builtin_functions_
;
507 // A helper function.
510 get_identifier_from_string(const std::string
& str
)
512 return get_identifier_with_length(str
.data(), str
.length());
515 // Define the built-in functions that are exposed to GCCGo.
517 Gcc_backend::Gcc_backend()
519 /* We need to define the fetch_and_add functions, since we use them
521 tree t
= this->integer_type(BITS_PER_UNIT
, 1)->get_tree();
522 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
523 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
524 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
527 t
= this->integer_type(BITS_PER_UNIT
* 2, 1)->get_tree();
528 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
529 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
530 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
533 t
= this->integer_type(BITS_PER_UNIT
* 4, 1)->get_tree();
534 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
535 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
536 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
539 t
= this->integer_type(BITS_PER_UNIT
* 8, 1)->get_tree();
540 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
541 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8",
542 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
545 // We use __builtin_expect for magic import functions.
546 this->define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
547 build_function_type_list(long_integer_type_node
,
548 long_integer_type_node
,
549 long_integer_type_node
,
553 // We use __builtin_memcmp for struct comparisons.
554 this->define_builtin(BUILT_IN_MEMCMP
, "__builtin_memcmp", "memcmp",
555 build_function_type_list(integer_type_node
,
562 // We provide some functions for the math library.
563 tree math_function_type
= build_function_type_list(double_type_node
,
566 tree math_function_type_long
=
567 build_function_type_list(long_double_type_node
, long_double_type_node
,
568 long_double_type_node
, NULL_TREE
);
569 tree math_function_type_two
= build_function_type_list(double_type_node
,
573 tree math_function_type_long_two
=
574 build_function_type_list(long_double_type_node
, long_double_type_node
,
575 long_double_type_node
, NULL_TREE
);
576 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
577 math_function_type
, true);
578 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
579 math_function_type_long
, true);
580 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
581 math_function_type
, true);
582 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
583 math_function_type_long
, true);
584 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
585 math_function_type
, true);
586 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
587 math_function_type_long
, true);
588 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
589 math_function_type_two
, true);
590 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
591 math_function_type_long_two
, true);
592 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
593 math_function_type
, true);
594 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
595 math_function_type_long
, true);
596 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
597 math_function_type
, true);
598 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
599 math_function_type_long
, true);
600 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
601 math_function_type
, true);
602 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
603 math_function_type_long
, true);
604 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
605 math_function_type
, true);
606 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
607 math_function_type_long
, true);
608 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
609 math_function_type
, true);
610 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
611 math_function_type_long
, true);
612 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
613 math_function_type
, true);
614 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
615 math_function_type_long
, true);
616 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
617 math_function_type_two
, true);
618 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
619 math_function_type_long_two
, true);
620 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
621 build_function_type_list(double_type_node
,
626 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
627 build_function_type_list(long_double_type_node
,
628 long_double_type_node
,
632 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
633 math_function_type
, true);
634 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
635 math_function_type_long
, true);
636 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
637 math_function_type
, true);
638 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
639 math_function_type_long
, true);
640 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
641 math_function_type
, true);
642 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
643 math_function_type_long
, true);
644 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
645 math_function_type
, true);
646 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
647 math_function_type_long
, true);
648 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
649 math_function_type
, true);
650 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
651 math_function_type_long
, true);
652 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
653 math_function_type
, true);
654 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
655 math_function_type_long
, true);
656 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
657 math_function_type
, true);
658 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
659 math_function_type_long
, true);
660 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
661 math_function_type
, true);
662 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
663 math_function_type_long
, true);
665 // We use __builtin_return_address in the thunk we build for
666 // functions which call recover.
667 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
669 build_function_type_list(ptr_type_node
,
674 // The compiler uses __builtin_trap for some exception handling
676 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
677 build_function_type(void_type_node
, void_list_node
),
681 // Get an unnamed integer type.
684 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
689 if (bits
== INT_TYPE_SIZE
)
690 type
= unsigned_type_node
;
691 else if (bits
== CHAR_TYPE_SIZE
)
692 type
= unsigned_char_type_node
;
693 else if (bits
== SHORT_TYPE_SIZE
)
694 type
= short_unsigned_type_node
;
695 else if (bits
== LONG_TYPE_SIZE
)
696 type
= long_unsigned_type_node
;
697 else if (bits
== LONG_LONG_TYPE_SIZE
)
698 type
= long_long_unsigned_type_node
;
700 type
= make_unsigned_type(bits
);
704 if (bits
== INT_TYPE_SIZE
)
705 type
= integer_type_node
;
706 else if (bits
== CHAR_TYPE_SIZE
)
707 type
= signed_char_type_node
;
708 else if (bits
== SHORT_TYPE_SIZE
)
709 type
= short_integer_type_node
;
710 else if (bits
== LONG_TYPE_SIZE
)
711 type
= long_integer_type_node
;
712 else if (bits
== LONG_LONG_TYPE_SIZE
)
713 type
= long_long_integer_type_node
;
715 type
= make_signed_type(bits
);
717 return this->make_type(type
);
720 // Get an unnamed float type.
723 Gcc_backend::float_type(int bits
)
726 if (bits
== FLOAT_TYPE_SIZE
)
727 type
= float_type_node
;
728 else if (bits
== DOUBLE_TYPE_SIZE
)
729 type
= double_type_node
;
730 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
731 type
= long_double_type_node
;
734 type
= make_node(REAL_TYPE
);
735 TYPE_PRECISION(type
) = bits
;
738 return this->make_type(type
);
741 // Get an unnamed complex type.
744 Gcc_backend::complex_type(int bits
)
747 if (bits
== FLOAT_TYPE_SIZE
* 2)
748 type
= complex_float_type_node
;
749 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
750 type
= complex_double_type_node
;
751 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
752 type
= complex_long_double_type_node
;
755 type
= make_node(REAL_TYPE
);
756 TYPE_PRECISION(type
) = bits
/ 2;
758 type
= build_complex_type(type
);
760 return this->make_type(type
);
763 // Get a pointer type.
766 Gcc_backend::pointer_type(Btype
* to_type
)
768 tree to_type_tree
= to_type
->get_tree();
769 if (to_type_tree
== error_mark_node
)
770 return this->error_type();
771 tree type
= build_pointer_type(to_type_tree
);
772 return this->make_type(type
);
775 // Make a function type.
778 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
779 const std::vector
<Btyped_identifier
>& parameters
,
780 const std::vector
<Btyped_identifier
>& results
,
781 Btype
* result_struct
,
784 tree args
= NULL_TREE
;
786 if (receiver
.btype
!= NULL
)
788 tree t
= receiver
.btype
->get_tree();
789 if (t
== error_mark_node
)
790 return this->error_type();
791 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
792 pp
= &TREE_CHAIN(*pp
);
795 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
796 p
!= parameters
.end();
799 tree t
= p
->btype
->get_tree();
800 if (t
== error_mark_node
)
801 return this->error_type();
802 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
803 pp
= &TREE_CHAIN(*pp
);
806 // Varargs is handled entirely at the Go level. When converted to
807 // GENERIC functions are not varargs.
808 *pp
= void_list_node
;
812 result
= void_type_node
;
813 else if (results
.size() == 1)
814 result
= results
.front().btype
->get_tree();
817 gcc_assert(result_struct
!= NULL
);
818 result
= result_struct
->get_tree();
820 if (result
== error_mark_node
)
821 return this->error_type();
823 tree fntype
= build_function_type(result
, args
);
824 if (fntype
== error_mark_node
)
825 return this->error_type();
827 return this->make_type(build_pointer_type(fntype
));
830 // Make a struct type.
833 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
835 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
838 // Fill in the fields of a struct type.
841 Gcc_backend::fill_in_struct(Btype
* fill
,
842 const std::vector
<Btyped_identifier
>& fields
)
844 tree fill_tree
= fill
->get_tree();
845 tree field_trees
= NULL_TREE
;
846 tree
* pp
= &field_trees
;
847 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
851 tree name_tree
= get_identifier_from_string(p
->name
);
852 tree type_tree
= p
->btype
->get_tree();
853 if (type_tree
== error_mark_node
)
854 return this->error_type();
855 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
857 DECL_CONTEXT(field
) = fill_tree
;
859 pp
= &DECL_CHAIN(field
);
861 TYPE_FIELDS(fill_tree
) = field_trees
;
862 layout_type(fill_tree
);
866 // Make an array type.
869 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
871 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
872 element_btype
, length
);
875 // Fill in an array type.
878 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
881 tree element_type_tree
= element_type
->get_tree();
882 tree length_tree
= length
->get_tree();
883 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
884 return this->error_type();
886 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
888 length_tree
= fold_convert(sizetype
, length_tree
);
890 // build_index_type takes the maximum index, which is one less than
892 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
896 tree fill_tree
= fill
->get_tree();
897 TREE_TYPE(fill_tree
) = element_type_tree
;
898 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
899 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
900 layout_type(fill_tree
);
902 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
903 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
904 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
905 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
906 TYPE_CANONICAL(fill_tree
) =
907 build_array_type(TYPE_CANONICAL(element_type_tree
),
908 TYPE_CANONICAL(index_type_tree
));
913 // Create a placeholder for a pointer type.
916 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
917 Location location
, bool)
919 tree ret
= build_distinct_type_copy(ptr_type_node
);
922 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
923 get_identifier_from_string(name
),
925 TYPE_NAME(ret
) = decl
;
927 return this->make_type(ret
);
930 // Set the real target type for a placeholder pointer type.
933 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
936 tree pt
= placeholder
->get_tree();
937 if (pt
== error_mark_node
)
939 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
940 tree tt
= to_type
->get_tree();
941 if (tt
== error_mark_node
)
943 placeholder
->set_tree(error_mark_node
);
946 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
947 TREE_TYPE(pt
) = TREE_TYPE(tt
);
948 if (TYPE_NAME(pt
) != NULL_TREE
)
950 // Build the data structure gcc wants to see for a typedef.
951 tree copy
= build_variant_type_copy(pt
);
952 TYPE_NAME(copy
) = NULL_TREE
;
953 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
958 // Set the real values for a placeholder function type.
961 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
963 return this->set_placeholder_pointer_type(placeholder
, ft
);
966 // Create a placeholder for a struct type.
969 Gcc_backend::placeholder_struct_type(const std::string
& name
,
972 tree ret
= make_node(RECORD_TYPE
);
975 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
976 get_identifier_from_string(name
),
978 TYPE_NAME(ret
) = decl
;
980 return this->make_type(ret
);
983 // Fill in the fields of a placeholder struct type.
986 Gcc_backend::set_placeholder_struct_type(
988 const std::vector
<Btyped_identifier
>& fields
)
990 tree t
= placeholder
->get_tree();
991 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
992 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
994 if (TYPE_NAME(t
) != NULL_TREE
)
996 // Build the data structure gcc wants to see for a typedef.
997 tree copy
= build_distinct_type_copy(t
);
998 TYPE_NAME(copy
) = NULL_TREE
;
999 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1002 return r
->get_tree() != error_mark_node
;
1005 // Create a placeholder for an array type.
1008 Gcc_backend::placeholder_array_type(const std::string
& name
,
1011 tree ret
= make_node(ARRAY_TYPE
);
1012 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1013 get_identifier_from_string(name
),
1015 TYPE_NAME(ret
) = decl
;
1016 return this->make_type(ret
);
1019 // Fill in the fields of a placeholder array type.
1022 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1023 Btype
* element_btype
,
1024 Bexpression
* length
)
1026 tree t
= placeholder
->get_tree();
1027 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1028 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1030 // Build the data structure gcc wants to see for a typedef.
1031 tree copy
= build_distinct_type_copy(t
);
1032 TYPE_NAME(copy
) = NULL_TREE
;
1033 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1035 return r
->get_tree() != error_mark_node
;
1038 // Return a named version of a type.
1041 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1044 tree type
= btype
->get_tree();
1045 if (type
== error_mark_node
)
1046 return this->error_type();
1048 // The middle-end expects a basic type to have a name. In Go every
1049 // basic type will have a name. The first time we see a basic type,
1050 // give it whatever Go name we have at this point.
1051 if (TYPE_NAME(type
) == NULL_TREE
1052 && location
.gcc_location() == BUILTINS_LOCATION
1053 && (TREE_CODE(type
) == INTEGER_TYPE
1054 || TREE_CODE(type
) == REAL_TYPE
1055 || TREE_CODE(type
) == COMPLEX_TYPE
1056 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1058 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1059 get_identifier_from_string(name
),
1061 TYPE_NAME(type
) = decl
;
1062 return this->make_type(type
);
1065 tree copy
= build_variant_type_copy(type
);
1066 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1067 get_identifier_from_string(name
),
1069 DECL_ORIGINAL_TYPE(decl
) = type
;
1070 TYPE_NAME(copy
) = decl
;
1071 return this->make_type(copy
);
1074 // Return a pointer type used as a marker for a circular type.
1077 Gcc_backend::circular_pointer_type(Btype
*, bool)
1079 return this->make_type(ptr_type_node
);
1082 // Return whether we might be looking at a circular type.
1085 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1087 return btype
->get_tree() == ptr_type_node
;
1090 // Return the size of a type.
1093 Gcc_backend::type_size(Btype
* btype
)
1095 tree t
= btype
->get_tree();
1096 if (t
== error_mark_node
)
1098 t
= TYPE_SIZE_UNIT(t
);
1099 gcc_assert(tree_fits_uhwi_p (t
));
1100 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1101 int64_t ret
= static_cast<int64_t>(val_wide
);
1102 gcc_assert(ret
>= 0 && static_cast<unsigned HOST_WIDE_INT
>(ret
) == val_wide
);
1106 // Return the alignment of a type.
1109 Gcc_backend::type_alignment(Btype
* btype
)
1111 tree t
= btype
->get_tree();
1112 if (t
== error_mark_node
)
1114 return TYPE_ALIGN_UNIT(t
);
1117 // Return the alignment of a struct field of type BTYPE.
1120 Gcc_backend::type_field_alignment(Btype
* btype
)
1122 tree t
= btype
->get_tree();
1123 if (t
== error_mark_node
)
1125 return go_field_alignment(t
);
1128 // Return the offset of a field in a struct.
1131 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1133 tree struct_tree
= btype
->get_tree();
1134 if (struct_tree
== error_mark_node
)
1136 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1137 tree field
= TYPE_FIELDS(struct_tree
);
1138 for (; index
> 0; --index
)
1140 field
= DECL_CHAIN(field
);
1141 gcc_assert(field
!= NULL_TREE
);
1143 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1144 int64_t ret
= static_cast<int64_t>(offset_wide
);
1145 gcc_assert(ret
== offset_wide
);
1149 // Return the zero value for a type.
1152 Gcc_backend::zero_expression(Btype
* btype
)
1154 tree t
= btype
->get_tree();
1156 if (t
== error_mark_node
)
1157 ret
= error_mark_node
;
1159 ret
= build_zero_cst(t
);
1160 return this->make_expression(ret
);
1163 // An expression that references a variable.
1166 Gcc_backend::var_expression(Bvariable
* var
, Location
)
1168 tree ret
= var
->get_tree();
1169 if (ret
== error_mark_node
)
1170 return this->error_expression();
1171 return this->make_expression(ret
);
1174 // An expression that indirectly references an expression.
1177 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1178 bool known_valid
, Location location
)
1180 tree expr_tree
= expr
->get_tree();
1181 tree type_tree
= btype
->get_tree();
1182 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1183 return this->error_expression();
1185 // If the type of EXPR is a recursive pointer type, then we
1186 // need to insert a cast before indirecting.
1187 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1188 if (VOID_TYPE_P(target_type_tree
))
1189 expr_tree
= fold_convert_loc(location
.gcc_location(),
1190 build_pointer_type(type_tree
), expr_tree
);
1192 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1195 TREE_THIS_NOTRAP(ret
) = 1;
1196 return this->make_expression(ret
);
1199 // Return an expression that declares a constant named NAME with the
1200 // constant value VAL in BTYPE.
1203 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1204 Bexpression
* val
, Location location
)
1206 tree type_tree
= btype
->get_tree();
1207 tree const_val
= val
->get_tree();
1208 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1209 return this->error_expression();
1211 tree name_tree
= get_identifier_from_string(name
);
1212 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1214 DECL_INITIAL(decl
) = const_val
;
1215 TREE_CONSTANT(decl
) = 1;
1216 TREE_READONLY(decl
) = 1;
1218 go_preserve_from_gc(decl
);
1219 return this->make_expression(decl
);
1222 // Return a typed value as a constant integer.
1225 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1227 tree t
= btype
->get_tree();
1228 if (t
== error_mark_node
)
1229 return this->error_expression();
1231 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1232 return this->make_expression(ret
);
1235 // Return a typed value as a constant floating-point number.
1238 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1240 tree t
= btype
->get_tree();
1242 if (t
== error_mark_node
)
1243 return this->error_expression();
1246 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1248 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1249 ret
= build_real(t
, r2
);
1250 return this->make_expression(ret
);
1253 // Return a typed real and imaginary value as a constant complex number.
1256 Gcc_backend::complex_constant_expression(Btype
* btype
, mpc_t val
)
1258 tree t
= btype
->get_tree();
1260 if (t
== error_mark_node
)
1261 return this->error_expression();
1264 real_from_mpfr(&r1
, mpc_realref(val
), TREE_TYPE(t
), GMP_RNDN
);
1266 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1269 real_from_mpfr(&r3
, mpc_imagref(val
), TREE_TYPE(t
), GMP_RNDN
);
1271 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1273 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1274 build_real(TREE_TYPE(t
), r4
));
1275 return this->make_expression(ret
);
1278 // Make a constant string expression.
1281 Gcc_backend::string_constant_expression(const std::string
& val
)
1283 tree index_type
= build_index_type(size_int(val
.length()));
1284 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1286 tree string_type
= build_array_type(const_char_type
, index_type
);
1287 string_type
= build_variant_type_copy(string_type
);
1288 TYPE_STRING_FLAG(string_type
) = 1;
1289 tree string_val
= build_string(val
.length(), val
.data());
1290 TREE_TYPE(string_val
) = string_type
;
1292 return this->make_expression(string_val
);
1295 // Make a constant boolean expression.
1298 Gcc_backend::boolean_constant_expression(bool val
)
1300 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1301 return this->make_expression(bool_cst
);
1304 // Return the real part of a complex expression.
1307 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1309 tree complex_tree
= bcomplex
->get_tree();
1310 if (complex_tree
== error_mark_node
)
1311 return this->error_expression();
1312 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1313 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1314 TREE_TYPE(TREE_TYPE(complex_tree
)),
1316 return this->make_expression(ret
);
1319 // Return the imaginary part of a complex expression.
1322 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1324 tree complex_tree
= bcomplex
->get_tree();
1325 if (complex_tree
== error_mark_node
)
1326 return this->error_expression();
1327 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1328 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1329 TREE_TYPE(TREE_TYPE(complex_tree
)),
1331 return this->make_expression(ret
);
1334 // Make a complex expression given its real and imaginary parts.
1337 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1340 tree real_tree
= breal
->get_tree();
1341 tree imag_tree
= bimag
->get_tree();
1342 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1343 return this->error_expression();
1344 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1345 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1346 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1347 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1348 build_complex_type(TREE_TYPE(real_tree
)),
1349 real_tree
, imag_tree
);
1350 return this->make_expression(ret
);
1353 // An expression that converts an expression to a different type.
1356 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1359 tree type_tree
= type
->get_tree();
1360 tree expr_tree
= expr
->get_tree();
1361 if (type_tree
== error_mark_node
1362 || expr_tree
== error_mark_node
1363 || TREE_TYPE(expr_tree
) == error_mark_node
)
1364 return this->error_expression();
1367 if (this->type_size(type
) == 0)
1369 // Do not convert zero-sized types.
1372 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1373 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1374 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1375 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1376 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1377 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1378 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1379 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1380 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1381 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1382 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1383 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1384 type_tree
, expr_tree
);
1386 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1388 return this->make_expression(ret
);
1391 // Get the address of a function.
1394 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1396 tree func
= bfunc
->get_tree();
1397 if (func
== error_mark_node
)
1398 return this->error_expression();
1400 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1401 return this->make_expression(ret
);
1404 // Get the address of an expression.
1407 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1409 tree expr
= bexpr
->get_tree();
1410 if (expr
== error_mark_node
)
1411 return this->error_expression();
1413 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1414 return this->make_expression(ret
);
1417 // Return an expression for the field at INDEX in BSTRUCT.
1420 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1423 tree struct_tree
= bstruct
->get_tree();
1424 if (struct_tree
== error_mark_node
1425 || TREE_TYPE(struct_tree
) == error_mark_node
)
1426 return this->error_expression();
1427 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1428 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1429 if (field
== NULL_TREE
)
1431 // This can happen for a type which refers to itself indirectly
1432 // and then turns out to be erroneous.
1433 return this->error_expression();
1435 for (unsigned int i
= index
; i
> 0; --i
)
1437 field
= DECL_CHAIN(field
);
1438 gcc_assert(field
!= NULL_TREE
);
1440 if (TREE_TYPE(field
) == error_mark_node
)
1441 return this->error_expression();
1442 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1443 TREE_TYPE(field
), struct_tree
, field
,
1445 if (TREE_CONSTANT(struct_tree
))
1446 TREE_CONSTANT(ret
) = 1;
1447 return this->make_expression(ret
);
1450 // Return an expression that executes BSTAT before BEXPR.
1453 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1456 tree stat
= bstat
->get_tree();
1457 tree expr
= bexpr
->get_tree();
1458 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1459 return this->error_expression();
1460 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1461 TREE_TYPE(expr
), stat
, expr
);
1462 return this->make_expression(ret
);
1465 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1466 // ELSE_EXPR otherwise.
1469 Gcc_backend::conditional_expression(Btype
* btype
, Bexpression
* condition
,
1470 Bexpression
* then_expr
,
1471 Bexpression
* else_expr
, Location location
)
1473 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1474 tree cond_tree
= condition
->get_tree();
1475 tree then_tree
= then_expr
->get_tree();
1476 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1477 if (type_tree
== error_mark_node
1478 || cond_tree
== error_mark_node
1479 || then_tree
== error_mark_node
1480 || else_tree
== error_mark_node
)
1481 return this->error_expression();
1482 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1483 cond_tree
, then_tree
, else_tree
);
1484 return this->make_expression(ret
);
1487 // Return an expression for the unary operation OP EXPR.
1490 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1492 tree expr_tree
= expr
->get_tree();
1493 if (expr_tree
== error_mark_node
1494 || TREE_TYPE(expr_tree
) == error_mark_node
)
1495 return this->error_expression();
1497 tree type_tree
= TREE_TYPE(expr_tree
);
1498 enum tree_code code
;
1501 case OPERATOR_MINUS
:
1503 tree computed_type
= excess_precision_type(type_tree
);
1504 if (computed_type
!= NULL_TREE
)
1506 expr_tree
= convert(computed_type
, expr_tree
);
1507 type_tree
= computed_type
;
1513 code
= TRUTH_NOT_EXPR
;
1516 code
= BIT_NOT_EXPR
;
1523 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1525 return this->make_expression(ret
);
1528 // Convert a gofrontend operator to an equivalent tree_code.
1530 static enum tree_code
1531 operator_to_tree_code(Operator op
, tree type
)
1533 enum tree_code code
;
1539 case OPERATOR_NOTEQ
:
1555 code
= TRUTH_ORIF_EXPR
;
1557 case OPERATOR_ANDAND
:
1558 code
= TRUTH_ANDIF_EXPR
;
1563 case OPERATOR_MINUS
:
1567 code
= BIT_IOR_EXPR
;
1570 code
= BIT_XOR_EXPR
;
1576 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1579 code
= TRUNC_DIV_EXPR
;
1582 code
= TRUNC_MOD_EXPR
;
1584 case OPERATOR_LSHIFT
:
1587 case OPERATOR_RSHIFT
:
1591 code
= BIT_AND_EXPR
;
1593 case OPERATOR_BITCLEAR
:
1594 code
= BIT_AND_EXPR
;
1603 // Return an expression for the binary operation LEFT OP RIGHT.
1606 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1607 Bexpression
* right
, Location location
)
1609 tree left_tree
= left
->get_tree();
1610 tree right_tree
= right
->get_tree();
1611 if (left_tree
== error_mark_node
1612 || right_tree
== error_mark_node
)
1613 return this->error_expression();
1614 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1616 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1617 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1618 tree computed_type
= excess_precision_type(type_tree
);
1619 if (computed_type
!= NULL_TREE
)
1621 left_tree
= convert(computed_type
, left_tree
);
1622 right_tree
= convert(computed_type
, right_tree
);
1623 type_tree
= computed_type
;
1626 // For comparison operators, the resulting type should be boolean.
1630 case OPERATOR_NOTEQ
:
1635 type_tree
= boolean_type_node
;
1641 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1642 left_tree
, right_tree
);
1643 return this->make_expression(ret
);
1646 // Return an expression that constructs BTYPE with VALS.
1649 Gcc_backend::constructor_expression(Btype
* btype
,
1650 const std::vector
<Bexpression
*>& vals
,
1653 tree type_tree
= btype
->get_tree();
1654 if (type_tree
== error_mark_node
)
1655 return this->error_expression();
1657 vec
<constructor_elt
, va_gc
> *init
;
1658 vec_alloc(init
, vals
.size());
1660 tree sink
= NULL_TREE
;
1661 bool is_constant
= true;
1662 tree field
= TYPE_FIELDS(type_tree
);
1663 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1665 ++p
, field
= DECL_CHAIN(field
))
1667 gcc_assert(field
!= NULL_TREE
);
1668 tree val
= (*p
)->get_tree();
1669 if (TREE_TYPE(field
) == error_mark_node
1670 || val
== error_mark_node
1671 || TREE_TYPE(val
) == error_mark_node
)
1672 return this->error_expression();
1674 if (int_size_in_bytes(TREE_TYPE(field
)) == 0)
1676 // GIMPLE cannot represent indices of zero-sized types so
1677 // trying to construct a map with zero-sized keys might lead
1678 // to errors. Instead, we evaluate each expression that
1679 // would have been added as a map element for its
1680 // side-effects and construct an empty map.
1681 append_to_statement_list(val
, &sink
);
1685 constructor_elt empty
= {NULL
, NULL
};
1686 constructor_elt
* elt
= init
->quick_push(empty
);
1688 elt
->value
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(field
),
1690 if (!TREE_CONSTANT(elt
->value
))
1691 is_constant
= false;
1693 gcc_assert(field
== NULL_TREE
);
1694 tree ret
= build_constructor(type_tree
, init
);
1696 TREE_CONSTANT(ret
) = 1;
1697 if (sink
!= NULL_TREE
)
1698 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1699 type_tree
, sink
, ret
);
1700 return this->make_expression(ret
);
1704 Gcc_backend::array_constructor_expression(
1705 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1706 const std::vector
<Bexpression
*>& vals
, Location location
)
1708 tree type_tree
= array_btype
->get_tree();
1709 if (type_tree
== error_mark_node
)
1710 return this->error_expression();
1712 gcc_assert(indexes
.size() == vals
.size());
1714 tree element_type
= TREE_TYPE(type_tree
);
1715 HOST_WIDE_INT element_size
= int_size_in_bytes(element_type
);
1716 vec
<constructor_elt
, va_gc
> *init
;
1717 vec_alloc(init
, element_size
== 0 ? 0 : vals
.size());
1719 tree sink
= NULL_TREE
;
1720 bool is_constant
= true;
1721 for (size_t i
= 0; i
< vals
.size(); ++i
)
1723 tree index
= size_int(indexes
[i
]);
1724 tree val
= (vals
[i
])->get_tree();
1726 if (index
== error_mark_node
1727 || val
== error_mark_node
)
1728 return this->error_expression();
1730 if (element_size
== 0)
1732 // GIMPLE cannot represent arrays of zero-sized types so trying
1733 // to construct an array of zero-sized values might lead to errors.
1734 // Instead, we evaluate each expression that would have been added as
1735 // an array value for its side-effects and construct an empty array.
1736 append_to_statement_list(val
, &sink
);
1740 if (!TREE_CONSTANT(val
))
1741 is_constant
= false;
1743 constructor_elt empty
= {NULL
, NULL
};
1744 constructor_elt
* elt
= init
->quick_push(empty
);
1749 tree ret
= build_constructor(type_tree
, init
);
1751 TREE_CONSTANT(ret
) = 1;
1752 if (sink
!= NULL_TREE
)
1753 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1754 type_tree
, sink
, ret
);
1755 return this->make_expression(ret
);
1758 // Return an expression for the address of BASE[INDEX].
1761 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
1764 tree base_tree
= base
->get_tree();
1765 tree index_tree
= index
->get_tree();
1766 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
1767 if (base_tree
== error_mark_node
1768 || TREE_TYPE(base_tree
) == error_mark_node
1769 || index_tree
== error_mark_node
1770 || element_type_tree
== error_mark_node
)
1771 return this->error_expression();
1773 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
1774 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
1775 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
1776 index_tree
, element_size
);
1777 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
1778 TREE_TYPE(base_tree
), base_tree
, offset
);
1779 return this->make_expression(ptr
);
1782 // Return an expression representing ARRAY[INDEX]
1785 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
1788 tree array_tree
= array
->get_tree();
1789 tree index_tree
= index
->get_tree();
1790 if (array_tree
== error_mark_node
1791 || TREE_TYPE(array_tree
) == error_mark_node
1792 || index_tree
== error_mark_node
)
1793 return this->error_expression();
1795 tree ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
1796 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
1797 index_tree
, NULL_TREE
, NULL_TREE
);
1798 return this->make_expression(ret
);
1801 // Create an expression for a call to FN_EXPR with FN_ARGS.
1803 Gcc_backend::call_expression(Bexpression
* fn_expr
,
1804 const std::vector
<Bexpression
*>& fn_args
,
1805 Bexpression
* chain_expr
, Location location
)
1807 tree fn
= fn_expr
->get_tree();
1808 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
1809 return this->error_expression();
1811 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
1812 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
1814 size_t nargs
= fn_args
.size();
1815 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
1816 for (size_t i
= 0; i
< nargs
; ++i
)
1818 args
[i
] = fn_args
.at(i
)->get_tree();
1819 if (args
[i
] == error_mark_node
)
1820 return this->error_expression();
1824 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
1825 fndecl
= TREE_OPERAND(fndecl
, 0);
1827 // This is to support builtin math functions when using 80387 math.
1828 tree excess_type
= NULL_TREE
;
1830 && TREE_CODE(fndecl
) == FUNCTION_DECL
1831 && DECL_IS_BUILTIN(fndecl
)
1832 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
1834 && ((SCALAR_FLOAT_TYPE_P(rettype
)
1835 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
1836 || (COMPLEX_FLOAT_TYPE_P(rettype
)
1837 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
1839 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
1840 if (excess_type
!= NULL_TREE
)
1842 tree excess_fndecl
= mathfn_built_in(excess_type
,
1843 DECL_FUNCTION_CODE(fndecl
));
1844 if (excess_fndecl
== NULL_TREE
)
1845 excess_type
= NULL_TREE
;
1848 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
1850 for (size_t i
= 0; i
< nargs
; ++i
)
1852 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
1853 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
1854 args
[i
] = ::convert(excess_type
, args
[i
]);
1861 build_call_array_loc(location
.gcc_location(),
1862 excess_type
!= NULL_TREE
? excess_type
: rettype
,
1866 CALL_EXPR_STATIC_CHAIN (ret
) = chain_expr
->get_tree();
1868 if (excess_type
!= NULL_TREE
)
1870 // Calling convert here can undo our excess precision change.
1871 // That may or may not be a bug in convert_to_real.
1872 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
1876 return this->make_expression(ret
);
1879 // Return an expression that allocates SIZE bytes on the stack.
1882 Gcc_backend::stack_allocation_expression(int64_t size
, Location location
)
1884 tree alloca
= builtin_decl_explicit(BUILT_IN_ALLOCA
);
1885 tree size_tree
= build_int_cst(integer_type_node
, size
);
1886 tree ret
= build_call_expr_loc(location
.gcc_location(), alloca
, 1, size_tree
);
1887 return this->make_expression(ret
);
1890 // An expression as a statement.
1893 Gcc_backend::expression_statement(Bexpression
* expr
)
1895 return this->make_statement(expr
->get_tree());
1898 // Variable initialization.
1901 Gcc_backend::init_statement(Bvariable
* var
, Bexpression
* init
)
1903 tree var_tree
= var
->get_tree();
1904 tree init_tree
= init
->get_tree();
1905 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1906 return this->error_statement();
1907 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
1909 // To avoid problems with GNU ld, we don't make zero-sized
1910 // externally visible variables. That might lead us to doing an
1911 // initialization of a zero-sized expression to a non-zero sized
1912 // variable, or vice-versa. Avoid crashes by omitting the
1913 // initializer. Such initializations don't mean anything anyhow.
1914 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
1915 && init_tree
!= NULL_TREE
1916 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
1918 DECL_INITIAL(var_tree
) = init_tree
;
1919 init_tree
= NULL_TREE
;
1922 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
1923 void_type_node
, var_tree
);
1924 if (init_tree
!= NULL_TREE
)
1925 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
1926 void_type_node
, init_tree
, ret
);
1928 return this->make_statement(ret
);
1934 Gcc_backend::assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
,
1937 tree lhs_tree
= lhs
->get_tree();
1938 tree rhs_tree
= rhs
->get_tree();
1939 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
1940 return this->error_statement();
1942 // To avoid problems with GNU ld, we don't make zero-sized
1943 // externally visible variables. That might lead us to doing an
1944 // assignment of a zero-sized expression to a non-zero sized
1945 // expression; avoid crashes here by avoiding assignments of
1946 // zero-sized expressions. Such assignments don't really mean
1948 if (int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
1949 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
1950 return this->compound_statement(this->expression_statement(lhs
),
1951 this->expression_statement(rhs
));
1953 // Sometimes the same unnamed Go type can be created multiple times
1954 // and thus have multiple tree representations. Make sure this does
1955 // not confuse the middle-end.
1956 if (TREE_TYPE(lhs_tree
) != TREE_TYPE(rhs_tree
))
1958 tree lhs_type_tree
= TREE_TYPE(lhs_tree
);
1959 gcc_assert(TREE_CODE(lhs_type_tree
) == TREE_CODE(TREE_TYPE(rhs_tree
)));
1960 if (POINTER_TYPE_P(lhs_type_tree
)
1961 || INTEGRAL_TYPE_P(lhs_type_tree
)
1962 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
1963 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
1964 rhs_tree
= fold_convert_loc(location
.gcc_location(), lhs_type_tree
,
1966 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
1967 || TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
)
1969 gcc_assert(int_size_in_bytes(lhs_type_tree
)
1970 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
1971 rhs_tree
= fold_build1_loc(location
.gcc_location(),
1973 lhs_type_tree
, rhs_tree
);
1977 return this->make_statement(fold_build2_loc(location
.gcc_location(),
1980 lhs_tree
, rhs_tree
));
1986 Gcc_backend::return_statement(Bfunction
* bfunction
,
1987 const std::vector
<Bexpression
*>& vals
,
1990 tree fntree
= bfunction
->get_tree();
1991 if (fntree
== error_mark_node
)
1992 return this->error_statement();
1993 tree result
= DECL_RESULT(fntree
);
1994 if (result
== error_mark_node
)
1995 return this->error_statement();
1999 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
2001 else if (vals
.size() == 1)
2003 tree val
= vals
.front()->get_tree();
2004 if (val
== error_mark_node
)
2005 return this->error_statement();
2006 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2007 void_type_node
, result
,
2008 vals
.front()->get_tree());
2009 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2010 void_type_node
, set
);
2014 // To return multiple values, copy the values into a temporary
2015 // variable of the right structure type, and then assign the
2016 // temporary variable to the DECL_RESULT in the return
2018 tree stmt_list
= NULL_TREE
;
2019 tree rettype
= TREE_TYPE(result
);
2021 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2022 push_struct_function(fntree
);
2024 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2025 tree rettmp
= create_tmp_var(rettype
, "RESULT");
2028 tree field
= TYPE_FIELDS(rettype
);
2029 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2031 p
++, field
= DECL_CHAIN(field
))
2033 gcc_assert(field
!= NULL_TREE
);
2034 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
2035 TREE_TYPE(field
), rettmp
, field
,
2037 tree val
= (*p
)->get_tree();
2038 if (val
== error_mark_node
)
2039 return this->error_statement();
2040 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2042 ref
, (*p
)->get_tree());
2043 append_to_statement_list(set
, &stmt_list
);
2045 gcc_assert(field
== NULL_TREE
);
2046 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2049 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2050 void_type_node
, set
);
2051 append_to_statement_list(ret_expr
, &stmt_list
);
2054 return this->make_statement(ret
);
2057 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2058 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2059 // NULL, it will always be executed. This is used for handling defers in Go
2060 // functions. In C++, the resulting code is of this form:
2061 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2064 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2065 Bstatement
* except_stmt
,
2066 Bstatement
* finally_stmt
,
2069 tree stat_tree
= bstat
->get_tree();
2070 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2071 tree finally_tree
= finally_stmt
== NULL
2073 : finally_stmt
->get_tree();
2075 if (stat_tree
== error_mark_node
2076 || except_tree
== error_mark_node
2077 || finally_tree
== error_mark_node
)
2078 return this->error_statement();
2080 if (except_tree
!= NULL_TREE
)
2081 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2082 void_type_node
, stat_tree
,
2083 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2084 void_type_node
, NULL
, except_tree
));
2085 if (finally_tree
!= NULL_TREE
)
2086 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2087 void_type_node
, stat_tree
, finally_tree
);
2088 return this->make_statement(stat_tree
);
2094 Gcc_backend::if_statement(Bexpression
* condition
, Bblock
* then_block
,
2095 Bblock
* else_block
, Location location
)
2097 tree cond_tree
= condition
->get_tree();
2098 tree then_tree
= then_block
->get_tree();
2099 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2100 if (cond_tree
== error_mark_node
2101 || then_tree
== error_mark_node
2102 || else_tree
== error_mark_node
)
2103 return this->error_statement();
2104 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2105 cond_tree
, then_tree
, else_tree
);
2106 return this->make_statement(ret
);
2112 Gcc_backend::switch_statement(
2113 Bfunction
* function
,
2115 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2116 const std::vector
<Bstatement
*>& statements
,
2117 Location switch_location
)
2119 gcc_assert(cases
.size() == statements
.size());
2121 tree decl
= function
->get_tree();
2122 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2123 push_struct_function(decl
);
2125 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2127 tree stmt_list
= NULL_TREE
;
2128 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2129 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2130 ps
!= statements
.end();
2135 source_location loc
= (*ps
!= NULL
2136 ? EXPR_LOCATION((*ps
)->get_tree())
2137 : UNKNOWN_LOCATION
);
2138 tree label
= create_artificial_label(loc
);
2139 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2140 append_to_statement_list(c
, &stmt_list
);
2144 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2148 tree t
= (*pcv
)->get_tree();
2149 if (t
== error_mark_node
)
2150 return this->error_statement();
2151 source_location loc
= EXPR_LOCATION(t
);
2152 tree label
= create_artificial_label(loc
);
2153 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2154 append_to_statement_list(c
, &stmt_list
);
2160 tree t
= (*ps
)->get_tree();
2161 if (t
== error_mark_node
)
2162 return this->error_statement();
2163 append_to_statement_list(t
, &stmt_list
);
2168 tree tv
= value
->get_tree();
2169 if (tv
== error_mark_node
)
2170 return this->error_statement();
2171 tree t
= build3_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2172 NULL_TREE
, tv
, stmt_list
, NULL_TREE
);
2173 return this->make_statement(t
);
2176 // Pair of statements.
2179 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2181 tree stmt_list
= NULL_TREE
;
2182 tree t
= s1
->get_tree();
2183 if (t
== error_mark_node
)
2184 return this->error_statement();
2185 append_to_statement_list(t
, &stmt_list
);
2187 if (t
== error_mark_node
)
2188 return this->error_statement();
2189 append_to_statement_list(t
, &stmt_list
);
2191 // If neither statement has any side effects, stmt_list can be NULL
2193 if (stmt_list
== NULL_TREE
)
2194 stmt_list
= integer_zero_node
;
2196 return this->make_statement(stmt_list
);
2199 // List of statements.
2202 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2204 tree stmt_list
= NULL_TREE
;
2205 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2206 p
!= statements
.end();
2209 tree t
= (*p
)->get_tree();
2210 if (t
== error_mark_node
)
2211 return this->error_statement();
2212 append_to_statement_list(t
, &stmt_list
);
2214 return this->make_statement(stmt_list
);
2217 // Make a block. For some reason gcc uses a dual structure for
2218 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2219 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2223 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2224 const std::vector
<Bvariable
*>& vars
,
2225 Location start_location
,
2228 tree block_tree
= make_node(BLOCK
);
2229 if (enclosing
== NULL
)
2231 tree fndecl
= function
->get_tree();
2232 gcc_assert(fndecl
!= NULL_TREE
);
2234 // We may have already created a block for local variables when
2235 // we take the address of a parameter.
2236 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2238 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2239 DECL_INITIAL(fndecl
) = block_tree
;
2243 tree superblock_tree
= DECL_INITIAL(fndecl
);
2244 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2246 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2248 pp
= &BLOCK_CHAIN(*pp
))
2255 tree superbind_tree
= enclosing
->get_tree();
2256 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2257 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2259 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2261 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2263 pp
= &BLOCK_CHAIN(*pp
))
2268 tree
* pp
= &BLOCK_VARS(block_tree
);
2269 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2273 *pp
= (*pv
)->get_tree();
2274 if (*pp
!= error_mark_node
)
2275 pp
= &DECL_CHAIN(*pp
);
2279 TREE_USED(block_tree
) = 1;
2281 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2282 void_type_node
, BLOCK_VARS(block_tree
),
2283 NULL_TREE
, block_tree
);
2284 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2285 return new Bblock(bind_tree
);
2288 // Add statements to a block.
2291 Gcc_backend::block_add_statements(Bblock
* bblock
,
2292 const std::vector
<Bstatement
*>& statements
)
2294 tree stmt_list
= NULL_TREE
;
2295 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2296 p
!= statements
.end();
2299 tree s
= (*p
)->get_tree();
2300 if (s
!= error_mark_node
)
2301 append_to_statement_list(s
, &stmt_list
);
2304 tree bind_tree
= bblock
->get_tree();
2305 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2306 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2309 // Return a block as a statement.
2312 Gcc_backend::block_statement(Bblock
* bblock
)
2314 tree bind_tree
= bblock
->get_tree();
2315 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2316 return this->make_statement(bind_tree
);
2319 // This is not static because we declare it with GTY(()) in go-c.h.
2320 tree go_non_zero_struct
;
2322 // Return a type corresponding to TYPE with non-zero size.
2325 Gcc_backend::non_zero_size_type(tree type
)
2327 if (int_size_in_bytes(type
) != 0)
2330 switch (TREE_CODE(type
))
2333 if (TYPE_FIELDS(type
) != NULL_TREE
)
2335 tree ns
= make_node(RECORD_TYPE
);
2336 tree field_trees
= NULL_TREE
;
2337 tree
*pp
= &field_trees
;
2338 for (tree field
= TYPE_FIELDS(type
);
2340 field
= DECL_CHAIN(field
))
2342 tree ft
= TREE_TYPE(field
);
2343 if (field
== TYPE_FIELDS(type
))
2344 ft
= non_zero_size_type(ft
);
2345 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2346 DECL_NAME(field
), ft
);
2347 DECL_CONTEXT(f
) = ns
;
2349 pp
= &DECL_CHAIN(f
);
2351 TYPE_FIELDS(ns
) = field_trees
;
2356 if (go_non_zero_struct
== NULL_TREE
)
2358 type
= make_node(RECORD_TYPE
);
2359 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2360 get_identifier("dummy"),
2362 DECL_CONTEXT(field
) = type
;
2363 TYPE_FIELDS(type
) = field
;
2365 go_non_zero_struct
= type
;
2367 return go_non_zero_struct
;
2371 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2372 return build_array_type_nelts(element_type
, 1);
2382 // Make a global variable.
2385 Gcc_backend::global_variable(const std::string
& package_name
,
2386 const std::string
& pkgpath
,
2387 const std::string
& name
,
2391 bool in_unique_section
,
2394 tree type_tree
= btype
->get_tree();
2395 if (type_tree
== error_mark_node
)
2396 return this->error_variable();
2398 // The GNU linker does not like dynamic variables with zero size.
2399 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2400 type_tree
= this->non_zero_size_type(type_tree
);
2402 std::string
var_name(package_name
);
2403 var_name
.push_back('.');
2404 var_name
.append(name
);
2405 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2406 get_identifier_from_string(var_name
),
2409 DECL_EXTERNAL(decl
) = 1;
2411 TREE_STATIC(decl
) = 1;
2414 TREE_PUBLIC(decl
) = 1;
2416 std::string
asm_name(pkgpath
);
2417 asm_name
.push_back('.');
2418 asm_name
.append(name
);
2419 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2421 TREE_USED(decl
) = 1;
2423 if (in_unique_section
)
2424 resolve_unique_section (decl
, 0, 1);
2426 go_preserve_from_gc(decl
);
2428 return new Bvariable(decl
);
2431 // Set the initial value of a global variable.
2434 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2436 tree expr_tree
= expr
->get_tree();
2437 if (expr_tree
== error_mark_node
)
2439 gcc_assert(TREE_CONSTANT(expr_tree
));
2440 tree var_decl
= var
->get_tree();
2441 if (var_decl
== error_mark_node
)
2443 DECL_INITIAL(var_decl
) = expr_tree
;
2445 // If this variable goes in a unique section, it may need to go into
2446 // a different one now that DECL_INITIAL is set.
2447 if (symtab_node::get(var_decl
)
2448 && symtab_node::get(var_decl
)->implicit_section
)
2450 set_decl_section_name (var_decl
, NULL
);
2451 resolve_unique_section (var_decl
,
2452 compute_reloc_for_constant (expr_tree
),
2457 // Make a local variable.
2460 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2461 Btype
* btype
, bool is_address_taken
,
2464 tree type_tree
= btype
->get_tree();
2465 if (type_tree
== error_mark_node
)
2466 return this->error_variable();
2467 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2468 get_identifier_from_string(name
),
2470 DECL_CONTEXT(decl
) = function
->get_tree();
2471 TREE_USED(decl
) = 1;
2472 if (is_address_taken
)
2473 TREE_ADDRESSABLE(decl
) = 1;
2474 go_preserve_from_gc(decl
);
2475 return new Bvariable(decl
);
2478 // Make a function parameter variable.
2481 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2482 Btype
* btype
, bool is_address_taken
,
2485 tree type_tree
= btype
->get_tree();
2486 if (type_tree
== error_mark_node
)
2487 return this->error_variable();
2488 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2489 get_identifier_from_string(name
),
2491 DECL_CONTEXT(decl
) = function
->get_tree();
2492 DECL_ARG_TYPE(decl
) = type_tree
;
2493 TREE_USED(decl
) = 1;
2494 if (is_address_taken
)
2495 TREE_ADDRESSABLE(decl
) = 1;
2496 go_preserve_from_gc(decl
);
2497 return new Bvariable(decl
);
2500 // Make a static chain variable.
2503 Gcc_backend::static_chain_variable(Bfunction
* function
, const std::string
& name
,
2504 Btype
* btype
, Location location
)
2506 tree type_tree
= btype
->get_tree();
2507 if (type_tree
== error_mark_node
)
2508 return this->error_variable();
2509 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2510 get_identifier_from_string(name
), type_tree
);
2511 tree fndecl
= function
->get_tree();
2512 DECL_CONTEXT(decl
) = fndecl
;
2513 DECL_ARG_TYPE(decl
) = type_tree
;
2514 TREE_USED(decl
) = 1;
2515 DECL_ARTIFICIAL(decl
) = 1;
2516 DECL_IGNORED_P(decl
) = 1;
2517 TREE_READONLY(decl
) = 1;
2519 struct function
*f
= DECL_STRUCT_FUNCTION(fndecl
);
2522 push_struct_function(fndecl
);
2524 f
= DECL_STRUCT_FUNCTION(fndecl
);
2526 gcc_assert(f
->static_chain_decl
== NULL
);
2527 f
->static_chain_decl
= decl
;
2528 DECL_STATIC_CHAIN(fndecl
) = 1;
2530 go_preserve_from_gc(decl
);
2531 return new Bvariable(decl
);
2534 // Make a temporary variable.
2537 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2538 Btype
* btype
, Bexpression
* binit
,
2539 bool is_address_taken
,
2541 Bstatement
** pstatement
)
2543 gcc_assert(function
!= NULL
);
2544 tree decl
= function
->get_tree();
2545 tree type_tree
= btype
->get_tree();
2546 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2547 if (type_tree
== error_mark_node
2548 || init_tree
== error_mark_node
2549 || decl
== error_mark_node
)
2551 *pstatement
= this->error_statement();
2552 return this->error_variable();
2556 // We can only use create_tmp_var if the type is not addressable.
2557 if (!TREE_ADDRESSABLE(type_tree
))
2559 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2560 push_struct_function(decl
);
2562 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2564 var
= create_tmp_var(type_tree
, "GOTMP");
2569 gcc_assert(bblock
!= NULL
);
2570 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2571 create_tmp_var_name("GOTMP"),
2573 DECL_ARTIFICIAL(var
) = 1;
2574 DECL_IGNORED_P(var
) = 1;
2576 DECL_CONTEXT(var
) = decl
;
2578 // We have to add this variable to the BLOCK and the BIND_EXPR.
2579 tree bind_tree
= bblock
->get_tree();
2580 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2581 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2582 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2583 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2584 BLOCK_VARS(block_tree
) = var
;
2585 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2588 if (this->type_size(btype
) != 0 && init_tree
!= NULL_TREE
)
2589 DECL_INITIAL(var
) = fold_convert_loc(location
.gcc_location(), type_tree
,
2592 if (is_address_taken
)
2593 TREE_ADDRESSABLE(var
) = 1;
2595 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2597 void_type_node
, var
));
2599 // Don't initialize VAR with BINIT, but still evaluate BINIT for
2600 // its side effects.
2601 if (this->type_size(btype
) == 0 && init_tree
!= NULL_TREE
)
2602 *pstatement
= this->compound_statement(this->expression_statement(binit
),
2605 return new Bvariable(var
);
2608 // Create an implicit variable that is compiler-defined. This is used when
2609 // generating GC root variables and storing the values of a slice initializer.
2612 Gcc_backend::implicit_variable(const std::string
& name
, Btype
* type
,
2613 bool is_hidden
, bool is_constant
,
2614 bool is_common
, int64_t alignment
)
2616 tree type_tree
= type
->get_tree();
2617 if (type_tree
== error_mark_node
)
2618 return this->error_variable();
2620 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2621 get_identifier_from_string(name
), type_tree
);
2622 DECL_EXTERNAL(decl
) = 0;
2623 TREE_PUBLIC(decl
) = !is_hidden
;
2624 TREE_STATIC(decl
) = 1;
2625 TREE_USED(decl
) = 1;
2626 DECL_ARTIFICIAL(decl
) = 1;
2629 DECL_COMMON(decl
) = 1;
2631 // When the initializer for one implicit_variable refers to another,
2632 // it needs to know the visibility of the referenced struct so that
2633 // compute_reloc_for_constant will return the right value. On many
2634 // systems calling make_decl_one_only will mark the decl as weak,
2635 // which will change the return value of compute_reloc_for_constant.
2636 // We can't reliably call make_decl_one_only yet, because we don't
2637 // yet know the initializer. This issue doesn't arise in C because
2638 // Go initializers, unlike C initializers, can be indirectly
2639 // recursive. To ensure that compute_reloc_for_constant computes
2640 // the right value if some other initializer refers to this one, we
2641 // mark this symbol as weak here. We undo that below in
2642 // immutable_struct_set_init before calling mark_decl_one_only.
2643 DECL_WEAK(decl
) = 1;
2647 TREE_READONLY(decl
) = 1;
2648 TREE_CONSTANT(decl
) = 1;
2652 DECL_ALIGN(decl
) = alignment
* BITS_PER_UNIT
;
2653 DECL_USER_ALIGN(decl
) = 1;
2656 go_preserve_from_gc(decl
);
2657 return new Bvariable(decl
);
2660 // Set the initalizer for a variable created by implicit_variable.
2661 // This is where we finish compiling the variable.
2664 Gcc_backend::implicit_variable_set_init(Bvariable
* var
, const std::string
&,
2665 Btype
*, bool, bool, bool is_common
,
2668 tree decl
= var
->get_tree();
2671 init_tree
= NULL_TREE
;
2673 init_tree
= init
->get_tree();
2674 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2677 DECL_INITIAL(decl
) = init_tree
;
2679 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2680 // See the comment where DECL_WEAK is set in implicit_variable.
2683 DECL_WEAK(decl
) = 0;
2684 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2687 resolve_unique_section(decl
, 2, 1);
2689 rest_of_decl_compilation(decl
, 1, 0);
2692 // Return a reference to an implicit variable defined in another package.
2695 Gcc_backend::implicit_variable_reference(const std::string
& name
, Btype
* btype
)
2697 tree type_tree
= btype
->get_tree();
2698 if (type_tree
== error_mark_node
)
2699 return this->error_variable();
2701 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2702 get_identifier_from_string(name
), type_tree
);
2703 DECL_EXTERNAL(decl
) = 0;
2704 TREE_PUBLIC(decl
) = 1;
2705 TREE_STATIC(decl
) = 1;
2706 DECL_ARTIFICIAL(decl
) = 1;
2707 go_preserve_from_gc(decl
);
2708 return new Bvariable(decl
);
2711 // Create a named immutable initialized data structure.
2714 Gcc_backend::immutable_struct(const std::string
& name
, bool is_hidden
,
2715 bool is_common
, Btype
* btype
, Location location
)
2717 tree type_tree
= btype
->get_tree();
2718 if (type_tree
== error_mark_node
)
2719 return this->error_variable();
2720 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2721 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2722 get_identifier_from_string(name
),
2723 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2724 TREE_STATIC(decl
) = 1;
2725 TREE_USED(decl
) = 1;
2726 TREE_READONLY(decl
) = 1;
2727 TREE_CONSTANT(decl
) = 1;
2728 DECL_ARTIFICIAL(decl
) = 1;
2730 TREE_PUBLIC(decl
) = 1;
2732 // When the initializer for one immutable_struct refers to another,
2733 // it needs to know the visibility of the referenced struct so that
2734 // compute_reloc_for_constant will return the right value. On many
2735 // systems calling make_decl_one_only will mark the decl as weak,
2736 // which will change the return value of compute_reloc_for_constant.
2737 // We can't reliably call make_decl_one_only yet, because we don't
2738 // yet know the initializer. This issue doesn't arise in C because
2739 // Go initializers, unlike C initializers, can be indirectly
2740 // recursive. To ensure that compute_reloc_for_constant computes
2741 // the right value if some other initializer refers to this one, we
2742 // mark this symbol as weak here. We undo that below in
2743 // immutable_struct_set_init before calling mark_decl_one_only.
2745 DECL_WEAK(decl
) = 1;
2747 // We don't call rest_of_decl_compilation until we have the
2750 go_preserve_from_gc(decl
);
2751 return new Bvariable(decl
);
2754 // Set the initializer for a variable created by immutable_struct.
2755 // This is where we finish compiling the variable.
2758 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
2759 bool, bool is_common
, Btype
*, Location
,
2760 Bexpression
* initializer
)
2762 tree decl
= var
->get_tree();
2763 tree init_tree
= initializer
->get_tree();
2764 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2767 DECL_INITIAL(decl
) = init_tree
;
2769 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2770 // See the comment where DECL_WEAK is set in immutable_struct.
2773 DECL_WEAK(decl
) = 0;
2774 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2777 // These variables are often unneeded in the final program, so put
2778 // them in their own section so that linker GC can discard them.
2779 resolve_unique_section(decl
,
2780 compute_reloc_for_constant (init_tree
),
2783 rest_of_decl_compilation(decl
, 1, 0);
2786 // Return a reference to an immutable initialized data structure
2787 // defined in another package.
2790 Gcc_backend::immutable_struct_reference(const std::string
& name
, Btype
* btype
,
2793 tree type_tree
= btype
->get_tree();
2794 if (type_tree
== error_mark_node
)
2795 return this->error_variable();
2796 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2797 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2798 get_identifier_from_string(name
),
2799 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2800 TREE_READONLY(decl
) = 1;
2801 TREE_CONSTANT(decl
) = 1;
2802 DECL_ARTIFICIAL(decl
) = 1;
2803 TREE_PUBLIC(decl
) = 1;
2804 DECL_EXTERNAL(decl
) = 1;
2805 go_preserve_from_gc(decl
);
2806 return new Bvariable(decl
);
2812 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
2818 tree func_tree
= function
->get_tree();
2819 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
2820 push_struct_function(func_tree
);
2822 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
2824 decl
= create_artificial_label(location
.gcc_location());
2830 tree id
= get_identifier_from_string(name
);
2831 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
2833 DECL_CONTEXT(decl
) = function
->get_tree();
2835 return new Blabel(decl
);
2838 // Make a statement which defines a label.
2841 Gcc_backend::label_definition_statement(Blabel
* label
)
2843 tree lab
= label
->get_tree();
2844 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
2845 void_type_node
, lab
);
2846 return this->make_statement(ret
);
2849 // Make a goto statement.
2852 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
2854 tree lab
= label
->get_tree();
2855 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
2857 return this->make_statement(ret
);
2860 // Get the address of a label.
2863 Gcc_backend::label_address(Blabel
* label
, Location location
)
2865 tree lab
= label
->get_tree();
2867 TREE_ADDRESSABLE(lab
) = 1;
2868 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
2869 build_fold_addr_expr_loc(location
.gcc_location(),
2871 return this->make_expression(ret
);
2874 // Declare or define a new function.
2877 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
2878 const std::string
& asm_name
, bool is_visible
,
2879 bool is_declaration
, bool is_inlinable
,
2880 bool disable_split_stack
, bool in_unique_section
,
2883 tree functype
= fntype
->get_tree();
2884 if (functype
!= error_mark_node
)
2886 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
2887 functype
= TREE_TYPE(functype
);
2889 tree id
= get_identifier_from_string(name
);
2890 if (functype
== error_mark_node
|| id
== error_mark_node
)
2891 return this->error_function();
2893 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
2894 if (!asm_name
.empty())
2895 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2897 TREE_PUBLIC(decl
) = 1;
2899 DECL_EXTERNAL(decl
) = 1;
2902 tree restype
= TREE_TYPE(functype
);
2904 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
2905 DECL_ARTIFICIAL(resdecl
) = 1;
2906 DECL_IGNORED_P(resdecl
) = 1;
2907 DECL_CONTEXT(resdecl
) = decl
;
2908 DECL_RESULT(decl
) = resdecl
;
2911 DECL_UNINLINABLE(decl
) = 1;
2912 if (disable_split_stack
)
2914 tree attr
= get_identifier("__no_split_stack__");
2915 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
2917 if (in_unique_section
)
2918 resolve_unique_section(decl
, 0, 1);
2920 go_preserve_from_gc(decl
);
2921 return new Bfunction(decl
);
2924 // Create a statement that runs all deferred calls for FUNCTION. This should
2925 // be a statement that looks like this in C++:
2927 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2930 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
2931 Bexpression
* defer
, Location location
)
2933 tree undefer_tree
= undefer
->get_tree();
2934 tree defer_tree
= defer
->get_tree();
2935 tree fntree
= function
->get_tree();
2937 if (undefer_tree
== error_mark_node
2938 || defer_tree
== error_mark_node
2939 || fntree
== error_mark_node
)
2940 return this->error_statement();
2942 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2943 push_struct_function(fntree
);
2945 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2947 tree stmt_list
= NULL
;
2948 Blabel
* blabel
= this->label(function
, "", location
);
2949 Bstatement
* label_def
= this->label_definition_statement(blabel
);
2950 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
2952 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
2953 tree jump
= jump_stmt
->get_tree();
2954 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
2955 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
2957 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
2958 append_to_statement_list(try_catch
, &stmt_list
);
2961 return this->make_statement(stmt_list
);
2964 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2965 // This will only be called for a function definition.
2968 Gcc_backend::function_set_parameters(Bfunction
* function
,
2969 const std::vector
<Bvariable
*>& param_vars
)
2971 tree func_tree
= function
->get_tree();
2972 if (func_tree
== error_mark_node
)
2975 tree params
= NULL_TREE
;
2977 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
2978 pv
!= param_vars
.end();
2981 *pp
= (*pv
)->get_tree();
2982 gcc_assert(*pp
!= error_mark_node
);
2983 pp
= &DECL_CHAIN(*pp
);
2986 DECL_ARGUMENTS(func_tree
) = params
;
2990 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2993 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
2995 tree func_tree
= function
->get_tree();
2996 tree code
= code_stmt
->get_tree();
2998 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
3000 DECL_SAVED_TREE(func_tree
) = code
;
3004 // Look up a named built-in function in the current backend implementation.
3005 // Returns NULL if no built-in function by that name exists.
3008 Gcc_backend::lookup_builtin(const std::string
& name
)
3010 if (this->builtin_functions_
.count(name
) != 0)
3011 return this->builtin_functions_
[name
];
3015 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3016 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3017 // emit early debugging information.
3020 Gcc_backend::write_global_definitions(
3021 const std::vector
<Btype
*>& type_decls
,
3022 const std::vector
<Bexpression
*>& constant_decls
,
3023 const std::vector
<Bfunction
*>& function_decls
,
3024 const std::vector
<Bvariable
*>& variable_decls
)
3026 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
3027 + function_decls
.size() + variable_decls
.size();
3029 tree
* defs
= new tree
[count_definitions
];
3031 // Convert all non-erroneous declarations into Gimple form.
3033 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
3034 p
!= variable_decls
.end();
3037 if ((*p
)->get_tree() != error_mark_node
)
3039 defs
[i
] = (*p
)->get_tree();
3040 go_preserve_from_gc(defs
[i
]);
3045 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
3046 p
!= type_decls
.end();
3049 tree type_tree
= (*p
)->get_tree();
3050 if (type_tree
!= error_mark_node
3051 && IS_TYPE_OR_DECL_P(type_tree
))
3053 defs
[i
] = TYPE_NAME(type_tree
);
3054 gcc_assert(defs
[i
] != NULL
);
3055 go_preserve_from_gc(defs
[i
]);
3059 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
3060 p
!= constant_decls
.end();
3063 if ((*p
)->get_tree() != error_mark_node
)
3065 defs
[i
] = (*p
)->get_tree();
3066 go_preserve_from_gc(defs
[i
]);
3070 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
3071 p
!= function_decls
.end();
3074 tree decl
= (*p
)->get_tree();
3075 if (decl
!= error_mark_node
)
3077 go_preserve_from_gc(decl
);
3078 gimplify_function_tree(decl
);
3079 cgraph_node::finalize_function(decl
, true);
3086 // Pass everything back to the middle-end.
3088 wrapup_global_declarations(defs
, i
);
3093 // Define a builtin function. BCODE is the builtin function code
3094 // defined by builtins.def. NAME is the name of the builtin function.
3095 // LIBNAME is the name of the corresponding library function, and is
3096 // NULL if there isn't one. FNTYPE is the type of the function.
3097 // CONST_P is true if the function has the const attribute.
3100 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
3101 const char* libname
, tree fntype
, bool const_p
)
3103 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
3104 libname
, NULL_TREE
);
3106 TREE_READONLY(decl
) = 1;
3107 set_builtin_decl(bcode
, decl
, true);
3108 this->builtin_functions_
[name
] = this->make_function(decl
);
3109 if (libname
!= NULL
)
3111 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
3114 TREE_READONLY(decl
) = 1;
3115 this->builtin_functions_
[libname
] = this->make_function(decl
);
3119 // Return the backend generator.
3124 return new Gcc_backend();