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 // 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.
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.
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();
101 t
= Type::make_boolean_type();
105 t
= Type::make_pointer_type(Type::lookup_bool_type());
109 t
= Type::lookup_integer_type("int");
113 t
= Type::lookup_integer_type("int32");
117 t
= Type::lookup_integer_type("int64");
121 t
= Type::lookup_integer_type("uint64");
125 t
= Type::lookup_integer_type("int32");
129 t
= Type::lookup_integer_type("uintptr");
133 t
= Type::lookup_float_type("float64");
137 t
= Type::lookup_complex_type("complex64");
141 t
= Type::lookup_complex_type("complex128");
145 t
= Type::lookup_string_type();
149 t
= Type::make_pointer_type(Type::make_void_type());
153 t
= Type::make_array_type(any
, NULL
);
157 t
= Type::make_map_type(any
, any
, bloc
);
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_ARRAY2STRING
:
195 Type::make_array_type(Type::make_string_type(),
196 Expression::make_integer_ul(2, NULL
,
198 at
->set_is_array_incomparable();
203 case RFT_ARRAY3STRING
:
206 Type::make_array_type(Type::make_string_type(),
207 Expression::make_integer_ul(3, NULL
,
209 at
->set_is_array_incomparable();
214 case RFT_ARRAY4STRING
:
217 Type::make_array_type(Type::make_string_type(),
218 Expression::make_integer_ul(4, NULL
,
220 at
->set_is_array_incomparable();
225 case RFT_ARRAY5STRING
:
228 Type::make_array_type(Type::make_string_type(),
229 Expression::make_integer_ul(5, NULL
,
231 at
->set_is_array_incomparable();
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.
246 convert_to_runtime_function_type(Runtime_function_type bft
, Expression
* e
,
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
);
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
);
288 go_assert(e
->type() == Type::make_type_descriptor_ptr_type());
293 // Convert all the types used for runtime functions to the backend
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();
306 t
->named_type()->convert(gogo
);
311 // The type used to define a runtime function.
313 struct Runtime_function
317 // Parameter types. Never more than 6, as it happens. RFT_VOID if
319 Runtime_function_type parameter_types
[6];
320 // Result types. Never more than 2, as it happens. RFT_VOID if not
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
337 runtime_function_declarations
[Runtime::NUMBER_OF_FUNCTIONS
];
339 // Get the declaration of a runtime function.
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]));
360 if (pb
->parameter_types
[i
] == RFT_VOID
)
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]);
375 if (pb
->result_types
[i
] == RFT_VOID
)
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
,
384 const char* n
= pb
->name
;
385 const char* n1
= strchr(n
, '.');
388 Named_object
* no
= Named_object::make_function_declaration(n
, NULL
,
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.
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
);
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
));
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.
437 Runtime::name_to_code(const std::string
& name
)
439 Function code
= Runtime::NUMBER_OF_FUNCTIONS
;
441 // Look through the known names for a match.
442 for (size_t i
= 0; i
< Runtime::NUMBER_OF_FUNCTIONS
; i
++)
444 const char* runtime_function_name
= runtime_functions
[i
].name
;
445 if (strcmp(runtime_function_name
, name
.c_str()) == 0)
446 code
= static_cast<Runtime::Function
>(i
);
447 // The names in the table have "runtime." prefix. We may be
448 // called with a name without the prefix. Try matching
449 // without the prefix as well.
450 if (strncmp(runtime_function_name
, "runtime.", 8) == 0
451 && strcmp(runtime_function_name
+ 8, name
.c_str()) == 0)
452 code
= static_cast<Runtime::Function
>(i
);