* tree.c (find_decls_types_r): Remove all non-VAR_DECLs from
[official-gcc.git] / gcc / go / go-gcc.cc
blob870e865fe8fbaf08caae1e845b7d0f4301200053
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2018 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
5 // This file is part of GCC.
7 // GCC is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3, or (at your option) any later
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 "fold-const.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
31 #include "varasm.h"
32 #include "tree-iterator.h"
33 #include "tm.h"
34 #include "function.h"
35 #include "cgraph.h"
36 #include "convert.h"
37 #include "gimple-expr.h"
38 #include "gimplify.h"
39 #include "langhooks.h"
40 #include "toplev.h"
41 #include "output.h"
42 #include "realmpfr.h"
43 #include "builtins.h"
45 #include "go-c.h"
46 #include "go-gcc.h"
48 #include "gogo.h"
49 #include "backend.h"
51 // A class wrapping a tree.
53 class Gcc_tree
55 public:
56 Gcc_tree(tree t)
57 : t_(t)
58 { }
60 tree
61 get_tree() const
62 { return this->t_; }
64 void
65 set_tree(tree t)
66 { this->t_ = t; }
68 private:
69 tree t_;
72 // In gcc, types, expressions, and statements are all trees.
73 class Btype : public Gcc_tree
75 public:
76 Btype(tree t)
77 : Gcc_tree(t)
78 { }
81 class Bexpression : public Gcc_tree
83 public:
84 Bexpression(tree t)
85 : Gcc_tree(t)
86 { }
89 class Bstatement : public Gcc_tree
91 public:
92 Bstatement(tree t)
93 : Gcc_tree(t)
94 { }
97 class Bfunction : public Gcc_tree
99 public:
100 Bfunction(tree t)
101 : Gcc_tree(t)
105 class Bblock : public Gcc_tree
107 public:
108 Bblock(tree t)
109 : Gcc_tree(t)
113 class Blabel : public Gcc_tree
115 public:
116 Blabel(tree t)
117 : Gcc_tree(t)
121 // Bvariable is a bit more complicated, because of zero-sized types.
122 // The GNU linker does not permit dynamic variables with zero size.
123 // When we see such a variable, we generate a version of the type with
124 // non-zero size. However, when referring to the global variable, we
125 // want an expression of zero size; otherwise, if, say, the global
126 // variable is passed to a function, we will be passing a
127 // non-zero-sized value to a zero-sized value, which can lead to a
128 // miscompilation.
130 class Bvariable
132 public:
133 Bvariable(tree t)
134 : t_(t), orig_type_(NULL)
137 Bvariable(tree t, tree orig_type)
138 : t_(t), orig_type_(orig_type)
141 // Get the tree for use as an expression.
142 tree
143 get_tree(Location) const;
145 // Get the actual decl;
146 tree
147 get_decl() const
148 { return this->t_; }
150 private:
151 tree t_;
152 tree orig_type_;
155 // Get the tree of a variable for use as an expression. If this is a
156 // zero-sized global, create an expression that refers to the decl but
157 // has zero size.
158 tree
159 Bvariable::get_tree(Location location) const
161 if (this->orig_type_ == NULL
162 || this->t_ == error_mark_node
163 || TREE_TYPE(this->t_) == this->orig_type_)
164 return this->t_;
165 // Return *(orig_type*)&decl. */
166 tree t = build_fold_addr_expr_loc(location.gcc_location(), this->t_);
167 t = fold_build1_loc(location.gcc_location(), NOP_EXPR,
168 build_pointer_type(this->orig_type_), t);
169 return build_fold_indirect_ref_loc(location.gcc_location(), t);
172 // This file implements the interface between the Go frontend proper
173 // and the gcc IR. This implements specific instantiations of
174 // abstract classes defined by the Go frontend proper. The Go
175 // frontend proper class methods of these classes to generate the
176 // backend representation.
178 class Gcc_backend : public Backend
180 public:
181 Gcc_backend();
183 // Types.
185 Btype*
186 error_type()
187 { return this->make_type(error_mark_node); }
189 Btype*
190 void_type()
191 { return this->make_type(void_type_node); }
193 Btype*
194 bool_type()
195 { return this->make_type(boolean_type_node); }
197 Btype*
198 integer_type(bool, int);
200 Btype*
201 float_type(int);
203 Btype*
204 complex_type(int);
206 Btype*
207 pointer_type(Btype*);
209 Btype*
210 function_type(const Btyped_identifier&,
211 const std::vector<Btyped_identifier>&,
212 const std::vector<Btyped_identifier>&,
213 Btype*,
214 const Location);
216 Btype*
217 struct_type(const std::vector<Btyped_identifier>&);
219 Btype*
220 array_type(Btype*, Bexpression*);
222 Btype*
223 placeholder_pointer_type(const std::string&, Location, bool);
225 bool
226 set_placeholder_pointer_type(Btype*, Btype*);
228 bool
229 set_placeholder_function_type(Btype*, Btype*);
231 Btype*
232 placeholder_struct_type(const std::string&, Location);
234 bool
235 set_placeholder_struct_type(Btype* placeholder,
236 const std::vector<Btyped_identifier>&);
238 Btype*
239 placeholder_array_type(const std::string&, Location);
241 bool
242 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
244 Btype*
245 named_type(const std::string&, Btype*, Location);
247 Btype*
248 circular_pointer_type(Btype*, bool);
250 bool
251 is_circular_pointer_type(Btype*);
253 int64_t
254 type_size(Btype*);
256 int64_t
257 type_alignment(Btype*);
259 int64_t
260 type_field_alignment(Btype*);
262 int64_t
263 type_field_offset(Btype*, size_t index);
265 // Expressions.
267 Bexpression*
268 zero_expression(Btype*);
270 Bexpression*
271 error_expression()
272 { return this->make_expression(error_mark_node); }
274 Bexpression*
275 nil_pointer_expression()
276 { return this->make_expression(null_pointer_node); }
278 Bexpression*
279 var_expression(Bvariable* var, Location);
281 Bexpression*
282 indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location);
284 Bexpression*
285 named_constant_expression(Btype* btype, const std::string& name,
286 Bexpression* val, Location);
288 Bexpression*
289 integer_constant_expression(Btype* btype, mpz_t val);
291 Bexpression*
292 float_constant_expression(Btype* btype, mpfr_t val);
294 Bexpression*
295 complex_constant_expression(Btype* btype, mpc_t val);
297 Bexpression*
298 string_constant_expression(const std::string& val);
300 Bexpression*
301 boolean_constant_expression(bool val);
303 Bexpression*
304 real_part_expression(Bexpression* bcomplex, Location);
306 Bexpression*
307 imag_part_expression(Bexpression* bcomplex, Location);
309 Bexpression*
310 complex_expression(Bexpression* breal, Bexpression* bimag, Location);
312 Bexpression*
313 convert_expression(Btype* type, Bexpression* expr, Location);
315 Bexpression*
316 function_code_expression(Bfunction*, Location);
318 Bexpression*
319 address_expression(Bexpression*, Location);
321 Bexpression*
322 struct_field_expression(Bexpression*, size_t, Location);
324 Bexpression*
325 compound_expression(Bstatement*, Bexpression*, Location);
327 Bexpression*
328 conditional_expression(Bfunction*, Btype*, Bexpression*, Bexpression*,
329 Bexpression*, Location);
331 Bexpression*
332 unary_expression(Operator, Bexpression*, Location);
334 Bexpression*
335 binary_expression(Operator, Bexpression*, Bexpression*, Location);
337 Bexpression*
338 constructor_expression(Btype*, const std::vector<Bexpression*>&, Location);
340 Bexpression*
341 array_constructor_expression(Btype*, const std::vector<unsigned long>&,
342 const std::vector<Bexpression*>&, Location);
344 Bexpression*
345 pointer_offset_expression(Bexpression* base, Bexpression* offset, Location);
347 Bexpression*
348 array_index_expression(Bexpression* array, Bexpression* index, Location);
350 Bexpression*
351 call_expression(Bfunction* caller, Bexpression* fn,
352 const std::vector<Bexpression*>& args,
353 Bexpression* static_chain, Location);
355 // Statements.
357 Bstatement*
358 error_statement()
359 { return this->make_statement(error_mark_node); }
361 Bstatement*
362 expression_statement(Bfunction*, Bexpression*);
364 Bstatement*
365 init_statement(Bfunction*, Bvariable* var, Bexpression* init);
367 Bstatement*
368 assignment_statement(Bfunction*, Bexpression* lhs, Bexpression* rhs,
369 Location);
371 Bstatement*
372 return_statement(Bfunction*, const std::vector<Bexpression*>&,
373 Location);
375 Bstatement*
376 if_statement(Bfunction*, Bexpression* condition, Bblock* then_block,
377 Bblock* else_block, Location);
379 Bstatement*
380 switch_statement(Bfunction* function, Bexpression* value,
381 const std::vector<std::vector<Bexpression*> >& cases,
382 const std::vector<Bstatement*>& statements,
383 Location);
385 Bstatement*
386 compound_statement(Bstatement*, Bstatement*);
388 Bstatement*
389 statement_list(const std::vector<Bstatement*>&);
391 Bstatement*
392 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
393 Bstatement* finally_stmt, Location);
395 // Blocks.
397 Bblock*
398 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
399 Location, Location);
401 void
402 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
404 Bstatement*
405 block_statement(Bblock*);
407 // Variables.
409 Bvariable*
410 error_variable()
411 { return new Bvariable(error_mark_node); }
413 Bvariable*
414 global_variable(const std::string& var_name,
415 const std::string& asm_name,
416 Btype* btype,
417 bool is_external,
418 bool is_hidden,
419 bool in_unique_section,
420 Location location);
422 void
423 global_variable_set_init(Bvariable*, Bexpression*);
425 Bvariable*
426 local_variable(Bfunction*, const std::string&, Btype*, Bvariable*, bool,
427 Location);
429 Bvariable*
430 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
431 Location);
433 Bvariable*
434 static_chain_variable(Bfunction*, const std::string&, Btype*, Location);
436 Bvariable*
437 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
438 Location, Bstatement**);
440 Bvariable*
441 implicit_variable(const std::string&, const std::string&, Btype*,
442 bool, bool, bool, int64_t);
444 void
445 implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
446 bool, bool, bool, 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 bool, bool, Btype*, Location);
455 void
456 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
457 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 bool is_visible, bool is_declaration, bool is_inlinable,
486 bool disable_split_stack, bool does_not_return,
487 bool in_unique_section, Location);
489 Bstatement*
490 function_defer_statement(Bfunction* function, Bexpression* undefer,
491 Bexpression* defer, Location);
493 bool
494 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
496 bool
497 function_set_body(Bfunction* function, Bstatement* code_stmt);
499 Bfunction*
500 lookup_builtin(const std::string&);
502 void
503 write_global_definitions(const std::vector<Btype*>&,
504 const std::vector<Bexpression*>&,
505 const std::vector<Bfunction*>&,
506 const std::vector<Bvariable*>&);
508 void
509 write_export_data(const char* bytes, unsigned int size);
512 private:
513 // Make a Bexpression from a tree.
514 Bexpression*
515 make_expression(tree t)
516 { return new Bexpression(t); }
518 // Make a Bstatement from a tree.
519 Bstatement*
520 make_statement(tree t)
521 { return new Bstatement(t); }
523 // Make a Btype from a tree.
524 Btype*
525 make_type(tree t)
526 { return new Btype(t); }
528 Bfunction*
529 make_function(tree t)
530 { return new Bfunction(t); }
532 Btype*
533 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
535 Btype*
536 fill_in_array(Btype*, Btype*, Bexpression*);
538 tree
539 non_zero_size_type(tree);
541 tree
542 convert_tree(tree, tree, Location);
544 private:
545 void
546 define_builtin(built_in_function bcode, const char* name, const char* libname,
547 tree fntype, bool const_p, bool noreturn_p);
549 // A mapping of the GCC built-ins exposed to GCCGo.
550 std::map<std::string, Bfunction*> builtin_functions_;
553 // A helper function to create a GCC identifier from a C++ string.
555 static inline tree
556 get_identifier_from_string(const std::string& str)
558 return get_identifier_with_length(str.data(), str.length());
561 // Define the built-in functions that are exposed to GCCGo.
563 Gcc_backend::Gcc_backend()
565 /* We need to define the fetch_and_add functions, since we use them
566 for ++ and --. */
567 tree t = this->integer_type(true, BITS_PER_UNIT)->get_tree();
568 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
569 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
570 NULL, build_function_type_list(t, p, t, NULL_TREE),
571 false, false);
573 t = this->integer_type(true, BITS_PER_UNIT * 2)->get_tree();
574 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
575 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
576 NULL, build_function_type_list(t, p, t, NULL_TREE),
577 false, false);
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),
583 false, false);
585 t = this->integer_type(true, BITS_PER_UNIT * 8)->get_tree();
586 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
587 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
588 NULL, build_function_type_list(t, p, t, NULL_TREE),
589 false, false);
591 // We use __builtin_expect for magic import functions.
592 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
593 build_function_type_list(long_integer_type_node,
594 long_integer_type_node,
595 long_integer_type_node,
596 NULL_TREE),
597 true, false);
599 // We use __builtin_memcmp for struct comparisons.
600 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
601 build_function_type_list(integer_type_node,
602 const_ptr_type_node,
603 const_ptr_type_node,
604 size_type_node,
605 NULL_TREE),
606 false, false);
608 // Used by runtime/internal/sys.
609 this->define_builtin(BUILT_IN_CTZ, "__builtin_ctz", "ctz",
610 build_function_type_list(integer_type_node,
611 unsigned_type_node,
612 NULL_TREE),
613 true, false);
614 this->define_builtin(BUILT_IN_CTZLL, "__builtin_ctzll", "ctzll",
615 build_function_type_list(integer_type_node,
616 long_long_unsigned_type_node,
617 NULL_TREE),
618 true, false);
619 this->define_builtin(BUILT_IN_BSWAP32, "__builtin_bswap32", "bswap32",
620 build_function_type_list(uint32_type_node,
621 uint32_type_node,
622 NULL_TREE),
623 true, false);
624 this->define_builtin(BUILT_IN_BSWAP64, "__builtin_bswap64", "bswap64",
625 build_function_type_list(uint64_type_node,
626 uint64_type_node,
627 NULL_TREE),
628 true, false);
630 // We provide some functions for the math library.
631 tree math_function_type = build_function_type_list(double_type_node,
632 double_type_node,
633 NULL_TREE);
634 tree math_function_type_long =
635 build_function_type_list(long_double_type_node, long_double_type_node,
636 NULL_TREE);
637 tree math_function_type_two = build_function_type_list(double_type_node,
638 double_type_node,
639 double_type_node,
640 NULL_TREE);
641 tree math_function_type_long_two =
642 build_function_type_list(long_double_type_node, long_double_type_node,
643 long_double_type_node, NULL_TREE);
644 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
645 math_function_type, true, false);
646 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
647 math_function_type_long, true, false);
648 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
649 math_function_type, true, false);
650 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
651 math_function_type_long, true, false);
652 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
653 math_function_type, true, false);
654 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
655 math_function_type_long, true, false);
656 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
657 math_function_type_two, true, false);
658 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
659 math_function_type_long_two, true, false);
660 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
661 math_function_type, true, false);
662 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
663 math_function_type_long, true, false);
664 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
665 math_function_type, true, false);
666 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
667 math_function_type_long, true, false);
668 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
669 math_function_type, true, false);
670 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
671 math_function_type_long, true, false);
672 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
673 math_function_type, true, false);
674 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
675 math_function_type_long, true, false);
676 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
677 math_function_type, true, false);
678 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
679 math_function_type_long, true, false);
680 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
681 math_function_type, true, false);
682 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
683 math_function_type_long, true, false);
684 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
685 math_function_type_two, true, false);
686 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
687 math_function_type_long_two, true, false);
688 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
689 build_function_type_list(double_type_node,
690 double_type_node,
691 integer_type_node,
692 NULL_TREE),
693 true, false);
694 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
695 build_function_type_list(long_double_type_node,
696 long_double_type_node,
697 integer_type_node,
698 NULL_TREE),
699 true, false);
700 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
701 math_function_type, true, false);
702 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
703 math_function_type_long, true, false);
704 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
705 math_function_type, true, false);
706 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
707 math_function_type_long, true, false);
708 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
709 math_function_type, true, false);
710 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
711 math_function_type_long, true, false);
712 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
713 math_function_type, true, false);
714 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
715 math_function_type_long, true, false);
716 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
717 math_function_type, true, false);
718 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
719 math_function_type_long, true, false);
720 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
721 math_function_type, true, false);
722 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
723 math_function_type_long, true, false);
724 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
725 math_function_type, true, false);
726 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
727 math_function_type_long, true, false);
728 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
729 math_function_type, true, false);
730 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
731 math_function_type_long, true, false);
733 // We use __builtin_return_address in the thunk we build for
734 // functions which call recover, and for runtime.getcallerpc.
735 t = build_function_type_list(ptr_type_node, unsigned_type_node, NULL_TREE);
736 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
737 NULL, t, false, false);
739 // The runtime calls __builtin_frame_address for runtime.getcallersp.
740 this->define_builtin(BUILT_IN_FRAME_ADDRESS, "__builtin_frame_address",
741 NULL, t, false, false);
743 // The runtime calls __builtin_extract_return_addr when recording
744 // the address to which a function returns.
745 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR,
746 "__builtin_extract_return_addr", NULL,
747 build_function_type_list(ptr_type_node,
748 ptr_type_node,
749 NULL_TREE),
750 false, false);
752 // The compiler uses __builtin_trap for some exception handling
753 // cases.
754 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
755 build_function_type(void_type_node, void_list_node),
756 false, true);
758 // The runtime uses __builtin_prefetch.
759 this->define_builtin(BUILT_IN_PREFETCH, "__builtin_prefetch", NULL,
760 build_varargs_function_type_list(void_type_node,
761 const_ptr_type_node,
762 NULL_TREE),
763 false, false);
765 // The compiler uses __builtin_unreachable for cases that can not
766 // occur.
767 this->define_builtin(BUILT_IN_UNREACHABLE, "__builtin_unreachable", NULL,
768 build_function_type(void_type_node, void_list_node),
769 true, true);
772 // Get an unnamed integer type.
774 Btype*
775 Gcc_backend::integer_type(bool is_unsigned, int bits)
777 tree type;
778 if (is_unsigned)
780 if (bits == INT_TYPE_SIZE)
781 type = unsigned_type_node;
782 else if (bits == CHAR_TYPE_SIZE)
783 type = unsigned_char_type_node;
784 else if (bits == SHORT_TYPE_SIZE)
785 type = short_unsigned_type_node;
786 else if (bits == LONG_TYPE_SIZE)
787 type = long_unsigned_type_node;
788 else if (bits == LONG_LONG_TYPE_SIZE)
789 type = long_long_unsigned_type_node;
790 else
791 type = make_unsigned_type(bits);
793 else
795 if (bits == INT_TYPE_SIZE)
796 type = integer_type_node;
797 else if (bits == CHAR_TYPE_SIZE)
798 type = signed_char_type_node;
799 else if (bits == SHORT_TYPE_SIZE)
800 type = short_integer_type_node;
801 else if (bits == LONG_TYPE_SIZE)
802 type = long_integer_type_node;
803 else if (bits == LONG_LONG_TYPE_SIZE)
804 type = long_long_integer_type_node;
805 else
806 type = make_signed_type(bits);
808 return this->make_type(type);
811 // Get an unnamed float type.
813 Btype*
814 Gcc_backend::float_type(int bits)
816 tree type;
817 if (bits == FLOAT_TYPE_SIZE)
818 type = float_type_node;
819 else if (bits == DOUBLE_TYPE_SIZE)
820 type = double_type_node;
821 else if (bits == LONG_DOUBLE_TYPE_SIZE)
822 type = long_double_type_node;
823 else
825 type = make_node(REAL_TYPE);
826 TYPE_PRECISION(type) = bits;
827 layout_type(type);
829 return this->make_type(type);
832 // Get an unnamed complex type.
834 Btype*
835 Gcc_backend::complex_type(int bits)
837 tree type;
838 if (bits == FLOAT_TYPE_SIZE * 2)
839 type = complex_float_type_node;
840 else if (bits == DOUBLE_TYPE_SIZE * 2)
841 type = complex_double_type_node;
842 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
843 type = complex_long_double_type_node;
844 else
846 type = make_node(REAL_TYPE);
847 TYPE_PRECISION(type) = bits / 2;
848 layout_type(type);
849 type = build_complex_type(type);
851 return this->make_type(type);
854 // Get a pointer type.
856 Btype*
857 Gcc_backend::pointer_type(Btype* to_type)
859 tree to_type_tree = to_type->get_tree();
860 if (to_type_tree == error_mark_node)
861 return this->error_type();
862 tree type = build_pointer_type(to_type_tree);
863 return this->make_type(type);
866 // Make a function type.
868 Btype*
869 Gcc_backend::function_type(const Btyped_identifier& receiver,
870 const std::vector<Btyped_identifier>& parameters,
871 const std::vector<Btyped_identifier>& results,
872 Btype* result_struct,
873 Location)
875 tree args = NULL_TREE;
876 tree* pp = &args;
877 if (receiver.btype != NULL)
879 tree t = receiver.btype->get_tree();
880 if (t == error_mark_node)
881 return this->error_type();
882 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
883 pp = &TREE_CHAIN(*pp);
886 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
887 p != parameters.end();
888 ++p)
890 tree t = p->btype->get_tree();
891 if (t == error_mark_node)
892 return this->error_type();
893 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
894 pp = &TREE_CHAIN(*pp);
897 // Varargs is handled entirely at the Go level. When converted to
898 // GENERIC functions are not varargs.
899 *pp = void_list_node;
901 tree result;
902 if (results.empty())
903 result = void_type_node;
904 else if (results.size() == 1)
905 result = results.front().btype->get_tree();
906 else
908 gcc_assert(result_struct != NULL);
909 result = result_struct->get_tree();
911 if (result == error_mark_node)
912 return this->error_type();
914 // The libffi library can not represent a zero-sized object. To
915 // avoid causing confusion on 32-bit SPARC, we treat a function that
916 // returns a zero-sized value as returning void. That should do no
917 // harm since there is no actual value to be returned. See
918 // https://gcc.gnu.org/PR72814 for details.
919 if (result != void_type_node && int_size_in_bytes(result) == 0)
920 result = void_type_node;
922 tree fntype = build_function_type(result, args);
923 if (fntype == error_mark_node)
924 return this->error_type();
926 return this->make_type(build_pointer_type(fntype));
929 // Make a struct type.
931 Btype*
932 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
934 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
937 // Fill in the fields of a struct type.
939 Btype*
940 Gcc_backend::fill_in_struct(Btype* fill,
941 const std::vector<Btyped_identifier>& fields)
943 tree fill_tree = fill->get_tree();
944 tree field_trees = NULL_TREE;
945 tree* pp = &field_trees;
946 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
947 p != fields.end();
948 ++p)
950 tree name_tree = get_identifier_from_string(p->name);
951 tree type_tree = p->btype->get_tree();
952 if (type_tree == error_mark_node)
953 return this->error_type();
954 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
955 type_tree);
956 DECL_CONTEXT(field) = fill_tree;
957 *pp = field;
958 pp = &DECL_CHAIN(field);
960 TYPE_FIELDS(fill_tree) = field_trees;
961 layout_type(fill_tree);
963 // Because Go permits converting between named struct types and
964 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
965 // because we don't try to maintain TYPE_CANONICAL for struct types,
966 // we need to tell the middle-end to use structural equality.
967 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
969 return fill;
972 // Make an array type.
974 Btype*
975 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
977 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
978 element_btype, length);
981 // Fill in an array type.
983 Btype*
984 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
985 Bexpression* length)
987 tree element_type_tree = element_type->get_tree();
988 tree length_tree = length->get_tree();
989 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
990 return this->error_type();
992 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
994 length_tree = fold_convert(sizetype, length_tree);
996 // build_index_type takes the maximum index, which is one less than
997 // the length.
998 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
999 length_tree,
1000 size_one_node));
1002 tree fill_tree = fill->get_tree();
1003 TREE_TYPE(fill_tree) = element_type_tree;
1004 TYPE_DOMAIN(fill_tree) = index_type_tree;
1005 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
1006 layout_type(fill_tree);
1008 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
1009 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
1010 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
1011 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
1012 TYPE_CANONICAL(fill_tree) =
1013 build_array_type(TYPE_CANONICAL(element_type_tree),
1014 TYPE_CANONICAL(index_type_tree));
1016 return fill;
1019 // Create a placeholder for a pointer type.
1021 Btype*
1022 Gcc_backend::placeholder_pointer_type(const std::string& name,
1023 Location location, bool)
1025 tree ret = build_distinct_type_copy(ptr_type_node);
1026 if (!name.empty())
1028 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1029 get_identifier_from_string(name),
1030 ret);
1031 TYPE_NAME(ret) = decl;
1033 return this->make_type(ret);
1036 // Set the real target type for a placeholder pointer type.
1038 bool
1039 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
1040 Btype* to_type)
1042 tree pt = placeholder->get_tree();
1043 if (pt == error_mark_node)
1044 return false;
1045 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
1046 tree tt = to_type->get_tree();
1047 if (tt == error_mark_node)
1049 placeholder->set_tree(error_mark_node);
1050 return false;
1052 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
1053 TREE_TYPE(pt) = TREE_TYPE(tt);
1054 if (TYPE_NAME(pt) != NULL_TREE)
1056 // Build the data structure gcc wants to see for a typedef.
1057 tree copy = build_variant_type_copy(pt);
1058 TYPE_NAME(copy) = NULL_TREE;
1059 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
1061 return true;
1064 // Set the real values for a placeholder function type.
1066 bool
1067 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
1069 return this->set_placeholder_pointer_type(placeholder, ft);
1072 // Create a placeholder for a struct type.
1074 Btype*
1075 Gcc_backend::placeholder_struct_type(const std::string& name,
1076 Location location)
1078 tree ret = make_node(RECORD_TYPE);
1079 if (!name.empty())
1081 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1082 get_identifier_from_string(name),
1083 ret);
1084 TYPE_NAME(ret) = decl;
1086 return this->make_type(ret);
1089 // Fill in the fields of a placeholder struct type.
1091 bool
1092 Gcc_backend::set_placeholder_struct_type(
1093 Btype* placeholder,
1094 const std::vector<Btyped_identifier>& fields)
1096 tree t = placeholder->get_tree();
1097 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
1098 Btype* r = this->fill_in_struct(placeholder, fields);
1100 if (TYPE_NAME(t) != NULL_TREE)
1102 // Build the data structure gcc wants to see for a typedef.
1103 tree copy = build_distinct_type_copy(t);
1104 TYPE_NAME(copy) = NULL_TREE;
1105 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1108 return r->get_tree() != error_mark_node;
1111 // Create a placeholder for an array type.
1113 Btype*
1114 Gcc_backend::placeholder_array_type(const std::string& name,
1115 Location location)
1117 tree ret = make_node(ARRAY_TYPE);
1118 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1119 get_identifier_from_string(name),
1120 ret);
1121 TYPE_NAME(ret) = decl;
1122 return this->make_type(ret);
1125 // Fill in the fields of a placeholder array type.
1127 bool
1128 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1129 Btype* element_btype,
1130 Bexpression* length)
1132 tree t = placeholder->get_tree();
1133 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1134 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1136 // Build the data structure gcc wants to see for a typedef.
1137 tree copy = build_distinct_type_copy(t);
1138 TYPE_NAME(copy) = NULL_TREE;
1139 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1141 return r->get_tree() != error_mark_node;
1144 // Return a named version of a type.
1146 Btype*
1147 Gcc_backend::named_type(const std::string& name, Btype* btype,
1148 Location location)
1150 tree type = btype->get_tree();
1151 if (type == error_mark_node)
1152 return this->error_type();
1154 // The middle-end expects a basic type to have a name. In Go every
1155 // basic type will have a name. The first time we see a basic type,
1156 // give it whatever Go name we have at this point.
1157 if (TYPE_NAME(type) == NULL_TREE
1158 && location.gcc_location() == BUILTINS_LOCATION
1159 && (TREE_CODE(type) == INTEGER_TYPE
1160 || TREE_CODE(type) == REAL_TYPE
1161 || TREE_CODE(type) == COMPLEX_TYPE
1162 || TREE_CODE(type) == BOOLEAN_TYPE))
1164 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1165 get_identifier_from_string(name),
1166 type);
1167 TYPE_NAME(type) = decl;
1168 return this->make_type(type);
1171 tree copy = build_variant_type_copy(type);
1172 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1173 get_identifier_from_string(name),
1174 copy);
1175 DECL_ORIGINAL_TYPE(decl) = type;
1176 TYPE_NAME(copy) = decl;
1177 return this->make_type(copy);
1180 // Return a pointer type used as a marker for a circular type.
1182 Btype*
1183 Gcc_backend::circular_pointer_type(Btype*, bool)
1185 return this->make_type(ptr_type_node);
1188 // Return whether we might be looking at a circular type.
1190 bool
1191 Gcc_backend::is_circular_pointer_type(Btype* btype)
1193 return btype->get_tree() == ptr_type_node;
1196 // Return the size of a type.
1198 int64_t
1199 Gcc_backend::type_size(Btype* btype)
1201 tree t = btype->get_tree();
1202 if (t == error_mark_node)
1203 return 1;
1204 if (t == void_type_node)
1205 return 0;
1206 t = TYPE_SIZE_UNIT(t);
1207 gcc_assert(tree_fits_uhwi_p (t));
1208 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1209 int64_t ret = static_cast<int64_t>(val_wide);
1210 if (ret < 0 || static_cast<unsigned HOST_WIDE_INT>(ret) != val_wide)
1211 return -1;
1212 return ret;
1215 // Return the alignment of a type.
1217 int64_t
1218 Gcc_backend::type_alignment(Btype* btype)
1220 tree t = btype->get_tree();
1221 if (t == error_mark_node)
1222 return 1;
1223 return TYPE_ALIGN_UNIT(t);
1226 // Return the alignment of a struct field of type BTYPE.
1228 int64_t
1229 Gcc_backend::type_field_alignment(Btype* btype)
1231 tree t = btype->get_tree();
1232 if (t == error_mark_node)
1233 return 1;
1234 return go_field_alignment(t);
1237 // Return the offset of a field in a struct.
1239 int64_t
1240 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1242 tree struct_tree = btype->get_tree();
1243 if (struct_tree == error_mark_node)
1244 return 0;
1245 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1246 tree field = TYPE_FIELDS(struct_tree);
1247 for (; index > 0; --index)
1249 field = DECL_CHAIN(field);
1250 gcc_assert(field != NULL_TREE);
1252 HOST_WIDE_INT offset_wide = int_byte_position(field);
1253 int64_t ret = static_cast<int64_t>(offset_wide);
1254 gcc_assert(ret == offset_wide);
1255 return ret;
1258 // Return the zero value for a type.
1260 Bexpression*
1261 Gcc_backend::zero_expression(Btype* btype)
1263 tree t = btype->get_tree();
1264 tree ret;
1265 if (t == error_mark_node)
1266 ret = error_mark_node;
1267 else
1268 ret = build_zero_cst(t);
1269 return this->make_expression(ret);
1272 // An expression that references a variable.
1274 Bexpression*
1275 Gcc_backend::var_expression(Bvariable* var, Location location)
1277 tree ret = var->get_tree(location);
1278 if (ret == error_mark_node)
1279 return this->error_expression();
1280 return this->make_expression(ret);
1283 // An expression that indirectly references an expression.
1285 Bexpression*
1286 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1287 bool known_valid, Location location)
1289 tree expr_tree = expr->get_tree();
1290 tree type_tree = btype->get_tree();
1291 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1292 return this->error_expression();
1294 // If the type of EXPR is a recursive pointer type, then we
1295 // need to insert a cast before indirecting.
1296 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1297 if (VOID_TYPE_P(target_type_tree))
1298 expr_tree = fold_convert_loc(location.gcc_location(),
1299 build_pointer_type(type_tree), expr_tree);
1301 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1302 expr_tree);
1303 if (known_valid)
1304 TREE_THIS_NOTRAP(ret) = 1;
1305 return this->make_expression(ret);
1308 // Return an expression that declares a constant named NAME with the
1309 // constant value VAL in BTYPE.
1311 Bexpression*
1312 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1313 Bexpression* val, Location location)
1315 tree type_tree = btype->get_tree();
1316 tree const_val = val->get_tree();
1317 if (type_tree == error_mark_node || const_val == error_mark_node)
1318 return this->error_expression();
1320 tree name_tree = get_identifier_from_string(name);
1321 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1322 type_tree);
1323 DECL_INITIAL(decl) = const_val;
1324 TREE_CONSTANT(decl) = 1;
1325 TREE_READONLY(decl) = 1;
1327 go_preserve_from_gc(decl);
1328 return this->make_expression(decl);
1331 // Return a typed value as a constant integer.
1333 Bexpression*
1334 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1336 tree t = btype->get_tree();
1337 if (t == error_mark_node)
1338 return this->error_expression();
1340 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1341 return this->make_expression(ret);
1344 // Return a typed value as a constant floating-point number.
1346 Bexpression*
1347 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1349 tree t = btype->get_tree();
1350 tree ret;
1351 if (t == error_mark_node)
1352 return this->error_expression();
1354 REAL_VALUE_TYPE r1;
1355 real_from_mpfr(&r1, val, t, GMP_RNDN);
1356 REAL_VALUE_TYPE r2;
1357 real_convert(&r2, TYPE_MODE(t), &r1);
1358 ret = build_real(t, r2);
1359 return this->make_expression(ret);
1362 // Return a typed real and imaginary value as a constant complex number.
1364 Bexpression*
1365 Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
1367 tree t = btype->get_tree();
1368 tree ret;
1369 if (t == error_mark_node)
1370 return this->error_expression();
1372 REAL_VALUE_TYPE r1;
1373 real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
1374 REAL_VALUE_TYPE r2;
1375 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1377 REAL_VALUE_TYPE r3;
1378 real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
1379 REAL_VALUE_TYPE r4;
1380 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1382 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1383 build_real(TREE_TYPE(t), r4));
1384 return this->make_expression(ret);
1387 // Make a constant string expression.
1389 Bexpression*
1390 Gcc_backend::string_constant_expression(const std::string& val)
1392 tree index_type = build_index_type(size_int(val.length()));
1393 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1394 TYPE_QUAL_CONST);
1395 tree string_type = build_array_type(const_char_type, index_type);
1396 TYPE_STRING_FLAG(string_type) = 1;
1397 tree string_val = build_string(val.length(), val.data());
1398 TREE_TYPE(string_val) = string_type;
1400 return this->make_expression(string_val);
1403 // Make a constant boolean expression.
1405 Bexpression*
1406 Gcc_backend::boolean_constant_expression(bool val)
1408 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1409 return this->make_expression(bool_cst);
1412 // Return the real part of a complex expression.
1414 Bexpression*
1415 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1417 tree complex_tree = bcomplex->get_tree();
1418 if (complex_tree == error_mark_node)
1419 return this->error_expression();
1420 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1421 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1422 TREE_TYPE(TREE_TYPE(complex_tree)),
1423 complex_tree);
1424 return this->make_expression(ret);
1427 // Return the imaginary part of a complex expression.
1429 Bexpression*
1430 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1432 tree complex_tree = bcomplex->get_tree();
1433 if (complex_tree == error_mark_node)
1434 return this->error_expression();
1435 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1436 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1437 TREE_TYPE(TREE_TYPE(complex_tree)),
1438 complex_tree);
1439 return this->make_expression(ret);
1442 // Make a complex expression given its real and imaginary parts.
1444 Bexpression*
1445 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1446 Location location)
1448 tree real_tree = breal->get_tree();
1449 tree imag_tree = bimag->get_tree();
1450 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1451 return this->error_expression();
1452 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1453 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1454 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1455 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1456 build_complex_type(TREE_TYPE(real_tree)),
1457 real_tree, imag_tree);
1458 return this->make_expression(ret);
1461 // An expression that converts an expression to a different type.
1463 Bexpression*
1464 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1465 Location location)
1467 tree type_tree = type->get_tree();
1468 tree expr_tree = expr->get_tree();
1469 if (type_tree == error_mark_node
1470 || expr_tree == error_mark_node
1471 || TREE_TYPE(expr_tree) == error_mark_node)
1472 return this->error_expression();
1474 tree ret;
1475 if (this->type_size(type) == 0
1476 || TREE_TYPE(expr_tree) == void_type_node)
1478 // Do not convert zero-sized types.
1479 ret = expr_tree;
1481 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1482 ret = fold(convert_to_integer(type_tree, expr_tree));
1483 else if (TREE_CODE(type_tree) == REAL_TYPE)
1484 ret = fold(convert_to_real(type_tree, expr_tree));
1485 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1486 ret = fold(convert_to_complex(type_tree, expr_tree));
1487 else if (TREE_CODE(type_tree) == POINTER_TYPE
1488 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1489 ret = fold(convert_to_pointer(type_tree, expr_tree));
1490 else if (TREE_CODE(type_tree) == RECORD_TYPE
1491 || TREE_CODE(type_tree) == ARRAY_TYPE)
1492 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1493 type_tree, expr_tree);
1494 else
1495 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1497 return this->make_expression(ret);
1500 // Get the address of a function.
1502 Bexpression*
1503 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1505 tree func = bfunc->get_tree();
1506 if (func == error_mark_node)
1507 return this->error_expression();
1509 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1510 return this->make_expression(ret);
1513 // Get the address of an expression.
1515 Bexpression*
1516 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1518 tree expr = bexpr->get_tree();
1519 if (expr == error_mark_node)
1520 return this->error_expression();
1522 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1523 return this->make_expression(ret);
1526 // Return an expression for the field at INDEX in BSTRUCT.
1528 Bexpression*
1529 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1530 Location location)
1532 tree struct_tree = bstruct->get_tree();
1533 if (struct_tree == error_mark_node
1534 || TREE_TYPE(struct_tree) == error_mark_node)
1535 return this->error_expression();
1536 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1537 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1538 if (field == NULL_TREE)
1540 // This can happen for a type which refers to itself indirectly
1541 // and then turns out to be erroneous.
1542 return this->error_expression();
1544 for (unsigned int i = index; i > 0; --i)
1546 field = DECL_CHAIN(field);
1547 gcc_assert(field != NULL_TREE);
1549 if (TREE_TYPE(field) == error_mark_node)
1550 return this->error_expression();
1551 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1552 TREE_TYPE(field), struct_tree, field,
1553 NULL_TREE);
1554 if (TREE_CONSTANT(struct_tree))
1555 TREE_CONSTANT(ret) = 1;
1556 return this->make_expression(ret);
1559 // Return an expression that executes BSTAT before BEXPR.
1561 Bexpression*
1562 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1563 Location location)
1565 tree stat = bstat->get_tree();
1566 tree expr = bexpr->get_tree();
1567 if (stat == error_mark_node || expr == error_mark_node)
1568 return this->error_expression();
1569 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1570 TREE_TYPE(expr), stat, expr);
1571 return this->make_expression(ret);
1574 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1575 // ELSE_EXPR otherwise.
1577 Bexpression*
1578 Gcc_backend::conditional_expression(Bfunction*, Btype* btype,
1579 Bexpression* condition,
1580 Bexpression* then_expr,
1581 Bexpression* else_expr, Location location)
1583 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1584 tree cond_tree = condition->get_tree();
1585 tree then_tree = then_expr->get_tree();
1586 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1587 if (type_tree == error_mark_node
1588 || cond_tree == error_mark_node
1589 || then_tree == error_mark_node
1590 || else_tree == error_mark_node)
1591 return this->error_expression();
1592 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1593 cond_tree, then_tree, else_tree);
1594 return this->make_expression(ret);
1597 // Return an expression for the unary operation OP EXPR.
1599 Bexpression*
1600 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1602 tree expr_tree = expr->get_tree();
1603 if (expr_tree == error_mark_node
1604 || TREE_TYPE(expr_tree) == error_mark_node)
1605 return this->error_expression();
1607 tree type_tree = TREE_TYPE(expr_tree);
1608 enum tree_code code;
1609 switch (op)
1611 case OPERATOR_MINUS:
1613 tree computed_type = excess_precision_type(type_tree);
1614 if (computed_type != NULL_TREE)
1616 expr_tree = convert(computed_type, expr_tree);
1617 type_tree = computed_type;
1619 code = NEGATE_EXPR;
1620 break;
1622 case OPERATOR_NOT:
1623 code = TRUTH_NOT_EXPR;
1624 break;
1625 case OPERATOR_XOR:
1626 code = BIT_NOT_EXPR;
1627 break;
1628 default:
1629 gcc_unreachable();
1630 break;
1633 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1634 expr_tree);
1635 return this->make_expression(ret);
1638 // Convert a gofrontend operator to an equivalent tree_code.
1640 static enum tree_code
1641 operator_to_tree_code(Operator op, tree type)
1643 enum tree_code code;
1644 switch (op)
1646 case OPERATOR_EQEQ:
1647 code = EQ_EXPR;
1648 break;
1649 case OPERATOR_NOTEQ:
1650 code = NE_EXPR;
1651 break;
1652 case OPERATOR_LT:
1653 code = LT_EXPR;
1654 break;
1655 case OPERATOR_LE:
1656 code = LE_EXPR;
1657 break;
1658 case OPERATOR_GT:
1659 code = GT_EXPR;
1660 break;
1661 case OPERATOR_GE:
1662 code = GE_EXPR;
1663 break;
1664 case OPERATOR_OROR:
1665 code = TRUTH_ORIF_EXPR;
1666 break;
1667 case OPERATOR_ANDAND:
1668 code = TRUTH_ANDIF_EXPR;
1669 break;
1670 case OPERATOR_PLUS:
1671 code = PLUS_EXPR;
1672 break;
1673 case OPERATOR_MINUS:
1674 code = MINUS_EXPR;
1675 break;
1676 case OPERATOR_OR:
1677 code = BIT_IOR_EXPR;
1678 break;
1679 case OPERATOR_XOR:
1680 code = BIT_XOR_EXPR;
1681 break;
1682 case OPERATOR_MULT:
1683 code = MULT_EXPR;
1684 break;
1685 case OPERATOR_DIV:
1686 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1687 code = RDIV_EXPR;
1688 else
1689 code = TRUNC_DIV_EXPR;
1690 break;
1691 case OPERATOR_MOD:
1692 code = TRUNC_MOD_EXPR;
1693 break;
1694 case OPERATOR_LSHIFT:
1695 code = LSHIFT_EXPR;
1696 break;
1697 case OPERATOR_RSHIFT:
1698 code = RSHIFT_EXPR;
1699 break;
1700 case OPERATOR_AND:
1701 code = BIT_AND_EXPR;
1702 break;
1703 case OPERATOR_BITCLEAR:
1704 code = BIT_AND_EXPR;
1705 break;
1706 default:
1707 gcc_unreachable();
1710 return code;
1713 // Return an expression for the binary operation LEFT OP RIGHT.
1715 Bexpression*
1716 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1717 Bexpression* right, Location location)
1719 tree left_tree = left->get_tree();
1720 tree right_tree = right->get_tree();
1721 if (left_tree == error_mark_node
1722 || right_tree == error_mark_node)
1723 return this->error_expression();
1724 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1726 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1727 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1728 tree computed_type = excess_precision_type(type_tree);
1729 if (computed_type != NULL_TREE)
1731 left_tree = convert(computed_type, left_tree);
1732 right_tree = convert(computed_type, right_tree);
1733 type_tree = computed_type;
1736 // For comparison operators, the resulting type should be boolean.
1737 switch (op)
1739 case OPERATOR_EQEQ:
1740 case OPERATOR_NOTEQ:
1741 case OPERATOR_LT:
1742 case OPERATOR_LE:
1743 case OPERATOR_GT:
1744 case OPERATOR_GE:
1745 type_tree = boolean_type_node;
1746 break;
1747 default:
1748 break;
1751 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1752 left_tree, right_tree);
1753 return this->make_expression(ret);
1756 // Return an expression that constructs BTYPE with VALS.
1758 Bexpression*
1759 Gcc_backend::constructor_expression(Btype* btype,
1760 const std::vector<Bexpression*>& vals,
1761 Location location)
1763 tree type_tree = btype->get_tree();
1764 if (type_tree == error_mark_node)
1765 return this->error_expression();
1767 vec<constructor_elt, va_gc> *init;
1768 vec_alloc(init, vals.size());
1770 tree sink = NULL_TREE;
1771 bool is_constant = true;
1772 tree field = TYPE_FIELDS(type_tree);
1773 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1774 p != vals.end();
1775 ++p, field = DECL_CHAIN(field))
1777 gcc_assert(field != NULL_TREE);
1778 tree val = (*p)->get_tree();
1779 if (TREE_TYPE(field) == error_mark_node
1780 || val == error_mark_node
1781 || TREE_TYPE(val) == error_mark_node)
1782 return this->error_expression();
1784 if (int_size_in_bytes(TREE_TYPE(field)) == 0)
1786 // GIMPLE cannot represent indices of zero-sized types so
1787 // trying to construct a map with zero-sized keys might lead
1788 // to errors. Instead, we evaluate each expression that
1789 // would have been added as a map element for its
1790 // side-effects and construct an empty map.
1791 append_to_statement_list(val, &sink);
1792 continue;
1795 constructor_elt empty = {NULL, NULL};
1796 constructor_elt* elt = init->quick_push(empty);
1797 elt->index = field;
1798 elt->value = this->convert_tree(TREE_TYPE(field), val, location);
1799 if (!TREE_CONSTANT(elt->value))
1800 is_constant = false;
1802 gcc_assert(field == NULL_TREE);
1803 tree ret = build_constructor(type_tree, init);
1804 if (is_constant)
1805 TREE_CONSTANT(ret) = 1;
1806 if (sink != NULL_TREE)
1807 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1808 type_tree, sink, ret);
1809 return this->make_expression(ret);
1812 Bexpression*
1813 Gcc_backend::array_constructor_expression(
1814 Btype* array_btype, const std::vector<unsigned long>& indexes,
1815 const std::vector<Bexpression*>& vals, Location location)
1817 tree type_tree = array_btype->get_tree();
1818 if (type_tree == error_mark_node)
1819 return this->error_expression();
1821 gcc_assert(indexes.size() == vals.size());
1823 tree element_type = TREE_TYPE(type_tree);
1824 HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
1825 vec<constructor_elt, va_gc> *init;
1826 vec_alloc(init, element_size == 0 ? 0 : vals.size());
1828 tree sink = NULL_TREE;
1829 bool is_constant = true;
1830 for (size_t i = 0; i < vals.size(); ++i)
1832 tree index = size_int(indexes[i]);
1833 tree val = (vals[i])->get_tree();
1835 if (index == error_mark_node
1836 || val == error_mark_node)
1837 return this->error_expression();
1839 if (element_size == 0)
1841 // GIMPLE cannot represent arrays of zero-sized types so trying
1842 // to construct an array of zero-sized values might lead to errors.
1843 // Instead, we evaluate each expression that would have been added as
1844 // an array value for its side-effects and construct an empty array.
1845 append_to_statement_list(val, &sink);
1846 continue;
1849 if (!TREE_CONSTANT(val))
1850 is_constant = false;
1852 constructor_elt empty = {NULL, NULL};
1853 constructor_elt* elt = init->quick_push(empty);
1854 elt->index = index;
1855 elt->value = val;
1858 tree ret = build_constructor(type_tree, init);
1859 if (is_constant)
1860 TREE_CONSTANT(ret) = 1;
1861 if (sink != NULL_TREE)
1862 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1863 type_tree, sink, ret);
1864 return this->make_expression(ret);
1867 // Return an expression for the address of BASE[INDEX].
1869 Bexpression*
1870 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1871 Location location)
1873 tree base_tree = base->get_tree();
1874 tree index_tree = index->get_tree();
1875 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1876 if (base_tree == error_mark_node
1877 || TREE_TYPE(base_tree) == error_mark_node
1878 || index_tree == error_mark_node
1879 || element_type_tree == error_mark_node)
1880 return this->error_expression();
1882 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1883 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1884 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1885 index_tree, element_size);
1886 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1887 TREE_TYPE(base_tree), base_tree, offset);
1888 return this->make_expression(ptr);
1891 // Return an expression representing ARRAY[INDEX]
1893 Bexpression*
1894 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1895 Location location)
1897 tree array_tree = array->get_tree();
1898 tree index_tree = index->get_tree();
1899 if (array_tree == error_mark_node
1900 || TREE_TYPE(array_tree) == error_mark_node
1901 || index_tree == error_mark_node)
1902 return this->error_expression();
1904 // A function call that returns a zero sized object will have been
1905 // changed to return void. If we see void here, assume we are
1906 // dealing with a zero sized type and just evaluate the operands.
1907 tree ret;
1908 if (TREE_TYPE(array_tree) != void_type_node)
1909 ret = build4_loc(location.gcc_location(), ARRAY_REF,
1910 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1911 index_tree, NULL_TREE, NULL_TREE);
1912 else
1913 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1914 void_type_node, array_tree, index_tree);
1916 return this->make_expression(ret);
1919 // Create an expression for a call to FN_EXPR with FN_ARGS.
1920 Bexpression*
1921 Gcc_backend::call_expression(Bfunction*, // containing fcn for call
1922 Bexpression* fn_expr,
1923 const std::vector<Bexpression*>& fn_args,
1924 Bexpression* chain_expr,
1925 Location location)
1927 tree fn = fn_expr->get_tree();
1928 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1929 return this->error_expression();
1931 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1932 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1934 size_t nargs = fn_args.size();
1935 tree* args = nargs == 0 ? NULL : new tree[nargs];
1936 for (size_t i = 0; i < nargs; ++i)
1938 args[i] = fn_args.at(i)->get_tree();
1939 if (args[i] == error_mark_node)
1940 return this->error_expression();
1943 tree fndecl = fn;
1944 if (TREE_CODE(fndecl) == ADDR_EXPR)
1945 fndecl = TREE_OPERAND(fndecl, 0);
1947 // This is to support builtin math functions when using 80387 math.
1948 tree excess_type = NULL_TREE;
1949 if (optimize
1950 && TREE_CODE(fndecl) == FUNCTION_DECL
1951 && DECL_IS_BUILTIN(fndecl)
1952 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
1953 && nargs > 0
1954 && ((SCALAR_FLOAT_TYPE_P(rettype)
1955 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1956 || (COMPLEX_FLOAT_TYPE_P(rettype)
1957 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1959 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1960 if (excess_type != NULL_TREE)
1962 tree excess_fndecl = mathfn_built_in(excess_type,
1963 DECL_FUNCTION_CODE(fndecl));
1964 if (excess_fndecl == NULL_TREE)
1965 excess_type = NULL_TREE;
1966 else
1968 fn = build_fold_addr_expr_loc(location.gcc_location(),
1969 excess_fndecl);
1970 for (size_t i = 0; i < nargs; ++i)
1972 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1973 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1974 args[i] = ::convert(excess_type, args[i]);
1980 tree ret =
1981 build_call_array_loc(location.gcc_location(),
1982 excess_type != NULL_TREE ? excess_type : rettype,
1983 fn, nargs, args);
1985 if (chain_expr)
1986 CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree();
1988 if (excess_type != NULL_TREE)
1990 // Calling convert here can undo our excess precision change.
1991 // That may or may not be a bug in convert_to_real.
1992 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1995 delete[] args;
1996 return this->make_expression(ret);
1999 // An expression as a statement.
2001 Bstatement*
2002 Gcc_backend::expression_statement(Bfunction*, Bexpression* expr)
2004 return this->make_statement(expr->get_tree());
2007 // Variable initialization.
2009 Bstatement*
2010 Gcc_backend::init_statement(Bfunction*, Bvariable* var, Bexpression* init)
2012 tree var_tree = var->get_decl();
2013 tree init_tree = init->get_tree();
2014 if (var_tree == error_mark_node || init_tree == error_mark_node)
2015 return this->error_statement();
2016 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
2018 // To avoid problems with GNU ld, we don't make zero-sized
2019 // externally visible variables. That might lead us to doing an
2020 // initialization of a zero-sized expression to a non-zero sized
2021 // variable, or vice-versa. Avoid crashes by omitting the
2022 // initializer. Such initializations don't mean anything anyhow.
2023 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
2024 && init_tree != NULL_TREE
2025 && TREE_TYPE(init_tree) != void_type_node
2026 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
2028 DECL_INITIAL(var_tree) = init_tree;
2029 init_tree = NULL_TREE;
2032 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
2033 void_type_node, var_tree);
2034 if (init_tree != NULL_TREE)
2035 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
2036 void_type_node, init_tree, ret);
2038 return this->make_statement(ret);
2041 // Assignment.
2043 Bstatement*
2044 Gcc_backend::assignment_statement(Bfunction* bfn, Bexpression* lhs,
2045 Bexpression* rhs, Location location)
2047 tree lhs_tree = lhs->get_tree();
2048 tree rhs_tree = rhs->get_tree();
2049 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
2050 return this->error_statement();
2052 // To avoid problems with GNU ld, we don't make zero-sized
2053 // externally visible variables. That might lead us to doing an
2054 // assignment of a zero-sized expression to a non-zero sized
2055 // expression; avoid crashes here by avoiding assignments of
2056 // zero-sized expressions. Such assignments don't really mean
2057 // anything anyhow.
2058 if (TREE_TYPE(lhs_tree) == void_type_node
2059 || int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
2060 || TREE_TYPE(rhs_tree) == void_type_node
2061 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
2062 return this->compound_statement(this->expression_statement(bfn, lhs),
2063 this->expression_statement(bfn, rhs));
2065 rhs_tree = this->convert_tree(TREE_TYPE(lhs_tree), rhs_tree, location);
2067 return this->make_statement(fold_build2_loc(location.gcc_location(),
2068 MODIFY_EXPR,
2069 void_type_node,
2070 lhs_tree, rhs_tree));
2073 // Return.
2075 Bstatement*
2076 Gcc_backend::return_statement(Bfunction* bfunction,
2077 const std::vector<Bexpression*>& vals,
2078 Location location)
2080 tree fntree = bfunction->get_tree();
2081 if (fntree == error_mark_node)
2082 return this->error_statement();
2083 tree result = DECL_RESULT(fntree);
2084 if (result == error_mark_node)
2085 return this->error_statement();
2087 // If the result size is zero bytes, we have set the function type
2088 // to have a result type of void, so don't return anything.
2089 // See the function_type method.
2090 tree res_type = TREE_TYPE(result);
2091 if (res_type == void_type_node || int_size_in_bytes(res_type) == 0)
2093 tree stmt_list = NULL_TREE;
2094 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2095 p != vals.end();
2096 p++)
2098 tree val = (*p)->get_tree();
2099 if (val == error_mark_node)
2100 return this->error_statement();
2101 append_to_statement_list(val, &stmt_list);
2103 tree ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2104 void_type_node, NULL_TREE);
2105 append_to_statement_list(ret, &stmt_list);
2106 return this->make_statement(stmt_list);
2109 tree ret;
2110 if (vals.empty())
2111 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
2112 NULL_TREE);
2113 else if (vals.size() == 1)
2115 tree val = vals.front()->get_tree();
2116 if (val == error_mark_node)
2117 return this->error_statement();
2118 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2119 void_type_node, result,
2120 vals.front()->get_tree());
2121 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2122 void_type_node, set);
2124 else
2126 // To return multiple values, copy the values into a temporary
2127 // variable of the right structure type, and then assign the
2128 // temporary variable to the DECL_RESULT in the return
2129 // statement.
2130 tree stmt_list = NULL_TREE;
2131 tree rettype = TREE_TYPE(result);
2133 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2134 push_struct_function(fntree);
2135 else
2136 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2137 tree rettmp = create_tmp_var(rettype, "RESULT");
2138 pop_cfun();
2140 tree field = TYPE_FIELDS(rettype);
2141 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2142 p != vals.end();
2143 p++, field = DECL_CHAIN(field))
2145 gcc_assert(field != NULL_TREE);
2146 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
2147 TREE_TYPE(field), rettmp, field,
2148 NULL_TREE);
2149 tree val = (*p)->get_tree();
2150 if (val == error_mark_node)
2151 return this->error_statement();
2152 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2153 void_type_node,
2154 ref, (*p)->get_tree());
2155 append_to_statement_list(set, &stmt_list);
2157 gcc_assert(field == NULL_TREE);
2158 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2159 void_type_node,
2160 result, rettmp);
2161 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2162 void_type_node, set);
2163 append_to_statement_list(ret_expr, &stmt_list);
2164 ret = stmt_list;
2166 return this->make_statement(ret);
2169 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2170 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2171 // NULL, it will always be executed. This is used for handling defers in Go
2172 // functions. In C++, the resulting code is of this form:
2173 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2175 Bstatement*
2176 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2177 Bstatement* except_stmt,
2178 Bstatement* finally_stmt,
2179 Location location)
2181 tree stat_tree = bstat->get_tree();
2182 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2183 tree finally_tree = finally_stmt == NULL
2184 ? NULL_TREE
2185 : finally_stmt->get_tree();
2187 if (stat_tree == error_mark_node
2188 || except_tree == error_mark_node
2189 || finally_tree == error_mark_node)
2190 return this->error_statement();
2192 if (except_tree != NULL_TREE)
2193 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2194 void_type_node, stat_tree,
2195 build2_loc(location.gcc_location(), CATCH_EXPR,
2196 void_type_node, NULL, except_tree));
2197 if (finally_tree != NULL_TREE)
2198 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2199 void_type_node, stat_tree, finally_tree);
2200 return this->make_statement(stat_tree);
2203 // If.
2205 Bstatement*
2206 Gcc_backend::if_statement(Bfunction*, Bexpression* condition,
2207 Bblock* then_block, Bblock* else_block,
2208 Location location)
2210 tree cond_tree = condition->get_tree();
2211 tree then_tree = then_block->get_tree();
2212 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2213 if (cond_tree == error_mark_node
2214 || then_tree == error_mark_node
2215 || else_tree == error_mark_node)
2216 return this->error_statement();
2217 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2218 cond_tree, then_tree, else_tree);
2219 return this->make_statement(ret);
2222 // Switch.
2224 Bstatement*
2225 Gcc_backend::switch_statement(
2226 Bfunction* function,
2227 Bexpression* value,
2228 const std::vector<std::vector<Bexpression*> >& cases,
2229 const std::vector<Bstatement*>& statements,
2230 Location switch_location)
2232 gcc_assert(cases.size() == statements.size());
2234 tree decl = function->get_tree();
2235 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2236 push_struct_function(decl);
2237 else
2238 push_cfun(DECL_STRUCT_FUNCTION(decl));
2240 tree stmt_list = NULL_TREE;
2241 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2242 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2243 ps != statements.end();
2244 ++ps, ++pc)
2246 if (pc->empty())
2248 source_location loc = (*ps != NULL
2249 ? EXPR_LOCATION((*ps)->get_tree())
2250 : UNKNOWN_LOCATION);
2251 tree label = create_artificial_label(loc);
2252 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2253 append_to_statement_list(c, &stmt_list);
2255 else
2257 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2258 pcv != pc->end();
2259 ++pcv)
2261 tree t = (*pcv)->get_tree();
2262 if (t == error_mark_node)
2263 return this->error_statement();
2264 source_location loc = EXPR_LOCATION(t);
2265 tree label = create_artificial_label(loc);
2266 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2267 append_to_statement_list(c, &stmt_list);
2271 if (*ps != NULL)
2273 tree t = (*ps)->get_tree();
2274 if (t == error_mark_node)
2275 return this->error_statement();
2276 append_to_statement_list(t, &stmt_list);
2279 pop_cfun();
2281 tree tv = value->get_tree();
2282 if (tv == error_mark_node)
2283 return this->error_statement();
2284 tree t = build2_loc(switch_location.gcc_location(), SWITCH_EXPR,
2285 NULL_TREE, tv, stmt_list);
2286 return this->make_statement(t);
2289 // Pair of statements.
2291 Bstatement*
2292 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2294 tree stmt_list = NULL_TREE;
2295 tree t = s1->get_tree();
2296 if (t == error_mark_node)
2297 return this->error_statement();
2298 append_to_statement_list(t, &stmt_list);
2299 t = s2->get_tree();
2300 if (t == error_mark_node)
2301 return this->error_statement();
2302 append_to_statement_list(t, &stmt_list);
2304 // If neither statement has any side effects, stmt_list can be NULL
2305 // at this point.
2306 if (stmt_list == NULL_TREE)
2307 stmt_list = integer_zero_node;
2309 return this->make_statement(stmt_list);
2312 // List of statements.
2314 Bstatement*
2315 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2317 tree stmt_list = NULL_TREE;
2318 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2319 p != statements.end();
2320 ++p)
2322 tree t = (*p)->get_tree();
2323 if (t == error_mark_node)
2324 return this->error_statement();
2325 append_to_statement_list(t, &stmt_list);
2327 return this->make_statement(stmt_list);
2330 // Make a block. For some reason gcc uses a dual structure for
2331 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2332 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2333 // the Bblock.
2335 Bblock*
2336 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2337 const std::vector<Bvariable*>& vars,
2338 Location start_location,
2339 Location)
2341 tree block_tree = make_node(BLOCK);
2342 if (enclosing == NULL)
2344 tree fndecl = function->get_tree();
2345 gcc_assert(fndecl != NULL_TREE);
2347 // We may have already created a block for local variables when
2348 // we take the address of a parameter.
2349 if (DECL_INITIAL(fndecl) == NULL_TREE)
2351 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2352 DECL_INITIAL(fndecl) = block_tree;
2354 else
2356 tree superblock_tree = DECL_INITIAL(fndecl);
2357 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2358 tree* pp;
2359 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2360 *pp != NULL_TREE;
2361 pp = &BLOCK_CHAIN(*pp))
2363 *pp = block_tree;
2366 else
2368 tree superbind_tree = enclosing->get_tree();
2369 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2370 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2372 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2373 tree* pp;
2374 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2375 *pp != NULL_TREE;
2376 pp = &BLOCK_CHAIN(*pp))
2378 *pp = block_tree;
2381 tree* pp = &BLOCK_VARS(block_tree);
2382 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2383 pv != vars.end();
2384 ++pv)
2386 *pp = (*pv)->get_decl();
2387 if (*pp != error_mark_node)
2388 pp = &DECL_CHAIN(*pp);
2390 *pp = NULL_TREE;
2392 TREE_USED(block_tree) = 1;
2394 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2395 void_type_node, BLOCK_VARS(block_tree),
2396 NULL_TREE, block_tree);
2397 TREE_SIDE_EFFECTS(bind_tree) = 1;
2398 return new Bblock(bind_tree);
2401 // Add statements to a block.
2403 void
2404 Gcc_backend::block_add_statements(Bblock* bblock,
2405 const std::vector<Bstatement*>& statements)
2407 tree stmt_list = NULL_TREE;
2408 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2409 p != statements.end();
2410 ++p)
2412 tree s = (*p)->get_tree();
2413 if (s != error_mark_node)
2414 append_to_statement_list(s, &stmt_list);
2417 tree bind_tree = bblock->get_tree();
2418 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2419 BIND_EXPR_BODY(bind_tree) = stmt_list;
2422 // Return a block as a statement.
2424 Bstatement*
2425 Gcc_backend::block_statement(Bblock* bblock)
2427 tree bind_tree = bblock->get_tree();
2428 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2429 return this->make_statement(bind_tree);
2432 // This is not static because we declare it with GTY(()) in go-c.h.
2433 tree go_non_zero_struct;
2435 // Return a type corresponding to TYPE with non-zero size.
2437 tree
2438 Gcc_backend::non_zero_size_type(tree type)
2440 if (int_size_in_bytes(type) != 0)
2441 return type;
2443 switch (TREE_CODE(type))
2445 case RECORD_TYPE:
2446 if (TYPE_FIELDS(type) != NULL_TREE)
2448 tree ns = make_node(RECORD_TYPE);
2449 tree field_trees = NULL_TREE;
2450 tree *pp = &field_trees;
2451 for (tree field = TYPE_FIELDS(type);
2452 field != NULL_TREE;
2453 field = DECL_CHAIN(field))
2455 tree ft = TREE_TYPE(field);
2456 if (field == TYPE_FIELDS(type))
2457 ft = non_zero_size_type(ft);
2458 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2459 DECL_NAME(field), ft);
2460 DECL_CONTEXT(f) = ns;
2461 *pp = f;
2462 pp = &DECL_CHAIN(f);
2464 TYPE_FIELDS(ns) = field_trees;
2465 layout_type(ns);
2466 return ns;
2469 if (go_non_zero_struct == NULL_TREE)
2471 type = make_node(RECORD_TYPE);
2472 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2473 get_identifier("dummy"),
2474 boolean_type_node);
2475 DECL_CONTEXT(field) = type;
2476 TYPE_FIELDS(type) = field;
2477 layout_type(type);
2478 go_non_zero_struct = type;
2480 return go_non_zero_struct;
2482 case ARRAY_TYPE:
2484 tree element_type = non_zero_size_type(TREE_TYPE(type));
2485 return build_array_type_nelts(element_type, 1);
2488 default:
2489 gcc_unreachable();
2492 gcc_unreachable();
2495 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type
2496 // can be created multiple times and thus have multiple tree
2497 // representations. Make sure this does not confuse the middle-end.
2499 tree
2500 Gcc_backend::convert_tree(tree type_tree, tree expr_tree, Location location)
2502 if (type_tree == TREE_TYPE(expr_tree))
2503 return expr_tree;
2505 if (type_tree == error_mark_node
2506 || expr_tree == error_mark_node
2507 || TREE_TYPE(expr_tree) == error_mark_node)
2508 return error_mark_node;
2510 gcc_assert(TREE_CODE(type_tree) == TREE_CODE(TREE_TYPE(expr_tree)));
2511 if (POINTER_TYPE_P(type_tree)
2512 || INTEGRAL_TYPE_P(type_tree)
2513 || SCALAR_FLOAT_TYPE_P(type_tree)
2514 || COMPLEX_FLOAT_TYPE_P(type_tree))
2515 return fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
2516 else if (TREE_CODE(type_tree) == RECORD_TYPE
2517 || TREE_CODE(type_tree) == ARRAY_TYPE)
2519 gcc_assert(int_size_in_bytes(type_tree)
2520 == int_size_in_bytes(TREE_TYPE(expr_tree)));
2521 if (TYPE_MAIN_VARIANT(type_tree)
2522 == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
2523 return fold_build1_loc(location.gcc_location(), NOP_EXPR,
2524 type_tree, expr_tree);
2525 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
2526 type_tree, expr_tree);
2529 gcc_unreachable();
2532 // Make a global variable.
2534 Bvariable*
2535 Gcc_backend::global_variable(const std::string& var_name,
2536 const std::string& asm_name,
2537 Btype* btype,
2538 bool is_external,
2539 bool is_hidden,
2540 bool in_unique_section,
2541 Location location)
2543 tree type_tree = btype->get_tree();
2544 if (type_tree == error_mark_node)
2545 return this->error_variable();
2547 // The GNU linker does not like dynamic variables with zero size.
2548 tree orig_type_tree = type_tree;
2549 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2550 type_tree = this->non_zero_size_type(type_tree);
2552 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2553 get_identifier_from_string(var_name),
2554 type_tree);
2555 if (is_external)
2556 DECL_EXTERNAL(decl) = 1;
2557 else
2558 TREE_STATIC(decl) = 1;
2559 if (!is_hidden)
2561 TREE_PUBLIC(decl) = 1;
2562 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2564 else
2566 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2569 TREE_USED(decl) = 1;
2571 if (in_unique_section)
2572 resolve_unique_section (decl, 0, 1);
2574 go_preserve_from_gc(decl);
2576 return new Bvariable(decl, orig_type_tree);
2579 // Set the initial value of a global variable.
2581 void
2582 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2584 tree expr_tree = expr->get_tree();
2585 if (expr_tree == error_mark_node)
2586 return;
2587 gcc_assert(TREE_CONSTANT(expr_tree));
2588 tree var_decl = var->get_decl();
2589 if (var_decl == error_mark_node)
2590 return;
2591 DECL_INITIAL(var_decl) = expr_tree;
2593 // If this variable goes in a unique section, it may need to go into
2594 // a different one now that DECL_INITIAL is set.
2595 if (symtab_node::get(var_decl)
2596 && symtab_node::get(var_decl)->implicit_section)
2598 set_decl_section_name (var_decl, NULL);
2599 resolve_unique_section (var_decl,
2600 compute_reloc_for_constant (expr_tree),
2605 // Make a local variable.
2607 Bvariable*
2608 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2609 Btype* btype, Bvariable* decl_var,
2610 bool is_address_taken, Location location)
2612 tree type_tree = btype->get_tree();
2613 if (type_tree == error_mark_node)
2614 return this->error_variable();
2615 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2616 get_identifier_from_string(name),
2617 type_tree);
2618 DECL_CONTEXT(decl) = function->get_tree();
2619 TREE_USED(decl) = 1;
2620 if (is_address_taken)
2621 TREE_ADDRESSABLE(decl) = 1;
2622 if (decl_var != NULL)
2624 DECL_HAS_VALUE_EXPR_P(decl) = 1;
2625 SET_DECL_VALUE_EXPR(decl, decl_var->get_decl());
2627 go_preserve_from_gc(decl);
2628 return new Bvariable(decl);
2631 // Make a function parameter variable.
2633 Bvariable*
2634 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2635 Btype* btype, bool is_address_taken,
2636 Location location)
2638 tree type_tree = btype->get_tree();
2639 if (type_tree == error_mark_node)
2640 return this->error_variable();
2641 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2642 get_identifier_from_string(name),
2643 type_tree);
2644 DECL_CONTEXT(decl) = function->get_tree();
2645 DECL_ARG_TYPE(decl) = type_tree;
2646 TREE_USED(decl) = 1;
2647 if (is_address_taken)
2648 TREE_ADDRESSABLE(decl) = 1;
2649 go_preserve_from_gc(decl);
2650 return new Bvariable(decl);
2653 // Make a static chain variable.
2655 Bvariable*
2656 Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name,
2657 Btype* btype, Location location)
2659 tree type_tree = btype->get_tree();
2660 if (type_tree == error_mark_node)
2661 return this->error_variable();
2662 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2663 get_identifier_from_string(name), type_tree);
2664 tree fndecl = function->get_tree();
2665 DECL_CONTEXT(decl) = fndecl;
2666 DECL_ARG_TYPE(decl) = type_tree;
2667 TREE_USED(decl) = 1;
2668 DECL_ARTIFICIAL(decl) = 1;
2669 DECL_IGNORED_P(decl) = 1;
2670 TREE_READONLY(decl) = 1;
2672 struct function *f = DECL_STRUCT_FUNCTION(fndecl);
2673 if (f == NULL)
2675 push_struct_function(fndecl);
2676 pop_cfun();
2677 f = DECL_STRUCT_FUNCTION(fndecl);
2679 gcc_assert(f->static_chain_decl == NULL);
2680 f->static_chain_decl = decl;
2681 DECL_STATIC_CHAIN(fndecl) = 1;
2683 go_preserve_from_gc(decl);
2684 return new Bvariable(decl);
2687 // Make a temporary variable.
2689 Bvariable*
2690 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2691 Btype* btype, Bexpression* binit,
2692 bool is_address_taken,
2693 Location location,
2694 Bstatement** pstatement)
2696 gcc_assert(function != NULL);
2697 tree decl = function->get_tree();
2698 tree type_tree = btype->get_tree();
2699 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2700 if (type_tree == error_mark_node
2701 || init_tree == error_mark_node
2702 || decl == error_mark_node)
2704 *pstatement = this->error_statement();
2705 return this->error_variable();
2708 tree var;
2709 // We can only use create_tmp_var if the type is not addressable.
2710 if (!TREE_ADDRESSABLE(type_tree))
2712 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2713 push_struct_function(decl);
2714 else
2715 push_cfun(DECL_STRUCT_FUNCTION(decl));
2717 var = create_tmp_var(type_tree, "GOTMP");
2718 pop_cfun();
2720 else
2722 gcc_assert(bblock != NULL);
2723 var = build_decl(location.gcc_location(), VAR_DECL,
2724 create_tmp_var_name("GOTMP"),
2725 type_tree);
2726 DECL_ARTIFICIAL(var) = 1;
2727 DECL_IGNORED_P(var) = 1;
2728 TREE_USED(var) = 1;
2729 DECL_CONTEXT(var) = decl;
2731 // We have to add this variable to the BLOCK and the BIND_EXPR.
2732 tree bind_tree = bblock->get_tree();
2733 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2734 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2735 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2736 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2737 BLOCK_VARS(block_tree) = var;
2738 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2741 if (this->type_size(btype) != 0
2742 && init_tree != NULL_TREE
2743 && TREE_TYPE(init_tree) != void_type_node)
2744 DECL_INITIAL(var) = this->convert_tree(type_tree, init_tree, location);
2746 if (is_address_taken)
2747 TREE_ADDRESSABLE(var) = 1;
2749 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2750 DECL_EXPR,
2751 void_type_node, var));
2753 // For a zero sized type, don't initialize VAR with BINIT, but still
2754 // evaluate BINIT for its side effects.
2755 if (init_tree != NULL_TREE
2756 && (this->type_size(btype) == 0
2757 || TREE_TYPE(init_tree) == void_type_node))
2758 *pstatement =
2759 this->compound_statement(this->expression_statement(function, binit),
2760 *pstatement);
2762 return new Bvariable(var);
2765 // Create an implicit variable that is compiler-defined. This is used when
2766 // generating GC root variables and storing the values of a slice initializer.
2768 Bvariable*
2769 Gcc_backend::implicit_variable(const std::string& name,
2770 const std::string& asm_name,
2771 Btype* type, bool is_hidden, bool is_constant,
2772 bool is_common, int64_t alignment)
2774 tree type_tree = type->get_tree();
2775 if (type_tree == error_mark_node)
2776 return this->error_variable();
2778 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2779 get_identifier_from_string(name), type_tree);
2780 DECL_EXTERNAL(decl) = 0;
2781 TREE_PUBLIC(decl) = !is_hidden;
2782 TREE_STATIC(decl) = 1;
2783 TREE_USED(decl) = 1;
2784 DECL_ARTIFICIAL(decl) = 1;
2785 if (is_common)
2787 DECL_COMMON(decl) = 1;
2789 // When the initializer for one implicit_variable refers to another,
2790 // it needs to know the visibility of the referenced struct so that
2791 // compute_reloc_for_constant will return the right value. On many
2792 // systems calling make_decl_one_only will mark the decl as weak,
2793 // which will change the return value of compute_reloc_for_constant.
2794 // We can't reliably call make_decl_one_only yet, because we don't
2795 // yet know the initializer. This issue doesn't arise in C because
2796 // Go initializers, unlike C initializers, can be indirectly
2797 // recursive. To ensure that compute_reloc_for_constant computes
2798 // the right value if some other initializer refers to this one, we
2799 // mark this symbol as weak here. We undo that below in
2800 // immutable_struct_set_init before calling mark_decl_one_only.
2801 DECL_WEAK(decl) = 1;
2803 if (is_constant)
2805 TREE_READONLY(decl) = 1;
2806 TREE_CONSTANT(decl) = 1;
2808 if (alignment != 0)
2810 SET_DECL_ALIGN(decl, alignment * BITS_PER_UNIT);
2811 DECL_USER_ALIGN(decl) = 1;
2813 if (! asm_name.empty())
2814 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2816 go_preserve_from_gc(decl);
2817 return new Bvariable(decl);
2820 // Set the initalizer for a variable created by implicit_variable.
2821 // This is where we finish compiling the variable.
2823 void
2824 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
2825 Btype*, bool, bool, bool is_common,
2826 Bexpression* init)
2828 tree decl = var->get_decl();
2829 tree init_tree;
2830 if (init == NULL)
2831 init_tree = NULL_TREE;
2832 else
2833 init_tree = init->get_tree();
2834 if (decl == error_mark_node || init_tree == error_mark_node)
2835 return;
2837 DECL_INITIAL(decl) = init_tree;
2839 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2840 // See the comment where DECL_WEAK is set in implicit_variable.
2841 if (is_common)
2843 DECL_WEAK(decl) = 0;
2844 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2847 resolve_unique_section(decl, 2, 1);
2849 rest_of_decl_compilation(decl, 1, 0);
2852 // Return a reference to an implicit variable defined in another package.
2854 Bvariable*
2855 Gcc_backend::implicit_variable_reference(const std::string& name,
2856 const std::string& asm_name,
2857 Btype* btype)
2859 tree type_tree = btype->get_tree();
2860 if (type_tree == error_mark_node)
2861 return this->error_variable();
2863 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2864 get_identifier_from_string(name), type_tree);
2865 DECL_EXTERNAL(decl) = 1;
2866 TREE_PUBLIC(decl) = 1;
2867 TREE_STATIC(decl) = 0;
2868 DECL_ARTIFICIAL(decl) = 1;
2869 if (! asm_name.empty())
2870 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2871 go_preserve_from_gc(decl);
2872 return new Bvariable(decl);
2875 // Create a named immutable initialized data structure.
2877 Bvariable*
2878 Gcc_backend::immutable_struct(const std::string& name,
2879 const std::string& asm_name,
2880 bool is_hidden,
2881 bool is_common, Btype* btype, Location location)
2883 tree type_tree = btype->get_tree();
2884 if (type_tree == error_mark_node)
2885 return this->error_variable();
2886 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2887 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2888 get_identifier_from_string(name),
2889 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2890 TREE_STATIC(decl) = 1;
2891 TREE_USED(decl) = 1;
2892 TREE_READONLY(decl) = 1;
2893 TREE_CONSTANT(decl) = 1;
2894 DECL_ARTIFICIAL(decl) = 1;
2895 if (!is_hidden)
2896 TREE_PUBLIC(decl) = 1;
2897 if (! asm_name.empty())
2898 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2900 // When the initializer for one immutable_struct refers to another,
2901 // it needs to know the visibility of the referenced struct so that
2902 // compute_reloc_for_constant will return the right value. On many
2903 // systems calling make_decl_one_only will mark the decl as weak,
2904 // which will change the return value of compute_reloc_for_constant.
2905 // We can't reliably call make_decl_one_only yet, because we don't
2906 // yet know the initializer. This issue doesn't arise in C because
2907 // Go initializers, unlike C initializers, can be indirectly
2908 // recursive. To ensure that compute_reloc_for_constant computes
2909 // the right value if some other initializer refers to this one, we
2910 // mark this symbol as weak here. We undo that below in
2911 // immutable_struct_set_init before calling mark_decl_one_only.
2912 if (is_common)
2913 DECL_WEAK(decl) = 1;
2915 // We don't call rest_of_decl_compilation until we have the
2916 // initializer.
2918 go_preserve_from_gc(decl);
2919 return new Bvariable(decl);
2922 // Set the initializer for a variable created by immutable_struct.
2923 // This is where we finish compiling the variable.
2925 void
2926 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2927 bool, bool is_common, Btype*, Location,
2928 Bexpression* initializer)
2930 tree decl = var->get_decl();
2931 tree init_tree = initializer->get_tree();
2932 if (decl == error_mark_node || init_tree == error_mark_node)
2933 return;
2935 DECL_INITIAL(decl) = init_tree;
2937 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2938 // See the comment where DECL_WEAK is set in immutable_struct.
2939 if (is_common)
2941 DECL_WEAK(decl) = 0;
2942 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2945 // These variables are often unneeded in the final program, so put
2946 // them in their own section so that linker GC can discard them.
2947 resolve_unique_section(decl,
2948 compute_reloc_for_constant (init_tree),
2951 rest_of_decl_compilation(decl, 1, 0);
2954 // Return a reference to an immutable initialized data structure
2955 // defined in another package.
2957 Bvariable*
2958 Gcc_backend::immutable_struct_reference(const std::string& name,
2959 const std::string& asm_name,
2960 Btype* btype,
2961 Location location)
2963 tree type_tree = btype->get_tree();
2964 if (type_tree == error_mark_node)
2965 return this->error_variable();
2966 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2967 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2968 get_identifier_from_string(name),
2969 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2970 TREE_READONLY(decl) = 1;
2971 TREE_CONSTANT(decl) = 1;
2972 DECL_ARTIFICIAL(decl) = 1;
2973 TREE_PUBLIC(decl) = 1;
2974 DECL_EXTERNAL(decl) = 1;
2975 if (! asm_name.empty())
2976 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2977 go_preserve_from_gc(decl);
2978 return new Bvariable(decl);
2981 // Make a label.
2983 Blabel*
2984 Gcc_backend::label(Bfunction* function, const std::string& name,
2985 Location location)
2987 tree decl;
2988 if (name.empty())
2990 tree func_tree = function->get_tree();
2991 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2992 push_struct_function(func_tree);
2993 else
2994 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2996 decl = create_artificial_label(location.gcc_location());
2998 pop_cfun();
3000 else
3002 tree id = get_identifier_from_string(name);
3003 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
3004 void_type_node);
3005 DECL_CONTEXT(decl) = function->get_tree();
3007 return new Blabel(decl);
3010 // Make a statement which defines a label.
3012 Bstatement*
3013 Gcc_backend::label_definition_statement(Blabel* label)
3015 tree lab = label->get_tree();
3016 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
3017 void_type_node, lab);
3018 return this->make_statement(ret);
3021 // Make a goto statement.
3023 Bstatement*
3024 Gcc_backend::goto_statement(Blabel* label, Location location)
3026 tree lab = label->get_tree();
3027 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
3028 lab);
3029 return this->make_statement(ret);
3032 // Get the address of a label.
3034 Bexpression*
3035 Gcc_backend::label_address(Blabel* label, Location location)
3037 tree lab = label->get_tree();
3038 TREE_USED(lab) = 1;
3039 TREE_ADDRESSABLE(lab) = 1;
3040 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
3041 build_fold_addr_expr_loc(location.gcc_location(),
3042 lab));
3043 return this->make_expression(ret);
3046 // Declare or define a new function.
3048 Bfunction*
3049 Gcc_backend::function(Btype* fntype, const std::string& name,
3050 const std::string& asm_name, bool is_visible,
3051 bool is_declaration, bool is_inlinable,
3052 bool disable_split_stack, bool does_not_return,
3053 bool in_unique_section, Location location)
3055 tree functype = fntype->get_tree();
3056 if (functype != error_mark_node)
3058 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
3059 functype = TREE_TYPE(functype);
3061 tree id = get_identifier_from_string(name);
3062 if (functype == error_mark_node || id == error_mark_node)
3063 return this->error_function();
3065 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
3066 if (! asm_name.empty())
3067 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
3068 if (is_visible)
3069 TREE_PUBLIC(decl) = 1;
3070 if (is_declaration)
3071 DECL_EXTERNAL(decl) = 1;
3072 else
3074 tree restype = TREE_TYPE(functype);
3075 tree resdecl =
3076 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
3077 DECL_ARTIFICIAL(resdecl) = 1;
3078 DECL_IGNORED_P(resdecl) = 1;
3079 DECL_CONTEXT(resdecl) = decl;
3080 DECL_RESULT(decl) = resdecl;
3082 if (!is_inlinable)
3083 DECL_UNINLINABLE(decl) = 1;
3084 if (disable_split_stack)
3086 tree attr = get_identifier ("no_split_stack");
3087 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
3089 if (does_not_return)
3090 TREE_THIS_VOLATILE(decl) = 1;
3091 if (in_unique_section)
3092 resolve_unique_section(decl, 0, 1);
3094 go_preserve_from_gc(decl);
3095 return new Bfunction(decl);
3098 // Create a statement that runs all deferred calls for FUNCTION. This should
3099 // be a statement that looks like this in C++:
3100 // finish:
3101 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3103 Bstatement*
3104 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
3105 Bexpression* defer, Location location)
3107 tree undefer_tree = undefer->get_tree();
3108 tree defer_tree = defer->get_tree();
3109 tree fntree = function->get_tree();
3111 if (undefer_tree == error_mark_node
3112 || defer_tree == error_mark_node
3113 || fntree == error_mark_node)
3114 return this->error_statement();
3116 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
3117 push_struct_function(fntree);
3118 else
3119 push_cfun(DECL_STRUCT_FUNCTION(fntree));
3121 tree stmt_list = NULL;
3122 Blabel* blabel = this->label(function, "", location);
3123 Bstatement* label_def = this->label_definition_statement(blabel);
3124 append_to_statement_list(label_def->get_tree(), &stmt_list);
3126 Bstatement* jump_stmt = this->goto_statement(blabel, location);
3127 tree jump = jump_stmt->get_tree();
3128 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
3129 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
3130 tree try_catch =
3131 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
3132 append_to_statement_list(try_catch, &stmt_list);
3133 pop_cfun();
3135 return this->make_statement(stmt_list);
3138 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3139 // This will only be called for a function definition.
3141 bool
3142 Gcc_backend::function_set_parameters(Bfunction* function,
3143 const std::vector<Bvariable*>& param_vars)
3145 tree func_tree = function->get_tree();
3146 if (func_tree == error_mark_node)
3147 return false;
3149 tree params = NULL_TREE;
3150 tree *pp = &params;
3151 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
3152 pv != param_vars.end();
3153 ++pv)
3155 *pp = (*pv)->get_decl();
3156 gcc_assert(*pp != error_mark_node);
3157 pp = &DECL_CHAIN(*pp);
3159 *pp = NULL_TREE;
3160 DECL_ARGUMENTS(func_tree) = params;
3161 return true;
3164 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3166 bool
3167 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
3169 tree func_tree = function->get_tree();
3170 tree code = code_stmt->get_tree();
3172 if (func_tree == error_mark_node || code == error_mark_node)
3173 return false;
3174 DECL_SAVED_TREE(func_tree) = code;
3175 return true;
3178 // Look up a named built-in function in the current backend implementation.
3179 // Returns NULL if no built-in function by that name exists.
3181 Bfunction*
3182 Gcc_backend::lookup_builtin(const std::string& name)
3184 if (this->builtin_functions_.count(name) != 0)
3185 return this->builtin_functions_[name];
3186 return NULL;
3189 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3190 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3191 // emit early debugging information.
3193 void
3194 Gcc_backend::write_global_definitions(
3195 const std::vector<Btype*>& type_decls,
3196 const std::vector<Bexpression*>& constant_decls,
3197 const std::vector<Bfunction*>& function_decls,
3198 const std::vector<Bvariable*>& variable_decls)
3200 size_t count_definitions = type_decls.size() + constant_decls.size()
3201 + function_decls.size() + variable_decls.size();
3203 tree* defs = new tree[count_definitions];
3205 // Convert all non-erroneous declarations into Gimple form.
3206 size_t i = 0;
3207 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
3208 p != variable_decls.end();
3209 ++p)
3211 tree v = (*p)->get_decl();
3212 if (v != error_mark_node)
3214 defs[i] = v;
3215 go_preserve_from_gc(defs[i]);
3216 ++i;
3220 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
3221 p != type_decls.end();
3222 ++p)
3224 tree type_tree = (*p)->get_tree();
3225 if (type_tree != error_mark_node
3226 && IS_TYPE_OR_DECL_P(type_tree))
3228 defs[i] = TYPE_NAME(type_tree);
3229 gcc_assert(defs[i] != NULL);
3230 go_preserve_from_gc(defs[i]);
3231 ++i;
3234 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
3235 p != constant_decls.end();
3236 ++p)
3238 if ((*p)->get_tree() != error_mark_node)
3240 defs[i] = (*p)->get_tree();
3241 go_preserve_from_gc(defs[i]);
3242 ++i;
3245 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
3246 p != function_decls.end();
3247 ++p)
3249 tree decl = (*p)->get_tree();
3250 if (decl != error_mark_node)
3252 go_preserve_from_gc(decl);
3253 gimplify_function_tree(decl);
3254 cgraph_node::finalize_function(decl, true);
3256 defs[i] = decl;
3257 ++i;
3261 // Pass everything back to the middle-end.
3263 wrapup_global_declarations(defs, i);
3265 delete[] defs;
3268 void
3269 Gcc_backend::write_export_data(const char* bytes, unsigned int size)
3271 go_write_export_data(bytes, size);
3275 // Define a builtin function. BCODE is the builtin function code
3276 // defined by builtins.def. NAME is the name of the builtin function.
3277 // LIBNAME is the name of the corresponding library function, and is
3278 // NULL if there isn't one. FNTYPE is the type of the function.
3279 // CONST_P is true if the function has the const attribute.
3280 // NORETURN_P is true if the function has the noreturn attribute.
3282 void
3283 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3284 const char* libname, tree fntype, bool const_p,
3285 bool noreturn_p)
3287 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3288 libname, NULL_TREE);
3289 if (const_p)
3290 TREE_READONLY(decl) = 1;
3291 if (noreturn_p)
3292 TREE_THIS_VOLATILE(decl) = 1;
3293 set_builtin_decl(bcode, decl, true);
3294 this->builtin_functions_[name] = this->make_function(decl);
3295 if (libname != NULL)
3297 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3298 NULL, NULL_TREE);
3299 if (const_p)
3300 TREE_READONLY(decl) = 1;
3301 if (noreturn_p)
3302 TREE_THIS_VOLATILE(decl) = 1;
3303 this->builtin_functions_[libname] = this->make_function(decl);
3307 // Return the backend generator.
3309 Backend*
3310 go_get_backend()
3312 return new Gcc_backend();