* tree-vect-loop-manip.c (vect_do_peeling): Do not use
[official-gcc.git] / gcc / go / go-gcc.cc
bloba50abdd856b8457b4ccbe4655f04b524fc1d2fbc
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2017 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 Bexpression*
356 stack_allocation_expression(int64_t size, Location);
358 // Statements.
360 Bstatement*
361 error_statement()
362 { return this->make_statement(error_mark_node); }
364 Bstatement*
365 expression_statement(Bfunction*, Bexpression*);
367 Bstatement*
368 init_statement(Bfunction*, Bvariable* var, Bexpression* init);
370 Bstatement*
371 assignment_statement(Bfunction*, Bexpression* lhs, Bexpression* rhs,
372 Location);
374 Bstatement*
375 return_statement(Bfunction*, const std::vector<Bexpression*>&,
376 Location);
378 Bstatement*
379 if_statement(Bfunction*, Bexpression* condition, Bblock* then_block,
380 Bblock* else_block, Location);
382 Bstatement*
383 switch_statement(Bfunction* function, Bexpression* value,
384 const std::vector<std::vector<Bexpression*> >& cases,
385 const std::vector<Bstatement*>& statements,
386 Location);
388 Bstatement*
389 compound_statement(Bstatement*, Bstatement*);
391 Bstatement*
392 statement_list(const std::vector<Bstatement*>&);
394 Bstatement*
395 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
396 Bstatement* finally_stmt, Location);
398 // Blocks.
400 Bblock*
401 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
402 Location, Location);
404 void
405 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
407 Bstatement*
408 block_statement(Bblock*);
410 // Variables.
412 Bvariable*
413 error_variable()
414 { return new Bvariable(error_mark_node); }
416 Bvariable*
417 global_variable(const std::string& var_name,
418 const std::string& asm_name,
419 Btype* btype,
420 bool is_external,
421 bool is_hidden,
422 bool in_unique_section,
423 Location location);
425 void
426 global_variable_set_init(Bvariable*, Bexpression*);
428 Bvariable*
429 local_variable(Bfunction*, const std::string&, Btype*, bool,
430 Location);
432 Bvariable*
433 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
434 Location);
436 Bvariable*
437 static_chain_variable(Bfunction*, const std::string&, Btype*, Location);
439 Bvariable*
440 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
441 Location, Bstatement**);
443 Bvariable*
444 implicit_variable(const std::string&, const std::string&, Btype*,
445 bool, bool, bool, int64_t);
447 void
448 implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
449 bool, bool, bool, Bexpression*);
451 Bvariable*
452 implicit_variable_reference(const std::string&, const std::string&, Btype*);
454 Bvariable*
455 immutable_struct(const std::string&, const std::string&,
456 bool, bool, Btype*, Location);
458 void
459 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
460 Location, Bexpression*);
462 Bvariable*
463 immutable_struct_reference(const std::string&, const std::string&,
464 Btype*, Location);
466 // Labels.
468 Blabel*
469 label(Bfunction*, const std::string& name, Location);
471 Bstatement*
472 label_definition_statement(Blabel*);
474 Bstatement*
475 goto_statement(Blabel*, Location);
477 Bexpression*
478 label_address(Blabel*, Location);
480 // Functions.
482 Bfunction*
483 error_function()
484 { return this->make_function(error_mark_node); }
486 Bfunction*
487 function(Btype* fntype, const std::string& name, const std::string& asm_name,
488 bool is_visible, bool is_declaration, bool is_inlinable,
489 bool disable_split_stack, bool in_unique_section, Location);
491 Bstatement*
492 function_defer_statement(Bfunction* function, Bexpression* undefer,
493 Bexpression* defer, Location);
495 bool
496 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
498 bool
499 function_set_body(Bfunction* function, Bstatement* code_stmt);
501 Bfunction*
502 lookup_builtin(const std::string&);
504 void
505 write_global_definitions(const std::vector<Btype*>&,
506 const std::vector<Bexpression*>&,
507 const std::vector<Bfunction*>&,
508 const std::vector<Bvariable*>&);
510 void
511 write_export_data(const char* bytes, unsigned int size);
514 private:
515 // Make a Bexpression from a tree.
516 Bexpression*
517 make_expression(tree t)
518 { return new Bexpression(t); }
520 // Make a Bstatement from a tree.
521 Bstatement*
522 make_statement(tree t)
523 { return new Bstatement(t); }
525 // Make a Btype from a tree.
526 Btype*
527 make_type(tree t)
528 { return new Btype(t); }
530 Bfunction*
531 make_function(tree t)
532 { return new Bfunction(t); }
534 Btype*
535 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
537 Btype*
538 fill_in_array(Btype*, Btype*, Bexpression*);
540 tree
541 non_zero_size_type(tree);
543 private:
544 void
545 define_builtin(built_in_function bcode, const char* name, const char* libname,
546 tree fntype, bool const_p, bool noreturn_p);
548 // A mapping of the GCC built-ins exposed to GCCGo.
549 std::map<std::string, Bfunction*> builtin_functions_;
552 // A helper function to create a GCC identifier from a C++ string.
554 static inline tree
555 get_identifier_from_string(const std::string& str)
557 return get_identifier_with_length(str.data(), str.length());
560 // Define the built-in functions that are exposed to GCCGo.
562 Gcc_backend::Gcc_backend()
564 /* We need to define the fetch_and_add functions, since we use them
565 for ++ and --. */
566 tree t = this->integer_type(true, BITS_PER_UNIT)->get_tree();
567 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
568 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
569 NULL, build_function_type_list(t, p, t, NULL_TREE),
570 false, false);
572 t = this->integer_type(true, BITS_PER_UNIT * 2)->get_tree();
573 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
574 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
575 NULL, build_function_type_list(t, p, t, NULL_TREE),
576 false, false);
578 t = this->integer_type(true, BITS_PER_UNIT * 4)->get_tree();
579 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
580 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
581 NULL, build_function_type_list(t, p, t, NULL_TREE),
582 false, false);
584 t = this->integer_type(true, BITS_PER_UNIT * 8)->get_tree();
585 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
586 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
587 NULL, build_function_type_list(t, p, t, NULL_TREE),
588 false, false);
590 // We use __builtin_expect for magic import functions.
591 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
592 build_function_type_list(long_integer_type_node,
593 long_integer_type_node,
594 long_integer_type_node,
595 NULL_TREE),
596 true, false);
598 // We use __builtin_memcmp for struct comparisons.
599 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
600 build_function_type_list(integer_type_node,
601 const_ptr_type_node,
602 const_ptr_type_node,
603 size_type_node,
604 NULL_TREE),
605 false, false);
607 // Used by runtime/internal/sys.
608 this->define_builtin(BUILT_IN_CTZ, "__builtin_ctz", "ctz",
609 build_function_type_list(integer_type_node,
610 unsigned_type_node,
611 NULL_TREE),
612 true, false);
613 this->define_builtin(BUILT_IN_CTZLL, "__builtin_ctzll", "ctzll",
614 build_function_type_list(integer_type_node,
615 long_long_unsigned_type_node,
616 NULL_TREE),
617 true, false);
618 this->define_builtin(BUILT_IN_BSWAP32, "__builtin_bswap32", "bswap32",
619 build_function_type_list(uint32_type_node,
620 uint32_type_node,
621 NULL_TREE),
622 true, false);
623 this->define_builtin(BUILT_IN_BSWAP64, "__builtin_bswap64", "bswap64",
624 build_function_type_list(uint64_type_node,
625 uint64_type_node,
626 NULL_TREE),
627 true, false);
629 // We provide some functions for the math library.
630 tree math_function_type = build_function_type_list(double_type_node,
631 double_type_node,
632 NULL_TREE);
633 tree math_function_type_long =
634 build_function_type_list(long_double_type_node, long_double_type_node,
635 long_double_type_node, NULL_TREE);
636 tree math_function_type_two = build_function_type_list(double_type_node,
637 double_type_node,
638 double_type_node,
639 NULL_TREE);
640 tree math_function_type_long_two =
641 build_function_type_list(long_double_type_node, long_double_type_node,
642 long_double_type_node, NULL_TREE);
643 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
644 math_function_type, true, false);
645 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
646 math_function_type_long, true, false);
647 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
648 math_function_type, true, false);
649 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
650 math_function_type_long, true, false);
651 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
652 math_function_type, true, false);
653 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
654 math_function_type_long, true, false);
655 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
656 math_function_type_two, true, false);
657 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
658 math_function_type_long_two, true, false);
659 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
660 math_function_type, true, false);
661 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
662 math_function_type_long, true, false);
663 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
664 math_function_type, true, false);
665 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
666 math_function_type_long, true, false);
667 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
668 math_function_type, true, false);
669 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
670 math_function_type_long, true, false);
671 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
672 math_function_type, true, false);
673 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
674 math_function_type_long, true, false);
675 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
676 math_function_type, true, false);
677 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
678 math_function_type_long, true, false);
679 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
680 math_function_type, true, false);
681 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
682 math_function_type_long, true, false);
683 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
684 math_function_type_two, true, false);
685 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
686 math_function_type_long_two, true, false);
687 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
688 build_function_type_list(double_type_node,
689 double_type_node,
690 integer_type_node,
691 NULL_TREE),
692 true, false);
693 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
694 build_function_type_list(long_double_type_node,
695 long_double_type_node,
696 integer_type_node,
697 NULL_TREE),
698 true, false);
699 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
700 math_function_type, true, false);
701 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
702 math_function_type_long, true, false);
703 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
704 math_function_type, true, false);
705 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
706 math_function_type_long, true, false);
707 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
708 math_function_type, true, false);
709 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
710 math_function_type_long, true, false);
711 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
712 math_function_type, true, false);
713 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
714 math_function_type_long, true, false);
715 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
716 math_function_type, true, false);
717 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
718 math_function_type_long, true, false);
719 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
720 math_function_type, true, false);
721 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
722 math_function_type_long, true, false);
723 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
724 math_function_type, true, false);
725 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
726 math_function_type_long, true, false);
727 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
728 math_function_type, true, false);
729 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
730 math_function_type_long, true, false);
732 // We use __builtin_return_address in the thunk we build for
733 // functions which call recover, and for runtime.getcallerpc.
734 t = build_function_type_list(ptr_type_node, unsigned_type_node, NULL_TREE);
735 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
736 NULL, t, false, false);
738 // The runtime calls __builtin_frame_address for runtime.getcallersp.
739 this->define_builtin(BUILT_IN_FRAME_ADDRESS, "__builtin_frame_address",
740 NULL, t, false, false);
742 // The runtime calls __builtin_extract_return_addr when recording
743 // the address to which a function returns.
744 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR,
745 "__builtin_extract_return_addr", NULL,
746 build_function_type_list(ptr_type_node,
747 ptr_type_node,
748 NULL_TREE),
749 false, false);
751 // The compiler uses __builtin_trap for some exception handling
752 // cases.
753 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
754 build_function_type(void_type_node, void_list_node),
755 false, true);
757 // The runtime uses __builtin_prefetch.
758 this->define_builtin(BUILT_IN_PREFETCH, "__builtin_prefetch", NULL,
759 build_varargs_function_type_list(void_type_node,
760 const_ptr_type_node,
761 NULL_TREE),
762 false, false);
765 // Get an unnamed integer type.
767 Btype*
768 Gcc_backend::integer_type(bool is_unsigned, int bits)
770 tree type;
771 if (is_unsigned)
773 if (bits == INT_TYPE_SIZE)
774 type = unsigned_type_node;
775 else if (bits == CHAR_TYPE_SIZE)
776 type = unsigned_char_type_node;
777 else if (bits == SHORT_TYPE_SIZE)
778 type = short_unsigned_type_node;
779 else if (bits == LONG_TYPE_SIZE)
780 type = long_unsigned_type_node;
781 else if (bits == LONG_LONG_TYPE_SIZE)
782 type = long_long_unsigned_type_node;
783 else
784 type = make_unsigned_type(bits);
786 else
788 if (bits == INT_TYPE_SIZE)
789 type = integer_type_node;
790 else if (bits == CHAR_TYPE_SIZE)
791 type = signed_char_type_node;
792 else if (bits == SHORT_TYPE_SIZE)
793 type = short_integer_type_node;
794 else if (bits == LONG_TYPE_SIZE)
795 type = long_integer_type_node;
796 else if (bits == LONG_LONG_TYPE_SIZE)
797 type = long_long_integer_type_node;
798 else
799 type = make_signed_type(bits);
801 return this->make_type(type);
804 // Get an unnamed float type.
806 Btype*
807 Gcc_backend::float_type(int bits)
809 tree type;
810 if (bits == FLOAT_TYPE_SIZE)
811 type = float_type_node;
812 else if (bits == DOUBLE_TYPE_SIZE)
813 type = double_type_node;
814 else if (bits == LONG_DOUBLE_TYPE_SIZE)
815 type = long_double_type_node;
816 else
818 type = make_node(REAL_TYPE);
819 TYPE_PRECISION(type) = bits;
820 layout_type(type);
822 return this->make_type(type);
825 // Get an unnamed complex type.
827 Btype*
828 Gcc_backend::complex_type(int bits)
830 tree type;
831 if (bits == FLOAT_TYPE_SIZE * 2)
832 type = complex_float_type_node;
833 else if (bits == DOUBLE_TYPE_SIZE * 2)
834 type = complex_double_type_node;
835 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
836 type = complex_long_double_type_node;
837 else
839 type = make_node(REAL_TYPE);
840 TYPE_PRECISION(type) = bits / 2;
841 layout_type(type);
842 type = build_complex_type(type);
844 return this->make_type(type);
847 // Get a pointer type.
849 Btype*
850 Gcc_backend::pointer_type(Btype* to_type)
852 tree to_type_tree = to_type->get_tree();
853 if (to_type_tree == error_mark_node)
854 return this->error_type();
855 tree type = build_pointer_type(to_type_tree);
856 return this->make_type(type);
859 // Make a function type.
861 Btype*
862 Gcc_backend::function_type(const Btyped_identifier& receiver,
863 const std::vector<Btyped_identifier>& parameters,
864 const std::vector<Btyped_identifier>& results,
865 Btype* result_struct,
866 Location)
868 tree args = NULL_TREE;
869 tree* pp = &args;
870 if (receiver.btype != NULL)
872 tree t = receiver.btype->get_tree();
873 if (t == error_mark_node)
874 return this->error_type();
875 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
876 pp = &TREE_CHAIN(*pp);
879 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
880 p != parameters.end();
881 ++p)
883 tree t = p->btype->get_tree();
884 if (t == error_mark_node)
885 return this->error_type();
886 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
887 pp = &TREE_CHAIN(*pp);
890 // Varargs is handled entirely at the Go level. When converted to
891 // GENERIC functions are not varargs.
892 *pp = void_list_node;
894 tree result;
895 if (results.empty())
896 result = void_type_node;
897 else if (results.size() == 1)
898 result = results.front().btype->get_tree();
899 else
901 gcc_assert(result_struct != NULL);
902 result = result_struct->get_tree();
904 if (result == error_mark_node)
905 return this->error_type();
907 // The libffi library can not represent a zero-sized object. To
908 // avoid causing confusion on 32-bit SPARC, we treat a function that
909 // returns a zero-sized value as returning void. That should do no
910 // harm since there is no actual value to be returned. See
911 // https://gcc.gnu.org/PR72814 for details.
912 if (result != void_type_node && int_size_in_bytes(result) == 0)
913 result = void_type_node;
915 tree fntype = build_function_type(result, args);
916 if (fntype == error_mark_node)
917 return this->error_type();
919 return this->make_type(build_pointer_type(fntype));
922 // Make a struct type.
924 Btype*
925 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
927 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
930 // Fill in the fields of a struct type.
932 Btype*
933 Gcc_backend::fill_in_struct(Btype* fill,
934 const std::vector<Btyped_identifier>& fields)
936 tree fill_tree = fill->get_tree();
937 tree field_trees = NULL_TREE;
938 tree* pp = &field_trees;
939 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
940 p != fields.end();
941 ++p)
943 tree name_tree = get_identifier_from_string(p->name);
944 tree type_tree = p->btype->get_tree();
945 if (type_tree == error_mark_node)
946 return this->error_type();
947 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
948 type_tree);
949 DECL_CONTEXT(field) = fill_tree;
950 *pp = field;
951 pp = &DECL_CHAIN(field);
953 TYPE_FIELDS(fill_tree) = field_trees;
954 layout_type(fill_tree);
955 return fill;
958 // Make an array type.
960 Btype*
961 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
963 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
964 element_btype, length);
967 // Fill in an array type.
969 Btype*
970 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
971 Bexpression* length)
973 tree element_type_tree = element_type->get_tree();
974 tree length_tree = length->get_tree();
975 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
976 return this->error_type();
978 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
980 length_tree = fold_convert(sizetype, length_tree);
982 // build_index_type takes the maximum index, which is one less than
983 // the length.
984 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
985 length_tree,
986 size_one_node));
988 tree fill_tree = fill->get_tree();
989 TREE_TYPE(fill_tree) = element_type_tree;
990 TYPE_DOMAIN(fill_tree) = index_type_tree;
991 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
992 layout_type(fill_tree);
994 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
995 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
996 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
997 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
998 TYPE_CANONICAL(fill_tree) =
999 build_array_type(TYPE_CANONICAL(element_type_tree),
1000 TYPE_CANONICAL(index_type_tree));
1002 return fill;
1005 // Create a placeholder for a pointer type.
1007 Btype*
1008 Gcc_backend::placeholder_pointer_type(const std::string& name,
1009 Location location, bool)
1011 tree ret = build_distinct_type_copy(ptr_type_node);
1012 if (!name.empty())
1014 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1015 get_identifier_from_string(name),
1016 ret);
1017 TYPE_NAME(ret) = decl;
1019 return this->make_type(ret);
1022 // Set the real target type for a placeholder pointer type.
1024 bool
1025 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
1026 Btype* to_type)
1028 tree pt = placeholder->get_tree();
1029 if (pt == error_mark_node)
1030 return false;
1031 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
1032 tree tt = to_type->get_tree();
1033 if (tt == error_mark_node)
1035 placeholder->set_tree(error_mark_node);
1036 return false;
1038 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
1039 TREE_TYPE(pt) = TREE_TYPE(tt);
1040 if (TYPE_NAME(pt) != NULL_TREE)
1042 // Build the data structure gcc wants to see for a typedef.
1043 tree copy = build_variant_type_copy(pt);
1044 TYPE_NAME(copy) = NULL_TREE;
1045 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
1047 return true;
1050 // Set the real values for a placeholder function type.
1052 bool
1053 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
1055 return this->set_placeholder_pointer_type(placeholder, ft);
1058 // Create a placeholder for a struct type.
1060 Btype*
1061 Gcc_backend::placeholder_struct_type(const std::string& name,
1062 Location location)
1064 tree ret = make_node(RECORD_TYPE);
1065 if (!name.empty())
1067 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1068 get_identifier_from_string(name),
1069 ret);
1070 TYPE_NAME(ret) = decl;
1072 return this->make_type(ret);
1075 // Fill in the fields of a placeholder struct type.
1077 bool
1078 Gcc_backend::set_placeholder_struct_type(
1079 Btype* placeholder,
1080 const std::vector<Btyped_identifier>& fields)
1082 tree t = placeholder->get_tree();
1083 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
1084 Btype* r = this->fill_in_struct(placeholder, fields);
1086 if (TYPE_NAME(t) != NULL_TREE)
1088 // Build the data structure gcc wants to see for a typedef.
1089 tree copy = build_distinct_type_copy(t);
1090 TYPE_NAME(copy) = NULL_TREE;
1091 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1094 return r->get_tree() != error_mark_node;
1097 // Create a placeholder for an array type.
1099 Btype*
1100 Gcc_backend::placeholder_array_type(const std::string& name,
1101 Location location)
1103 tree ret = make_node(ARRAY_TYPE);
1104 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1105 get_identifier_from_string(name),
1106 ret);
1107 TYPE_NAME(ret) = decl;
1108 return this->make_type(ret);
1111 // Fill in the fields of a placeholder array type.
1113 bool
1114 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1115 Btype* element_btype,
1116 Bexpression* length)
1118 tree t = placeholder->get_tree();
1119 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1120 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1122 // Build the data structure gcc wants to see for a typedef.
1123 tree copy = build_distinct_type_copy(t);
1124 TYPE_NAME(copy) = NULL_TREE;
1125 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1127 return r->get_tree() != error_mark_node;
1130 // Return a named version of a type.
1132 Btype*
1133 Gcc_backend::named_type(const std::string& name, Btype* btype,
1134 Location location)
1136 tree type = btype->get_tree();
1137 if (type == error_mark_node)
1138 return this->error_type();
1140 // The middle-end expects a basic type to have a name. In Go every
1141 // basic type will have a name. The first time we see a basic type,
1142 // give it whatever Go name we have at this point.
1143 if (TYPE_NAME(type) == NULL_TREE
1144 && location.gcc_location() == BUILTINS_LOCATION
1145 && (TREE_CODE(type) == INTEGER_TYPE
1146 || TREE_CODE(type) == REAL_TYPE
1147 || TREE_CODE(type) == COMPLEX_TYPE
1148 || TREE_CODE(type) == BOOLEAN_TYPE))
1150 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1151 get_identifier_from_string(name),
1152 type);
1153 TYPE_NAME(type) = decl;
1154 return this->make_type(type);
1157 tree copy = build_variant_type_copy(type);
1158 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1159 get_identifier_from_string(name),
1160 copy);
1161 DECL_ORIGINAL_TYPE(decl) = type;
1162 TYPE_NAME(copy) = decl;
1163 return this->make_type(copy);
1166 // Return a pointer type used as a marker for a circular type.
1168 Btype*
1169 Gcc_backend::circular_pointer_type(Btype*, bool)
1171 return this->make_type(ptr_type_node);
1174 // Return whether we might be looking at a circular type.
1176 bool
1177 Gcc_backend::is_circular_pointer_type(Btype* btype)
1179 return btype->get_tree() == ptr_type_node;
1182 // Return the size of a type.
1184 int64_t
1185 Gcc_backend::type_size(Btype* btype)
1187 tree t = btype->get_tree();
1188 if (t == error_mark_node)
1189 return 1;
1190 t = TYPE_SIZE_UNIT(t);
1191 gcc_assert(tree_fits_uhwi_p (t));
1192 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1193 int64_t ret = static_cast<int64_t>(val_wide);
1194 if (ret < 0 || static_cast<unsigned HOST_WIDE_INT>(ret) != val_wide)
1195 return -1;
1196 return ret;
1199 // Return the alignment of a type.
1201 int64_t
1202 Gcc_backend::type_alignment(Btype* btype)
1204 tree t = btype->get_tree();
1205 if (t == error_mark_node)
1206 return 1;
1207 return TYPE_ALIGN_UNIT(t);
1210 // Return the alignment of a struct field of type BTYPE.
1212 int64_t
1213 Gcc_backend::type_field_alignment(Btype* btype)
1215 tree t = btype->get_tree();
1216 if (t == error_mark_node)
1217 return 1;
1218 return go_field_alignment(t);
1221 // Return the offset of a field in a struct.
1223 int64_t
1224 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1226 tree struct_tree = btype->get_tree();
1227 if (struct_tree == error_mark_node)
1228 return 0;
1229 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1230 tree field = TYPE_FIELDS(struct_tree);
1231 for (; index > 0; --index)
1233 field = DECL_CHAIN(field);
1234 gcc_assert(field != NULL_TREE);
1236 HOST_WIDE_INT offset_wide = int_byte_position(field);
1237 int64_t ret = static_cast<int64_t>(offset_wide);
1238 gcc_assert(ret == offset_wide);
1239 return ret;
1242 // Return the zero value for a type.
1244 Bexpression*
1245 Gcc_backend::zero_expression(Btype* btype)
1247 tree t = btype->get_tree();
1248 tree ret;
1249 if (t == error_mark_node)
1250 ret = error_mark_node;
1251 else
1252 ret = build_zero_cst(t);
1253 return this->make_expression(ret);
1256 // An expression that references a variable.
1258 Bexpression*
1259 Gcc_backend::var_expression(Bvariable* var, Location location)
1261 tree ret = var->get_tree(location);
1262 if (ret == error_mark_node)
1263 return this->error_expression();
1264 return this->make_expression(ret);
1267 // An expression that indirectly references an expression.
1269 Bexpression*
1270 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1271 bool known_valid, Location location)
1273 tree expr_tree = expr->get_tree();
1274 tree type_tree = btype->get_tree();
1275 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1276 return this->error_expression();
1278 // If the type of EXPR is a recursive pointer type, then we
1279 // need to insert a cast before indirecting.
1280 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1281 if (VOID_TYPE_P(target_type_tree))
1282 expr_tree = fold_convert_loc(location.gcc_location(),
1283 build_pointer_type(type_tree), expr_tree);
1285 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1286 expr_tree);
1287 if (known_valid)
1288 TREE_THIS_NOTRAP(ret) = 1;
1289 return this->make_expression(ret);
1292 // Return an expression that declares a constant named NAME with the
1293 // constant value VAL in BTYPE.
1295 Bexpression*
1296 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1297 Bexpression* val, Location location)
1299 tree type_tree = btype->get_tree();
1300 tree const_val = val->get_tree();
1301 if (type_tree == error_mark_node || const_val == error_mark_node)
1302 return this->error_expression();
1304 tree name_tree = get_identifier_from_string(name);
1305 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1306 type_tree);
1307 DECL_INITIAL(decl) = const_val;
1308 TREE_CONSTANT(decl) = 1;
1309 TREE_READONLY(decl) = 1;
1311 go_preserve_from_gc(decl);
1312 return this->make_expression(decl);
1315 // Return a typed value as a constant integer.
1317 Bexpression*
1318 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1320 tree t = btype->get_tree();
1321 if (t == error_mark_node)
1322 return this->error_expression();
1324 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1325 return this->make_expression(ret);
1328 // Return a typed value as a constant floating-point number.
1330 Bexpression*
1331 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1333 tree t = btype->get_tree();
1334 tree ret;
1335 if (t == error_mark_node)
1336 return this->error_expression();
1338 REAL_VALUE_TYPE r1;
1339 real_from_mpfr(&r1, val, t, GMP_RNDN);
1340 REAL_VALUE_TYPE r2;
1341 real_convert(&r2, TYPE_MODE(t), &r1);
1342 ret = build_real(t, r2);
1343 return this->make_expression(ret);
1346 // Return a typed real and imaginary value as a constant complex number.
1348 Bexpression*
1349 Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
1351 tree t = btype->get_tree();
1352 tree ret;
1353 if (t == error_mark_node)
1354 return this->error_expression();
1356 REAL_VALUE_TYPE r1;
1357 real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
1358 REAL_VALUE_TYPE r2;
1359 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1361 REAL_VALUE_TYPE r3;
1362 real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
1363 REAL_VALUE_TYPE r4;
1364 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1366 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1367 build_real(TREE_TYPE(t), r4));
1368 return this->make_expression(ret);
1371 // Make a constant string expression.
1373 Bexpression*
1374 Gcc_backend::string_constant_expression(const std::string& val)
1376 tree index_type = build_index_type(size_int(val.length()));
1377 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1378 TYPE_QUAL_CONST);
1379 tree string_type = build_array_type(const_char_type, index_type);
1380 TYPE_STRING_FLAG(string_type) = 1;
1381 tree string_val = build_string(val.length(), val.data());
1382 TREE_TYPE(string_val) = string_type;
1384 return this->make_expression(string_val);
1387 // Make a constant boolean expression.
1389 Bexpression*
1390 Gcc_backend::boolean_constant_expression(bool val)
1392 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1393 return this->make_expression(bool_cst);
1396 // Return the real part of a complex expression.
1398 Bexpression*
1399 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1401 tree complex_tree = bcomplex->get_tree();
1402 if (complex_tree == error_mark_node)
1403 return this->error_expression();
1404 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1405 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1406 TREE_TYPE(TREE_TYPE(complex_tree)),
1407 complex_tree);
1408 return this->make_expression(ret);
1411 // Return the imaginary part of a complex expression.
1413 Bexpression*
1414 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1416 tree complex_tree = bcomplex->get_tree();
1417 if (complex_tree == error_mark_node)
1418 return this->error_expression();
1419 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1420 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1421 TREE_TYPE(TREE_TYPE(complex_tree)),
1422 complex_tree);
1423 return this->make_expression(ret);
1426 // Make a complex expression given its real and imaginary parts.
1428 Bexpression*
1429 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1430 Location location)
1432 tree real_tree = breal->get_tree();
1433 tree imag_tree = bimag->get_tree();
1434 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1435 return this->error_expression();
1436 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1437 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1438 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1439 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1440 build_complex_type(TREE_TYPE(real_tree)),
1441 real_tree, imag_tree);
1442 return this->make_expression(ret);
1445 // An expression that converts an expression to a different type.
1447 Bexpression*
1448 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1449 Location location)
1451 tree type_tree = type->get_tree();
1452 tree expr_tree = expr->get_tree();
1453 if (type_tree == error_mark_node
1454 || expr_tree == error_mark_node
1455 || TREE_TYPE(expr_tree) == error_mark_node)
1456 return this->error_expression();
1458 tree ret;
1459 if (this->type_size(type) == 0)
1461 // Do not convert zero-sized types.
1462 ret = expr_tree;
1464 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1465 ret = fold(convert_to_integer(type_tree, expr_tree));
1466 else if (TREE_CODE(type_tree) == REAL_TYPE)
1467 ret = fold(convert_to_real(type_tree, expr_tree));
1468 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1469 ret = fold(convert_to_complex(type_tree, expr_tree));
1470 else if (TREE_CODE(type_tree) == POINTER_TYPE
1471 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1472 ret = fold(convert_to_pointer(type_tree, expr_tree));
1473 else if (TREE_CODE(type_tree) == RECORD_TYPE
1474 || TREE_CODE(type_tree) == ARRAY_TYPE)
1475 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1476 type_tree, expr_tree);
1477 else
1478 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1480 return this->make_expression(ret);
1483 // Get the address of a function.
1485 Bexpression*
1486 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1488 tree func = bfunc->get_tree();
1489 if (func == error_mark_node)
1490 return this->error_expression();
1492 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1493 return this->make_expression(ret);
1496 // Get the address of an expression.
1498 Bexpression*
1499 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1501 tree expr = bexpr->get_tree();
1502 if (expr == error_mark_node)
1503 return this->error_expression();
1505 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1506 return this->make_expression(ret);
1509 // Return an expression for the field at INDEX in BSTRUCT.
1511 Bexpression*
1512 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1513 Location location)
1515 tree struct_tree = bstruct->get_tree();
1516 if (struct_tree == error_mark_node
1517 || TREE_TYPE(struct_tree) == error_mark_node)
1518 return this->error_expression();
1519 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1520 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1521 if (field == NULL_TREE)
1523 // This can happen for a type which refers to itself indirectly
1524 // and then turns out to be erroneous.
1525 return this->error_expression();
1527 for (unsigned int i = index; i > 0; --i)
1529 field = DECL_CHAIN(field);
1530 gcc_assert(field != NULL_TREE);
1532 if (TREE_TYPE(field) == error_mark_node)
1533 return this->error_expression();
1534 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1535 TREE_TYPE(field), struct_tree, field,
1536 NULL_TREE);
1537 if (TREE_CONSTANT(struct_tree))
1538 TREE_CONSTANT(ret) = 1;
1539 return this->make_expression(ret);
1542 // Return an expression that executes BSTAT before BEXPR.
1544 Bexpression*
1545 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1546 Location location)
1548 tree stat = bstat->get_tree();
1549 tree expr = bexpr->get_tree();
1550 if (stat == error_mark_node || expr == error_mark_node)
1551 return this->error_expression();
1552 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1553 TREE_TYPE(expr), stat, expr);
1554 return this->make_expression(ret);
1557 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1558 // ELSE_EXPR otherwise.
1560 Bexpression*
1561 Gcc_backend::conditional_expression(Bfunction*, Btype* btype,
1562 Bexpression* condition,
1563 Bexpression* then_expr,
1564 Bexpression* else_expr, Location location)
1566 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1567 tree cond_tree = condition->get_tree();
1568 tree then_tree = then_expr->get_tree();
1569 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1570 if (type_tree == error_mark_node
1571 || cond_tree == error_mark_node
1572 || then_tree == error_mark_node
1573 || else_tree == error_mark_node)
1574 return this->error_expression();
1575 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1576 cond_tree, then_tree, else_tree);
1577 return this->make_expression(ret);
1580 // Return an expression for the unary operation OP EXPR.
1582 Bexpression*
1583 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1585 tree expr_tree = expr->get_tree();
1586 if (expr_tree == error_mark_node
1587 || TREE_TYPE(expr_tree) == error_mark_node)
1588 return this->error_expression();
1590 tree type_tree = TREE_TYPE(expr_tree);
1591 enum tree_code code;
1592 switch (op)
1594 case OPERATOR_MINUS:
1596 tree computed_type = excess_precision_type(type_tree);
1597 if (computed_type != NULL_TREE)
1599 expr_tree = convert(computed_type, expr_tree);
1600 type_tree = computed_type;
1602 code = NEGATE_EXPR;
1603 break;
1605 case OPERATOR_NOT:
1606 code = TRUTH_NOT_EXPR;
1607 break;
1608 case OPERATOR_XOR:
1609 code = BIT_NOT_EXPR;
1610 break;
1611 default:
1612 gcc_unreachable();
1613 break;
1616 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1617 expr_tree);
1618 return this->make_expression(ret);
1621 // Convert a gofrontend operator to an equivalent tree_code.
1623 static enum tree_code
1624 operator_to_tree_code(Operator op, tree type)
1626 enum tree_code code;
1627 switch (op)
1629 case OPERATOR_EQEQ:
1630 code = EQ_EXPR;
1631 break;
1632 case OPERATOR_NOTEQ:
1633 code = NE_EXPR;
1634 break;
1635 case OPERATOR_LT:
1636 code = LT_EXPR;
1637 break;
1638 case OPERATOR_LE:
1639 code = LE_EXPR;
1640 break;
1641 case OPERATOR_GT:
1642 code = GT_EXPR;
1643 break;
1644 case OPERATOR_GE:
1645 code = GE_EXPR;
1646 break;
1647 case OPERATOR_OROR:
1648 code = TRUTH_ORIF_EXPR;
1649 break;
1650 case OPERATOR_ANDAND:
1651 code = TRUTH_ANDIF_EXPR;
1652 break;
1653 case OPERATOR_PLUS:
1654 code = PLUS_EXPR;
1655 break;
1656 case OPERATOR_MINUS:
1657 code = MINUS_EXPR;
1658 break;
1659 case OPERATOR_OR:
1660 code = BIT_IOR_EXPR;
1661 break;
1662 case OPERATOR_XOR:
1663 code = BIT_XOR_EXPR;
1664 break;
1665 case OPERATOR_MULT:
1666 code = MULT_EXPR;
1667 break;
1668 case OPERATOR_DIV:
1669 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1670 code = RDIV_EXPR;
1671 else
1672 code = TRUNC_DIV_EXPR;
1673 break;
1674 case OPERATOR_MOD:
1675 code = TRUNC_MOD_EXPR;
1676 break;
1677 case OPERATOR_LSHIFT:
1678 code = LSHIFT_EXPR;
1679 break;
1680 case OPERATOR_RSHIFT:
1681 code = RSHIFT_EXPR;
1682 break;
1683 case OPERATOR_AND:
1684 code = BIT_AND_EXPR;
1685 break;
1686 case OPERATOR_BITCLEAR:
1687 code = BIT_AND_EXPR;
1688 break;
1689 default:
1690 gcc_unreachable();
1693 return code;
1696 // Return an expression for the binary operation LEFT OP RIGHT.
1698 Bexpression*
1699 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1700 Bexpression* right, Location location)
1702 tree left_tree = left->get_tree();
1703 tree right_tree = right->get_tree();
1704 if (left_tree == error_mark_node
1705 || right_tree == error_mark_node)
1706 return this->error_expression();
1707 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1709 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1710 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1711 tree computed_type = excess_precision_type(type_tree);
1712 if (computed_type != NULL_TREE)
1714 left_tree = convert(computed_type, left_tree);
1715 right_tree = convert(computed_type, right_tree);
1716 type_tree = computed_type;
1719 // For comparison operators, the resulting type should be boolean.
1720 switch (op)
1722 case OPERATOR_EQEQ:
1723 case OPERATOR_NOTEQ:
1724 case OPERATOR_LT:
1725 case OPERATOR_LE:
1726 case OPERATOR_GT:
1727 case OPERATOR_GE:
1728 type_tree = boolean_type_node;
1729 break;
1730 default:
1731 break;
1734 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1735 left_tree, right_tree);
1736 return this->make_expression(ret);
1739 // Return an expression that constructs BTYPE with VALS.
1741 Bexpression*
1742 Gcc_backend::constructor_expression(Btype* btype,
1743 const std::vector<Bexpression*>& vals,
1744 Location location)
1746 tree type_tree = btype->get_tree();
1747 if (type_tree == error_mark_node)
1748 return this->error_expression();
1750 vec<constructor_elt, va_gc> *init;
1751 vec_alloc(init, vals.size());
1753 tree sink = NULL_TREE;
1754 bool is_constant = true;
1755 tree field = TYPE_FIELDS(type_tree);
1756 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1757 p != vals.end();
1758 ++p, field = DECL_CHAIN(field))
1760 gcc_assert(field != NULL_TREE);
1761 tree val = (*p)->get_tree();
1762 if (TREE_TYPE(field) == error_mark_node
1763 || val == error_mark_node
1764 || TREE_TYPE(val) == error_mark_node)
1765 return this->error_expression();
1767 if (int_size_in_bytes(TREE_TYPE(field)) == 0)
1769 // GIMPLE cannot represent indices of zero-sized types so
1770 // trying to construct a map with zero-sized keys might lead
1771 // to errors. Instead, we evaluate each expression that
1772 // would have been added as a map element for its
1773 // side-effects and construct an empty map.
1774 append_to_statement_list(val, &sink);
1775 continue;
1778 constructor_elt empty = {NULL, NULL};
1779 constructor_elt* elt = init->quick_push(empty);
1780 elt->index = field;
1781 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
1782 val);
1783 if (!TREE_CONSTANT(elt->value))
1784 is_constant = false;
1786 gcc_assert(field == NULL_TREE);
1787 tree ret = build_constructor(type_tree, init);
1788 if (is_constant)
1789 TREE_CONSTANT(ret) = 1;
1790 if (sink != NULL_TREE)
1791 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1792 type_tree, sink, ret);
1793 return this->make_expression(ret);
1796 Bexpression*
1797 Gcc_backend::array_constructor_expression(
1798 Btype* array_btype, const std::vector<unsigned long>& indexes,
1799 const std::vector<Bexpression*>& vals, Location location)
1801 tree type_tree = array_btype->get_tree();
1802 if (type_tree == error_mark_node)
1803 return this->error_expression();
1805 gcc_assert(indexes.size() == vals.size());
1807 tree element_type = TREE_TYPE(type_tree);
1808 HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
1809 vec<constructor_elt, va_gc> *init;
1810 vec_alloc(init, element_size == 0 ? 0 : vals.size());
1812 tree sink = NULL_TREE;
1813 bool is_constant = true;
1814 for (size_t i = 0; i < vals.size(); ++i)
1816 tree index = size_int(indexes[i]);
1817 tree val = (vals[i])->get_tree();
1819 if (index == error_mark_node
1820 || val == error_mark_node)
1821 return this->error_expression();
1823 if (element_size == 0)
1825 // GIMPLE cannot represent arrays of zero-sized types so trying
1826 // to construct an array of zero-sized values might lead to errors.
1827 // Instead, we evaluate each expression that would have been added as
1828 // an array value for its side-effects and construct an empty array.
1829 append_to_statement_list(val, &sink);
1830 continue;
1833 if (!TREE_CONSTANT(val))
1834 is_constant = false;
1836 constructor_elt empty = {NULL, NULL};
1837 constructor_elt* elt = init->quick_push(empty);
1838 elt->index = index;
1839 elt->value = val;
1842 tree ret = build_constructor(type_tree, init);
1843 if (is_constant)
1844 TREE_CONSTANT(ret) = 1;
1845 if (sink != NULL_TREE)
1846 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1847 type_tree, sink, ret);
1848 return this->make_expression(ret);
1851 // Return an expression for the address of BASE[INDEX].
1853 Bexpression*
1854 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1855 Location location)
1857 tree base_tree = base->get_tree();
1858 tree index_tree = index->get_tree();
1859 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1860 if (base_tree == error_mark_node
1861 || TREE_TYPE(base_tree) == error_mark_node
1862 || index_tree == error_mark_node
1863 || element_type_tree == error_mark_node)
1864 return this->error_expression();
1866 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1867 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1868 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1869 index_tree, element_size);
1870 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1871 TREE_TYPE(base_tree), base_tree, offset);
1872 return this->make_expression(ptr);
1875 // Return an expression representing ARRAY[INDEX]
1877 Bexpression*
1878 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1879 Location location)
1881 tree array_tree = array->get_tree();
1882 tree index_tree = index->get_tree();
1883 if (array_tree == error_mark_node
1884 || TREE_TYPE(array_tree) == error_mark_node
1885 || index_tree == error_mark_node)
1886 return this->error_expression();
1888 tree ret = build4_loc(location.gcc_location(), ARRAY_REF,
1889 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1890 index_tree, NULL_TREE, NULL_TREE);
1891 return this->make_expression(ret);
1894 // Create an expression for a call to FN_EXPR with FN_ARGS.
1895 Bexpression*
1896 Gcc_backend::call_expression(Bfunction*, // containing fcn for call
1897 Bexpression* fn_expr,
1898 const std::vector<Bexpression*>& fn_args,
1899 Bexpression* chain_expr,
1900 Location location)
1902 tree fn = fn_expr->get_tree();
1903 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1904 return this->error_expression();
1906 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1907 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1909 size_t nargs = fn_args.size();
1910 tree* args = nargs == 0 ? NULL : new tree[nargs];
1911 for (size_t i = 0; i < nargs; ++i)
1913 args[i] = fn_args.at(i)->get_tree();
1914 if (args[i] == error_mark_node)
1915 return this->error_expression();
1918 tree fndecl = fn;
1919 if (TREE_CODE(fndecl) == ADDR_EXPR)
1920 fndecl = TREE_OPERAND(fndecl, 0);
1922 // This is to support builtin math functions when using 80387 math.
1923 tree excess_type = NULL_TREE;
1924 if (optimize
1925 && TREE_CODE(fndecl) == FUNCTION_DECL
1926 && DECL_IS_BUILTIN(fndecl)
1927 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
1928 && nargs > 0
1929 && ((SCALAR_FLOAT_TYPE_P(rettype)
1930 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1931 || (COMPLEX_FLOAT_TYPE_P(rettype)
1932 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1934 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1935 if (excess_type != NULL_TREE)
1937 tree excess_fndecl = mathfn_built_in(excess_type,
1938 DECL_FUNCTION_CODE(fndecl));
1939 if (excess_fndecl == NULL_TREE)
1940 excess_type = NULL_TREE;
1941 else
1943 fn = build_fold_addr_expr_loc(location.gcc_location(),
1944 excess_fndecl);
1945 for (size_t i = 0; i < nargs; ++i)
1947 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1948 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1949 args[i] = ::convert(excess_type, args[i]);
1955 tree ret =
1956 build_call_array_loc(location.gcc_location(),
1957 excess_type != NULL_TREE ? excess_type : rettype,
1958 fn, nargs, args);
1960 if (chain_expr)
1961 CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree();
1963 if (excess_type != NULL_TREE)
1965 // Calling convert here can undo our excess precision change.
1966 // That may or may not be a bug in convert_to_real.
1967 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1970 delete[] args;
1971 return this->make_expression(ret);
1974 // Return an expression that allocates SIZE bytes on the stack.
1976 Bexpression*
1977 Gcc_backend::stack_allocation_expression(int64_t size, Location location)
1979 tree alloca = builtin_decl_explicit(BUILT_IN_ALLOCA);
1980 tree size_tree = build_int_cst(integer_type_node, size);
1981 tree ret = build_call_expr_loc(location.gcc_location(), alloca, 1, size_tree);
1982 tree memset = builtin_decl_explicit(BUILT_IN_MEMSET);
1983 ret = build_call_expr_loc(location.gcc_location(), memset, 3,
1984 ret, integer_zero_node, size_tree);
1985 return this->make_expression(ret);
1988 // An expression as a statement.
1990 Bstatement*
1991 Gcc_backend::expression_statement(Bfunction*, Bexpression* expr)
1993 return this->make_statement(expr->get_tree());
1996 // Variable initialization.
1998 Bstatement*
1999 Gcc_backend::init_statement(Bfunction*, Bvariable* var, Bexpression* init)
2001 tree var_tree = var->get_decl();
2002 tree init_tree = init->get_tree();
2003 if (var_tree == error_mark_node || init_tree == error_mark_node)
2004 return this->error_statement();
2005 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
2007 // To avoid problems with GNU ld, we don't make zero-sized
2008 // externally visible variables. That might lead us to doing an
2009 // initialization of a zero-sized expression to a non-zero sized
2010 // variable, or vice-versa. Avoid crashes by omitting the
2011 // initializer. Such initializations don't mean anything anyhow.
2012 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
2013 && init_tree != NULL_TREE
2014 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
2016 DECL_INITIAL(var_tree) = init_tree;
2017 init_tree = NULL_TREE;
2020 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
2021 void_type_node, var_tree);
2022 if (init_tree != NULL_TREE)
2023 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
2024 void_type_node, init_tree, ret);
2026 return this->make_statement(ret);
2029 // Assignment.
2031 Bstatement*
2032 Gcc_backend::assignment_statement(Bfunction* bfn, Bexpression* lhs,
2033 Bexpression* rhs, Location location)
2035 tree lhs_tree = lhs->get_tree();
2036 tree rhs_tree = rhs->get_tree();
2037 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
2038 return this->error_statement();
2040 // To avoid problems with GNU ld, we don't make zero-sized
2041 // externally visible variables. That might lead us to doing an
2042 // assignment of a zero-sized expression to a non-zero sized
2043 // expression; avoid crashes here by avoiding assignments of
2044 // zero-sized expressions. Such assignments don't really mean
2045 // anything anyhow.
2046 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
2047 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
2048 return this->compound_statement(this->expression_statement(bfn, lhs),
2049 this->expression_statement(bfn, rhs));
2051 // Sometimes the same unnamed Go type can be created multiple times
2052 // and thus have multiple tree representations. Make sure this does
2053 // not confuse the middle-end.
2054 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
2056 tree lhs_type_tree = TREE_TYPE(lhs_tree);
2057 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
2058 if (POINTER_TYPE_P(lhs_type_tree)
2059 || INTEGRAL_TYPE_P(lhs_type_tree)
2060 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
2061 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
2062 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
2063 rhs_tree);
2064 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
2065 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
2067 gcc_assert(int_size_in_bytes(lhs_type_tree)
2068 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
2069 rhs_tree = fold_build1_loc(location.gcc_location(),
2070 VIEW_CONVERT_EXPR,
2071 lhs_type_tree, rhs_tree);
2075 return this->make_statement(fold_build2_loc(location.gcc_location(),
2076 MODIFY_EXPR,
2077 void_type_node,
2078 lhs_tree, rhs_tree));
2081 // Return.
2083 Bstatement*
2084 Gcc_backend::return_statement(Bfunction* bfunction,
2085 const std::vector<Bexpression*>& vals,
2086 Location location)
2088 tree fntree = bfunction->get_tree();
2089 if (fntree == error_mark_node)
2090 return this->error_statement();
2091 tree result = DECL_RESULT(fntree);
2092 if (result == error_mark_node)
2093 return this->error_statement();
2095 // If the result size is zero bytes, we have set the function type
2096 // to have a result type of void, so don't return anything.
2097 // See the function_type method.
2098 tree res_type = TREE_TYPE(result);
2099 if (res_type == void_type_node || int_size_in_bytes(res_type) == 0)
2101 tree stmt_list = NULL_TREE;
2102 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2103 p != vals.end();
2104 p++)
2106 tree val = (*p)->get_tree();
2107 if (val == error_mark_node)
2108 return this->error_statement();
2109 append_to_statement_list(val, &stmt_list);
2111 tree ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2112 void_type_node, NULL_TREE);
2113 append_to_statement_list(ret, &stmt_list);
2114 return this->make_statement(stmt_list);
2117 tree ret;
2118 if (vals.empty())
2119 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
2120 NULL_TREE);
2121 else if (vals.size() == 1)
2123 tree val = vals.front()->get_tree();
2124 if (val == error_mark_node)
2125 return this->error_statement();
2126 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2127 void_type_node, result,
2128 vals.front()->get_tree());
2129 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2130 void_type_node, set);
2132 else
2134 // To return multiple values, copy the values into a temporary
2135 // variable of the right structure type, and then assign the
2136 // temporary variable to the DECL_RESULT in the return
2137 // statement.
2138 tree stmt_list = NULL_TREE;
2139 tree rettype = TREE_TYPE(result);
2141 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2142 push_struct_function(fntree);
2143 else
2144 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2145 tree rettmp = create_tmp_var(rettype, "RESULT");
2146 pop_cfun();
2148 tree field = TYPE_FIELDS(rettype);
2149 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2150 p != vals.end();
2151 p++, field = DECL_CHAIN(field))
2153 gcc_assert(field != NULL_TREE);
2154 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
2155 TREE_TYPE(field), rettmp, field,
2156 NULL_TREE);
2157 tree val = (*p)->get_tree();
2158 if (val == error_mark_node)
2159 return this->error_statement();
2160 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2161 void_type_node,
2162 ref, (*p)->get_tree());
2163 append_to_statement_list(set, &stmt_list);
2165 gcc_assert(field == NULL_TREE);
2166 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2167 void_type_node,
2168 result, rettmp);
2169 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2170 void_type_node, set);
2171 append_to_statement_list(ret_expr, &stmt_list);
2172 ret = stmt_list;
2174 return this->make_statement(ret);
2177 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2178 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2179 // NULL, it will always be executed. This is used for handling defers in Go
2180 // functions. In C++, the resulting code is of this form:
2181 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2183 Bstatement*
2184 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2185 Bstatement* except_stmt,
2186 Bstatement* finally_stmt,
2187 Location location)
2189 tree stat_tree = bstat->get_tree();
2190 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2191 tree finally_tree = finally_stmt == NULL
2192 ? NULL_TREE
2193 : finally_stmt->get_tree();
2195 if (stat_tree == error_mark_node
2196 || except_tree == error_mark_node
2197 || finally_tree == error_mark_node)
2198 return this->error_statement();
2200 if (except_tree != NULL_TREE)
2201 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2202 void_type_node, stat_tree,
2203 build2_loc(location.gcc_location(), CATCH_EXPR,
2204 void_type_node, NULL, except_tree));
2205 if (finally_tree != NULL_TREE)
2206 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2207 void_type_node, stat_tree, finally_tree);
2208 return this->make_statement(stat_tree);
2211 // If.
2213 Bstatement*
2214 Gcc_backend::if_statement(Bfunction*, Bexpression* condition,
2215 Bblock* then_block, Bblock* else_block,
2216 Location location)
2218 tree cond_tree = condition->get_tree();
2219 tree then_tree = then_block->get_tree();
2220 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2221 if (cond_tree == error_mark_node
2222 || then_tree == error_mark_node
2223 || else_tree == error_mark_node)
2224 return this->error_statement();
2225 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2226 cond_tree, then_tree, else_tree);
2227 return this->make_statement(ret);
2230 // Switch.
2232 Bstatement*
2233 Gcc_backend::switch_statement(
2234 Bfunction* function,
2235 Bexpression* value,
2236 const std::vector<std::vector<Bexpression*> >& cases,
2237 const std::vector<Bstatement*>& statements,
2238 Location switch_location)
2240 gcc_assert(cases.size() == statements.size());
2242 tree decl = function->get_tree();
2243 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2244 push_struct_function(decl);
2245 else
2246 push_cfun(DECL_STRUCT_FUNCTION(decl));
2248 tree stmt_list = NULL_TREE;
2249 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2250 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2251 ps != statements.end();
2252 ++ps, ++pc)
2254 if (pc->empty())
2256 source_location loc = (*ps != NULL
2257 ? EXPR_LOCATION((*ps)->get_tree())
2258 : UNKNOWN_LOCATION);
2259 tree label = create_artificial_label(loc);
2260 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2261 append_to_statement_list(c, &stmt_list);
2263 else
2265 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2266 pcv != pc->end();
2267 ++pcv)
2269 tree t = (*pcv)->get_tree();
2270 if (t == error_mark_node)
2271 return this->error_statement();
2272 source_location loc = EXPR_LOCATION(t);
2273 tree label = create_artificial_label(loc);
2274 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2275 append_to_statement_list(c, &stmt_list);
2279 if (*ps != NULL)
2281 tree t = (*ps)->get_tree();
2282 if (t == error_mark_node)
2283 return this->error_statement();
2284 append_to_statement_list(t, &stmt_list);
2287 pop_cfun();
2289 tree tv = value->get_tree();
2290 if (tv == error_mark_node)
2291 return this->error_statement();
2292 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
2293 NULL_TREE, tv, stmt_list, NULL_TREE);
2294 return this->make_statement(t);
2297 // Pair of statements.
2299 Bstatement*
2300 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2302 tree stmt_list = NULL_TREE;
2303 tree t = s1->get_tree();
2304 if (t == error_mark_node)
2305 return this->error_statement();
2306 append_to_statement_list(t, &stmt_list);
2307 t = s2->get_tree();
2308 if (t == error_mark_node)
2309 return this->error_statement();
2310 append_to_statement_list(t, &stmt_list);
2312 // If neither statement has any side effects, stmt_list can be NULL
2313 // at this point.
2314 if (stmt_list == NULL_TREE)
2315 stmt_list = integer_zero_node;
2317 return this->make_statement(stmt_list);
2320 // List of statements.
2322 Bstatement*
2323 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2325 tree stmt_list = NULL_TREE;
2326 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2327 p != statements.end();
2328 ++p)
2330 tree t = (*p)->get_tree();
2331 if (t == error_mark_node)
2332 return this->error_statement();
2333 append_to_statement_list(t, &stmt_list);
2335 return this->make_statement(stmt_list);
2338 // Make a block. For some reason gcc uses a dual structure for
2339 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2340 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2341 // the Bblock.
2343 Bblock*
2344 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2345 const std::vector<Bvariable*>& vars,
2346 Location start_location,
2347 Location)
2349 tree block_tree = make_node(BLOCK);
2350 if (enclosing == NULL)
2352 tree fndecl = function->get_tree();
2353 gcc_assert(fndecl != NULL_TREE);
2355 // We may have already created a block for local variables when
2356 // we take the address of a parameter.
2357 if (DECL_INITIAL(fndecl) == NULL_TREE)
2359 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2360 DECL_INITIAL(fndecl) = block_tree;
2362 else
2364 tree superblock_tree = DECL_INITIAL(fndecl);
2365 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2366 tree* pp;
2367 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2368 *pp != NULL_TREE;
2369 pp = &BLOCK_CHAIN(*pp))
2371 *pp = block_tree;
2374 else
2376 tree superbind_tree = enclosing->get_tree();
2377 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2378 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2380 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2381 tree* pp;
2382 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2383 *pp != NULL_TREE;
2384 pp = &BLOCK_CHAIN(*pp))
2386 *pp = block_tree;
2389 tree* pp = &BLOCK_VARS(block_tree);
2390 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2391 pv != vars.end();
2392 ++pv)
2394 *pp = (*pv)->get_decl();
2395 if (*pp != error_mark_node)
2396 pp = &DECL_CHAIN(*pp);
2398 *pp = NULL_TREE;
2400 TREE_USED(block_tree) = 1;
2402 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2403 void_type_node, BLOCK_VARS(block_tree),
2404 NULL_TREE, block_tree);
2405 TREE_SIDE_EFFECTS(bind_tree) = 1;
2406 return new Bblock(bind_tree);
2409 // Add statements to a block.
2411 void
2412 Gcc_backend::block_add_statements(Bblock* bblock,
2413 const std::vector<Bstatement*>& statements)
2415 tree stmt_list = NULL_TREE;
2416 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2417 p != statements.end();
2418 ++p)
2420 tree s = (*p)->get_tree();
2421 if (s != error_mark_node)
2422 append_to_statement_list(s, &stmt_list);
2425 tree bind_tree = bblock->get_tree();
2426 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2427 BIND_EXPR_BODY(bind_tree) = stmt_list;
2430 // Return a block as a statement.
2432 Bstatement*
2433 Gcc_backend::block_statement(Bblock* bblock)
2435 tree bind_tree = bblock->get_tree();
2436 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2437 return this->make_statement(bind_tree);
2440 // This is not static because we declare it with GTY(()) in go-c.h.
2441 tree go_non_zero_struct;
2443 // Return a type corresponding to TYPE with non-zero size.
2445 tree
2446 Gcc_backend::non_zero_size_type(tree type)
2448 if (int_size_in_bytes(type) != 0)
2449 return type;
2451 switch (TREE_CODE(type))
2453 case RECORD_TYPE:
2454 if (TYPE_FIELDS(type) != NULL_TREE)
2456 tree ns = make_node(RECORD_TYPE);
2457 tree field_trees = NULL_TREE;
2458 tree *pp = &field_trees;
2459 for (tree field = TYPE_FIELDS(type);
2460 field != NULL_TREE;
2461 field = DECL_CHAIN(field))
2463 tree ft = TREE_TYPE(field);
2464 if (field == TYPE_FIELDS(type))
2465 ft = non_zero_size_type(ft);
2466 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2467 DECL_NAME(field), ft);
2468 DECL_CONTEXT(f) = ns;
2469 *pp = f;
2470 pp = &DECL_CHAIN(f);
2472 TYPE_FIELDS(ns) = field_trees;
2473 layout_type(ns);
2474 return ns;
2477 if (go_non_zero_struct == NULL_TREE)
2479 type = make_node(RECORD_TYPE);
2480 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2481 get_identifier("dummy"),
2482 boolean_type_node);
2483 DECL_CONTEXT(field) = type;
2484 TYPE_FIELDS(type) = field;
2485 layout_type(type);
2486 go_non_zero_struct = type;
2488 return go_non_zero_struct;
2490 case ARRAY_TYPE:
2492 tree element_type = non_zero_size_type(TREE_TYPE(type));
2493 return build_array_type_nelts(element_type, 1);
2496 default:
2497 gcc_unreachable();
2500 gcc_unreachable();
2503 // Make a global variable.
2505 Bvariable*
2506 Gcc_backend::global_variable(const std::string& var_name,
2507 const std::string& asm_name,
2508 Btype* btype,
2509 bool is_external,
2510 bool is_hidden,
2511 bool in_unique_section,
2512 Location location)
2514 tree type_tree = btype->get_tree();
2515 if (type_tree == error_mark_node)
2516 return this->error_variable();
2518 // The GNU linker does not like dynamic variables with zero size.
2519 tree orig_type_tree = type_tree;
2520 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2521 type_tree = this->non_zero_size_type(type_tree);
2523 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2524 get_identifier_from_string(var_name),
2525 type_tree);
2526 if (is_external)
2527 DECL_EXTERNAL(decl) = 1;
2528 else
2529 TREE_STATIC(decl) = 1;
2530 if (!is_hidden)
2532 TREE_PUBLIC(decl) = 1;
2533 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2535 else
2537 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2540 TREE_USED(decl) = 1;
2542 if (in_unique_section)
2543 resolve_unique_section (decl, 0, 1);
2545 go_preserve_from_gc(decl);
2547 return new Bvariable(decl, orig_type_tree);
2550 // Set the initial value of a global variable.
2552 void
2553 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2555 tree expr_tree = expr->get_tree();
2556 if (expr_tree == error_mark_node)
2557 return;
2558 gcc_assert(TREE_CONSTANT(expr_tree));
2559 tree var_decl = var->get_decl();
2560 if (var_decl == error_mark_node)
2561 return;
2562 DECL_INITIAL(var_decl) = expr_tree;
2564 // If this variable goes in a unique section, it may need to go into
2565 // a different one now that DECL_INITIAL is set.
2566 if (symtab_node::get(var_decl)
2567 && symtab_node::get(var_decl)->implicit_section)
2569 set_decl_section_name (var_decl, NULL);
2570 resolve_unique_section (var_decl,
2571 compute_reloc_for_constant (expr_tree),
2576 // Make a local variable.
2578 Bvariable*
2579 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2580 Btype* btype, bool is_address_taken,
2581 Location location)
2583 tree type_tree = btype->get_tree();
2584 if (type_tree == error_mark_node)
2585 return this->error_variable();
2586 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2587 get_identifier_from_string(name),
2588 type_tree);
2589 DECL_CONTEXT(decl) = function->get_tree();
2590 TREE_USED(decl) = 1;
2591 if (is_address_taken)
2592 TREE_ADDRESSABLE(decl) = 1;
2593 go_preserve_from_gc(decl);
2594 return new Bvariable(decl);
2597 // Make a function parameter variable.
2599 Bvariable*
2600 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2601 Btype* btype, bool is_address_taken,
2602 Location location)
2604 tree type_tree = btype->get_tree();
2605 if (type_tree == error_mark_node)
2606 return this->error_variable();
2607 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2608 get_identifier_from_string(name),
2609 type_tree);
2610 DECL_CONTEXT(decl) = function->get_tree();
2611 DECL_ARG_TYPE(decl) = type_tree;
2612 TREE_USED(decl) = 1;
2613 if (is_address_taken)
2614 TREE_ADDRESSABLE(decl) = 1;
2615 go_preserve_from_gc(decl);
2616 return new Bvariable(decl);
2619 // Make a static chain variable.
2621 Bvariable*
2622 Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name,
2623 Btype* btype, Location location)
2625 tree type_tree = btype->get_tree();
2626 if (type_tree == error_mark_node)
2627 return this->error_variable();
2628 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2629 get_identifier_from_string(name), type_tree);
2630 tree fndecl = function->get_tree();
2631 DECL_CONTEXT(decl) = fndecl;
2632 DECL_ARG_TYPE(decl) = type_tree;
2633 TREE_USED(decl) = 1;
2634 DECL_ARTIFICIAL(decl) = 1;
2635 DECL_IGNORED_P(decl) = 1;
2636 TREE_READONLY(decl) = 1;
2638 struct function *f = DECL_STRUCT_FUNCTION(fndecl);
2639 if (f == NULL)
2641 push_struct_function(fndecl);
2642 pop_cfun();
2643 f = DECL_STRUCT_FUNCTION(fndecl);
2645 gcc_assert(f->static_chain_decl == NULL);
2646 f->static_chain_decl = decl;
2647 DECL_STATIC_CHAIN(fndecl) = 1;
2649 go_preserve_from_gc(decl);
2650 return new Bvariable(decl);
2653 // Make a temporary variable.
2655 Bvariable*
2656 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2657 Btype* btype, Bexpression* binit,
2658 bool is_address_taken,
2659 Location location,
2660 Bstatement** pstatement)
2662 gcc_assert(function != NULL);
2663 tree decl = function->get_tree();
2664 tree type_tree = btype->get_tree();
2665 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2666 if (type_tree == error_mark_node
2667 || init_tree == error_mark_node
2668 || decl == error_mark_node)
2670 *pstatement = this->error_statement();
2671 return this->error_variable();
2674 tree var;
2675 // We can only use create_tmp_var if the type is not addressable.
2676 if (!TREE_ADDRESSABLE(type_tree))
2678 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2679 push_struct_function(decl);
2680 else
2681 push_cfun(DECL_STRUCT_FUNCTION(decl));
2683 var = create_tmp_var(type_tree, "GOTMP");
2684 pop_cfun();
2686 else
2688 gcc_assert(bblock != NULL);
2689 var = build_decl(location.gcc_location(), VAR_DECL,
2690 create_tmp_var_name("GOTMP"),
2691 type_tree);
2692 DECL_ARTIFICIAL(var) = 1;
2693 DECL_IGNORED_P(var) = 1;
2694 TREE_USED(var) = 1;
2695 DECL_CONTEXT(var) = decl;
2697 // We have to add this variable to the BLOCK and the BIND_EXPR.
2698 tree bind_tree = bblock->get_tree();
2699 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2700 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2701 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2702 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2703 BLOCK_VARS(block_tree) = var;
2704 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2707 if (this->type_size(btype) != 0 && init_tree != NULL_TREE)
2708 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
2709 init_tree);
2711 if (is_address_taken)
2712 TREE_ADDRESSABLE(var) = 1;
2714 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2715 DECL_EXPR,
2716 void_type_node, var));
2718 // Don't initialize VAR with BINIT, but still evaluate BINIT for
2719 // its side effects.
2720 if (this->type_size(btype) == 0 && init_tree != NULL_TREE)
2721 *pstatement =
2722 this->compound_statement(this->expression_statement(function, binit),
2723 *pstatement);
2725 return new Bvariable(var);
2728 // Create an implicit variable that is compiler-defined. This is used when
2729 // generating GC root variables and storing the values of a slice initializer.
2731 Bvariable*
2732 Gcc_backend::implicit_variable(const std::string& name,
2733 const std::string& asm_name,
2734 Btype* type, bool is_hidden, bool is_constant,
2735 bool is_common, int64_t alignment)
2737 tree type_tree = type->get_tree();
2738 if (type_tree == error_mark_node)
2739 return this->error_variable();
2741 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2742 get_identifier_from_string(name), type_tree);
2743 DECL_EXTERNAL(decl) = 0;
2744 TREE_PUBLIC(decl) = !is_hidden;
2745 TREE_STATIC(decl) = 1;
2746 TREE_USED(decl) = 1;
2747 DECL_ARTIFICIAL(decl) = 1;
2748 if (is_common)
2750 DECL_COMMON(decl) = 1;
2752 // When the initializer for one implicit_variable refers to another,
2753 // it needs to know the visibility of the referenced struct so that
2754 // compute_reloc_for_constant will return the right value. On many
2755 // systems calling make_decl_one_only will mark the decl as weak,
2756 // which will change the return value of compute_reloc_for_constant.
2757 // We can't reliably call make_decl_one_only yet, because we don't
2758 // yet know the initializer. This issue doesn't arise in C because
2759 // Go initializers, unlike C initializers, can be indirectly
2760 // recursive. To ensure that compute_reloc_for_constant computes
2761 // the right value if some other initializer refers to this one, we
2762 // mark this symbol as weak here. We undo that below in
2763 // immutable_struct_set_init before calling mark_decl_one_only.
2764 DECL_WEAK(decl) = 1;
2766 if (is_constant)
2768 TREE_READONLY(decl) = 1;
2769 TREE_CONSTANT(decl) = 1;
2771 if (alignment != 0)
2773 SET_DECL_ALIGN(decl, alignment * BITS_PER_UNIT);
2774 DECL_USER_ALIGN(decl) = 1;
2776 if (! asm_name.empty())
2777 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2779 go_preserve_from_gc(decl);
2780 return new Bvariable(decl);
2783 // Set the initalizer for a variable created by implicit_variable.
2784 // This is where we finish compiling the variable.
2786 void
2787 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
2788 Btype*, bool, bool, bool is_common,
2789 Bexpression* init)
2791 tree decl = var->get_decl();
2792 tree init_tree;
2793 if (init == NULL)
2794 init_tree = NULL_TREE;
2795 else
2796 init_tree = init->get_tree();
2797 if (decl == error_mark_node || init_tree == error_mark_node)
2798 return;
2800 DECL_INITIAL(decl) = init_tree;
2802 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2803 // See the comment where DECL_WEAK is set in implicit_variable.
2804 if (is_common)
2806 DECL_WEAK(decl) = 0;
2807 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2810 resolve_unique_section(decl, 2, 1);
2812 rest_of_decl_compilation(decl, 1, 0);
2815 // Return a reference to an implicit variable defined in another package.
2817 Bvariable*
2818 Gcc_backend::implicit_variable_reference(const std::string& name,
2819 const std::string& asm_name,
2820 Btype* btype)
2822 tree type_tree = btype->get_tree();
2823 if (type_tree == error_mark_node)
2824 return this->error_variable();
2826 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2827 get_identifier_from_string(name), type_tree);
2828 DECL_EXTERNAL(decl) = 1;
2829 TREE_PUBLIC(decl) = 1;
2830 TREE_STATIC(decl) = 0;
2831 DECL_ARTIFICIAL(decl) = 1;
2832 if (! asm_name.empty())
2833 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2834 go_preserve_from_gc(decl);
2835 return new Bvariable(decl);
2838 // Create a named immutable initialized data structure.
2840 Bvariable*
2841 Gcc_backend::immutable_struct(const std::string& name,
2842 const std::string& asm_name,
2843 bool is_hidden,
2844 bool is_common, Btype* btype, Location location)
2846 tree type_tree = btype->get_tree();
2847 if (type_tree == error_mark_node)
2848 return this->error_variable();
2849 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2850 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2851 get_identifier_from_string(name),
2852 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2853 TREE_STATIC(decl) = 1;
2854 TREE_USED(decl) = 1;
2855 TREE_READONLY(decl) = 1;
2856 TREE_CONSTANT(decl) = 1;
2857 DECL_ARTIFICIAL(decl) = 1;
2858 if (!is_hidden)
2859 TREE_PUBLIC(decl) = 1;
2860 if (! asm_name.empty())
2861 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2863 // When the initializer for one immutable_struct refers to another,
2864 // it needs to know the visibility of the referenced struct so that
2865 // compute_reloc_for_constant will return the right value. On many
2866 // systems calling make_decl_one_only will mark the decl as weak,
2867 // which will change the return value of compute_reloc_for_constant.
2868 // We can't reliably call make_decl_one_only yet, because we don't
2869 // yet know the initializer. This issue doesn't arise in C because
2870 // Go initializers, unlike C initializers, can be indirectly
2871 // recursive. To ensure that compute_reloc_for_constant computes
2872 // the right value if some other initializer refers to this one, we
2873 // mark this symbol as weak here. We undo that below in
2874 // immutable_struct_set_init before calling mark_decl_one_only.
2875 if (is_common)
2876 DECL_WEAK(decl) = 1;
2878 // We don't call rest_of_decl_compilation until we have the
2879 // initializer.
2881 go_preserve_from_gc(decl);
2882 return new Bvariable(decl);
2885 // Set the initializer for a variable created by immutable_struct.
2886 // This is where we finish compiling the variable.
2888 void
2889 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2890 bool, bool is_common, Btype*, Location,
2891 Bexpression* initializer)
2893 tree decl = var->get_decl();
2894 tree init_tree = initializer->get_tree();
2895 if (decl == error_mark_node || init_tree == error_mark_node)
2896 return;
2898 DECL_INITIAL(decl) = init_tree;
2900 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2901 // See the comment where DECL_WEAK is set in immutable_struct.
2902 if (is_common)
2904 DECL_WEAK(decl) = 0;
2905 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2908 // These variables are often unneeded in the final program, so put
2909 // them in their own section so that linker GC can discard them.
2910 resolve_unique_section(decl,
2911 compute_reloc_for_constant (init_tree),
2914 rest_of_decl_compilation(decl, 1, 0);
2917 // Return a reference to an immutable initialized data structure
2918 // defined in another package.
2920 Bvariable*
2921 Gcc_backend::immutable_struct_reference(const std::string& name,
2922 const std::string& asm_name,
2923 Btype* btype,
2924 Location location)
2926 tree type_tree = btype->get_tree();
2927 if (type_tree == error_mark_node)
2928 return this->error_variable();
2929 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2930 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2931 get_identifier_from_string(name),
2932 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2933 TREE_READONLY(decl) = 1;
2934 TREE_CONSTANT(decl) = 1;
2935 DECL_ARTIFICIAL(decl) = 1;
2936 TREE_PUBLIC(decl) = 1;
2937 DECL_EXTERNAL(decl) = 1;
2938 if (! asm_name.empty())
2939 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2940 go_preserve_from_gc(decl);
2941 return new Bvariable(decl);
2944 // Make a label.
2946 Blabel*
2947 Gcc_backend::label(Bfunction* function, const std::string& name,
2948 Location location)
2950 tree decl;
2951 if (name.empty())
2953 tree func_tree = function->get_tree();
2954 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2955 push_struct_function(func_tree);
2956 else
2957 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2959 decl = create_artificial_label(location.gcc_location());
2961 pop_cfun();
2963 else
2965 tree id = get_identifier_from_string(name);
2966 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
2967 void_type_node);
2968 DECL_CONTEXT(decl) = function->get_tree();
2970 return new Blabel(decl);
2973 // Make a statement which defines a label.
2975 Bstatement*
2976 Gcc_backend::label_definition_statement(Blabel* label)
2978 tree lab = label->get_tree();
2979 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
2980 void_type_node, lab);
2981 return this->make_statement(ret);
2984 // Make a goto statement.
2986 Bstatement*
2987 Gcc_backend::goto_statement(Blabel* label, Location location)
2989 tree lab = label->get_tree();
2990 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
2991 lab);
2992 return this->make_statement(ret);
2995 // Get the address of a label.
2997 Bexpression*
2998 Gcc_backend::label_address(Blabel* label, Location location)
3000 tree lab = label->get_tree();
3001 TREE_USED(lab) = 1;
3002 TREE_ADDRESSABLE(lab) = 1;
3003 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
3004 build_fold_addr_expr_loc(location.gcc_location(),
3005 lab));
3006 return this->make_expression(ret);
3009 // Declare or define a new function.
3011 Bfunction*
3012 Gcc_backend::function(Btype* fntype, const std::string& name,
3013 const std::string& asm_name, bool is_visible,
3014 bool is_declaration, bool is_inlinable,
3015 bool disable_split_stack, bool in_unique_section,
3016 Location location)
3018 tree functype = fntype->get_tree();
3019 if (functype != error_mark_node)
3021 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
3022 functype = TREE_TYPE(functype);
3024 tree id = get_identifier_from_string(name);
3025 if (functype == error_mark_node || id == error_mark_node)
3026 return this->error_function();
3028 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
3029 if (! asm_name.empty())
3030 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
3031 if (is_visible)
3032 TREE_PUBLIC(decl) = 1;
3033 if (is_declaration)
3034 DECL_EXTERNAL(decl) = 1;
3035 else
3037 tree restype = TREE_TYPE(functype);
3038 tree resdecl =
3039 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
3040 DECL_ARTIFICIAL(resdecl) = 1;
3041 DECL_IGNORED_P(resdecl) = 1;
3042 DECL_CONTEXT(resdecl) = decl;
3043 DECL_RESULT(decl) = resdecl;
3045 if (!is_inlinable)
3046 DECL_UNINLINABLE(decl) = 1;
3047 if (disable_split_stack)
3049 tree attr = get_identifier ("no_split_stack");
3050 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
3052 if (in_unique_section)
3053 resolve_unique_section(decl, 0, 1);
3055 go_preserve_from_gc(decl);
3056 return new Bfunction(decl);
3059 // Create a statement that runs all deferred calls for FUNCTION. This should
3060 // be a statement that looks like this in C++:
3061 // finish:
3062 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3064 Bstatement*
3065 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
3066 Bexpression* defer, Location location)
3068 tree undefer_tree = undefer->get_tree();
3069 tree defer_tree = defer->get_tree();
3070 tree fntree = function->get_tree();
3072 if (undefer_tree == error_mark_node
3073 || defer_tree == error_mark_node
3074 || fntree == error_mark_node)
3075 return this->error_statement();
3077 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
3078 push_struct_function(fntree);
3079 else
3080 push_cfun(DECL_STRUCT_FUNCTION(fntree));
3082 tree stmt_list = NULL;
3083 Blabel* blabel = this->label(function, "", location);
3084 Bstatement* label_def = this->label_definition_statement(blabel);
3085 append_to_statement_list(label_def->get_tree(), &stmt_list);
3087 Bstatement* jump_stmt = this->goto_statement(blabel, location);
3088 tree jump = jump_stmt->get_tree();
3089 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
3090 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
3091 tree try_catch =
3092 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
3093 append_to_statement_list(try_catch, &stmt_list);
3094 pop_cfun();
3096 return this->make_statement(stmt_list);
3099 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3100 // This will only be called for a function definition.
3102 bool
3103 Gcc_backend::function_set_parameters(Bfunction* function,
3104 const std::vector<Bvariable*>& param_vars)
3106 tree func_tree = function->get_tree();
3107 if (func_tree == error_mark_node)
3108 return false;
3110 tree params = NULL_TREE;
3111 tree *pp = &params;
3112 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
3113 pv != param_vars.end();
3114 ++pv)
3116 *pp = (*pv)->get_decl();
3117 gcc_assert(*pp != error_mark_node);
3118 pp = &DECL_CHAIN(*pp);
3120 *pp = NULL_TREE;
3121 DECL_ARGUMENTS(func_tree) = params;
3122 return true;
3125 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3127 bool
3128 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
3130 tree func_tree = function->get_tree();
3131 tree code = code_stmt->get_tree();
3133 if (func_tree == error_mark_node || code == error_mark_node)
3134 return false;
3135 DECL_SAVED_TREE(func_tree) = code;
3136 return true;
3139 // Look up a named built-in function in the current backend implementation.
3140 // Returns NULL if no built-in function by that name exists.
3142 Bfunction*
3143 Gcc_backend::lookup_builtin(const std::string& name)
3145 if (this->builtin_functions_.count(name) != 0)
3146 return this->builtin_functions_[name];
3147 return NULL;
3150 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3151 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3152 // emit early debugging information.
3154 void
3155 Gcc_backend::write_global_definitions(
3156 const std::vector<Btype*>& type_decls,
3157 const std::vector<Bexpression*>& constant_decls,
3158 const std::vector<Bfunction*>& function_decls,
3159 const std::vector<Bvariable*>& variable_decls)
3161 size_t count_definitions = type_decls.size() + constant_decls.size()
3162 + function_decls.size() + variable_decls.size();
3164 tree* defs = new tree[count_definitions];
3166 // Convert all non-erroneous declarations into Gimple form.
3167 size_t i = 0;
3168 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
3169 p != variable_decls.end();
3170 ++p)
3172 tree v = (*p)->get_decl();
3173 if (v != error_mark_node)
3175 defs[i] = v;
3176 go_preserve_from_gc(defs[i]);
3177 ++i;
3181 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
3182 p != type_decls.end();
3183 ++p)
3185 tree type_tree = (*p)->get_tree();
3186 if (type_tree != error_mark_node
3187 && IS_TYPE_OR_DECL_P(type_tree))
3189 defs[i] = TYPE_NAME(type_tree);
3190 gcc_assert(defs[i] != NULL);
3191 go_preserve_from_gc(defs[i]);
3192 ++i;
3195 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
3196 p != constant_decls.end();
3197 ++p)
3199 if ((*p)->get_tree() != error_mark_node)
3201 defs[i] = (*p)->get_tree();
3202 go_preserve_from_gc(defs[i]);
3203 ++i;
3206 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
3207 p != function_decls.end();
3208 ++p)
3210 tree decl = (*p)->get_tree();
3211 if (decl != error_mark_node)
3213 go_preserve_from_gc(decl);
3214 gimplify_function_tree(decl);
3215 cgraph_node::finalize_function(decl, true);
3217 defs[i] = decl;
3218 ++i;
3222 // Pass everything back to the middle-end.
3224 wrapup_global_declarations(defs, i);
3226 delete[] defs;
3229 void
3230 Gcc_backend::write_export_data(const char* bytes, unsigned int size)
3232 go_write_export_data(bytes, size);
3236 // Define a builtin function. BCODE is the builtin function code
3237 // defined by builtins.def. NAME is the name of the builtin function.
3238 // LIBNAME is the name of the corresponding library function, and is
3239 // NULL if there isn't one. FNTYPE is the type of the function.
3240 // CONST_P is true if the function has the const attribute.
3241 // NORETURN_P is true if the function has the noreturn attribute.
3243 void
3244 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3245 const char* libname, tree fntype, bool const_p,
3246 bool noreturn_p)
3248 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3249 libname, NULL_TREE);
3250 if (const_p)
3251 TREE_READONLY(decl) = 1;
3252 if (noreturn_p)
3253 TREE_THIS_VOLATILE(decl) = 1;
3254 set_builtin_decl(bcode, decl, true);
3255 this->builtin_functions_[name] = this->make_function(decl);
3256 if (libname != NULL)
3258 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3259 NULL, NULL_TREE);
3260 if (const_p)
3261 TREE_READONLY(decl) = 1;
3262 if (noreturn_p)
3263 TREE_THIS_VOLATILE(decl) = 1;
3264 this->builtin_functions_[libname] = this->make_function(decl);
3268 // Return the backend generator.
3270 Backend*
3271 go_get_backend()
3273 return new Gcc_backend();