jit: document union types
[official-gcc.git] / gcc / go / go-gcc.cc
blob41bcdf8c0ffb800b33b4c16f94603f051369a38c
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2015 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
5 // This file is part of GCC.
7 // GCC is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3, or (at your option) any later
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 "vec.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "options.h"
31 #include "inchash.h"
32 #include "tree.h"
33 #include "fold-const.h"
34 #include "stringpool.h"
35 #include "stor-layout.h"
36 #include "varasm.h"
37 #include "tree-iterator.h"
38 #include "hash-map.h"
39 #include "tm.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "cgraph.h"
43 #include "convert.h"
44 #include "gimple-expr.h"
45 #include "gimplify.h"
46 #include "langhooks.h"
47 #include "toplev.h"
48 #include "output.h"
49 #include "realmpfr.h"
50 #include "builtins.h"
52 #include "go-c.h"
54 #include "gogo.h"
55 #include "backend.h"
57 // A class wrapping a tree.
59 class Gcc_tree
61 public:
62 Gcc_tree(tree t)
63 : t_(t)
64 { }
66 tree
67 get_tree() const
68 { return this->t_; }
70 void
71 set_tree(tree t)
72 { this->t_ = t; }
74 private:
75 tree t_;
78 // In gcc, types, expressions, and statements are all trees.
79 class Btype : public Gcc_tree
81 public:
82 Btype(tree t)
83 : Gcc_tree(t)
84 { }
87 class Bexpression : public Gcc_tree
89 public:
90 Bexpression(tree t)
91 : Gcc_tree(t)
92 { }
95 class Bstatement : public Gcc_tree
97 public:
98 Bstatement(tree t)
99 : Gcc_tree(t)
103 class Bfunction : public Gcc_tree
105 public:
106 Bfunction(tree t)
107 : Gcc_tree(t)
111 class Bblock : public Gcc_tree
113 public:
114 Bblock(tree t)
115 : Gcc_tree(t)
119 class Bvariable : public Gcc_tree
121 public:
122 Bvariable(tree t)
123 : Gcc_tree(t)
127 class Blabel : public Gcc_tree
129 public:
130 Blabel(tree t)
131 : Gcc_tree(t)
135 // This file implements the interface between the Go frontend proper
136 // and the gcc IR. This implements specific instantiations of
137 // abstract classes defined by the Go frontend proper. The Go
138 // frontend proper class methods of these classes to generate the
139 // backend representation.
141 class Gcc_backend : public Backend
143 public:
144 Gcc_backend();
146 // Types.
148 Btype*
149 error_type()
150 { return this->make_type(error_mark_node); }
152 Btype*
153 void_type()
154 { return this->make_type(void_type_node); }
156 Btype*
157 bool_type()
158 { return this->make_type(boolean_type_node); }
160 Btype*
161 integer_type(bool, int);
163 Btype*
164 float_type(int);
166 Btype*
167 complex_type(int);
169 Btype*
170 pointer_type(Btype*);
172 Btype*
173 function_type(const Btyped_identifier&,
174 const std::vector<Btyped_identifier>&,
175 const std::vector<Btyped_identifier>&,
176 Btype*,
177 const Location);
179 Btype*
180 struct_type(const std::vector<Btyped_identifier>&);
182 Btype*
183 array_type(Btype*, Bexpression*);
185 Btype*
186 placeholder_pointer_type(const std::string&, Location, bool);
188 bool
189 set_placeholder_pointer_type(Btype*, Btype*);
191 bool
192 set_placeholder_function_type(Btype*, Btype*);
194 Btype*
195 placeholder_struct_type(const std::string&, Location);
197 bool
198 set_placeholder_struct_type(Btype* placeholder,
199 const std::vector<Btyped_identifier>&);
201 Btype*
202 placeholder_array_type(const std::string&, Location);
204 bool
205 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
207 Btype*
208 named_type(const std::string&, Btype*, Location);
210 Btype*
211 circular_pointer_type(Btype*, bool);
213 bool
214 is_circular_pointer_type(Btype*);
216 int64_t
217 type_size(Btype*);
219 int64_t
220 type_alignment(Btype*);
222 int64_t
223 type_field_alignment(Btype*);
225 int64_t
226 type_field_offset(Btype*, size_t index);
228 // Expressions.
230 Bexpression*
231 zero_expression(Btype*);
233 Bexpression*
234 error_expression()
235 { return this->make_expression(error_mark_node); }
237 Bexpression*
238 nil_pointer_expression()
239 { return this->make_expression(null_pointer_node); }
241 Bexpression*
242 var_expression(Bvariable* var, Location);
244 Bexpression*
245 indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location);
247 Bexpression*
248 named_constant_expression(Btype* btype, const std::string& name,
249 Bexpression* val, Location);
251 Bexpression*
252 integer_constant_expression(Btype* btype, mpz_t val);
254 Bexpression*
255 float_constant_expression(Btype* btype, mpfr_t val);
257 Bexpression*
258 complex_constant_expression(Btype* btype, mpc_t val);
260 Bexpression*
261 string_constant_expression(const std::string& val);
263 Bexpression*
264 boolean_constant_expression(bool val);
266 Bexpression*
267 real_part_expression(Bexpression* bcomplex, Location);
269 Bexpression*
270 imag_part_expression(Bexpression* bcomplex, Location);
272 Bexpression*
273 complex_expression(Bexpression* breal, Bexpression* bimag, Location);
275 Bexpression*
276 convert_expression(Btype* type, Bexpression* expr, Location);
278 Bexpression*
279 function_code_expression(Bfunction*, Location);
281 Bexpression*
282 address_expression(Bexpression*, Location);
284 Bexpression*
285 struct_field_expression(Bexpression*, size_t, Location);
287 Bexpression*
288 compound_expression(Bstatement*, Bexpression*, Location);
290 Bexpression*
291 conditional_expression(Btype*, Bexpression*, Bexpression*, Bexpression*,
292 Location);
294 Bexpression*
295 unary_expression(Operator, Bexpression*, Location);
297 Bexpression*
298 binary_expression(Operator, Bexpression*, Bexpression*, Location);
300 Bexpression*
301 constructor_expression(Btype*, const std::vector<Bexpression*>&, Location);
303 Bexpression*
304 array_constructor_expression(Btype*, const std::vector<unsigned long>&,
305 const std::vector<Bexpression*>&, Location);
307 Bexpression*
308 pointer_offset_expression(Bexpression* base, Bexpression* offset, Location);
310 Bexpression*
311 array_index_expression(Bexpression* array, Bexpression* index, Location);
313 Bexpression*
314 call_expression(Bexpression* fn, const std::vector<Bexpression*>& args,
315 Bexpression* static_chain, Location);
317 Bexpression*
318 stack_allocation_expression(int64_t size, Location);
320 // Statements.
322 Bstatement*
323 error_statement()
324 { return this->make_statement(error_mark_node); }
326 Bstatement*
327 expression_statement(Bexpression*);
329 Bstatement*
330 init_statement(Bvariable* var, Bexpression* init);
332 Bstatement*
333 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
335 Bstatement*
336 return_statement(Bfunction*, const std::vector<Bexpression*>&,
337 Location);
339 Bstatement*
340 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
341 Location);
343 Bstatement*
344 switch_statement(Bfunction* function, Bexpression* value,
345 const std::vector<std::vector<Bexpression*> >& cases,
346 const std::vector<Bstatement*>& statements,
347 Location);
349 Bstatement*
350 compound_statement(Bstatement*, Bstatement*);
352 Bstatement*
353 statement_list(const std::vector<Bstatement*>&);
355 Bstatement*
356 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
357 Bstatement* finally_stmt, Location);
359 // Blocks.
361 Bblock*
362 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
363 Location, Location);
365 void
366 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
368 Bstatement*
369 block_statement(Bblock*);
371 // Variables.
373 Bvariable*
374 error_variable()
375 { return new Bvariable(error_mark_node); }
377 Bvariable*
378 global_variable(const std::string& package_name,
379 const std::string& pkgpath,
380 const std::string& name,
381 Btype* btype,
382 bool is_external,
383 bool is_hidden,
384 bool in_unique_section,
385 Location location);
387 void
388 global_variable_set_init(Bvariable*, Bexpression*);
390 Bvariable*
391 local_variable(Bfunction*, const std::string&, Btype*, bool,
392 Location);
394 Bvariable*
395 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
396 Location);
398 Bvariable*
399 static_chain_variable(Bfunction*, const std::string&, Btype*, Location);
401 Bvariable*
402 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
403 Location, Bstatement**);
405 Bvariable*
406 implicit_variable(const std::string&, Btype*, bool, bool, bool,
407 int64_t);
409 void
410 implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
411 bool, bool, bool, Bexpression*);
413 Bvariable*
414 implicit_variable_reference(const std::string&, Btype*);
416 Bvariable*
417 immutable_struct(const std::string&, bool, bool, Btype*, Location);
419 void
420 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
421 Location, Bexpression*);
423 Bvariable*
424 immutable_struct_reference(const std::string&, Btype*, Location);
426 // Labels.
428 Blabel*
429 label(Bfunction*, const std::string& name, Location);
431 Bstatement*
432 label_definition_statement(Blabel*);
434 Bstatement*
435 goto_statement(Blabel*, Location);
437 Bexpression*
438 label_address(Blabel*, Location);
440 // Functions.
442 Bfunction*
443 error_function()
444 { return this->make_function(error_mark_node); }
446 Bfunction*
447 function(Btype* fntype, const std::string& name, const std::string& asm_name,
448 bool is_visible, bool is_declaration, bool is_inlinable,
449 bool disable_split_stack, bool in_unique_section, Location);
451 Bstatement*
452 function_defer_statement(Bfunction* function, Bexpression* undefer,
453 Bexpression* defer, Location);
455 bool
456 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
458 bool
459 function_set_body(Bfunction* function, Bstatement* code_stmt);
461 Bfunction*
462 lookup_builtin(const std::string&);
464 void
465 write_global_definitions(const std::vector<Btype*>&,
466 const std::vector<Bexpression*>&,
467 const std::vector<Bfunction*>&,
468 const std::vector<Bvariable*>&);
470 private:
471 // Make a Bexpression from a tree.
472 Bexpression*
473 make_expression(tree t)
474 { return new Bexpression(t); }
476 // Make a Bstatement from a tree.
477 Bstatement*
478 make_statement(tree t)
479 { return new Bstatement(t); }
481 // Make a Btype from a tree.
482 Btype*
483 make_type(tree t)
484 { return new Btype(t); }
486 Bfunction*
487 make_function(tree t)
488 { return new Bfunction(t); }
490 Btype*
491 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
493 Btype*
494 fill_in_array(Btype*, Btype*, Bexpression*);
496 tree
497 non_zero_size_type(tree);
499 private:
500 void
501 define_builtin(built_in_function bcode, const char* name, const char* libname,
502 tree fntype, bool const_p);
504 // A mapping of the GCC built-ins exposed to GCCGo.
505 std::map<std::string, Bfunction*> builtin_functions_;
508 // A helper function.
510 static inline tree
511 get_identifier_from_string(const std::string& str)
513 return get_identifier_with_length(str.data(), str.length());
516 // Define the built-in functions that are exposed to GCCGo.
518 Gcc_backend::Gcc_backend()
520 /* We need to define the fetch_and_add functions, since we use them
521 for ++ and --. */
522 tree t = this->integer_type(BITS_PER_UNIT, 1)->get_tree();
523 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
524 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
525 NULL, build_function_type_list(t, p, t, NULL_TREE),
526 false);
528 t = this->integer_type(BITS_PER_UNIT * 2, 1)->get_tree();
529 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
530 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
531 NULL, build_function_type_list(t, p, t, NULL_TREE),
532 false);
534 t = this->integer_type(BITS_PER_UNIT * 4, 1)->get_tree();
535 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
536 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
537 NULL, build_function_type_list(t, p, t, NULL_TREE),
538 false);
540 t = this->integer_type(BITS_PER_UNIT * 8, 1)->get_tree();
541 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
542 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
543 NULL, build_function_type_list(t, p, t, NULL_TREE),
544 false);
546 // We use __builtin_expect for magic import functions.
547 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
548 build_function_type_list(long_integer_type_node,
549 long_integer_type_node,
550 long_integer_type_node,
551 NULL_TREE),
552 true);
554 // We use __builtin_memcmp for struct comparisons.
555 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
556 build_function_type_list(integer_type_node,
557 const_ptr_type_node,
558 const_ptr_type_node,
559 size_type_node,
560 NULL_TREE),
561 false);
563 // We provide some functions for the math library.
564 tree math_function_type = build_function_type_list(double_type_node,
565 double_type_node,
566 NULL_TREE);
567 tree math_function_type_long =
568 build_function_type_list(long_double_type_node, long_double_type_node,
569 long_double_type_node, NULL_TREE);
570 tree math_function_type_two = build_function_type_list(double_type_node,
571 double_type_node,
572 double_type_node,
573 NULL_TREE);
574 tree math_function_type_long_two =
575 build_function_type_list(long_double_type_node, long_double_type_node,
576 long_double_type_node, NULL_TREE);
577 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
578 math_function_type, true);
579 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
580 math_function_type_long, true);
581 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
582 math_function_type, true);
583 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
584 math_function_type_long, true);
585 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
586 math_function_type, true);
587 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
588 math_function_type_long, true);
589 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
590 math_function_type_two, true);
591 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
592 math_function_type_long_two, true);
593 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
594 math_function_type, true);
595 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
596 math_function_type_long, true);
597 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
598 math_function_type, true);
599 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
600 math_function_type_long, true);
601 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
602 math_function_type, true);
603 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
604 math_function_type_long, true);
605 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
606 math_function_type, true);
607 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
608 math_function_type_long, true);
609 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
610 math_function_type, true);
611 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
612 math_function_type_long, true);
613 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
614 math_function_type, true);
615 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
616 math_function_type_long, true);
617 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
618 math_function_type_two, true);
619 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
620 math_function_type_long_two, true);
621 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
622 build_function_type_list(double_type_node,
623 double_type_node,
624 integer_type_node,
625 NULL_TREE),
626 true);
627 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
628 build_function_type_list(long_double_type_node,
629 long_double_type_node,
630 integer_type_node,
631 NULL_TREE),
632 true);
633 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
634 math_function_type, true);
635 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
636 math_function_type_long, true);
637 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
638 math_function_type, true);
639 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
640 math_function_type_long, true);
641 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
642 math_function_type, true);
643 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
644 math_function_type_long, true);
645 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
646 math_function_type, true);
647 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
648 math_function_type_long, true);
649 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
650 math_function_type, true);
651 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
652 math_function_type_long, true);
653 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
654 math_function_type, true);
655 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
656 math_function_type_long, true);
657 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
658 math_function_type, true);
659 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
660 math_function_type_long, true);
661 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
662 math_function_type, true);
663 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
664 math_function_type_long, true);
666 // We use __builtin_return_address in the thunk we build for
667 // functions which call recover.
668 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
669 NULL,
670 build_function_type_list(ptr_type_node,
671 unsigned_type_node,
672 NULL_TREE),
673 false);
675 // The compiler uses __builtin_trap for some exception handling
676 // cases.
677 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
678 build_function_type(void_type_node, void_list_node),
679 false);
682 // Get an unnamed integer type.
684 Btype*
685 Gcc_backend::integer_type(bool is_unsigned, int bits)
687 tree type;
688 if (is_unsigned)
690 if (bits == INT_TYPE_SIZE)
691 type = unsigned_type_node;
692 else if (bits == CHAR_TYPE_SIZE)
693 type = unsigned_char_type_node;
694 else if (bits == SHORT_TYPE_SIZE)
695 type = short_unsigned_type_node;
696 else if (bits == LONG_TYPE_SIZE)
697 type = long_unsigned_type_node;
698 else if (bits == LONG_LONG_TYPE_SIZE)
699 type = long_long_unsigned_type_node;
700 else
701 type = make_unsigned_type(bits);
703 else
705 if (bits == INT_TYPE_SIZE)
706 type = integer_type_node;
707 else if (bits == CHAR_TYPE_SIZE)
708 type = signed_char_type_node;
709 else if (bits == SHORT_TYPE_SIZE)
710 type = short_integer_type_node;
711 else if (bits == LONG_TYPE_SIZE)
712 type = long_integer_type_node;
713 else if (bits == LONG_LONG_TYPE_SIZE)
714 type = long_long_integer_type_node;
715 else
716 type = make_signed_type(bits);
718 return this->make_type(type);
721 // Get an unnamed float type.
723 Btype*
724 Gcc_backend::float_type(int bits)
726 tree type;
727 if (bits == FLOAT_TYPE_SIZE)
728 type = float_type_node;
729 else if (bits == DOUBLE_TYPE_SIZE)
730 type = double_type_node;
731 else if (bits == LONG_DOUBLE_TYPE_SIZE)
732 type = long_double_type_node;
733 else
735 type = make_node(REAL_TYPE);
736 TYPE_PRECISION(type) = bits;
737 layout_type(type);
739 return this->make_type(type);
742 // Get an unnamed complex type.
744 Btype*
745 Gcc_backend::complex_type(int bits)
747 tree type;
748 if (bits == FLOAT_TYPE_SIZE * 2)
749 type = complex_float_type_node;
750 else if (bits == DOUBLE_TYPE_SIZE * 2)
751 type = complex_double_type_node;
752 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
753 type = complex_long_double_type_node;
754 else
756 type = make_node(REAL_TYPE);
757 TYPE_PRECISION(type) = bits / 2;
758 layout_type(type);
759 type = build_complex_type(type);
761 return this->make_type(type);
764 // Get a pointer type.
766 Btype*
767 Gcc_backend::pointer_type(Btype* to_type)
769 tree to_type_tree = to_type->get_tree();
770 if (to_type_tree == error_mark_node)
771 return this->error_type();
772 tree type = build_pointer_type(to_type_tree);
773 return this->make_type(type);
776 // Make a function type.
778 Btype*
779 Gcc_backend::function_type(const Btyped_identifier& receiver,
780 const std::vector<Btyped_identifier>& parameters,
781 const std::vector<Btyped_identifier>& results,
782 Btype* result_struct,
783 Location)
785 tree args = NULL_TREE;
786 tree* pp = &args;
787 if (receiver.btype != NULL)
789 tree t = receiver.btype->get_tree();
790 if (t == error_mark_node)
791 return this->error_type();
792 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
793 pp = &TREE_CHAIN(*pp);
796 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
797 p != parameters.end();
798 ++p)
800 tree t = p->btype->get_tree();
801 if (t == error_mark_node)
802 return this->error_type();
803 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
804 pp = &TREE_CHAIN(*pp);
807 // Varargs is handled entirely at the Go level. When converted to
808 // GENERIC functions are not varargs.
809 *pp = void_list_node;
811 tree result;
812 if (results.empty())
813 result = void_type_node;
814 else if (results.size() == 1)
815 result = results.front().btype->get_tree();
816 else
818 gcc_assert(result_struct != NULL);
819 result = result_struct->get_tree();
821 if (result == error_mark_node)
822 return this->error_type();
824 tree fntype = build_function_type(result, args);
825 if (fntype == error_mark_node)
826 return this->error_type();
828 return this->make_type(build_pointer_type(fntype));
831 // Make a struct type.
833 Btype*
834 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
836 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
839 // Fill in the fields of a struct type.
841 Btype*
842 Gcc_backend::fill_in_struct(Btype* fill,
843 const std::vector<Btyped_identifier>& fields)
845 tree fill_tree = fill->get_tree();
846 tree field_trees = NULL_TREE;
847 tree* pp = &field_trees;
848 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
849 p != fields.end();
850 ++p)
852 tree name_tree = get_identifier_from_string(p->name);
853 tree type_tree = p->btype->get_tree();
854 if (type_tree == error_mark_node)
855 return this->error_type();
856 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
857 type_tree);
858 DECL_CONTEXT(field) = fill_tree;
859 *pp = field;
860 pp = &DECL_CHAIN(field);
862 TYPE_FIELDS(fill_tree) = field_trees;
863 layout_type(fill_tree);
864 return fill;
867 // Make an array type.
869 Btype*
870 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
872 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
873 element_btype, length);
876 // Fill in an array type.
878 Btype*
879 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
880 Bexpression* length)
882 tree element_type_tree = element_type->get_tree();
883 tree length_tree = length->get_tree();
884 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
885 return this->error_type();
887 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
889 length_tree = fold_convert(sizetype, length_tree);
891 // build_index_type takes the maximum index, which is one less than
892 // the length.
893 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
894 length_tree,
895 size_one_node));
897 tree fill_tree = fill->get_tree();
898 TREE_TYPE(fill_tree) = element_type_tree;
899 TYPE_DOMAIN(fill_tree) = index_type_tree;
900 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
901 layout_type(fill_tree);
903 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
904 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
905 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
906 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
907 TYPE_CANONICAL(fill_tree) =
908 build_array_type(TYPE_CANONICAL(element_type_tree),
909 TYPE_CANONICAL(index_type_tree));
911 return fill;
914 // Create a placeholder for a pointer type.
916 Btype*
917 Gcc_backend::placeholder_pointer_type(const std::string& name,
918 Location location, bool)
920 tree ret = build_distinct_type_copy(ptr_type_node);
921 if (!name.empty())
923 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
924 get_identifier_from_string(name),
925 ret);
926 TYPE_NAME(ret) = decl;
928 return this->make_type(ret);
931 // Set the real target type for a placeholder pointer type.
933 bool
934 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
935 Btype* to_type)
937 tree pt = placeholder->get_tree();
938 if (pt == error_mark_node)
939 return false;
940 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
941 tree tt = to_type->get_tree();
942 if (tt == error_mark_node)
944 placeholder->set_tree(error_mark_node);
945 return false;
947 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
948 TREE_TYPE(pt) = TREE_TYPE(tt);
949 if (TYPE_NAME(pt) != NULL_TREE)
951 // Build the data structure gcc wants to see for a typedef.
952 tree copy = build_variant_type_copy(pt);
953 TYPE_NAME(copy) = NULL_TREE;
954 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
956 return true;
959 // Set the real values for a placeholder function type.
961 bool
962 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
964 return this->set_placeholder_pointer_type(placeholder, ft);
967 // Create a placeholder for a struct type.
969 Btype*
970 Gcc_backend::placeholder_struct_type(const std::string& name,
971 Location location)
973 tree ret = make_node(RECORD_TYPE);
974 if (!name.empty())
976 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
977 get_identifier_from_string(name),
978 ret);
979 TYPE_NAME(ret) = decl;
981 return this->make_type(ret);
984 // Fill in the fields of a placeholder struct type.
986 bool
987 Gcc_backend::set_placeholder_struct_type(
988 Btype* placeholder,
989 const std::vector<Btyped_identifier>& fields)
991 tree t = placeholder->get_tree();
992 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
993 Btype* r = this->fill_in_struct(placeholder, fields);
995 if (TYPE_NAME(t) != NULL_TREE)
997 // Build the data structure gcc wants to see for a typedef.
998 tree copy = build_distinct_type_copy(t);
999 TYPE_NAME(copy) = NULL_TREE;
1000 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1003 return r->get_tree() != error_mark_node;
1006 // Create a placeholder for an array type.
1008 Btype*
1009 Gcc_backend::placeholder_array_type(const std::string& name,
1010 Location location)
1012 tree ret = make_node(ARRAY_TYPE);
1013 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1014 get_identifier_from_string(name),
1015 ret);
1016 TYPE_NAME(ret) = decl;
1017 return this->make_type(ret);
1020 // Fill in the fields of a placeholder array type.
1022 bool
1023 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1024 Btype* element_btype,
1025 Bexpression* length)
1027 tree t = placeholder->get_tree();
1028 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1029 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1031 // Build the data structure gcc wants to see for a typedef.
1032 tree copy = build_distinct_type_copy(t);
1033 TYPE_NAME(copy) = NULL_TREE;
1034 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1036 return r->get_tree() != error_mark_node;
1039 // Return a named version of a type.
1041 Btype*
1042 Gcc_backend::named_type(const std::string& name, Btype* btype,
1043 Location location)
1045 tree type = btype->get_tree();
1046 if (type == error_mark_node)
1047 return this->error_type();
1049 // The middle-end expects a basic type to have a name. In Go every
1050 // basic type will have a name. The first time we see a basic type,
1051 // give it whatever Go name we have at this point.
1052 if (TYPE_NAME(type) == NULL_TREE
1053 && location.gcc_location() == BUILTINS_LOCATION
1054 && (TREE_CODE(type) == INTEGER_TYPE
1055 || TREE_CODE(type) == REAL_TYPE
1056 || TREE_CODE(type) == COMPLEX_TYPE
1057 || TREE_CODE(type) == BOOLEAN_TYPE))
1059 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1060 get_identifier_from_string(name),
1061 type);
1062 TYPE_NAME(type) = decl;
1063 return this->make_type(type);
1066 tree copy = build_variant_type_copy(type);
1067 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1068 get_identifier_from_string(name),
1069 copy);
1070 DECL_ORIGINAL_TYPE(decl) = type;
1071 TYPE_NAME(copy) = decl;
1072 return this->make_type(copy);
1075 // Return a pointer type used as a marker for a circular type.
1077 Btype*
1078 Gcc_backend::circular_pointer_type(Btype*, bool)
1080 return this->make_type(ptr_type_node);
1083 // Return whether we might be looking at a circular type.
1085 bool
1086 Gcc_backend::is_circular_pointer_type(Btype* btype)
1088 return btype->get_tree() == ptr_type_node;
1091 // Return the size of a type.
1093 int64_t
1094 Gcc_backend::type_size(Btype* btype)
1096 tree t = btype->get_tree();
1097 if (t == error_mark_node)
1098 return 1;
1099 t = TYPE_SIZE_UNIT(t);
1100 gcc_assert(tree_fits_uhwi_p (t));
1101 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1102 int64_t ret = static_cast<int64_t>(val_wide);
1103 gcc_assert(ret >= 0 && static_cast<unsigned HOST_WIDE_INT>(ret) == val_wide);
1104 return ret;
1107 // Return the alignment of a type.
1109 int64_t
1110 Gcc_backend::type_alignment(Btype* btype)
1112 tree t = btype->get_tree();
1113 if (t == error_mark_node)
1114 return 1;
1115 return TYPE_ALIGN_UNIT(t);
1118 // Return the alignment of a struct field of type BTYPE.
1120 int64_t
1121 Gcc_backend::type_field_alignment(Btype* btype)
1123 tree t = btype->get_tree();
1124 if (t == error_mark_node)
1125 return 1;
1126 return go_field_alignment(t);
1129 // Return the offset of a field in a struct.
1131 int64_t
1132 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1134 tree struct_tree = btype->get_tree();
1135 if (struct_tree == error_mark_node)
1136 return 0;
1137 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1138 tree field = TYPE_FIELDS(struct_tree);
1139 for (; index > 0; --index)
1141 field = DECL_CHAIN(field);
1142 gcc_assert(field != NULL_TREE);
1144 HOST_WIDE_INT offset_wide = int_byte_position(field);
1145 int64_t ret = static_cast<int64_t>(offset_wide);
1146 gcc_assert(ret == offset_wide);
1147 return ret;
1150 // Return the zero value for a type.
1152 Bexpression*
1153 Gcc_backend::zero_expression(Btype* btype)
1155 tree t = btype->get_tree();
1156 tree ret;
1157 if (t == error_mark_node)
1158 ret = error_mark_node;
1159 else
1160 ret = build_zero_cst(t);
1161 return this->make_expression(ret);
1164 // An expression that references a variable.
1166 Bexpression*
1167 Gcc_backend::var_expression(Bvariable* var, Location)
1169 tree ret = var->get_tree();
1170 if (ret == error_mark_node)
1171 return this->error_expression();
1172 return this->make_expression(ret);
1175 // An expression that indirectly references an expression.
1177 Bexpression*
1178 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1179 bool known_valid, Location location)
1181 tree expr_tree = expr->get_tree();
1182 tree type_tree = btype->get_tree();
1183 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1184 return this->error_expression();
1186 // If the type of EXPR is a recursive pointer type, then we
1187 // need to insert a cast before indirecting.
1188 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1189 if (VOID_TYPE_P(target_type_tree))
1190 expr_tree = fold_convert_loc(location.gcc_location(),
1191 build_pointer_type(type_tree), expr_tree);
1193 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1194 expr_tree);
1195 if (known_valid)
1196 TREE_THIS_NOTRAP(ret) = 1;
1197 return this->make_expression(ret);
1200 // Return an expression that declares a constant named NAME with the
1201 // constant value VAL in BTYPE.
1203 Bexpression*
1204 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1205 Bexpression* val, Location location)
1207 tree type_tree = btype->get_tree();
1208 tree const_val = val->get_tree();
1209 if (type_tree == error_mark_node || const_val == error_mark_node)
1210 return this->error_expression();
1212 tree name_tree = get_identifier_from_string(name);
1213 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1214 type_tree);
1215 DECL_INITIAL(decl) = const_val;
1216 TREE_CONSTANT(decl) = 1;
1217 TREE_READONLY(decl) = 1;
1219 go_preserve_from_gc(decl);
1220 return this->make_expression(decl);
1223 // Return a typed value as a constant integer.
1225 Bexpression*
1226 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1228 tree t = btype->get_tree();
1229 if (t == error_mark_node)
1230 return this->error_expression();
1232 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1233 return this->make_expression(ret);
1236 // Return a typed value as a constant floating-point number.
1238 Bexpression*
1239 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1241 tree t = btype->get_tree();
1242 tree ret;
1243 if (t == error_mark_node)
1244 return this->error_expression();
1246 REAL_VALUE_TYPE r1;
1247 real_from_mpfr(&r1, val, t, GMP_RNDN);
1248 REAL_VALUE_TYPE r2;
1249 real_convert(&r2, TYPE_MODE(t), &r1);
1250 ret = build_real(t, r2);
1251 return this->make_expression(ret);
1254 // Return a typed real and imaginary value as a constant complex number.
1256 Bexpression*
1257 Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
1259 tree t = btype->get_tree();
1260 tree ret;
1261 if (t == error_mark_node)
1262 return this->error_expression();
1264 REAL_VALUE_TYPE r1;
1265 real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
1266 REAL_VALUE_TYPE r2;
1267 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1269 REAL_VALUE_TYPE r3;
1270 real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
1271 REAL_VALUE_TYPE r4;
1272 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1274 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1275 build_real(TREE_TYPE(t), r4));
1276 return this->make_expression(ret);
1279 // Make a constant string expression.
1281 Bexpression*
1282 Gcc_backend::string_constant_expression(const std::string& val)
1284 tree index_type = build_index_type(size_int(val.length()));
1285 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1286 TYPE_QUAL_CONST);
1287 tree string_type = build_array_type(const_char_type, index_type);
1288 string_type = build_variant_type_copy(string_type);
1289 TYPE_STRING_FLAG(string_type) = 1;
1290 tree string_val = build_string(val.length(), val.data());
1291 TREE_TYPE(string_val) = string_type;
1293 return this->make_expression(string_val);
1296 // Make a constant boolean expression.
1298 Bexpression*
1299 Gcc_backend::boolean_constant_expression(bool val)
1301 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1302 return this->make_expression(bool_cst);
1305 // Return the real part of a complex expression.
1307 Bexpression*
1308 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1310 tree complex_tree = bcomplex->get_tree();
1311 if (complex_tree == error_mark_node)
1312 return this->error_expression();
1313 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1314 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1315 TREE_TYPE(TREE_TYPE(complex_tree)),
1316 complex_tree);
1317 return this->make_expression(ret);
1320 // Return the imaginary part of a complex expression.
1322 Bexpression*
1323 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1325 tree complex_tree = bcomplex->get_tree();
1326 if (complex_tree == error_mark_node)
1327 return this->error_expression();
1328 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1329 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1330 TREE_TYPE(TREE_TYPE(complex_tree)),
1331 complex_tree);
1332 return this->make_expression(ret);
1335 // Make a complex expression given its real and imaginary parts.
1337 Bexpression*
1338 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1339 Location location)
1341 tree real_tree = breal->get_tree();
1342 tree imag_tree = bimag->get_tree();
1343 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1344 return this->error_expression();
1345 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1346 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1347 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1348 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1349 build_complex_type(TREE_TYPE(real_tree)),
1350 real_tree, imag_tree);
1351 return this->make_expression(ret);
1354 // An expression that converts an expression to a different type.
1356 Bexpression*
1357 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1358 Location location)
1360 tree type_tree = type->get_tree();
1361 tree expr_tree = expr->get_tree();
1362 if (type_tree == error_mark_node
1363 || expr_tree == error_mark_node
1364 || TREE_TYPE(expr_tree) == error_mark_node)
1365 return this->error_expression();
1367 tree ret;
1368 if (this->type_size(type) == 0)
1370 // Do not convert zero-sized types.
1371 ret = expr_tree;
1373 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1374 ret = fold(convert_to_integer(type_tree, expr_tree));
1375 else if (TREE_CODE(type_tree) == REAL_TYPE)
1376 ret = fold(convert_to_real(type_tree, expr_tree));
1377 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1378 ret = fold(convert_to_complex(type_tree, expr_tree));
1379 else if (TREE_CODE(type_tree) == POINTER_TYPE
1380 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1381 ret = fold(convert_to_pointer(type_tree, expr_tree));
1382 else if (TREE_CODE(type_tree) == RECORD_TYPE
1383 || TREE_CODE(type_tree) == ARRAY_TYPE)
1384 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1385 type_tree, expr_tree);
1386 else
1387 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1389 return this->make_expression(ret);
1392 // Get the address of a function.
1394 Bexpression*
1395 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1397 tree func = bfunc->get_tree();
1398 if (func == error_mark_node)
1399 return this->error_expression();
1401 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1402 return this->make_expression(ret);
1405 // Get the address of an expression.
1407 Bexpression*
1408 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1410 tree expr = bexpr->get_tree();
1411 if (expr == error_mark_node)
1412 return this->error_expression();
1414 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1415 return this->make_expression(ret);
1418 // Return an expression for the field at INDEX in BSTRUCT.
1420 Bexpression*
1421 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1422 Location location)
1424 tree struct_tree = bstruct->get_tree();
1425 if (struct_tree == error_mark_node
1426 || TREE_TYPE(struct_tree) == error_mark_node)
1427 return this->error_expression();
1428 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1429 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1430 if (field == NULL_TREE)
1432 // This can happen for a type which refers to itself indirectly
1433 // and then turns out to be erroneous.
1434 return this->error_expression();
1436 for (unsigned int i = index; i > 0; --i)
1438 field = DECL_CHAIN(field);
1439 gcc_assert(field != NULL_TREE);
1441 if (TREE_TYPE(field) == error_mark_node)
1442 return this->error_expression();
1443 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1444 TREE_TYPE(field), struct_tree, field,
1445 NULL_TREE);
1446 if (TREE_CONSTANT(struct_tree))
1447 TREE_CONSTANT(ret) = 1;
1448 return this->make_expression(ret);
1451 // Return an expression that executes BSTAT before BEXPR.
1453 Bexpression*
1454 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1455 Location location)
1457 tree stat = bstat->get_tree();
1458 tree expr = bexpr->get_tree();
1459 if (stat == error_mark_node || expr == error_mark_node)
1460 return this->error_expression();
1461 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1462 TREE_TYPE(expr), stat, expr);
1463 return this->make_expression(ret);
1466 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1467 // ELSE_EXPR otherwise.
1469 Bexpression*
1470 Gcc_backend::conditional_expression(Btype* btype, Bexpression* condition,
1471 Bexpression* then_expr,
1472 Bexpression* else_expr, Location location)
1474 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1475 tree cond_tree = condition->get_tree();
1476 tree then_tree = then_expr->get_tree();
1477 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1478 if (type_tree == error_mark_node
1479 || cond_tree == error_mark_node
1480 || then_tree == error_mark_node
1481 || else_tree == error_mark_node)
1482 return this->error_expression();
1483 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1484 cond_tree, then_tree, else_tree);
1485 return this->make_expression(ret);
1488 // Return an expression for the unary operation OP EXPR.
1490 Bexpression*
1491 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1493 tree expr_tree = expr->get_tree();
1494 if (expr_tree == error_mark_node
1495 || TREE_TYPE(expr_tree) == error_mark_node)
1496 return this->error_expression();
1498 tree type_tree = TREE_TYPE(expr_tree);
1499 enum tree_code code;
1500 switch (op)
1502 case OPERATOR_MINUS:
1504 tree computed_type = excess_precision_type(type_tree);
1505 if (computed_type != NULL_TREE)
1507 expr_tree = convert(computed_type, expr_tree);
1508 type_tree = computed_type;
1510 code = NEGATE_EXPR;
1511 break;
1513 case OPERATOR_NOT:
1514 code = TRUTH_NOT_EXPR;
1515 break;
1516 case OPERATOR_XOR:
1517 code = BIT_NOT_EXPR;
1518 break;
1519 default:
1520 gcc_unreachable();
1521 break;
1524 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1525 expr_tree);
1526 return this->make_expression(ret);
1529 // Convert a gofrontend operator to an equivalent tree_code.
1531 static enum tree_code
1532 operator_to_tree_code(Operator op, tree type)
1534 enum tree_code code;
1535 switch (op)
1537 case OPERATOR_EQEQ:
1538 code = EQ_EXPR;
1539 break;
1540 case OPERATOR_NOTEQ:
1541 code = NE_EXPR;
1542 break;
1543 case OPERATOR_LT:
1544 code = LT_EXPR;
1545 break;
1546 case OPERATOR_LE:
1547 code = LE_EXPR;
1548 break;
1549 case OPERATOR_GT:
1550 code = GT_EXPR;
1551 break;
1552 case OPERATOR_GE:
1553 code = GE_EXPR;
1554 break;
1555 case OPERATOR_OROR:
1556 code = TRUTH_ORIF_EXPR;
1557 break;
1558 case OPERATOR_ANDAND:
1559 code = TRUTH_ANDIF_EXPR;
1560 break;
1561 case OPERATOR_PLUS:
1562 code = PLUS_EXPR;
1563 break;
1564 case OPERATOR_MINUS:
1565 code = MINUS_EXPR;
1566 break;
1567 case OPERATOR_OR:
1568 code = BIT_IOR_EXPR;
1569 break;
1570 case OPERATOR_XOR:
1571 code = BIT_XOR_EXPR;
1572 break;
1573 case OPERATOR_MULT:
1574 code = MULT_EXPR;
1575 break;
1576 case OPERATOR_DIV:
1577 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1578 code = RDIV_EXPR;
1579 else
1580 code = TRUNC_DIV_EXPR;
1581 break;
1582 case OPERATOR_MOD:
1583 code = TRUNC_MOD_EXPR;
1584 break;
1585 case OPERATOR_LSHIFT:
1586 code = LSHIFT_EXPR;
1587 break;
1588 case OPERATOR_RSHIFT:
1589 code = RSHIFT_EXPR;
1590 break;
1591 case OPERATOR_AND:
1592 code = BIT_AND_EXPR;
1593 break;
1594 case OPERATOR_BITCLEAR:
1595 code = BIT_AND_EXPR;
1596 break;
1597 default:
1598 gcc_unreachable();
1601 return code;
1604 // Return an expression for the binary operation LEFT OP RIGHT.
1606 Bexpression*
1607 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1608 Bexpression* right, Location location)
1610 tree left_tree = left->get_tree();
1611 tree right_tree = right->get_tree();
1612 if (left_tree == error_mark_node
1613 || right_tree == error_mark_node)
1614 return this->error_expression();
1615 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1617 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1618 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1619 tree computed_type = excess_precision_type(type_tree);
1620 if (computed_type != NULL_TREE)
1622 left_tree = convert(computed_type, left_tree);
1623 right_tree = convert(computed_type, right_tree);
1624 type_tree = computed_type;
1627 // For comparison operators, the resulting type should be boolean.
1628 switch (op)
1630 case OPERATOR_EQEQ:
1631 case OPERATOR_NOTEQ:
1632 case OPERATOR_LT:
1633 case OPERATOR_LE:
1634 case OPERATOR_GT:
1635 case OPERATOR_GE:
1636 type_tree = boolean_type_node;
1637 break;
1638 default:
1639 break;
1642 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1643 left_tree, right_tree);
1644 return this->make_expression(ret);
1647 // Return an expression that constructs BTYPE with VALS.
1649 Bexpression*
1650 Gcc_backend::constructor_expression(Btype* btype,
1651 const std::vector<Bexpression*>& vals,
1652 Location location)
1654 tree type_tree = btype->get_tree();
1655 if (type_tree == error_mark_node)
1656 return this->error_expression();
1658 vec<constructor_elt, va_gc> *init;
1659 vec_alloc(init, vals.size());
1661 tree sink = NULL_TREE;
1662 bool is_constant = true;
1663 tree field = TYPE_FIELDS(type_tree);
1664 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1665 p != vals.end();
1666 ++p, field = DECL_CHAIN(field))
1668 gcc_assert(field != NULL_TREE);
1669 tree val = (*p)->get_tree();
1670 if (TREE_TYPE(field) == error_mark_node
1671 || val == error_mark_node
1672 || TREE_TYPE(val) == error_mark_node)
1673 return this->error_expression();
1675 if (int_size_in_bytes(TREE_TYPE(field)) == 0)
1677 // GIMPLE cannot represent indices of zero-sized types so
1678 // trying to construct a map with zero-sized keys might lead
1679 // to errors. Instead, we evaluate each expression that
1680 // would have been added as a map element for its
1681 // side-effects and construct an empty map.
1682 append_to_statement_list(val, &sink);
1683 continue;
1686 constructor_elt empty = {NULL, NULL};
1687 constructor_elt* elt = init->quick_push(empty);
1688 elt->index = field;
1689 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
1690 val);
1691 if (!TREE_CONSTANT(elt->value))
1692 is_constant = false;
1694 gcc_assert(field == NULL_TREE);
1695 tree ret = build_constructor(type_tree, init);
1696 if (is_constant)
1697 TREE_CONSTANT(ret) = 1;
1698 if (sink != NULL_TREE)
1699 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1700 type_tree, sink, ret);
1701 return this->make_expression(ret);
1704 Bexpression*
1705 Gcc_backend::array_constructor_expression(
1706 Btype* array_btype, const std::vector<unsigned long>& indexes,
1707 const std::vector<Bexpression*>& vals, Location location)
1709 tree type_tree = array_btype->get_tree();
1710 if (type_tree == error_mark_node)
1711 return this->error_expression();
1713 gcc_assert(indexes.size() == vals.size());
1715 tree element_type = TREE_TYPE(type_tree);
1716 HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
1717 vec<constructor_elt, va_gc> *init;
1718 vec_alloc(init, element_size == 0 ? 0 : vals.size());
1720 tree sink = NULL_TREE;
1721 bool is_constant = true;
1722 for (size_t i = 0; i < vals.size(); ++i)
1724 tree index = size_int(indexes[i]);
1725 tree val = (vals[i])->get_tree();
1727 if (index == error_mark_node
1728 || val == error_mark_node)
1729 return this->error_expression();
1731 if (element_size == 0)
1733 // GIMPLE cannot represent arrays of zero-sized types so trying
1734 // to construct an array of zero-sized values might lead to errors.
1735 // Instead, we evaluate each expression that would have been added as
1736 // an array value for its side-effects and construct an empty array.
1737 append_to_statement_list(val, &sink);
1738 continue;
1741 if (!TREE_CONSTANT(val))
1742 is_constant = false;
1744 constructor_elt empty = {NULL, NULL};
1745 constructor_elt* elt = init->quick_push(empty);
1746 elt->index = index;
1747 elt->value = val;
1750 tree ret = build_constructor(type_tree, init);
1751 if (is_constant)
1752 TREE_CONSTANT(ret) = 1;
1753 if (sink != NULL_TREE)
1754 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1755 type_tree, sink, ret);
1756 return this->make_expression(ret);
1759 // Return an expression for the address of BASE[INDEX].
1761 Bexpression*
1762 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1763 Location location)
1765 tree base_tree = base->get_tree();
1766 tree index_tree = index->get_tree();
1767 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1768 if (base_tree == error_mark_node
1769 || TREE_TYPE(base_tree) == error_mark_node
1770 || index_tree == error_mark_node
1771 || element_type_tree == error_mark_node)
1772 return this->error_expression();
1774 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1775 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1776 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1777 index_tree, element_size);
1778 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1779 TREE_TYPE(base_tree), base_tree, offset);
1780 return this->make_expression(ptr);
1783 // Return an expression representing ARRAY[INDEX]
1785 Bexpression*
1786 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1787 Location location)
1789 tree array_tree = array->get_tree();
1790 tree index_tree = index->get_tree();
1791 if (array_tree == error_mark_node
1792 || TREE_TYPE(array_tree) == error_mark_node
1793 || index_tree == error_mark_node)
1794 return this->error_expression();
1796 tree ret = build4_loc(location.gcc_location(), ARRAY_REF,
1797 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1798 index_tree, NULL_TREE, NULL_TREE);
1799 return this->make_expression(ret);
1802 // Create an expression for a call to FN_EXPR with FN_ARGS.
1803 Bexpression*
1804 Gcc_backend::call_expression(Bexpression* fn_expr,
1805 const std::vector<Bexpression*>& fn_args,
1806 Bexpression* chain_expr, Location location)
1808 tree fn = fn_expr->get_tree();
1809 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1810 return this->error_expression();
1812 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1813 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1815 size_t nargs = fn_args.size();
1816 tree* args = nargs == 0 ? NULL : new tree[nargs];
1817 for (size_t i = 0; i < nargs; ++i)
1819 args[i] = fn_args.at(i)->get_tree();
1820 if (args[i] == error_mark_node)
1821 return this->error_expression();
1824 tree fndecl = fn;
1825 if (TREE_CODE(fndecl) == ADDR_EXPR)
1826 fndecl = TREE_OPERAND(fndecl, 0);
1828 // This is to support builtin math functions when using 80387 math.
1829 tree excess_type = NULL_TREE;
1830 if (optimize
1831 && TREE_CODE(fndecl) == FUNCTION_DECL
1832 && DECL_IS_BUILTIN(fndecl)
1833 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
1834 && nargs > 0
1835 && ((SCALAR_FLOAT_TYPE_P(rettype)
1836 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1837 || (COMPLEX_FLOAT_TYPE_P(rettype)
1838 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1840 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1841 if (excess_type != NULL_TREE)
1843 tree excess_fndecl = mathfn_built_in(excess_type,
1844 DECL_FUNCTION_CODE(fndecl));
1845 if (excess_fndecl == NULL_TREE)
1846 excess_type = NULL_TREE;
1847 else
1849 fn = build_fold_addr_expr_loc(location.gcc_location(),
1850 excess_fndecl);
1851 for (size_t i = 0; i < nargs; ++i)
1853 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1854 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1855 args[i] = ::convert(excess_type, args[i]);
1861 tree ret =
1862 build_call_array_loc(location.gcc_location(),
1863 excess_type != NULL_TREE ? excess_type : rettype,
1864 fn, nargs, args);
1866 if (chain_expr)
1867 CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree();
1869 if (excess_type != NULL_TREE)
1871 // Calling convert here can undo our excess precision change.
1872 // That may or may not be a bug in convert_to_real.
1873 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1876 delete[] args;
1877 return this->make_expression(ret);
1880 // Return an expression that allocates SIZE bytes on the stack.
1882 Bexpression*
1883 Gcc_backend::stack_allocation_expression(int64_t size, Location location)
1885 tree alloca = builtin_decl_explicit(BUILT_IN_ALLOCA);
1886 tree size_tree = build_int_cst(integer_type_node, size);
1887 tree ret = build_call_expr_loc(location.gcc_location(), alloca, 1, size_tree);
1888 return this->make_expression(ret);
1891 // An expression as a statement.
1893 Bstatement*
1894 Gcc_backend::expression_statement(Bexpression* expr)
1896 return this->make_statement(expr->get_tree());
1899 // Variable initialization.
1901 Bstatement*
1902 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1904 tree var_tree = var->get_tree();
1905 tree init_tree = init->get_tree();
1906 if (var_tree == error_mark_node || init_tree == error_mark_node)
1907 return this->error_statement();
1908 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1910 // To avoid problems with GNU ld, we don't make zero-sized
1911 // externally visible variables. That might lead us to doing an
1912 // initialization of a zero-sized expression to a non-zero sized
1913 // variable, or vice-versa. Avoid crashes by omitting the
1914 // initializer. Such initializations don't mean anything anyhow.
1915 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1916 && init_tree != NULL_TREE
1917 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1919 DECL_INITIAL(var_tree) = init_tree;
1920 init_tree = NULL_TREE;
1923 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1924 void_type_node, var_tree);
1925 if (init_tree != NULL_TREE)
1926 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1927 void_type_node, init_tree, ret);
1929 return this->make_statement(ret);
1932 // Assignment.
1934 Bstatement*
1935 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1936 Location location)
1938 tree lhs_tree = lhs->get_tree();
1939 tree rhs_tree = rhs->get_tree();
1940 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1941 return this->error_statement();
1943 // To avoid problems with GNU ld, we don't make zero-sized
1944 // externally visible variables. That might lead us to doing an
1945 // assignment of a zero-sized expression to a non-zero sized
1946 // expression; avoid crashes here by avoiding assignments of
1947 // zero-sized expressions. Such assignments don't really mean
1948 // anything anyhow.
1949 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1950 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1951 return this->compound_statement(this->expression_statement(lhs),
1952 this->expression_statement(rhs));
1954 // Sometimes the same unnamed Go type can be created multiple times
1955 // and thus have multiple tree representations. Make sure this does
1956 // not confuse the middle-end.
1957 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1959 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1960 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1961 if (POINTER_TYPE_P(lhs_type_tree)
1962 || INTEGRAL_TYPE_P(lhs_type_tree)
1963 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1964 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1965 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1966 rhs_tree);
1967 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1968 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1970 gcc_assert(int_size_in_bytes(lhs_type_tree)
1971 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1972 rhs_tree = fold_build1_loc(location.gcc_location(),
1973 VIEW_CONVERT_EXPR,
1974 lhs_type_tree, rhs_tree);
1978 return this->make_statement(fold_build2_loc(location.gcc_location(),
1979 MODIFY_EXPR,
1980 void_type_node,
1981 lhs_tree, rhs_tree));
1984 // Return.
1986 Bstatement*
1987 Gcc_backend::return_statement(Bfunction* bfunction,
1988 const std::vector<Bexpression*>& vals,
1989 Location location)
1991 tree fntree = bfunction->get_tree();
1992 if (fntree == error_mark_node)
1993 return this->error_statement();
1994 tree result = DECL_RESULT(fntree);
1995 if (result == error_mark_node)
1996 return this->error_statement();
1998 tree ret;
1999 if (vals.empty())
2000 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
2001 NULL_TREE);
2002 else if (vals.size() == 1)
2004 tree val = vals.front()->get_tree();
2005 if (val == error_mark_node)
2006 return this->error_statement();
2007 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2008 void_type_node, result,
2009 vals.front()->get_tree());
2010 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2011 void_type_node, set);
2013 else
2015 // To return multiple values, copy the values into a temporary
2016 // variable of the right structure type, and then assign the
2017 // temporary variable to the DECL_RESULT in the return
2018 // statement.
2019 tree stmt_list = NULL_TREE;
2020 tree rettype = TREE_TYPE(result);
2022 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2023 push_struct_function(fntree);
2024 else
2025 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2026 tree rettmp = create_tmp_var(rettype, "RESULT");
2027 pop_cfun();
2029 tree field = TYPE_FIELDS(rettype);
2030 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2031 p != vals.end();
2032 p++, field = DECL_CHAIN(field))
2034 gcc_assert(field != NULL_TREE);
2035 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
2036 TREE_TYPE(field), rettmp, field,
2037 NULL_TREE);
2038 tree val = (*p)->get_tree();
2039 if (val == error_mark_node)
2040 return this->error_statement();
2041 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2042 void_type_node,
2043 ref, (*p)->get_tree());
2044 append_to_statement_list(set, &stmt_list);
2046 gcc_assert(field == NULL_TREE);
2047 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2048 void_type_node,
2049 result, rettmp);
2050 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2051 void_type_node, set);
2052 append_to_statement_list(ret_expr, &stmt_list);
2053 ret = stmt_list;
2055 return this->make_statement(ret);
2058 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2059 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2060 // NULL, it will always be executed. This is used for handling defers in Go
2061 // functions. In C++, the resulting code is of this form:
2062 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2064 Bstatement*
2065 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2066 Bstatement* except_stmt,
2067 Bstatement* finally_stmt,
2068 Location location)
2070 tree stat_tree = bstat->get_tree();
2071 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2072 tree finally_tree = finally_stmt == NULL
2073 ? NULL_TREE
2074 : finally_stmt->get_tree();
2076 if (stat_tree == error_mark_node
2077 || except_tree == error_mark_node
2078 || finally_tree == error_mark_node)
2079 return this->error_statement();
2081 if (except_tree != NULL_TREE)
2082 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2083 void_type_node, stat_tree,
2084 build2_loc(location.gcc_location(), CATCH_EXPR,
2085 void_type_node, NULL, except_tree));
2086 if (finally_tree != NULL_TREE)
2087 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2088 void_type_node, stat_tree, finally_tree);
2089 return this->make_statement(stat_tree);
2092 // If.
2094 Bstatement*
2095 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
2096 Bblock* else_block, Location location)
2098 tree cond_tree = condition->get_tree();
2099 tree then_tree = then_block->get_tree();
2100 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2101 if (cond_tree == error_mark_node
2102 || then_tree == error_mark_node
2103 || else_tree == error_mark_node)
2104 return this->error_statement();
2105 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2106 cond_tree, then_tree, else_tree);
2107 return this->make_statement(ret);
2110 // Switch.
2112 Bstatement*
2113 Gcc_backend::switch_statement(
2114 Bfunction* function,
2115 Bexpression* value,
2116 const std::vector<std::vector<Bexpression*> >& cases,
2117 const std::vector<Bstatement*>& statements,
2118 Location switch_location)
2120 gcc_assert(cases.size() == statements.size());
2122 tree decl = function->get_tree();
2123 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2124 push_struct_function(decl);
2125 else
2126 push_cfun(DECL_STRUCT_FUNCTION(decl));
2128 tree stmt_list = NULL_TREE;
2129 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2130 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2131 ps != statements.end();
2132 ++ps, ++pc)
2134 if (pc->empty())
2136 source_location loc = (*ps != NULL
2137 ? EXPR_LOCATION((*ps)->get_tree())
2138 : UNKNOWN_LOCATION);
2139 tree label = create_artificial_label(loc);
2140 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2141 append_to_statement_list(c, &stmt_list);
2143 else
2145 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2146 pcv != pc->end();
2147 ++pcv)
2149 tree t = (*pcv)->get_tree();
2150 if (t == error_mark_node)
2151 return this->error_statement();
2152 source_location loc = EXPR_LOCATION(t);
2153 tree label = create_artificial_label(loc);
2154 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2155 append_to_statement_list(c, &stmt_list);
2159 if (*ps != NULL)
2161 tree t = (*ps)->get_tree();
2162 if (t == error_mark_node)
2163 return this->error_statement();
2164 append_to_statement_list(t, &stmt_list);
2167 pop_cfun();
2169 tree tv = value->get_tree();
2170 if (tv == error_mark_node)
2171 return this->error_statement();
2172 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
2173 NULL_TREE, tv, stmt_list, NULL_TREE);
2174 return this->make_statement(t);
2177 // Pair of statements.
2179 Bstatement*
2180 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2182 tree stmt_list = NULL_TREE;
2183 tree t = s1->get_tree();
2184 if (t == error_mark_node)
2185 return this->error_statement();
2186 append_to_statement_list(t, &stmt_list);
2187 t = s2->get_tree();
2188 if (t == error_mark_node)
2189 return this->error_statement();
2190 append_to_statement_list(t, &stmt_list);
2192 // If neither statement has any side effects, stmt_list can be NULL
2193 // at this point.
2194 if (stmt_list == NULL_TREE)
2195 stmt_list = integer_zero_node;
2197 return this->make_statement(stmt_list);
2200 // List of statements.
2202 Bstatement*
2203 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2205 tree stmt_list = NULL_TREE;
2206 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2207 p != statements.end();
2208 ++p)
2210 tree t = (*p)->get_tree();
2211 if (t == error_mark_node)
2212 return this->error_statement();
2213 append_to_statement_list(t, &stmt_list);
2215 return this->make_statement(stmt_list);
2218 // Make a block. For some reason gcc uses a dual structure for
2219 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2220 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2221 // the Bblock.
2223 Bblock*
2224 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2225 const std::vector<Bvariable*>& vars,
2226 Location start_location,
2227 Location)
2229 tree block_tree = make_node(BLOCK);
2230 if (enclosing == NULL)
2232 tree fndecl = function->get_tree();
2233 gcc_assert(fndecl != NULL_TREE);
2235 // We may have already created a block for local variables when
2236 // we take the address of a parameter.
2237 if (DECL_INITIAL(fndecl) == NULL_TREE)
2239 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2240 DECL_INITIAL(fndecl) = block_tree;
2242 else
2244 tree superblock_tree = DECL_INITIAL(fndecl);
2245 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2246 tree* pp;
2247 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2248 *pp != NULL_TREE;
2249 pp = &BLOCK_CHAIN(*pp))
2251 *pp = block_tree;
2254 else
2256 tree superbind_tree = enclosing->get_tree();
2257 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2258 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2260 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2261 tree* pp;
2262 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2263 *pp != NULL_TREE;
2264 pp = &BLOCK_CHAIN(*pp))
2266 *pp = block_tree;
2269 tree* pp = &BLOCK_VARS(block_tree);
2270 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2271 pv != vars.end();
2272 ++pv)
2274 *pp = (*pv)->get_tree();
2275 if (*pp != error_mark_node)
2276 pp = &DECL_CHAIN(*pp);
2278 *pp = NULL_TREE;
2280 TREE_USED(block_tree) = 1;
2282 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2283 void_type_node, BLOCK_VARS(block_tree),
2284 NULL_TREE, block_tree);
2285 TREE_SIDE_EFFECTS(bind_tree) = 1;
2286 return new Bblock(bind_tree);
2289 // Add statements to a block.
2291 void
2292 Gcc_backend::block_add_statements(Bblock* bblock,
2293 const std::vector<Bstatement*>& statements)
2295 tree stmt_list = NULL_TREE;
2296 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2297 p != statements.end();
2298 ++p)
2300 tree s = (*p)->get_tree();
2301 if (s != error_mark_node)
2302 append_to_statement_list(s, &stmt_list);
2305 tree bind_tree = bblock->get_tree();
2306 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2307 BIND_EXPR_BODY(bind_tree) = stmt_list;
2310 // Return a block as a statement.
2312 Bstatement*
2313 Gcc_backend::block_statement(Bblock* bblock)
2315 tree bind_tree = bblock->get_tree();
2316 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2317 return this->make_statement(bind_tree);
2320 // This is not static because we declare it with GTY(()) in go-c.h.
2321 tree go_non_zero_struct;
2323 // Return a type corresponding to TYPE with non-zero size.
2325 tree
2326 Gcc_backend::non_zero_size_type(tree type)
2328 if (int_size_in_bytes(type) != 0)
2329 return type;
2331 switch (TREE_CODE(type))
2333 case RECORD_TYPE:
2334 if (TYPE_FIELDS(type) != NULL_TREE)
2336 tree ns = make_node(RECORD_TYPE);
2337 tree field_trees = NULL_TREE;
2338 tree *pp = &field_trees;
2339 for (tree field = TYPE_FIELDS(type);
2340 field != NULL_TREE;
2341 field = DECL_CHAIN(field))
2343 tree ft = TREE_TYPE(field);
2344 if (field == TYPE_FIELDS(type))
2345 ft = non_zero_size_type(ft);
2346 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2347 DECL_NAME(field), ft);
2348 DECL_CONTEXT(f) = ns;
2349 *pp = f;
2350 pp = &DECL_CHAIN(f);
2352 TYPE_FIELDS(ns) = field_trees;
2353 layout_type(ns);
2354 return ns;
2357 if (go_non_zero_struct == NULL_TREE)
2359 type = make_node(RECORD_TYPE);
2360 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2361 get_identifier("dummy"),
2362 boolean_type_node);
2363 DECL_CONTEXT(field) = type;
2364 TYPE_FIELDS(type) = field;
2365 layout_type(type);
2366 go_non_zero_struct = type;
2368 return go_non_zero_struct;
2370 case ARRAY_TYPE:
2372 tree element_type = non_zero_size_type(TREE_TYPE(type));
2373 return build_array_type_nelts(element_type, 1);
2376 default:
2377 gcc_unreachable();
2380 gcc_unreachable();
2383 // Make a global variable.
2385 Bvariable*
2386 Gcc_backend::global_variable(const std::string& package_name,
2387 const std::string& pkgpath,
2388 const std::string& name,
2389 Btype* btype,
2390 bool is_external,
2391 bool is_hidden,
2392 bool in_unique_section,
2393 Location location)
2395 tree type_tree = btype->get_tree();
2396 if (type_tree == error_mark_node)
2397 return this->error_variable();
2399 // The GNU linker does not like dynamic variables with zero size.
2400 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2401 type_tree = this->non_zero_size_type(type_tree);
2403 std::string var_name(package_name);
2404 var_name.push_back('.');
2405 var_name.append(name);
2406 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2407 get_identifier_from_string(var_name),
2408 type_tree);
2409 if (is_external)
2410 DECL_EXTERNAL(decl) = 1;
2411 else
2412 TREE_STATIC(decl) = 1;
2413 if (!is_hidden)
2415 TREE_PUBLIC(decl) = 1;
2417 std::string asm_name(pkgpath);
2418 asm_name.push_back('.');
2419 asm_name.append(name);
2420 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2422 TREE_USED(decl) = 1;
2424 if (in_unique_section)
2425 resolve_unique_section (decl, 0, 1);
2427 go_preserve_from_gc(decl);
2429 return new Bvariable(decl);
2432 // Set the initial value of a global variable.
2434 void
2435 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2437 tree expr_tree = expr->get_tree();
2438 if (expr_tree == error_mark_node)
2439 return;
2440 gcc_assert(TREE_CONSTANT(expr_tree));
2441 tree var_decl = var->get_tree();
2442 if (var_decl == error_mark_node)
2443 return;
2444 DECL_INITIAL(var_decl) = expr_tree;
2446 // If this variable goes in a unique section, it may need to go into
2447 // a different one now that DECL_INITIAL is set.
2448 if (symtab_node::get(var_decl)
2449 && symtab_node::get(var_decl)->implicit_section)
2451 set_decl_section_name (var_decl, NULL);
2452 resolve_unique_section (var_decl,
2453 compute_reloc_for_constant (expr_tree),
2458 // Make a local variable.
2460 Bvariable*
2461 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2462 Btype* btype, bool is_address_taken,
2463 Location location)
2465 tree type_tree = btype->get_tree();
2466 if (type_tree == error_mark_node)
2467 return this->error_variable();
2468 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2469 get_identifier_from_string(name),
2470 type_tree);
2471 DECL_CONTEXT(decl) = function->get_tree();
2472 TREE_USED(decl) = 1;
2473 if (is_address_taken)
2474 TREE_ADDRESSABLE(decl) = 1;
2475 go_preserve_from_gc(decl);
2476 return new Bvariable(decl);
2479 // Make a function parameter variable.
2481 Bvariable*
2482 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2483 Btype* btype, bool is_address_taken,
2484 Location location)
2486 tree type_tree = btype->get_tree();
2487 if (type_tree == error_mark_node)
2488 return this->error_variable();
2489 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2490 get_identifier_from_string(name),
2491 type_tree);
2492 DECL_CONTEXT(decl) = function->get_tree();
2493 DECL_ARG_TYPE(decl) = type_tree;
2494 TREE_USED(decl) = 1;
2495 if (is_address_taken)
2496 TREE_ADDRESSABLE(decl) = 1;
2497 go_preserve_from_gc(decl);
2498 return new Bvariable(decl);
2501 // Make a static chain variable.
2503 Bvariable*
2504 Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name,
2505 Btype* btype, Location location)
2507 tree type_tree = btype->get_tree();
2508 if (type_tree == error_mark_node)
2509 return this->error_variable();
2510 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2511 get_identifier_from_string(name), type_tree);
2512 tree fndecl = function->get_tree();
2513 DECL_CONTEXT(decl) = fndecl;
2514 DECL_ARG_TYPE(decl) = type_tree;
2515 TREE_USED(decl) = 1;
2516 DECL_ARTIFICIAL(decl) = 1;
2517 DECL_IGNORED_P(decl) = 1;
2518 TREE_READONLY(decl) = 1;
2520 struct function *f = DECL_STRUCT_FUNCTION(fndecl);
2521 if (f == NULL)
2523 push_struct_function(fndecl);
2524 pop_cfun();
2525 f = DECL_STRUCT_FUNCTION(fndecl);
2527 gcc_assert(f->static_chain_decl == NULL);
2528 f->static_chain_decl = decl;
2529 DECL_STATIC_CHAIN(fndecl) = 1;
2531 go_preserve_from_gc(decl);
2532 return new Bvariable(decl);
2535 // Make a temporary variable.
2537 Bvariable*
2538 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2539 Btype* btype, Bexpression* binit,
2540 bool is_address_taken,
2541 Location location,
2542 Bstatement** pstatement)
2544 gcc_assert(function != NULL);
2545 tree decl = function->get_tree();
2546 tree type_tree = btype->get_tree();
2547 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2548 if (type_tree == error_mark_node
2549 || init_tree == error_mark_node
2550 || decl == error_mark_node)
2552 *pstatement = this->error_statement();
2553 return this->error_variable();
2556 tree var;
2557 // We can only use create_tmp_var if the type is not addressable.
2558 if (!TREE_ADDRESSABLE(type_tree))
2560 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2561 push_struct_function(decl);
2562 else
2563 push_cfun(DECL_STRUCT_FUNCTION(decl));
2565 var = create_tmp_var(type_tree, "GOTMP");
2566 pop_cfun();
2568 else
2570 gcc_assert(bblock != NULL);
2571 var = build_decl(location.gcc_location(), VAR_DECL,
2572 create_tmp_var_name("GOTMP"),
2573 type_tree);
2574 DECL_ARTIFICIAL(var) = 1;
2575 DECL_IGNORED_P(var) = 1;
2576 TREE_USED(var) = 1;
2577 DECL_CONTEXT(var) = decl;
2579 // We have to add this variable to the BLOCK and the BIND_EXPR.
2580 tree bind_tree = bblock->get_tree();
2581 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2582 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2583 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2584 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2585 BLOCK_VARS(block_tree) = var;
2586 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2589 if (this->type_size(btype) != 0 && init_tree != NULL_TREE)
2590 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
2591 init_tree);
2593 if (is_address_taken)
2594 TREE_ADDRESSABLE(var) = 1;
2596 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2597 DECL_EXPR,
2598 void_type_node, var));
2600 // Don't initialize VAR with BINIT, but still evaluate BINIT for
2601 // its side effects.
2602 if (this->type_size(btype) == 0 && init_tree != NULL_TREE)
2603 *pstatement = this->compound_statement(this->expression_statement(binit),
2604 *pstatement);
2606 return new Bvariable(var);
2609 // Create an implicit variable that is compiler-defined. This is used when
2610 // generating GC root variables and storing the values of a slice initializer.
2612 Bvariable*
2613 Gcc_backend::implicit_variable(const std::string& name, Btype* type,
2614 bool is_hidden, bool is_constant,
2615 bool is_common, int64_t alignment)
2617 tree type_tree = type->get_tree();
2618 if (type_tree == error_mark_node)
2619 return this->error_variable();
2621 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2622 get_identifier_from_string(name), type_tree);
2623 DECL_EXTERNAL(decl) = 0;
2624 TREE_PUBLIC(decl) = !is_hidden;
2625 TREE_STATIC(decl) = 1;
2626 TREE_USED(decl) = 1;
2627 DECL_ARTIFICIAL(decl) = 1;
2628 if (is_common)
2630 DECL_COMMON(decl) = 1;
2632 // When the initializer for one implicit_variable refers to another,
2633 // it needs to know the visibility of the referenced struct so that
2634 // compute_reloc_for_constant will return the right value. On many
2635 // systems calling make_decl_one_only will mark the decl as weak,
2636 // which will change the return value of compute_reloc_for_constant.
2637 // We can't reliably call make_decl_one_only yet, because we don't
2638 // yet know the initializer. This issue doesn't arise in C because
2639 // Go initializers, unlike C initializers, can be indirectly
2640 // recursive. To ensure that compute_reloc_for_constant computes
2641 // the right value if some other initializer refers to this one, we
2642 // mark this symbol as weak here. We undo that below in
2643 // immutable_struct_set_init before calling mark_decl_one_only.
2644 DECL_WEAK(decl) = 1;
2646 if (is_constant)
2648 TREE_READONLY(decl) = 1;
2649 TREE_CONSTANT(decl) = 1;
2651 if (alignment != 0)
2653 DECL_ALIGN(decl) = alignment * BITS_PER_UNIT;
2654 DECL_USER_ALIGN(decl) = 1;
2657 go_preserve_from_gc(decl);
2658 return new Bvariable(decl);
2661 // Set the initalizer for a variable created by implicit_variable.
2662 // This is where we finish compiling the variable.
2664 void
2665 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
2666 Btype*, bool, bool, bool is_common,
2667 Bexpression* init)
2669 tree decl = var->get_tree();
2670 tree init_tree;
2671 if (init == NULL)
2672 init_tree = NULL_TREE;
2673 else
2674 init_tree = init->get_tree();
2675 if (decl == error_mark_node || init_tree == error_mark_node)
2676 return;
2678 DECL_INITIAL(decl) = init_tree;
2680 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2681 // See the comment where DECL_WEAK is set in implicit_variable.
2682 if (is_common)
2684 DECL_WEAK(decl) = 0;
2685 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2688 resolve_unique_section(decl, 2, 1);
2690 rest_of_decl_compilation(decl, 1, 0);
2693 // Return a reference to an implicit variable defined in another package.
2695 Bvariable*
2696 Gcc_backend::implicit_variable_reference(const std::string& name, Btype* btype)
2698 tree type_tree = btype->get_tree();
2699 if (type_tree == error_mark_node)
2700 return this->error_variable();
2702 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2703 get_identifier_from_string(name), type_tree);
2704 DECL_EXTERNAL(decl) = 0;
2705 TREE_PUBLIC(decl) = 1;
2706 TREE_STATIC(decl) = 1;
2707 DECL_ARTIFICIAL(decl) = 1;
2708 go_preserve_from_gc(decl);
2709 return new Bvariable(decl);
2712 // Create a named immutable initialized data structure.
2714 Bvariable*
2715 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
2716 bool is_common, Btype* btype, Location location)
2718 tree type_tree = btype->get_tree();
2719 if (type_tree == error_mark_node)
2720 return this->error_variable();
2721 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2722 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2723 get_identifier_from_string(name),
2724 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2725 TREE_STATIC(decl) = 1;
2726 TREE_USED(decl) = 1;
2727 TREE_READONLY(decl) = 1;
2728 TREE_CONSTANT(decl) = 1;
2729 DECL_ARTIFICIAL(decl) = 1;
2730 if (!is_hidden)
2731 TREE_PUBLIC(decl) = 1;
2733 // When the initializer for one immutable_struct refers to another,
2734 // it needs to know the visibility of the referenced struct so that
2735 // compute_reloc_for_constant will return the right value. On many
2736 // systems calling make_decl_one_only will mark the decl as weak,
2737 // which will change the return value of compute_reloc_for_constant.
2738 // We can't reliably call make_decl_one_only yet, because we don't
2739 // yet know the initializer. This issue doesn't arise in C because
2740 // Go initializers, unlike C initializers, can be indirectly
2741 // recursive. To ensure that compute_reloc_for_constant computes
2742 // the right value if some other initializer refers to this one, we
2743 // mark this symbol as weak here. We undo that below in
2744 // immutable_struct_set_init before calling mark_decl_one_only.
2745 if (is_common)
2746 DECL_WEAK(decl) = 1;
2748 // We don't call rest_of_decl_compilation until we have the
2749 // initializer.
2751 go_preserve_from_gc(decl);
2752 return new Bvariable(decl);
2755 // Set the initializer for a variable created by immutable_struct.
2756 // This is where we finish compiling the variable.
2758 void
2759 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2760 bool, bool is_common, Btype*, Location,
2761 Bexpression* initializer)
2763 tree decl = var->get_tree();
2764 tree init_tree = initializer->get_tree();
2765 if (decl == error_mark_node || init_tree == error_mark_node)
2766 return;
2768 DECL_INITIAL(decl) = init_tree;
2770 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2771 // See the comment where DECL_WEAK is set in immutable_struct.
2772 if (is_common)
2774 DECL_WEAK(decl) = 0;
2775 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2778 // These variables are often unneeded in the final program, so put
2779 // them in their own section so that linker GC can discard them.
2780 resolve_unique_section(decl,
2781 compute_reloc_for_constant (init_tree),
2784 rest_of_decl_compilation(decl, 1, 0);
2787 // Return a reference to an immutable initialized data structure
2788 // defined in another package.
2790 Bvariable*
2791 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
2792 Location location)
2794 tree type_tree = btype->get_tree();
2795 if (type_tree == error_mark_node)
2796 return this->error_variable();
2797 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2798 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2799 get_identifier_from_string(name),
2800 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2801 TREE_READONLY(decl) = 1;
2802 TREE_CONSTANT(decl) = 1;
2803 DECL_ARTIFICIAL(decl) = 1;
2804 TREE_PUBLIC(decl) = 1;
2805 DECL_EXTERNAL(decl) = 1;
2806 go_preserve_from_gc(decl);
2807 return new Bvariable(decl);
2810 // Make a label.
2812 Blabel*
2813 Gcc_backend::label(Bfunction* function, const std::string& name,
2814 Location location)
2816 tree decl;
2817 if (name.empty())
2819 tree func_tree = function->get_tree();
2820 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2821 push_struct_function(func_tree);
2822 else
2823 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2825 decl = create_artificial_label(location.gcc_location());
2827 pop_cfun();
2829 else
2831 tree id = get_identifier_from_string(name);
2832 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
2833 void_type_node);
2834 DECL_CONTEXT(decl) = function->get_tree();
2836 return new Blabel(decl);
2839 // Make a statement which defines a label.
2841 Bstatement*
2842 Gcc_backend::label_definition_statement(Blabel* label)
2844 tree lab = label->get_tree();
2845 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
2846 void_type_node, lab);
2847 return this->make_statement(ret);
2850 // Make a goto statement.
2852 Bstatement*
2853 Gcc_backend::goto_statement(Blabel* label, Location location)
2855 tree lab = label->get_tree();
2856 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
2857 lab);
2858 return this->make_statement(ret);
2861 // Get the address of a label.
2863 Bexpression*
2864 Gcc_backend::label_address(Blabel* label, Location location)
2866 tree lab = label->get_tree();
2867 TREE_USED(lab) = 1;
2868 TREE_ADDRESSABLE(lab) = 1;
2869 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
2870 build_fold_addr_expr_loc(location.gcc_location(),
2871 lab));
2872 return this->make_expression(ret);
2875 // Declare or define a new function.
2877 Bfunction*
2878 Gcc_backend::function(Btype* fntype, const std::string& name,
2879 const std::string& asm_name, bool is_visible,
2880 bool is_declaration, bool is_inlinable,
2881 bool disable_split_stack, bool in_unique_section,
2882 Location location)
2884 tree functype = fntype->get_tree();
2885 if (functype != error_mark_node)
2887 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
2888 functype = TREE_TYPE(functype);
2890 tree id = get_identifier_from_string(name);
2891 if (functype == error_mark_node || id == error_mark_node)
2892 return this->error_function();
2894 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
2895 if (!asm_name.empty())
2896 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2897 if (is_visible)
2898 TREE_PUBLIC(decl) = 1;
2899 if (is_declaration)
2900 DECL_EXTERNAL(decl) = 1;
2901 else
2903 tree restype = TREE_TYPE(functype);
2904 tree resdecl =
2905 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
2906 DECL_ARTIFICIAL(resdecl) = 1;
2907 DECL_IGNORED_P(resdecl) = 1;
2908 DECL_CONTEXT(resdecl) = decl;
2909 DECL_RESULT(decl) = resdecl;
2911 if (!is_inlinable)
2912 DECL_UNINLINABLE(decl) = 1;
2913 if (disable_split_stack)
2915 tree attr = get_identifier("__no_split_stack__");
2916 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
2918 if (in_unique_section)
2919 resolve_unique_section(decl, 0, 1);
2921 go_preserve_from_gc(decl);
2922 return new Bfunction(decl);
2925 // Create a statement that runs all deferred calls for FUNCTION. This should
2926 // be a statement that looks like this in C++:
2927 // finish:
2928 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2930 Bstatement*
2931 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
2932 Bexpression* defer, Location location)
2934 tree undefer_tree = undefer->get_tree();
2935 tree defer_tree = defer->get_tree();
2936 tree fntree = function->get_tree();
2938 if (undefer_tree == error_mark_node
2939 || defer_tree == error_mark_node
2940 || fntree == error_mark_node)
2941 return this->error_statement();
2943 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2944 push_struct_function(fntree);
2945 else
2946 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2948 tree stmt_list = NULL;
2949 Blabel* blabel = this->label(function, "", location);
2950 Bstatement* label_def = this->label_definition_statement(blabel);
2951 append_to_statement_list(label_def->get_tree(), &stmt_list);
2953 Bstatement* jump_stmt = this->goto_statement(blabel, location);
2954 tree jump = jump_stmt->get_tree();
2955 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
2956 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
2957 tree try_catch =
2958 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
2959 append_to_statement_list(try_catch, &stmt_list);
2960 pop_cfun();
2962 return this->make_statement(stmt_list);
2965 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2966 // This will only be called for a function definition.
2968 bool
2969 Gcc_backend::function_set_parameters(Bfunction* function,
2970 const std::vector<Bvariable*>& param_vars)
2972 tree func_tree = function->get_tree();
2973 if (func_tree == error_mark_node)
2974 return false;
2976 tree params = NULL_TREE;
2977 tree *pp = &params;
2978 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
2979 pv != param_vars.end();
2980 ++pv)
2982 *pp = (*pv)->get_tree();
2983 gcc_assert(*pp != error_mark_node);
2984 pp = &DECL_CHAIN(*pp);
2986 *pp = NULL_TREE;
2987 DECL_ARGUMENTS(func_tree) = params;
2988 return true;
2991 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2993 bool
2994 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
2996 tree func_tree = function->get_tree();
2997 tree code = code_stmt->get_tree();
2999 if (func_tree == error_mark_node || code == error_mark_node)
3000 return false;
3001 DECL_SAVED_TREE(func_tree) = code;
3002 return true;
3005 // Look up a named built-in function in the current backend implementation.
3006 // Returns NULL if no built-in function by that name exists.
3008 Bfunction*
3009 Gcc_backend::lookup_builtin(const std::string& name)
3011 if (this->builtin_functions_.count(name) != 0)
3012 return this->builtin_functions_[name];
3013 return NULL;
3016 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3017 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3018 // emit early debugging information.
3020 void
3021 Gcc_backend::write_global_definitions(
3022 const std::vector<Btype*>& type_decls,
3023 const std::vector<Bexpression*>& constant_decls,
3024 const std::vector<Bfunction*>& function_decls,
3025 const std::vector<Bvariable*>& variable_decls)
3027 size_t count_definitions = type_decls.size() + constant_decls.size()
3028 + function_decls.size() + variable_decls.size();
3030 tree* defs = new tree[count_definitions];
3032 // Convert all non-erroneous declarations into Gimple form.
3033 size_t i = 0;
3034 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
3035 p != variable_decls.end();
3036 ++p)
3038 if ((*p)->get_tree() != error_mark_node)
3040 defs[i] = (*p)->get_tree();
3041 go_preserve_from_gc(defs[i]);
3042 ++i;
3046 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
3047 p != type_decls.end();
3048 ++p)
3050 tree type_tree = (*p)->get_tree();
3051 if (type_tree != error_mark_node
3052 && IS_TYPE_OR_DECL_P(type_tree))
3054 defs[i] = TYPE_NAME(type_tree);
3055 gcc_assert(defs[i] != NULL);
3056 go_preserve_from_gc(defs[i]);
3057 ++i;
3060 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
3061 p != constant_decls.end();
3062 ++p)
3064 if ((*p)->get_tree() != error_mark_node)
3066 defs[i] = (*p)->get_tree();
3067 go_preserve_from_gc(defs[i]);
3068 ++i;
3071 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
3072 p != function_decls.end();
3073 ++p)
3075 tree decl = (*p)->get_tree();
3076 if (decl != error_mark_node)
3078 go_preserve_from_gc(decl);
3079 gimplify_function_tree(decl);
3080 cgraph_node::finalize_function(decl, true);
3082 defs[i] = decl;
3083 ++i;
3087 // Pass everything back to the middle-end.
3089 wrapup_global_declarations(defs, i);
3091 delete[] defs;
3094 // Define a builtin function. BCODE is the builtin function code
3095 // defined by builtins.def. NAME is the name of the builtin function.
3096 // LIBNAME is the name of the corresponding library function, and is
3097 // NULL if there isn't one. FNTYPE is the type of the function.
3098 // CONST_P is true if the function has the const attribute.
3100 void
3101 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3102 const char* libname, tree fntype, bool const_p)
3104 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3105 libname, NULL_TREE);
3106 if (const_p)
3107 TREE_READONLY(decl) = 1;
3108 set_builtin_decl(bcode, decl, true);
3109 this->builtin_functions_[name] = this->make_function(decl);
3110 if (libname != NULL)
3112 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3113 NULL, NULL_TREE);
3114 if (const_p)
3115 TREE_READONLY(decl) = 1;
3116 this->builtin_functions_[libname] = this->make_function(decl);
3120 // Return the backend generator.
3122 Backend*
3123 go_get_backend()
3125 return new Gcc_backend();