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.
11 #include "expressions.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
21 // Parameter and result types used by runtime functions.
23 enum Runtime_function_type
25 // General indicator that value is not used.
27 // Go untyped bool, C type _Bool.
29 // Go type *bool, C type _Bool*.
31 // Go type int, C type intgo.
33 // Go type int32, C type int32_t.
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 rune, C type int32_t.
43 // Go type float64, C type double.
45 // Go type complex64, C type __complex float.
47 // Go type complex128, C type __complex double.
49 // Go type string, C type struct __go_string.
51 // Go type unsafe.Pointer, C type "void *".
53 // Go type []any, C type struct __go_open_array.
55 // Go type map[any]any, C type struct __go_map *.
57 // Pointer to map iteration type.
59 // Go type chan any, C type struct __go_channel *.
61 // Go type non-empty interface, C type struct __go_interface.
63 // Go type interface{}, C type struct __go_empty_interface.
65 // Go type func(unsafe.Pointer), C type void (*) (void *).
67 // Pointer to Go type descriptor.
69 // Pointer to map descriptor.
72 NUMBER_OF_RUNTIME_FUNCTION_TYPES
75 // The Type structures for the runtime function types.
77 static Type
* runtime_function_types
[NUMBER_OF_RUNTIME_FUNCTION_TYPES
];
79 // Get the Type for a Runtime_function_type code.
82 runtime_function_type(Runtime_function_type bft
)
84 go_assert(bft
< NUMBER_OF_RUNTIME_FUNCTION_TYPES
);
85 Type
* any
= Type::make_pointer_type(Type::make_void_type());
86 if (runtime_function_types
[bft
] == NULL
)
88 const Location bloc
= Linemap::predeclared_location();
97 t
= Type::make_boolean_type();
101 t
= Type::make_pointer_type(Type::lookup_bool_type());
105 t
= Type::lookup_integer_type("int");
109 t
= Type::lookup_integer_type("int32");
113 t
= Type::lookup_integer_type("int64");
117 t
= Type::lookup_integer_type("uint64");
121 t
= Type::lookup_integer_type("int32");
125 t
= Type::lookup_integer_type("uintptr");
129 t
= Type::lookup_float_type("float64");
133 t
= Type::lookup_complex_type("complex64");
137 t
= Type::lookup_complex_type("complex128");
141 t
= Type::lookup_string_type();
145 t
= Type::make_pointer_type(Type::make_void_type());
149 t
= Type::make_array_type(any
, NULL
);
153 t
= Type::make_map_type(any
, any
, bloc
);
157 t
= Type::make_pointer_type(Runtime::map_iteration_type());
161 t
= Type::make_channel_type(true, true, any
);
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();
176 t
= Type::make_empty_interface_type(bloc
);
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
);
189 t
= Type::make_type_descriptor_ptr_type();
192 case RFT_MAPDESCRIPTOR
:
193 t
= Type::make_pointer_type(Map_type::make_map_descriptor_type());
197 runtime_function_types
[bft
] = t
;
200 return runtime_function_types
[bft
];
203 // Convert an expression to the type to pass to a runtime function.
206 convert_to_runtime_function_type(Runtime_function_type bft
, Expression
* e
,
231 Type
* t
= runtime_function_type(bft
);
232 if (!Type::are_identical(t
, e
->type(), true, NULL
))
233 e
= Expression::make_cast(t
, e
, loc
);
242 return Expression::make_unsafe_cast(runtime_function_type(bft
), e
, loc
);
245 go_assert(e
->type() == Type::make_type_descriptor_ptr_type());
248 case RFT_MAPDESCRIPTOR
:
249 go_assert(e
->type()->points_to()
250 == Map_type::make_map_descriptor_type());
255 // Convert all the types used for runtime functions to the backend
259 Runtime::convert_types(Gogo
* gogo
)
261 for (int i
= 0; i
< static_cast<int>(NUMBER_OF_RUNTIME_FUNCTION_TYPES
); ++i
)
263 Type
* t
= runtime_function_types
[i
];
264 if (t
!= NULL
&& t
->named_type() != NULL
)
266 bool r
= t
->verify();
268 t
->named_type()->convert(gogo
);
273 // The type used to define a runtime function.
275 struct Runtime_function
279 // Parameter types. Never more than 6, as it happens. RFT_VOID if
281 Runtime_function_type parameter_types
[6];
282 // Result types. Never more than 2, as it happens. RFT_VOID if not
284 Runtime_function_type result_types
[2];
287 static const Runtime_function runtime_functions
[] =
290 #define DEF_GO_RUNTIME(CODE, NAME, PARAMS, RESULTS) { NAME, PARAMS, RESULTS } ,
292 #include "runtime.def"
294 #undef DEF_GO_RUNTIME
299 runtime_function_declarations
[Runtime::NUMBER_OF_FUNCTIONS
];
301 // Get the declaration of a runtime function.
304 Runtime::runtime_declaration(Function code
)
306 go_assert(code
< Runtime::NUMBER_OF_FUNCTIONS
);
307 if (runtime_function_declarations
[code
] == NULL
)
309 const Runtime_function
* pb
= &runtime_functions
[code
];
311 Location bloc
= Linemap::predeclared_location();
313 Typed_identifier_list
* param_types
= NULL
;
314 if (pb
->parameter_types
[0] != RFT_VOID
)
316 param_types
= new Typed_identifier_list();
317 for (unsigned int i
= 0;
318 i
< (sizeof(pb
->parameter_types
)
319 / sizeof (pb
->parameter_types
[0]));
322 if (pb
->parameter_types
[i
] == RFT_VOID
)
324 Type
* t
= runtime_function_type(pb
->parameter_types
[i
]);
325 param_types
->push_back(Typed_identifier("", t
, bloc
));
329 Typed_identifier_list
* result_types
= NULL
;
330 if (pb
->result_types
[0] != RFT_VOID
)
332 result_types
= new Typed_identifier_list();
333 for (unsigned int i
= 0;
334 i
< sizeof(pb
->result_types
) / sizeof(pb
->result_types
[0]);
337 if (pb
->result_types
[i
] == RFT_VOID
)
339 Type
* t
= runtime_function_type(pb
->result_types
[i
]);
340 result_types
->push_back(Typed_identifier("", t
, bloc
));
344 Function_type
* fntype
= Type::make_function_type(NULL
, param_types
,
346 const char* n
= pb
->name
;
347 const char* n1
= strchr(n
, '.');
350 Named_object
* no
= Named_object::make_function_declaration(n
, NULL
,
352 no
->func_declaration_value()->set_asm_name(pb
->name
);
354 runtime_function_declarations
[code
] = no
;
357 return runtime_function_declarations
[code
];
360 // Make a call to a runtime function.
363 Runtime::make_call(Runtime::Function code
, Location loc
,
364 int param_count
, ...)
366 go_assert(code
< Runtime::NUMBER_OF_FUNCTIONS
);
368 const Runtime_function
* pb
= &runtime_functions
[code
];
370 go_assert(static_cast<size_t>(param_count
)
371 <= sizeof(pb
->parameter_types
) / sizeof(pb
->parameter_types
[0]));
373 Named_object
* no
= runtime_declaration(code
);
374 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
376 Expression_list
* args
= new Expression_list();
377 args
->reserve(param_count
);
380 va_start(ap
, param_count
);
381 for (int i
= 0; i
< param_count
; ++i
)
383 Expression
* e
= va_arg(ap
, Expression
*);
384 Runtime_function_type rft
= pb
->parameter_types
[i
];
385 args
->push_back(convert_to_runtime_function_type(rft
, e
, loc
));
389 return Expression::make_call(func
, args
, false, loc
);
392 // The type we use for a map iteration. This is really a struct which
393 // is four pointers long. This must match the runtime struct
397 Runtime::map_iteration_type()
399 const unsigned long map_iteration_size
= 4;
401 Expression::make_integer_ul(map_iteration_size
, NULL
,
402 Linemap::predeclared_location());
403 return Type::make_array_type(runtime_function_type(RFT_POINTER
), iexpr
);