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
43 // A class wrapping a tree.
60 // In gcc, types, expressions, and statements are all trees.
61 class Btype : public Gcc_tree
69 class Bexpression : public Gcc_tree
77 class Bstatement : public Gcc_tree
85 class Bfunction : public Gcc_tree
93 class Blabel : public Gcc_tree
101 // This file implements the interface between the Go frontend proper
102 // and the gcc IR. This implements specific instantiations of
103 // abstract classes defined by the Go frontend proper. The Go
104 // frontend proper class methods of these classes to generate the
105 // backend representation.
107 class Gcc_backend : public Backend
114 { gcc_unreachable(); }
118 { gcc_unreachable(); }
122 { gcc_unreachable(); }
125 integer_type(bool /* is_unsigned */, int /* bits */)
126 { gcc_unreachable(); }
129 float_type(int /* bits */)
130 { gcc_unreachable(); }
134 { gcc_unreachable(); }
137 function_type(const Function_type*, Btype* /* receiver */,
138 const Btypes* /* parameters */,
139 const Btypes* /* results */)
140 { gcc_unreachable(); }
143 struct_type(const Struct_type*, const Btypes* /* field_types */)
144 { gcc_unreachable(); }
147 array_type(const Btype* /* element_type */, const Bexpression* /* length */)
148 { gcc_unreachable(); }
151 slice_type(const Btype* /* element_type */)
152 { gcc_unreachable(); }
155 map_type(const Btype* /* key_type */, const Btype* /* value_type */,
157 { gcc_unreachable(); }
160 channel_type(const Btype* /* element_type */)
161 { gcc_unreachable(); }
164 interface_type(const Interface_type*, const Btypes* /* method_types */)
165 { gcc_unreachable(); }
170 assignment_statement(Bexpression* lhs, Bexpression* rhs, source_location);
173 return_statement(Bfunction*, const std::vector<Bexpression*>&,
179 label(Bfunction*, const std::string& name, source_location);
182 label_definition_statement(Blabel*);
185 goto_statement(Blabel*, source_location);
188 label_address(Blabel*, source_location);
191 // Make a Bexpression from a tree.
193 make_expression(tree t)
194 { return new Bexpression(t); }
196 // Make a Bstatement from a tree.
198 make_statement(tree t)
199 { return new Bstatement(t); }
202 // A helper function.
205 get_identifier_from_string(const std::string& str)
207 return get_identifier_with_length(str.data(), str.length());
212 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
213 source_location location)
215 tree lhs_tree = lhs->get_tree();
216 tree rhs_tree = rhs->get_tree();
217 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
218 return this->make_statement(error_mark_node);
219 return this->make_statement(fold_build2_loc(location, MODIFY_EXPR,
221 lhs_tree, rhs_tree));
227 Gcc_backend::return_statement(Bfunction* bfunction,
228 const std::vector<Bexpression*>& vals,
229 source_location location)
231 tree fntree = bfunction->get_tree();
232 if (fntree == error_mark_node)
233 return this->make_statement(error_mark_node);
234 tree result = DECL_RESULT(fntree);
235 if (result == error_mark_node)
236 return this->make_statement(error_mark_node);
239 ret = fold_build1_loc(location, RETURN_EXPR, void_type_node, NULL_TREE);
240 else if (vals.size() == 1)
242 tree val = vals.front()->get_tree();
243 if (val == error_mark_node)
244 return this->make_statement(error_mark_node);
245 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
246 result, vals.front()->get_tree());
247 ret = fold_build1_loc(location, RETURN_EXPR, void_type_node, set);
251 // To return multiple values, copy the values into a temporary
252 // variable of the right structure type, and then assign the
253 // temporary variable to the DECL_RESULT in the return
255 tree stmt_list = NULL_TREE;
256 tree rettype = TREE_TYPE(result);
257 tree rettmp = create_tmp_var(rettype, "RESULT");
258 tree field = TYPE_FIELDS(rettype);
259 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
261 p++, field = DECL_CHAIN(field))
263 gcc_assert(field != NULL_TREE);
264 tree ref = fold_build3_loc(location, COMPONENT_REF, TREE_TYPE(field),
265 rettmp, field, NULL_TREE);
266 tree val = (*p)->get_tree();
267 if (val == error_mark_node)
268 return this->make_statement(error_mark_node);
269 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
270 ref, (*p)->get_tree());
271 append_to_statement_list(set, &stmt_list);
273 gcc_assert(field == NULL_TREE);
274 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
276 tree ret_expr = fold_build1_loc(location, RETURN_EXPR, void_type_node,
278 append_to_statement_list(ret_expr, &stmt_list);
281 return this->make_statement(ret);
287 Gcc_backend::label(Bfunction* function, const std::string& name,
288 source_location location)
292 decl = create_artificial_label(location);
295 tree id = get_identifier_from_string(name);
296 decl = build_decl(location, LABEL_DECL, id, void_type_node);
297 DECL_CONTEXT(decl) = function->get_tree();
299 return new Blabel(decl);
302 // Make a statement which defines a label.
305 Gcc_backend::label_definition_statement(Blabel* label)
307 tree lab = label->get_tree();
308 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
309 void_type_node, lab);
310 return this->make_statement(ret);
313 // Make a goto statement.
316 Gcc_backend::goto_statement(Blabel* label, source_location location)
318 tree lab = label->get_tree();
319 tree ret = fold_build1_loc(location, GOTO_EXPR, void_type_node, lab);
320 return this->make_statement(ret);
323 // Get the address of a label.
326 Gcc_backend::label_address(Blabel* label, source_location location)
328 tree lab = label->get_tree();
330 TREE_ADDRESSABLE(lab) = 1;
331 tree ret = fold_convert_loc(location, ptr_type_node,
332 build_fold_addr_expr_loc(location, lab));
333 return this->make_expression(ret);
336 // The single backend.
338 static Gcc_backend gcc_backend;
340 // Return the backend generator.
348 // FIXME: Temporary functions while converting to the new backend
354 return new Bexpression(t);
358 tree_to_function(tree t)
360 return new Bfunction(t);
364 expression_to_tree(Bexpression* be)
366 return be->get_tree();
370 statement_to_tree(Bstatement* bs)
372 return bs->get_tree();