* Add TARGET_CANNOT_SUBSTITUTE_MEM_EQUIV target macro.
[official-gcc.git] / gcc / go / go-gcc.cc
blob9aad6b07b5c79356da9cfc5f92266f32db5f9d70
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)
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());
1698 vec<constructor_elt, va_gc> *init;
1699 vec_alloc(init, vals.size());
1701 bool is_constant = true;
1702 for (size_t i = 0; i < vals.size(); ++i)
1704 tree index = size_int(indexes[i]);
1705 tree val = (vals[i])->get_tree();
1707 if (index == error_mark_node
1708 || val == error_mark_node)
1709 return this->error_expression();
1711 if (!TREE_CONSTANT(val))
1712 is_constant = false;
1714 constructor_elt empty = {NULL, NULL};
1715 constructor_elt* elt = init->quick_push(empty);
1716 elt->index = index;
1717 elt->value = val;
1720 tree ret = build_constructor(type_tree, init);
1721 if (is_constant)
1722 TREE_CONSTANT(ret) = 1;
1723 return this->make_expression(ret);
1726 // Return an expression for the address of BASE[INDEX].
1728 Bexpression*
1729 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1730 Location location)
1732 tree base_tree = base->get_tree();
1733 tree index_tree = index->get_tree();
1734 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1735 if (base_tree == error_mark_node
1736 || TREE_TYPE(base_tree) == error_mark_node
1737 || index_tree == error_mark_node
1738 || element_type_tree == error_mark_node)
1739 return this->error_expression();
1741 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1742 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1743 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1744 index_tree, element_size);
1745 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1746 TREE_TYPE(base_tree), base_tree, offset);
1747 return this->make_expression(ptr);
1750 // Return an expression representing ARRAY[INDEX]
1752 Bexpression*
1753 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1754 Location location)
1756 tree array_tree = array->get_tree();
1757 tree index_tree = index->get_tree();
1758 if (array_tree == error_mark_node
1759 || TREE_TYPE(array_tree) == error_mark_node
1760 || index_tree == error_mark_node)
1761 return this->error_expression();
1763 tree ret = build4_loc(location.gcc_location(), ARRAY_REF,
1764 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1765 index_tree, NULL_TREE, NULL_TREE);
1766 return this->make_expression(ret);
1769 // Create an expression for a call to FN_EXPR with FN_ARGS.
1770 Bexpression*
1771 Gcc_backend::call_expression(Bexpression* fn_expr,
1772 const std::vector<Bexpression*>& fn_args,
1773 Location location)
1775 tree fn = fn_expr->get_tree();
1776 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1777 return this->error_expression();
1779 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1780 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1782 size_t nargs = fn_args.size();
1783 tree* args = nargs == 0 ? NULL : new tree[nargs];
1784 for (size_t i = 0; i < nargs; ++i)
1786 args[i] = fn_args.at(i)->get_tree();
1787 if (args[i] == error_mark_node)
1788 return this->error_expression();
1791 tree fndecl = fn;
1792 if (TREE_CODE(fndecl) == ADDR_EXPR)
1793 fndecl = TREE_OPERAND(fndecl, 0);
1795 // This is to support builtin math functions when using 80387 math.
1796 tree excess_type = NULL_TREE;
1797 if (optimize
1798 && TREE_CODE(fndecl) == FUNCTION_DECL
1799 && DECL_IS_BUILTIN(fndecl)
1800 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
1801 && nargs > 0
1802 && ((SCALAR_FLOAT_TYPE_P(rettype)
1803 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1804 || (COMPLEX_FLOAT_TYPE_P(rettype)
1805 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1807 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1808 if (excess_type != NULL_TREE)
1810 tree excess_fndecl = mathfn_built_in(excess_type,
1811 DECL_FUNCTION_CODE(fndecl));
1812 if (excess_fndecl == NULL_TREE)
1813 excess_type = NULL_TREE;
1814 else
1816 fn = build_fold_addr_expr_loc(location.gcc_location(),
1817 excess_fndecl);
1818 for (size_t i = 0; i < nargs; ++i)
1820 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1821 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1822 args[i] = ::convert(excess_type, args[i]);
1828 tree ret =
1829 build_call_array_loc(location.gcc_location(),
1830 excess_type != NULL_TREE ? excess_type : rettype,
1831 fn, nargs, args);
1833 if (excess_type != NULL_TREE)
1835 // Calling convert here can undo our excess precision change.
1836 // That may or may not be a bug in convert_to_real.
1837 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1840 delete[] args;
1841 return this->make_expression(ret);
1844 // An expression as a statement.
1846 Bstatement*
1847 Gcc_backend::expression_statement(Bexpression* expr)
1849 return this->make_statement(expr->get_tree());
1852 // Variable initialization.
1854 Bstatement*
1855 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1857 tree var_tree = var->get_tree();
1858 tree init_tree = init->get_tree();
1859 if (var_tree == error_mark_node || init_tree == error_mark_node)
1860 return this->error_statement();
1861 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1863 // To avoid problems with GNU ld, we don't make zero-sized
1864 // externally visible variables. That might lead us to doing an
1865 // initialization of a zero-sized expression to a non-zero sized
1866 // variable, or vice-versa. Avoid crashes by omitting the
1867 // initializer. Such initializations don't mean anything anyhow.
1868 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1869 && init_tree != NULL_TREE
1870 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1872 DECL_INITIAL(var_tree) = init_tree;
1873 init_tree = NULL_TREE;
1876 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1877 void_type_node, var_tree);
1878 if (init_tree != NULL_TREE)
1879 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1880 void_type_node, init_tree, ret);
1882 return this->make_statement(ret);
1885 // Assignment.
1887 Bstatement*
1888 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1889 Location location)
1891 tree lhs_tree = lhs->get_tree();
1892 tree rhs_tree = rhs->get_tree();
1893 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1894 return this->error_statement();
1896 // To avoid problems with GNU ld, we don't make zero-sized
1897 // externally visible variables. That might lead us to doing an
1898 // assignment of a zero-sized expression to a non-zero sized
1899 // expression; avoid crashes here by avoiding assignments of
1900 // zero-sized expressions. Such assignments don't really mean
1901 // anything anyhow.
1902 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1903 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1904 return this->compound_statement(this->expression_statement(lhs),
1905 this->expression_statement(rhs));
1907 // Sometimes the same unnamed Go type can be created multiple times
1908 // and thus have multiple tree representations. Make sure this does
1909 // not confuse the middle-end.
1910 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1912 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1913 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1914 if (POINTER_TYPE_P(lhs_type_tree)
1915 || INTEGRAL_TYPE_P(lhs_type_tree)
1916 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1917 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1918 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1919 rhs_tree);
1920 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1921 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1923 gcc_assert(int_size_in_bytes(lhs_type_tree)
1924 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1925 rhs_tree = fold_build1_loc(location.gcc_location(),
1926 VIEW_CONVERT_EXPR,
1927 lhs_type_tree, rhs_tree);
1931 return this->make_statement(fold_build2_loc(location.gcc_location(),
1932 MODIFY_EXPR,
1933 void_type_node,
1934 lhs_tree, rhs_tree));
1937 // Return.
1939 Bstatement*
1940 Gcc_backend::return_statement(Bfunction* bfunction,
1941 const std::vector<Bexpression*>& vals,
1942 Location location)
1944 tree fntree = bfunction->get_tree();
1945 if (fntree == error_mark_node)
1946 return this->error_statement();
1947 tree result = DECL_RESULT(fntree);
1948 if (result == error_mark_node)
1949 return this->error_statement();
1951 tree ret;
1952 if (vals.empty())
1953 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
1954 NULL_TREE);
1955 else if (vals.size() == 1)
1957 tree val = vals.front()->get_tree();
1958 if (val == error_mark_node)
1959 return this->error_statement();
1960 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1961 void_type_node, result,
1962 vals.front()->get_tree());
1963 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1964 void_type_node, set);
1966 else
1968 // To return multiple values, copy the values into a temporary
1969 // variable of the right structure type, and then assign the
1970 // temporary variable to the DECL_RESULT in the return
1971 // statement.
1972 tree stmt_list = NULL_TREE;
1973 tree rettype = TREE_TYPE(result);
1975 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
1976 push_struct_function(fntree);
1977 else
1978 push_cfun(DECL_STRUCT_FUNCTION(fntree));
1979 tree rettmp = create_tmp_var(rettype, "RESULT");
1980 pop_cfun();
1982 tree field = TYPE_FIELDS(rettype);
1983 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1984 p != vals.end();
1985 p++, field = DECL_CHAIN(field))
1987 gcc_assert(field != NULL_TREE);
1988 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1989 TREE_TYPE(field), rettmp, field,
1990 NULL_TREE);
1991 tree val = (*p)->get_tree();
1992 if (val == error_mark_node)
1993 return this->error_statement();
1994 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1995 void_type_node,
1996 ref, (*p)->get_tree());
1997 append_to_statement_list(set, &stmt_list);
1999 gcc_assert(field == NULL_TREE);
2000 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2001 void_type_node,
2002 result, rettmp);
2003 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2004 void_type_node, set);
2005 append_to_statement_list(ret_expr, &stmt_list);
2006 ret = stmt_list;
2008 return this->make_statement(ret);
2011 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2012 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2013 // NULL, it will always be executed. This is used for handling defers in Go
2014 // functions. In C++, the resulting code is of this form:
2015 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2017 Bstatement*
2018 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2019 Bstatement* except_stmt,
2020 Bstatement* finally_stmt,
2021 Location location)
2023 tree stat_tree = bstat->get_tree();
2024 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2025 tree finally_tree = finally_stmt == NULL
2026 ? NULL_TREE
2027 : finally_stmt->get_tree();
2029 if (stat_tree == error_mark_node
2030 || except_tree == error_mark_node
2031 || finally_tree == error_mark_node)
2032 return this->error_statement();
2034 if (except_tree != NULL_TREE)
2035 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2036 void_type_node, stat_tree,
2037 build2_loc(location.gcc_location(), CATCH_EXPR,
2038 void_type_node, NULL, except_tree));
2039 if (finally_tree != NULL_TREE)
2040 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2041 void_type_node, stat_tree, finally_tree);
2042 return this->make_statement(stat_tree);
2045 // If.
2047 Bstatement*
2048 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
2049 Bblock* else_block, Location location)
2051 tree cond_tree = condition->get_tree();
2052 tree then_tree = then_block->get_tree();
2053 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2054 if (cond_tree == error_mark_node
2055 || then_tree == error_mark_node
2056 || else_tree == error_mark_node)
2057 return this->error_statement();
2058 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2059 cond_tree, then_tree, else_tree);
2060 return this->make_statement(ret);
2063 // Switch.
2065 Bstatement*
2066 Gcc_backend::switch_statement(
2067 Bfunction* function,
2068 Bexpression* value,
2069 const std::vector<std::vector<Bexpression*> >& cases,
2070 const std::vector<Bstatement*>& statements,
2071 Location switch_location)
2073 gcc_assert(cases.size() == statements.size());
2075 tree decl = function->get_tree();
2076 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2077 push_struct_function(decl);
2078 else
2079 push_cfun(DECL_STRUCT_FUNCTION(decl));
2081 tree stmt_list = NULL_TREE;
2082 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2083 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2084 ps != statements.end();
2085 ++ps, ++pc)
2087 if (pc->empty())
2089 source_location loc = (*ps != NULL
2090 ? EXPR_LOCATION((*ps)->get_tree())
2091 : UNKNOWN_LOCATION);
2092 tree label = create_artificial_label(loc);
2093 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2094 append_to_statement_list(c, &stmt_list);
2096 else
2098 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2099 pcv != pc->end();
2100 ++pcv)
2102 tree t = (*pcv)->get_tree();
2103 if (t == error_mark_node)
2104 return this->error_statement();
2105 source_location loc = EXPR_LOCATION(t);
2106 tree label = create_artificial_label(loc);
2107 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2108 append_to_statement_list(c, &stmt_list);
2112 if (*ps != NULL)
2114 tree t = (*ps)->get_tree();
2115 if (t == error_mark_node)
2116 return this->error_statement();
2117 append_to_statement_list(t, &stmt_list);
2120 pop_cfun();
2122 tree tv = value->get_tree();
2123 if (tv == error_mark_node)
2124 return this->error_statement();
2125 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
2126 NULL_TREE, tv, stmt_list, NULL_TREE);
2127 return this->make_statement(t);
2130 // Pair of statements.
2132 Bstatement*
2133 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2135 tree stmt_list = NULL_TREE;
2136 tree t = s1->get_tree();
2137 if (t == error_mark_node)
2138 return this->error_statement();
2139 append_to_statement_list(t, &stmt_list);
2140 t = s2->get_tree();
2141 if (t == error_mark_node)
2142 return this->error_statement();
2143 append_to_statement_list(t, &stmt_list);
2145 // If neither statement has any side effects, stmt_list can be NULL
2146 // at this point.
2147 if (stmt_list == NULL_TREE)
2148 stmt_list = integer_zero_node;
2150 return this->make_statement(stmt_list);
2153 // List of statements.
2155 Bstatement*
2156 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2158 tree stmt_list = NULL_TREE;
2159 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2160 p != statements.end();
2161 ++p)
2163 tree t = (*p)->get_tree();
2164 if (t == error_mark_node)
2165 return this->error_statement();
2166 append_to_statement_list(t, &stmt_list);
2168 return this->make_statement(stmt_list);
2171 // Make a block. For some reason gcc uses a dual structure for
2172 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2173 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2174 // the Bblock.
2176 Bblock*
2177 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2178 const std::vector<Bvariable*>& vars,
2179 Location start_location,
2180 Location)
2182 tree block_tree = make_node(BLOCK);
2183 if (enclosing == NULL)
2185 tree fndecl = function->get_tree();
2186 gcc_assert(fndecl != NULL_TREE);
2188 // We may have already created a block for local variables when
2189 // we take the address of a parameter.
2190 if (DECL_INITIAL(fndecl) == NULL_TREE)
2192 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2193 DECL_INITIAL(fndecl) = block_tree;
2195 else
2197 tree superblock_tree = DECL_INITIAL(fndecl);
2198 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2199 tree* pp;
2200 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2201 *pp != NULL_TREE;
2202 pp = &BLOCK_CHAIN(*pp))
2204 *pp = block_tree;
2207 else
2209 tree superbind_tree = enclosing->get_tree();
2210 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2211 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2213 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2214 tree* pp;
2215 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2216 *pp != NULL_TREE;
2217 pp = &BLOCK_CHAIN(*pp))
2219 *pp = block_tree;
2222 tree* pp = &BLOCK_VARS(block_tree);
2223 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2224 pv != vars.end();
2225 ++pv)
2227 *pp = (*pv)->get_tree();
2228 if (*pp != error_mark_node)
2229 pp = &DECL_CHAIN(*pp);
2231 *pp = NULL_TREE;
2233 TREE_USED(block_tree) = 1;
2235 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2236 void_type_node, BLOCK_VARS(block_tree),
2237 NULL_TREE, block_tree);
2238 TREE_SIDE_EFFECTS(bind_tree) = 1;
2239 return new Bblock(bind_tree);
2242 // Add statements to a block.
2244 void
2245 Gcc_backend::block_add_statements(Bblock* bblock,
2246 const std::vector<Bstatement*>& statements)
2248 tree stmt_list = NULL_TREE;
2249 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2250 p != statements.end();
2251 ++p)
2253 tree s = (*p)->get_tree();
2254 if (s != error_mark_node)
2255 append_to_statement_list(s, &stmt_list);
2258 tree bind_tree = bblock->get_tree();
2259 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2260 BIND_EXPR_BODY(bind_tree) = stmt_list;
2263 // Return a block as a statement.
2265 Bstatement*
2266 Gcc_backend::block_statement(Bblock* bblock)
2268 tree bind_tree = bblock->get_tree();
2269 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2270 return this->make_statement(bind_tree);
2273 // This is not static because we declare it with GTY(()) in go-c.h.
2274 tree go_non_zero_struct;
2276 // Return a type corresponding to TYPE with non-zero size.
2278 tree
2279 Gcc_backend::non_zero_size_type(tree type)
2281 if (int_size_in_bytes(type) != 0)
2282 return type;
2284 switch (TREE_CODE(type))
2286 case RECORD_TYPE:
2287 if (TYPE_FIELDS(type) != NULL_TREE)
2289 tree ns = make_node(RECORD_TYPE);
2290 tree field_trees = NULL_TREE;
2291 tree *pp = &field_trees;
2292 for (tree field = TYPE_FIELDS(type);
2293 field != NULL_TREE;
2294 field = DECL_CHAIN(field))
2296 tree ft = TREE_TYPE(field);
2297 if (field == TYPE_FIELDS(type))
2298 ft = non_zero_size_type(ft);
2299 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2300 DECL_NAME(field), ft);
2301 DECL_CONTEXT(f) = ns;
2302 *pp = f;
2303 pp = &DECL_CHAIN(f);
2305 TYPE_FIELDS(ns) = field_trees;
2306 layout_type(ns);
2307 return ns;
2310 if (go_non_zero_struct == NULL_TREE)
2312 type = make_node(RECORD_TYPE);
2313 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2314 get_identifier("dummy"),
2315 boolean_type_node);
2316 DECL_CONTEXT(field) = type;
2317 TYPE_FIELDS(type) = field;
2318 layout_type(type);
2319 go_non_zero_struct = type;
2321 return go_non_zero_struct;
2323 case ARRAY_TYPE:
2325 tree element_type = non_zero_size_type(TREE_TYPE(type));
2326 return build_array_type_nelts(element_type, 1);
2329 default:
2330 gcc_unreachable();
2333 gcc_unreachable();
2336 // Make a global variable.
2338 Bvariable*
2339 Gcc_backend::global_variable(const std::string& package_name,
2340 const std::string& pkgpath,
2341 const std::string& name,
2342 Btype* btype,
2343 bool is_external,
2344 bool is_hidden,
2345 bool in_unique_section,
2346 Location location)
2348 tree type_tree = btype->get_tree();
2349 if (type_tree == error_mark_node)
2350 return this->error_variable();
2352 // The GNU linker does not like dynamic variables with zero size.
2353 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2354 type_tree = this->non_zero_size_type(type_tree);
2356 std::string var_name(package_name);
2357 var_name.push_back('.');
2358 var_name.append(name);
2359 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2360 get_identifier_from_string(var_name),
2361 type_tree);
2362 if (is_external)
2363 DECL_EXTERNAL(decl) = 1;
2364 else
2365 TREE_STATIC(decl) = 1;
2366 if (!is_hidden)
2368 TREE_PUBLIC(decl) = 1;
2370 std::string asm_name(pkgpath);
2371 asm_name.push_back('.');
2372 asm_name.append(name);
2373 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2375 TREE_USED(decl) = 1;
2377 if (in_unique_section)
2378 resolve_unique_section (decl, 0, 1);
2380 go_preserve_from_gc(decl);
2382 return new Bvariable(decl);
2385 // Set the initial value of a global variable.
2387 void
2388 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2390 tree expr_tree = expr->get_tree();
2391 if (expr_tree == error_mark_node)
2392 return;
2393 gcc_assert(TREE_CONSTANT(expr_tree));
2394 tree var_decl = var->get_tree();
2395 if (var_decl == error_mark_node)
2396 return;
2397 DECL_INITIAL(var_decl) = expr_tree;
2399 // If this variable goes in a unique section, it may need to go into
2400 // a different one now that DECL_INITIAL is set.
2401 if (symtab_node::get(var_decl)
2402 && symtab_node::get(var_decl)->implicit_section)
2404 set_decl_section_name (var_decl, NULL);
2405 resolve_unique_section (var_decl,
2406 compute_reloc_for_constant (expr_tree),
2411 // Make a local variable.
2413 Bvariable*
2414 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2415 Btype* btype, bool is_address_taken,
2416 Location location)
2418 tree type_tree = btype->get_tree();
2419 if (type_tree == error_mark_node)
2420 return this->error_variable();
2421 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2422 get_identifier_from_string(name),
2423 type_tree);
2424 DECL_CONTEXT(decl) = function->get_tree();
2425 TREE_USED(decl) = 1;
2426 if (is_address_taken)
2427 TREE_ADDRESSABLE(decl) = 1;
2428 go_preserve_from_gc(decl);
2429 return new Bvariable(decl);
2432 // Make a function parameter variable.
2434 Bvariable*
2435 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2436 Btype* btype, bool is_address_taken,
2437 Location location)
2439 tree type_tree = btype->get_tree();
2440 if (type_tree == error_mark_node)
2441 return this->error_variable();
2442 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2443 get_identifier_from_string(name),
2444 type_tree);
2445 DECL_CONTEXT(decl) = function->get_tree();
2446 DECL_ARG_TYPE(decl) = type_tree;
2447 TREE_USED(decl) = 1;
2448 if (is_address_taken)
2449 TREE_ADDRESSABLE(decl) = 1;
2450 go_preserve_from_gc(decl);
2451 return new Bvariable(decl);
2454 // Make a temporary variable.
2456 Bvariable*
2457 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2458 Btype* btype, Bexpression* binit,
2459 bool is_address_taken,
2460 Location location,
2461 Bstatement** pstatement)
2463 gcc_assert(function != NULL);
2464 tree decl = function->get_tree();
2465 tree type_tree = btype->get_tree();
2466 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2467 if (type_tree == error_mark_node
2468 || init_tree == error_mark_node
2469 || decl == error_mark_node)
2471 *pstatement = this->error_statement();
2472 return this->error_variable();
2475 tree var;
2476 // We can only use create_tmp_var if the type is not addressable.
2477 if (!TREE_ADDRESSABLE(type_tree))
2479 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2480 push_struct_function(decl);
2481 else
2482 push_cfun(DECL_STRUCT_FUNCTION(decl));
2484 var = create_tmp_var(type_tree, "GOTMP");
2485 pop_cfun();
2487 else
2489 gcc_assert(bblock != NULL);
2490 var = build_decl(location.gcc_location(), VAR_DECL,
2491 create_tmp_var_name("GOTMP"),
2492 type_tree);
2493 DECL_ARTIFICIAL(var) = 1;
2494 DECL_IGNORED_P(var) = 1;
2495 TREE_USED(var) = 1;
2496 DECL_CONTEXT(var) = decl;
2498 // We have to add this variable to the BLOCK and the BIND_EXPR.
2499 tree bind_tree = bblock->get_tree();
2500 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2501 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2502 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2503 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2504 BLOCK_VARS(block_tree) = var;
2505 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2508 if (init_tree != NULL_TREE)
2509 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
2510 init_tree);
2512 if (is_address_taken)
2513 TREE_ADDRESSABLE(var) = 1;
2515 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2516 DECL_EXPR,
2517 void_type_node, var));
2518 return new Bvariable(var);
2521 // Create an implicit variable that is compiler-defined. This is used when
2522 // generating GC root variables and storing the values of a slice initializer.
2524 Bvariable*
2525 Gcc_backend::implicit_variable(const std::string& name, Btype* type,
2526 bool is_hidden, bool is_constant,
2527 bool is_common, size_t alignment)
2529 tree type_tree = type->get_tree();
2530 if (type_tree == error_mark_node)
2531 return this->error_variable();
2533 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2534 get_identifier_from_string(name), type_tree);
2535 DECL_EXTERNAL(decl) = 0;
2536 TREE_PUBLIC(decl) = !is_hidden;
2537 TREE_STATIC(decl) = 1;
2538 TREE_USED(decl) = 1;
2539 DECL_ARTIFICIAL(decl) = 1;
2540 if (is_common)
2542 DECL_COMMON(decl) = 1;
2544 // When the initializer for one implicit_variable refers to another,
2545 // it needs to know the visibility of the referenced struct so that
2546 // compute_reloc_for_constant will return the right value. On many
2547 // systems calling make_decl_one_only will mark the decl as weak,
2548 // which will change the return value of compute_reloc_for_constant.
2549 // We can't reliably call make_decl_one_only yet, because we don't
2550 // yet know the initializer. This issue doesn't arise in C because
2551 // Go initializers, unlike C initializers, can be indirectly
2552 // recursive. To ensure that compute_reloc_for_constant computes
2553 // the right value if some other initializer refers to this one, we
2554 // mark this symbol as weak here. We undo that below in
2555 // immutable_struct_set_init before calling mark_decl_one_only.
2556 DECL_WEAK(decl) = 1;
2558 if (is_constant)
2560 TREE_READONLY(decl) = 1;
2561 TREE_CONSTANT(decl) = 1;
2563 if (alignment != 0)
2565 DECL_ALIGN(decl) = alignment * BITS_PER_UNIT;
2566 DECL_USER_ALIGN(decl) = 1;
2569 go_preserve_from_gc(decl);
2570 return new Bvariable(decl);
2573 // Set the initalizer for a variable created by implicit_variable.
2574 // This is where we finish compiling the variable.
2576 void
2577 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
2578 Btype*, bool, bool, bool is_common,
2579 Bexpression* init)
2581 tree decl = var->get_tree();
2582 tree init_tree;
2583 if (init == NULL)
2584 init_tree = NULL_TREE;
2585 else
2586 init_tree = init->get_tree();
2587 if (decl == error_mark_node || init_tree == error_mark_node)
2588 return;
2590 DECL_INITIAL(decl) = init_tree;
2592 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2593 // See the comment where DECL_WEAK is set in implicit_variable.
2594 if (is_common)
2596 DECL_WEAK(decl) = 0;
2597 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2600 resolve_unique_section(decl, 2, 1);
2602 rest_of_decl_compilation(decl, 1, 0);
2605 // Return a reference to an implicit variable defined in another package.
2607 Bvariable*
2608 Gcc_backend::implicit_variable_reference(const std::string& name, Btype* btype)
2610 tree type_tree = btype->get_tree();
2611 if (type_tree == error_mark_node)
2612 return this->error_variable();
2614 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2615 get_identifier_from_string(name), type_tree);
2616 DECL_EXTERNAL(decl) = 0;
2617 TREE_PUBLIC(decl) = 1;
2618 TREE_STATIC(decl) = 1;
2619 DECL_ARTIFICIAL(decl) = 1;
2620 go_preserve_from_gc(decl);
2621 return new Bvariable(decl);
2624 // Create a named immutable initialized data structure.
2626 Bvariable*
2627 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
2628 bool is_common, Btype* btype, Location location)
2630 tree type_tree = btype->get_tree();
2631 if (type_tree == error_mark_node)
2632 return this->error_variable();
2633 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2634 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2635 get_identifier_from_string(name),
2636 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2637 TREE_STATIC(decl) = 1;
2638 TREE_USED(decl) = 1;
2639 TREE_READONLY(decl) = 1;
2640 TREE_CONSTANT(decl) = 1;
2641 DECL_ARTIFICIAL(decl) = 1;
2642 if (!is_hidden)
2643 TREE_PUBLIC(decl) = 1;
2645 // When the initializer for one immutable_struct refers to another,
2646 // it needs to know the visibility of the referenced struct so that
2647 // compute_reloc_for_constant will return the right value. On many
2648 // systems calling make_decl_one_only will mark the decl as weak,
2649 // which will change the return value of compute_reloc_for_constant.
2650 // We can't reliably call make_decl_one_only yet, because we don't
2651 // yet know the initializer. This issue doesn't arise in C because
2652 // Go initializers, unlike C initializers, can be indirectly
2653 // recursive. To ensure that compute_reloc_for_constant computes
2654 // the right value if some other initializer refers to this one, we
2655 // mark this symbol as weak here. We undo that below in
2656 // immutable_struct_set_init before calling mark_decl_one_only.
2657 if (is_common)
2658 DECL_WEAK(decl) = 1;
2660 // We don't call rest_of_decl_compilation until we have the
2661 // initializer.
2663 go_preserve_from_gc(decl);
2664 return new Bvariable(decl);
2667 // Set the initializer for a variable created by immutable_struct.
2668 // This is where we finish compiling the variable.
2670 void
2671 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2672 bool, bool is_common, Btype*, Location,
2673 Bexpression* initializer)
2675 tree decl = var->get_tree();
2676 tree init_tree = initializer->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 immutable_struct.
2684 if (is_common)
2686 DECL_WEAK(decl) = 0;
2687 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2690 // These variables are often unneeded in the final program, so put
2691 // them in their own section so that linker GC can discard them.
2692 resolve_unique_section(decl,
2693 compute_reloc_for_constant (init_tree),
2696 rest_of_decl_compilation(decl, 1, 0);
2699 // Return a reference to an immutable initialized data structure
2700 // defined in another package.
2702 Bvariable*
2703 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
2704 Location location)
2706 tree type_tree = btype->get_tree();
2707 if (type_tree == error_mark_node)
2708 return this->error_variable();
2709 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2710 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2711 get_identifier_from_string(name),
2712 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2713 TREE_READONLY(decl) = 1;
2714 TREE_CONSTANT(decl) = 1;
2715 DECL_ARTIFICIAL(decl) = 1;
2716 TREE_PUBLIC(decl) = 1;
2717 DECL_EXTERNAL(decl) = 1;
2718 go_preserve_from_gc(decl);
2719 return new Bvariable(decl);
2722 // Make a label.
2724 Blabel*
2725 Gcc_backend::label(Bfunction* function, const std::string& name,
2726 Location location)
2728 tree decl;
2729 if (name.empty())
2731 tree func_tree = function->get_tree();
2732 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2733 push_struct_function(func_tree);
2734 else
2735 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2737 decl = create_artificial_label(location.gcc_location());
2739 pop_cfun();
2741 else
2743 tree id = get_identifier_from_string(name);
2744 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
2745 void_type_node);
2746 DECL_CONTEXT(decl) = function->get_tree();
2748 return new Blabel(decl);
2751 // Make a statement which defines a label.
2753 Bstatement*
2754 Gcc_backend::label_definition_statement(Blabel* label)
2756 tree lab = label->get_tree();
2757 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
2758 void_type_node, lab);
2759 return this->make_statement(ret);
2762 // Make a goto statement.
2764 Bstatement*
2765 Gcc_backend::goto_statement(Blabel* label, Location location)
2767 tree lab = label->get_tree();
2768 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
2769 lab);
2770 return this->make_statement(ret);
2773 // Get the address of a label.
2775 Bexpression*
2776 Gcc_backend::label_address(Blabel* label, Location location)
2778 tree lab = label->get_tree();
2779 TREE_USED(lab) = 1;
2780 TREE_ADDRESSABLE(lab) = 1;
2781 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
2782 build_fold_addr_expr_loc(location.gcc_location(),
2783 lab));
2784 return this->make_expression(ret);
2787 // Declare or define a new function.
2789 Bfunction*
2790 Gcc_backend::function(Btype* fntype, const std::string& name,
2791 const std::string& asm_name, bool is_visible,
2792 bool is_declaration, bool is_inlinable,
2793 bool disable_split_stack, bool in_unique_section,
2794 Location location)
2796 tree functype = fntype->get_tree();
2797 if (functype != error_mark_node)
2799 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
2800 functype = TREE_TYPE(functype);
2802 tree id = get_identifier_from_string(name);
2803 if (functype == error_mark_node || id == error_mark_node)
2804 return this->error_function();
2806 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
2807 if (!asm_name.empty())
2808 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2809 if (is_visible)
2810 TREE_PUBLIC(decl) = 1;
2811 if (is_declaration)
2812 DECL_EXTERNAL(decl) = 1;
2813 else
2815 tree restype = TREE_TYPE(functype);
2816 tree resdecl =
2817 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
2818 DECL_ARTIFICIAL(resdecl) = 1;
2819 DECL_IGNORED_P(resdecl) = 1;
2820 DECL_CONTEXT(resdecl) = decl;
2821 DECL_RESULT(decl) = resdecl;
2823 if (!is_inlinable)
2824 DECL_UNINLINABLE(decl) = 1;
2825 if (disable_split_stack)
2827 tree attr = get_identifier("__no_split_stack__");
2828 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
2830 if (in_unique_section)
2831 resolve_unique_section(decl, 0, 1);
2833 go_preserve_from_gc(decl);
2834 return new Bfunction(decl);
2837 // Create a statement that runs all deferred calls for FUNCTION. This should
2838 // be a statement that looks like this in C++:
2839 // finish:
2840 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2842 Bstatement*
2843 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
2844 Bexpression* defer, Location location)
2846 tree undefer_tree = undefer->get_tree();
2847 tree defer_tree = defer->get_tree();
2848 tree fntree = function->get_tree();
2850 if (undefer_tree == error_mark_node
2851 || defer_tree == error_mark_node
2852 || fntree == error_mark_node)
2853 return this->error_statement();
2855 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2856 push_struct_function(fntree);
2857 else
2858 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2860 tree stmt_list = NULL;
2861 Blabel* blabel = this->label(function, "", location);
2862 Bstatement* label_def = this->label_definition_statement(blabel);
2863 append_to_statement_list(label_def->get_tree(), &stmt_list);
2865 Bstatement* jump_stmt = this->goto_statement(blabel, location);
2866 tree jump = jump_stmt->get_tree();
2867 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
2868 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
2869 tree try_catch =
2870 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
2871 append_to_statement_list(try_catch, &stmt_list);
2872 pop_cfun();
2874 return this->make_statement(stmt_list);
2877 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2878 // This will only be called for a function definition.
2880 bool
2881 Gcc_backend::function_set_parameters(Bfunction* function,
2882 const std::vector<Bvariable*>& param_vars)
2884 tree func_tree = function->get_tree();
2885 if (func_tree == error_mark_node)
2886 return false;
2888 tree params = NULL_TREE;
2889 tree *pp = &params;
2890 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
2891 pv != param_vars.end();
2892 ++pv)
2894 *pp = (*pv)->get_tree();
2895 gcc_assert(*pp != error_mark_node);
2896 pp = &DECL_CHAIN(*pp);
2898 *pp = NULL_TREE;
2899 DECL_ARGUMENTS(func_tree) = params;
2900 return true;
2903 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2905 bool
2906 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
2908 tree func_tree = function->get_tree();
2909 tree code = code_stmt->get_tree();
2911 if (func_tree == error_mark_node || code == error_mark_node)
2912 return false;
2913 DECL_SAVED_TREE(func_tree) = code;
2914 return true;
2917 // Look up a named built-in function in the current backend implementation.
2918 // Returns NULL if no built-in function by that name exists.
2920 Bfunction*
2921 Gcc_backend::lookup_builtin(const std::string& name)
2923 if (this->builtin_functions_.count(name) != 0)
2924 return this->builtin_functions_[name];
2925 return NULL;
2928 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
2929 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
2931 void
2932 Gcc_backend::write_global_definitions(
2933 const std::vector<Btype*>& type_decls,
2934 const std::vector<Bexpression*>& constant_decls,
2935 const std::vector<Bfunction*>& function_decls,
2936 const std::vector<Bvariable*>& variable_decls)
2938 size_t count_definitions = type_decls.size() + constant_decls.size()
2939 + function_decls.size() + variable_decls.size();
2941 tree* defs = new tree[count_definitions];
2943 // Convert all non-erroneous declarations into Gimple form.
2944 size_t i = 0;
2945 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
2946 p != variable_decls.end();
2947 ++p)
2949 if ((*p)->get_tree() != error_mark_node)
2951 defs[i] = (*p)->get_tree();
2952 go_preserve_from_gc(defs[i]);
2953 ++i;
2957 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
2958 p != type_decls.end();
2959 ++p)
2961 tree type_tree = (*p)->get_tree();
2962 if (type_tree != error_mark_node
2963 && IS_TYPE_OR_DECL_P(type_tree))
2965 defs[i] = TYPE_NAME(type_tree);
2966 gcc_assert(defs[i] != NULL);
2967 go_preserve_from_gc(defs[i]);
2968 ++i;
2971 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
2972 p != constant_decls.end();
2973 ++p)
2975 if ((*p)->get_tree() != error_mark_node)
2977 defs[i] = (*p)->get_tree();
2978 go_preserve_from_gc(defs[i]);
2979 ++i;
2982 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
2983 p != function_decls.end();
2984 ++p)
2986 tree decl = (*p)->get_tree();
2987 if (decl != error_mark_node)
2989 go_preserve_from_gc(decl);
2990 gimplify_function_tree(decl);
2991 cgraph_node::finalize_function(decl, true);
2993 defs[i] = decl;
2994 ++i;
2998 // Pass everything back to the middle-end.
3000 wrapup_global_declarations(defs, i);
3002 symtab->finalize_compilation_unit();
3004 check_global_declarations(defs, i);
3005 emit_debug_global_declarations(defs, i);
3007 delete[] defs;
3010 // Define a builtin function. BCODE is the builtin function code
3011 // defined by builtins.def. NAME is the name of the builtin function.
3012 // LIBNAME is the name of the corresponding library function, and is
3013 // NULL if there isn't one. FNTYPE is the type of the function.
3014 // CONST_P is true if the function has the const attribute.
3016 void
3017 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3018 const char* libname, tree fntype, bool const_p)
3020 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3021 libname, NULL_TREE);
3022 if (const_p)
3023 TREE_READONLY(decl) = 1;
3024 set_builtin_decl(bcode, decl, true);
3025 this->builtin_functions_[name] = this->make_function(decl);
3026 if (libname != NULL)
3028 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3029 NULL, NULL_TREE);
3030 if (const_p)
3031 TREE_READONLY(decl) = 1;
3032 this->builtin_functions_[libname] = this->make_function(decl);
3036 // Return the backend generator.
3038 Backend*
3039 go_get_backend()
3041 return new Gcc_backend();