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 non-empty interface, C type struct __go_interface.
59 // Go type interface{}, C type struct __go_empty_interface.
61 // Go type func(unsafe.Pointer), C type void (*) (void *).
63 // Pointer to Go type descriptor.
65 // Pointer to map 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 Location bloc = Linemap::predeclared_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());
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();
161 t = Type::make_empty_interface_type(bloc);
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);
174 t = Type::make_type_descriptor_ptr_type();
177 case RFT_MAPDESCRIPTOR:
178 t = Type::make_pointer_type(Map_type::make_map_descriptor_type());
182 runtime_function_types[bft] = t;
185 return runtime_function_types[bft];
188 // Convert an expression to the type to pass to a runtime function.
191 convert_to_runtime_function_type(Runtime_function_type bft, Expression* e,
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);
224 return Expression::make_unsafe_cast(runtime_function_type(bft), e, loc);
227 go_assert(e->type() == Type::make_type_descriptor_ptr_type());
230 case RFT_MAPDESCRIPTOR:
231 go_assert(e->type()->points_to()
232 == Map_type::make_map_descriptor_type());
237 // Convert all the types used for runtime functions to the backend
241 Runtime::convert_types(Gogo* gogo)
243 for (int i = 0; i < static_cast<int>(NUMBER_OF_RUNTIME_FUNCTION_TYPES); ++i)
245 Type* t = runtime_function_types[i];
246 if (t != NULL && t->named_type() != NULL)
248 bool r = t->verify();
250 t->named_type()->convert(gogo);
255 // The type used to define a runtime function.
257 struct Runtime_function
261 // Parameter types. Never more than 6, as it happens. RFT_VOID if
263 Runtime_function_type parameter_types[6];
264 // Result types. Never more than 2, as it happens. RFT_VOID if not
266 Runtime_function_type result_types[2];
269 static const Runtime_function runtime_functions[] =
272 #define DEF_GO_RUNTIME(CODE, NAME, PARAMS, RESULTS) { NAME, PARAMS, RESULTS } ,
274 #include "runtime.def"
276 #undef DEF_GO_RUNTIME
281 runtime_function_declarations[Runtime::NUMBER_OF_FUNCTIONS];
283 // Get the declaration of a runtime function.
286 Runtime::runtime_declaration(Function code)
288 go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
289 if (runtime_function_declarations[code] == NULL)
291 const Runtime_function* pb = &runtime_functions[code];
293 Location bloc = Linemap::predeclared_location();
295 Typed_identifier_list* param_types = NULL;
296 if (pb->parameter_types[0] != RFT_VOID)
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]));
304 if (pb->parameter_types[i] == RFT_VOID)
306 Type* t = runtime_function_type(pb->parameter_types[i]);
307 param_types->push_back(Typed_identifier("", t, bloc));
311 Typed_identifier_list* result_types = NULL;
312 if (pb->result_types[0] != RFT_VOID)
314 result_types = new Typed_identifier_list();
315 for (unsigned int i = 0;
316 i < sizeof(pb->result_types) / sizeof(pb->result_types[0]);
319 if (pb->result_types[i] == RFT_VOID)
321 Type* t = runtime_function_type(pb->result_types[i]);
322 result_types->push_back(Typed_identifier("", t, bloc));
326 Function_type* fntype = Type::make_function_type(NULL, param_types,
328 const char* n = pb->name;
329 const char* n1 = strchr(n, '.');
332 Named_object* no = Named_object::make_function_declaration(n, NULL,
334 no->func_declaration_value()->set_asm_name(pb->name);
336 runtime_function_declarations[code] = no;
339 return runtime_function_declarations[code];
342 // Make a call to a runtime function.
345 Runtime::make_call(Runtime::Function code, Location loc,
346 int param_count, ...)
348 go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
350 const Runtime_function* pb = &runtime_functions[code];
352 go_assert(static_cast<size_t>(param_count)
353 <= sizeof(pb->parameter_types) / sizeof(pb->parameter_types[0]));
355 Named_object* no = runtime_declaration(code);
356 Expression* func = Expression::make_func_reference(no, NULL, loc);
358 Expression_list* args = new Expression_list();
359 args->reserve(param_count);
362 va_start(ap, param_count);
363 for (int i = 0; i < param_count; ++i)
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));
371 return Expression::make_call(func, args, false, loc);
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
379 Runtime::map_iteration_type()
381 const unsigned long map_iteration_size = 4;
384 mpz_init_set_ui(ival, map_iteration_size);
385 Expression* iexpr = Expression::make_integer(&ival, NULL,
386 Linemap::predeclared_location());
389 return Type::make_array_type(runtime_function_type(RFT_POINTER), iexpr);