[AArch64] PR target/65491: Classify V1TF vectors as AAPCS64 short vectors rather...
[official-gcc.git] / gcc / go / go-gcc.cc
blob82ce3ee6d2eef7acaaf86ccdb662b5d55b697858
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 Bexpression*
328 stack_allocation_expression(int64_t size, Location);
330 // Statements.
332 Bstatement*
333 error_statement()
334 { return this->make_statement(error_mark_node); }
336 Bstatement*
337 expression_statement(Bexpression*);
339 Bstatement*
340 init_statement(Bvariable* var, Bexpression* init);
342 Bstatement*
343 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
345 Bstatement*
346 return_statement(Bfunction*, const std::vector<Bexpression*>&,
347 Location);
349 Bstatement*
350 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
351 Location);
353 Bstatement*
354 switch_statement(Bfunction* function, Bexpression* value,
355 const std::vector<std::vector<Bexpression*> >& cases,
356 const std::vector<Bstatement*>& statements,
357 Location);
359 Bstatement*
360 compound_statement(Bstatement*, Bstatement*);
362 Bstatement*
363 statement_list(const std::vector<Bstatement*>&);
365 Bstatement*
366 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
367 Bstatement* finally_stmt, Location);
369 // Blocks.
371 Bblock*
372 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
373 Location, Location);
375 void
376 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
378 Bstatement*
379 block_statement(Bblock*);
381 // Variables.
383 Bvariable*
384 error_variable()
385 { return new Bvariable(error_mark_node); }
387 Bvariable*
388 global_variable(const std::string& package_name,
389 const std::string& pkgpath,
390 const std::string& name,
391 Btype* btype,
392 bool is_external,
393 bool is_hidden,
394 bool in_unique_section,
395 Location location);
397 void
398 global_variable_set_init(Bvariable*, Bexpression*);
400 Bvariable*
401 local_variable(Bfunction*, const std::string&, Btype*, bool,
402 Location);
404 Bvariable*
405 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
406 Location);
408 Bvariable*
409 static_chain_variable(Bfunction*, const std::string&, Btype*, Location);
411 Bvariable*
412 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
413 Location, Bstatement**);
415 Bvariable*
416 implicit_variable(const std::string&, Btype*, bool, bool, bool,
417 int64_t);
419 void
420 implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
421 bool, bool, bool, Bexpression*);
423 Bvariable*
424 implicit_variable_reference(const std::string&, Btype*);
426 Bvariable*
427 immutable_struct(const std::string&, bool, bool, Btype*, Location);
429 void
430 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
431 Location, Bexpression*);
433 Bvariable*
434 immutable_struct_reference(const std::string&, Btype*, Location);
436 // Labels.
438 Blabel*
439 label(Bfunction*, const std::string& name, Location);
441 Bstatement*
442 label_definition_statement(Blabel*);
444 Bstatement*
445 goto_statement(Blabel*, Location);
447 Bexpression*
448 label_address(Blabel*, Location);
450 // Functions.
452 Bfunction*
453 error_function()
454 { return this->make_function(error_mark_node); }
456 Bfunction*
457 function(Btype* fntype, const std::string& name, const std::string& asm_name,
458 bool is_visible, bool is_declaration, bool is_inlinable,
459 bool disable_split_stack, bool in_unique_section, Location);
461 Bstatement*
462 function_defer_statement(Bfunction* function, Bexpression* undefer,
463 Bexpression* defer, Location);
465 bool
466 function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
468 bool
469 function_set_body(Bfunction* function, Bstatement* code_stmt);
471 Bfunction*
472 lookup_builtin(const std::string&);
474 void
475 write_global_definitions(const std::vector<Btype*>&,
476 const std::vector<Bexpression*>&,
477 const std::vector<Bfunction*>&,
478 const std::vector<Bvariable*>&);
480 private:
481 // Make a Bexpression from a tree.
482 Bexpression*
483 make_expression(tree t)
484 { return new Bexpression(t); }
486 // Make a Bstatement from a tree.
487 Bstatement*
488 make_statement(tree t)
489 { return new Bstatement(t); }
491 // Make a Btype from a tree.
492 Btype*
493 make_type(tree t)
494 { return new Btype(t); }
496 Bfunction*
497 make_function(tree t)
498 { return new Bfunction(t); }
500 Btype*
501 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
503 Btype*
504 fill_in_array(Btype*, Btype*, Bexpression*);
506 tree
507 non_zero_size_type(tree);
509 private:
510 void
511 define_builtin(built_in_function bcode, const char* name, const char* libname,
512 tree fntype, bool const_p);
514 // A mapping of the GCC built-ins exposed to GCCGo.
515 std::map<std::string, Bfunction*> builtin_functions_;
518 // A helper function.
520 static inline tree
521 get_identifier_from_string(const std::string& str)
523 return get_identifier_with_length(str.data(), str.length());
526 // Define the built-in functions that are exposed to GCCGo.
528 Gcc_backend::Gcc_backend()
530 /* We need to define the fetch_and_add functions, since we use them
531 for ++ and --. */
532 tree t = this->integer_type(BITS_PER_UNIT, 1)->get_tree();
533 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
534 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
535 NULL, build_function_type_list(t, p, t, NULL_TREE),
536 false);
538 t = this->integer_type(BITS_PER_UNIT * 2, 1)->get_tree();
539 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
540 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
541 NULL, build_function_type_list(t, p, t, NULL_TREE),
542 false);
544 t = this->integer_type(BITS_PER_UNIT * 4, 1)->get_tree();
545 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
546 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
547 NULL, build_function_type_list(t, p, t, NULL_TREE),
548 false);
550 t = this->integer_type(BITS_PER_UNIT * 8, 1)->get_tree();
551 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
552 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
553 NULL, build_function_type_list(t, p, t, NULL_TREE),
554 false);
556 // We use __builtin_expect for magic import functions.
557 this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
558 build_function_type_list(long_integer_type_node,
559 long_integer_type_node,
560 long_integer_type_node,
561 NULL_TREE),
562 true);
564 // We use __builtin_memcmp for struct comparisons.
565 this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
566 build_function_type_list(integer_type_node,
567 const_ptr_type_node,
568 const_ptr_type_node,
569 size_type_node,
570 NULL_TREE),
571 false);
573 // We provide some functions for the math library.
574 tree math_function_type = build_function_type_list(double_type_node,
575 double_type_node,
576 NULL_TREE);
577 tree math_function_type_long =
578 build_function_type_list(long_double_type_node, long_double_type_node,
579 long_double_type_node, NULL_TREE);
580 tree math_function_type_two = build_function_type_list(double_type_node,
581 double_type_node,
582 double_type_node,
583 NULL_TREE);
584 tree math_function_type_long_two =
585 build_function_type_list(long_double_type_node, long_double_type_node,
586 long_double_type_node, NULL_TREE);
587 this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
588 math_function_type, true);
589 this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
590 math_function_type_long, true);
591 this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
592 math_function_type, true);
593 this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
594 math_function_type_long, true);
595 this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
596 math_function_type, true);
597 this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
598 math_function_type_long, true);
599 this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
600 math_function_type_two, true);
601 this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
602 math_function_type_long_two, true);
603 this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
604 math_function_type, true);
605 this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
606 math_function_type_long, true);
607 this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
608 math_function_type, true);
609 this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
610 math_function_type_long, true);
611 this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
612 math_function_type, true);
613 this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
614 math_function_type_long, true);
615 this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
616 math_function_type, true);
617 this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
618 math_function_type_long, true);
619 this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
620 math_function_type, true);
621 this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
622 math_function_type_long, true);
623 this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
624 math_function_type, true);
625 this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
626 math_function_type_long, true);
627 this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
628 math_function_type_two, true);
629 this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
630 math_function_type_long_two, true);
631 this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
632 build_function_type_list(double_type_node,
633 double_type_node,
634 integer_type_node,
635 NULL_TREE),
636 true);
637 this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
638 build_function_type_list(long_double_type_node,
639 long_double_type_node,
640 integer_type_node,
641 NULL_TREE),
642 true);
643 this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
644 math_function_type, true);
645 this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
646 math_function_type_long, true);
647 this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
648 math_function_type, true);
649 this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
650 math_function_type_long, true);
651 this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
652 math_function_type, true);
653 this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
654 math_function_type_long, true);
655 this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
656 math_function_type, true);
657 this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
658 math_function_type_long, true);
659 this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
660 math_function_type, true);
661 this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
662 math_function_type_long, true);
663 this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
664 math_function_type, true);
665 this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
666 math_function_type_long, true);
667 this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
668 math_function_type, true);
669 this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
670 math_function_type_long, true);
671 this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
672 math_function_type, true);
673 this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
674 math_function_type_long, true);
676 // We use __builtin_return_address in the thunk we build for
677 // functions which call recover.
678 this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
679 NULL,
680 build_function_type_list(ptr_type_node,
681 unsigned_type_node,
682 NULL_TREE),
683 false);
685 // The compiler uses __builtin_trap for some exception handling
686 // cases.
687 this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
688 build_function_type(void_type_node, void_list_node),
689 false);
692 // Get an unnamed integer type.
694 Btype*
695 Gcc_backend::integer_type(bool is_unsigned, int bits)
697 tree type;
698 if (is_unsigned)
700 if (bits == INT_TYPE_SIZE)
701 type = unsigned_type_node;
702 else if (bits == CHAR_TYPE_SIZE)
703 type = unsigned_char_type_node;
704 else if (bits == SHORT_TYPE_SIZE)
705 type = short_unsigned_type_node;
706 else if (bits == LONG_TYPE_SIZE)
707 type = long_unsigned_type_node;
708 else if (bits == LONG_LONG_TYPE_SIZE)
709 type = long_long_unsigned_type_node;
710 else
711 type = make_unsigned_type(bits);
713 else
715 if (bits == INT_TYPE_SIZE)
716 type = integer_type_node;
717 else if (bits == CHAR_TYPE_SIZE)
718 type = signed_char_type_node;
719 else if (bits == SHORT_TYPE_SIZE)
720 type = short_integer_type_node;
721 else if (bits == LONG_TYPE_SIZE)
722 type = long_integer_type_node;
723 else if (bits == LONG_LONG_TYPE_SIZE)
724 type = long_long_integer_type_node;
725 else
726 type = make_signed_type(bits);
728 return this->make_type(type);
731 // Get an unnamed float type.
733 Btype*
734 Gcc_backend::float_type(int bits)
736 tree type;
737 if (bits == FLOAT_TYPE_SIZE)
738 type = float_type_node;
739 else if (bits == DOUBLE_TYPE_SIZE)
740 type = double_type_node;
741 else if (bits == LONG_DOUBLE_TYPE_SIZE)
742 type = long_double_type_node;
743 else
745 type = make_node(REAL_TYPE);
746 TYPE_PRECISION(type) = bits;
747 layout_type(type);
749 return this->make_type(type);
752 // Get an unnamed complex type.
754 Btype*
755 Gcc_backend::complex_type(int bits)
757 tree type;
758 if (bits == FLOAT_TYPE_SIZE * 2)
759 type = complex_float_type_node;
760 else if (bits == DOUBLE_TYPE_SIZE * 2)
761 type = complex_double_type_node;
762 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
763 type = complex_long_double_type_node;
764 else
766 type = make_node(REAL_TYPE);
767 TYPE_PRECISION(type) = bits / 2;
768 layout_type(type);
769 type = build_complex_type(type);
771 return this->make_type(type);
774 // Get a pointer type.
776 Btype*
777 Gcc_backend::pointer_type(Btype* to_type)
779 tree to_type_tree = to_type->get_tree();
780 if (to_type_tree == error_mark_node)
781 return this->error_type();
782 tree type = build_pointer_type(to_type_tree);
783 return this->make_type(type);
786 // Make a function type.
788 Btype*
789 Gcc_backend::function_type(const Btyped_identifier& receiver,
790 const std::vector<Btyped_identifier>& parameters,
791 const std::vector<Btyped_identifier>& results,
792 Btype* result_struct,
793 Location)
795 tree args = NULL_TREE;
796 tree* pp = &args;
797 if (receiver.btype != NULL)
799 tree t = receiver.btype->get_tree();
800 if (t == error_mark_node)
801 return this->error_type();
802 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
803 pp = &TREE_CHAIN(*pp);
806 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
807 p != parameters.end();
808 ++p)
810 tree t = p->btype->get_tree();
811 if (t == error_mark_node)
812 return this->error_type();
813 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
814 pp = &TREE_CHAIN(*pp);
817 // Varargs is handled entirely at the Go level. When converted to
818 // GENERIC functions are not varargs.
819 *pp = void_list_node;
821 tree result;
822 if (results.empty())
823 result = void_type_node;
824 else if (results.size() == 1)
825 result = results.front().btype->get_tree();
826 else
828 gcc_assert(result_struct != NULL);
829 result = result_struct->get_tree();
831 if (result == error_mark_node)
832 return this->error_type();
834 tree fntype = build_function_type(result, args);
835 if (fntype == error_mark_node)
836 return this->error_type();
838 return this->make_type(build_pointer_type(fntype));
841 // Make a struct type.
843 Btype*
844 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
846 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
849 // Fill in the fields of a struct type.
851 Btype*
852 Gcc_backend::fill_in_struct(Btype* fill,
853 const std::vector<Btyped_identifier>& fields)
855 tree fill_tree = fill->get_tree();
856 tree field_trees = NULL_TREE;
857 tree* pp = &field_trees;
858 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
859 p != fields.end();
860 ++p)
862 tree name_tree = get_identifier_from_string(p->name);
863 tree type_tree = p->btype->get_tree();
864 if (type_tree == error_mark_node)
865 return this->error_type();
866 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
867 type_tree);
868 DECL_CONTEXT(field) = fill_tree;
869 *pp = field;
870 pp = &DECL_CHAIN(field);
872 TYPE_FIELDS(fill_tree) = field_trees;
873 layout_type(fill_tree);
874 return fill;
877 // Make an array type.
879 Btype*
880 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
882 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
883 element_btype, length);
886 // Fill in an array type.
888 Btype*
889 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
890 Bexpression* length)
892 tree element_type_tree = element_type->get_tree();
893 tree length_tree = length->get_tree();
894 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
895 return this->error_type();
897 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
899 length_tree = fold_convert(sizetype, length_tree);
901 // build_index_type takes the maximum index, which is one less than
902 // the length.
903 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
904 length_tree,
905 size_one_node));
907 tree fill_tree = fill->get_tree();
908 TREE_TYPE(fill_tree) = element_type_tree;
909 TYPE_DOMAIN(fill_tree) = index_type_tree;
910 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
911 layout_type(fill_tree);
913 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
914 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
915 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
916 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
917 TYPE_CANONICAL(fill_tree) =
918 build_array_type(TYPE_CANONICAL(element_type_tree),
919 TYPE_CANONICAL(index_type_tree));
921 return fill;
924 // Create a placeholder for a pointer type.
926 Btype*
927 Gcc_backend::placeholder_pointer_type(const std::string& name,
928 Location location, bool)
930 tree ret = build_distinct_type_copy(ptr_type_node);
931 if (!name.empty())
933 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
934 get_identifier_from_string(name),
935 ret);
936 TYPE_NAME(ret) = decl;
938 return this->make_type(ret);
941 // Set the real target type for a placeholder pointer type.
943 bool
944 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
945 Btype* to_type)
947 tree pt = placeholder->get_tree();
948 if (pt == error_mark_node)
949 return false;
950 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
951 tree tt = to_type->get_tree();
952 if (tt == error_mark_node)
954 placeholder->set_tree(error_mark_node);
955 return false;
957 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
958 TREE_TYPE(pt) = TREE_TYPE(tt);
959 if (TYPE_NAME(pt) != NULL_TREE)
961 // Build the data structure gcc wants to see for a typedef.
962 tree copy = build_variant_type_copy(pt);
963 TYPE_NAME(copy) = NULL_TREE;
964 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
966 return true;
969 // Set the real values for a placeholder function type.
971 bool
972 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
974 return this->set_placeholder_pointer_type(placeholder, ft);
977 // Create a placeholder for a struct type.
979 Btype*
980 Gcc_backend::placeholder_struct_type(const std::string& name,
981 Location location)
983 tree ret = make_node(RECORD_TYPE);
984 if (!name.empty())
986 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
987 get_identifier_from_string(name),
988 ret);
989 TYPE_NAME(ret) = decl;
991 return this->make_type(ret);
994 // Fill in the fields of a placeholder struct type.
996 bool
997 Gcc_backend::set_placeholder_struct_type(
998 Btype* placeholder,
999 const std::vector<Btyped_identifier>& fields)
1001 tree t = placeholder->get_tree();
1002 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
1003 Btype* r = this->fill_in_struct(placeholder, fields);
1005 if (TYPE_NAME(t) != NULL_TREE)
1007 // Build the data structure gcc wants to see for a typedef.
1008 tree copy = build_distinct_type_copy(t);
1009 TYPE_NAME(copy) = NULL_TREE;
1010 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1013 return r->get_tree() != error_mark_node;
1016 // Create a placeholder for an array type.
1018 Btype*
1019 Gcc_backend::placeholder_array_type(const std::string& name,
1020 Location location)
1022 tree ret = make_node(ARRAY_TYPE);
1023 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1024 get_identifier_from_string(name),
1025 ret);
1026 TYPE_NAME(ret) = decl;
1027 return this->make_type(ret);
1030 // Fill in the fields of a placeholder array type.
1032 bool
1033 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
1034 Btype* element_btype,
1035 Bexpression* length)
1037 tree t = placeholder->get_tree();
1038 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
1039 Btype* r = this->fill_in_array(placeholder, element_btype, length);
1041 // Build the data structure gcc wants to see for a typedef.
1042 tree copy = build_distinct_type_copy(t);
1043 TYPE_NAME(copy) = NULL_TREE;
1044 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
1046 return r->get_tree() != error_mark_node;
1049 // Return a named version of a type.
1051 Btype*
1052 Gcc_backend::named_type(const std::string& name, Btype* btype,
1053 Location location)
1055 tree type = btype->get_tree();
1056 if (type == error_mark_node)
1057 return this->error_type();
1059 // The middle-end expects a basic type to have a name. In Go every
1060 // basic type will have a name. The first time we see a basic type,
1061 // give it whatever Go name we have at this point.
1062 if (TYPE_NAME(type) == NULL_TREE
1063 && location.gcc_location() == BUILTINS_LOCATION
1064 && (TREE_CODE(type) == INTEGER_TYPE
1065 || TREE_CODE(type) == REAL_TYPE
1066 || TREE_CODE(type) == COMPLEX_TYPE
1067 || TREE_CODE(type) == BOOLEAN_TYPE))
1069 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
1070 get_identifier_from_string(name),
1071 type);
1072 TYPE_NAME(type) = decl;
1073 return this->make_type(type);
1076 tree copy = build_variant_type_copy(type);
1077 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
1078 get_identifier_from_string(name),
1079 copy);
1080 DECL_ORIGINAL_TYPE(decl) = type;
1081 TYPE_NAME(copy) = decl;
1082 return this->make_type(copy);
1085 // Return a pointer type used as a marker for a circular type.
1087 Btype*
1088 Gcc_backend::circular_pointer_type(Btype*, bool)
1090 return this->make_type(ptr_type_node);
1093 // Return whether we might be looking at a circular type.
1095 bool
1096 Gcc_backend::is_circular_pointer_type(Btype* btype)
1098 return btype->get_tree() == ptr_type_node;
1101 // Return the size of a type.
1103 int64_t
1104 Gcc_backend::type_size(Btype* btype)
1106 tree t = btype->get_tree();
1107 if (t == error_mark_node)
1108 return 1;
1109 t = TYPE_SIZE_UNIT(t);
1110 gcc_assert(tree_fits_uhwi_p (t));
1111 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
1112 int64_t ret = static_cast<int64_t>(val_wide);
1113 gcc_assert(ret >= 0 && static_cast<unsigned HOST_WIDE_INT>(ret) == val_wide);
1114 return ret;
1117 // Return the alignment of a type.
1119 int64_t
1120 Gcc_backend::type_alignment(Btype* btype)
1122 tree t = btype->get_tree();
1123 if (t == error_mark_node)
1124 return 1;
1125 return TYPE_ALIGN_UNIT(t);
1128 // Return the alignment of a struct field of type BTYPE.
1130 int64_t
1131 Gcc_backend::type_field_alignment(Btype* btype)
1133 tree t = btype->get_tree();
1134 if (t == error_mark_node)
1135 return 1;
1136 return go_field_alignment(t);
1139 // Return the offset of a field in a struct.
1141 int64_t
1142 Gcc_backend::type_field_offset(Btype* btype, size_t index)
1144 tree struct_tree = btype->get_tree();
1145 if (struct_tree == error_mark_node)
1146 return 0;
1147 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
1148 tree field = TYPE_FIELDS(struct_tree);
1149 for (; index > 0; --index)
1151 field = DECL_CHAIN(field);
1152 gcc_assert(field != NULL_TREE);
1154 HOST_WIDE_INT offset_wide = int_byte_position(field);
1155 int64_t ret = static_cast<int64_t>(offset_wide);
1156 gcc_assert(ret == offset_wide);
1157 return ret;
1160 // Return the zero value for a type.
1162 Bexpression*
1163 Gcc_backend::zero_expression(Btype* btype)
1165 tree t = btype->get_tree();
1166 tree ret;
1167 if (t == error_mark_node)
1168 ret = error_mark_node;
1169 else
1170 ret = build_zero_cst(t);
1171 return this->make_expression(ret);
1174 // An expression that references a variable.
1176 Bexpression*
1177 Gcc_backend::var_expression(Bvariable* var, Location)
1179 tree ret = var->get_tree();
1180 if (ret == error_mark_node)
1181 return this->error_expression();
1182 return this->make_expression(ret);
1185 // An expression that indirectly references an expression.
1187 Bexpression*
1188 Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
1189 bool known_valid, Location location)
1191 tree expr_tree = expr->get_tree();
1192 tree type_tree = btype->get_tree();
1193 if (expr_tree == error_mark_node || type_tree == error_mark_node)
1194 return this->error_expression();
1196 // If the type of EXPR is a recursive pointer type, then we
1197 // need to insert a cast before indirecting.
1198 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
1199 if (VOID_TYPE_P(target_type_tree))
1200 expr_tree = fold_convert_loc(location.gcc_location(),
1201 build_pointer_type(type_tree), expr_tree);
1203 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
1204 expr_tree);
1205 if (known_valid)
1206 TREE_THIS_NOTRAP(ret) = 1;
1207 return this->make_expression(ret);
1210 // Return an expression that declares a constant named NAME with the
1211 // constant value VAL in BTYPE.
1213 Bexpression*
1214 Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
1215 Bexpression* val, Location location)
1217 tree type_tree = btype->get_tree();
1218 tree const_val = val->get_tree();
1219 if (type_tree == error_mark_node || const_val == error_mark_node)
1220 return this->error_expression();
1222 tree name_tree = get_identifier_from_string(name);
1223 tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
1224 type_tree);
1225 DECL_INITIAL(decl) = const_val;
1226 TREE_CONSTANT(decl) = 1;
1227 TREE_READONLY(decl) = 1;
1229 go_preserve_from_gc(decl);
1230 return this->make_expression(decl);
1233 // Return a typed value as a constant integer.
1235 Bexpression*
1236 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
1238 tree t = btype->get_tree();
1239 if (t == error_mark_node)
1240 return this->error_expression();
1242 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
1243 return this->make_expression(ret);
1246 // Return a typed value as a constant floating-point number.
1248 Bexpression*
1249 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
1251 tree t = btype->get_tree();
1252 tree ret;
1253 if (t == error_mark_node)
1254 return this->error_expression();
1256 REAL_VALUE_TYPE r1;
1257 real_from_mpfr(&r1, val, t, GMP_RNDN);
1258 REAL_VALUE_TYPE r2;
1259 real_convert(&r2, TYPE_MODE(t), &r1);
1260 ret = build_real(t, r2);
1261 return this->make_expression(ret);
1264 // Return a typed real and imaginary value as a constant complex number.
1266 Bexpression*
1267 Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
1269 tree t = btype->get_tree();
1270 tree ret;
1271 if (t == error_mark_node)
1272 return this->error_expression();
1274 REAL_VALUE_TYPE r1;
1275 real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
1276 REAL_VALUE_TYPE r2;
1277 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
1279 REAL_VALUE_TYPE r3;
1280 real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
1281 REAL_VALUE_TYPE r4;
1282 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
1284 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
1285 build_real(TREE_TYPE(t), r4));
1286 return this->make_expression(ret);
1289 // Make a constant string expression.
1291 Bexpression*
1292 Gcc_backend::string_constant_expression(const std::string& val)
1294 tree index_type = build_index_type(size_int(val.length()));
1295 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1296 TYPE_QUAL_CONST);
1297 tree string_type = build_array_type(const_char_type, index_type);
1298 string_type = build_variant_type_copy(string_type);
1299 TYPE_STRING_FLAG(string_type) = 1;
1300 tree string_val = build_string(val.length(), val.data());
1301 TREE_TYPE(string_val) = string_type;
1303 return this->make_expression(string_val);
1306 // Make a constant boolean expression.
1308 Bexpression*
1309 Gcc_backend::boolean_constant_expression(bool val)
1311 tree bool_cst = val ? boolean_true_node : boolean_false_node;
1312 return this->make_expression(bool_cst);
1315 // Return the real part of a complex expression.
1317 Bexpression*
1318 Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
1320 tree complex_tree = bcomplex->get_tree();
1321 if (complex_tree == error_mark_node)
1322 return this->error_expression();
1323 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1324 tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
1325 TREE_TYPE(TREE_TYPE(complex_tree)),
1326 complex_tree);
1327 return this->make_expression(ret);
1330 // Return the imaginary part of a complex expression.
1332 Bexpression*
1333 Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
1335 tree complex_tree = bcomplex->get_tree();
1336 if (complex_tree == error_mark_node)
1337 return this->error_expression();
1338 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
1339 tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
1340 TREE_TYPE(TREE_TYPE(complex_tree)),
1341 complex_tree);
1342 return this->make_expression(ret);
1345 // Make a complex expression given its real and imaginary parts.
1347 Bexpression*
1348 Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
1349 Location location)
1351 tree real_tree = breal->get_tree();
1352 tree imag_tree = bimag->get_tree();
1353 if (real_tree == error_mark_node || imag_tree == error_mark_node)
1354 return this->error_expression();
1355 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
1356 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
1357 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
1358 tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
1359 build_complex_type(TREE_TYPE(real_tree)),
1360 real_tree, imag_tree);
1361 return this->make_expression(ret);
1364 // An expression that converts an expression to a different type.
1366 Bexpression*
1367 Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
1368 Location location)
1370 tree type_tree = type->get_tree();
1371 tree expr_tree = expr->get_tree();
1372 if (type_tree == error_mark_node
1373 || expr_tree == error_mark_node
1374 || TREE_TYPE(expr_tree) == error_mark_node)
1375 return this->error_expression();
1377 tree ret;
1378 if (this->type_size(type) == 0)
1380 // Do not convert zero-sized types.
1381 ret = expr_tree;
1383 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
1384 ret = fold(convert_to_integer(type_tree, expr_tree));
1385 else if (TREE_CODE(type_tree) == REAL_TYPE)
1386 ret = fold(convert_to_real(type_tree, expr_tree));
1387 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
1388 ret = fold(convert_to_complex(type_tree, expr_tree));
1389 else if (TREE_CODE(type_tree) == POINTER_TYPE
1390 && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
1391 ret = fold(convert_to_pointer(type_tree, expr_tree));
1392 else if (TREE_CODE(type_tree) == RECORD_TYPE
1393 || TREE_CODE(type_tree) == ARRAY_TYPE)
1394 ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
1395 type_tree, expr_tree);
1396 else
1397 ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
1399 return this->make_expression(ret);
1402 // Get the address of a function.
1404 Bexpression*
1405 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
1407 tree func = bfunc->get_tree();
1408 if (func == error_mark_node)
1409 return this->error_expression();
1411 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1412 return this->make_expression(ret);
1415 // Get the address of an expression.
1417 Bexpression*
1418 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1420 tree expr = bexpr->get_tree();
1421 if (expr == error_mark_node)
1422 return this->error_expression();
1424 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1425 return this->make_expression(ret);
1428 // Return an expression for the field at INDEX in BSTRUCT.
1430 Bexpression*
1431 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1432 Location location)
1434 tree struct_tree = bstruct->get_tree();
1435 if (struct_tree == error_mark_node
1436 || TREE_TYPE(struct_tree) == error_mark_node)
1437 return this->error_expression();
1438 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1439 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1440 if (field == NULL_TREE)
1442 // This can happen for a type which refers to itself indirectly
1443 // and then turns out to be erroneous.
1444 return this->error_expression();
1446 for (unsigned int i = index; i > 0; --i)
1448 field = DECL_CHAIN(field);
1449 gcc_assert(field != NULL_TREE);
1451 if (TREE_TYPE(field) == error_mark_node)
1452 return this->error_expression();
1453 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1454 TREE_TYPE(field), struct_tree, field,
1455 NULL_TREE);
1456 if (TREE_CONSTANT(struct_tree))
1457 TREE_CONSTANT(ret) = 1;
1458 return this->make_expression(ret);
1461 // Return an expression that executes BSTAT before BEXPR.
1463 Bexpression*
1464 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1465 Location location)
1467 tree stat = bstat->get_tree();
1468 tree expr = bexpr->get_tree();
1469 if (stat == error_mark_node || expr == error_mark_node)
1470 return this->error_expression();
1471 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1472 TREE_TYPE(expr), stat, expr);
1473 return this->make_expression(ret);
1476 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1477 // ELSE_EXPR otherwise.
1479 Bexpression*
1480 Gcc_backend::conditional_expression(Btype* btype, Bexpression* condition,
1481 Bexpression* then_expr,
1482 Bexpression* else_expr, Location location)
1484 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1485 tree cond_tree = condition->get_tree();
1486 tree then_tree = then_expr->get_tree();
1487 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1488 if (type_tree == error_mark_node
1489 || cond_tree == error_mark_node
1490 || then_tree == error_mark_node
1491 || else_tree == error_mark_node)
1492 return this->error_expression();
1493 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1494 cond_tree, then_tree, else_tree);
1495 return this->make_expression(ret);
1498 // Return an expression for the unary operation OP EXPR.
1500 Bexpression*
1501 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1503 tree expr_tree = expr->get_tree();
1504 if (expr_tree == error_mark_node
1505 || TREE_TYPE(expr_tree) == error_mark_node)
1506 return this->error_expression();
1508 tree type_tree = TREE_TYPE(expr_tree);
1509 enum tree_code code;
1510 switch (op)
1512 case OPERATOR_MINUS:
1514 tree computed_type = excess_precision_type(type_tree);
1515 if (computed_type != NULL_TREE)
1517 expr_tree = convert(computed_type, expr_tree);
1518 type_tree = computed_type;
1520 code = NEGATE_EXPR;
1521 break;
1523 case OPERATOR_NOT:
1524 code = TRUTH_NOT_EXPR;
1525 break;
1526 case OPERATOR_XOR:
1527 code = BIT_NOT_EXPR;
1528 break;
1529 default:
1530 gcc_unreachable();
1531 break;
1534 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1535 expr_tree);
1536 return this->make_expression(ret);
1539 // Convert a gofrontend operator to an equivalent tree_code.
1541 static enum tree_code
1542 operator_to_tree_code(Operator op, tree type)
1544 enum tree_code code;
1545 switch (op)
1547 case OPERATOR_EQEQ:
1548 code = EQ_EXPR;
1549 break;
1550 case OPERATOR_NOTEQ:
1551 code = NE_EXPR;
1552 break;
1553 case OPERATOR_LT:
1554 code = LT_EXPR;
1555 break;
1556 case OPERATOR_LE:
1557 code = LE_EXPR;
1558 break;
1559 case OPERATOR_GT:
1560 code = GT_EXPR;
1561 break;
1562 case OPERATOR_GE:
1563 code = GE_EXPR;
1564 break;
1565 case OPERATOR_OROR:
1566 code = TRUTH_ORIF_EXPR;
1567 break;
1568 case OPERATOR_ANDAND:
1569 code = TRUTH_ANDIF_EXPR;
1570 break;
1571 case OPERATOR_PLUS:
1572 code = PLUS_EXPR;
1573 break;
1574 case OPERATOR_MINUS:
1575 code = MINUS_EXPR;
1576 break;
1577 case OPERATOR_OR:
1578 code = BIT_IOR_EXPR;
1579 break;
1580 case OPERATOR_XOR:
1581 code = BIT_XOR_EXPR;
1582 break;
1583 case OPERATOR_MULT:
1584 code = MULT_EXPR;
1585 break;
1586 case OPERATOR_DIV:
1587 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1588 code = RDIV_EXPR;
1589 else
1590 code = TRUNC_DIV_EXPR;
1591 break;
1592 case OPERATOR_MOD:
1593 code = TRUNC_MOD_EXPR;
1594 break;
1595 case OPERATOR_LSHIFT:
1596 code = LSHIFT_EXPR;
1597 break;
1598 case OPERATOR_RSHIFT:
1599 code = RSHIFT_EXPR;
1600 break;
1601 case OPERATOR_AND:
1602 code = BIT_AND_EXPR;
1603 break;
1604 case OPERATOR_BITCLEAR:
1605 code = BIT_AND_EXPR;
1606 break;
1607 default:
1608 gcc_unreachable();
1611 return code;
1614 // Return an expression for the binary operation LEFT OP RIGHT.
1616 Bexpression*
1617 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1618 Bexpression* right, Location location)
1620 tree left_tree = left->get_tree();
1621 tree right_tree = right->get_tree();
1622 if (left_tree == error_mark_node
1623 || right_tree == error_mark_node)
1624 return this->error_expression();
1625 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1627 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1628 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1629 tree computed_type = excess_precision_type(type_tree);
1630 if (computed_type != NULL_TREE)
1632 left_tree = convert(computed_type, left_tree);
1633 right_tree = convert(computed_type, right_tree);
1634 type_tree = computed_type;
1637 // For comparison operators, the resulting type should be boolean.
1638 switch (op)
1640 case OPERATOR_EQEQ:
1641 case OPERATOR_NOTEQ:
1642 case OPERATOR_LT:
1643 case OPERATOR_LE:
1644 case OPERATOR_GT:
1645 case OPERATOR_GE:
1646 type_tree = boolean_type_node;
1647 break;
1648 default:
1649 break;
1652 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1653 left_tree, right_tree);
1654 return this->make_expression(ret);
1657 // Return an expression that constructs BTYPE with VALS.
1659 Bexpression*
1660 Gcc_backend::constructor_expression(Btype* btype,
1661 const std::vector<Bexpression*>& vals,
1662 Location location)
1664 tree type_tree = btype->get_tree();
1665 if (type_tree == error_mark_node)
1666 return this->error_expression();
1668 vec<constructor_elt, va_gc> *init;
1669 vec_alloc(init, vals.size());
1671 tree sink = NULL_TREE;
1672 bool is_constant = true;
1673 tree field = TYPE_FIELDS(type_tree);
1674 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1675 p != vals.end();
1676 ++p, field = DECL_CHAIN(field))
1678 gcc_assert(field != NULL_TREE);
1679 tree val = (*p)->get_tree();
1680 if (TREE_TYPE(field) == error_mark_node
1681 || val == error_mark_node
1682 || TREE_TYPE(val) == error_mark_node)
1683 return this->error_expression();
1685 if (int_size_in_bytes(TREE_TYPE(field)) == 0)
1687 // GIMPLE cannot represent indices of zero-sized types so
1688 // trying to construct a map with zero-sized keys might lead
1689 // to errors. Instead, we evaluate each expression that
1690 // would have been added as a map element for its
1691 // side-effects and construct an empty map.
1692 append_to_statement_list(val, &sink);
1693 continue;
1696 constructor_elt empty = {NULL, NULL};
1697 constructor_elt* elt = init->quick_push(empty);
1698 elt->index = field;
1699 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
1700 val);
1701 if (!TREE_CONSTANT(elt->value))
1702 is_constant = false;
1704 gcc_assert(field == NULL_TREE);
1705 tree ret = build_constructor(type_tree, init);
1706 if (is_constant)
1707 TREE_CONSTANT(ret) = 1;
1708 if (sink != NULL_TREE)
1709 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1710 type_tree, sink, ret);
1711 return this->make_expression(ret);
1714 Bexpression*
1715 Gcc_backend::array_constructor_expression(
1716 Btype* array_btype, const std::vector<unsigned long>& indexes,
1717 const std::vector<Bexpression*>& vals, Location location)
1719 tree type_tree = array_btype->get_tree();
1720 if (type_tree == error_mark_node)
1721 return this->error_expression();
1723 gcc_assert(indexes.size() == vals.size());
1725 tree element_type = TREE_TYPE(type_tree);
1726 HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
1727 vec<constructor_elt, va_gc> *init;
1728 vec_alloc(init, element_size == 0 ? 0 : vals.size());
1730 tree sink = NULL_TREE;
1731 bool is_constant = true;
1732 for (size_t i = 0; i < vals.size(); ++i)
1734 tree index = size_int(indexes[i]);
1735 tree val = (vals[i])->get_tree();
1737 if (index == error_mark_node
1738 || val == error_mark_node)
1739 return this->error_expression();
1741 if (element_size == 0)
1743 // GIMPLE cannot represent arrays of zero-sized types so trying
1744 // to construct an array of zero-sized values might lead to errors.
1745 // Instead, we evaluate each expression that would have been added as
1746 // an array value for its side-effects and construct an empty array.
1747 append_to_statement_list(val, &sink);
1748 continue;
1751 if (!TREE_CONSTANT(val))
1752 is_constant = false;
1754 constructor_elt empty = {NULL, NULL};
1755 constructor_elt* elt = init->quick_push(empty);
1756 elt->index = index;
1757 elt->value = val;
1760 tree ret = build_constructor(type_tree, init);
1761 if (is_constant)
1762 TREE_CONSTANT(ret) = 1;
1763 if (sink != NULL_TREE)
1764 ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1765 type_tree, sink, ret);
1766 return this->make_expression(ret);
1769 // Return an expression for the address of BASE[INDEX].
1771 Bexpression*
1772 Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
1773 Location location)
1775 tree base_tree = base->get_tree();
1776 tree index_tree = index->get_tree();
1777 tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
1778 if (base_tree == error_mark_node
1779 || TREE_TYPE(base_tree) == error_mark_node
1780 || index_tree == error_mark_node
1781 || element_type_tree == error_mark_node)
1782 return this->error_expression();
1784 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
1785 index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
1786 tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
1787 index_tree, element_size);
1788 tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
1789 TREE_TYPE(base_tree), base_tree, offset);
1790 return this->make_expression(ptr);
1793 // Return an expression representing ARRAY[INDEX]
1795 Bexpression*
1796 Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
1797 Location location)
1799 tree array_tree = array->get_tree();
1800 tree index_tree = index->get_tree();
1801 if (array_tree == error_mark_node
1802 || TREE_TYPE(array_tree) == error_mark_node
1803 || index_tree == error_mark_node)
1804 return this->error_expression();
1806 tree ret = build4_loc(location.gcc_location(), ARRAY_REF,
1807 TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
1808 index_tree, NULL_TREE, NULL_TREE);
1809 return this->make_expression(ret);
1812 // Create an expression for a call to FN_EXPR with FN_ARGS.
1813 Bexpression*
1814 Gcc_backend::call_expression(Bexpression* fn_expr,
1815 const std::vector<Bexpression*>& fn_args,
1816 Bexpression* chain_expr, Location location)
1818 tree fn = fn_expr->get_tree();
1819 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
1820 return this->error_expression();
1822 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
1823 tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
1825 size_t nargs = fn_args.size();
1826 tree* args = nargs == 0 ? NULL : new tree[nargs];
1827 for (size_t i = 0; i < nargs; ++i)
1829 args[i] = fn_args.at(i)->get_tree();
1830 if (args[i] == error_mark_node)
1831 return this->error_expression();
1834 tree fndecl = fn;
1835 if (TREE_CODE(fndecl) == ADDR_EXPR)
1836 fndecl = TREE_OPERAND(fndecl, 0);
1838 // This is to support builtin math functions when using 80387 math.
1839 tree excess_type = NULL_TREE;
1840 if (optimize
1841 && TREE_CODE(fndecl) == FUNCTION_DECL
1842 && DECL_IS_BUILTIN(fndecl)
1843 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
1844 && nargs > 0
1845 && ((SCALAR_FLOAT_TYPE_P(rettype)
1846 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
1847 || (COMPLEX_FLOAT_TYPE_P(rettype)
1848 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
1850 excess_type = excess_precision_type(TREE_TYPE(args[0]));
1851 if (excess_type != NULL_TREE)
1853 tree excess_fndecl = mathfn_built_in(excess_type,
1854 DECL_FUNCTION_CODE(fndecl));
1855 if (excess_fndecl == NULL_TREE)
1856 excess_type = NULL_TREE;
1857 else
1859 fn = build_fold_addr_expr_loc(location.gcc_location(),
1860 excess_fndecl);
1861 for (size_t i = 0; i < nargs; ++i)
1863 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
1864 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
1865 args[i] = ::convert(excess_type, args[i]);
1871 tree ret =
1872 build_call_array_loc(location.gcc_location(),
1873 excess_type != NULL_TREE ? excess_type : rettype,
1874 fn, nargs, args);
1876 if (chain_expr)
1877 CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree();
1879 if (excess_type != NULL_TREE)
1881 // Calling convert here can undo our excess precision change.
1882 // That may or may not be a bug in convert_to_real.
1883 ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
1886 delete[] args;
1887 return this->make_expression(ret);
1890 // Return an expression that allocates SIZE bytes on the stack.
1892 Bexpression*
1893 Gcc_backend::stack_allocation_expression(int64_t size, Location location)
1895 tree alloca = builtin_decl_explicit(BUILT_IN_ALLOCA);
1896 tree size_tree = build_int_cst(integer_type_node, size);
1897 tree ret = build_call_expr_loc(location.gcc_location(), alloca, 1, size_tree);
1898 return this->make_expression(ret);
1901 // An expression as a statement.
1903 Bstatement*
1904 Gcc_backend::expression_statement(Bexpression* expr)
1906 return this->make_statement(expr->get_tree());
1909 // Variable initialization.
1911 Bstatement*
1912 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1914 tree var_tree = var->get_tree();
1915 tree init_tree = init->get_tree();
1916 if (var_tree == error_mark_node || init_tree == error_mark_node)
1917 return this->error_statement();
1918 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1920 // To avoid problems with GNU ld, we don't make zero-sized
1921 // externally visible variables. That might lead us to doing an
1922 // initialization of a zero-sized expression to a non-zero sized
1923 // variable, or vice-versa. Avoid crashes by omitting the
1924 // initializer. Such initializations don't mean anything anyhow.
1925 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1926 && init_tree != NULL_TREE
1927 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1929 DECL_INITIAL(var_tree) = init_tree;
1930 init_tree = NULL_TREE;
1933 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1934 void_type_node, var_tree);
1935 if (init_tree != NULL_TREE)
1936 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1937 void_type_node, init_tree, ret);
1939 return this->make_statement(ret);
1942 // Assignment.
1944 Bstatement*
1945 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1946 Location location)
1948 tree lhs_tree = lhs->get_tree();
1949 tree rhs_tree = rhs->get_tree();
1950 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1951 return this->error_statement();
1953 // To avoid problems with GNU ld, we don't make zero-sized
1954 // externally visible variables. That might lead us to doing an
1955 // assignment of a zero-sized expression to a non-zero sized
1956 // expression; avoid crashes here by avoiding assignments of
1957 // zero-sized expressions. Such assignments don't really mean
1958 // anything anyhow.
1959 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1960 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1961 return this->compound_statement(this->expression_statement(lhs),
1962 this->expression_statement(rhs));
1964 // Sometimes the same unnamed Go type can be created multiple times
1965 // and thus have multiple tree representations. Make sure this does
1966 // not confuse the middle-end.
1967 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1969 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1970 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1971 if (POINTER_TYPE_P(lhs_type_tree)
1972 || INTEGRAL_TYPE_P(lhs_type_tree)
1973 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1974 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1975 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1976 rhs_tree);
1977 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1978 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1980 gcc_assert(int_size_in_bytes(lhs_type_tree)
1981 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1982 rhs_tree = fold_build1_loc(location.gcc_location(),
1983 VIEW_CONVERT_EXPR,
1984 lhs_type_tree, rhs_tree);
1988 return this->make_statement(fold_build2_loc(location.gcc_location(),
1989 MODIFY_EXPR,
1990 void_type_node,
1991 lhs_tree, rhs_tree));
1994 // Return.
1996 Bstatement*
1997 Gcc_backend::return_statement(Bfunction* bfunction,
1998 const std::vector<Bexpression*>& vals,
1999 Location location)
2001 tree fntree = bfunction->get_tree();
2002 if (fntree == error_mark_node)
2003 return this->error_statement();
2004 tree result = DECL_RESULT(fntree);
2005 if (result == error_mark_node)
2006 return this->error_statement();
2008 tree ret;
2009 if (vals.empty())
2010 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
2011 NULL_TREE);
2012 else if (vals.size() == 1)
2014 tree val = vals.front()->get_tree();
2015 if (val == error_mark_node)
2016 return this->error_statement();
2017 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2018 void_type_node, result,
2019 vals.front()->get_tree());
2020 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2021 void_type_node, set);
2023 else
2025 // To return multiple values, copy the values into a temporary
2026 // variable of the right structure type, and then assign the
2027 // temporary variable to the DECL_RESULT in the return
2028 // statement.
2029 tree stmt_list = NULL_TREE;
2030 tree rettype = TREE_TYPE(result);
2032 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2033 push_struct_function(fntree);
2034 else
2035 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2036 tree rettmp = create_tmp_var(rettype, "RESULT");
2037 pop_cfun();
2039 tree field = TYPE_FIELDS(rettype);
2040 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
2041 p != vals.end();
2042 p++, field = DECL_CHAIN(field))
2044 gcc_assert(field != NULL_TREE);
2045 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
2046 TREE_TYPE(field), rettmp, field,
2047 NULL_TREE);
2048 tree val = (*p)->get_tree();
2049 if (val == error_mark_node)
2050 return this->error_statement();
2051 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2052 void_type_node,
2053 ref, (*p)->get_tree());
2054 append_to_statement_list(set, &stmt_list);
2056 gcc_assert(field == NULL_TREE);
2057 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
2058 void_type_node,
2059 result, rettmp);
2060 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
2061 void_type_node, set);
2062 append_to_statement_list(ret_expr, &stmt_list);
2063 ret = stmt_list;
2065 return this->make_statement(ret);
2068 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2069 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2070 // NULL, it will always be executed. This is used for handling defers in Go
2071 // functions. In C++, the resulting code is of this form:
2072 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2074 Bstatement*
2075 Gcc_backend::exception_handler_statement(Bstatement* bstat,
2076 Bstatement* except_stmt,
2077 Bstatement* finally_stmt,
2078 Location location)
2080 tree stat_tree = bstat->get_tree();
2081 tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
2082 tree finally_tree = finally_stmt == NULL
2083 ? NULL_TREE
2084 : finally_stmt->get_tree();
2086 if (stat_tree == error_mark_node
2087 || except_tree == error_mark_node
2088 || finally_tree == error_mark_node)
2089 return this->error_statement();
2091 if (except_tree != NULL_TREE)
2092 stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
2093 void_type_node, stat_tree,
2094 build2_loc(location.gcc_location(), CATCH_EXPR,
2095 void_type_node, NULL, except_tree));
2096 if (finally_tree != NULL_TREE)
2097 stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
2098 void_type_node, stat_tree, finally_tree);
2099 return this->make_statement(stat_tree);
2102 // If.
2104 Bstatement*
2105 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
2106 Bblock* else_block, Location location)
2108 tree cond_tree = condition->get_tree();
2109 tree then_tree = then_block->get_tree();
2110 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
2111 if (cond_tree == error_mark_node
2112 || then_tree == error_mark_node
2113 || else_tree == error_mark_node)
2114 return this->error_statement();
2115 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
2116 cond_tree, then_tree, else_tree);
2117 return this->make_statement(ret);
2120 // Switch.
2122 Bstatement*
2123 Gcc_backend::switch_statement(
2124 Bfunction* function,
2125 Bexpression* value,
2126 const std::vector<std::vector<Bexpression*> >& cases,
2127 const std::vector<Bstatement*>& statements,
2128 Location switch_location)
2130 gcc_assert(cases.size() == statements.size());
2132 tree decl = function->get_tree();
2133 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2134 push_struct_function(decl);
2135 else
2136 push_cfun(DECL_STRUCT_FUNCTION(decl));
2138 tree stmt_list = NULL_TREE;
2139 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
2140 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
2141 ps != statements.end();
2142 ++ps, ++pc)
2144 if (pc->empty())
2146 source_location loc = (*ps != NULL
2147 ? EXPR_LOCATION((*ps)->get_tree())
2148 : UNKNOWN_LOCATION);
2149 tree label = create_artificial_label(loc);
2150 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
2151 append_to_statement_list(c, &stmt_list);
2153 else
2155 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
2156 pcv != pc->end();
2157 ++pcv)
2159 tree t = (*pcv)->get_tree();
2160 if (t == error_mark_node)
2161 return this->error_statement();
2162 source_location loc = EXPR_LOCATION(t);
2163 tree label = create_artificial_label(loc);
2164 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
2165 append_to_statement_list(c, &stmt_list);
2169 if (*ps != NULL)
2171 tree t = (*ps)->get_tree();
2172 if (t == error_mark_node)
2173 return this->error_statement();
2174 append_to_statement_list(t, &stmt_list);
2177 pop_cfun();
2179 tree tv = value->get_tree();
2180 if (tv == error_mark_node)
2181 return this->error_statement();
2182 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
2183 NULL_TREE, tv, stmt_list, NULL_TREE);
2184 return this->make_statement(t);
2187 // Pair of statements.
2189 Bstatement*
2190 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
2192 tree stmt_list = NULL_TREE;
2193 tree t = s1->get_tree();
2194 if (t == error_mark_node)
2195 return this->error_statement();
2196 append_to_statement_list(t, &stmt_list);
2197 t = s2->get_tree();
2198 if (t == error_mark_node)
2199 return this->error_statement();
2200 append_to_statement_list(t, &stmt_list);
2202 // If neither statement has any side effects, stmt_list can be NULL
2203 // at this point.
2204 if (stmt_list == NULL_TREE)
2205 stmt_list = integer_zero_node;
2207 return this->make_statement(stmt_list);
2210 // List of statements.
2212 Bstatement*
2213 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
2215 tree stmt_list = NULL_TREE;
2216 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2217 p != statements.end();
2218 ++p)
2220 tree t = (*p)->get_tree();
2221 if (t == error_mark_node)
2222 return this->error_statement();
2223 append_to_statement_list(t, &stmt_list);
2225 return this->make_statement(stmt_list);
2228 // Make a block. For some reason gcc uses a dual structure for
2229 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2230 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2231 // the Bblock.
2233 Bblock*
2234 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
2235 const std::vector<Bvariable*>& vars,
2236 Location start_location,
2237 Location)
2239 tree block_tree = make_node(BLOCK);
2240 if (enclosing == NULL)
2242 tree fndecl = function->get_tree();
2243 gcc_assert(fndecl != NULL_TREE);
2245 // We may have already created a block for local variables when
2246 // we take the address of a parameter.
2247 if (DECL_INITIAL(fndecl) == NULL_TREE)
2249 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
2250 DECL_INITIAL(fndecl) = block_tree;
2252 else
2254 tree superblock_tree = DECL_INITIAL(fndecl);
2255 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2256 tree* pp;
2257 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2258 *pp != NULL_TREE;
2259 pp = &BLOCK_CHAIN(*pp))
2261 *pp = block_tree;
2264 else
2266 tree superbind_tree = enclosing->get_tree();
2267 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
2268 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
2270 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
2271 tree* pp;
2272 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
2273 *pp != NULL_TREE;
2274 pp = &BLOCK_CHAIN(*pp))
2276 *pp = block_tree;
2279 tree* pp = &BLOCK_VARS(block_tree);
2280 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
2281 pv != vars.end();
2282 ++pv)
2284 *pp = (*pv)->get_tree();
2285 if (*pp != error_mark_node)
2286 pp = &DECL_CHAIN(*pp);
2288 *pp = NULL_TREE;
2290 TREE_USED(block_tree) = 1;
2292 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
2293 void_type_node, BLOCK_VARS(block_tree),
2294 NULL_TREE, block_tree);
2295 TREE_SIDE_EFFECTS(bind_tree) = 1;
2296 return new Bblock(bind_tree);
2299 // Add statements to a block.
2301 void
2302 Gcc_backend::block_add_statements(Bblock* bblock,
2303 const std::vector<Bstatement*>& statements)
2305 tree stmt_list = NULL_TREE;
2306 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
2307 p != statements.end();
2308 ++p)
2310 tree s = (*p)->get_tree();
2311 if (s != error_mark_node)
2312 append_to_statement_list(s, &stmt_list);
2315 tree bind_tree = bblock->get_tree();
2316 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2317 BIND_EXPR_BODY(bind_tree) = stmt_list;
2320 // Return a block as a statement.
2322 Bstatement*
2323 Gcc_backend::block_statement(Bblock* bblock)
2325 tree bind_tree = bblock->get_tree();
2326 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2327 return this->make_statement(bind_tree);
2330 // This is not static because we declare it with GTY(()) in go-c.h.
2331 tree go_non_zero_struct;
2333 // Return a type corresponding to TYPE with non-zero size.
2335 tree
2336 Gcc_backend::non_zero_size_type(tree type)
2338 if (int_size_in_bytes(type) != 0)
2339 return type;
2341 switch (TREE_CODE(type))
2343 case RECORD_TYPE:
2344 if (TYPE_FIELDS(type) != NULL_TREE)
2346 tree ns = make_node(RECORD_TYPE);
2347 tree field_trees = NULL_TREE;
2348 tree *pp = &field_trees;
2349 for (tree field = TYPE_FIELDS(type);
2350 field != NULL_TREE;
2351 field = DECL_CHAIN(field))
2353 tree ft = TREE_TYPE(field);
2354 if (field == TYPE_FIELDS(type))
2355 ft = non_zero_size_type(ft);
2356 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
2357 DECL_NAME(field), ft);
2358 DECL_CONTEXT(f) = ns;
2359 *pp = f;
2360 pp = &DECL_CHAIN(f);
2362 TYPE_FIELDS(ns) = field_trees;
2363 layout_type(ns);
2364 return ns;
2367 if (go_non_zero_struct == NULL_TREE)
2369 type = make_node(RECORD_TYPE);
2370 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
2371 get_identifier("dummy"),
2372 boolean_type_node);
2373 DECL_CONTEXT(field) = type;
2374 TYPE_FIELDS(type) = field;
2375 layout_type(type);
2376 go_non_zero_struct = type;
2378 return go_non_zero_struct;
2380 case ARRAY_TYPE:
2382 tree element_type = non_zero_size_type(TREE_TYPE(type));
2383 return build_array_type_nelts(element_type, 1);
2386 default:
2387 gcc_unreachable();
2390 gcc_unreachable();
2393 // Make a global variable.
2395 Bvariable*
2396 Gcc_backend::global_variable(const std::string& package_name,
2397 const std::string& pkgpath,
2398 const std::string& name,
2399 Btype* btype,
2400 bool is_external,
2401 bool is_hidden,
2402 bool in_unique_section,
2403 Location location)
2405 tree type_tree = btype->get_tree();
2406 if (type_tree == error_mark_node)
2407 return this->error_variable();
2409 // The GNU linker does not like dynamic variables with zero size.
2410 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
2411 type_tree = this->non_zero_size_type(type_tree);
2413 std::string var_name(package_name);
2414 var_name.push_back('.');
2415 var_name.append(name);
2416 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2417 get_identifier_from_string(var_name),
2418 type_tree);
2419 if (is_external)
2420 DECL_EXTERNAL(decl) = 1;
2421 else
2422 TREE_STATIC(decl) = 1;
2423 if (!is_hidden)
2425 TREE_PUBLIC(decl) = 1;
2427 std::string asm_name(pkgpath);
2428 asm_name.push_back('.');
2429 asm_name.append(name);
2430 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2432 TREE_USED(decl) = 1;
2434 if (in_unique_section)
2435 resolve_unique_section (decl, 0, 1);
2437 go_preserve_from_gc(decl);
2439 return new Bvariable(decl);
2442 // Set the initial value of a global variable.
2444 void
2445 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
2447 tree expr_tree = expr->get_tree();
2448 if (expr_tree == error_mark_node)
2449 return;
2450 gcc_assert(TREE_CONSTANT(expr_tree));
2451 tree var_decl = var->get_tree();
2452 if (var_decl == error_mark_node)
2453 return;
2454 DECL_INITIAL(var_decl) = expr_tree;
2456 // If this variable goes in a unique section, it may need to go into
2457 // a different one now that DECL_INITIAL is set.
2458 if (symtab_node::get(var_decl)
2459 && symtab_node::get(var_decl)->implicit_section)
2461 set_decl_section_name (var_decl, NULL);
2462 resolve_unique_section (var_decl,
2463 compute_reloc_for_constant (expr_tree),
2468 // Make a local variable.
2470 Bvariable*
2471 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
2472 Btype* btype, bool is_address_taken,
2473 Location location)
2475 tree type_tree = btype->get_tree();
2476 if (type_tree == error_mark_node)
2477 return this->error_variable();
2478 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2479 get_identifier_from_string(name),
2480 type_tree);
2481 DECL_CONTEXT(decl) = function->get_tree();
2482 TREE_USED(decl) = 1;
2483 if (is_address_taken)
2484 TREE_ADDRESSABLE(decl) = 1;
2485 go_preserve_from_gc(decl);
2486 return new Bvariable(decl);
2489 // Make a function parameter variable.
2491 Bvariable*
2492 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
2493 Btype* btype, bool is_address_taken,
2494 Location location)
2496 tree type_tree = btype->get_tree();
2497 if (type_tree == error_mark_node)
2498 return this->error_variable();
2499 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2500 get_identifier_from_string(name),
2501 type_tree);
2502 DECL_CONTEXT(decl) = function->get_tree();
2503 DECL_ARG_TYPE(decl) = type_tree;
2504 TREE_USED(decl) = 1;
2505 if (is_address_taken)
2506 TREE_ADDRESSABLE(decl) = 1;
2507 go_preserve_from_gc(decl);
2508 return new Bvariable(decl);
2511 // Make a static chain variable.
2513 Bvariable*
2514 Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name,
2515 Btype* btype, Location location)
2517 tree type_tree = btype->get_tree();
2518 if (type_tree == error_mark_node)
2519 return this->error_variable();
2520 tree decl = build_decl(location.gcc_location(), PARM_DECL,
2521 get_identifier_from_string(name), type_tree);
2522 tree fndecl = function->get_tree();
2523 DECL_CONTEXT(decl) = fndecl;
2524 DECL_ARG_TYPE(decl) = type_tree;
2525 TREE_USED(decl) = 1;
2526 DECL_ARTIFICIAL(decl) = 1;
2527 DECL_IGNORED_P(decl) = 1;
2528 TREE_READONLY(decl) = 1;
2530 struct function *f = DECL_STRUCT_FUNCTION(fndecl);
2531 if (f == NULL)
2533 push_struct_function(fndecl);
2534 pop_cfun();
2535 f = DECL_STRUCT_FUNCTION(fndecl);
2537 gcc_assert(f->static_chain_decl == NULL);
2538 f->static_chain_decl = decl;
2539 DECL_STATIC_CHAIN(fndecl) = 1;
2541 go_preserve_from_gc(decl);
2542 return new Bvariable(decl);
2545 // Make a temporary variable.
2547 Bvariable*
2548 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
2549 Btype* btype, Bexpression* binit,
2550 bool is_address_taken,
2551 Location location,
2552 Bstatement** pstatement)
2554 gcc_assert(function != NULL);
2555 tree decl = function->get_tree();
2556 tree type_tree = btype->get_tree();
2557 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
2558 if (type_tree == error_mark_node
2559 || init_tree == error_mark_node
2560 || decl == error_mark_node)
2562 *pstatement = this->error_statement();
2563 return this->error_variable();
2566 tree var;
2567 // We can only use create_tmp_var if the type is not addressable.
2568 if (!TREE_ADDRESSABLE(type_tree))
2570 if (DECL_STRUCT_FUNCTION(decl) == NULL)
2571 push_struct_function(decl);
2572 else
2573 push_cfun(DECL_STRUCT_FUNCTION(decl));
2575 var = create_tmp_var(type_tree, "GOTMP");
2576 pop_cfun();
2578 else
2580 gcc_assert(bblock != NULL);
2581 var = build_decl(location.gcc_location(), VAR_DECL,
2582 create_tmp_var_name("GOTMP"),
2583 type_tree);
2584 DECL_ARTIFICIAL(var) = 1;
2585 DECL_IGNORED_P(var) = 1;
2586 TREE_USED(var) = 1;
2587 DECL_CONTEXT(var) = decl;
2589 // We have to add this variable to the BLOCK and the BIND_EXPR.
2590 tree bind_tree = bblock->get_tree();
2591 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
2592 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
2593 gcc_assert(TREE_CODE(block_tree) == BLOCK);
2594 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
2595 BLOCK_VARS(block_tree) = var;
2596 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
2599 if (this->type_size(btype) != 0 && init_tree != NULL_TREE)
2600 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
2601 init_tree);
2603 if (is_address_taken)
2604 TREE_ADDRESSABLE(var) = 1;
2606 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
2607 DECL_EXPR,
2608 void_type_node, var));
2610 // Don't initialize VAR with BINIT, but still evaluate BINIT for
2611 // its side effects.
2612 if (this->type_size(btype) == 0 && init_tree != NULL_TREE)
2613 *pstatement = this->compound_statement(this->expression_statement(binit),
2614 *pstatement);
2616 return new Bvariable(var);
2619 // Create an implicit variable that is compiler-defined. This is used when
2620 // generating GC root variables and storing the values of a slice initializer.
2622 Bvariable*
2623 Gcc_backend::implicit_variable(const std::string& name, Btype* type,
2624 bool is_hidden, bool is_constant,
2625 bool is_common, int64_t alignment)
2627 tree type_tree = type->get_tree();
2628 if (type_tree == error_mark_node)
2629 return this->error_variable();
2631 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2632 get_identifier_from_string(name), type_tree);
2633 DECL_EXTERNAL(decl) = 0;
2634 TREE_PUBLIC(decl) = !is_hidden;
2635 TREE_STATIC(decl) = 1;
2636 TREE_USED(decl) = 1;
2637 DECL_ARTIFICIAL(decl) = 1;
2638 if (is_common)
2640 DECL_COMMON(decl) = 1;
2642 // When the initializer for one implicit_variable refers to another,
2643 // it needs to know the visibility of the referenced struct so that
2644 // compute_reloc_for_constant will return the right value. On many
2645 // systems calling make_decl_one_only will mark the decl as weak,
2646 // which will change the return value of compute_reloc_for_constant.
2647 // We can't reliably call make_decl_one_only yet, because we don't
2648 // yet know the initializer. This issue doesn't arise in C because
2649 // Go initializers, unlike C initializers, can be indirectly
2650 // recursive. To ensure that compute_reloc_for_constant computes
2651 // the right value if some other initializer refers to this one, we
2652 // mark this symbol as weak here. We undo that below in
2653 // immutable_struct_set_init before calling mark_decl_one_only.
2654 DECL_WEAK(decl) = 1;
2656 if (is_constant)
2658 TREE_READONLY(decl) = 1;
2659 TREE_CONSTANT(decl) = 1;
2661 if (alignment != 0)
2663 DECL_ALIGN(decl) = alignment * BITS_PER_UNIT;
2664 DECL_USER_ALIGN(decl) = 1;
2667 go_preserve_from_gc(decl);
2668 return new Bvariable(decl);
2671 // Set the initalizer for a variable created by implicit_variable.
2672 // This is where we finish compiling the variable.
2674 void
2675 Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
2676 Btype*, bool, bool, bool is_common,
2677 Bexpression* init)
2679 tree decl = var->get_tree();
2680 tree init_tree;
2681 if (init == NULL)
2682 init_tree = NULL_TREE;
2683 else
2684 init_tree = init->get_tree();
2685 if (decl == error_mark_node || init_tree == error_mark_node)
2686 return;
2688 DECL_INITIAL(decl) = init_tree;
2690 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2691 // See the comment where DECL_WEAK is set in implicit_variable.
2692 if (is_common)
2694 DECL_WEAK(decl) = 0;
2695 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2698 resolve_unique_section(decl, 2, 1);
2700 rest_of_decl_compilation(decl, 1, 0);
2703 // Return a reference to an implicit variable defined in another package.
2705 Bvariable*
2706 Gcc_backend::implicit_variable_reference(const std::string& name, Btype* btype)
2708 tree type_tree = btype->get_tree();
2709 if (type_tree == error_mark_node)
2710 return this->error_variable();
2712 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
2713 get_identifier_from_string(name), type_tree);
2714 DECL_EXTERNAL(decl) = 0;
2715 TREE_PUBLIC(decl) = 1;
2716 TREE_STATIC(decl) = 1;
2717 DECL_ARTIFICIAL(decl) = 1;
2718 go_preserve_from_gc(decl);
2719 return new Bvariable(decl);
2722 // Create a named immutable initialized data structure.
2724 Bvariable*
2725 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
2726 bool is_common, Btype* btype, Location location)
2728 tree type_tree = btype->get_tree();
2729 if (type_tree == error_mark_node)
2730 return this->error_variable();
2731 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2732 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2733 get_identifier_from_string(name),
2734 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2735 TREE_STATIC(decl) = 1;
2736 TREE_USED(decl) = 1;
2737 TREE_READONLY(decl) = 1;
2738 TREE_CONSTANT(decl) = 1;
2739 DECL_ARTIFICIAL(decl) = 1;
2740 if (!is_hidden)
2741 TREE_PUBLIC(decl) = 1;
2743 // When the initializer for one immutable_struct refers to another,
2744 // it needs to know the visibility of the referenced struct so that
2745 // compute_reloc_for_constant will return the right value. On many
2746 // systems calling make_decl_one_only will mark the decl as weak,
2747 // which will change the return value of compute_reloc_for_constant.
2748 // We can't reliably call make_decl_one_only yet, because we don't
2749 // yet know the initializer. This issue doesn't arise in C because
2750 // Go initializers, unlike C initializers, can be indirectly
2751 // recursive. To ensure that compute_reloc_for_constant computes
2752 // the right value if some other initializer refers to this one, we
2753 // mark this symbol as weak here. We undo that below in
2754 // immutable_struct_set_init before calling mark_decl_one_only.
2755 if (is_common)
2756 DECL_WEAK(decl) = 1;
2758 // We don't call rest_of_decl_compilation until we have the
2759 // initializer.
2761 go_preserve_from_gc(decl);
2762 return new Bvariable(decl);
2765 // Set the initializer for a variable created by immutable_struct.
2766 // This is where we finish compiling the variable.
2768 void
2769 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
2770 bool, bool is_common, Btype*, Location,
2771 Bexpression* initializer)
2773 tree decl = var->get_tree();
2774 tree init_tree = initializer->get_tree();
2775 if (decl == error_mark_node || init_tree == error_mark_node)
2776 return;
2778 DECL_INITIAL(decl) = init_tree;
2780 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2781 // See the comment where DECL_WEAK is set in immutable_struct.
2782 if (is_common)
2784 DECL_WEAK(decl) = 0;
2785 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2788 // These variables are often unneeded in the final program, so put
2789 // them in their own section so that linker GC can discard them.
2790 resolve_unique_section(decl,
2791 compute_reloc_for_constant (init_tree),
2794 rest_of_decl_compilation(decl, 1, 0);
2797 // Return a reference to an immutable initialized data structure
2798 // defined in another package.
2800 Bvariable*
2801 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
2802 Location location)
2804 tree type_tree = btype->get_tree();
2805 if (type_tree == error_mark_node)
2806 return this->error_variable();
2807 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2808 tree decl = build_decl(location.gcc_location(), VAR_DECL,
2809 get_identifier_from_string(name),
2810 build_qualified_type(type_tree, TYPE_QUAL_CONST));
2811 TREE_READONLY(decl) = 1;
2812 TREE_CONSTANT(decl) = 1;
2813 DECL_ARTIFICIAL(decl) = 1;
2814 TREE_PUBLIC(decl) = 1;
2815 DECL_EXTERNAL(decl) = 1;
2816 go_preserve_from_gc(decl);
2817 return new Bvariable(decl);
2820 // Make a label.
2822 Blabel*
2823 Gcc_backend::label(Bfunction* function, const std::string& name,
2824 Location location)
2826 tree decl;
2827 if (name.empty())
2829 tree func_tree = function->get_tree();
2830 if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
2831 push_struct_function(func_tree);
2832 else
2833 push_cfun(DECL_STRUCT_FUNCTION(func_tree));
2835 decl = create_artificial_label(location.gcc_location());
2837 pop_cfun();
2839 else
2841 tree id = get_identifier_from_string(name);
2842 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
2843 void_type_node);
2844 DECL_CONTEXT(decl) = function->get_tree();
2846 return new Blabel(decl);
2849 // Make a statement which defines a label.
2851 Bstatement*
2852 Gcc_backend::label_definition_statement(Blabel* label)
2854 tree lab = label->get_tree();
2855 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
2856 void_type_node, lab);
2857 return this->make_statement(ret);
2860 // Make a goto statement.
2862 Bstatement*
2863 Gcc_backend::goto_statement(Blabel* label, Location location)
2865 tree lab = label->get_tree();
2866 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
2867 lab);
2868 return this->make_statement(ret);
2871 // Get the address of a label.
2873 Bexpression*
2874 Gcc_backend::label_address(Blabel* label, Location location)
2876 tree lab = label->get_tree();
2877 TREE_USED(lab) = 1;
2878 TREE_ADDRESSABLE(lab) = 1;
2879 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
2880 build_fold_addr_expr_loc(location.gcc_location(),
2881 lab));
2882 return this->make_expression(ret);
2885 // Declare or define a new function.
2887 Bfunction*
2888 Gcc_backend::function(Btype* fntype, const std::string& name,
2889 const std::string& asm_name, bool is_visible,
2890 bool is_declaration, bool is_inlinable,
2891 bool disable_split_stack, bool in_unique_section,
2892 Location location)
2894 tree functype = fntype->get_tree();
2895 if (functype != error_mark_node)
2897 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
2898 functype = TREE_TYPE(functype);
2900 tree id = get_identifier_from_string(name);
2901 if (functype == error_mark_node || id == error_mark_node)
2902 return this->error_function();
2904 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
2905 if (!asm_name.empty())
2906 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2907 if (is_visible)
2908 TREE_PUBLIC(decl) = 1;
2909 if (is_declaration)
2910 DECL_EXTERNAL(decl) = 1;
2911 else
2913 tree restype = TREE_TYPE(functype);
2914 tree resdecl =
2915 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
2916 DECL_ARTIFICIAL(resdecl) = 1;
2917 DECL_IGNORED_P(resdecl) = 1;
2918 DECL_CONTEXT(resdecl) = decl;
2919 DECL_RESULT(decl) = resdecl;
2921 if (!is_inlinable)
2922 DECL_UNINLINABLE(decl) = 1;
2923 if (disable_split_stack)
2925 tree attr = get_identifier("__no_split_stack__");
2926 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
2928 if (in_unique_section)
2929 resolve_unique_section(decl, 0, 1);
2931 go_preserve_from_gc(decl);
2932 return new Bfunction(decl);
2935 // Create a statement that runs all deferred calls for FUNCTION. This should
2936 // be a statement that looks like this in C++:
2937 // finish:
2938 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2940 Bstatement*
2941 Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
2942 Bexpression* defer, Location location)
2944 tree undefer_tree = undefer->get_tree();
2945 tree defer_tree = defer->get_tree();
2946 tree fntree = function->get_tree();
2948 if (undefer_tree == error_mark_node
2949 || defer_tree == error_mark_node
2950 || fntree == error_mark_node)
2951 return this->error_statement();
2953 if (DECL_STRUCT_FUNCTION(fntree) == NULL)
2954 push_struct_function(fntree);
2955 else
2956 push_cfun(DECL_STRUCT_FUNCTION(fntree));
2958 tree stmt_list = NULL;
2959 Blabel* blabel = this->label(function, "", location);
2960 Bstatement* label_def = this->label_definition_statement(blabel);
2961 append_to_statement_list(label_def->get_tree(), &stmt_list);
2963 Bstatement* jump_stmt = this->goto_statement(blabel, location);
2964 tree jump = jump_stmt->get_tree();
2965 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
2966 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
2967 tree try_catch =
2968 build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
2969 append_to_statement_list(try_catch, &stmt_list);
2970 pop_cfun();
2972 return this->make_statement(stmt_list);
2975 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2976 // This will only be called for a function definition.
2978 bool
2979 Gcc_backend::function_set_parameters(Bfunction* function,
2980 const std::vector<Bvariable*>& param_vars)
2982 tree func_tree = function->get_tree();
2983 if (func_tree == error_mark_node)
2984 return false;
2986 tree params = NULL_TREE;
2987 tree *pp = &params;
2988 for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
2989 pv != param_vars.end();
2990 ++pv)
2992 *pp = (*pv)->get_tree();
2993 gcc_assert(*pp != error_mark_node);
2994 pp = &DECL_CHAIN(*pp);
2996 *pp = NULL_TREE;
2997 DECL_ARGUMENTS(func_tree) = params;
2998 return true;
3001 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3003 bool
3004 Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
3006 tree func_tree = function->get_tree();
3007 tree code = code_stmt->get_tree();
3009 if (func_tree == error_mark_node || code == error_mark_node)
3010 return false;
3011 DECL_SAVED_TREE(func_tree) = code;
3012 return true;
3015 // Look up a named built-in function in the current backend implementation.
3016 // Returns NULL if no built-in function by that name exists.
3018 Bfunction*
3019 Gcc_backend::lookup_builtin(const std::string& name)
3021 if (this->builtin_functions_.count(name) != 0)
3022 return this->builtin_functions_[name];
3023 return NULL;
3026 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3027 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
3029 void
3030 Gcc_backend::write_global_definitions(
3031 const std::vector<Btype*>& type_decls,
3032 const std::vector<Bexpression*>& constant_decls,
3033 const std::vector<Bfunction*>& function_decls,
3034 const std::vector<Bvariable*>& variable_decls)
3036 size_t count_definitions = type_decls.size() + constant_decls.size()
3037 + function_decls.size() + variable_decls.size();
3039 tree* defs = new tree[count_definitions];
3041 // Convert all non-erroneous declarations into Gimple form.
3042 size_t i = 0;
3043 for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
3044 p != variable_decls.end();
3045 ++p)
3047 if ((*p)->get_tree() != error_mark_node)
3049 defs[i] = (*p)->get_tree();
3050 go_preserve_from_gc(defs[i]);
3051 ++i;
3055 for (std::vector<Btype*>::const_iterator p = type_decls.begin();
3056 p != type_decls.end();
3057 ++p)
3059 tree type_tree = (*p)->get_tree();
3060 if (type_tree != error_mark_node
3061 && IS_TYPE_OR_DECL_P(type_tree))
3063 defs[i] = TYPE_NAME(type_tree);
3064 gcc_assert(defs[i] != NULL);
3065 go_preserve_from_gc(defs[i]);
3066 ++i;
3069 for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
3070 p != constant_decls.end();
3071 ++p)
3073 if ((*p)->get_tree() != error_mark_node)
3075 defs[i] = (*p)->get_tree();
3076 go_preserve_from_gc(defs[i]);
3077 ++i;
3080 for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
3081 p != function_decls.end();
3082 ++p)
3084 tree decl = (*p)->get_tree();
3085 if (decl != error_mark_node)
3087 go_preserve_from_gc(decl);
3088 gimplify_function_tree(decl);
3089 cgraph_node::finalize_function(decl, true);
3091 defs[i] = decl;
3092 ++i;
3096 // Pass everything back to the middle-end.
3098 wrapup_global_declarations(defs, i);
3100 symtab->finalize_compilation_unit();
3102 check_global_declarations(defs, i);
3103 emit_debug_global_declarations(defs, i);
3105 delete[] defs;
3108 // Define a builtin function. BCODE is the builtin function code
3109 // defined by builtins.def. NAME is the name of the builtin function.
3110 // LIBNAME is the name of the corresponding library function, and is
3111 // NULL if there isn't one. FNTYPE is the type of the function.
3112 // CONST_P is true if the function has the const attribute.
3114 void
3115 Gcc_backend::define_builtin(built_in_function bcode, const char* name,
3116 const char* libname, tree fntype, bool const_p)
3118 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
3119 libname, NULL_TREE);
3120 if (const_p)
3121 TREE_READONLY(decl) = 1;
3122 set_builtin_decl(bcode, decl, true);
3123 this->builtin_functions_[name] = this->make_function(decl);
3124 if (libname != NULL)
3126 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
3127 NULL, NULL_TREE);
3128 if (const_p)
3129 TREE_READONLY(decl) = 1;
3130 this->builtin_functions_[libname] = this->make_function(decl);
3134 // Return the backend generator.
3136 Backend*
3137 go_get_backend()
3139 return new Gcc_backend();