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.
27 #ifndef ENABLE_BUILD_WITH_CXX
33 #include "tree-iterator.h"
37 #ifndef ENABLE_BUILD_WITH_CXX
46 // A class wrapping a tree.
67 // In gcc, types, expressions, and statements are all trees.
68 class Btype : public Gcc_tree
76 class Bexpression : public Gcc_tree
84 class Bstatement : public Gcc_tree
92 class Bfunction : public Gcc_tree
100 class Bblock : public Gcc_tree
108 class Bvariable : public Gcc_tree
116 class Blabel : public Gcc_tree
124 // This file implements the interface between the Go frontend proper
125 // and the gcc IR. This implements specific instantiations of
126 // abstract classes defined by the Go frontend proper. The Go
127 // frontend proper class methods of these classes to generate the
128 // backend representation.
130 class Gcc_backend : public Backend
137 { return this->make_type(error_mark_node); }
141 { return this->make_type(void_type_node); }
145 { return this->make_type(boolean_type_node); }
148 integer_type(bool, int);
157 pointer_type(Btype*);
160 function_type(const Btyped_identifier&,
161 const std::vector<Btyped_identifier>&,
162 const std::vector<Btyped_identifier>&,
166 struct_type(const std::vector<Btyped_identifier>&);
169 array_type(Btype*, Bexpression*);
172 placeholder_pointer_type(const std::string&, Location, bool);
175 set_placeholder_pointer_type(Btype*, Btype*);
178 set_placeholder_function_type(Btype*, Btype*);
181 placeholder_struct_type(const std::string&, Location);
184 set_placeholder_struct_type(Btype* placeholder,
185 const std::vector<Btyped_identifier>&);
188 placeholder_array_type(const std::string&, Location);
191 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
194 named_type(const std::string&, Btype*, Location);
197 circular_pointer_type(Btype*, bool);
200 is_circular_pointer_type(Btype*);
206 type_alignment(Btype*);
209 type_field_alignment(Btype*);
212 type_field_offset(Btype*, size_t index);
217 zero_expression(Btype*);
223 { return this->make_statement(error_mark_node); }
226 expression_statement(Bexpression*);
229 init_statement(Bvariable* var, Bexpression* init);
232 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
235 return_statement(Bfunction*, const std::vector<Bexpression*>&,
239 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
243 switch_statement(Bexpression* value,
244 const std::vector<std::vector<Bexpression*> >& cases,
245 const std::vector<Bstatement*>& statements,
249 compound_statement(Bstatement*, Bstatement*);
252 statement_list(const std::vector<Bstatement*>&);
257 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
261 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
264 block_statement(Bblock*);
270 { return new Bvariable(error_mark_node); }
273 global_variable(const std::string& package_name,
274 const std::string& unique_prefix,
275 const std::string& name,
282 global_variable_set_init(Bvariable*, Bexpression*);
285 local_variable(Bfunction*, const std::string&, Btype*, bool,
289 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
293 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
294 Location, Bstatement**);
297 immutable_struct(const std::string&, bool, Btype*, Location);
300 immutable_struct_set_init(Bvariable*, const std::string&, bool, Btype*,
301 Location, Bexpression*);
304 immutable_struct_reference(const std::string&, Btype*, Location);
309 label(Bfunction*, const std::string& name, Location);
312 label_definition_statement(Blabel*);
315 goto_statement(Blabel*, Location);
318 label_address(Blabel*, Location);
321 // Make a Bexpression from a tree.
323 make_expression(tree t)
324 { return new Bexpression(t); }
326 // Make a Bstatement from a tree.
328 make_statement(tree t)
329 { return new Bstatement(t); }
331 // Make a Btype from a tree.
334 { return new Btype(t); }
337 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
340 fill_in_array(Btype*, Btype*, Bexpression*);
343 non_zero_size_type(tree);
346 // A helper function.
349 get_identifier_from_string(const std::string& str)
351 return get_identifier_with_length(str.data(), str.length());
354 // Get an unnamed integer type.
357 Gcc_backend::integer_type(bool is_unsigned, int bits)
362 if (bits == INT_TYPE_SIZE)
363 type = unsigned_type_node;
364 else if (bits == CHAR_TYPE_SIZE)
365 type = unsigned_char_type_node;
366 else if (bits == SHORT_TYPE_SIZE)
367 type = short_unsigned_type_node;
368 else if (bits == LONG_TYPE_SIZE)
369 type = long_unsigned_type_node;
370 else if (bits == LONG_LONG_TYPE_SIZE)
371 type = long_long_unsigned_type_node;
373 type = make_unsigned_type(bits);
377 if (bits == INT_TYPE_SIZE)
378 type = integer_type_node;
379 else if (bits == CHAR_TYPE_SIZE)
380 type = signed_char_type_node;
381 else if (bits == SHORT_TYPE_SIZE)
382 type = short_integer_type_node;
383 else if (bits == LONG_TYPE_SIZE)
384 type = long_integer_type_node;
385 else if (bits == LONG_LONG_TYPE_SIZE)
386 type = long_long_integer_type_node;
388 type = make_signed_type(bits);
390 return this->make_type(type);
393 // Get an unnamed float type.
396 Gcc_backend::float_type(int bits)
399 if (bits == FLOAT_TYPE_SIZE)
400 type = float_type_node;
401 else if (bits == DOUBLE_TYPE_SIZE)
402 type = double_type_node;
403 else if (bits == LONG_DOUBLE_TYPE_SIZE)
404 type = long_double_type_node;
407 type = make_node(REAL_TYPE);
408 TYPE_PRECISION(type) = bits;
411 return this->make_type(type);
414 // Get an unnamed complex type.
417 Gcc_backend::complex_type(int bits)
420 if (bits == FLOAT_TYPE_SIZE * 2)
421 type = complex_float_type_node;
422 else if (bits == DOUBLE_TYPE_SIZE * 2)
423 type = complex_double_type_node;
424 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
425 type = complex_long_double_type_node;
428 type = make_node(REAL_TYPE);
429 TYPE_PRECISION(type) = bits / 2;
431 type = build_complex_type(type);
433 return this->make_type(type);
436 // Get a pointer type.
439 Gcc_backend::pointer_type(Btype* to_type)
441 tree to_type_tree = to_type->get_tree();
442 if (to_type_tree == error_mark_node)
443 return this->error_type();
444 tree type = build_pointer_type(to_type_tree);
445 return this->make_type(type);
448 // Make a function type.
451 Gcc_backend::function_type(const Btyped_identifier& receiver,
452 const std::vector<Btyped_identifier>& parameters,
453 const std::vector<Btyped_identifier>& results,
456 tree args = NULL_TREE;
458 if (receiver.btype != NULL)
460 tree t = receiver.btype->get_tree();
461 if (t == error_mark_node)
462 return this->error_type();
463 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
464 pp = &TREE_CHAIN(*pp);
467 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
468 p != parameters.end();
471 tree t = p->btype->get_tree();
472 if (t == error_mark_node)
473 return this->error_type();
474 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
475 pp = &TREE_CHAIN(*pp);
478 // Varargs is handled entirely at the Go level. When converted to
479 // GENERIC functions are not varargs.
480 *pp = void_list_node;
484 result = void_type_node;
485 else if (results.size() == 1)
486 result = results.front().btype->get_tree();
489 result = make_node(RECORD_TYPE);
490 tree field_trees = NULL_TREE;
492 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
496 const std::string name = (p->name.empty()
499 tree name_tree = get_identifier_from_string(name);
500 tree field_type_tree = p->btype->get_tree();
501 if (field_type_tree == error_mark_node)
502 return this->error_type();
503 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
504 tree field = build_decl(location.gcc_location(), FIELD_DECL,
505 name_tree, field_type_tree);
506 DECL_CONTEXT(field) = result;
508 pp = &DECL_CHAIN(field);
510 TYPE_FIELDS(result) = field_trees;
513 if (result == error_mark_node)
514 return this->error_type();
516 tree fntype = build_function_type(result, args);
517 if (fntype == error_mark_node)
518 return this->error_type();
520 return this->make_type(build_pointer_type(fntype));
523 // Make a struct type.
526 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
528 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
531 // Fill in the fields of a struct type.
534 Gcc_backend::fill_in_struct(Btype* fill,
535 const std::vector<Btyped_identifier>& fields)
537 tree fill_tree = fill->get_tree();
538 tree field_trees = NULL_TREE;
539 tree* pp = &field_trees;
540 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
544 tree name_tree = get_identifier_from_string(p->name);
545 tree type_tree = p->btype->get_tree();
546 if (type_tree == error_mark_node)
547 return this->error_type();
548 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
550 DECL_CONTEXT(field) = fill_tree;
552 pp = &DECL_CHAIN(field);
554 TYPE_FIELDS(fill_tree) = field_trees;
555 layout_type(fill_tree);
559 // Make an array type.
562 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
564 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
565 element_btype, length);
568 // Fill in an array type.
571 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
574 tree element_type_tree = element_type->get_tree();
575 tree length_tree = length->get_tree();
576 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
577 return this->error_type();
579 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
581 length_tree = fold_convert(sizetype, length_tree);
583 // build_index_type takes the maximum index, which is one less than
585 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
589 tree fill_tree = fill->get_tree();
590 TREE_TYPE(fill_tree) = element_type_tree;
591 TYPE_DOMAIN(fill_tree) = index_type_tree;
592 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
593 layout_type(fill_tree);
595 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
596 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
597 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
598 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
599 TYPE_CANONICAL(fill_tree) =
600 build_array_type(TYPE_CANONICAL(element_type_tree),
601 TYPE_CANONICAL(index_type_tree));
606 // Create a placeholder for a pointer type.
609 Gcc_backend::placeholder_pointer_type(const std::string& name,
610 Location location, bool)
612 tree ret = build_distinct_type_copy(ptr_type_node);
615 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
616 get_identifier_from_string(name),
618 TYPE_NAME(ret) = decl;
620 return this->make_type(ret);
623 // Set the real target type for a placeholder pointer type.
626 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
629 tree pt = placeholder->get_tree();
630 if (pt == error_mark_node)
632 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
633 tree tt = to_type->get_tree();
634 if (tt == error_mark_node)
636 placeholder->set_tree(error_mark_node);
639 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
640 TREE_TYPE(pt) = TREE_TYPE(tt);
641 if (TYPE_NAME(pt) != NULL_TREE)
643 // Build the data structure gcc wants to see for a typedef.
644 tree copy = build_variant_type_copy(pt);
645 TYPE_NAME(copy) = NULL_TREE;
646 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
651 // Set the real values for a placeholder function type.
654 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
656 return this->set_placeholder_pointer_type(placeholder, ft);
659 // Create a placeholder for a struct type.
662 Gcc_backend::placeholder_struct_type(const std::string& name,
665 tree ret = make_node(RECORD_TYPE);
668 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
669 get_identifier_from_string(name),
671 TYPE_NAME(ret) = decl;
673 return this->make_type(ret);
676 // Fill in the fields of a placeholder struct type.
679 Gcc_backend::set_placeholder_struct_type(
681 const std::vector<Btyped_identifier>& fields)
683 tree t = placeholder->get_tree();
684 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
685 Btype* r = this->fill_in_struct(placeholder, fields);
687 if (TYPE_NAME(t) != NULL_TREE)
689 // Build the data structure gcc wants to see for a typedef.
690 tree copy = build_distinct_type_copy(t);
691 TYPE_NAME(copy) = NULL_TREE;
692 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
695 return r->get_tree() != error_mark_node;
698 // Create a placeholder for an array type.
701 Gcc_backend::placeholder_array_type(const std::string& name,
704 tree ret = make_node(ARRAY_TYPE);
705 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
706 get_identifier_from_string(name),
708 TYPE_NAME(ret) = decl;
709 return this->make_type(ret);
712 // Fill in the fields of a placeholder array type.
715 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
716 Btype* element_btype,
719 tree t = placeholder->get_tree();
720 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
721 Btype* r = this->fill_in_array(placeholder, element_btype, length);
723 // Build the data structure gcc wants to see for a typedef.
724 tree copy = build_distinct_type_copy(t);
725 TYPE_NAME(copy) = NULL_TREE;
726 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
728 return r->get_tree() != error_mark_node;
731 // Return a named version of a type.
734 Gcc_backend::named_type(const std::string& name, Btype* btype,
737 tree type = btype->get_tree();
738 if (type == error_mark_node)
739 return this->error_type();
741 // The middle-end expects a basic type to have a name. In Go every
742 // basic type will have a name. The first time we see a basic type,
743 // give it whatever Go name we have at this point.
744 if (TYPE_NAME(type) == NULL_TREE
745 && location.gcc_location() == BUILTINS_LOCATION
746 && (TREE_CODE(type) == INTEGER_TYPE
747 || TREE_CODE(type) == REAL_TYPE
748 || TREE_CODE(type) == COMPLEX_TYPE
749 || TREE_CODE(type) == BOOLEAN_TYPE))
751 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
752 get_identifier_from_string(name),
754 TYPE_NAME(type) = decl;
755 return this->make_type(type);
758 tree copy = build_variant_type_copy(type);
759 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
760 get_identifier_from_string(name),
762 DECL_ORIGINAL_TYPE(decl) = type;
763 TYPE_NAME(copy) = decl;
764 return this->make_type(copy);
767 // Return a pointer type used as a marker for a circular type.
770 Gcc_backend::circular_pointer_type(Btype*, bool)
772 return this->make_type(ptr_type_node);
775 // Return whether we might be looking at a circular type.
778 Gcc_backend::is_circular_pointer_type(Btype* btype)
780 return btype->get_tree() == ptr_type_node;
783 // Return the size of a type.
786 Gcc_backend::type_size(Btype* btype)
788 tree t = btype->get_tree();
789 if (t == error_mark_node)
791 t = TYPE_SIZE_UNIT(t);
792 gcc_assert(TREE_CODE(t) == INTEGER_CST);
793 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
794 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
795 size_t ret = static_cast<size_t>(val_wide);
796 gcc_assert(ret == val_wide);
800 // Return the alignment of a type.
803 Gcc_backend::type_alignment(Btype* btype)
805 tree t = btype->get_tree();
806 if (t == error_mark_node)
808 return TYPE_ALIGN_UNIT(t);
811 // Return the alignment of a struct field of type BTYPE.
814 Gcc_backend::type_field_alignment(Btype* btype)
816 tree t = btype->get_tree();
817 if (t == error_mark_node)
819 return go_field_alignment(t);
822 // Return the offset of a field in a struct.
825 Gcc_backend::type_field_offset(Btype* btype, size_t index)
827 tree struct_tree = btype->get_tree();
828 if (struct_tree == error_mark_node)
830 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
831 tree field = TYPE_FIELDS(struct_tree);
832 for (; index > 0; --index)
834 field = DECL_CHAIN(field);
835 gcc_assert(field != NULL_TREE);
837 HOST_WIDE_INT offset_wide = int_byte_position(field);
838 gcc_assert(offset_wide >= 0);
839 size_t ret = static_cast<size_t>(offset_wide);
840 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
844 // Return the zero value for a type.
847 Gcc_backend::zero_expression(Btype* btype)
849 tree t = btype->get_tree();
851 if (t == error_mark_node)
852 ret = error_mark_node;
854 ret = build_zero_cst(t);
855 return tree_to_expr(ret);
858 // An expression as a statement.
861 Gcc_backend::expression_statement(Bexpression* expr)
863 return this->make_statement(expr->get_tree());
866 // Variable initialization.
869 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
871 tree var_tree = var->get_tree();
872 tree init_tree = init->get_tree();
873 if (var_tree == error_mark_node || init_tree == error_mark_node)
874 return this->error_statement();
875 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
877 // To avoid problems with GNU ld, we don't make zero-sized
878 // externally visible variables. That might lead us to doing an
879 // initialization of a zero-sized expression to a non-zero sized
880 // variable, or vice-versa. Avoid crashes by omitting the
881 // initializer. Such initializations don't mean anything anyhow.
882 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
883 && init_tree != NULL_TREE
884 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
886 DECL_INITIAL(var_tree) = init_tree;
887 init_tree = NULL_TREE;
890 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
891 void_type_node, var_tree);
892 if (init_tree != NULL_TREE)
893 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
894 void_type_node, init_tree, ret);
896 return this->make_statement(ret);
902 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
905 tree lhs_tree = lhs->get_tree();
906 tree rhs_tree = rhs->get_tree();
907 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
908 return this->error_statement();
910 // To avoid problems with GNU ld, we don't make zero-sized
911 // externally visible variables. That might lead us to doing an
912 // assignment of a zero-sized expression to a non-zero sized
913 // expression; avoid crashes here by avoiding assignments of
914 // zero-sized expressions. Such assignments don't really mean
916 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
917 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
918 return this->compound_statement(this->expression_statement(lhs),
919 this->expression_statement(rhs));
921 // Sometimes the same unnamed Go type can be created multiple times
922 // and thus have multiple tree representations. Make sure this does
923 // not confuse the middle-end.
924 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
926 tree lhs_type_tree = TREE_TYPE(lhs_tree);
927 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
928 if (POINTER_TYPE_P(lhs_type_tree)
929 || INTEGRAL_TYPE_P(lhs_type_tree)
930 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
931 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
932 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
934 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
935 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
937 gcc_assert(int_size_in_bytes(lhs_type_tree)
938 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
939 rhs_tree = fold_build1_loc(location.gcc_location(),
941 lhs_type_tree, rhs_tree);
945 return this->make_statement(fold_build2_loc(location.gcc_location(),
948 lhs_tree, rhs_tree));
954 Gcc_backend::return_statement(Bfunction* bfunction,
955 const std::vector<Bexpression*>& vals,
958 tree fntree = bfunction->get_tree();
959 if (fntree == error_mark_node)
960 return this->error_statement();
961 tree result = DECL_RESULT(fntree);
962 if (result == error_mark_node)
963 return this->error_statement();
966 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
968 else if (vals.size() == 1)
970 tree val = vals.front()->get_tree();
971 if (val == error_mark_node)
972 return this->error_statement();
973 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
974 void_type_node, result,
975 vals.front()->get_tree());
976 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
977 void_type_node, set);
981 // To return multiple values, copy the values into a temporary
982 // variable of the right structure type, and then assign the
983 // temporary variable to the DECL_RESULT in the return
985 tree stmt_list = NULL_TREE;
986 tree rettype = TREE_TYPE(result);
987 tree rettmp = create_tmp_var(rettype, "RESULT");
988 tree field = TYPE_FIELDS(rettype);
989 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
991 p++, field = DECL_CHAIN(field))
993 gcc_assert(field != NULL_TREE);
994 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
995 TREE_TYPE(field), rettmp, field,
997 tree val = (*p)->get_tree();
998 if (val == error_mark_node)
999 return this->error_statement();
1000 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1002 ref, (*p)->get_tree());
1003 append_to_statement_list(set, &stmt_list);
1005 gcc_assert(field == NULL_TREE);
1006 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1009 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1010 void_type_node, set);
1011 append_to_statement_list(ret_expr, &stmt_list);
1014 return this->make_statement(ret);
1020 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
1021 Bblock* else_block, Location location)
1023 tree cond_tree = condition->get_tree();
1024 tree then_tree = then_block->get_tree();
1025 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
1026 if (cond_tree == error_mark_node
1027 || then_tree == error_mark_node
1028 || else_tree == error_mark_node)
1029 return this->error_statement();
1030 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
1031 cond_tree, then_tree, else_tree);
1032 return this->make_statement(ret);
1038 Gcc_backend::switch_statement(
1040 const std::vector<std::vector<Bexpression*> >& cases,
1041 const std::vector<Bstatement*>& statements,
1042 Location switch_location)
1044 gcc_assert(cases.size() == statements.size());
1046 tree stmt_list = NULL_TREE;
1047 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
1048 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
1049 ps != statements.end();
1054 source_location loc = (*ps != NULL
1055 ? EXPR_LOCATION((*ps)->get_tree())
1056 : UNKNOWN_LOCATION);
1057 tree label = create_artificial_label(loc);
1058 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
1059 append_to_statement_list(c, &stmt_list);
1063 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1067 tree t = (*pcv)->get_tree();
1068 if (t == error_mark_node)
1069 return this->error_statement();
1070 source_location loc = EXPR_LOCATION(t);
1071 tree label = create_artificial_label(loc);
1072 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1073 append_to_statement_list(c, &stmt_list);
1079 tree t = (*ps)->get_tree();
1080 if (t == error_mark_node)
1081 return this->error_statement();
1082 append_to_statement_list(t, &stmt_list);
1086 tree tv = value->get_tree();
1087 if (tv == error_mark_node)
1088 return this->error_statement();
1089 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1090 NULL_TREE, tv, stmt_list, NULL_TREE);
1091 return this->make_statement(t);
1094 // Pair of statements.
1097 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1099 tree stmt_list = NULL_TREE;
1100 tree t = s1->get_tree();
1101 if (t == error_mark_node)
1102 return this->error_statement();
1103 append_to_statement_list(t, &stmt_list);
1105 if (t == error_mark_node)
1106 return this->error_statement();
1107 append_to_statement_list(t, &stmt_list);
1108 return this->make_statement(stmt_list);
1111 // List of statements.
1114 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1116 tree stmt_list = NULL_TREE;
1117 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1118 p != statements.end();
1121 tree t = (*p)->get_tree();
1122 if (t == error_mark_node)
1123 return this->error_statement();
1124 append_to_statement_list(t, &stmt_list);
1126 return this->make_statement(stmt_list);
1129 // Make a block. For some reason gcc uses a dual structure for
1130 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1131 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1135 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1136 const std::vector<Bvariable*>& vars,
1137 Location start_location,
1140 tree block_tree = make_node(BLOCK);
1141 if (enclosing == NULL)
1143 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1144 // until we have a proper representation of the init function.
1146 if (function == NULL)
1147 fndecl = current_function_decl;
1149 fndecl = function->get_tree();
1150 gcc_assert(fndecl != NULL_TREE);
1152 // We may have already created a block for local variables when
1153 // we take the address of a parameter.
1154 if (DECL_INITIAL(fndecl) == NULL_TREE)
1156 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1157 DECL_INITIAL(fndecl) = block_tree;
1161 tree superblock_tree = DECL_INITIAL(fndecl);
1162 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1164 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1166 pp = &BLOCK_CHAIN(*pp))
1173 tree superbind_tree = enclosing->get_tree();
1174 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1175 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1177 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1179 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1181 pp = &BLOCK_CHAIN(*pp))
1186 tree* pp = &BLOCK_VARS(block_tree);
1187 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1191 *pp = (*pv)->get_tree();
1192 if (*pp != error_mark_node)
1193 pp = &DECL_CHAIN(*pp);
1197 TREE_USED(block_tree) = 1;
1199 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1200 void_type_node, BLOCK_VARS(block_tree),
1201 NULL_TREE, block_tree);
1202 TREE_SIDE_EFFECTS(bind_tree) = 1;
1204 return new Bblock(bind_tree);
1207 // Add statements to a block.
1210 Gcc_backend::block_add_statements(Bblock* bblock,
1211 const std::vector<Bstatement*>& statements)
1213 tree stmt_list = NULL_TREE;
1214 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1215 p != statements.end();
1218 tree s = (*p)->get_tree();
1219 if (s != error_mark_node)
1220 append_to_statement_list(s, &stmt_list);
1223 tree bind_tree = bblock->get_tree();
1224 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1225 BIND_EXPR_BODY(bind_tree) = stmt_list;
1228 // Return a block as a statement.
1231 Gcc_backend::block_statement(Bblock* bblock)
1233 tree bind_tree = bblock->get_tree();
1234 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1235 return this->make_statement(bind_tree);
1238 // This is not static because we declare it with GTY(()) in go-c.h.
1239 tree go_non_zero_struct;
1241 // Return a type corresponding to TYPE with non-zero size.
1244 Gcc_backend::non_zero_size_type(tree type)
1246 if (int_size_in_bytes(type) != 0)
1249 switch (TREE_CODE(type))
1253 if (go_non_zero_struct == NULL_TREE)
1255 type = make_node(RECORD_TYPE);
1256 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
1257 get_identifier("dummy"),
1259 DECL_CONTEXT(field) = type;
1260 TYPE_FIELDS(type) = field;
1262 go_non_zero_struct = type;
1264 return go_non_zero_struct;
1269 tree element_type = non_zero_size_type(TREE_TYPE(type));
1270 return build_array_type_nelts(element_type, 1);
1280 // Make a global variable.
1283 Gcc_backend::global_variable(const std::string& package_name,
1284 const std::string& unique_prefix,
1285 const std::string& name,
1291 tree type_tree = btype->get_tree();
1292 if (type_tree == error_mark_node)
1293 return this->error_variable();
1295 // The GNU linker does not like dynamic variables with zero size.
1296 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
1297 type_tree = this->non_zero_size_type(type_tree);
1299 std::string var_name(package_name);
1300 var_name.push_back('.');
1301 var_name.append(name);
1302 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1303 get_identifier_from_string(var_name),
1306 DECL_EXTERNAL(decl) = 1;
1308 TREE_STATIC(decl) = 1;
1311 TREE_PUBLIC(decl) = 1;
1313 std::string asm_name(unique_prefix);
1314 asm_name.push_back('.');
1315 asm_name.append(var_name);
1316 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1318 TREE_USED(decl) = 1;
1320 go_preserve_from_gc(decl);
1322 return new Bvariable(decl);
1325 // Set the initial value of a global variable.
1328 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1330 tree expr_tree = expr->get_tree();
1331 if (expr_tree == error_mark_node)
1333 gcc_assert(TREE_CONSTANT(expr_tree));
1334 tree var_decl = var->get_tree();
1335 if (var_decl == error_mark_node)
1337 DECL_INITIAL(var_decl) = expr_tree;
1340 // Make a local variable.
1343 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1344 Btype* btype, bool is_address_taken,
1347 tree type_tree = btype->get_tree();
1348 if (type_tree == error_mark_node)
1349 return this->error_variable();
1350 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1351 get_identifier_from_string(name),
1353 DECL_CONTEXT(decl) = function->get_tree();
1354 TREE_USED(decl) = 1;
1355 if (is_address_taken)
1356 TREE_ADDRESSABLE(decl) = 1;
1357 go_preserve_from_gc(decl);
1358 return new Bvariable(decl);
1361 // Make a function parameter variable.
1364 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1365 Btype* btype, bool is_address_taken,
1368 tree type_tree = btype->get_tree();
1369 if (type_tree == error_mark_node)
1370 return this->error_variable();
1371 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1372 get_identifier_from_string(name),
1374 DECL_CONTEXT(decl) = function->get_tree();
1375 DECL_ARG_TYPE(decl) = type_tree;
1376 TREE_USED(decl) = 1;
1377 if (is_address_taken)
1378 TREE_ADDRESSABLE(decl) = 1;
1379 go_preserve_from_gc(decl);
1380 return new Bvariable(decl);
1383 // Make a temporary variable.
1386 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1387 Btype* btype, Bexpression* binit,
1388 bool is_address_taken,
1390 Bstatement** pstatement)
1392 tree type_tree = btype->get_tree();
1393 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1394 if (type_tree == error_mark_node || init_tree == error_mark_node)
1396 *pstatement = this->error_statement();
1397 return this->error_variable();
1401 // We can only use create_tmp_var if the type is not addressable.
1402 if (!TREE_ADDRESSABLE(type_tree))
1403 var = create_tmp_var(type_tree, "GOTMP");
1406 gcc_assert(bblock != NULL);
1407 var = build_decl(location.gcc_location(), VAR_DECL,
1408 create_tmp_var_name("GOTMP"),
1410 DECL_ARTIFICIAL(var) = 1;
1411 DECL_IGNORED_P(var) = 1;
1413 // FIXME: Permitting function to be NULL here is a temporary
1414 // measure until we have a proper representation of the init
1416 if (function != NULL)
1417 DECL_CONTEXT(var) = function->get_tree();
1420 gcc_assert(current_function_decl != NULL_TREE);
1421 DECL_CONTEXT(var) = current_function_decl;
1424 // We have to add this variable to the BLOCK and the BIND_EXPR.
1425 tree bind_tree = bblock->get_tree();
1426 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1427 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1428 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1429 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1430 BLOCK_VARS(block_tree) = var;
1431 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1434 if (init_tree != NULL_TREE)
1435 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1438 if (is_address_taken)
1439 TREE_ADDRESSABLE(var) = 1;
1441 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1443 void_type_node, var));
1444 return new Bvariable(var);
1447 // Create a named immutable initialized data structure.
1450 Gcc_backend::immutable_struct(const std::string& name, bool, Btype* btype,
1453 tree type_tree = btype->get_tree();
1454 if (type_tree == error_mark_node)
1455 return this->error_variable();
1456 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1457 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1458 get_identifier_from_string(name),
1459 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1460 TREE_STATIC(decl) = 1;
1461 TREE_READONLY(decl) = 1;
1462 TREE_CONSTANT(decl) = 1;
1463 TREE_USED(decl) = 1;
1464 DECL_ARTIFICIAL(decl) = 1;
1466 // We don't call rest_of_decl_compilation until we have the
1469 go_preserve_from_gc(decl);
1470 return new Bvariable(decl);
1473 // Set the initializer for a variable created by immutable_struct.
1474 // This is where we finish compiling the variable.
1477 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1478 bool is_common, Btype*,
1480 Bexpression* initializer)
1482 tree decl = var->get_tree();
1483 tree init_tree = initializer->get_tree();
1484 if (decl == error_mark_node || init_tree == error_mark_node)
1487 DECL_INITIAL(decl) = init_tree;
1489 // We can't call make_decl_one_only until we set DECL_INITIAL.
1491 TREE_PUBLIC(decl) = 1;
1494 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1495 resolve_unique_section(decl, 1, 0);
1498 rest_of_decl_compilation(decl, 1, 0);
1501 // Return a reference to an immutable initialized data structure
1502 // defined in another package.
1505 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1508 tree type_tree = btype->get_tree();
1509 if (type_tree == error_mark_node)
1510 return this->error_variable();
1511 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1512 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1513 get_identifier_from_string(name),
1514 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1515 TREE_READONLY(decl) = 1;
1516 TREE_CONSTANT(decl) = 1;
1517 DECL_ARTIFICIAL(decl) = 1;
1518 TREE_PUBLIC(decl) = 1;
1519 DECL_EXTERNAL(decl) = 1;
1520 go_preserve_from_gc(decl);
1521 return new Bvariable(decl);
1527 Gcc_backend::label(Bfunction* function, const std::string& name,
1532 decl = create_artificial_label(location.gcc_location());
1535 tree id = get_identifier_from_string(name);
1536 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1538 DECL_CONTEXT(decl) = function->get_tree();
1540 return new Blabel(decl);
1543 // Make a statement which defines a label.
1546 Gcc_backend::label_definition_statement(Blabel* label)
1548 tree lab = label->get_tree();
1549 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1550 void_type_node, lab);
1551 return this->make_statement(ret);
1554 // Make a goto statement.
1557 Gcc_backend::goto_statement(Blabel* label, Location location)
1559 tree lab = label->get_tree();
1560 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1562 return this->make_statement(ret);
1565 // Get the address of a label.
1568 Gcc_backend::label_address(Blabel* label, Location location)
1570 tree lab = label->get_tree();
1572 TREE_ADDRESSABLE(lab) = 1;
1573 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1574 build_fold_addr_expr_loc(location.gcc_location(),
1576 return this->make_expression(ret);
1579 // The single backend.
1581 static Gcc_backend gcc_backend;
1583 // Return the backend generator.
1588 return &gcc_backend;
1591 // FIXME: Temporary functions while converting to the new backend
1595 tree_to_type(tree t)
1597 return new Btype(t);
1601 tree_to_expr(tree t)
1603 return new Bexpression(t);
1607 tree_to_stat(tree t)
1609 return new Bstatement(t);
1613 tree_to_function(tree t)
1615 return new Bfunction(t);
1619 tree_to_block(tree t)
1621 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1622 return new Bblock(t);
1626 type_to_tree(Btype* bt)
1628 return bt->get_tree();
1632 expr_to_tree(Bexpression* be)
1634 return be->get_tree();
1638 stat_to_tree(Bstatement* bs)
1640 return bs->get_tree();
1644 block_to_tree(Bblock* bb)
1646 return bb->get_tree();
1650 var_to_tree(Bvariable* bv)
1652 return bv->get_tree();