OSDN Git Service

7893d45f1b9641599a807ddfccf233b1731fe0c5
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / runtime.cc
1 // runtime.cc -- runtime functions called by generated code
2
3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #include "go-system.h"
8
9 #include <gmp.h>
10
11 #include "gogo.h"
12 #include "types.h"
13 #include "expressions.h"
14 #include "runtime.h"
15
16 // The frontend generates calls to various runtime functions.  They
17 // are implemented in libgo/runtime.  This is how the runtime
18 // functions are represented in the frontend.  Note that there is
19 // currently nothing which ensures that the compiler's understanding
20 // of the runtime function matches the actual implementation in
21 // libgo/runtime.
22
23 // Parameter and result types used by runtime functions.
24
25 enum Runtime_function_type
26 {
27   // General indicator that value is not used.
28   RFT_VOID,
29   // Go type bool, C type _Bool.
30   RFT_BOOL,
31   // Go type *bool, C type _Bool*.
32   RFT_BOOLPTR,
33   // Go type int, C type int.
34   RFT_INT,
35   // Go type int64, C type int64_t.
36   RFT_INT64,
37   // Go type uint64, C type uint64_t.
38   RFT_UINT64,
39   // Go type uintptr, C type uintptr_t.
40   RFT_UINTPTR,
41   // Go type float64, C type double.
42   RFT_FLOAT64,
43   // Go type complex128, C type __complex double.
44   RFT_COMPLEX128,
45   // Go type string, C type struct __go_string.
46   RFT_STRING,
47   // Go type unsafe.Pointer, C type "void *".
48   RFT_POINTER,
49   // Go type []any, C type struct __go_open_array.
50   RFT_SLICE,
51   // Go type map[any]any, C type struct __go_map *.
52   RFT_MAP,
53   // Pointer to map iteration type.
54   RFT_MAPITER,
55   // Go type chan any, C type struct __go_channel *.
56   RFT_CHAN,
57   // Go type non-empty interface, C type struct __go_interface.
58   RFT_IFACE,
59   // Go type interface{}, C type struct __go_empty_interface.
60   RFT_EFACE,
61   // Go type func(unsafe.Pointer), C type void (*) (void *).
62   RFT_FUNC_PTR,
63   // Pointer to Go type descriptor.
64   RFT_TYPE,
65   // Pointer to map descriptor.
66   RFT_MAPDESCRIPTOR,
67
68   NUMBER_OF_RUNTIME_FUNCTION_TYPES
69 };
70
71 // The Type structures for the runtime function types.
72
73 static Type* runtime_function_types[NUMBER_OF_RUNTIME_FUNCTION_TYPES];
74
75 // Get the Type for a Runtime_function_type code.
76
77 static Type*
78 runtime_function_type(Runtime_function_type bft)
79 {
80   go_assert(bft < NUMBER_OF_RUNTIME_FUNCTION_TYPES);
81   if (runtime_function_types[bft] == NULL)
82     {
83       const Location bloc = Linemap::predeclared_location();
84       Type* t;
85       switch (bft)
86         {
87         default:
88         case RFT_VOID:
89           go_unreachable();
90
91         case RFT_BOOL:
92           t = Type::lookup_bool_type();
93           break;
94
95         case RFT_BOOLPTR:
96           t = Type::make_pointer_type(Type::lookup_bool_type());
97           break;
98
99         case RFT_INT:
100           t = Type::lookup_integer_type("int");
101           break;
102
103         case RFT_INT64:
104           t = Type::lookup_integer_type("int64");
105           break;
106
107         case RFT_UINT64:
108           t = Type::lookup_integer_type("uint64");
109           break;
110
111         case RFT_UINTPTR:
112           t = Type::lookup_integer_type("uintptr");
113           break;
114
115         case RFT_FLOAT64:
116           t = Type::lookup_float_type("float64");
117           break;
118
119         case RFT_COMPLEX128:
120           t = Type::lookup_complex_type("complex128");
121           break;
122
123         case RFT_STRING:
124           t = Type::lookup_string_type();
125           break;
126
127         case RFT_POINTER:
128           t = Type::make_pointer_type(Type::make_void_type());
129           break;
130
131         case RFT_SLICE:
132           t = Type::make_array_type(Type::make_void_type(), NULL);
133           break;
134
135         case RFT_MAP:
136           t = Type::make_map_type(Type::make_void_type(),
137                                   Type::make_void_type(),
138                                   bloc);
139           break;
140
141         case RFT_MAPITER:
142           t = Type::make_pointer_type(Runtime::map_iteration_type());
143           break;
144
145         case RFT_CHAN:
146           t = Type::make_channel_type(true, true, Type::make_void_type());
147           break;
148
149         case RFT_IFACE:
150           {
151             Typed_identifier_list* methods = new Typed_identifier_list();
152             Type* mtype = Type::make_function_type(NULL, NULL, NULL, bloc);
153             methods->push_back(Typed_identifier("x", mtype, bloc));
154             Interface_type* it = Type::make_interface_type(methods, bloc);
155             it->finalize_methods();
156             t = it;
157           }
158           break;
159
160         case RFT_EFACE:
161           t = Type::make_empty_interface_type(bloc);
162           break;
163
164         case RFT_FUNC_PTR:
165           {
166             Typed_identifier_list* param_types = new Typed_identifier_list();
167             Type* ptrtype = runtime_function_type(RFT_POINTER);
168             param_types->push_back(Typed_identifier("", ptrtype, bloc));
169             t = Type::make_function_type(NULL, param_types, NULL, bloc);
170           }
171           break;
172
173         case RFT_TYPE:
174           t = Type::make_type_descriptor_ptr_type();
175           break;
176
177         case RFT_MAPDESCRIPTOR:
178           t = Type::make_pointer_type(Map_type::make_map_descriptor_type());
179           break;
180         }
181
182       runtime_function_types[bft] = t;
183     }
184
185   return runtime_function_types[bft];
186 }
187
188 // Convert an expression to the type to pass to a runtime function.
189
190 static Expression*
191 convert_to_runtime_function_type(Runtime_function_type bft, Expression* e,
192                                  Location loc)
193 {
194   switch (bft)
195     {
196     default:
197     case RFT_VOID:
198       go_unreachable();
199
200     case RFT_BOOL:
201     case RFT_BOOLPTR:
202     case RFT_INT:
203     case RFT_INT64:
204     case RFT_UINT64:
205     case RFT_UINTPTR:
206     case RFT_FLOAT64:
207     case RFT_COMPLEX128:
208     case RFT_STRING:
209     case RFT_POINTER:
210     case RFT_MAPITER:
211     case RFT_FUNC_PTR:
212       {
213         Type* t = runtime_function_type(bft);
214         if (!Type::are_identical(t, e->type(), true, NULL))
215           e = Expression::make_cast(t, e, loc);
216         return e;
217       }
218
219     case RFT_SLICE:
220     case RFT_MAP:
221     case RFT_CHAN:
222     case RFT_IFACE:
223     case RFT_EFACE:
224       return Expression::make_unsafe_cast(runtime_function_type(bft), e, loc);
225
226     case RFT_TYPE:
227       go_assert(e->type() == Type::make_type_descriptor_ptr_type());
228       return e;
229
230     case RFT_MAPDESCRIPTOR:
231       go_assert(e->type()->points_to()
232                 == Map_type::make_map_descriptor_type());
233       return e;
234     }
235 }
236
237 // Convert all the types used for runtime functions to the backend
238 // representation.
239
240 void
241 Runtime::convert_types(Gogo* gogo)
242 {
243   for (int i = 0; i < static_cast<int>(NUMBER_OF_RUNTIME_FUNCTION_TYPES); ++i)
244     {
245       Type* t = runtime_function_types[i];
246       if (t != NULL && t->named_type() != NULL)
247         {
248           bool r = t->verify();
249           go_assert(r);
250           t->named_type()->convert(gogo);
251         }
252     }
253 }
254
255 // The type used to define a runtime function.
256
257 struct Runtime_function
258 {
259   // Function name.
260   const char* name;
261   // Parameter types.  Never more than 6, as it happens.  RFT_VOID if
262   // not used.
263   Runtime_function_type parameter_types[6];
264   // Result types.  Never more than 2, as it happens.  RFT_VOID if not
265   // used.
266   Runtime_function_type result_types[2];
267 };
268
269 static const Runtime_function runtime_functions[] =
270 {
271
272 #define DEF_GO_RUNTIME(CODE, NAME, PARAMS, RESULTS) { NAME, PARAMS, RESULTS } ,
273
274 #include "runtime.def"
275
276 #undef DEF_GO_RUNTIME
277
278 };
279
280 static Named_object*
281 runtime_function_declarations[Runtime::NUMBER_OF_FUNCTIONS];
282
283 // Get the declaration of a runtime function.
284
285 Named_object*
286 Runtime::runtime_declaration(Function code)
287 {
288   go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
289   if (runtime_function_declarations[code] == NULL)
290     {
291       const Runtime_function* pb = &runtime_functions[code];
292
293       Location bloc = Linemap::predeclared_location();
294
295       Typed_identifier_list* param_types = NULL;
296       if (pb->parameter_types[0] != RFT_VOID)
297         {
298           param_types = new Typed_identifier_list();
299           for (unsigned int i = 0;
300                i < (sizeof(pb->parameter_types)
301                     / sizeof (pb->parameter_types[0]));
302                i++)
303             {
304               if (pb->parameter_types[i] == RFT_VOID)
305                 break;
306               Type* t = runtime_function_type(pb->parameter_types[i]);
307               param_types->push_back(Typed_identifier("", t, bloc));
308             }
309         }
310
311       Typed_identifier_list* result_types = NULL;
312       if (pb->result_types[0] != RFT_VOID)
313         {
314           result_types = new Typed_identifier_list();
315           for (unsigned int i = 0;
316                i < sizeof(pb->result_types) / sizeof(pb->result_types[0]);
317                i++)
318             {
319               if (pb->result_types[i] == RFT_VOID)
320                 break;
321               Type* t = runtime_function_type(pb->result_types[i]);
322               result_types->push_back(Typed_identifier("", t, bloc));
323             }
324         }
325
326       Function_type* fntype = Type::make_function_type(NULL, param_types,
327                                                        result_types, bloc);
328       const char* n = pb->name;
329       const char* n1 = strchr(n, '.');
330       if (n1 != NULL)
331         n = n1 + 1;
332       Named_object* no = Named_object::make_function_declaration(n, NULL,
333                                                                  fntype, bloc);
334       no->func_declaration_value()->set_asm_name(pb->name);
335
336       runtime_function_declarations[code] = no;
337     }
338
339   return runtime_function_declarations[code];
340 }
341
342 // Make a call to a runtime function.
343
344 Call_expression*
345 Runtime::make_call(Runtime::Function code, Location loc,
346                    int param_count, ...)
347 {
348   go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
349
350   const Runtime_function* pb = &runtime_functions[code];
351
352   go_assert(static_cast<size_t>(param_count)
353              <= sizeof(pb->parameter_types) / sizeof(pb->parameter_types[0]));
354
355   Named_object* no = runtime_declaration(code);
356   Expression* func = Expression::make_func_reference(no, NULL, loc);
357
358   Expression_list* args = new Expression_list();
359   args->reserve(param_count);
360
361   va_list ap;
362   va_start(ap, param_count);
363   for (int i = 0; i < param_count; ++i)
364     {
365       Expression* e = va_arg(ap, Expression*);
366       Runtime_function_type rft = pb->parameter_types[i];
367       args->push_back(convert_to_runtime_function_type(rft, e, loc));
368     }
369   va_end(ap);
370
371   return Expression::make_call(func, args, false, loc);
372 }
373
374 // The type we use for a map iteration.  This is really a struct which
375 // is four pointers long.  This must match the runtime struct
376 // __go_hash_iter.
377
378 Type*
379 Runtime::map_iteration_type()
380 {
381   const unsigned long map_iteration_size = 4;
382
383   mpz_t ival;
384   mpz_init_set_ui(ival, map_iteration_size);
385   Expression* iexpr = Expression::make_integer(&ival, NULL,
386                                                Linemap::predeclared_location());
387   mpz_clear(ival);
388
389   return Type::make_array_type(runtime_function_type(RFT_POINTER), iexpr);
390 }