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"
36 #ifndef ENABLE_BUILD_WITH_CXX
45 // A class wrapping a tree.
62 // In gcc, types, expressions, and statements are all trees.
63 class Btype : public Gcc_tree
71 class Bexpression : public Gcc_tree
79 class Bstatement : public Gcc_tree
87 class Bfunction : public Gcc_tree
95 class Bblock : public Gcc_tree
103 class Bvariable : public Gcc_tree
111 class Blabel : public Gcc_tree
119 // This file implements the interface between the Go frontend proper
120 // and the gcc IR. This implements specific instantiations of
121 // abstract classes defined by the Go frontend proper. The Go
122 // frontend proper class methods of these classes to generate the
123 // backend representation.
125 class Gcc_backend : public Backend
132 { return this->make_type(error_mark_node); }
136 { return this->make_type(void_type_node); }
140 { return this->make_type(boolean_type_node); }
143 integer_type(bool, int);
152 pointer_type(Btype*);
155 function_type(const Btyped_identifier&,
156 const std::vector<Btyped_identifier>&,
157 const std::vector<Btyped_identifier>&,
161 struct_type(const std::vector<Btyped_identifier>&);
164 array_type(Btype*, Bexpression*);
167 placeholder_pointer_type(const std::string&, source_location, bool);
170 set_placeholder_pointer_type(Btype*, Btype*);
173 set_placeholder_function_type(Btype*, Btype*);
176 placeholder_struct_type(const std::string&, source_location);
179 set_placeholder_struct_type(Btype* placeholder,
180 const std::vector<Btyped_identifier>&);
183 placeholder_array_type(const std::string&, source_location);
186 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
189 named_type(const std::string&, Btype*, source_location);
192 circular_pointer_type(Btype*, bool);
195 is_circular_pointer_type(Btype*);
201 { return this->make_statement(error_mark_node); }
204 expression_statement(Bexpression*);
207 init_statement(Bvariable* var, Bexpression* init);
210 assignment_statement(Bexpression* lhs, Bexpression* rhs, source_location);
213 return_statement(Bfunction*, const std::vector<Bexpression*>&,
217 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
221 switch_statement(Bexpression* value,
222 const std::vector<std::vector<Bexpression*> >& cases,
223 const std::vector<Bstatement*>& statements,
227 compound_statement(Bstatement*, Bstatement*);
230 statement_list(const std::vector<Bstatement*>&);
235 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
236 source_location, source_location);
239 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
242 block_statement(Bblock*);
248 { return new Bvariable(error_mark_node); }
251 global_variable(const std::string& package_name,
252 const std::string& unique_prefix,
253 const std::string& name,
257 source_location location);
260 global_variable_set_init(Bvariable*, Bexpression*);
263 local_variable(Bfunction*, const std::string&, Btype*, bool,
267 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
271 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
272 source_location, Bstatement**);
277 label(Bfunction*, const std::string& name, source_location);
280 label_definition_statement(Blabel*);
283 goto_statement(Blabel*, source_location);
286 label_address(Blabel*, source_location);
289 // Make a Bexpression from a tree.
291 make_expression(tree t)
292 { return new Bexpression(t); }
294 // Make a Bstatement from a tree.
296 make_statement(tree t)
297 { return new Bstatement(t); }
299 // Make a Btype from a tree.
302 { return new Btype(t); }
305 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
308 fill_in_array(Btype*, Btype*, Bexpression*);
311 // A helper function.
314 get_identifier_from_string(const std::string& str)
316 return get_identifier_with_length(str.data(), str.length());
319 // Get an unnamed integer type.
322 Gcc_backend::integer_type(bool is_unsigned, int bits)
327 if (bits == INT_TYPE_SIZE)
328 type = unsigned_type_node;
329 else if (bits == CHAR_TYPE_SIZE)
330 type = unsigned_char_type_node;
331 else if (bits == SHORT_TYPE_SIZE)
332 type = short_unsigned_type_node;
333 else if (bits == LONG_TYPE_SIZE)
334 type = long_unsigned_type_node;
335 else if (bits == LONG_LONG_TYPE_SIZE)
336 type = long_long_unsigned_type_node;
338 type = make_unsigned_type(bits);
342 if (bits == INT_TYPE_SIZE)
343 type = integer_type_node;
344 else if (bits == CHAR_TYPE_SIZE)
345 type = signed_char_type_node;
346 else if (bits == SHORT_TYPE_SIZE)
347 type = short_integer_type_node;
348 else if (bits == LONG_TYPE_SIZE)
349 type = long_integer_type_node;
350 else if (bits == LONG_LONG_TYPE_SIZE)
351 type = long_long_integer_type_node;
353 type = make_signed_type(bits);
355 return this->make_type(type);
358 // Get an unnamed float type.
361 Gcc_backend::float_type(int bits)
364 if (bits == FLOAT_TYPE_SIZE)
365 type = float_type_node;
366 else if (bits == DOUBLE_TYPE_SIZE)
367 type = double_type_node;
368 else if (bits == LONG_DOUBLE_TYPE_SIZE)
369 type = long_double_type_node;
372 type = make_node(REAL_TYPE);
373 TYPE_PRECISION(type) = bits;
376 return this->make_type(type);
379 // Get an unnamed complex type.
382 Gcc_backend::complex_type(int bits)
385 if (bits == FLOAT_TYPE_SIZE * 2)
386 type = complex_float_type_node;
387 else if (bits == DOUBLE_TYPE_SIZE * 2)
388 type = complex_double_type_node;
389 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
390 type = complex_long_double_type_node;
393 type = make_node(REAL_TYPE);
394 TYPE_PRECISION(type) = bits / 2;
396 type = build_complex_type(type);
398 return this->make_type(type);
401 // Get a pointer type.
404 Gcc_backend::pointer_type(Btype* to_type)
406 tree to_type_tree = to_type->get_tree();
407 if (to_type_tree == error_mark_node)
408 return this->error_type();
409 tree type = build_pointer_type(to_type_tree);
410 return this->make_type(type);
413 // Make a function type.
416 Gcc_backend::function_type(const Btyped_identifier& receiver,
417 const std::vector<Btyped_identifier>& parameters,
418 const std::vector<Btyped_identifier>& results,
419 source_location location)
421 tree args = NULL_TREE;
423 if (receiver.btype != NULL)
425 tree t = receiver.btype->get_tree();
426 if (t == error_mark_node)
427 return this->error_type();
428 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
429 pp = &TREE_CHAIN(*pp);
432 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
433 p != parameters.end();
436 tree t = p->btype->get_tree();
437 if (t == error_mark_node)
438 return this->error_type();
439 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
440 pp = &TREE_CHAIN(*pp);
443 // Varargs is handled entirely at the Go level. When converted to
444 // GENERIC functions are not varargs.
445 *pp = void_list_node;
449 result = void_type_node;
450 else if (results.size() == 1)
451 result = results.front().btype->get_tree();
454 result = make_node(RECORD_TYPE);
455 tree field_trees = NULL_TREE;
457 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
461 const std::string name = (p->name.empty()
464 tree name_tree = get_identifier_from_string(name);
465 tree field_type_tree = p->btype->get_tree();
466 if (field_type_tree == error_mark_node)
467 return this->error_type();
468 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
469 tree field = build_decl(location, FIELD_DECL, name_tree,
471 DECL_CONTEXT(field) = result;
473 pp = &DECL_CHAIN(field);
475 TYPE_FIELDS(result) = field_trees;
478 if (result == error_mark_node)
479 return this->error_type();
481 tree fntype = build_function_type(result, args);
482 if (fntype == error_mark_node)
483 return this->error_type();
485 return this->make_type(build_pointer_type(fntype));
488 // Make a struct type.
491 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
493 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
496 // Fill in the fields of a struct type.
499 Gcc_backend::fill_in_struct(Btype* fill,
500 const std::vector<Btyped_identifier>& fields)
502 tree fill_tree = fill->get_tree();
503 tree field_trees = NULL_TREE;
504 tree* pp = &field_trees;
505 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
509 tree name_tree = get_identifier_from_string(p->name);
510 tree type_tree = p->btype->get_tree();
511 if (type_tree == error_mark_node)
512 return this->error_type();
513 tree field = build_decl(p->location, FIELD_DECL, name_tree, type_tree);
514 DECL_CONTEXT(field) = fill_tree;
516 pp = &DECL_CHAIN(field);
518 TYPE_FIELDS(fill_tree) = field_trees;
519 layout_type(fill_tree);
523 // Make an array type.
526 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
528 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
529 element_btype, length);
532 // Fill in an array type.
535 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
538 tree element_type_tree = element_type->get_tree();
539 tree length_tree = length->get_tree();
540 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
541 return this->error_type();
543 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
545 length_tree = fold_convert(sizetype, length_tree);
547 // build_index_type takes the maximum index, which is one less than
549 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
553 tree fill_tree = fill->get_tree();
554 TREE_TYPE(fill_tree) = element_type_tree;
555 TYPE_DOMAIN(fill_tree) = index_type_tree;
556 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
557 layout_type(fill_tree);
559 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
560 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
561 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
562 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
563 TYPE_CANONICAL(fill_tree) =
564 build_array_type(TYPE_CANONICAL(element_type_tree),
565 TYPE_CANONICAL(index_type_tree));
570 // Create a placeholder for a pointer type.
573 Gcc_backend::placeholder_pointer_type(const std::string& name,
574 source_location location, bool)
576 tree ret = build_variant_type_copy(ptr_type_node);
579 tree decl = build_decl(location, TYPE_DECL,
580 get_identifier_from_string(name),
582 TYPE_NAME(ret) = decl;
584 return this->make_type(ret);
587 // Set the real target type for a placeholder pointer type.
590 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
593 tree pt = placeholder->get_tree();
594 if (pt == error_mark_node)
596 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
597 tree tt = to_type->get_tree();
598 if (tt == error_mark_node)
603 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
604 TREE_TYPE(pt) = TREE_TYPE(tt);
608 // Set the real values for a placeholder function type.
611 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
613 return this->set_placeholder_pointer_type(placeholder, ft);
616 // Create a placeholder for a struct type.
619 Gcc_backend::placeholder_struct_type(const std::string& name,
620 source_location location)
622 tree ret = make_node(RECORD_TYPE);
623 tree decl = build_decl(location, TYPE_DECL,
624 get_identifier_from_string(name),
626 TYPE_NAME(ret) = decl;
627 return this->make_type(ret);
630 // Fill in the fields of a placeholder struct type.
633 Gcc_backend::set_placeholder_struct_type(
635 const std::vector<Btyped_identifier>& fields)
637 tree t = placeholder->get_tree();
638 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
639 Btype* r = this->fill_in_struct(placeholder, fields);
640 return r->get_tree() != error_mark_node;
643 // Create a placeholder for an array type.
646 Gcc_backend::placeholder_array_type(const std::string& name,
647 source_location location)
649 tree ret = make_node(ARRAY_TYPE);
650 tree decl = build_decl(location, TYPE_DECL,
651 get_identifier_from_string(name),
653 TYPE_NAME(ret) = decl;
654 return this->make_type(ret);
657 // Fill in the fields of a placeholder array type.
660 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
661 Btype* element_btype,
664 tree t = placeholder->get_tree();
665 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
666 Btype* r = this->fill_in_array(placeholder, element_btype, length);
667 return r->get_tree() != error_mark_node;
670 // Return a named version of a type.
673 Gcc_backend::named_type(const std::string& name, Btype* btype,
674 source_location location)
676 tree type = btype->get_tree();
677 if (type == error_mark_node)
678 return this->error_type();
679 type = build_variant_type_copy(type);
680 tree decl = build_decl(location, TYPE_DECL,
681 get_identifier_from_string(name),
683 TYPE_NAME(type) = decl;
684 return this->make_type(type);
687 // Return a pointer type used as a marker for a circular type.
690 Gcc_backend::circular_pointer_type(Btype*, bool)
692 return this->make_type(ptr_type_node);
695 // Return whether we might be looking at a circular type.
698 Gcc_backend::is_circular_pointer_type(Btype* btype)
700 return btype->get_tree() == ptr_type_node;
703 // An expression as a statement.
706 Gcc_backend::expression_statement(Bexpression* expr)
708 return this->make_statement(expr->get_tree());
711 // Variable initialization.
714 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
716 tree var_tree = var->get_tree();
717 tree init_tree = init->get_tree();
718 if (var_tree == error_mark_node || init_tree == error_mark_node)
719 return this->error_statement();
720 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
721 DECL_INITIAL(var_tree) = init_tree;
722 return this->make_statement(build1_loc(DECL_SOURCE_LOCATION(var_tree),
723 DECL_EXPR, void_type_node, var_tree));
729 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
730 source_location location)
732 tree lhs_tree = lhs->get_tree();
733 tree rhs_tree = rhs->get_tree();
734 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
735 return this->error_statement();
736 return this->make_statement(fold_build2_loc(location, MODIFY_EXPR,
738 lhs_tree, rhs_tree));
744 Gcc_backend::return_statement(Bfunction* bfunction,
745 const std::vector<Bexpression*>& vals,
746 source_location location)
748 tree fntree = bfunction->get_tree();
749 if (fntree == error_mark_node)
750 return this->error_statement();
751 tree result = DECL_RESULT(fntree);
752 if (result == error_mark_node)
753 return this->error_statement();
756 ret = fold_build1_loc(location, RETURN_EXPR, void_type_node, NULL_TREE);
757 else if (vals.size() == 1)
759 tree val = vals.front()->get_tree();
760 if (val == error_mark_node)
761 return this->error_statement();
762 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
763 result, vals.front()->get_tree());
764 ret = fold_build1_loc(location, RETURN_EXPR, void_type_node, set);
768 // To return multiple values, copy the values into a temporary
769 // variable of the right structure type, and then assign the
770 // temporary variable to the DECL_RESULT in the return
772 tree stmt_list = NULL_TREE;
773 tree rettype = TREE_TYPE(result);
774 tree rettmp = create_tmp_var(rettype, "RESULT");
775 tree field = TYPE_FIELDS(rettype);
776 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
778 p++, field = DECL_CHAIN(field))
780 gcc_assert(field != NULL_TREE);
781 tree ref = fold_build3_loc(location, COMPONENT_REF, TREE_TYPE(field),
782 rettmp, field, NULL_TREE);
783 tree val = (*p)->get_tree();
784 if (val == error_mark_node)
785 return this->error_statement();
786 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
787 ref, (*p)->get_tree());
788 append_to_statement_list(set, &stmt_list);
790 gcc_assert(field == NULL_TREE);
791 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
793 tree ret_expr = fold_build1_loc(location, RETURN_EXPR, void_type_node,
795 append_to_statement_list(ret_expr, &stmt_list);
798 return this->make_statement(ret);
804 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
805 Bblock* else_block, source_location location)
807 tree cond_tree = condition->get_tree();
808 tree then_tree = then_block->get_tree();
809 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
810 if (cond_tree == error_mark_node
811 || then_tree == error_mark_node
812 || else_tree == error_mark_node)
813 return this->error_statement();
814 tree ret = build3_loc(location, COND_EXPR, void_type_node, cond_tree,
815 then_tree, else_tree);
816 return this->make_statement(ret);
822 Gcc_backend::switch_statement(
824 const std::vector<std::vector<Bexpression*> >& cases,
825 const std::vector<Bstatement*>& statements,
826 source_location switch_location)
828 gcc_assert(cases.size() == statements.size());
830 tree stmt_list = NULL_TREE;
831 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
832 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
833 ps != statements.end();
838 source_location loc = (*ps != NULL
839 ? EXPR_LOCATION((*ps)->get_tree())
841 tree label = create_artificial_label(loc);
842 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
843 append_to_statement_list(c, &stmt_list);
847 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
851 tree t = (*pcv)->get_tree();
852 if (t == error_mark_node)
853 return this->error_statement();
854 source_location loc = EXPR_LOCATION(t);
855 tree label = create_artificial_label(loc);
856 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
857 append_to_statement_list(c, &stmt_list);
863 tree t = (*ps)->get_tree();
864 if (t == error_mark_node)
865 return this->error_statement();
866 append_to_statement_list(t, &stmt_list);
870 tree tv = value->get_tree();
871 if (tv == error_mark_node)
872 return this->error_statement();
873 tree t = build3_loc(switch_location, SWITCH_EXPR, void_type_node,
874 tv, stmt_list, NULL_TREE);
875 return this->make_statement(t);
878 // Pair of statements.
881 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
883 tree stmt_list = NULL_TREE;
884 tree t = s1->get_tree();
885 if (t == error_mark_node)
886 return this->error_statement();
887 append_to_statement_list(t, &stmt_list);
889 if (t == error_mark_node)
890 return this->error_statement();
891 append_to_statement_list(t, &stmt_list);
892 return this->make_statement(stmt_list);
895 // List of statements.
898 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
900 tree stmt_list = NULL_TREE;
901 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
902 p != statements.end();
905 tree t = (*p)->get_tree();
906 if (t == error_mark_node)
907 return this->error_statement();
908 append_to_statement_list(t, &stmt_list);
910 return this->make_statement(stmt_list);
913 // Make a block. For some reason gcc uses a dual structure for
914 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
915 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
919 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
920 const std::vector<Bvariable*>& vars,
921 source_location start_location,
924 tree block_tree = make_node(BLOCK);
925 if (enclosing == NULL)
927 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
928 // until we have a proper representation of the init function.
930 if (function == NULL)
931 fndecl = current_function_decl;
933 fndecl = function->get_tree();
934 gcc_assert(fndecl != NULL_TREE);
936 // We may have already created a block for local variables when
937 // we take the address of a parameter.
938 if (DECL_INITIAL(fndecl) == NULL_TREE)
940 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
941 DECL_INITIAL(fndecl) = block_tree;
945 tree superblock_tree = DECL_INITIAL(fndecl);
946 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
948 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
950 pp = &BLOCK_CHAIN(*pp))
957 tree superbind_tree = enclosing->get_tree();
958 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
959 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
961 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
963 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
965 pp = &BLOCK_CHAIN(*pp))
970 tree* pp = &BLOCK_VARS(block_tree);
971 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
975 *pp = (*pv)->get_tree();
976 if (*pp != error_mark_node)
977 pp = &DECL_CHAIN(*pp);
981 TREE_USED(block_tree) = 1;
983 tree bind_tree = build3_loc(start_location, BIND_EXPR, void_type_node,
984 BLOCK_VARS(block_tree), NULL_TREE, block_tree);
985 TREE_SIDE_EFFECTS(bind_tree) = 1;
987 return new Bblock(bind_tree);
990 // Add statements to a block.
993 Gcc_backend::block_add_statements(Bblock* bblock,
994 const std::vector<Bstatement*>& statements)
996 tree stmt_list = NULL_TREE;
997 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
998 p != statements.end();
1001 tree s = (*p)->get_tree();
1002 if (s != error_mark_node)
1003 append_to_statement_list(s, &stmt_list);
1006 tree bind_tree = bblock->get_tree();
1007 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1008 BIND_EXPR_BODY(bind_tree) = stmt_list;
1011 // Return a block as a statement.
1014 Gcc_backend::block_statement(Bblock* bblock)
1016 tree bind_tree = bblock->get_tree();
1017 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1018 return this->make_statement(bind_tree);
1021 // Make a global variable.
1024 Gcc_backend::global_variable(const std::string& package_name,
1025 const std::string& unique_prefix,
1026 const std::string& name,
1030 source_location location)
1032 tree type_tree = btype->get_tree();
1033 if (type_tree == error_mark_node)
1034 return this->error_variable();
1036 std::string var_name(package_name);
1037 var_name.push_back('.');
1038 var_name.append(name);
1039 tree decl = build_decl(location, VAR_DECL,
1040 get_identifier_from_string(var_name),
1043 DECL_EXTERNAL(decl) = 1;
1045 TREE_STATIC(decl) = 1;
1048 TREE_PUBLIC(decl) = 1;
1050 std::string asm_name(unique_prefix);
1051 asm_name.push_back('.');
1052 asm_name.append(var_name);
1053 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1055 TREE_USED(decl) = 1;
1057 go_preserve_from_gc(decl);
1059 return new Bvariable(decl);
1062 // Set the initial value of a global variable.
1065 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1067 tree expr_tree = expr->get_tree();
1068 if (expr_tree == error_mark_node)
1070 gcc_assert(TREE_CONSTANT(expr_tree));
1071 tree var_decl = var->get_tree();
1072 if (var_decl == error_mark_node)
1074 DECL_INITIAL(var_decl) = expr_tree;
1077 // Make a local variable.
1080 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1081 Btype* btype, bool is_address_taken,
1082 source_location location)
1084 tree type_tree = btype->get_tree();
1085 if (type_tree == error_mark_node)
1086 return this->error_variable();
1087 tree decl = build_decl(location, VAR_DECL,
1088 get_identifier_from_string(name),
1090 DECL_CONTEXT(decl) = function->get_tree();
1091 TREE_USED(decl) = 1;
1092 if (is_address_taken)
1093 TREE_ADDRESSABLE(decl) = 1;
1094 go_preserve_from_gc(decl);
1095 return new Bvariable(decl);
1098 // Make a function parameter variable.
1101 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1102 Btype* btype, bool is_address_taken,
1103 source_location location)
1105 tree type_tree = btype->get_tree();
1106 if (type_tree == error_mark_node)
1107 return this->error_variable();
1108 tree decl = build_decl(location, PARM_DECL,
1109 get_identifier_from_string(name),
1111 DECL_CONTEXT(decl) = function->get_tree();
1112 DECL_ARG_TYPE(decl) = type_tree;
1113 TREE_USED(decl) = 1;
1114 if (is_address_taken)
1115 TREE_ADDRESSABLE(decl) = 1;
1116 go_preserve_from_gc(decl);
1117 return new Bvariable(decl);
1120 // Make a temporary variable.
1123 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1124 Btype* btype, Bexpression* binit,
1125 bool is_address_taken,
1126 source_location location,
1127 Bstatement** pstatement)
1129 tree type_tree = btype->get_tree();
1130 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1131 if (type_tree == error_mark_node || init_tree == error_mark_node)
1133 *pstatement = this->error_statement();
1134 return this->error_variable();
1138 // We can only use create_tmp_var if the type is not addressable.
1139 if (!TREE_ADDRESSABLE(type_tree))
1140 var = create_tmp_var(type_tree, "GOTMP");
1143 gcc_assert(bblock != NULL);
1144 var = build_decl(location, VAR_DECL,
1145 create_tmp_var_name("GOTMP"),
1147 DECL_ARTIFICIAL(var) = 1;
1148 DECL_IGNORED_P(var) = 1;
1150 // FIXME: Permitting function to be NULL here is a temporary
1151 // measure until we have a proper representation of the init
1153 if (function != NULL)
1154 DECL_CONTEXT(var) = function->get_tree();
1157 gcc_assert(current_function_decl != NULL_TREE);
1158 DECL_CONTEXT(var) = current_function_decl;
1161 // We have to add this variable to the BLOCK and the BIND_EXPR.
1162 tree bind_tree = bblock->get_tree();
1163 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1164 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1165 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1166 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1167 BLOCK_VARS(block_tree) = var;
1168 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1171 if (init_tree != NULL_TREE)
1172 DECL_INITIAL(var) = fold_convert_loc(location, type_tree, init_tree);
1174 if (is_address_taken)
1175 TREE_ADDRESSABLE(var) = 1;
1177 *pstatement = this->make_statement(build1_loc(location, DECL_EXPR,
1178 void_type_node, var));
1179 return new Bvariable(var);
1185 Gcc_backend::label(Bfunction* function, const std::string& name,
1186 source_location location)
1190 decl = create_artificial_label(location);
1193 tree id = get_identifier_from_string(name);
1194 decl = build_decl(location, LABEL_DECL, id, void_type_node);
1195 DECL_CONTEXT(decl) = function->get_tree();
1197 return new Blabel(decl);
1200 // Make a statement which defines a label.
1203 Gcc_backend::label_definition_statement(Blabel* label)
1205 tree lab = label->get_tree();
1206 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1207 void_type_node, lab);
1208 return this->make_statement(ret);
1211 // Make a goto statement.
1214 Gcc_backend::goto_statement(Blabel* label, source_location location)
1216 tree lab = label->get_tree();
1217 tree ret = fold_build1_loc(location, GOTO_EXPR, void_type_node, lab);
1218 return this->make_statement(ret);
1221 // Get the address of a label.
1224 Gcc_backend::label_address(Blabel* label, source_location location)
1226 tree lab = label->get_tree();
1228 TREE_ADDRESSABLE(lab) = 1;
1229 tree ret = fold_convert_loc(location, ptr_type_node,
1230 build_fold_addr_expr_loc(location, lab));
1231 return this->make_expression(ret);
1234 // The single backend.
1236 static Gcc_backend gcc_backend;
1238 // Return the backend generator.
1243 return &gcc_backend;
1246 // FIXME: Temporary functions while converting to the new backend
1250 tree_to_type(tree t)
1252 return new Btype(t);
1256 tree_to_expr(tree t)
1258 return new Bexpression(t);
1262 tree_to_stat(tree t)
1264 return new Bstatement(t);
1268 tree_to_function(tree t)
1270 return new Bfunction(t);
1274 tree_to_block(tree t)
1276 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1277 return new Bblock(t);
1281 type_to_tree(Btype* bt)
1283 return bt->get_tree();
1287 expr_to_tree(Bexpression* be)
1289 return be->get_tree();
1293 stat_to_tree(Bstatement* bs)
1295 return bs->get_tree();
1299 block_to_tree(Bblock* bb)
1301 return bb->get_tree();
1305 var_to_tree(Bvariable* bv)
1307 return bv->get_tree();