OSDN Git Service

Use backend interface for constant switch statements.
[pf3gnuchains/gcc-fork.git] / gcc / go / go-gcc.cc
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
4
5 // This file is part of GCC.
6
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
10 // version.
11
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
15 // for more details.
16
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/>.
20
21 #include "go-system.h"
22
23 // This has to be included outside of extern "C", so we have to
24 // include it here before tree.h includes it later.
25 #include <gmp.h>
26
27 #ifndef ENABLE_BUILD_WITH_CXX
28 extern "C"
29 {
30 #endif
31
32 #include "tree.h"
33 #include "tree-iterator.h"
34 #include "gimple.h"
35
36 #ifndef ENABLE_BUILD_WITH_CXX
37 }
38 #endif
39
40 #include "gogo.h"
41 #include "backend.h"
42
43 // A class wrapping a tree.
44
45 class Gcc_tree
46 {
47  public:
48   Gcc_tree(tree t)
49     : t_(t)
50   { }
51
52   tree
53   get_tree()
54   { return this->t_; }
55
56  private:
57   tree t_;
58 };
59
60 // In gcc, types, expressions, and statements are all trees.
61 class Btype : public Gcc_tree
62 {
63  public:
64   Btype(tree t)
65     : Gcc_tree(t)
66   { }
67 };
68
69 class Bexpression : public Gcc_tree
70 {
71  public:
72   Bexpression(tree t)
73     : Gcc_tree(t)
74   { }
75 };
76
77 class Bstatement : public Gcc_tree
78 {
79  public:
80   Bstatement(tree t)
81     : Gcc_tree(t)
82   { }
83 };
84
85 class Bfunction : public Gcc_tree
86 {
87  public:
88   Bfunction(tree t)
89     : Gcc_tree(t)
90   { }
91 };
92
93 class Blabel : public Gcc_tree
94 {
95  public:
96   Blabel(tree t)
97     : Gcc_tree(t)
98   { }
99 };
100
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.
106
107 class Gcc_backend : public Backend
108 {
109  public:
110   // Types.
111
112   Btype*
113   error_type()
114   { gcc_unreachable(); }
115
116   Btype*
117   void_type()
118   { gcc_unreachable(); }
119
120   Btype*
121   bool_type()
122   { gcc_unreachable(); }
123
124   Btype*
125   integer_type(bool /* is_unsigned */, int /* bits */)
126   { gcc_unreachable(); }
127
128   Btype*
129   float_type(int /* bits */)
130   { gcc_unreachable(); }
131
132   Btype*
133   string_type()
134   { gcc_unreachable(); }
135
136   Btype*
137   function_type(const Function_type*, Btype* /* receiver */,
138                 const Btypes* /* parameters */,
139                 const Btypes* /* results */)
140   { gcc_unreachable(); }
141
142   Btype*
143   struct_type(const Struct_type*, const Btypes* /* field_types */)
144   { gcc_unreachable(); }
145
146   Btype*
147   array_type(const Btype* /* element_type */, const Bexpression* /* length */)
148   { gcc_unreachable(); }
149
150   Btype*
151   slice_type(const Btype* /* element_type */)
152   { gcc_unreachable(); }
153
154   Btype*
155   map_type(const Btype* /* key_type */, const Btype* /* value_type */,
156            source_location)
157   { gcc_unreachable(); }
158
159   Btype*
160   channel_type(const Btype* /* element_type */)
161   { gcc_unreachable(); }
162
163   Btype*
164   interface_type(const Interface_type*, const Btypes* /* method_types */)
165   { gcc_unreachable(); }
166
167   // Statements.
168
169   Bstatement*
170   expression_statement(Bexpression*);
171
172   Bstatement*
173   assignment_statement(Bexpression* lhs, Bexpression* rhs, source_location);
174
175   Bstatement*
176   return_statement(Bfunction*, const std::vector<Bexpression*>&,
177                    source_location);
178
179   Bstatement*
180   if_statement(Bexpression* condition, Bstatement* then_block,
181                Bstatement* else_block, source_location);
182
183   Bstatement*
184   switch_statement(Bexpression* value,
185                    const std::vector<std::vector<Bexpression*> >& cases,
186                    const std::vector<Bstatement*>& statements,
187                    source_location);
188
189   Bstatement*
190   statement_list(const std::vector<Bstatement*>&);
191
192   // Labels.
193
194   Blabel*
195   label(Bfunction*, const std::string& name, source_location);
196
197   Bstatement*
198   label_definition_statement(Blabel*);
199
200   Bstatement*
201   goto_statement(Blabel*, source_location);
202
203   Bexpression*
204   label_address(Blabel*, source_location);
205
206  private:
207   // Make a Bexpression from a tree.
208   Bexpression*
209   make_expression(tree t)
210   { return new Bexpression(t); }
211
212   // Make a Bstatement from a tree.
213   Bstatement*
214   make_statement(tree t)
215   { return new Bstatement(t); }
216 };
217
218 // A helper function.
219
220 static inline tree
221 get_identifier_from_string(const std::string& str)
222 {
223   return get_identifier_with_length(str.data(), str.length());
224 }
225
226 // An expression as a statement.
227
228 Bstatement*
229 Gcc_backend::expression_statement(Bexpression* expr)
230 {
231   return this->make_statement(expr->get_tree());
232 }
233
234 // Assignment.
235
236 Bstatement*
237 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
238                                   source_location location)
239 {
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,
245                                               void_type_node,
246                                               lhs_tree, rhs_tree));
247 }
248
249 // Return.
250
251 Bstatement*
252 Gcc_backend::return_statement(Bfunction* bfunction,
253                               const std::vector<Bexpression*>& vals,
254                               source_location location)
255 {
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);
262   tree ret;
263   if (vals.empty())
264     ret = fold_build1_loc(location, RETURN_EXPR, void_type_node, NULL_TREE);
265   else if (vals.size() == 1)
266     {
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);
273     }
274   else
275     {
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
279       // statement.
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();
285            p != vals.end();
286            p++, field = DECL_CHAIN(field))
287         {
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);
297         }
298       gcc_assert(field == NULL_TREE);
299       tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
300                                  result, rettmp);
301       tree ret_expr = fold_build1_loc(location, RETURN_EXPR, void_type_node,
302                                       set);
303       append_to_statement_list(ret_expr, &stmt_list);
304       ret = stmt_list;
305     }
306   return this->make_statement(ret);
307 }
308
309 // If.
310
311 Bstatement*
312 Gcc_backend::if_statement(Bexpression* condition, Bstatement* then_block,
313                           Bstatement* else_block, source_location location)
314 {
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);
325 }
326
327 // Switch.
328
329 Bstatement*
330 Gcc_backend::switch_statement(
331     Bexpression* value,
332     const std::vector<std::vector<Bexpression*> >& cases,
333     const std::vector<Bstatement*>& statements,
334     source_location switch_location)
335 {
336   gcc_assert(cases.size() == statements.size());
337
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();
342        ++ps, ++pc)
343     {
344       if (pc->empty())
345         {
346           source_location loc = (*ps != NULL
347                                  ? EXPR_LOCATION((*ps)->get_tree())
348                                  : UNKNOWN_LOCATION);
349           tree label = create_artificial_label(loc);
350           tree c = build3_loc(loc, CASE_LABEL_EXPR, void_type_node, NULL_TREE,
351                               NULL_TREE, label);
352           append_to_statement_list(c, &stmt_list);
353         }
354       else
355         {
356           for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
357                pcv != pc->end();
358                ++pcv)
359             {
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);
368             }
369         }
370
371       if (*ps != NULL)
372         {
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);
377         }
378     }
379
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);
386 }
387
388 // List of statements.
389
390 Bstatement*
391 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
392 {
393   tree stmt_list = NULL_TREE;
394   for (std::vector<Bstatement*>::const_iterator p = statements.begin();
395        p != statements.end();
396        ++p)
397     {
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);
402     }
403   return this->make_statement(stmt_list);
404 }
405
406 // Make a label.
407
408 Blabel*
409 Gcc_backend::label(Bfunction* function, const std::string& name,
410                    source_location location)
411 {
412   tree decl;
413   if (name.empty())
414     decl = create_artificial_label(location);
415   else
416     {
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();
420     }
421   return new Blabel(decl);
422 }
423
424 // Make a statement which defines a label.
425
426 Bstatement*
427 Gcc_backend::label_definition_statement(Blabel* label)
428 {
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);
433 }
434
435 // Make a goto statement.
436
437 Bstatement*
438 Gcc_backend::goto_statement(Blabel* label, source_location location)
439 {
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);
443 }
444
445 // Get the address of a label.
446
447 Bexpression*
448 Gcc_backend::label_address(Blabel* label, source_location location)
449 {
450   tree lab = label->get_tree();
451   TREE_USED(lab) = 1;
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);
456 }
457
458 // The single backend.
459
460 static Gcc_backend gcc_backend;
461
462 // Return the backend generator.
463
464 Backend*
465 go_get_backend()
466 {
467   return &gcc_backend;
468 }
469
470 // FIXME: Temporary functions while converting to the new backend
471 // interface.
472
473 Bexpression*
474 tree_to_expr(tree t)
475 {
476   return new Bexpression(t);
477 }
478
479 Bstatement*
480 tree_to_stat(tree t)
481 {
482   return new Bstatement(t);
483 }
484
485 Bfunction*
486 tree_to_function(tree t)
487 {
488   return new Bfunction(t);
489 }
490
491 tree
492 expr_to_tree(Bexpression* be)
493 {
494   return be->get_tree();
495 }
496
497 tree
498 stat_to_tree(Bstatement* bs)
499 {
500   return bs->get_tree();
501 }