Allow Objective-c++ to recognise lambdas.
[official-gcc.git] / gcc / go / go-gcc.cc
blobc1006303cb6eda479846443336593869a4b7c906
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2014 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
5 // This file is part of GCC.
7 // GCC is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3, or (at your option) any later
10 // version.
12 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 #include "go-system.h"
23 // This has to be included outside of extern "C", so we have to
24 // include it here before tree.h includes it later.
25 #include <gmp.h>
27 #include "tree.h"
28 #include "stringpool.h"
29 #include "stor-layout.h"
30 #include "varasm.h"
31 #include "tree-iterator.h"
32 #include "hash-map.h"
33 #include "is-a.h"
34 #include "plugin-api.h"
35 #include "vec.h"
36 #include "hashtab.h"
37 #include "hash-set.h"
38 #include "machmode.h"
39 #include "tm.h"
40 #include "hard-reg-set.h"
41 #include "input.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 "real.h"
52 #include "realmpfr.h"
53 #include "builtins.h"
55 #include "go-c.h"
57 #include "gogo.h"
58 #include "backend.h"
60 // A class wrapping a tree.
62 class Gcc_tree
64 public:
65 Gcc_tree(tree t)
66 : t_(t)
67 { }
69 tree
70 get_tree() const
71 { return this->t_; }
73 void
74 set_tree(tree t)
75 { this->t_ = t; }
77 private:
78 tree t_;
81 // In gcc, types, expressions, and statements are all trees.
82 class Btype : public Gcc_tree
84 public:
85 Btype(tree t)
86 : Gcc_tree(t)
87 { }
90 class Bexpression : public Gcc_tree
92 public:
93 Bexpression(tree t)
94 : Gcc_tree(t)
95 { }
98 class Bstatement : public Gcc_tree
100 public:
101 Bstatement(tree t)
102 : Gcc_tree(t)
106 class Bfunction : public Gcc_tree
108 public:
109 Bfunction(tree t)
110 : Gcc_tree(t)
114 class Bblock : public Gcc_tree
116 public:
117 Bblock(tree t)
118 : Gcc_tree(t)
122 class Bvariable : public Gcc_tree
124 public:
125 Bvariable(tree t)
126 : Gcc_tree(t)
130 class Blabel : public Gcc_tree
132 public:
133 Blabel(tree t)
134 : Gcc_tree(t)
138 // This file implements the interface between the Go frontend proper
139 // and the gcc IR. This implements specific instantiations of
140 // abstract classes defined by the Go frontend proper. The Go
141 // frontend proper class methods of these classes to generate the
142 // backend representation.
144 class Gcc_backend : public Backend
146 public:
147 Gcc_backend();
149 // Types.
151 Btype*
152 error_type()
153 { return this->make_type(error_mark_node); }
155 Btype*
156 void_type()
157 { return this->make_type(void_type_node); }
159 Btype*
160 bool_type()
161 { return this->make_type(boolean_type_node); }
163 Btype*
164 integer_type(bool, int);
166 Btype*
167 float_type(int);
169 Btype*
170 complex_type(int);
172 Btype*
173 pointer_type(Btype*);
175 Btype*
176 function_type(const Btyped_identifier&,
177 const std::vector<Btyped_identifier>&,
178 const std::vector<Btyped_identifier>&,
179 Btype*,
180 const Location);
182 Btype*
183 struct_type(const std::vector<Btyped_identifier>&);
185 Btype*
186 array_type(Btype*, Bexpression*);
188 Btype*
189 placeholder_pointer_type(const std::string&, Location, bool);
191 bool
192 set_placeholder_pointer_type(Btype*, Btype*);
194 bool
195 set_placeholder_function_type(Btype*, Btype*);
197 Btype*
198 placeholder_struct_type(const std::string&, Location);
200 bool
201 set_placeholder_struct_type(Btype* placeholder,
202 const std::vector<Btyped_identifier>&);
204 Btype*
205 placeholder_array_type(const std::string&, Location);
207 bool
208 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
210 Btype*
211 named_type(const std::string&, Btype*, Location);
213 Btype*
214 circular_pointer_type(Btype*, bool);
216 bool
217 is_circular_pointer_type(Btype*);
219 size_t
220 type_size(Btype*);
222 size_t
223 type_alignment(Btype*);
225 size_t
226 type_field_alignment(Btype*);
228 size_t
229 type_field_offset(Btype*, size_t index);
231 // Expressions.
233 Bexpression*
234 zero_expression(Btype*);
236 Bexpression*
237 error_expression()
238 { return this->make_expression(error_mark_node); }
240 Bexpression*
241 nil_pointer_expression()
242 { return this->make_expression(null_pointer_node); }
244 Bexpression*
245 var_expression(Bvariable* var, Location);
247 Bexpression*
248 indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location);
250 Bexpression*
251 named_constant_expression(Btype* btype, const std::string& name,
252 Bexpression* val, Location);
254 Bexpression*
255 integer_constant_expression(Btype* btype, mpz_t val);
257 Bexpression*
258 float_constant_expression(Btype* btype, mpfr_t val);
260 Bexpression*
261 complex_constant_expression(Btype* btype, mpc_t val);
263 Bexpression*
264 string_constant_expression(const std::string& val);
266 Bexpression*
267 boolean_constant_expression(bool val);
269 Bexpression*
270 real_part_expression(Bexpression* bcomplex, Location);
272 Bexpression*
273 imag_part_expression(Bexpression* bcomplex, Location);
275 Bexpression*
276 complex_expression(Bexpression* breal, Bexpression* bimag, Location);
278 Bexpression*
279 convert_expression(Btype* type, Bexpression* expr, Location);
281 Bexpression*
282 function_code_expression(Bfunction*, Location);
284 Bexpression*
285 address_expression(Bexpression*, Location);
287 Bexpression*
288 struct_field_expression(Bexpression*, size_t, Location);
290 Bexpression*
291 compound_expression(Bstatement*, Bexpression*, Location);
293 Bexpression*
294 conditional_expression(Btype*, Bexpression*, Bexpression*, Bexpression*,
295 Location);
297 Bexpression*
298 unary_expression(Operator, Bexpression*, Location);
300 Bexpression*
301 binary_expression(Operator, Bexpression*, Bexpression*, Location);
303 Bexpression*
304 constructor_expression(Btype*, const std::vector<Bexpression*>&, Location);
306 Bexpression*
307 array_constructor_expression(Btype*, const std::vector<unsigned long>&,
308 const std::vector<Bexpression*>&, Location);
310 Bexpression*
311 pointer_offset_expression(Bexpression* base, Bexpression* offset, Location);
313 Bexpression*
314 array_index_expression(Bexpression* array, Bexpression* index, Location);
316 Bexpression*
317 call_expression(Bexpression* fn, const std::vector<Bexpression*>& args,
318 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 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
400 Location, Bstatement**);
402 Bvariable*
403 implicit_variable(const std::string&, Btype*, bool, bool, bool,
404 size_t);
406 void
407 implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
408 bool, bool, bool, Bexpression*);
410 Bvariable*
411 implicit_variable_reference(const std::string&, Btype*);
413 Bvariable*
414 immutable_struct(const std::string&, bool, bool, Btype*, Location);
416 void
417 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
418 Location, Bexpression*);
420 Bvariable*
421 immutable_struct_reference(const std::string&, Btype*, Location);
423 // Labels.
425 Blabel*
426 label(Bfunction*, const std::string& name, Location);
428 Bstatement*
429 label_definition_statement(Blabel*);
431 Bstatement*
432 goto_statement(Blabel*, Location);
434 Bexpression*
435 label_address(Blabel*, Location);
437 // Functions.
439 Bfunction*
440 error_function()
441 { return this->make_function(error_mark_node); }
443 Bfunction*
444 function(Btype* fntype, const std::string& name, const std::string& asm_name,
445 bool is_visible, bool is_declaration, bool is_inlinable,
446 bool disable_split_stack, bool in_unique_section, Location);
448 Bstatement*
449 function_defer_statement(Bfunction* function, Bexpression* undefer,
450 Bexpression* defer, Location);
452 bool
453 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
455 bool
456 function_set_body(Bfunction* function, Bstatement* code_stmt);
458 Bfunction*
459 lookup_builtin(const std::string&);
461 void
462 write_global_definitions(const std::vector<Btype*>&,
463 const std::vector<Bexpression*>&,
464 const std::vector<Bfunction*>&,
465 const std::vector<Bvariable*>&);
467 private:
468 // Make a Bexpression from a tree.
469 Bexpression*
470 make_expression(tree t)
471 { return new Bexpression(t); }
473 // Make a Bstatement from a tree.
474 Bstatement*
475 make_statement(tree t)
476 { return new Bstatement(t); }
478 // Make a Btype from a tree.
479 Btype*
480 make_type(tree t)
481 { return new Btype(t); }
483 Bfunction*
484 make_function(tree t)
485 { return new Bfunction(t); }
487 Btype*
488 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
490 Btype*
491 fill_in_array(Btype*, Btype*, Bexpression*);
493 tree
494 non_zero_size_type(tree);
496 private:
497 void
498 define_builtin(built_in_function bcode, const char* name, const char* libname,
499 tree fntype, bool const_p);
501 // A mapping of the GCC built-ins exposed to GCCGo.
502 std::map<std::string, Bfunction*> builtin_functions_;
505 // A helper function.
507 static inline tree
508 get_identifier_from_string(const std::string& str)
510 return get_identifier_with_length(str.data(), str.length());
513 // Define the built-in functions that are exposed to GCCGo.
515 Gcc_backend::Gcc_backend()
517 /* We need to define the fetch_and_add functions, since we use them
518 for ++ and --. */
519 tree t = this->integer_type(BITS_PER_UNIT, 1)->get_tree();
520 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
521 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
522 NULL, build_function_type_list(t, p, t, NULL_TREE),
523 false);
525 t = this->integer_type(BITS_PER_UNIT * 2, 1)->get_tree();
526 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
527 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
528 NULL, build_function_type_list(t, p, t, NULL_TREE),
529 false);
531 t = this->integer_type(BITS_PER_UNIT * 4, 1)->get_tree();
532 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
533 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
534 NULL, build_function_type_list(t, p, t, NULL_TREE),
535 false);
537 t = this->integer_type(BITS_PER_UNIT * 8, 1)->get_tree();
538 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
539 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
540 NULL, build_function_type_list(t, p, t, NULL_TREE),
541 false);
543 // We use __builtin_expect for magic import functions.
544 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
545 build_function_type_list(long_integer_type_node,
546 long_integer_type_node,
547 long_integer_type_node,
548 NULL_TREE),
549 true);
551 // We use __builtin_memcmp for struct comparisons.
552 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
553 build_function_type_list(integer_type_node,
554 const_ptr_type_node,
555 const_ptr_type_node,
556 size_type_node,
557 NULL_TREE),
558 false);
560 // We provide some functions for the math library.
561 tree math_function_type = build_function_type_list(double_type_node,
562 double_type_node,
563 NULL_TREE);
564 tree math_function_type_long =
565 build_function_type_list(long_double_type_node, long_double_type_node,
566 long_double_type_node, NULL_TREE);
567 tree math_function_type_two = build_function_type_list(double_type_node,
568 double_type_node,
569 double_type_node,
570 NULL_TREE);
571 tree math_function_type_long_two =
572 build_function_type_list(long_double_type_node, long_double_type_node,
573 long_double_type_node, NULL_TREE);
574 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
575 math_function_type, true);
576 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
577 math_function_type_long, true);
578 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
579 math_function_type, true);
580 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
581 math_function_type_long, true);
582 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
583 math_function_type, true);
584 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
585 math_function_type_long, true);
586 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
587 math_function_type_two, true);
588 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
589 math_function_type_long_two, true);
590 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
591 math_function_type, true);
592 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
593 math_function_type_long, true);
594 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
595 math_function_type, true);
596 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
597 math_function_type_long, true);
598 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
599 math_function_type, true);
600 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
601 math_function_type_long, true);
602 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
603 math_function_type, true);
604 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
605 math_function_type_long, true);
606 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
607 math_function_type, true);
608 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
609 math_function_type_long, true);
610 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
611 math_function_type, true);
612 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
613 math_function_type_long, true);
614 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
615 math_function_type_two, true);
616 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
617 math_function_type_long_two, true);
618 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
619 build_function_type_list(double_type_node,
620 double_type_node,
621 integer_type_node,
622 NULL_TREE),
623 true);
624 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
625 build_function_type_list(long_double_type_node,
626 long_double_type_node,
627 integer_type_node,
628 NULL_TREE),
629 true);
630 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
631 math_function_type, true);
632 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
633 math_function_type_long, true);
634 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
635 math_function_type, true);
636 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
637 math_function_type_long, true);
638 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
639 math_function_type, true);
640 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
641 math_function_type_long, true);
642 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
643 math_function_type, true);
644 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
645 math_function_type_long, true);
646 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
647 math_function_type, true);
648 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
649 math_function_type_long, true);
650 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
651 math_function_type, true);
652 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
653 math_function_type_long, true);
654 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
655 math_function_type, true);
656 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
657 math_function_type_long, true);
658 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
659 math_function_type, true);
660 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
661 math_function_type_long, true);
663 // We use __builtin_return_address in the thunk we build for
664 // functions which call recover.
665 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
666 NULL,
667 build_function_type_list(ptr_type_node,
668 unsigned_type_node,
669 NULL_TREE),
670 false);
672 // The compiler uses __builtin_trap for some exception handling
673 // cases.
674 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
675 build_function_type(void_type_node, void_list_node),
676 false);
679 // Get an unnamed integer type.
681 Btype*
682 Gcc_backend::integer_type(bool is_unsigned, int bits)
684 tree type;
685 if (is_unsigned)
687 if (bits == INT_TYPE_SIZE)
688 type = unsigned_type_node;
689 else if (bits == CHAR_TYPE_SIZE)
690 type = unsigned_char_type_node;
691 else if (bits == SHORT_TYPE_SIZE)
692 type = short_unsigned_type_node;
693 else if (bits == LONG_TYPE_SIZE)
694 type = long_unsigned_type_node;
695 else if (bits == LONG_LONG_TYPE_SIZE)
696 type = long_long_unsigned_type_node;
697 else
698 type = make_unsigned_type(bits);
700 else
702 if (bits == INT_TYPE_SIZE)
703 type = integer_type_node;
704 else if (bits == CHAR_TYPE_SIZE)
705 type = signed_char_type_node;
706 else if (bits == SHORT_TYPE_SIZE)
707 type = short_integer_type_node;
708 else if (bits == LONG_TYPE_SIZE)
709 type = long_integer_type_node;
710 else if (bits == LONG_LONG_TYPE_SIZE)
711 type = long_long_integer_type_node;
712 else
713 type = make_signed_type(bits);
715 return this->make_type(type);
718 // Get an unnamed float type.
720 Btype*
721 Gcc_backend::float_type(int bits)
723 tree type;
724 if (bits == FLOAT_TYPE_SIZE)
725 type = float_type_node;
726 else if (bits == DOUBLE_TYPE_SIZE)
727 type = double_type_node;
728 else if (bits == LONG_DOUBLE_TYPE_SIZE)
729 type = long_double_type_node;
730 else
732 type = make_node(REAL_TYPE);
733 TYPE_PRECISION(type) = bits;
734 layout_type(type);
736 return this->make_type(type);
739 // Get an unnamed complex type.
741 Btype*
742 Gcc_backend::complex_type(int bits)
744 tree type;
745 if (bits == FLOAT_TYPE_SIZE * 2)
746 type = complex_float_type_node;
747 else if (bits == DOUBLE_TYPE_SIZE * 2)
748 type = complex_double_type_node;
749 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
750 type = complex_long_double_type_node;
751 else
753 type = make_node(REAL_TYPE);
754 TYPE_PRECISION(type) = bits / 2;
755 layout_type(type);
756 type = build_complex_type(type);
758 return this->make_type(type);
761 // Get a pointer type.
763 Btype*
764 Gcc_backend::pointer_type(Btype* to_type)
766 tree to_type_tree = to_type->get_tree();
767 if (to_type_tree == error_mark_node)
768 return this->error_type();
769 tree type = build_pointer_type(to_type_tree);
770 return this->make_type(type);
773 // Make a function type.
775 Btype*
776 Gcc_backend::function_type(const Btyped_identifier& receiver,
777 const std::vector<Btyped_identifier>& parameters,
778 const std::vector<Btyped_identifier>& results,
779 Btype* result_struct,
780 Location)
782 tree args = NULL_TREE;
783 tree* pp = &args;
784 if (receiver.btype != NULL)
786 tree t = receiver.btype->get_tree();
787 if (t == error_mark_node)
788 return this->error_type();
789 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
790 pp = &TREE_CHAIN(*pp);
793 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
794 p != parameters.end();
795 ++p)
797 tree t = p->btype->get_tree();
798 if (t == error_mark_node)
799 return this->error_type();
800 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
801 pp = &TREE_CHAIN(*pp);
804 // Varargs is handled entirely at the Go level. When converted to
805 // GENERIC functions are not varargs.
806 *pp = void_list_node;
808 tree result;
809 if (results.empty())
810 result = void_type_node;
811 else if (results.size() == 1)
812 result = results.front().btype->get_tree();
813 else
815 gcc_assert(result_struct != NULL);
816 result = result_struct->get_tree();
818 if (result == error_mark_node)
819 return this->error_type();
821 tree fntype = build_function_type(result, args);
822 if (fntype == error_mark_node)
823 return this->error_type();
825 return this->make_type(build_pointer_type(fntype));
828 // Make a struct type.
830 Btype*
831 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
833 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
836 // Fill in the fields of a struct type.
838 Btype*
839 Gcc_backend::fill_in_struct(Btype* fill,
840 const std::vector<Btyped_identifier>& fields)
842 tree fill_tree = fill->get_tree();
843 tree field_trees = NULL_TREE;
844 tree* pp = &field_trees;
845 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
846 p != fields.end();
847 ++p)
849 tree name_tree = get_identifier_from_string(p->name);
850 tree type_tree = p->btype->get_tree();
851 if (type_tree == error_mark_node)
852 return this->error_type();
853 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
854 type_tree);
855 DECL_CONTEXT(field) = fill_tree;
856 *pp = field;
857 pp = &DECL_CHAIN(field);
859 TYPE_FIELDS(fill_tree) = field_trees;
860 layout_type(fill_tree);
861 return fill;
864 // Make an array type.
866 Btype*
867 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
869 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
870 element_btype, length);
873 // Fill in an array type.
875 Btype*
876 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
877 Bexpression* length)
879 tree element_type_tree = element_type->get_tree();
880 tree length_tree = length->get_tree();
881 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
882 return this->error_type();
884 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
886 length_tree = fold_convert(sizetype, length_tree);
888 // build_index_type takes the maximum index, which is one less than
889 // the length.
890 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
891 length_tree,
892 size_one_node));
894 tree fill_tree = fill->get_tree();
895 TREE_TYPE(fill_tree) = element_type_tree;
896 TYPE_DOMAIN(fill_tree) = index_type_tree;
897 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
898 layout_type(fill_tree);
900 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
901 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
902 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
903 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
904 TYPE_CANONICAL(fill_tree) =
905 build_array_type(TYPE_CANONICAL(element_type_tree),
906 TYPE_CANONICAL(index_type_tree));
908 return fill;
911 // Create a placeholder for a pointer type.
913 Btype*
914 Gcc_backend::placeholder_pointer_type(const std::string& name,
915 Location location, bool)
917 tree ret = build_distinct_type_copy(ptr_type_node);
918 if (!name.empty())
920 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
921 get_identifier_from_string(name),
922 ret);
923 TYPE_NAME(ret) = decl;
925 return this->make_type(ret);
928 // Set the real target type for a placeholder pointer type.
930 bool
931 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
932 Btype* to_type)
934 tree pt = placeholder->get_tree();
935 if (pt == error_mark_node)
936 return false;
937 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
938 tree tt = to_type->get_tree();
939 if (tt == error_mark_node)
941 placeholder->set_tree(error_mark_node);
942 return false;
944 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
945 TREE_TYPE(pt) = TREE_TYPE(tt);
946 if (TYPE_NAME(pt) != NULL_TREE)
948 // Build the data structure gcc wants to see for a typedef.
949 tree copy = build_variant_type_copy(pt);
950 TYPE_NAME(copy) = NULL_TREE;
951 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
953 return true;
956 // Set the real values for a placeholder function type.
958 bool
959 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
961 return this->set_placeholder_pointer_type(placeholder, ft);
964 // Create a placeholder for a struct type.
966 Btype*
967 Gcc_backend::placeholder_struct_type(const std::string& name,
968 Location location)
970 tree ret = make_node(RECORD_TYPE);
971 if (!name.empty())
973 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
974 get_identifier_from_string(name),
975 ret);
976 TYPE_NAME(ret) = decl;
978 return this->make_type(ret);
981 // Fill in the fields of a placeholder struct type.
983 bool
984 Gcc_backend::set_placeholder_struct_type(
985 Btype* placeholder,
986 const std::vector<Btyped_identifier>& fields)
988 tree t = placeholder->get_tree();
989 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
990 Btype* r = this->fill_in_struct(placeholder, fields);
992 if (TYPE_NAME(t) != NULL_TREE)
994 // Build the data structure gcc wants to see for a typedef.
995 tree copy = build_distinct_type_copy(t);
996 TYPE_NAME(copy) = NULL_TREE;
997 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1000 return r->get_tree() != error_mark_node;
1003 // Create a placeholder for an array type.
1005 Btype*
1006 Gcc_backend::placeholder_array_type(const std::string& name,
1007 Location location)
1009 tree ret = make_node(ARRAY_TYPE);
1010 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1011 get_identifier_from_string(name),
1012 ret);
1013 TYPE_NAME(ret) = decl;
1014 return this->make_type(ret);
1017 // Fill in the fields of a placeholder array type.
1019 bool
1020 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1021 Btype* element_btype,
1022 Bexpression* length)
1024 tree t = placeholder->get_tree();
1025 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1026 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1028 // Build the data structure gcc wants to see for a typedef.
1029 tree copy = build_distinct_type_copy(t);
1030 TYPE_NAME(copy) = NULL_TREE;
1031 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1033 return r->get_tree() != error_mark_node;
1036 // Return a named version of a type.
1038 Btype*
1039 Gcc_backend::named_type(const std::string& name, Btype* btype,
1040 Location location)
1042 tree type = btype->get_tree();
1043 if (type == error_mark_node)
1044 return this->error_type();
1046 // The middle-end expects a basic type to have a name. In Go every
1047 // basic type will have a name. The first time we see a basic type,
1048 // give it whatever Go name we have at this point.
1049 if (TYPE_NAME(type) == NULL_TREE
1050 && location.gcc_location() == BUILTINS_LOCATION
1051 && (TREE_CODE(type) == INTEGER_TYPE
1052 || TREE_CODE(type) == REAL_TYPE
1053 || TREE_CODE(type) == COMPLEX_TYPE
1054 || TREE_CODE(type) == BOOLEAN_TYPE))
1056 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1057 get_identifier_from_string(name),
1058 type);
1059 TYPE_NAME(type) = decl;
1060 return this->make_type(type);
1063 tree copy = build_variant_type_copy(type);
1064 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1065 get_identifier_from_string(name),
1066 copy);
1067 DECL_ORIGINAL_TYPE(decl) = type;
1068 TYPE_NAME(copy) = decl;
1069 return this->make_type(copy);
1072 // Return a pointer type used as a marker for a circular type.
1074 Btype*
1075 Gcc_backend::circular_pointer_type(Btype*, bool)
1077 return this->make_type(ptr_type_node);
1080 // Return whether we might be looking at a circular type.
1082 bool
1083 Gcc_backend::is_circular_pointer_type(Btype* btype)
1085 return btype->get_tree() == ptr_type_node;
1088 // Return the size of a type.
1090 size_t
1091 Gcc_backend::type_size(Btype* btype)
1093 tree t = btype->get_tree();
1094 if (t == error_mark_node)
1095 return 1;
1096 t = TYPE_SIZE_UNIT(t);
1097 gcc_assert(tree_fits_uhwi_p (t));
1098 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1099 size_t ret = static_cast<size_t>(val_wide);
1100 gcc_assert(ret == val_wide);
1101 return ret;
1104 // Return the alignment of a type.
1106 size_t
1107 Gcc_backend::type_alignment(Btype* btype)
1109 tree t = btype->get_tree();
1110 if (t == error_mark_node)
1111 return 1;
1112 return TYPE_ALIGN_UNIT(t);
1115 // Return the alignment of a struct field of type BTYPE.
1117 size_t
1118 Gcc_backend::type_field_alignment(Btype* btype)
1120 tree t = btype->get_tree();
1121 if (t == error_mark_node)
1122 return 1;
1123 return go_field_alignment(t);
1126 // Return the offset of a field in a struct.
1128 size_t
1129 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1131 tree struct_tree = btype->get_tree();
1132 if (struct_tree == error_mark_node)
1133 return 0;
1134 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1135 tree field = TYPE_FIELDS(struct_tree);
1136 for (; index > 0; --index)
1138 field = DECL_CHAIN(field);
1139 gcc_assert(field != NULL_TREE);
1141 HOST_WIDE_INT offset_wide = int_byte_position(field);
1142 gcc_assert(offset_wide >= 0);
1143 size_t ret = static_cast<size_t>(offset_wide);
1144 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
1145 return ret;
1148 // Return the zero value for a type.
1150 Bexpression*
1151 Gcc_backend::zero_expression(Btype* btype)
1153 tree t = btype->get_tree();
1154 tree ret;
1155 if (t == error_mark_node)
1156 ret = error_mark_node;
1157 else
1158 ret = build_zero_cst(t);
1159 return this->make_expression(ret);
1162 // An expression that references a variable.
1164 Bexpression*
1165 Gcc_backend::var_expression(Bvariable* var, Location)
1167 tree ret = var->get_tree();
1168 if (ret == error_mark_node)
1169 return this->error_expression();
1170 return this->make_expression(ret);
1173 // An expression that indirectly references an expression.
1175 Bexpression*
1176 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1177 bool known_valid, Location location)
1179 tree expr_tree = expr->get_tree();
1180 tree type_tree = btype->get_tree();
1181 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1182 return this->error_expression();
1184 // If the type of EXPR is a recursive pointer type, then we
1185 // need to insert a cast before indirecting.
1186 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1187 if (VOID_TYPE_P(target_type_tree))
1188 expr_tree = fold_convert_loc(location.gcc_location(),
1189 build_pointer_type(type_tree), expr_tree);
1191 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1192 expr_tree);
1193 if (known_valid)
1194 TREE_THIS_NOTRAP(ret) = 1;
1195 return this->make_expression(ret);
1198 // Return an expression that declares a constant named NAME with the
1199 // constant value VAL in BTYPE.
1201 Bexpression*
1202 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1203 Bexpression* val, Location location)
1205 tree type_tree = btype->get_tree();
1206 tree const_val = val->get_tree();
1207 if (type_tree == error_mark_node || const_val == error_mark_node)
1208 return this->error_expression();
1210 tree name_tree = get_identifier_from_string(name);
1211 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1212 type_tree);
1213 DECL_INITIAL(decl) = const_val;
1214 TREE_CONSTANT(decl) = 1;
1215 TREE_READONLY(decl) = 1;
1217 go_preserve_from_gc(decl);
1218 return this->make_expression(decl);
1221 // Return a typed value as a constant integer.
1223 Bexpression*
1224 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1226 tree t = btype->get_tree();
1227 if (t == error_mark_node)
1228 return this->error_expression();
1230 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1231 return this->make_expression(ret);
1234 // Return a typed value as a constant floating-point number.
1236 Bexpression*
1237 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1239 tree t = btype->get_tree();
1240 tree ret;
1241 if (t == error_mark_node)
1242 return this->error_expression();
1244 REAL_VALUE_TYPE r1;
1245 real_from_mpfr(&r1, val, t, GMP_RNDN);
1246 REAL_VALUE_TYPE r2;
1247 real_convert(&r2, TYPE_MODE(t), &r1);
1248 ret = build_real(t, r2);
1249 return this->make_expression(ret);
1252 // Return a typed real and imaginary value as a constant complex number.
1254 Bexpression*
1255 Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
1257 tree t = btype->get_tree();
1258 tree ret;
1259 if (t == error_mark_node)
1260 return this->error_expression();
1262 REAL_VALUE_TYPE r1;
1263 real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
1264 REAL_VALUE_TYPE r2;
1265 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1267 REAL_VALUE_TYPE r3;
1268 real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
1269 REAL_VALUE_TYPE r4;
1270 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1272 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1273 build_real(TREE_TYPE(t), r4));
1274 return this->make_expression(ret);
1277 // Make a constant string expression.
1279 Bexpression*
1280 Gcc_backend::string_constant_expression(const std::string& val)
1282 tree index_type = build_index_type(size_int(val.length()));
1283 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1284 TYPE_QUAL_CONST);
1285 tree string_type = build_array_type(const_char_type, index_type);
1286 string_type = build_variant_type_copy(string_type);
1287 TYPE_STRING_FLAG(string_type) = 1;
1288 tree string_val = build_string(val.length(), val.data());
1289 TREE_TYPE(string_val) = string_type;
1291 return this->make_expression(string_val);
1294 // Make a constant boolean expression.
1296 Bexpression*
1297 Gcc_backend::boolean_constant_expression(bool val)
1299 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1300 return this->make_expression(bool_cst);
1303 // Return the real part of a complex expression.
1305 Bexpression*
1306 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1308 tree complex_tree = bcomplex->get_tree();
1309 if (complex_tree == error_mark_node)
1310 return this->error_expression();
1311 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1312 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1313 TREE_TYPE(TREE_TYPE(complex_tree)),
1314 complex_tree);
1315 return this->make_expression(ret);
1318 // Return the imaginary part of a complex expression.
1320 Bexpression*
1321 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1323 tree complex_tree = bcomplex->get_tree();
1324 if (complex_tree == error_mark_node)
1325 return this->error_expression();
1326 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1327 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1328 TREE_TYPE(TREE_TYPE(complex_tree)),
1329 complex_tree);
1330 return this->make_expression(ret);
1333 // Make a complex expression given its real and imaginary parts.
1335 Bexpression*
1336 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1337 Location location)
1339 tree real_tree = breal->get_tree();
1340 tree imag_tree = bimag->get_tree();
1341 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1342 return this->error_expression();
1343 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1344 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1345 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1346 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1347 build_complex_type(TREE_TYPE(real_tree)),
1348 real_tree, imag_tree);
1349 return this->make_expression(ret);
1352 // An expression that converts an expression to a different type.
1354 Bexpression*
1355 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1356 Location location)
1358 tree type_tree = type->get_tree();
1359 tree expr_tree = expr->get_tree();
1360 if (type_tree == error_mark_node
1361 || expr_tree == error_mark_node
1362 || TREE_TYPE(expr_tree) == error_mark_node)
1363 return this->error_expression();
1365 tree ret;
1366 if (this->type_size(type) == 0)
1368 // Do not convert zero-sized types.
1369 ret = expr_tree;
1371 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1372 ret = fold(convert_to_integer(type_tree, expr_tree));
1373 else if (TREE_CODE(type_tree) == REAL_TYPE)
1374 ret = fold(convert_to_real(type_tree, expr_tree));
1375 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1376 ret = fold(convert_to_complex(type_tree, expr_tree));
1377 else if (TREE_CODE(type_tree) == POINTER_TYPE
1378 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1379 ret = fold(convert_to_pointer(type_tree, expr_tree));
1380 else if (TREE_CODE(type_tree) == RECORD_TYPE
1381 || TREE_CODE(type_tree) == ARRAY_TYPE)
1382 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1383 type_tree, expr_tree);
1384 else
1385 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1387 return this->make_expression(ret);
1390 // Get the address of a function.
1392 Bexpression*
1393 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1395 tree func = bfunc->get_tree();
1396 if (func == error_mark_node)
1397 return this->error_expression();
1399 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1400 return this->make_expression(ret);
1403 // Get the address of an expression.
1405 Bexpression*
1406 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1408 tree expr = bexpr->get_tree();
1409 if (expr == error_mark_node)
1410 return this->error_expression();
1412 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1413 return this->make_expression(ret);
1416 // Return an expression for the field at INDEX in BSTRUCT.
1418 Bexpression*
1419 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1420 Location location)
1422 tree struct_tree = bstruct->get_tree();
1423 if (struct_tree == error_mark_node
1424 || TREE_TYPE(struct_tree) == error_mark_node)
1425 return this->error_expression();
1426 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1427 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1428 if (field == NULL_TREE)
1430 // This can happen for a type which refers to itself indirectly
1431 // and then turns out to be erroneous.
1432 return this->error_expression();
1434 for (unsigned int i = index; i > 0; --i)
1436 field = DECL_CHAIN(field);
1437 gcc_assert(field != NULL_TREE);
1439 if (TREE_TYPE(field) == error_mark_node)
1440 return this->error_expression();
1441 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1442 TREE_TYPE(field), struct_tree, field,
1443 NULL_TREE);
1444 if (TREE_CONSTANT(struct_tree))
1445 TREE_CONSTANT(ret) = 1;
1446 return this->make_expression(ret);
1449 // Return an expression that executes BSTAT before BEXPR.
1451 Bexpression*
1452 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1453 Location location)
1455 tree stat = bstat->get_tree();
1456 tree expr = bexpr->get_tree();
1457 if (stat == error_mark_node || expr == error_mark_node)
1458 return this->error_expression();
1459 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1460 TREE_TYPE(expr), stat, expr);
1461 return this->make_expression(ret);
1464 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1465 // ELSE_EXPR otherwise.
1467 Bexpression*
1468 Gcc_backend::conditional_expression(Btype* btype, Bexpression* condition,
1469 Bexpression* then_expr,
1470 Bexpression* else_expr, Location location)
1472 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1473 tree cond_tree = condition->get_tree();
1474 tree then_tree = then_expr->get_tree();
1475 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1476 if (type_tree == error_mark_node
1477 || cond_tree == error_mark_node
1478 || then_tree == error_mark_node
1479 || else_tree == error_mark_node)
1480 return this->error_expression();
1481 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1482 cond_tree, then_tree, else_tree);
1483 return this->make_expression(ret);
1486 // Return an expression for the unary operation OP EXPR.
1488 Bexpression*
1489 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1491 tree expr_tree = expr->get_tree();
1492 if (expr_tree == error_mark_node
1493 || TREE_TYPE(expr_tree) == error_mark_node)
1494 return this->error_expression();
1496 tree type_tree = TREE_TYPE(expr_tree);
1497 enum tree_code code;
1498 switch (op)
1500 case OPERATOR_MINUS:
1502 tree computed_type = excess_precision_type(type_tree);
1503 if (computed_type != NULL_TREE)
1505 expr_tree = convert(computed_type, expr_tree);
1506 type_tree = computed_type;
1508 code = NEGATE_EXPR;
1509 break;
1511 case OPERATOR_NOT:
1512 code = TRUTH_NOT_EXPR;
1513 break;
1514 case OPERATOR_XOR:
1515 code = BIT_NOT_EXPR;
1516 break;
1517 default:
1518 gcc_unreachable();
1519 break;
1522 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1523 expr_tree);
1524 return this->make_expression(ret);
1527 // Convert a gofrontend operator to an equivalent tree_code.
1529 static enum tree_code
1530 operator_to_tree_code(Operator op, tree type)
1532 enum tree_code code;
1533 switch (op)
1535 case OPERATOR_EQEQ:
1536 code = EQ_EXPR;
1537 break;
1538 case OPERATOR_NOTEQ:
1539 code = NE_EXPR;
1540 break;
1541 case OPERATOR_LT:
1542 code = LT_EXPR;
1543 break;
1544 case OPERATOR_LE:
1545 code = LE_EXPR;
1546 break;
1547 case OPERATOR_GT:
1548 code = GT_EXPR;
1549 break;
1550 case OPERATOR_GE:
1551 code = GE_EXPR;
1552 break;
1553 case OPERATOR_OROR:
1554 code = TRUTH_ORIF_EXPR;
1555 break;
1556 case OPERATOR_ANDAND:
1557 code = TRUTH_ANDIF_EXPR;
1558 break;
1559 case OPERATOR_PLUS:
1560 code = PLUS_EXPR;
1561 break;
1562 case OPERATOR_MINUS:
1563 code = MINUS_EXPR;
1564 break;
1565 case OPERATOR_OR:
1566 code = BIT_IOR_EXPR;
1567 break;
1568 case OPERATOR_XOR:
1569 code = BIT_XOR_EXPR;
1570 break;
1571 case OPERATOR_MULT:
1572 code = MULT_EXPR;
1573 break;
1574 case OPERATOR_DIV:
1575 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1576 code = RDIV_EXPR;
1577 else
1578 code = TRUNC_DIV_EXPR;
1579 break;
1580 case OPERATOR_MOD:
1581 code = TRUNC_MOD_EXPR;
1582 break;
1583 case OPERATOR_LSHIFT:
1584 code = LSHIFT_EXPR;
1585 break;
1586 case OPERATOR_RSHIFT:
1587 code = RSHIFT_EXPR;
1588 break;
1589 case OPERATOR_AND:
1590 code = BIT_AND_EXPR;
1591 break;
1592 case OPERATOR_BITCLEAR:
1593 code = BIT_AND_EXPR;
1594 break;
1595 default:
1596 gcc_unreachable();
1599 return code;
1602 // Return an expression for the binary operation LEFT OP RIGHT.
1604 Bexpression*
1605 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1606 Bexpression* right, Location location)
1608 tree left_tree = left->get_tree();
1609 tree right_tree = right->get_tree();
1610 if (left_tree == error_mark_node
1611 || right_tree == error_mark_node)
1612 return this->error_expression();
1613 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1615 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1616 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1617 tree computed_type = excess_precision_type(type_tree);
1618 if (computed_type != NULL_TREE)
1620 left_tree = convert(computed_type, left_tree);
1621 right_tree = convert(computed_type, right_tree);
1622 type_tree = computed_type;
1625 // For comparison operators, the resulting type should be boolean.
1626 switch (op)
1628 case OPERATOR_EQEQ:
1629 case OPERATOR_NOTEQ:
1630 case OPERATOR_LT:
1631 case OPERATOR_LE:
1632 case OPERATOR_GT:
1633 case OPERATOR_GE:
1634 type_tree = boolean_type_node;
1635 break;
1636 default:
1637 break;
1640 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1641 left_tree, right_tree);
1642 return this->make_expression(ret);
1645 // Return an expression that constructs BTYPE with VALS.
1647 Bexpression*
1648 Gcc_backend::constructor_expression(Btype* btype,
1649 const std::vector<Bexpression*>& vals,
1650 Location location)
1652 tree type_tree = btype->get_tree();
1653 if (type_tree == error_mark_node)
1654 return this->error_expression();
1656 vec<constructor_elt, va_gc> *init;
1657 vec_alloc(init, vals.size());
1659 bool is_constant = true;
1660 tree field = TYPE_FIELDS(type_tree);
1661 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1662 p != vals.end();
1663 ++p, field = DECL_CHAIN(field))
1665 gcc_assert(field != NULL_TREE);
1666 tree val = (*p)->get_tree();
1667 if (TREE_TYPE(field) == error_mark_node
1668 || val == error_mark_node
1669 || TREE_TYPE(val) == error_mark_node)
1670 return this->error_expression();
1672 constructor_elt empty = {NULL, NULL};
1673 constructor_elt* elt = init->quick_push(empty);
1674 elt->index = field;
1675 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
1676 val);
1677 if (!TREE_CONSTANT(elt->value))
1678 is_constant = false;
1680 gcc_assert(field == NULL_TREE);
1681 tree ret = build_constructor(type_tree, init);
1682 if (is_constant)
1683 TREE_CONSTANT(ret) = 1;
1685 return this->make_expression(ret);
1688 Bexpression*
1689 Gcc_backend::array_constructor_expression(
1690 Btype* array_btype, const std::vector<unsigned long>& indexes,
1691 const std::vector<Bexpression*>& vals, Location location)
1693 tree type_tree = array_btype->get_tree();
1694 if (type_tree == error_mark_node)
1695 return this->error_expression();
1697 gcc_assert(indexes.size() == vals.size());
1699 tree element_type = TREE_TYPE(type_tree);
1700 HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
1701 vec<constructor_elt, va_gc> *init;
1702 vec_alloc(init, element_size == 0 ? 0 : vals.size());
1704 tree sink = NULL_TREE;
1705 bool is_constant = true;
1706 for (size_t i = 0; i < vals.size(); ++i)
1708 tree index = size_int(indexes[i]);
1709 tree val = (vals[i])->get_tree();
1711 if (index == error_mark_node
1712 || val == error_mark_node)
1713 return this->error_expression();
1715 if (element_size == 0)
1717 // GIMPLE cannot represent arrays of zero-sized types so trying
1718 // to construct an array of zero-sized values might lead to errors.
1719 // Instead, we evaluate each expression that would have been added as
1720 // an array value for its side-effects and construct an empty array.
1721 append_to_statement_list(val, &sink);
1722 continue;
1725 if (!TREE_CONSTANT(val))
1726 is_constant = false;
1728 constructor_elt empty = {NULL, NULL};
1729 constructor_elt* elt = init->quick_push(empty);
1730 elt->index = index;
1731 elt->value = val;
1734 tree ret = build_constructor(type_tree, init);
1735 if (is_constant)
1736 TREE_CONSTANT(ret) = 1;
1737 if (sink != NULL_TREE)
1738 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1739 type_tree, sink, ret);
1740 return this->make_expression(ret);
1743 // Return an expression for the address of BASE[INDEX].
1745 Bexpression*
1746 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1747 Location location)
1749 tree base_tree = base->get_tree();
1750 tree index_tree = index->get_tree();
1751 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1752 if (base_tree == error_mark_node
1753 || TREE_TYPE(base_tree) == error_mark_node
1754 || index_tree == error_mark_node
1755 || element_type_tree == error_mark_node)
1756 return this->error_expression();
1758 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1759 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1760 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1761 index_tree, element_size);
1762 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1763 TREE_TYPE(base_tree), base_tree, offset);
1764 return this->make_expression(ptr);
1767 // Return an expression representing ARRAY[INDEX]
1769 Bexpression*
1770 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1771 Location location)
1773 tree array_tree = array->get_tree();
1774 tree index_tree = index->get_tree();
1775 if (array_tree == error_mark_node
1776 || TREE_TYPE(array_tree) == error_mark_node
1777 || index_tree == error_mark_node)
1778 return this->error_expression();
1780 tree ret = build4_loc(location.gcc_location(), ARRAY_REF,
1781 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1782 index_tree, NULL_TREE, NULL_TREE);
1783 return this->make_expression(ret);
1786 // Create an expression for a call to FN_EXPR with FN_ARGS.
1787 Bexpression*
1788 Gcc_backend::call_expression(Bexpression* fn_expr,
1789 const std::vector<Bexpression*>& fn_args,
1790 Location location)
1792 tree fn = fn_expr->get_tree();
1793 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1794 return this->error_expression();
1796 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1797 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1799 size_t nargs = fn_args.size();
1800 tree* args = nargs == 0 ? NULL : new tree[nargs];
1801 for (size_t i = 0; i < nargs; ++i)
1803 args[i] = fn_args.at(i)->get_tree();
1804 if (args[i] == error_mark_node)
1805 return this->error_expression();
1808 tree fndecl = fn;
1809 if (TREE_CODE(fndecl) == ADDR_EXPR)
1810 fndecl = TREE_OPERAND(fndecl, 0);
1812 // This is to support builtin math functions when using 80387 math.
1813 tree excess_type = NULL_TREE;
1814 if (optimize
1815 && TREE_CODE(fndecl) == FUNCTION_DECL
1816 && DECL_IS_BUILTIN(fndecl)
1817 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
1818 && nargs > 0
1819 && ((SCALAR_FLOAT_TYPE_P(rettype)
1820 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1821 || (COMPLEX_FLOAT_TYPE_P(rettype)
1822 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1824 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1825 if (excess_type != NULL_TREE)
1827 tree excess_fndecl = mathfn_built_in(excess_type,
1828 DECL_FUNCTION_CODE(fndecl));
1829 if (excess_fndecl == NULL_TREE)
1830 excess_type = NULL_TREE;
1831 else
1833 fn = build_fold_addr_expr_loc(location.gcc_location(),
1834 excess_fndecl);
1835 for (size_t i = 0; i < nargs; ++i)
1837 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1838 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1839 args[i] = ::convert(excess_type, args[i]);
1845 tree ret =
1846 build_call_array_loc(location.gcc_location(),
1847 excess_type != NULL_TREE ? excess_type : rettype,
1848 fn, nargs, args);
1850 if (excess_type != NULL_TREE)
1852 // Calling convert here can undo our excess precision change.
1853 // That may or may not be a bug in convert_to_real.
1854 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1857 delete[] args;
1858 return this->make_expression(ret);
1861 // An expression as a statement.
1863 Bstatement*
1864 Gcc_backend::expression_statement(Bexpression* expr)
1866 return this->make_statement(expr->get_tree());
1869 // Variable initialization.
1871 Bstatement*
1872 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1874 tree var_tree = var->get_tree();
1875 tree init_tree = init->get_tree();
1876 if (var_tree == error_mark_node || init_tree == error_mark_node)
1877 return this->error_statement();
1878 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1880 // To avoid problems with GNU ld, we don't make zero-sized
1881 // externally visible variables. That might lead us to doing an
1882 // initialization of a zero-sized expression to a non-zero sized
1883 // variable, or vice-versa. Avoid crashes by omitting the
1884 // initializer. Such initializations don't mean anything anyhow.
1885 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1886 && init_tree != NULL_TREE
1887 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1889 DECL_INITIAL(var_tree) = init_tree;
1890 init_tree = NULL_TREE;
1893 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1894 void_type_node, var_tree);
1895 if (init_tree != NULL_TREE)
1896 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1897 void_type_node, init_tree, ret);
1899 return this->make_statement(ret);
1902 // Assignment.
1904 Bstatement*
1905 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1906 Location location)
1908 tree lhs_tree = lhs->get_tree();
1909 tree rhs_tree = rhs->get_tree();
1910 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1911 return this->error_statement();
1913 // To avoid problems with GNU ld, we don't make zero-sized
1914 // externally visible variables. That might lead us to doing an
1915 // assignment of a zero-sized expression to a non-zero sized
1916 // expression; avoid crashes here by avoiding assignments of
1917 // zero-sized expressions. Such assignments don't really mean
1918 // anything anyhow.
1919 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1920 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1921 return this->compound_statement(this->expression_statement(lhs),
1922 this->expression_statement(rhs));
1924 // Sometimes the same unnamed Go type can be created multiple times
1925 // and thus have multiple tree representations. Make sure this does
1926 // not confuse the middle-end.
1927 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1929 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1930 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1931 if (POINTER_TYPE_P(lhs_type_tree)
1932 || INTEGRAL_TYPE_P(lhs_type_tree)
1933 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1934 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1935 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1936 rhs_tree);
1937 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1938 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1940 gcc_assert(int_size_in_bytes(lhs_type_tree)
1941 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1942 rhs_tree = fold_build1_loc(location.gcc_location(),
1943 VIEW_CONVERT_EXPR,
1944 lhs_type_tree, rhs_tree);
1948 return this->make_statement(fold_build2_loc(location.gcc_location(),
1949 MODIFY_EXPR,
1950 void_type_node,
1951 lhs_tree, rhs_tree));
1954 // Return.
1956 Bstatement*
1957 Gcc_backend::return_statement(Bfunction* bfunction,
1958 const std::vector<Bexpression*>& vals,
1959 Location location)
1961 tree fntree = bfunction->get_tree();
1962 if (fntree == error_mark_node)
1963 return this->error_statement();
1964 tree result = DECL_RESULT(fntree);
1965 if (result == error_mark_node)
1966 return this->error_statement();
1968 tree ret;
1969 if (vals.empty())
1970 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
1971 NULL_TREE);
1972 else if (vals.size() == 1)
1974 tree val = vals.front()->get_tree();
1975 if (val == error_mark_node)
1976 return this->error_statement();
1977 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1978 void_type_node, result,
1979 vals.front()->get_tree());
1980 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1981 void_type_node, set);
1983 else
1985 // To return multiple values, copy the values into a temporary
1986 // variable of the right structure type, and then assign the
1987 // temporary variable to the DECL_RESULT in the return
1988 // statement.
1989 tree stmt_list = NULL_TREE;
1990 tree rettype = TREE_TYPE(result);
1992 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
1993 push_struct_function(fntree);
1994 else
1995 push_cfun(DECL_STRUCT_FUNCTION(fntree));
1996 tree rettmp = create_tmp_var(rettype, "RESULT");
1997 pop_cfun();
1999 tree field = TYPE_FIELDS(rettype);
2000 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2001 p != vals.end();
2002 p++, field = DECL_CHAIN(field))
2004 gcc_assert(field != NULL_TREE);
2005 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
2006 TREE_TYPE(field), rettmp, field,
2007 NULL_TREE);
2008 tree val = (*p)->get_tree();
2009 if (val == error_mark_node)
2010 return this->error_statement();
2011 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2012 void_type_node,
2013 ref, (*p)->get_tree());
2014 append_to_statement_list(set, &stmt_list);
2016 gcc_assert(field == NULL_TREE);
2017 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2018 void_type_node,
2019 result, rettmp);
2020 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2021 void_type_node, set);
2022 append_to_statement_list(ret_expr, &stmt_list);
2023 ret = stmt_list;
2025 return this->make_statement(ret);
2028 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2029 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2030 // NULL, it will always be executed. This is used for handling defers in Go
2031 // functions. In C++, the resulting code is of this form:
2032 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2034 Bstatement*
2035 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2036 Bstatement* except_stmt,
2037 Bstatement* finally_stmt,
2038 Location location)
2040 tree stat_tree = bstat->get_tree();
2041 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2042 tree finally_tree = finally_stmt == NULL
2043 ? NULL_TREE
2044 : finally_stmt->get_tree();
2046 if (stat_tree == error_mark_node
2047 || except_tree == error_mark_node
2048 || finally_tree == error_mark_node)
2049 return this->error_statement();
2051 if (except_tree != NULL_TREE)
2052 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2053 void_type_node, stat_tree,
2054 build2_loc(location.gcc_location(), CATCH_EXPR,
2055 void_type_node, NULL, except_tree));
2056 if (finally_tree != NULL_TREE)
2057 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2058 void_type_node, stat_tree, finally_tree);
2059 return this->make_statement(stat_tree);
2062 // If.
2064 Bstatement*
2065 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
2066 Bblock* else_block, Location location)
2068 tree cond_tree = condition->get_tree();
2069 tree then_tree = then_block->get_tree();
2070 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2071 if (cond_tree == error_mark_node
2072 || then_tree == error_mark_node
2073 || else_tree == error_mark_node)
2074 return this->error_statement();
2075 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2076 cond_tree, then_tree, else_tree);
2077 return this->make_statement(ret);
2080 // Switch.
2082 Bstatement*
2083 Gcc_backend::switch_statement(
2084 Bfunction* function,
2085 Bexpression* value,
2086 const std::vector<std::vector<Bexpression*> >& cases,
2087 const std::vector<Bstatement*>& statements,
2088 Location switch_location)
2090 gcc_assert(cases.size() == statements.size());
2092 tree decl = function->get_tree();
2093 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2094 push_struct_function(decl);
2095 else
2096 push_cfun(DECL_STRUCT_FUNCTION(decl));
2098 tree stmt_list = NULL_TREE;
2099 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2100 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2101 ps != statements.end();
2102 ++ps, ++pc)
2104 if (pc->empty())
2106 source_location loc = (*ps != NULL
2107 ? EXPR_LOCATION((*ps)->get_tree())
2108 : UNKNOWN_LOCATION);
2109 tree label = create_artificial_label(loc);
2110 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2111 append_to_statement_list(c, &stmt_list);
2113 else
2115 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2116 pcv != pc->end();
2117 ++pcv)
2119 tree t = (*pcv)->get_tree();
2120 if (t == error_mark_node)
2121 return this->error_statement();
2122 source_location loc = EXPR_LOCATION(t);
2123 tree label = create_artificial_label(loc);
2124 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2125 append_to_statement_list(c, &stmt_list);
2129 if (*ps != NULL)
2131 tree t = (*ps)->get_tree();
2132 if (t == error_mark_node)
2133 return this->error_statement();
2134 append_to_statement_list(t, &stmt_list);
2137 pop_cfun();
2139 tree tv = value->get_tree();
2140 if (tv == error_mark_node)
2141 return this->error_statement();
2142 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
2143 NULL_TREE, tv, stmt_list, NULL_TREE);
2144 return this->make_statement(t);
2147 // Pair of statements.
2149 Bstatement*
2150 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2152 tree stmt_list = NULL_TREE;
2153 tree t = s1->get_tree();
2154 if (t == error_mark_node)
2155 return this->error_statement();
2156 append_to_statement_list(t, &stmt_list);
2157 t = s2->get_tree();
2158 if (t == error_mark_node)
2159 return this->error_statement();
2160 append_to_statement_list(t, &stmt_list);
2162 // If neither statement has any side effects, stmt_list can be NULL
2163 // at this point.
2164 if (stmt_list == NULL_TREE)
2165 stmt_list = integer_zero_node;
2167 return this->make_statement(stmt_list);
2170 // List of statements.
2172 Bstatement*
2173 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2175 tree stmt_list = NULL_TREE;
2176 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2177 p != statements.end();
2178 ++p)
2180 tree t = (*p)->get_tree();
2181 if (t == error_mark_node)
2182 return this->error_statement();
2183 append_to_statement_list(t, &stmt_list);
2185 return this->make_statement(stmt_list);
2188 // Make a block. For some reason gcc uses a dual structure for
2189 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2190 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2191 // the Bblock.
2193 Bblock*
2194 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2195 const std::vector<Bvariable*>& vars,
2196 Location start_location,
2197 Location)
2199 tree block_tree = make_node(BLOCK);
2200 if (enclosing == NULL)
2202 tree fndecl = function->get_tree();
2203 gcc_assert(fndecl != NULL_TREE);
2205 // We may have already created a block for local variables when
2206 // we take the address of a parameter.
2207 if (DECL_INITIAL(fndecl) == NULL_TREE)
2209 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2210 DECL_INITIAL(fndecl) = block_tree;
2212 else
2214 tree superblock_tree = DECL_INITIAL(fndecl);
2215 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2216 tree* pp;
2217 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2218 *pp != NULL_TREE;
2219 pp = &BLOCK_CHAIN(*pp))
2221 *pp = block_tree;
2224 else
2226 tree superbind_tree = enclosing->get_tree();
2227 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2228 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2230 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2231 tree* pp;
2232 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2233 *pp != NULL_TREE;
2234 pp = &BLOCK_CHAIN(*pp))
2236 *pp = block_tree;
2239 tree* pp = &BLOCK_VARS(block_tree);
2240 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2241 pv != vars.end();
2242 ++pv)
2244 *pp = (*pv)->get_tree();
2245 if (*pp != error_mark_node)
2246 pp = &DECL_CHAIN(*pp);
2248 *pp = NULL_TREE;
2250 TREE_USED(block_tree) = 1;
2252 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2253 void_type_node, BLOCK_VARS(block_tree),
2254 NULL_TREE, block_tree);
2255 TREE_SIDE_EFFECTS(bind_tree) = 1;
2256 return new Bblock(bind_tree);
2259 // Add statements to a block.
2261 void
2262 Gcc_backend::block_add_statements(Bblock* bblock,
2263 const std::vector<Bstatement*>& statements)
2265 tree stmt_list = NULL_TREE;
2266 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2267 p != statements.end();
2268 ++p)
2270 tree s = (*p)->get_tree();
2271 if (s != error_mark_node)
2272 append_to_statement_list(s, &stmt_list);
2275 tree bind_tree = bblock->get_tree();
2276 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2277 BIND_EXPR_BODY(bind_tree) = stmt_list;
2280 // Return a block as a statement.
2282 Bstatement*
2283 Gcc_backend::block_statement(Bblock* bblock)
2285 tree bind_tree = bblock->get_tree();
2286 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2287 return this->make_statement(bind_tree);
2290 // This is not static because we declare it with GTY(()) in go-c.h.
2291 tree go_non_zero_struct;
2293 // Return a type corresponding to TYPE with non-zero size.
2295 tree
2296 Gcc_backend::non_zero_size_type(tree type)
2298 if (int_size_in_bytes(type) != 0)
2299 return type;
2301 switch (TREE_CODE(type))
2303 case RECORD_TYPE:
2304 if (TYPE_FIELDS(type) != NULL_TREE)
2306 tree ns = make_node(RECORD_TYPE);
2307 tree field_trees = NULL_TREE;
2308 tree *pp = &field_trees;
2309 for (tree field = TYPE_FIELDS(type);
2310 field != NULL_TREE;
2311 field = DECL_CHAIN(field))
2313 tree ft = TREE_TYPE(field);
2314 if (field == TYPE_FIELDS(type))
2315 ft = non_zero_size_type(ft);
2316 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2317 DECL_NAME(field), ft);
2318 DECL_CONTEXT(f) = ns;
2319 *pp = f;
2320 pp = &DECL_CHAIN(f);
2322 TYPE_FIELDS(ns) = field_trees;
2323 layout_type(ns);
2324 return ns;
2327 if (go_non_zero_struct == NULL_TREE)
2329 type = make_node(RECORD_TYPE);
2330 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2331 get_identifier("dummy"),
2332 boolean_type_node);
2333 DECL_CONTEXT(field) = type;
2334 TYPE_FIELDS(type) = field;
2335 layout_type(type);
2336 go_non_zero_struct = type;
2338 return go_non_zero_struct;
2340 case ARRAY_TYPE:
2342 tree element_type = non_zero_size_type(TREE_TYPE(type));
2343 return build_array_type_nelts(element_type, 1);
2346 default:
2347 gcc_unreachable();
2350 gcc_unreachable();
2353 // Make a global variable.
2355 Bvariable*
2356 Gcc_backend::global_variable(const std::string& package_name,
2357 const std::string& pkgpath,
2358 const std::string& name,
2359 Btype* btype,
2360 bool is_external,
2361 bool is_hidden,
2362 bool in_unique_section,
2363 Location location)
2365 tree type_tree = btype->get_tree();
2366 if (type_tree == error_mark_node)
2367 return this->error_variable();
2369 // The GNU linker does not like dynamic variables with zero size.
2370 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2371 type_tree = this->non_zero_size_type(type_tree);
2373 std::string var_name(package_name);
2374 var_name.push_back('.');
2375 var_name.append(name);
2376 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2377 get_identifier_from_string(var_name),
2378 type_tree);
2379 if (is_external)
2380 DECL_EXTERNAL(decl) = 1;
2381 else
2382 TREE_STATIC(decl) = 1;
2383 if (!is_hidden)
2385 TREE_PUBLIC(decl) = 1;
2387 std::string asm_name(pkgpath);
2388 asm_name.push_back('.');
2389 asm_name.append(name);
2390 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2392 TREE_USED(decl) = 1;
2394 if (in_unique_section)
2395 resolve_unique_section (decl, 0, 1);
2397 go_preserve_from_gc(decl);
2399 return new Bvariable(decl);
2402 // Set the initial value of a global variable.
2404 void
2405 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2407 tree expr_tree = expr->get_tree();
2408 if (expr_tree == error_mark_node)
2409 return;
2410 gcc_assert(TREE_CONSTANT(expr_tree));
2411 tree var_decl = var->get_tree();
2412 if (var_decl == error_mark_node)
2413 return;
2414 DECL_INITIAL(var_decl) = expr_tree;
2416 // If this variable goes in a unique section, it may need to go into
2417 // a different one now that DECL_INITIAL is set.
2418 if (symtab_node::get(var_decl)
2419 && symtab_node::get(var_decl)->implicit_section)
2421 set_decl_section_name (var_decl, NULL);
2422 resolve_unique_section (var_decl,
2423 compute_reloc_for_constant (expr_tree),
2428 // Make a local variable.
2430 Bvariable*
2431 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2432 Btype* btype, bool is_address_taken,
2433 Location location)
2435 tree type_tree = btype->get_tree();
2436 if (type_tree == error_mark_node)
2437 return this->error_variable();
2438 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2439 get_identifier_from_string(name),
2440 type_tree);
2441 DECL_CONTEXT(decl) = function->get_tree();
2442 TREE_USED(decl) = 1;
2443 if (is_address_taken)
2444 TREE_ADDRESSABLE(decl) = 1;
2445 go_preserve_from_gc(decl);
2446 return new Bvariable(decl);
2449 // Make a function parameter variable.
2451 Bvariable*
2452 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2453 Btype* btype, bool is_address_taken,
2454 Location location)
2456 tree type_tree = btype->get_tree();
2457 if (type_tree == error_mark_node)
2458 return this->error_variable();
2459 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2460 get_identifier_from_string(name),
2461 type_tree);
2462 DECL_CONTEXT(decl) = function->get_tree();
2463 DECL_ARG_TYPE(decl) = type_tree;
2464 TREE_USED(decl) = 1;
2465 if (is_address_taken)
2466 TREE_ADDRESSABLE(decl) = 1;
2467 go_preserve_from_gc(decl);
2468 return new Bvariable(decl);
2471 // Make a temporary variable.
2473 Bvariable*
2474 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2475 Btype* btype, Bexpression* binit,
2476 bool is_address_taken,
2477 Location location,
2478 Bstatement** pstatement)
2480 gcc_assert(function != NULL);
2481 tree decl = function->get_tree();
2482 tree type_tree = btype->get_tree();
2483 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2484 if (type_tree == error_mark_node
2485 || init_tree == error_mark_node
2486 || decl == error_mark_node)
2488 *pstatement = this->error_statement();
2489 return this->error_variable();
2492 tree var;
2493 // We can only use create_tmp_var if the type is not addressable.
2494 if (!TREE_ADDRESSABLE(type_tree))
2496 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2497 push_struct_function(decl);
2498 else
2499 push_cfun(DECL_STRUCT_FUNCTION(decl));
2501 var = create_tmp_var(type_tree, "GOTMP");
2502 pop_cfun();
2504 else
2506 gcc_assert(bblock != NULL);
2507 var = build_decl(location.gcc_location(), VAR_DECL,
2508 create_tmp_var_name("GOTMP"),
2509 type_tree);
2510 DECL_ARTIFICIAL(var) = 1;
2511 DECL_IGNORED_P(var) = 1;
2512 TREE_USED(var) = 1;
2513 DECL_CONTEXT(var) = decl;
2515 // We have to add this variable to the BLOCK and the BIND_EXPR.
2516 tree bind_tree = bblock->get_tree();
2517 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2518 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2519 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2520 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2521 BLOCK_VARS(block_tree) = var;
2522 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2525 if (init_tree != NULL_TREE)
2526 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
2527 init_tree);
2529 if (is_address_taken)
2530 TREE_ADDRESSABLE(var) = 1;
2532 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2533 DECL_EXPR,
2534 void_type_node, var));
2535 return new Bvariable(var);
2538 // Create an implicit variable that is compiler-defined. This is used when
2539 // generating GC root variables and storing the values of a slice initializer.
2541 Bvariable*
2542 Gcc_backend::implicit_variable(const std::string& name, Btype* type,
2543 bool is_hidden, bool is_constant,
2544 bool is_common, size_t alignment)
2546 tree type_tree = type->get_tree();
2547 if (type_tree == error_mark_node)
2548 return this->error_variable();
2550 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2551 get_identifier_from_string(name), type_tree);
2552 DECL_EXTERNAL(decl) = 0;
2553 TREE_PUBLIC(decl) = !is_hidden;
2554 TREE_STATIC(decl) = 1;
2555 TREE_USED(decl) = 1;
2556 DECL_ARTIFICIAL(decl) = 1;
2557 if (is_common)
2559 DECL_COMMON(decl) = 1;
2561 // When the initializer for one implicit_variable refers to another,
2562 // it needs to know the visibility of the referenced struct so that
2563 // compute_reloc_for_constant will return the right value. On many
2564 // systems calling make_decl_one_only will mark the decl as weak,
2565 // which will change the return value of compute_reloc_for_constant.
2566 // We can't reliably call make_decl_one_only yet, because we don't
2567 // yet know the initializer. This issue doesn't arise in C because
2568 // Go initializers, unlike C initializers, can be indirectly
2569 // recursive. To ensure that compute_reloc_for_constant computes
2570 // the right value if some other initializer refers to this one, we
2571 // mark this symbol as weak here. We undo that below in
2572 // immutable_struct_set_init before calling mark_decl_one_only.
2573 DECL_WEAK(decl) = 1;
2575 if (is_constant)
2577 TREE_READONLY(decl) = 1;
2578 TREE_CONSTANT(decl) = 1;
2580 if (alignment != 0)
2582 DECL_ALIGN(decl) = alignment * BITS_PER_UNIT;
2583 DECL_USER_ALIGN(decl) = 1;
2586 go_preserve_from_gc(decl);
2587 return new Bvariable(decl);
2590 // Set the initalizer for a variable created by implicit_variable.
2591 // This is where we finish compiling the variable.
2593 void
2594 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
2595 Btype*, bool, bool, bool is_common,
2596 Bexpression* init)
2598 tree decl = var->get_tree();
2599 tree init_tree;
2600 if (init == NULL)
2601 init_tree = NULL_TREE;
2602 else
2603 init_tree = init->get_tree();
2604 if (decl == error_mark_node || init_tree == error_mark_node)
2605 return;
2607 DECL_INITIAL(decl) = init_tree;
2609 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2610 // See the comment where DECL_WEAK is set in implicit_variable.
2611 if (is_common)
2613 DECL_WEAK(decl) = 0;
2614 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2617 resolve_unique_section(decl, 2, 1);
2619 rest_of_decl_compilation(decl, 1, 0);
2622 // Return a reference to an implicit variable defined in another package.
2624 Bvariable*
2625 Gcc_backend::implicit_variable_reference(const std::string& name, Btype* btype)
2627 tree type_tree = btype->get_tree();
2628 if (type_tree == error_mark_node)
2629 return this->error_variable();
2631 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2632 get_identifier_from_string(name), type_tree);
2633 DECL_EXTERNAL(decl) = 0;
2634 TREE_PUBLIC(decl) = 1;
2635 TREE_STATIC(decl) = 1;
2636 DECL_ARTIFICIAL(decl) = 1;
2637 go_preserve_from_gc(decl);
2638 return new Bvariable(decl);
2641 // Create a named immutable initialized data structure.
2643 Bvariable*
2644 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
2645 bool is_common, Btype* btype, Location location)
2647 tree type_tree = btype->get_tree();
2648 if (type_tree == error_mark_node)
2649 return this->error_variable();
2650 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2651 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2652 get_identifier_from_string(name),
2653 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2654 TREE_STATIC(decl) = 1;
2655 TREE_USED(decl) = 1;
2656 TREE_READONLY(decl) = 1;
2657 TREE_CONSTANT(decl) = 1;
2658 DECL_ARTIFICIAL(decl) = 1;
2659 if (!is_hidden)
2660 TREE_PUBLIC(decl) = 1;
2662 // When the initializer for one immutable_struct refers to another,
2663 // it needs to know the visibility of the referenced struct so that
2664 // compute_reloc_for_constant will return the right value. On many
2665 // systems calling make_decl_one_only will mark the decl as weak,
2666 // which will change the return value of compute_reloc_for_constant.
2667 // We can't reliably call make_decl_one_only yet, because we don't
2668 // yet know the initializer. This issue doesn't arise in C because
2669 // Go initializers, unlike C initializers, can be indirectly
2670 // recursive. To ensure that compute_reloc_for_constant computes
2671 // the right value if some other initializer refers to this one, we
2672 // mark this symbol as weak here. We undo that below in
2673 // immutable_struct_set_init before calling mark_decl_one_only.
2674 if (is_common)
2675 DECL_WEAK(decl) = 1;
2677 // We don't call rest_of_decl_compilation until we have the
2678 // initializer.
2680 go_preserve_from_gc(decl);
2681 return new Bvariable(decl);
2684 // Set the initializer for a variable created by immutable_struct.
2685 // This is where we finish compiling the variable.
2687 void
2688 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2689 bool, bool is_common, Btype*, Location,
2690 Bexpression* initializer)
2692 tree decl = var->get_tree();
2693 tree init_tree = initializer->get_tree();
2694 if (decl == error_mark_node || init_tree == error_mark_node)
2695 return;
2697 DECL_INITIAL(decl) = init_tree;
2699 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2700 // See the comment where DECL_WEAK is set in immutable_struct.
2701 if (is_common)
2703 DECL_WEAK(decl) = 0;
2704 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2707 // These variables are often unneeded in the final program, so put
2708 // them in their own section so that linker GC can discard them.
2709 resolve_unique_section(decl,
2710 compute_reloc_for_constant (init_tree),
2713 rest_of_decl_compilation(decl, 1, 0);
2716 // Return a reference to an immutable initialized data structure
2717 // defined in another package.
2719 Bvariable*
2720 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
2721 Location location)
2723 tree type_tree = btype->get_tree();
2724 if (type_tree == error_mark_node)
2725 return this->error_variable();
2726 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2727 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2728 get_identifier_from_string(name),
2729 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2730 TREE_READONLY(decl) = 1;
2731 TREE_CONSTANT(decl) = 1;
2732 DECL_ARTIFICIAL(decl) = 1;
2733 TREE_PUBLIC(decl) = 1;
2734 DECL_EXTERNAL(decl) = 1;
2735 go_preserve_from_gc(decl);
2736 return new Bvariable(decl);
2739 // Make a label.
2741 Blabel*
2742 Gcc_backend::label(Bfunction* function, const std::string& name,
2743 Location location)
2745 tree decl;
2746 if (name.empty())
2748 tree func_tree = function->get_tree();
2749 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2750 push_struct_function(func_tree);
2751 else
2752 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2754 decl = create_artificial_label(location.gcc_location());
2756 pop_cfun();
2758 else
2760 tree id = get_identifier_from_string(name);
2761 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
2762 void_type_node);
2763 DECL_CONTEXT(decl) = function->get_tree();
2765 return new Blabel(decl);
2768 // Make a statement which defines a label.
2770 Bstatement*
2771 Gcc_backend::label_definition_statement(Blabel* label)
2773 tree lab = label->get_tree();
2774 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
2775 void_type_node, lab);
2776 return this->make_statement(ret);
2779 // Make a goto statement.
2781 Bstatement*
2782 Gcc_backend::goto_statement(Blabel* label, Location location)
2784 tree lab = label->get_tree();
2785 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
2786 lab);
2787 return this->make_statement(ret);
2790 // Get the address of a label.
2792 Bexpression*
2793 Gcc_backend::label_address(Blabel* label, Location location)
2795 tree lab = label->get_tree();
2796 TREE_USED(lab) = 1;
2797 TREE_ADDRESSABLE(lab) = 1;
2798 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
2799 build_fold_addr_expr_loc(location.gcc_location(),
2800 lab));
2801 return this->make_expression(ret);
2804 // Declare or define a new function.
2806 Bfunction*
2807 Gcc_backend::function(Btype* fntype, const std::string& name,
2808 const std::string& asm_name, bool is_visible,
2809 bool is_declaration, bool is_inlinable,
2810 bool disable_split_stack, bool in_unique_section,
2811 Location location)
2813 tree functype = fntype->get_tree();
2814 if (functype != error_mark_node)
2816 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
2817 functype = TREE_TYPE(functype);
2819 tree id = get_identifier_from_string(name);
2820 if (functype == error_mark_node || id == error_mark_node)
2821 return this->error_function();
2823 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
2824 if (!asm_name.empty())
2825 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2826 if (is_visible)
2827 TREE_PUBLIC(decl) = 1;
2828 if (is_declaration)
2829 DECL_EXTERNAL(decl) = 1;
2830 else
2832 tree restype = TREE_TYPE(functype);
2833 tree resdecl =
2834 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
2835 DECL_ARTIFICIAL(resdecl) = 1;
2836 DECL_IGNORED_P(resdecl) = 1;
2837 DECL_CONTEXT(resdecl) = decl;
2838 DECL_RESULT(decl) = resdecl;
2840 if (!is_inlinable)
2841 DECL_UNINLINABLE(decl) = 1;
2842 if (disable_split_stack)
2844 tree attr = get_identifier("__no_split_stack__");
2845 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
2847 if (in_unique_section)
2848 resolve_unique_section(decl, 0, 1);
2850 go_preserve_from_gc(decl);
2851 return new Bfunction(decl);
2854 // Create a statement that runs all deferred calls for FUNCTION. This should
2855 // be a statement that looks like this in C++:
2856 // finish:
2857 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2859 Bstatement*
2860 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
2861 Bexpression* defer, Location location)
2863 tree undefer_tree = undefer->get_tree();
2864 tree defer_tree = defer->get_tree();
2865 tree fntree = function->get_tree();
2867 if (undefer_tree == error_mark_node
2868 || defer_tree == error_mark_node
2869 || fntree == error_mark_node)
2870 return this->error_statement();
2872 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2873 push_struct_function(fntree);
2874 else
2875 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2877 tree stmt_list = NULL;
2878 Blabel* blabel = this->label(function, "", location);
2879 Bstatement* label_def = this->label_definition_statement(blabel);
2880 append_to_statement_list(label_def->get_tree(), &stmt_list);
2882 Bstatement* jump_stmt = this->goto_statement(blabel, location);
2883 tree jump = jump_stmt->get_tree();
2884 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
2885 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
2886 tree try_catch =
2887 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
2888 append_to_statement_list(try_catch, &stmt_list);
2889 pop_cfun();
2891 return this->make_statement(stmt_list);
2894 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2895 // This will only be called for a function definition.
2897 bool
2898 Gcc_backend::function_set_parameters(Bfunction* function,
2899 const std::vector<Bvariable*>& param_vars)
2901 tree func_tree = function->get_tree();
2902 if (func_tree == error_mark_node)
2903 return false;
2905 tree params = NULL_TREE;
2906 tree *pp = &params;
2907 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
2908 pv != param_vars.end();
2909 ++pv)
2911 *pp = (*pv)->get_tree();
2912 gcc_assert(*pp != error_mark_node);
2913 pp = &DECL_CHAIN(*pp);
2915 *pp = NULL_TREE;
2916 DECL_ARGUMENTS(func_tree) = params;
2917 return true;
2920 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2922 bool
2923 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
2925 tree func_tree = function->get_tree();
2926 tree code = code_stmt->get_tree();
2928 if (func_tree == error_mark_node || code == error_mark_node)
2929 return false;
2930 DECL_SAVED_TREE(func_tree) = code;
2931 return true;
2934 // Look up a named built-in function in the current backend implementation.
2935 // Returns NULL if no built-in function by that name exists.
2937 Bfunction*
2938 Gcc_backend::lookup_builtin(const std::string& name)
2940 if (this->builtin_functions_.count(name) != 0)
2941 return this->builtin_functions_[name];
2942 return NULL;
2945 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
2946 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
2948 void
2949 Gcc_backend::write_global_definitions(
2950 const std::vector<Btype*>& type_decls,
2951 const std::vector<Bexpression*>& constant_decls,
2952 const std::vector<Bfunction*>& function_decls,
2953 const std::vector<Bvariable*>& variable_decls)
2955 size_t count_definitions = type_decls.size() + constant_decls.size()
2956 + function_decls.size() + variable_decls.size();
2958 tree* defs = new tree[count_definitions];
2960 // Convert all non-erroneous declarations into Gimple form.
2961 size_t i = 0;
2962 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
2963 p != variable_decls.end();
2964 ++p)
2966 if ((*p)->get_tree() != error_mark_node)
2968 defs[i] = (*p)->get_tree();
2969 go_preserve_from_gc(defs[i]);
2970 ++i;
2974 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
2975 p != type_decls.end();
2976 ++p)
2978 tree type_tree = (*p)->get_tree();
2979 if (type_tree != error_mark_node
2980 && IS_TYPE_OR_DECL_P(type_tree))
2982 defs[i] = TYPE_NAME(type_tree);
2983 gcc_assert(defs[i] != NULL);
2984 go_preserve_from_gc(defs[i]);
2985 ++i;
2988 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
2989 p != constant_decls.end();
2990 ++p)
2992 if ((*p)->get_tree() != error_mark_node)
2994 defs[i] = (*p)->get_tree();
2995 go_preserve_from_gc(defs[i]);
2996 ++i;
2999 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
3000 p != function_decls.end();
3001 ++p)
3003 tree decl = (*p)->get_tree();
3004 if (decl != error_mark_node)
3006 go_preserve_from_gc(decl);
3007 gimplify_function_tree(decl);
3008 cgraph_node::finalize_function(decl, true);
3010 defs[i] = decl;
3011 ++i;
3015 // Pass everything back to the middle-end.
3017 wrapup_global_declarations(defs, i);
3019 symtab->finalize_compilation_unit();
3021 check_global_declarations(defs, i);
3022 emit_debug_global_declarations(defs, i);
3024 delete[] defs;
3027 // Define a builtin function. BCODE is the builtin function code
3028 // defined by builtins.def. NAME is the name of the builtin function.
3029 // LIBNAME is the name of the corresponding library function, and is
3030 // NULL if there isn't one. FNTYPE is the type of the function.
3031 // CONST_P is true if the function has the const attribute.
3033 void
3034 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3035 const char* libname, tree fntype, bool const_p)
3037 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3038 libname, NULL_TREE);
3039 if (const_p)
3040 TREE_READONLY(decl) = 1;
3041 set_builtin_decl(bcode, decl, true);
3042 this->builtin_functions_[name] = this->make_function(decl);
3043 if (libname != NULL)
3045 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3046 NULL, NULL_TREE);
3047 if (const_p)
3048 TREE_READONLY(decl) = 1;
3049 this->builtin_functions_[libname] = this->make_function(decl);
3053 // Return the backend generator.
3055 Backend*
3056 go_get_backend()
3058 return new Gcc_backend();