PR target/66563
[official-gcc.git] / gcc / go / go-gcc.cc
blobcee100b762b87e80ba964645f78b944d1526306d
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 "plugin-api.h"
40 #include "tm.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "ipa-ref.h"
44 #include "cgraph.h"
45 #include "convert.h"
46 #include "gimple-expr.h"
47 #include "gimplify.h"
48 #include "langhooks.h"
49 #include "toplev.h"
50 #include "output.h"
51 #include "realmpfr.h"
52 #include "builtins.h"
54 #include "go-c.h"
56 #include "gogo.h"
57 #include "backend.h"
59 // A class wrapping a tree.
61 class Gcc_tree
63 public:
64 Gcc_tree(tree t)
65 : t_(t)
66 { }
68 tree
69 get_tree() const
70 { return this->t_; }
72 void
73 set_tree(tree t)
74 { this->t_ = t; }
76 private:
77 tree t_;
80 // In gcc, types, expressions, and statements are all trees.
81 class Btype : public Gcc_tree
83 public:
84 Btype(tree t)
85 : Gcc_tree(t)
86 { }
89 class Bexpression : public Gcc_tree
91 public:
92 Bexpression(tree t)
93 : Gcc_tree(t)
94 { }
97 class Bstatement : public Gcc_tree
99 public:
100 Bstatement(tree t)
101 : Gcc_tree(t)
105 class Bfunction : public Gcc_tree
107 public:
108 Bfunction(tree t)
109 : Gcc_tree(t)
113 class Bblock : public Gcc_tree
115 public:
116 Bblock(tree t)
117 : Gcc_tree(t)
121 class Bvariable : public Gcc_tree
123 public:
124 Bvariable(tree t)
125 : Gcc_tree(t)
129 class Blabel : public Gcc_tree
131 public:
132 Blabel(tree t)
133 : Gcc_tree(t)
137 // This file implements the interface between the Go frontend proper
138 // and the gcc IR. This implements specific instantiations of
139 // abstract classes defined by the Go frontend proper. The Go
140 // frontend proper class methods of these classes to generate the
141 // backend representation.
143 class Gcc_backend : public Backend
145 public:
146 Gcc_backend();
148 // Types.
150 Btype*
151 error_type()
152 { return this->make_type(error_mark_node); }
154 Btype*
155 void_type()
156 { return this->make_type(void_type_node); }
158 Btype*
159 bool_type()
160 { return this->make_type(boolean_type_node); }
162 Btype*
163 integer_type(bool, int);
165 Btype*
166 float_type(int);
168 Btype*
169 complex_type(int);
171 Btype*
172 pointer_type(Btype*);
174 Btype*
175 function_type(const Btyped_identifier&,
176 const std::vector<Btyped_identifier>&,
177 const std::vector<Btyped_identifier>&,
178 Btype*,
179 const Location);
181 Btype*
182 struct_type(const std::vector<Btyped_identifier>&);
184 Btype*
185 array_type(Btype*, Bexpression*);
187 Btype*
188 placeholder_pointer_type(const std::string&, Location, bool);
190 bool
191 set_placeholder_pointer_type(Btype*, Btype*);
193 bool
194 set_placeholder_function_type(Btype*, Btype*);
196 Btype*
197 placeholder_struct_type(const std::string&, Location);
199 bool
200 set_placeholder_struct_type(Btype* placeholder,
201 const std::vector<Btyped_identifier>&);
203 Btype*
204 placeholder_array_type(const std::string&, Location);
206 bool
207 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
209 Btype*
210 named_type(const std::string&, Btype*, Location);
212 Btype*
213 circular_pointer_type(Btype*, bool);
215 bool
216 is_circular_pointer_type(Btype*);
218 int64_t
219 type_size(Btype*);
221 int64_t
222 type_alignment(Btype*);
224 int64_t
225 type_field_alignment(Btype*);
227 int64_t
228 type_field_offset(Btype*, size_t index);
230 // Expressions.
232 Bexpression*
233 zero_expression(Btype*);
235 Bexpression*
236 error_expression()
237 { return this->make_expression(error_mark_node); }
239 Bexpression*
240 nil_pointer_expression()
241 { return this->make_expression(null_pointer_node); }
243 Bexpression*
244 var_expression(Bvariable* var, Location);
246 Bexpression*
247 indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location);
249 Bexpression*
250 named_constant_expression(Btype* btype, const std::string& name,
251 Bexpression* val, Location);
253 Bexpression*
254 integer_constant_expression(Btype* btype, mpz_t val);
256 Bexpression*
257 float_constant_expression(Btype* btype, mpfr_t val);
259 Bexpression*
260 complex_constant_expression(Btype* btype, mpc_t val);
262 Bexpression*
263 string_constant_expression(const std::string& val);
265 Bexpression*
266 boolean_constant_expression(bool val);
268 Bexpression*
269 real_part_expression(Bexpression* bcomplex, Location);
271 Bexpression*
272 imag_part_expression(Bexpression* bcomplex, Location);
274 Bexpression*
275 complex_expression(Bexpression* breal, Bexpression* bimag, Location);
277 Bexpression*
278 convert_expression(Btype* type, Bexpression* expr, Location);
280 Bexpression*
281 function_code_expression(Bfunction*, Location);
283 Bexpression*
284 address_expression(Bexpression*, Location);
286 Bexpression*
287 struct_field_expression(Bexpression*, size_t, Location);
289 Bexpression*
290 compound_expression(Bstatement*, Bexpression*, Location);
292 Bexpression*
293 conditional_expression(Btype*, Bexpression*, Bexpression*, Bexpression*,
294 Location);
296 Bexpression*
297 unary_expression(Operator, Bexpression*, Location);
299 Bexpression*
300 binary_expression(Operator, Bexpression*, Bexpression*, Location);
302 Bexpression*
303 constructor_expression(Btype*, const std::vector<Bexpression*>&, Location);
305 Bexpression*
306 array_constructor_expression(Btype*, const std::vector<unsigned long>&,
307 const std::vector<Bexpression*>&, Location);
309 Bexpression*
310 pointer_offset_expression(Bexpression* base, Bexpression* offset, Location);
312 Bexpression*
313 array_index_expression(Bexpression* array, Bexpression* index, Location);
315 Bexpression*
316 call_expression(Bexpression* fn, const std::vector<Bexpression*>& args,
317 Bexpression* static_chain, Location);
319 Bexpression*
320 stack_allocation_expression(int64_t size, Location);
322 // Statements.
324 Bstatement*
325 error_statement()
326 { return this->make_statement(error_mark_node); }
328 Bstatement*
329 expression_statement(Bexpression*);
331 Bstatement*
332 init_statement(Bvariable* var, Bexpression* init);
334 Bstatement*
335 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
337 Bstatement*
338 return_statement(Bfunction*, const std::vector<Bexpression*>&,
339 Location);
341 Bstatement*
342 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
343 Location);
345 Bstatement*
346 switch_statement(Bfunction* function, Bexpression* value,
347 const std::vector<std::vector<Bexpression*> >& cases,
348 const std::vector<Bstatement*>& statements,
349 Location);
351 Bstatement*
352 compound_statement(Bstatement*, Bstatement*);
354 Bstatement*
355 statement_list(const std::vector<Bstatement*>&);
357 Bstatement*
358 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
359 Bstatement* finally_stmt, Location);
361 // Blocks.
363 Bblock*
364 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
365 Location, Location);
367 void
368 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
370 Bstatement*
371 block_statement(Bblock*);
373 // Variables.
375 Bvariable*
376 error_variable()
377 { return new Bvariable(error_mark_node); }
379 Bvariable*
380 global_variable(const std::string& package_name,
381 const std::string& pkgpath,
382 const std::string& name,
383 Btype* btype,
384 bool is_external,
385 bool is_hidden,
386 bool in_unique_section,
387 Location location);
389 void
390 global_variable_set_init(Bvariable*, Bexpression*);
392 Bvariable*
393 local_variable(Bfunction*, const std::string&, Btype*, bool,
394 Location);
396 Bvariable*
397 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
398 Location);
400 Bvariable*
401 static_chain_variable(Bfunction*, const std::string&, Btype*, Location);
403 Bvariable*
404 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
405 Location, Bstatement**);
407 Bvariable*
408 implicit_variable(const std::string&, Btype*, bool, bool, bool,
409 int64_t);
411 void
412 implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
413 bool, bool, bool, Bexpression*);
415 Bvariable*
416 implicit_variable_reference(const std::string&, Btype*);
418 Bvariable*
419 immutable_struct(const std::string&, bool, bool, Btype*, Location);
421 void
422 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
423 Location, Bexpression*);
425 Bvariable*
426 immutable_struct_reference(const std::string&, Btype*, Location);
428 // Labels.
430 Blabel*
431 label(Bfunction*, const std::string& name, Location);
433 Bstatement*
434 label_definition_statement(Blabel*);
436 Bstatement*
437 goto_statement(Blabel*, Location);
439 Bexpression*
440 label_address(Blabel*, Location);
442 // Functions.
444 Bfunction*
445 error_function()
446 { return this->make_function(error_mark_node); }
448 Bfunction*
449 function(Btype* fntype, const std::string& name, const std::string& asm_name,
450 bool is_visible, bool is_declaration, bool is_inlinable,
451 bool disable_split_stack, bool in_unique_section, Location);
453 Bstatement*
454 function_defer_statement(Bfunction* function, Bexpression* undefer,
455 Bexpression* defer, Location);
457 bool
458 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
460 bool
461 function_set_body(Bfunction* function, Bstatement* code_stmt);
463 Bfunction*
464 lookup_builtin(const std::string&);
466 void
467 write_global_definitions(const std::vector<Btype*>&,
468 const std::vector<Bexpression*>&,
469 const std::vector<Bfunction*>&,
470 const std::vector<Bvariable*>&);
472 private:
473 // Make a Bexpression from a tree.
474 Bexpression*
475 make_expression(tree t)
476 { return new Bexpression(t); }
478 // Make a Bstatement from a tree.
479 Bstatement*
480 make_statement(tree t)
481 { return new Bstatement(t); }
483 // Make a Btype from a tree.
484 Btype*
485 make_type(tree t)
486 { return new Btype(t); }
488 Bfunction*
489 make_function(tree t)
490 { return new Bfunction(t); }
492 Btype*
493 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
495 Btype*
496 fill_in_array(Btype*, Btype*, Bexpression*);
498 tree
499 non_zero_size_type(tree);
501 private:
502 void
503 define_builtin(built_in_function bcode, const char* name, const char* libname,
504 tree fntype, bool const_p);
506 // A mapping of the GCC built-ins exposed to GCCGo.
507 std::map<std::string, Bfunction*> builtin_functions_;
510 // A helper function.
512 static inline tree
513 get_identifier_from_string(const std::string& str)
515 return get_identifier_with_length(str.data(), str.length());
518 // Define the built-in functions that are exposed to GCCGo.
520 Gcc_backend::Gcc_backend()
522 /* We need to define the fetch_and_add functions, since we use them
523 for ++ and --. */
524 tree t = this->integer_type(BITS_PER_UNIT, 1)->get_tree();
525 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
526 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
527 NULL, build_function_type_list(t, p, t, NULL_TREE),
528 false);
530 t = this->integer_type(BITS_PER_UNIT * 2, 1)->get_tree();
531 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
532 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
533 NULL, build_function_type_list(t, p, t, NULL_TREE),
534 false);
536 t = this->integer_type(BITS_PER_UNIT * 4, 1)->get_tree();
537 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
538 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
539 NULL, build_function_type_list(t, p, t, NULL_TREE),
540 false);
542 t = this->integer_type(BITS_PER_UNIT * 8, 1)->get_tree();
543 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
544 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
545 NULL, build_function_type_list(t, p, t, NULL_TREE),
546 false);
548 // We use __builtin_expect for magic import functions.
549 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
550 build_function_type_list(long_integer_type_node,
551 long_integer_type_node,
552 long_integer_type_node,
553 NULL_TREE),
554 true);
556 // We use __builtin_memcmp for struct comparisons.
557 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
558 build_function_type_list(integer_type_node,
559 const_ptr_type_node,
560 const_ptr_type_node,
561 size_type_node,
562 NULL_TREE),
563 false);
565 // We provide some functions for the math library.
566 tree math_function_type = build_function_type_list(double_type_node,
567 double_type_node,
568 NULL_TREE);
569 tree math_function_type_long =
570 build_function_type_list(long_double_type_node, long_double_type_node,
571 long_double_type_node, NULL_TREE);
572 tree math_function_type_two = build_function_type_list(double_type_node,
573 double_type_node,
574 double_type_node,
575 NULL_TREE);
576 tree math_function_type_long_two =
577 build_function_type_list(long_double_type_node, long_double_type_node,
578 long_double_type_node, NULL_TREE);
579 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
580 math_function_type, true);
581 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
582 math_function_type_long, true);
583 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
584 math_function_type, true);
585 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
586 math_function_type_long, true);
587 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
588 math_function_type, true);
589 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
590 math_function_type_long, true);
591 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
592 math_function_type_two, true);
593 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
594 math_function_type_long_two, true);
595 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
596 math_function_type, true);
597 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
598 math_function_type_long, true);
599 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
600 math_function_type, true);
601 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
602 math_function_type_long, true);
603 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
604 math_function_type, true);
605 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
606 math_function_type_long, true);
607 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
608 math_function_type, true);
609 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
610 math_function_type_long, true);
611 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
612 math_function_type, true);
613 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
614 math_function_type_long, true);
615 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
616 math_function_type, true);
617 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
618 math_function_type_long, true);
619 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
620 math_function_type_two, true);
621 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
622 math_function_type_long_two, true);
623 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
624 build_function_type_list(double_type_node,
625 double_type_node,
626 integer_type_node,
627 NULL_TREE),
628 true);
629 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
630 build_function_type_list(long_double_type_node,
631 long_double_type_node,
632 integer_type_node,
633 NULL_TREE),
634 true);
635 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
636 math_function_type, true);
637 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
638 math_function_type_long, true);
639 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
640 math_function_type, true);
641 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
642 math_function_type_long, true);
643 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
644 math_function_type, true);
645 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
646 math_function_type_long, true);
647 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
648 math_function_type, true);
649 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
650 math_function_type_long, true);
651 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
652 math_function_type, true);
653 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
654 math_function_type_long, true);
655 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
656 math_function_type, true);
657 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
658 math_function_type_long, true);
659 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
660 math_function_type, true);
661 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
662 math_function_type_long, true);
663 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
664 math_function_type, true);
665 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
666 math_function_type_long, true);
668 // We use __builtin_return_address in the thunk we build for
669 // functions which call recover.
670 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
671 NULL,
672 build_function_type_list(ptr_type_node,
673 unsigned_type_node,
674 NULL_TREE),
675 false);
677 // The compiler uses __builtin_trap for some exception handling
678 // cases.
679 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
680 build_function_type(void_type_node, void_list_node),
681 false);
684 // Get an unnamed integer type.
686 Btype*
687 Gcc_backend::integer_type(bool is_unsigned, int bits)
689 tree type;
690 if (is_unsigned)
692 if (bits == INT_TYPE_SIZE)
693 type = unsigned_type_node;
694 else if (bits == CHAR_TYPE_SIZE)
695 type = unsigned_char_type_node;
696 else if (bits == SHORT_TYPE_SIZE)
697 type = short_unsigned_type_node;
698 else if (bits == LONG_TYPE_SIZE)
699 type = long_unsigned_type_node;
700 else if (bits == LONG_LONG_TYPE_SIZE)
701 type = long_long_unsigned_type_node;
702 else
703 type = make_unsigned_type(bits);
705 else
707 if (bits == INT_TYPE_SIZE)
708 type = integer_type_node;
709 else if (bits == CHAR_TYPE_SIZE)
710 type = signed_char_type_node;
711 else if (bits == SHORT_TYPE_SIZE)
712 type = short_integer_type_node;
713 else if (bits == LONG_TYPE_SIZE)
714 type = long_integer_type_node;
715 else if (bits == LONG_LONG_TYPE_SIZE)
716 type = long_long_integer_type_node;
717 else
718 type = make_signed_type(bits);
720 return this->make_type(type);
723 // Get an unnamed float type.
725 Btype*
726 Gcc_backend::float_type(int bits)
728 tree type;
729 if (bits == FLOAT_TYPE_SIZE)
730 type = float_type_node;
731 else if (bits == DOUBLE_TYPE_SIZE)
732 type = double_type_node;
733 else if (bits == LONG_DOUBLE_TYPE_SIZE)
734 type = long_double_type_node;
735 else
737 type = make_node(REAL_TYPE);
738 TYPE_PRECISION(type) = bits;
739 layout_type(type);
741 return this->make_type(type);
744 // Get an unnamed complex type.
746 Btype*
747 Gcc_backend::complex_type(int bits)
749 tree type;
750 if (bits == FLOAT_TYPE_SIZE * 2)
751 type = complex_float_type_node;
752 else if (bits == DOUBLE_TYPE_SIZE * 2)
753 type = complex_double_type_node;
754 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
755 type = complex_long_double_type_node;
756 else
758 type = make_node(REAL_TYPE);
759 TYPE_PRECISION(type) = bits / 2;
760 layout_type(type);
761 type = build_complex_type(type);
763 return this->make_type(type);
766 // Get a pointer type.
768 Btype*
769 Gcc_backend::pointer_type(Btype* to_type)
771 tree to_type_tree = to_type->get_tree();
772 if (to_type_tree == error_mark_node)
773 return this->error_type();
774 tree type = build_pointer_type(to_type_tree);
775 return this->make_type(type);
778 // Make a function type.
780 Btype*
781 Gcc_backend::function_type(const Btyped_identifier& receiver,
782 const std::vector<Btyped_identifier>& parameters,
783 const std::vector<Btyped_identifier>& results,
784 Btype* result_struct,
785 Location)
787 tree args = NULL_TREE;
788 tree* pp = &args;
789 if (receiver.btype != NULL)
791 tree t = receiver.btype->get_tree();
792 if (t == error_mark_node)
793 return this->error_type();
794 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
795 pp = &TREE_CHAIN(*pp);
798 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
799 p != parameters.end();
800 ++p)
802 tree t = p->btype->get_tree();
803 if (t == error_mark_node)
804 return this->error_type();
805 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
806 pp = &TREE_CHAIN(*pp);
809 // Varargs is handled entirely at the Go level. When converted to
810 // GENERIC functions are not varargs.
811 *pp = void_list_node;
813 tree result;
814 if (results.empty())
815 result = void_type_node;
816 else if (results.size() == 1)
817 result = results.front().btype->get_tree();
818 else
820 gcc_assert(result_struct != NULL);
821 result = result_struct->get_tree();
823 if (result == error_mark_node)
824 return this->error_type();
826 tree fntype = build_function_type(result, args);
827 if (fntype == error_mark_node)
828 return this->error_type();
830 return this->make_type(build_pointer_type(fntype));
833 // Make a struct type.
835 Btype*
836 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
838 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
841 // Fill in the fields of a struct type.
843 Btype*
844 Gcc_backend::fill_in_struct(Btype* fill,
845 const std::vector<Btyped_identifier>& fields)
847 tree fill_tree = fill->get_tree();
848 tree field_trees = NULL_TREE;
849 tree* pp = &field_trees;
850 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
851 p != fields.end();
852 ++p)
854 tree name_tree = get_identifier_from_string(p->name);
855 tree type_tree = p->btype->get_tree();
856 if (type_tree == error_mark_node)
857 return this->error_type();
858 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
859 type_tree);
860 DECL_CONTEXT(field) = fill_tree;
861 *pp = field;
862 pp = &DECL_CHAIN(field);
864 TYPE_FIELDS(fill_tree) = field_trees;
865 layout_type(fill_tree);
866 return fill;
869 // Make an array type.
871 Btype*
872 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
874 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
875 element_btype, length);
878 // Fill in an array type.
880 Btype*
881 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
882 Bexpression* length)
884 tree element_type_tree = element_type->get_tree();
885 tree length_tree = length->get_tree();
886 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
887 return this->error_type();
889 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
891 length_tree = fold_convert(sizetype, length_tree);
893 // build_index_type takes the maximum index, which is one less than
894 // the length.
895 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
896 length_tree,
897 size_one_node));
899 tree fill_tree = fill->get_tree();
900 TREE_TYPE(fill_tree) = element_type_tree;
901 TYPE_DOMAIN(fill_tree) = index_type_tree;
902 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
903 layout_type(fill_tree);
905 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
906 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
907 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
908 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
909 TYPE_CANONICAL(fill_tree) =
910 build_array_type(TYPE_CANONICAL(element_type_tree),
911 TYPE_CANONICAL(index_type_tree));
913 return fill;
916 // Create a placeholder for a pointer type.
918 Btype*
919 Gcc_backend::placeholder_pointer_type(const std::string& name,
920 Location location, bool)
922 tree ret = build_distinct_type_copy(ptr_type_node);
923 if (!name.empty())
925 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
926 get_identifier_from_string(name),
927 ret);
928 TYPE_NAME(ret) = decl;
930 return this->make_type(ret);
933 // Set the real target type for a placeholder pointer type.
935 bool
936 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
937 Btype* to_type)
939 tree pt = placeholder->get_tree();
940 if (pt == error_mark_node)
941 return false;
942 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
943 tree tt = to_type->get_tree();
944 if (tt == error_mark_node)
946 placeholder->set_tree(error_mark_node);
947 return false;
949 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
950 TREE_TYPE(pt) = TREE_TYPE(tt);
951 if (TYPE_NAME(pt) != NULL_TREE)
953 // Build the data structure gcc wants to see for a typedef.
954 tree copy = build_variant_type_copy(pt);
955 TYPE_NAME(copy) = NULL_TREE;
956 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
958 return true;
961 // Set the real values for a placeholder function type.
963 bool
964 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
966 return this->set_placeholder_pointer_type(placeholder, ft);
969 // Create a placeholder for a struct type.
971 Btype*
972 Gcc_backend::placeholder_struct_type(const std::string& name,
973 Location location)
975 tree ret = make_node(RECORD_TYPE);
976 if (!name.empty())
978 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
979 get_identifier_from_string(name),
980 ret);
981 TYPE_NAME(ret) = decl;
983 return this->make_type(ret);
986 // Fill in the fields of a placeholder struct type.
988 bool
989 Gcc_backend::set_placeholder_struct_type(
990 Btype* placeholder,
991 const std::vector<Btyped_identifier>& fields)
993 tree t = placeholder->get_tree();
994 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
995 Btype* r = this->fill_in_struct(placeholder, fields);
997 if (TYPE_NAME(t) != NULL_TREE)
999 // Build the data structure gcc wants to see for a typedef.
1000 tree copy = build_distinct_type_copy(t);
1001 TYPE_NAME(copy) = NULL_TREE;
1002 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1005 return r->get_tree() != error_mark_node;
1008 // Create a placeholder for an array type.
1010 Btype*
1011 Gcc_backend::placeholder_array_type(const std::string& name,
1012 Location location)
1014 tree ret = make_node(ARRAY_TYPE);
1015 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1016 get_identifier_from_string(name),
1017 ret);
1018 TYPE_NAME(ret) = decl;
1019 return this->make_type(ret);
1022 // Fill in the fields of a placeholder array type.
1024 bool
1025 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1026 Btype* element_btype,
1027 Bexpression* length)
1029 tree t = placeholder->get_tree();
1030 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1031 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1033 // Build the data structure gcc wants to see for a typedef.
1034 tree copy = build_distinct_type_copy(t);
1035 TYPE_NAME(copy) = NULL_TREE;
1036 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1038 return r->get_tree() != error_mark_node;
1041 // Return a named version of a type.
1043 Btype*
1044 Gcc_backend::named_type(const std::string& name, Btype* btype,
1045 Location location)
1047 tree type = btype->get_tree();
1048 if (type == error_mark_node)
1049 return this->error_type();
1051 // The middle-end expects a basic type to have a name. In Go every
1052 // basic type will have a name. The first time we see a basic type,
1053 // give it whatever Go name we have at this point.
1054 if (TYPE_NAME(type) == NULL_TREE
1055 && location.gcc_location() == BUILTINS_LOCATION
1056 && (TREE_CODE(type) == INTEGER_TYPE
1057 || TREE_CODE(type) == REAL_TYPE
1058 || TREE_CODE(type) == COMPLEX_TYPE
1059 || TREE_CODE(type) == BOOLEAN_TYPE))
1061 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1062 get_identifier_from_string(name),
1063 type);
1064 TYPE_NAME(type) = decl;
1065 return this->make_type(type);
1068 tree copy = build_variant_type_copy(type);
1069 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1070 get_identifier_from_string(name),
1071 copy);
1072 DECL_ORIGINAL_TYPE(decl) = type;
1073 TYPE_NAME(copy) = decl;
1074 return this->make_type(copy);
1077 // Return a pointer type used as a marker for a circular type.
1079 Btype*
1080 Gcc_backend::circular_pointer_type(Btype*, bool)
1082 return this->make_type(ptr_type_node);
1085 // Return whether we might be looking at a circular type.
1087 bool
1088 Gcc_backend::is_circular_pointer_type(Btype* btype)
1090 return btype->get_tree() == ptr_type_node;
1093 // Return the size of a type.
1095 int64_t
1096 Gcc_backend::type_size(Btype* btype)
1098 tree t = btype->get_tree();
1099 if (t == error_mark_node)
1100 return 1;
1101 t = TYPE_SIZE_UNIT(t);
1102 gcc_assert(tree_fits_uhwi_p (t));
1103 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1104 int64_t ret = static_cast<int64_t>(val_wide);
1105 gcc_assert(ret >= 0 && static_cast<unsigned HOST_WIDE_INT>(ret) == val_wide);
1106 return ret;
1109 // Return the alignment of a type.
1111 int64_t
1112 Gcc_backend::type_alignment(Btype* btype)
1114 tree t = btype->get_tree();
1115 if (t == error_mark_node)
1116 return 1;
1117 return TYPE_ALIGN_UNIT(t);
1120 // Return the alignment of a struct field of type BTYPE.
1122 int64_t
1123 Gcc_backend::type_field_alignment(Btype* btype)
1125 tree t = btype->get_tree();
1126 if (t == error_mark_node)
1127 return 1;
1128 return go_field_alignment(t);
1131 // Return the offset of a field in a struct.
1133 int64_t
1134 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1136 tree struct_tree = btype->get_tree();
1137 if (struct_tree == error_mark_node)
1138 return 0;
1139 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1140 tree field = TYPE_FIELDS(struct_tree);
1141 for (; index > 0; --index)
1143 field = DECL_CHAIN(field);
1144 gcc_assert(field != NULL_TREE);
1146 HOST_WIDE_INT offset_wide = int_byte_position(field);
1147 int64_t ret = static_cast<int64_t>(offset_wide);
1148 gcc_assert(ret == offset_wide);
1149 return ret;
1152 // Return the zero value for a type.
1154 Bexpression*
1155 Gcc_backend::zero_expression(Btype* btype)
1157 tree t = btype->get_tree();
1158 tree ret;
1159 if (t == error_mark_node)
1160 ret = error_mark_node;
1161 else
1162 ret = build_zero_cst(t);
1163 return this->make_expression(ret);
1166 // An expression that references a variable.
1168 Bexpression*
1169 Gcc_backend::var_expression(Bvariable* var, Location)
1171 tree ret = var->get_tree();
1172 if (ret == error_mark_node)
1173 return this->error_expression();
1174 return this->make_expression(ret);
1177 // An expression that indirectly references an expression.
1179 Bexpression*
1180 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1181 bool known_valid, Location location)
1183 tree expr_tree = expr->get_tree();
1184 tree type_tree = btype->get_tree();
1185 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1186 return this->error_expression();
1188 // If the type of EXPR is a recursive pointer type, then we
1189 // need to insert a cast before indirecting.
1190 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1191 if (VOID_TYPE_P(target_type_tree))
1192 expr_tree = fold_convert_loc(location.gcc_location(),
1193 build_pointer_type(type_tree), expr_tree);
1195 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1196 expr_tree);
1197 if (known_valid)
1198 TREE_THIS_NOTRAP(ret) = 1;
1199 return this->make_expression(ret);
1202 // Return an expression that declares a constant named NAME with the
1203 // constant value VAL in BTYPE.
1205 Bexpression*
1206 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1207 Bexpression* val, Location location)
1209 tree type_tree = btype->get_tree();
1210 tree const_val = val->get_tree();
1211 if (type_tree == error_mark_node || const_val == error_mark_node)
1212 return this->error_expression();
1214 tree name_tree = get_identifier_from_string(name);
1215 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1216 type_tree);
1217 DECL_INITIAL(decl) = const_val;
1218 TREE_CONSTANT(decl) = 1;
1219 TREE_READONLY(decl) = 1;
1221 go_preserve_from_gc(decl);
1222 return this->make_expression(decl);
1225 // Return a typed value as a constant integer.
1227 Bexpression*
1228 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1230 tree t = btype->get_tree();
1231 if (t == error_mark_node)
1232 return this->error_expression();
1234 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1235 return this->make_expression(ret);
1238 // Return a typed value as a constant floating-point number.
1240 Bexpression*
1241 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1243 tree t = btype->get_tree();
1244 tree ret;
1245 if (t == error_mark_node)
1246 return this->error_expression();
1248 REAL_VALUE_TYPE r1;
1249 real_from_mpfr(&r1, val, t, GMP_RNDN);
1250 REAL_VALUE_TYPE r2;
1251 real_convert(&r2, TYPE_MODE(t), &r1);
1252 ret = build_real(t, r2);
1253 return this->make_expression(ret);
1256 // Return a typed real and imaginary value as a constant complex number.
1258 Bexpression*
1259 Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
1261 tree t = btype->get_tree();
1262 tree ret;
1263 if (t == error_mark_node)
1264 return this->error_expression();
1266 REAL_VALUE_TYPE r1;
1267 real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
1268 REAL_VALUE_TYPE r2;
1269 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1271 REAL_VALUE_TYPE r3;
1272 real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
1273 REAL_VALUE_TYPE r4;
1274 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1276 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1277 build_real(TREE_TYPE(t), r4));
1278 return this->make_expression(ret);
1281 // Make a constant string expression.
1283 Bexpression*
1284 Gcc_backend::string_constant_expression(const std::string& val)
1286 tree index_type = build_index_type(size_int(val.length()));
1287 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1288 TYPE_QUAL_CONST);
1289 tree string_type = build_array_type(const_char_type, index_type);
1290 string_type = build_variant_type_copy(string_type);
1291 TYPE_STRING_FLAG(string_type) = 1;
1292 tree string_val = build_string(val.length(), val.data());
1293 TREE_TYPE(string_val) = string_type;
1295 return this->make_expression(string_val);
1298 // Make a constant boolean expression.
1300 Bexpression*
1301 Gcc_backend::boolean_constant_expression(bool val)
1303 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1304 return this->make_expression(bool_cst);
1307 // Return the real part of a complex expression.
1309 Bexpression*
1310 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1312 tree complex_tree = bcomplex->get_tree();
1313 if (complex_tree == error_mark_node)
1314 return this->error_expression();
1315 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1316 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1317 TREE_TYPE(TREE_TYPE(complex_tree)),
1318 complex_tree);
1319 return this->make_expression(ret);
1322 // Return the imaginary part of a complex expression.
1324 Bexpression*
1325 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1327 tree complex_tree = bcomplex->get_tree();
1328 if (complex_tree == error_mark_node)
1329 return this->error_expression();
1330 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1331 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1332 TREE_TYPE(TREE_TYPE(complex_tree)),
1333 complex_tree);
1334 return this->make_expression(ret);
1337 // Make a complex expression given its real and imaginary parts.
1339 Bexpression*
1340 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1341 Location location)
1343 tree real_tree = breal->get_tree();
1344 tree imag_tree = bimag->get_tree();
1345 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1346 return this->error_expression();
1347 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1348 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1349 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1350 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1351 build_complex_type(TREE_TYPE(real_tree)),
1352 real_tree, imag_tree);
1353 return this->make_expression(ret);
1356 // An expression that converts an expression to a different type.
1358 Bexpression*
1359 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1360 Location location)
1362 tree type_tree = type->get_tree();
1363 tree expr_tree = expr->get_tree();
1364 if (type_tree == error_mark_node
1365 || expr_tree == error_mark_node
1366 || TREE_TYPE(expr_tree) == error_mark_node)
1367 return this->error_expression();
1369 tree ret;
1370 if (this->type_size(type) == 0)
1372 // Do not convert zero-sized types.
1373 ret = expr_tree;
1375 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1376 ret = fold(convert_to_integer(type_tree, expr_tree));
1377 else if (TREE_CODE(type_tree) == REAL_TYPE)
1378 ret = fold(convert_to_real(type_tree, expr_tree));
1379 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1380 ret = fold(convert_to_complex(type_tree, expr_tree));
1381 else if (TREE_CODE(type_tree) == POINTER_TYPE
1382 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1383 ret = fold(convert_to_pointer(type_tree, expr_tree));
1384 else if (TREE_CODE(type_tree) == RECORD_TYPE
1385 || TREE_CODE(type_tree) == ARRAY_TYPE)
1386 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1387 type_tree, expr_tree);
1388 else
1389 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1391 return this->make_expression(ret);
1394 // Get the address of a function.
1396 Bexpression*
1397 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1399 tree func = bfunc->get_tree();
1400 if (func == error_mark_node)
1401 return this->error_expression();
1403 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1404 return this->make_expression(ret);
1407 // Get the address of an expression.
1409 Bexpression*
1410 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1412 tree expr = bexpr->get_tree();
1413 if (expr == error_mark_node)
1414 return this->error_expression();
1416 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1417 return this->make_expression(ret);
1420 // Return an expression for the field at INDEX in BSTRUCT.
1422 Bexpression*
1423 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1424 Location location)
1426 tree struct_tree = bstruct->get_tree();
1427 if (struct_tree == error_mark_node
1428 || TREE_TYPE(struct_tree) == error_mark_node)
1429 return this->error_expression();
1430 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1431 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1432 if (field == NULL_TREE)
1434 // This can happen for a type which refers to itself indirectly
1435 // and then turns out to be erroneous.
1436 return this->error_expression();
1438 for (unsigned int i = index; i > 0; --i)
1440 field = DECL_CHAIN(field);
1441 gcc_assert(field != NULL_TREE);
1443 if (TREE_TYPE(field) == error_mark_node)
1444 return this->error_expression();
1445 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1446 TREE_TYPE(field), struct_tree, field,
1447 NULL_TREE);
1448 if (TREE_CONSTANT(struct_tree))
1449 TREE_CONSTANT(ret) = 1;
1450 return this->make_expression(ret);
1453 // Return an expression that executes BSTAT before BEXPR.
1455 Bexpression*
1456 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1457 Location location)
1459 tree stat = bstat->get_tree();
1460 tree expr = bexpr->get_tree();
1461 if (stat == error_mark_node || expr == error_mark_node)
1462 return this->error_expression();
1463 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1464 TREE_TYPE(expr), stat, expr);
1465 return this->make_expression(ret);
1468 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1469 // ELSE_EXPR otherwise.
1471 Bexpression*
1472 Gcc_backend::conditional_expression(Btype* btype, Bexpression* condition,
1473 Bexpression* then_expr,
1474 Bexpression* else_expr, Location location)
1476 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1477 tree cond_tree = condition->get_tree();
1478 tree then_tree = then_expr->get_tree();
1479 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1480 if (type_tree == error_mark_node
1481 || cond_tree == error_mark_node
1482 || then_tree == error_mark_node
1483 || else_tree == error_mark_node)
1484 return this->error_expression();
1485 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1486 cond_tree, then_tree, else_tree);
1487 return this->make_expression(ret);
1490 // Return an expression for the unary operation OP EXPR.
1492 Bexpression*
1493 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1495 tree expr_tree = expr->get_tree();
1496 if (expr_tree == error_mark_node
1497 || TREE_TYPE(expr_tree) == error_mark_node)
1498 return this->error_expression();
1500 tree type_tree = TREE_TYPE(expr_tree);
1501 enum tree_code code;
1502 switch (op)
1504 case OPERATOR_MINUS:
1506 tree computed_type = excess_precision_type(type_tree);
1507 if (computed_type != NULL_TREE)
1509 expr_tree = convert(computed_type, expr_tree);
1510 type_tree = computed_type;
1512 code = NEGATE_EXPR;
1513 break;
1515 case OPERATOR_NOT:
1516 code = TRUTH_NOT_EXPR;
1517 break;
1518 case OPERATOR_XOR:
1519 code = BIT_NOT_EXPR;
1520 break;
1521 default:
1522 gcc_unreachable();
1523 break;
1526 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1527 expr_tree);
1528 return this->make_expression(ret);
1531 // Convert a gofrontend operator to an equivalent tree_code.
1533 static enum tree_code
1534 operator_to_tree_code(Operator op, tree type)
1536 enum tree_code code;
1537 switch (op)
1539 case OPERATOR_EQEQ:
1540 code = EQ_EXPR;
1541 break;
1542 case OPERATOR_NOTEQ:
1543 code = NE_EXPR;
1544 break;
1545 case OPERATOR_LT:
1546 code = LT_EXPR;
1547 break;
1548 case OPERATOR_LE:
1549 code = LE_EXPR;
1550 break;
1551 case OPERATOR_GT:
1552 code = GT_EXPR;
1553 break;
1554 case OPERATOR_GE:
1555 code = GE_EXPR;
1556 break;
1557 case OPERATOR_OROR:
1558 code = TRUTH_ORIF_EXPR;
1559 break;
1560 case OPERATOR_ANDAND:
1561 code = TRUTH_ANDIF_EXPR;
1562 break;
1563 case OPERATOR_PLUS:
1564 code = PLUS_EXPR;
1565 break;
1566 case OPERATOR_MINUS:
1567 code = MINUS_EXPR;
1568 break;
1569 case OPERATOR_OR:
1570 code = BIT_IOR_EXPR;
1571 break;
1572 case OPERATOR_XOR:
1573 code = BIT_XOR_EXPR;
1574 break;
1575 case OPERATOR_MULT:
1576 code = MULT_EXPR;
1577 break;
1578 case OPERATOR_DIV:
1579 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1580 code = RDIV_EXPR;
1581 else
1582 code = TRUNC_DIV_EXPR;
1583 break;
1584 case OPERATOR_MOD:
1585 code = TRUNC_MOD_EXPR;
1586 break;
1587 case OPERATOR_LSHIFT:
1588 code = LSHIFT_EXPR;
1589 break;
1590 case OPERATOR_RSHIFT:
1591 code = RSHIFT_EXPR;
1592 break;
1593 case OPERATOR_AND:
1594 code = BIT_AND_EXPR;
1595 break;
1596 case OPERATOR_BITCLEAR:
1597 code = BIT_AND_EXPR;
1598 break;
1599 default:
1600 gcc_unreachable();
1603 return code;
1606 // Return an expression for the binary operation LEFT OP RIGHT.
1608 Bexpression*
1609 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1610 Bexpression* right, Location location)
1612 tree left_tree = left->get_tree();
1613 tree right_tree = right->get_tree();
1614 if (left_tree == error_mark_node
1615 || right_tree == error_mark_node)
1616 return this->error_expression();
1617 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1619 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1620 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1621 tree computed_type = excess_precision_type(type_tree);
1622 if (computed_type != NULL_TREE)
1624 left_tree = convert(computed_type, left_tree);
1625 right_tree = convert(computed_type, right_tree);
1626 type_tree = computed_type;
1629 // For comparison operators, the resulting type should be boolean.
1630 switch (op)
1632 case OPERATOR_EQEQ:
1633 case OPERATOR_NOTEQ:
1634 case OPERATOR_LT:
1635 case OPERATOR_LE:
1636 case OPERATOR_GT:
1637 case OPERATOR_GE:
1638 type_tree = boolean_type_node;
1639 break;
1640 default:
1641 break;
1644 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1645 left_tree, right_tree);
1646 return this->make_expression(ret);
1649 // Return an expression that constructs BTYPE with VALS.
1651 Bexpression*
1652 Gcc_backend::constructor_expression(Btype* btype,
1653 const std::vector<Bexpression*>& vals,
1654 Location location)
1656 tree type_tree = btype->get_tree();
1657 if (type_tree == error_mark_node)
1658 return this->error_expression();
1660 vec<constructor_elt, va_gc> *init;
1661 vec_alloc(init, vals.size());
1663 tree sink = NULL_TREE;
1664 bool is_constant = true;
1665 tree field = TYPE_FIELDS(type_tree);
1666 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1667 p != vals.end();
1668 ++p, field = DECL_CHAIN(field))
1670 gcc_assert(field != NULL_TREE);
1671 tree val = (*p)->get_tree();
1672 if (TREE_TYPE(field) == error_mark_node
1673 || val == error_mark_node
1674 || TREE_TYPE(val) == error_mark_node)
1675 return this->error_expression();
1677 if (int_size_in_bytes(TREE_TYPE(field)) == 0)
1679 // GIMPLE cannot represent indices of zero-sized types so
1680 // trying to construct a map with zero-sized keys might lead
1681 // to errors. Instead, we evaluate each expression that
1682 // would have been added as a map element for its
1683 // side-effects and construct an empty map.
1684 append_to_statement_list(val, &sink);
1685 continue;
1688 constructor_elt empty = {NULL, NULL};
1689 constructor_elt* elt = init->quick_push(empty);
1690 elt->index = field;
1691 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
1692 val);
1693 if (!TREE_CONSTANT(elt->value))
1694 is_constant = false;
1696 gcc_assert(field == NULL_TREE);
1697 tree ret = build_constructor(type_tree, init);
1698 if (is_constant)
1699 TREE_CONSTANT(ret) = 1;
1700 if (sink != NULL_TREE)
1701 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1702 type_tree, sink, ret);
1703 return this->make_expression(ret);
1706 Bexpression*
1707 Gcc_backend::array_constructor_expression(
1708 Btype* array_btype, const std::vector<unsigned long>& indexes,
1709 const std::vector<Bexpression*>& vals, Location location)
1711 tree type_tree = array_btype->get_tree();
1712 if (type_tree == error_mark_node)
1713 return this->error_expression();
1715 gcc_assert(indexes.size() == vals.size());
1717 tree element_type = TREE_TYPE(type_tree);
1718 HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
1719 vec<constructor_elt, va_gc> *init;
1720 vec_alloc(init, element_size == 0 ? 0 : vals.size());
1722 tree sink = NULL_TREE;
1723 bool is_constant = true;
1724 for (size_t i = 0; i < vals.size(); ++i)
1726 tree index = size_int(indexes[i]);
1727 tree val = (vals[i])->get_tree();
1729 if (index == error_mark_node
1730 || val == error_mark_node)
1731 return this->error_expression();
1733 if (element_size == 0)
1735 // GIMPLE cannot represent arrays of zero-sized types so trying
1736 // to construct an array of zero-sized values might lead to errors.
1737 // Instead, we evaluate each expression that would have been added as
1738 // an array value for its side-effects and construct an empty array.
1739 append_to_statement_list(val, &sink);
1740 continue;
1743 if (!TREE_CONSTANT(val))
1744 is_constant = false;
1746 constructor_elt empty = {NULL, NULL};
1747 constructor_elt* elt = init->quick_push(empty);
1748 elt->index = index;
1749 elt->value = val;
1752 tree ret = build_constructor(type_tree, init);
1753 if (is_constant)
1754 TREE_CONSTANT(ret) = 1;
1755 if (sink != NULL_TREE)
1756 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1757 type_tree, sink, ret);
1758 return this->make_expression(ret);
1761 // Return an expression for the address of BASE[INDEX].
1763 Bexpression*
1764 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1765 Location location)
1767 tree base_tree = base->get_tree();
1768 tree index_tree = index->get_tree();
1769 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1770 if (base_tree == error_mark_node
1771 || TREE_TYPE(base_tree) == error_mark_node
1772 || index_tree == error_mark_node
1773 || element_type_tree == error_mark_node)
1774 return this->error_expression();
1776 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1777 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1778 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1779 index_tree, element_size);
1780 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1781 TREE_TYPE(base_tree), base_tree, offset);
1782 return this->make_expression(ptr);
1785 // Return an expression representing ARRAY[INDEX]
1787 Bexpression*
1788 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1789 Location location)
1791 tree array_tree = array->get_tree();
1792 tree index_tree = index->get_tree();
1793 if (array_tree == error_mark_node
1794 || TREE_TYPE(array_tree) == error_mark_node
1795 || index_tree == error_mark_node)
1796 return this->error_expression();
1798 tree ret = build4_loc(location.gcc_location(), ARRAY_REF,
1799 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1800 index_tree, NULL_TREE, NULL_TREE);
1801 return this->make_expression(ret);
1804 // Create an expression for a call to FN_EXPR with FN_ARGS.
1805 Bexpression*
1806 Gcc_backend::call_expression(Bexpression* fn_expr,
1807 const std::vector<Bexpression*>& fn_args,
1808 Bexpression* chain_expr, Location location)
1810 tree fn = fn_expr->get_tree();
1811 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1812 return this->error_expression();
1814 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1815 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1817 size_t nargs = fn_args.size();
1818 tree* args = nargs == 0 ? NULL : new tree[nargs];
1819 for (size_t i = 0; i < nargs; ++i)
1821 args[i] = fn_args.at(i)->get_tree();
1822 if (args[i] == error_mark_node)
1823 return this->error_expression();
1826 tree fndecl = fn;
1827 if (TREE_CODE(fndecl) == ADDR_EXPR)
1828 fndecl = TREE_OPERAND(fndecl, 0);
1830 // This is to support builtin math functions when using 80387 math.
1831 tree excess_type = NULL_TREE;
1832 if (optimize
1833 && TREE_CODE(fndecl) == FUNCTION_DECL
1834 && DECL_IS_BUILTIN(fndecl)
1835 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
1836 && nargs > 0
1837 && ((SCALAR_FLOAT_TYPE_P(rettype)
1838 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1839 || (COMPLEX_FLOAT_TYPE_P(rettype)
1840 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1842 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1843 if (excess_type != NULL_TREE)
1845 tree excess_fndecl = mathfn_built_in(excess_type,
1846 DECL_FUNCTION_CODE(fndecl));
1847 if (excess_fndecl == NULL_TREE)
1848 excess_type = NULL_TREE;
1849 else
1851 fn = build_fold_addr_expr_loc(location.gcc_location(),
1852 excess_fndecl);
1853 for (size_t i = 0; i < nargs; ++i)
1855 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1856 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1857 args[i] = ::convert(excess_type, args[i]);
1863 tree ret =
1864 build_call_array_loc(location.gcc_location(),
1865 excess_type != NULL_TREE ? excess_type : rettype,
1866 fn, nargs, args);
1868 if (chain_expr)
1869 CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree();
1871 if (excess_type != NULL_TREE)
1873 // Calling convert here can undo our excess precision change.
1874 // That may or may not be a bug in convert_to_real.
1875 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1878 delete[] args;
1879 return this->make_expression(ret);
1882 // Return an expression that allocates SIZE bytes on the stack.
1884 Bexpression*
1885 Gcc_backend::stack_allocation_expression(int64_t size, Location location)
1887 tree alloca = builtin_decl_explicit(BUILT_IN_ALLOCA);
1888 tree size_tree = build_int_cst(integer_type_node, size);
1889 tree ret = build_call_expr_loc(location.gcc_location(), alloca, 1, size_tree);
1890 return this->make_expression(ret);
1893 // An expression as a statement.
1895 Bstatement*
1896 Gcc_backend::expression_statement(Bexpression* expr)
1898 return this->make_statement(expr->get_tree());
1901 // Variable initialization.
1903 Bstatement*
1904 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1906 tree var_tree = var->get_tree();
1907 tree init_tree = init->get_tree();
1908 if (var_tree == error_mark_node || init_tree == error_mark_node)
1909 return this->error_statement();
1910 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1912 // To avoid problems with GNU ld, we don't make zero-sized
1913 // externally visible variables. That might lead us to doing an
1914 // initialization of a zero-sized expression to a non-zero sized
1915 // variable, or vice-versa. Avoid crashes by omitting the
1916 // initializer. Such initializations don't mean anything anyhow.
1917 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1918 && init_tree != NULL_TREE
1919 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1921 DECL_INITIAL(var_tree) = init_tree;
1922 init_tree = NULL_TREE;
1925 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1926 void_type_node, var_tree);
1927 if (init_tree != NULL_TREE)
1928 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1929 void_type_node, init_tree, ret);
1931 return this->make_statement(ret);
1934 // Assignment.
1936 Bstatement*
1937 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1938 Location location)
1940 tree lhs_tree = lhs->get_tree();
1941 tree rhs_tree = rhs->get_tree();
1942 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1943 return this->error_statement();
1945 // To avoid problems with GNU ld, we don't make zero-sized
1946 // externally visible variables. That might lead us to doing an
1947 // assignment of a zero-sized expression to a non-zero sized
1948 // expression; avoid crashes here by avoiding assignments of
1949 // zero-sized expressions. Such assignments don't really mean
1950 // anything anyhow.
1951 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1952 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1953 return this->compound_statement(this->expression_statement(lhs),
1954 this->expression_statement(rhs));
1956 // Sometimes the same unnamed Go type can be created multiple times
1957 // and thus have multiple tree representations. Make sure this does
1958 // not confuse the middle-end.
1959 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1961 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1962 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1963 if (POINTER_TYPE_P(lhs_type_tree)
1964 || INTEGRAL_TYPE_P(lhs_type_tree)
1965 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1966 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1967 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1968 rhs_tree);
1969 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1970 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1972 gcc_assert(int_size_in_bytes(lhs_type_tree)
1973 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1974 rhs_tree = fold_build1_loc(location.gcc_location(),
1975 VIEW_CONVERT_EXPR,
1976 lhs_type_tree, rhs_tree);
1980 return this->make_statement(fold_build2_loc(location.gcc_location(),
1981 MODIFY_EXPR,
1982 void_type_node,
1983 lhs_tree, rhs_tree));
1986 // Return.
1988 Bstatement*
1989 Gcc_backend::return_statement(Bfunction* bfunction,
1990 const std::vector<Bexpression*>& vals,
1991 Location location)
1993 tree fntree = bfunction->get_tree();
1994 if (fntree == error_mark_node)
1995 return this->error_statement();
1996 tree result = DECL_RESULT(fntree);
1997 if (result == error_mark_node)
1998 return this->error_statement();
2000 tree ret;
2001 if (vals.empty())
2002 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
2003 NULL_TREE);
2004 else if (vals.size() == 1)
2006 tree val = vals.front()->get_tree();
2007 if (val == error_mark_node)
2008 return this->error_statement();
2009 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2010 void_type_node, result,
2011 vals.front()->get_tree());
2012 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2013 void_type_node, set);
2015 else
2017 // To return multiple values, copy the values into a temporary
2018 // variable of the right structure type, and then assign the
2019 // temporary variable to the DECL_RESULT in the return
2020 // statement.
2021 tree stmt_list = NULL_TREE;
2022 tree rettype = TREE_TYPE(result);
2024 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2025 push_struct_function(fntree);
2026 else
2027 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2028 tree rettmp = create_tmp_var(rettype, "RESULT");
2029 pop_cfun();
2031 tree field = TYPE_FIELDS(rettype);
2032 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2033 p != vals.end();
2034 p++, field = DECL_CHAIN(field))
2036 gcc_assert(field != NULL_TREE);
2037 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
2038 TREE_TYPE(field), rettmp, field,
2039 NULL_TREE);
2040 tree val = (*p)->get_tree();
2041 if (val == error_mark_node)
2042 return this->error_statement();
2043 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2044 void_type_node,
2045 ref, (*p)->get_tree());
2046 append_to_statement_list(set, &stmt_list);
2048 gcc_assert(field == NULL_TREE);
2049 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2050 void_type_node,
2051 result, rettmp);
2052 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2053 void_type_node, set);
2054 append_to_statement_list(ret_expr, &stmt_list);
2055 ret = stmt_list;
2057 return this->make_statement(ret);
2060 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2061 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2062 // NULL, it will always be executed. This is used for handling defers in Go
2063 // functions. In C++, the resulting code is of this form:
2064 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2066 Bstatement*
2067 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2068 Bstatement* except_stmt,
2069 Bstatement* finally_stmt,
2070 Location location)
2072 tree stat_tree = bstat->get_tree();
2073 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2074 tree finally_tree = finally_stmt == NULL
2075 ? NULL_TREE
2076 : finally_stmt->get_tree();
2078 if (stat_tree == error_mark_node
2079 || except_tree == error_mark_node
2080 || finally_tree == error_mark_node)
2081 return this->error_statement();
2083 if (except_tree != NULL_TREE)
2084 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2085 void_type_node, stat_tree,
2086 build2_loc(location.gcc_location(), CATCH_EXPR,
2087 void_type_node, NULL, except_tree));
2088 if (finally_tree != NULL_TREE)
2089 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2090 void_type_node, stat_tree, finally_tree);
2091 return this->make_statement(stat_tree);
2094 // If.
2096 Bstatement*
2097 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
2098 Bblock* else_block, Location location)
2100 tree cond_tree = condition->get_tree();
2101 tree then_tree = then_block->get_tree();
2102 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2103 if (cond_tree == error_mark_node
2104 || then_tree == error_mark_node
2105 || else_tree == error_mark_node)
2106 return this->error_statement();
2107 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2108 cond_tree, then_tree, else_tree);
2109 return this->make_statement(ret);
2112 // Switch.
2114 Bstatement*
2115 Gcc_backend::switch_statement(
2116 Bfunction* function,
2117 Bexpression* value,
2118 const std::vector<std::vector<Bexpression*> >& cases,
2119 const std::vector<Bstatement*>& statements,
2120 Location switch_location)
2122 gcc_assert(cases.size() == statements.size());
2124 tree decl = function->get_tree();
2125 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2126 push_struct_function(decl);
2127 else
2128 push_cfun(DECL_STRUCT_FUNCTION(decl));
2130 tree stmt_list = NULL_TREE;
2131 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2132 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2133 ps != statements.end();
2134 ++ps, ++pc)
2136 if (pc->empty())
2138 source_location loc = (*ps != NULL
2139 ? EXPR_LOCATION((*ps)->get_tree())
2140 : UNKNOWN_LOCATION);
2141 tree label = create_artificial_label(loc);
2142 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2143 append_to_statement_list(c, &stmt_list);
2145 else
2147 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2148 pcv != pc->end();
2149 ++pcv)
2151 tree t = (*pcv)->get_tree();
2152 if (t == error_mark_node)
2153 return this->error_statement();
2154 source_location loc = EXPR_LOCATION(t);
2155 tree label = create_artificial_label(loc);
2156 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2157 append_to_statement_list(c, &stmt_list);
2161 if (*ps != NULL)
2163 tree t = (*ps)->get_tree();
2164 if (t == error_mark_node)
2165 return this->error_statement();
2166 append_to_statement_list(t, &stmt_list);
2169 pop_cfun();
2171 tree tv = value->get_tree();
2172 if (tv == error_mark_node)
2173 return this->error_statement();
2174 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
2175 NULL_TREE, tv, stmt_list, NULL_TREE);
2176 return this->make_statement(t);
2179 // Pair of statements.
2181 Bstatement*
2182 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2184 tree stmt_list = NULL_TREE;
2185 tree t = s1->get_tree();
2186 if (t == error_mark_node)
2187 return this->error_statement();
2188 append_to_statement_list(t, &stmt_list);
2189 t = s2->get_tree();
2190 if (t == error_mark_node)
2191 return this->error_statement();
2192 append_to_statement_list(t, &stmt_list);
2194 // If neither statement has any side effects, stmt_list can be NULL
2195 // at this point.
2196 if (stmt_list == NULL_TREE)
2197 stmt_list = integer_zero_node;
2199 return this->make_statement(stmt_list);
2202 // List of statements.
2204 Bstatement*
2205 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2207 tree stmt_list = NULL_TREE;
2208 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2209 p != statements.end();
2210 ++p)
2212 tree t = (*p)->get_tree();
2213 if (t == error_mark_node)
2214 return this->error_statement();
2215 append_to_statement_list(t, &stmt_list);
2217 return this->make_statement(stmt_list);
2220 // Make a block. For some reason gcc uses a dual structure for
2221 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2222 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2223 // the Bblock.
2225 Bblock*
2226 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2227 const std::vector<Bvariable*>& vars,
2228 Location start_location,
2229 Location)
2231 tree block_tree = make_node(BLOCK);
2232 if (enclosing == NULL)
2234 tree fndecl = function->get_tree();
2235 gcc_assert(fndecl != NULL_TREE);
2237 // We may have already created a block for local variables when
2238 // we take the address of a parameter.
2239 if (DECL_INITIAL(fndecl) == NULL_TREE)
2241 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2242 DECL_INITIAL(fndecl) = block_tree;
2244 else
2246 tree superblock_tree = DECL_INITIAL(fndecl);
2247 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2248 tree* pp;
2249 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2250 *pp != NULL_TREE;
2251 pp = &BLOCK_CHAIN(*pp))
2253 *pp = block_tree;
2256 else
2258 tree superbind_tree = enclosing->get_tree();
2259 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2260 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2262 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2263 tree* pp;
2264 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2265 *pp != NULL_TREE;
2266 pp = &BLOCK_CHAIN(*pp))
2268 *pp = block_tree;
2271 tree* pp = &BLOCK_VARS(block_tree);
2272 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2273 pv != vars.end();
2274 ++pv)
2276 *pp = (*pv)->get_tree();
2277 if (*pp != error_mark_node)
2278 pp = &DECL_CHAIN(*pp);
2280 *pp = NULL_TREE;
2282 TREE_USED(block_tree) = 1;
2284 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2285 void_type_node, BLOCK_VARS(block_tree),
2286 NULL_TREE, block_tree);
2287 TREE_SIDE_EFFECTS(bind_tree) = 1;
2288 return new Bblock(bind_tree);
2291 // Add statements to a block.
2293 void
2294 Gcc_backend::block_add_statements(Bblock* bblock,
2295 const std::vector<Bstatement*>& statements)
2297 tree stmt_list = NULL_TREE;
2298 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2299 p != statements.end();
2300 ++p)
2302 tree s = (*p)->get_tree();
2303 if (s != error_mark_node)
2304 append_to_statement_list(s, &stmt_list);
2307 tree bind_tree = bblock->get_tree();
2308 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2309 BIND_EXPR_BODY(bind_tree) = stmt_list;
2312 // Return a block as a statement.
2314 Bstatement*
2315 Gcc_backend::block_statement(Bblock* bblock)
2317 tree bind_tree = bblock->get_tree();
2318 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2319 return this->make_statement(bind_tree);
2322 // This is not static because we declare it with GTY(()) in go-c.h.
2323 tree go_non_zero_struct;
2325 // Return a type corresponding to TYPE with non-zero size.
2327 tree
2328 Gcc_backend::non_zero_size_type(tree type)
2330 if (int_size_in_bytes(type) != 0)
2331 return type;
2333 switch (TREE_CODE(type))
2335 case RECORD_TYPE:
2336 if (TYPE_FIELDS(type) != NULL_TREE)
2338 tree ns = make_node(RECORD_TYPE);
2339 tree field_trees = NULL_TREE;
2340 tree *pp = &field_trees;
2341 for (tree field = TYPE_FIELDS(type);
2342 field != NULL_TREE;
2343 field = DECL_CHAIN(field))
2345 tree ft = TREE_TYPE(field);
2346 if (field == TYPE_FIELDS(type))
2347 ft = non_zero_size_type(ft);
2348 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2349 DECL_NAME(field), ft);
2350 DECL_CONTEXT(f) = ns;
2351 *pp = f;
2352 pp = &DECL_CHAIN(f);
2354 TYPE_FIELDS(ns) = field_trees;
2355 layout_type(ns);
2356 return ns;
2359 if (go_non_zero_struct == NULL_TREE)
2361 type = make_node(RECORD_TYPE);
2362 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2363 get_identifier("dummy"),
2364 boolean_type_node);
2365 DECL_CONTEXT(field) = type;
2366 TYPE_FIELDS(type) = field;
2367 layout_type(type);
2368 go_non_zero_struct = type;
2370 return go_non_zero_struct;
2372 case ARRAY_TYPE:
2374 tree element_type = non_zero_size_type(TREE_TYPE(type));
2375 return build_array_type_nelts(element_type, 1);
2378 default:
2379 gcc_unreachable();
2382 gcc_unreachable();
2385 // Make a global variable.
2387 Bvariable*
2388 Gcc_backend::global_variable(const std::string& package_name,
2389 const std::string& pkgpath,
2390 const std::string& name,
2391 Btype* btype,
2392 bool is_external,
2393 bool is_hidden,
2394 bool in_unique_section,
2395 Location location)
2397 tree type_tree = btype->get_tree();
2398 if (type_tree == error_mark_node)
2399 return this->error_variable();
2401 // The GNU linker does not like dynamic variables with zero size.
2402 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2403 type_tree = this->non_zero_size_type(type_tree);
2405 std::string var_name(package_name);
2406 var_name.push_back('.');
2407 var_name.append(name);
2408 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2409 get_identifier_from_string(var_name),
2410 type_tree);
2411 if (is_external)
2412 DECL_EXTERNAL(decl) = 1;
2413 else
2414 TREE_STATIC(decl) = 1;
2415 if (!is_hidden)
2417 TREE_PUBLIC(decl) = 1;
2419 std::string asm_name(pkgpath);
2420 asm_name.push_back('.');
2421 asm_name.append(name);
2422 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2424 TREE_USED(decl) = 1;
2426 if (in_unique_section)
2427 resolve_unique_section (decl, 0, 1);
2429 go_preserve_from_gc(decl);
2431 return new Bvariable(decl);
2434 // Set the initial value of a global variable.
2436 void
2437 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2439 tree expr_tree = expr->get_tree();
2440 if (expr_tree == error_mark_node)
2441 return;
2442 gcc_assert(TREE_CONSTANT(expr_tree));
2443 tree var_decl = var->get_tree();
2444 if (var_decl == error_mark_node)
2445 return;
2446 DECL_INITIAL(var_decl) = expr_tree;
2448 // If this variable goes in a unique section, it may need to go into
2449 // a different one now that DECL_INITIAL is set.
2450 if (symtab_node::get(var_decl)
2451 && symtab_node::get(var_decl)->implicit_section)
2453 set_decl_section_name (var_decl, NULL);
2454 resolve_unique_section (var_decl,
2455 compute_reloc_for_constant (expr_tree),
2460 // Make a local variable.
2462 Bvariable*
2463 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2464 Btype* btype, bool is_address_taken,
2465 Location location)
2467 tree type_tree = btype->get_tree();
2468 if (type_tree == error_mark_node)
2469 return this->error_variable();
2470 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2471 get_identifier_from_string(name),
2472 type_tree);
2473 DECL_CONTEXT(decl) = function->get_tree();
2474 TREE_USED(decl) = 1;
2475 if (is_address_taken)
2476 TREE_ADDRESSABLE(decl) = 1;
2477 go_preserve_from_gc(decl);
2478 return new Bvariable(decl);
2481 // Make a function parameter variable.
2483 Bvariable*
2484 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2485 Btype* btype, bool is_address_taken,
2486 Location location)
2488 tree type_tree = btype->get_tree();
2489 if (type_tree == error_mark_node)
2490 return this->error_variable();
2491 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2492 get_identifier_from_string(name),
2493 type_tree);
2494 DECL_CONTEXT(decl) = function->get_tree();
2495 DECL_ARG_TYPE(decl) = type_tree;
2496 TREE_USED(decl) = 1;
2497 if (is_address_taken)
2498 TREE_ADDRESSABLE(decl) = 1;
2499 go_preserve_from_gc(decl);
2500 return new Bvariable(decl);
2503 // Make a static chain variable.
2505 Bvariable*
2506 Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name,
2507 Btype* btype, Location location)
2509 tree type_tree = btype->get_tree();
2510 if (type_tree == error_mark_node)
2511 return this->error_variable();
2512 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2513 get_identifier_from_string(name), type_tree);
2514 tree fndecl = function->get_tree();
2515 DECL_CONTEXT(decl) = fndecl;
2516 DECL_ARG_TYPE(decl) = type_tree;
2517 TREE_USED(decl) = 1;
2518 DECL_ARTIFICIAL(decl) = 1;
2519 DECL_IGNORED_P(decl) = 1;
2520 TREE_READONLY(decl) = 1;
2522 struct function *f = DECL_STRUCT_FUNCTION(fndecl);
2523 if (f == NULL)
2525 push_struct_function(fndecl);
2526 pop_cfun();
2527 f = DECL_STRUCT_FUNCTION(fndecl);
2529 gcc_assert(f->static_chain_decl == NULL);
2530 f->static_chain_decl = decl;
2531 DECL_STATIC_CHAIN(fndecl) = 1;
2533 go_preserve_from_gc(decl);
2534 return new Bvariable(decl);
2537 // Make a temporary variable.
2539 Bvariable*
2540 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2541 Btype* btype, Bexpression* binit,
2542 bool is_address_taken,
2543 Location location,
2544 Bstatement** pstatement)
2546 gcc_assert(function != NULL);
2547 tree decl = function->get_tree();
2548 tree type_tree = btype->get_tree();
2549 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2550 if (type_tree == error_mark_node
2551 || init_tree == error_mark_node
2552 || decl == error_mark_node)
2554 *pstatement = this->error_statement();
2555 return this->error_variable();
2558 tree var;
2559 // We can only use create_tmp_var if the type is not addressable.
2560 if (!TREE_ADDRESSABLE(type_tree))
2562 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2563 push_struct_function(decl);
2564 else
2565 push_cfun(DECL_STRUCT_FUNCTION(decl));
2567 var = create_tmp_var(type_tree, "GOTMP");
2568 pop_cfun();
2570 else
2572 gcc_assert(bblock != NULL);
2573 var = build_decl(location.gcc_location(), VAR_DECL,
2574 create_tmp_var_name("GOTMP"),
2575 type_tree);
2576 DECL_ARTIFICIAL(var) = 1;
2577 DECL_IGNORED_P(var) = 1;
2578 TREE_USED(var) = 1;
2579 DECL_CONTEXT(var) = decl;
2581 // We have to add this variable to the BLOCK and the BIND_EXPR.
2582 tree bind_tree = bblock->get_tree();
2583 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2584 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2585 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2586 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2587 BLOCK_VARS(block_tree) = var;
2588 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2591 if (this->type_size(btype) != 0 && init_tree != NULL_TREE)
2592 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
2593 init_tree);
2595 if (is_address_taken)
2596 TREE_ADDRESSABLE(var) = 1;
2598 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2599 DECL_EXPR,
2600 void_type_node, var));
2602 // Don't initialize VAR with BINIT, but still evaluate BINIT for
2603 // its side effects.
2604 if (this->type_size(btype) == 0 && init_tree != NULL_TREE)
2605 *pstatement = this->compound_statement(this->expression_statement(binit),
2606 *pstatement);
2608 return new Bvariable(var);
2611 // Create an implicit variable that is compiler-defined. This is used when
2612 // generating GC root variables and storing the values of a slice initializer.
2614 Bvariable*
2615 Gcc_backend::implicit_variable(const std::string& name, Btype* type,
2616 bool is_hidden, bool is_constant,
2617 bool is_common, int64_t alignment)
2619 tree type_tree = type->get_tree();
2620 if (type_tree == error_mark_node)
2621 return this->error_variable();
2623 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2624 get_identifier_from_string(name), type_tree);
2625 DECL_EXTERNAL(decl) = 0;
2626 TREE_PUBLIC(decl) = !is_hidden;
2627 TREE_STATIC(decl) = 1;
2628 TREE_USED(decl) = 1;
2629 DECL_ARTIFICIAL(decl) = 1;
2630 if (is_common)
2632 DECL_COMMON(decl) = 1;
2634 // When the initializer for one implicit_variable refers to another,
2635 // it needs to know the visibility of the referenced struct so that
2636 // compute_reloc_for_constant will return the right value. On many
2637 // systems calling make_decl_one_only will mark the decl as weak,
2638 // which will change the return value of compute_reloc_for_constant.
2639 // We can't reliably call make_decl_one_only yet, because we don't
2640 // yet know the initializer. This issue doesn't arise in C because
2641 // Go initializers, unlike C initializers, can be indirectly
2642 // recursive. To ensure that compute_reloc_for_constant computes
2643 // the right value if some other initializer refers to this one, we
2644 // mark this symbol as weak here. We undo that below in
2645 // immutable_struct_set_init before calling mark_decl_one_only.
2646 DECL_WEAK(decl) = 1;
2648 if (is_constant)
2650 TREE_READONLY(decl) = 1;
2651 TREE_CONSTANT(decl) = 1;
2653 if (alignment != 0)
2655 DECL_ALIGN(decl) = alignment * BITS_PER_UNIT;
2656 DECL_USER_ALIGN(decl) = 1;
2659 go_preserve_from_gc(decl);
2660 return new Bvariable(decl);
2663 // Set the initalizer for a variable created by implicit_variable.
2664 // This is where we finish compiling the variable.
2666 void
2667 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
2668 Btype*, bool, bool, bool is_common,
2669 Bexpression* init)
2671 tree decl = var->get_tree();
2672 tree init_tree;
2673 if (init == NULL)
2674 init_tree = NULL_TREE;
2675 else
2676 init_tree = init->get_tree();
2677 if (decl == error_mark_node || init_tree == error_mark_node)
2678 return;
2680 DECL_INITIAL(decl) = init_tree;
2682 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2683 // See the comment where DECL_WEAK is set in implicit_variable.
2684 if (is_common)
2686 DECL_WEAK(decl) = 0;
2687 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2690 resolve_unique_section(decl, 2, 1);
2692 rest_of_decl_compilation(decl, 1, 0);
2695 // Return a reference to an implicit variable defined in another package.
2697 Bvariable*
2698 Gcc_backend::implicit_variable_reference(const std::string& name, Btype* btype)
2700 tree type_tree = btype->get_tree();
2701 if (type_tree == error_mark_node)
2702 return this->error_variable();
2704 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2705 get_identifier_from_string(name), type_tree);
2706 DECL_EXTERNAL(decl) = 0;
2707 TREE_PUBLIC(decl) = 1;
2708 TREE_STATIC(decl) = 1;
2709 DECL_ARTIFICIAL(decl) = 1;
2710 go_preserve_from_gc(decl);
2711 return new Bvariable(decl);
2714 // Create a named immutable initialized data structure.
2716 Bvariable*
2717 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
2718 bool is_common, Btype* btype, Location location)
2720 tree type_tree = btype->get_tree();
2721 if (type_tree == error_mark_node)
2722 return this->error_variable();
2723 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2724 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2725 get_identifier_from_string(name),
2726 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2727 TREE_STATIC(decl) = 1;
2728 TREE_USED(decl) = 1;
2729 TREE_READONLY(decl) = 1;
2730 TREE_CONSTANT(decl) = 1;
2731 DECL_ARTIFICIAL(decl) = 1;
2732 if (!is_hidden)
2733 TREE_PUBLIC(decl) = 1;
2735 // When the initializer for one immutable_struct refers to another,
2736 // it needs to know the visibility of the referenced struct so that
2737 // compute_reloc_for_constant will return the right value. On many
2738 // systems calling make_decl_one_only will mark the decl as weak,
2739 // which will change the return value of compute_reloc_for_constant.
2740 // We can't reliably call make_decl_one_only yet, because we don't
2741 // yet know the initializer. This issue doesn't arise in C because
2742 // Go initializers, unlike C initializers, can be indirectly
2743 // recursive. To ensure that compute_reloc_for_constant computes
2744 // the right value if some other initializer refers to this one, we
2745 // mark this symbol as weak here. We undo that below in
2746 // immutable_struct_set_init before calling mark_decl_one_only.
2747 if (is_common)
2748 DECL_WEAK(decl) = 1;
2750 // We don't call rest_of_decl_compilation until we have the
2751 // initializer.
2753 go_preserve_from_gc(decl);
2754 return new Bvariable(decl);
2757 // Set the initializer for a variable created by immutable_struct.
2758 // This is where we finish compiling the variable.
2760 void
2761 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2762 bool, bool is_common, Btype*, Location,
2763 Bexpression* initializer)
2765 tree decl = var->get_tree();
2766 tree init_tree = initializer->get_tree();
2767 if (decl == error_mark_node || init_tree == error_mark_node)
2768 return;
2770 DECL_INITIAL(decl) = init_tree;
2772 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2773 // See the comment where DECL_WEAK is set in immutable_struct.
2774 if (is_common)
2776 DECL_WEAK(decl) = 0;
2777 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2780 // These variables are often unneeded in the final program, so put
2781 // them in their own section so that linker GC can discard them.
2782 resolve_unique_section(decl,
2783 compute_reloc_for_constant (init_tree),
2786 rest_of_decl_compilation(decl, 1, 0);
2789 // Return a reference to an immutable initialized data structure
2790 // defined in another package.
2792 Bvariable*
2793 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
2794 Location location)
2796 tree type_tree = btype->get_tree();
2797 if (type_tree == error_mark_node)
2798 return this->error_variable();
2799 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2800 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2801 get_identifier_from_string(name),
2802 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2803 TREE_READONLY(decl) = 1;
2804 TREE_CONSTANT(decl) = 1;
2805 DECL_ARTIFICIAL(decl) = 1;
2806 TREE_PUBLIC(decl) = 1;
2807 DECL_EXTERNAL(decl) = 1;
2808 go_preserve_from_gc(decl);
2809 return new Bvariable(decl);
2812 // Make a label.
2814 Blabel*
2815 Gcc_backend::label(Bfunction* function, const std::string& name,
2816 Location location)
2818 tree decl;
2819 if (name.empty())
2821 tree func_tree = function->get_tree();
2822 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2823 push_struct_function(func_tree);
2824 else
2825 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2827 decl = create_artificial_label(location.gcc_location());
2829 pop_cfun();
2831 else
2833 tree id = get_identifier_from_string(name);
2834 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
2835 void_type_node);
2836 DECL_CONTEXT(decl) = function->get_tree();
2838 return new Blabel(decl);
2841 // Make a statement which defines a label.
2843 Bstatement*
2844 Gcc_backend::label_definition_statement(Blabel* label)
2846 tree lab = label->get_tree();
2847 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
2848 void_type_node, lab);
2849 return this->make_statement(ret);
2852 // Make a goto statement.
2854 Bstatement*
2855 Gcc_backend::goto_statement(Blabel* label, Location location)
2857 tree lab = label->get_tree();
2858 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
2859 lab);
2860 return this->make_statement(ret);
2863 // Get the address of a label.
2865 Bexpression*
2866 Gcc_backend::label_address(Blabel* label, Location location)
2868 tree lab = label->get_tree();
2869 TREE_USED(lab) = 1;
2870 TREE_ADDRESSABLE(lab) = 1;
2871 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
2872 build_fold_addr_expr_loc(location.gcc_location(),
2873 lab));
2874 return this->make_expression(ret);
2877 // Declare or define a new function.
2879 Bfunction*
2880 Gcc_backend::function(Btype* fntype, const std::string& name,
2881 const std::string& asm_name, bool is_visible,
2882 bool is_declaration, bool is_inlinable,
2883 bool disable_split_stack, bool in_unique_section,
2884 Location location)
2886 tree functype = fntype->get_tree();
2887 if (functype != error_mark_node)
2889 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
2890 functype = TREE_TYPE(functype);
2892 tree id = get_identifier_from_string(name);
2893 if (functype == error_mark_node || id == error_mark_node)
2894 return this->error_function();
2896 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
2897 if (!asm_name.empty())
2898 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2899 if (is_visible)
2900 TREE_PUBLIC(decl) = 1;
2901 if (is_declaration)
2902 DECL_EXTERNAL(decl) = 1;
2903 else
2905 tree restype = TREE_TYPE(functype);
2906 tree resdecl =
2907 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
2908 DECL_ARTIFICIAL(resdecl) = 1;
2909 DECL_IGNORED_P(resdecl) = 1;
2910 DECL_CONTEXT(resdecl) = decl;
2911 DECL_RESULT(decl) = resdecl;
2913 if (!is_inlinable)
2914 DECL_UNINLINABLE(decl) = 1;
2915 if (disable_split_stack)
2917 tree attr = get_identifier("__no_split_stack__");
2918 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
2920 if (in_unique_section)
2921 resolve_unique_section(decl, 0, 1);
2923 go_preserve_from_gc(decl);
2924 return new Bfunction(decl);
2927 // Create a statement that runs all deferred calls for FUNCTION. This should
2928 // be a statement that looks like this in C++:
2929 // finish:
2930 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2932 Bstatement*
2933 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
2934 Bexpression* defer, Location location)
2936 tree undefer_tree = undefer->get_tree();
2937 tree defer_tree = defer->get_tree();
2938 tree fntree = function->get_tree();
2940 if (undefer_tree == error_mark_node
2941 || defer_tree == error_mark_node
2942 || fntree == error_mark_node)
2943 return this->error_statement();
2945 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2946 push_struct_function(fntree);
2947 else
2948 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2950 tree stmt_list = NULL;
2951 Blabel* blabel = this->label(function, "", location);
2952 Bstatement* label_def = this->label_definition_statement(blabel);
2953 append_to_statement_list(label_def->get_tree(), &stmt_list);
2955 Bstatement* jump_stmt = this->goto_statement(blabel, location);
2956 tree jump = jump_stmt->get_tree();
2957 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
2958 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
2959 tree try_catch =
2960 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
2961 append_to_statement_list(try_catch, &stmt_list);
2962 pop_cfun();
2964 return this->make_statement(stmt_list);
2967 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2968 // This will only be called for a function definition.
2970 bool
2971 Gcc_backend::function_set_parameters(Bfunction* function,
2972 const std::vector<Bvariable*>& param_vars)
2974 tree func_tree = function->get_tree();
2975 if (func_tree == error_mark_node)
2976 return false;
2978 tree params = NULL_TREE;
2979 tree *pp = &params;
2980 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
2981 pv != param_vars.end();
2982 ++pv)
2984 *pp = (*pv)->get_tree();
2985 gcc_assert(*pp != error_mark_node);
2986 pp = &DECL_CHAIN(*pp);
2988 *pp = NULL_TREE;
2989 DECL_ARGUMENTS(func_tree) = params;
2990 return true;
2993 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2995 bool
2996 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
2998 tree func_tree = function->get_tree();
2999 tree code = code_stmt->get_tree();
3001 if (func_tree == error_mark_node || code == error_mark_node)
3002 return false;
3003 DECL_SAVED_TREE(func_tree) = code;
3004 return true;
3007 // Look up a named built-in function in the current backend implementation.
3008 // Returns NULL if no built-in function by that name exists.
3010 Bfunction*
3011 Gcc_backend::lookup_builtin(const std::string& name)
3013 if (this->builtin_functions_.count(name) != 0)
3014 return this->builtin_functions_[name];
3015 return NULL;
3018 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3019 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3020 // emit early debugging information.
3022 void
3023 Gcc_backend::write_global_definitions(
3024 const std::vector<Btype*>& type_decls,
3025 const std::vector<Bexpression*>& constant_decls,
3026 const std::vector<Bfunction*>& function_decls,
3027 const std::vector<Bvariable*>& variable_decls)
3029 size_t count_definitions = type_decls.size() + constant_decls.size()
3030 + function_decls.size() + variable_decls.size();
3032 tree* defs = new tree[count_definitions];
3034 // Convert all non-erroneous declarations into Gimple form.
3035 size_t i = 0;
3036 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
3037 p != variable_decls.end();
3038 ++p)
3040 if ((*p)->get_tree() != error_mark_node)
3042 defs[i] = (*p)->get_tree();
3043 go_preserve_from_gc(defs[i]);
3044 ++i;
3048 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
3049 p != type_decls.end();
3050 ++p)
3052 tree type_tree = (*p)->get_tree();
3053 if (type_tree != error_mark_node
3054 && IS_TYPE_OR_DECL_P(type_tree))
3056 defs[i] = TYPE_NAME(type_tree);
3057 gcc_assert(defs[i] != NULL);
3058 go_preserve_from_gc(defs[i]);
3059 ++i;
3062 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
3063 p != constant_decls.end();
3064 ++p)
3066 if ((*p)->get_tree() != error_mark_node)
3068 defs[i] = (*p)->get_tree();
3069 go_preserve_from_gc(defs[i]);
3070 ++i;
3073 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
3074 p != function_decls.end();
3075 ++p)
3077 tree decl = (*p)->get_tree();
3078 if (decl != error_mark_node)
3080 go_preserve_from_gc(decl);
3081 gimplify_function_tree(decl);
3082 cgraph_node::finalize_function(decl, true);
3084 defs[i] = decl;
3085 ++i;
3089 // Pass everything back to the middle-end.
3091 wrapup_global_declarations(defs, i);
3093 delete[] defs;
3096 // Define a builtin function. BCODE is the builtin function code
3097 // defined by builtins.def. NAME is the name of the builtin function.
3098 // LIBNAME is the name of the corresponding library function, and is
3099 // NULL if there isn't one. FNTYPE is the type of the function.
3100 // CONST_P is true if the function has the const attribute.
3102 void
3103 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3104 const char* libname, tree fntype, bool const_p)
3106 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3107 libname, NULL_TREE);
3108 if (const_p)
3109 TREE_READONLY(decl) = 1;
3110 set_builtin_decl(bcode, decl, true);
3111 this->builtin_functions_[name] = this->make_function(decl);
3112 if (libname != NULL)
3114 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3115 NULL, NULL_TREE);
3116 if (const_p)
3117 TREE_READONLY(decl) = 1;
3118 this->builtin_functions_[libname] = this->make_function(decl);
3122 // Return the backend generator.
3124 Backend*
3125 go_get_backend()
3127 return new Gcc_backend();