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.
63 // In gcc, types, expressions, and statements are all trees.
64 class Btype : public Gcc_tree
72 class Bexpression : public Gcc_tree
80 class Bstatement : public Gcc_tree
88 class Bfunction : public Gcc_tree
96 class Bblock : public Gcc_tree
104 class Bvariable : public Gcc_tree
112 class Blabel : public Gcc_tree
120 // This file implements the interface between the Go frontend proper
121 // and the gcc IR. This implements specific instantiations of
122 // abstract classes defined by the Go frontend proper. The Go
123 // frontend proper class methods of these classes to generate the
124 // backend representation.
126 class Gcc_backend : public Backend
133 { return this->make_type(error_mark_node); }
137 { return this->make_type(void_type_node); }
141 { return this->make_type(boolean_type_node); }
144 integer_type(bool, int);
153 pointer_type(Btype*);
156 function_type(const Btyped_identifier&,
157 const std::vector<Btyped_identifier>&,
158 const std::vector<Btyped_identifier>&,
162 struct_type(const std::vector<Btyped_identifier>&);
165 array_type(Btype*, Bexpression*);
168 placeholder_pointer_type(const std::string&, Location, bool);
171 set_placeholder_pointer_type(Btype*, Btype*);
174 set_placeholder_function_type(Btype*, Btype*);
177 placeholder_struct_type(const std::string&, Location);
180 set_placeholder_struct_type(Btype* placeholder,
181 const std::vector<Btyped_identifier>&);
184 placeholder_array_type(const std::string&, Location);
187 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
190 named_type(const std::string&, Btype*, Location);
193 circular_pointer_type(Btype*, bool);
196 is_circular_pointer_type(Btype*);
202 type_alignment(Btype*);
205 type_field_alignment(Btype*);
208 type_field_offset(Btype*, size_t index);
213 zero_expression(Btype*);
219 { return this->make_statement(error_mark_node); }
222 expression_statement(Bexpression*);
225 init_statement(Bvariable* var, Bexpression* init);
228 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
231 return_statement(Bfunction*, const std::vector<Bexpression*>&,
235 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
239 switch_statement(Bexpression* value,
240 const std::vector<std::vector<Bexpression*> >& cases,
241 const std::vector<Bstatement*>& statements,
245 compound_statement(Bstatement*, Bstatement*);
248 statement_list(const std::vector<Bstatement*>&);
253 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
257 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
260 block_statement(Bblock*);
266 { return new Bvariable(error_mark_node); }
269 global_variable(const std::string& package_name,
270 const std::string& unique_prefix,
271 const std::string& name,
278 global_variable_set_init(Bvariable*, Bexpression*);
281 local_variable(Bfunction*, const std::string&, Btype*, bool,
285 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
289 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
290 Location, Bstatement**);
293 immutable_struct(const std::string&, bool, Btype*, Location);
296 immutable_struct_set_init(Bvariable*, const std::string&, bool, Btype*,
297 Location, Bexpression*);
300 immutable_struct_reference(const std::string&, Btype*, Location);
305 label(Bfunction*, const std::string& name, Location);
308 label_definition_statement(Blabel*);
311 goto_statement(Blabel*, Location);
314 label_address(Blabel*, Location);
317 // Make a Bexpression from a tree.
319 make_expression(tree t)
320 { return new Bexpression(t); }
322 // Make a Bstatement from a tree.
324 make_statement(tree t)
325 { return new Bstatement(t); }
327 // Make a Btype from a tree.
330 { return new Btype(t); }
333 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
336 fill_in_array(Btype*, Btype*, Bexpression*);
339 // A helper function.
342 get_identifier_from_string(const std::string& str)
344 return get_identifier_with_length(str.data(), str.length());
347 // Get an unnamed integer type.
350 Gcc_backend::integer_type(bool is_unsigned, int bits)
355 if (bits == INT_TYPE_SIZE)
356 type = unsigned_type_node;
357 else if (bits == CHAR_TYPE_SIZE)
358 type = unsigned_char_type_node;
359 else if (bits == SHORT_TYPE_SIZE)
360 type = short_unsigned_type_node;
361 else if (bits == LONG_TYPE_SIZE)
362 type = long_unsigned_type_node;
363 else if (bits == LONG_LONG_TYPE_SIZE)
364 type = long_long_unsigned_type_node;
366 type = make_unsigned_type(bits);
370 if (bits == INT_TYPE_SIZE)
371 type = integer_type_node;
372 else if (bits == CHAR_TYPE_SIZE)
373 type = signed_char_type_node;
374 else if (bits == SHORT_TYPE_SIZE)
375 type = short_integer_type_node;
376 else if (bits == LONG_TYPE_SIZE)
377 type = long_integer_type_node;
378 else if (bits == LONG_LONG_TYPE_SIZE)
379 type = long_long_integer_type_node;
381 type = make_signed_type(bits);
383 return this->make_type(type);
386 // Get an unnamed float type.
389 Gcc_backend::float_type(int bits)
392 if (bits == FLOAT_TYPE_SIZE)
393 type = float_type_node;
394 else if (bits == DOUBLE_TYPE_SIZE)
395 type = double_type_node;
396 else if (bits == LONG_DOUBLE_TYPE_SIZE)
397 type = long_double_type_node;
400 type = make_node(REAL_TYPE);
401 TYPE_PRECISION(type) = bits;
404 return this->make_type(type);
407 // Get an unnamed complex type.
410 Gcc_backend::complex_type(int bits)
413 if (bits == FLOAT_TYPE_SIZE * 2)
414 type = complex_float_type_node;
415 else if (bits == DOUBLE_TYPE_SIZE * 2)
416 type = complex_double_type_node;
417 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
418 type = complex_long_double_type_node;
421 type = make_node(REAL_TYPE);
422 TYPE_PRECISION(type) = bits / 2;
424 type = build_complex_type(type);
426 return this->make_type(type);
429 // Get a pointer type.
432 Gcc_backend::pointer_type(Btype* to_type)
434 tree to_type_tree = to_type->get_tree();
435 if (to_type_tree == error_mark_node)
436 return this->error_type();
437 tree type = build_pointer_type(to_type_tree);
438 return this->make_type(type);
441 // Make a function type.
444 Gcc_backend::function_type(const Btyped_identifier& receiver,
445 const std::vector<Btyped_identifier>& parameters,
446 const std::vector<Btyped_identifier>& results,
449 tree args = NULL_TREE;
451 if (receiver.btype != NULL)
453 tree t = receiver.btype->get_tree();
454 if (t == error_mark_node)
455 return this->error_type();
456 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
457 pp = &TREE_CHAIN(*pp);
460 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
461 p != parameters.end();
464 tree t = p->btype->get_tree();
465 if (t == error_mark_node)
466 return this->error_type();
467 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
468 pp = &TREE_CHAIN(*pp);
471 // Varargs is handled entirely at the Go level. When converted to
472 // GENERIC functions are not varargs.
473 *pp = void_list_node;
477 result = void_type_node;
478 else if (results.size() == 1)
479 result = results.front().btype->get_tree();
482 result = make_node(RECORD_TYPE);
483 tree field_trees = NULL_TREE;
485 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
489 const std::string name = (p->name.empty()
492 tree name_tree = get_identifier_from_string(name);
493 tree field_type_tree = p->btype->get_tree();
494 if (field_type_tree == error_mark_node)
495 return this->error_type();
496 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
497 tree field = build_decl(location.gcc_location(), FIELD_DECL,
498 name_tree, field_type_tree);
499 DECL_CONTEXT(field) = result;
501 pp = &DECL_CHAIN(field);
503 TYPE_FIELDS(result) = field_trees;
506 if (result == error_mark_node)
507 return this->error_type();
509 tree fntype = build_function_type(result, args);
510 if (fntype == error_mark_node)
511 return this->error_type();
513 return this->make_type(build_pointer_type(fntype));
516 // Make a struct type.
519 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
521 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
524 // Fill in the fields of a struct type.
527 Gcc_backend::fill_in_struct(Btype* fill,
528 const std::vector<Btyped_identifier>& fields)
530 tree fill_tree = fill->get_tree();
531 tree field_trees = NULL_TREE;
532 tree* pp = &field_trees;
533 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
537 tree name_tree = get_identifier_from_string(p->name);
538 tree type_tree = p->btype->get_tree();
539 if (type_tree == error_mark_node)
540 return this->error_type();
541 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
543 DECL_CONTEXT(field) = fill_tree;
545 pp = &DECL_CHAIN(field);
547 TYPE_FIELDS(fill_tree) = field_trees;
548 layout_type(fill_tree);
552 // Make an array type.
555 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
557 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
558 element_btype, length);
561 // Fill in an array type.
564 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
567 tree element_type_tree = element_type->get_tree();
568 tree length_tree = length->get_tree();
569 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
570 return this->error_type();
572 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
574 length_tree = fold_convert(sizetype, length_tree);
576 // build_index_type takes the maximum index, which is one less than
578 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
582 tree fill_tree = fill->get_tree();
583 TREE_TYPE(fill_tree) = element_type_tree;
584 TYPE_DOMAIN(fill_tree) = index_type_tree;
585 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
586 layout_type(fill_tree);
588 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
589 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
590 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
591 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
592 TYPE_CANONICAL(fill_tree) =
593 build_array_type(TYPE_CANONICAL(element_type_tree),
594 TYPE_CANONICAL(index_type_tree));
599 // Create a placeholder for a pointer type.
602 Gcc_backend::placeholder_pointer_type(const std::string& name,
603 Location location, bool)
605 tree ret = build_variant_type_copy(ptr_type_node);
608 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
609 get_identifier_from_string(name),
611 TYPE_NAME(ret) = decl;
613 return this->make_type(ret);
616 // Set the real target type for a placeholder pointer type.
619 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
622 tree pt = placeholder->get_tree();
623 if (pt == error_mark_node)
625 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
626 tree tt = to_type->get_tree();
627 if (tt == error_mark_node)
632 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
633 TREE_TYPE(pt) = TREE_TYPE(tt);
634 if (TYPE_NAME(pt) != NULL_TREE)
636 // Build the data structure gcc wants to see for a typedef.
637 tree copy = build_variant_type_copy(pt);
638 TYPE_NAME(copy) = NULL_TREE;
639 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
644 // Set the real values for a placeholder function type.
647 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
649 return this->set_placeholder_pointer_type(placeholder, ft);
652 // Create a placeholder for a struct type.
655 Gcc_backend::placeholder_struct_type(const std::string& name,
658 tree ret = make_node(RECORD_TYPE);
661 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
662 get_identifier_from_string(name),
664 TYPE_NAME(ret) = decl;
666 return this->make_type(ret);
669 // Fill in the fields of a placeholder struct type.
672 Gcc_backend::set_placeholder_struct_type(
674 const std::vector<Btyped_identifier>& fields)
676 tree t = placeholder->get_tree();
677 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
678 Btype* r = this->fill_in_struct(placeholder, fields);
680 if (TYPE_NAME(t) != NULL_TREE)
682 // Build the data structure gcc wants to see for a typedef.
683 tree copy = build_distinct_type_copy(t);
684 TYPE_NAME(copy) = NULL_TREE;
685 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
688 return r->get_tree() != error_mark_node;
691 // Create a placeholder for an array type.
694 Gcc_backend::placeholder_array_type(const std::string& name,
697 tree ret = make_node(ARRAY_TYPE);
698 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
699 get_identifier_from_string(name),
701 TYPE_NAME(ret) = decl;
702 return this->make_type(ret);
705 // Fill in the fields of a placeholder array type.
708 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
709 Btype* element_btype,
712 tree t = placeholder->get_tree();
713 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
714 Btype* r = this->fill_in_array(placeholder, element_btype, length);
716 // Build the data structure gcc wants to see for a typedef.
717 tree copy = build_distinct_type_copy(t);
718 TYPE_NAME(copy) = NULL_TREE;
719 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
721 return r->get_tree() != error_mark_node;
724 // Return a named version of a type.
727 Gcc_backend::named_type(const std::string& name, Btype* btype,
730 tree type = btype->get_tree();
731 if (type == error_mark_node)
732 return this->error_type();
734 // The middle-end expects a basic type to have a name. In Go every
735 // basic type will have a name. The first time we see a basic type,
736 // give it whatever Go name we have at this point.
737 if (TYPE_NAME(type) == NULL_TREE
738 && location.gcc_location() == BUILTINS_LOCATION
739 && (TREE_CODE(type) == INTEGER_TYPE
740 || TREE_CODE(type) == REAL_TYPE
741 || TREE_CODE(type) == COMPLEX_TYPE
742 || TREE_CODE(type) == BOOLEAN_TYPE))
744 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
745 get_identifier_from_string(name),
747 TYPE_NAME(type) = decl;
748 return this->make_type(type);
751 tree copy = build_variant_type_copy(type);
752 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
753 get_identifier_from_string(name),
755 DECL_ORIGINAL_TYPE(decl) = type;
756 TYPE_NAME(copy) = decl;
757 return this->make_type(copy);
760 // Return a pointer type used as a marker for a circular type.
763 Gcc_backend::circular_pointer_type(Btype*, bool)
765 return this->make_type(ptr_type_node);
768 // Return whether we might be looking at a circular type.
771 Gcc_backend::is_circular_pointer_type(Btype* btype)
773 return btype->get_tree() == ptr_type_node;
776 // Return the size of a type.
779 Gcc_backend::type_size(Btype* btype)
781 tree t = btype->get_tree();
782 if (t == error_mark_node)
784 t = TYPE_SIZE_UNIT(t);
785 gcc_assert(TREE_CODE(t) == INTEGER_CST);
786 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
787 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
788 size_t ret = static_cast<size_t>(val_wide);
789 gcc_assert(ret == val_wide);
793 // Return the alignment of a type.
796 Gcc_backend::type_alignment(Btype* btype)
798 tree t = btype->get_tree();
799 if (t == error_mark_node)
801 return TYPE_ALIGN_UNIT(t);
804 // Return the alignment of a struct field of type BTYPE.
807 Gcc_backend::type_field_alignment(Btype* btype)
809 tree t = btype->get_tree();
810 if (t == error_mark_node)
812 return go_field_alignment(t);
815 // Return the offset of a field in a struct.
818 Gcc_backend::type_field_offset(Btype* btype, size_t index)
820 tree struct_tree = btype->get_tree();
821 if (struct_tree == error_mark_node)
823 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
824 tree field = TYPE_FIELDS(struct_tree);
825 for (; index > 0; --index)
827 field = DECL_CHAIN(field);
828 gcc_assert(field != NULL_TREE);
830 HOST_WIDE_INT offset_wide = int_byte_position(field);
831 gcc_assert(offset_wide >= 0);
832 size_t ret = static_cast<size_t>(offset_wide);
833 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
837 // Return the zero value for a type.
840 Gcc_backend::zero_expression(Btype* btype)
842 tree t = btype->get_tree();
844 if (t == error_mark_node)
845 ret = error_mark_node;
847 ret = build_zero_cst(t);
848 return tree_to_expr(ret);
851 // An expression as a statement.
854 Gcc_backend::expression_statement(Bexpression* expr)
856 return this->make_statement(expr->get_tree());
859 // Variable initialization.
862 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
864 tree var_tree = var->get_tree();
865 tree init_tree = init->get_tree();
866 if (var_tree == error_mark_node || init_tree == error_mark_node)
867 return this->error_statement();
868 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
869 DECL_INITIAL(var_tree) = init_tree;
870 return this->make_statement(build1_loc(DECL_SOURCE_LOCATION(var_tree),
871 DECL_EXPR, void_type_node, var_tree));
877 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
880 tree lhs_tree = lhs->get_tree();
881 tree rhs_tree = rhs->get_tree();
882 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
883 return this->error_statement();
884 return this->make_statement(fold_build2_loc(location.gcc_location(),
887 lhs_tree, rhs_tree));
893 Gcc_backend::return_statement(Bfunction* bfunction,
894 const std::vector<Bexpression*>& vals,
897 tree fntree = bfunction->get_tree();
898 if (fntree == error_mark_node)
899 return this->error_statement();
900 tree result = DECL_RESULT(fntree);
901 if (result == error_mark_node)
902 return this->error_statement();
905 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
907 else if (vals.size() == 1)
909 tree val = vals.front()->get_tree();
910 if (val == error_mark_node)
911 return this->error_statement();
912 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
913 void_type_node, result,
914 vals.front()->get_tree());
915 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
916 void_type_node, set);
920 // To return multiple values, copy the values into a temporary
921 // variable of the right structure type, and then assign the
922 // temporary variable to the DECL_RESULT in the return
924 tree stmt_list = NULL_TREE;
925 tree rettype = TREE_TYPE(result);
926 tree rettmp = create_tmp_var(rettype, "RESULT");
927 tree field = TYPE_FIELDS(rettype);
928 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
930 p++, field = DECL_CHAIN(field))
932 gcc_assert(field != NULL_TREE);
933 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
934 TREE_TYPE(field), rettmp, field,
936 tree val = (*p)->get_tree();
937 if (val == error_mark_node)
938 return this->error_statement();
939 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
941 ref, (*p)->get_tree());
942 append_to_statement_list(set, &stmt_list);
944 gcc_assert(field == NULL_TREE);
945 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
948 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
949 void_type_node, set);
950 append_to_statement_list(ret_expr, &stmt_list);
953 return this->make_statement(ret);
959 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
960 Bblock* else_block, Location location)
962 tree cond_tree = condition->get_tree();
963 tree then_tree = then_block->get_tree();
964 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
965 if (cond_tree == error_mark_node
966 || then_tree == error_mark_node
967 || else_tree == error_mark_node)
968 return this->error_statement();
969 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
970 cond_tree, then_tree, else_tree);
971 return this->make_statement(ret);
977 Gcc_backend::switch_statement(
979 const std::vector<std::vector<Bexpression*> >& cases,
980 const std::vector<Bstatement*>& statements,
981 Location switch_location)
983 gcc_assert(cases.size() == statements.size());
985 tree stmt_list = NULL_TREE;
986 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
987 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
988 ps != statements.end();
993 source_location loc = (*ps != NULL
994 ? EXPR_LOCATION((*ps)->get_tree())
996 tree label = create_artificial_label(loc);
997 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
998 append_to_statement_list(c, &stmt_list);
1002 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1006 tree t = (*pcv)->get_tree();
1007 if (t == error_mark_node)
1008 return this->error_statement();
1009 source_location loc = EXPR_LOCATION(t);
1010 tree label = create_artificial_label(loc);
1011 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1012 append_to_statement_list(c, &stmt_list);
1018 tree t = (*ps)->get_tree();
1019 if (t == error_mark_node)
1020 return this->error_statement();
1021 append_to_statement_list(t, &stmt_list);
1025 tree tv = value->get_tree();
1026 if (tv == error_mark_node)
1027 return this->error_statement();
1028 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1029 void_type_node, tv, stmt_list, NULL_TREE);
1030 return this->make_statement(t);
1033 // Pair of statements.
1036 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1038 tree stmt_list = NULL_TREE;
1039 tree t = s1->get_tree();
1040 if (t == error_mark_node)
1041 return this->error_statement();
1042 append_to_statement_list(t, &stmt_list);
1044 if (t == error_mark_node)
1045 return this->error_statement();
1046 append_to_statement_list(t, &stmt_list);
1047 return this->make_statement(stmt_list);
1050 // List of statements.
1053 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1055 tree stmt_list = NULL_TREE;
1056 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1057 p != statements.end();
1060 tree t = (*p)->get_tree();
1061 if (t == error_mark_node)
1062 return this->error_statement();
1063 append_to_statement_list(t, &stmt_list);
1065 return this->make_statement(stmt_list);
1068 // Make a block. For some reason gcc uses a dual structure for
1069 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1070 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1074 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1075 const std::vector<Bvariable*>& vars,
1076 Location start_location,
1079 tree block_tree = make_node(BLOCK);
1080 if (enclosing == NULL)
1082 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1083 // until we have a proper representation of the init function.
1085 if (function == NULL)
1086 fndecl = current_function_decl;
1088 fndecl = function->get_tree();
1089 gcc_assert(fndecl != NULL_TREE);
1091 // We may have already created a block for local variables when
1092 // we take the address of a parameter.
1093 if (DECL_INITIAL(fndecl) == NULL_TREE)
1095 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1096 DECL_INITIAL(fndecl) = block_tree;
1100 tree superblock_tree = DECL_INITIAL(fndecl);
1101 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1103 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1105 pp = &BLOCK_CHAIN(*pp))
1112 tree superbind_tree = enclosing->get_tree();
1113 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1114 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1116 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1118 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1120 pp = &BLOCK_CHAIN(*pp))
1125 tree* pp = &BLOCK_VARS(block_tree);
1126 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1130 *pp = (*pv)->get_tree();
1131 if (*pp != error_mark_node)
1132 pp = &DECL_CHAIN(*pp);
1136 TREE_USED(block_tree) = 1;
1138 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1139 void_type_node, BLOCK_VARS(block_tree),
1140 NULL_TREE, block_tree);
1141 TREE_SIDE_EFFECTS(bind_tree) = 1;
1143 return new Bblock(bind_tree);
1146 // Add statements to a block.
1149 Gcc_backend::block_add_statements(Bblock* bblock,
1150 const std::vector<Bstatement*>& statements)
1152 tree stmt_list = NULL_TREE;
1153 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1154 p != statements.end();
1157 tree s = (*p)->get_tree();
1158 if (s != error_mark_node)
1159 append_to_statement_list(s, &stmt_list);
1162 tree bind_tree = bblock->get_tree();
1163 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1164 BIND_EXPR_BODY(bind_tree) = stmt_list;
1167 // Return a block as a statement.
1170 Gcc_backend::block_statement(Bblock* bblock)
1172 tree bind_tree = bblock->get_tree();
1173 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1174 return this->make_statement(bind_tree);
1177 // Make a global variable.
1180 Gcc_backend::global_variable(const std::string& package_name,
1181 const std::string& unique_prefix,
1182 const std::string& name,
1188 tree type_tree = btype->get_tree();
1189 if (type_tree == error_mark_node)
1190 return this->error_variable();
1192 std::string var_name(package_name);
1193 var_name.push_back('.');
1194 var_name.append(name);
1195 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1196 get_identifier_from_string(var_name),
1199 DECL_EXTERNAL(decl) = 1;
1201 TREE_STATIC(decl) = 1;
1204 TREE_PUBLIC(decl) = 1;
1206 std::string asm_name(unique_prefix);
1207 asm_name.push_back('.');
1208 asm_name.append(var_name);
1209 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1211 TREE_USED(decl) = 1;
1213 go_preserve_from_gc(decl);
1215 return new Bvariable(decl);
1218 // Set the initial value of a global variable.
1221 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1223 tree expr_tree = expr->get_tree();
1224 if (expr_tree == error_mark_node)
1226 gcc_assert(TREE_CONSTANT(expr_tree));
1227 tree var_decl = var->get_tree();
1228 if (var_decl == error_mark_node)
1230 DECL_INITIAL(var_decl) = expr_tree;
1233 // Make a local variable.
1236 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1237 Btype* btype, bool is_address_taken,
1240 tree type_tree = btype->get_tree();
1241 if (type_tree == error_mark_node)
1242 return this->error_variable();
1243 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1244 get_identifier_from_string(name),
1246 DECL_CONTEXT(decl) = function->get_tree();
1247 TREE_USED(decl) = 1;
1248 if (is_address_taken)
1249 TREE_ADDRESSABLE(decl) = 1;
1250 go_preserve_from_gc(decl);
1251 return new Bvariable(decl);
1254 // Make a function parameter variable.
1257 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1258 Btype* btype, bool is_address_taken,
1261 tree type_tree = btype->get_tree();
1262 if (type_tree == error_mark_node)
1263 return this->error_variable();
1264 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1265 get_identifier_from_string(name),
1267 DECL_CONTEXT(decl) = function->get_tree();
1268 DECL_ARG_TYPE(decl) = type_tree;
1269 TREE_USED(decl) = 1;
1270 if (is_address_taken)
1271 TREE_ADDRESSABLE(decl) = 1;
1272 go_preserve_from_gc(decl);
1273 return new Bvariable(decl);
1276 // Make a temporary variable.
1279 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1280 Btype* btype, Bexpression* binit,
1281 bool is_address_taken,
1283 Bstatement** pstatement)
1285 tree type_tree = btype->get_tree();
1286 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1287 if (type_tree == error_mark_node || init_tree == error_mark_node)
1289 *pstatement = this->error_statement();
1290 return this->error_variable();
1294 // We can only use create_tmp_var if the type is not addressable.
1295 if (!TREE_ADDRESSABLE(type_tree))
1296 var = create_tmp_var(type_tree, "GOTMP");
1299 gcc_assert(bblock != NULL);
1300 var = build_decl(location.gcc_location(), VAR_DECL,
1301 create_tmp_var_name("GOTMP"),
1303 DECL_ARTIFICIAL(var) = 1;
1304 DECL_IGNORED_P(var) = 1;
1306 // FIXME: Permitting function to be NULL here is a temporary
1307 // measure until we have a proper representation of the init
1309 if (function != NULL)
1310 DECL_CONTEXT(var) = function->get_tree();
1313 gcc_assert(current_function_decl != NULL_TREE);
1314 DECL_CONTEXT(var) = current_function_decl;
1317 // We have to add this variable to the BLOCK and the BIND_EXPR.
1318 tree bind_tree = bblock->get_tree();
1319 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1320 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1321 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1322 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1323 BLOCK_VARS(block_tree) = var;
1324 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1327 if (init_tree != NULL_TREE)
1328 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1331 if (is_address_taken)
1332 TREE_ADDRESSABLE(var) = 1;
1334 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1336 void_type_node, var));
1337 return new Bvariable(var);
1340 // Create a named immutable initialized data structure.
1343 Gcc_backend::immutable_struct(const std::string& name, bool, Btype* btype,
1346 tree type_tree = btype->get_tree();
1347 if (type_tree == error_mark_node)
1348 return this->error_variable();
1349 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1350 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1351 get_identifier_from_string(name),
1352 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1353 TREE_STATIC(decl) = 1;
1354 TREE_READONLY(decl) = 1;
1355 TREE_CONSTANT(decl) = 1;
1356 TREE_USED(decl) = 1;
1357 DECL_ARTIFICIAL(decl) = 1;
1359 // We don't call rest_of_decl_compilation until we have the
1362 go_preserve_from_gc(decl);
1363 return new Bvariable(decl);
1366 // Set the initializer for a variable created by immutable_struct.
1367 // This is where we finish compiling the variable.
1370 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1371 bool is_common, Btype*,
1373 Bexpression* initializer)
1375 tree decl = var->get_tree();
1376 tree init_tree = initializer->get_tree();
1377 if (decl == error_mark_node || init_tree == error_mark_node)
1380 DECL_INITIAL(decl) = init_tree;
1382 // We can't call make_decl_one_only until we set DECL_INITIAL.
1384 TREE_PUBLIC(decl) = 1;
1387 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1388 resolve_unique_section(decl, 1, 0);
1391 rest_of_decl_compilation(decl, 1, 0);
1394 // Return a reference to an immutable initialized data structure
1395 // defined in another package.
1398 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1401 tree type_tree = btype->get_tree();
1402 if (type_tree == error_mark_node)
1403 return this->error_variable();
1404 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1405 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1406 get_identifier_from_string(name),
1407 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1408 TREE_READONLY(decl) = 1;
1409 TREE_CONSTANT(decl) = 1;
1410 DECL_ARTIFICIAL(decl) = 1;
1411 TREE_PUBLIC(decl) = 1;
1412 DECL_EXTERNAL(decl) = 1;
1413 go_preserve_from_gc(decl);
1414 return new Bvariable(decl);
1420 Gcc_backend::label(Bfunction* function, const std::string& name,
1425 decl = create_artificial_label(location.gcc_location());
1428 tree id = get_identifier_from_string(name);
1429 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1431 DECL_CONTEXT(decl) = function->get_tree();
1433 return new Blabel(decl);
1436 // Make a statement which defines a label.
1439 Gcc_backend::label_definition_statement(Blabel* label)
1441 tree lab = label->get_tree();
1442 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1443 void_type_node, lab);
1444 return this->make_statement(ret);
1447 // Make a goto statement.
1450 Gcc_backend::goto_statement(Blabel* label, Location location)
1452 tree lab = label->get_tree();
1453 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1455 return this->make_statement(ret);
1458 // Get the address of a label.
1461 Gcc_backend::label_address(Blabel* label, Location location)
1463 tree lab = label->get_tree();
1465 TREE_ADDRESSABLE(lab) = 1;
1466 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1467 build_fold_addr_expr_loc(location.gcc_location(),
1469 return this->make_expression(ret);
1472 // The single backend.
1474 static Gcc_backend gcc_backend;
1476 // Return the backend generator.
1481 return &gcc_backend;
1484 // FIXME: Temporary functions while converting to the new backend
1488 tree_to_type(tree t)
1490 return new Btype(t);
1494 tree_to_expr(tree t)
1496 return new Bexpression(t);
1500 tree_to_stat(tree t)
1502 return new Bstatement(t);
1506 tree_to_function(tree t)
1508 return new Bfunction(t);
1512 tree_to_block(tree t)
1514 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1515 return new Bblock(t);
1519 type_to_tree(Btype* bt)
1521 return bt->get_tree();
1525 expr_to_tree(Bexpression* be)
1527 return be->get_tree();
1531 stat_to_tree(Bstatement* bs)
1533 return bs->get_tree();
1537 block_to_tree(Bblock* bb)
1539 return bb->get_tree();
1543 var_to_tree(Bvariable* bv)
1545 return bv->get_tree();