1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
// runtime.cc -- runtime functions called by generated code
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go-system.h"
#include "gogo.h"
#include "types.h"
#include "expressions.h"
#include "runtime.h"
// The frontend generates calls to various runtime functions. They
// are implemented in libgo/runtime. This is how the runtime
// functions are represented in the frontend. Note that there is
// currently nothing which ensures that the compiler's understanding
// of the runtime function matches the actual implementation in
// libgo/runtime.
// Parameter and result types used by runtime functions.
enum Runtime_function_type
{
// General indicator that value is not used.
RFT_VOID,
// Go untyped bool, C type _Bool.
RFT_BOOL,
// Go type *bool, C type _Bool*.
RFT_BOOLPTR,
// Go type int, C type intgo.
RFT_INT,
// Go type int32, C type int32_t.
RFT_INT32,
// Go type int64, C type int64_t.
RFT_INT64,
// Go type uint64, C type uint64_t.
RFT_UINT64,
// Go type uintptr, C type uintptr_t.
RFT_UINTPTR,
// Go type rune, C type int32_t.
RFT_RUNE,
// Go type float64, C type double.
RFT_FLOAT64,
// Go type complex64, C type __complex float.
RFT_COMPLEX64,
// Go type complex128, C type __complex double.
RFT_COMPLEX128,
// Go type string, C type struct __go_string.
RFT_STRING,
// Go type unsafe.Pointer, C type "void *".
RFT_POINTER,
// Go type []any, C type struct __go_open_array.
RFT_SLICE,
// Go type map[any]any, C type struct __go_map *.
RFT_MAP,
// Go type chan any, C type struct __go_channel *.
RFT_CHAN,
// Go type non-empty interface, C type struct __go_interface.
RFT_IFACE,
// Go type interface{}, C type struct __go_empty_interface.
RFT_EFACE,
// Go type func(unsafe.Pointer), C type void (*) (void *).
RFT_FUNC_PTR,
// Pointer to Go type descriptor.
RFT_TYPE,
// [2]string.
RFT_ARRAY2STRING,
// [3]string.
RFT_ARRAY3STRING,
// [4]string.
RFT_ARRAY4STRING,
// [5]string.
RFT_ARRAY5STRING,
NUMBER_OF_RUNTIME_FUNCTION_TYPES
};
// The Type structures for the runtime function types.
static Type* runtime_function_types[NUMBER_OF_RUNTIME_FUNCTION_TYPES];
// Get the Type for a Runtime_function_type code.
static Type*
runtime_function_type(Runtime_function_type bft)
{
go_assert(bft < NUMBER_OF_RUNTIME_FUNCTION_TYPES);
Type* any = Type::make_pointer_type(Type::make_void_type());
if (runtime_function_types[bft] == NULL)
{
const Location bloc = Linemap::predeclared_location();
Type* t;
switch (bft)
{
default:
case RFT_VOID:
go_unreachable();
case RFT_BOOL:
t = Type::make_boolean_type();
break;
case RFT_BOOLPTR:
t = Type::make_pointer_type(Type::lookup_bool_type());
break;
case RFT_INT:
t = Type::lookup_integer_type("int");
break;
case RFT_INT32:
t = Type::lookup_integer_type("int32");
break;
case RFT_INT64:
t = Type::lookup_integer_type("int64");
break;
case RFT_UINT64:
t = Type::lookup_integer_type("uint64");
break;
case RFT_RUNE:
t = Type::lookup_integer_type("int32");
break;
case RFT_UINTPTR:
t = Type::lookup_integer_type("uintptr");
break;
case RFT_FLOAT64:
t = Type::lookup_float_type("float64");
break;
case RFT_COMPLEX64:
t = Type::lookup_complex_type("complex64");
break;
case RFT_COMPLEX128:
t = Type::lookup_complex_type("complex128");
break;
case RFT_STRING:
t = Type::lookup_string_type();
break;
case RFT_POINTER:
t = Type::make_pointer_type(Type::make_void_type());
break;
case RFT_SLICE:
t = Type::make_array_type(any, NULL);
break;
case RFT_MAP:
t = Type::make_map_type(any, any, bloc);
break;
case RFT_CHAN:
t = Type::make_channel_type(true, true, any);
break;
case RFT_IFACE:
{
Typed_identifier_list* methods = new Typed_identifier_list();
Type* mtype = Type::make_function_type(NULL, NULL, NULL, bloc);
methods->push_back(Typed_identifier("x", mtype, bloc));
Interface_type* it = Type::make_interface_type(methods, bloc);
it->finalize_methods();
t = it;
}
break;
case RFT_EFACE:
t = Type::make_empty_interface_type(bloc);
break;
case RFT_FUNC_PTR:
{
Typed_identifier_list* param_types = new Typed_identifier_list();
Type* ptrtype = runtime_function_type(RFT_POINTER);
param_types->push_back(Typed_identifier("", ptrtype, bloc));
t = Type::make_function_type(NULL, param_types, NULL, bloc);
}
break;
case RFT_TYPE:
t = Type::make_type_descriptor_ptr_type();
break;
case RFT_ARRAY2STRING:
{
Array_type* at =
Type::make_array_type(Type::make_string_type(),
Expression::make_integer_ul(2, NULL,
bloc));
at->set_is_array_incomparable();
t = at;
}
break;
case RFT_ARRAY3STRING:
{
Array_type* at =
Type::make_array_type(Type::make_string_type(),
Expression::make_integer_ul(3, NULL,
bloc));
at->set_is_array_incomparable();
t = at;
}
break;
case RFT_ARRAY4STRING:
{
Array_type* at =
Type::make_array_type(Type::make_string_type(),
Expression::make_integer_ul(4, NULL,
bloc));
at->set_is_array_incomparable();
t = at;
}
break;
case RFT_ARRAY5STRING:
{
Array_type* at =
Type::make_array_type(Type::make_string_type(),
Expression::make_integer_ul(5, NULL,
bloc));
at->set_is_array_incomparable();
t = at;
}
break;
}
runtime_function_types[bft] = t;
}
return runtime_function_types[bft];
}
// Convert an expression to the type to pass to a runtime function.
static Expression*
convert_to_runtime_function_type(Runtime_function_type bft, Expression* e,
Location loc)
{
switch (bft)
{
default:
case RFT_VOID:
go_unreachable();
case RFT_BOOL:
case RFT_BOOLPTR:
case RFT_INT:
case RFT_INT32:
case RFT_INT64:
case RFT_UINT64:
case RFT_UINTPTR:
case RFT_RUNE:
case RFT_FLOAT64:
case RFT_COMPLEX64:
case RFT_COMPLEX128:
case RFT_STRING:
case RFT_POINTER:
case RFT_FUNC_PTR:
{
Type* t = runtime_function_type(bft);
if (!Type::are_identical(t, e->type(), true, NULL))
e = Expression::make_cast(t, e, loc);
return e;
}
case RFT_SLICE:
case RFT_MAP:
case RFT_CHAN:
case RFT_IFACE:
case RFT_EFACE:
case RFT_ARRAY2STRING:
case RFT_ARRAY3STRING:
case RFT_ARRAY4STRING:
case RFT_ARRAY5STRING:
return Expression::make_unsafe_cast(runtime_function_type(bft), e, loc);
case RFT_TYPE:
go_assert(e->type() == Type::make_type_descriptor_ptr_type());
return e;
}
}
// Convert all the types used for runtime functions to the backend
// representation.
void
Runtime::convert_types(Gogo* gogo)
{
for (int i = 0; i < static_cast<int>(NUMBER_OF_RUNTIME_FUNCTION_TYPES); ++i)
{
Type* t = runtime_function_types[i];
if (t != NULL && t->named_type() != NULL)
{
bool r = t->verify();
go_assert(r);
t->named_type()->convert(gogo);
}
}
}
// The type used to define a runtime function.
struct Runtime_function
{
// Function name.
const char* name;
// Parameter types. Never more than 6, as it happens. RFT_VOID if
// not used.
Runtime_function_type parameter_types[6];
// Result types. Never more than 2, as it happens. RFT_VOID if not
// used.
Runtime_function_type result_types[2];
};
static const Runtime_function runtime_functions[] =
{
#define DEF_GO_RUNTIME(CODE, NAME, PARAMS, RESULTS) { NAME, PARAMS, RESULTS } ,
#include "runtime.def"
#undef DEF_GO_RUNTIME
};
static Named_object*
runtime_function_declarations[Runtime::NUMBER_OF_FUNCTIONS];
// Get the declaration of a runtime function.
Named_object*
Runtime::runtime_declaration(Function code)
{
go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
if (runtime_function_declarations[code] == NULL)
{
const Runtime_function* pb = &runtime_functions[code];
Location bloc = Linemap::predeclared_location();
Typed_identifier_list* param_types = NULL;
if (pb->parameter_types[0] != RFT_VOID)
{
param_types = new Typed_identifier_list();
for (unsigned int i = 0;
i < (sizeof(pb->parameter_types)
/ sizeof (pb->parameter_types[0]));
i++)
{
if (pb->parameter_types[i] == RFT_VOID)
break;
Type* t = runtime_function_type(pb->parameter_types[i]);
param_types->push_back(Typed_identifier("", t, bloc));
}
}
Typed_identifier_list* result_types = NULL;
if (pb->result_types[0] != RFT_VOID)
{
result_types = new Typed_identifier_list();
for (unsigned int i = 0;
i < sizeof(pb->result_types) / sizeof(pb->result_types[0]);
i++)
{
if (pb->result_types[i] == RFT_VOID)
break;
Type* t = runtime_function_type(pb->result_types[i]);
result_types->push_back(Typed_identifier("", t, bloc));
}
}
Function_type* fntype = Type::make_function_type(NULL, param_types,
result_types, bloc);
const char* n = pb->name;
const char* n1 = strchr(n, '.');
if (n1 != NULL)
n = n1 + 1;
Named_object* no = Named_object::make_function_declaration(n, NULL,
fntype, bloc);
no->func_declaration_value()->set_asm_name(pb->name);
runtime_function_declarations[code] = no;
}
return runtime_function_declarations[code];
}
// Make a call to a runtime function.
Call_expression*
Runtime::make_call(Runtime::Function code, Location loc,
int param_count, ...)
{
go_assert(code < Runtime::NUMBER_OF_FUNCTIONS);
const Runtime_function* pb = &runtime_functions[code];
go_assert(static_cast<size_t>(param_count)
<= sizeof(pb->parameter_types) / sizeof(pb->parameter_types[0]));
Named_object* no = runtime_declaration(code);
Expression* func = Expression::make_func_reference(no, NULL, loc);
Expression_list* args = new Expression_list();
args->reserve(param_count);
va_list ap;
va_start(ap, param_count);
for (int i = 0; i < param_count; ++i)
{
Expression* e = va_arg(ap, Expression*);
Runtime_function_type rft = pb->parameter_types[i];
args->push_back(convert_to_runtime_function_type(rft, e, loc));
}
va_end(ap);
return Expression::make_call(func, args, false, loc);
}
// Get the runtime code for a named builtin function. This is used as a helper
// when creating function references for call expressions. Every reference to
// a builtin runtime function should have the associated runtime code. If the
// name is ambiguous and can refer to many runtime codes, return
// NUMBER_OF_FUNCTIONS.
Runtime::Function
Runtime::name_to_code(const std::string& name)
{
Function code = Runtime::NUMBER_OF_FUNCTIONS;
// Look through the known names for a match.
for (size_t i = 0; i < Runtime::NUMBER_OF_FUNCTIONS; i++)
{
const char* runtime_function_name = runtime_functions[i].name;
if (strcmp(runtime_function_name, name.c_str()) == 0)
code = static_cast<Runtime::Function>(i);
// The names in the table have "runtime." prefix. We may be
// called with a name without the prefix. Try matching
// without the prefix as well.
if (strncmp(runtime_function_name, "runtime.", 8) == 0
&& strcmp(runtime_function_name + 8, name.c_str()) == 0)
code = static_cast<Runtime::Function>(i);
}
return code;
}
|