Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / go / go-gcc.cc
blob6aec2877d7b450e5287d5da9c1844d2d1f72c0a4
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 "basic-block.h"
33 #include "gimple-expr.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "real.h"
37 #include "realmpfr.h"
39 #include "go-c.h"
41 #include "gogo.h"
42 #include "backend.h"
44 // A class wrapping a tree.
46 class Gcc_tree
48 public:
49 Gcc_tree(tree t)
50 : t_(t)
51 { }
53 tree
54 get_tree() const
55 { return this->t_; }
57 void
58 set_tree(tree t)
59 { this->t_ = t; }
61 private:
62 tree t_;
65 // In gcc, types, expressions, and statements are all trees.
66 class Btype : public Gcc_tree
68 public:
69 Btype(tree t)
70 : Gcc_tree(t)
71 { }
74 class Bexpression : public Gcc_tree
76 public:
77 Bexpression(tree t)
78 : Gcc_tree(t)
79 { }
82 class Bstatement : public Gcc_tree
84 public:
85 Bstatement(tree t)
86 : Gcc_tree(t)
87 { }
90 class Bfunction : public Gcc_tree
92 public:
93 Bfunction(tree t)
94 : Gcc_tree(t)
95 { }
98 class Bblock : public Gcc_tree
100 public:
101 Bblock(tree t)
102 : Gcc_tree(t)
106 class Bvariable : public Gcc_tree
108 public:
109 Bvariable(tree t)
110 : Gcc_tree(t)
114 class Blabel : public Gcc_tree
116 public:
117 Blabel(tree t)
118 : Gcc_tree(t)
122 // This file implements the interface between the Go frontend proper
123 // and the gcc IR. This implements specific instantiations of
124 // abstract classes defined by the Go frontend proper. The Go
125 // frontend proper class methods of these classes to generate the
126 // backend representation.
128 class Gcc_backend : public Backend
130 public:
131 // Types.
133 Btype*
134 error_type()
135 { return this->make_type(error_mark_node); }
137 Btype*
138 void_type()
139 { return this->make_type(void_type_node); }
141 Btype*
142 bool_type()
143 { return this->make_type(boolean_type_node); }
145 Btype*
146 integer_type(bool, int);
148 Btype*
149 float_type(int);
151 Btype*
152 complex_type(int);
154 Btype*
155 pointer_type(Btype*);
157 Btype*
158 function_type(const Btyped_identifier&,
159 const std::vector<Btyped_identifier>&,
160 const std::vector<Btyped_identifier>&,
161 Btype*,
162 const Location);
164 Btype*
165 struct_type(const std::vector<Btyped_identifier>&);
167 Btype*
168 array_type(Btype*, Bexpression*);
170 Btype*
171 placeholder_pointer_type(const std::string&, Location, bool);
173 bool
174 set_placeholder_pointer_type(Btype*, Btype*);
176 bool
177 set_placeholder_function_type(Btype*, Btype*);
179 Btype*
180 placeholder_struct_type(const std::string&, Location);
182 bool
183 set_placeholder_struct_type(Btype* placeholder,
184 const std::vector<Btyped_identifier>&);
186 Btype*
187 placeholder_array_type(const std::string&, Location);
189 bool
190 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
192 Btype*
193 named_type(const std::string&, Btype*, Location);
195 Btype*
196 circular_pointer_type(Btype*, bool);
198 bool
199 is_circular_pointer_type(Btype*);
201 size_t
202 type_size(Btype*);
204 size_t
205 type_alignment(Btype*);
207 size_t
208 type_field_alignment(Btype*);
210 size_t
211 type_field_offset(Btype*, size_t index);
213 // Expressions.
215 Bexpression*
216 zero_expression(Btype*);
218 Bexpression*
219 error_expression()
220 { return this->make_expression(error_mark_node); }
222 Bexpression*
223 var_expression(Bvariable* var, Location);
225 Bexpression*
226 indirect_expression(Bexpression* expr, bool known_valid, Location);
228 Bexpression*
229 integer_constant_expression(Btype* btype, mpz_t val);
231 Bexpression*
232 float_constant_expression(Btype* btype, mpfr_t val);
234 Bexpression*
235 complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag);
237 Bexpression*
238 convert_expression(Btype* type, Bexpression* expr, Location);
240 Bexpression*
241 function_code_expression(Bfunction*, Location);
243 Bexpression*
244 address_expression(Bexpression*, Location);
246 Bexpression*
247 struct_field_expression(Bexpression*, size_t, Location);
249 Bexpression*
250 compound_expression(Bstatement*, Bexpression*, Location);
252 Bexpression*
253 conditional_expression(Btype*, Bexpression*, Bexpression*, Bexpression*,
254 Location);
256 Bexpression*
257 unary_expression(Operator, Bexpression*, Location);
259 Bexpression*
260 binary_expression(Operator, Bexpression*, Bexpression*, Location);
262 // Statements.
264 Bstatement*
265 error_statement()
266 { return this->make_statement(error_mark_node); }
268 Bstatement*
269 expression_statement(Bexpression*);
271 Bstatement*
272 init_statement(Bvariable* var, Bexpression* init);
274 Bstatement*
275 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
277 Bstatement*
278 return_statement(Bfunction*, const std::vector<Bexpression*>&,
279 Location);
281 Bstatement*
282 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
283 Location);
285 Bstatement*
286 switch_statement(Bexpression* value,
287 const std::vector<std::vector<Bexpression*> >& cases,
288 const std::vector<Bstatement*>& statements,
289 Location);
291 Bstatement*
292 compound_statement(Bstatement*, Bstatement*);
294 Bstatement*
295 statement_list(const std::vector<Bstatement*>&);
297 // Blocks.
299 Bblock*
300 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
301 Location, Location);
303 void
304 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
306 Bstatement*
307 block_statement(Bblock*);
309 // Variables.
311 Bvariable*
312 error_variable()
313 { return new Bvariable(error_mark_node); }
315 Bvariable*
316 global_variable(const std::string& package_name,
317 const std::string& pkgpath,
318 const std::string& name,
319 Btype* btype,
320 bool is_external,
321 bool is_hidden,
322 bool in_unique_section,
323 Location location);
325 void
326 global_variable_set_init(Bvariable*, Bexpression*);
328 Bvariable*
329 local_variable(Bfunction*, const std::string&, Btype*, bool,
330 Location);
332 Bvariable*
333 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
334 Location);
336 Bvariable*
337 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
338 Location, Bstatement**);
340 Bvariable*
341 immutable_struct(const std::string&, bool, bool, Btype*, Location);
343 void
344 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
345 Location, Bexpression*);
347 Bvariable*
348 immutable_struct_reference(const std::string&, Btype*, Location);
350 // Labels.
352 Blabel*
353 label(Bfunction*, const std::string& name, Location);
355 Bstatement*
356 label_definition_statement(Blabel*);
358 Bstatement*
359 goto_statement(Blabel*, Location);
361 Bexpression*
362 label_address(Blabel*, Location);
364 // Functions.
366 Bfunction*
367 error_function()
368 { return this->make_function(error_mark_node); }
370 Bfunction*
371 function(Btype* fntype, const std::string& name, const std::string& asm_name,
372 bool is_visible, bool is_declaration, bool is_inlinable,
373 bool disable_split_stack, bool in_unique_section, Location);
375 private:
376 // Make a Bexpression from a tree.
377 Bexpression*
378 make_expression(tree t)
379 { return new Bexpression(t); }
381 // Make a Bstatement from a tree.
382 Bstatement*
383 make_statement(tree t)
384 { return new Bstatement(t); }
386 // Make a Btype from a tree.
387 Btype*
388 make_type(tree t)
389 { return new Btype(t); }
391 Bfunction*
392 make_function(tree t)
393 { return new Bfunction(t); }
395 Btype*
396 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
398 Btype*
399 fill_in_array(Btype*, Btype*, Bexpression*);
401 tree
402 non_zero_size_type(tree);
405 // A helper function.
407 static inline tree
408 get_identifier_from_string(const std::string& str)
410 return get_identifier_with_length(str.data(), str.length());
413 // Get an unnamed integer type.
415 Btype*
416 Gcc_backend::integer_type(bool is_unsigned, int bits)
418 tree type;
419 if (is_unsigned)
421 if (bits == INT_TYPE_SIZE)
422 type = unsigned_type_node;
423 else if (bits == CHAR_TYPE_SIZE)
424 type = unsigned_char_type_node;
425 else if (bits == SHORT_TYPE_SIZE)
426 type = short_unsigned_type_node;
427 else if (bits == LONG_TYPE_SIZE)
428 type = long_unsigned_type_node;
429 else if (bits == LONG_LONG_TYPE_SIZE)
430 type = long_long_unsigned_type_node;
431 else
432 type = make_unsigned_type(bits);
434 else
436 if (bits == INT_TYPE_SIZE)
437 type = integer_type_node;
438 else if (bits == CHAR_TYPE_SIZE)
439 type = signed_char_type_node;
440 else if (bits == SHORT_TYPE_SIZE)
441 type = short_integer_type_node;
442 else if (bits == LONG_TYPE_SIZE)
443 type = long_integer_type_node;
444 else if (bits == LONG_LONG_TYPE_SIZE)
445 type = long_long_integer_type_node;
446 else
447 type = make_signed_type(bits);
449 return this->make_type(type);
452 // Get an unnamed float type.
454 Btype*
455 Gcc_backend::float_type(int bits)
457 tree type;
458 if (bits == FLOAT_TYPE_SIZE)
459 type = float_type_node;
460 else if (bits == DOUBLE_TYPE_SIZE)
461 type = double_type_node;
462 else if (bits == LONG_DOUBLE_TYPE_SIZE)
463 type = long_double_type_node;
464 else
466 type = make_node(REAL_TYPE);
467 TYPE_PRECISION(type) = bits;
468 layout_type(type);
470 return this->make_type(type);
473 // Get an unnamed complex type.
475 Btype*
476 Gcc_backend::complex_type(int bits)
478 tree type;
479 if (bits == FLOAT_TYPE_SIZE * 2)
480 type = complex_float_type_node;
481 else if (bits == DOUBLE_TYPE_SIZE * 2)
482 type = complex_double_type_node;
483 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
484 type = complex_long_double_type_node;
485 else
487 type = make_node(REAL_TYPE);
488 TYPE_PRECISION(type) = bits / 2;
489 layout_type(type);
490 type = build_complex_type(type);
492 return this->make_type(type);
495 // Get a pointer type.
497 Btype*
498 Gcc_backend::pointer_type(Btype* to_type)
500 tree to_type_tree = to_type->get_tree();
501 if (to_type_tree == error_mark_node)
502 return this->error_type();
503 tree type = build_pointer_type(to_type_tree);
504 return this->make_type(type);
507 // Make a function type.
509 Btype*
510 Gcc_backend::function_type(const Btyped_identifier& receiver,
511 const std::vector<Btyped_identifier>& parameters,
512 const std::vector<Btyped_identifier>& results,
513 Btype* result_struct,
514 Location)
516 tree args = NULL_TREE;
517 tree* pp = &args;
518 if (receiver.btype != NULL)
520 tree t = receiver.btype->get_tree();
521 if (t == error_mark_node)
522 return this->error_type();
523 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
524 pp = &TREE_CHAIN(*pp);
527 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
528 p != parameters.end();
529 ++p)
531 tree t = p->btype->get_tree();
532 if (t == error_mark_node)
533 return this->error_type();
534 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
535 pp = &TREE_CHAIN(*pp);
538 // Varargs is handled entirely at the Go level. When converted to
539 // GENERIC functions are not varargs.
540 *pp = void_list_node;
542 tree result;
543 if (results.empty())
544 result = void_type_node;
545 else if (results.size() == 1)
546 result = results.front().btype->get_tree();
547 else
549 gcc_assert(result_struct != NULL);
550 result = result_struct->get_tree();
552 if (result == error_mark_node)
553 return this->error_type();
555 tree fntype = build_function_type(result, args);
556 if (fntype == error_mark_node)
557 return this->error_type();
559 return this->make_type(build_pointer_type(fntype));
562 // Make a struct type.
564 Btype*
565 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
567 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
570 // Fill in the fields of a struct type.
572 Btype*
573 Gcc_backend::fill_in_struct(Btype* fill,
574 const std::vector<Btyped_identifier>& fields)
576 tree fill_tree = fill->get_tree();
577 tree field_trees = NULL_TREE;
578 tree* pp = &field_trees;
579 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
580 p != fields.end();
581 ++p)
583 tree name_tree = get_identifier_from_string(p->name);
584 tree type_tree = p->btype->get_tree();
585 if (type_tree == error_mark_node)
586 return this->error_type();
587 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
588 type_tree);
589 DECL_CONTEXT(field) = fill_tree;
590 *pp = field;
591 pp = &DECL_CHAIN(field);
593 TYPE_FIELDS(fill_tree) = field_trees;
594 layout_type(fill_tree);
595 return fill;
598 // Make an array type.
600 Btype*
601 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
603 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
604 element_btype, length);
607 // Fill in an array type.
609 Btype*
610 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
611 Bexpression* length)
613 tree element_type_tree = element_type->get_tree();
614 tree length_tree = length->get_tree();
615 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
616 return this->error_type();
618 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
620 length_tree = fold_convert(sizetype, length_tree);
622 // build_index_type takes the maximum index, which is one less than
623 // the length.
624 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
625 length_tree,
626 size_one_node));
628 tree fill_tree = fill->get_tree();
629 TREE_TYPE(fill_tree) = element_type_tree;
630 TYPE_DOMAIN(fill_tree) = index_type_tree;
631 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
632 layout_type(fill_tree);
634 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
635 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
636 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
637 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
638 TYPE_CANONICAL(fill_tree) =
639 build_array_type(TYPE_CANONICAL(element_type_tree),
640 TYPE_CANONICAL(index_type_tree));
642 return fill;
645 // Create a placeholder for a pointer type.
647 Btype*
648 Gcc_backend::placeholder_pointer_type(const std::string& name,
649 Location location, bool)
651 tree ret = build_distinct_type_copy(ptr_type_node);
652 if (!name.empty())
654 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
655 get_identifier_from_string(name),
656 ret);
657 TYPE_NAME(ret) = decl;
659 return this->make_type(ret);
662 // Set the real target type for a placeholder pointer type.
664 bool
665 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
666 Btype* to_type)
668 tree pt = placeholder->get_tree();
669 if (pt == error_mark_node)
670 return false;
671 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
672 tree tt = to_type->get_tree();
673 if (tt == error_mark_node)
675 placeholder->set_tree(error_mark_node);
676 return false;
678 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
679 TREE_TYPE(pt) = TREE_TYPE(tt);
680 if (TYPE_NAME(pt) != NULL_TREE)
682 // Build the data structure gcc wants to see for a typedef.
683 tree copy = build_variant_type_copy(pt);
684 TYPE_NAME(copy) = NULL_TREE;
685 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
687 return true;
690 // Set the real values for a placeholder function type.
692 bool
693 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
695 return this->set_placeholder_pointer_type(placeholder, ft);
698 // Create a placeholder for a struct type.
700 Btype*
701 Gcc_backend::placeholder_struct_type(const std::string& name,
702 Location location)
704 tree ret = make_node(RECORD_TYPE);
705 if (!name.empty())
707 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
708 get_identifier_from_string(name),
709 ret);
710 TYPE_NAME(ret) = decl;
712 return this->make_type(ret);
715 // Fill in the fields of a placeholder struct type.
717 bool
718 Gcc_backend::set_placeholder_struct_type(
719 Btype* placeholder,
720 const std::vector<Btyped_identifier>& fields)
722 tree t = placeholder->get_tree();
723 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
724 Btype* r = this->fill_in_struct(placeholder, fields);
726 if (TYPE_NAME(t) != NULL_TREE)
728 // Build the data structure gcc wants to see for a typedef.
729 tree copy = build_distinct_type_copy(t);
730 TYPE_NAME(copy) = NULL_TREE;
731 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
734 return r->get_tree() != error_mark_node;
737 // Create a placeholder for an array type.
739 Btype*
740 Gcc_backend::placeholder_array_type(const std::string& name,
741 Location location)
743 tree ret = make_node(ARRAY_TYPE);
744 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
745 get_identifier_from_string(name),
746 ret);
747 TYPE_NAME(ret) = decl;
748 return this->make_type(ret);
751 // Fill in the fields of a placeholder array type.
753 bool
754 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
755 Btype* element_btype,
756 Bexpression* length)
758 tree t = placeholder->get_tree();
759 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
760 Btype* r = this->fill_in_array(placeholder, element_btype, length);
762 // Build the data structure gcc wants to see for a typedef.
763 tree copy = build_distinct_type_copy(t);
764 TYPE_NAME(copy) = NULL_TREE;
765 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
767 return r->get_tree() != error_mark_node;
770 // Return a named version of a type.
772 Btype*
773 Gcc_backend::named_type(const std::string& name, Btype* btype,
774 Location location)
776 tree type = btype->get_tree();
777 if (type == error_mark_node)
778 return this->error_type();
780 // The middle-end expects a basic type to have a name. In Go every
781 // basic type will have a name. The first time we see a basic type,
782 // give it whatever Go name we have at this point.
783 if (TYPE_NAME(type) == NULL_TREE
784 && location.gcc_location() == BUILTINS_LOCATION
785 && (TREE_CODE(type) == INTEGER_TYPE
786 || TREE_CODE(type) == REAL_TYPE
787 || TREE_CODE(type) == COMPLEX_TYPE
788 || TREE_CODE(type) == BOOLEAN_TYPE))
790 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
791 get_identifier_from_string(name),
792 type);
793 TYPE_NAME(type) = decl;
794 return this->make_type(type);
797 tree copy = build_variant_type_copy(type);
798 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
799 get_identifier_from_string(name),
800 copy);
801 DECL_ORIGINAL_TYPE(decl) = type;
802 TYPE_NAME(copy) = decl;
803 return this->make_type(copy);
806 // Return a pointer type used as a marker for a circular type.
808 Btype*
809 Gcc_backend::circular_pointer_type(Btype*, bool)
811 return this->make_type(ptr_type_node);
814 // Return whether we might be looking at a circular type.
816 bool
817 Gcc_backend::is_circular_pointer_type(Btype* btype)
819 return btype->get_tree() == ptr_type_node;
822 // Return the size of a type.
824 size_t
825 Gcc_backend::type_size(Btype* btype)
827 tree t = btype->get_tree();
828 if (t == error_mark_node)
829 return 1;
830 t = TYPE_SIZE_UNIT(t);
831 gcc_assert(TREE_CODE(t) == INTEGER_CST);
832 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
833 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
834 size_t ret = static_cast<size_t>(val_wide);
835 gcc_assert(ret == val_wide);
836 return ret;
839 // Return the alignment of a type.
841 size_t
842 Gcc_backend::type_alignment(Btype* btype)
844 tree t = btype->get_tree();
845 if (t == error_mark_node)
846 return 1;
847 return TYPE_ALIGN_UNIT(t);
850 // Return the alignment of a struct field of type BTYPE.
852 size_t
853 Gcc_backend::type_field_alignment(Btype* btype)
855 tree t = btype->get_tree();
856 if (t == error_mark_node)
857 return 1;
858 return go_field_alignment(t);
861 // Return the offset of a field in a struct.
863 size_t
864 Gcc_backend::type_field_offset(Btype* btype, size_t index)
866 tree struct_tree = btype->get_tree();
867 if (struct_tree == error_mark_node)
868 return 0;
869 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
870 tree field = TYPE_FIELDS(struct_tree);
871 for (; index > 0; --index)
873 field = DECL_CHAIN(field);
874 gcc_assert(field != NULL_TREE);
876 HOST_WIDE_INT offset_wide = int_byte_position(field);
877 gcc_assert(offset_wide >= 0);
878 size_t ret = static_cast<size_t>(offset_wide);
879 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
880 return ret;
883 // Return the zero value for a type.
885 Bexpression*
886 Gcc_backend::zero_expression(Btype* btype)
888 tree t = btype->get_tree();
889 tree ret;
890 if (t == error_mark_node)
891 ret = error_mark_node;
892 else
893 ret = build_zero_cst(t);
894 return tree_to_expr(ret);
897 // An expression that references a variable.
899 Bexpression*
900 Gcc_backend::var_expression(Bvariable* var, Location)
902 tree ret = var->get_tree();
903 if (ret == error_mark_node)
904 return this->error_expression();
905 return tree_to_expr(ret);
908 // An expression that indirectly references an expression.
910 Bexpression*
911 Gcc_backend::indirect_expression(Bexpression* expr, bool known_valid,
912 Location location)
914 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
915 expr->get_tree());
916 if (known_valid)
917 TREE_THIS_NOTRAP(ret) = 1;
918 return tree_to_expr(ret);
921 // Return a typed value as a constant integer.
923 Bexpression*
924 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
926 tree t = btype->get_tree();
927 if (t == error_mark_node)
928 return this->error_expression();
930 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
931 return tree_to_expr(ret);
934 // Return a typed value as a constant floating-point number.
936 Bexpression*
937 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
939 tree t = btype->get_tree();
940 tree ret;
941 if (t == error_mark_node)
942 return this->error_expression();
944 REAL_VALUE_TYPE r1;
945 real_from_mpfr(&r1, val, t, GMP_RNDN);
946 REAL_VALUE_TYPE r2;
947 real_convert(&r2, TYPE_MODE(t), &r1);
948 ret = build_real(t, r2);
949 return tree_to_expr(ret);
952 // Return a typed real and imaginary value as a constant complex number.
954 Bexpression*
955 Gcc_backend::complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag)
957 tree t = btype->get_tree();
958 tree ret;
959 if (t == error_mark_node)
960 return this->error_expression();
962 REAL_VALUE_TYPE r1;
963 real_from_mpfr(&r1, real, TREE_TYPE(t), GMP_RNDN);
964 REAL_VALUE_TYPE r2;
965 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
967 REAL_VALUE_TYPE r3;
968 real_from_mpfr(&r3, imag, TREE_TYPE(t), GMP_RNDN);
969 REAL_VALUE_TYPE r4;
970 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
972 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
973 build_real(TREE_TYPE(t), r4));
974 return tree_to_expr(ret);
977 // An expression that converts an expression to a different type.
979 Bexpression*
980 Gcc_backend::convert_expression(Btype* type, Bexpression* expr, Location)
982 tree type_tree = type->get_tree();
983 tree expr_tree = expr->get_tree();
984 if (type_tree == error_mark_node || expr_tree == error_mark_node)
985 return this->error_expression();
987 tree ret = fold_convert(type_tree, expr_tree);
988 return tree_to_expr(ret);
991 // Get the address of a function.
993 Bexpression*
994 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
996 tree func = bfunc->get_tree();
997 if (func == error_mark_node)
998 return this->error_expression();
1000 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1001 return this->make_expression(ret);
1004 // Get the address of an expression.
1006 Bexpression*
1007 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1009 tree expr = bexpr->get_tree();
1010 if (expr == error_mark_node)
1011 return this->error_expression();
1013 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1014 return this->make_expression(ret);
1017 // Return an expression for the field at INDEX in BSTRUCT.
1019 Bexpression*
1020 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1021 Location location)
1023 tree struct_tree = bstruct->get_tree();
1024 if (struct_tree == error_mark_node
1025 || TREE_TYPE(struct_tree) == error_mark_node)
1026 return this->error_expression();
1027 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1028 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1029 if (field == NULL_TREE)
1031 // This can happen for a type which refers to itself indirectly
1032 // and then turns out to be erroneous.
1033 return this->error_expression();
1035 for (unsigned int i = index; i > 0; --i)
1037 field = DECL_CHAIN(field);
1038 gcc_assert(field != NULL_TREE);
1040 if (TREE_TYPE(field) == error_mark_node)
1041 return this->error_expression();
1042 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1043 TREE_TYPE(field), struct_tree, field,
1044 NULL_TREE);
1045 if (TREE_CONSTANT(struct_tree))
1046 TREE_CONSTANT(ret) = 1;
1047 return tree_to_expr(ret);
1050 // Return an expression that executes BSTAT before BEXPR.
1052 Bexpression*
1053 Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
1054 Location location)
1056 tree stat = bstat->get_tree();
1057 tree expr = bexpr->get_tree();
1058 if (stat == error_mark_node || expr == error_mark_node)
1059 return this->error_expression();
1060 tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
1061 TREE_TYPE(expr), stat, expr);
1062 return this->make_expression(ret);
1065 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1066 // ELSE_EXPR otherwise.
1068 Bexpression*
1069 Gcc_backend::conditional_expression(Btype* btype, Bexpression* condition,
1070 Bexpression* then_expr,
1071 Bexpression* else_expr, Location location)
1073 tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
1074 tree cond_tree = condition->get_tree();
1075 tree then_tree = then_expr->get_tree();
1076 tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
1077 if (type_tree == error_mark_node
1078 || cond_tree == error_mark_node
1079 || then_tree == error_mark_node
1080 || else_tree == error_mark_node)
1081 return this->error_expression();
1082 tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
1083 cond_tree, then_tree, else_tree);
1084 return this->make_expression(ret);
1087 // Return an expression for the unary operation OP EXPR.
1089 Bexpression*
1090 Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
1092 tree expr_tree = expr->get_tree();
1093 if (expr_tree == error_mark_node
1094 || TREE_TYPE(expr_tree) == error_mark_node)
1095 return this->error_expression();
1097 tree type_tree = TREE_TYPE(expr_tree);
1098 enum tree_code code;
1099 switch (op)
1101 case OPERATOR_MINUS:
1103 tree computed_type = excess_precision_type(type_tree);
1104 if (computed_type != NULL_TREE)
1106 expr_tree = convert(computed_type, expr_tree);
1107 type_tree = computed_type;
1109 code = NEGATE_EXPR;
1110 break;
1112 case OPERATOR_NOT:
1113 code = TRUTH_NOT_EXPR;
1114 break;
1115 case OPERATOR_XOR:
1116 code = BIT_NOT_EXPR;
1117 break;
1118 default:
1119 gcc_unreachable();
1120 break;
1123 tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
1124 expr_tree);
1125 return this->make_expression(ret);
1128 // Convert a gofrontend operator to an equivalent tree_code.
1130 static enum tree_code
1131 operator_to_tree_code(Operator op, tree type)
1133 enum tree_code code;
1134 switch (op)
1136 case OPERATOR_EQEQ:
1137 code = EQ_EXPR;
1138 break;
1139 case OPERATOR_NOTEQ:
1140 code = NE_EXPR;
1141 break;
1142 case OPERATOR_LT:
1143 code = LT_EXPR;
1144 break;
1145 case OPERATOR_LE:
1146 code = LE_EXPR;
1147 break;
1148 case OPERATOR_GT:
1149 code = GT_EXPR;
1150 break;
1151 case OPERATOR_GE:
1152 code = GE_EXPR;
1153 break;
1154 case OPERATOR_OROR:
1155 code = TRUTH_ORIF_EXPR;
1156 break;
1157 case OPERATOR_ANDAND:
1158 code = TRUTH_ANDIF_EXPR;
1159 break;
1160 case OPERATOR_PLUS:
1161 code = PLUS_EXPR;
1162 break;
1163 case OPERATOR_MINUS:
1164 code = MINUS_EXPR;
1165 break;
1166 case OPERATOR_OR:
1167 code = BIT_IOR_EXPR;
1168 break;
1169 case OPERATOR_XOR:
1170 code = BIT_XOR_EXPR;
1171 break;
1172 case OPERATOR_MULT:
1173 code = MULT_EXPR;
1174 break;
1175 case OPERATOR_DIV:
1176 if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
1177 code = RDIV_EXPR;
1178 else
1179 code = TRUNC_DIV_EXPR;
1180 break;
1181 case OPERATOR_MOD:
1182 code = TRUNC_MOD_EXPR;
1183 break;
1184 case OPERATOR_LSHIFT:
1185 code = LSHIFT_EXPR;
1186 break;
1187 case OPERATOR_RSHIFT:
1188 code = RSHIFT_EXPR;
1189 break;
1190 case OPERATOR_AND:
1191 code = BIT_AND_EXPR;
1192 break;
1193 case OPERATOR_BITCLEAR:
1194 code = BIT_AND_EXPR;
1195 break;
1196 default:
1197 gcc_unreachable();
1200 return code;
1203 // Return an expression for the binary operation LEFT OP RIGHT.
1205 Bexpression*
1206 Gcc_backend::binary_expression(Operator op, Bexpression* left,
1207 Bexpression* right, Location location)
1209 tree left_tree = left->get_tree();
1210 tree right_tree = right->get_tree();
1211 if (left_tree == error_mark_node
1212 || right_tree == error_mark_node)
1213 return this->error_expression();
1214 enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
1216 bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
1217 tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
1218 tree computed_type = excess_precision_type(type_tree);
1219 if (computed_type != NULL_TREE)
1221 left_tree = convert(computed_type, left_tree);
1222 right_tree = convert(computed_type, right_tree);
1223 type_tree = computed_type;
1226 // For comparison operators, the resulting type should be boolean.
1227 switch (op)
1229 case OPERATOR_EQEQ:
1230 case OPERATOR_NOTEQ:
1231 case OPERATOR_LT:
1232 case OPERATOR_LE:
1233 case OPERATOR_GT:
1234 case OPERATOR_GE:
1235 type_tree = boolean_type_node;
1236 break;
1237 default:
1238 break;
1241 tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
1242 left_tree, right_tree);
1243 return this->make_expression(ret);
1246 // An expression as a statement.
1248 Bstatement*
1249 Gcc_backend::expression_statement(Bexpression* expr)
1251 return this->make_statement(expr->get_tree());
1254 // Variable initialization.
1256 Bstatement*
1257 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1259 tree var_tree = var->get_tree();
1260 tree init_tree = init->get_tree();
1261 if (var_tree == error_mark_node || init_tree == error_mark_node)
1262 return this->error_statement();
1263 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1265 // To avoid problems with GNU ld, we don't make zero-sized
1266 // externally visible variables. That might lead us to doing an
1267 // initialization of a zero-sized expression to a non-zero sized
1268 // variable, or vice-versa. Avoid crashes by omitting the
1269 // initializer. Such initializations don't mean anything anyhow.
1270 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1271 && init_tree != NULL_TREE
1272 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1274 DECL_INITIAL(var_tree) = init_tree;
1275 init_tree = NULL_TREE;
1278 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1279 void_type_node, var_tree);
1280 if (init_tree != NULL_TREE)
1281 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1282 void_type_node, init_tree, ret);
1284 return this->make_statement(ret);
1287 // Assignment.
1289 Bstatement*
1290 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1291 Location location)
1293 tree lhs_tree = lhs->get_tree();
1294 tree rhs_tree = rhs->get_tree();
1295 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1296 return this->error_statement();
1298 // To avoid problems with GNU ld, we don't make zero-sized
1299 // externally visible variables. That might lead us to doing an
1300 // assignment of a zero-sized expression to a non-zero sized
1301 // expression; avoid crashes here by avoiding assignments of
1302 // zero-sized expressions. Such assignments don't really mean
1303 // anything anyhow.
1304 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1305 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1306 return this->compound_statement(this->expression_statement(lhs),
1307 this->expression_statement(rhs));
1309 // Sometimes the same unnamed Go type can be created multiple times
1310 // and thus have multiple tree representations. Make sure this does
1311 // not confuse the middle-end.
1312 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1314 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1315 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1316 if (POINTER_TYPE_P(lhs_type_tree)
1317 || INTEGRAL_TYPE_P(lhs_type_tree)
1318 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1319 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1320 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1321 rhs_tree);
1322 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1323 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1325 gcc_assert(int_size_in_bytes(lhs_type_tree)
1326 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1327 rhs_tree = fold_build1_loc(location.gcc_location(),
1328 VIEW_CONVERT_EXPR,
1329 lhs_type_tree, rhs_tree);
1333 return this->make_statement(fold_build2_loc(location.gcc_location(),
1334 MODIFY_EXPR,
1335 void_type_node,
1336 lhs_tree, rhs_tree));
1339 // Return.
1341 Bstatement*
1342 Gcc_backend::return_statement(Bfunction* bfunction,
1343 const std::vector<Bexpression*>& vals,
1344 Location location)
1346 tree fntree = bfunction->get_tree();
1347 if (fntree == error_mark_node)
1348 return this->error_statement();
1349 tree result = DECL_RESULT(fntree);
1350 if (result == error_mark_node)
1351 return this->error_statement();
1352 tree ret;
1353 if (vals.empty())
1354 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
1355 NULL_TREE);
1356 else if (vals.size() == 1)
1358 tree val = vals.front()->get_tree();
1359 if (val == error_mark_node)
1360 return this->error_statement();
1361 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1362 void_type_node, result,
1363 vals.front()->get_tree());
1364 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1365 void_type_node, set);
1367 else
1369 // To return multiple values, copy the values into a temporary
1370 // variable of the right structure type, and then assign the
1371 // temporary variable to the DECL_RESULT in the return
1372 // statement.
1373 tree stmt_list = NULL_TREE;
1374 tree rettype = TREE_TYPE(result);
1375 tree rettmp = create_tmp_var(rettype, "RESULT");
1376 tree field = TYPE_FIELDS(rettype);
1377 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1378 p != vals.end();
1379 p++, field = DECL_CHAIN(field))
1381 gcc_assert(field != NULL_TREE);
1382 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1383 TREE_TYPE(field), rettmp, field,
1384 NULL_TREE);
1385 tree val = (*p)->get_tree();
1386 if (val == error_mark_node)
1387 return this->error_statement();
1388 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1389 void_type_node,
1390 ref, (*p)->get_tree());
1391 append_to_statement_list(set, &stmt_list);
1393 gcc_assert(field == NULL_TREE);
1394 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1395 void_type_node,
1396 result, rettmp);
1397 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1398 void_type_node, set);
1399 append_to_statement_list(ret_expr, &stmt_list);
1400 ret = stmt_list;
1402 return this->make_statement(ret);
1405 // If.
1407 Bstatement*
1408 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
1409 Bblock* else_block, Location location)
1411 tree cond_tree = condition->get_tree();
1412 tree then_tree = then_block->get_tree();
1413 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
1414 if (cond_tree == error_mark_node
1415 || then_tree == error_mark_node
1416 || else_tree == error_mark_node)
1417 return this->error_statement();
1418 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
1419 cond_tree, then_tree, else_tree);
1420 return this->make_statement(ret);
1423 // Switch.
1425 Bstatement*
1426 Gcc_backend::switch_statement(
1427 Bexpression* value,
1428 const std::vector<std::vector<Bexpression*> >& cases,
1429 const std::vector<Bstatement*>& statements,
1430 Location switch_location)
1432 gcc_assert(cases.size() == statements.size());
1434 tree stmt_list = NULL_TREE;
1435 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
1436 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
1437 ps != statements.end();
1438 ++ps, ++pc)
1440 if (pc->empty())
1442 source_location loc = (*ps != NULL
1443 ? EXPR_LOCATION((*ps)->get_tree())
1444 : UNKNOWN_LOCATION);
1445 tree label = create_artificial_label(loc);
1446 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
1447 append_to_statement_list(c, &stmt_list);
1449 else
1451 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1452 pcv != pc->end();
1453 ++pcv)
1455 tree t = (*pcv)->get_tree();
1456 if (t == error_mark_node)
1457 return this->error_statement();
1458 source_location loc = EXPR_LOCATION(t);
1459 tree label = create_artificial_label(loc);
1460 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1461 append_to_statement_list(c, &stmt_list);
1465 if (*ps != NULL)
1467 tree t = (*ps)->get_tree();
1468 if (t == error_mark_node)
1469 return this->error_statement();
1470 append_to_statement_list(t, &stmt_list);
1474 tree tv = value->get_tree();
1475 if (tv == error_mark_node)
1476 return this->error_statement();
1477 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1478 NULL_TREE, tv, stmt_list, NULL_TREE);
1479 return this->make_statement(t);
1482 // Pair of statements.
1484 Bstatement*
1485 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1487 tree stmt_list = NULL_TREE;
1488 tree t = s1->get_tree();
1489 if (t == error_mark_node)
1490 return this->error_statement();
1491 append_to_statement_list(t, &stmt_list);
1492 t = s2->get_tree();
1493 if (t == error_mark_node)
1494 return this->error_statement();
1495 append_to_statement_list(t, &stmt_list);
1496 return this->make_statement(stmt_list);
1499 // List of statements.
1501 Bstatement*
1502 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1504 tree stmt_list = NULL_TREE;
1505 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1506 p != statements.end();
1507 ++p)
1509 tree t = (*p)->get_tree();
1510 if (t == error_mark_node)
1511 return this->error_statement();
1512 append_to_statement_list(t, &stmt_list);
1514 return this->make_statement(stmt_list);
1517 // Make a block. For some reason gcc uses a dual structure for
1518 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1519 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1520 // the Bblock.
1522 Bblock*
1523 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1524 const std::vector<Bvariable*>& vars,
1525 Location start_location,
1526 Location)
1528 tree block_tree = make_node(BLOCK);
1529 if (enclosing == NULL)
1531 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1532 // until we have a proper representation of the init function.
1533 tree fndecl;
1534 if (function == NULL)
1535 fndecl = current_function_decl;
1536 else
1537 fndecl = function->get_tree();
1538 gcc_assert(fndecl != NULL_TREE);
1540 // We may have already created a block for local variables when
1541 // we take the address of a parameter.
1542 if (DECL_INITIAL(fndecl) == NULL_TREE)
1544 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1545 DECL_INITIAL(fndecl) = block_tree;
1547 else
1549 tree superblock_tree = DECL_INITIAL(fndecl);
1550 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1551 tree* pp;
1552 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1553 *pp != NULL_TREE;
1554 pp = &BLOCK_CHAIN(*pp))
1556 *pp = block_tree;
1559 else
1561 tree superbind_tree = enclosing->get_tree();
1562 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1563 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1565 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1566 tree* pp;
1567 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1568 *pp != NULL_TREE;
1569 pp = &BLOCK_CHAIN(*pp))
1571 *pp = block_tree;
1574 tree* pp = &BLOCK_VARS(block_tree);
1575 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1576 pv != vars.end();
1577 ++pv)
1579 *pp = (*pv)->get_tree();
1580 if (*pp != error_mark_node)
1581 pp = &DECL_CHAIN(*pp);
1583 *pp = NULL_TREE;
1585 TREE_USED(block_tree) = 1;
1587 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1588 void_type_node, BLOCK_VARS(block_tree),
1589 NULL_TREE, block_tree);
1590 TREE_SIDE_EFFECTS(bind_tree) = 1;
1592 return new Bblock(bind_tree);
1595 // Add statements to a block.
1597 void
1598 Gcc_backend::block_add_statements(Bblock* bblock,
1599 const std::vector<Bstatement*>& statements)
1601 tree stmt_list = NULL_TREE;
1602 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1603 p != statements.end();
1604 ++p)
1606 tree s = (*p)->get_tree();
1607 if (s != error_mark_node)
1608 append_to_statement_list(s, &stmt_list);
1611 tree bind_tree = bblock->get_tree();
1612 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1613 BIND_EXPR_BODY(bind_tree) = stmt_list;
1616 // Return a block as a statement.
1618 Bstatement*
1619 Gcc_backend::block_statement(Bblock* bblock)
1621 tree bind_tree = bblock->get_tree();
1622 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1623 return this->make_statement(bind_tree);
1626 // This is not static because we declare it with GTY(()) in go-c.h.
1627 tree go_non_zero_struct;
1629 // Return a type corresponding to TYPE with non-zero size.
1631 tree
1632 Gcc_backend::non_zero_size_type(tree type)
1634 if (int_size_in_bytes(type) != 0)
1635 return type;
1637 switch (TREE_CODE(type))
1639 case RECORD_TYPE:
1640 if (TYPE_FIELDS(type) != NULL_TREE)
1642 tree ns = make_node(RECORD_TYPE);
1643 tree field_trees = NULL_TREE;
1644 tree *pp = &field_trees;
1645 for (tree field = TYPE_FIELDS(type);
1646 field != NULL_TREE;
1647 field = DECL_CHAIN(field))
1649 tree ft = TREE_TYPE(field);
1650 if (field == TYPE_FIELDS(type))
1651 ft = non_zero_size_type(ft);
1652 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
1653 DECL_NAME(field), ft);
1654 DECL_CONTEXT(f) = ns;
1655 *pp = f;
1656 pp = &DECL_CHAIN(f);
1658 TYPE_FIELDS(ns) = field_trees;
1659 layout_type(ns);
1660 return ns;
1663 if (go_non_zero_struct == NULL_TREE)
1665 type = make_node(RECORD_TYPE);
1666 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
1667 get_identifier("dummy"),
1668 boolean_type_node);
1669 DECL_CONTEXT(field) = type;
1670 TYPE_FIELDS(type) = field;
1671 layout_type(type);
1672 go_non_zero_struct = type;
1674 return go_non_zero_struct;
1676 case ARRAY_TYPE:
1678 tree element_type = non_zero_size_type(TREE_TYPE(type));
1679 return build_array_type_nelts(element_type, 1);
1682 default:
1683 gcc_unreachable();
1686 gcc_unreachable();
1689 // Make a global variable.
1691 Bvariable*
1692 Gcc_backend::global_variable(const std::string& package_name,
1693 const std::string& pkgpath,
1694 const std::string& name,
1695 Btype* btype,
1696 bool is_external,
1697 bool is_hidden,
1698 bool in_unique_section,
1699 Location location)
1701 tree type_tree = btype->get_tree();
1702 if (type_tree == error_mark_node)
1703 return this->error_variable();
1705 // The GNU linker does not like dynamic variables with zero size.
1706 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
1707 type_tree = this->non_zero_size_type(type_tree);
1709 std::string var_name(package_name);
1710 var_name.push_back('.');
1711 var_name.append(name);
1712 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1713 get_identifier_from_string(var_name),
1714 type_tree);
1715 if (is_external)
1716 DECL_EXTERNAL(decl) = 1;
1717 else
1718 TREE_STATIC(decl) = 1;
1719 if (!is_hidden)
1721 TREE_PUBLIC(decl) = 1;
1723 std::string asm_name(pkgpath);
1724 asm_name.push_back('.');
1725 asm_name.append(name);
1726 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1728 TREE_USED(decl) = 1;
1730 if (in_unique_section)
1731 resolve_unique_section (decl, 0, 1);
1733 go_preserve_from_gc(decl);
1735 return new Bvariable(decl);
1738 // Set the initial value of a global variable.
1740 void
1741 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1743 tree expr_tree = expr->get_tree();
1744 if (expr_tree == error_mark_node)
1745 return;
1746 gcc_assert(TREE_CONSTANT(expr_tree));
1747 tree var_decl = var->get_tree();
1748 if (var_decl == error_mark_node)
1749 return;
1750 DECL_INITIAL(var_decl) = expr_tree;
1752 // If this variable goes in a unique section, it may need to go into
1753 // a different one now that DECL_INITIAL is set.
1754 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl))
1756 DECL_SECTION_NAME (var_decl) = NULL_TREE;
1757 resolve_unique_section (var_decl,
1758 compute_reloc_for_constant (expr_tree),
1763 // Make a local variable.
1765 Bvariable*
1766 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1767 Btype* btype, bool is_address_taken,
1768 Location location)
1770 tree type_tree = btype->get_tree();
1771 if (type_tree == error_mark_node)
1772 return this->error_variable();
1773 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1774 get_identifier_from_string(name),
1775 type_tree);
1776 DECL_CONTEXT(decl) = function->get_tree();
1777 TREE_USED(decl) = 1;
1778 if (is_address_taken)
1779 TREE_ADDRESSABLE(decl) = 1;
1780 go_preserve_from_gc(decl);
1781 return new Bvariable(decl);
1784 // Make a function parameter variable.
1786 Bvariable*
1787 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1788 Btype* btype, bool is_address_taken,
1789 Location location)
1791 tree type_tree = btype->get_tree();
1792 if (type_tree == error_mark_node)
1793 return this->error_variable();
1794 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1795 get_identifier_from_string(name),
1796 type_tree);
1797 DECL_CONTEXT(decl) = function->get_tree();
1798 DECL_ARG_TYPE(decl) = type_tree;
1799 TREE_USED(decl) = 1;
1800 if (is_address_taken)
1801 TREE_ADDRESSABLE(decl) = 1;
1802 go_preserve_from_gc(decl);
1803 return new Bvariable(decl);
1806 // Make a temporary variable.
1808 Bvariable*
1809 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1810 Btype* btype, Bexpression* binit,
1811 bool is_address_taken,
1812 Location location,
1813 Bstatement** pstatement)
1815 tree type_tree = btype->get_tree();
1816 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1817 if (type_tree == error_mark_node || init_tree == error_mark_node)
1819 *pstatement = this->error_statement();
1820 return this->error_variable();
1823 tree var;
1824 // We can only use create_tmp_var if the type is not addressable.
1825 if (!TREE_ADDRESSABLE(type_tree))
1826 var = create_tmp_var(type_tree, "GOTMP");
1827 else
1829 gcc_assert(bblock != NULL);
1830 var = build_decl(location.gcc_location(), VAR_DECL,
1831 create_tmp_var_name("GOTMP"),
1832 type_tree);
1833 DECL_ARTIFICIAL(var) = 1;
1834 DECL_IGNORED_P(var) = 1;
1835 TREE_USED(var) = 1;
1836 // FIXME: Permitting function to be NULL here is a temporary
1837 // measure until we have a proper representation of the init
1838 // function.
1839 if (function != NULL)
1840 DECL_CONTEXT(var) = function->get_tree();
1841 else
1843 gcc_assert(current_function_decl != NULL_TREE);
1844 DECL_CONTEXT(var) = current_function_decl;
1847 // We have to add this variable to the BLOCK and the BIND_EXPR.
1848 tree bind_tree = bblock->get_tree();
1849 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1850 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1851 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1852 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1853 BLOCK_VARS(block_tree) = var;
1854 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1857 if (init_tree != NULL_TREE)
1858 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1859 init_tree);
1861 if (is_address_taken)
1862 TREE_ADDRESSABLE(var) = 1;
1864 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1865 DECL_EXPR,
1866 void_type_node, var));
1867 return new Bvariable(var);
1870 // Create a named immutable initialized data structure.
1872 Bvariable*
1873 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
1874 bool is_common, Btype* btype, Location location)
1876 tree type_tree = btype->get_tree();
1877 if (type_tree == error_mark_node)
1878 return this->error_variable();
1879 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1880 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1881 get_identifier_from_string(name),
1882 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1883 TREE_STATIC(decl) = 1;
1884 TREE_READONLY(decl) = 1;
1885 TREE_CONSTANT(decl) = 1;
1886 TREE_USED(decl) = 1;
1887 DECL_ARTIFICIAL(decl) = 1;
1888 if (!is_hidden)
1889 TREE_PUBLIC(decl) = 1;
1891 // When the initializer for one immutable_struct refers to another,
1892 // it needs to know the visibility of the referenced struct so that
1893 // compute_reloc_for_constant will return the right value. On many
1894 // systems calling make_decl_one_only will mark the decl as weak,
1895 // which will change the return value of compute_reloc_for_constant.
1896 // We can't reliably call make_decl_one_only yet, because we don't
1897 // yet know the initializer. This issue doesn't arise in C because
1898 // Go initializers, unlike C initializers, can be indirectly
1899 // recursive. To ensure that compute_reloc_for_constant computes
1900 // the right value if some other initializer refers to this one, we
1901 // mark this symbol as weak here. We undo that below in
1902 // immutable_struct_set_init before calling mark_decl_one_only.
1903 if (is_common)
1904 DECL_WEAK(decl) = 1;
1906 // We don't call rest_of_decl_compilation until we have the
1907 // initializer.
1909 go_preserve_from_gc(decl);
1910 return new Bvariable(decl);
1913 // Set the initializer for a variable created by immutable_struct.
1914 // This is where we finish compiling the variable.
1916 void
1917 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1918 bool, bool is_common, Btype*, Location,
1919 Bexpression* initializer)
1921 tree decl = var->get_tree();
1922 tree init_tree = initializer->get_tree();
1923 if (decl == error_mark_node || init_tree == error_mark_node)
1924 return;
1926 DECL_INITIAL(decl) = init_tree;
1928 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
1929 // See the comment where DECL_WEAK is set in immutable_struct.
1930 if (is_common)
1932 DECL_WEAK(decl) = 0;
1933 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1936 // These variables are often unneeded in the final program, so put
1937 // them in their own section so that linker GC can discard them.
1938 resolve_unique_section(decl,
1939 compute_reloc_for_constant (init_tree),
1942 rest_of_decl_compilation(decl, 1, 0);
1945 // Return a reference to an immutable initialized data structure
1946 // defined in another package.
1948 Bvariable*
1949 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1950 Location location)
1952 tree type_tree = btype->get_tree();
1953 if (type_tree == error_mark_node)
1954 return this->error_variable();
1955 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1956 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1957 get_identifier_from_string(name),
1958 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1959 TREE_READONLY(decl) = 1;
1960 TREE_CONSTANT(decl) = 1;
1961 DECL_ARTIFICIAL(decl) = 1;
1962 TREE_PUBLIC(decl) = 1;
1963 DECL_EXTERNAL(decl) = 1;
1964 go_preserve_from_gc(decl);
1965 return new Bvariable(decl);
1968 // Make a label.
1970 Blabel*
1971 Gcc_backend::label(Bfunction* function, const std::string& name,
1972 Location location)
1974 tree decl;
1975 if (name.empty())
1976 decl = create_artificial_label(location.gcc_location());
1977 else
1979 tree id = get_identifier_from_string(name);
1980 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1981 void_type_node);
1982 DECL_CONTEXT(decl) = function->get_tree();
1984 return new Blabel(decl);
1987 // Make a statement which defines a label.
1989 Bstatement*
1990 Gcc_backend::label_definition_statement(Blabel* label)
1992 tree lab = label->get_tree();
1993 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1994 void_type_node, lab);
1995 return this->make_statement(ret);
1998 // Make a goto statement.
2000 Bstatement*
2001 Gcc_backend::goto_statement(Blabel* label, Location location)
2003 tree lab = label->get_tree();
2004 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
2005 lab);
2006 return this->make_statement(ret);
2009 // Get the address of a label.
2011 Bexpression*
2012 Gcc_backend::label_address(Blabel* label, Location location)
2014 tree lab = label->get_tree();
2015 TREE_USED(lab) = 1;
2016 TREE_ADDRESSABLE(lab) = 1;
2017 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
2018 build_fold_addr_expr_loc(location.gcc_location(),
2019 lab));
2020 return this->make_expression(ret);
2023 // Declare or define a new function.
2025 Bfunction*
2026 Gcc_backend::function(Btype* fntype, const std::string& name,
2027 const std::string& asm_name, bool is_visible,
2028 bool is_declaration, bool is_inlinable,
2029 bool disable_split_stack, bool in_unique_section,
2030 Location location)
2032 tree functype = fntype->get_tree();
2033 if (functype != error_mark_node)
2035 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
2036 functype = TREE_TYPE(functype);
2038 tree id = get_identifier_from_string(name);
2039 if (functype == error_mark_node || id == error_mark_node)
2040 return this->error_function();
2042 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
2043 if (!asm_name.empty())
2044 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
2045 if (is_visible)
2046 TREE_PUBLIC(decl) = 1;
2047 if (is_declaration)
2048 DECL_EXTERNAL(decl) = 1;
2049 else
2051 tree restype = TREE_TYPE(functype);
2052 tree resdecl =
2053 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
2054 DECL_ARTIFICIAL(resdecl) = 1;
2055 DECL_IGNORED_P(resdecl) = 1;
2056 DECL_CONTEXT(resdecl) = decl;
2057 DECL_RESULT(decl) = resdecl;
2059 if (!is_inlinable)
2060 DECL_UNINLINABLE(decl) = 1;
2061 if (disable_split_stack)
2063 tree attr = get_identifier("__no_split_stack__");
2064 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
2066 if (in_unique_section)
2067 resolve_unique_section(decl, 0, 1);
2069 go_preserve_from_gc(decl);
2070 return new Bfunction(decl);
2073 // The single backend.
2075 static Gcc_backend gcc_backend;
2077 // Return the backend generator.
2079 Backend*
2080 go_get_backend()
2082 return &gcc_backend;
2085 // FIXME: Temporary functions while converting to the new backend
2086 // interface.
2088 Btype*
2089 tree_to_type(tree t)
2091 return new Btype(t);
2094 Bexpression*
2095 tree_to_expr(tree t)
2097 return new Bexpression(t);
2100 Bstatement*
2101 tree_to_stat(tree t)
2103 return new Bstatement(t);
2106 Bfunction*
2107 tree_to_function(tree t)
2109 return new Bfunction(t);
2112 Bblock*
2113 tree_to_block(tree t)
2115 gcc_assert(TREE_CODE(t) == BIND_EXPR);
2116 return new Bblock(t);
2119 tree
2120 type_to_tree(Btype* bt)
2122 return bt->get_tree();
2125 tree
2126 expr_to_tree(Bexpression* be)
2128 return be->get_tree();
2131 tree
2132 stat_to_tree(Bstatement* bs)
2134 return bs->get_tree();
2137 tree
2138 block_to_tree(Bblock* bb)
2140 return bb->get_tree();
2143 tree
2144 var_to_tree(Bvariable* bv)
2146 return bv->get_tree();
2149 tree
2150 function_to_tree(Bfunction* bf)
2152 return bf->get_tree();