1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011 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*);
201 zero_expression(Btype*);
207 { return this->make_statement(error_mark_node); }
210 expression_statement(Bexpression*);
213 init_statement(Bvariable* var, Bexpression* init);
216 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
219 return_statement(Bfunction*, const std::vector<Bexpression*>&,
223 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
227 switch_statement(Bexpression* value,
228 const std::vector<std::vector<Bexpression*> >& cases,
229 const std::vector<Bstatement*>& statements,
233 compound_statement(Bstatement*, Bstatement*);
236 statement_list(const std::vector<Bstatement*>&);
241 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
245 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
248 block_statement(Bblock*);
254 { return new Bvariable(error_mark_node); }
257 global_variable(const std::string& package_name,
258 const std::string& unique_prefix,
259 const std::string& name,
266 global_variable_set_init(Bvariable*, Bexpression*);
269 local_variable(Bfunction*, const std::string&, Btype*, bool,
273 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
277 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
278 Location, Bstatement**);
281 immutable_struct(const std::string&, bool, Btype*, Location);
284 immutable_struct_set_init(Bvariable*, const std::string&, bool, Btype*,
285 Location, Bexpression*);
288 immutable_struct_reference(const std::string&, Btype*, Location);
293 label(Bfunction*, const std::string& name, Location);
296 label_definition_statement(Blabel*);
299 goto_statement(Blabel*, Location);
302 label_address(Blabel*, Location);
305 // Make a Bexpression from a tree.
307 make_expression(tree t)
308 { return new Bexpression(t); }
310 // Make a Bstatement from a tree.
312 make_statement(tree t)
313 { return new Bstatement(t); }
315 // Make a Btype from a tree.
318 { return new Btype(t); }
321 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
324 fill_in_array(Btype*, Btype*, Bexpression*);
327 // A helper function.
330 get_identifier_from_string(const std::string& str)
332 return get_identifier_with_length(str.data(), str.length());
335 // Get an unnamed integer type.
338 Gcc_backend::integer_type(bool is_unsigned, int bits)
343 if (bits == INT_TYPE_SIZE)
344 type = unsigned_type_node;
345 else if (bits == CHAR_TYPE_SIZE)
346 type = unsigned_char_type_node;
347 else if (bits == SHORT_TYPE_SIZE)
348 type = short_unsigned_type_node;
349 else if (bits == LONG_TYPE_SIZE)
350 type = long_unsigned_type_node;
351 else if (bits == LONG_LONG_TYPE_SIZE)
352 type = long_long_unsigned_type_node;
354 type = make_unsigned_type(bits);
358 if (bits == INT_TYPE_SIZE)
359 type = integer_type_node;
360 else if (bits == CHAR_TYPE_SIZE)
361 type = signed_char_type_node;
362 else if (bits == SHORT_TYPE_SIZE)
363 type = short_integer_type_node;
364 else if (bits == LONG_TYPE_SIZE)
365 type = long_integer_type_node;
366 else if (bits == LONG_LONG_TYPE_SIZE)
367 type = long_long_integer_type_node;
369 type = make_signed_type(bits);
371 return this->make_type(type);
374 // Get an unnamed float type.
377 Gcc_backend::float_type(int bits)
380 if (bits == FLOAT_TYPE_SIZE)
381 type = float_type_node;
382 else if (bits == DOUBLE_TYPE_SIZE)
383 type = double_type_node;
384 else if (bits == LONG_DOUBLE_TYPE_SIZE)
385 type = long_double_type_node;
388 type = make_node(REAL_TYPE);
389 TYPE_PRECISION(type) = bits;
392 return this->make_type(type);
395 // Get an unnamed complex type.
398 Gcc_backend::complex_type(int bits)
401 if (bits == FLOAT_TYPE_SIZE * 2)
402 type = complex_float_type_node;
403 else if (bits == DOUBLE_TYPE_SIZE * 2)
404 type = complex_double_type_node;
405 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
406 type = complex_long_double_type_node;
409 type = make_node(REAL_TYPE);
410 TYPE_PRECISION(type) = bits / 2;
412 type = build_complex_type(type);
414 return this->make_type(type);
417 // Get a pointer type.
420 Gcc_backend::pointer_type(Btype* to_type)
422 tree to_type_tree = to_type->get_tree();
423 if (to_type_tree == error_mark_node)
424 return this->error_type();
425 tree type = build_pointer_type(to_type_tree);
426 return this->make_type(type);
429 // Make a function type.
432 Gcc_backend::function_type(const Btyped_identifier& receiver,
433 const std::vector<Btyped_identifier>& parameters,
434 const std::vector<Btyped_identifier>& results,
437 tree args = NULL_TREE;
439 if (receiver.btype != NULL)
441 tree t = receiver.btype->get_tree();
442 if (t == error_mark_node)
443 return this->error_type();
444 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
445 pp = &TREE_CHAIN(*pp);
448 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
449 p != parameters.end();
452 tree t = p->btype->get_tree();
453 if (t == error_mark_node)
454 return this->error_type();
455 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
456 pp = &TREE_CHAIN(*pp);
459 // Varargs is handled entirely at the Go level. When converted to
460 // GENERIC functions are not varargs.
461 *pp = void_list_node;
465 result = void_type_node;
466 else if (results.size() == 1)
467 result = results.front().btype->get_tree();
470 result = make_node(RECORD_TYPE);
471 tree field_trees = NULL_TREE;
473 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
477 const std::string name = (p->name.empty()
480 tree name_tree = get_identifier_from_string(name);
481 tree field_type_tree = p->btype->get_tree();
482 if (field_type_tree == error_mark_node)
483 return this->error_type();
484 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
485 tree field = build_decl(location.gcc_location(), FIELD_DECL,
486 name_tree, field_type_tree);
487 DECL_CONTEXT(field) = result;
489 pp = &DECL_CHAIN(field);
491 TYPE_FIELDS(result) = field_trees;
494 if (result == error_mark_node)
495 return this->error_type();
497 tree fntype = build_function_type(result, args);
498 if (fntype == error_mark_node)
499 return this->error_type();
501 return this->make_type(build_pointer_type(fntype));
504 // Make a struct type.
507 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
509 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
512 // Fill in the fields of a struct type.
515 Gcc_backend::fill_in_struct(Btype* fill,
516 const std::vector<Btyped_identifier>& fields)
518 tree fill_tree = fill->get_tree();
519 tree field_trees = NULL_TREE;
520 tree* pp = &field_trees;
521 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
525 tree name_tree = get_identifier_from_string(p->name);
526 tree type_tree = p->btype->get_tree();
527 if (type_tree == error_mark_node)
528 return this->error_type();
529 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
531 DECL_CONTEXT(field) = fill_tree;
533 pp = &DECL_CHAIN(field);
535 TYPE_FIELDS(fill_tree) = field_trees;
536 layout_type(fill_tree);
540 // Make an array type.
543 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
545 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
546 element_btype, length);
549 // Fill in an array type.
552 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
555 tree element_type_tree = element_type->get_tree();
556 tree length_tree = length->get_tree();
557 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
558 return this->error_type();
560 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
562 length_tree = fold_convert(sizetype, length_tree);
564 // build_index_type takes the maximum index, which is one less than
566 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
570 tree fill_tree = fill->get_tree();
571 TREE_TYPE(fill_tree) = element_type_tree;
572 TYPE_DOMAIN(fill_tree) = index_type_tree;
573 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
574 layout_type(fill_tree);
576 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
577 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
578 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
579 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
580 TYPE_CANONICAL(fill_tree) =
581 build_array_type(TYPE_CANONICAL(element_type_tree),
582 TYPE_CANONICAL(index_type_tree));
587 // Create a placeholder for a pointer type.
590 Gcc_backend::placeholder_pointer_type(const std::string& name,
591 Location location, bool)
593 tree ret = build_variant_type_copy(ptr_type_node);
596 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
597 get_identifier_from_string(name),
599 TYPE_NAME(ret) = decl;
601 return this->make_type(ret);
604 // Set the real target type for a placeholder pointer type.
607 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
610 tree pt = placeholder->get_tree();
611 if (pt == error_mark_node)
613 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
614 tree tt = to_type->get_tree();
615 if (tt == error_mark_node)
620 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
621 TREE_TYPE(pt) = TREE_TYPE(tt);
622 if (TYPE_NAME(pt) != NULL_TREE)
624 // Build the data structure gcc wants to see for a typedef.
625 tree copy = build_variant_type_copy(pt);
626 TYPE_NAME(copy) = NULL_TREE;
627 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
632 // Set the real values for a placeholder function type.
635 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
637 return this->set_placeholder_pointer_type(placeholder, ft);
640 // Create a placeholder for a struct type.
643 Gcc_backend::placeholder_struct_type(const std::string& name,
646 tree ret = make_node(RECORD_TYPE);
647 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
648 get_identifier_from_string(name),
650 TYPE_NAME(ret) = decl;
651 return this->make_type(ret);
654 // Fill in the fields of a placeholder struct type.
657 Gcc_backend::set_placeholder_struct_type(
659 const std::vector<Btyped_identifier>& fields)
661 tree t = placeholder->get_tree();
662 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
663 Btype* r = this->fill_in_struct(placeholder, fields);
665 // Build the data structure gcc wants to see for a typedef.
666 tree copy = build_variant_type_copy(t);
667 TYPE_NAME(copy) = NULL_TREE;
668 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
670 return r->get_tree() != error_mark_node;
673 // Create a placeholder for an array type.
676 Gcc_backend::placeholder_array_type(const std::string& name,
679 tree ret = make_node(ARRAY_TYPE);
680 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
681 get_identifier_from_string(name),
683 TYPE_NAME(ret) = decl;
684 return this->make_type(ret);
687 // Fill in the fields of a placeholder array type.
690 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
691 Btype* element_btype,
694 tree t = placeholder->get_tree();
695 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
696 Btype* r = this->fill_in_array(placeholder, element_btype, length);
698 // Build the data structure gcc wants to see for a typedef.
699 tree copy = build_variant_type_copy(t);
700 TYPE_NAME(copy) = NULL_TREE;
701 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
703 return r->get_tree() != error_mark_node;
706 // Return a named version of a type.
709 Gcc_backend::named_type(const std::string& name, Btype* btype,
712 tree type = btype->get_tree();
713 if (type == error_mark_node)
714 return this->error_type();
715 tree copy = build_variant_type_copy(type);
716 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
717 get_identifier_from_string(name),
719 DECL_ORIGINAL_TYPE(decl) = type;
720 TYPE_NAME(copy) = decl;
721 return this->make_type(copy);
724 // Return a pointer type used as a marker for a circular type.
727 Gcc_backend::circular_pointer_type(Btype*, bool)
729 return this->make_type(ptr_type_node);
732 // Return whether we might be looking at a circular type.
735 Gcc_backend::is_circular_pointer_type(Btype* btype)
737 return btype->get_tree() == ptr_type_node;
740 // Return the zero value for a type.
743 Gcc_backend::zero_expression(Btype* btype)
745 tree t = btype->get_tree();
747 if (t == error_mark_node)
748 ret = error_mark_node;
750 ret = build_zero_cst(t);
751 return tree_to_expr(ret);
754 // An expression as a statement.
757 Gcc_backend::expression_statement(Bexpression* expr)
759 return this->make_statement(expr->get_tree());
762 // Variable initialization.
765 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
767 tree var_tree = var->get_tree();
768 tree init_tree = init->get_tree();
769 if (var_tree == error_mark_node || init_tree == error_mark_node)
770 return this->error_statement();
771 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
772 DECL_INITIAL(var_tree) = init_tree;
773 return this->make_statement(build1_loc(DECL_SOURCE_LOCATION(var_tree),
774 DECL_EXPR, void_type_node, var_tree));
780 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
783 tree lhs_tree = lhs->get_tree();
784 tree rhs_tree = rhs->get_tree();
785 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
786 return this->error_statement();
787 return this->make_statement(fold_build2_loc(location.gcc_location(),
790 lhs_tree, rhs_tree));
796 Gcc_backend::return_statement(Bfunction* bfunction,
797 const std::vector<Bexpression*>& vals,
800 tree fntree = bfunction->get_tree();
801 if (fntree == error_mark_node)
802 return this->error_statement();
803 tree result = DECL_RESULT(fntree);
804 if (result == error_mark_node)
805 return this->error_statement();
808 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
810 else if (vals.size() == 1)
812 tree val = vals.front()->get_tree();
813 if (val == error_mark_node)
814 return this->error_statement();
815 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
816 void_type_node, result,
817 vals.front()->get_tree());
818 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
819 void_type_node, set);
823 // To return multiple values, copy the values into a temporary
824 // variable of the right structure type, and then assign the
825 // temporary variable to the DECL_RESULT in the return
827 tree stmt_list = NULL_TREE;
828 tree rettype = TREE_TYPE(result);
829 tree rettmp = create_tmp_var(rettype, "RESULT");
830 tree field = TYPE_FIELDS(rettype);
831 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
833 p++, field = DECL_CHAIN(field))
835 gcc_assert(field != NULL_TREE);
836 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
837 TREE_TYPE(field), rettmp, field,
839 tree val = (*p)->get_tree();
840 if (val == error_mark_node)
841 return this->error_statement();
842 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
844 ref, (*p)->get_tree());
845 append_to_statement_list(set, &stmt_list);
847 gcc_assert(field == NULL_TREE);
848 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
851 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
852 void_type_node, set);
853 append_to_statement_list(ret_expr, &stmt_list);
856 return this->make_statement(ret);
862 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
863 Bblock* else_block, Location location)
865 tree cond_tree = condition->get_tree();
866 tree then_tree = then_block->get_tree();
867 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
868 if (cond_tree == error_mark_node
869 || then_tree == error_mark_node
870 || else_tree == error_mark_node)
871 return this->error_statement();
872 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
873 cond_tree, then_tree, else_tree);
874 return this->make_statement(ret);
880 Gcc_backend::switch_statement(
882 const std::vector<std::vector<Bexpression*> >& cases,
883 const std::vector<Bstatement*>& statements,
884 Location switch_location)
886 gcc_assert(cases.size() == statements.size());
888 tree stmt_list = NULL_TREE;
889 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
890 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
891 ps != statements.end();
896 source_location loc = (*ps != NULL
897 ? EXPR_LOCATION((*ps)->get_tree())
899 tree label = create_artificial_label(loc);
900 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
901 append_to_statement_list(c, &stmt_list);
905 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
909 tree t = (*pcv)->get_tree();
910 if (t == error_mark_node)
911 return this->error_statement();
912 source_location loc = EXPR_LOCATION(t);
913 tree label = create_artificial_label(loc);
914 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
915 append_to_statement_list(c, &stmt_list);
921 tree t = (*ps)->get_tree();
922 if (t == error_mark_node)
923 return this->error_statement();
924 append_to_statement_list(t, &stmt_list);
928 tree tv = value->get_tree();
929 if (tv == error_mark_node)
930 return this->error_statement();
931 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
932 void_type_node, tv, stmt_list, NULL_TREE);
933 return this->make_statement(t);
936 // Pair of statements.
939 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
941 tree stmt_list = NULL_TREE;
942 tree t = s1->get_tree();
943 if (t == error_mark_node)
944 return this->error_statement();
945 append_to_statement_list(t, &stmt_list);
947 if (t == error_mark_node)
948 return this->error_statement();
949 append_to_statement_list(t, &stmt_list);
950 return this->make_statement(stmt_list);
953 // List of statements.
956 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
958 tree stmt_list = NULL_TREE;
959 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
960 p != statements.end();
963 tree t = (*p)->get_tree();
964 if (t == error_mark_node)
965 return this->error_statement();
966 append_to_statement_list(t, &stmt_list);
968 return this->make_statement(stmt_list);
971 // Make a block. For some reason gcc uses a dual structure for
972 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
973 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
977 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
978 const std::vector<Bvariable*>& vars,
979 Location start_location,
982 tree block_tree = make_node(BLOCK);
983 if (enclosing == NULL)
985 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
986 // until we have a proper representation of the init function.
988 if (function == NULL)
989 fndecl = current_function_decl;
991 fndecl = function->get_tree();
992 gcc_assert(fndecl != NULL_TREE);
994 // We may have already created a block for local variables when
995 // we take the address of a parameter.
996 if (DECL_INITIAL(fndecl) == NULL_TREE)
998 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
999 DECL_INITIAL(fndecl) = block_tree;
1003 tree superblock_tree = DECL_INITIAL(fndecl);
1004 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1006 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1008 pp = &BLOCK_CHAIN(*pp))
1015 tree superbind_tree = enclosing->get_tree();
1016 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1017 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1019 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1021 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1023 pp = &BLOCK_CHAIN(*pp))
1028 tree* pp = &BLOCK_VARS(block_tree);
1029 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1033 *pp = (*pv)->get_tree();
1034 if (*pp != error_mark_node)
1035 pp = &DECL_CHAIN(*pp);
1039 TREE_USED(block_tree) = 1;
1041 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1042 void_type_node, BLOCK_VARS(block_tree),
1043 NULL_TREE, block_tree);
1044 TREE_SIDE_EFFECTS(bind_tree) = 1;
1046 return new Bblock(bind_tree);
1049 // Add statements to a block.
1052 Gcc_backend::block_add_statements(Bblock* bblock,
1053 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 s = (*p)->get_tree();
1061 if (s != error_mark_node)
1062 append_to_statement_list(s, &stmt_list);
1065 tree bind_tree = bblock->get_tree();
1066 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1067 BIND_EXPR_BODY(bind_tree) = stmt_list;
1070 // Return a block as a statement.
1073 Gcc_backend::block_statement(Bblock* bblock)
1075 tree bind_tree = bblock->get_tree();
1076 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1077 return this->make_statement(bind_tree);
1080 // Make a global variable.
1083 Gcc_backend::global_variable(const std::string& package_name,
1084 const std::string& unique_prefix,
1085 const std::string& name,
1091 tree type_tree = btype->get_tree();
1092 if (type_tree == error_mark_node)
1093 return this->error_variable();
1095 std::string var_name(package_name);
1096 var_name.push_back('.');
1097 var_name.append(name);
1098 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1099 get_identifier_from_string(var_name),
1102 DECL_EXTERNAL(decl) = 1;
1104 TREE_STATIC(decl) = 1;
1107 TREE_PUBLIC(decl) = 1;
1109 std::string asm_name(unique_prefix);
1110 asm_name.push_back('.');
1111 asm_name.append(var_name);
1112 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1114 TREE_USED(decl) = 1;
1116 go_preserve_from_gc(decl);
1118 return new Bvariable(decl);
1121 // Set the initial value of a global variable.
1124 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1126 tree expr_tree = expr->get_tree();
1127 if (expr_tree == error_mark_node)
1129 gcc_assert(TREE_CONSTANT(expr_tree));
1130 tree var_decl = var->get_tree();
1131 if (var_decl == error_mark_node)
1133 DECL_INITIAL(var_decl) = expr_tree;
1136 // Make a local variable.
1139 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1140 Btype* btype, bool is_address_taken,
1143 tree type_tree = btype->get_tree();
1144 if (type_tree == error_mark_node)
1145 return this->error_variable();
1146 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1147 get_identifier_from_string(name),
1149 DECL_CONTEXT(decl) = function->get_tree();
1150 TREE_USED(decl) = 1;
1151 if (is_address_taken)
1152 TREE_ADDRESSABLE(decl) = 1;
1153 go_preserve_from_gc(decl);
1154 return new Bvariable(decl);
1157 // Make a function parameter variable.
1160 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1161 Btype* btype, bool is_address_taken,
1164 tree type_tree = btype->get_tree();
1165 if (type_tree == error_mark_node)
1166 return this->error_variable();
1167 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1168 get_identifier_from_string(name),
1170 DECL_CONTEXT(decl) = function->get_tree();
1171 DECL_ARG_TYPE(decl) = type_tree;
1172 TREE_USED(decl) = 1;
1173 if (is_address_taken)
1174 TREE_ADDRESSABLE(decl) = 1;
1175 go_preserve_from_gc(decl);
1176 return new Bvariable(decl);
1179 // Make a temporary variable.
1182 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1183 Btype* btype, Bexpression* binit,
1184 bool is_address_taken,
1186 Bstatement** pstatement)
1188 tree type_tree = btype->get_tree();
1189 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1190 if (type_tree == error_mark_node || init_tree == error_mark_node)
1192 *pstatement = this->error_statement();
1193 return this->error_variable();
1197 // We can only use create_tmp_var if the type is not addressable.
1198 if (!TREE_ADDRESSABLE(type_tree))
1199 var = create_tmp_var(type_tree, "GOTMP");
1202 gcc_assert(bblock != NULL);
1203 var = build_decl(location.gcc_location(), VAR_DECL,
1204 create_tmp_var_name("GOTMP"),
1206 DECL_ARTIFICIAL(var) = 1;
1207 DECL_IGNORED_P(var) = 1;
1209 // FIXME: Permitting function to be NULL here is a temporary
1210 // measure until we have a proper representation of the init
1212 if (function != NULL)
1213 DECL_CONTEXT(var) = function->get_tree();
1216 gcc_assert(current_function_decl != NULL_TREE);
1217 DECL_CONTEXT(var) = current_function_decl;
1220 // We have to add this variable to the BLOCK and the BIND_EXPR.
1221 tree bind_tree = bblock->get_tree();
1222 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1223 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1224 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1225 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1226 BLOCK_VARS(block_tree) = var;
1227 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1230 if (init_tree != NULL_TREE)
1231 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1234 if (is_address_taken)
1235 TREE_ADDRESSABLE(var) = 1;
1237 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1239 void_type_node, var));
1240 return new Bvariable(var);
1243 // Create a named immutable initialized data structure.
1246 Gcc_backend::immutable_struct(const std::string& name, bool, Btype* btype,
1249 tree type_tree = btype->get_tree();
1250 if (type_tree == error_mark_node)
1251 return this->error_variable();
1252 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1253 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1254 get_identifier_from_string(name),
1255 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1256 TREE_STATIC(decl) = 1;
1257 TREE_READONLY(decl) = 1;
1258 TREE_CONSTANT(decl) = 1;
1259 TREE_USED(decl) = 1;
1260 DECL_ARTIFICIAL(decl) = 1;
1262 // We don't call rest_of_decl_compilation until we have the
1265 go_preserve_from_gc(decl);
1266 return new Bvariable(decl);
1269 // Set the initializer for a variable created by immutable_struct.
1270 // This is where we finish compiling the variable.
1273 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1274 bool is_common, Btype*,
1276 Bexpression* initializer)
1278 tree decl = var->get_tree();
1279 tree init_tree = initializer->get_tree();
1280 if (decl == error_mark_node || init_tree == error_mark_node)
1283 DECL_INITIAL(decl) = init_tree;
1285 // We can't call make_decl_one_only until we set DECL_INITIAL.
1287 TREE_PUBLIC(decl) = 1;
1290 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1291 resolve_unique_section(decl, 1, 0);
1294 rest_of_decl_compilation(decl, 1, 0);
1297 // Return a reference to an immutable initialized data structure
1298 // defined in another package.
1301 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1304 tree type_tree = btype->get_tree();
1305 if (type_tree == error_mark_node)
1306 return this->error_variable();
1307 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1308 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1309 get_identifier_from_string(name),
1310 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1311 TREE_READONLY(decl) = 1;
1312 TREE_CONSTANT(decl) = 1;
1313 DECL_ARTIFICIAL(decl) = 1;
1314 TREE_PUBLIC(decl) = 1;
1315 DECL_EXTERNAL(decl) = 1;
1316 go_preserve_from_gc(decl);
1317 return new Bvariable(decl);
1323 Gcc_backend::label(Bfunction* function, const std::string& name,
1328 decl = create_artificial_label(location.gcc_location());
1331 tree id = get_identifier_from_string(name);
1332 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1334 DECL_CONTEXT(decl) = function->get_tree();
1336 return new Blabel(decl);
1339 // Make a statement which defines a label.
1342 Gcc_backend::label_definition_statement(Blabel* label)
1344 tree lab = label->get_tree();
1345 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1346 void_type_node, lab);
1347 return this->make_statement(ret);
1350 // Make a goto statement.
1353 Gcc_backend::goto_statement(Blabel* label, Location location)
1355 tree lab = label->get_tree();
1356 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1358 return this->make_statement(ret);
1361 // Get the address of a label.
1364 Gcc_backend::label_address(Blabel* label, Location location)
1366 tree lab = label->get_tree();
1368 TREE_ADDRESSABLE(lab) = 1;
1369 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1370 build_fold_addr_expr_loc(location.gcc_location(),
1372 return this->make_expression(ret);
1375 // The single backend.
1377 static Gcc_backend gcc_backend;
1379 // Return the backend generator.
1384 return &gcc_backend;
1387 // FIXME: Temporary functions while converting to the new backend
1391 tree_to_type(tree t)
1393 return new Btype(t);
1397 tree_to_expr(tree t)
1399 return new Bexpression(t);
1403 tree_to_stat(tree t)
1405 return new Bstatement(t);
1409 tree_to_function(tree t)
1411 return new Bfunction(t);
1415 tree_to_block(tree t)
1417 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1418 return new Bblock(t);
1422 type_to_tree(Btype* bt)
1424 return bt->get_tree();
1428 expr_to_tree(Bexpression* be)
1430 return be->get_tree();
1434 stat_to_tree(Bstatement* bs)
1436 return bs->get_tree();
1440 block_to_tree(Bblock* bb)
1442 return bb->get_tree();
1446 var_to_tree(Bvariable* bv)
1448 return bv->get_tree();