Merge from trunk:
[official-gcc.git] / main / gcc / go / go-gcc.cc
blob97904d06d4352b5669be1647031a8324f63e5cfc
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 "cgraph.h"
33 #include "convert.h"
34 #include "basic-block.h"
35 #include "gimple-expr.h"
36 #include "gimplify.h"
37 #include "langhooks.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "real.h"
41 #include "realmpfr.h"
42 #include "builtins.h"
44 #include "go-c.h"
46 #include "gogo.h"
47 #include "backend.h"
49 // A class wrapping a tree.
51 class Gcc_tree
53 public:
54 Gcc_tree(tree t)
55 : t_(t)
56 { }
58 tree
59 get_tree() const
60 { return this->t_; }
62 void
63 set_tree(tree t)
64 { this->t_ = t; }
66 private:
67 tree t_;
70 // In gcc, types, expressions, and statements are all trees.
71 class Btype : public Gcc_tree
73 public:
74 Btype(tree t)
75 : Gcc_tree(t)
76 { }
79 class Bexpression : public Gcc_tree
81 public:
82 Bexpression(tree t)
83 : Gcc_tree(t)
84 { }
87 class Bstatement : public Gcc_tree
89 public:
90 Bstatement(tree t)
91 : Gcc_tree(t)
92 { }
95 class Bfunction : public Gcc_tree
97 public:
98 Bfunction(tree t)
99 : Gcc_tree(t)
103 class Bblock : public Gcc_tree
105 public:
106 Bblock(tree t)
107 : Gcc_tree(t)
111 class Bvariable : public Gcc_tree
113 public:
114 Bvariable(tree t)
115 : Gcc_tree(t)
119 class Blabel : public Gcc_tree
121 public:
122 Blabel(tree t)
123 : Gcc_tree(t)
127 // This file implements the interface between the Go frontend proper
128 // and the gcc IR. This implements specific instantiations of
129 // abstract classes defined by the Go frontend proper. The Go
130 // frontend proper class methods of these classes to generate the
131 // backend representation.
133 class Gcc_backend : public Backend
135 public:
136 Gcc_backend();
138 // Types.
140 Btype*
141 error_type()
142 { return this->make_type(error_mark_node); }
144 Btype*
145 void_type()
146 { return this->make_type(void_type_node); }
148 Btype*
149 bool_type()
150 { return this->make_type(boolean_type_node); }
152 Btype*
153 integer_type(bool, int);
155 Btype*
156 float_type(int);
158 Btype*
159 complex_type(int);
161 Btype*
162 pointer_type(Btype*);
164 Btype*
165 function_type(const Btyped_identifier&,
166 const std::vector<Btyped_identifier>&,
167 const std::vector<Btyped_identifier>&,
168 Btype*,
169 const Location);
171 Btype*
172 struct_type(const std::vector<Btyped_identifier>&);
174 Btype*
175 array_type(Btype*, Bexpression*);
177 Btype*
178 placeholder_pointer_type(const std::string&, Location, bool);
180 bool
181 set_placeholder_pointer_type(Btype*, Btype*);
183 bool
184 set_placeholder_function_type(Btype*, Btype*);
186 Btype*
187 placeholder_struct_type(const std::string&, Location);
189 bool
190 set_placeholder_struct_type(Btype* placeholder,
191 const std::vector<Btyped_identifier>&);
193 Btype*
194 placeholder_array_type(const std::string&, Location);
196 bool
197 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
199 Btype*
200 named_type(const std::string&, Btype*, Location);
202 Btype*
203 circular_pointer_type(Btype*, bool);
205 bool
206 is_circular_pointer_type(Btype*);
208 size_t
209 type_size(Btype*);
211 size_t
212 type_alignment(Btype*);
214 size_t
215 type_field_alignment(Btype*);
217 size_t
218 type_field_offset(Btype*, size_t index);
220 // Expressions.
222 Bexpression*
223 zero_expression(Btype*);
225 Bexpression*
226 error_expression()
227 { return this->make_expression(error_mark_node); }
229 Bexpression*
230 nil_pointer_expression()
231 { return this->make_expression(null_pointer_node); }
233 Bexpression*
234 var_expression(Bvariable* var, Location);
236 Bexpression*
237 indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location);
239 Bexpression*
240 named_constant_expression(Btype* btype, const std::string& name,
241 Bexpression* val, Location);
243 Bexpression*
244 integer_constant_expression(Btype* btype, mpz_t val);
246 Bexpression*
247 float_constant_expression(Btype* btype, mpfr_t val);
249 Bexpression*
250 complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag);
252 Bexpression*
253 string_constant_expression(const std::string& val);
255 Bexpression*
256 boolean_constant_expression(bool val);
258 Bexpression*
259 real_part_expression(Bexpression* bcomplex, Location);
261 Bexpression*
262 imag_part_expression(Bexpression* bcomplex, Location);
264 Bexpression*
265 complex_expression(Bexpression* breal, Bexpression* bimag, Location);
267 Bexpression*
268 convert_expression(Btype* type, Bexpression* expr, Location);
270 Bexpression*
271 function_code_expression(Bfunction*, Location);
273 Bexpression*
274 address_expression(Bexpression*, Location);
276 Bexpression*
277 struct_field_expression(Bexpression*, size_t, Location);
279 Bexpression*
280 compound_expression(Bstatement*, Bexpression*, Location);
282 Bexpression*
283 conditional_expression(Btype*, Bexpression*, Bexpression*, Bexpression*,
284 Location);
286 Bexpression*
287 unary_expression(Operator, Bexpression*, Location);
289 Bexpression*
290 binary_expression(Operator, Bexpression*, Bexpression*, Location);
292 Bexpression*
293 constructor_expression(Btype*, const std::vector<Bexpression*>&, Location);
295 Bexpression*
296 array_constructor_expression(Btype*, const std::vector<unsigned long>&,
297 const std::vector<Bexpression*>&, Location);
299 Bexpression*
300 pointer_offset_expression(Bexpression* base, Bexpression* offset, Location);
302 Bexpression*
303 array_index_expression(Bexpression* array, Bexpression* index, Location);
305 Bexpression*
306 call_expression(Bexpression* fn, const std::vector<Bexpression*>& args,
307 Location);
309 // Statements.
311 Bstatement*
312 error_statement()
313 { return this->make_statement(error_mark_node); }
315 Bstatement*
316 expression_statement(Bexpression*);
318 Bstatement*
319 init_statement(Bvariable* var, Bexpression* init);
321 Bstatement*
322 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
324 Bstatement*
325 return_statement(Bfunction*, const std::vector<Bexpression*>&,
326 Location);
328 Bstatement*
329 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
330 Location);
332 Bstatement*
333 switch_statement(Bfunction* function, Bexpression* value,
334 const std::vector<std::vector<Bexpression*> >& cases,
335 const std::vector<Bstatement*>& statements,
336 Location);
338 Bstatement*
339 compound_statement(Bstatement*, Bstatement*);
341 Bstatement*
342 statement_list(const std::vector<Bstatement*>&);
344 Bstatement*
345 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
346 Bstatement* finally_stmt, Location);
348 // Blocks.
350 Bblock*
351 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
352 Location, Location);
354 void
355 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
357 Bstatement*
358 block_statement(Bblock*);
360 // Variables.
362 Bvariable*
363 error_variable()
364 { return new Bvariable(error_mark_node); }
366 Bvariable*
367 global_variable(const std::string& package_name,
368 const std::string& pkgpath,
369 const std::string& name,
370 Btype* btype,
371 bool is_external,
372 bool is_hidden,
373 bool in_unique_section,
374 Location location);
376 void
377 global_variable_set_init(Bvariable*, Bexpression*);
379 Bvariable*
380 local_variable(Bfunction*, const std::string&, Btype*, bool,
381 Location);
383 Bvariable*
384 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
385 Location);
387 Bvariable*
388 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
389 Location, Bstatement**);
391 Bvariable*
392 implicit_variable(const std::string&, Btype*, Bexpression*, bool, bool,
393 size_t);
395 Bvariable*
396 immutable_struct(const std::string&, bool, bool, Btype*, Location);
398 void
399 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
400 Location, Bexpression*);
402 Bvariable*
403 immutable_struct_reference(const std::string&, Btype*, Location);
405 // Labels.
407 Blabel*
408 label(Bfunction*, const std::string& name, Location);
410 Bstatement*
411 label_definition_statement(Blabel*);
413 Bstatement*
414 goto_statement(Blabel*, Location);
416 Bexpression*
417 label_address(Blabel*, Location);
419 // Functions.
421 Bfunction*
422 error_function()
423 { return this->make_function(error_mark_node); }
425 Bfunction*
426 function(Btype* fntype, const std::string& name, const std::string& asm_name,
427 bool is_visible, bool is_declaration, bool is_inlinable,
428 bool disable_split_stack, bool in_unique_section, Location);
430 Bstatement*
431 function_defer_statement(Bfunction* function, Bexpression* undefer,
432 Bexpression* defer, Location);
434 bool
435 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
437 bool
438 function_set_body(Bfunction* function, Bstatement* code_stmt);
440 Bfunction*
441 lookup_builtin(const std::string&);
443 void
444 write_global_definitions(const std::vector<Btype*>&,
445 const std::vector<Bexpression*>&,
446 const std::vector<Bfunction*>&,
447 const std::vector<Bvariable*>&);
449 private:
450 // Make a Bexpression from a tree.
451 Bexpression*
452 make_expression(tree t)
453 { return new Bexpression(t); }
455 // Make a Bstatement from a tree.
456 Bstatement*
457 make_statement(tree t)
458 { return new Bstatement(t); }
460 // Make a Btype from a tree.
461 Btype*
462 make_type(tree t)
463 { return new Btype(t); }
465 Bfunction*
466 make_function(tree t)
467 { return new Bfunction(t); }
469 Btype*
470 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
472 Btype*
473 fill_in_array(Btype*, Btype*, Bexpression*);
475 tree
476 non_zero_size_type(tree);
478 private:
479 void
480 define_builtin(built_in_function bcode, const char* name, const char* libname,
481 tree fntype, bool const_p);
483 // A mapping of the GCC built-ins exposed to GCCGo.
484 std::map<std::string, Bfunction*> builtin_functions_;
487 // A helper function.
489 static inline tree
490 get_identifier_from_string(const std::string& str)
492 return get_identifier_with_length(str.data(), str.length());
495 // Define the built-in functions that are exposed to GCCGo.
497 Gcc_backend::Gcc_backend()
499 /* We need to define the fetch_and_add functions, since we use them
500 for ++ and --. */
501 tree t = this->integer_type(BITS_PER_UNIT, 1)->get_tree();
502 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
503 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
504 NULL, build_function_type_list(t, p, t, NULL_TREE),
505 false);
507 t = this->integer_type(BITS_PER_UNIT * 2, 1)->get_tree();
508 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
509 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
510 NULL, build_function_type_list(t, p, t, NULL_TREE),
511 false);
513 t = this->integer_type(BITS_PER_UNIT * 4, 1)->get_tree();
514 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
515 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
516 NULL, build_function_type_list(t, p, t, NULL_TREE),
517 false);
519 t = this->integer_type(BITS_PER_UNIT * 8, 1)->get_tree();
520 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
521 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
522 NULL, build_function_type_list(t, p, t, NULL_TREE),
523 false);
525 // We use __builtin_expect for magic import functions.
526 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
527 build_function_type_list(long_integer_type_node,
528 long_integer_type_node,
529 long_integer_type_node,
530 NULL_TREE),
531 true);
533 // We use __builtin_memcmp for struct comparisons.
534 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
535 build_function_type_list(integer_type_node,
536 const_ptr_type_node,
537 const_ptr_type_node,
538 size_type_node,
539 NULL_TREE),
540 false);
542 // We provide some functions for the math library.
543 tree math_function_type = build_function_type_list(double_type_node,
544 double_type_node,
545 NULL_TREE);
546 tree math_function_type_long =
547 build_function_type_list(long_double_type_node, long_double_type_node,
548 long_double_type_node, NULL_TREE);
549 tree math_function_type_two = build_function_type_list(double_type_node,
550 double_type_node,
551 double_type_node,
552 NULL_TREE);
553 tree math_function_type_long_two =
554 build_function_type_list(long_double_type_node, long_double_type_node,
555 long_double_type_node, NULL_TREE);
556 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
557 math_function_type, true);
558 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
559 math_function_type_long, true);
560 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
561 math_function_type, true);
562 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
563 math_function_type_long, true);
564 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
565 math_function_type, true);
566 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
567 math_function_type_long, true);
568 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
569 math_function_type_two, true);
570 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
571 math_function_type_long_two, true);
572 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
573 math_function_type, true);
574 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
575 math_function_type_long, true);
576 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
577 math_function_type, true);
578 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
579 math_function_type_long, true);
580 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
581 math_function_type, true);
582 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
583 math_function_type_long, true);
584 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
585 math_function_type, true);
586 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
587 math_function_type_long, true);
588 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
589 math_function_type, true);
590 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
591 math_function_type_long, true);
592 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
593 math_function_type, true);
594 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
595 math_function_type_long, true);
596 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
597 math_function_type_two, true);
598 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
599 math_function_type_long_two, true);
600 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
601 build_function_type_list(double_type_node,
602 double_type_node,
603 integer_type_node,
604 NULL_TREE),
605 true);
606 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
607 build_function_type_list(long_double_type_node,
608 long_double_type_node,
609 integer_type_node,
610 NULL_TREE),
611 true);
612 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
613 math_function_type, true);
614 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
615 math_function_type_long, true);
616 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
617 math_function_type, true);
618 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
619 math_function_type_long, true);
620 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
621 math_function_type, true);
622 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
623 math_function_type_long, true);
624 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
625 math_function_type, true);
626 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
627 math_function_type_long, true);
628 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
629 math_function_type, true);
630 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
631 math_function_type_long, true);
632 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
633 math_function_type, true);
634 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
635 math_function_type_long, true);
636 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
637 math_function_type, true);
638 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
639 math_function_type_long, true);
640 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
641 math_function_type, true);
642 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
643 math_function_type_long, true);
645 // We use __builtin_return_address in the thunk we build for
646 // functions which call recover.
647 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
648 NULL,
649 build_function_type_list(ptr_type_node,
650 unsigned_type_node,
651 NULL_TREE),
652 false);
654 // The compiler uses __builtin_trap for some exception handling
655 // cases.
656 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
657 build_function_type(void_type_node, void_list_node),
658 false);
661 // Get an unnamed integer type.
663 Btype*
664 Gcc_backend::integer_type(bool is_unsigned, int bits)
666 tree type;
667 if (is_unsigned)
669 if (bits == INT_TYPE_SIZE)
670 type = unsigned_type_node;
671 else if (bits == CHAR_TYPE_SIZE)
672 type = unsigned_char_type_node;
673 else if (bits == SHORT_TYPE_SIZE)
674 type = short_unsigned_type_node;
675 else if (bits == LONG_TYPE_SIZE)
676 type = long_unsigned_type_node;
677 else if (bits == LONG_LONG_TYPE_SIZE)
678 type = long_long_unsigned_type_node;
679 else
680 type = make_unsigned_type(bits);
682 else
684 if (bits == INT_TYPE_SIZE)
685 type = integer_type_node;
686 else if (bits == CHAR_TYPE_SIZE)
687 type = signed_char_type_node;
688 else if (bits == SHORT_TYPE_SIZE)
689 type = short_integer_type_node;
690 else if (bits == LONG_TYPE_SIZE)
691 type = long_integer_type_node;
692 else if (bits == LONG_LONG_TYPE_SIZE)
693 type = long_long_integer_type_node;
694 else
695 type = make_signed_type(bits);
697 return this->make_type(type);
700 // Get an unnamed float type.
702 Btype*
703 Gcc_backend::float_type(int bits)
705 tree type;
706 if (bits == FLOAT_TYPE_SIZE)
707 type = float_type_node;
708 else if (bits == DOUBLE_TYPE_SIZE)
709 type = double_type_node;
710 else if (bits == LONG_DOUBLE_TYPE_SIZE)
711 type = long_double_type_node;
712 else
714 type = make_node(REAL_TYPE);
715 TYPE_PRECISION(type) = bits;
716 layout_type(type);
718 return this->make_type(type);
721 // Get an unnamed complex type.
723 Btype*
724 Gcc_backend::complex_type(int bits)
726 tree type;
727 if (bits == FLOAT_TYPE_SIZE * 2)
728 type = complex_float_type_node;
729 else if (bits == DOUBLE_TYPE_SIZE * 2)
730 type = complex_double_type_node;
731 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
732 type = complex_long_double_type_node;
733 else
735 type = make_node(REAL_TYPE);
736 TYPE_PRECISION(type) = bits / 2;
737 layout_type(type);
738 type = build_complex_type(type);
740 return this->make_type(type);
743 // Get a pointer type.
745 Btype*
746 Gcc_backend::pointer_type(Btype* to_type)
748 tree to_type_tree = to_type->get_tree();
749 if (to_type_tree == error_mark_node)
750 return this->error_type();
751 tree type = build_pointer_type(to_type_tree);
752 return this->make_type(type);
755 // Make a function type.
757 Btype*
758 Gcc_backend::function_type(const Btyped_identifier& receiver,
759 const std::vector<Btyped_identifier>& parameters,
760 const std::vector<Btyped_identifier>& results,
761 Btype* result_struct,
762 Location)
764 tree args = NULL_TREE;
765 tree* pp = &args;
766 if (receiver.btype != NULL)
768 tree t = receiver.btype->get_tree();
769 if (t == error_mark_node)
770 return this->error_type();
771 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
772 pp = &TREE_CHAIN(*pp);
775 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
776 p != parameters.end();
777 ++p)
779 tree t = p->btype->get_tree();
780 if (t == error_mark_node)
781 return this->error_type();
782 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
783 pp = &TREE_CHAIN(*pp);
786 // Varargs is handled entirely at the Go level. When converted to
787 // GENERIC functions are not varargs.
788 *pp = void_list_node;
790 tree result;
791 if (results.empty())
792 result = void_type_node;
793 else if (results.size() == 1)
794 result = results.front().btype->get_tree();
795 else
797 gcc_assert(result_struct != NULL);
798 result = result_struct->get_tree();
800 if (result == error_mark_node)
801 return this->error_type();
803 tree fntype = build_function_type(result, args);
804 if (fntype == error_mark_node)
805 return this->error_type();
807 return this->make_type(build_pointer_type(fntype));
810 // Make a struct type.
812 Btype*
813 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
815 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
818 // Fill in the fields of a struct type.
820 Btype*
821 Gcc_backend::fill_in_struct(Btype* fill,
822 const std::vector<Btyped_identifier>& fields)
824 tree fill_tree = fill->get_tree();
825 tree field_trees = NULL_TREE;
826 tree* pp = &field_trees;
827 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
828 p != fields.end();
829 ++p)
831 tree name_tree = get_identifier_from_string(p->name);
832 tree type_tree = p->btype->get_tree();
833 if (type_tree == error_mark_node)
834 return this->error_type();
835 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
836 type_tree);
837 DECL_CONTEXT(field) = fill_tree;
838 *pp = field;
839 pp = &DECL_CHAIN(field);
841 TYPE_FIELDS(fill_tree) = field_trees;
842 layout_type(fill_tree);
843 return fill;
846 // Make an array type.
848 Btype*
849 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
851 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
852 element_btype, length);
855 // Fill in an array type.
857 Btype*
858 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
859 Bexpression* length)
861 tree element_type_tree = element_type->get_tree();
862 tree length_tree = length->get_tree();
863 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
864 return this->error_type();
866 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
868 length_tree = fold_convert(sizetype, length_tree);
870 // build_index_type takes the maximum index, which is one less than
871 // the length.
872 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
873 length_tree,
874 size_one_node));
876 tree fill_tree = fill->get_tree();
877 TREE_TYPE(fill_tree) = element_type_tree;
878 TYPE_DOMAIN(fill_tree) = index_type_tree;
879 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
880 layout_type(fill_tree);
882 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
883 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
884 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
885 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
886 TYPE_CANONICAL(fill_tree) =
887 build_array_type(TYPE_CANONICAL(element_type_tree),
888 TYPE_CANONICAL(index_type_tree));
890 return fill;
893 // Create a placeholder for a pointer type.
895 Btype*
896 Gcc_backend::placeholder_pointer_type(const std::string& name,
897 Location location, bool)
899 tree ret = build_distinct_type_copy(ptr_type_node);
900 if (!name.empty())
902 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
903 get_identifier_from_string(name),
904 ret);
905 TYPE_NAME(ret) = decl;
907 return this->make_type(ret);
910 // Set the real target type for a placeholder pointer type.
912 bool
913 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
914 Btype* to_type)
916 tree pt = placeholder->get_tree();
917 if (pt == error_mark_node)
918 return false;
919 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
920 tree tt = to_type->get_tree();
921 if (tt == error_mark_node)
923 placeholder->set_tree(error_mark_node);
924 return false;
926 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
927 TREE_TYPE(pt) = TREE_TYPE(tt);
928 if (TYPE_NAME(pt) != NULL_TREE)
930 // Build the data structure gcc wants to see for a typedef.
931 tree copy = build_variant_type_copy(pt);
932 TYPE_NAME(copy) = NULL_TREE;
933 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
935 return true;
938 // Set the real values for a placeholder function type.
940 bool
941 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
943 return this->set_placeholder_pointer_type(placeholder, ft);
946 // Create a placeholder for a struct type.
948 Btype*
949 Gcc_backend::placeholder_struct_type(const std::string& name,
950 Location location)
952 tree ret = make_node(RECORD_TYPE);
953 if (!name.empty())
955 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
956 get_identifier_from_string(name),
957 ret);
958 TYPE_NAME(ret) = decl;
960 return this->make_type(ret);
963 // Fill in the fields of a placeholder struct type.
965 bool
966 Gcc_backend::set_placeholder_struct_type(
967 Btype* placeholder,
968 const std::vector<Btyped_identifier>& fields)
970 tree t = placeholder->get_tree();
971 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
972 Btype* r = this->fill_in_struct(placeholder, fields);
974 if (TYPE_NAME(t) != NULL_TREE)
976 // Build the data structure gcc wants to see for a typedef.
977 tree copy = build_distinct_type_copy(t);
978 TYPE_NAME(copy) = NULL_TREE;
979 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
982 return r->get_tree() != error_mark_node;
985 // Create a placeholder for an array type.
987 Btype*
988 Gcc_backend::placeholder_array_type(const std::string& name,
989 Location location)
991 tree ret = make_node(ARRAY_TYPE);
992 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
993 get_identifier_from_string(name),
994 ret);
995 TYPE_NAME(ret) = decl;
996 return this->make_type(ret);
999 // Fill in the fields of a placeholder array type.
1001 bool
1002 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1003 Btype* element_btype,
1004 Bexpression* length)
1006 tree t = placeholder->get_tree();
1007 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1008 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1010 // Build the data structure gcc wants to see for a typedef.
1011 tree copy = build_distinct_type_copy(t);
1012 TYPE_NAME(copy) = NULL_TREE;
1013 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1015 return r->get_tree() != error_mark_node;
1018 // Return a named version of a type.
1020 Btype*
1021 Gcc_backend::named_type(const std::string& name, Btype* btype,
1022 Location location)
1024 tree type = btype->get_tree();
1025 if (type == error_mark_node)
1026 return this->error_type();
1028 // The middle-end expects a basic type to have a name. In Go every
1029 // basic type will have a name. The first time we see a basic type,
1030 // give it whatever Go name we have at this point.
1031 if (TYPE_NAME(type) == NULL_TREE
1032 && location.gcc_location() == BUILTINS_LOCATION
1033 && (TREE_CODE(type) == INTEGER_TYPE
1034 || TREE_CODE(type) == REAL_TYPE
1035 || TREE_CODE(type) == COMPLEX_TYPE
1036 || TREE_CODE(type) == BOOLEAN_TYPE))
1038 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1039 get_identifier_from_string(name),
1040 type);
1041 TYPE_NAME(type) = decl;
1042 return this->make_type(type);
1045 tree copy = build_variant_type_copy(type);
1046 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1047 get_identifier_from_string(name),
1048 copy);
1049 DECL_ORIGINAL_TYPE(decl) = type;
1050 TYPE_NAME(copy) = decl;
1051 return this->make_type(copy);
1054 // Return a pointer type used as a marker for a circular type.
1056 Btype*
1057 Gcc_backend::circular_pointer_type(Btype*, bool)
1059 return this->make_type(ptr_type_node);
1062 // Return whether we might be looking at a circular type.
1064 bool
1065 Gcc_backend::is_circular_pointer_type(Btype* btype)
1067 return btype->get_tree() == ptr_type_node;
1070 // Return the size of a type.
1072 size_t
1073 Gcc_backend::type_size(Btype* btype)
1075 tree t = btype->get_tree();
1076 if (t == error_mark_node)
1077 return 1;
1078 t = TYPE_SIZE_UNIT(t);
1079 gcc_assert(tree_fits_uhwi_p (t));
1080 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1081 size_t ret = static_cast<size_t>(val_wide);
1082 gcc_assert(ret == val_wide);
1083 return ret;
1086 // Return the alignment of a type.
1088 size_t
1089 Gcc_backend::type_alignment(Btype* btype)
1091 tree t = btype->get_tree();
1092 if (t == error_mark_node)
1093 return 1;
1094 return TYPE_ALIGN_UNIT(t);
1097 // Return the alignment of a struct field of type BTYPE.
1099 size_t
1100 Gcc_backend::type_field_alignment(Btype* btype)
1102 tree t = btype->get_tree();
1103 if (t == error_mark_node)
1104 return 1;
1105 return go_field_alignment(t);
1108 // Return the offset of a field in a struct.
1110 size_t
1111 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1113 tree struct_tree = btype->get_tree();
1114 if (struct_tree == error_mark_node)
1115 return 0;
1116 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1117 tree field = TYPE_FIELDS(struct_tree);
1118 for (; index > 0; --index)
1120 field = DECL_CHAIN(field);
1121 gcc_assert(field != NULL_TREE);
1123 HOST_WIDE_INT offset_wide = int_byte_position(field);
1124 gcc_assert(offset_wide >= 0);
1125 size_t ret = static_cast<size_t>(offset_wide);
1126 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
1127 return ret;
1130 // Return the zero value for a type.
1132 Bexpression*
1133 Gcc_backend::zero_expression(Btype* btype)
1135 tree t = btype->get_tree();
1136 tree ret;
1137 if (t == error_mark_node)
1138 ret = error_mark_node;
1139 else
1140 ret = build_zero_cst(t);
1141 return this->make_expression(ret);
1144 // An expression that references a variable.
1146 Bexpression*
1147 Gcc_backend::var_expression(Bvariable* var, Location)
1149 tree ret = var->get_tree();
1150 if (ret == error_mark_node)
1151 return this->error_expression();
1152 return this->make_expression(ret);
1155 // An expression that indirectly references an expression.
1157 Bexpression*
1158 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1159 bool known_valid, Location location)
1161 tree expr_tree = expr->get_tree();
1162 tree type_tree = btype->get_tree();
1163 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1164 return this->error_expression();
1166 // If the type of EXPR is a recursive pointer type, then we
1167 // need to insert a cast before indirecting.
1168 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1169 if (VOID_TYPE_P(target_type_tree))
1170 expr_tree = fold_convert_loc(location.gcc_location(),
1171 build_pointer_type(type_tree), expr_tree);
1173 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1174 expr_tree);
1175 if (known_valid)
1176 TREE_THIS_NOTRAP(ret) = 1;
1177 return this->make_expression(ret);
1180 // Return an expression that declares a constant named NAME with the
1181 // constant value VAL in BTYPE.
1183 Bexpression*
1184 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1185 Bexpression* val, Location location)
1187 tree type_tree = btype->get_tree();
1188 tree const_val = val->get_tree();
1189 if (type_tree == error_mark_node || const_val == error_mark_node)
1190 return this->error_expression();
1192 tree name_tree = get_identifier_from_string(name);
1193 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1194 type_tree);
1195 DECL_INITIAL(decl) = const_val;
1196 TREE_CONSTANT(decl) = 1;
1197 TREE_READONLY(decl) = 1;
1199 go_preserve_from_gc(decl);
1200 return this->make_expression(decl);
1203 // Return a typed value as a constant integer.
1205 Bexpression*
1206 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1208 tree t = btype->get_tree();
1209 if (t == error_mark_node)
1210 return this->error_expression();
1212 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1213 return this->make_expression(ret);
1216 // Return a typed value as a constant floating-point number.
1218 Bexpression*
1219 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1221 tree t = btype->get_tree();
1222 tree ret;
1223 if (t == error_mark_node)
1224 return this->error_expression();
1226 REAL_VALUE_TYPE r1;
1227 real_from_mpfr(&r1, val, t, GMP_RNDN);
1228 REAL_VALUE_TYPE r2;
1229 real_convert(&r2, TYPE_MODE(t), &r1);
1230 ret = build_real(t, r2);
1231 return this->make_expression(ret);
1234 // Return a typed real and imaginary value as a constant complex number.
1236 Bexpression*
1237 Gcc_backend::complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag)
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, real, TREE_TYPE(t), GMP_RNDN);
1246 REAL_VALUE_TYPE r2;
1247 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1249 REAL_VALUE_TYPE r3;
1250 real_from_mpfr(&r3, imag, TREE_TYPE(t), GMP_RNDN);
1251 REAL_VALUE_TYPE r4;
1252 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1254 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1255 build_real(TREE_TYPE(t), r4));
1256 return this->make_expression(ret);
1259 // Make a constant string expression.
1261 Bexpression*
1262 Gcc_backend::string_constant_expression(const std::string& val)
1264 tree index_type = build_index_type(size_int(val.length()));
1265 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1266 TYPE_QUAL_CONST);
1267 tree string_type = build_array_type(const_char_type, index_type);
1268 string_type = build_variant_type_copy(string_type);
1269 TYPE_STRING_FLAG(string_type) = 1;
1270 tree string_val = build_string(val.length(), val.data());
1271 TREE_TYPE(string_val) = string_type;
1273 return this->make_expression(string_val);
1276 // Make a constant boolean expression.
1278 Bexpression*
1279 Gcc_backend::boolean_constant_expression(bool val)
1281 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1282 return this->make_expression(bool_cst);
1285 // Return the real part of a complex expression.
1287 Bexpression*
1288 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1290 tree complex_tree = bcomplex->get_tree();
1291 if (complex_tree == error_mark_node)
1292 return this->error_expression();
1293 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1294 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1295 TREE_TYPE(TREE_TYPE(complex_tree)),
1296 complex_tree);
1297 return this->make_expression(ret);
1300 // Return the imaginary part of a complex expression.
1302 Bexpression*
1303 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1305 tree complex_tree = bcomplex->get_tree();
1306 if (complex_tree == error_mark_node)
1307 return this->error_expression();
1308 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1309 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1310 TREE_TYPE(TREE_TYPE(complex_tree)),
1311 complex_tree);
1312 return this->make_expression(ret);
1315 // Make a complex expression given its real and imaginary parts.
1317 Bexpression*
1318 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1319 Location location)
1321 tree real_tree = breal->get_tree();
1322 tree imag_tree = bimag->get_tree();
1323 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1324 return this->error_expression();
1325 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1326 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1327 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1328 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1329 build_complex_type(TREE_TYPE(real_tree)),
1330 real_tree, imag_tree);
1331 return this->make_expression(ret);
1334 // An expression that converts an expression to a different type.
1336 Bexpression*
1337 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1338 Location location)
1340 tree type_tree = type->get_tree();
1341 tree expr_tree = expr->get_tree();
1342 if (type_tree == error_mark_node
1343 || expr_tree == error_mark_node
1344 || TREE_TYPE(expr_tree) == error_mark_node)
1345 return this->error_expression();
1347 tree ret;
1348 if (this->type_size(type) == 0)
1350 // Do not convert zero-sized types.
1351 ret = expr_tree;
1353 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1354 ret = fold(convert_to_integer(type_tree, expr_tree));
1355 else if (TREE_CODE(type_tree) == REAL_TYPE)
1356 ret = fold(convert_to_real(type_tree, expr_tree));
1357 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1358 ret = fold(convert_to_complex(type_tree, expr_tree));
1359 else if (TREE_CODE(type_tree) == POINTER_TYPE
1360 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1361 ret = fold(convert_to_pointer(type_tree, expr_tree));
1362 else if (TREE_CODE(type_tree) == RECORD_TYPE
1363 || TREE_CODE(type_tree) == ARRAY_TYPE)
1364 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1365 type_tree, expr_tree);
1366 else
1367 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1369 return this->make_expression(ret);
1372 // Get the address of a function.
1374 Bexpression*
1375 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1377 tree func = bfunc->get_tree();
1378 if (func == error_mark_node)
1379 return this->error_expression();
1381 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1382 return this->make_expression(ret);
1385 // Get the address of an expression.
1387 Bexpression*
1388 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1390 tree expr = bexpr->get_tree();
1391 if (expr == error_mark_node)
1392 return this->error_expression();
1394 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1395 return this->make_expression(ret);
1398 // Return an expression for the field at INDEX in BSTRUCT.
1400 Bexpression*
1401 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1402 Location location)
1404 tree struct_tree = bstruct->get_tree();
1405 if (struct_tree == error_mark_node
1406 || TREE_TYPE(struct_tree) == error_mark_node)
1407 return this->error_expression();
1408 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1409 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1410 if (field == NULL_TREE)
1412 // This can happen for a type which refers to itself indirectly
1413 // and then turns out to be erroneous.
1414 return this->error_expression();
1416 for (unsigned int i = index; i > 0; --i)
1418 field = DECL_CHAIN(field);
1419 gcc_assert(field != NULL_TREE);
1421 if (TREE_TYPE(field) == error_mark_node)
1422 return this->error_expression();
1423 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1424 TREE_TYPE(field), struct_tree, field,
1425 NULL_TREE);
1426 if (TREE_CONSTANT(struct_tree))
1427 TREE_CONSTANT(ret) = 1;
1428 return this->make_expression(ret);
1431 // Return an expression that executes BSTAT before BEXPR.
1433 Bexpression*
1434 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1435 Location location)
1437 tree stat = bstat->get_tree();
1438 tree expr = bexpr->get_tree();
1439 if (stat == error_mark_node || expr == error_mark_node)
1440 return this->error_expression();
1441 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1442 TREE_TYPE(expr), stat, expr);
1443 return this->make_expression(ret);
1446 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1447 // ELSE_EXPR otherwise.
1449 Bexpression*
1450 Gcc_backend::conditional_expression(Btype* btype, Bexpression* condition,
1451 Bexpression* then_expr,
1452 Bexpression* else_expr, Location location)
1454 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1455 tree cond_tree = condition->get_tree();
1456 tree then_tree = then_expr->get_tree();
1457 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1458 if (type_tree == error_mark_node
1459 || cond_tree == error_mark_node
1460 || then_tree == error_mark_node
1461 || else_tree == error_mark_node)
1462 return this->error_expression();
1463 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1464 cond_tree, then_tree, else_tree);
1465 return this->make_expression(ret);
1468 // Return an expression for the unary operation OP EXPR.
1470 Bexpression*
1471 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1473 tree expr_tree = expr->get_tree();
1474 if (expr_tree == error_mark_node
1475 || TREE_TYPE(expr_tree) == error_mark_node)
1476 return this->error_expression();
1478 tree type_tree = TREE_TYPE(expr_tree);
1479 enum tree_code code;
1480 switch (op)
1482 case OPERATOR_MINUS:
1484 tree computed_type = excess_precision_type(type_tree);
1485 if (computed_type != NULL_TREE)
1487 expr_tree = convert(computed_type, expr_tree);
1488 type_tree = computed_type;
1490 code = NEGATE_EXPR;
1491 break;
1493 case OPERATOR_NOT:
1494 code = TRUTH_NOT_EXPR;
1495 break;
1496 case OPERATOR_XOR:
1497 code = BIT_NOT_EXPR;
1498 break;
1499 default:
1500 gcc_unreachable();
1501 break;
1504 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1505 expr_tree);
1506 return this->make_expression(ret);
1509 // Convert a gofrontend operator to an equivalent tree_code.
1511 static enum tree_code
1512 operator_to_tree_code(Operator op, tree type)
1514 enum tree_code code;
1515 switch (op)
1517 case OPERATOR_EQEQ:
1518 code = EQ_EXPR;
1519 break;
1520 case OPERATOR_NOTEQ:
1521 code = NE_EXPR;
1522 break;
1523 case OPERATOR_LT:
1524 code = LT_EXPR;
1525 break;
1526 case OPERATOR_LE:
1527 code = LE_EXPR;
1528 break;
1529 case OPERATOR_GT:
1530 code = GT_EXPR;
1531 break;
1532 case OPERATOR_GE:
1533 code = GE_EXPR;
1534 break;
1535 case OPERATOR_OROR:
1536 code = TRUTH_ORIF_EXPR;
1537 break;
1538 case OPERATOR_ANDAND:
1539 code = TRUTH_ANDIF_EXPR;
1540 break;
1541 case OPERATOR_PLUS:
1542 code = PLUS_EXPR;
1543 break;
1544 case OPERATOR_MINUS:
1545 code = MINUS_EXPR;
1546 break;
1547 case OPERATOR_OR:
1548 code = BIT_IOR_EXPR;
1549 break;
1550 case OPERATOR_XOR:
1551 code = BIT_XOR_EXPR;
1552 break;
1553 case OPERATOR_MULT:
1554 code = MULT_EXPR;
1555 break;
1556 case OPERATOR_DIV:
1557 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1558 code = RDIV_EXPR;
1559 else
1560 code = TRUNC_DIV_EXPR;
1561 break;
1562 case OPERATOR_MOD:
1563 code = TRUNC_MOD_EXPR;
1564 break;
1565 case OPERATOR_LSHIFT:
1566 code = LSHIFT_EXPR;
1567 break;
1568 case OPERATOR_RSHIFT:
1569 code = RSHIFT_EXPR;
1570 break;
1571 case OPERATOR_AND:
1572 code = BIT_AND_EXPR;
1573 break;
1574 case OPERATOR_BITCLEAR:
1575 code = BIT_AND_EXPR;
1576 break;
1577 default:
1578 gcc_unreachable();
1581 return code;
1584 // Return an expression for the binary operation LEFT OP RIGHT.
1586 Bexpression*
1587 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1588 Bexpression* right, Location location)
1590 tree left_tree = left->get_tree();
1591 tree right_tree = right->get_tree();
1592 if (left_tree == error_mark_node
1593 || right_tree == error_mark_node)
1594 return this->error_expression();
1595 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1597 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1598 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1599 tree computed_type = excess_precision_type(type_tree);
1600 if (computed_type != NULL_TREE)
1602 left_tree = convert(computed_type, left_tree);
1603 right_tree = convert(computed_type, right_tree);
1604 type_tree = computed_type;
1607 // For comparison operators, the resulting type should be boolean.
1608 switch (op)
1610 case OPERATOR_EQEQ:
1611 case OPERATOR_NOTEQ:
1612 case OPERATOR_LT:
1613 case OPERATOR_LE:
1614 case OPERATOR_GT:
1615 case OPERATOR_GE:
1616 type_tree = boolean_type_node;
1617 break;
1618 default:
1619 break;
1622 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1623 left_tree, right_tree);
1624 return this->make_expression(ret);
1627 // Return an expression that constructs BTYPE with VALS.
1629 Bexpression*
1630 Gcc_backend::constructor_expression(Btype* btype,
1631 const std::vector<Bexpression*>& vals,
1632 Location location)
1634 tree type_tree = btype->get_tree();
1635 if (type_tree == error_mark_node)
1636 return this->error_expression();
1638 vec<constructor_elt, va_gc> *init;
1639 vec_alloc(init, vals.size());
1641 bool is_constant = true;
1642 tree field = TYPE_FIELDS(type_tree);
1643 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1644 p != vals.end();
1645 ++p, field = DECL_CHAIN(field))
1647 gcc_assert(field != NULL_TREE);
1648 tree val = (*p)->get_tree();
1649 if (TREE_TYPE(field) == error_mark_node
1650 || val == error_mark_node
1651 || TREE_TYPE(val) == error_mark_node)
1652 return this->error_expression();
1654 constructor_elt empty = {NULL, NULL};
1655 constructor_elt* elt = init->quick_push(empty);
1656 elt->index = field;
1657 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
1658 val);
1659 if (!TREE_CONSTANT(elt->value))
1660 is_constant = false;
1662 gcc_assert(field == NULL_TREE);
1663 tree ret = build_constructor(type_tree, init);
1664 if (is_constant)
1665 TREE_CONSTANT(ret) = 1;
1667 return this->make_expression(ret);
1670 Bexpression*
1671 Gcc_backend::array_constructor_expression(
1672 Btype* array_btype, const std::vector<unsigned long>& indexes,
1673 const std::vector<Bexpression*>& vals, Location)
1675 tree type_tree = array_btype->get_tree();
1676 if (type_tree == error_mark_node)
1677 return this->error_expression();
1679 gcc_assert(indexes.size() == vals.size());
1680 vec<constructor_elt, va_gc> *init;
1681 vec_alloc(init, vals.size());
1683 bool is_constant = true;
1684 for (size_t i = 0; i < vals.size(); ++i)
1686 tree index = size_int(indexes[i]);
1687 tree val = (vals[i])->get_tree();
1689 if (index == error_mark_node
1690 || val == error_mark_node)
1691 return this->error_expression();
1693 if (!TREE_CONSTANT(val))
1694 is_constant = false;
1696 constructor_elt empty = {NULL, NULL};
1697 constructor_elt* elt = init->quick_push(empty);
1698 elt->index = index;
1699 elt->value = val;
1702 tree ret = build_constructor(type_tree, init);
1703 if (is_constant)
1704 TREE_CONSTANT(ret) = 1;
1705 return this->make_expression(ret);
1708 // Return an expression for the address of BASE[INDEX].
1710 Bexpression*
1711 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1712 Location location)
1714 tree base_tree = base->get_tree();
1715 tree index_tree = index->get_tree();
1716 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1717 if (base_tree == error_mark_node
1718 || TREE_TYPE(base_tree) == error_mark_node
1719 || index_tree == error_mark_node
1720 || element_type_tree == error_mark_node)
1721 return this->error_expression();
1723 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1724 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1725 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1726 index_tree, element_size);
1727 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1728 TREE_TYPE(base_tree), base_tree, offset);
1729 return this->make_expression(ptr);
1732 // Return an expression representing ARRAY[INDEX]
1734 Bexpression*
1735 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1736 Location location)
1738 tree array_tree = array->get_tree();
1739 tree index_tree = index->get_tree();
1740 if (array_tree == error_mark_node
1741 || TREE_TYPE(array_tree) == error_mark_node
1742 || index_tree == error_mark_node)
1743 return this->error_expression();
1745 tree ret = build4_loc(location.gcc_location(), ARRAY_REF,
1746 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1747 index_tree, NULL_TREE, NULL_TREE);
1748 return this->make_expression(ret);
1751 // Create an expression for a call to FN_EXPR with FN_ARGS.
1752 Bexpression*
1753 Gcc_backend::call_expression(Bexpression* fn_expr,
1754 const std::vector<Bexpression*>& fn_args,
1755 Location location)
1757 tree fn = fn_expr->get_tree();
1758 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1759 return this->error_expression();
1761 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1762 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1764 size_t nargs = fn_args.size();
1765 tree* args = nargs == 0 ? NULL : new tree[nargs];
1766 for (size_t i = 0; i < nargs; ++i)
1768 args[i] = fn_args.at(i)->get_tree();
1769 if (args[i] == error_mark_node)
1770 return this->error_expression();
1773 tree fndecl = fn;
1774 if (TREE_CODE(fndecl) == ADDR_EXPR)
1775 fndecl = TREE_OPERAND(fndecl, 0);
1777 // This is to support builtin math functions when using 80387 math.
1778 tree excess_type = NULL_TREE;
1779 if (optimize
1780 && TREE_CODE(fndecl) == FUNCTION_DECL
1781 && DECL_IS_BUILTIN(fndecl)
1782 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
1783 && nargs > 0
1784 && ((SCALAR_FLOAT_TYPE_P(rettype)
1785 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1786 || (COMPLEX_FLOAT_TYPE_P(rettype)
1787 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1789 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1790 if (excess_type != NULL_TREE)
1792 tree excess_fndecl = mathfn_built_in(excess_type,
1793 DECL_FUNCTION_CODE(fndecl));
1794 if (excess_fndecl == NULL_TREE)
1795 excess_type = NULL_TREE;
1796 else
1798 fn = build_fold_addr_expr_loc(location.gcc_location(),
1799 excess_fndecl);
1800 for (size_t i = 0; i < nargs; ++i)
1802 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1803 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1804 args[i] = ::convert(excess_type, args[i]);
1810 tree ret =
1811 build_call_array_loc(location.gcc_location(),
1812 excess_type != NULL_TREE ? excess_type : rettype,
1813 fn, nargs, args);
1815 if (excess_type != NULL_TREE)
1817 // Calling convert here can undo our excess precision change.
1818 // That may or may not be a bug in convert_to_real.
1819 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1822 delete[] args;
1823 return this->make_expression(ret);
1826 // An expression as a statement.
1828 Bstatement*
1829 Gcc_backend::expression_statement(Bexpression* expr)
1831 return this->make_statement(expr->get_tree());
1834 // Variable initialization.
1836 Bstatement*
1837 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1839 tree var_tree = var->get_tree();
1840 tree init_tree = init->get_tree();
1841 if (var_tree == error_mark_node || init_tree == error_mark_node)
1842 return this->error_statement();
1843 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1845 // To avoid problems with GNU ld, we don't make zero-sized
1846 // externally visible variables. That might lead us to doing an
1847 // initialization of a zero-sized expression to a non-zero sized
1848 // variable, or vice-versa. Avoid crashes by omitting the
1849 // initializer. Such initializations don't mean anything anyhow.
1850 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1851 && init_tree != NULL_TREE
1852 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1854 DECL_INITIAL(var_tree) = init_tree;
1855 init_tree = NULL_TREE;
1858 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1859 void_type_node, var_tree);
1860 if (init_tree != NULL_TREE)
1861 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1862 void_type_node, init_tree, ret);
1864 return this->make_statement(ret);
1867 // Assignment.
1869 Bstatement*
1870 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1871 Location location)
1873 tree lhs_tree = lhs->get_tree();
1874 tree rhs_tree = rhs->get_tree();
1875 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1876 return this->error_statement();
1878 // To avoid problems with GNU ld, we don't make zero-sized
1879 // externally visible variables. That might lead us to doing an
1880 // assignment of a zero-sized expression to a non-zero sized
1881 // expression; avoid crashes here by avoiding assignments of
1882 // zero-sized expressions. Such assignments don't really mean
1883 // anything anyhow.
1884 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1885 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1886 return this->compound_statement(this->expression_statement(lhs),
1887 this->expression_statement(rhs));
1889 // Sometimes the same unnamed Go type can be created multiple times
1890 // and thus have multiple tree representations. Make sure this does
1891 // not confuse the middle-end.
1892 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1894 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1895 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1896 if (POINTER_TYPE_P(lhs_type_tree)
1897 || INTEGRAL_TYPE_P(lhs_type_tree)
1898 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1899 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1900 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1901 rhs_tree);
1902 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1903 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1905 gcc_assert(int_size_in_bytes(lhs_type_tree)
1906 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1907 rhs_tree = fold_build1_loc(location.gcc_location(),
1908 VIEW_CONVERT_EXPR,
1909 lhs_type_tree, rhs_tree);
1913 return this->make_statement(fold_build2_loc(location.gcc_location(),
1914 MODIFY_EXPR,
1915 void_type_node,
1916 lhs_tree, rhs_tree));
1919 // Return.
1921 Bstatement*
1922 Gcc_backend::return_statement(Bfunction* bfunction,
1923 const std::vector<Bexpression*>& vals,
1924 Location location)
1926 tree fntree = bfunction->get_tree();
1927 if (fntree == error_mark_node)
1928 return this->error_statement();
1929 tree result = DECL_RESULT(fntree);
1930 if (result == error_mark_node)
1931 return this->error_statement();
1933 tree ret;
1934 if (vals.empty())
1935 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
1936 NULL_TREE);
1937 else if (vals.size() == 1)
1939 tree val = vals.front()->get_tree();
1940 if (val == error_mark_node)
1941 return this->error_statement();
1942 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1943 void_type_node, result,
1944 vals.front()->get_tree());
1945 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1946 void_type_node, set);
1948 else
1950 // To return multiple values, copy the values into a temporary
1951 // variable of the right structure type, and then assign the
1952 // temporary variable to the DECL_RESULT in the return
1953 // statement.
1954 tree stmt_list = NULL_TREE;
1955 tree rettype = TREE_TYPE(result);
1957 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
1958 push_struct_function(fntree);
1959 else
1960 push_cfun(DECL_STRUCT_FUNCTION(fntree));
1961 tree rettmp = create_tmp_var(rettype, "RESULT");
1962 pop_cfun();
1964 tree field = TYPE_FIELDS(rettype);
1965 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1966 p != vals.end();
1967 p++, field = DECL_CHAIN(field))
1969 gcc_assert(field != NULL_TREE);
1970 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1971 TREE_TYPE(field), rettmp, field,
1972 NULL_TREE);
1973 tree val = (*p)->get_tree();
1974 if (val == error_mark_node)
1975 return this->error_statement();
1976 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1977 void_type_node,
1978 ref, (*p)->get_tree());
1979 append_to_statement_list(set, &stmt_list);
1981 gcc_assert(field == NULL_TREE);
1982 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1983 void_type_node,
1984 result, rettmp);
1985 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1986 void_type_node, set);
1987 append_to_statement_list(ret_expr, &stmt_list);
1988 ret = stmt_list;
1990 return this->make_statement(ret);
1993 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
1994 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
1995 // NULL, it will always be executed. This is used for handling defers in Go
1996 // functions. In C++, the resulting code is of this form:
1997 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
1999 Bstatement*
2000 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2001 Bstatement* except_stmt,
2002 Bstatement* finally_stmt,
2003 Location location)
2005 tree stat_tree = bstat->get_tree();
2006 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2007 tree finally_tree = finally_stmt == NULL
2008 ? NULL_TREE
2009 : finally_stmt->get_tree();
2011 if (stat_tree == error_mark_node
2012 || except_tree == error_mark_node
2013 || finally_tree == error_mark_node)
2014 return this->error_statement();
2016 if (except_tree != NULL_TREE)
2017 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2018 void_type_node, stat_tree,
2019 build2_loc(location.gcc_location(), CATCH_EXPR,
2020 void_type_node, NULL, except_tree));
2021 if (finally_tree != NULL_TREE)
2022 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2023 void_type_node, stat_tree, finally_tree);
2024 return this->make_statement(stat_tree);
2027 // If.
2029 Bstatement*
2030 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
2031 Bblock* else_block, Location location)
2033 tree cond_tree = condition->get_tree();
2034 tree then_tree = then_block->get_tree();
2035 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2036 if (cond_tree == error_mark_node
2037 || then_tree == error_mark_node
2038 || else_tree == error_mark_node)
2039 return this->error_statement();
2040 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2041 cond_tree, then_tree, else_tree);
2042 return this->make_statement(ret);
2045 // Switch.
2047 Bstatement*
2048 Gcc_backend::switch_statement(
2049 Bfunction* function,
2050 Bexpression* value,
2051 const std::vector<std::vector<Bexpression*> >& cases,
2052 const std::vector<Bstatement*>& statements,
2053 Location switch_location)
2055 gcc_assert(cases.size() == statements.size());
2057 tree decl = function->get_tree();
2058 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2059 push_struct_function(decl);
2060 else
2061 push_cfun(DECL_STRUCT_FUNCTION(decl));
2063 tree stmt_list = NULL_TREE;
2064 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2065 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2066 ps != statements.end();
2067 ++ps, ++pc)
2069 if (pc->empty())
2071 source_location loc = (*ps != NULL
2072 ? EXPR_LOCATION((*ps)->get_tree())
2073 : UNKNOWN_LOCATION);
2074 tree label = create_artificial_label(loc);
2075 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2076 append_to_statement_list(c, &stmt_list);
2078 else
2080 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2081 pcv != pc->end();
2082 ++pcv)
2084 tree t = (*pcv)->get_tree();
2085 if (t == error_mark_node)
2086 return this->error_statement();
2087 source_location loc = EXPR_LOCATION(t);
2088 tree label = create_artificial_label(loc);
2089 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2090 append_to_statement_list(c, &stmt_list);
2094 if (*ps != NULL)
2096 tree t = (*ps)->get_tree();
2097 if (t == error_mark_node)
2098 return this->error_statement();
2099 append_to_statement_list(t, &stmt_list);
2102 pop_cfun();
2104 tree tv = value->get_tree();
2105 if (tv == error_mark_node)
2106 return this->error_statement();
2107 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
2108 NULL_TREE, tv, stmt_list, NULL_TREE);
2109 return this->make_statement(t);
2112 // Pair of statements.
2114 Bstatement*
2115 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2117 tree stmt_list = NULL_TREE;
2118 tree t = s1->get_tree();
2119 if (t == error_mark_node)
2120 return this->error_statement();
2121 append_to_statement_list(t, &stmt_list);
2122 t = s2->get_tree();
2123 if (t == error_mark_node)
2124 return this->error_statement();
2125 append_to_statement_list(t, &stmt_list);
2126 return this->make_statement(stmt_list);
2129 // List of statements.
2131 Bstatement*
2132 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2134 tree stmt_list = NULL_TREE;
2135 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2136 p != statements.end();
2137 ++p)
2139 tree t = (*p)->get_tree();
2140 if (t == error_mark_node)
2141 return this->error_statement();
2142 append_to_statement_list(t, &stmt_list);
2144 return this->make_statement(stmt_list);
2147 // Make a block. For some reason gcc uses a dual structure for
2148 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2149 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2150 // the Bblock.
2152 Bblock*
2153 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2154 const std::vector<Bvariable*>& vars,
2155 Location start_location,
2156 Location)
2158 tree block_tree = make_node(BLOCK);
2159 if (enclosing == NULL)
2161 tree fndecl = function->get_tree();
2162 gcc_assert(fndecl != NULL_TREE);
2164 // We may have already created a block for local variables when
2165 // we take the address of a parameter.
2166 if (DECL_INITIAL(fndecl) == NULL_TREE)
2168 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2169 DECL_INITIAL(fndecl) = block_tree;
2171 else
2173 tree superblock_tree = DECL_INITIAL(fndecl);
2174 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2175 tree* pp;
2176 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2177 *pp != NULL_TREE;
2178 pp = &BLOCK_CHAIN(*pp))
2180 *pp = block_tree;
2183 else
2185 tree superbind_tree = enclosing->get_tree();
2186 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2187 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2189 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2190 tree* pp;
2191 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2192 *pp != NULL_TREE;
2193 pp = &BLOCK_CHAIN(*pp))
2195 *pp = block_tree;
2198 tree* pp = &BLOCK_VARS(block_tree);
2199 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2200 pv != vars.end();
2201 ++pv)
2203 *pp = (*pv)->get_tree();
2204 if (*pp != error_mark_node)
2205 pp = &DECL_CHAIN(*pp);
2207 *pp = NULL_TREE;
2209 TREE_USED(block_tree) = 1;
2211 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2212 void_type_node, BLOCK_VARS(block_tree),
2213 NULL_TREE, block_tree);
2214 TREE_SIDE_EFFECTS(bind_tree) = 1;
2215 return new Bblock(bind_tree);
2218 // Add statements to a block.
2220 void
2221 Gcc_backend::block_add_statements(Bblock* bblock,
2222 const std::vector<Bstatement*>& statements)
2224 tree stmt_list = NULL_TREE;
2225 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2226 p != statements.end();
2227 ++p)
2229 tree s = (*p)->get_tree();
2230 if (s != error_mark_node)
2231 append_to_statement_list(s, &stmt_list);
2234 tree bind_tree = bblock->get_tree();
2235 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2236 BIND_EXPR_BODY(bind_tree) = stmt_list;
2239 // Return a block as a statement.
2241 Bstatement*
2242 Gcc_backend::block_statement(Bblock* bblock)
2244 tree bind_tree = bblock->get_tree();
2245 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2246 return this->make_statement(bind_tree);
2249 // This is not static because we declare it with GTY(()) in go-c.h.
2250 tree go_non_zero_struct;
2252 // Return a type corresponding to TYPE with non-zero size.
2254 tree
2255 Gcc_backend::non_zero_size_type(tree type)
2257 if (int_size_in_bytes(type) != 0)
2258 return type;
2260 switch (TREE_CODE(type))
2262 case RECORD_TYPE:
2263 if (TYPE_FIELDS(type) != NULL_TREE)
2265 tree ns = make_node(RECORD_TYPE);
2266 tree field_trees = NULL_TREE;
2267 tree *pp = &field_trees;
2268 for (tree field = TYPE_FIELDS(type);
2269 field != NULL_TREE;
2270 field = DECL_CHAIN(field))
2272 tree ft = TREE_TYPE(field);
2273 if (field == TYPE_FIELDS(type))
2274 ft = non_zero_size_type(ft);
2275 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2276 DECL_NAME(field), ft);
2277 DECL_CONTEXT(f) = ns;
2278 *pp = f;
2279 pp = &DECL_CHAIN(f);
2281 TYPE_FIELDS(ns) = field_trees;
2282 layout_type(ns);
2283 return ns;
2286 if (go_non_zero_struct == NULL_TREE)
2288 type = make_node(RECORD_TYPE);
2289 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2290 get_identifier("dummy"),
2291 boolean_type_node);
2292 DECL_CONTEXT(field) = type;
2293 TYPE_FIELDS(type) = field;
2294 layout_type(type);
2295 go_non_zero_struct = type;
2297 return go_non_zero_struct;
2299 case ARRAY_TYPE:
2301 tree element_type = non_zero_size_type(TREE_TYPE(type));
2302 return build_array_type_nelts(element_type, 1);
2305 default:
2306 gcc_unreachable();
2309 gcc_unreachable();
2312 // Make a global variable.
2314 Bvariable*
2315 Gcc_backend::global_variable(const std::string& package_name,
2316 const std::string& pkgpath,
2317 const std::string& name,
2318 Btype* btype,
2319 bool is_external,
2320 bool is_hidden,
2321 bool in_unique_section,
2322 Location location)
2324 tree type_tree = btype->get_tree();
2325 if (type_tree == error_mark_node)
2326 return this->error_variable();
2328 // The GNU linker does not like dynamic variables with zero size.
2329 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2330 type_tree = this->non_zero_size_type(type_tree);
2332 std::string var_name(package_name);
2333 var_name.push_back('.');
2334 var_name.append(name);
2335 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2336 get_identifier_from_string(var_name),
2337 type_tree);
2338 if (is_external)
2339 DECL_EXTERNAL(decl) = 1;
2340 else
2341 TREE_STATIC(decl) = 1;
2342 if (!is_hidden)
2344 TREE_PUBLIC(decl) = 1;
2346 std::string asm_name(pkgpath);
2347 asm_name.push_back('.');
2348 asm_name.append(name);
2349 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2351 TREE_USED(decl) = 1;
2353 if (in_unique_section)
2354 resolve_unique_section (decl, 0, 1);
2356 go_preserve_from_gc(decl);
2358 return new Bvariable(decl);
2361 // Set the initial value of a global variable.
2363 void
2364 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2366 tree expr_tree = expr->get_tree();
2367 if (expr_tree == error_mark_node)
2368 return;
2369 gcc_assert(TREE_CONSTANT(expr_tree));
2370 tree var_decl = var->get_tree();
2371 if (var_decl == error_mark_node)
2372 return;
2373 DECL_INITIAL(var_decl) = expr_tree;
2375 // If this variable goes in a unique section, it may need to go into
2376 // a different one now that DECL_INITIAL is set.
2377 if (symtab_node::get(var_decl)
2378 && symtab_node::get(var_decl)->implicit_section)
2380 set_decl_section_name (var_decl, NULL);
2381 resolve_unique_section (var_decl,
2382 compute_reloc_for_constant (expr_tree),
2387 // Make a local variable.
2389 Bvariable*
2390 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2391 Btype* btype, bool is_address_taken,
2392 Location location)
2394 tree type_tree = btype->get_tree();
2395 if (type_tree == error_mark_node)
2396 return this->error_variable();
2397 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2398 get_identifier_from_string(name),
2399 type_tree);
2400 DECL_CONTEXT(decl) = function->get_tree();
2401 TREE_USED(decl) = 1;
2402 if (is_address_taken)
2403 TREE_ADDRESSABLE(decl) = 1;
2404 go_preserve_from_gc(decl);
2405 return new Bvariable(decl);
2408 // Make a function parameter variable.
2410 Bvariable*
2411 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2412 Btype* btype, bool is_address_taken,
2413 Location location)
2415 tree type_tree = btype->get_tree();
2416 if (type_tree == error_mark_node)
2417 return this->error_variable();
2418 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2419 get_identifier_from_string(name),
2420 type_tree);
2421 DECL_CONTEXT(decl) = function->get_tree();
2422 DECL_ARG_TYPE(decl) = type_tree;
2423 TREE_USED(decl) = 1;
2424 if (is_address_taken)
2425 TREE_ADDRESSABLE(decl) = 1;
2426 go_preserve_from_gc(decl);
2427 return new Bvariable(decl);
2430 // Make a temporary variable.
2432 Bvariable*
2433 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2434 Btype* btype, Bexpression* binit,
2435 bool is_address_taken,
2436 Location location,
2437 Bstatement** pstatement)
2439 gcc_assert(function != NULL);
2440 tree decl = function->get_tree();
2441 tree type_tree = btype->get_tree();
2442 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2443 if (type_tree == error_mark_node
2444 || init_tree == error_mark_node
2445 || decl == error_mark_node)
2447 *pstatement = this->error_statement();
2448 return this->error_variable();
2451 tree var;
2452 // We can only use create_tmp_var if the type is not addressable.
2453 if (!TREE_ADDRESSABLE(type_tree))
2455 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2456 push_struct_function(decl);
2457 else
2458 push_cfun(DECL_STRUCT_FUNCTION(decl));
2460 var = create_tmp_var(type_tree, "GOTMP");
2461 pop_cfun();
2463 else
2465 gcc_assert(bblock != NULL);
2466 var = build_decl(location.gcc_location(), VAR_DECL,
2467 create_tmp_var_name("GOTMP"),
2468 type_tree);
2469 DECL_ARTIFICIAL(var) = 1;
2470 DECL_IGNORED_P(var) = 1;
2471 TREE_USED(var) = 1;
2472 DECL_CONTEXT(var) = decl;
2474 // We have to add this variable to the BLOCK and the BIND_EXPR.
2475 tree bind_tree = bblock->get_tree();
2476 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2477 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2478 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2479 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2480 BLOCK_VARS(block_tree) = var;
2481 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2484 if (init_tree != NULL_TREE)
2485 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
2486 init_tree);
2488 if (is_address_taken)
2489 TREE_ADDRESSABLE(var) = 1;
2491 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2492 DECL_EXPR,
2493 void_type_node, var));
2494 return new Bvariable(var);
2497 // Create an implicit variable that is compiler-defined. This is used when
2498 // generating GC root variables and storing the values of a slice initializer.
2500 Bvariable*
2501 Gcc_backend::implicit_variable(const std::string& name, Btype* type,
2502 Bexpression* init, bool is_constant,
2503 bool is_common, size_t alignment)
2505 tree type_tree = type->get_tree();
2506 tree init_tree;
2507 if (init == NULL)
2508 init_tree = NULL_TREE;
2509 else
2510 init_tree = init->get_tree();
2511 if (type_tree == error_mark_node || init_tree == error_mark_node)
2512 return this->error_variable();
2514 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2515 get_identifier_from_string(name), type_tree);
2516 DECL_EXTERNAL(decl) = 0;
2517 TREE_PUBLIC(decl) = 0;
2518 TREE_STATIC(decl) = 1;
2519 DECL_ARTIFICIAL(decl) = 1;
2520 if (is_common)
2522 DECL_COMMON(decl) = 1;
2523 TREE_PUBLIC(decl) = 1;
2524 gcc_assert(init_tree == NULL_TREE);
2526 else if (is_constant)
2528 TREE_READONLY(decl) = 1;
2529 TREE_CONSTANT(decl) = 1;
2531 DECL_INITIAL(decl) = init_tree;
2533 if (alignment != 0)
2535 DECL_ALIGN(decl) = alignment * BITS_PER_UNIT;
2536 DECL_USER_ALIGN(decl) = 1;
2539 rest_of_decl_compilation(decl, 1, 0);
2541 return new Bvariable(decl);
2544 // Create a named immutable initialized data structure.
2546 Bvariable*
2547 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
2548 bool is_common, Btype* btype, Location location)
2550 tree type_tree = btype->get_tree();
2551 if (type_tree == error_mark_node)
2552 return this->error_variable();
2553 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2554 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2555 get_identifier_from_string(name),
2556 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2557 TREE_STATIC(decl) = 1;
2558 TREE_USED(decl) = 1;
2559 TREE_READONLY(decl) = 1;
2560 TREE_CONSTANT(decl) = 1;
2561 DECL_ARTIFICIAL(decl) = 1;
2562 if (!is_hidden)
2563 TREE_PUBLIC(decl) = 1;
2565 // When the initializer for one immutable_struct refers to another,
2566 // it needs to know the visibility of the referenced struct so that
2567 // compute_reloc_for_constant will return the right value. On many
2568 // systems calling make_decl_one_only will mark the decl as weak,
2569 // which will change the return value of compute_reloc_for_constant.
2570 // We can't reliably call make_decl_one_only yet, because we don't
2571 // yet know the initializer. This issue doesn't arise in C because
2572 // Go initializers, unlike C initializers, can be indirectly
2573 // recursive. To ensure that compute_reloc_for_constant computes
2574 // the right value if some other initializer refers to this one, we
2575 // mark this symbol as weak here. We undo that below in
2576 // immutable_struct_set_init before calling mark_decl_one_only.
2577 if (is_common)
2578 DECL_WEAK(decl) = 1;
2580 // We don't call rest_of_decl_compilation until we have the
2581 // initializer.
2583 go_preserve_from_gc(decl);
2584 return new Bvariable(decl);
2587 // Set the initializer for a variable created by immutable_struct.
2588 // This is where we finish compiling the variable.
2590 void
2591 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2592 bool, bool is_common, Btype*, Location,
2593 Bexpression* initializer)
2595 tree decl = var->get_tree();
2596 tree init_tree = initializer->get_tree();
2597 if (decl == error_mark_node || init_tree == error_mark_node)
2598 return;
2600 DECL_INITIAL(decl) = init_tree;
2602 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2603 // See the comment where DECL_WEAK is set in immutable_struct.
2604 if (is_common)
2606 DECL_WEAK(decl) = 0;
2607 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2610 // These variables are often unneeded in the final program, so put
2611 // them in their own section so that linker GC can discard them.
2612 resolve_unique_section(decl,
2613 compute_reloc_for_constant (init_tree),
2616 rest_of_decl_compilation(decl, 1, 0);
2619 // Return a reference to an immutable initialized data structure
2620 // defined in another package.
2622 Bvariable*
2623 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
2624 Location location)
2626 tree type_tree = btype->get_tree();
2627 if (type_tree == error_mark_node)
2628 return this->error_variable();
2629 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2630 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2631 get_identifier_from_string(name),
2632 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2633 TREE_READONLY(decl) = 1;
2634 TREE_CONSTANT(decl) = 1;
2635 DECL_ARTIFICIAL(decl) = 1;
2636 TREE_PUBLIC(decl) = 1;
2637 DECL_EXTERNAL(decl) = 1;
2638 go_preserve_from_gc(decl);
2639 return new Bvariable(decl);
2642 // Make a label.
2644 Blabel*
2645 Gcc_backend::label(Bfunction* function, const std::string& name,
2646 Location location)
2648 tree decl;
2649 if (name.empty())
2651 tree func_tree = function->get_tree();
2652 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2653 push_struct_function(func_tree);
2654 else
2655 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2657 decl = create_artificial_label(location.gcc_location());
2659 pop_cfun();
2661 else
2663 tree id = get_identifier_from_string(name);
2664 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
2665 void_type_node);
2666 DECL_CONTEXT(decl) = function->get_tree();
2668 return new Blabel(decl);
2671 // Make a statement which defines a label.
2673 Bstatement*
2674 Gcc_backend::label_definition_statement(Blabel* label)
2676 tree lab = label->get_tree();
2677 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
2678 void_type_node, lab);
2679 return this->make_statement(ret);
2682 // Make a goto statement.
2684 Bstatement*
2685 Gcc_backend::goto_statement(Blabel* label, Location location)
2687 tree lab = label->get_tree();
2688 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
2689 lab);
2690 return this->make_statement(ret);
2693 // Get the address of a label.
2695 Bexpression*
2696 Gcc_backend::label_address(Blabel* label, Location location)
2698 tree lab = label->get_tree();
2699 TREE_USED(lab) = 1;
2700 TREE_ADDRESSABLE(lab) = 1;
2701 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
2702 build_fold_addr_expr_loc(location.gcc_location(),
2703 lab));
2704 return this->make_expression(ret);
2707 // Declare or define a new function.
2709 Bfunction*
2710 Gcc_backend::function(Btype* fntype, const std::string& name,
2711 const std::string& asm_name, bool is_visible,
2712 bool is_declaration, bool is_inlinable,
2713 bool disable_split_stack, bool in_unique_section,
2714 Location location)
2716 tree functype = fntype->get_tree();
2717 if (functype != error_mark_node)
2719 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
2720 functype = TREE_TYPE(functype);
2722 tree id = get_identifier_from_string(name);
2723 if (functype == error_mark_node || id == error_mark_node)
2724 return this->error_function();
2726 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
2727 if (!asm_name.empty())
2728 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2729 if (is_visible)
2730 TREE_PUBLIC(decl) = 1;
2731 if (is_declaration)
2732 DECL_EXTERNAL(decl) = 1;
2733 else
2735 tree restype = TREE_TYPE(functype);
2736 tree resdecl =
2737 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
2738 DECL_ARTIFICIAL(resdecl) = 1;
2739 DECL_IGNORED_P(resdecl) = 1;
2740 DECL_CONTEXT(resdecl) = decl;
2741 DECL_RESULT(decl) = resdecl;
2743 if (!is_inlinable)
2744 DECL_UNINLINABLE(decl) = 1;
2745 if (disable_split_stack)
2747 tree attr = get_identifier("__no_split_stack__");
2748 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
2750 if (in_unique_section)
2751 resolve_unique_section(decl, 0, 1);
2753 go_preserve_from_gc(decl);
2754 return new Bfunction(decl);
2757 // Create a statement that runs all deferred calls for FUNCTION. This should
2758 // be a statement that looks like this in C++:
2759 // finish:
2760 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2762 Bstatement*
2763 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
2764 Bexpression* defer, Location location)
2766 tree undefer_tree = undefer->get_tree();
2767 tree defer_tree = defer->get_tree();
2768 tree fntree = function->get_tree();
2770 if (undefer_tree == error_mark_node
2771 || defer_tree == error_mark_node
2772 || fntree == error_mark_node)
2773 return this->error_statement();
2775 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2776 push_struct_function(fntree);
2777 else
2778 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2780 tree stmt_list = NULL;
2781 Blabel* blabel = this->label(function, "", location);
2782 Bstatement* label_def = this->label_definition_statement(blabel);
2783 append_to_statement_list(label_def->get_tree(), &stmt_list);
2785 Bstatement* jump_stmt = this->goto_statement(blabel, location);
2786 tree jump = jump_stmt->get_tree();
2787 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
2788 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
2789 tree try_catch =
2790 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
2791 append_to_statement_list(try_catch, &stmt_list);
2792 pop_cfun();
2794 return this->make_statement(stmt_list);
2797 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2798 // This will only be called for a function definition.
2800 bool
2801 Gcc_backend::function_set_parameters(Bfunction* function,
2802 const std::vector<Bvariable*>& param_vars)
2804 tree func_tree = function->get_tree();
2805 if (func_tree == error_mark_node)
2806 return false;
2808 tree params = NULL_TREE;
2809 tree *pp = &params;
2810 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
2811 pv != param_vars.end();
2812 ++pv)
2814 *pp = (*pv)->get_tree();
2815 gcc_assert(*pp != error_mark_node);
2816 pp = &DECL_CHAIN(*pp);
2818 *pp = NULL_TREE;
2819 DECL_ARGUMENTS(func_tree) = params;
2820 return true;
2823 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2825 bool
2826 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
2828 tree func_tree = function->get_tree();
2829 tree code = code_stmt->get_tree();
2831 if (func_tree == error_mark_node || code == error_mark_node)
2832 return false;
2833 DECL_SAVED_TREE(func_tree) = code;
2834 return true;
2837 // Look up a named built-in function in the current backend implementation.
2838 // Returns NULL if no built-in function by that name exists.
2840 Bfunction*
2841 Gcc_backend::lookup_builtin(const std::string& name)
2843 if (this->builtin_functions_.count(name) != 0)
2844 return this->builtin_functions_[name];
2845 return NULL;
2848 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
2849 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
2851 void
2852 Gcc_backend::write_global_definitions(
2853 const std::vector<Btype*>& type_decls,
2854 const std::vector<Bexpression*>& constant_decls,
2855 const std::vector<Bfunction*>& function_decls,
2856 const std::vector<Bvariable*>& variable_decls)
2858 size_t count_definitions = type_decls.size() + constant_decls.size()
2859 + function_decls.size() + variable_decls.size();
2861 tree* defs = new tree[count_definitions];
2863 // Convert all non-erroneous declarations into Gimple form.
2864 size_t i = 0;
2865 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
2866 p != variable_decls.end();
2867 ++p)
2869 if ((*p)->get_tree() != error_mark_node)
2871 defs[i] = (*p)->get_tree();
2872 go_preserve_from_gc(defs[i]);
2873 ++i;
2877 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
2878 p != type_decls.end();
2879 ++p)
2881 tree type_tree = (*p)->get_tree();
2882 if (type_tree != error_mark_node
2883 && IS_TYPE_OR_DECL_P(type_tree))
2885 defs[i] = TYPE_NAME(type_tree);
2886 gcc_assert(defs[i] != NULL);
2887 go_preserve_from_gc(defs[i]);
2888 ++i;
2891 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
2892 p != constant_decls.end();
2893 ++p)
2895 if ((*p)->get_tree() != error_mark_node)
2897 defs[i] = (*p)->get_tree();
2898 go_preserve_from_gc(defs[i]);
2899 ++i;
2902 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
2903 p != function_decls.end();
2904 ++p)
2906 tree decl = (*p)->get_tree();
2907 if (decl != error_mark_node)
2909 go_preserve_from_gc(decl);
2910 gimplify_function_tree(decl);
2911 cgraph_finalize_function(decl, true);
2913 defs[i] = decl;
2914 ++i;
2918 // Pass everything back to the middle-end.
2920 wrapup_global_declarations(defs, i);
2922 finalize_compilation_unit();
2924 check_global_declarations(defs, i);
2925 emit_debug_global_declarations(defs, i);
2927 delete[] defs;
2930 // Define a builtin function. BCODE is the builtin function code
2931 // defined by builtins.def. NAME is the name of the builtin function.
2932 // LIBNAME is the name of the corresponding library function, and is
2933 // NULL if there isn't one. FNTYPE is the type of the function.
2934 // CONST_P is true if the function has the const attribute.
2936 void
2937 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
2938 const char* libname, tree fntype, bool const_p)
2940 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
2941 libname, NULL_TREE);
2942 if (const_p)
2943 TREE_READONLY(decl) = 1;
2944 set_builtin_decl(bcode, decl, true);
2945 this->builtin_functions_[name] = this->make_function(decl);
2946 if (libname != NULL)
2948 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
2949 NULL, NULL_TREE);
2950 if (const_p)
2951 TREE_READONLY(decl) = 1;
2952 this->builtin_functions_[libname] = this->make_function(decl);
2956 // Return the backend generator.
2958 Backend*
2959 go_get_backend()
2961 return new Gcc_backend();