Daily bump.
[official-gcc.git] / gcc / go / go-gcc.cc
blob08f014fa02e0693e14f8f3e337e6e7b7ec456d56
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2015 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
5 // This file is part of GCC.
7 // GCC is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3, or (at your option) any later
10 // version.
12 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 #include "go-system.h"
23 // This has to be included outside of extern "C", so we have to
24 // include it here before tree.h includes it later.
25 #include <gmp.h>
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "options.h"
35 #include "wide-int.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "fold-const.h"
39 #include "stringpool.h"
40 #include "stor-layout.h"
41 #include "varasm.h"
42 #include "tree-iterator.h"
43 #include "hash-map.h"
44 #include "is-a.h"
45 #include "plugin-api.h"
46 #include "tm.h"
47 #include "hard-reg-set.h"
48 #include "input.h"
49 #include "function.h"
50 #include "ipa-ref.h"
51 #include "cgraph.h"
52 #include "convert.h"
53 #include "gimple-expr.h"
54 #include "gimplify.h"
55 #include "langhooks.h"
56 #include "toplev.h"
57 #include "output.h"
58 #include "real.h"
59 #include "realmpfr.h"
60 #include "builtins.h"
62 #include "go-c.h"
64 #include "gogo.h"
65 #include "backend.h"
67 // A class wrapping a tree.
69 class Gcc_tree
71 public:
72 Gcc_tree(tree t)
73 : t_(t)
74 { }
76 tree
77 get_tree() const
78 { return this->t_; }
80 void
81 set_tree(tree t)
82 { this->t_ = t; }
84 private:
85 tree t_;
88 // In gcc, types, expressions, and statements are all trees.
89 class Btype : public Gcc_tree
91 public:
92 Btype(tree t)
93 : Gcc_tree(t)
94 { }
97 class Bexpression : public Gcc_tree
99 public:
100 Bexpression(tree t)
101 : Gcc_tree(t)
105 class Bstatement : public Gcc_tree
107 public:
108 Bstatement(tree t)
109 : Gcc_tree(t)
113 class Bfunction : public Gcc_tree
115 public:
116 Bfunction(tree t)
117 : Gcc_tree(t)
121 class Bblock : public Gcc_tree
123 public:
124 Bblock(tree t)
125 : Gcc_tree(t)
129 class Bvariable : public Gcc_tree
131 public:
132 Bvariable(tree t)
133 : Gcc_tree(t)
137 class Blabel : public Gcc_tree
139 public:
140 Blabel(tree t)
141 : Gcc_tree(t)
145 // This file implements the interface between the Go frontend proper
146 // and the gcc IR. This implements specific instantiations of
147 // abstract classes defined by the Go frontend proper. The Go
148 // frontend proper class methods of these classes to generate the
149 // backend representation.
151 class Gcc_backend : public Backend
153 public:
154 Gcc_backend();
156 // Types.
158 Btype*
159 error_type()
160 { return this->make_type(error_mark_node); }
162 Btype*
163 void_type()
164 { return this->make_type(void_type_node); }
166 Btype*
167 bool_type()
168 { return this->make_type(boolean_type_node); }
170 Btype*
171 integer_type(bool, int);
173 Btype*
174 float_type(int);
176 Btype*
177 complex_type(int);
179 Btype*
180 pointer_type(Btype*);
182 Btype*
183 function_type(const Btyped_identifier&,
184 const std::vector<Btyped_identifier>&,
185 const std::vector<Btyped_identifier>&,
186 Btype*,
187 const Location);
189 Btype*
190 struct_type(const std::vector<Btyped_identifier>&);
192 Btype*
193 array_type(Btype*, Bexpression*);
195 Btype*
196 placeholder_pointer_type(const std::string&, Location, bool);
198 bool
199 set_placeholder_pointer_type(Btype*, Btype*);
201 bool
202 set_placeholder_function_type(Btype*, Btype*);
204 Btype*
205 placeholder_struct_type(const std::string&, Location);
207 bool
208 set_placeholder_struct_type(Btype* placeholder,
209 const std::vector<Btyped_identifier>&);
211 Btype*
212 placeholder_array_type(const std::string&, Location);
214 bool
215 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
217 Btype*
218 named_type(const std::string&, Btype*, Location);
220 Btype*
221 circular_pointer_type(Btype*, bool);
223 bool
224 is_circular_pointer_type(Btype*);
226 int64_t
227 type_size(Btype*);
229 int64_t
230 type_alignment(Btype*);
232 int64_t
233 type_field_alignment(Btype*);
235 int64_t
236 type_field_offset(Btype*, size_t index);
238 // Expressions.
240 Bexpression*
241 zero_expression(Btype*);
243 Bexpression*
244 error_expression()
245 { return this->make_expression(error_mark_node); }
247 Bexpression*
248 nil_pointer_expression()
249 { return this->make_expression(null_pointer_node); }
251 Bexpression*
252 var_expression(Bvariable* var, Location);
254 Bexpression*
255 indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location);
257 Bexpression*
258 named_constant_expression(Btype* btype, const std::string& name,
259 Bexpression* val, Location);
261 Bexpression*
262 integer_constant_expression(Btype* btype, mpz_t val);
264 Bexpression*
265 float_constant_expression(Btype* btype, mpfr_t val);
267 Bexpression*
268 complex_constant_expression(Btype* btype, mpc_t val);
270 Bexpression*
271 string_constant_expression(const std::string& val);
273 Bexpression*
274 boolean_constant_expression(bool val);
276 Bexpression*
277 real_part_expression(Bexpression* bcomplex, Location);
279 Bexpression*
280 imag_part_expression(Bexpression* bcomplex, Location);
282 Bexpression*
283 complex_expression(Bexpression* breal, Bexpression* bimag, Location);
285 Bexpression*
286 convert_expression(Btype* type, Bexpression* expr, Location);
288 Bexpression*
289 function_code_expression(Bfunction*, Location);
291 Bexpression*
292 address_expression(Bexpression*, Location);
294 Bexpression*
295 struct_field_expression(Bexpression*, size_t, Location);
297 Bexpression*
298 compound_expression(Bstatement*, Bexpression*, Location);
300 Bexpression*
301 conditional_expression(Btype*, Bexpression*, Bexpression*, Bexpression*,
302 Location);
304 Bexpression*
305 unary_expression(Operator, Bexpression*, Location);
307 Bexpression*
308 binary_expression(Operator, Bexpression*, Bexpression*, Location);
310 Bexpression*
311 constructor_expression(Btype*, const std::vector<Bexpression*>&, Location);
313 Bexpression*
314 array_constructor_expression(Btype*, const std::vector<unsigned long>&,
315 const std::vector<Bexpression*>&, Location);
317 Bexpression*
318 pointer_offset_expression(Bexpression* base, Bexpression* offset, Location);
320 Bexpression*
321 array_index_expression(Bexpression* array, Bexpression* index, Location);
323 Bexpression*
324 call_expression(Bexpression* fn, const std::vector<Bexpression*>& args,
325 Bexpression* static_chain, Location);
327 // Statements.
329 Bstatement*
330 error_statement()
331 { return this->make_statement(error_mark_node); }
333 Bstatement*
334 expression_statement(Bexpression*);
336 Bstatement*
337 init_statement(Bvariable* var, Bexpression* init);
339 Bstatement*
340 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
342 Bstatement*
343 return_statement(Bfunction*, const std::vector<Bexpression*>&,
344 Location);
346 Bstatement*
347 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
348 Location);
350 Bstatement*
351 switch_statement(Bfunction* function, Bexpression* value,
352 const std::vector<std::vector<Bexpression*> >& cases,
353 const std::vector<Bstatement*>& statements,
354 Location);
356 Bstatement*
357 compound_statement(Bstatement*, Bstatement*);
359 Bstatement*
360 statement_list(const std::vector<Bstatement*>&);
362 Bstatement*
363 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
364 Bstatement* finally_stmt, Location);
366 // Blocks.
368 Bblock*
369 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
370 Location, Location);
372 void
373 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
375 Bstatement*
376 block_statement(Bblock*);
378 // Variables.
380 Bvariable*
381 error_variable()
382 { return new Bvariable(error_mark_node); }
384 Bvariable*
385 global_variable(const std::string& package_name,
386 const std::string& pkgpath,
387 const std::string& name,
388 Btype* btype,
389 bool is_external,
390 bool is_hidden,
391 bool in_unique_section,
392 Location location);
394 void
395 global_variable_set_init(Bvariable*, Bexpression*);
397 Bvariable*
398 local_variable(Bfunction*, const std::string&, Btype*, bool,
399 Location);
401 Bvariable*
402 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
403 Location);
405 Bvariable*
406 static_chain_variable(Bfunction*, const std::string&, Btype*, Location);
408 Bvariable*
409 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
410 Location, Bstatement**);
412 Bvariable*
413 implicit_variable(const std::string&, Btype*, bool, bool, bool,
414 int64_t);
416 void
417 implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
418 bool, bool, bool, Bexpression*);
420 Bvariable*
421 implicit_variable_reference(const std::string&, Btype*);
423 Bvariable*
424 immutable_struct(const std::string&, bool, bool, Btype*, Location);
426 void
427 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
428 Location, Bexpression*);
430 Bvariable*
431 immutable_struct_reference(const std::string&, Btype*, Location);
433 // Labels.
435 Blabel*
436 label(Bfunction*, const std::string& name, Location);
438 Bstatement*
439 label_definition_statement(Blabel*);
441 Bstatement*
442 goto_statement(Blabel*, Location);
444 Bexpression*
445 label_address(Blabel*, Location);
447 // Functions.
449 Bfunction*
450 error_function()
451 { return this->make_function(error_mark_node); }
453 Bfunction*
454 function(Btype* fntype, const std::string& name, const std::string& asm_name,
455 bool is_visible, bool is_declaration, bool is_inlinable,
456 bool disable_split_stack, bool in_unique_section, Location);
458 Bstatement*
459 function_defer_statement(Bfunction* function, Bexpression* undefer,
460 Bexpression* defer, Location);
462 bool
463 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
465 bool
466 function_set_body(Bfunction* function, Bstatement* code_stmt);
468 Bfunction*
469 lookup_builtin(const std::string&);
471 void
472 write_global_definitions(const std::vector<Btype*>&,
473 const std::vector<Bexpression*>&,
474 const std::vector<Bfunction*>&,
475 const std::vector<Bvariable*>&);
477 private:
478 // Make a Bexpression from a tree.
479 Bexpression*
480 make_expression(tree t)
481 { return new Bexpression(t); }
483 // Make a Bstatement from a tree.
484 Bstatement*
485 make_statement(tree t)
486 { return new Bstatement(t); }
488 // Make a Btype from a tree.
489 Btype*
490 make_type(tree t)
491 { return new Btype(t); }
493 Bfunction*
494 make_function(tree t)
495 { return new Bfunction(t); }
497 Btype*
498 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
500 Btype*
501 fill_in_array(Btype*, Btype*, Bexpression*);
503 tree
504 non_zero_size_type(tree);
506 private:
507 void
508 define_builtin(built_in_function bcode, const char* name, const char* libname,
509 tree fntype, bool const_p);
511 // A mapping of the GCC built-ins exposed to GCCGo.
512 std::map<std::string, Bfunction*> builtin_functions_;
515 // A helper function.
517 static inline tree
518 get_identifier_from_string(const std::string& str)
520 return get_identifier_with_length(str.data(), str.length());
523 // Define the built-in functions that are exposed to GCCGo.
525 Gcc_backend::Gcc_backend()
527 /* We need to define the fetch_and_add functions, since we use them
528 for ++ and --. */
529 tree t = this->integer_type(BITS_PER_UNIT, 1)->get_tree();
530 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
531 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
532 NULL, build_function_type_list(t, p, t, NULL_TREE),
533 false);
535 t = this->integer_type(BITS_PER_UNIT * 2, 1)->get_tree();
536 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
537 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
538 NULL, build_function_type_list(t, p, t, NULL_TREE),
539 false);
541 t = this->integer_type(BITS_PER_UNIT * 4, 1)->get_tree();
542 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
543 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
544 NULL, build_function_type_list(t, p, t, NULL_TREE),
545 false);
547 t = this->integer_type(BITS_PER_UNIT * 8, 1)->get_tree();
548 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
549 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
550 NULL, build_function_type_list(t, p, t, NULL_TREE),
551 false);
553 // We use __builtin_expect for magic import functions.
554 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
555 build_function_type_list(long_integer_type_node,
556 long_integer_type_node,
557 long_integer_type_node,
558 NULL_TREE),
559 true);
561 // We use __builtin_memcmp for struct comparisons.
562 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
563 build_function_type_list(integer_type_node,
564 const_ptr_type_node,
565 const_ptr_type_node,
566 size_type_node,
567 NULL_TREE),
568 false);
570 // We provide some functions for the math library.
571 tree math_function_type = build_function_type_list(double_type_node,
572 double_type_node,
573 NULL_TREE);
574 tree math_function_type_long =
575 build_function_type_list(long_double_type_node, long_double_type_node,
576 long_double_type_node, NULL_TREE);
577 tree math_function_type_two = build_function_type_list(double_type_node,
578 double_type_node,
579 double_type_node,
580 NULL_TREE);
581 tree math_function_type_long_two =
582 build_function_type_list(long_double_type_node, long_double_type_node,
583 long_double_type_node, NULL_TREE);
584 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
585 math_function_type, true);
586 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
587 math_function_type_long, true);
588 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
589 math_function_type, true);
590 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
591 math_function_type_long, true);
592 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
593 math_function_type, true);
594 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
595 math_function_type_long, true);
596 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
597 math_function_type_two, true);
598 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
599 math_function_type_long_two, true);
600 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
601 math_function_type, true);
602 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
603 math_function_type_long, true);
604 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
605 math_function_type, true);
606 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
607 math_function_type_long, true);
608 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
609 math_function_type, true);
610 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
611 math_function_type_long, true);
612 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
613 math_function_type, true);
614 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
615 math_function_type_long, true);
616 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
617 math_function_type, true);
618 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
619 math_function_type_long, true);
620 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
621 math_function_type, true);
622 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
623 math_function_type_long, true);
624 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
625 math_function_type_two, true);
626 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
627 math_function_type_long_two, true);
628 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
629 build_function_type_list(double_type_node,
630 double_type_node,
631 integer_type_node,
632 NULL_TREE),
633 true);
634 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
635 build_function_type_list(long_double_type_node,
636 long_double_type_node,
637 integer_type_node,
638 NULL_TREE),
639 true);
640 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
641 math_function_type, true);
642 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
643 math_function_type_long, true);
644 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
645 math_function_type, true);
646 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
647 math_function_type_long, true);
648 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
649 math_function_type, true);
650 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
651 math_function_type_long, true);
652 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
653 math_function_type, true);
654 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
655 math_function_type_long, true);
656 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
657 math_function_type, true);
658 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
659 math_function_type_long, true);
660 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
661 math_function_type, true);
662 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
663 math_function_type_long, true);
664 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
665 math_function_type, true);
666 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
667 math_function_type_long, true);
668 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
669 math_function_type, true);
670 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
671 math_function_type_long, true);
673 // We use __builtin_return_address in the thunk we build for
674 // functions which call recover.
675 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
676 NULL,
677 build_function_type_list(ptr_type_node,
678 unsigned_type_node,
679 NULL_TREE),
680 false);
682 // The compiler uses __builtin_trap for some exception handling
683 // cases.
684 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
685 build_function_type(void_type_node, void_list_node),
686 false);
689 // Get an unnamed integer type.
691 Btype*
692 Gcc_backend::integer_type(bool is_unsigned, int bits)
694 tree type;
695 if (is_unsigned)
697 if (bits == INT_TYPE_SIZE)
698 type = unsigned_type_node;
699 else if (bits == CHAR_TYPE_SIZE)
700 type = unsigned_char_type_node;
701 else if (bits == SHORT_TYPE_SIZE)
702 type = short_unsigned_type_node;
703 else if (bits == LONG_TYPE_SIZE)
704 type = long_unsigned_type_node;
705 else if (bits == LONG_LONG_TYPE_SIZE)
706 type = long_long_unsigned_type_node;
707 else
708 type = make_unsigned_type(bits);
710 else
712 if (bits == INT_TYPE_SIZE)
713 type = integer_type_node;
714 else if (bits == CHAR_TYPE_SIZE)
715 type = signed_char_type_node;
716 else if (bits == SHORT_TYPE_SIZE)
717 type = short_integer_type_node;
718 else if (bits == LONG_TYPE_SIZE)
719 type = long_integer_type_node;
720 else if (bits == LONG_LONG_TYPE_SIZE)
721 type = long_long_integer_type_node;
722 else
723 type = make_signed_type(bits);
725 return this->make_type(type);
728 // Get an unnamed float type.
730 Btype*
731 Gcc_backend::float_type(int bits)
733 tree type;
734 if (bits == FLOAT_TYPE_SIZE)
735 type = float_type_node;
736 else if (bits == DOUBLE_TYPE_SIZE)
737 type = double_type_node;
738 else if (bits == LONG_DOUBLE_TYPE_SIZE)
739 type = long_double_type_node;
740 else
742 type = make_node(REAL_TYPE);
743 TYPE_PRECISION(type) = bits;
744 layout_type(type);
746 return this->make_type(type);
749 // Get an unnamed complex type.
751 Btype*
752 Gcc_backend::complex_type(int bits)
754 tree type;
755 if (bits == FLOAT_TYPE_SIZE * 2)
756 type = complex_float_type_node;
757 else if (bits == DOUBLE_TYPE_SIZE * 2)
758 type = complex_double_type_node;
759 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
760 type = complex_long_double_type_node;
761 else
763 type = make_node(REAL_TYPE);
764 TYPE_PRECISION(type) = bits / 2;
765 layout_type(type);
766 type = build_complex_type(type);
768 return this->make_type(type);
771 // Get a pointer type.
773 Btype*
774 Gcc_backend::pointer_type(Btype* to_type)
776 tree to_type_tree = to_type->get_tree();
777 if (to_type_tree == error_mark_node)
778 return this->error_type();
779 tree type = build_pointer_type(to_type_tree);
780 return this->make_type(type);
783 // Make a function type.
785 Btype*
786 Gcc_backend::function_type(const Btyped_identifier& receiver,
787 const std::vector<Btyped_identifier>& parameters,
788 const std::vector<Btyped_identifier>& results,
789 Btype* result_struct,
790 Location)
792 tree args = NULL_TREE;
793 tree* pp = &args;
794 if (receiver.btype != NULL)
796 tree t = receiver.btype->get_tree();
797 if (t == error_mark_node)
798 return this->error_type();
799 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
800 pp = &TREE_CHAIN(*pp);
803 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
804 p != parameters.end();
805 ++p)
807 tree t = p->btype->get_tree();
808 if (t == error_mark_node)
809 return this->error_type();
810 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
811 pp = &TREE_CHAIN(*pp);
814 // Varargs is handled entirely at the Go level. When converted to
815 // GENERIC functions are not varargs.
816 *pp = void_list_node;
818 tree result;
819 if (results.empty())
820 result = void_type_node;
821 else if (results.size() == 1)
822 result = results.front().btype->get_tree();
823 else
825 gcc_assert(result_struct != NULL);
826 result = result_struct->get_tree();
828 if (result == error_mark_node)
829 return this->error_type();
831 tree fntype = build_function_type(result, args);
832 if (fntype == error_mark_node)
833 return this->error_type();
835 return this->make_type(build_pointer_type(fntype));
838 // Make a struct type.
840 Btype*
841 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
843 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
846 // Fill in the fields of a struct type.
848 Btype*
849 Gcc_backend::fill_in_struct(Btype* fill,
850 const std::vector<Btyped_identifier>& fields)
852 tree fill_tree = fill->get_tree();
853 tree field_trees = NULL_TREE;
854 tree* pp = &field_trees;
855 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
856 p != fields.end();
857 ++p)
859 tree name_tree = get_identifier_from_string(p->name);
860 tree type_tree = p->btype->get_tree();
861 if (type_tree == error_mark_node)
862 return this->error_type();
863 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
864 type_tree);
865 DECL_CONTEXT(field) = fill_tree;
866 *pp = field;
867 pp = &DECL_CHAIN(field);
869 TYPE_FIELDS(fill_tree) = field_trees;
870 layout_type(fill_tree);
871 return fill;
874 // Make an array type.
876 Btype*
877 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
879 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
880 element_btype, length);
883 // Fill in an array type.
885 Btype*
886 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
887 Bexpression* length)
889 tree element_type_tree = element_type->get_tree();
890 tree length_tree = length->get_tree();
891 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
892 return this->error_type();
894 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
896 length_tree = fold_convert(sizetype, length_tree);
898 // build_index_type takes the maximum index, which is one less than
899 // the length.
900 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
901 length_tree,
902 size_one_node));
904 tree fill_tree = fill->get_tree();
905 TREE_TYPE(fill_tree) = element_type_tree;
906 TYPE_DOMAIN(fill_tree) = index_type_tree;
907 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
908 layout_type(fill_tree);
910 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
911 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
912 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
913 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
914 TYPE_CANONICAL(fill_tree) =
915 build_array_type(TYPE_CANONICAL(element_type_tree),
916 TYPE_CANONICAL(index_type_tree));
918 return fill;
921 // Create a placeholder for a pointer type.
923 Btype*
924 Gcc_backend::placeholder_pointer_type(const std::string& name,
925 Location location, bool)
927 tree ret = build_distinct_type_copy(ptr_type_node);
928 if (!name.empty())
930 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
931 get_identifier_from_string(name),
932 ret);
933 TYPE_NAME(ret) = decl;
935 return this->make_type(ret);
938 // Set the real target type for a placeholder pointer type.
940 bool
941 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
942 Btype* to_type)
944 tree pt = placeholder->get_tree();
945 if (pt == error_mark_node)
946 return false;
947 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
948 tree tt = to_type->get_tree();
949 if (tt == error_mark_node)
951 placeholder->set_tree(error_mark_node);
952 return false;
954 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
955 TREE_TYPE(pt) = TREE_TYPE(tt);
956 if (TYPE_NAME(pt) != NULL_TREE)
958 // Build the data structure gcc wants to see for a typedef.
959 tree copy = build_variant_type_copy(pt);
960 TYPE_NAME(copy) = NULL_TREE;
961 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
963 return true;
966 // Set the real values for a placeholder function type.
968 bool
969 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
971 return this->set_placeholder_pointer_type(placeholder, ft);
974 // Create a placeholder for a struct type.
976 Btype*
977 Gcc_backend::placeholder_struct_type(const std::string& name,
978 Location location)
980 tree ret = make_node(RECORD_TYPE);
981 if (!name.empty())
983 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
984 get_identifier_from_string(name),
985 ret);
986 TYPE_NAME(ret) = decl;
988 return this->make_type(ret);
991 // Fill in the fields of a placeholder struct type.
993 bool
994 Gcc_backend::set_placeholder_struct_type(
995 Btype* placeholder,
996 const std::vector<Btyped_identifier>& fields)
998 tree t = placeholder->get_tree();
999 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
1000 Btype* r = this->fill_in_struct(placeholder, fields);
1002 if (TYPE_NAME(t) != NULL_TREE)
1004 // Build the data structure gcc wants to see for a typedef.
1005 tree copy = build_distinct_type_copy(t);
1006 TYPE_NAME(copy) = NULL_TREE;
1007 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1010 return r->get_tree() != error_mark_node;
1013 // Create a placeholder for an array type.
1015 Btype*
1016 Gcc_backend::placeholder_array_type(const std::string& name,
1017 Location location)
1019 tree ret = make_node(ARRAY_TYPE);
1020 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1021 get_identifier_from_string(name),
1022 ret);
1023 TYPE_NAME(ret) = decl;
1024 return this->make_type(ret);
1027 // Fill in the fields of a placeholder array type.
1029 bool
1030 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1031 Btype* element_btype,
1032 Bexpression* length)
1034 tree t = placeholder->get_tree();
1035 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1036 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1038 // Build the data structure gcc wants to see for a typedef.
1039 tree copy = build_distinct_type_copy(t);
1040 TYPE_NAME(copy) = NULL_TREE;
1041 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1043 return r->get_tree() != error_mark_node;
1046 // Return a named version of a type.
1048 Btype*
1049 Gcc_backend::named_type(const std::string& name, Btype* btype,
1050 Location location)
1052 tree type = btype->get_tree();
1053 if (type == error_mark_node)
1054 return this->error_type();
1056 // The middle-end expects a basic type to have a name. In Go every
1057 // basic type will have a name. The first time we see a basic type,
1058 // give it whatever Go name we have at this point.
1059 if (TYPE_NAME(type) == NULL_TREE
1060 && location.gcc_location() == BUILTINS_LOCATION
1061 && (TREE_CODE(type) == INTEGER_TYPE
1062 || TREE_CODE(type) == REAL_TYPE
1063 || TREE_CODE(type) == COMPLEX_TYPE
1064 || TREE_CODE(type) == BOOLEAN_TYPE))
1066 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1067 get_identifier_from_string(name),
1068 type);
1069 TYPE_NAME(type) = decl;
1070 return this->make_type(type);
1073 tree copy = build_variant_type_copy(type);
1074 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1075 get_identifier_from_string(name),
1076 copy);
1077 DECL_ORIGINAL_TYPE(decl) = type;
1078 TYPE_NAME(copy) = decl;
1079 return this->make_type(copy);
1082 // Return a pointer type used as a marker for a circular type.
1084 Btype*
1085 Gcc_backend::circular_pointer_type(Btype*, bool)
1087 return this->make_type(ptr_type_node);
1090 // Return whether we might be looking at a circular type.
1092 bool
1093 Gcc_backend::is_circular_pointer_type(Btype* btype)
1095 return btype->get_tree() == ptr_type_node;
1098 // Return the size of a type.
1100 int64_t
1101 Gcc_backend::type_size(Btype* btype)
1103 tree t = btype->get_tree();
1104 if (t == error_mark_node)
1105 return 1;
1106 t = TYPE_SIZE_UNIT(t);
1107 gcc_assert(tree_fits_uhwi_p (t));
1108 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1109 int64_t ret = static_cast<int64_t>(val_wide);
1110 gcc_assert(ret >= 0 && static_cast<unsigned HOST_WIDE_INT>(ret) == val_wide);
1111 return ret;
1114 // Return the alignment of a type.
1116 int64_t
1117 Gcc_backend::type_alignment(Btype* btype)
1119 tree t = btype->get_tree();
1120 if (t == error_mark_node)
1121 return 1;
1122 return TYPE_ALIGN_UNIT(t);
1125 // Return the alignment of a struct field of type BTYPE.
1127 int64_t
1128 Gcc_backend::type_field_alignment(Btype* btype)
1130 tree t = btype->get_tree();
1131 if (t == error_mark_node)
1132 return 1;
1133 return go_field_alignment(t);
1136 // Return the offset of a field in a struct.
1138 int64_t
1139 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1141 tree struct_tree = btype->get_tree();
1142 if (struct_tree == error_mark_node)
1143 return 0;
1144 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1145 tree field = TYPE_FIELDS(struct_tree);
1146 for (; index > 0; --index)
1148 field = DECL_CHAIN(field);
1149 gcc_assert(field != NULL_TREE);
1151 HOST_WIDE_INT offset_wide = int_byte_position(field);
1152 int64_t ret = static_cast<int64_t>(offset_wide);
1153 gcc_assert(ret == offset_wide);
1154 return ret;
1157 // Return the zero value for a type.
1159 Bexpression*
1160 Gcc_backend::zero_expression(Btype* btype)
1162 tree t = btype->get_tree();
1163 tree ret;
1164 if (t == error_mark_node)
1165 ret = error_mark_node;
1166 else
1167 ret = build_zero_cst(t);
1168 return this->make_expression(ret);
1171 // An expression that references a variable.
1173 Bexpression*
1174 Gcc_backend::var_expression(Bvariable* var, Location)
1176 tree ret = var->get_tree();
1177 if (ret == error_mark_node)
1178 return this->error_expression();
1179 return this->make_expression(ret);
1182 // An expression that indirectly references an expression.
1184 Bexpression*
1185 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1186 bool known_valid, Location location)
1188 tree expr_tree = expr->get_tree();
1189 tree type_tree = btype->get_tree();
1190 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1191 return this->error_expression();
1193 // If the type of EXPR is a recursive pointer type, then we
1194 // need to insert a cast before indirecting.
1195 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1196 if (VOID_TYPE_P(target_type_tree))
1197 expr_tree = fold_convert_loc(location.gcc_location(),
1198 build_pointer_type(type_tree), expr_tree);
1200 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1201 expr_tree);
1202 if (known_valid)
1203 TREE_THIS_NOTRAP(ret) = 1;
1204 return this->make_expression(ret);
1207 // Return an expression that declares a constant named NAME with the
1208 // constant value VAL in BTYPE.
1210 Bexpression*
1211 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1212 Bexpression* val, Location location)
1214 tree type_tree = btype->get_tree();
1215 tree const_val = val->get_tree();
1216 if (type_tree == error_mark_node || const_val == error_mark_node)
1217 return this->error_expression();
1219 tree name_tree = get_identifier_from_string(name);
1220 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1221 type_tree);
1222 DECL_INITIAL(decl) = const_val;
1223 TREE_CONSTANT(decl) = 1;
1224 TREE_READONLY(decl) = 1;
1226 go_preserve_from_gc(decl);
1227 return this->make_expression(decl);
1230 // Return a typed value as a constant integer.
1232 Bexpression*
1233 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1235 tree t = btype->get_tree();
1236 if (t == error_mark_node)
1237 return this->error_expression();
1239 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1240 return this->make_expression(ret);
1243 // Return a typed value as a constant floating-point number.
1245 Bexpression*
1246 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1248 tree t = btype->get_tree();
1249 tree ret;
1250 if (t == error_mark_node)
1251 return this->error_expression();
1253 REAL_VALUE_TYPE r1;
1254 real_from_mpfr(&r1, val, t, GMP_RNDN);
1255 REAL_VALUE_TYPE r2;
1256 real_convert(&r2, TYPE_MODE(t), &r1);
1257 ret = build_real(t, r2);
1258 return this->make_expression(ret);
1261 // Return a typed real and imaginary value as a constant complex number.
1263 Bexpression*
1264 Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
1266 tree t = btype->get_tree();
1267 tree ret;
1268 if (t == error_mark_node)
1269 return this->error_expression();
1271 REAL_VALUE_TYPE r1;
1272 real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
1273 REAL_VALUE_TYPE r2;
1274 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1276 REAL_VALUE_TYPE r3;
1277 real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
1278 REAL_VALUE_TYPE r4;
1279 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1281 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1282 build_real(TREE_TYPE(t), r4));
1283 return this->make_expression(ret);
1286 // Make a constant string expression.
1288 Bexpression*
1289 Gcc_backend::string_constant_expression(const std::string& val)
1291 tree index_type = build_index_type(size_int(val.length()));
1292 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1293 TYPE_QUAL_CONST);
1294 tree string_type = build_array_type(const_char_type, index_type);
1295 string_type = build_variant_type_copy(string_type);
1296 TYPE_STRING_FLAG(string_type) = 1;
1297 tree string_val = build_string(val.length(), val.data());
1298 TREE_TYPE(string_val) = string_type;
1300 return this->make_expression(string_val);
1303 // Make a constant boolean expression.
1305 Bexpression*
1306 Gcc_backend::boolean_constant_expression(bool val)
1308 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1309 return this->make_expression(bool_cst);
1312 // Return the real part of a complex expression.
1314 Bexpression*
1315 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1317 tree complex_tree = bcomplex->get_tree();
1318 if (complex_tree == error_mark_node)
1319 return this->error_expression();
1320 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1321 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1322 TREE_TYPE(TREE_TYPE(complex_tree)),
1323 complex_tree);
1324 return this->make_expression(ret);
1327 // Return the imaginary part of a complex expression.
1329 Bexpression*
1330 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1332 tree complex_tree = bcomplex->get_tree();
1333 if (complex_tree == error_mark_node)
1334 return this->error_expression();
1335 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1336 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1337 TREE_TYPE(TREE_TYPE(complex_tree)),
1338 complex_tree);
1339 return this->make_expression(ret);
1342 // Make a complex expression given its real and imaginary parts.
1344 Bexpression*
1345 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1346 Location location)
1348 tree real_tree = breal->get_tree();
1349 tree imag_tree = bimag->get_tree();
1350 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1351 return this->error_expression();
1352 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1353 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1354 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1355 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1356 build_complex_type(TREE_TYPE(real_tree)),
1357 real_tree, imag_tree);
1358 return this->make_expression(ret);
1361 // An expression that converts an expression to a different type.
1363 Bexpression*
1364 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1365 Location location)
1367 tree type_tree = type->get_tree();
1368 tree expr_tree = expr->get_tree();
1369 if (type_tree == error_mark_node
1370 || expr_tree == error_mark_node
1371 || TREE_TYPE(expr_tree) == error_mark_node)
1372 return this->error_expression();
1374 tree ret;
1375 if (this->type_size(type) == 0)
1377 // Do not convert zero-sized types.
1378 ret = expr_tree;
1380 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1381 ret = fold(convert_to_integer(type_tree, expr_tree));
1382 else if (TREE_CODE(type_tree) == REAL_TYPE)
1383 ret = fold(convert_to_real(type_tree, expr_tree));
1384 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1385 ret = fold(convert_to_complex(type_tree, expr_tree));
1386 else if (TREE_CODE(type_tree) == POINTER_TYPE
1387 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1388 ret = fold(convert_to_pointer(type_tree, expr_tree));
1389 else if (TREE_CODE(type_tree) == RECORD_TYPE
1390 || TREE_CODE(type_tree) == ARRAY_TYPE)
1391 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1392 type_tree, expr_tree);
1393 else
1394 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1396 return this->make_expression(ret);
1399 // Get the address of a function.
1401 Bexpression*
1402 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1404 tree func = bfunc->get_tree();
1405 if (func == error_mark_node)
1406 return this->error_expression();
1408 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1409 return this->make_expression(ret);
1412 // Get the address of an expression.
1414 Bexpression*
1415 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1417 tree expr = bexpr->get_tree();
1418 if (expr == error_mark_node)
1419 return this->error_expression();
1421 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1422 return this->make_expression(ret);
1425 // Return an expression for the field at INDEX in BSTRUCT.
1427 Bexpression*
1428 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1429 Location location)
1431 tree struct_tree = bstruct->get_tree();
1432 if (struct_tree == error_mark_node
1433 || TREE_TYPE(struct_tree) == error_mark_node)
1434 return this->error_expression();
1435 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1436 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1437 if (field == NULL_TREE)
1439 // This can happen for a type which refers to itself indirectly
1440 // and then turns out to be erroneous.
1441 return this->error_expression();
1443 for (unsigned int i = index; i > 0; --i)
1445 field = DECL_CHAIN(field);
1446 gcc_assert(field != NULL_TREE);
1448 if (TREE_TYPE(field) == error_mark_node)
1449 return this->error_expression();
1450 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1451 TREE_TYPE(field), struct_tree, field,
1452 NULL_TREE);
1453 if (TREE_CONSTANT(struct_tree))
1454 TREE_CONSTANT(ret) = 1;
1455 return this->make_expression(ret);
1458 // Return an expression that executes BSTAT before BEXPR.
1460 Bexpression*
1461 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1462 Location location)
1464 tree stat = bstat->get_tree();
1465 tree expr = bexpr->get_tree();
1466 if (stat == error_mark_node || expr == error_mark_node)
1467 return this->error_expression();
1468 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1469 TREE_TYPE(expr), stat, expr);
1470 return this->make_expression(ret);
1473 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1474 // ELSE_EXPR otherwise.
1476 Bexpression*
1477 Gcc_backend::conditional_expression(Btype* btype, Bexpression* condition,
1478 Bexpression* then_expr,
1479 Bexpression* else_expr, Location location)
1481 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1482 tree cond_tree = condition->get_tree();
1483 tree then_tree = then_expr->get_tree();
1484 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1485 if (type_tree == error_mark_node
1486 || cond_tree == error_mark_node
1487 || then_tree == error_mark_node
1488 || else_tree == error_mark_node)
1489 return this->error_expression();
1490 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1491 cond_tree, then_tree, else_tree);
1492 return this->make_expression(ret);
1495 // Return an expression for the unary operation OP EXPR.
1497 Bexpression*
1498 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1500 tree expr_tree = expr->get_tree();
1501 if (expr_tree == error_mark_node
1502 || TREE_TYPE(expr_tree) == error_mark_node)
1503 return this->error_expression();
1505 tree type_tree = TREE_TYPE(expr_tree);
1506 enum tree_code code;
1507 switch (op)
1509 case OPERATOR_MINUS:
1511 tree computed_type = excess_precision_type(type_tree);
1512 if (computed_type != NULL_TREE)
1514 expr_tree = convert(computed_type, expr_tree);
1515 type_tree = computed_type;
1517 code = NEGATE_EXPR;
1518 break;
1520 case OPERATOR_NOT:
1521 code = TRUTH_NOT_EXPR;
1522 break;
1523 case OPERATOR_XOR:
1524 code = BIT_NOT_EXPR;
1525 break;
1526 default:
1527 gcc_unreachable();
1528 break;
1531 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1532 expr_tree);
1533 return this->make_expression(ret);
1536 // Convert a gofrontend operator to an equivalent tree_code.
1538 static enum tree_code
1539 operator_to_tree_code(Operator op, tree type)
1541 enum tree_code code;
1542 switch (op)
1544 case OPERATOR_EQEQ:
1545 code = EQ_EXPR;
1546 break;
1547 case OPERATOR_NOTEQ:
1548 code = NE_EXPR;
1549 break;
1550 case OPERATOR_LT:
1551 code = LT_EXPR;
1552 break;
1553 case OPERATOR_LE:
1554 code = LE_EXPR;
1555 break;
1556 case OPERATOR_GT:
1557 code = GT_EXPR;
1558 break;
1559 case OPERATOR_GE:
1560 code = GE_EXPR;
1561 break;
1562 case OPERATOR_OROR:
1563 code = TRUTH_ORIF_EXPR;
1564 break;
1565 case OPERATOR_ANDAND:
1566 code = TRUTH_ANDIF_EXPR;
1567 break;
1568 case OPERATOR_PLUS:
1569 code = PLUS_EXPR;
1570 break;
1571 case OPERATOR_MINUS:
1572 code = MINUS_EXPR;
1573 break;
1574 case OPERATOR_OR:
1575 code = BIT_IOR_EXPR;
1576 break;
1577 case OPERATOR_XOR:
1578 code = BIT_XOR_EXPR;
1579 break;
1580 case OPERATOR_MULT:
1581 code = MULT_EXPR;
1582 break;
1583 case OPERATOR_DIV:
1584 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1585 code = RDIV_EXPR;
1586 else
1587 code = TRUNC_DIV_EXPR;
1588 break;
1589 case OPERATOR_MOD:
1590 code = TRUNC_MOD_EXPR;
1591 break;
1592 case OPERATOR_LSHIFT:
1593 code = LSHIFT_EXPR;
1594 break;
1595 case OPERATOR_RSHIFT:
1596 code = RSHIFT_EXPR;
1597 break;
1598 case OPERATOR_AND:
1599 code = BIT_AND_EXPR;
1600 break;
1601 case OPERATOR_BITCLEAR:
1602 code = BIT_AND_EXPR;
1603 break;
1604 default:
1605 gcc_unreachable();
1608 return code;
1611 // Return an expression for the binary operation LEFT OP RIGHT.
1613 Bexpression*
1614 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1615 Bexpression* right, Location location)
1617 tree left_tree = left->get_tree();
1618 tree right_tree = right->get_tree();
1619 if (left_tree == error_mark_node
1620 || right_tree == error_mark_node)
1621 return this->error_expression();
1622 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1624 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1625 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1626 tree computed_type = excess_precision_type(type_tree);
1627 if (computed_type != NULL_TREE)
1629 left_tree = convert(computed_type, left_tree);
1630 right_tree = convert(computed_type, right_tree);
1631 type_tree = computed_type;
1634 // For comparison operators, the resulting type should be boolean.
1635 switch (op)
1637 case OPERATOR_EQEQ:
1638 case OPERATOR_NOTEQ:
1639 case OPERATOR_LT:
1640 case OPERATOR_LE:
1641 case OPERATOR_GT:
1642 case OPERATOR_GE:
1643 type_tree = boolean_type_node;
1644 break;
1645 default:
1646 break;
1649 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1650 left_tree, right_tree);
1651 return this->make_expression(ret);
1654 // Return an expression that constructs BTYPE with VALS.
1656 Bexpression*
1657 Gcc_backend::constructor_expression(Btype* btype,
1658 const std::vector<Bexpression*>& vals,
1659 Location location)
1661 tree type_tree = btype->get_tree();
1662 if (type_tree == error_mark_node)
1663 return this->error_expression();
1665 vec<constructor_elt, va_gc> *init;
1666 vec_alloc(init, vals.size());
1668 tree sink = NULL_TREE;
1669 bool is_constant = true;
1670 tree field = TYPE_FIELDS(type_tree);
1671 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1672 p != vals.end();
1673 ++p, field = DECL_CHAIN(field))
1675 gcc_assert(field != NULL_TREE);
1676 tree val = (*p)->get_tree();
1677 if (TREE_TYPE(field) == error_mark_node
1678 || val == error_mark_node
1679 || TREE_TYPE(val) == error_mark_node)
1680 return this->error_expression();
1682 if (int_size_in_bytes(TREE_TYPE(field)) == 0)
1684 // GIMPLE cannot represent indices of zero-sized types so
1685 // trying to construct a map with zero-sized keys might lead
1686 // to errors. Instead, we evaluate each expression that
1687 // would have been added as a map element for its
1688 // side-effects and construct an empty map.
1689 append_to_statement_list(val, &sink);
1690 continue;
1693 constructor_elt empty = {NULL, NULL};
1694 constructor_elt* elt = init->quick_push(empty);
1695 elt->index = field;
1696 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
1697 val);
1698 if (!TREE_CONSTANT(elt->value))
1699 is_constant = false;
1701 gcc_assert(field == NULL_TREE);
1702 tree ret = build_constructor(type_tree, init);
1703 if (is_constant)
1704 TREE_CONSTANT(ret) = 1;
1705 if (sink != NULL_TREE)
1706 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1707 type_tree, sink, ret);
1708 return this->make_expression(ret);
1711 Bexpression*
1712 Gcc_backend::array_constructor_expression(
1713 Btype* array_btype, const std::vector<unsigned long>& indexes,
1714 const std::vector<Bexpression*>& vals, Location location)
1716 tree type_tree = array_btype->get_tree();
1717 if (type_tree == error_mark_node)
1718 return this->error_expression();
1720 gcc_assert(indexes.size() == vals.size());
1722 tree element_type = TREE_TYPE(type_tree);
1723 HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
1724 vec<constructor_elt, va_gc> *init;
1725 vec_alloc(init, element_size == 0 ? 0 : vals.size());
1727 tree sink = NULL_TREE;
1728 bool is_constant = true;
1729 for (size_t i = 0; i < vals.size(); ++i)
1731 tree index = size_int(indexes[i]);
1732 tree val = (vals[i])->get_tree();
1734 if (index == error_mark_node
1735 || val == error_mark_node)
1736 return this->error_expression();
1738 if (element_size == 0)
1740 // GIMPLE cannot represent arrays of zero-sized types so trying
1741 // to construct an array of zero-sized values might lead to errors.
1742 // Instead, we evaluate each expression that would have been added as
1743 // an array value for its side-effects and construct an empty array.
1744 append_to_statement_list(val, &sink);
1745 continue;
1748 if (!TREE_CONSTANT(val))
1749 is_constant = false;
1751 constructor_elt empty = {NULL, NULL};
1752 constructor_elt* elt = init->quick_push(empty);
1753 elt->index = index;
1754 elt->value = val;
1757 tree ret = build_constructor(type_tree, init);
1758 if (is_constant)
1759 TREE_CONSTANT(ret) = 1;
1760 if (sink != NULL_TREE)
1761 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1762 type_tree, sink, ret);
1763 return this->make_expression(ret);
1766 // Return an expression for the address of BASE[INDEX].
1768 Bexpression*
1769 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1770 Location location)
1772 tree base_tree = base->get_tree();
1773 tree index_tree = index->get_tree();
1774 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1775 if (base_tree == error_mark_node
1776 || TREE_TYPE(base_tree) == error_mark_node
1777 || index_tree == error_mark_node
1778 || element_type_tree == error_mark_node)
1779 return this->error_expression();
1781 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1782 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1783 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1784 index_tree, element_size);
1785 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1786 TREE_TYPE(base_tree), base_tree, offset);
1787 return this->make_expression(ptr);
1790 // Return an expression representing ARRAY[INDEX]
1792 Bexpression*
1793 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1794 Location location)
1796 tree array_tree = array->get_tree();
1797 tree index_tree = index->get_tree();
1798 if (array_tree == error_mark_node
1799 || TREE_TYPE(array_tree) == error_mark_node
1800 || index_tree == error_mark_node)
1801 return this->error_expression();
1803 tree ret = build4_loc(location.gcc_location(), ARRAY_REF,
1804 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1805 index_tree, NULL_TREE, NULL_TREE);
1806 return this->make_expression(ret);
1809 // Create an expression for a call to FN_EXPR with FN_ARGS.
1810 Bexpression*
1811 Gcc_backend::call_expression(Bexpression* fn_expr,
1812 const std::vector<Bexpression*>& fn_args,
1813 Bexpression* chain_expr, Location location)
1815 tree fn = fn_expr->get_tree();
1816 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1817 return this->error_expression();
1819 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1820 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1822 size_t nargs = fn_args.size();
1823 tree* args = nargs == 0 ? NULL : new tree[nargs];
1824 for (size_t i = 0; i < nargs; ++i)
1826 args[i] = fn_args.at(i)->get_tree();
1827 if (args[i] == error_mark_node)
1828 return this->error_expression();
1831 tree fndecl = fn;
1832 if (TREE_CODE(fndecl) == ADDR_EXPR)
1833 fndecl = TREE_OPERAND(fndecl, 0);
1835 // This is to support builtin math functions when using 80387 math.
1836 tree excess_type = NULL_TREE;
1837 if (optimize
1838 && TREE_CODE(fndecl) == FUNCTION_DECL
1839 && DECL_IS_BUILTIN(fndecl)
1840 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
1841 && nargs > 0
1842 && ((SCALAR_FLOAT_TYPE_P(rettype)
1843 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1844 || (COMPLEX_FLOAT_TYPE_P(rettype)
1845 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1847 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1848 if (excess_type != NULL_TREE)
1850 tree excess_fndecl = mathfn_built_in(excess_type,
1851 DECL_FUNCTION_CODE(fndecl));
1852 if (excess_fndecl == NULL_TREE)
1853 excess_type = NULL_TREE;
1854 else
1856 fn = build_fold_addr_expr_loc(location.gcc_location(),
1857 excess_fndecl);
1858 for (size_t i = 0; i < nargs; ++i)
1860 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1861 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1862 args[i] = ::convert(excess_type, args[i]);
1868 tree ret =
1869 build_call_array_loc(location.gcc_location(),
1870 excess_type != NULL_TREE ? excess_type : rettype,
1871 fn, nargs, args);
1873 if (chain_expr)
1874 CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree();
1876 if (excess_type != NULL_TREE)
1878 // Calling convert here can undo our excess precision change.
1879 // That may or may not be a bug in convert_to_real.
1880 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1883 delete[] args;
1884 return this->make_expression(ret);
1887 // An expression as a statement.
1889 Bstatement*
1890 Gcc_backend::expression_statement(Bexpression* expr)
1892 return this->make_statement(expr->get_tree());
1895 // Variable initialization.
1897 Bstatement*
1898 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1900 tree var_tree = var->get_tree();
1901 tree init_tree = init->get_tree();
1902 if (var_tree == error_mark_node || init_tree == error_mark_node)
1903 return this->error_statement();
1904 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1906 // To avoid problems with GNU ld, we don't make zero-sized
1907 // externally visible variables. That might lead us to doing an
1908 // initialization of a zero-sized expression to a non-zero sized
1909 // variable, or vice-versa. Avoid crashes by omitting the
1910 // initializer. Such initializations don't mean anything anyhow.
1911 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1912 && init_tree != NULL_TREE
1913 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1915 DECL_INITIAL(var_tree) = init_tree;
1916 init_tree = NULL_TREE;
1919 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1920 void_type_node, var_tree);
1921 if (init_tree != NULL_TREE)
1922 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1923 void_type_node, init_tree, ret);
1925 return this->make_statement(ret);
1928 // Assignment.
1930 Bstatement*
1931 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1932 Location location)
1934 tree lhs_tree = lhs->get_tree();
1935 tree rhs_tree = rhs->get_tree();
1936 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1937 return this->error_statement();
1939 // To avoid problems with GNU ld, we don't make zero-sized
1940 // externally visible variables. That might lead us to doing an
1941 // assignment of a zero-sized expression to a non-zero sized
1942 // expression; avoid crashes here by avoiding assignments of
1943 // zero-sized expressions. Such assignments don't really mean
1944 // anything anyhow.
1945 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1946 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1947 return this->compound_statement(this->expression_statement(lhs),
1948 this->expression_statement(rhs));
1950 // Sometimes the same unnamed Go type can be created multiple times
1951 // and thus have multiple tree representations. Make sure this does
1952 // not confuse the middle-end.
1953 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1955 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1956 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1957 if (POINTER_TYPE_P(lhs_type_tree)
1958 || INTEGRAL_TYPE_P(lhs_type_tree)
1959 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1960 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1961 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1962 rhs_tree);
1963 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1964 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1966 gcc_assert(int_size_in_bytes(lhs_type_tree)
1967 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1968 rhs_tree = fold_build1_loc(location.gcc_location(),
1969 VIEW_CONVERT_EXPR,
1970 lhs_type_tree, rhs_tree);
1974 return this->make_statement(fold_build2_loc(location.gcc_location(),
1975 MODIFY_EXPR,
1976 void_type_node,
1977 lhs_tree, rhs_tree));
1980 // Return.
1982 Bstatement*
1983 Gcc_backend::return_statement(Bfunction* bfunction,
1984 const std::vector<Bexpression*>& vals,
1985 Location location)
1987 tree fntree = bfunction->get_tree();
1988 if (fntree == error_mark_node)
1989 return this->error_statement();
1990 tree result = DECL_RESULT(fntree);
1991 if (result == error_mark_node)
1992 return this->error_statement();
1994 tree ret;
1995 if (vals.empty())
1996 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
1997 NULL_TREE);
1998 else if (vals.size() == 1)
2000 tree val = vals.front()->get_tree();
2001 if (val == error_mark_node)
2002 return this->error_statement();
2003 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2004 void_type_node, result,
2005 vals.front()->get_tree());
2006 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2007 void_type_node, set);
2009 else
2011 // To return multiple values, copy the values into a temporary
2012 // variable of the right structure type, and then assign the
2013 // temporary variable to the DECL_RESULT in the return
2014 // statement.
2015 tree stmt_list = NULL_TREE;
2016 tree rettype = TREE_TYPE(result);
2018 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2019 push_struct_function(fntree);
2020 else
2021 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2022 tree rettmp = create_tmp_var(rettype, "RESULT");
2023 pop_cfun();
2025 tree field = TYPE_FIELDS(rettype);
2026 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2027 p != vals.end();
2028 p++, field = DECL_CHAIN(field))
2030 gcc_assert(field != NULL_TREE);
2031 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
2032 TREE_TYPE(field), rettmp, field,
2033 NULL_TREE);
2034 tree val = (*p)->get_tree();
2035 if (val == error_mark_node)
2036 return this->error_statement();
2037 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2038 void_type_node,
2039 ref, (*p)->get_tree());
2040 append_to_statement_list(set, &stmt_list);
2042 gcc_assert(field == NULL_TREE);
2043 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2044 void_type_node,
2045 result, rettmp);
2046 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2047 void_type_node, set);
2048 append_to_statement_list(ret_expr, &stmt_list);
2049 ret = stmt_list;
2051 return this->make_statement(ret);
2054 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2055 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2056 // NULL, it will always be executed. This is used for handling defers in Go
2057 // functions. In C++, the resulting code is of this form:
2058 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2060 Bstatement*
2061 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2062 Bstatement* except_stmt,
2063 Bstatement* finally_stmt,
2064 Location location)
2066 tree stat_tree = bstat->get_tree();
2067 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2068 tree finally_tree = finally_stmt == NULL
2069 ? NULL_TREE
2070 : finally_stmt->get_tree();
2072 if (stat_tree == error_mark_node
2073 || except_tree == error_mark_node
2074 || finally_tree == error_mark_node)
2075 return this->error_statement();
2077 if (except_tree != NULL_TREE)
2078 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2079 void_type_node, stat_tree,
2080 build2_loc(location.gcc_location(), CATCH_EXPR,
2081 void_type_node, NULL, except_tree));
2082 if (finally_tree != NULL_TREE)
2083 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2084 void_type_node, stat_tree, finally_tree);
2085 return this->make_statement(stat_tree);
2088 // If.
2090 Bstatement*
2091 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
2092 Bblock* else_block, Location location)
2094 tree cond_tree = condition->get_tree();
2095 tree then_tree = then_block->get_tree();
2096 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2097 if (cond_tree == error_mark_node
2098 || then_tree == error_mark_node
2099 || else_tree == error_mark_node)
2100 return this->error_statement();
2101 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2102 cond_tree, then_tree, else_tree);
2103 return this->make_statement(ret);
2106 // Switch.
2108 Bstatement*
2109 Gcc_backend::switch_statement(
2110 Bfunction* function,
2111 Bexpression* value,
2112 const std::vector<std::vector<Bexpression*> >& cases,
2113 const std::vector<Bstatement*>& statements,
2114 Location switch_location)
2116 gcc_assert(cases.size() == statements.size());
2118 tree decl = function->get_tree();
2119 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2120 push_struct_function(decl);
2121 else
2122 push_cfun(DECL_STRUCT_FUNCTION(decl));
2124 tree stmt_list = NULL_TREE;
2125 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2126 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2127 ps != statements.end();
2128 ++ps, ++pc)
2130 if (pc->empty())
2132 source_location loc = (*ps != NULL
2133 ? EXPR_LOCATION((*ps)->get_tree())
2134 : UNKNOWN_LOCATION);
2135 tree label = create_artificial_label(loc);
2136 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2137 append_to_statement_list(c, &stmt_list);
2139 else
2141 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2142 pcv != pc->end();
2143 ++pcv)
2145 tree t = (*pcv)->get_tree();
2146 if (t == error_mark_node)
2147 return this->error_statement();
2148 source_location loc = EXPR_LOCATION(t);
2149 tree label = create_artificial_label(loc);
2150 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2151 append_to_statement_list(c, &stmt_list);
2155 if (*ps != NULL)
2157 tree t = (*ps)->get_tree();
2158 if (t == error_mark_node)
2159 return this->error_statement();
2160 append_to_statement_list(t, &stmt_list);
2163 pop_cfun();
2165 tree tv = value->get_tree();
2166 if (tv == error_mark_node)
2167 return this->error_statement();
2168 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
2169 NULL_TREE, tv, stmt_list, NULL_TREE);
2170 return this->make_statement(t);
2173 // Pair of statements.
2175 Bstatement*
2176 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2178 tree stmt_list = NULL_TREE;
2179 tree t = s1->get_tree();
2180 if (t == error_mark_node)
2181 return this->error_statement();
2182 append_to_statement_list(t, &stmt_list);
2183 t = s2->get_tree();
2184 if (t == error_mark_node)
2185 return this->error_statement();
2186 append_to_statement_list(t, &stmt_list);
2188 // If neither statement has any side effects, stmt_list can be NULL
2189 // at this point.
2190 if (stmt_list == NULL_TREE)
2191 stmt_list = integer_zero_node;
2193 return this->make_statement(stmt_list);
2196 // List of statements.
2198 Bstatement*
2199 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2201 tree stmt_list = NULL_TREE;
2202 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2203 p != statements.end();
2204 ++p)
2206 tree t = (*p)->get_tree();
2207 if (t == error_mark_node)
2208 return this->error_statement();
2209 append_to_statement_list(t, &stmt_list);
2211 return this->make_statement(stmt_list);
2214 // Make a block. For some reason gcc uses a dual structure for
2215 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2216 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2217 // the Bblock.
2219 Bblock*
2220 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2221 const std::vector<Bvariable*>& vars,
2222 Location start_location,
2223 Location)
2225 tree block_tree = make_node(BLOCK);
2226 if (enclosing == NULL)
2228 tree fndecl = function->get_tree();
2229 gcc_assert(fndecl != NULL_TREE);
2231 // We may have already created a block for local variables when
2232 // we take the address of a parameter.
2233 if (DECL_INITIAL(fndecl) == NULL_TREE)
2235 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2236 DECL_INITIAL(fndecl) = block_tree;
2238 else
2240 tree superblock_tree = DECL_INITIAL(fndecl);
2241 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2242 tree* pp;
2243 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2244 *pp != NULL_TREE;
2245 pp = &BLOCK_CHAIN(*pp))
2247 *pp = block_tree;
2250 else
2252 tree superbind_tree = enclosing->get_tree();
2253 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2254 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2256 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2257 tree* pp;
2258 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2259 *pp != NULL_TREE;
2260 pp = &BLOCK_CHAIN(*pp))
2262 *pp = block_tree;
2265 tree* pp = &BLOCK_VARS(block_tree);
2266 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2267 pv != vars.end();
2268 ++pv)
2270 *pp = (*pv)->get_tree();
2271 if (*pp != error_mark_node)
2272 pp = &DECL_CHAIN(*pp);
2274 *pp = NULL_TREE;
2276 TREE_USED(block_tree) = 1;
2278 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2279 void_type_node, BLOCK_VARS(block_tree),
2280 NULL_TREE, block_tree);
2281 TREE_SIDE_EFFECTS(bind_tree) = 1;
2282 return new Bblock(bind_tree);
2285 // Add statements to a block.
2287 void
2288 Gcc_backend::block_add_statements(Bblock* bblock,
2289 const std::vector<Bstatement*>& statements)
2291 tree stmt_list = NULL_TREE;
2292 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2293 p != statements.end();
2294 ++p)
2296 tree s = (*p)->get_tree();
2297 if (s != error_mark_node)
2298 append_to_statement_list(s, &stmt_list);
2301 tree bind_tree = bblock->get_tree();
2302 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2303 BIND_EXPR_BODY(bind_tree) = stmt_list;
2306 // Return a block as a statement.
2308 Bstatement*
2309 Gcc_backend::block_statement(Bblock* bblock)
2311 tree bind_tree = bblock->get_tree();
2312 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2313 return this->make_statement(bind_tree);
2316 // This is not static because we declare it with GTY(()) in go-c.h.
2317 tree go_non_zero_struct;
2319 // Return a type corresponding to TYPE with non-zero size.
2321 tree
2322 Gcc_backend::non_zero_size_type(tree type)
2324 if (int_size_in_bytes(type) != 0)
2325 return type;
2327 switch (TREE_CODE(type))
2329 case RECORD_TYPE:
2330 if (TYPE_FIELDS(type) != NULL_TREE)
2332 tree ns = make_node(RECORD_TYPE);
2333 tree field_trees = NULL_TREE;
2334 tree *pp = &field_trees;
2335 for (tree field = TYPE_FIELDS(type);
2336 field != NULL_TREE;
2337 field = DECL_CHAIN(field))
2339 tree ft = TREE_TYPE(field);
2340 if (field == TYPE_FIELDS(type))
2341 ft = non_zero_size_type(ft);
2342 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2343 DECL_NAME(field), ft);
2344 DECL_CONTEXT(f) = ns;
2345 *pp = f;
2346 pp = &DECL_CHAIN(f);
2348 TYPE_FIELDS(ns) = field_trees;
2349 layout_type(ns);
2350 return ns;
2353 if (go_non_zero_struct == NULL_TREE)
2355 type = make_node(RECORD_TYPE);
2356 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2357 get_identifier("dummy"),
2358 boolean_type_node);
2359 DECL_CONTEXT(field) = type;
2360 TYPE_FIELDS(type) = field;
2361 layout_type(type);
2362 go_non_zero_struct = type;
2364 return go_non_zero_struct;
2366 case ARRAY_TYPE:
2368 tree element_type = non_zero_size_type(TREE_TYPE(type));
2369 return build_array_type_nelts(element_type, 1);
2372 default:
2373 gcc_unreachable();
2376 gcc_unreachable();
2379 // Make a global variable.
2381 Bvariable*
2382 Gcc_backend::global_variable(const std::string& package_name,
2383 const std::string& pkgpath,
2384 const std::string& name,
2385 Btype* btype,
2386 bool is_external,
2387 bool is_hidden,
2388 bool in_unique_section,
2389 Location location)
2391 tree type_tree = btype->get_tree();
2392 if (type_tree == error_mark_node)
2393 return this->error_variable();
2395 // The GNU linker does not like dynamic variables with zero size.
2396 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2397 type_tree = this->non_zero_size_type(type_tree);
2399 std::string var_name(package_name);
2400 var_name.push_back('.');
2401 var_name.append(name);
2402 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2403 get_identifier_from_string(var_name),
2404 type_tree);
2405 if (is_external)
2406 DECL_EXTERNAL(decl) = 1;
2407 else
2408 TREE_STATIC(decl) = 1;
2409 if (!is_hidden)
2411 TREE_PUBLIC(decl) = 1;
2413 std::string asm_name(pkgpath);
2414 asm_name.push_back('.');
2415 asm_name.append(name);
2416 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2418 TREE_USED(decl) = 1;
2420 if (in_unique_section)
2421 resolve_unique_section (decl, 0, 1);
2423 go_preserve_from_gc(decl);
2425 return new Bvariable(decl);
2428 // Set the initial value of a global variable.
2430 void
2431 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2433 tree expr_tree = expr->get_tree();
2434 if (expr_tree == error_mark_node)
2435 return;
2436 gcc_assert(TREE_CONSTANT(expr_tree));
2437 tree var_decl = var->get_tree();
2438 if (var_decl == error_mark_node)
2439 return;
2440 DECL_INITIAL(var_decl) = expr_tree;
2442 // If this variable goes in a unique section, it may need to go into
2443 // a different one now that DECL_INITIAL is set.
2444 if (symtab_node::get(var_decl)
2445 && symtab_node::get(var_decl)->implicit_section)
2447 set_decl_section_name (var_decl, NULL);
2448 resolve_unique_section (var_decl,
2449 compute_reloc_for_constant (expr_tree),
2454 // Make a local variable.
2456 Bvariable*
2457 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2458 Btype* btype, bool is_address_taken,
2459 Location location)
2461 tree type_tree = btype->get_tree();
2462 if (type_tree == error_mark_node)
2463 return this->error_variable();
2464 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2465 get_identifier_from_string(name),
2466 type_tree);
2467 DECL_CONTEXT(decl) = function->get_tree();
2468 TREE_USED(decl) = 1;
2469 if (is_address_taken)
2470 TREE_ADDRESSABLE(decl) = 1;
2471 go_preserve_from_gc(decl);
2472 return new Bvariable(decl);
2475 // Make a function parameter variable.
2477 Bvariable*
2478 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2479 Btype* btype, bool is_address_taken,
2480 Location location)
2482 tree type_tree = btype->get_tree();
2483 if (type_tree == error_mark_node)
2484 return this->error_variable();
2485 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2486 get_identifier_from_string(name),
2487 type_tree);
2488 DECL_CONTEXT(decl) = function->get_tree();
2489 DECL_ARG_TYPE(decl) = type_tree;
2490 TREE_USED(decl) = 1;
2491 if (is_address_taken)
2492 TREE_ADDRESSABLE(decl) = 1;
2493 go_preserve_from_gc(decl);
2494 return new Bvariable(decl);
2497 // Make a static chain variable.
2499 Bvariable*
2500 Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name,
2501 Btype* btype, Location location)
2503 tree type_tree = btype->get_tree();
2504 if (type_tree == error_mark_node)
2505 return this->error_variable();
2506 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2507 get_identifier_from_string(name), type_tree);
2508 tree fndecl = function->get_tree();
2509 DECL_CONTEXT(decl) = fndecl;
2510 DECL_ARG_TYPE(decl) = type_tree;
2511 TREE_USED(decl) = 1;
2512 DECL_ARTIFICIAL(decl) = 1;
2513 DECL_IGNORED_P(decl) = 1;
2514 TREE_READONLY(decl) = 1;
2516 struct function *f = DECL_STRUCT_FUNCTION(fndecl);
2517 if (f == NULL)
2519 push_struct_function(fndecl);
2520 pop_cfun();
2521 f = DECL_STRUCT_FUNCTION(fndecl);
2523 gcc_assert(f->static_chain_decl == NULL);
2524 f->static_chain_decl = decl;
2525 DECL_STATIC_CHAIN(fndecl) = 1;
2527 go_preserve_from_gc(decl);
2528 return new Bvariable(decl);
2531 // Make a temporary variable.
2533 Bvariable*
2534 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2535 Btype* btype, Bexpression* binit,
2536 bool is_address_taken,
2537 Location location,
2538 Bstatement** pstatement)
2540 gcc_assert(function != NULL);
2541 tree decl = function->get_tree();
2542 tree type_tree = btype->get_tree();
2543 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2544 if (type_tree == error_mark_node
2545 || init_tree == error_mark_node
2546 || decl == error_mark_node)
2548 *pstatement = this->error_statement();
2549 return this->error_variable();
2552 tree var;
2553 // We can only use create_tmp_var if the type is not addressable.
2554 if (!TREE_ADDRESSABLE(type_tree))
2556 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2557 push_struct_function(decl);
2558 else
2559 push_cfun(DECL_STRUCT_FUNCTION(decl));
2561 var = create_tmp_var(type_tree, "GOTMP");
2562 pop_cfun();
2564 else
2566 gcc_assert(bblock != NULL);
2567 var = build_decl(location.gcc_location(), VAR_DECL,
2568 create_tmp_var_name("GOTMP"),
2569 type_tree);
2570 DECL_ARTIFICIAL(var) = 1;
2571 DECL_IGNORED_P(var) = 1;
2572 TREE_USED(var) = 1;
2573 DECL_CONTEXT(var) = decl;
2575 // We have to add this variable to the BLOCK and the BIND_EXPR.
2576 tree bind_tree = bblock->get_tree();
2577 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2578 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2579 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2580 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2581 BLOCK_VARS(block_tree) = var;
2582 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2585 if (this->type_size(btype) != 0 && init_tree != NULL_TREE)
2586 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
2587 init_tree);
2589 if (is_address_taken)
2590 TREE_ADDRESSABLE(var) = 1;
2592 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2593 DECL_EXPR,
2594 void_type_node, var));
2596 // Don't initialize VAR with BINIT, but still evaluate BINIT for
2597 // its side effects.
2598 if (this->type_size(btype) == 0 && init_tree != NULL_TREE)
2599 *pstatement = this->compound_statement(this->expression_statement(binit),
2600 *pstatement);
2602 return new Bvariable(var);
2605 // Create an implicit variable that is compiler-defined. This is used when
2606 // generating GC root variables and storing the values of a slice initializer.
2608 Bvariable*
2609 Gcc_backend::implicit_variable(const std::string& name, Btype* type,
2610 bool is_hidden, bool is_constant,
2611 bool is_common, int64_t alignment)
2613 tree type_tree = type->get_tree();
2614 if (type_tree == error_mark_node)
2615 return this->error_variable();
2617 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2618 get_identifier_from_string(name), type_tree);
2619 DECL_EXTERNAL(decl) = 0;
2620 TREE_PUBLIC(decl) = !is_hidden;
2621 TREE_STATIC(decl) = 1;
2622 TREE_USED(decl) = 1;
2623 DECL_ARTIFICIAL(decl) = 1;
2624 if (is_common)
2626 DECL_COMMON(decl) = 1;
2628 // When the initializer for one implicit_variable refers to another,
2629 // it needs to know the visibility of the referenced struct so that
2630 // compute_reloc_for_constant will return the right value. On many
2631 // systems calling make_decl_one_only will mark the decl as weak,
2632 // which will change the return value of compute_reloc_for_constant.
2633 // We can't reliably call make_decl_one_only yet, because we don't
2634 // yet know the initializer. This issue doesn't arise in C because
2635 // Go initializers, unlike C initializers, can be indirectly
2636 // recursive. To ensure that compute_reloc_for_constant computes
2637 // the right value if some other initializer refers to this one, we
2638 // mark this symbol as weak here. We undo that below in
2639 // immutable_struct_set_init before calling mark_decl_one_only.
2640 DECL_WEAK(decl) = 1;
2642 if (is_constant)
2644 TREE_READONLY(decl) = 1;
2645 TREE_CONSTANT(decl) = 1;
2647 if (alignment != 0)
2649 DECL_ALIGN(decl) = alignment * BITS_PER_UNIT;
2650 DECL_USER_ALIGN(decl) = 1;
2653 go_preserve_from_gc(decl);
2654 return new Bvariable(decl);
2657 // Set the initalizer for a variable created by implicit_variable.
2658 // This is where we finish compiling the variable.
2660 void
2661 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
2662 Btype*, bool, bool, bool is_common,
2663 Bexpression* init)
2665 tree decl = var->get_tree();
2666 tree init_tree;
2667 if (init == NULL)
2668 init_tree = NULL_TREE;
2669 else
2670 init_tree = init->get_tree();
2671 if (decl == error_mark_node || init_tree == error_mark_node)
2672 return;
2674 DECL_INITIAL(decl) = init_tree;
2676 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2677 // See the comment where DECL_WEAK is set in implicit_variable.
2678 if (is_common)
2680 DECL_WEAK(decl) = 0;
2681 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2684 resolve_unique_section(decl, 2, 1);
2686 rest_of_decl_compilation(decl, 1, 0);
2689 // Return a reference to an implicit variable defined in another package.
2691 Bvariable*
2692 Gcc_backend::implicit_variable_reference(const std::string& name, Btype* btype)
2694 tree type_tree = btype->get_tree();
2695 if (type_tree == error_mark_node)
2696 return this->error_variable();
2698 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2699 get_identifier_from_string(name), type_tree);
2700 DECL_EXTERNAL(decl) = 0;
2701 TREE_PUBLIC(decl) = 1;
2702 TREE_STATIC(decl) = 1;
2703 DECL_ARTIFICIAL(decl) = 1;
2704 go_preserve_from_gc(decl);
2705 return new Bvariable(decl);
2708 // Create a named immutable initialized data structure.
2710 Bvariable*
2711 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
2712 bool is_common, Btype* btype, Location location)
2714 tree type_tree = btype->get_tree();
2715 if (type_tree == error_mark_node)
2716 return this->error_variable();
2717 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2718 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2719 get_identifier_from_string(name),
2720 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2721 TREE_STATIC(decl) = 1;
2722 TREE_USED(decl) = 1;
2723 TREE_READONLY(decl) = 1;
2724 TREE_CONSTANT(decl) = 1;
2725 DECL_ARTIFICIAL(decl) = 1;
2726 if (!is_hidden)
2727 TREE_PUBLIC(decl) = 1;
2729 // When the initializer for one immutable_struct refers to another,
2730 // it needs to know the visibility of the referenced struct so that
2731 // compute_reloc_for_constant will return the right value. On many
2732 // systems calling make_decl_one_only will mark the decl as weak,
2733 // which will change the return value of compute_reloc_for_constant.
2734 // We can't reliably call make_decl_one_only yet, because we don't
2735 // yet know the initializer. This issue doesn't arise in C because
2736 // Go initializers, unlike C initializers, can be indirectly
2737 // recursive. To ensure that compute_reloc_for_constant computes
2738 // the right value if some other initializer refers to this one, we
2739 // mark this symbol as weak here. We undo that below in
2740 // immutable_struct_set_init before calling mark_decl_one_only.
2741 if (is_common)
2742 DECL_WEAK(decl) = 1;
2744 // We don't call rest_of_decl_compilation until we have the
2745 // initializer.
2747 go_preserve_from_gc(decl);
2748 return new Bvariable(decl);
2751 // Set the initializer for a variable created by immutable_struct.
2752 // This is where we finish compiling the variable.
2754 void
2755 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2756 bool, bool is_common, Btype*, Location,
2757 Bexpression* initializer)
2759 tree decl = var->get_tree();
2760 tree init_tree = initializer->get_tree();
2761 if (decl == error_mark_node || init_tree == error_mark_node)
2762 return;
2764 DECL_INITIAL(decl) = init_tree;
2766 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2767 // See the comment where DECL_WEAK is set in immutable_struct.
2768 if (is_common)
2770 DECL_WEAK(decl) = 0;
2771 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2774 // These variables are often unneeded in the final program, so put
2775 // them in their own section so that linker GC can discard them.
2776 resolve_unique_section(decl,
2777 compute_reloc_for_constant (init_tree),
2780 rest_of_decl_compilation(decl, 1, 0);
2783 // Return a reference to an immutable initialized data structure
2784 // defined in another package.
2786 Bvariable*
2787 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
2788 Location location)
2790 tree type_tree = btype->get_tree();
2791 if (type_tree == error_mark_node)
2792 return this->error_variable();
2793 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2794 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2795 get_identifier_from_string(name),
2796 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2797 TREE_READONLY(decl) = 1;
2798 TREE_CONSTANT(decl) = 1;
2799 DECL_ARTIFICIAL(decl) = 1;
2800 TREE_PUBLIC(decl) = 1;
2801 DECL_EXTERNAL(decl) = 1;
2802 go_preserve_from_gc(decl);
2803 return new Bvariable(decl);
2806 // Make a label.
2808 Blabel*
2809 Gcc_backend::label(Bfunction* function, const std::string& name,
2810 Location location)
2812 tree decl;
2813 if (name.empty())
2815 tree func_tree = function->get_tree();
2816 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2817 push_struct_function(func_tree);
2818 else
2819 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2821 decl = create_artificial_label(location.gcc_location());
2823 pop_cfun();
2825 else
2827 tree id = get_identifier_from_string(name);
2828 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
2829 void_type_node);
2830 DECL_CONTEXT(decl) = function->get_tree();
2832 return new Blabel(decl);
2835 // Make a statement which defines a label.
2837 Bstatement*
2838 Gcc_backend::label_definition_statement(Blabel* label)
2840 tree lab = label->get_tree();
2841 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
2842 void_type_node, lab);
2843 return this->make_statement(ret);
2846 // Make a goto statement.
2848 Bstatement*
2849 Gcc_backend::goto_statement(Blabel* label, Location location)
2851 tree lab = label->get_tree();
2852 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
2853 lab);
2854 return this->make_statement(ret);
2857 // Get the address of a label.
2859 Bexpression*
2860 Gcc_backend::label_address(Blabel* label, Location location)
2862 tree lab = label->get_tree();
2863 TREE_USED(lab) = 1;
2864 TREE_ADDRESSABLE(lab) = 1;
2865 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
2866 build_fold_addr_expr_loc(location.gcc_location(),
2867 lab));
2868 return this->make_expression(ret);
2871 // Declare or define a new function.
2873 Bfunction*
2874 Gcc_backend::function(Btype* fntype, const std::string& name,
2875 const std::string& asm_name, bool is_visible,
2876 bool is_declaration, bool is_inlinable,
2877 bool disable_split_stack, bool in_unique_section,
2878 Location location)
2880 tree functype = fntype->get_tree();
2881 if (functype != error_mark_node)
2883 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
2884 functype = TREE_TYPE(functype);
2886 tree id = get_identifier_from_string(name);
2887 if (functype == error_mark_node || id == error_mark_node)
2888 return this->error_function();
2890 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
2891 if (!asm_name.empty())
2892 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2893 if (is_visible)
2894 TREE_PUBLIC(decl) = 1;
2895 if (is_declaration)
2896 DECL_EXTERNAL(decl) = 1;
2897 else
2899 tree restype = TREE_TYPE(functype);
2900 tree resdecl =
2901 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
2902 DECL_ARTIFICIAL(resdecl) = 1;
2903 DECL_IGNORED_P(resdecl) = 1;
2904 DECL_CONTEXT(resdecl) = decl;
2905 DECL_RESULT(decl) = resdecl;
2907 if (!is_inlinable)
2908 DECL_UNINLINABLE(decl) = 1;
2909 if (disable_split_stack)
2911 tree attr = get_identifier("__no_split_stack__");
2912 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
2914 if (in_unique_section)
2915 resolve_unique_section(decl, 0, 1);
2917 go_preserve_from_gc(decl);
2918 return new Bfunction(decl);
2921 // Create a statement that runs all deferred calls for FUNCTION. This should
2922 // be a statement that looks like this in C++:
2923 // finish:
2924 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2926 Bstatement*
2927 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
2928 Bexpression* defer, Location location)
2930 tree undefer_tree = undefer->get_tree();
2931 tree defer_tree = defer->get_tree();
2932 tree fntree = function->get_tree();
2934 if (undefer_tree == error_mark_node
2935 || defer_tree == error_mark_node
2936 || fntree == error_mark_node)
2937 return this->error_statement();
2939 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2940 push_struct_function(fntree);
2941 else
2942 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2944 tree stmt_list = NULL;
2945 Blabel* blabel = this->label(function, "", location);
2946 Bstatement* label_def = this->label_definition_statement(blabel);
2947 append_to_statement_list(label_def->get_tree(), &stmt_list);
2949 Bstatement* jump_stmt = this->goto_statement(blabel, location);
2950 tree jump = jump_stmt->get_tree();
2951 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
2952 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
2953 tree try_catch =
2954 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
2955 append_to_statement_list(try_catch, &stmt_list);
2956 pop_cfun();
2958 return this->make_statement(stmt_list);
2961 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2962 // This will only be called for a function definition.
2964 bool
2965 Gcc_backend::function_set_parameters(Bfunction* function,
2966 const std::vector<Bvariable*>& param_vars)
2968 tree func_tree = function->get_tree();
2969 if (func_tree == error_mark_node)
2970 return false;
2972 tree params = NULL_TREE;
2973 tree *pp = &params;
2974 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
2975 pv != param_vars.end();
2976 ++pv)
2978 *pp = (*pv)->get_tree();
2979 gcc_assert(*pp != error_mark_node);
2980 pp = &DECL_CHAIN(*pp);
2982 *pp = NULL_TREE;
2983 DECL_ARGUMENTS(func_tree) = params;
2984 return true;
2987 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2989 bool
2990 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
2992 tree func_tree = function->get_tree();
2993 tree code = code_stmt->get_tree();
2995 if (func_tree == error_mark_node || code == error_mark_node)
2996 return false;
2997 DECL_SAVED_TREE(func_tree) = code;
2998 return true;
3001 // Look up a named built-in function in the current backend implementation.
3002 // Returns NULL if no built-in function by that name exists.
3004 Bfunction*
3005 Gcc_backend::lookup_builtin(const std::string& name)
3007 if (this->builtin_functions_.count(name) != 0)
3008 return this->builtin_functions_[name];
3009 return NULL;
3012 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3013 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
3015 void
3016 Gcc_backend::write_global_definitions(
3017 const std::vector<Btype*>& type_decls,
3018 const std::vector<Bexpression*>& constant_decls,
3019 const std::vector<Bfunction*>& function_decls,
3020 const std::vector<Bvariable*>& variable_decls)
3022 size_t count_definitions = type_decls.size() + constant_decls.size()
3023 + function_decls.size() + variable_decls.size();
3025 tree* defs = new tree[count_definitions];
3027 // Convert all non-erroneous declarations into Gimple form.
3028 size_t i = 0;
3029 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
3030 p != variable_decls.end();
3031 ++p)
3033 if ((*p)->get_tree() != error_mark_node)
3035 defs[i] = (*p)->get_tree();
3036 go_preserve_from_gc(defs[i]);
3037 ++i;
3041 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
3042 p != type_decls.end();
3043 ++p)
3045 tree type_tree = (*p)->get_tree();
3046 if (type_tree != error_mark_node
3047 && IS_TYPE_OR_DECL_P(type_tree))
3049 defs[i] = TYPE_NAME(type_tree);
3050 gcc_assert(defs[i] != NULL);
3051 go_preserve_from_gc(defs[i]);
3052 ++i;
3055 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
3056 p != constant_decls.end();
3057 ++p)
3059 if ((*p)->get_tree() != error_mark_node)
3061 defs[i] = (*p)->get_tree();
3062 go_preserve_from_gc(defs[i]);
3063 ++i;
3066 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
3067 p != function_decls.end();
3068 ++p)
3070 tree decl = (*p)->get_tree();
3071 if (decl != error_mark_node)
3073 go_preserve_from_gc(decl);
3074 gimplify_function_tree(decl);
3075 cgraph_node::finalize_function(decl, true);
3077 defs[i] = decl;
3078 ++i;
3082 // Pass everything back to the middle-end.
3084 wrapup_global_declarations(defs, i);
3086 symtab->finalize_compilation_unit();
3088 check_global_declarations(defs, i);
3089 emit_debug_global_declarations(defs, i);
3091 delete[] defs;
3094 // Define a builtin function. BCODE is the builtin function code
3095 // defined by builtins.def. NAME is the name of the builtin function.
3096 // LIBNAME is the name of the corresponding library function, and is
3097 // NULL if there isn't one. FNTYPE is the type of the function.
3098 // CONST_P is true if the function has the const attribute.
3100 void
3101 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3102 const char* libname, tree fntype, bool const_p)
3104 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3105 libname, NULL_TREE);
3106 if (const_p)
3107 TREE_READONLY(decl) = 1;
3108 set_builtin_decl(bcode, decl, true);
3109 this->builtin_functions_[name] = this->make_function(decl);
3110 if (libname != NULL)
3112 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3113 NULL, NULL_TREE);
3114 if (const_p)
3115 TREE_READONLY(decl) = 1;
3116 this->builtin_functions_[libname] = this->make_function(decl);
3120 // Return the backend generator.
3122 Backend*
3123 go_get_backend()
3125 return new Gcc_backend();