OSDN Git Service

Use backend interface for labels and goto 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   assignment_statement(Bexpression* lhs, Bexpression* rhs, source_location);
171
172   Bstatement*
173   return_statement(Bfunction*, const std::vector<Bexpression*>&,
174                    source_location);
175
176   // Labels.
177
178   Blabel*
179   label(Bfunction*, const std::string& name, source_location);
180
181   Bstatement*
182   label_definition_statement(Blabel*);
183
184   Bstatement*
185   goto_statement(Blabel*, source_location);
186
187   Bexpression*
188   label_address(Blabel*, source_location);
189
190  private:
191   // Make a Bexpression from a tree.
192   Bexpression*
193   make_expression(tree t)
194   { return new Bexpression(t); }
195
196   // Make a Bstatement from a tree.
197   Bstatement*
198   make_statement(tree t)
199   { return new Bstatement(t); }
200 };
201
202 // A helper function.
203
204 static inline tree
205 get_identifier_from_string(const std::string& str)
206 {
207   return get_identifier_with_length(str.data(), str.length());
208 }
209 // Assignment.
210
211 Bstatement*
212 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
213                                   source_location location)
214 {
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,
220                                               void_type_node,
221                                               lhs_tree, rhs_tree));
222 }
223
224 // Return.
225
226 Bstatement*
227 Gcc_backend::return_statement(Bfunction* bfunction,
228                               const std::vector<Bexpression*>& vals,
229                               source_location location)
230 {
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);
237   tree ret;
238   if (vals.empty())
239     ret = fold_build1_loc(location, RETURN_EXPR, void_type_node, NULL_TREE);
240   else if (vals.size() == 1)
241     {
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);
248     }
249   else
250     {
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
254       // statement.
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();
260            p != vals.end();
261            p++, field = DECL_CHAIN(field))
262         {
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);
272         }
273       gcc_assert(field == NULL_TREE);
274       tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
275                                  result, rettmp);
276       tree ret_expr = fold_build1_loc(location, RETURN_EXPR, void_type_node,
277                                       set);
278       append_to_statement_list(ret_expr, &stmt_list);
279       ret = stmt_list;
280     }
281   return this->make_statement(ret);
282 }
283
284 // Make a label.
285
286 Blabel*
287 Gcc_backend::label(Bfunction* function, const std::string& name,
288                    source_location location)
289 {
290   tree decl;
291   if (name.empty())
292     decl = create_artificial_label(location);
293   else
294     {
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();
298     }
299   return new Blabel(decl);
300 }
301
302 // Make a statement which defines a label.
303
304 Bstatement*
305 Gcc_backend::label_definition_statement(Blabel* label)
306 {
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);
311 }
312
313 // Make a goto statement.
314
315 Bstatement*
316 Gcc_backend::goto_statement(Blabel* label, source_location location)
317 {
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);
321 }
322
323 // Get the address of a label.
324
325 Bexpression*
326 Gcc_backend::label_address(Blabel* label, source_location location)
327 {
328   tree lab = label->get_tree();
329   TREE_USED(lab) = 1;
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);
334 }
335
336 // The single backend.
337
338 static Gcc_backend gcc_backend;
339
340 // Return the backend generator.
341
342 Backend*
343 go_get_backend()
344 {
345   return &gcc_backend;
346 }
347
348 // FIXME: Temporary functions while converting to the new backend
349 // interface.
350
351 Bexpression*
352 tree_to_expr(tree t)
353 {
354   return new Bexpression(t);
355 }
356
357 Bfunction*
358 tree_to_function(tree t)
359 {
360   return new Bfunction(t);
361 }
362
363 tree
364 expression_to_tree(Bexpression* be)
365 {
366   return be->get_tree();
367 }
368
369 tree
370 statement_to_tree(Bstatement* bs)
371 {
372   return bs->get_tree();
373 }