compiler: recognize Go 1.18 runtime/internal/atomic methods
[official-gcc.git] / gcc / go / go-gcc.cc
blobf3de7a8c183d33986d20a08759ff6d9f9b27981c
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2022 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
10 // version.
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
15 // for more details.
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.
25 #include <gmp.h>
27 #include "tree.h"
28 #include "opts.h"
29 #include "fold-const.h"
30 #include "stringpool.h"
31 #include "stor-layout.h"
32 #include "varasm.h"
33 #include "tree-iterator.h"
34 #include "tm.h"
35 #include "function.h"
36 #include "cgraph.h"
37 #include "convert.h"
38 #include "gimple-expr.h"
39 #include "gimplify.h"
40 #include "langhooks.h"
41 #include "toplev.h"
42 #include "output.h"
43 #include "realmpfr.h"
44 #include "builtins.h"
46 #include "go-c.h"
47 #include "go-gcc.h"
49 #include "gogo.h"
50 #include "backend.h"
52 // A class wrapping a tree.
54 class Gcc_tree
56 public:
57 Gcc_tree(tree t)
58 : t_(t)
59 { }
61 tree
62 get_tree() const
63 { return this->t_; }
65 void
66 set_tree(tree t)
67 { this->t_ = t; }
69 private:
70 tree t_;
73 // In gcc, types, expressions, and statements are all trees.
74 class Btype : public Gcc_tree
76 public:
77 Btype(tree t)
78 : Gcc_tree(t)
79 { }
82 class Bexpression : public Gcc_tree
84 public:
85 Bexpression(tree t)
86 : Gcc_tree(t)
87 { }
90 class Bstatement : public Gcc_tree
92 public:
93 Bstatement(tree t)
94 : Gcc_tree(t)
95 { }
98 class Bfunction : public Gcc_tree
100 public:
101 Bfunction(tree t)
102 : Gcc_tree(t)
106 class Bblock : public Gcc_tree
108 public:
109 Bblock(tree t)
110 : Gcc_tree(t)
114 class Blabel : public Gcc_tree
116 public:
117 Blabel(tree t)
118 : Gcc_tree(t)
122 // Bvariable is a bit more complicated, because of zero-sized types.
123 // The GNU linker does not permit dynamic variables with zero size.
124 // When we see such a variable, we generate a version of the type with
125 // non-zero size. However, when referring to the global variable, we
126 // want an expression of zero size; otherwise, if, say, the global
127 // variable is passed to a function, we will be passing a
128 // non-zero-sized value to a zero-sized value, which can lead to a
129 // miscompilation.
131 class Bvariable
133 public:
134 Bvariable(tree t)
135 : t_(t), orig_type_(NULL)
138 Bvariable(tree t, tree orig_type)
139 : t_(t), orig_type_(orig_type)
142 // Get the tree for use as an expression.
143 tree
144 get_tree(Location) const;
146 // Get the actual decl;
147 tree
148 get_decl() const
149 { return this->t_; }
151 private:
152 tree t_;
153 tree orig_type_;
156 // Get the tree of a variable for use as an expression. If this is a
157 // zero-sized global, create an expression that refers to the decl but
158 // has zero size.
159 tree
160 Bvariable::get_tree(Location location) const
162 if (this->orig_type_ == NULL
163 || this->t_ == error_mark_node
164 || TREE_TYPE(this->t_) == this->orig_type_)
165 return this->t_;
166 // Return *(orig_type*)&decl. */
167 tree t = build_fold_addr_expr_loc(location.gcc_location(), this->t_);
168 t = fold_build1_loc(location.gcc_location(), NOP_EXPR,
169 build_pointer_type(this->orig_type_), t);
170 return build_fold_indirect_ref_loc(location.gcc_location(), t);
173 // This file implements the interface between the Go frontend proper
174 // and the gcc IR. This implements specific instantiations of
175 // abstract classes defined by the Go frontend proper. The Go
176 // frontend proper class methods of these classes to generate the
177 // backend representation.
179 class Gcc_backend : public Backend
181 public:
182 Gcc_backend();
184 // Types.
186 Btype*
187 error_type()
188 { return this->make_type(error_mark_node); }
190 Btype*
191 void_type()
192 { return this->make_type(void_type_node); }
194 Btype*
195 bool_type()
196 { return this->make_type(boolean_type_node); }
198 Btype*
199 integer_type(bool, int);
201 Btype*
202 float_type(int);
204 Btype*
205 complex_type(int);
207 Btype*
208 pointer_type(Btype*);
210 Btype*
211 function_type(const Btyped_identifier&,
212 const std::vector<Btyped_identifier>&,
213 const std::vector<Btyped_identifier>&,
214 Btype*,
215 const Location);
217 Btype*
218 struct_type(const std::vector<Btyped_identifier>&);
220 Btype*
221 array_type(Btype*, Bexpression*);
223 Btype*
224 placeholder_pointer_type(const std::string&, Location, bool);
226 bool
227 set_placeholder_pointer_type(Btype*, Btype*);
229 bool
230 set_placeholder_function_type(Btype*, Btype*);
232 Btype*
233 placeholder_struct_type(const std::string&, Location);
235 bool
236 set_placeholder_struct_type(Btype* placeholder,
237 const std::vector<Btyped_identifier>&);
239 Btype*
240 placeholder_array_type(const std::string&, Location);
242 bool
243 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
245 Btype*
246 named_type(const std::string&, Btype*, Location);
248 Btype*
249 circular_pointer_type(Btype*, bool);
251 bool
252 is_circular_pointer_type(Btype*);
254 int64_t
255 type_size(Btype*);
257 int64_t
258 type_alignment(Btype*);
260 int64_t
261 type_field_alignment(Btype*);
263 int64_t
264 type_field_offset(Btype*, size_t index);
266 // Expressions.
268 Bexpression*
269 zero_expression(Btype*);
271 Bexpression*
272 error_expression()
273 { return this->make_expression(error_mark_node); }
275 Bexpression*
276 nil_pointer_expression()
277 { return this->make_expression(null_pointer_node); }
279 Bexpression*
280 var_expression(Bvariable* var, Location);
282 Bexpression*
283 indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location);
285 Bexpression*
286 named_constant_expression(Btype* btype, const std::string& name,
287 Bexpression* val, Location);
289 Bexpression*
290 integer_constant_expression(Btype* btype, mpz_t val);
292 Bexpression*
293 float_constant_expression(Btype* btype, mpfr_t val);
295 Bexpression*
296 complex_constant_expression(Btype* btype, mpc_t val);
298 Bexpression*
299 string_constant_expression(const std::string& val);
301 Bexpression*
302 boolean_constant_expression(bool val);
304 Bexpression*
305 real_part_expression(Bexpression* bcomplex, Location);
307 Bexpression*
308 imag_part_expression(Bexpression* bcomplex, Location);
310 Bexpression*
311 complex_expression(Bexpression* breal, Bexpression* bimag, Location);
313 Bexpression*
314 convert_expression(Btype* type, Bexpression* expr, Location);
316 Bexpression*
317 function_code_expression(Bfunction*, Location);
319 Bexpression*
320 address_expression(Bexpression*, Location);
322 Bexpression*
323 struct_field_expression(Bexpression*, size_t, Location);
325 Bexpression*
326 compound_expression(Bstatement*, Bexpression*, Location);
328 Bexpression*
329 conditional_expression(Bfunction*, Btype*, Bexpression*, Bexpression*,
330 Bexpression*, Location);
332 Bexpression*
333 unary_expression(Operator, Bexpression*, Location);
335 Bexpression*
336 binary_expression(Operator, Bexpression*, Bexpression*, Location);
338 Bexpression*
339 constructor_expression(Btype*, const std::vector<Bexpression*>&, Location);
341 Bexpression*
342 array_constructor_expression(Btype*, const std::vector<unsigned long>&,
343 const std::vector<Bexpression*>&, Location);
345 Bexpression*
346 pointer_offset_expression(Bexpression* base, Bexpression* offset, Location);
348 Bexpression*
349 array_index_expression(Bexpression* array, Bexpression* index, Location);
351 Bexpression*
352 call_expression(Bfunction* caller, Bexpression* fn,
353 const std::vector<Bexpression*>& args,
354 Bexpression* static_chain, Location);
356 // Statements.
358 Bstatement*
359 error_statement()
360 { return this->make_statement(error_mark_node); }
362 Bstatement*
363 expression_statement(Bfunction*, Bexpression*);
365 Bstatement*
366 init_statement(Bfunction*, Bvariable* var, Bexpression* init);
368 Bstatement*
369 assignment_statement(Bfunction*, Bexpression* lhs, Bexpression* rhs,
370 Location);
372 Bstatement*
373 return_statement(Bfunction*, const std::vector<Bexpression*>&,
374 Location);
376 Bstatement*
377 if_statement(Bfunction*, Bexpression* condition, Bblock* then_block,
378 Bblock* else_block, Location);
380 Bstatement*
381 switch_statement(Bfunction* function, Bexpression* value,
382 const std::vector<std::vector<Bexpression*> >& cases,
383 const std::vector<Bstatement*>& statements,
384 Location);
386 Bstatement*
387 compound_statement(Bstatement*, Bstatement*);
389 Bstatement*
390 statement_list(const std::vector<Bstatement*>&);
392 Bstatement*
393 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
394 Bstatement* finally_stmt, Location);
396 // Blocks.
398 Bblock*
399 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
400 Location, Location);
402 void
403 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
405 Bstatement*
406 block_statement(Bblock*);
408 // Variables.
410 Bvariable*
411 error_variable()
412 { return new Bvariable(error_mark_node); }
414 Bvariable*
415 global_variable(const std::string& var_name,
416 const std::string& asm_name,
417 Btype* btype,
418 unsigned int flags,
419 Location location);
421 void
422 global_variable_set_init(Bvariable*, Bexpression*);
424 Bvariable*
425 local_variable(Bfunction*, const std::string&, Btype*, Bvariable*,
426 unsigned int, Location);
428 Bvariable*
429 parameter_variable(Bfunction*, const std::string&, Btype*, unsigned int,
430 Location);
432 Bvariable*
433 static_chain_variable(Bfunction*, const std::string&, Btype*, unsigned int,
434 Location);
436 Bvariable*
437 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, unsigned int,
438 Location, Bstatement**);
440 Bvariable*
441 implicit_variable(const std::string&, const std::string&, Btype*,
442 unsigned int, int64_t);
444 void
445 implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
446 unsigned int, Bexpression*);
448 Bvariable*
449 implicit_variable_reference(const std::string&, const std::string&, Btype*);
451 Bvariable*
452 immutable_struct(const std::string&, const std::string&,
453 unsigned int, Btype*, Location);
455 void
456 immutable_struct_set_init(Bvariable*, const std::string&, unsigned int,
457 Btype*, Location, Bexpression*);
459 Bvariable*
460 immutable_struct_reference(const std::string&, const std::string&,
461 Btype*, Location);
463 // Labels.
465 Blabel*
466 label(Bfunction*, const std::string& name, Location);
468 Bstatement*
469 label_definition_statement(Blabel*);
471 Bstatement*
472 goto_statement(Blabel*, Location);
474 Bexpression*
475 label_address(Blabel*, Location);
477 // Functions.
479 Bfunction*
480 error_function()
481 { return this->make_function(error_mark_node); }
483 Bfunction*
484 function(Btype* fntype, const std::string& name, const std::string& asm_name,
485 unsigned int flags, Location);
487 Bstatement*
488 function_defer_statement(Bfunction* function, Bexpression* undefer,
489 Bexpression* defer, Location);
491 bool
492 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
494 bool
495 function_set_body(Bfunction* function, Bstatement* code_stmt);
497 Bfunction*
498 lookup_builtin(const std::string&);
500 void
501 write_global_definitions(const std::vector<Btype*>&,
502 const std::vector<Bexpression*>&,
503 const std::vector<Bfunction*>&,
504 const std::vector<Bvariable*>&);
506 void
507 write_export_data(const char* bytes, unsigned int size);
510 private:
511 // Make a Bexpression from a tree.
512 Bexpression*
513 make_expression(tree t)
514 { return new Bexpression(t); }
516 // Make a Bstatement from a tree.
517 Bstatement*
518 make_statement(tree t)
519 { return new Bstatement(t); }
521 // Make a Btype from a tree.
522 Btype*
523 make_type(tree t)
524 { return new Btype(t); }
526 Bfunction*
527 make_function(tree t)
528 { return new Bfunction(t); }
530 Btype*
531 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
533 Btype*
534 fill_in_array(Btype*, Btype*, Bexpression*);
536 tree
537 non_zero_size_type(tree);
539 tree
540 convert_tree(tree, tree, Location);
542 private:
543 static const int builtin_const = 1 << 0;
544 static const int builtin_noreturn = 1 << 1;
545 static const int builtin_novops = 1 << 2;
547 void
548 define_builtin(built_in_function bcode, const char* name, const char* libname,
549 tree fntype, int flags);
551 // A mapping of the GCC built-ins exposed to GCCGo.
552 std::map<std::string, Bfunction*> builtin_functions_;
555 // A helper function to create a GCC identifier from a C++ string.
557 static inline tree
558 get_identifier_from_string(const std::string& str)
560 return get_identifier_with_length(str.data(), str.length());
563 // Define the built-in functions that are exposed to GCCGo.
565 Gcc_backend::Gcc_backend()
567 /* We need to define the fetch_and_add functions, since we use them
568 for ++ and --. */
569 tree t = this->integer_type(true, BITS_PER_UNIT)->get_tree();
570 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
571 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
572 NULL, build_function_type_list(t, p, t, NULL_TREE), 0);
574 t = this->integer_type(true, BITS_PER_UNIT * 2)->get_tree();
575 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
576 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
577 NULL, build_function_type_list(t, p, t, NULL_TREE), 0);
579 t = this->integer_type(true, BITS_PER_UNIT * 4)->get_tree();
580 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
581 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
582 NULL, build_function_type_list(t, p, t, NULL_TREE), 0);
584 t = this->integer_type(true, BITS_PER_UNIT * 8)->get_tree();
585 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
586 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
587 NULL, build_function_type_list(t, p, t, NULL_TREE), 0);
589 // We use __builtin_expect for magic import functions.
590 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
591 build_function_type_list(long_integer_type_node,
592 long_integer_type_node,
593 long_integer_type_node,
594 NULL_TREE),
595 builtin_const);
597 // We use __builtin_memcmp for struct comparisons.
598 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
599 build_function_type_list(integer_type_node,
600 const_ptr_type_node,
601 const_ptr_type_node,
602 size_type_node,
603 NULL_TREE),
606 // We use __builtin_memmove for copying data.
607 this->define_builtin(BUILT_IN_MEMMOVE, "__builtin_memmove", "memmove",
608 build_function_type_list(void_type_node,
609 ptr_type_node,
610 const_ptr_type_node,
611 size_type_node,
612 NULL_TREE),
615 // We use __builtin_memset for zeroing data.
616 this->define_builtin(BUILT_IN_MEMSET, "__builtin_memset", "memset",
617 build_function_type_list(void_type_node,
618 ptr_type_node,
619 integer_type_node,
620 size_type_node,
621 NULL_TREE),
624 // Used by runtime/internal/sys and math/bits.
625 this->define_builtin(BUILT_IN_CTZ, "__builtin_ctz", "ctz",
626 build_function_type_list(integer_type_node,
627 unsigned_type_node,
628 NULL_TREE),
629 builtin_const);
630 this->define_builtin(BUILT_IN_CTZLL, "__builtin_ctzll", "ctzll",
631 build_function_type_list(integer_type_node,
632 long_long_unsigned_type_node,
633 NULL_TREE),
634 builtin_const);
635 this->define_builtin(BUILT_IN_CLZ, "__builtin_clz", "clz",
636 build_function_type_list(integer_type_node,
637 unsigned_type_node,
638 NULL_TREE),
639 builtin_const);
640 this->define_builtin(BUILT_IN_CLZLL, "__builtin_clzll", "clzll",
641 build_function_type_list(integer_type_node,
642 long_long_unsigned_type_node,
643 NULL_TREE),
644 builtin_const);
645 this->define_builtin(BUILT_IN_POPCOUNT, "__builtin_popcount", "popcount",
646 build_function_type_list(integer_type_node,
647 unsigned_type_node,
648 NULL_TREE),
649 builtin_const);
650 this->define_builtin(BUILT_IN_POPCOUNTLL, "__builtin_popcountll", "popcountll",
651 build_function_type_list(integer_type_node,
652 long_long_unsigned_type_node,
653 NULL_TREE),
654 builtin_const);
655 this->define_builtin(BUILT_IN_BSWAP16, "__builtin_bswap16", "bswap16",
656 build_function_type_list(uint16_type_node,
657 uint16_type_node,
658 NULL_TREE),
659 builtin_const);
660 this->define_builtin(BUILT_IN_BSWAP32, "__builtin_bswap32", "bswap32",
661 build_function_type_list(uint32_type_node,
662 uint32_type_node,
663 NULL_TREE),
664 builtin_const);
665 this->define_builtin(BUILT_IN_BSWAP64, "__builtin_bswap64", "bswap64",
666 build_function_type_list(uint64_type_node,
667 uint64_type_node,
668 NULL_TREE),
669 builtin_const);
671 // We provide some functions for the math library.
672 tree math_function_type = build_function_type_list(double_type_node,
673 double_type_node,
674 NULL_TREE);
675 tree math_function_type_long =
676 build_function_type_list(long_double_type_node, long_double_type_node,
677 NULL_TREE);
678 tree math_function_type_two = build_function_type_list(double_type_node,
679 double_type_node,
680 double_type_node,
681 NULL_TREE);
682 tree math_function_type_long_two =
683 build_function_type_list(long_double_type_node, long_double_type_node,
684 long_double_type_node, NULL_TREE);
685 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
686 math_function_type, builtin_const);
687 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
688 math_function_type_long, builtin_const);
689 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
690 math_function_type, builtin_const);
691 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
692 math_function_type_long, builtin_const);
693 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
694 math_function_type, builtin_const);
695 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
696 math_function_type_long, builtin_const);
697 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
698 math_function_type_two, builtin_const);
699 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
700 math_function_type_long_two, builtin_const);
701 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
702 math_function_type, builtin_const);
703 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
704 math_function_type_long, builtin_const);
705 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
706 math_function_type, builtin_const);
707 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
708 math_function_type_long, builtin_const);
709 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
710 math_function_type, builtin_const);
711 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
712 math_function_type_long, builtin_const);
713 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
714 math_function_type, builtin_const);
715 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
716 math_function_type_long, builtin_const);
717 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
718 math_function_type, builtin_const);
719 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
720 math_function_type_long, builtin_const);
721 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
722 math_function_type, builtin_const);
723 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
724 math_function_type_long, builtin_const);
725 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
726 math_function_type_two, builtin_const);
727 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
728 math_function_type_long_two, builtin_const);
729 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
730 build_function_type_list(double_type_node,
731 double_type_node,
732 integer_type_node,
733 NULL_TREE),
734 builtin_const);
735 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
736 build_function_type_list(long_double_type_node,
737 long_double_type_node,
738 integer_type_node,
739 NULL_TREE),
740 builtin_const);
741 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
742 math_function_type, builtin_const);
743 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
744 math_function_type_long, builtin_const);
745 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
746 math_function_type, builtin_const);
747 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
748 math_function_type_long, builtin_const);
749 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
750 math_function_type, builtin_const);
751 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
752 math_function_type_long, builtin_const);
753 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
754 math_function_type, builtin_const);
755 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
756 math_function_type_long, builtin_const);
757 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
758 math_function_type, builtin_const);
759 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
760 math_function_type_long, builtin_const);
761 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
762 math_function_type, builtin_const);
763 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
764 math_function_type_long, builtin_const);
765 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
766 math_function_type, builtin_const);
767 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
768 math_function_type_long, builtin_const);
769 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
770 math_function_type, builtin_const);
771 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
772 math_function_type_long, builtin_const);
774 // We use __builtin_return_address in the thunk we build for
775 // functions which call recover, and for runtime.getcallerpc.
776 t = build_function_type_list(ptr_type_node, unsigned_type_node, NULL_TREE);
777 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
778 NULL, t, 0);
780 // The runtime calls __builtin_dwarf_cfa for runtime.getcallersp.
781 t = build_function_type_list(ptr_type_node, NULL_TREE);
782 this->define_builtin(BUILT_IN_DWARF_CFA, "__builtin_dwarf_cfa",
783 NULL, t, 0);
785 // The runtime calls __builtin_extract_return_addr when recording
786 // the address to which a function returns.
787 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR,
788 "__builtin_extract_return_addr", NULL,
789 build_function_type_list(ptr_type_node,
790 ptr_type_node,
791 NULL_TREE),
794 // The compiler uses __builtin_trap for some exception handling
795 // cases.
796 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
797 build_function_type(void_type_node, void_list_node),
798 builtin_noreturn);
800 // The runtime uses __builtin_prefetch.
801 this->define_builtin(BUILT_IN_PREFETCH, "__builtin_prefetch", NULL,
802 build_varargs_function_type_list(void_type_node,
803 const_ptr_type_node,
804 NULL_TREE),
805 builtin_novops);
807 // The compiler uses __builtin_unreachable for cases that cannot
808 // occur.
809 this->define_builtin(BUILT_IN_UNREACHABLE, "__builtin_unreachable", NULL,
810 build_function_type(void_type_node, void_list_node),
811 builtin_const | builtin_noreturn);
813 // We provide some atomic functions.
814 t = build_function_type_list(uint32_type_node,
815 ptr_type_node,
816 integer_type_node,
817 NULL_TREE);
818 this->define_builtin(BUILT_IN_ATOMIC_LOAD_4, "__atomic_load_4", NULL,
819 t, 0);
821 t = build_function_type_list(uint64_type_node,
822 ptr_type_node,
823 integer_type_node,
824 NULL_TREE);
825 this->define_builtin(BUILT_IN_ATOMIC_LOAD_8, "__atomic_load_8", NULL,
826 t, 0);
828 t = build_function_type_list(void_type_node,
829 ptr_type_node,
830 uint32_type_node,
831 integer_type_node,
832 NULL_TREE);
833 this->define_builtin(BUILT_IN_ATOMIC_STORE_4, "__atomic_store_4", NULL,
834 t, 0);
836 t = build_function_type_list(void_type_node,
837 ptr_type_node,
838 uint64_type_node,
839 integer_type_node,
840 NULL_TREE);
841 this->define_builtin(BUILT_IN_ATOMIC_STORE_8, "__atomic_store_8", NULL,
842 t, 0);
844 t = build_function_type_list(uint32_type_node,
845 ptr_type_node,
846 uint32_type_node,
847 integer_type_node,
848 NULL_TREE);
849 this->define_builtin(BUILT_IN_ATOMIC_EXCHANGE_4, "__atomic_exchange_4", NULL,
850 t, 0);
852 t = build_function_type_list(uint64_type_node,
853 ptr_type_node,
854 uint64_type_node,
855 integer_type_node,
856 NULL_TREE);
857 this->define_builtin(BUILT_IN_ATOMIC_EXCHANGE_8, "__atomic_exchange_8", NULL,
858 t, 0);
860 t = build_function_type_list(boolean_type_node,
861 ptr_type_node,
862 ptr_type_node,
863 uint32_type_node,
864 boolean_type_node,
865 integer_type_node,
866 integer_type_node,
867 NULL_TREE);
868 this->define_builtin(BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4,
869 "__atomic_compare_exchange_4", NULL,
870 t, 0);
872 t = build_function_type_list(boolean_type_node,
873 ptr_type_node,
874 ptr_type_node,
875 uint64_type_node,
876 boolean_type_node,
877 integer_type_node,
878 integer_type_node,
879 NULL_TREE);
880 this->define_builtin(BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8,
881 "__atomic_compare_exchange_8", NULL,
882 t, 0);
884 t = build_function_type_list(uint32_type_node,
885 ptr_type_node,
886 uint32_type_node,
887 integer_type_node,
888 NULL_TREE);
889 this->define_builtin(BUILT_IN_ATOMIC_ADD_FETCH_4, "__atomic_add_fetch_4", NULL,
890 t, 0);
892 t = build_function_type_list(uint64_type_node,
893 ptr_type_node,
894 uint64_type_node,
895 integer_type_node,
896 NULL_TREE);
897 this->define_builtin(BUILT_IN_ATOMIC_ADD_FETCH_8, "__atomic_add_fetch_8", NULL,
898 t, 0);
900 t = build_function_type_list(unsigned_char_type_node,
901 ptr_type_node,
902 integer_type_node,
903 NULL_TREE);
904 this->define_builtin(BUILT_IN_ATOMIC_LOAD_1, "__atomic_load_1", NULL, t, 0);
906 t = build_function_type_list(void_type_node,
907 ptr_type_node,
908 unsigned_char_type_node,
909 integer_type_node,
910 NULL_TREE);
911 this->define_builtin(BUILT_IN_ATOMIC_STORE_1, "__atomic_store_1", NULL,
912 t, 0);
914 t = build_function_type_list(unsigned_char_type_node,
915 ptr_type_node,
916 unsigned_char_type_node,
917 integer_type_node,
918 NULL_TREE);
919 this->define_builtin(BUILT_IN_ATOMIC_AND_FETCH_1, "__atomic_and_fetch_1", NULL,
920 t, 0);
921 this->define_builtin(BUILT_IN_ATOMIC_FETCH_AND_1, "__atomic_fetch_and_1", NULL,
922 t, 0);
924 t = build_function_type_list(unsigned_char_type_node,
925 ptr_type_node,
926 unsigned_char_type_node,
927 integer_type_node,
928 NULL_TREE);
929 this->define_builtin(BUILT_IN_ATOMIC_OR_FETCH_1, "__atomic_or_fetch_1", NULL,
930 t, 0);
931 this->define_builtin(BUILT_IN_ATOMIC_FETCH_OR_1, "__atomic_fetch_or_1", NULL,
932 t, 0);
935 // Get an unnamed integer type.
937 Btype*
938 Gcc_backend::integer_type(bool is_unsigned, int bits)
940 tree type;
941 if (is_unsigned)
943 if (bits == INT_TYPE_SIZE)
944 type = unsigned_type_node;
945 else if (bits == CHAR_TYPE_SIZE)
946 type = unsigned_char_type_node;
947 else if (bits == SHORT_TYPE_SIZE)
948 type = short_unsigned_type_node;
949 else if (bits == LONG_TYPE_SIZE)
950 type = long_unsigned_type_node;
951 else if (bits == LONG_LONG_TYPE_SIZE)
952 type = long_long_unsigned_type_node;
953 else
954 type = make_unsigned_type(bits);
956 else
958 if (bits == INT_TYPE_SIZE)
959 type = integer_type_node;
960 else if (bits == CHAR_TYPE_SIZE)
961 type = signed_char_type_node;
962 else if (bits == SHORT_TYPE_SIZE)
963 type = short_integer_type_node;
964 else if (bits == LONG_TYPE_SIZE)
965 type = long_integer_type_node;
966 else if (bits == LONG_LONG_TYPE_SIZE)
967 type = long_long_integer_type_node;
968 else
969 type = make_signed_type(bits);
971 return this->make_type(type);
974 // Get an unnamed float type.
976 Btype*
977 Gcc_backend::float_type(int bits)
979 tree type;
980 if (bits == FLOAT_TYPE_SIZE)
981 type = float_type_node;
982 else if (bits == DOUBLE_TYPE_SIZE)
983 type = double_type_node;
984 else if (bits == LONG_DOUBLE_TYPE_SIZE)
985 type = long_double_type_node;
986 else
988 type = make_node(REAL_TYPE);
989 TYPE_PRECISION(type) = bits;
990 layout_type(type);
992 return this->make_type(type);
995 // Get an unnamed complex type.
997 Btype*
998 Gcc_backend::complex_type(int bits)
1000 tree type;
1001 if (bits == FLOAT_TYPE_SIZE * 2)
1002 type = complex_float_type_node;
1003 else if (bits == DOUBLE_TYPE_SIZE * 2)
1004 type = complex_double_type_node;
1005 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
1006 type = complex_long_double_type_node;
1007 else
1009 type = make_node(REAL_TYPE);
1010 TYPE_PRECISION(type) = bits / 2;
1011 layout_type(type);
1012 type = build_complex_type(type);
1014 return this->make_type(type);
1017 // Get a pointer type.
1019 Btype*
1020 Gcc_backend::pointer_type(Btype* to_type)
1022 tree to_type_tree = to_type->get_tree();
1023 if (to_type_tree == error_mark_node)
1024 return this->error_type();
1025 tree type = build_pointer_type(to_type_tree);
1026 return this->make_type(type);
1029 // Make a function type.
1031 Btype*
1032 Gcc_backend::function_type(const Btyped_identifier& receiver,
1033 const std::vector<Btyped_identifier>& parameters,
1034 const std::vector<Btyped_identifier>& results,
1035 Btype* result_struct,
1036 Location)
1038 tree args = NULL_TREE;
1039 tree* pp = &args;
1040 if (receiver.btype != NULL)
1042 tree t = receiver.btype->get_tree();
1043 if (t == error_mark_node)
1044 return this->error_type();
1045 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
1046 pp = &TREE_CHAIN(*pp);
1049 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
1050 p != parameters.end();
1051 ++p)
1053 tree t = p->btype->get_tree();
1054 if (t == error_mark_node)
1055 return this->error_type();
1056 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
1057 pp = &TREE_CHAIN(*pp);
1060 // Varargs is handled entirely at the Go level. When converted to
1061 // GENERIC functions are not varargs.
1062 *pp = void_list_node;
1064 tree result;
1065 if (results.empty())
1066 result = void_type_node;
1067 else if (results.size() == 1)
1068 result = results.front().btype->get_tree();
1069 else
1071 gcc_assert(result_struct != NULL);
1072 result = result_struct->get_tree();
1074 if (result == error_mark_node)
1075 return this->error_type();
1077 // The libffi library cannot represent a zero-sized object. To
1078 // avoid causing confusion on 32-bit SPARC, we treat a function that
1079 // returns a zero-sized value as returning void. That should do no
1080 // harm since there is no actual value to be returned. See
1081 // https://gcc.gnu.org/PR72814 for details.
1082 if (result != void_type_node && int_size_in_bytes(result) == 0)
1083 result = void_type_node;
1085 tree fntype = build_function_type(result, args);
1086 if (fntype == error_mark_node)
1087 return this->error_type();
1089 return this->make_type(build_pointer_type(fntype));
1092 // Make a struct type.
1094 Btype*
1095 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
1097 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
1100 // Fill in the fields of a struct type.
1102 Btype*
1103 Gcc_backend::fill_in_struct(Btype* fill,
1104 const std::vector<Btyped_identifier>& fields)
1106 tree fill_tree = fill->get_tree();
1107 tree field_trees = NULL_TREE;
1108 tree* pp = &field_trees;
1109 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
1110 p != fields.end();
1111 ++p)
1113 tree name_tree = get_identifier_from_string(p->name);
1114 tree type_tree = p->btype->get_tree();
1115 if (type_tree == error_mark_node)
1116 return this->error_type();
1117 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
1118 type_tree);
1119 DECL_CONTEXT(field) = fill_tree;
1120 *pp = field;
1121 pp = &DECL_CHAIN(field);
1123 TYPE_FIELDS(fill_tree) = field_trees;
1124 layout_type(fill_tree);
1126 // Because Go permits converting between named struct types and
1127 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
1128 // because we don't try to maintain TYPE_CANONICAL for struct types,
1129 // we need to tell the middle-end to use structural equality.
1130 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
1132 return fill;
1135 // Make an array type.
1137 Btype*
1138 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
1140 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
1141 element_btype, length);
1144 // Fill in an array type.
1146 Btype*
1147 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
1148 Bexpression* length)
1150 tree element_type_tree = element_type->get_tree();
1151 tree length_tree = length->get_tree();
1152 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
1153 return this->error_type();
1155 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
1157 length_tree = fold_convert(sizetype, length_tree);
1159 // build_index_type takes the maximum index, which is one less than
1160 // the length.
1161 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
1162 length_tree,
1163 size_one_node));
1165 tree fill_tree = fill->get_tree();
1166 TREE_TYPE(fill_tree) = element_type_tree;
1167 TYPE_DOMAIN(fill_tree) = index_type_tree;
1168 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
1169 layout_type(fill_tree);
1171 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
1172 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
1173 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
1174 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
1175 TYPE_CANONICAL(fill_tree) =
1176 build_array_type(TYPE_CANONICAL(element_type_tree),
1177 TYPE_CANONICAL(index_type_tree));
1179 return fill;
1182 // Create a placeholder for a pointer type.
1184 Btype*
1185 Gcc_backend::placeholder_pointer_type(const std::string& name,
1186 Location location, bool)
1188 tree ret = build_distinct_type_copy(ptr_type_node);
1189 if (!name.empty())
1191 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1192 get_identifier_from_string(name),
1193 ret);
1194 TYPE_NAME(ret) = decl;
1196 return this->make_type(ret);
1199 // Set the real target type for a placeholder pointer type.
1201 bool
1202 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
1203 Btype* to_type)
1205 tree pt = placeholder->get_tree();
1206 if (pt == error_mark_node)
1207 return false;
1208 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
1209 tree tt = to_type->get_tree();
1210 if (tt == error_mark_node)
1212 placeholder->set_tree(error_mark_node);
1213 return false;
1215 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
1216 TREE_TYPE(pt) = TREE_TYPE(tt);
1217 TYPE_CANONICAL(pt) = TYPE_CANONICAL(tt);
1218 if (TYPE_NAME(pt) != NULL_TREE)
1220 // Build the data structure gcc wants to see for a typedef.
1221 tree copy = build_variant_type_copy(pt);
1222 TYPE_NAME(copy) = NULL_TREE;
1223 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
1225 return true;
1228 // Set the real values for a placeholder function type.
1230 bool
1231 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
1233 return this->set_placeholder_pointer_type(placeholder, ft);
1236 // Create a placeholder for a struct type.
1238 Btype*
1239 Gcc_backend::placeholder_struct_type(const std::string& name,
1240 Location location)
1242 tree ret = make_node(RECORD_TYPE);
1243 if (!name.empty())
1245 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1246 get_identifier_from_string(name),
1247 ret);
1248 TYPE_NAME(ret) = decl;
1250 // The struct type that eventually replaces this placeholder will require
1251 // structural equality. The placeholder must too, so that the requirement
1252 // for structural equality propagates to references that are constructed
1253 // before the replacement occurs.
1254 SET_TYPE_STRUCTURAL_EQUALITY(ret);
1256 return this->make_type(ret);
1259 // Fill in the fields of a placeholder struct type.
1261 bool
1262 Gcc_backend::set_placeholder_struct_type(
1263 Btype* placeholder,
1264 const std::vector<Btyped_identifier>& fields)
1266 tree t = placeholder->get_tree();
1267 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
1268 Btype* r = this->fill_in_struct(placeholder, fields);
1270 if (TYPE_NAME(t) != NULL_TREE)
1272 // Build the data structure gcc wants to see for a typedef.
1273 tree copy = build_distinct_type_copy(t);
1274 TYPE_NAME(copy) = NULL_TREE;
1275 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1276 TYPE_SIZE(copy) = NULL_TREE;
1277 Btype* bc = this->make_type(copy);
1278 this->fill_in_struct(bc, fields);
1279 delete bc;
1282 return r->get_tree() != error_mark_node;
1285 // Create a placeholder for an array type.
1287 Btype*
1288 Gcc_backend::placeholder_array_type(const std::string& name,
1289 Location location)
1291 tree ret = make_node(ARRAY_TYPE);
1292 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1293 get_identifier_from_string(name),
1294 ret);
1295 TYPE_NAME(ret) = decl;
1296 return this->make_type(ret);
1299 // Fill in the fields of a placeholder array type.
1301 bool
1302 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1303 Btype* element_btype,
1304 Bexpression* length)
1306 tree t = placeholder->get_tree();
1307 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1308 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1310 // Build the data structure gcc wants to see for a typedef.
1311 tree copy = build_distinct_type_copy(t);
1312 TYPE_NAME(copy) = NULL_TREE;
1313 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1315 return r->get_tree() != error_mark_node;
1318 // Return a named version of a type.
1320 Btype*
1321 Gcc_backend::named_type(const std::string& name, Btype* btype,
1322 Location location)
1324 tree type = btype->get_tree();
1325 if (type == error_mark_node)
1326 return this->error_type();
1328 // The middle-end expects a basic type to have a name. In Go every
1329 // basic type will have a name. The first time we see a basic type,
1330 // give it whatever Go name we have at this point.
1331 if (TYPE_NAME(type) == NULL_TREE
1332 && location.gcc_location() == BUILTINS_LOCATION
1333 && (TREE_CODE(type) == INTEGER_TYPE
1334 || TREE_CODE(type) == REAL_TYPE
1335 || TREE_CODE(type) == COMPLEX_TYPE
1336 || TREE_CODE(type) == BOOLEAN_TYPE))
1338 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1339 get_identifier_from_string(name),
1340 type);
1341 TYPE_NAME(type) = decl;
1342 return this->make_type(type);
1345 tree copy = build_variant_type_copy(type);
1346 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1347 get_identifier_from_string(name),
1348 copy);
1349 DECL_ORIGINAL_TYPE(decl) = type;
1350 TYPE_NAME(copy) = decl;
1351 return this->make_type(copy);
1354 // Return a pointer type used as a marker for a circular type.
1356 Btype*
1357 Gcc_backend::circular_pointer_type(Btype*, bool)
1359 return this->make_type(ptr_type_node);
1362 // Return whether we might be looking at a circular type.
1364 bool
1365 Gcc_backend::is_circular_pointer_type(Btype* btype)
1367 return btype->get_tree() == ptr_type_node;
1370 // Return the size of a type.
1372 int64_t
1373 Gcc_backend::type_size(Btype* btype)
1375 tree t = btype->get_tree();
1376 if (t == error_mark_node)
1377 return 1;
1378 if (t == void_type_node)
1379 return 0;
1380 t = TYPE_SIZE_UNIT(t);
1381 gcc_assert(tree_fits_uhwi_p (t));
1382 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1383 int64_t ret = static_cast<int64_t>(val_wide);
1384 if (ret < 0 || static_cast<unsigned HOST_WIDE_INT>(ret) != val_wide)
1385 return -1;
1386 return ret;
1389 // Return the alignment of a type.
1391 int64_t
1392 Gcc_backend::type_alignment(Btype* btype)
1394 tree t = btype->get_tree();
1395 if (t == error_mark_node)
1396 return 1;
1397 return TYPE_ALIGN_UNIT(t);
1400 // Return the alignment of a struct field of type BTYPE.
1402 int64_t
1403 Gcc_backend::type_field_alignment(Btype* btype)
1405 tree t = btype->get_tree();
1406 if (t == error_mark_node)
1407 return 1;
1408 return go_field_alignment(t);
1411 // Return the offset of a field in a struct.
1413 int64_t
1414 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1416 tree struct_tree = btype->get_tree();
1417 if (struct_tree == error_mark_node)
1418 return 0;
1419 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1420 tree field = TYPE_FIELDS(struct_tree);
1421 for (; index > 0; --index)
1423 field = DECL_CHAIN(field);
1424 gcc_assert(field != NULL_TREE);
1426 HOST_WIDE_INT offset_wide = int_byte_position(field);
1427 int64_t ret = static_cast<int64_t>(offset_wide);
1428 gcc_assert(ret == offset_wide);
1429 return ret;
1432 // Return the zero value for a type.
1434 Bexpression*
1435 Gcc_backend::zero_expression(Btype* btype)
1437 tree t = btype->get_tree();
1438 tree ret;
1439 if (t == error_mark_node)
1440 ret = error_mark_node;
1441 else
1442 ret = build_zero_cst(t);
1443 return this->make_expression(ret);
1446 // An expression that references a variable.
1448 Bexpression*
1449 Gcc_backend::var_expression(Bvariable* var, Location location)
1451 tree ret = var->get_tree(location);
1452 if (ret == error_mark_node)
1453 return this->error_expression();
1454 return this->make_expression(ret);
1457 // An expression that indirectly references an expression.
1459 Bexpression*
1460 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1461 bool known_valid, Location location)
1463 tree expr_tree = expr->get_tree();
1464 tree type_tree = btype->get_tree();
1465 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1466 return this->error_expression();
1468 // If the type of EXPR is a recursive pointer type, then we
1469 // need to insert a cast before indirecting.
1470 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1471 if (VOID_TYPE_P(target_type_tree))
1472 expr_tree = fold_convert_loc(location.gcc_location(),
1473 build_pointer_type(type_tree), expr_tree);
1475 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1476 expr_tree);
1477 if (known_valid)
1478 TREE_THIS_NOTRAP(ret) = 1;
1479 return this->make_expression(ret);
1482 // Return an expression that declares a constant named NAME with the
1483 // constant value VAL in BTYPE.
1485 Bexpression*
1486 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1487 Bexpression* val, Location location)
1489 tree type_tree = btype->get_tree();
1490 tree const_val = val->get_tree();
1491 if (type_tree == error_mark_node || const_val == error_mark_node)
1492 return this->error_expression();
1494 tree name_tree = get_identifier_from_string(name);
1495 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1496 type_tree);
1497 DECL_INITIAL(decl) = const_val;
1498 TREE_CONSTANT(decl) = 1;
1499 TREE_READONLY(decl) = 1;
1501 go_preserve_from_gc(decl);
1502 return this->make_expression(decl);
1505 // Return a typed value as a constant integer.
1507 Bexpression*
1508 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1510 tree t = btype->get_tree();
1511 if (t == error_mark_node)
1512 return this->error_expression();
1514 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1515 return this->make_expression(ret);
1518 // Return a typed value as a constant floating-point number.
1520 Bexpression*
1521 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1523 tree t = btype->get_tree();
1524 tree ret;
1525 if (t == error_mark_node)
1526 return this->error_expression();
1528 REAL_VALUE_TYPE r1;
1529 real_from_mpfr(&r1, val, t, GMP_RNDN);
1530 REAL_VALUE_TYPE r2;
1531 real_convert(&r2, TYPE_MODE(t), &r1);
1532 ret = build_real(t, r2);
1533 return this->make_expression(ret);
1536 // Return a typed real and imaginary value as a constant complex number.
1538 Bexpression*
1539 Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
1541 tree t = btype->get_tree();
1542 tree ret;
1543 if (t == error_mark_node)
1544 return this->error_expression();
1546 REAL_VALUE_TYPE r1;
1547 real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
1548 REAL_VALUE_TYPE r2;
1549 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1551 REAL_VALUE_TYPE r3;
1552 real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
1553 REAL_VALUE_TYPE r4;
1554 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1556 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1557 build_real(TREE_TYPE(t), r4));
1558 return this->make_expression(ret);
1561 // Make a constant string expression.
1563 Bexpression*
1564 Gcc_backend::string_constant_expression(const std::string& val)
1566 tree index_type = build_index_type(size_int(val.length()));
1567 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1568 TYPE_QUAL_CONST);
1569 tree string_type = build_array_type(const_char_type, index_type);
1570 TYPE_STRING_FLAG(string_type) = 1;
1571 tree string_val = build_string(val.length(), val.data());
1572 TREE_TYPE(string_val) = string_type;
1574 return this->make_expression(string_val);
1577 // Make a constant boolean expression.
1579 Bexpression*
1580 Gcc_backend::boolean_constant_expression(bool val)
1582 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1583 return this->make_expression(bool_cst);
1586 // Return the real part of a complex expression.
1588 Bexpression*
1589 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1591 tree complex_tree = bcomplex->get_tree();
1592 if (complex_tree == error_mark_node)
1593 return this->error_expression();
1594 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1595 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1596 TREE_TYPE(TREE_TYPE(complex_tree)),
1597 complex_tree);
1598 return this->make_expression(ret);
1601 // Return the imaginary part of a complex expression.
1603 Bexpression*
1604 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1606 tree complex_tree = bcomplex->get_tree();
1607 if (complex_tree == error_mark_node)
1608 return this->error_expression();
1609 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1610 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1611 TREE_TYPE(TREE_TYPE(complex_tree)),
1612 complex_tree);
1613 return this->make_expression(ret);
1616 // Make a complex expression given its real and imaginary parts.
1618 Bexpression*
1619 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1620 Location location)
1622 tree real_tree = breal->get_tree();
1623 tree imag_tree = bimag->get_tree();
1624 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1625 return this->error_expression();
1626 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1627 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1628 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1629 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1630 build_complex_type(TREE_TYPE(real_tree)),
1631 real_tree, imag_tree);
1632 return this->make_expression(ret);
1635 // An expression that converts an expression to a different type.
1637 Bexpression*
1638 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1639 Location location)
1641 tree type_tree = type->get_tree();
1642 tree expr_tree = expr->get_tree();
1643 if (type_tree == error_mark_node
1644 || expr_tree == error_mark_node
1645 || TREE_TYPE(expr_tree) == error_mark_node)
1646 return this->error_expression();
1648 tree ret;
1649 if (this->type_size(type) == 0
1650 || TREE_TYPE(expr_tree) == void_type_node)
1652 // Do not convert zero-sized types.
1653 ret = expr_tree;
1655 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1656 ret = fold(convert_to_integer(type_tree, expr_tree));
1657 else if (TREE_CODE(type_tree) == REAL_TYPE)
1658 ret = fold(convert_to_real(type_tree, expr_tree));
1659 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1660 ret = fold(convert_to_complex(type_tree, expr_tree));
1661 else if (TREE_CODE(type_tree) == POINTER_TYPE
1662 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1663 ret = fold(convert_to_pointer(type_tree, expr_tree));
1664 else if (TREE_CODE(type_tree) == RECORD_TYPE
1665 || TREE_CODE(type_tree) == ARRAY_TYPE)
1666 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1667 type_tree, expr_tree);
1668 else
1669 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1671 return this->make_expression(ret);
1674 // Get the address of a function.
1676 Bexpression*
1677 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1679 tree func = bfunc->get_tree();
1680 if (func == error_mark_node)
1681 return this->error_expression();
1683 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1684 return this->make_expression(ret);
1687 // Get the address of an expression.
1689 Bexpression*
1690 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1692 tree expr = bexpr->get_tree();
1693 if (expr == error_mark_node)
1694 return this->error_expression();
1696 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1697 return this->make_expression(ret);
1700 // Return an expression for the field at INDEX in BSTRUCT.
1702 Bexpression*
1703 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1704 Location location)
1706 tree struct_tree = bstruct->get_tree();
1707 if (struct_tree == error_mark_node
1708 || TREE_TYPE(struct_tree) == error_mark_node)
1709 return this->error_expression();
1710 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1711 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1712 if (field == NULL_TREE)
1714 // This can happen for a type which refers to itself indirectly
1715 // and then turns out to be erroneous.
1716 return this->error_expression();
1718 for (unsigned int i = index; i > 0; --i)
1720 field = DECL_CHAIN(field);
1721 gcc_assert(field != NULL_TREE);
1723 if (TREE_TYPE(field) == error_mark_node)
1724 return this->error_expression();
1725 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1726 TREE_TYPE(field), struct_tree, field,
1727 NULL_TREE);
1728 if (TREE_CONSTANT(struct_tree))
1729 TREE_CONSTANT(ret) = 1;
1730 return this->make_expression(ret);
1733 // Return an expression that executes BSTAT before BEXPR.
1735 Bexpression*
1736 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1737 Location location)
1739 tree stat = bstat->get_tree();
1740 tree expr = bexpr->get_tree();
1741 if (stat == error_mark_node || expr == error_mark_node)
1742 return this->error_expression();
1743 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1744 TREE_TYPE(expr), stat, expr);
1745 return this->make_expression(ret);
1748 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1749 // ELSE_EXPR otherwise.
1751 Bexpression*
1752 Gcc_backend::conditional_expression(Bfunction*, Btype* btype,
1753 Bexpression* condition,
1754 Bexpression* then_expr,
1755 Bexpression* else_expr, Location location)
1757 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1758 tree cond_tree = condition->get_tree();
1759 tree then_tree = then_expr->get_tree();
1760 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1761 if (type_tree == error_mark_node
1762 || cond_tree == error_mark_node
1763 || then_tree == error_mark_node
1764 || else_tree == error_mark_node)
1765 return this->error_expression();
1766 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1767 cond_tree, then_tree, else_tree);
1768 return this->make_expression(ret);
1771 // Return an expression for the unary operation OP EXPR.
1773 Bexpression*
1774 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1776 tree expr_tree = expr->get_tree();
1777 if (expr_tree == error_mark_node
1778 || TREE_TYPE(expr_tree) == error_mark_node)
1779 return this->error_expression();
1781 tree type_tree = TREE_TYPE(expr_tree);
1782 enum tree_code code;
1783 switch (op)
1785 case OPERATOR_MINUS:
1787 tree computed_type = excess_precision_type(type_tree);
1788 if (computed_type != NULL_TREE)
1790 expr_tree = convert(computed_type, expr_tree);
1791 type_tree = computed_type;
1793 code = NEGATE_EXPR;
1794 break;
1796 case OPERATOR_NOT:
1797 code = TRUTH_NOT_EXPR;
1798 break;
1799 case OPERATOR_XOR:
1800 code = BIT_NOT_EXPR;
1801 break;
1802 default:
1803 gcc_unreachable();
1804 break;
1807 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1808 expr_tree);
1809 return this->make_expression(ret);
1812 // Convert a gofrontend operator to an equivalent tree_code.
1814 static enum tree_code
1815 operator_to_tree_code(Operator op, tree type)
1817 enum tree_code code;
1818 switch (op)
1820 case OPERATOR_EQEQ:
1821 code = EQ_EXPR;
1822 break;
1823 case OPERATOR_NOTEQ:
1824 code = NE_EXPR;
1825 break;
1826 case OPERATOR_LT:
1827 code = LT_EXPR;
1828 break;
1829 case OPERATOR_LE:
1830 code = LE_EXPR;
1831 break;
1832 case OPERATOR_GT:
1833 code = GT_EXPR;
1834 break;
1835 case OPERATOR_GE:
1836 code = GE_EXPR;
1837 break;
1838 case OPERATOR_OROR:
1839 code = TRUTH_ORIF_EXPR;
1840 break;
1841 case OPERATOR_ANDAND:
1842 code = TRUTH_ANDIF_EXPR;
1843 break;
1844 case OPERATOR_PLUS:
1845 code = PLUS_EXPR;
1846 break;
1847 case OPERATOR_MINUS:
1848 code = MINUS_EXPR;
1849 break;
1850 case OPERATOR_OR:
1851 code = BIT_IOR_EXPR;
1852 break;
1853 case OPERATOR_XOR:
1854 code = BIT_XOR_EXPR;
1855 break;
1856 case OPERATOR_MULT:
1857 code = MULT_EXPR;
1858 break;
1859 case OPERATOR_DIV:
1860 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1861 code = RDIV_EXPR;
1862 else
1863 code = TRUNC_DIV_EXPR;
1864 break;
1865 case OPERATOR_MOD:
1866 code = TRUNC_MOD_EXPR;
1867 break;
1868 case OPERATOR_LSHIFT:
1869 code = LSHIFT_EXPR;
1870 break;
1871 case OPERATOR_RSHIFT:
1872 code = RSHIFT_EXPR;
1873 break;
1874 case OPERATOR_AND:
1875 code = BIT_AND_EXPR;
1876 break;
1877 case OPERATOR_BITCLEAR:
1878 code = BIT_AND_EXPR;
1879 break;
1880 default:
1881 gcc_unreachable();
1884 return code;
1887 // Return an expression for the binary operation LEFT OP RIGHT.
1889 Bexpression*
1890 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1891 Bexpression* right, Location location)
1893 tree left_tree = left->get_tree();
1894 tree right_tree = right->get_tree();
1895 if (left_tree == error_mark_node
1896 || right_tree == error_mark_node)
1897 return this->error_expression();
1898 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1900 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1901 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1902 tree computed_type = excess_precision_type(type_tree);
1903 if (computed_type != NULL_TREE)
1905 left_tree = convert(computed_type, left_tree);
1906 right_tree = convert(computed_type, right_tree);
1907 type_tree = computed_type;
1910 // For comparison operators, the resulting type should be boolean.
1911 switch (op)
1913 case OPERATOR_EQEQ:
1914 case OPERATOR_NOTEQ:
1915 case OPERATOR_LT:
1916 case OPERATOR_LE:
1917 case OPERATOR_GT:
1918 case OPERATOR_GE:
1919 type_tree = boolean_type_node;
1920 break;
1921 default:
1922 break;
1925 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1926 left_tree, right_tree);
1927 return this->make_expression(ret);
1930 // Return an expression that constructs BTYPE with VALS.
1932 Bexpression*
1933 Gcc_backend::constructor_expression(Btype* btype,
1934 const std::vector<Bexpression*>& vals,
1935 Location location)
1937 tree type_tree = btype->get_tree();
1938 if (type_tree == error_mark_node)
1939 return this->error_expression();
1941 vec<constructor_elt, va_gc> *init;
1942 vec_alloc(init, vals.size());
1944 tree sink = NULL_TREE;
1945 bool is_constant = true;
1946 tree field = TYPE_FIELDS(type_tree);
1947 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1948 p != vals.end();
1949 ++p, field = DECL_CHAIN(field))
1951 gcc_assert(field != NULL_TREE);
1952 tree val = (*p)->get_tree();
1953 if (TREE_TYPE(field) == error_mark_node
1954 || val == error_mark_node
1955 || TREE_TYPE(val) == error_mark_node)
1956 return this->error_expression();
1958 if (int_size_in_bytes(TREE_TYPE(field)) == 0)
1960 // GIMPLE cannot represent indices of zero-sized types so
1961 // trying to construct a map with zero-sized keys might lead
1962 // to errors. Instead, we evaluate each expression that
1963 // would have been added as a map element for its
1964 // side-effects and construct an empty map.
1965 append_to_statement_list(val, &sink);
1966 continue;
1969 constructor_elt empty = {NULL, NULL};
1970 constructor_elt* elt = init->quick_push(empty);
1971 elt->index = field;
1972 elt->value = this->convert_tree(TREE_TYPE(field), val, location);
1973 if (!TREE_CONSTANT(elt->value))
1974 is_constant = false;
1976 gcc_assert(field == NULL_TREE);
1977 tree ret = build_constructor(type_tree, init);
1978 if (is_constant)
1979 TREE_CONSTANT(ret) = 1;
1980 if (sink != NULL_TREE)
1981 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1982 type_tree, sink, ret);
1983 return this->make_expression(ret);
1986 Bexpression*
1987 Gcc_backend::array_constructor_expression(
1988 Btype* array_btype, const std::vector<unsigned long>& indexes,
1989 const std::vector<Bexpression*>& vals, Location location)
1991 tree type_tree = array_btype->get_tree();
1992 if (type_tree == error_mark_node)
1993 return this->error_expression();
1995 gcc_assert(indexes.size() == vals.size());
1997 tree element_type = TREE_TYPE(type_tree);
1998 HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
1999 vec<constructor_elt, va_gc> *init;
2000 vec_alloc(init, element_size == 0 ? 0 : vals.size());
2002 tree sink = NULL_TREE;
2003 bool is_constant = true;
2004 for (size_t i = 0; i < vals.size(); ++i)
2006 tree index = size_int(indexes[i]);
2007 tree val = (vals[i])->get_tree();
2009 if (index == error_mark_node
2010 || val == error_mark_node)
2011 return this->error_expression();
2013 if (element_size == 0)
2015 // GIMPLE cannot represent arrays of zero-sized types so trying
2016 // to construct an array of zero-sized values might lead to errors.
2017 // Instead, we evaluate each expression that would have been added as
2018 // an array value for its side-effects and construct an empty array.
2019 append_to_statement_list(val, &sink);
2020 continue;
2023 if (!TREE_CONSTANT(val))
2024 is_constant = false;
2026 constructor_elt empty = {NULL, NULL};
2027 constructor_elt* elt = init->quick_push(empty);
2028 elt->index = index;
2029 elt->value = val;
2032 tree ret = build_constructor(type_tree, init);
2033 if (is_constant)
2034 TREE_CONSTANT(ret) = 1;
2035 if (sink != NULL_TREE)
2036 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
2037 type_tree, sink, ret);
2038 return this->make_expression(ret);
2041 // Return an expression for the address of BASE[INDEX].
2043 Bexpression*
2044 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
2045 Location location)
2047 tree base_tree = base->get_tree();
2048 tree index_tree = index->get_tree();
2049 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
2050 if (base_tree == error_mark_node
2051 || TREE_TYPE(base_tree) == error_mark_node
2052 || index_tree == error_mark_node
2053 || element_type_tree == error_mark_node)
2054 return this->error_expression();
2056 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
2057 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
2058 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
2059 index_tree, element_size);
2060 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
2061 TREE_TYPE(base_tree), base_tree, offset);
2062 return this->make_expression(ptr);
2065 // Return an expression representing ARRAY[INDEX]
2067 Bexpression*
2068 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
2069 Location location)
2071 tree array_tree = array->get_tree();
2072 tree index_tree = index->get_tree();
2073 if (array_tree == error_mark_node
2074 || TREE_TYPE(array_tree) == error_mark_node
2075 || index_tree == error_mark_node)
2076 return this->error_expression();
2078 // A function call that returns a zero sized object will have been
2079 // changed to return void. If we see void here, assume we are
2080 // dealing with a zero sized type and just evaluate the operands.
2081 tree ret;
2082 if (TREE_TYPE(array_tree) != void_type_node)
2083 ret = build4_loc(location.gcc_location(), ARRAY_REF,
2084 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
2085 index_tree, NULL_TREE, NULL_TREE);
2086 else
2087 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
2088 void_type_node, array_tree, index_tree);
2090 return this->make_expression(ret);
2093 // Create an expression for a call to FN_EXPR with FN_ARGS.
2094 Bexpression*
2095 Gcc_backend::call_expression(Bfunction*, // containing fcn for call
2096 Bexpression* fn_expr,
2097 const std::vector<Bexpression*>& fn_args,
2098 Bexpression* chain_expr,
2099 Location location)
2101 tree fn = fn_expr->get_tree();
2102 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
2103 return this->error_expression();
2105 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
2106 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
2108 size_t nargs = fn_args.size();
2109 tree* args = nargs == 0 ? NULL : new tree[nargs];
2110 for (size_t i = 0; i < nargs; ++i)
2112 args[i] = fn_args.at(i)->get_tree();
2113 if (args[i] == error_mark_node)
2114 return this->error_expression();
2117 tree fndecl = fn;
2118 if (TREE_CODE(fndecl) == ADDR_EXPR)
2119 fndecl = TREE_OPERAND(fndecl, 0);
2121 // This is to support builtin math functions when using 80387 math.
2122 tree excess_type = NULL_TREE;
2123 if (optimize
2124 && TREE_CODE(fndecl) == FUNCTION_DECL
2125 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2126 && DECL_IS_UNDECLARED_BUILTIN (fndecl)
2127 && nargs > 0
2128 && ((SCALAR_FLOAT_TYPE_P(rettype)
2129 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
2130 || (COMPLEX_FLOAT_TYPE_P(rettype)
2131 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
2133 excess_type = excess_precision_type(TREE_TYPE(args[0]));
2134 if (excess_type != NULL_TREE)
2136 tree excess_fndecl = mathfn_built_in(excess_type,
2137 DECL_FUNCTION_CODE(fndecl));
2138 if (excess_fndecl == NULL_TREE)
2139 excess_type = NULL_TREE;
2140 else
2142 fn = build_fold_addr_expr_loc(location.gcc_location(),
2143 excess_fndecl);
2144 for (size_t i = 0; i < nargs; ++i)
2146 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
2147 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
2148 args[i] = ::convert(excess_type, args[i]);
2154 tree ret =
2155 build_call_array_loc(location.gcc_location(),
2156 excess_type != NULL_TREE ? excess_type : rettype,
2157 fn, nargs, args);
2159 if (chain_expr)
2160 CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree();
2162 if (excess_type != NULL_TREE)
2164 // Calling convert here can undo our excess precision change.
2165 // That may or may not be a bug in convert_to_real.
2166 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
2169 delete[] args;
2170 return this->make_expression(ret);
2173 // An expression as a statement.
2175 Bstatement*
2176 Gcc_backend::expression_statement(Bfunction*, Bexpression* expr)
2178 return this->make_statement(expr->get_tree());
2181 // Variable initialization.
2183 Bstatement*
2184 Gcc_backend::init_statement(Bfunction*, Bvariable* var, Bexpression* init)
2186 tree var_tree = var->get_decl();
2187 tree init_tree = init->get_tree();
2188 if (var_tree == error_mark_node || init_tree == error_mark_node)
2189 return this->error_statement();
2190 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
2192 // To avoid problems with GNU ld, we don't make zero-sized
2193 // externally visible variables. That might lead us to doing an
2194 // initialization of a zero-sized expression to a non-zero sized
2195 // variable, or vice-versa. Avoid crashes by omitting the
2196 // initializer. Such initializations don't mean anything anyhow.
2197 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
2198 && init_tree != NULL_TREE
2199 && TREE_TYPE(init_tree) != void_type_node
2200 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
2202 DECL_INITIAL(var_tree) = init_tree;
2203 init_tree = NULL_TREE;
2206 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
2207 void_type_node, var_tree);
2208 if (init_tree != NULL_TREE)
2209 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
2210 void_type_node, init_tree, ret);
2212 return this->make_statement(ret);
2215 // Assignment.
2217 Bstatement*
2218 Gcc_backend::assignment_statement(Bfunction* bfn, Bexpression* lhs,
2219 Bexpression* rhs, Location location)
2221 tree lhs_tree = lhs->get_tree();
2222 tree rhs_tree = rhs->get_tree();
2223 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
2224 return this->error_statement();
2226 // To avoid problems with GNU ld, we don't make zero-sized
2227 // externally visible variables. That might lead us to doing an
2228 // assignment of a zero-sized expression to a non-zero sized
2229 // expression; avoid crashes here by avoiding assignments of
2230 // zero-sized expressions. Such assignments don't really mean
2231 // anything anyhow.
2232 if (TREE_TYPE(lhs_tree) == void_type_node
2233 || int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
2234 || TREE_TYPE(rhs_tree) == void_type_node
2235 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
2236 return this->compound_statement(this->expression_statement(bfn, lhs),
2237 this->expression_statement(bfn, rhs));
2239 rhs_tree = this->convert_tree(TREE_TYPE(lhs_tree), rhs_tree, location);
2241 return this->make_statement(fold_build2_loc(location.gcc_location(),
2242 MODIFY_EXPR,
2243 void_type_node,
2244 lhs_tree, rhs_tree));
2247 // Return.
2249 Bstatement*
2250 Gcc_backend::return_statement(Bfunction* bfunction,
2251 const std::vector<Bexpression*>& vals,
2252 Location location)
2254 tree fntree = bfunction->get_tree();
2255 if (fntree == error_mark_node)
2256 return this->error_statement();
2257 tree result = DECL_RESULT(fntree);
2258 if (result == error_mark_node)
2259 return this->error_statement();
2261 // If the result size is zero bytes, we have set the function type
2262 // to have a result type of void, so don't return anything.
2263 // See the function_type method.
2264 tree res_type = TREE_TYPE(result);
2265 if (res_type == void_type_node || int_size_in_bytes(res_type) == 0)
2267 tree stmt_list = NULL_TREE;
2268 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2269 p != vals.end();
2270 p++)
2272 tree val = (*p)->get_tree();
2273 if (val == error_mark_node)
2274 return this->error_statement();
2275 append_to_statement_list(val, &stmt_list);
2277 tree ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2278 void_type_node, NULL_TREE);
2279 append_to_statement_list(ret, &stmt_list);
2280 return this->make_statement(stmt_list);
2283 tree ret;
2284 if (vals.empty())
2285 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
2286 NULL_TREE);
2287 else if (vals.size() == 1)
2289 tree val = vals.front()->get_tree();
2290 if (val == error_mark_node)
2291 return this->error_statement();
2292 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2293 void_type_node, result,
2294 vals.front()->get_tree());
2295 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2296 void_type_node, set);
2298 else
2300 // To return multiple values, copy the values into a temporary
2301 // variable of the right structure type, and then assign the
2302 // temporary variable to the DECL_RESULT in the return
2303 // statement.
2304 tree stmt_list = NULL_TREE;
2305 tree rettype = TREE_TYPE(result);
2307 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2308 push_struct_function(fntree);
2309 else
2310 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2311 tree rettmp = create_tmp_var(rettype, "RESULT");
2312 pop_cfun();
2314 tree field = TYPE_FIELDS(rettype);
2315 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2316 p != vals.end();
2317 p++, field = DECL_CHAIN(field))
2319 gcc_assert(field != NULL_TREE);
2320 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
2321 TREE_TYPE(field), rettmp, field,
2322 NULL_TREE);
2323 tree val = (*p)->get_tree();
2324 if (val == error_mark_node)
2325 return this->error_statement();
2326 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2327 void_type_node,
2328 ref, (*p)->get_tree());
2329 append_to_statement_list(set, &stmt_list);
2331 gcc_assert(field == NULL_TREE);
2332 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2333 void_type_node,
2334 result, rettmp);
2335 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2336 void_type_node, set);
2337 append_to_statement_list(ret_expr, &stmt_list);
2338 ret = stmt_list;
2340 return this->make_statement(ret);
2343 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2344 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2345 // NULL, it will always be executed. This is used for handling defers in Go
2346 // functions. In C++, the resulting code is of this form:
2347 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2349 Bstatement*
2350 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2351 Bstatement* except_stmt,
2352 Bstatement* finally_stmt,
2353 Location location)
2355 tree stat_tree = bstat->get_tree();
2356 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2357 tree finally_tree = finally_stmt == NULL
2358 ? NULL_TREE
2359 : finally_stmt->get_tree();
2361 if (stat_tree == error_mark_node
2362 || except_tree == error_mark_node
2363 || finally_tree == error_mark_node)
2364 return this->error_statement();
2366 if (except_tree != NULL_TREE)
2367 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2368 void_type_node, stat_tree,
2369 build2_loc(location.gcc_location(), CATCH_EXPR,
2370 void_type_node, NULL, except_tree));
2371 if (finally_tree != NULL_TREE)
2372 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2373 void_type_node, stat_tree, finally_tree);
2374 return this->make_statement(stat_tree);
2377 // If.
2379 Bstatement*
2380 Gcc_backend::if_statement(Bfunction*, Bexpression* condition,
2381 Bblock* then_block, Bblock* else_block,
2382 Location location)
2384 tree cond_tree = condition->get_tree();
2385 tree then_tree = then_block->get_tree();
2386 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2387 if (cond_tree == error_mark_node
2388 || then_tree == error_mark_node
2389 || else_tree == error_mark_node)
2390 return this->error_statement();
2391 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2392 cond_tree, then_tree, else_tree);
2393 return this->make_statement(ret);
2396 // Switch.
2398 Bstatement*
2399 Gcc_backend::switch_statement(
2400 Bfunction* function,
2401 Bexpression* value,
2402 const std::vector<std::vector<Bexpression*> >& cases,
2403 const std::vector<Bstatement*>& statements,
2404 Location switch_location)
2406 gcc_assert(cases.size() == statements.size());
2408 tree decl = function->get_tree();
2409 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2410 push_struct_function(decl);
2411 else
2412 push_cfun(DECL_STRUCT_FUNCTION(decl));
2414 tree stmt_list = NULL_TREE;
2415 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2416 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2417 ps != statements.end();
2418 ++ps, ++pc)
2420 if (pc->empty())
2422 location_t loc = (*ps != NULL
2423 ? EXPR_LOCATION((*ps)->get_tree())
2424 : UNKNOWN_LOCATION);
2425 tree label = create_artificial_label(loc);
2426 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2427 append_to_statement_list(c, &stmt_list);
2429 else
2431 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2432 pcv != pc->end();
2433 ++pcv)
2435 tree t = (*pcv)->get_tree();
2436 if (t == error_mark_node)
2437 return this->error_statement();
2438 location_t loc = EXPR_LOCATION(t);
2439 tree label = create_artificial_label(loc);
2440 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2441 append_to_statement_list(c, &stmt_list);
2445 if (*ps != NULL)
2447 tree t = (*ps)->get_tree();
2448 if (t == error_mark_node)
2449 return this->error_statement();
2450 append_to_statement_list(t, &stmt_list);
2453 pop_cfun();
2455 tree tv = value->get_tree();
2456 if (tv == error_mark_node)
2457 return this->error_statement();
2458 tree t = build2_loc(switch_location.gcc_location(), SWITCH_EXPR,
2459 NULL_TREE, tv, stmt_list);
2460 return this->make_statement(t);
2463 // Pair of statements.
2465 Bstatement*
2466 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2468 tree stmt_list = NULL_TREE;
2469 tree t = s1->get_tree();
2470 if (t == error_mark_node)
2471 return this->error_statement();
2472 append_to_statement_list(t, &stmt_list);
2473 t = s2->get_tree();
2474 if (t == error_mark_node)
2475 return this->error_statement();
2476 append_to_statement_list(t, &stmt_list);
2478 // If neither statement has any side effects, stmt_list can be NULL
2479 // at this point.
2480 if (stmt_list == NULL_TREE)
2481 stmt_list = integer_zero_node;
2483 return this->make_statement(stmt_list);
2486 // List of statements.
2488 Bstatement*
2489 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2491 tree stmt_list = NULL_TREE;
2492 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2493 p != statements.end();
2494 ++p)
2496 tree t = (*p)->get_tree();
2497 if (t == error_mark_node)
2498 return this->error_statement();
2499 append_to_statement_list(t, &stmt_list);
2501 return this->make_statement(stmt_list);
2504 // Make a block. For some reason gcc uses a dual structure for
2505 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2506 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2507 // the Bblock.
2509 Bblock*
2510 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2511 const std::vector<Bvariable*>& vars,
2512 Location start_location,
2513 Location)
2515 tree block_tree = make_node(BLOCK);
2516 if (enclosing == NULL)
2518 tree fndecl = function->get_tree();
2519 gcc_assert(fndecl != NULL_TREE);
2521 // We may have already created a block for local variables when
2522 // we take the address of a parameter.
2523 if (DECL_INITIAL(fndecl) == NULL_TREE)
2525 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2526 DECL_INITIAL(fndecl) = block_tree;
2528 else
2530 tree superblock_tree = DECL_INITIAL(fndecl);
2531 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2532 tree* pp;
2533 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2534 *pp != NULL_TREE;
2535 pp = &BLOCK_CHAIN(*pp))
2537 *pp = block_tree;
2540 else
2542 tree superbind_tree = enclosing->get_tree();
2543 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2544 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2546 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2547 tree* pp;
2548 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2549 *pp != NULL_TREE;
2550 pp = &BLOCK_CHAIN(*pp))
2552 *pp = block_tree;
2555 tree* pp = &BLOCK_VARS(block_tree);
2556 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2557 pv != vars.end();
2558 ++pv)
2560 *pp = (*pv)->get_decl();
2561 if (*pp != error_mark_node)
2562 pp = &DECL_CHAIN(*pp);
2564 *pp = NULL_TREE;
2566 TREE_USED(block_tree) = 1;
2568 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2569 void_type_node, BLOCK_VARS(block_tree),
2570 NULL_TREE, block_tree);
2571 TREE_SIDE_EFFECTS(bind_tree) = 1;
2572 return new Bblock(bind_tree);
2575 // Add statements to a block.
2577 void
2578 Gcc_backend::block_add_statements(Bblock* bblock,
2579 const std::vector<Bstatement*>& statements)
2581 tree stmt_list = NULL_TREE;
2582 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2583 p != statements.end();
2584 ++p)
2586 tree s = (*p)->get_tree();
2587 if (s != error_mark_node)
2588 append_to_statement_list(s, &stmt_list);
2591 tree bind_tree = bblock->get_tree();
2592 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2593 BIND_EXPR_BODY(bind_tree) = stmt_list;
2596 // Return a block as a statement.
2598 Bstatement*
2599 Gcc_backend::block_statement(Bblock* bblock)
2601 tree bind_tree = bblock->get_tree();
2602 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2603 return this->make_statement(bind_tree);
2606 // This is not static because we declare it with GTY(()) in go-c.h.
2607 tree go_non_zero_struct;
2609 // Return a type corresponding to TYPE with non-zero size.
2611 tree
2612 Gcc_backend::non_zero_size_type(tree type)
2614 if (int_size_in_bytes(type) != 0)
2615 return type;
2617 switch (TREE_CODE(type))
2619 case RECORD_TYPE:
2620 if (TYPE_FIELDS(type) != NULL_TREE)
2622 tree ns = make_node(RECORD_TYPE);
2623 tree field_trees = NULL_TREE;
2624 tree *pp = &field_trees;
2625 for (tree field = TYPE_FIELDS(type);
2626 field != NULL_TREE;
2627 field = DECL_CHAIN(field))
2629 tree ft = TREE_TYPE(field);
2630 if (field == TYPE_FIELDS(type))
2631 ft = non_zero_size_type(ft);
2632 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2633 DECL_NAME(field), ft);
2634 DECL_CONTEXT(f) = ns;
2635 *pp = f;
2636 pp = &DECL_CHAIN(f);
2638 TYPE_FIELDS(ns) = field_trees;
2639 layout_type(ns);
2640 return ns;
2643 if (go_non_zero_struct == NULL_TREE)
2645 type = make_node(RECORD_TYPE);
2646 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2647 get_identifier("dummy"),
2648 boolean_type_node);
2649 DECL_CONTEXT(field) = type;
2650 TYPE_FIELDS(type) = field;
2651 layout_type(type);
2652 go_non_zero_struct = type;
2654 return go_non_zero_struct;
2656 case ARRAY_TYPE:
2658 tree element_type = non_zero_size_type(TREE_TYPE(type));
2659 return build_array_type_nelts(element_type, 1);
2662 default:
2663 gcc_unreachable();
2666 gcc_unreachable();
2669 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type
2670 // can be created multiple times and thus have multiple tree
2671 // representations. Make sure this does not confuse the middle-end.
2673 tree
2674 Gcc_backend::convert_tree(tree type_tree, tree expr_tree, Location location)
2676 if (type_tree == TREE_TYPE(expr_tree))
2677 return expr_tree;
2679 if (type_tree == error_mark_node
2680 || expr_tree == error_mark_node
2681 || TREE_TYPE(expr_tree) == error_mark_node)
2682 return error_mark_node;
2684 gcc_assert(TREE_CODE(type_tree) == TREE_CODE(TREE_TYPE(expr_tree)));
2685 if (POINTER_TYPE_P(type_tree)
2686 || INTEGRAL_TYPE_P(type_tree)
2687 || SCALAR_FLOAT_TYPE_P(type_tree)
2688 || COMPLEX_FLOAT_TYPE_P(type_tree))
2689 return fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
2690 else if (TREE_CODE(type_tree) == RECORD_TYPE
2691 || TREE_CODE(type_tree) == ARRAY_TYPE)
2693 gcc_assert(int_size_in_bytes(type_tree)
2694 == int_size_in_bytes(TREE_TYPE(expr_tree)));
2695 if (TYPE_MAIN_VARIANT(type_tree)
2696 == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
2697 return fold_build1_loc(location.gcc_location(), NOP_EXPR,
2698 type_tree, expr_tree);
2699 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
2700 type_tree, expr_tree);
2703 gcc_unreachable();
2706 // Make a global variable.
2708 Bvariable*
2709 Gcc_backend::global_variable(const std::string& var_name,
2710 const std::string& asm_name,
2711 Btype* btype,
2712 unsigned int flags,
2713 Location location)
2715 tree type_tree = btype->get_tree();
2716 if (type_tree == error_mark_node)
2717 return this->error_variable();
2719 // The GNU linker does not like dynamic variables with zero size.
2720 tree orig_type_tree = type_tree;
2721 bool is_external = (flags & variable_is_external) != 0;
2722 bool is_hidden = (flags & variable_is_hidden) != 0;
2723 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2724 type_tree = this->non_zero_size_type(type_tree);
2726 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2727 get_identifier_from_string(var_name),
2728 type_tree);
2729 if ((flags & variable_is_external) != 0)
2731 DECL_EXTERNAL(decl) = 1;
2732 flags &=~ variable_is_external;
2734 else
2735 TREE_STATIC(decl) = 1;
2737 if ((flags & variable_is_hidden) == 0)
2738 TREE_PUBLIC(decl) = 1;
2739 else
2740 flags &=~ variable_is_hidden;
2742 if ((flags & variable_address_is_taken) != 0)
2744 TREE_ADDRESSABLE(decl) = 1;
2745 flags &=~ variable_address_is_taken;
2748 // We take the address in Bvariable::get_tree if orig_type_tree is
2749 // different from type_tree.
2750 if (orig_type_tree != type_tree)
2751 TREE_ADDRESSABLE(decl) = 1;
2753 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2755 TREE_USED(decl) = 1;
2757 if ((flags & variable_in_unique_section) != 0)
2759 resolve_unique_section (decl, 0, 1);
2760 flags &=~ variable_in_unique_section;
2763 gcc_assert(flags == 0);
2765 go_preserve_from_gc(decl);
2767 return new Bvariable(decl, orig_type_tree);
2770 // Set the initial value of a global variable.
2772 void
2773 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2775 tree expr_tree = expr->get_tree();
2776 if (expr_tree == error_mark_node)
2777 return;
2778 gcc_assert(TREE_CONSTANT(expr_tree));
2779 tree var_decl = var->get_decl();
2780 if (var_decl == error_mark_node)
2781 return;
2782 DECL_INITIAL(var_decl) = expr_tree;
2784 // If this variable goes in a unique section, it may need to go into
2785 // a different one now that DECL_INITIAL is set.
2786 if (symtab_node::get(var_decl)
2787 && symtab_node::get(var_decl)->implicit_section)
2789 set_decl_section_name (var_decl, (const char *) NULL);
2790 resolve_unique_section (var_decl,
2791 compute_reloc_for_constant (expr_tree),
2796 // Make a local variable.
2798 Bvariable*
2799 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2800 Btype* btype, Bvariable* decl_var,
2801 unsigned int flags, Location location)
2803 tree type_tree = btype->get_tree();
2804 if (type_tree == error_mark_node)
2805 return this->error_variable();
2806 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2807 get_identifier_from_string(name),
2808 type_tree);
2809 DECL_CONTEXT(decl) = function->get_tree();
2810 TREE_USED(decl) = 1;
2811 if ((flags & variable_address_is_taken) != 0)
2813 TREE_ADDRESSABLE(decl) = 1;
2814 flags &=~ variable_address_is_taken;
2816 if (decl_var != NULL)
2818 DECL_HAS_VALUE_EXPR_P(decl) = 1;
2819 SET_DECL_VALUE_EXPR(decl, decl_var->get_decl());
2821 go_assert(flags == 0);
2822 go_preserve_from_gc(decl);
2823 return new Bvariable(decl);
2826 // Make a function parameter variable.
2828 Bvariable*
2829 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2830 Btype* btype, unsigned int flags,
2831 Location location)
2833 tree type_tree = btype->get_tree();
2834 if (type_tree == error_mark_node)
2835 return this->error_variable();
2836 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2837 get_identifier_from_string(name),
2838 type_tree);
2839 DECL_CONTEXT(decl) = function->get_tree();
2840 DECL_ARG_TYPE(decl) = type_tree;
2841 TREE_USED(decl) = 1;
2842 if ((flags & variable_address_is_taken) != 0)
2844 TREE_ADDRESSABLE(decl) = 1;
2845 flags &=~ variable_address_is_taken;
2847 go_assert(flags == 0);
2848 go_preserve_from_gc(decl);
2849 return new Bvariable(decl);
2852 // Make a static chain variable.
2854 Bvariable*
2855 Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name,
2856 Btype* btype, unsigned int flags,
2857 Location location)
2859 tree type_tree = btype->get_tree();
2860 if (type_tree == error_mark_node)
2861 return this->error_variable();
2862 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2863 get_identifier_from_string(name), type_tree);
2864 tree fndecl = function->get_tree();
2865 DECL_CONTEXT(decl) = fndecl;
2866 DECL_ARG_TYPE(decl) = type_tree;
2867 TREE_USED(decl) = 1;
2868 DECL_ARTIFICIAL(decl) = 1;
2869 DECL_IGNORED_P(decl) = 1;
2870 DECL_NAMELESS(decl) = 1;
2871 TREE_READONLY(decl) = 1;
2873 struct function *f = DECL_STRUCT_FUNCTION(fndecl);
2874 if (f == NULL)
2876 push_struct_function(fndecl);
2877 pop_cfun();
2878 f = DECL_STRUCT_FUNCTION(fndecl);
2880 gcc_assert(f->static_chain_decl == NULL);
2881 f->static_chain_decl = decl;
2882 DECL_STATIC_CHAIN(fndecl) = 1;
2883 go_assert(flags == 0);
2885 go_preserve_from_gc(decl);
2886 return new Bvariable(decl);
2889 // Make a temporary variable.
2891 Bvariable*
2892 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2893 Btype* btype, Bexpression* binit,
2894 unsigned int flags,
2895 Location location,
2896 Bstatement** pstatement)
2898 gcc_assert(function != NULL);
2899 tree decl = function->get_tree();
2900 tree type_tree = btype->get_tree();
2901 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2902 if (type_tree == error_mark_node
2903 || init_tree == error_mark_node
2904 || decl == error_mark_node)
2906 *pstatement = this->error_statement();
2907 return this->error_variable();
2910 tree var;
2911 // We can only use create_tmp_var if the type is not addressable.
2912 if (!TREE_ADDRESSABLE(type_tree))
2914 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2915 push_struct_function(decl);
2916 else
2917 push_cfun(DECL_STRUCT_FUNCTION(decl));
2919 var = create_tmp_var(type_tree, "GOTMP");
2920 pop_cfun();
2922 else
2924 gcc_assert(bblock != NULL);
2925 var = build_decl(location.gcc_location(), VAR_DECL,
2926 create_tmp_var_name("GOTMP"),
2927 type_tree);
2928 DECL_ARTIFICIAL(var) = 1;
2929 DECL_IGNORED_P(var) = 1;
2930 DECL_NAMELESS(var) = 1;
2931 TREE_USED(var) = 1;
2932 DECL_CONTEXT(var) = decl;
2934 // We have to add this variable to the BLOCK and the BIND_EXPR.
2935 tree bind_tree = bblock->get_tree();
2936 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2937 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2938 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2939 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2940 BLOCK_VARS(block_tree) = var;
2941 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2944 if (this->type_size(btype) != 0
2945 && init_tree != NULL_TREE
2946 && TREE_TYPE(init_tree) != void_type_node)
2947 DECL_INITIAL(var) = this->convert_tree(type_tree, init_tree, location);
2949 if ((flags & variable_address_is_taken) != 0)
2951 TREE_ADDRESSABLE(var) = 1;
2952 flags &=~ variable_address_is_taken;
2955 gcc_assert(flags == 0);
2957 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2958 DECL_EXPR,
2959 void_type_node, var));
2961 // For a zero sized type, don't initialize VAR with BINIT, but still
2962 // evaluate BINIT for its side effects.
2963 if (init_tree != NULL_TREE
2964 && (this->type_size(btype) == 0
2965 || TREE_TYPE(init_tree) == void_type_node))
2966 *pstatement =
2967 this->compound_statement(this->expression_statement(function, binit),
2968 *pstatement);
2970 return new Bvariable(var);
2973 // Create an implicit variable that is compiler-defined. This is used when
2974 // generating GC root variables and storing the values of a slice initializer.
2976 Bvariable*
2977 Gcc_backend::implicit_variable(const std::string& name,
2978 const std::string& asm_name,
2979 Btype* type, unsigned int flags,
2980 int64_t alignment)
2982 tree type_tree = type->get_tree();
2983 if (type_tree == error_mark_node)
2984 return this->error_variable();
2986 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2987 get_identifier_from_string(name), type_tree);
2988 DECL_EXTERNAL(decl) = 0;
2989 if ((flags & variable_is_hidden) != 0)
2990 flags &=~ variable_is_hidden;
2991 else
2992 TREE_PUBLIC(decl) = 1;
2993 TREE_STATIC(decl) = 1;
2994 TREE_USED(decl) = 1;
2995 DECL_ARTIFICIAL(decl) = 1;
2996 if ((flags & variable_is_common) != 0)
2998 DECL_COMMON(decl) = 1;
3000 // When the initializer for one implicit_variable refers to another,
3001 // it needs to know the visibility of the referenced struct so that
3002 // compute_reloc_for_constant will return the right value. On many
3003 // systems calling make_decl_one_only will mark the decl as weak,
3004 // which will change the return value of compute_reloc_for_constant.
3005 // We can't reliably call make_decl_one_only yet, because we don't
3006 // yet know the initializer. This issue doesn't arise in C because
3007 // Go initializers, unlike C initializers, can be indirectly
3008 // recursive. To ensure that compute_reloc_for_constant computes
3009 // the right value if some other initializer refers to this one, we
3010 // mark this symbol as weak here. We undo that below in
3011 // immutable_struct_set_init before calling mark_decl_one_only.
3012 DECL_WEAK(decl) = 1;
3014 flags &=~ variable_is_common;
3016 if ((flags & variable_is_constant) != 0)
3018 TREE_READONLY(decl) = 1;
3019 TREE_CONSTANT(decl) = 1;
3020 flags &=~ variable_is_constant;
3022 if ((flags & variable_address_is_taken) != 0)
3024 TREE_ADDRESSABLE(decl) = 1;
3025 flags &=~ variable_address_is_taken;
3027 if (alignment != 0)
3029 SET_DECL_ALIGN(decl, alignment * BITS_PER_UNIT);
3030 DECL_USER_ALIGN(decl) = 1;
3032 if (! asm_name.empty())
3033 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
3034 gcc_assert(flags == 0);
3036 go_preserve_from_gc(decl);
3037 return new Bvariable(decl);
3040 // Set the initalizer for a variable created by implicit_variable.
3041 // This is where we finish compiling the variable.
3043 void
3044 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
3045 Btype*, unsigned int flags,
3046 Bexpression* init)
3048 tree decl = var->get_decl();
3049 tree init_tree;
3050 if (init == NULL)
3051 init_tree = NULL_TREE;
3052 else
3053 init_tree = init->get_tree();
3054 if (decl == error_mark_node || init_tree == error_mark_node)
3055 return;
3057 DECL_INITIAL(decl) = init_tree;
3059 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
3060 // See the comment where DECL_WEAK is set in implicit_variable.
3061 if ((flags & variable_is_common) != 0)
3063 DECL_WEAK(decl) = 0;
3064 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
3067 resolve_unique_section(decl, 2, 1);
3069 rest_of_decl_compilation(decl, 1, 0);
3072 // Return a reference to an implicit variable defined in another package.
3074 Bvariable*
3075 Gcc_backend::implicit_variable_reference(const std::string& name,
3076 const std::string& asm_name,
3077 Btype* btype)
3079 tree type_tree = btype->get_tree();
3080 if (type_tree == error_mark_node)
3081 return this->error_variable();
3083 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
3084 get_identifier_from_string(name), type_tree);
3085 DECL_EXTERNAL(decl) = 1;
3086 TREE_PUBLIC(decl) = 1;
3087 TREE_STATIC(decl) = 0;
3088 DECL_ARTIFICIAL(decl) = 1;
3089 if (! asm_name.empty())
3090 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
3091 go_preserve_from_gc(decl);
3092 return new Bvariable(decl);
3095 // Create a named immutable initialized data structure.
3097 Bvariable*
3098 Gcc_backend::immutable_struct(const std::string& name,
3099 const std::string& asm_name,
3100 unsigned int flags, Btype* btype,
3101 Location location)
3103 tree type_tree = btype->get_tree();
3104 if (type_tree == error_mark_node)
3105 return this->error_variable();
3106 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
3107 tree decl = build_decl(location.gcc_location(), VAR_DECL,
3108 get_identifier_from_string(name),
3109 build_qualified_type(type_tree, TYPE_QUAL_CONST));
3110 TREE_STATIC(decl) = 1;
3111 TREE_USED(decl) = 1;
3112 TREE_READONLY(decl) = 1;
3113 TREE_CONSTANT(decl) = 1;
3114 DECL_ARTIFICIAL(decl) = 1;
3115 if ((flags & variable_is_hidden) != 0)
3116 flags &=~ variable_is_hidden;
3117 else
3118 TREE_PUBLIC(decl) = 1;
3119 if (! asm_name.empty())
3120 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
3121 if ((flags & variable_address_is_taken) != 0)
3123 TREE_ADDRESSABLE(decl) = 1;
3124 flags &=~ variable_address_is_taken;
3127 // When the initializer for one immutable_struct refers to another,
3128 // it needs to know the visibility of the referenced struct so that
3129 // compute_reloc_for_constant will return the right value. On many
3130 // systems calling make_decl_one_only will mark the decl as weak,
3131 // which will change the return value of compute_reloc_for_constant.
3132 // We can't reliably call make_decl_one_only yet, because we don't
3133 // yet know the initializer. This issue doesn't arise in C because
3134 // Go initializers, unlike C initializers, can be indirectly
3135 // recursive. To ensure that compute_reloc_for_constant computes
3136 // the right value if some other initializer refers to this one, we
3137 // mark this symbol as weak here. We undo that below in
3138 // immutable_struct_set_init before calling mark_decl_one_only.
3139 if ((flags & variable_is_common) != 0)
3141 DECL_WEAK(decl) = 1;
3142 flags &=~ variable_is_common;
3145 gcc_assert(flags == 0);
3147 // We don't call rest_of_decl_compilation until we have the
3148 // initializer.
3150 go_preserve_from_gc(decl);
3151 return new Bvariable(decl);
3154 // Set the initializer for a variable created by immutable_struct.
3155 // This is where we finish compiling the variable.
3157 void
3158 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
3159 unsigned int flags, Btype*, Location,
3160 Bexpression* initializer)
3162 tree decl = var->get_decl();
3163 tree init_tree = initializer->get_tree();
3164 if (decl == error_mark_node || init_tree == error_mark_node)
3165 return;
3167 DECL_INITIAL(decl) = init_tree;
3169 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
3170 // See the comment where DECL_WEAK is set in immutable_struct.
3171 if ((flags & variable_is_common) != 0)
3173 DECL_WEAK(decl) = 0;
3174 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
3177 // These variables are often unneeded in the final program, so put
3178 // them in their own section so that linker GC can discard them.
3179 resolve_unique_section(decl,
3180 compute_reloc_for_constant (init_tree),
3183 rest_of_decl_compilation(decl, 1, 0);
3186 // Return a reference to an immutable initialized data structure
3187 // defined in another package.
3189 Bvariable*
3190 Gcc_backend::immutable_struct_reference(const std::string& name,
3191 const std::string& asm_name,
3192 Btype* btype,
3193 Location location)
3195 tree type_tree = btype->get_tree();
3196 if (type_tree == error_mark_node)
3197 return this->error_variable();
3198 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
3199 tree decl = build_decl(location.gcc_location(), VAR_DECL,
3200 get_identifier_from_string(name),
3201 build_qualified_type(type_tree, TYPE_QUAL_CONST));
3202 TREE_READONLY(decl) = 1;
3203 TREE_CONSTANT(decl) = 1;
3204 DECL_ARTIFICIAL(decl) = 1;
3205 TREE_PUBLIC(decl) = 1;
3206 DECL_EXTERNAL(decl) = 1;
3207 if (! asm_name.empty())
3208 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
3209 go_preserve_from_gc(decl);
3210 return new Bvariable(decl);
3213 // Make a label.
3215 Blabel*
3216 Gcc_backend::label(Bfunction* function, const std::string& name,
3217 Location location)
3219 tree decl;
3220 if (name.empty())
3222 tree func_tree = function->get_tree();
3223 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
3224 push_struct_function(func_tree);
3225 else
3226 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
3228 decl = create_artificial_label(location.gcc_location());
3230 pop_cfun();
3232 else
3234 tree id = get_identifier_from_string(name);
3235 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
3236 void_type_node);
3237 DECL_CONTEXT(decl) = function->get_tree();
3239 return new Blabel(decl);
3242 // Make a statement which defines a label.
3244 Bstatement*
3245 Gcc_backend::label_definition_statement(Blabel* label)
3247 tree lab = label->get_tree();
3248 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
3249 void_type_node, lab);
3250 return this->make_statement(ret);
3253 // Make a goto statement.
3255 Bstatement*
3256 Gcc_backend::goto_statement(Blabel* label, Location location)
3258 tree lab = label->get_tree();
3259 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
3260 lab);
3261 return this->make_statement(ret);
3264 // Get the address of a label.
3266 Bexpression*
3267 Gcc_backend::label_address(Blabel* label, Location location)
3269 tree lab = label->get_tree();
3270 TREE_USED(lab) = 1;
3271 TREE_ADDRESSABLE(lab) = 1;
3272 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
3273 build_fold_addr_expr_loc(location.gcc_location(),
3274 lab));
3275 return this->make_expression(ret);
3278 // Declare or define a new function.
3280 Bfunction*
3281 Gcc_backend::function(Btype* fntype, const std::string& name,
3282 const std::string& asm_name, unsigned int flags,
3283 Location location)
3285 tree functype = fntype->get_tree();
3286 if (functype != error_mark_node)
3288 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
3289 functype = TREE_TYPE(functype);
3291 tree id = get_identifier_from_string(name);
3292 if (functype == error_mark_node || id == error_mark_node)
3293 return this->error_function();
3295 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
3296 if (! asm_name.empty())
3297 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
3298 if ((flags & function_is_visible) != 0)
3299 TREE_PUBLIC(decl) = 1;
3300 if ((flags & function_is_declaration) != 0)
3301 DECL_EXTERNAL(decl) = 1;
3302 else
3304 tree restype = TREE_TYPE(functype);
3305 tree resdecl =
3306 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
3307 DECL_ARTIFICIAL(resdecl) = 1;
3308 DECL_IGNORED_P(resdecl) = 1;
3309 DECL_NAMELESS(resdecl) = 1;
3310 DECL_CONTEXT(resdecl) = decl;
3311 DECL_RESULT(decl) = resdecl;
3313 if ((flags & function_is_inlinable) == 0)
3314 DECL_UNINLINABLE(decl) = 1;
3315 if ((flags & function_no_split_stack) != 0)
3317 tree attr = get_identifier ("no_split_stack");
3318 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
3320 if ((flags & function_does_not_return) != 0)
3321 TREE_THIS_VOLATILE(decl) = 1;
3322 if ((flags & function_in_unique_section) != 0)
3323 resolve_unique_section(decl, 0, 1);
3324 if ((flags & function_only_inline) != 0)
3326 TREE_PUBLIC (decl) = 1;
3327 DECL_EXTERNAL(decl) = 1;
3328 DECL_DECLARED_INLINE_P(decl) = 1;
3331 // Optimize thunk functions for size. A thunk created for a defer
3332 // statement that may call recover looks like:
3333 // if runtime.setdeferretaddr(L1) {
3334 // goto L1
3335 // }
3336 // realfn()
3337 // L1:
3338 // The idea is that L1 should be the address to which realfn
3339 // returns. This only works if this little function is not over
3340 // optimized. At some point GCC started duplicating the epilogue in
3341 // the basic-block reordering pass, breaking this assumption.
3342 // Optimizing the function for size avoids duplicating the epilogue.
3343 // This optimization shouldn't matter for any thunk since all thunks
3344 // are small.
3345 size_t pos = name.find("..thunk");
3346 if (pos != std::string::npos)
3348 for (pos += 7; pos < name.length(); ++pos)
3350 if (name[pos] < '0' || name[pos] > '9')
3351 break;
3353 if (pos == name.length())
3355 struct cl_optimization cur_opts;
3356 cl_optimization_save(&cur_opts, &global_options,
3357 &global_options_set);
3358 global_options.x_optimize_size = 1;
3359 global_options.x_optimize_fast = 0;
3360 global_options.x_optimize_debug = 0;
3361 DECL_FUNCTION_SPECIFIC_OPTIMIZATION(decl) =
3362 build_optimization_node(&global_options, &global_options_set);
3363 cl_optimization_restore(&global_options, &global_options_set,
3364 &cur_opts);
3368 go_preserve_from_gc(decl);
3369 return new Bfunction(decl);
3372 // Create a statement that runs all deferred calls for FUNCTION. This should
3373 // be a statement that looks like this in C++:
3374 // finish:
3375 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3377 Bstatement*
3378 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
3379 Bexpression* defer, Location location)
3381 tree undefer_tree = undefer->get_tree();
3382 tree defer_tree = defer->get_tree();
3383 tree fntree = function->get_tree();
3385 if (undefer_tree == error_mark_node
3386 || defer_tree == error_mark_node
3387 || fntree == error_mark_node)
3388 return this->error_statement();
3390 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
3391 push_struct_function(fntree);
3392 else
3393 push_cfun(DECL_STRUCT_FUNCTION(fntree));
3395 tree stmt_list = NULL;
3396 Blabel* blabel = this->label(function, "", location);
3397 Bstatement* label_def = this->label_definition_statement(blabel);
3398 append_to_statement_list(label_def->get_tree(), &stmt_list);
3400 Bstatement* jump_stmt = this->goto_statement(blabel, location);
3401 tree jump = jump_stmt->get_tree();
3402 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
3403 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
3404 tree try_catch =
3405 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
3406 append_to_statement_list(try_catch, &stmt_list);
3407 pop_cfun();
3409 return this->make_statement(stmt_list);
3412 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3413 // This will only be called for a function definition.
3415 bool
3416 Gcc_backend::function_set_parameters(Bfunction* function,
3417 const std::vector<Bvariable*>& param_vars)
3419 tree func_tree = function->get_tree();
3420 if (func_tree == error_mark_node)
3421 return false;
3423 tree params = NULL_TREE;
3424 tree *pp = &params;
3425 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
3426 pv != param_vars.end();
3427 ++pv)
3429 *pp = (*pv)->get_decl();
3430 gcc_assert(*pp != error_mark_node);
3431 pp = &DECL_CHAIN(*pp);
3433 *pp = NULL_TREE;
3434 DECL_ARGUMENTS(func_tree) = params;
3435 return true;
3438 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3440 bool
3441 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
3443 tree func_tree = function->get_tree();
3444 tree code = code_stmt->get_tree();
3446 if (func_tree == error_mark_node || code == error_mark_node)
3447 return false;
3448 DECL_SAVED_TREE(func_tree) = code;
3449 return true;
3452 // Look up a named built-in function in the current backend implementation.
3453 // Returns NULL if no built-in function by that name exists.
3455 Bfunction*
3456 Gcc_backend::lookup_builtin(const std::string& name)
3458 if (this->builtin_functions_.count(name) != 0)
3459 return this->builtin_functions_[name];
3460 return NULL;
3463 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3464 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3465 // emit early debugging information.
3467 void
3468 Gcc_backend::write_global_definitions(
3469 const std::vector<Btype*>& type_decls,
3470 const std::vector<Bexpression*>& constant_decls,
3471 const std::vector<Bfunction*>& function_decls,
3472 const std::vector<Bvariable*>& variable_decls)
3474 size_t count_definitions = type_decls.size() + constant_decls.size()
3475 + function_decls.size() + variable_decls.size();
3477 tree* defs = new tree[count_definitions];
3479 // Convert all non-erroneous declarations into Gimple form.
3480 size_t i = 0;
3481 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
3482 p != variable_decls.end();
3483 ++p)
3485 tree v = (*p)->get_decl();
3486 if (v != error_mark_node)
3488 defs[i] = v;
3489 go_preserve_from_gc(defs[i]);
3490 ++i;
3494 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
3495 p != type_decls.end();
3496 ++p)
3498 tree type_tree = (*p)->get_tree();
3499 if (type_tree != error_mark_node
3500 && IS_TYPE_OR_DECL_P(type_tree))
3502 defs[i] = TYPE_NAME(type_tree);
3503 gcc_assert(defs[i] != NULL);
3504 go_preserve_from_gc(defs[i]);
3505 ++i;
3508 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
3509 p != constant_decls.end();
3510 ++p)
3512 if ((*p)->get_tree() != error_mark_node)
3514 defs[i] = (*p)->get_tree();
3515 go_preserve_from_gc(defs[i]);
3516 ++i;
3519 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
3520 p != function_decls.end();
3521 ++p)
3523 tree decl = (*p)->get_tree();
3524 if (decl != error_mark_node)
3526 go_preserve_from_gc(decl);
3527 if (DECL_STRUCT_FUNCTION(decl) == NULL)
3528 allocate_struct_function(decl, false);
3529 cgraph_node::finalize_function(decl, true);
3531 defs[i] = decl;
3532 ++i;
3536 // Pass everything back to the middle-end.
3538 wrapup_global_declarations(defs, i);
3540 delete[] defs;
3543 void
3544 Gcc_backend::write_export_data(const char* bytes, unsigned int size)
3546 go_write_export_data(bytes, size);
3550 // Define a builtin function. BCODE is the builtin function code
3551 // defined by builtins.def. NAME is the name of the builtin function.
3552 // LIBNAME is the name of the corresponding library function, and is
3553 // NULL if there isn't one. FNTYPE is the type of the function.
3554 // CONST_P is true if the function has the const attribute.
3555 // NORETURN_P is true if the function has the noreturn attribute.
3557 void
3558 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3559 const char* libname, tree fntype, int flags)
3561 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3562 libname, NULL_TREE);
3563 if ((flags & builtin_const) != 0)
3564 TREE_READONLY(decl) = 1;
3565 if ((flags & builtin_noreturn) != 0)
3566 TREE_THIS_VOLATILE(decl) = 1;
3567 if ((flags & builtin_novops) != 0)
3568 DECL_IS_NOVOPS(decl) = 1;
3569 set_builtin_decl(bcode, decl, true);
3570 this->builtin_functions_[name] = this->make_function(decl);
3571 if (libname != NULL)
3573 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3574 NULL, NULL_TREE);
3575 if ((flags & builtin_const) != 0)
3576 TREE_READONLY(decl) = 1;
3577 if ((flags & builtin_noreturn) != 0)
3578 TREE_THIS_VOLATILE(decl) = 1;
3579 if ((flags & builtin_novops) != 0)
3580 DECL_IS_NOVOPS(decl) = 1;
3581 this->builtin_functions_[libname] = this->make_function(decl);
3585 // Return the backend generator.
3587 Backend*
3588 go_get_backend()
3590 return new Gcc_backend();