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.
13 #include "expressions.h"
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
23 // Parameter and result types used by runtime functions.
25 enum Runtime_function_type
27 // General indicator that value is not used.
29 // Go type bool, C type _Bool.
31 // Go type *bool, C type _Bool*.
33 // Go type int, C type int.
35 // Go type int64, C type int64_t.
37 // Go type uint64, C type uint64_t.
39 // Go type uintptr, C type uintptr_t.
41 // Go type float64, C type double.
43 // Go type complex128, C type __complex double.
45 // Go type string, C type struct __go_string.
47 // Go type unsafe.Pointer, C type "void *".
49 // Go type []any, C type struct __go_open_array.
51 // Go type map[any]any, C type struct __go_map *.
53 // Pointer to map iteration type.
55 // Go type chan any, C type struct __go_channel *.
57 // Go type *chan any, C type struct __go_channel **.
59 // Go type non-empty interface, C type struct __go_interface.
61 // Go type interface{}, C type struct __go_empty_interface.
63 // Go type func(unsafe.Pointer), C type void (*) (void *).
65 // Pointer to Go type descriptor.
68 NUMBER_OF_RUNTIME_FUNCTION_TYPES
71 // The Type structures for the runtime function types.
73 static Type* runtime_function_types[NUMBER_OF_RUNTIME_FUNCTION_TYPES];
75 // Get the Type for a Runtime_function_type code.
78 runtime_function_type(Runtime_function_type bft)
80 go_assert(bft < NUMBER_OF_RUNTIME_FUNCTION_TYPES);
81 if (runtime_function_types[bft] == NULL)
83 const source_location bloc = BUILTINS_LOCATION;
92 t = Type::lookup_bool_type();
96 t = Type::make_pointer_type(Type::lookup_bool_type());
100 t = Type::lookup_integer_type("int");
104 t = Type::lookup_integer_type("int64");
108 t = Type::lookup_integer_type("uint64");
112 t = Type::lookup_integer_type("uintptr");
116 t = Type::lookup_float_type("float64");
120 t = Type::lookup_complex_type("complex128");
124 t = Type::lookup_string_type();
128 t = Type::make_pointer_type(Type::make_void_type());
132 t = Type::make_array_type(Type::make_void_type(), NULL);
136 t = Type::make_map_type(Type::make_void_type(),
137 Type::make_void_type(),
142 t = Type::make_pointer_type(Runtime::map_iteration_type());
146 t = Type::make_channel_type(true, true, Type::make_void_type());
150 t = Type::make_pointer_type(runtime_function_type(RFT_CHAN));
155 Typed_identifier_list* methods = new Typed_identifier_list();
156 Type* mtype = Type::make_function_type(NULL, NULL, NULL, bloc);
157 methods->push_back(Typed_identifier("x", mtype, bloc));
158 t = Type::make_interface_type(methods, bloc);
163 t = Type::make_interface_type(NULL, bloc);
168 Typed_identifier_list* param_types = new Typed_identifier_list();
169 Type* ptrtype = runtime_function_type(RFT_POINTER);
170 param_types->push_back(Typed_identifier("", ptrtype, bloc));
171 t = Type::make_function_type(NULL, param_types, NULL, bloc);
176 t = Type::make_type_descriptor_ptr_type();
180 runtime_function_types[bft] = t;
183 return runtime_function_types[bft];
186 // Convert an expression to the type to pass to a runtime function.
189 convert_to_runtime_function_type(Runtime_function_type bft, Expression* e,
211 Type* t = runtime_function_type(bft);
212 if (!Type::are_identical(t, e->type(), true, NULL))
213 e = Expression::make_cast(t, e, loc);
223 return Expression::make_unsafe_cast(runtime_function_type(bft), e, loc);
226 go_assert(e->type() == Type::make_type_descriptor_ptr_type());
231 // Convert all the types used for runtime functions to the backend
235 Runtime::convert_types(Gogo* gogo)
237 for (int i = 0; i < static_cast<int>(NUMBER_OF_RUNTIME_FUNCTION_TYPES); ++i)
239 Type* t = runtime_function_types[i];
240 if (t != NULL && t->named_type() != NULL)
242 bool r = t->verify();
244 t->named_type()->convert(gogo);
249 // The type used to define a runtime function.
251 struct Runtime_function
255 // Parameter types. Never more than 6, as it happens. RFT_VOID if
257 Runtime_function_type parameter_types[6];
258 // Result types. Never more than 2, as it happens. RFT_VOID if not
260 Runtime_function_type result_types[2];
263 static const Runtime_function runtime_functions[] =
266 #define DEF_GO_RUNTIME(CODE, NAME, PARAMS, RESULTS) { NAME, PARAMS, RESULTS } ,
268 #include "runtime.def"
270 #undef DEF_GO_RUNTIME
275 runtime_function_declarations[Runtime::NUMBER_OF_FUNCTIONS];
277 // Get the declaration of a runtime function.
280 Runtime::runtime_declaration(Function code)
282 go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
283 if (runtime_function_declarations[code] == NULL)
285 const Runtime_function* pb = &runtime_functions[code];
287 source_location bloc = BUILTINS_LOCATION;
289 Typed_identifier_list* param_types = NULL;
290 if (pb->parameter_types[0] != RFT_VOID)
292 param_types = new Typed_identifier_list();
293 for (unsigned int i = 0;
294 i < (sizeof(pb->parameter_types)
295 / sizeof (pb->parameter_types[0]));
298 if (pb->parameter_types[i] == RFT_VOID)
300 Type* t = runtime_function_type(pb->parameter_types[i]);
301 param_types->push_back(Typed_identifier("", t, bloc));
305 Typed_identifier_list* result_types = NULL;
306 if (pb->result_types[0] != RFT_VOID)
308 result_types = new Typed_identifier_list();
309 for (unsigned int i = 0;
310 i < sizeof(pb->result_types) / sizeof(pb->result_types[0]);
313 if (pb->result_types[i] == RFT_VOID)
315 Type* t = runtime_function_type(pb->result_types[i]);
316 result_types->push_back(Typed_identifier("", t, bloc));
320 Function_type* fntype = Type::make_function_type(NULL, param_types,
322 const char* n = pb->name;
323 const char* n1 = strchr(n, '.');
326 Named_object* no = Named_object::make_function_declaration(n, NULL,
328 no->func_declaration_value()->set_asm_name(pb->name);
330 runtime_function_declarations[code] = no;
333 return runtime_function_declarations[code];
336 // Make a call to a runtime function.
339 Runtime::make_call(Runtime::Function code, source_location loc,
340 int param_count, ...)
342 go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
344 const Runtime_function* pb = &runtime_functions[code];
346 go_assert(static_cast<size_t>(param_count)
347 <= sizeof(pb->parameter_types) / sizeof(pb->parameter_types[0]));
349 Named_object* no = runtime_declaration(code);
350 Expression* func = Expression::make_func_reference(no, NULL, loc);
352 Expression_list* args = new Expression_list();
353 args->reserve(param_count);
356 va_start(ap, param_count);
357 for (int i = 0; i < param_count; ++i)
359 Expression* e = va_arg(ap, Expression*);
360 Runtime_function_type rft = pb->parameter_types[i];
361 args->push_back(convert_to_runtime_function_type(rft, e, loc));
365 return Expression::make_call(func, args, false, loc);
368 // The type we use for a map iteration. This is really a struct which
369 // is four pointers long. This must match the runtime struct
373 Runtime::map_iteration_type()
375 const unsigned long map_iteration_size = 4;
378 mpz_init_set_ui(ival, map_iteration_size);
379 Expression* iexpr = Expression::make_integer(&ival, NULL, BUILTINS_LOCATION);
382 return Type::make_array_type(runtime_function_type(RFT_POINTER), iexpr);
385 // Return the type used to pass a list of general channels to the
386 // select runtime function.
389 Runtime::chanptr_type()
391 return runtime_function_type(RFT_CHANPTR);