Remove model 0x3f from Haswell
[official-gcc.git] / gcc / go / go-gcc.cc
blobdb8fd5e3355980443ab765c5c11ac935eaf4d3b1
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 "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 // Statements.
251 Bstatement*
252 error_statement()
253 { return this->make_statement(error_mark_node); }
255 Bstatement*
256 expression_statement(Bexpression*);
258 Bstatement*
259 init_statement(Bvariable* var, Bexpression* init);
261 Bstatement*
262 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
264 Bstatement*
265 return_statement(Bfunction*, const std::vector<Bexpression*>&,
266 Location);
268 Bstatement*
269 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
270 Location);
272 Bstatement*
273 switch_statement(Bexpression* value,
274 const std::vector<std::vector<Bexpression*> >& cases,
275 const std::vector<Bstatement*>& statements,
276 Location);
278 Bstatement*
279 compound_statement(Bstatement*, Bstatement*);
281 Bstatement*
282 statement_list(const std::vector<Bstatement*>&);
284 // Blocks.
286 Bblock*
287 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
288 Location, Location);
290 void
291 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
293 Bstatement*
294 block_statement(Bblock*);
296 // Variables.
298 Bvariable*
299 error_variable()
300 { return new Bvariable(error_mark_node); }
302 Bvariable*
303 global_variable(const std::string& package_name,
304 const std::string& pkgpath,
305 const std::string& name,
306 Btype* btype,
307 bool is_external,
308 bool is_hidden,
309 bool in_unique_section,
310 Location location);
312 void
313 global_variable_set_init(Bvariable*, Bexpression*);
315 Bvariable*
316 local_variable(Bfunction*, const std::string&, Btype*, bool,
317 Location);
319 Bvariable*
320 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
321 Location);
323 Bvariable*
324 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
325 Location, Bstatement**);
327 Bvariable*
328 immutable_struct(const std::string&, bool, bool, Btype*, Location);
330 void
331 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
332 Location, Bexpression*);
334 Bvariable*
335 immutable_struct_reference(const std::string&, Btype*, Location);
337 // Labels.
339 Blabel*
340 label(Bfunction*, const std::string& name, Location);
342 Bstatement*
343 label_definition_statement(Blabel*);
345 Bstatement*
346 goto_statement(Blabel*, Location);
348 Bexpression*
349 label_address(Blabel*, Location);
351 // Functions.
353 Bfunction*
354 error_function()
355 { return this->make_function(error_mark_node); }
357 Bfunction*
358 function(Btype* fntype, const std::string& name, const std::string& asm_name,
359 bool is_visible, bool is_declaration, bool is_inlinable,
360 bool disable_split_stack, bool in_unique_section, Location);
362 private:
363 // Make a Bexpression from a tree.
364 Bexpression*
365 make_expression(tree t)
366 { return new Bexpression(t); }
368 // Make a Bstatement from a tree.
369 Bstatement*
370 make_statement(tree t)
371 { return new Bstatement(t); }
373 // Make a Btype from a tree.
374 Btype*
375 make_type(tree t)
376 { return new Btype(t); }
378 Bfunction*
379 make_function(tree t)
380 { return new Bfunction(t); }
382 Btype*
383 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
385 Btype*
386 fill_in_array(Btype*, Btype*, Bexpression*);
388 tree
389 non_zero_size_type(tree);
392 // A helper function.
394 static inline tree
395 get_identifier_from_string(const std::string& str)
397 return get_identifier_with_length(str.data(), str.length());
400 // Get an unnamed integer type.
402 Btype*
403 Gcc_backend::integer_type(bool is_unsigned, int bits)
405 tree type;
406 if (is_unsigned)
408 if (bits == INT_TYPE_SIZE)
409 type = unsigned_type_node;
410 else if (bits == CHAR_TYPE_SIZE)
411 type = unsigned_char_type_node;
412 else if (bits == SHORT_TYPE_SIZE)
413 type = short_unsigned_type_node;
414 else if (bits == LONG_TYPE_SIZE)
415 type = long_unsigned_type_node;
416 else if (bits == LONG_LONG_TYPE_SIZE)
417 type = long_long_unsigned_type_node;
418 else
419 type = make_unsigned_type(bits);
421 else
423 if (bits == INT_TYPE_SIZE)
424 type = integer_type_node;
425 else if (bits == CHAR_TYPE_SIZE)
426 type = signed_char_type_node;
427 else if (bits == SHORT_TYPE_SIZE)
428 type = short_integer_type_node;
429 else if (bits == LONG_TYPE_SIZE)
430 type = long_integer_type_node;
431 else if (bits == LONG_LONG_TYPE_SIZE)
432 type = long_long_integer_type_node;
433 else
434 type = make_signed_type(bits);
436 return this->make_type(type);
439 // Get an unnamed float type.
441 Btype*
442 Gcc_backend::float_type(int bits)
444 tree type;
445 if (bits == FLOAT_TYPE_SIZE)
446 type = float_type_node;
447 else if (bits == DOUBLE_TYPE_SIZE)
448 type = double_type_node;
449 else if (bits == LONG_DOUBLE_TYPE_SIZE)
450 type = long_double_type_node;
451 else
453 type = make_node(REAL_TYPE);
454 TYPE_PRECISION(type) = bits;
455 layout_type(type);
457 return this->make_type(type);
460 // Get an unnamed complex type.
462 Btype*
463 Gcc_backend::complex_type(int bits)
465 tree type;
466 if (bits == FLOAT_TYPE_SIZE * 2)
467 type = complex_float_type_node;
468 else if (bits == DOUBLE_TYPE_SIZE * 2)
469 type = complex_double_type_node;
470 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
471 type = complex_long_double_type_node;
472 else
474 type = make_node(REAL_TYPE);
475 TYPE_PRECISION(type) = bits / 2;
476 layout_type(type);
477 type = build_complex_type(type);
479 return this->make_type(type);
482 // Get a pointer type.
484 Btype*
485 Gcc_backend::pointer_type(Btype* to_type)
487 tree to_type_tree = to_type->get_tree();
488 if (to_type_tree == error_mark_node)
489 return this->error_type();
490 tree type = build_pointer_type(to_type_tree);
491 return this->make_type(type);
494 // Make a function type.
496 Btype*
497 Gcc_backend::function_type(const Btyped_identifier& receiver,
498 const std::vector<Btyped_identifier>& parameters,
499 const std::vector<Btyped_identifier>& results,
500 Btype* result_struct,
501 Location)
503 tree args = NULL_TREE;
504 tree* pp = &args;
505 if (receiver.btype != NULL)
507 tree t = receiver.btype->get_tree();
508 if (t == error_mark_node)
509 return this->error_type();
510 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
511 pp = &TREE_CHAIN(*pp);
514 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
515 p != parameters.end();
516 ++p)
518 tree t = p->btype->get_tree();
519 if (t == error_mark_node)
520 return this->error_type();
521 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
522 pp = &TREE_CHAIN(*pp);
525 // Varargs is handled entirely at the Go level. When converted to
526 // GENERIC functions are not varargs.
527 *pp = void_list_node;
529 tree result;
530 if (results.empty())
531 result = void_type_node;
532 else if (results.size() == 1)
533 result = results.front().btype->get_tree();
534 else
536 gcc_assert(result_struct != NULL);
537 result = result_struct->get_tree();
539 if (result == error_mark_node)
540 return this->error_type();
542 tree fntype = build_function_type(result, args);
543 if (fntype == error_mark_node)
544 return this->error_type();
546 return this->make_type(build_pointer_type(fntype));
549 // Make a struct type.
551 Btype*
552 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
554 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
557 // Fill in the fields of a struct type.
559 Btype*
560 Gcc_backend::fill_in_struct(Btype* fill,
561 const std::vector<Btyped_identifier>& fields)
563 tree fill_tree = fill->get_tree();
564 tree field_trees = NULL_TREE;
565 tree* pp = &field_trees;
566 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
567 p != fields.end();
568 ++p)
570 tree name_tree = get_identifier_from_string(p->name);
571 tree type_tree = p->btype->get_tree();
572 if (type_tree == error_mark_node)
573 return this->error_type();
574 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
575 type_tree);
576 DECL_CONTEXT(field) = fill_tree;
577 *pp = field;
578 pp = &DECL_CHAIN(field);
580 TYPE_FIELDS(fill_tree) = field_trees;
581 layout_type(fill_tree);
582 return fill;
585 // Make an array type.
587 Btype*
588 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
590 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
591 element_btype, length);
594 // Fill in an array type.
596 Btype*
597 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
598 Bexpression* length)
600 tree element_type_tree = element_type->get_tree();
601 tree length_tree = length->get_tree();
602 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
603 return this->error_type();
605 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
607 length_tree = fold_convert(sizetype, length_tree);
609 // build_index_type takes the maximum index, which is one less than
610 // the length.
611 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
612 length_tree,
613 size_one_node));
615 tree fill_tree = fill->get_tree();
616 TREE_TYPE(fill_tree) = element_type_tree;
617 TYPE_DOMAIN(fill_tree) = index_type_tree;
618 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
619 layout_type(fill_tree);
621 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
622 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
623 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
624 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
625 TYPE_CANONICAL(fill_tree) =
626 build_array_type(TYPE_CANONICAL(element_type_tree),
627 TYPE_CANONICAL(index_type_tree));
629 return fill;
632 // Create a placeholder for a pointer type.
634 Btype*
635 Gcc_backend::placeholder_pointer_type(const std::string& name,
636 Location location, bool)
638 tree ret = build_distinct_type_copy(ptr_type_node);
639 if (!name.empty())
641 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
642 get_identifier_from_string(name),
643 ret);
644 TYPE_NAME(ret) = decl;
646 return this->make_type(ret);
649 // Set the real target type for a placeholder pointer type.
651 bool
652 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
653 Btype* to_type)
655 tree pt = placeholder->get_tree();
656 if (pt == error_mark_node)
657 return false;
658 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
659 tree tt = to_type->get_tree();
660 if (tt == error_mark_node)
662 placeholder->set_tree(error_mark_node);
663 return false;
665 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
666 TREE_TYPE(pt) = TREE_TYPE(tt);
667 if (TYPE_NAME(pt) != NULL_TREE)
669 // Build the data structure gcc wants to see for a typedef.
670 tree copy = build_variant_type_copy(pt);
671 TYPE_NAME(copy) = NULL_TREE;
672 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
674 return true;
677 // Set the real values for a placeholder function type.
679 bool
680 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
682 return this->set_placeholder_pointer_type(placeholder, ft);
685 // Create a placeholder for a struct type.
687 Btype*
688 Gcc_backend::placeholder_struct_type(const std::string& name,
689 Location location)
691 tree ret = make_node(RECORD_TYPE);
692 if (!name.empty())
694 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
695 get_identifier_from_string(name),
696 ret);
697 TYPE_NAME(ret) = decl;
699 return this->make_type(ret);
702 // Fill in the fields of a placeholder struct type.
704 bool
705 Gcc_backend::set_placeholder_struct_type(
706 Btype* placeholder,
707 const std::vector<Btyped_identifier>& fields)
709 tree t = placeholder->get_tree();
710 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
711 Btype* r = this->fill_in_struct(placeholder, fields);
713 if (TYPE_NAME(t) != NULL_TREE)
715 // Build the data structure gcc wants to see for a typedef.
716 tree copy = build_distinct_type_copy(t);
717 TYPE_NAME(copy) = NULL_TREE;
718 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
721 return r->get_tree() != error_mark_node;
724 // Create a placeholder for an array type.
726 Btype*
727 Gcc_backend::placeholder_array_type(const std::string& name,
728 Location location)
730 tree ret = make_node(ARRAY_TYPE);
731 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
732 get_identifier_from_string(name),
733 ret);
734 TYPE_NAME(ret) = decl;
735 return this->make_type(ret);
738 // Fill in the fields of a placeholder array type.
740 bool
741 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
742 Btype* element_btype,
743 Bexpression* length)
745 tree t = placeholder->get_tree();
746 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
747 Btype* r = this->fill_in_array(placeholder, element_btype, length);
749 // Build the data structure gcc wants to see for a typedef.
750 tree copy = build_distinct_type_copy(t);
751 TYPE_NAME(copy) = NULL_TREE;
752 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
754 return r->get_tree() != error_mark_node;
757 // Return a named version of a type.
759 Btype*
760 Gcc_backend::named_type(const std::string& name, Btype* btype,
761 Location location)
763 tree type = btype->get_tree();
764 if (type == error_mark_node)
765 return this->error_type();
767 // The middle-end expects a basic type to have a name. In Go every
768 // basic type will have a name. The first time we see a basic type,
769 // give it whatever Go name we have at this point.
770 if (TYPE_NAME(type) == NULL_TREE
771 && location.gcc_location() == BUILTINS_LOCATION
772 && (TREE_CODE(type) == INTEGER_TYPE
773 || TREE_CODE(type) == REAL_TYPE
774 || TREE_CODE(type) == COMPLEX_TYPE
775 || TREE_CODE(type) == BOOLEAN_TYPE))
777 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
778 get_identifier_from_string(name),
779 type);
780 TYPE_NAME(type) = decl;
781 return this->make_type(type);
784 tree copy = build_variant_type_copy(type);
785 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
786 get_identifier_from_string(name),
787 copy);
788 DECL_ORIGINAL_TYPE(decl) = type;
789 TYPE_NAME(copy) = decl;
790 return this->make_type(copy);
793 // Return a pointer type used as a marker for a circular type.
795 Btype*
796 Gcc_backend::circular_pointer_type(Btype*, bool)
798 return this->make_type(ptr_type_node);
801 // Return whether we might be looking at a circular type.
803 bool
804 Gcc_backend::is_circular_pointer_type(Btype* btype)
806 return btype->get_tree() == ptr_type_node;
809 // Return the size of a type.
811 size_t
812 Gcc_backend::type_size(Btype* btype)
814 tree t = btype->get_tree();
815 if (t == error_mark_node)
816 return 1;
817 t = TYPE_SIZE_UNIT(t);
818 gcc_assert(TREE_CODE(t) == INTEGER_CST);
819 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
820 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
821 size_t ret = static_cast<size_t>(val_wide);
822 gcc_assert(ret == val_wide);
823 return ret;
826 // Return the alignment of a type.
828 size_t
829 Gcc_backend::type_alignment(Btype* btype)
831 tree t = btype->get_tree();
832 if (t == error_mark_node)
833 return 1;
834 return TYPE_ALIGN_UNIT(t);
837 // Return the alignment of a struct field of type BTYPE.
839 size_t
840 Gcc_backend::type_field_alignment(Btype* btype)
842 tree t = btype->get_tree();
843 if (t == error_mark_node)
844 return 1;
845 return go_field_alignment(t);
848 // Return the offset of a field in a struct.
850 size_t
851 Gcc_backend::type_field_offset(Btype* btype, size_t index)
853 tree struct_tree = btype->get_tree();
854 if (struct_tree == error_mark_node)
855 return 0;
856 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
857 tree field = TYPE_FIELDS(struct_tree);
858 for (; index > 0; --index)
860 field = DECL_CHAIN(field);
861 gcc_assert(field != NULL_TREE);
863 HOST_WIDE_INT offset_wide = int_byte_position(field);
864 gcc_assert(offset_wide >= 0);
865 size_t ret = static_cast<size_t>(offset_wide);
866 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
867 return ret;
870 // Return the zero value for a type.
872 Bexpression*
873 Gcc_backend::zero_expression(Btype* btype)
875 tree t = btype->get_tree();
876 tree ret;
877 if (t == error_mark_node)
878 ret = error_mark_node;
879 else
880 ret = build_zero_cst(t);
881 return tree_to_expr(ret);
884 // An expression that references a variable.
886 Bexpression*
887 Gcc_backend::var_expression(Bvariable* var, Location)
889 tree ret = var->get_tree();
890 if (ret == error_mark_node)
891 return this->error_expression();
892 return tree_to_expr(ret);
895 // An expression that indirectly references an expression.
897 Bexpression*
898 Gcc_backend::indirect_expression(Bexpression* expr, bool known_valid,
899 Location location)
901 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
902 expr->get_tree());
903 if (known_valid)
904 TREE_THIS_NOTRAP(ret) = 1;
905 return tree_to_expr(ret);
908 // Return a typed value as a constant integer.
910 Bexpression*
911 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
913 tree t = btype->get_tree();
914 if (t == error_mark_node)
915 return this->error_expression();
917 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
918 return tree_to_expr(ret);
921 // Return a typed value as a constant floating-point number.
923 Bexpression*
924 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
926 tree t = btype->get_tree();
927 tree ret;
928 if (t == error_mark_node)
929 return this->error_expression();
931 REAL_VALUE_TYPE r1;
932 real_from_mpfr(&r1, val, t, GMP_RNDN);
933 REAL_VALUE_TYPE r2;
934 real_convert(&r2, TYPE_MODE(t), &r1);
935 ret = build_real(t, r2);
936 return tree_to_expr(ret);
939 // Return a typed real and imaginary value as a constant complex number.
941 Bexpression*
942 Gcc_backend::complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag)
944 tree t = btype->get_tree();
945 tree ret;
946 if (t == error_mark_node)
947 return this->error_expression();
949 REAL_VALUE_TYPE r1;
950 real_from_mpfr(&r1, real, TREE_TYPE(t), GMP_RNDN);
951 REAL_VALUE_TYPE r2;
952 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
954 REAL_VALUE_TYPE r3;
955 real_from_mpfr(&r3, imag, TREE_TYPE(t), GMP_RNDN);
956 REAL_VALUE_TYPE r4;
957 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
959 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
960 build_real(TREE_TYPE(t), r4));
961 return tree_to_expr(ret);
964 // An expression that converts an expression to a different type.
966 Bexpression*
967 Gcc_backend::convert_expression(Btype* type, Bexpression* expr, Location)
969 tree type_tree = type->get_tree();
970 tree expr_tree = expr->get_tree();
971 if (type_tree == error_mark_node || expr_tree == error_mark_node)
972 return this->error_expression();
974 tree ret = fold_convert(type_tree, expr_tree);
975 return tree_to_expr(ret);
978 // Get the address of a function.
980 Bexpression*
981 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
983 tree func = bfunc->get_tree();
984 if (func == error_mark_node)
985 return this->error_expression();
987 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
988 return this->make_expression(ret);
991 // Get the address of an expression.
993 Bexpression*
994 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
996 tree expr = bexpr->get_tree();
997 if (expr == error_mark_node)
998 return this->error_expression();
1000 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1001 return this->make_expression(ret);
1004 // Return an expression for the field at INDEX in BSTRUCT.
1006 Bexpression*
1007 Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
1008 Location location)
1010 tree struct_tree = bstruct->get_tree();
1011 if (struct_tree == error_mark_node
1012 || TREE_TYPE(struct_tree) == error_mark_node)
1013 return this->error_expression();
1014 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
1015 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
1016 if (field == NULL_TREE)
1018 // This can happen for a type which refers to itself indirectly
1019 // and then turns out to be erroneous.
1020 return this->error_expression();
1022 for (unsigned int i = index; i > 0; --i)
1024 field = DECL_CHAIN(field);
1025 gcc_assert(field != NULL_TREE);
1027 if (TREE_TYPE(field) == error_mark_node)
1028 return this->error_expression();
1029 tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1030 TREE_TYPE(field), struct_tree, field,
1031 NULL_TREE);
1032 if (TREE_CONSTANT(struct_tree))
1033 TREE_CONSTANT(ret) = 1;
1034 return tree_to_expr(ret);
1037 // An expression as a statement.
1039 Bstatement*
1040 Gcc_backend::expression_statement(Bexpression* expr)
1042 return this->make_statement(expr->get_tree());
1045 // Variable initialization.
1047 Bstatement*
1048 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1050 tree var_tree = var->get_tree();
1051 tree init_tree = init->get_tree();
1052 if (var_tree == error_mark_node || init_tree == error_mark_node)
1053 return this->error_statement();
1054 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1056 // To avoid problems with GNU ld, we don't make zero-sized
1057 // externally visible variables. That might lead us to doing an
1058 // initialization of a zero-sized expression to a non-zero sized
1059 // variable, or vice-versa. Avoid crashes by omitting the
1060 // initializer. Such initializations don't mean anything anyhow.
1061 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1062 && init_tree != NULL_TREE
1063 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1065 DECL_INITIAL(var_tree) = init_tree;
1066 init_tree = NULL_TREE;
1069 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1070 void_type_node, var_tree);
1071 if (init_tree != NULL_TREE)
1072 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1073 void_type_node, init_tree, ret);
1075 return this->make_statement(ret);
1078 // Assignment.
1080 Bstatement*
1081 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1082 Location location)
1084 tree lhs_tree = lhs->get_tree();
1085 tree rhs_tree = rhs->get_tree();
1086 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1087 return this->error_statement();
1089 // To avoid problems with GNU ld, we don't make zero-sized
1090 // externally visible variables. That might lead us to doing an
1091 // assignment of a zero-sized expression to a non-zero sized
1092 // expression; avoid crashes here by avoiding assignments of
1093 // zero-sized expressions. Such assignments don't really mean
1094 // anything anyhow.
1095 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1096 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1097 return this->compound_statement(this->expression_statement(lhs),
1098 this->expression_statement(rhs));
1100 // Sometimes the same unnamed Go type can be created multiple times
1101 // and thus have multiple tree representations. Make sure this does
1102 // not confuse the middle-end.
1103 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1105 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1106 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1107 if (POINTER_TYPE_P(lhs_type_tree)
1108 || INTEGRAL_TYPE_P(lhs_type_tree)
1109 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1110 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1111 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1112 rhs_tree);
1113 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1114 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1116 gcc_assert(int_size_in_bytes(lhs_type_tree)
1117 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1118 rhs_tree = fold_build1_loc(location.gcc_location(),
1119 VIEW_CONVERT_EXPR,
1120 lhs_type_tree, rhs_tree);
1124 return this->make_statement(fold_build2_loc(location.gcc_location(),
1125 MODIFY_EXPR,
1126 void_type_node,
1127 lhs_tree, rhs_tree));
1130 // Return.
1132 Bstatement*
1133 Gcc_backend::return_statement(Bfunction* bfunction,
1134 const std::vector<Bexpression*>& vals,
1135 Location location)
1137 tree fntree = bfunction->get_tree();
1138 if (fntree == error_mark_node)
1139 return this->error_statement();
1140 tree result = DECL_RESULT(fntree);
1141 if (result == error_mark_node)
1142 return this->error_statement();
1143 tree ret;
1144 if (vals.empty())
1145 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
1146 NULL_TREE);
1147 else if (vals.size() == 1)
1149 tree val = vals.front()->get_tree();
1150 if (val == error_mark_node)
1151 return this->error_statement();
1152 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1153 void_type_node, result,
1154 vals.front()->get_tree());
1155 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1156 void_type_node, set);
1158 else
1160 // To return multiple values, copy the values into a temporary
1161 // variable of the right structure type, and then assign the
1162 // temporary variable to the DECL_RESULT in the return
1163 // statement.
1164 tree stmt_list = NULL_TREE;
1165 tree rettype = TREE_TYPE(result);
1166 tree rettmp = create_tmp_var(rettype, "RESULT");
1167 tree field = TYPE_FIELDS(rettype);
1168 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1169 p != vals.end();
1170 p++, field = DECL_CHAIN(field))
1172 gcc_assert(field != NULL_TREE);
1173 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1174 TREE_TYPE(field), rettmp, field,
1175 NULL_TREE);
1176 tree val = (*p)->get_tree();
1177 if (val == error_mark_node)
1178 return this->error_statement();
1179 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1180 void_type_node,
1181 ref, (*p)->get_tree());
1182 append_to_statement_list(set, &stmt_list);
1184 gcc_assert(field == NULL_TREE);
1185 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1186 void_type_node,
1187 result, rettmp);
1188 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1189 void_type_node, set);
1190 append_to_statement_list(ret_expr, &stmt_list);
1191 ret = stmt_list;
1193 return this->make_statement(ret);
1196 // If.
1198 Bstatement*
1199 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
1200 Bblock* else_block, Location location)
1202 tree cond_tree = condition->get_tree();
1203 tree then_tree = then_block->get_tree();
1204 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
1205 if (cond_tree == error_mark_node
1206 || then_tree == error_mark_node
1207 || else_tree == error_mark_node)
1208 return this->error_statement();
1209 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
1210 cond_tree, then_tree, else_tree);
1211 return this->make_statement(ret);
1214 // Switch.
1216 Bstatement*
1217 Gcc_backend::switch_statement(
1218 Bexpression* value,
1219 const std::vector<std::vector<Bexpression*> >& cases,
1220 const std::vector<Bstatement*>& statements,
1221 Location switch_location)
1223 gcc_assert(cases.size() == statements.size());
1225 tree stmt_list = NULL_TREE;
1226 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
1227 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
1228 ps != statements.end();
1229 ++ps, ++pc)
1231 if (pc->empty())
1233 source_location loc = (*ps != NULL
1234 ? EXPR_LOCATION((*ps)->get_tree())
1235 : UNKNOWN_LOCATION);
1236 tree label = create_artificial_label(loc);
1237 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
1238 append_to_statement_list(c, &stmt_list);
1240 else
1242 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1243 pcv != pc->end();
1244 ++pcv)
1246 tree t = (*pcv)->get_tree();
1247 if (t == error_mark_node)
1248 return this->error_statement();
1249 source_location loc = EXPR_LOCATION(t);
1250 tree label = create_artificial_label(loc);
1251 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1252 append_to_statement_list(c, &stmt_list);
1256 if (*ps != NULL)
1258 tree t = (*ps)->get_tree();
1259 if (t == error_mark_node)
1260 return this->error_statement();
1261 append_to_statement_list(t, &stmt_list);
1265 tree tv = value->get_tree();
1266 if (tv == error_mark_node)
1267 return this->error_statement();
1268 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1269 NULL_TREE, tv, stmt_list, NULL_TREE);
1270 return this->make_statement(t);
1273 // Pair of statements.
1275 Bstatement*
1276 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1278 tree stmt_list = NULL_TREE;
1279 tree t = s1->get_tree();
1280 if (t == error_mark_node)
1281 return this->error_statement();
1282 append_to_statement_list(t, &stmt_list);
1283 t = s2->get_tree();
1284 if (t == error_mark_node)
1285 return this->error_statement();
1286 append_to_statement_list(t, &stmt_list);
1287 return this->make_statement(stmt_list);
1290 // List of statements.
1292 Bstatement*
1293 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1295 tree stmt_list = NULL_TREE;
1296 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1297 p != statements.end();
1298 ++p)
1300 tree t = (*p)->get_tree();
1301 if (t == error_mark_node)
1302 return this->error_statement();
1303 append_to_statement_list(t, &stmt_list);
1305 return this->make_statement(stmt_list);
1308 // Make a block. For some reason gcc uses a dual structure for
1309 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1310 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1311 // the Bblock.
1313 Bblock*
1314 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1315 const std::vector<Bvariable*>& vars,
1316 Location start_location,
1317 Location)
1319 tree block_tree = make_node(BLOCK);
1320 if (enclosing == NULL)
1322 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1323 // until we have a proper representation of the init function.
1324 tree fndecl;
1325 if (function == NULL)
1326 fndecl = current_function_decl;
1327 else
1328 fndecl = function->get_tree();
1329 gcc_assert(fndecl != NULL_TREE);
1331 // We may have already created a block for local variables when
1332 // we take the address of a parameter.
1333 if (DECL_INITIAL(fndecl) == NULL_TREE)
1335 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1336 DECL_INITIAL(fndecl) = block_tree;
1338 else
1340 tree superblock_tree = DECL_INITIAL(fndecl);
1341 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1342 tree* pp;
1343 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1344 *pp != NULL_TREE;
1345 pp = &BLOCK_CHAIN(*pp))
1347 *pp = block_tree;
1350 else
1352 tree superbind_tree = enclosing->get_tree();
1353 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1354 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1356 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1357 tree* pp;
1358 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1359 *pp != NULL_TREE;
1360 pp = &BLOCK_CHAIN(*pp))
1362 *pp = block_tree;
1365 tree* pp = &BLOCK_VARS(block_tree);
1366 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1367 pv != vars.end();
1368 ++pv)
1370 *pp = (*pv)->get_tree();
1371 if (*pp != error_mark_node)
1372 pp = &DECL_CHAIN(*pp);
1374 *pp = NULL_TREE;
1376 TREE_USED(block_tree) = 1;
1378 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1379 void_type_node, BLOCK_VARS(block_tree),
1380 NULL_TREE, block_tree);
1381 TREE_SIDE_EFFECTS(bind_tree) = 1;
1383 return new Bblock(bind_tree);
1386 // Add statements to a block.
1388 void
1389 Gcc_backend::block_add_statements(Bblock* bblock,
1390 const std::vector<Bstatement*>& statements)
1392 tree stmt_list = NULL_TREE;
1393 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1394 p != statements.end();
1395 ++p)
1397 tree s = (*p)->get_tree();
1398 if (s != error_mark_node)
1399 append_to_statement_list(s, &stmt_list);
1402 tree bind_tree = bblock->get_tree();
1403 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1404 BIND_EXPR_BODY(bind_tree) = stmt_list;
1407 // Return a block as a statement.
1409 Bstatement*
1410 Gcc_backend::block_statement(Bblock* bblock)
1412 tree bind_tree = bblock->get_tree();
1413 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1414 return this->make_statement(bind_tree);
1417 // This is not static because we declare it with GTY(()) in go-c.h.
1418 tree go_non_zero_struct;
1420 // Return a type corresponding to TYPE with non-zero size.
1422 tree
1423 Gcc_backend::non_zero_size_type(tree type)
1425 if (int_size_in_bytes(type) != 0)
1426 return type;
1428 switch (TREE_CODE(type))
1430 case RECORD_TYPE:
1431 if (TYPE_FIELDS(type) != NULL_TREE)
1433 tree ns = make_node(RECORD_TYPE);
1434 tree field_trees = NULL_TREE;
1435 tree *pp = &field_trees;
1436 for (tree field = TYPE_FIELDS(type);
1437 field != NULL_TREE;
1438 field = DECL_CHAIN(field))
1440 tree ft = TREE_TYPE(field);
1441 if (field == TYPE_FIELDS(type))
1442 ft = non_zero_size_type(ft);
1443 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
1444 DECL_NAME(field), ft);
1445 DECL_CONTEXT(f) = ns;
1446 *pp = f;
1447 pp = &DECL_CHAIN(f);
1449 TYPE_FIELDS(ns) = field_trees;
1450 layout_type(ns);
1451 return ns;
1454 if (go_non_zero_struct == NULL_TREE)
1456 type = make_node(RECORD_TYPE);
1457 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
1458 get_identifier("dummy"),
1459 boolean_type_node);
1460 DECL_CONTEXT(field) = type;
1461 TYPE_FIELDS(type) = field;
1462 layout_type(type);
1463 go_non_zero_struct = type;
1465 return go_non_zero_struct;
1467 case ARRAY_TYPE:
1469 tree element_type = non_zero_size_type(TREE_TYPE(type));
1470 return build_array_type_nelts(element_type, 1);
1473 default:
1474 gcc_unreachable();
1477 gcc_unreachable();
1480 // Make a global variable.
1482 Bvariable*
1483 Gcc_backend::global_variable(const std::string& package_name,
1484 const std::string& pkgpath,
1485 const std::string& name,
1486 Btype* btype,
1487 bool is_external,
1488 bool is_hidden,
1489 bool in_unique_section,
1490 Location location)
1492 tree type_tree = btype->get_tree();
1493 if (type_tree == error_mark_node)
1494 return this->error_variable();
1496 // The GNU linker does not like dynamic variables with zero size.
1497 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
1498 type_tree = this->non_zero_size_type(type_tree);
1500 std::string var_name(package_name);
1501 var_name.push_back('.');
1502 var_name.append(name);
1503 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1504 get_identifier_from_string(var_name),
1505 type_tree);
1506 if (is_external)
1507 DECL_EXTERNAL(decl) = 1;
1508 else
1509 TREE_STATIC(decl) = 1;
1510 if (!is_hidden)
1512 TREE_PUBLIC(decl) = 1;
1514 std::string asm_name(pkgpath);
1515 asm_name.push_back('.');
1516 asm_name.append(name);
1517 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1519 TREE_USED(decl) = 1;
1521 if (in_unique_section)
1522 resolve_unique_section (decl, 0, 1);
1524 go_preserve_from_gc(decl);
1526 return new Bvariable(decl);
1529 // Set the initial value of a global variable.
1531 void
1532 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1534 tree expr_tree = expr->get_tree();
1535 if (expr_tree == error_mark_node)
1536 return;
1537 gcc_assert(TREE_CONSTANT(expr_tree));
1538 tree var_decl = var->get_tree();
1539 if (var_decl == error_mark_node)
1540 return;
1541 DECL_INITIAL(var_decl) = expr_tree;
1543 // If this variable goes in a unique section, it may need to go into
1544 // a different one now that DECL_INITIAL is set.
1545 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl))
1547 DECL_SECTION_NAME (var_decl) = NULL_TREE;
1548 resolve_unique_section (var_decl,
1549 compute_reloc_for_constant (expr_tree),
1554 // Make a local variable.
1556 Bvariable*
1557 Gcc_backend::local_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(), VAR_DECL,
1565 get_identifier_from_string(name),
1566 type_tree);
1567 DECL_CONTEXT(decl) = function->get_tree();
1568 TREE_USED(decl) = 1;
1569 if (is_address_taken)
1570 TREE_ADDRESSABLE(decl) = 1;
1571 go_preserve_from_gc(decl);
1572 return new Bvariable(decl);
1575 // Make a function parameter variable.
1577 Bvariable*
1578 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1579 Btype* btype, bool is_address_taken,
1580 Location location)
1582 tree type_tree = btype->get_tree();
1583 if (type_tree == error_mark_node)
1584 return this->error_variable();
1585 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1586 get_identifier_from_string(name),
1587 type_tree);
1588 DECL_CONTEXT(decl) = function->get_tree();
1589 DECL_ARG_TYPE(decl) = type_tree;
1590 TREE_USED(decl) = 1;
1591 if (is_address_taken)
1592 TREE_ADDRESSABLE(decl) = 1;
1593 go_preserve_from_gc(decl);
1594 return new Bvariable(decl);
1597 // Make a temporary variable.
1599 Bvariable*
1600 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1601 Btype* btype, Bexpression* binit,
1602 bool is_address_taken,
1603 Location location,
1604 Bstatement** pstatement)
1606 tree type_tree = btype->get_tree();
1607 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1608 if (type_tree == error_mark_node || init_tree == error_mark_node)
1610 *pstatement = this->error_statement();
1611 return this->error_variable();
1614 tree var;
1615 // We can only use create_tmp_var if the type is not addressable.
1616 if (!TREE_ADDRESSABLE(type_tree))
1617 var = create_tmp_var(type_tree, "GOTMP");
1618 else
1620 gcc_assert(bblock != NULL);
1621 var = build_decl(location.gcc_location(), VAR_DECL,
1622 create_tmp_var_name("GOTMP"),
1623 type_tree);
1624 DECL_ARTIFICIAL(var) = 1;
1625 DECL_IGNORED_P(var) = 1;
1626 TREE_USED(var) = 1;
1627 // FIXME: Permitting function to be NULL here is a temporary
1628 // measure until we have a proper representation of the init
1629 // function.
1630 if (function != NULL)
1631 DECL_CONTEXT(var) = function->get_tree();
1632 else
1634 gcc_assert(current_function_decl != NULL_TREE);
1635 DECL_CONTEXT(var) = current_function_decl;
1638 // We have to add this variable to the BLOCK and the BIND_EXPR.
1639 tree bind_tree = bblock->get_tree();
1640 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1641 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1642 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1643 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1644 BLOCK_VARS(block_tree) = var;
1645 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1648 if (init_tree != NULL_TREE)
1649 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1650 init_tree);
1652 if (is_address_taken)
1653 TREE_ADDRESSABLE(var) = 1;
1655 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1656 DECL_EXPR,
1657 void_type_node, var));
1658 return new Bvariable(var);
1661 // Create a named immutable initialized data structure.
1663 Bvariable*
1664 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
1665 bool, Btype* btype, Location location)
1667 tree type_tree = btype->get_tree();
1668 if (type_tree == error_mark_node)
1669 return this->error_variable();
1670 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1671 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1672 get_identifier_from_string(name),
1673 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1674 TREE_STATIC(decl) = 1;
1675 TREE_READONLY(decl) = 1;
1676 TREE_CONSTANT(decl) = 1;
1677 TREE_USED(decl) = 1;
1678 DECL_ARTIFICIAL(decl) = 1;
1679 if (!is_hidden)
1680 TREE_PUBLIC(decl) = 1;
1682 // We don't call rest_of_decl_compilation until we have the
1683 // initializer.
1685 go_preserve_from_gc(decl);
1686 return new Bvariable(decl);
1689 // Set the initializer for a variable created by immutable_struct.
1690 // This is where we finish compiling the variable.
1692 void
1693 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1694 bool, bool is_common, Btype*, Location,
1695 Bexpression* initializer)
1697 tree decl = var->get_tree();
1698 tree init_tree = initializer->get_tree();
1699 if (decl == error_mark_node || init_tree == error_mark_node)
1700 return;
1702 DECL_INITIAL(decl) = init_tree;
1704 // We can't call make_decl_one_only until we set DECL_INITIAL.
1705 if (is_common)
1706 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1708 // These variables are often unneeded in the final program, so put
1709 // them in their own section so that linker GC can discard them.
1710 resolve_unique_section(decl,
1711 compute_reloc_for_constant (init_tree),
1714 rest_of_decl_compilation(decl, 1, 0);
1717 // Return a reference to an immutable initialized data structure
1718 // defined in another package.
1720 Bvariable*
1721 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1722 Location location)
1724 tree type_tree = btype->get_tree();
1725 if (type_tree == error_mark_node)
1726 return this->error_variable();
1727 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1728 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1729 get_identifier_from_string(name),
1730 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1731 TREE_READONLY(decl) = 1;
1732 TREE_CONSTANT(decl) = 1;
1733 DECL_ARTIFICIAL(decl) = 1;
1734 TREE_PUBLIC(decl) = 1;
1735 DECL_EXTERNAL(decl) = 1;
1736 go_preserve_from_gc(decl);
1737 return new Bvariable(decl);
1740 // Make a label.
1742 Blabel*
1743 Gcc_backend::label(Bfunction* function, const std::string& name,
1744 Location location)
1746 tree decl;
1747 if (name.empty())
1748 decl = create_artificial_label(location.gcc_location());
1749 else
1751 tree id = get_identifier_from_string(name);
1752 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1753 void_type_node);
1754 DECL_CONTEXT(decl) = function->get_tree();
1756 return new Blabel(decl);
1759 // Make a statement which defines a label.
1761 Bstatement*
1762 Gcc_backend::label_definition_statement(Blabel* label)
1764 tree lab = label->get_tree();
1765 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1766 void_type_node, lab);
1767 return this->make_statement(ret);
1770 // Make a goto statement.
1772 Bstatement*
1773 Gcc_backend::goto_statement(Blabel* label, Location location)
1775 tree lab = label->get_tree();
1776 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1777 lab);
1778 return this->make_statement(ret);
1781 // Get the address of a label.
1783 Bexpression*
1784 Gcc_backend::label_address(Blabel* label, Location location)
1786 tree lab = label->get_tree();
1787 TREE_USED(lab) = 1;
1788 TREE_ADDRESSABLE(lab) = 1;
1789 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1790 build_fold_addr_expr_loc(location.gcc_location(),
1791 lab));
1792 return this->make_expression(ret);
1795 // Declare or define a new function.
1797 Bfunction*
1798 Gcc_backend::function(Btype* fntype, const std::string& name,
1799 const std::string& asm_name, bool is_visible,
1800 bool is_declaration, bool is_inlinable,
1801 bool disable_split_stack, bool in_unique_section,
1802 Location location)
1804 tree functype = fntype->get_tree();
1805 if (functype != error_mark_node)
1807 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
1808 functype = TREE_TYPE(functype);
1810 tree id = get_identifier_from_string(name);
1811 if (functype == error_mark_node || id == error_mark_node)
1812 return this->error_function();
1814 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
1815 if (!asm_name.empty())
1816 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1817 if (is_visible)
1818 TREE_PUBLIC(decl) = 1;
1819 if (is_declaration)
1820 DECL_EXTERNAL(decl) = 1;
1821 else
1823 tree restype = TREE_TYPE(functype);
1824 tree resdecl =
1825 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
1826 DECL_ARTIFICIAL(resdecl) = 1;
1827 DECL_IGNORED_P(resdecl) = 1;
1828 DECL_CONTEXT(resdecl) = decl;
1829 DECL_RESULT(decl) = resdecl;
1831 if (!is_inlinable)
1832 DECL_UNINLINABLE(decl) = 1;
1833 if (disable_split_stack)
1835 tree attr = get_identifier("__no_split_stack__");
1836 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
1838 if (in_unique_section)
1839 resolve_unique_section(decl, 0, 1);
1841 go_preserve_from_gc(decl);
1842 return new Bfunction(decl);
1845 // The single backend.
1847 static Gcc_backend gcc_backend;
1849 // Return the backend generator.
1851 Backend*
1852 go_get_backend()
1854 return &gcc_backend;
1857 // FIXME: Temporary functions while converting to the new backend
1858 // interface.
1860 Btype*
1861 tree_to_type(tree t)
1863 return new Btype(t);
1866 Bexpression*
1867 tree_to_expr(tree t)
1869 return new Bexpression(t);
1872 Bstatement*
1873 tree_to_stat(tree t)
1875 return new Bstatement(t);
1878 Bfunction*
1879 tree_to_function(tree t)
1881 return new Bfunction(t);
1884 Bblock*
1885 tree_to_block(tree t)
1887 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1888 return new Bblock(t);
1891 tree
1892 type_to_tree(Btype* bt)
1894 return bt->get_tree();
1897 tree
1898 expr_to_tree(Bexpression* be)
1900 return be->get_tree();
1903 tree
1904 stat_to_tree(Bstatement* bs)
1906 return bs->get_tree();
1909 tree
1910 block_to_tree(Bblock* bb)
1912 return bb->get_tree();
1915 tree
1916 var_to_tree(Bvariable* bv)
1918 return bv->get_tree();
1921 tree
1922 function_to_tree(Bfunction* bf)
1924 return bf->get_tree();