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 // A helper function.
346 get_identifier_from_string(const std::string& str)
348 return get_identifier_with_length(str.data(), str.length());
351 // Get an unnamed integer type.
354 Gcc_backend::integer_type(bool is_unsigned, int bits)
359 if (bits == INT_TYPE_SIZE)
360 type = unsigned_type_node;
361 else if (bits == CHAR_TYPE_SIZE)
362 type = unsigned_char_type_node;
363 else if (bits == SHORT_TYPE_SIZE)
364 type = short_unsigned_type_node;
365 else if (bits == LONG_TYPE_SIZE)
366 type = long_unsigned_type_node;
367 else if (bits == LONG_LONG_TYPE_SIZE)
368 type = long_long_unsigned_type_node;
370 type = make_unsigned_type(bits);
374 if (bits == INT_TYPE_SIZE)
375 type = integer_type_node;
376 else if (bits == CHAR_TYPE_SIZE)
377 type = signed_char_type_node;
378 else if (bits == SHORT_TYPE_SIZE)
379 type = short_integer_type_node;
380 else if (bits == LONG_TYPE_SIZE)
381 type = long_integer_type_node;
382 else if (bits == LONG_LONG_TYPE_SIZE)
383 type = long_long_integer_type_node;
385 type = make_signed_type(bits);
387 return this->make_type(type);
390 // Get an unnamed float type.
393 Gcc_backend::float_type(int bits)
396 if (bits == FLOAT_TYPE_SIZE)
397 type = float_type_node;
398 else if (bits == DOUBLE_TYPE_SIZE)
399 type = double_type_node;
400 else if (bits == LONG_DOUBLE_TYPE_SIZE)
401 type = long_double_type_node;
404 type = make_node(REAL_TYPE);
405 TYPE_PRECISION(type) = bits;
408 return this->make_type(type);
411 // Get an unnamed complex type.
414 Gcc_backend::complex_type(int bits)
417 if (bits == FLOAT_TYPE_SIZE * 2)
418 type = complex_float_type_node;
419 else if (bits == DOUBLE_TYPE_SIZE * 2)
420 type = complex_double_type_node;
421 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
422 type = complex_long_double_type_node;
425 type = make_node(REAL_TYPE);
426 TYPE_PRECISION(type) = bits / 2;
428 type = build_complex_type(type);
430 return this->make_type(type);
433 // Get a pointer type.
436 Gcc_backend::pointer_type(Btype* to_type)
438 tree to_type_tree = to_type->get_tree();
439 if (to_type_tree == error_mark_node)
440 return this->error_type();
441 tree type = build_pointer_type(to_type_tree);
442 return this->make_type(type);
445 // Make a function type.
448 Gcc_backend::function_type(const Btyped_identifier& receiver,
449 const std::vector<Btyped_identifier>& parameters,
450 const std::vector<Btyped_identifier>& results,
453 tree args = NULL_TREE;
455 if (receiver.btype != NULL)
457 tree t = receiver.btype->get_tree();
458 if (t == error_mark_node)
459 return this->error_type();
460 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
461 pp = &TREE_CHAIN(*pp);
464 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
465 p != parameters.end();
468 tree t = p->btype->get_tree();
469 if (t == error_mark_node)
470 return this->error_type();
471 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
472 pp = &TREE_CHAIN(*pp);
475 // Varargs is handled entirely at the Go level. When converted to
476 // GENERIC functions are not varargs.
477 *pp = void_list_node;
481 result = void_type_node;
482 else if (results.size() == 1)
483 result = results.front().btype->get_tree();
486 result = make_node(RECORD_TYPE);
487 tree field_trees = NULL_TREE;
489 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
493 const std::string name = (p->name.empty()
496 tree name_tree = get_identifier_from_string(name);
497 tree field_type_tree = p->btype->get_tree();
498 if (field_type_tree == error_mark_node)
499 return this->error_type();
500 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
501 tree field = build_decl(location.gcc_location(), FIELD_DECL,
502 name_tree, field_type_tree);
503 DECL_CONTEXT(field) = result;
505 pp = &DECL_CHAIN(field);
507 TYPE_FIELDS(result) = field_trees;
510 if (result == error_mark_node)
511 return this->error_type();
513 tree fntype = build_function_type(result, args);
514 if (fntype == error_mark_node)
515 return this->error_type();
517 return this->make_type(build_pointer_type(fntype));
520 // Make a struct type.
523 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
525 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
528 // Fill in the fields of a struct type.
531 Gcc_backend::fill_in_struct(Btype* fill,
532 const std::vector<Btyped_identifier>& fields)
534 tree fill_tree = fill->get_tree();
535 tree field_trees = NULL_TREE;
536 tree* pp = &field_trees;
537 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
541 tree name_tree = get_identifier_from_string(p->name);
542 tree type_tree = p->btype->get_tree();
543 if (type_tree == error_mark_node)
544 return this->error_type();
545 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
547 DECL_CONTEXT(field) = fill_tree;
549 pp = &DECL_CHAIN(field);
551 TYPE_FIELDS(fill_tree) = field_trees;
552 layout_type(fill_tree);
556 // Make an array type.
559 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
561 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
562 element_btype, length);
565 // Fill in an array type.
568 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
571 tree element_type_tree = element_type->get_tree();
572 tree length_tree = length->get_tree();
573 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
574 return this->error_type();
576 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
578 length_tree = fold_convert(sizetype, length_tree);
580 // build_index_type takes the maximum index, which is one less than
582 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
586 tree fill_tree = fill->get_tree();
587 TREE_TYPE(fill_tree) = element_type_tree;
588 TYPE_DOMAIN(fill_tree) = index_type_tree;
589 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
590 layout_type(fill_tree);
592 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
593 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
594 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
595 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
596 TYPE_CANONICAL(fill_tree) =
597 build_array_type(TYPE_CANONICAL(element_type_tree),
598 TYPE_CANONICAL(index_type_tree));
603 // Create a placeholder for a pointer type.
606 Gcc_backend::placeholder_pointer_type(const std::string& name,
607 Location location, bool)
609 tree ret = build_distinct_type_copy(ptr_type_node);
612 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
613 get_identifier_from_string(name),
615 TYPE_NAME(ret) = decl;
617 return this->make_type(ret);
620 // Set the real target type for a placeholder pointer type.
623 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
626 tree pt = placeholder->get_tree();
627 if (pt == error_mark_node)
629 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
630 tree tt = to_type->get_tree();
631 if (tt == error_mark_node)
633 placeholder->set_tree(error_mark_node);
636 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
637 TREE_TYPE(pt) = TREE_TYPE(tt);
638 if (TYPE_NAME(pt) != NULL_TREE)
640 // Build the data structure gcc wants to see for a typedef.
641 tree copy = build_variant_type_copy(pt);
642 TYPE_NAME(copy) = NULL_TREE;
643 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
648 // Set the real values for a placeholder function type.
651 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
653 return this->set_placeholder_pointer_type(placeholder, ft);
656 // Create a placeholder for a struct type.
659 Gcc_backend::placeholder_struct_type(const std::string& name,
662 tree ret = make_node(RECORD_TYPE);
665 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
666 get_identifier_from_string(name),
668 TYPE_NAME(ret) = decl;
670 return this->make_type(ret);
673 // Fill in the fields of a placeholder struct type.
676 Gcc_backend::set_placeholder_struct_type(
678 const std::vector<Btyped_identifier>& fields)
680 tree t = placeholder->get_tree();
681 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
682 Btype* r = this->fill_in_struct(placeholder, fields);
684 if (TYPE_NAME(t) != NULL_TREE)
686 // Build the data structure gcc wants to see for a typedef.
687 tree copy = build_distinct_type_copy(t);
688 TYPE_NAME(copy) = NULL_TREE;
689 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
692 return r->get_tree() != error_mark_node;
695 // Create a placeholder for an array type.
698 Gcc_backend::placeholder_array_type(const std::string& name,
701 tree ret = make_node(ARRAY_TYPE);
702 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
703 get_identifier_from_string(name),
705 TYPE_NAME(ret) = decl;
706 return this->make_type(ret);
709 // Fill in the fields of a placeholder array type.
712 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
713 Btype* element_btype,
716 tree t = placeholder->get_tree();
717 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
718 Btype* r = this->fill_in_array(placeholder, element_btype, length);
720 // Build the data structure gcc wants to see for a typedef.
721 tree copy = build_distinct_type_copy(t);
722 TYPE_NAME(copy) = NULL_TREE;
723 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
725 return r->get_tree() != error_mark_node;
728 // Return a named version of a type.
731 Gcc_backend::named_type(const std::string& name, Btype* btype,
734 tree type = btype->get_tree();
735 if (type == error_mark_node)
736 return this->error_type();
738 // The middle-end expects a basic type to have a name. In Go every
739 // basic type will have a name. The first time we see a basic type,
740 // give it whatever Go name we have at this point.
741 if (TYPE_NAME(type) == NULL_TREE
742 && location.gcc_location() == BUILTINS_LOCATION
743 && (TREE_CODE(type) == INTEGER_TYPE
744 || TREE_CODE(type) == REAL_TYPE
745 || TREE_CODE(type) == COMPLEX_TYPE
746 || TREE_CODE(type) == BOOLEAN_TYPE))
748 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
749 get_identifier_from_string(name),
751 TYPE_NAME(type) = decl;
752 return this->make_type(type);
755 tree copy = build_variant_type_copy(type);
756 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
757 get_identifier_from_string(name),
759 DECL_ORIGINAL_TYPE(decl) = type;
760 TYPE_NAME(copy) = decl;
761 return this->make_type(copy);
764 // Return a pointer type used as a marker for a circular type.
767 Gcc_backend::circular_pointer_type(Btype*, bool)
769 return this->make_type(ptr_type_node);
772 // Return whether we might be looking at a circular type.
775 Gcc_backend::is_circular_pointer_type(Btype* btype)
777 return btype->get_tree() == ptr_type_node;
780 // Return the size of a type.
783 Gcc_backend::type_size(Btype* btype)
785 tree t = btype->get_tree();
786 if (t == error_mark_node)
788 t = TYPE_SIZE_UNIT(t);
789 gcc_assert(TREE_CODE(t) == INTEGER_CST);
790 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
791 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
792 size_t ret = static_cast<size_t>(val_wide);
793 gcc_assert(ret == val_wide);
797 // Return the alignment of a type.
800 Gcc_backend::type_alignment(Btype* btype)
802 tree t = btype->get_tree();
803 if (t == error_mark_node)
805 return TYPE_ALIGN_UNIT(t);
808 // Return the alignment of a struct field of type BTYPE.
811 Gcc_backend::type_field_alignment(Btype* btype)
813 tree t = btype->get_tree();
814 if (t == error_mark_node)
816 return go_field_alignment(t);
819 // Return the offset of a field in a struct.
822 Gcc_backend::type_field_offset(Btype* btype, size_t index)
824 tree struct_tree = btype->get_tree();
825 if (struct_tree == error_mark_node)
827 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
828 tree field = TYPE_FIELDS(struct_tree);
829 for (; index > 0; --index)
831 field = DECL_CHAIN(field);
832 gcc_assert(field != NULL_TREE);
834 HOST_WIDE_INT offset_wide = int_byte_position(field);
835 gcc_assert(offset_wide >= 0);
836 size_t ret = static_cast<size_t>(offset_wide);
837 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
841 // Return the zero value for a type.
844 Gcc_backend::zero_expression(Btype* btype)
846 tree t = btype->get_tree();
848 if (t == error_mark_node)
849 ret = error_mark_node;
851 ret = build_zero_cst(t);
852 return tree_to_expr(ret);
855 // An expression as a statement.
858 Gcc_backend::expression_statement(Bexpression* expr)
860 return this->make_statement(expr->get_tree());
863 // Variable initialization.
866 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
868 tree var_tree = var->get_tree();
869 tree init_tree = init->get_tree();
870 if (var_tree == error_mark_node || init_tree == error_mark_node)
871 return this->error_statement();
872 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
873 DECL_INITIAL(var_tree) = init_tree;
874 return this->make_statement(build1_loc(DECL_SOURCE_LOCATION(var_tree),
875 DECL_EXPR, void_type_node, var_tree));
881 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
884 tree lhs_tree = lhs->get_tree();
885 tree rhs_tree = rhs->get_tree();
886 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
887 return this->error_statement();
888 return this->make_statement(fold_build2_loc(location.gcc_location(),
891 lhs_tree, rhs_tree));
897 Gcc_backend::return_statement(Bfunction* bfunction,
898 const std::vector<Bexpression*>& vals,
901 tree fntree = bfunction->get_tree();
902 if (fntree == error_mark_node)
903 return this->error_statement();
904 tree result = DECL_RESULT(fntree);
905 if (result == error_mark_node)
906 return this->error_statement();
909 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
911 else if (vals.size() == 1)
913 tree val = vals.front()->get_tree();
914 if (val == error_mark_node)
915 return this->error_statement();
916 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
917 void_type_node, result,
918 vals.front()->get_tree());
919 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
920 void_type_node, set);
924 // To return multiple values, copy the values into a temporary
925 // variable of the right structure type, and then assign the
926 // temporary variable to the DECL_RESULT in the return
928 tree stmt_list = NULL_TREE;
929 tree rettype = TREE_TYPE(result);
930 tree rettmp = create_tmp_var(rettype, "RESULT");
931 tree field = TYPE_FIELDS(rettype);
932 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
934 p++, field = DECL_CHAIN(field))
936 gcc_assert(field != NULL_TREE);
937 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
938 TREE_TYPE(field), rettmp, field,
940 tree val = (*p)->get_tree();
941 if (val == error_mark_node)
942 return this->error_statement();
943 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
945 ref, (*p)->get_tree());
946 append_to_statement_list(set, &stmt_list);
948 gcc_assert(field == NULL_TREE);
949 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
952 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
953 void_type_node, set);
954 append_to_statement_list(ret_expr, &stmt_list);
957 return this->make_statement(ret);
963 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
964 Bblock* else_block, Location location)
966 tree cond_tree = condition->get_tree();
967 tree then_tree = then_block->get_tree();
968 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
969 if (cond_tree == error_mark_node
970 || then_tree == error_mark_node
971 || else_tree == error_mark_node)
972 return this->error_statement();
973 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
974 cond_tree, then_tree, else_tree);
975 return this->make_statement(ret);
981 Gcc_backend::switch_statement(
983 const std::vector<std::vector<Bexpression*> >& cases,
984 const std::vector<Bstatement*>& statements,
985 Location switch_location)
987 gcc_assert(cases.size() == statements.size());
989 tree stmt_list = NULL_TREE;
990 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
991 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
992 ps != statements.end();
997 source_location loc = (*ps != NULL
998 ? EXPR_LOCATION((*ps)->get_tree())
1000 tree label = create_artificial_label(loc);
1001 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
1002 append_to_statement_list(c, &stmt_list);
1006 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1010 tree t = (*pcv)->get_tree();
1011 if (t == error_mark_node)
1012 return this->error_statement();
1013 source_location loc = EXPR_LOCATION(t);
1014 tree label = create_artificial_label(loc);
1015 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1016 append_to_statement_list(c, &stmt_list);
1022 tree t = (*ps)->get_tree();
1023 if (t == error_mark_node)
1024 return this->error_statement();
1025 append_to_statement_list(t, &stmt_list);
1029 tree tv = value->get_tree();
1030 if (tv == error_mark_node)
1031 return this->error_statement();
1032 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1033 void_type_node, tv, stmt_list, NULL_TREE);
1034 return this->make_statement(t);
1037 // Pair of statements.
1040 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1042 tree stmt_list = NULL_TREE;
1043 tree t = s1->get_tree();
1044 if (t == error_mark_node)
1045 return this->error_statement();
1046 append_to_statement_list(t, &stmt_list);
1048 if (t == error_mark_node)
1049 return this->error_statement();
1050 append_to_statement_list(t, &stmt_list);
1051 return this->make_statement(stmt_list);
1054 // List of statements.
1057 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1059 tree stmt_list = NULL_TREE;
1060 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1061 p != statements.end();
1064 tree t = (*p)->get_tree();
1065 if (t == error_mark_node)
1066 return this->error_statement();
1067 append_to_statement_list(t, &stmt_list);
1069 return this->make_statement(stmt_list);
1072 // Make a block. For some reason gcc uses a dual structure for
1073 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1074 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1078 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1079 const std::vector<Bvariable*>& vars,
1080 Location start_location,
1083 tree block_tree = make_node(BLOCK);
1084 if (enclosing == NULL)
1086 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1087 // until we have a proper representation of the init function.
1089 if (function == NULL)
1090 fndecl = current_function_decl;
1092 fndecl = function->get_tree();
1093 gcc_assert(fndecl != NULL_TREE);
1095 // We may have already created a block for local variables when
1096 // we take the address of a parameter.
1097 if (DECL_INITIAL(fndecl) == NULL_TREE)
1099 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1100 DECL_INITIAL(fndecl) = block_tree;
1104 tree superblock_tree = DECL_INITIAL(fndecl);
1105 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1107 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1109 pp = &BLOCK_CHAIN(*pp))
1116 tree superbind_tree = enclosing->get_tree();
1117 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1118 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1120 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1122 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1124 pp = &BLOCK_CHAIN(*pp))
1129 tree* pp = &BLOCK_VARS(block_tree);
1130 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1134 *pp = (*pv)->get_tree();
1135 if (*pp != error_mark_node)
1136 pp = &DECL_CHAIN(*pp);
1140 TREE_USED(block_tree) = 1;
1142 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1143 void_type_node, BLOCK_VARS(block_tree),
1144 NULL_TREE, block_tree);
1145 TREE_SIDE_EFFECTS(bind_tree) = 1;
1147 return new Bblock(bind_tree);
1150 // Add statements to a block.
1153 Gcc_backend::block_add_statements(Bblock* bblock,
1154 const std::vector<Bstatement*>& statements)
1156 tree stmt_list = NULL_TREE;
1157 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1158 p != statements.end();
1161 tree s = (*p)->get_tree();
1162 if (s != error_mark_node)
1163 append_to_statement_list(s, &stmt_list);
1166 tree bind_tree = bblock->get_tree();
1167 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1168 BIND_EXPR_BODY(bind_tree) = stmt_list;
1171 // Return a block as a statement.
1174 Gcc_backend::block_statement(Bblock* bblock)
1176 tree bind_tree = bblock->get_tree();
1177 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1178 return this->make_statement(bind_tree);
1181 // Make a global variable.
1184 Gcc_backend::global_variable(const std::string& package_name,
1185 const std::string& unique_prefix,
1186 const std::string& name,
1192 tree type_tree = btype->get_tree();
1193 if (type_tree == error_mark_node)
1194 return this->error_variable();
1196 std::string var_name(package_name);
1197 var_name.push_back('.');
1198 var_name.append(name);
1199 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1200 get_identifier_from_string(var_name),
1203 DECL_EXTERNAL(decl) = 1;
1205 TREE_STATIC(decl) = 1;
1208 TREE_PUBLIC(decl) = 1;
1210 std::string asm_name(unique_prefix);
1211 asm_name.push_back('.');
1212 asm_name.append(var_name);
1213 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1215 TREE_USED(decl) = 1;
1217 go_preserve_from_gc(decl);
1219 return new Bvariable(decl);
1222 // Set the initial value of a global variable.
1225 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1227 tree expr_tree = expr->get_tree();
1228 if (expr_tree == error_mark_node)
1230 gcc_assert(TREE_CONSTANT(expr_tree));
1231 tree var_decl = var->get_tree();
1232 if (var_decl == error_mark_node)
1234 DECL_INITIAL(var_decl) = expr_tree;
1237 // Make a local variable.
1240 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1241 Btype* btype, bool is_address_taken,
1244 tree type_tree = btype->get_tree();
1245 if (type_tree == error_mark_node)
1246 return this->error_variable();
1247 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1248 get_identifier_from_string(name),
1250 DECL_CONTEXT(decl) = function->get_tree();
1251 TREE_USED(decl) = 1;
1252 if (is_address_taken)
1253 TREE_ADDRESSABLE(decl) = 1;
1254 go_preserve_from_gc(decl);
1255 return new Bvariable(decl);
1258 // Make a function parameter variable.
1261 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1262 Btype* btype, bool is_address_taken,
1265 tree type_tree = btype->get_tree();
1266 if (type_tree == error_mark_node)
1267 return this->error_variable();
1268 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1269 get_identifier_from_string(name),
1271 DECL_CONTEXT(decl) = function->get_tree();
1272 DECL_ARG_TYPE(decl) = type_tree;
1273 TREE_USED(decl) = 1;
1274 if (is_address_taken)
1275 TREE_ADDRESSABLE(decl) = 1;
1276 go_preserve_from_gc(decl);
1277 return new Bvariable(decl);
1280 // Make a temporary variable.
1283 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1284 Btype* btype, Bexpression* binit,
1285 bool is_address_taken,
1287 Bstatement** pstatement)
1289 tree type_tree = btype->get_tree();
1290 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1291 if (type_tree == error_mark_node || init_tree == error_mark_node)
1293 *pstatement = this->error_statement();
1294 return this->error_variable();
1298 // We can only use create_tmp_var if the type is not addressable.
1299 if (!TREE_ADDRESSABLE(type_tree))
1300 var = create_tmp_var(type_tree, "GOTMP");
1303 gcc_assert(bblock != NULL);
1304 var = build_decl(location.gcc_location(), VAR_DECL,
1305 create_tmp_var_name("GOTMP"),
1307 DECL_ARTIFICIAL(var) = 1;
1308 DECL_IGNORED_P(var) = 1;
1310 // FIXME: Permitting function to be NULL here is a temporary
1311 // measure until we have a proper representation of the init
1313 if (function != NULL)
1314 DECL_CONTEXT(var) = function->get_tree();
1317 gcc_assert(current_function_decl != NULL_TREE);
1318 DECL_CONTEXT(var) = current_function_decl;
1321 // We have to add this variable to the BLOCK and the BIND_EXPR.
1322 tree bind_tree = bblock->get_tree();
1323 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1324 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1325 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1326 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1327 BLOCK_VARS(block_tree) = var;
1328 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1331 if (init_tree != NULL_TREE)
1332 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1335 if (is_address_taken)
1336 TREE_ADDRESSABLE(var) = 1;
1338 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1340 void_type_node, var));
1341 return new Bvariable(var);
1344 // Create a named immutable initialized data structure.
1347 Gcc_backend::immutable_struct(const std::string& name, bool, Btype* btype,
1350 tree type_tree = btype->get_tree();
1351 if (type_tree == error_mark_node)
1352 return this->error_variable();
1353 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1354 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1355 get_identifier_from_string(name),
1356 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1357 TREE_STATIC(decl) = 1;
1358 TREE_READONLY(decl) = 1;
1359 TREE_CONSTANT(decl) = 1;
1360 TREE_USED(decl) = 1;
1361 DECL_ARTIFICIAL(decl) = 1;
1363 // We don't call rest_of_decl_compilation until we have the
1366 go_preserve_from_gc(decl);
1367 return new Bvariable(decl);
1370 // Set the initializer for a variable created by immutable_struct.
1371 // This is where we finish compiling the variable.
1374 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1375 bool is_common, Btype*,
1377 Bexpression* initializer)
1379 tree decl = var->get_tree();
1380 tree init_tree = initializer->get_tree();
1381 if (decl == error_mark_node || init_tree == error_mark_node)
1384 DECL_INITIAL(decl) = init_tree;
1386 // We can't call make_decl_one_only until we set DECL_INITIAL.
1388 TREE_PUBLIC(decl) = 1;
1391 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1392 resolve_unique_section(decl, 1, 0);
1395 rest_of_decl_compilation(decl, 1, 0);
1398 // Return a reference to an immutable initialized data structure
1399 // defined in another package.
1402 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1405 tree type_tree = btype->get_tree();
1406 if (type_tree == error_mark_node)
1407 return this->error_variable();
1408 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1409 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1410 get_identifier_from_string(name),
1411 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1412 TREE_READONLY(decl) = 1;
1413 TREE_CONSTANT(decl) = 1;
1414 DECL_ARTIFICIAL(decl) = 1;
1415 TREE_PUBLIC(decl) = 1;
1416 DECL_EXTERNAL(decl) = 1;
1417 go_preserve_from_gc(decl);
1418 return new Bvariable(decl);
1424 Gcc_backend::label(Bfunction* function, const std::string& name,
1429 decl = create_artificial_label(location.gcc_location());
1432 tree id = get_identifier_from_string(name);
1433 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1435 DECL_CONTEXT(decl) = function->get_tree();
1437 return new Blabel(decl);
1440 // Make a statement which defines a label.
1443 Gcc_backend::label_definition_statement(Blabel* label)
1445 tree lab = label->get_tree();
1446 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1447 void_type_node, lab);
1448 return this->make_statement(ret);
1451 // Make a goto statement.
1454 Gcc_backend::goto_statement(Blabel* label, Location location)
1456 tree lab = label->get_tree();
1457 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1459 return this->make_statement(ret);
1462 // Get the address of a label.
1465 Gcc_backend::label_address(Blabel* label, Location location)
1467 tree lab = label->get_tree();
1469 TREE_ADDRESSABLE(lab) = 1;
1470 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1471 build_fold_addr_expr_loc(location.gcc_location(),
1473 return this->make_expression(ret);
1476 // The single backend.
1478 static Gcc_backend gcc_backend;
1480 // Return the backend generator.
1485 return &gcc_backend;
1488 // FIXME: Temporary functions while converting to the new backend
1492 tree_to_type(tree t)
1494 return new Btype(t);
1498 tree_to_expr(tree t)
1500 return new Bexpression(t);
1504 tree_to_stat(tree t)
1506 return new Bstatement(t);
1510 tree_to_function(tree t)
1512 return new Bfunction(t);
1516 tree_to_block(tree t)
1518 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1519 return new Bblock(t);
1523 type_to_tree(Btype* bt)
1525 return bt->get_tree();
1529 expr_to_tree(Bexpression* be)
1531 return be->get_tree();
1535 stat_to_tree(Bstatement* bs)
1537 return bs->get_tree();
1541 block_to_tree(Bblock* bb)
1543 return bb->get_tree();
1547 var_to_tree(Bvariable* bv)
1549 return bv->get_tree();