Add missing dir to create_testsuite_files script
[official-gcc.git] / gcc / go / go-gcc.cc
blobbe2302932f740233f5d9fde3282b00af5974bda7
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2018 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
5 // This file is part of GCC.
7 // GCC is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3, or (at your option) any later
10 // version.
12 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 #include "go-system.h"
23 // This has to be included outside of extern "C", so we have to
24 // include it here before tree.h includes it later.
25 #include <gmp.h>
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
31 #include "varasm.h"
32 #include "tree-iterator.h"
33 #include "tm.h"
34 #include "function.h"
35 #include "cgraph.h"
36 #include "convert.h"
37 #include "gimple-expr.h"
38 #include "gimplify.h"
39 #include "langhooks.h"
40 #include "toplev.h"
41 #include "output.h"
42 #include "realmpfr.h"
43 #include "builtins.h"
45 #include "go-c.h"
46 #include "go-gcc.h"
48 #include "gogo.h"
49 #include "backend.h"
51 // A class wrapping a tree.
53 class Gcc_tree
55 public:
56 Gcc_tree(tree t)
57 : t_(t)
58 { }
60 tree
61 get_tree() const
62 { return this->t_; }
64 void
65 set_tree(tree t)
66 { this->t_ = t; }
68 private:
69 tree t_;
72 // In gcc, types, expressions, and statements are all trees.
73 class Btype : public Gcc_tree
75 public:
76 Btype(tree t)
77 : Gcc_tree(t)
78 { }
81 class Bexpression : public Gcc_tree
83 public:
84 Bexpression(tree t)
85 : Gcc_tree(t)
86 { }
89 class Bstatement : public Gcc_tree
91 public:
92 Bstatement(tree t)
93 : Gcc_tree(t)
94 { }
97 class Bfunction : public Gcc_tree
99 public:
100 Bfunction(tree t)
101 : Gcc_tree(t)
105 class Bblock : public Gcc_tree
107 public:
108 Bblock(tree t)
109 : Gcc_tree(t)
113 class Blabel : public Gcc_tree
115 public:
116 Blabel(tree t)
117 : Gcc_tree(t)
121 // Bvariable is a bit more complicated, because of zero-sized types.
122 // The GNU linker does not permit dynamic variables with zero size.
123 // When we see such a variable, we generate a version of the type with
124 // non-zero size. However, when referring to the global variable, we
125 // want an expression of zero size; otherwise, if, say, the global
126 // variable is passed to a function, we will be passing a
127 // non-zero-sized value to a zero-sized value, which can lead to a
128 // miscompilation.
130 class Bvariable
132 public:
133 Bvariable(tree t)
134 : t_(t), orig_type_(NULL)
137 Bvariable(tree t, tree orig_type)
138 : t_(t), orig_type_(orig_type)
141 // Get the tree for use as an expression.
142 tree
143 get_tree(Location) const;
145 // Get the actual decl;
146 tree
147 get_decl() const
148 { return this->t_; }
150 private:
151 tree t_;
152 tree orig_type_;
155 // Get the tree of a variable for use as an expression. If this is a
156 // zero-sized global, create an expression that refers to the decl but
157 // has zero size.
158 tree
159 Bvariable::get_tree(Location location) const
161 if (this->orig_type_ == NULL
162 || this->t_ == error_mark_node
163 || TREE_TYPE(this->t_) == this->orig_type_)
164 return this->t_;
165 // Return *(orig_type*)&decl. */
166 tree t = build_fold_addr_expr_loc(location.gcc_location(), this->t_);
167 t = fold_build1_loc(location.gcc_location(), NOP_EXPR,
168 build_pointer_type(this->orig_type_), t);
169 return build_fold_indirect_ref_loc(location.gcc_location(), t);
172 // This file implements the interface between the Go frontend proper
173 // and the gcc IR. This implements specific instantiations of
174 // abstract classes defined by the Go frontend proper. The Go
175 // frontend proper class methods of these classes to generate the
176 // backend representation.
178 class Gcc_backend : public Backend
180 public:
181 Gcc_backend();
183 // Types.
185 Btype*
186 error_type()
187 { return this->make_type(error_mark_node); }
189 Btype*
190 void_type()
191 { return this->make_type(void_type_node); }
193 Btype*
194 bool_type()
195 { return this->make_type(boolean_type_node); }
197 Btype*
198 integer_type(bool, int);
200 Btype*
201 float_type(int);
203 Btype*
204 complex_type(int);
206 Btype*
207 pointer_type(Btype*);
209 Btype*
210 function_type(const Btyped_identifier&,
211 const std::vector<Btyped_identifier>&,
212 const std::vector<Btyped_identifier>&,
213 Btype*,
214 const Location);
216 Btype*
217 struct_type(const std::vector<Btyped_identifier>&);
219 Btype*
220 array_type(Btype*, Bexpression*);
222 Btype*
223 placeholder_pointer_type(const std::string&, Location, bool);
225 bool
226 set_placeholder_pointer_type(Btype*, Btype*);
228 bool
229 set_placeholder_function_type(Btype*, Btype*);
231 Btype*
232 placeholder_struct_type(const std::string&, Location);
234 bool
235 set_placeholder_struct_type(Btype* placeholder,
236 const std::vector<Btyped_identifier>&);
238 Btype*
239 placeholder_array_type(const std::string&, Location);
241 bool
242 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
244 Btype*
245 named_type(const std::string&, Btype*, Location);
247 Btype*
248 circular_pointer_type(Btype*, bool);
250 bool
251 is_circular_pointer_type(Btype*);
253 int64_t
254 type_size(Btype*);
256 int64_t
257 type_alignment(Btype*);
259 int64_t
260 type_field_alignment(Btype*);
262 int64_t
263 type_field_offset(Btype*, size_t index);
265 // Expressions.
267 Bexpression*
268 zero_expression(Btype*);
270 Bexpression*
271 error_expression()
272 { return this->make_expression(error_mark_node); }
274 Bexpression*
275 nil_pointer_expression()
276 { return this->make_expression(null_pointer_node); }
278 Bexpression*
279 var_expression(Bvariable* var, Location);
281 Bexpression*
282 indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location);
284 Bexpression*
285 named_constant_expression(Btype* btype, const std::string& name,
286 Bexpression* val, Location);
288 Bexpression*
289 integer_constant_expression(Btype* btype, mpz_t val);
291 Bexpression*
292 float_constant_expression(Btype* btype, mpfr_t val);
294 Bexpression*
295 complex_constant_expression(Btype* btype, mpc_t val);
297 Bexpression*
298 string_constant_expression(const std::string& val);
300 Bexpression*
301 boolean_constant_expression(bool val);
303 Bexpression*
304 real_part_expression(Bexpression* bcomplex, Location);
306 Bexpression*
307 imag_part_expression(Bexpression* bcomplex, Location);
309 Bexpression*
310 complex_expression(Bexpression* breal, Bexpression* bimag, Location);
312 Bexpression*
313 convert_expression(Btype* type, Bexpression* expr, Location);
315 Bexpression*
316 function_code_expression(Bfunction*, Location);
318 Bexpression*
319 address_expression(Bexpression*, Location);
321 Bexpression*
322 struct_field_expression(Bexpression*, size_t, Location);
324 Bexpression*
325 compound_expression(Bstatement*, Bexpression*, Location);
327 Bexpression*
328 conditional_expression(Bfunction*, Btype*, Bexpression*, Bexpression*,
329 Bexpression*, Location);
331 Bexpression*
332 unary_expression(Operator, Bexpression*, Location);
334 Bexpression*
335 binary_expression(Operator, Bexpression*, Bexpression*, Location);
337 Bexpression*
338 constructor_expression(Btype*, const std::vector<Bexpression*>&, Location);
340 Bexpression*
341 array_constructor_expression(Btype*, const std::vector<unsigned long>&,
342 const std::vector<Bexpression*>&, Location);
344 Bexpression*
345 pointer_offset_expression(Bexpression* base, Bexpression* offset, Location);
347 Bexpression*
348 array_index_expression(Bexpression* array, Bexpression* index, Location);
350 Bexpression*
351 call_expression(Bfunction* caller, Bexpression* fn,
352 const std::vector<Bexpression*>& args,
353 Bexpression* static_chain, Location);
355 // Statements.
357 Bstatement*
358 error_statement()
359 { return this->make_statement(error_mark_node); }
361 Bstatement*
362 expression_statement(Bfunction*, Bexpression*);
364 Bstatement*
365 init_statement(Bfunction*, Bvariable* var, Bexpression* init);
367 Bstatement*
368 assignment_statement(Bfunction*, Bexpression* lhs, Bexpression* rhs,
369 Location);
371 Bstatement*
372 return_statement(Bfunction*, const std::vector<Bexpression*>&,
373 Location);
375 Bstatement*
376 if_statement(Bfunction*, Bexpression* condition, Bblock* then_block,
377 Bblock* else_block, Location);
379 Bstatement*
380 switch_statement(Bfunction* function, Bexpression* value,
381 const std::vector<std::vector<Bexpression*> >& cases,
382 const std::vector<Bstatement*>& statements,
383 Location);
385 Bstatement*
386 compound_statement(Bstatement*, Bstatement*);
388 Bstatement*
389 statement_list(const std::vector<Bstatement*>&);
391 Bstatement*
392 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
393 Bstatement* finally_stmt, Location);
395 // Blocks.
397 Bblock*
398 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
399 Location, Location);
401 void
402 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
404 Bstatement*
405 block_statement(Bblock*);
407 // Variables.
409 Bvariable*
410 error_variable()
411 { return new Bvariable(error_mark_node); }
413 Bvariable*
414 global_variable(const std::string& var_name,
415 const std::string& asm_name,
416 Btype* btype,
417 bool is_external,
418 bool is_hidden,
419 bool in_unique_section,
420 Location location);
422 void
423 global_variable_set_init(Bvariable*, Bexpression*);
425 Bvariable*
426 local_variable(Bfunction*, const std::string&, Btype*, Bvariable*, bool,
427 Location);
429 Bvariable*
430 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
431 Location);
433 Bvariable*
434 static_chain_variable(Bfunction*, const std::string&, Btype*, Location);
436 Bvariable*
437 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
438 Location, Bstatement**);
440 Bvariable*
441 implicit_variable(const std::string&, const std::string&, Btype*,
442 bool, bool, bool, int64_t);
444 void
445 implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
446 bool, bool, bool, Bexpression*);
448 Bvariable*
449 implicit_variable_reference(const std::string&, const std::string&, Btype*);
451 Bvariable*
452 immutable_struct(const std::string&, const std::string&,
453 bool, bool, Btype*, Location);
455 void
456 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
457 Location, Bexpression*);
459 Bvariable*
460 immutable_struct_reference(const std::string&, const std::string&,
461 Btype*, Location);
463 // Labels.
465 Blabel*
466 label(Bfunction*, const std::string& name, Location);
468 Bstatement*
469 label_definition_statement(Blabel*);
471 Bstatement*
472 goto_statement(Blabel*, Location);
474 Bexpression*
475 label_address(Blabel*, Location);
477 // Functions.
479 Bfunction*
480 error_function()
481 { return this->make_function(error_mark_node); }
483 Bfunction*
484 function(Btype* fntype, const std::string& name, const std::string& asm_name,
485 unsigned int flags, Location);
487 Bstatement*
488 function_defer_statement(Bfunction* function, Bexpression* undefer,
489 Bexpression* defer, Location);
491 bool
492 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
494 bool
495 function_set_body(Bfunction* function, Bstatement* code_stmt);
497 Bfunction*
498 lookup_builtin(const std::string&);
500 void
501 write_global_definitions(const std::vector<Btype*>&,
502 const std::vector<Bexpression*>&,
503 const std::vector<Bfunction*>&,
504 const std::vector<Bvariable*>&);
506 void
507 write_export_data(const char* bytes, unsigned int size);
510 private:
511 // Make a Bexpression from a tree.
512 Bexpression*
513 make_expression(tree t)
514 { return new Bexpression(t); }
516 // Make a Bstatement from a tree.
517 Bstatement*
518 make_statement(tree t)
519 { return new Bstatement(t); }
521 // Make a Btype from a tree.
522 Btype*
523 make_type(tree t)
524 { return new Btype(t); }
526 Bfunction*
527 make_function(tree t)
528 { return new Bfunction(t); }
530 Btype*
531 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
533 Btype*
534 fill_in_array(Btype*, Btype*, Bexpression*);
536 tree
537 non_zero_size_type(tree);
539 tree
540 convert_tree(tree, tree, Location);
542 private:
543 void
544 define_builtin(built_in_function bcode, const char* name, const char* libname,
545 tree fntype, bool const_p, bool noreturn_p);
547 // A mapping of the GCC built-ins exposed to GCCGo.
548 std::map<std::string, Bfunction*> builtin_functions_;
551 // A helper function to create a GCC identifier from a C++ string.
553 static inline tree
554 get_identifier_from_string(const std::string& str)
556 return get_identifier_with_length(str.data(), str.length());
559 // Define the built-in functions that are exposed to GCCGo.
561 Gcc_backend::Gcc_backend()
563 /* We need to define the fetch_and_add functions, since we use them
564 for ++ and --. */
565 tree t = this->integer_type(true, BITS_PER_UNIT)->get_tree();
566 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
567 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
568 NULL, build_function_type_list(t, p, t, NULL_TREE),
569 false, false);
571 t = this->integer_type(true, BITS_PER_UNIT * 2)->get_tree();
572 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
573 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
574 NULL, build_function_type_list(t, p, t, NULL_TREE),
575 false, false);
577 t = this->integer_type(true, BITS_PER_UNIT * 4)->get_tree();
578 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
579 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
580 NULL, build_function_type_list(t, p, t, NULL_TREE),
581 false, false);
583 t = this->integer_type(true, BITS_PER_UNIT * 8)->get_tree();
584 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
585 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
586 NULL, build_function_type_list(t, p, t, NULL_TREE),
587 false, false);
589 // We use __builtin_expect for magic import functions.
590 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
591 build_function_type_list(long_integer_type_node,
592 long_integer_type_node,
593 long_integer_type_node,
594 NULL_TREE),
595 true, false);
597 // We use __builtin_memcmp for struct comparisons.
598 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
599 build_function_type_list(integer_type_node,
600 const_ptr_type_node,
601 const_ptr_type_node,
602 size_type_node,
603 NULL_TREE),
604 false, false);
606 // Used by runtime/internal/sys.
607 this->define_builtin(BUILT_IN_CTZ, "__builtin_ctz", "ctz",
608 build_function_type_list(integer_type_node,
609 unsigned_type_node,
610 NULL_TREE),
611 true, false);
612 this->define_builtin(BUILT_IN_CTZLL, "__builtin_ctzll", "ctzll",
613 build_function_type_list(integer_type_node,
614 long_long_unsigned_type_node,
615 NULL_TREE),
616 true, false);
617 this->define_builtin(BUILT_IN_BSWAP32, "__builtin_bswap32", "bswap32",
618 build_function_type_list(uint32_type_node,
619 uint32_type_node,
620 NULL_TREE),
621 true, false);
622 this->define_builtin(BUILT_IN_BSWAP64, "__builtin_bswap64", "bswap64",
623 build_function_type_list(uint64_type_node,
624 uint64_type_node,
625 NULL_TREE),
626 true, false);
628 // We provide some functions for the math library.
629 tree math_function_type = build_function_type_list(double_type_node,
630 double_type_node,
631 NULL_TREE);
632 tree math_function_type_long =
633 build_function_type_list(long_double_type_node, long_double_type_node,
634 NULL_TREE);
635 tree math_function_type_two = build_function_type_list(double_type_node,
636 double_type_node,
637 double_type_node,
638 NULL_TREE);
639 tree math_function_type_long_two =
640 build_function_type_list(long_double_type_node, long_double_type_node,
641 long_double_type_node, NULL_TREE);
642 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
643 math_function_type, true, false);
644 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
645 math_function_type_long, true, false);
646 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
647 math_function_type, true, false);
648 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
649 math_function_type_long, true, false);
650 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
651 math_function_type, true, false);
652 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
653 math_function_type_long, true, false);
654 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
655 math_function_type_two, true, false);
656 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
657 math_function_type_long_two, true, false);
658 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
659 math_function_type, true, false);
660 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
661 math_function_type_long, true, false);
662 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
663 math_function_type, true, false);
664 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
665 math_function_type_long, true, false);
666 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
667 math_function_type, true, false);
668 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
669 math_function_type_long, true, false);
670 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
671 math_function_type, true, false);
672 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
673 math_function_type_long, true, false);
674 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
675 math_function_type, true, false);
676 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
677 math_function_type_long, true, false);
678 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
679 math_function_type, true, false);
680 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
681 math_function_type_long, true, false);
682 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
683 math_function_type_two, true, false);
684 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
685 math_function_type_long_two, true, false);
686 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
687 build_function_type_list(double_type_node,
688 double_type_node,
689 integer_type_node,
690 NULL_TREE),
691 true, false);
692 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
693 build_function_type_list(long_double_type_node,
694 long_double_type_node,
695 integer_type_node,
696 NULL_TREE),
697 true, false);
698 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
699 math_function_type, true, false);
700 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
701 math_function_type_long, true, false);
702 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
703 math_function_type, true, false);
704 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
705 math_function_type_long, true, false);
706 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
707 math_function_type, true, false);
708 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
709 math_function_type_long, true, false);
710 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
711 math_function_type, true, false);
712 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
713 math_function_type_long, true, false);
714 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
715 math_function_type, true, false);
716 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
717 math_function_type_long, true, false);
718 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
719 math_function_type, true, false);
720 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
721 math_function_type_long, true, false);
722 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
723 math_function_type, true, false);
724 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
725 math_function_type_long, true, false);
726 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
727 math_function_type, true, false);
728 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
729 math_function_type_long, true, false);
731 // We use __builtin_return_address in the thunk we build for
732 // functions which call recover, and for runtime.getcallerpc.
733 t = build_function_type_list(ptr_type_node, unsigned_type_node, NULL_TREE);
734 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
735 NULL, t, false, false);
737 // The runtime calls __builtin_frame_address for runtime.getcallersp.
738 this->define_builtin(BUILT_IN_FRAME_ADDRESS, "__builtin_frame_address",
739 NULL, t, false, false);
741 // The runtime calls __builtin_extract_return_addr when recording
742 // the address to which a function returns.
743 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR,
744 "__builtin_extract_return_addr", NULL,
745 build_function_type_list(ptr_type_node,
746 ptr_type_node,
747 NULL_TREE),
748 false, false);
750 // The compiler uses __builtin_trap for some exception handling
751 // cases.
752 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
753 build_function_type(void_type_node, void_list_node),
754 false, true);
756 // The runtime uses __builtin_prefetch.
757 this->define_builtin(BUILT_IN_PREFETCH, "__builtin_prefetch", NULL,
758 build_varargs_function_type_list(void_type_node,
759 const_ptr_type_node,
760 NULL_TREE),
761 false, false);
763 // The compiler uses __builtin_unreachable for cases that can not
764 // occur.
765 this->define_builtin(BUILT_IN_UNREACHABLE, "__builtin_unreachable", NULL,
766 build_function_type(void_type_node, void_list_node),
767 true, true);
770 // Get an unnamed integer type.
772 Btype*
773 Gcc_backend::integer_type(bool is_unsigned, int bits)
775 tree type;
776 if (is_unsigned)
778 if (bits == INT_TYPE_SIZE)
779 type = unsigned_type_node;
780 else if (bits == CHAR_TYPE_SIZE)
781 type = unsigned_char_type_node;
782 else if (bits == SHORT_TYPE_SIZE)
783 type = short_unsigned_type_node;
784 else if (bits == LONG_TYPE_SIZE)
785 type = long_unsigned_type_node;
786 else if (bits == LONG_LONG_TYPE_SIZE)
787 type = long_long_unsigned_type_node;
788 else
789 type = make_unsigned_type(bits);
791 else
793 if (bits == INT_TYPE_SIZE)
794 type = integer_type_node;
795 else if (bits == CHAR_TYPE_SIZE)
796 type = signed_char_type_node;
797 else if (bits == SHORT_TYPE_SIZE)
798 type = short_integer_type_node;
799 else if (bits == LONG_TYPE_SIZE)
800 type = long_integer_type_node;
801 else if (bits == LONG_LONG_TYPE_SIZE)
802 type = long_long_integer_type_node;
803 else
804 type = make_signed_type(bits);
806 return this->make_type(type);
809 // Get an unnamed float type.
811 Btype*
812 Gcc_backend::float_type(int bits)
814 tree type;
815 if (bits == FLOAT_TYPE_SIZE)
816 type = float_type_node;
817 else if (bits == DOUBLE_TYPE_SIZE)
818 type = double_type_node;
819 else if (bits == LONG_DOUBLE_TYPE_SIZE)
820 type = long_double_type_node;
821 else
823 type = make_node(REAL_TYPE);
824 TYPE_PRECISION(type) = bits;
825 layout_type(type);
827 return this->make_type(type);
830 // Get an unnamed complex type.
832 Btype*
833 Gcc_backend::complex_type(int bits)
835 tree type;
836 if (bits == FLOAT_TYPE_SIZE * 2)
837 type = complex_float_type_node;
838 else if (bits == DOUBLE_TYPE_SIZE * 2)
839 type = complex_double_type_node;
840 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
841 type = complex_long_double_type_node;
842 else
844 type = make_node(REAL_TYPE);
845 TYPE_PRECISION(type) = bits / 2;
846 layout_type(type);
847 type = build_complex_type(type);
849 return this->make_type(type);
852 // Get a pointer type.
854 Btype*
855 Gcc_backend::pointer_type(Btype* to_type)
857 tree to_type_tree = to_type->get_tree();
858 if (to_type_tree == error_mark_node)
859 return this->error_type();
860 tree type = build_pointer_type(to_type_tree);
861 return this->make_type(type);
864 // Make a function type.
866 Btype*
867 Gcc_backend::function_type(const Btyped_identifier& receiver,
868 const std::vector<Btyped_identifier>& parameters,
869 const std::vector<Btyped_identifier>& results,
870 Btype* result_struct,
871 Location)
873 tree args = NULL_TREE;
874 tree* pp = &args;
875 if (receiver.btype != NULL)
877 tree t = receiver.btype->get_tree();
878 if (t == error_mark_node)
879 return this->error_type();
880 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
881 pp = &TREE_CHAIN(*pp);
884 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
885 p != parameters.end();
886 ++p)
888 tree t = p->btype->get_tree();
889 if (t == error_mark_node)
890 return this->error_type();
891 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
892 pp = &TREE_CHAIN(*pp);
895 // Varargs is handled entirely at the Go level. When converted to
896 // GENERIC functions are not varargs.
897 *pp = void_list_node;
899 tree result;
900 if (results.empty())
901 result = void_type_node;
902 else if (results.size() == 1)
903 result = results.front().btype->get_tree();
904 else
906 gcc_assert(result_struct != NULL);
907 result = result_struct->get_tree();
909 if (result == error_mark_node)
910 return this->error_type();
912 // The libffi library can not represent a zero-sized object. To
913 // avoid causing confusion on 32-bit SPARC, we treat a function that
914 // returns a zero-sized value as returning void. That should do no
915 // harm since there is no actual value to be returned. See
916 // https://gcc.gnu.org/PR72814 for details.
917 if (result != void_type_node && int_size_in_bytes(result) == 0)
918 result = void_type_node;
920 tree fntype = build_function_type(result, args);
921 if (fntype == error_mark_node)
922 return this->error_type();
924 return this->make_type(build_pointer_type(fntype));
927 // Make a struct type.
929 Btype*
930 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
932 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
935 // Fill in the fields of a struct type.
937 Btype*
938 Gcc_backend::fill_in_struct(Btype* fill,
939 const std::vector<Btyped_identifier>& fields)
941 tree fill_tree = fill->get_tree();
942 tree field_trees = NULL_TREE;
943 tree* pp = &field_trees;
944 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
945 p != fields.end();
946 ++p)
948 tree name_tree = get_identifier_from_string(p->name);
949 tree type_tree = p->btype->get_tree();
950 if (type_tree == error_mark_node)
951 return this->error_type();
952 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
953 type_tree);
954 DECL_CONTEXT(field) = fill_tree;
955 *pp = field;
956 pp = &DECL_CHAIN(field);
958 TYPE_FIELDS(fill_tree) = field_trees;
959 layout_type(fill_tree);
961 // Because Go permits converting between named struct types and
962 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
963 // because we don't try to maintain TYPE_CANONICAL for struct types,
964 // we need to tell the middle-end to use structural equality.
965 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
967 return fill;
970 // Make an array type.
972 Btype*
973 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
975 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
976 element_btype, length);
979 // Fill in an array type.
981 Btype*
982 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
983 Bexpression* length)
985 tree element_type_tree = element_type->get_tree();
986 tree length_tree = length->get_tree();
987 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
988 return this->error_type();
990 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
992 length_tree = fold_convert(sizetype, length_tree);
994 // build_index_type takes the maximum index, which is one less than
995 // the length.
996 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
997 length_tree,
998 size_one_node));
1000 tree fill_tree = fill->get_tree();
1001 TREE_TYPE(fill_tree) = element_type_tree;
1002 TYPE_DOMAIN(fill_tree) = index_type_tree;
1003 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
1004 layout_type(fill_tree);
1006 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
1007 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
1008 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
1009 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
1010 TYPE_CANONICAL(fill_tree) =
1011 build_array_type(TYPE_CANONICAL(element_type_tree),
1012 TYPE_CANONICAL(index_type_tree));
1014 return fill;
1017 // Create a placeholder for a pointer type.
1019 Btype*
1020 Gcc_backend::placeholder_pointer_type(const std::string& name,
1021 Location location, bool)
1023 tree ret = build_distinct_type_copy(ptr_type_node);
1024 if (!name.empty())
1026 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1027 get_identifier_from_string(name),
1028 ret);
1029 TYPE_NAME(ret) = decl;
1031 return this->make_type(ret);
1034 // Set the real target type for a placeholder pointer type.
1036 bool
1037 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
1038 Btype* to_type)
1040 tree pt = placeholder->get_tree();
1041 if (pt == error_mark_node)
1042 return false;
1043 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
1044 tree tt = to_type->get_tree();
1045 if (tt == error_mark_node)
1047 placeholder->set_tree(error_mark_node);
1048 return false;
1050 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
1051 TREE_TYPE(pt) = TREE_TYPE(tt);
1052 if (TYPE_NAME(pt) != NULL_TREE)
1054 // Build the data structure gcc wants to see for a typedef.
1055 tree copy = build_variant_type_copy(pt);
1056 TYPE_NAME(copy) = NULL_TREE;
1057 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
1059 return true;
1062 // Set the real values for a placeholder function type.
1064 bool
1065 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
1067 return this->set_placeholder_pointer_type(placeholder, ft);
1070 // Create a placeholder for a struct type.
1072 Btype*
1073 Gcc_backend::placeholder_struct_type(const std::string& name,
1074 Location location)
1076 tree ret = make_node(RECORD_TYPE);
1077 if (!name.empty())
1079 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1080 get_identifier_from_string(name),
1081 ret);
1082 TYPE_NAME(ret) = decl;
1084 return this->make_type(ret);
1087 // Fill in the fields of a placeholder struct type.
1089 bool
1090 Gcc_backend::set_placeholder_struct_type(
1091 Btype* placeholder,
1092 const std::vector<Btyped_identifier>& fields)
1094 tree t = placeholder->get_tree();
1095 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
1096 Btype* r = this->fill_in_struct(placeholder, fields);
1098 if (TYPE_NAME(t) != NULL_TREE)
1100 // Build the data structure gcc wants to see for a typedef.
1101 tree copy = build_variant_type_copy(t);
1102 TYPE_NAME(copy) = NULL_TREE;
1103 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1106 return r->get_tree() != error_mark_node;
1109 // Create a placeholder for an array type.
1111 Btype*
1112 Gcc_backend::placeholder_array_type(const std::string& name,
1113 Location location)
1115 tree ret = make_node(ARRAY_TYPE);
1116 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1117 get_identifier_from_string(name),
1118 ret);
1119 TYPE_NAME(ret) = decl;
1120 return this->make_type(ret);
1123 // Fill in the fields of a placeholder array type.
1125 bool
1126 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1127 Btype* element_btype,
1128 Bexpression* length)
1130 tree t = placeholder->get_tree();
1131 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1132 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1134 // Build the data structure gcc wants to see for a typedef.
1135 tree copy = build_distinct_type_copy(t);
1136 TYPE_NAME(copy) = NULL_TREE;
1137 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1139 return r->get_tree() != error_mark_node;
1142 // Return a named version of a type.
1144 Btype*
1145 Gcc_backend::named_type(const std::string& name, Btype* btype,
1146 Location location)
1148 tree type = btype->get_tree();
1149 if (type == error_mark_node)
1150 return this->error_type();
1152 // The middle-end expects a basic type to have a name. In Go every
1153 // basic type will have a name. The first time we see a basic type,
1154 // give it whatever Go name we have at this point.
1155 if (TYPE_NAME(type) == NULL_TREE
1156 && location.gcc_location() == BUILTINS_LOCATION
1157 && (TREE_CODE(type) == INTEGER_TYPE
1158 || TREE_CODE(type) == REAL_TYPE
1159 || TREE_CODE(type) == COMPLEX_TYPE
1160 || TREE_CODE(type) == BOOLEAN_TYPE))
1162 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1163 get_identifier_from_string(name),
1164 type);
1165 TYPE_NAME(type) = decl;
1166 return this->make_type(type);
1169 tree copy = build_variant_type_copy(type);
1170 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1171 get_identifier_from_string(name),
1172 copy);
1173 DECL_ORIGINAL_TYPE(decl) = type;
1174 TYPE_NAME(copy) = decl;
1175 return this->make_type(copy);
1178 // Return a pointer type used as a marker for a circular type.
1180 Btype*
1181 Gcc_backend::circular_pointer_type(Btype*, bool)
1183 return this->make_type(ptr_type_node);
1186 // Return whether we might be looking at a circular type.
1188 bool
1189 Gcc_backend::is_circular_pointer_type(Btype* btype)
1191 return btype->get_tree() == ptr_type_node;
1194 // Return the size of a type.
1196 int64_t
1197 Gcc_backend::type_size(Btype* btype)
1199 tree t = btype->get_tree();
1200 if (t == error_mark_node)
1201 return 1;
1202 if (t == void_type_node)
1203 return 0;
1204 t = TYPE_SIZE_UNIT(t);
1205 gcc_assert(tree_fits_uhwi_p (t));
1206 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1207 int64_t ret = static_cast<int64_t>(val_wide);
1208 if (ret < 0 || static_cast<unsigned HOST_WIDE_INT>(ret) != val_wide)
1209 return -1;
1210 return ret;
1213 // Return the alignment of a type.
1215 int64_t
1216 Gcc_backend::type_alignment(Btype* btype)
1218 tree t = btype->get_tree();
1219 if (t == error_mark_node)
1220 return 1;
1221 return TYPE_ALIGN_UNIT(t);
1224 // Return the alignment of a struct field of type BTYPE.
1226 int64_t
1227 Gcc_backend::type_field_alignment(Btype* btype)
1229 tree t = btype->get_tree();
1230 if (t == error_mark_node)
1231 return 1;
1232 return go_field_alignment(t);
1235 // Return the offset of a field in a struct.
1237 int64_t
1238 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1240 tree struct_tree = btype->get_tree();
1241 if (struct_tree == error_mark_node)
1242 return 0;
1243 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1244 tree field = TYPE_FIELDS(struct_tree);
1245 for (; index > 0; --index)
1247 field = DECL_CHAIN(field);
1248 gcc_assert(field != NULL_TREE);
1250 HOST_WIDE_INT offset_wide = int_byte_position(field);
1251 int64_t ret = static_cast<int64_t>(offset_wide);
1252 gcc_assert(ret == offset_wide);
1253 return ret;
1256 // Return the zero value for a type.
1258 Bexpression*
1259 Gcc_backend::zero_expression(Btype* btype)
1261 tree t = btype->get_tree();
1262 tree ret;
1263 if (t == error_mark_node)
1264 ret = error_mark_node;
1265 else
1266 ret = build_zero_cst(t);
1267 return this->make_expression(ret);
1270 // An expression that references a variable.
1272 Bexpression*
1273 Gcc_backend::var_expression(Bvariable* var, Location location)
1275 tree ret = var->get_tree(location);
1276 if (ret == error_mark_node)
1277 return this->error_expression();
1278 return this->make_expression(ret);
1281 // An expression that indirectly references an expression.
1283 Bexpression*
1284 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1285 bool known_valid, Location location)
1287 tree expr_tree = expr->get_tree();
1288 tree type_tree = btype->get_tree();
1289 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1290 return this->error_expression();
1292 // If the type of EXPR is a recursive pointer type, then we
1293 // need to insert a cast before indirecting.
1294 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1295 if (VOID_TYPE_P(target_type_tree))
1296 expr_tree = fold_convert_loc(location.gcc_location(),
1297 build_pointer_type(type_tree), expr_tree);
1299 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1300 expr_tree);
1301 if (known_valid)
1302 TREE_THIS_NOTRAP(ret) = 1;
1303 return this->make_expression(ret);
1306 // Return an expression that declares a constant named NAME with the
1307 // constant value VAL in BTYPE.
1309 Bexpression*
1310 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1311 Bexpression* val, Location location)
1313 tree type_tree = btype->get_tree();
1314 tree const_val = val->get_tree();
1315 if (type_tree == error_mark_node || const_val == error_mark_node)
1316 return this->error_expression();
1318 tree name_tree = get_identifier_from_string(name);
1319 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1320 type_tree);
1321 DECL_INITIAL(decl) = const_val;
1322 TREE_CONSTANT(decl) = 1;
1323 TREE_READONLY(decl) = 1;
1325 go_preserve_from_gc(decl);
1326 return this->make_expression(decl);
1329 // Return a typed value as a constant integer.
1331 Bexpression*
1332 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1334 tree t = btype->get_tree();
1335 if (t == error_mark_node)
1336 return this->error_expression();
1338 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1339 return this->make_expression(ret);
1342 // Return a typed value as a constant floating-point number.
1344 Bexpression*
1345 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1347 tree t = btype->get_tree();
1348 tree ret;
1349 if (t == error_mark_node)
1350 return this->error_expression();
1352 REAL_VALUE_TYPE r1;
1353 real_from_mpfr(&r1, val, t, GMP_RNDN);
1354 REAL_VALUE_TYPE r2;
1355 real_convert(&r2, TYPE_MODE(t), &r1);
1356 ret = build_real(t, r2);
1357 return this->make_expression(ret);
1360 // Return a typed real and imaginary value as a constant complex number.
1362 Bexpression*
1363 Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
1365 tree t = btype->get_tree();
1366 tree ret;
1367 if (t == error_mark_node)
1368 return this->error_expression();
1370 REAL_VALUE_TYPE r1;
1371 real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
1372 REAL_VALUE_TYPE r2;
1373 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1375 REAL_VALUE_TYPE r3;
1376 real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
1377 REAL_VALUE_TYPE r4;
1378 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1380 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1381 build_real(TREE_TYPE(t), r4));
1382 return this->make_expression(ret);
1385 // Make a constant string expression.
1387 Bexpression*
1388 Gcc_backend::string_constant_expression(const std::string& val)
1390 tree index_type = build_index_type(size_int(val.length()));
1391 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1392 TYPE_QUAL_CONST);
1393 tree string_type = build_array_type(const_char_type, index_type);
1394 TYPE_STRING_FLAG(string_type) = 1;
1395 tree string_val = build_string(val.length(), val.data());
1396 TREE_TYPE(string_val) = string_type;
1398 return this->make_expression(string_val);
1401 // Make a constant boolean expression.
1403 Bexpression*
1404 Gcc_backend::boolean_constant_expression(bool val)
1406 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1407 return this->make_expression(bool_cst);
1410 // Return the real part of a complex expression.
1412 Bexpression*
1413 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1415 tree complex_tree = bcomplex->get_tree();
1416 if (complex_tree == error_mark_node)
1417 return this->error_expression();
1418 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1419 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1420 TREE_TYPE(TREE_TYPE(complex_tree)),
1421 complex_tree);
1422 return this->make_expression(ret);
1425 // Return the imaginary part of a complex expression.
1427 Bexpression*
1428 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1430 tree complex_tree = bcomplex->get_tree();
1431 if (complex_tree == error_mark_node)
1432 return this->error_expression();
1433 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1434 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1435 TREE_TYPE(TREE_TYPE(complex_tree)),
1436 complex_tree);
1437 return this->make_expression(ret);
1440 // Make a complex expression given its real and imaginary parts.
1442 Bexpression*
1443 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1444 Location location)
1446 tree real_tree = breal->get_tree();
1447 tree imag_tree = bimag->get_tree();
1448 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1449 return this->error_expression();
1450 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1451 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1452 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1453 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1454 build_complex_type(TREE_TYPE(real_tree)),
1455 real_tree, imag_tree);
1456 return this->make_expression(ret);
1459 // An expression that converts an expression to a different type.
1461 Bexpression*
1462 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1463 Location location)
1465 tree type_tree = type->get_tree();
1466 tree expr_tree = expr->get_tree();
1467 if (type_tree == error_mark_node
1468 || expr_tree == error_mark_node
1469 || TREE_TYPE(expr_tree) == error_mark_node)
1470 return this->error_expression();
1472 tree ret;
1473 if (this->type_size(type) == 0
1474 || TREE_TYPE(expr_tree) == void_type_node)
1476 // Do not convert zero-sized types.
1477 ret = expr_tree;
1479 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1480 ret = fold(convert_to_integer(type_tree, expr_tree));
1481 else if (TREE_CODE(type_tree) == REAL_TYPE)
1482 ret = fold(convert_to_real(type_tree, expr_tree));
1483 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1484 ret = fold(convert_to_complex(type_tree, expr_tree));
1485 else if (TREE_CODE(type_tree) == POINTER_TYPE
1486 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1487 ret = fold(convert_to_pointer(type_tree, expr_tree));
1488 else if (TREE_CODE(type_tree) == RECORD_TYPE
1489 || TREE_CODE(type_tree) == ARRAY_TYPE)
1490 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1491 type_tree, expr_tree);
1492 else
1493 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1495 return this->make_expression(ret);
1498 // Get the address of a function.
1500 Bexpression*
1501 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1503 tree func = bfunc->get_tree();
1504 if (func == error_mark_node)
1505 return this->error_expression();
1507 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1508 return this->make_expression(ret);
1511 // Get the address of an expression.
1513 Bexpression*
1514 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1516 tree expr = bexpr->get_tree();
1517 if (expr == error_mark_node)
1518 return this->error_expression();
1520 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1521 return this->make_expression(ret);
1524 // Return an expression for the field at INDEX in BSTRUCT.
1526 Bexpression*
1527 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1528 Location location)
1530 tree struct_tree = bstruct->get_tree();
1531 if (struct_tree == error_mark_node
1532 || TREE_TYPE(struct_tree) == error_mark_node)
1533 return this->error_expression();
1534 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1535 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1536 if (field == NULL_TREE)
1538 // This can happen for a type which refers to itself indirectly
1539 // and then turns out to be erroneous.
1540 return this->error_expression();
1542 for (unsigned int i = index; i > 0; --i)
1544 field = DECL_CHAIN(field);
1545 gcc_assert(field != NULL_TREE);
1547 if (TREE_TYPE(field) == error_mark_node)
1548 return this->error_expression();
1549 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1550 TREE_TYPE(field), struct_tree, field,
1551 NULL_TREE);
1552 if (TREE_CONSTANT(struct_tree))
1553 TREE_CONSTANT(ret) = 1;
1554 return this->make_expression(ret);
1557 // Return an expression that executes BSTAT before BEXPR.
1559 Bexpression*
1560 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1561 Location location)
1563 tree stat = bstat->get_tree();
1564 tree expr = bexpr->get_tree();
1565 if (stat == error_mark_node || expr == error_mark_node)
1566 return this->error_expression();
1567 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1568 TREE_TYPE(expr), stat, expr);
1569 return this->make_expression(ret);
1572 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1573 // ELSE_EXPR otherwise.
1575 Bexpression*
1576 Gcc_backend::conditional_expression(Bfunction*, Btype* btype,
1577 Bexpression* condition,
1578 Bexpression* then_expr,
1579 Bexpression* else_expr, Location location)
1581 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1582 tree cond_tree = condition->get_tree();
1583 tree then_tree = then_expr->get_tree();
1584 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1585 if (type_tree == error_mark_node
1586 || cond_tree == error_mark_node
1587 || then_tree == error_mark_node
1588 || else_tree == error_mark_node)
1589 return this->error_expression();
1590 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1591 cond_tree, then_tree, else_tree);
1592 return this->make_expression(ret);
1595 // Return an expression for the unary operation OP EXPR.
1597 Bexpression*
1598 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1600 tree expr_tree = expr->get_tree();
1601 if (expr_tree == error_mark_node
1602 || TREE_TYPE(expr_tree) == error_mark_node)
1603 return this->error_expression();
1605 tree type_tree = TREE_TYPE(expr_tree);
1606 enum tree_code code;
1607 switch (op)
1609 case OPERATOR_MINUS:
1611 tree computed_type = excess_precision_type(type_tree);
1612 if (computed_type != NULL_TREE)
1614 expr_tree = convert(computed_type, expr_tree);
1615 type_tree = computed_type;
1617 code = NEGATE_EXPR;
1618 break;
1620 case OPERATOR_NOT:
1621 code = TRUTH_NOT_EXPR;
1622 break;
1623 case OPERATOR_XOR:
1624 code = BIT_NOT_EXPR;
1625 break;
1626 default:
1627 gcc_unreachable();
1628 break;
1631 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1632 expr_tree);
1633 return this->make_expression(ret);
1636 // Convert a gofrontend operator to an equivalent tree_code.
1638 static enum tree_code
1639 operator_to_tree_code(Operator op, tree type)
1641 enum tree_code code;
1642 switch (op)
1644 case OPERATOR_EQEQ:
1645 code = EQ_EXPR;
1646 break;
1647 case OPERATOR_NOTEQ:
1648 code = NE_EXPR;
1649 break;
1650 case OPERATOR_LT:
1651 code = LT_EXPR;
1652 break;
1653 case OPERATOR_LE:
1654 code = LE_EXPR;
1655 break;
1656 case OPERATOR_GT:
1657 code = GT_EXPR;
1658 break;
1659 case OPERATOR_GE:
1660 code = GE_EXPR;
1661 break;
1662 case OPERATOR_OROR:
1663 code = TRUTH_ORIF_EXPR;
1664 break;
1665 case OPERATOR_ANDAND:
1666 code = TRUTH_ANDIF_EXPR;
1667 break;
1668 case OPERATOR_PLUS:
1669 code = PLUS_EXPR;
1670 break;
1671 case OPERATOR_MINUS:
1672 code = MINUS_EXPR;
1673 break;
1674 case OPERATOR_OR:
1675 code = BIT_IOR_EXPR;
1676 break;
1677 case OPERATOR_XOR:
1678 code = BIT_XOR_EXPR;
1679 break;
1680 case OPERATOR_MULT:
1681 code = MULT_EXPR;
1682 break;
1683 case OPERATOR_DIV:
1684 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1685 code = RDIV_EXPR;
1686 else
1687 code = TRUNC_DIV_EXPR;
1688 break;
1689 case OPERATOR_MOD:
1690 code = TRUNC_MOD_EXPR;
1691 break;
1692 case OPERATOR_LSHIFT:
1693 code = LSHIFT_EXPR;
1694 break;
1695 case OPERATOR_RSHIFT:
1696 code = RSHIFT_EXPR;
1697 break;
1698 case OPERATOR_AND:
1699 code = BIT_AND_EXPR;
1700 break;
1701 case OPERATOR_BITCLEAR:
1702 code = BIT_AND_EXPR;
1703 break;
1704 default:
1705 gcc_unreachable();
1708 return code;
1711 // Return an expression for the binary operation LEFT OP RIGHT.
1713 Bexpression*
1714 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1715 Bexpression* right, Location location)
1717 tree left_tree = left->get_tree();
1718 tree right_tree = right->get_tree();
1719 if (left_tree == error_mark_node
1720 || right_tree == error_mark_node)
1721 return this->error_expression();
1722 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1724 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1725 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1726 tree computed_type = excess_precision_type(type_tree);
1727 if (computed_type != NULL_TREE)
1729 left_tree = convert(computed_type, left_tree);
1730 right_tree = convert(computed_type, right_tree);
1731 type_tree = computed_type;
1734 // For comparison operators, the resulting type should be boolean.
1735 switch (op)
1737 case OPERATOR_EQEQ:
1738 case OPERATOR_NOTEQ:
1739 case OPERATOR_LT:
1740 case OPERATOR_LE:
1741 case OPERATOR_GT:
1742 case OPERATOR_GE:
1743 type_tree = boolean_type_node;
1744 break;
1745 default:
1746 break;
1749 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1750 left_tree, right_tree);
1751 return this->make_expression(ret);
1754 // Return an expression that constructs BTYPE with VALS.
1756 Bexpression*
1757 Gcc_backend::constructor_expression(Btype* btype,
1758 const std::vector<Bexpression*>& vals,
1759 Location location)
1761 tree type_tree = btype->get_tree();
1762 if (type_tree == error_mark_node)
1763 return this->error_expression();
1765 vec<constructor_elt, va_gc> *init;
1766 vec_alloc(init, vals.size());
1768 tree sink = NULL_TREE;
1769 bool is_constant = true;
1770 tree field = TYPE_FIELDS(type_tree);
1771 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1772 p != vals.end();
1773 ++p, field = DECL_CHAIN(field))
1775 gcc_assert(field != NULL_TREE);
1776 tree val = (*p)->get_tree();
1777 if (TREE_TYPE(field) == error_mark_node
1778 || val == error_mark_node
1779 || TREE_TYPE(val) == error_mark_node)
1780 return this->error_expression();
1782 if (int_size_in_bytes(TREE_TYPE(field)) == 0)
1784 // GIMPLE cannot represent indices of zero-sized types so
1785 // trying to construct a map with zero-sized keys might lead
1786 // to errors. Instead, we evaluate each expression that
1787 // would have been added as a map element for its
1788 // side-effects and construct an empty map.
1789 append_to_statement_list(val, &sink);
1790 continue;
1793 constructor_elt empty = {NULL, NULL};
1794 constructor_elt* elt = init->quick_push(empty);
1795 elt->index = field;
1796 elt->value = this->convert_tree(TREE_TYPE(field), val, location);
1797 if (!TREE_CONSTANT(elt->value))
1798 is_constant = false;
1800 gcc_assert(field == NULL_TREE);
1801 tree ret = build_constructor(type_tree, init);
1802 if (is_constant)
1803 TREE_CONSTANT(ret) = 1;
1804 if (sink != NULL_TREE)
1805 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1806 type_tree, sink, ret);
1807 return this->make_expression(ret);
1810 Bexpression*
1811 Gcc_backend::array_constructor_expression(
1812 Btype* array_btype, const std::vector<unsigned long>& indexes,
1813 const std::vector<Bexpression*>& vals, Location location)
1815 tree type_tree = array_btype->get_tree();
1816 if (type_tree == error_mark_node)
1817 return this->error_expression();
1819 gcc_assert(indexes.size() == vals.size());
1821 tree element_type = TREE_TYPE(type_tree);
1822 HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
1823 vec<constructor_elt, va_gc> *init;
1824 vec_alloc(init, element_size == 0 ? 0 : vals.size());
1826 tree sink = NULL_TREE;
1827 bool is_constant = true;
1828 for (size_t i = 0; i < vals.size(); ++i)
1830 tree index = size_int(indexes[i]);
1831 tree val = (vals[i])->get_tree();
1833 if (index == error_mark_node
1834 || val == error_mark_node)
1835 return this->error_expression();
1837 if (element_size == 0)
1839 // GIMPLE cannot represent arrays of zero-sized types so trying
1840 // to construct an array of zero-sized values might lead to errors.
1841 // Instead, we evaluate each expression that would have been added as
1842 // an array value for its side-effects and construct an empty array.
1843 append_to_statement_list(val, &sink);
1844 continue;
1847 if (!TREE_CONSTANT(val))
1848 is_constant = false;
1850 constructor_elt empty = {NULL, NULL};
1851 constructor_elt* elt = init->quick_push(empty);
1852 elt->index = index;
1853 elt->value = val;
1856 tree ret = build_constructor(type_tree, init);
1857 if (is_constant)
1858 TREE_CONSTANT(ret) = 1;
1859 if (sink != NULL_TREE)
1860 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1861 type_tree, sink, ret);
1862 return this->make_expression(ret);
1865 // Return an expression for the address of BASE[INDEX].
1867 Bexpression*
1868 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1869 Location location)
1871 tree base_tree = base->get_tree();
1872 tree index_tree = index->get_tree();
1873 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1874 if (base_tree == error_mark_node
1875 || TREE_TYPE(base_tree) == error_mark_node
1876 || index_tree == error_mark_node
1877 || element_type_tree == error_mark_node)
1878 return this->error_expression();
1880 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1881 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1882 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1883 index_tree, element_size);
1884 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1885 TREE_TYPE(base_tree), base_tree, offset);
1886 return this->make_expression(ptr);
1889 // Return an expression representing ARRAY[INDEX]
1891 Bexpression*
1892 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1893 Location location)
1895 tree array_tree = array->get_tree();
1896 tree index_tree = index->get_tree();
1897 if (array_tree == error_mark_node
1898 || TREE_TYPE(array_tree) == error_mark_node
1899 || index_tree == error_mark_node)
1900 return this->error_expression();
1902 // A function call that returns a zero sized object will have been
1903 // changed to return void. If we see void here, assume we are
1904 // dealing with a zero sized type and just evaluate the operands.
1905 tree ret;
1906 if (TREE_TYPE(array_tree) != void_type_node)
1907 ret = build4_loc(location.gcc_location(), ARRAY_REF,
1908 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1909 index_tree, NULL_TREE, NULL_TREE);
1910 else
1911 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1912 void_type_node, array_tree, index_tree);
1914 return this->make_expression(ret);
1917 // Create an expression for a call to FN_EXPR with FN_ARGS.
1918 Bexpression*
1919 Gcc_backend::call_expression(Bfunction*, // containing fcn for call
1920 Bexpression* fn_expr,
1921 const std::vector<Bexpression*>& fn_args,
1922 Bexpression* chain_expr,
1923 Location location)
1925 tree fn = fn_expr->get_tree();
1926 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1927 return this->error_expression();
1929 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1930 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1932 size_t nargs = fn_args.size();
1933 tree* args = nargs == 0 ? NULL : new tree[nargs];
1934 for (size_t i = 0; i < nargs; ++i)
1936 args[i] = fn_args.at(i)->get_tree();
1937 if (args[i] == error_mark_node)
1938 return this->error_expression();
1941 tree fndecl = fn;
1942 if (TREE_CODE(fndecl) == ADDR_EXPR)
1943 fndecl = TREE_OPERAND(fndecl, 0);
1945 // This is to support builtin math functions when using 80387 math.
1946 tree excess_type = NULL_TREE;
1947 if (optimize
1948 && TREE_CODE(fndecl) == FUNCTION_DECL
1949 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1950 && DECL_IS_BUILTIN (fndecl)
1951 && nargs > 0
1952 && ((SCALAR_FLOAT_TYPE_P(rettype)
1953 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1954 || (COMPLEX_FLOAT_TYPE_P(rettype)
1955 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1957 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1958 if (excess_type != NULL_TREE)
1960 tree excess_fndecl = mathfn_built_in(excess_type,
1961 DECL_FUNCTION_CODE(fndecl));
1962 if (excess_fndecl == NULL_TREE)
1963 excess_type = NULL_TREE;
1964 else
1966 fn = build_fold_addr_expr_loc(location.gcc_location(),
1967 excess_fndecl);
1968 for (size_t i = 0; i < nargs; ++i)
1970 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1971 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1972 args[i] = ::convert(excess_type, args[i]);
1978 tree ret =
1979 build_call_array_loc(location.gcc_location(),
1980 excess_type != NULL_TREE ? excess_type : rettype,
1981 fn, nargs, args);
1983 if (chain_expr)
1984 CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree();
1986 if (excess_type != NULL_TREE)
1988 // Calling convert here can undo our excess precision change.
1989 // That may or may not be a bug in convert_to_real.
1990 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1993 delete[] args;
1994 return this->make_expression(ret);
1997 // An expression as a statement.
1999 Bstatement*
2000 Gcc_backend::expression_statement(Bfunction*, Bexpression* expr)
2002 return this->make_statement(expr->get_tree());
2005 // Variable initialization.
2007 Bstatement*
2008 Gcc_backend::init_statement(Bfunction*, Bvariable* var, Bexpression* init)
2010 tree var_tree = var->get_decl();
2011 tree init_tree = init->get_tree();
2012 if (var_tree == error_mark_node || init_tree == error_mark_node)
2013 return this->error_statement();
2014 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
2016 // To avoid problems with GNU ld, we don't make zero-sized
2017 // externally visible variables. That might lead us to doing an
2018 // initialization of a zero-sized expression to a non-zero sized
2019 // variable, or vice-versa. Avoid crashes by omitting the
2020 // initializer. Such initializations don't mean anything anyhow.
2021 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
2022 && init_tree != NULL_TREE
2023 && TREE_TYPE(init_tree) != void_type_node
2024 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
2026 DECL_INITIAL(var_tree) = init_tree;
2027 init_tree = NULL_TREE;
2030 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
2031 void_type_node, var_tree);
2032 if (init_tree != NULL_TREE)
2033 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
2034 void_type_node, init_tree, ret);
2036 return this->make_statement(ret);
2039 // Assignment.
2041 Bstatement*
2042 Gcc_backend::assignment_statement(Bfunction* bfn, Bexpression* lhs,
2043 Bexpression* rhs, Location location)
2045 tree lhs_tree = lhs->get_tree();
2046 tree rhs_tree = rhs->get_tree();
2047 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
2048 return this->error_statement();
2050 // To avoid problems with GNU ld, we don't make zero-sized
2051 // externally visible variables. That might lead us to doing an
2052 // assignment of a zero-sized expression to a non-zero sized
2053 // expression; avoid crashes here by avoiding assignments of
2054 // zero-sized expressions. Such assignments don't really mean
2055 // anything anyhow.
2056 if (TREE_TYPE(lhs_tree) == void_type_node
2057 || int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
2058 || TREE_TYPE(rhs_tree) == void_type_node
2059 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
2060 return this->compound_statement(this->expression_statement(bfn, lhs),
2061 this->expression_statement(bfn, rhs));
2063 rhs_tree = this->convert_tree(TREE_TYPE(lhs_tree), rhs_tree, location);
2065 return this->make_statement(fold_build2_loc(location.gcc_location(),
2066 MODIFY_EXPR,
2067 void_type_node,
2068 lhs_tree, rhs_tree));
2071 // Return.
2073 Bstatement*
2074 Gcc_backend::return_statement(Bfunction* bfunction,
2075 const std::vector<Bexpression*>& vals,
2076 Location location)
2078 tree fntree = bfunction->get_tree();
2079 if (fntree == error_mark_node)
2080 return this->error_statement();
2081 tree result = DECL_RESULT(fntree);
2082 if (result == error_mark_node)
2083 return this->error_statement();
2085 // If the result size is zero bytes, we have set the function type
2086 // to have a result type of void, so don't return anything.
2087 // See the function_type method.
2088 tree res_type = TREE_TYPE(result);
2089 if (res_type == void_type_node || int_size_in_bytes(res_type) == 0)
2091 tree stmt_list = NULL_TREE;
2092 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2093 p != vals.end();
2094 p++)
2096 tree val = (*p)->get_tree();
2097 if (val == error_mark_node)
2098 return this->error_statement();
2099 append_to_statement_list(val, &stmt_list);
2101 tree ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2102 void_type_node, NULL_TREE);
2103 append_to_statement_list(ret, &stmt_list);
2104 return this->make_statement(stmt_list);
2107 tree ret;
2108 if (vals.empty())
2109 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
2110 NULL_TREE);
2111 else if (vals.size() == 1)
2113 tree val = vals.front()->get_tree();
2114 if (val == error_mark_node)
2115 return this->error_statement();
2116 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2117 void_type_node, result,
2118 vals.front()->get_tree());
2119 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2120 void_type_node, set);
2122 else
2124 // To return multiple values, copy the values into a temporary
2125 // variable of the right structure type, and then assign the
2126 // temporary variable to the DECL_RESULT in the return
2127 // statement.
2128 tree stmt_list = NULL_TREE;
2129 tree rettype = TREE_TYPE(result);
2131 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2132 push_struct_function(fntree);
2133 else
2134 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2135 tree rettmp = create_tmp_var(rettype, "RESULT");
2136 pop_cfun();
2138 tree field = TYPE_FIELDS(rettype);
2139 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2140 p != vals.end();
2141 p++, field = DECL_CHAIN(field))
2143 gcc_assert(field != NULL_TREE);
2144 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
2145 TREE_TYPE(field), rettmp, field,
2146 NULL_TREE);
2147 tree val = (*p)->get_tree();
2148 if (val == error_mark_node)
2149 return this->error_statement();
2150 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2151 void_type_node,
2152 ref, (*p)->get_tree());
2153 append_to_statement_list(set, &stmt_list);
2155 gcc_assert(field == NULL_TREE);
2156 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2157 void_type_node,
2158 result, rettmp);
2159 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2160 void_type_node, set);
2161 append_to_statement_list(ret_expr, &stmt_list);
2162 ret = stmt_list;
2164 return this->make_statement(ret);
2167 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2168 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2169 // NULL, it will always be executed. This is used for handling defers in Go
2170 // functions. In C++, the resulting code is of this form:
2171 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2173 Bstatement*
2174 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2175 Bstatement* except_stmt,
2176 Bstatement* finally_stmt,
2177 Location location)
2179 tree stat_tree = bstat->get_tree();
2180 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2181 tree finally_tree = finally_stmt == NULL
2182 ? NULL_TREE
2183 : finally_stmt->get_tree();
2185 if (stat_tree == error_mark_node
2186 || except_tree == error_mark_node
2187 || finally_tree == error_mark_node)
2188 return this->error_statement();
2190 if (except_tree != NULL_TREE)
2191 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2192 void_type_node, stat_tree,
2193 build2_loc(location.gcc_location(), CATCH_EXPR,
2194 void_type_node, NULL, except_tree));
2195 if (finally_tree != NULL_TREE)
2196 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2197 void_type_node, stat_tree, finally_tree);
2198 return this->make_statement(stat_tree);
2201 // If.
2203 Bstatement*
2204 Gcc_backend::if_statement(Bfunction*, Bexpression* condition,
2205 Bblock* then_block, Bblock* else_block,
2206 Location location)
2208 tree cond_tree = condition->get_tree();
2209 tree then_tree = then_block->get_tree();
2210 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2211 if (cond_tree == error_mark_node
2212 || then_tree == error_mark_node
2213 || else_tree == error_mark_node)
2214 return this->error_statement();
2215 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2216 cond_tree, then_tree, else_tree);
2217 return this->make_statement(ret);
2220 // Switch.
2222 Bstatement*
2223 Gcc_backend::switch_statement(
2224 Bfunction* function,
2225 Bexpression* value,
2226 const std::vector<std::vector<Bexpression*> >& cases,
2227 const std::vector<Bstatement*>& statements,
2228 Location switch_location)
2230 gcc_assert(cases.size() == statements.size());
2232 tree decl = function->get_tree();
2233 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2234 push_struct_function(decl);
2235 else
2236 push_cfun(DECL_STRUCT_FUNCTION(decl));
2238 tree stmt_list = NULL_TREE;
2239 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2240 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2241 ps != statements.end();
2242 ++ps, ++pc)
2244 if (pc->empty())
2246 location_t loc = (*ps != NULL
2247 ? EXPR_LOCATION((*ps)->get_tree())
2248 : UNKNOWN_LOCATION);
2249 tree label = create_artificial_label(loc);
2250 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2251 append_to_statement_list(c, &stmt_list);
2253 else
2255 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2256 pcv != pc->end();
2257 ++pcv)
2259 tree t = (*pcv)->get_tree();
2260 if (t == error_mark_node)
2261 return this->error_statement();
2262 location_t loc = EXPR_LOCATION(t);
2263 tree label = create_artificial_label(loc);
2264 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2265 append_to_statement_list(c, &stmt_list);
2269 if (*ps != NULL)
2271 tree t = (*ps)->get_tree();
2272 if (t == error_mark_node)
2273 return this->error_statement();
2274 append_to_statement_list(t, &stmt_list);
2277 pop_cfun();
2279 tree tv = value->get_tree();
2280 if (tv == error_mark_node)
2281 return this->error_statement();
2282 tree t = build2_loc(switch_location.gcc_location(), SWITCH_EXPR,
2283 NULL_TREE, tv, stmt_list);
2284 return this->make_statement(t);
2287 // Pair of statements.
2289 Bstatement*
2290 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2292 tree stmt_list = NULL_TREE;
2293 tree t = s1->get_tree();
2294 if (t == error_mark_node)
2295 return this->error_statement();
2296 append_to_statement_list(t, &stmt_list);
2297 t = s2->get_tree();
2298 if (t == error_mark_node)
2299 return this->error_statement();
2300 append_to_statement_list(t, &stmt_list);
2302 // If neither statement has any side effects, stmt_list can be NULL
2303 // at this point.
2304 if (stmt_list == NULL_TREE)
2305 stmt_list = integer_zero_node;
2307 return this->make_statement(stmt_list);
2310 // List of statements.
2312 Bstatement*
2313 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2315 tree stmt_list = NULL_TREE;
2316 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2317 p != statements.end();
2318 ++p)
2320 tree t = (*p)->get_tree();
2321 if (t == error_mark_node)
2322 return this->error_statement();
2323 append_to_statement_list(t, &stmt_list);
2325 return this->make_statement(stmt_list);
2328 // Make a block. For some reason gcc uses a dual structure for
2329 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2330 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2331 // the Bblock.
2333 Bblock*
2334 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2335 const std::vector<Bvariable*>& vars,
2336 Location start_location,
2337 Location)
2339 tree block_tree = make_node(BLOCK);
2340 if (enclosing == NULL)
2342 tree fndecl = function->get_tree();
2343 gcc_assert(fndecl != NULL_TREE);
2345 // We may have already created a block for local variables when
2346 // we take the address of a parameter.
2347 if (DECL_INITIAL(fndecl) == NULL_TREE)
2349 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2350 DECL_INITIAL(fndecl) = block_tree;
2352 else
2354 tree superblock_tree = DECL_INITIAL(fndecl);
2355 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2356 tree* pp;
2357 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2358 *pp != NULL_TREE;
2359 pp = &BLOCK_CHAIN(*pp))
2361 *pp = block_tree;
2364 else
2366 tree superbind_tree = enclosing->get_tree();
2367 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2368 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2370 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2371 tree* pp;
2372 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2373 *pp != NULL_TREE;
2374 pp = &BLOCK_CHAIN(*pp))
2376 *pp = block_tree;
2379 tree* pp = &BLOCK_VARS(block_tree);
2380 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2381 pv != vars.end();
2382 ++pv)
2384 *pp = (*pv)->get_decl();
2385 if (*pp != error_mark_node)
2386 pp = &DECL_CHAIN(*pp);
2388 *pp = NULL_TREE;
2390 TREE_USED(block_tree) = 1;
2392 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2393 void_type_node, BLOCK_VARS(block_tree),
2394 NULL_TREE, block_tree);
2395 TREE_SIDE_EFFECTS(bind_tree) = 1;
2396 return new Bblock(bind_tree);
2399 // Add statements to a block.
2401 void
2402 Gcc_backend::block_add_statements(Bblock* bblock,
2403 const std::vector<Bstatement*>& statements)
2405 tree stmt_list = NULL_TREE;
2406 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2407 p != statements.end();
2408 ++p)
2410 tree s = (*p)->get_tree();
2411 if (s != error_mark_node)
2412 append_to_statement_list(s, &stmt_list);
2415 tree bind_tree = bblock->get_tree();
2416 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2417 BIND_EXPR_BODY(bind_tree) = stmt_list;
2420 // Return a block as a statement.
2422 Bstatement*
2423 Gcc_backend::block_statement(Bblock* bblock)
2425 tree bind_tree = bblock->get_tree();
2426 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2427 return this->make_statement(bind_tree);
2430 // This is not static because we declare it with GTY(()) in go-c.h.
2431 tree go_non_zero_struct;
2433 // Return a type corresponding to TYPE with non-zero size.
2435 tree
2436 Gcc_backend::non_zero_size_type(tree type)
2438 if (int_size_in_bytes(type) != 0)
2439 return type;
2441 switch (TREE_CODE(type))
2443 case RECORD_TYPE:
2444 if (TYPE_FIELDS(type) != NULL_TREE)
2446 tree ns = make_node(RECORD_TYPE);
2447 tree field_trees = NULL_TREE;
2448 tree *pp = &field_trees;
2449 for (tree field = TYPE_FIELDS(type);
2450 field != NULL_TREE;
2451 field = DECL_CHAIN(field))
2453 tree ft = TREE_TYPE(field);
2454 if (field == TYPE_FIELDS(type))
2455 ft = non_zero_size_type(ft);
2456 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2457 DECL_NAME(field), ft);
2458 DECL_CONTEXT(f) = ns;
2459 *pp = f;
2460 pp = &DECL_CHAIN(f);
2462 TYPE_FIELDS(ns) = field_trees;
2463 layout_type(ns);
2464 return ns;
2467 if (go_non_zero_struct == NULL_TREE)
2469 type = make_node(RECORD_TYPE);
2470 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2471 get_identifier("dummy"),
2472 boolean_type_node);
2473 DECL_CONTEXT(field) = type;
2474 TYPE_FIELDS(type) = field;
2475 layout_type(type);
2476 go_non_zero_struct = type;
2478 return go_non_zero_struct;
2480 case ARRAY_TYPE:
2482 tree element_type = non_zero_size_type(TREE_TYPE(type));
2483 return build_array_type_nelts(element_type, 1);
2486 default:
2487 gcc_unreachable();
2490 gcc_unreachable();
2493 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type
2494 // can be created multiple times and thus have multiple tree
2495 // representations. Make sure this does not confuse the middle-end.
2497 tree
2498 Gcc_backend::convert_tree(tree type_tree, tree expr_tree, Location location)
2500 if (type_tree == TREE_TYPE(expr_tree))
2501 return expr_tree;
2503 if (type_tree == error_mark_node
2504 || expr_tree == error_mark_node
2505 || TREE_TYPE(expr_tree) == error_mark_node)
2506 return error_mark_node;
2508 gcc_assert(TREE_CODE(type_tree) == TREE_CODE(TREE_TYPE(expr_tree)));
2509 if (POINTER_TYPE_P(type_tree)
2510 || INTEGRAL_TYPE_P(type_tree)
2511 || SCALAR_FLOAT_TYPE_P(type_tree)
2512 || COMPLEX_FLOAT_TYPE_P(type_tree))
2513 return fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
2514 else if (TREE_CODE(type_tree) == RECORD_TYPE
2515 || TREE_CODE(type_tree) == ARRAY_TYPE)
2517 gcc_assert(int_size_in_bytes(type_tree)
2518 == int_size_in_bytes(TREE_TYPE(expr_tree)));
2519 if (TYPE_MAIN_VARIANT(type_tree)
2520 == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
2521 return fold_build1_loc(location.gcc_location(), NOP_EXPR,
2522 type_tree, expr_tree);
2523 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
2524 type_tree, expr_tree);
2527 gcc_unreachable();
2530 // Make a global variable.
2532 Bvariable*
2533 Gcc_backend::global_variable(const std::string& var_name,
2534 const std::string& asm_name,
2535 Btype* btype,
2536 bool is_external,
2537 bool is_hidden,
2538 bool in_unique_section,
2539 Location location)
2541 tree type_tree = btype->get_tree();
2542 if (type_tree == error_mark_node)
2543 return this->error_variable();
2545 // The GNU linker does not like dynamic variables with zero size.
2546 tree orig_type_tree = type_tree;
2547 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2548 type_tree = this->non_zero_size_type(type_tree);
2550 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2551 get_identifier_from_string(var_name),
2552 type_tree);
2553 if (is_external)
2554 DECL_EXTERNAL(decl) = 1;
2555 else
2556 TREE_STATIC(decl) = 1;
2557 if (!is_hidden)
2559 TREE_PUBLIC(decl) = 1;
2560 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2562 else
2564 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2567 TREE_USED(decl) = 1;
2569 if (in_unique_section)
2570 resolve_unique_section (decl, 0, 1);
2572 go_preserve_from_gc(decl);
2574 return new Bvariable(decl, orig_type_tree);
2577 // Set the initial value of a global variable.
2579 void
2580 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2582 tree expr_tree = expr->get_tree();
2583 if (expr_tree == error_mark_node)
2584 return;
2585 gcc_assert(TREE_CONSTANT(expr_tree));
2586 tree var_decl = var->get_decl();
2587 if (var_decl == error_mark_node)
2588 return;
2589 DECL_INITIAL(var_decl) = expr_tree;
2591 // If this variable goes in a unique section, it may need to go into
2592 // a different one now that DECL_INITIAL is set.
2593 if (symtab_node::get(var_decl)
2594 && symtab_node::get(var_decl)->implicit_section)
2596 set_decl_section_name (var_decl, NULL);
2597 resolve_unique_section (var_decl,
2598 compute_reloc_for_constant (expr_tree),
2603 // Make a local variable.
2605 Bvariable*
2606 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2607 Btype* btype, Bvariable* decl_var,
2608 bool is_address_taken, Location location)
2610 tree type_tree = btype->get_tree();
2611 if (type_tree == error_mark_node)
2612 return this->error_variable();
2613 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2614 get_identifier_from_string(name),
2615 type_tree);
2616 DECL_CONTEXT(decl) = function->get_tree();
2617 TREE_USED(decl) = 1;
2618 if (is_address_taken)
2619 TREE_ADDRESSABLE(decl) = 1;
2620 if (decl_var != NULL)
2622 DECL_HAS_VALUE_EXPR_P(decl) = 1;
2623 SET_DECL_VALUE_EXPR(decl, decl_var->get_decl());
2625 go_preserve_from_gc(decl);
2626 return new Bvariable(decl);
2629 // Make a function parameter variable.
2631 Bvariable*
2632 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2633 Btype* btype, bool is_address_taken,
2634 Location location)
2636 tree type_tree = btype->get_tree();
2637 if (type_tree == error_mark_node)
2638 return this->error_variable();
2639 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2640 get_identifier_from_string(name),
2641 type_tree);
2642 DECL_CONTEXT(decl) = function->get_tree();
2643 DECL_ARG_TYPE(decl) = type_tree;
2644 TREE_USED(decl) = 1;
2645 if (is_address_taken)
2646 TREE_ADDRESSABLE(decl) = 1;
2647 go_preserve_from_gc(decl);
2648 return new Bvariable(decl);
2651 // Make a static chain variable.
2653 Bvariable*
2654 Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name,
2655 Btype* btype, Location location)
2657 tree type_tree = btype->get_tree();
2658 if (type_tree == error_mark_node)
2659 return this->error_variable();
2660 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2661 get_identifier_from_string(name), type_tree);
2662 tree fndecl = function->get_tree();
2663 DECL_CONTEXT(decl) = fndecl;
2664 DECL_ARG_TYPE(decl) = type_tree;
2665 TREE_USED(decl) = 1;
2666 DECL_ARTIFICIAL(decl) = 1;
2667 DECL_IGNORED_P(decl) = 1;
2668 TREE_READONLY(decl) = 1;
2670 struct function *f = DECL_STRUCT_FUNCTION(fndecl);
2671 if (f == NULL)
2673 push_struct_function(fndecl);
2674 pop_cfun();
2675 f = DECL_STRUCT_FUNCTION(fndecl);
2677 gcc_assert(f->static_chain_decl == NULL);
2678 f->static_chain_decl = decl;
2679 DECL_STATIC_CHAIN(fndecl) = 1;
2681 go_preserve_from_gc(decl);
2682 return new Bvariable(decl);
2685 // Make a temporary variable.
2687 Bvariable*
2688 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2689 Btype* btype, Bexpression* binit,
2690 bool is_address_taken,
2691 Location location,
2692 Bstatement** pstatement)
2694 gcc_assert(function != NULL);
2695 tree decl = function->get_tree();
2696 tree type_tree = btype->get_tree();
2697 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2698 if (type_tree == error_mark_node
2699 || init_tree == error_mark_node
2700 || decl == error_mark_node)
2702 *pstatement = this->error_statement();
2703 return this->error_variable();
2706 tree var;
2707 // We can only use create_tmp_var if the type is not addressable.
2708 if (!TREE_ADDRESSABLE(type_tree))
2710 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2711 push_struct_function(decl);
2712 else
2713 push_cfun(DECL_STRUCT_FUNCTION(decl));
2715 var = create_tmp_var(type_tree, "GOTMP");
2716 pop_cfun();
2718 else
2720 gcc_assert(bblock != NULL);
2721 var = build_decl(location.gcc_location(), VAR_DECL,
2722 create_tmp_var_name("GOTMP"),
2723 type_tree);
2724 DECL_ARTIFICIAL(var) = 1;
2725 DECL_IGNORED_P(var) = 1;
2726 TREE_USED(var) = 1;
2727 DECL_CONTEXT(var) = decl;
2729 // We have to add this variable to the BLOCK and the BIND_EXPR.
2730 tree bind_tree = bblock->get_tree();
2731 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2732 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2733 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2734 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2735 BLOCK_VARS(block_tree) = var;
2736 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2739 if (this->type_size(btype) != 0
2740 && init_tree != NULL_TREE
2741 && TREE_TYPE(init_tree) != void_type_node)
2742 DECL_INITIAL(var) = this->convert_tree(type_tree, init_tree, location);
2744 if (is_address_taken)
2745 TREE_ADDRESSABLE(var) = 1;
2747 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2748 DECL_EXPR,
2749 void_type_node, var));
2751 // For a zero sized type, don't initialize VAR with BINIT, but still
2752 // evaluate BINIT for its side effects.
2753 if (init_tree != NULL_TREE
2754 && (this->type_size(btype) == 0
2755 || TREE_TYPE(init_tree) == void_type_node))
2756 *pstatement =
2757 this->compound_statement(this->expression_statement(function, binit),
2758 *pstatement);
2760 return new Bvariable(var);
2763 // Create an implicit variable that is compiler-defined. This is used when
2764 // generating GC root variables and storing the values of a slice initializer.
2766 Bvariable*
2767 Gcc_backend::implicit_variable(const std::string& name,
2768 const std::string& asm_name,
2769 Btype* type, bool is_hidden, bool is_constant,
2770 bool is_common, int64_t alignment)
2772 tree type_tree = type->get_tree();
2773 if (type_tree == error_mark_node)
2774 return this->error_variable();
2776 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2777 get_identifier_from_string(name), type_tree);
2778 DECL_EXTERNAL(decl) = 0;
2779 TREE_PUBLIC(decl) = !is_hidden;
2780 TREE_STATIC(decl) = 1;
2781 TREE_USED(decl) = 1;
2782 DECL_ARTIFICIAL(decl) = 1;
2783 if (is_common)
2785 DECL_COMMON(decl) = 1;
2787 // When the initializer for one implicit_variable refers to another,
2788 // it needs to know the visibility of the referenced struct so that
2789 // compute_reloc_for_constant will return the right value. On many
2790 // systems calling make_decl_one_only will mark the decl as weak,
2791 // which will change the return value of compute_reloc_for_constant.
2792 // We can't reliably call make_decl_one_only yet, because we don't
2793 // yet know the initializer. This issue doesn't arise in C because
2794 // Go initializers, unlike C initializers, can be indirectly
2795 // recursive. To ensure that compute_reloc_for_constant computes
2796 // the right value if some other initializer refers to this one, we
2797 // mark this symbol as weak here. We undo that below in
2798 // immutable_struct_set_init before calling mark_decl_one_only.
2799 DECL_WEAK(decl) = 1;
2801 if (is_constant)
2803 TREE_READONLY(decl) = 1;
2804 TREE_CONSTANT(decl) = 1;
2806 if (alignment != 0)
2808 SET_DECL_ALIGN(decl, alignment * BITS_PER_UNIT);
2809 DECL_USER_ALIGN(decl) = 1;
2811 if (! asm_name.empty())
2812 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2814 go_preserve_from_gc(decl);
2815 return new Bvariable(decl);
2818 // Set the initalizer for a variable created by implicit_variable.
2819 // This is where we finish compiling the variable.
2821 void
2822 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
2823 Btype*, bool, bool, bool is_common,
2824 Bexpression* init)
2826 tree decl = var->get_decl();
2827 tree init_tree;
2828 if (init == NULL)
2829 init_tree = NULL_TREE;
2830 else
2831 init_tree = init->get_tree();
2832 if (decl == error_mark_node || init_tree == error_mark_node)
2833 return;
2835 DECL_INITIAL(decl) = init_tree;
2837 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2838 // See the comment where DECL_WEAK is set in implicit_variable.
2839 if (is_common)
2841 DECL_WEAK(decl) = 0;
2842 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2845 resolve_unique_section(decl, 2, 1);
2847 rest_of_decl_compilation(decl, 1, 0);
2850 // Return a reference to an implicit variable defined in another package.
2852 Bvariable*
2853 Gcc_backend::implicit_variable_reference(const std::string& name,
2854 const std::string& asm_name,
2855 Btype* btype)
2857 tree type_tree = btype->get_tree();
2858 if (type_tree == error_mark_node)
2859 return this->error_variable();
2861 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2862 get_identifier_from_string(name), type_tree);
2863 DECL_EXTERNAL(decl) = 1;
2864 TREE_PUBLIC(decl) = 1;
2865 TREE_STATIC(decl) = 0;
2866 DECL_ARTIFICIAL(decl) = 1;
2867 if (! asm_name.empty())
2868 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2869 go_preserve_from_gc(decl);
2870 return new Bvariable(decl);
2873 // Create a named immutable initialized data structure.
2875 Bvariable*
2876 Gcc_backend::immutable_struct(const std::string& name,
2877 const std::string& asm_name,
2878 bool is_hidden,
2879 bool is_common, Btype* btype, Location location)
2881 tree type_tree = btype->get_tree();
2882 if (type_tree == error_mark_node)
2883 return this->error_variable();
2884 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2885 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2886 get_identifier_from_string(name),
2887 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2888 TREE_STATIC(decl) = 1;
2889 TREE_USED(decl) = 1;
2890 TREE_READONLY(decl) = 1;
2891 TREE_CONSTANT(decl) = 1;
2892 DECL_ARTIFICIAL(decl) = 1;
2893 if (!is_hidden)
2894 TREE_PUBLIC(decl) = 1;
2895 if (! asm_name.empty())
2896 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2898 // When the initializer for one immutable_struct refers to another,
2899 // it needs to know the visibility of the referenced struct so that
2900 // compute_reloc_for_constant will return the right value. On many
2901 // systems calling make_decl_one_only will mark the decl as weak,
2902 // which will change the return value of compute_reloc_for_constant.
2903 // We can't reliably call make_decl_one_only yet, because we don't
2904 // yet know the initializer. This issue doesn't arise in C because
2905 // Go initializers, unlike C initializers, can be indirectly
2906 // recursive. To ensure that compute_reloc_for_constant computes
2907 // the right value if some other initializer refers to this one, we
2908 // mark this symbol as weak here. We undo that below in
2909 // immutable_struct_set_init before calling mark_decl_one_only.
2910 if (is_common)
2911 DECL_WEAK(decl) = 1;
2913 // We don't call rest_of_decl_compilation until we have the
2914 // initializer.
2916 go_preserve_from_gc(decl);
2917 return new Bvariable(decl);
2920 // Set the initializer for a variable created by immutable_struct.
2921 // This is where we finish compiling the variable.
2923 void
2924 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2925 bool, bool is_common, Btype*, Location,
2926 Bexpression* initializer)
2928 tree decl = var->get_decl();
2929 tree init_tree = initializer->get_tree();
2930 if (decl == error_mark_node || init_tree == error_mark_node)
2931 return;
2933 DECL_INITIAL(decl) = init_tree;
2935 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2936 // See the comment where DECL_WEAK is set in immutable_struct.
2937 if (is_common)
2939 DECL_WEAK(decl) = 0;
2940 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2943 // These variables are often unneeded in the final program, so put
2944 // them in their own section so that linker GC can discard them.
2945 resolve_unique_section(decl,
2946 compute_reloc_for_constant (init_tree),
2949 rest_of_decl_compilation(decl, 1, 0);
2952 // Return a reference to an immutable initialized data structure
2953 // defined in another package.
2955 Bvariable*
2956 Gcc_backend::immutable_struct_reference(const std::string& name,
2957 const std::string& asm_name,
2958 Btype* btype,
2959 Location location)
2961 tree type_tree = btype->get_tree();
2962 if (type_tree == error_mark_node)
2963 return this->error_variable();
2964 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2965 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2966 get_identifier_from_string(name),
2967 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2968 TREE_READONLY(decl) = 1;
2969 TREE_CONSTANT(decl) = 1;
2970 DECL_ARTIFICIAL(decl) = 1;
2971 TREE_PUBLIC(decl) = 1;
2972 DECL_EXTERNAL(decl) = 1;
2973 if (! asm_name.empty())
2974 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2975 go_preserve_from_gc(decl);
2976 return new Bvariable(decl);
2979 // Make a label.
2981 Blabel*
2982 Gcc_backend::label(Bfunction* function, const std::string& name,
2983 Location location)
2985 tree decl;
2986 if (name.empty())
2988 tree func_tree = function->get_tree();
2989 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2990 push_struct_function(func_tree);
2991 else
2992 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2994 decl = create_artificial_label(location.gcc_location());
2996 pop_cfun();
2998 else
3000 tree id = get_identifier_from_string(name);
3001 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
3002 void_type_node);
3003 DECL_CONTEXT(decl) = function->get_tree();
3005 return new Blabel(decl);
3008 // Make a statement which defines a label.
3010 Bstatement*
3011 Gcc_backend::label_definition_statement(Blabel* label)
3013 tree lab = label->get_tree();
3014 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
3015 void_type_node, lab);
3016 return this->make_statement(ret);
3019 // Make a goto statement.
3021 Bstatement*
3022 Gcc_backend::goto_statement(Blabel* label, Location location)
3024 tree lab = label->get_tree();
3025 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
3026 lab);
3027 return this->make_statement(ret);
3030 // Get the address of a label.
3032 Bexpression*
3033 Gcc_backend::label_address(Blabel* label, Location location)
3035 tree lab = label->get_tree();
3036 TREE_USED(lab) = 1;
3037 TREE_ADDRESSABLE(lab) = 1;
3038 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
3039 build_fold_addr_expr_loc(location.gcc_location(),
3040 lab));
3041 return this->make_expression(ret);
3044 // Declare or define a new function.
3046 Bfunction*
3047 Gcc_backend::function(Btype* fntype, const std::string& name,
3048 const std::string& asm_name, unsigned int flags,
3049 Location location)
3051 tree functype = fntype->get_tree();
3052 if (functype != error_mark_node)
3054 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
3055 functype = TREE_TYPE(functype);
3057 tree id = get_identifier_from_string(name);
3058 if (functype == error_mark_node || id == error_mark_node)
3059 return this->error_function();
3061 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
3062 if (! asm_name.empty())
3063 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
3064 if ((flags & function_is_visible) != 0)
3065 TREE_PUBLIC(decl) = 1;
3066 if ((flags & function_is_declaration) != 0)
3067 DECL_EXTERNAL(decl) = 1;
3068 else
3070 tree restype = TREE_TYPE(functype);
3071 tree resdecl =
3072 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
3073 DECL_ARTIFICIAL(resdecl) = 1;
3074 DECL_IGNORED_P(resdecl) = 1;
3075 DECL_CONTEXT(resdecl) = decl;
3076 DECL_RESULT(decl) = resdecl;
3078 if ((flags & function_is_inlinable) == 0)
3079 DECL_UNINLINABLE(decl) = 1;
3080 if ((flags & function_no_split_stack) != 0)
3082 tree attr = get_identifier ("no_split_stack");
3083 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
3085 if ((flags & function_does_not_return) != 0)
3086 TREE_THIS_VOLATILE(decl) = 1;
3087 if ((flags & function_in_unique_section) != 0)
3088 resolve_unique_section(decl, 0, 1);
3090 go_preserve_from_gc(decl);
3091 return new Bfunction(decl);
3094 // Create a statement that runs all deferred calls for FUNCTION. This should
3095 // be a statement that looks like this in C++:
3096 // finish:
3097 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3099 Bstatement*
3100 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
3101 Bexpression* defer, Location location)
3103 tree undefer_tree = undefer->get_tree();
3104 tree defer_tree = defer->get_tree();
3105 tree fntree = function->get_tree();
3107 if (undefer_tree == error_mark_node
3108 || defer_tree == error_mark_node
3109 || fntree == error_mark_node)
3110 return this->error_statement();
3112 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
3113 push_struct_function(fntree);
3114 else
3115 push_cfun(DECL_STRUCT_FUNCTION(fntree));
3117 tree stmt_list = NULL;
3118 Blabel* blabel = this->label(function, "", location);
3119 Bstatement* label_def = this->label_definition_statement(blabel);
3120 append_to_statement_list(label_def->get_tree(), &stmt_list);
3122 Bstatement* jump_stmt = this->goto_statement(blabel, location);
3123 tree jump = jump_stmt->get_tree();
3124 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
3125 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
3126 tree try_catch =
3127 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
3128 append_to_statement_list(try_catch, &stmt_list);
3129 pop_cfun();
3131 return this->make_statement(stmt_list);
3134 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3135 // This will only be called for a function definition.
3137 bool
3138 Gcc_backend::function_set_parameters(Bfunction* function,
3139 const std::vector<Bvariable*>& param_vars)
3141 tree func_tree = function->get_tree();
3142 if (func_tree == error_mark_node)
3143 return false;
3145 tree params = NULL_TREE;
3146 tree *pp = &params;
3147 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
3148 pv != param_vars.end();
3149 ++pv)
3151 *pp = (*pv)->get_decl();
3152 gcc_assert(*pp != error_mark_node);
3153 pp = &DECL_CHAIN(*pp);
3155 *pp = NULL_TREE;
3156 DECL_ARGUMENTS(func_tree) = params;
3157 return true;
3160 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3162 bool
3163 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
3165 tree func_tree = function->get_tree();
3166 tree code = code_stmt->get_tree();
3168 if (func_tree == error_mark_node || code == error_mark_node)
3169 return false;
3170 DECL_SAVED_TREE(func_tree) = code;
3171 return true;
3174 // Look up a named built-in function in the current backend implementation.
3175 // Returns NULL if no built-in function by that name exists.
3177 Bfunction*
3178 Gcc_backend::lookup_builtin(const std::string& name)
3180 if (this->builtin_functions_.count(name) != 0)
3181 return this->builtin_functions_[name];
3182 return NULL;
3185 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3186 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3187 // emit early debugging information.
3189 void
3190 Gcc_backend::write_global_definitions(
3191 const std::vector<Btype*>& type_decls,
3192 const std::vector<Bexpression*>& constant_decls,
3193 const std::vector<Bfunction*>& function_decls,
3194 const std::vector<Bvariable*>& variable_decls)
3196 size_t count_definitions = type_decls.size() + constant_decls.size()
3197 + function_decls.size() + variable_decls.size();
3199 tree* defs = new tree[count_definitions];
3201 // Convert all non-erroneous declarations into Gimple form.
3202 size_t i = 0;
3203 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
3204 p != variable_decls.end();
3205 ++p)
3207 tree v = (*p)->get_decl();
3208 if (v != error_mark_node)
3210 defs[i] = v;
3211 go_preserve_from_gc(defs[i]);
3212 ++i;
3216 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
3217 p != type_decls.end();
3218 ++p)
3220 tree type_tree = (*p)->get_tree();
3221 if (type_tree != error_mark_node
3222 && IS_TYPE_OR_DECL_P(type_tree))
3224 defs[i] = TYPE_NAME(type_tree);
3225 gcc_assert(defs[i] != NULL);
3226 go_preserve_from_gc(defs[i]);
3227 ++i;
3230 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
3231 p != constant_decls.end();
3232 ++p)
3234 if ((*p)->get_tree() != error_mark_node)
3236 defs[i] = (*p)->get_tree();
3237 go_preserve_from_gc(defs[i]);
3238 ++i;
3241 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
3242 p != function_decls.end();
3243 ++p)
3245 tree decl = (*p)->get_tree();
3246 if (decl != error_mark_node)
3248 go_preserve_from_gc(decl);
3249 if (DECL_STRUCT_FUNCTION(decl) == NULL)
3250 allocate_struct_function(decl, false);
3251 cgraph_node::finalize_function(decl, true);
3253 defs[i] = decl;
3254 ++i;
3258 // Pass everything back to the middle-end.
3260 wrapup_global_declarations(defs, i);
3262 delete[] defs;
3265 void
3266 Gcc_backend::write_export_data(const char* bytes, unsigned int size)
3268 go_write_export_data(bytes, size);
3272 // Define a builtin function. BCODE is the builtin function code
3273 // defined by builtins.def. NAME is the name of the builtin function.
3274 // LIBNAME is the name of the corresponding library function, and is
3275 // NULL if there isn't one. FNTYPE is the type of the function.
3276 // CONST_P is true if the function has the const attribute.
3277 // NORETURN_P is true if the function has the noreturn attribute.
3279 void
3280 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3281 const char* libname, tree fntype, bool const_p,
3282 bool noreturn_p)
3284 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3285 libname, NULL_TREE);
3286 if (const_p)
3287 TREE_READONLY(decl) = 1;
3288 if (noreturn_p)
3289 TREE_THIS_VOLATILE(decl) = 1;
3290 set_builtin_decl(bcode, decl, true);
3291 this->builtin_functions_[name] = this->make_function(decl);
3292 if (libname != NULL)
3294 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3295 NULL, NULL_TREE);
3296 if (const_p)
3297 TREE_READONLY(decl) = 1;
3298 if (noreturn_p)
3299 TREE_THIS_VOLATILE(decl) = 1;
3300 this->builtin_functions_[libname] = this->make_function(decl);
3304 // Return the backend generator.
3306 Backend*
3307 go_get_backend()
3309 return new Gcc_backend();