Reverting merge from trunk
[official-gcc.git] / gcc / go / go-gcc.cc
blob5ff529fc62b1e0d442ed915051d0c0f1ef078da6
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2013 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 "tree-iterator.h"
29 #include "gimple.h"
30 #include "toplev.h"
31 #include "output.h"
32 #include "real.h"
33 #include "realmpfr.h"
35 #include "go-c.h"
37 #include "gogo.h"
38 #include "backend.h"
40 // A class wrapping a tree.
42 class Gcc_tree
44 public:
45 Gcc_tree(tree t)
46 : t_(t)
47 { }
49 tree
50 get_tree() const
51 { return this->t_; }
53 void
54 set_tree(tree t)
55 { this->t_ = t; }
57 private:
58 tree t_;
61 // In gcc, types, expressions, and statements are all trees.
62 class Btype : public Gcc_tree
64 public:
65 Btype(tree t)
66 : Gcc_tree(t)
67 { }
70 class Bexpression : public Gcc_tree
72 public:
73 Bexpression(tree t)
74 : Gcc_tree(t)
75 { }
78 class Bstatement : public Gcc_tree
80 public:
81 Bstatement(tree t)
82 : Gcc_tree(t)
83 { }
86 class Bfunction : public Gcc_tree
88 public:
89 Bfunction(tree t)
90 : Gcc_tree(t)
91 { }
94 class Bblock : public Gcc_tree
96 public:
97 Bblock(tree t)
98 : Gcc_tree(t)
99 { }
102 class Bvariable : public Gcc_tree
104 public:
105 Bvariable(tree t)
106 : Gcc_tree(t)
110 class Blabel : public Gcc_tree
112 public:
113 Blabel(tree t)
114 : Gcc_tree(t)
118 // This file implements the interface between the Go frontend proper
119 // and the gcc IR. This implements specific instantiations of
120 // abstract classes defined by the Go frontend proper. The Go
121 // frontend proper class methods of these classes to generate the
122 // backend representation.
124 class Gcc_backend : public Backend
126 public:
127 // Types.
129 Btype*
130 error_type()
131 { return this->make_type(error_mark_node); }
133 Btype*
134 void_type()
135 { return this->make_type(void_type_node); }
137 Btype*
138 bool_type()
139 { return this->make_type(boolean_type_node); }
141 Btype*
142 integer_type(bool, int);
144 Btype*
145 float_type(int);
147 Btype*
148 complex_type(int);
150 Btype*
151 pointer_type(Btype*);
153 Btype*
154 function_type(const Btyped_identifier&,
155 const std::vector<Btyped_identifier>&,
156 const std::vector<Btyped_identifier>&,
157 const Location);
159 Btype*
160 struct_type(const std::vector<Btyped_identifier>&);
162 Btype*
163 array_type(Btype*, Bexpression*);
165 Btype*
166 placeholder_pointer_type(const std::string&, Location, bool);
168 bool
169 set_placeholder_pointer_type(Btype*, Btype*);
171 bool
172 set_placeholder_function_type(Btype*, Btype*);
174 Btype*
175 placeholder_struct_type(const std::string&, Location);
177 bool
178 set_placeholder_struct_type(Btype* placeholder,
179 const std::vector<Btyped_identifier>&);
181 Btype*
182 placeholder_array_type(const std::string&, Location);
184 bool
185 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
187 Btype*
188 named_type(const std::string&, Btype*, Location);
190 Btype*
191 circular_pointer_type(Btype*, bool);
193 bool
194 is_circular_pointer_type(Btype*);
196 size_t
197 type_size(Btype*);
199 size_t
200 type_alignment(Btype*);
202 size_t
203 type_field_alignment(Btype*);
205 size_t
206 type_field_offset(Btype*, size_t index);
208 // Expressions.
210 Bexpression*
211 zero_expression(Btype*);
213 Bexpression*
214 error_expression()
215 { return this->make_expression(error_mark_node); }
217 Bexpression*
218 var_expression(Bvariable* var, Location);
220 Bexpression*
221 indirect_expression(Bexpression* expr, bool known_valid, Location);
223 Bexpression*
224 integer_constant_expression(Btype* btype, mpz_t val);
226 Bexpression*
227 float_constant_expression(Btype* btype, mpfr_t val);
229 Bexpression*
230 complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag);
232 Bexpression*
233 convert_expression(Btype* type, Bexpression* expr, Location);
235 Bexpression*
236 function_code_expression(Bfunction*, Location);
238 Bexpression*
239 address_expression(Bexpression*, Location);
241 // Statements.
243 Bstatement*
244 error_statement()
245 { return this->make_statement(error_mark_node); }
247 Bstatement*
248 expression_statement(Bexpression*);
250 Bstatement*
251 init_statement(Bvariable* var, Bexpression* init);
253 Bstatement*
254 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
256 Bstatement*
257 return_statement(Bfunction*, const std::vector<Bexpression*>&,
258 Location);
260 Bstatement*
261 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
262 Location);
264 Bstatement*
265 switch_statement(Bexpression* value,
266 const std::vector<std::vector<Bexpression*> >& cases,
267 const std::vector<Bstatement*>& statements,
268 Location);
270 Bstatement*
271 compound_statement(Bstatement*, Bstatement*);
273 Bstatement*
274 statement_list(const std::vector<Bstatement*>&);
276 // Blocks.
278 Bblock*
279 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
280 Location, Location);
282 void
283 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
285 Bstatement*
286 block_statement(Bblock*);
288 // Variables.
290 Bvariable*
291 error_variable()
292 { return new Bvariable(error_mark_node); }
294 Bvariable*
295 global_variable(const std::string& package_name,
296 const std::string& pkgpath,
297 const std::string& name,
298 Btype* btype,
299 bool is_external,
300 bool is_hidden,
301 bool in_unique_section,
302 Location location);
304 void
305 global_variable_set_init(Bvariable*, Bexpression*);
307 Bvariable*
308 local_variable(Bfunction*, const std::string&, Btype*, bool,
309 Location);
311 Bvariable*
312 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
313 Location);
315 Bvariable*
316 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
317 Location, Bstatement**);
319 Bvariable*
320 immutable_struct(const std::string&, bool, bool, Btype*, Location);
322 void
323 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
324 Location, Bexpression*);
326 Bvariable*
327 immutable_struct_reference(const std::string&, Btype*, Location);
329 // Labels.
331 Blabel*
332 label(Bfunction*, const std::string& name, Location);
334 Bstatement*
335 label_definition_statement(Blabel*);
337 Bstatement*
338 goto_statement(Blabel*, Location);
340 Bexpression*
341 label_address(Blabel*, Location);
343 // Functions.
345 Bfunction*
346 error_function()
347 { return this->make_function(error_mark_node); }
349 Bfunction*
350 function(Btype* fntype, const std::string& name, const std::string& asm_name,
351 bool is_visible, bool is_declaration, bool is_inlinable,
352 bool disable_split_stack, bool in_unique_section, Location);
354 private:
355 // Make a Bexpression from a tree.
356 Bexpression*
357 make_expression(tree t)
358 { return new Bexpression(t); }
360 // Make a Bstatement from a tree.
361 Bstatement*
362 make_statement(tree t)
363 { return new Bstatement(t); }
365 // Make a Btype from a tree.
366 Btype*
367 make_type(tree t)
368 { return new Btype(t); }
370 Bfunction*
371 make_function(tree t)
372 { return new Bfunction(t); }
374 Btype*
375 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
377 Btype*
378 fill_in_array(Btype*, Btype*, Bexpression*);
380 tree
381 non_zero_size_type(tree);
384 // A helper function.
386 static inline tree
387 get_identifier_from_string(const std::string& str)
389 return get_identifier_with_length(str.data(), str.length());
392 // Get an unnamed integer type.
394 Btype*
395 Gcc_backend::integer_type(bool is_unsigned, int bits)
397 tree type;
398 if (is_unsigned)
400 if (bits == INT_TYPE_SIZE)
401 type = unsigned_type_node;
402 else if (bits == CHAR_TYPE_SIZE)
403 type = unsigned_char_type_node;
404 else if (bits == SHORT_TYPE_SIZE)
405 type = short_unsigned_type_node;
406 else if (bits == LONG_TYPE_SIZE)
407 type = long_unsigned_type_node;
408 else if (bits == LONG_LONG_TYPE_SIZE)
409 type = long_long_unsigned_type_node;
410 else
411 type = make_unsigned_type(bits);
413 else
415 if (bits == INT_TYPE_SIZE)
416 type = integer_type_node;
417 else if (bits == CHAR_TYPE_SIZE)
418 type = signed_char_type_node;
419 else if (bits == SHORT_TYPE_SIZE)
420 type = short_integer_type_node;
421 else if (bits == LONG_TYPE_SIZE)
422 type = long_integer_type_node;
423 else if (bits == LONG_LONG_TYPE_SIZE)
424 type = long_long_integer_type_node;
425 else
426 type = make_signed_type(bits);
428 return this->make_type(type);
431 // Get an unnamed float type.
433 Btype*
434 Gcc_backend::float_type(int bits)
436 tree type;
437 if (bits == FLOAT_TYPE_SIZE)
438 type = float_type_node;
439 else if (bits == DOUBLE_TYPE_SIZE)
440 type = double_type_node;
441 else if (bits == LONG_DOUBLE_TYPE_SIZE)
442 type = long_double_type_node;
443 else
445 type = make_node(REAL_TYPE);
446 TYPE_PRECISION(type) = bits;
447 layout_type(type);
449 return this->make_type(type);
452 // Get an unnamed complex type.
454 Btype*
455 Gcc_backend::complex_type(int bits)
457 tree type;
458 if (bits == FLOAT_TYPE_SIZE * 2)
459 type = complex_float_type_node;
460 else if (bits == DOUBLE_TYPE_SIZE * 2)
461 type = complex_double_type_node;
462 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
463 type = complex_long_double_type_node;
464 else
466 type = make_node(REAL_TYPE);
467 TYPE_PRECISION(type) = bits / 2;
468 layout_type(type);
469 type = build_complex_type(type);
471 return this->make_type(type);
474 // Get a pointer type.
476 Btype*
477 Gcc_backend::pointer_type(Btype* to_type)
479 tree to_type_tree = to_type->get_tree();
480 if (to_type_tree == error_mark_node)
481 return this->error_type();
482 tree type = build_pointer_type(to_type_tree);
483 return this->make_type(type);
486 // Make a function type.
488 Btype*
489 Gcc_backend::function_type(const Btyped_identifier& receiver,
490 const std::vector<Btyped_identifier>& parameters,
491 const std::vector<Btyped_identifier>& results,
492 Location location)
494 tree args = NULL_TREE;
495 tree* pp = &args;
496 if (receiver.btype != NULL)
498 tree t = receiver.btype->get_tree();
499 if (t == error_mark_node)
500 return this->error_type();
501 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
502 pp = &TREE_CHAIN(*pp);
505 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
506 p != parameters.end();
507 ++p)
509 tree t = p->btype->get_tree();
510 if (t == error_mark_node)
511 return this->error_type();
512 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
513 pp = &TREE_CHAIN(*pp);
516 // Varargs is handled entirely at the Go level. When converted to
517 // GENERIC functions are not varargs.
518 *pp = void_list_node;
520 tree result;
521 if (results.empty())
522 result = void_type_node;
523 else if (results.size() == 1)
524 result = results.front().btype->get_tree();
525 else
527 result = make_node(RECORD_TYPE);
528 tree field_trees = NULL_TREE;
529 pp = &field_trees;
530 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
531 p != results.end();
532 ++p)
534 const std::string name = (p->name.empty()
535 ? "UNNAMED"
536 : p->name);
537 tree name_tree = get_identifier_from_string(name);
538 tree field_type_tree = p->btype->get_tree();
539 if (field_type_tree == error_mark_node)
540 return this->error_type();
541 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
542 tree field = build_decl(location.gcc_location(), FIELD_DECL,
543 name_tree, field_type_tree);
544 DECL_CONTEXT(field) = result;
545 *pp = field;
546 pp = &DECL_CHAIN(field);
548 TYPE_FIELDS(result) = field_trees;
549 layout_type(result);
551 if (result == error_mark_node)
552 return this->error_type();
554 tree fntype = build_function_type(result, args);
555 if (fntype == error_mark_node)
556 return this->error_type();
558 return this->make_type(build_pointer_type(fntype));
561 // Make a struct type.
563 Btype*
564 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
566 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
569 // Fill in the fields of a struct type.
571 Btype*
572 Gcc_backend::fill_in_struct(Btype* fill,
573 const std::vector<Btyped_identifier>& fields)
575 tree fill_tree = fill->get_tree();
576 tree field_trees = NULL_TREE;
577 tree* pp = &field_trees;
578 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
579 p != fields.end();
580 ++p)
582 tree name_tree = get_identifier_from_string(p->name);
583 tree type_tree = p->btype->get_tree();
584 if (type_tree == error_mark_node)
585 return this->error_type();
586 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
587 type_tree);
588 DECL_CONTEXT(field) = fill_tree;
589 *pp = field;
590 pp = &DECL_CHAIN(field);
592 TYPE_FIELDS(fill_tree) = field_trees;
593 layout_type(fill_tree);
594 return fill;
597 // Make an array type.
599 Btype*
600 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
602 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
603 element_btype, length);
606 // Fill in an array type.
608 Btype*
609 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
610 Bexpression* length)
612 tree element_type_tree = element_type->get_tree();
613 tree length_tree = length->get_tree();
614 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
615 return this->error_type();
617 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
619 length_tree = fold_convert(sizetype, length_tree);
621 // build_index_type takes the maximum index, which is one less than
622 // the length.
623 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
624 length_tree,
625 size_one_node));
627 tree fill_tree = fill->get_tree();
628 TREE_TYPE(fill_tree) = element_type_tree;
629 TYPE_DOMAIN(fill_tree) = index_type_tree;
630 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
631 layout_type(fill_tree);
633 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
634 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
635 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
636 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
637 TYPE_CANONICAL(fill_tree) =
638 build_array_type(TYPE_CANONICAL(element_type_tree),
639 TYPE_CANONICAL(index_type_tree));
641 return fill;
644 // Create a placeholder for a pointer type.
646 Btype*
647 Gcc_backend::placeholder_pointer_type(const std::string& name,
648 Location location, bool)
650 tree ret = build_distinct_type_copy(ptr_type_node);
651 if (!name.empty())
653 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
654 get_identifier_from_string(name),
655 ret);
656 TYPE_NAME(ret) = decl;
658 return this->make_type(ret);
661 // Set the real target type for a placeholder pointer type.
663 bool
664 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
665 Btype* to_type)
667 tree pt = placeholder->get_tree();
668 if (pt == error_mark_node)
669 return false;
670 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
671 tree tt = to_type->get_tree();
672 if (tt == error_mark_node)
674 placeholder->set_tree(error_mark_node);
675 return false;
677 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
678 TREE_TYPE(pt) = TREE_TYPE(tt);
679 if (TYPE_NAME(pt) != NULL_TREE)
681 // Build the data structure gcc wants to see for a typedef.
682 tree copy = build_variant_type_copy(pt);
683 TYPE_NAME(copy) = NULL_TREE;
684 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
686 return true;
689 // Set the real values for a placeholder function type.
691 bool
692 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
694 return this->set_placeholder_pointer_type(placeholder, ft);
697 // Create a placeholder for a struct type.
699 Btype*
700 Gcc_backend::placeholder_struct_type(const std::string& name,
701 Location location)
703 tree ret = make_node(RECORD_TYPE);
704 if (!name.empty())
706 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
707 get_identifier_from_string(name),
708 ret);
709 TYPE_NAME(ret) = decl;
711 return this->make_type(ret);
714 // Fill in the fields of a placeholder struct type.
716 bool
717 Gcc_backend::set_placeholder_struct_type(
718 Btype* placeholder,
719 const std::vector<Btyped_identifier>& fields)
721 tree t = placeholder->get_tree();
722 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
723 Btype* r = this->fill_in_struct(placeholder, fields);
725 if (TYPE_NAME(t) != NULL_TREE)
727 // Build the data structure gcc wants to see for a typedef.
728 tree copy = build_distinct_type_copy(t);
729 TYPE_NAME(copy) = NULL_TREE;
730 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
733 return r->get_tree() != error_mark_node;
736 // Create a placeholder for an array type.
738 Btype*
739 Gcc_backend::placeholder_array_type(const std::string& name,
740 Location location)
742 tree ret = make_node(ARRAY_TYPE);
743 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
744 get_identifier_from_string(name),
745 ret);
746 TYPE_NAME(ret) = decl;
747 return this->make_type(ret);
750 // Fill in the fields of a placeholder array type.
752 bool
753 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
754 Btype* element_btype,
755 Bexpression* length)
757 tree t = placeholder->get_tree();
758 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
759 Btype* r = this->fill_in_array(placeholder, element_btype, length);
761 // Build the data structure gcc wants to see for a typedef.
762 tree copy = build_distinct_type_copy(t);
763 TYPE_NAME(copy) = NULL_TREE;
764 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
766 return r->get_tree() != error_mark_node;
769 // Return a named version of a type.
771 Btype*
772 Gcc_backend::named_type(const std::string& name, Btype* btype,
773 Location location)
775 tree type = btype->get_tree();
776 if (type == error_mark_node)
777 return this->error_type();
779 // The middle-end expects a basic type to have a name. In Go every
780 // basic type will have a name. The first time we see a basic type,
781 // give it whatever Go name we have at this point.
782 if (TYPE_NAME(type) == NULL_TREE
783 && location.gcc_location() == BUILTINS_LOCATION
784 && (TREE_CODE(type) == INTEGER_TYPE
785 || TREE_CODE(type) == REAL_TYPE
786 || TREE_CODE(type) == COMPLEX_TYPE
787 || TREE_CODE(type) == BOOLEAN_TYPE))
789 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
790 get_identifier_from_string(name),
791 type);
792 TYPE_NAME(type) = decl;
793 return this->make_type(type);
796 tree copy = build_variant_type_copy(type);
797 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
798 get_identifier_from_string(name),
799 copy);
800 DECL_ORIGINAL_TYPE(decl) = type;
801 TYPE_NAME(copy) = decl;
802 return this->make_type(copy);
805 // Return a pointer type used as a marker for a circular type.
807 Btype*
808 Gcc_backend::circular_pointer_type(Btype*, bool)
810 return this->make_type(ptr_type_node);
813 // Return whether we might be looking at a circular type.
815 bool
816 Gcc_backend::is_circular_pointer_type(Btype* btype)
818 return btype->get_tree() == ptr_type_node;
821 // Return the size of a type.
823 size_t
824 Gcc_backend::type_size(Btype* btype)
826 tree t = btype->get_tree();
827 if (t == error_mark_node)
828 return 1;
829 t = TYPE_SIZE_UNIT(t);
830 gcc_assert(TREE_CODE(t) == INTEGER_CST);
831 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
832 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
833 size_t ret = static_cast<size_t>(val_wide);
834 gcc_assert(ret == val_wide);
835 return ret;
838 // Return the alignment of a type.
840 size_t
841 Gcc_backend::type_alignment(Btype* btype)
843 tree t = btype->get_tree();
844 if (t == error_mark_node)
845 return 1;
846 return TYPE_ALIGN_UNIT(t);
849 // Return the alignment of a struct field of type BTYPE.
851 size_t
852 Gcc_backend::type_field_alignment(Btype* btype)
854 tree t = btype->get_tree();
855 if (t == error_mark_node)
856 return 1;
857 return go_field_alignment(t);
860 // Return the offset of a field in a struct.
862 size_t
863 Gcc_backend::type_field_offset(Btype* btype, size_t index)
865 tree struct_tree = btype->get_tree();
866 if (struct_tree == error_mark_node)
867 return 0;
868 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
869 tree field = TYPE_FIELDS(struct_tree);
870 for (; index > 0; --index)
872 field = DECL_CHAIN(field);
873 gcc_assert(field != NULL_TREE);
875 HOST_WIDE_INT offset_wide = int_byte_position(field);
876 gcc_assert(offset_wide >= 0);
877 size_t ret = static_cast<size_t>(offset_wide);
878 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
879 return ret;
882 // Return the zero value for a type.
884 Bexpression*
885 Gcc_backend::zero_expression(Btype* btype)
887 tree t = btype->get_tree();
888 tree ret;
889 if (t == error_mark_node)
890 ret = error_mark_node;
891 else
892 ret = build_zero_cst(t);
893 return tree_to_expr(ret);
896 // An expression that references a variable.
898 Bexpression*
899 Gcc_backend::var_expression(Bvariable* var, Location)
901 tree ret = var->get_tree();
902 if (ret == error_mark_node)
903 return this->error_expression();
904 return tree_to_expr(ret);
907 // An expression that indirectly references an expression.
909 Bexpression*
910 Gcc_backend::indirect_expression(Bexpression* expr, bool known_valid,
911 Location location)
913 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
914 expr->get_tree());
915 if (known_valid)
916 TREE_THIS_NOTRAP(ret) = 1;
917 return tree_to_expr(ret);
920 // Return a typed value as a constant integer.
922 Bexpression*
923 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
925 tree t = btype->get_tree();
926 if (t == error_mark_node)
927 return this->error_expression();
929 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
930 return tree_to_expr(ret);
933 // Return a typed value as a constant floating-point number.
935 Bexpression*
936 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
938 tree t = btype->get_tree();
939 tree ret;
940 if (t == error_mark_node)
941 return this->error_expression();
943 REAL_VALUE_TYPE r1;
944 real_from_mpfr(&r1, val, t, GMP_RNDN);
945 REAL_VALUE_TYPE r2;
946 real_convert(&r2, TYPE_MODE(t), &r1);
947 ret = build_real(t, r2);
948 return tree_to_expr(ret);
951 // Return a typed real and imaginary value as a constant complex number.
953 Bexpression*
954 Gcc_backend::complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag)
956 tree t = btype->get_tree();
957 tree ret;
958 if (t == error_mark_node)
959 return this->error_expression();
961 REAL_VALUE_TYPE r1;
962 real_from_mpfr(&r1, real, TREE_TYPE(t), GMP_RNDN);
963 REAL_VALUE_TYPE r2;
964 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
966 REAL_VALUE_TYPE r3;
967 real_from_mpfr(&r3, imag, TREE_TYPE(t), GMP_RNDN);
968 REAL_VALUE_TYPE r4;
969 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
971 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
972 build_real(TREE_TYPE(t), r4));
973 return tree_to_expr(ret);
976 // An expression that converts an expression to a different type.
978 Bexpression*
979 Gcc_backend::convert_expression(Btype* type, Bexpression* expr, Location)
981 tree type_tree = type->get_tree();
982 tree expr_tree = expr->get_tree();
983 if (type_tree == error_mark_node || expr_tree == error_mark_node)
984 return this->error_expression();
986 tree ret = fold_convert(type_tree, expr_tree);
987 return tree_to_expr(ret);
990 // Get the address of a function.
992 Bexpression*
993 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
995 tree func = bfunc->get_tree();
996 if (func == error_mark_node)
997 return this->error_expression();
999 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1000 return this->make_expression(ret);
1003 // Get the address of an expression.
1005 Bexpression*
1006 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1008 tree expr = bexpr->get_tree();
1009 if (expr == error_mark_node)
1010 return this->error_expression();
1012 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1013 return this->make_expression(ret);
1016 // An expression as a statement.
1018 Bstatement*
1019 Gcc_backend::expression_statement(Bexpression* expr)
1021 return this->make_statement(expr->get_tree());
1024 // Variable initialization.
1026 Bstatement*
1027 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1029 tree var_tree = var->get_tree();
1030 tree init_tree = init->get_tree();
1031 if (var_tree == error_mark_node || init_tree == error_mark_node)
1032 return this->error_statement();
1033 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1035 // To avoid problems with GNU ld, we don't make zero-sized
1036 // externally visible variables. That might lead us to doing an
1037 // initialization of a zero-sized expression to a non-zero sized
1038 // variable, or vice-versa. Avoid crashes by omitting the
1039 // initializer. Such initializations don't mean anything anyhow.
1040 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1041 && init_tree != NULL_TREE
1042 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1044 DECL_INITIAL(var_tree) = init_tree;
1045 init_tree = NULL_TREE;
1048 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1049 void_type_node, var_tree);
1050 if (init_tree != NULL_TREE)
1051 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1052 void_type_node, init_tree, ret);
1054 return this->make_statement(ret);
1057 // Assignment.
1059 Bstatement*
1060 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1061 Location location)
1063 tree lhs_tree = lhs->get_tree();
1064 tree rhs_tree = rhs->get_tree();
1065 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1066 return this->error_statement();
1068 // To avoid problems with GNU ld, we don't make zero-sized
1069 // externally visible variables. That might lead us to doing an
1070 // assignment of a zero-sized expression to a non-zero sized
1071 // expression; avoid crashes here by avoiding assignments of
1072 // zero-sized expressions. Such assignments don't really mean
1073 // anything anyhow.
1074 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1075 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1076 return this->compound_statement(this->expression_statement(lhs),
1077 this->expression_statement(rhs));
1079 // Sometimes the same unnamed Go type can be created multiple times
1080 // and thus have multiple tree representations. Make sure this does
1081 // not confuse the middle-end.
1082 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1084 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1085 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1086 if (POINTER_TYPE_P(lhs_type_tree)
1087 || INTEGRAL_TYPE_P(lhs_type_tree)
1088 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1089 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1090 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1091 rhs_tree);
1092 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1093 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1095 gcc_assert(int_size_in_bytes(lhs_type_tree)
1096 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1097 rhs_tree = fold_build1_loc(location.gcc_location(),
1098 VIEW_CONVERT_EXPR,
1099 lhs_type_tree, rhs_tree);
1103 return this->make_statement(fold_build2_loc(location.gcc_location(),
1104 MODIFY_EXPR,
1105 void_type_node,
1106 lhs_tree, rhs_tree));
1109 // Return.
1111 Bstatement*
1112 Gcc_backend::return_statement(Bfunction* bfunction,
1113 const std::vector<Bexpression*>& vals,
1114 Location location)
1116 tree fntree = bfunction->get_tree();
1117 if (fntree == error_mark_node)
1118 return this->error_statement();
1119 tree result = DECL_RESULT(fntree);
1120 if (result == error_mark_node)
1121 return this->error_statement();
1122 tree ret;
1123 if (vals.empty())
1124 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
1125 NULL_TREE);
1126 else if (vals.size() == 1)
1128 tree val = vals.front()->get_tree();
1129 if (val == error_mark_node)
1130 return this->error_statement();
1131 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1132 void_type_node, result,
1133 vals.front()->get_tree());
1134 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1135 void_type_node, set);
1137 else
1139 // To return multiple values, copy the values into a temporary
1140 // variable of the right structure type, and then assign the
1141 // temporary variable to the DECL_RESULT in the return
1142 // statement.
1143 tree stmt_list = NULL_TREE;
1144 tree rettype = TREE_TYPE(result);
1145 tree rettmp = create_tmp_var(rettype, "RESULT");
1146 tree field = TYPE_FIELDS(rettype);
1147 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1148 p != vals.end();
1149 p++, field = DECL_CHAIN(field))
1151 gcc_assert(field != NULL_TREE);
1152 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1153 TREE_TYPE(field), rettmp, field,
1154 NULL_TREE);
1155 tree val = (*p)->get_tree();
1156 if (val == error_mark_node)
1157 return this->error_statement();
1158 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1159 void_type_node,
1160 ref, (*p)->get_tree());
1161 append_to_statement_list(set, &stmt_list);
1163 gcc_assert(field == NULL_TREE);
1164 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1165 void_type_node,
1166 result, rettmp);
1167 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1168 void_type_node, set);
1169 append_to_statement_list(ret_expr, &stmt_list);
1170 ret = stmt_list;
1172 return this->make_statement(ret);
1175 // If.
1177 Bstatement*
1178 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
1179 Bblock* else_block, Location location)
1181 tree cond_tree = condition->get_tree();
1182 tree then_tree = then_block->get_tree();
1183 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
1184 if (cond_tree == error_mark_node
1185 || then_tree == error_mark_node
1186 || else_tree == error_mark_node)
1187 return this->error_statement();
1188 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
1189 cond_tree, then_tree, else_tree);
1190 return this->make_statement(ret);
1193 // Switch.
1195 Bstatement*
1196 Gcc_backend::switch_statement(
1197 Bexpression* value,
1198 const std::vector<std::vector<Bexpression*> >& cases,
1199 const std::vector<Bstatement*>& statements,
1200 Location switch_location)
1202 gcc_assert(cases.size() == statements.size());
1204 tree stmt_list = NULL_TREE;
1205 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
1206 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
1207 ps != statements.end();
1208 ++ps, ++pc)
1210 if (pc->empty())
1212 source_location loc = (*ps != NULL
1213 ? EXPR_LOCATION((*ps)->get_tree())
1214 : UNKNOWN_LOCATION);
1215 tree label = create_artificial_label(loc);
1216 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
1217 append_to_statement_list(c, &stmt_list);
1219 else
1221 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1222 pcv != pc->end();
1223 ++pcv)
1225 tree t = (*pcv)->get_tree();
1226 if (t == error_mark_node)
1227 return this->error_statement();
1228 source_location loc = EXPR_LOCATION(t);
1229 tree label = create_artificial_label(loc);
1230 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1231 append_to_statement_list(c, &stmt_list);
1235 if (*ps != NULL)
1237 tree t = (*ps)->get_tree();
1238 if (t == error_mark_node)
1239 return this->error_statement();
1240 append_to_statement_list(t, &stmt_list);
1244 tree tv = value->get_tree();
1245 if (tv == error_mark_node)
1246 return this->error_statement();
1247 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1248 NULL_TREE, tv, stmt_list, NULL_TREE);
1249 return this->make_statement(t);
1252 // Pair of statements.
1254 Bstatement*
1255 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1257 tree stmt_list = NULL_TREE;
1258 tree t = s1->get_tree();
1259 if (t == error_mark_node)
1260 return this->error_statement();
1261 append_to_statement_list(t, &stmt_list);
1262 t = s2->get_tree();
1263 if (t == error_mark_node)
1264 return this->error_statement();
1265 append_to_statement_list(t, &stmt_list);
1266 return this->make_statement(stmt_list);
1269 // List of statements.
1271 Bstatement*
1272 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1274 tree stmt_list = NULL_TREE;
1275 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1276 p != statements.end();
1277 ++p)
1279 tree t = (*p)->get_tree();
1280 if (t == error_mark_node)
1281 return this->error_statement();
1282 append_to_statement_list(t, &stmt_list);
1284 return this->make_statement(stmt_list);
1287 // Make a block. For some reason gcc uses a dual structure for
1288 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1289 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1290 // the Bblock.
1292 Bblock*
1293 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1294 const std::vector<Bvariable*>& vars,
1295 Location start_location,
1296 Location)
1298 tree block_tree = make_node(BLOCK);
1299 if (enclosing == NULL)
1301 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1302 // until we have a proper representation of the init function.
1303 tree fndecl;
1304 if (function == NULL)
1305 fndecl = current_function_decl;
1306 else
1307 fndecl = function->get_tree();
1308 gcc_assert(fndecl != NULL_TREE);
1310 // We may have already created a block for local variables when
1311 // we take the address of a parameter.
1312 if (DECL_INITIAL(fndecl) == NULL_TREE)
1314 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1315 DECL_INITIAL(fndecl) = block_tree;
1317 else
1319 tree superblock_tree = DECL_INITIAL(fndecl);
1320 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1321 tree* pp;
1322 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1323 *pp != NULL_TREE;
1324 pp = &BLOCK_CHAIN(*pp))
1326 *pp = block_tree;
1329 else
1331 tree superbind_tree = enclosing->get_tree();
1332 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1333 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1335 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1336 tree* pp;
1337 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1338 *pp != NULL_TREE;
1339 pp = &BLOCK_CHAIN(*pp))
1341 *pp = block_tree;
1344 tree* pp = &BLOCK_VARS(block_tree);
1345 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1346 pv != vars.end();
1347 ++pv)
1349 *pp = (*pv)->get_tree();
1350 if (*pp != error_mark_node)
1351 pp = &DECL_CHAIN(*pp);
1353 *pp = NULL_TREE;
1355 TREE_USED(block_tree) = 1;
1357 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1358 void_type_node, BLOCK_VARS(block_tree),
1359 NULL_TREE, block_tree);
1360 TREE_SIDE_EFFECTS(bind_tree) = 1;
1362 return new Bblock(bind_tree);
1365 // Add statements to a block.
1367 void
1368 Gcc_backend::block_add_statements(Bblock* bblock,
1369 const std::vector<Bstatement*>& statements)
1371 tree stmt_list = NULL_TREE;
1372 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1373 p != statements.end();
1374 ++p)
1376 tree s = (*p)->get_tree();
1377 if (s != error_mark_node)
1378 append_to_statement_list(s, &stmt_list);
1381 tree bind_tree = bblock->get_tree();
1382 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1383 BIND_EXPR_BODY(bind_tree) = stmt_list;
1386 // Return a block as a statement.
1388 Bstatement*
1389 Gcc_backend::block_statement(Bblock* bblock)
1391 tree bind_tree = bblock->get_tree();
1392 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1393 return this->make_statement(bind_tree);
1396 // This is not static because we declare it with GTY(()) in go-c.h.
1397 tree go_non_zero_struct;
1399 // Return a type corresponding to TYPE with non-zero size.
1401 tree
1402 Gcc_backend::non_zero_size_type(tree type)
1404 if (int_size_in_bytes(type) != 0)
1405 return type;
1407 switch (TREE_CODE(type))
1409 case RECORD_TYPE:
1410 if (TYPE_FIELDS(type) != NULL_TREE)
1412 tree ns = make_node(RECORD_TYPE);
1413 tree field_trees = NULL_TREE;
1414 tree *pp = &field_trees;
1415 for (tree field = TYPE_FIELDS(type);
1416 field != NULL_TREE;
1417 field = DECL_CHAIN(field))
1419 tree ft = TREE_TYPE(field);
1420 if (field == TYPE_FIELDS(type))
1421 ft = non_zero_size_type(ft);
1422 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
1423 DECL_NAME(field), ft);
1424 DECL_CONTEXT(f) = ns;
1425 *pp = f;
1426 pp = &DECL_CHAIN(f);
1428 TYPE_FIELDS(ns) = field_trees;
1429 layout_type(ns);
1430 return ns;
1433 if (go_non_zero_struct == NULL_TREE)
1435 type = make_node(RECORD_TYPE);
1436 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
1437 get_identifier("dummy"),
1438 boolean_type_node);
1439 DECL_CONTEXT(field) = type;
1440 TYPE_FIELDS(type) = field;
1441 layout_type(type);
1442 go_non_zero_struct = type;
1444 return go_non_zero_struct;
1446 case ARRAY_TYPE:
1448 tree element_type = non_zero_size_type(TREE_TYPE(type));
1449 return build_array_type_nelts(element_type, 1);
1452 default:
1453 gcc_unreachable();
1456 gcc_unreachable();
1459 // Make a global variable.
1461 Bvariable*
1462 Gcc_backend::global_variable(const std::string& package_name,
1463 const std::string& pkgpath,
1464 const std::string& name,
1465 Btype* btype,
1466 bool is_external,
1467 bool is_hidden,
1468 bool in_unique_section,
1469 Location location)
1471 tree type_tree = btype->get_tree();
1472 if (type_tree == error_mark_node)
1473 return this->error_variable();
1475 // The GNU linker does not like dynamic variables with zero size.
1476 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
1477 type_tree = this->non_zero_size_type(type_tree);
1479 std::string var_name(package_name);
1480 var_name.push_back('.');
1481 var_name.append(name);
1482 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1483 get_identifier_from_string(var_name),
1484 type_tree);
1485 if (is_external)
1486 DECL_EXTERNAL(decl) = 1;
1487 else
1488 TREE_STATIC(decl) = 1;
1489 if (!is_hidden)
1491 TREE_PUBLIC(decl) = 1;
1493 std::string asm_name(pkgpath);
1494 asm_name.push_back('.');
1495 asm_name.append(name);
1496 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1498 TREE_USED(decl) = 1;
1500 if (in_unique_section)
1501 resolve_unique_section (decl, 0, 1);
1503 go_preserve_from_gc(decl);
1505 return new Bvariable(decl);
1508 // Set the initial value of a global variable.
1510 void
1511 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1513 tree expr_tree = expr->get_tree();
1514 if (expr_tree == error_mark_node)
1515 return;
1516 gcc_assert(TREE_CONSTANT(expr_tree));
1517 tree var_decl = var->get_tree();
1518 if (var_decl == error_mark_node)
1519 return;
1520 DECL_INITIAL(var_decl) = expr_tree;
1522 // If this variable goes in a unique section, it may need to go into
1523 // a different one now that DECL_INITIAL is set.
1524 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl))
1526 DECL_SECTION_NAME (var_decl) = NULL_TREE;
1527 resolve_unique_section (var_decl,
1528 compute_reloc_for_constant (expr_tree),
1533 // Make a local variable.
1535 Bvariable*
1536 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1537 Btype* btype, bool is_address_taken,
1538 Location location)
1540 tree type_tree = btype->get_tree();
1541 if (type_tree == error_mark_node)
1542 return this->error_variable();
1543 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1544 get_identifier_from_string(name),
1545 type_tree);
1546 DECL_CONTEXT(decl) = function->get_tree();
1547 TREE_USED(decl) = 1;
1548 if (is_address_taken)
1549 TREE_ADDRESSABLE(decl) = 1;
1550 go_preserve_from_gc(decl);
1551 return new Bvariable(decl);
1554 // Make a function parameter variable.
1556 Bvariable*
1557 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1558 Btype* btype, bool is_address_taken,
1559 Location location)
1561 tree type_tree = btype->get_tree();
1562 if (type_tree == error_mark_node)
1563 return this->error_variable();
1564 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1565 get_identifier_from_string(name),
1566 type_tree);
1567 DECL_CONTEXT(decl) = function->get_tree();
1568 DECL_ARG_TYPE(decl) = type_tree;
1569 TREE_USED(decl) = 1;
1570 if (is_address_taken)
1571 TREE_ADDRESSABLE(decl) = 1;
1572 go_preserve_from_gc(decl);
1573 return new Bvariable(decl);
1576 // Make a temporary variable.
1578 Bvariable*
1579 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1580 Btype* btype, Bexpression* binit,
1581 bool is_address_taken,
1582 Location location,
1583 Bstatement** pstatement)
1585 tree type_tree = btype->get_tree();
1586 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1587 if (type_tree == error_mark_node || init_tree == error_mark_node)
1589 *pstatement = this->error_statement();
1590 return this->error_variable();
1593 tree var;
1594 // We can only use create_tmp_var if the type is not addressable.
1595 if (!TREE_ADDRESSABLE(type_tree))
1596 var = create_tmp_var(type_tree, "GOTMP");
1597 else
1599 gcc_assert(bblock != NULL);
1600 var = build_decl(location.gcc_location(), VAR_DECL,
1601 create_tmp_var_name("GOTMP"),
1602 type_tree);
1603 DECL_ARTIFICIAL(var) = 1;
1604 DECL_IGNORED_P(var) = 1;
1605 TREE_USED(var) = 1;
1606 // FIXME: Permitting function to be NULL here is a temporary
1607 // measure until we have a proper representation of the init
1608 // function.
1609 if (function != NULL)
1610 DECL_CONTEXT(var) = function->get_tree();
1611 else
1613 gcc_assert(current_function_decl != NULL_TREE);
1614 DECL_CONTEXT(var) = current_function_decl;
1617 // We have to add this variable to the BLOCK and the BIND_EXPR.
1618 tree bind_tree = bblock->get_tree();
1619 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1620 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1621 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1622 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1623 BLOCK_VARS(block_tree) = var;
1624 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1627 if (init_tree != NULL_TREE)
1628 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1629 init_tree);
1631 if (is_address_taken)
1632 TREE_ADDRESSABLE(var) = 1;
1634 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1635 DECL_EXPR,
1636 void_type_node, var));
1637 return new Bvariable(var);
1640 // Create a named immutable initialized data structure.
1642 Bvariable*
1643 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
1644 bool, Btype* btype, Location location)
1646 tree type_tree = btype->get_tree();
1647 if (type_tree == error_mark_node)
1648 return this->error_variable();
1649 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1650 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1651 get_identifier_from_string(name),
1652 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1653 TREE_STATIC(decl) = 1;
1654 TREE_READONLY(decl) = 1;
1655 TREE_CONSTANT(decl) = 1;
1656 TREE_USED(decl) = 1;
1657 DECL_ARTIFICIAL(decl) = 1;
1658 if (!is_hidden)
1659 TREE_PUBLIC(decl) = 1;
1661 // We don't call rest_of_decl_compilation until we have the
1662 // initializer.
1664 go_preserve_from_gc(decl);
1665 return new Bvariable(decl);
1668 // Set the initializer for a variable created by immutable_struct.
1669 // This is where we finish compiling the variable.
1671 void
1672 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1673 bool, bool is_common, Btype*, Location,
1674 Bexpression* initializer)
1676 tree decl = var->get_tree();
1677 tree init_tree = initializer->get_tree();
1678 if (decl == error_mark_node || init_tree == error_mark_node)
1679 return;
1681 DECL_INITIAL(decl) = init_tree;
1683 // We can't call make_decl_one_only until we set DECL_INITIAL.
1684 if (is_common)
1685 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1687 // These variables are often unneeded in the final program, so put
1688 // them in their own section so that linker GC can discard them.
1689 resolve_unique_section(decl,
1690 compute_reloc_for_constant (init_tree),
1693 rest_of_decl_compilation(decl, 1, 0);
1696 // Return a reference to an immutable initialized data structure
1697 // defined in another package.
1699 Bvariable*
1700 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1701 Location location)
1703 tree type_tree = btype->get_tree();
1704 if (type_tree == error_mark_node)
1705 return this->error_variable();
1706 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1707 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1708 get_identifier_from_string(name),
1709 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1710 TREE_READONLY(decl) = 1;
1711 TREE_CONSTANT(decl) = 1;
1712 DECL_ARTIFICIAL(decl) = 1;
1713 TREE_PUBLIC(decl) = 1;
1714 DECL_EXTERNAL(decl) = 1;
1715 go_preserve_from_gc(decl);
1716 return new Bvariable(decl);
1719 // Make a label.
1721 Blabel*
1722 Gcc_backend::label(Bfunction* function, const std::string& name,
1723 Location location)
1725 tree decl;
1726 if (name.empty())
1727 decl = create_artificial_label(location.gcc_location());
1728 else
1730 tree id = get_identifier_from_string(name);
1731 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1732 void_type_node);
1733 DECL_CONTEXT(decl) = function->get_tree();
1735 return new Blabel(decl);
1738 // Make a statement which defines a label.
1740 Bstatement*
1741 Gcc_backend::label_definition_statement(Blabel* label)
1743 tree lab = label->get_tree();
1744 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1745 void_type_node, lab);
1746 return this->make_statement(ret);
1749 // Make a goto statement.
1751 Bstatement*
1752 Gcc_backend::goto_statement(Blabel* label, Location location)
1754 tree lab = label->get_tree();
1755 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1756 lab);
1757 return this->make_statement(ret);
1760 // Get the address of a label.
1762 Bexpression*
1763 Gcc_backend::label_address(Blabel* label, Location location)
1765 tree lab = label->get_tree();
1766 TREE_USED(lab) = 1;
1767 TREE_ADDRESSABLE(lab) = 1;
1768 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1769 build_fold_addr_expr_loc(location.gcc_location(),
1770 lab));
1771 return this->make_expression(ret);
1774 // Declare or define a new function.
1776 Bfunction*
1777 Gcc_backend::function(Btype* fntype, const std::string& name,
1778 const std::string& asm_name, bool is_visible,
1779 bool is_declaration, bool is_inlinable,
1780 bool disable_split_stack, bool in_unique_section,
1781 Location location)
1783 tree functype = fntype->get_tree();
1784 if (functype != error_mark_node)
1786 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
1787 functype = TREE_TYPE(functype);
1789 tree id = get_identifier_from_string(name);
1790 if (functype == error_mark_node || id == error_mark_node)
1791 return this->error_function();
1793 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
1794 if (!asm_name.empty())
1795 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1796 if (is_visible)
1797 TREE_PUBLIC(decl) = 1;
1798 if (is_declaration)
1799 DECL_EXTERNAL(decl) = 1;
1800 else
1802 tree restype = TREE_TYPE(functype);
1803 tree resdecl =
1804 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
1805 DECL_ARTIFICIAL(resdecl) = 1;
1806 DECL_IGNORED_P(resdecl) = 1;
1807 DECL_CONTEXT(resdecl) = decl;
1808 DECL_RESULT(decl) = resdecl;
1810 if (!is_inlinable)
1811 DECL_UNINLINABLE(decl) = 1;
1812 if (disable_split_stack)
1814 tree attr = get_identifier("__no_split_stack__");
1815 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
1817 if (in_unique_section)
1818 resolve_unique_section(decl, 0, 1);
1820 go_preserve_from_gc(decl);
1821 return new Bfunction(decl);
1824 // The single backend.
1826 static Gcc_backend gcc_backend;
1828 // Return the backend generator.
1830 Backend*
1831 go_get_backend()
1833 return &gcc_backend;
1836 // FIXME: Temporary functions while converting to the new backend
1837 // interface.
1839 Btype*
1840 tree_to_type(tree t)
1842 return new Btype(t);
1845 Bexpression*
1846 tree_to_expr(tree t)
1848 return new Bexpression(t);
1851 Bstatement*
1852 tree_to_stat(tree t)
1854 return new Bstatement(t);
1857 Bfunction*
1858 tree_to_function(tree t)
1860 return new Bfunction(t);
1863 Bblock*
1864 tree_to_block(tree t)
1866 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1867 return new Bblock(t);
1870 tree
1871 type_to_tree(Btype* bt)
1873 return bt->get_tree();
1876 tree
1877 expr_to_tree(Bexpression* be)
1879 return be->get_tree();
1882 tree
1883 stat_to_tree(Bstatement* bs)
1885 return bs->get_tree();
1888 tree
1889 block_to_tree(Bblock* bb)
1891 return bb->get_tree();
1894 tree
1895 var_to_tree(Bvariable* bv)
1897 return bv->get_tree();
1900 tree
1901 function_to_tree(Bfunction* bf)
1903 return bf->get_tree();