* auto-profile.c (afdo_annotate_cfg): Use update_max_bb_count.
[official-gcc.git] / gcc / go / gofrontend / runtime.cc
bloba92144980222e9d9540f19ff55c1757b3ef855c5
1 // runtime.cc -- runtime functions called by generated code
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.
7 #include "go-system.h"
9 #include "gogo.h"
10 #include "types.h"
11 #include "expressions.h"
12 #include "runtime.h"
14 // The frontend generates calls to various runtime functions. They
15 // are implemented in libgo/runtime. This is how the runtime
16 // functions are represented in the frontend. Note that there is
17 // currently nothing which ensures that the compiler's understanding
18 // of the runtime function matches the actual implementation in
19 // libgo/runtime.
21 // Parameter and result types used by runtime functions.
23 enum Runtime_function_type
25 // General indicator that value is not used.
26 RFT_VOID,
27 // Go untyped bool, C type _Bool.
28 RFT_BOOL,
29 // Go type *bool, C type _Bool*.
30 RFT_BOOLPTR,
31 // Go type int, C type intgo.
32 RFT_INT,
33 // Go type int32, C type int32_t.
34 RFT_INT32,
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 rune, C type int32_t.
42 RFT_RUNE,
43 // Go type float64, C type double.
44 RFT_FLOAT64,
45 // Go type complex64, C type __complex float.
46 RFT_COMPLEX64,
47 // Go type complex128, C type __complex double.
48 RFT_COMPLEX128,
49 // Go type string, C type struct __go_string.
50 RFT_STRING,
51 // Go type unsafe.Pointer, C type "void *".
52 RFT_POINTER,
53 // Go type []any, C type struct __go_open_array.
54 RFT_SLICE,
55 // Go type map[any]any, C type struct __go_map *.
56 RFT_MAP,
57 // Go type chan any, C type struct __go_channel *.
58 RFT_CHAN,
59 // Go type non-empty interface, C type struct __go_interface.
60 RFT_IFACE,
61 // Go type interface{}, C type struct __go_empty_interface.
62 RFT_EFACE,
63 // Go type func(unsafe.Pointer), C type void (*) (void *).
64 RFT_FUNC_PTR,
65 // Pointer to Go type descriptor.
66 RFT_TYPE,
67 // [2]string.
68 RFT_ARRAY2STRING,
69 // [3]string.
70 RFT_ARRAY3STRING,
71 // [4]string.
72 RFT_ARRAY4STRING,
73 // [5]string.
74 RFT_ARRAY5STRING,
76 NUMBER_OF_RUNTIME_FUNCTION_TYPES
79 // The Type structures for the runtime function types.
81 static Type* runtime_function_types[NUMBER_OF_RUNTIME_FUNCTION_TYPES];
83 // Get the Type for a Runtime_function_type code.
85 static Type*
86 runtime_function_type(Runtime_function_type bft)
88 go_assert(bft < NUMBER_OF_RUNTIME_FUNCTION_TYPES);
89 Type* any = Type::make_pointer_type(Type::make_void_type());
90 if (runtime_function_types[bft] == NULL)
92 const Location bloc = Linemap::predeclared_location();
93 Type* t;
94 switch (bft)
96 default:
97 case RFT_VOID:
98 go_unreachable();
100 case RFT_BOOL:
101 t = Type::make_boolean_type();
102 break;
104 case RFT_BOOLPTR:
105 t = Type::make_pointer_type(Type::lookup_bool_type());
106 break;
108 case RFT_INT:
109 t = Type::lookup_integer_type("int");
110 break;
112 case RFT_INT32:
113 t = Type::lookup_integer_type("int32");
114 break;
116 case RFT_INT64:
117 t = Type::lookup_integer_type("int64");
118 break;
120 case RFT_UINT64:
121 t = Type::lookup_integer_type("uint64");
122 break;
124 case RFT_RUNE:
125 t = Type::lookup_integer_type("int32");
126 break;
128 case RFT_UINTPTR:
129 t = Type::lookup_integer_type("uintptr");
130 break;
132 case RFT_FLOAT64:
133 t = Type::lookup_float_type("float64");
134 break;
136 case RFT_COMPLEX64:
137 t = Type::lookup_complex_type("complex64");
138 break;
140 case RFT_COMPLEX128:
141 t = Type::lookup_complex_type("complex128");
142 break;
144 case RFT_STRING:
145 t = Type::lookup_string_type();
146 break;
148 case RFT_POINTER:
149 t = Type::make_pointer_type(Type::make_void_type());
150 break;
152 case RFT_SLICE:
153 t = Type::make_array_type(any, NULL);
154 break;
156 case RFT_MAP:
157 t = Type::make_map_type(any, any, bloc);
158 break;
160 case RFT_CHAN:
161 t = Type::make_channel_type(true, true, any);
162 break;
164 case RFT_IFACE:
166 Typed_identifier_list* methods = new Typed_identifier_list();
167 Type* mtype = Type::make_function_type(NULL, NULL, NULL, bloc);
168 methods->push_back(Typed_identifier("x", mtype, bloc));
169 Interface_type* it = Type::make_interface_type(methods, bloc);
170 it->finalize_methods();
171 t = it;
173 break;
175 case RFT_EFACE:
176 t = Type::make_empty_interface_type(bloc);
177 break;
179 case RFT_FUNC_PTR:
181 Typed_identifier_list* param_types = new Typed_identifier_list();
182 Type* ptrtype = runtime_function_type(RFT_POINTER);
183 param_types->push_back(Typed_identifier("", ptrtype, bloc));
184 t = Type::make_function_type(NULL, param_types, NULL, bloc);
186 break;
188 case RFT_TYPE:
189 t = Type::make_type_descriptor_ptr_type();
190 break;
192 case RFT_ARRAY2STRING:
194 Array_type* at =
195 Type::make_array_type(Type::make_string_type(),
196 Expression::make_integer_ul(2, NULL,
197 bloc));
198 at->set_is_array_incomparable();
199 t = at;
201 break;
203 case RFT_ARRAY3STRING:
205 Array_type* at =
206 Type::make_array_type(Type::make_string_type(),
207 Expression::make_integer_ul(3, NULL,
208 bloc));
209 at->set_is_array_incomparable();
210 t = at;
212 break;
214 case RFT_ARRAY4STRING:
216 Array_type* at =
217 Type::make_array_type(Type::make_string_type(),
218 Expression::make_integer_ul(4, NULL,
219 bloc));
220 at->set_is_array_incomparable();
221 t = at;
223 break;
225 case RFT_ARRAY5STRING:
227 Array_type* at =
228 Type::make_array_type(Type::make_string_type(),
229 Expression::make_integer_ul(5, NULL,
230 bloc));
231 at->set_is_array_incomparable();
232 t = at;
234 break;
237 runtime_function_types[bft] = t;
240 return runtime_function_types[bft];
243 // Convert an expression to the type to pass to a runtime function.
245 static Expression*
246 convert_to_runtime_function_type(Runtime_function_type bft, Expression* e,
247 Location loc)
249 switch (bft)
251 default:
252 case RFT_VOID:
253 go_unreachable();
255 case RFT_BOOL:
256 case RFT_BOOLPTR:
257 case RFT_INT:
258 case RFT_INT32:
259 case RFT_INT64:
260 case RFT_UINT64:
261 case RFT_UINTPTR:
262 case RFT_RUNE:
263 case RFT_FLOAT64:
264 case RFT_COMPLEX64:
265 case RFT_COMPLEX128:
266 case RFT_STRING:
267 case RFT_POINTER:
268 case RFT_FUNC_PTR:
270 Type* t = runtime_function_type(bft);
271 if (!Type::are_identical(t, e->type(), true, NULL))
272 e = Expression::make_cast(t, e, loc);
273 return e;
276 case RFT_SLICE:
277 case RFT_MAP:
278 case RFT_CHAN:
279 case RFT_IFACE:
280 case RFT_EFACE:
281 case RFT_ARRAY2STRING:
282 case RFT_ARRAY3STRING:
283 case RFT_ARRAY4STRING:
284 case RFT_ARRAY5STRING:
285 return Expression::make_unsafe_cast(runtime_function_type(bft), e, loc);
287 case RFT_TYPE:
288 go_assert(e->type() == Type::make_type_descriptor_ptr_type());
289 return e;
293 // Convert all the types used for runtime functions to the backend
294 // representation.
296 void
297 Runtime::convert_types(Gogo* gogo)
299 for (int i = 0; i < static_cast<int>(NUMBER_OF_RUNTIME_FUNCTION_TYPES); ++i)
301 Type* t = runtime_function_types[i];
302 if (t != NULL && t->named_type() != NULL)
304 bool r = t->verify();
305 go_assert(r);
306 t->named_type()->convert(gogo);
311 // The type used to define a runtime function.
313 struct Runtime_function
315 // Function name.
316 const char* name;
317 // Parameter types. Never more than 6, as it happens. RFT_VOID if
318 // not used.
319 Runtime_function_type parameter_types[6];
320 // Result types. Never more than 2, as it happens. RFT_VOID if not
321 // used.
322 Runtime_function_type result_types[2];
325 static const Runtime_function runtime_functions[] =
328 #define DEF_GO_RUNTIME(CODE, NAME, PARAMS, RESULTS) { NAME, PARAMS, RESULTS } ,
330 #include "runtime.def"
332 #undef DEF_GO_RUNTIME
336 static Named_object*
337 runtime_function_declarations[Runtime::NUMBER_OF_FUNCTIONS];
339 // Get the declaration of a runtime function.
341 Named_object*
342 Runtime::runtime_declaration(Function code)
344 go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
345 if (runtime_function_declarations[code] == NULL)
347 const Runtime_function* pb = &runtime_functions[code];
349 Location bloc = Linemap::predeclared_location();
351 Typed_identifier_list* param_types = NULL;
352 if (pb->parameter_types[0] != RFT_VOID)
354 param_types = new Typed_identifier_list();
355 for (unsigned int i = 0;
356 i < (sizeof(pb->parameter_types)
357 / sizeof (pb->parameter_types[0]));
358 i++)
360 if (pb->parameter_types[i] == RFT_VOID)
361 break;
362 Type* t = runtime_function_type(pb->parameter_types[i]);
363 param_types->push_back(Typed_identifier("", t, bloc));
367 Typed_identifier_list* result_types = NULL;
368 if (pb->result_types[0] != RFT_VOID)
370 result_types = new Typed_identifier_list();
371 for (unsigned int i = 0;
372 i < sizeof(pb->result_types) / sizeof(pb->result_types[0]);
373 i++)
375 if (pb->result_types[i] == RFT_VOID)
376 break;
377 Type* t = runtime_function_type(pb->result_types[i]);
378 result_types->push_back(Typed_identifier("", t, bloc));
382 Function_type* fntype = Type::make_function_type(NULL, param_types,
383 result_types, bloc);
384 const char* n = pb->name;
385 const char* n1 = strchr(n, '.');
386 if (n1 != NULL)
387 n = n1 + 1;
388 Named_object* no = Named_object::make_function_declaration(n, NULL,
389 fntype, bloc);
390 no->func_declaration_value()->set_asm_name(pb->name);
392 runtime_function_declarations[code] = no;
395 return runtime_function_declarations[code];
398 // Make a call to a runtime function.
400 Call_expression*
401 Runtime::make_call(Runtime::Function code, Location loc,
402 int param_count, ...)
404 go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
406 const Runtime_function* pb = &runtime_functions[code];
408 go_assert(static_cast<size_t>(param_count)
409 <= sizeof(pb->parameter_types) / sizeof(pb->parameter_types[0]));
411 Named_object* no = runtime_declaration(code);
412 Expression* func = Expression::make_func_reference(no, NULL, loc);
414 Expression_list* args = new Expression_list();
415 args->reserve(param_count);
417 va_list ap;
418 va_start(ap, param_count);
419 for (int i = 0; i < param_count; ++i)
421 Expression* e = va_arg(ap, Expression*);
422 Runtime_function_type rft = pb->parameter_types[i];
423 args->push_back(convert_to_runtime_function_type(rft, e, loc));
425 va_end(ap);
427 return Expression::make_call(func, args, false, loc);
430 // Get the runtime code for a named builtin function. This is used as a helper
431 // when creating function references for call expressions. Every reference to
432 // a builtin runtime function should have the associated runtime code. If the
433 // name is ambiguous and can refer to many runtime codes, return
434 // NUMBER_OF_FUNCTIONS.
436 Runtime::Function
437 Runtime::name_to_code(const std::string& name)
439 Function code = Runtime::NUMBER_OF_FUNCTIONS;
441 // Aliases seen in function declaration code.
442 // TODO(cmang): Add other aliases.
443 if (name == "new")
444 code = Runtime::NEW;
445 else if (name == "close")
446 code = Runtime::CLOSE;
447 else if (name == "copy")
448 code = Runtime::SLICECOPY;
449 else if (name == "append")
450 code = Runtime::GROWSLICE;
451 else if (name == "delete")
452 code = Runtime::MAPDELETE;
453 else
455 // Look through the known names for a match.
456 for (size_t i = 0; i < Runtime::NUMBER_OF_FUNCTIONS; i++)
458 if (strcmp(runtime_functions[i].name, name.c_str()) == 0)
459 code = static_cast<Runtime::Function>(i);
462 return code;