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 expression_statement(Bexpression*);
173 assignment_statement(Bexpression* lhs, Bexpression* rhs, source_location);
176 return_statement(Bfunction*, const std::vector<Bexpression*>&,
180 if_statement(Bexpression* condition, Bstatement* then_block,
181 Bstatement* else_block, source_location);
184 switch_statement(Bexpression* value,
185 const std::vector<std::vector<Bexpression*> >& cases,
186 const std::vector<Bstatement*>& statements,
190 statement_list(const std::vector<Bstatement*>&);
195 label(Bfunction*, const std::string& name, source_location);
198 label_definition_statement(Blabel*);
201 goto_statement(Blabel*, source_location);
204 label_address(Blabel*, source_location);
207 // Make a Bexpression from a tree.
209 make_expression(tree t)
210 { return new Bexpression(t); }
212 // Make a Bstatement from a tree.
214 make_statement(tree t)
215 { return new Bstatement(t); }
218 // A helper function.
221 get_identifier_from_string(const std::string& str)
223 return get_identifier_with_length(str.data(), str.length());
226 // An expression as a statement.
229 Gcc_backend::expression_statement(Bexpression* expr)
231 return this->make_statement(expr->get_tree());
237 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
238 source_location location)
240 tree lhs_tree = lhs->get_tree();
241 tree rhs_tree = rhs->get_tree();
242 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
243 return this->make_statement(error_mark_node);
244 return this->make_statement(fold_build2_loc(location, MODIFY_EXPR,
246 lhs_tree, rhs_tree));
252 Gcc_backend::return_statement(Bfunction* bfunction,
253 const std::vector<Bexpression*>& vals,
254 source_location location)
256 tree fntree = bfunction->get_tree();
257 if (fntree == error_mark_node)
258 return this->make_statement(error_mark_node);
259 tree result = DECL_RESULT(fntree);
260 if (result == error_mark_node)
261 return this->make_statement(error_mark_node);
264 ret = fold_build1_loc(location, RETURN_EXPR, void_type_node, NULL_TREE);
265 else if (vals.size() == 1)
267 tree val = vals.front()->get_tree();
268 if (val == error_mark_node)
269 return this->make_statement(error_mark_node);
270 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
271 result, vals.front()->get_tree());
272 ret = fold_build1_loc(location, RETURN_EXPR, void_type_node, set);
276 // To return multiple values, copy the values into a temporary
277 // variable of the right structure type, and then assign the
278 // temporary variable to the DECL_RESULT in the return
280 tree stmt_list = NULL_TREE;
281 tree rettype = TREE_TYPE(result);
282 tree rettmp = create_tmp_var(rettype, "RESULT");
283 tree field = TYPE_FIELDS(rettype);
284 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
286 p++, field = DECL_CHAIN(field))
288 gcc_assert(field != NULL_TREE);
289 tree ref = fold_build3_loc(location, COMPONENT_REF, TREE_TYPE(field),
290 rettmp, field, NULL_TREE);
291 tree val = (*p)->get_tree();
292 if (val == error_mark_node)
293 return this->make_statement(error_mark_node);
294 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
295 ref, (*p)->get_tree());
296 append_to_statement_list(set, &stmt_list);
298 gcc_assert(field == NULL_TREE);
299 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
301 tree ret_expr = fold_build1_loc(location, RETURN_EXPR, void_type_node,
303 append_to_statement_list(ret_expr, &stmt_list);
306 return this->make_statement(ret);
312 Gcc_backend::if_statement(Bexpression* condition, Bstatement* then_block,
313 Bstatement* else_block, source_location location)
315 tree cond_tree = condition->get_tree();
316 tree then_tree = then_block->get_tree();
317 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
318 if (cond_tree == error_mark_node
319 || then_tree == error_mark_node
320 || else_tree == error_mark_node)
321 return this->make_statement(error_mark_node);
322 tree ret = build3_loc(location, COND_EXPR, void_type_node, cond_tree,
323 then_tree, else_tree);
324 return this->make_statement(ret);
330 Gcc_backend::switch_statement(
332 const std::vector<std::vector<Bexpression*> >& cases,
333 const std::vector<Bstatement*>& statements,
334 source_location switch_location)
336 gcc_assert(cases.size() == statements.size());
338 tree stmt_list = NULL_TREE;
339 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
340 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
341 ps != statements.end();
346 source_location loc = (*ps != NULL
347 ? EXPR_LOCATION((*ps)->get_tree())
349 tree label = create_artificial_label(loc);
350 tree c = build3_loc(loc, CASE_LABEL_EXPR, void_type_node, NULL_TREE,
352 append_to_statement_list(c, &stmt_list);
356 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
360 tree t = (*pcv)->get_tree();
361 if (t == error_mark_node)
362 return this->make_statement(error_mark_node);
363 source_location loc = EXPR_LOCATION(t);
364 tree label = create_artificial_label(loc);
365 tree c = build3_loc(loc, CASE_LABEL_EXPR, void_type_node,
366 (*pcv)->get_tree(), NULL_TREE, label);
367 append_to_statement_list(c, &stmt_list);
373 tree t = (*ps)->get_tree();
374 if (t == error_mark_node)
375 return this->make_statement(error_mark_node);
376 append_to_statement_list(t, &stmt_list);
380 tree tv = value->get_tree();
381 if (tv == error_mark_node)
382 return this->make_statement(error_mark_node);
383 tree t = build3_loc(switch_location, SWITCH_EXPR, void_type_node,
384 tv, stmt_list, NULL_TREE);
385 return this->make_statement(t);
388 // List of statements.
391 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
393 tree stmt_list = NULL_TREE;
394 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
395 p != statements.end();
398 tree t = (*p)->get_tree();
399 if (t == error_mark_node)
400 return this->make_statement(error_mark_node);
401 append_to_statement_list(t, &stmt_list);
403 return this->make_statement(stmt_list);
409 Gcc_backend::label(Bfunction* function, const std::string& name,
410 source_location location)
414 decl = create_artificial_label(location);
417 tree id = get_identifier_from_string(name);
418 decl = build_decl(location, LABEL_DECL, id, void_type_node);
419 DECL_CONTEXT(decl) = function->get_tree();
421 return new Blabel(decl);
424 // Make a statement which defines a label.
427 Gcc_backend::label_definition_statement(Blabel* label)
429 tree lab = label->get_tree();
430 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
431 void_type_node, lab);
432 return this->make_statement(ret);
435 // Make a goto statement.
438 Gcc_backend::goto_statement(Blabel* label, source_location location)
440 tree lab = label->get_tree();
441 tree ret = fold_build1_loc(location, GOTO_EXPR, void_type_node, lab);
442 return this->make_statement(ret);
445 // Get the address of a label.
448 Gcc_backend::label_address(Blabel* label, source_location location)
450 tree lab = label->get_tree();
452 TREE_ADDRESSABLE(lab) = 1;
453 tree ret = fold_convert_loc(location, ptr_type_node,
454 build_fold_addr_expr_loc(location, lab));
455 return this->make_expression(ret);
458 // The single backend.
460 static Gcc_backend gcc_backend;
462 // Return the backend generator.
470 // FIXME: Temporary functions while converting to the new backend
476 return new Bexpression(t);
482 return new Bstatement(t);
486 tree_to_function(tree t)
488 return new Bfunction(t);
492 expr_to_tree(Bexpression* be)
494 return be->get_tree();
498 stat_to_tree(Bstatement* bs)
500 return bs->get_tree();