1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011, 2012 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
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
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.
28 #include "tree-iterator.h"
37 // A class wrapping a tree.
58 // In gcc, types, expressions, and statements are all trees.
59 class Btype : public Gcc_tree
67 class Bexpression : public Gcc_tree
75 class Bstatement : public Gcc_tree
83 class Bfunction : public Gcc_tree
91 class Bblock : public Gcc_tree
99 class Bvariable : public Gcc_tree
107 class Blabel : public Gcc_tree
115 // This file implements the interface between the Go frontend proper
116 // and the gcc IR. This implements specific instantiations of
117 // abstract classes defined by the Go frontend proper. The Go
118 // frontend proper class methods of these classes to generate the
119 // backend representation.
121 class Gcc_backend : public Backend
128 { return this->make_type(error_mark_node); }
132 { return this->make_type(void_type_node); }
136 { return this->make_type(boolean_type_node); }
139 integer_type(bool, int);
148 pointer_type(Btype*);
151 function_type(const Btyped_identifier&,
152 const std::vector<Btyped_identifier>&,
153 const std::vector<Btyped_identifier>&,
157 struct_type(const std::vector<Btyped_identifier>&);
160 array_type(Btype*, Bexpression*);
163 placeholder_pointer_type(const std::string&, Location, bool);
166 set_placeholder_pointer_type(Btype*, Btype*);
169 set_placeholder_function_type(Btype*, Btype*);
172 placeholder_struct_type(const std::string&, Location);
175 set_placeholder_struct_type(Btype* placeholder,
176 const std::vector<Btyped_identifier>&);
179 placeholder_array_type(const std::string&, Location);
182 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
185 named_type(const std::string&, Btype*, Location);
188 circular_pointer_type(Btype*, bool);
191 is_circular_pointer_type(Btype*);
197 type_alignment(Btype*);
200 type_field_alignment(Btype*);
203 type_field_offset(Btype*, size_t index);
208 zero_expression(Btype*);
214 { return this->make_statement(error_mark_node); }
217 expression_statement(Bexpression*);
220 init_statement(Bvariable* var, Bexpression* init);
223 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
226 return_statement(Bfunction*, const std::vector<Bexpression*>&,
230 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
234 switch_statement(Bexpression* value,
235 const std::vector<std::vector<Bexpression*> >& cases,
236 const std::vector<Bstatement*>& statements,
240 compound_statement(Bstatement*, Bstatement*);
243 statement_list(const std::vector<Bstatement*>&);
248 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
252 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
255 block_statement(Bblock*);
261 { return new Bvariable(error_mark_node); }
264 global_variable(const std::string& package_name,
265 const std::string& pkgpath,
266 const std::string& name,
273 global_variable_set_init(Bvariable*, Bexpression*);
276 local_variable(Bfunction*, const std::string&, Btype*, bool,
280 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
284 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
285 Location, Bstatement**);
288 immutable_struct(const std::string&, bool, Btype*, Location);
291 immutable_struct_set_init(Bvariable*, const std::string&, bool, Btype*,
292 Location, Bexpression*);
295 immutable_struct_reference(const std::string&, Btype*, Location);
300 label(Bfunction*, const std::string& name, Location);
303 label_definition_statement(Blabel*);
306 goto_statement(Blabel*, Location);
309 label_address(Blabel*, Location);
312 // Make a Bexpression from a tree.
314 make_expression(tree t)
315 { return new Bexpression(t); }
317 // Make a Bstatement from a tree.
319 make_statement(tree t)
320 { return new Bstatement(t); }
322 // Make a Btype from a tree.
325 { return new Btype(t); }
328 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
331 fill_in_array(Btype*, Btype*, Bexpression*);
334 non_zero_size_type(tree);
337 // A helper function.
340 get_identifier_from_string(const std::string& str)
342 return get_identifier_with_length(str.data(), str.length());
345 // Get an unnamed integer type.
348 Gcc_backend::integer_type(bool is_unsigned, int bits)
353 if (bits == INT_TYPE_SIZE)
354 type = unsigned_type_node;
355 else if (bits == CHAR_TYPE_SIZE)
356 type = unsigned_char_type_node;
357 else if (bits == SHORT_TYPE_SIZE)
358 type = short_unsigned_type_node;
359 else if (bits == LONG_TYPE_SIZE)
360 type = long_unsigned_type_node;
361 else if (bits == LONG_LONG_TYPE_SIZE)
362 type = long_long_unsigned_type_node;
364 type = make_unsigned_type(bits);
368 if (bits == INT_TYPE_SIZE)
369 type = integer_type_node;
370 else if (bits == CHAR_TYPE_SIZE)
371 type = signed_char_type_node;
372 else if (bits == SHORT_TYPE_SIZE)
373 type = short_integer_type_node;
374 else if (bits == LONG_TYPE_SIZE)
375 type = long_integer_type_node;
376 else if (bits == LONG_LONG_TYPE_SIZE)
377 type = long_long_integer_type_node;
379 type = make_signed_type(bits);
381 return this->make_type(type);
384 // Get an unnamed float type.
387 Gcc_backend::float_type(int bits)
390 if (bits == FLOAT_TYPE_SIZE)
391 type = float_type_node;
392 else if (bits == DOUBLE_TYPE_SIZE)
393 type = double_type_node;
394 else if (bits == LONG_DOUBLE_TYPE_SIZE)
395 type = long_double_type_node;
398 type = make_node(REAL_TYPE);
399 TYPE_PRECISION(type) = bits;
402 return this->make_type(type);
405 // Get an unnamed complex type.
408 Gcc_backend::complex_type(int bits)
411 if (bits == FLOAT_TYPE_SIZE * 2)
412 type = complex_float_type_node;
413 else if (bits == DOUBLE_TYPE_SIZE * 2)
414 type = complex_double_type_node;
415 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
416 type = complex_long_double_type_node;
419 type = make_node(REAL_TYPE);
420 TYPE_PRECISION(type) = bits / 2;
422 type = build_complex_type(type);
424 return this->make_type(type);
427 // Get a pointer type.
430 Gcc_backend::pointer_type(Btype* to_type)
432 tree to_type_tree = to_type->get_tree();
433 if (to_type_tree == error_mark_node)
434 return this->error_type();
435 tree type = build_pointer_type(to_type_tree);
436 return this->make_type(type);
439 // Make a function type.
442 Gcc_backend::function_type(const Btyped_identifier& receiver,
443 const std::vector<Btyped_identifier>& parameters,
444 const std::vector<Btyped_identifier>& results,
447 tree args = NULL_TREE;
449 if (receiver.btype != NULL)
451 tree t = receiver.btype->get_tree();
452 if (t == error_mark_node)
453 return this->error_type();
454 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
455 pp = &TREE_CHAIN(*pp);
458 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
459 p != parameters.end();
462 tree t = p->btype->get_tree();
463 if (t == error_mark_node)
464 return this->error_type();
465 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
466 pp = &TREE_CHAIN(*pp);
469 // Varargs is handled entirely at the Go level. When converted to
470 // GENERIC functions are not varargs.
471 *pp = void_list_node;
475 result = void_type_node;
476 else if (results.size() == 1)
477 result = results.front().btype->get_tree();
480 result = make_node(RECORD_TYPE);
481 tree field_trees = NULL_TREE;
483 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
487 const std::string name = (p->name.empty()
490 tree name_tree = get_identifier_from_string(name);
491 tree field_type_tree = p->btype->get_tree();
492 if (field_type_tree == error_mark_node)
493 return this->error_type();
494 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
495 tree field = build_decl(location.gcc_location(), FIELD_DECL,
496 name_tree, field_type_tree);
497 DECL_CONTEXT(field) = result;
499 pp = &DECL_CHAIN(field);
501 TYPE_FIELDS(result) = field_trees;
504 if (result == error_mark_node)
505 return this->error_type();
507 tree fntype = build_function_type(result, args);
508 if (fntype == error_mark_node)
509 return this->error_type();
511 return this->make_type(build_pointer_type(fntype));
514 // Make a struct type.
517 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
519 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
522 // Fill in the fields of a struct type.
525 Gcc_backend::fill_in_struct(Btype* fill,
526 const std::vector<Btyped_identifier>& fields)
528 tree fill_tree = fill->get_tree();
529 tree field_trees = NULL_TREE;
530 tree* pp = &field_trees;
531 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
535 tree name_tree = get_identifier_from_string(p->name);
536 tree type_tree = p->btype->get_tree();
537 if (type_tree == error_mark_node)
538 return this->error_type();
539 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
541 DECL_CONTEXT(field) = fill_tree;
543 pp = &DECL_CHAIN(field);
545 TYPE_FIELDS(fill_tree) = field_trees;
546 layout_type(fill_tree);
550 // Make an array type.
553 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
555 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
556 element_btype, length);
559 // Fill in an array type.
562 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
565 tree element_type_tree = element_type->get_tree();
566 tree length_tree = length->get_tree();
567 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
568 return this->error_type();
570 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
572 length_tree = fold_convert(sizetype, length_tree);
574 // build_index_type takes the maximum index, which is one less than
576 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
580 tree fill_tree = fill->get_tree();
581 TREE_TYPE(fill_tree) = element_type_tree;
582 TYPE_DOMAIN(fill_tree) = index_type_tree;
583 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
584 layout_type(fill_tree);
586 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
587 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
588 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
589 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
590 TYPE_CANONICAL(fill_tree) =
591 build_array_type(TYPE_CANONICAL(element_type_tree),
592 TYPE_CANONICAL(index_type_tree));
597 // Create a placeholder for a pointer type.
600 Gcc_backend::placeholder_pointer_type(const std::string& name,
601 Location location, bool)
603 tree ret = build_distinct_type_copy(ptr_type_node);
606 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
607 get_identifier_from_string(name),
609 TYPE_NAME(ret) = decl;
611 return this->make_type(ret);
614 // Set the real target type for a placeholder pointer type.
617 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
620 tree pt = placeholder->get_tree();
621 if (pt == error_mark_node)
623 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
624 tree tt = to_type->get_tree();
625 if (tt == error_mark_node)
627 placeholder->set_tree(error_mark_node);
630 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
631 TREE_TYPE(pt) = TREE_TYPE(tt);
632 if (TYPE_NAME(pt) != NULL_TREE)
634 // Build the data structure gcc wants to see for a typedef.
635 tree copy = build_variant_type_copy(pt);
636 TYPE_NAME(copy) = NULL_TREE;
637 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
642 // Set the real values for a placeholder function type.
645 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
647 return this->set_placeholder_pointer_type(placeholder, ft);
650 // Create a placeholder for a struct type.
653 Gcc_backend::placeholder_struct_type(const std::string& name,
656 tree ret = make_node(RECORD_TYPE);
659 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
660 get_identifier_from_string(name),
662 TYPE_NAME(ret) = decl;
664 return this->make_type(ret);
667 // Fill in the fields of a placeholder struct type.
670 Gcc_backend::set_placeholder_struct_type(
672 const std::vector<Btyped_identifier>& fields)
674 tree t = placeholder->get_tree();
675 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
676 Btype* r = this->fill_in_struct(placeholder, fields);
678 if (TYPE_NAME(t) != NULL_TREE)
680 // Build the data structure gcc wants to see for a typedef.
681 tree copy = build_distinct_type_copy(t);
682 TYPE_NAME(copy) = NULL_TREE;
683 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
686 return r->get_tree() != error_mark_node;
689 // Create a placeholder for an array type.
692 Gcc_backend::placeholder_array_type(const std::string& name,
695 tree ret = make_node(ARRAY_TYPE);
696 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
697 get_identifier_from_string(name),
699 TYPE_NAME(ret) = decl;
700 return this->make_type(ret);
703 // Fill in the fields of a placeholder array type.
706 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
707 Btype* element_btype,
710 tree t = placeholder->get_tree();
711 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
712 Btype* r = this->fill_in_array(placeholder, element_btype, length);
714 // Build the data structure gcc wants to see for a typedef.
715 tree copy = build_distinct_type_copy(t);
716 TYPE_NAME(copy) = NULL_TREE;
717 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
719 return r->get_tree() != error_mark_node;
722 // Return a named version of a type.
725 Gcc_backend::named_type(const std::string& name, Btype* btype,
728 tree type = btype->get_tree();
729 if (type == error_mark_node)
730 return this->error_type();
732 // The middle-end expects a basic type to have a name. In Go every
733 // basic type will have a name. The first time we see a basic type,
734 // give it whatever Go name we have at this point.
735 if (TYPE_NAME(type) == NULL_TREE
736 && location.gcc_location() == BUILTINS_LOCATION
737 && (TREE_CODE(type) == INTEGER_TYPE
738 || TREE_CODE(type) == REAL_TYPE
739 || TREE_CODE(type) == COMPLEX_TYPE
740 || TREE_CODE(type) == BOOLEAN_TYPE))
742 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
743 get_identifier_from_string(name),
745 TYPE_NAME(type) = decl;
746 return this->make_type(type);
749 tree copy = build_variant_type_copy(type);
750 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
751 get_identifier_from_string(name),
753 DECL_ORIGINAL_TYPE(decl) = type;
754 TYPE_NAME(copy) = decl;
755 return this->make_type(copy);
758 // Return a pointer type used as a marker for a circular type.
761 Gcc_backend::circular_pointer_type(Btype*, bool)
763 return this->make_type(ptr_type_node);
766 // Return whether we might be looking at a circular type.
769 Gcc_backend::is_circular_pointer_type(Btype* btype)
771 return btype->get_tree() == ptr_type_node;
774 // Return the size of a type.
777 Gcc_backend::type_size(Btype* btype)
779 tree t = btype->get_tree();
780 if (t == error_mark_node)
782 t = TYPE_SIZE_UNIT(t);
783 gcc_assert(TREE_CODE(t) == INTEGER_CST);
784 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
785 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
786 size_t ret = static_cast<size_t>(val_wide);
787 gcc_assert(ret == val_wide);
791 // Return the alignment of a type.
794 Gcc_backend::type_alignment(Btype* btype)
796 tree t = btype->get_tree();
797 if (t == error_mark_node)
799 return TYPE_ALIGN_UNIT(t);
802 // Return the alignment of a struct field of type BTYPE.
805 Gcc_backend::type_field_alignment(Btype* btype)
807 tree t = btype->get_tree();
808 if (t == error_mark_node)
810 return go_field_alignment(t);
813 // Return the offset of a field in a struct.
816 Gcc_backend::type_field_offset(Btype* btype, size_t index)
818 tree struct_tree = btype->get_tree();
819 if (struct_tree == error_mark_node)
821 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
822 tree field = TYPE_FIELDS(struct_tree);
823 for (; index > 0; --index)
825 field = DECL_CHAIN(field);
826 gcc_assert(field != NULL_TREE);
828 HOST_WIDE_INT offset_wide = int_byte_position(field);
829 gcc_assert(offset_wide >= 0);
830 size_t ret = static_cast<size_t>(offset_wide);
831 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
835 // Return the zero value for a type.
838 Gcc_backend::zero_expression(Btype* btype)
840 tree t = btype->get_tree();
842 if (t == error_mark_node)
843 ret = error_mark_node;
845 ret = build_zero_cst(t);
846 return tree_to_expr(ret);
849 // An expression as a statement.
852 Gcc_backend::expression_statement(Bexpression* expr)
854 return this->make_statement(expr->get_tree());
857 // Variable initialization.
860 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
862 tree var_tree = var->get_tree();
863 tree init_tree = init->get_tree();
864 if (var_tree == error_mark_node || init_tree == error_mark_node)
865 return this->error_statement();
866 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
868 // To avoid problems with GNU ld, we don't make zero-sized
869 // externally visible variables. That might lead us to doing an
870 // initialization of a zero-sized expression to a non-zero sized
871 // variable, or vice-versa. Avoid crashes by omitting the
872 // initializer. Such initializations don't mean anything anyhow.
873 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
874 && init_tree != NULL_TREE
875 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
877 DECL_INITIAL(var_tree) = init_tree;
878 init_tree = NULL_TREE;
881 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
882 void_type_node, var_tree);
883 if (init_tree != NULL_TREE)
884 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
885 void_type_node, init_tree, ret);
887 return this->make_statement(ret);
893 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
896 tree lhs_tree = lhs->get_tree();
897 tree rhs_tree = rhs->get_tree();
898 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
899 return this->error_statement();
901 // To avoid problems with GNU ld, we don't make zero-sized
902 // externally visible variables. That might lead us to doing an
903 // assignment of a zero-sized expression to a non-zero sized
904 // expression; avoid crashes here by avoiding assignments of
905 // zero-sized expressions. Such assignments don't really mean
907 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
908 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
909 return this->compound_statement(this->expression_statement(lhs),
910 this->expression_statement(rhs));
912 // Sometimes the same unnamed Go type can be created multiple times
913 // and thus have multiple tree representations. Make sure this does
914 // not confuse the middle-end.
915 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
917 tree lhs_type_tree = TREE_TYPE(lhs_tree);
918 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
919 if (POINTER_TYPE_P(lhs_type_tree)
920 || INTEGRAL_TYPE_P(lhs_type_tree)
921 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
922 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
923 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
925 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
926 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
928 gcc_assert(int_size_in_bytes(lhs_type_tree)
929 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
930 rhs_tree = fold_build1_loc(location.gcc_location(),
932 lhs_type_tree, rhs_tree);
936 return this->make_statement(fold_build2_loc(location.gcc_location(),
939 lhs_tree, rhs_tree));
945 Gcc_backend::return_statement(Bfunction* bfunction,
946 const std::vector<Bexpression*>& vals,
949 tree fntree = bfunction->get_tree();
950 if (fntree == error_mark_node)
951 return this->error_statement();
952 tree result = DECL_RESULT(fntree);
953 if (result == error_mark_node)
954 return this->error_statement();
957 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
959 else if (vals.size() == 1)
961 tree val = vals.front()->get_tree();
962 if (val == error_mark_node)
963 return this->error_statement();
964 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
965 void_type_node, result,
966 vals.front()->get_tree());
967 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
968 void_type_node, set);
972 // To return multiple values, copy the values into a temporary
973 // variable of the right structure type, and then assign the
974 // temporary variable to the DECL_RESULT in the return
976 tree stmt_list = NULL_TREE;
977 tree rettype = TREE_TYPE(result);
978 tree rettmp = create_tmp_var(rettype, "RESULT");
979 tree field = TYPE_FIELDS(rettype);
980 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
982 p++, field = DECL_CHAIN(field))
984 gcc_assert(field != NULL_TREE);
985 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
986 TREE_TYPE(field), rettmp, field,
988 tree val = (*p)->get_tree();
989 if (val == error_mark_node)
990 return this->error_statement();
991 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
993 ref, (*p)->get_tree());
994 append_to_statement_list(set, &stmt_list);
996 gcc_assert(field == NULL_TREE);
997 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1000 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1001 void_type_node, set);
1002 append_to_statement_list(ret_expr, &stmt_list);
1005 return this->make_statement(ret);
1011 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
1012 Bblock* else_block, Location location)
1014 tree cond_tree = condition->get_tree();
1015 tree then_tree = then_block->get_tree();
1016 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
1017 if (cond_tree == error_mark_node
1018 || then_tree == error_mark_node
1019 || else_tree == error_mark_node)
1020 return this->error_statement();
1021 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
1022 cond_tree, then_tree, else_tree);
1023 return this->make_statement(ret);
1029 Gcc_backend::switch_statement(
1031 const std::vector<std::vector<Bexpression*> >& cases,
1032 const std::vector<Bstatement*>& statements,
1033 Location switch_location)
1035 gcc_assert(cases.size() == statements.size());
1037 tree stmt_list = NULL_TREE;
1038 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
1039 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
1040 ps != statements.end();
1045 source_location loc = (*ps != NULL
1046 ? EXPR_LOCATION((*ps)->get_tree())
1047 : UNKNOWN_LOCATION);
1048 tree label = create_artificial_label(loc);
1049 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
1050 append_to_statement_list(c, &stmt_list);
1054 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1058 tree t = (*pcv)->get_tree();
1059 if (t == error_mark_node)
1060 return this->error_statement();
1061 source_location loc = EXPR_LOCATION(t);
1062 tree label = create_artificial_label(loc);
1063 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1064 append_to_statement_list(c, &stmt_list);
1070 tree t = (*ps)->get_tree();
1071 if (t == error_mark_node)
1072 return this->error_statement();
1073 append_to_statement_list(t, &stmt_list);
1077 tree tv = value->get_tree();
1078 if (tv == error_mark_node)
1079 return this->error_statement();
1080 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1081 NULL_TREE, tv, stmt_list, NULL_TREE);
1082 return this->make_statement(t);
1085 // Pair of statements.
1088 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1090 tree stmt_list = NULL_TREE;
1091 tree t = s1->get_tree();
1092 if (t == error_mark_node)
1093 return this->error_statement();
1094 append_to_statement_list(t, &stmt_list);
1096 if (t == error_mark_node)
1097 return this->error_statement();
1098 append_to_statement_list(t, &stmt_list);
1099 return this->make_statement(stmt_list);
1102 // List of statements.
1105 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1107 tree stmt_list = NULL_TREE;
1108 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1109 p != statements.end();
1112 tree t = (*p)->get_tree();
1113 if (t == error_mark_node)
1114 return this->error_statement();
1115 append_to_statement_list(t, &stmt_list);
1117 return this->make_statement(stmt_list);
1120 // Make a block. For some reason gcc uses a dual structure for
1121 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1122 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1126 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1127 const std::vector<Bvariable*>& vars,
1128 Location start_location,
1131 tree block_tree = make_node(BLOCK);
1132 if (enclosing == NULL)
1134 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1135 // until we have a proper representation of the init function.
1137 if (function == NULL)
1138 fndecl = current_function_decl;
1140 fndecl = function->get_tree();
1141 gcc_assert(fndecl != NULL_TREE);
1143 // We may have already created a block for local variables when
1144 // we take the address of a parameter.
1145 if (DECL_INITIAL(fndecl) == NULL_TREE)
1147 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1148 DECL_INITIAL(fndecl) = block_tree;
1152 tree superblock_tree = DECL_INITIAL(fndecl);
1153 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1155 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1157 pp = &BLOCK_CHAIN(*pp))
1164 tree superbind_tree = enclosing->get_tree();
1165 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1166 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1168 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1170 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1172 pp = &BLOCK_CHAIN(*pp))
1177 tree* pp = &BLOCK_VARS(block_tree);
1178 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1182 *pp = (*pv)->get_tree();
1183 if (*pp != error_mark_node)
1184 pp = &DECL_CHAIN(*pp);
1188 TREE_USED(block_tree) = 1;
1190 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1191 void_type_node, BLOCK_VARS(block_tree),
1192 NULL_TREE, block_tree);
1193 TREE_SIDE_EFFECTS(bind_tree) = 1;
1195 return new Bblock(bind_tree);
1198 // Add statements to a block.
1201 Gcc_backend::block_add_statements(Bblock* bblock,
1202 const std::vector<Bstatement*>& statements)
1204 tree stmt_list = NULL_TREE;
1205 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1206 p != statements.end();
1209 tree s = (*p)->get_tree();
1210 if (s != error_mark_node)
1211 append_to_statement_list(s, &stmt_list);
1214 tree bind_tree = bblock->get_tree();
1215 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1216 BIND_EXPR_BODY(bind_tree) = stmt_list;
1219 // Return a block as a statement.
1222 Gcc_backend::block_statement(Bblock* bblock)
1224 tree bind_tree = bblock->get_tree();
1225 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1226 return this->make_statement(bind_tree);
1229 // This is not static because we declare it with GTY(()) in go-c.h.
1230 tree go_non_zero_struct;
1232 // Return a type corresponding to TYPE with non-zero size.
1235 Gcc_backend::non_zero_size_type(tree type)
1237 if (int_size_in_bytes(type) != 0)
1240 switch (TREE_CODE(type))
1244 if (go_non_zero_struct == NULL_TREE)
1246 type = make_node(RECORD_TYPE);
1247 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
1248 get_identifier("dummy"),
1250 DECL_CONTEXT(field) = type;
1251 TYPE_FIELDS(type) = field;
1253 go_non_zero_struct = type;
1255 return go_non_zero_struct;
1260 tree element_type = non_zero_size_type(TREE_TYPE(type));
1261 return build_array_type_nelts(element_type, 1);
1271 // Make a global variable.
1274 Gcc_backend::global_variable(const std::string& package_name,
1275 const std::string& pkgpath,
1276 const std::string& name,
1282 tree type_tree = btype->get_tree();
1283 if (type_tree == error_mark_node)
1284 return this->error_variable();
1286 // The GNU linker does not like dynamic variables with zero size.
1287 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
1288 type_tree = this->non_zero_size_type(type_tree);
1290 std::string var_name(package_name);
1291 var_name.push_back('.');
1292 var_name.append(name);
1293 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1294 get_identifier_from_string(var_name),
1297 DECL_EXTERNAL(decl) = 1;
1299 TREE_STATIC(decl) = 1;
1302 TREE_PUBLIC(decl) = 1;
1304 std::string asm_name(pkgpath);
1305 asm_name.push_back('.');
1306 asm_name.append(name);
1307 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1309 TREE_USED(decl) = 1;
1311 go_preserve_from_gc(decl);
1313 return new Bvariable(decl);
1316 // Set the initial value of a global variable.
1319 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1321 tree expr_tree = expr->get_tree();
1322 if (expr_tree == error_mark_node)
1324 gcc_assert(TREE_CONSTANT(expr_tree));
1325 tree var_decl = var->get_tree();
1326 if (var_decl == error_mark_node)
1328 DECL_INITIAL(var_decl) = expr_tree;
1331 // Make a local variable.
1334 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1335 Btype* btype, bool is_address_taken,
1338 tree type_tree = btype->get_tree();
1339 if (type_tree == error_mark_node)
1340 return this->error_variable();
1341 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1342 get_identifier_from_string(name),
1344 DECL_CONTEXT(decl) = function->get_tree();
1345 TREE_USED(decl) = 1;
1346 if (is_address_taken)
1347 TREE_ADDRESSABLE(decl) = 1;
1348 go_preserve_from_gc(decl);
1349 return new Bvariable(decl);
1352 // Make a function parameter variable.
1355 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1356 Btype* btype, bool is_address_taken,
1359 tree type_tree = btype->get_tree();
1360 if (type_tree == error_mark_node)
1361 return this->error_variable();
1362 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1363 get_identifier_from_string(name),
1365 DECL_CONTEXT(decl) = function->get_tree();
1366 DECL_ARG_TYPE(decl) = type_tree;
1367 TREE_USED(decl) = 1;
1368 if (is_address_taken)
1369 TREE_ADDRESSABLE(decl) = 1;
1370 go_preserve_from_gc(decl);
1371 return new Bvariable(decl);
1374 // Make a temporary variable.
1377 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1378 Btype* btype, Bexpression* binit,
1379 bool is_address_taken,
1381 Bstatement** pstatement)
1383 tree type_tree = btype->get_tree();
1384 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1385 if (type_tree == error_mark_node || init_tree == error_mark_node)
1387 *pstatement = this->error_statement();
1388 return this->error_variable();
1392 // We can only use create_tmp_var if the type is not addressable.
1393 if (!TREE_ADDRESSABLE(type_tree))
1394 var = create_tmp_var(type_tree, "GOTMP");
1397 gcc_assert(bblock != NULL);
1398 var = build_decl(location.gcc_location(), VAR_DECL,
1399 create_tmp_var_name("GOTMP"),
1401 DECL_ARTIFICIAL(var) = 1;
1402 DECL_IGNORED_P(var) = 1;
1404 // FIXME: Permitting function to be NULL here is a temporary
1405 // measure until we have a proper representation of the init
1407 if (function != NULL)
1408 DECL_CONTEXT(var) = function->get_tree();
1411 gcc_assert(current_function_decl != NULL_TREE);
1412 DECL_CONTEXT(var) = current_function_decl;
1415 // We have to add this variable to the BLOCK and the BIND_EXPR.
1416 tree bind_tree = bblock->get_tree();
1417 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1418 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1419 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1420 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1421 BLOCK_VARS(block_tree) = var;
1422 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1425 if (init_tree != NULL_TREE)
1426 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1429 if (is_address_taken)
1430 TREE_ADDRESSABLE(var) = 1;
1432 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1434 void_type_node, var));
1435 return new Bvariable(var);
1438 // Create a named immutable initialized data structure.
1441 Gcc_backend::immutable_struct(const std::string& name, bool, Btype* btype,
1444 tree type_tree = btype->get_tree();
1445 if (type_tree == error_mark_node)
1446 return this->error_variable();
1447 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1448 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1449 get_identifier_from_string(name),
1450 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1451 TREE_STATIC(decl) = 1;
1452 TREE_READONLY(decl) = 1;
1453 TREE_CONSTANT(decl) = 1;
1454 TREE_USED(decl) = 1;
1455 DECL_ARTIFICIAL(decl) = 1;
1457 // We don't call rest_of_decl_compilation until we have the
1460 go_preserve_from_gc(decl);
1461 return new Bvariable(decl);
1464 // Set the initializer for a variable created by immutable_struct.
1465 // This is where we finish compiling the variable.
1468 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1469 bool is_common, Btype*,
1471 Bexpression* initializer)
1473 tree decl = var->get_tree();
1474 tree init_tree = initializer->get_tree();
1475 if (decl == error_mark_node || init_tree == error_mark_node)
1478 DECL_INITIAL(decl) = init_tree;
1480 // We can't call make_decl_one_only until we set DECL_INITIAL.
1482 TREE_PUBLIC(decl) = 1;
1485 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1486 resolve_unique_section(decl, 1, 0);
1489 rest_of_decl_compilation(decl, 1, 0);
1492 // Return a reference to an immutable initialized data structure
1493 // defined in another package.
1496 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1499 tree type_tree = btype->get_tree();
1500 if (type_tree == error_mark_node)
1501 return this->error_variable();
1502 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1503 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1504 get_identifier_from_string(name),
1505 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1506 TREE_READONLY(decl) = 1;
1507 TREE_CONSTANT(decl) = 1;
1508 DECL_ARTIFICIAL(decl) = 1;
1509 TREE_PUBLIC(decl) = 1;
1510 DECL_EXTERNAL(decl) = 1;
1511 go_preserve_from_gc(decl);
1512 return new Bvariable(decl);
1518 Gcc_backend::label(Bfunction* function, const std::string& name,
1523 decl = create_artificial_label(location.gcc_location());
1526 tree id = get_identifier_from_string(name);
1527 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1529 DECL_CONTEXT(decl) = function->get_tree();
1531 return new Blabel(decl);
1534 // Make a statement which defines a label.
1537 Gcc_backend::label_definition_statement(Blabel* label)
1539 tree lab = label->get_tree();
1540 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1541 void_type_node, lab);
1542 return this->make_statement(ret);
1545 // Make a goto statement.
1548 Gcc_backend::goto_statement(Blabel* label, Location location)
1550 tree lab = label->get_tree();
1551 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1553 return this->make_statement(ret);
1556 // Get the address of a label.
1559 Gcc_backend::label_address(Blabel* label, Location location)
1561 tree lab = label->get_tree();
1563 TREE_ADDRESSABLE(lab) = 1;
1564 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1565 build_fold_addr_expr_loc(location.gcc_location(),
1567 return this->make_expression(ret);
1570 // The single backend.
1572 static Gcc_backend gcc_backend;
1574 // Return the backend generator.
1579 return &gcc_backend;
1582 // FIXME: Temporary functions while converting to the new backend
1586 tree_to_type(tree t)
1588 return new Btype(t);
1592 tree_to_expr(tree t)
1594 return new Bexpression(t);
1598 tree_to_stat(tree t)
1600 return new Bstatement(t);
1604 tree_to_function(tree t)
1606 return new Bfunction(t);
1610 tree_to_block(tree t)
1612 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1613 return new Bblock(t);
1617 type_to_tree(Btype* bt)
1619 return bt->get_tree();
1623 expr_to_tree(Bexpression* be)
1625 return be->get_tree();
1629 stat_to_tree(Bstatement* bs)
1631 return bs->get_tree();
1635 block_to_tree(Bblock* bb)
1637 return bb->get_tree();
1641 var_to_tree(Bvariable* bv)
1643 return bv->get_tree();