github.com/goccy/go-jit@v0.0.0-20200514131505-ff78d45cf6af/internal/ccall/jit-type.c (about) 1 /* 2 * jit-type.c - Functions for manipulating type descriptors. 3 * 4 * Copyright (C) 2004 Southern Storm Software, Pty Ltd. 5 * 6 * This file is part of the libjit library. 7 * 8 * The libjit library is free software: you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public License 10 * as published by the Free Software Foundation, either version 2.1 of 11 * the License, or (at your option) any later version. 12 * 13 * The libjit library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with the libjit library. If not, see 20 * <http://www.gnu.org/licenses/>. 21 */ 22 23 #include "jit-internal.h" 24 #include "jit-apply-rules.h" 25 #include "jit-rules.h" 26 27 /*@ 28 29 @cindex jit-type.h 30 @tindex jit_type_t 31 32 The functions that are defined in @code{<jit/jit-type.h>} allow 33 the library user to create and manipulate objects that represent 34 native system types. For example, @code{jit_type_int} represents 35 the signed 32-bit integer type. 36 37 Each @code{jit_type_t} object represents a basic system type, 38 be it a primitive, a struct, a union, a pointer, or a function signature. 39 The library uses this information to lay out values in memory. 40 41 The following pre-defined types are available: 42 43 @table @code 44 @vindex jit_type_void 45 @item jit_type_void 46 Represents the @code{void} type. 47 48 @vindex jit_type_sbyte 49 @item jit_type_sbyte 50 Represents a signed 8-bit integer type. 51 52 @vindex jit_type_ubyte 53 @item jit_type_ubyte 54 Represents an unsigned 8-bit integer type. 55 56 @vindex jit_type_short 57 @item jit_type_short 58 Represents a signed 16-bit integer type. 59 60 @vindex jit_type_ushort 61 @item jit_type_ushort 62 Represents an unsigned 16-bit integer type. 63 64 @vindex jit_type_int 65 @item jit_type_int 66 Represents a signed 32-bit integer type. 67 68 @vindex jit_type_uint 69 @item jit_type_uint 70 Represents an unsigned 32-bit integer type. 71 72 @vindex jit_type_nint 73 @item jit_type_nint 74 Represents a signed integer type that has the same size and 75 alignment as a native pointer. 76 77 @vindex jit_type_nuint 78 @item jit_type_nuint 79 Represents an unsigned integer type that has the same size and 80 alignment as a native pointer. 81 82 @vindex jit_type_long 83 @item jit_type_long 84 Represents a signed 64-bit integer type. 85 86 @vindex jit_type_ulong 87 @item jit_type_ulong 88 Represents an unsigned 64-bit integer type. 89 90 @vindex jit_type_float32 91 @item jit_type_float32 92 Represents a 32-bit floating point type. 93 94 @vindex jit_type_float64 95 @item jit_type_float64 96 Represents a 64-bit floating point type. 97 98 @vindex jit_type_nfloat 99 @item jit_type_nfloat 100 Represents a floating point type that represents the greatest 101 precision supported on the native platform. 102 103 @vindex jit_type_void_ptr 104 @item jit_type_void_ptr 105 Represents the system's @code{void *} type. This can be used wherever 106 a native pointer type is required. 107 @end table 108 109 Type descriptors are reference counted. You can make a copy of a type 110 descriptor using the @code{jit_type_copy} function, and free the copy with 111 @code{jit_type_free}. 112 113 Some languages have special versions of the primitive numeric types 114 (e.g. boolean types, 16-bit Unicode character types, enumerations, etc). 115 If it is important to distinguish these special versions from the 116 numeric types, then you should use the @code{jit_type_create_tagged} 117 function below. 118 119 The following types correspond to the system types on the local 120 platform. i.e. @code{jit_type_sys_int} will be the same size as 121 @code{long} on the local platform, whereas @code{jit_type_long} is 122 always 64 bits in size. These types should not be used to compile 123 code that is intended to work identically on all platforms: 124 125 @table @code 126 @vindex jit_type_sys_bool 127 @item jit_type_sys_bool 128 Corresponds to the system @code{bool} type. 129 130 @vindex jit_type_sys_char 131 @item jit_type_sys_char 132 Corresponds to the system @code{char} type. This may be either signed 133 or unsigned, depending upon the underlying system. 134 135 @vindex jit_type_sys_schar 136 @item jit_type_sys_schar 137 Corresponds to the system @code{signed char} type. 138 139 @vindex jit_type_sys_uchar 140 @item jit_type_sys_uchar 141 Corresponds to the system @code{unsigned char} type. 142 143 @vindex jit_type_sys_short 144 @item jit_type_sys_short 145 Corresponds to the system @code{short} type. 146 147 @vindex jit_type_sys_ushort 148 @item jit_type_sys_ushort 149 Corresponds to the system @code{unsigned short} type. 150 151 @vindex jit_type_sys_int 152 @item jit_type_sys_int 153 Corresponds to the system @code{int} type. 154 155 @vindex jit_type_sys_uint 156 @item jit_type_sys_uint 157 Corresponds to the system @code{unsigned int} type. 158 159 @vindex jit_type_sys_long 160 @item jit_type_sys_long 161 Corresponds to the system @code{long} type. 162 163 @vindex jit_type_sys_ulong 164 @item jit_type_sys_ulong 165 Corresponds to the system @code{unsigned long} type. 166 167 @vindex jit_type_sys_longlong 168 @item jit_type_sys_longlong 169 Corresponds to the system @code{long long} type (@code{__int64} under Win32). 170 171 @vindex jit_type_sys_ulonglong 172 @item jit_type_sys_ulonglong 173 Corresponds to the system @code{unsigned long long} type 174 (@code{unsigned __int64} under Win32). 175 176 @vindex jit_type_sys_float 177 @item jit_type_sys_float 178 Corresponds to the system @code{float} type. 179 180 @vindex jit_type_sys_double 181 @item jit_type_sys_double 182 Corresponds to the system @code{double} type. 183 184 @vindex jit_type_sys_long_double 185 @item jit_type_sys_long_double 186 Corresponds to the system @code{long double} type. 187 @end table 188 189 @*/ 190 191 /* 192 * Pre-defined type descriptors. 193 */ 194 struct _jit_type const _jit_type_void_def = 195 {1, JIT_TYPE_VOID, 0, 1, 0, 1, 1}; 196 jit_type_t const jit_type_void = (jit_type_t)&_jit_type_void_def; 197 struct _jit_type const _jit_type_sbyte_def = 198 {1, JIT_TYPE_SBYTE, 0, 1, 0, sizeof(jit_sbyte), JIT_ALIGN_SBYTE}; 199 jit_type_t const jit_type_sbyte = (jit_type_t)&_jit_type_sbyte_def; 200 struct _jit_type const _jit_type_ubyte_def = 201 {1, JIT_TYPE_UBYTE, 0, 1, 0, sizeof(jit_ubyte), JIT_ALIGN_UBYTE}; 202 jit_type_t const jit_type_ubyte = (jit_type_t)&_jit_type_ubyte_def; 203 struct _jit_type const _jit_type_short_def = 204 {1, JIT_TYPE_SHORT, 0, 1, 0, sizeof(jit_short), JIT_ALIGN_SHORT}; 205 jit_type_t const jit_type_short = (jit_type_t)&_jit_type_short_def; 206 struct _jit_type const _jit_type_ushort_def = 207 {1, JIT_TYPE_USHORT, 0, 1, 0, sizeof(jit_ushort), JIT_ALIGN_USHORT}; 208 jit_type_t const jit_type_ushort = (jit_type_t)&_jit_type_ushort_def; 209 struct _jit_type const _jit_type_int_def = 210 {1, JIT_TYPE_INT, 0, 1, 0, sizeof(jit_int), JIT_ALIGN_INT}; 211 jit_type_t const jit_type_int = (jit_type_t)&_jit_type_int_def; 212 struct _jit_type const _jit_type_uint_def = 213 {1, JIT_TYPE_UINT, 0, 1, 0, sizeof(jit_uint), JIT_ALIGN_UINT}; 214 jit_type_t const jit_type_uint = (jit_type_t)&_jit_type_uint_def; 215 struct _jit_type const _jit_type_nint_def = 216 {1, JIT_TYPE_NINT, 0, 1, 0, sizeof(jit_nint), JIT_ALIGN_NINT}; 217 jit_type_t const jit_type_nint = (jit_type_t)&_jit_type_nint_def; 218 struct _jit_type const _jit_type_nuint_def = 219 {1, JIT_TYPE_NUINT, 0, 1, 0, sizeof(jit_nuint), JIT_ALIGN_NUINT}; 220 jit_type_t const jit_type_nuint = (jit_type_t)&_jit_type_nuint_def; 221 struct _jit_type const _jit_type_long_def = 222 {1, JIT_TYPE_LONG, 0, 1, 0, sizeof(jit_long), JIT_ALIGN_LONG}; 223 jit_type_t const jit_type_long = (jit_type_t)&_jit_type_long_def; 224 struct _jit_type const _jit_type_ulong_def = 225 {1, JIT_TYPE_ULONG, 0, 1, 0, sizeof(jit_ulong), JIT_ALIGN_ULONG}; 226 jit_type_t const jit_type_ulong = (jit_type_t)&_jit_type_ulong_def; 227 struct _jit_type const _jit_type_float32_def = 228 {1, JIT_TYPE_FLOAT32, 0, 1, 0, sizeof(jit_float32), JIT_ALIGN_FLOAT32}; 229 jit_type_t const jit_type_float32 = (jit_type_t)&_jit_type_float32_def; 230 struct _jit_type const _jit_type_float64_def = 231 {1, JIT_TYPE_FLOAT64, 0, 1, 0, sizeof(jit_float64), JIT_ALIGN_FLOAT64}; 232 jit_type_t const jit_type_float64 = (jit_type_t)&_jit_type_float64_def; 233 struct _jit_type const _jit_type_nfloat_def = 234 {1, JIT_TYPE_NFLOAT, 0, 1, 0, sizeof(jit_nfloat), JIT_ALIGN_NFLOAT}; 235 jit_type_t const jit_type_nfloat = (jit_type_t)&_jit_type_nfloat_def; 236 struct _jit_type const _jit_type_void_ptr_def = 237 {1, JIT_TYPE_PTR, 0, 1, 0, sizeof(void *), JIT_ALIGN_PTR, 238 (jit_type_t)&_jit_type_void_def}; 239 jit_type_t const jit_type_void_ptr = (jit_type_t)&_jit_type_void_ptr_def; 240 241 /* 242 * Type descriptors for the system "char", "int", "long", etc types. 243 * These are defined to one of the above values, tagged with a value 244 * that indicates which system type it is referring to. 245 */ 246 #define DECLARE_TAGGED(name,real,tag) \ 247 static struct jit_tagged_type const name##_tagged = \ 248 {{1, JIT_TYPE_FIRST_TAGGED + (tag), 0, 1, 0, 0, 0, \ 249 (jit_type_t)&_jit_type_##real}, 0, 0}; \ 250 jit_type_t const jit_type_##name = (jit_type_t)&name##_tagged 251 DECLARE_TAGGED(sys_bool, ubyte_def, JIT_TYPETAG_SYS_BOOL); 252 #ifdef __CHAR_UNSIGNED__ 253 DECLARE_TAGGED(sys_char, ubyte_def, JIT_TYPETAG_SYS_CHAR); 254 #else 255 DECLARE_TAGGED(sys_char, sbyte_def, JIT_TYPETAG_SYS_CHAR); 256 #endif 257 DECLARE_TAGGED(sys_schar, sbyte_def, JIT_TYPETAG_SYS_SCHAR); 258 DECLARE_TAGGED(sys_uchar, ubyte_def, JIT_TYPETAG_SYS_UCHAR); 259 #if SIZEOF_SHORT == 4 260 DECLARE_TAGGED(sys_short, int_def, JIT_TYPETAG_SYS_SHORT); 261 DECLARE_TAGGED(sys_ushort, uint_def, JIT_TYPETAG_SYS_USHORT); 262 #elif SIZEOF_SHORT == 8 263 DECLARE_TAGGED(sys_short, long_def, JIT_TYPETAG_SYS_SHORT); 264 DECLARE_TAGGED(sys_ushort, ulong_def, JIT_TYPETAG_SYS_USHORT); 265 #else 266 DECLARE_TAGGED(sys_short, short_def, JIT_TYPETAG_SYS_SHORT); 267 DECLARE_TAGGED(sys_ushort, ushort_def, JIT_TYPETAG_SYS_USHORT); 268 #endif 269 #if SIZEOF_INT == 8 270 DECLARE_TAGGED(sys_int, long_def, JIT_TYPETAG_SYS_INT); 271 DECLARE_TAGGED(sys_uint, ulong_def, JIT_TYPETAG_SYS_UINT); 272 #elif SIZEOF_INT == 2 273 DECLARE_TAGGED(sys_int, short_def, JIT_TYPETAG_SYS_INT); 274 DECLARE_TAGGED(sys_uint, ushort_def, JIT_TYPETAG_SYS_UINT); 275 #else 276 DECLARE_TAGGED(sys_int, int_def, JIT_TYPETAG_SYS_INT); 277 DECLARE_TAGGED(sys_uint, uint_def, JIT_TYPETAG_SYS_UINT); 278 #endif 279 #if SIZEOF_LONG == 8 280 DECLARE_TAGGED(sys_long, long_def, JIT_TYPETAG_SYS_LONG); 281 DECLARE_TAGGED(sys_ulong, ulong_def, JIT_TYPETAG_SYS_ULONG); 282 #elif SIZEOF_LONG == 2 283 DECLARE_TAGGED(sys_long, short_def, JIT_TYPETAG_SYS_LONG); 284 DECLARE_TAGGED(sys_ulong, ushort_def, JIT_TYPETAG_SYS_ULONG); 285 #else 286 DECLARE_TAGGED(sys_long, int_def, JIT_TYPETAG_SYS_LONG); 287 DECLARE_TAGGED(sys_ulong, uint_def, JIT_TYPETAG_SYS_ULONG); 288 #endif 289 #if SIZEOF_LONG_LONG == 8 || SIZEOF___INT64 == 8 290 DECLARE_TAGGED(sys_longlong, long_def, JIT_TYPETAG_SYS_LONGLONG); 291 DECLARE_TAGGED(sys_ulonglong, ulong_def, JIT_TYPETAG_SYS_ULONGLONG); 292 #elif SIZEOF_LONG_LONG == 4 293 DECLARE_TAGGED(sys_longlong, int_def, JIT_TYPETAG_SYS_LONGLONG); 294 DECLARE_TAGGED(sys_ulonglong, uint_def, JIT_TYPETAG_SYS_ULONGLONG); 295 #elif SIZEOF_LONG_LONG == 2 296 DECLARE_TAGGED(sys_longlong, short_def, JIT_TYPETAG_SYS_LONGLONG); 297 DECLARE_TAGGED(sys_ulonglong, ushort_def, JIT_TYPETAG_SYS_ULONGLONG); 298 #else 299 DECLARE_TAGGED(sys_longlong, long_def, JIT_TYPETAG_SYS_LONGLONG); 300 DECLARE_TAGGED(sys_ulonglong, ulong_def, JIT_TYPETAG_SYS_ULONGLONG); 301 #endif 302 DECLARE_TAGGED(sys_float, float32_def, JIT_TYPETAG_SYS_FLOAT); 303 DECLARE_TAGGED(sys_double, float64_def, JIT_TYPETAG_SYS_DOUBLE); 304 DECLARE_TAGGED(sys_long_double, nfloat_def, JIT_TYPETAG_SYS_LONGDOUBLE); 305 306 /* 307 * Special offset flags. 308 */ 309 #define JIT_OFFSET_IS_INTERNAL (((jit_nuint)1) << (sizeof(jit_nint) * 8 - 1)) 310 #define JIT_OFFSET_NOT_SET (~((jit_nuint)0)) 311 312 /* 313 * Layout flags. 314 */ 315 #define JIT_LAYOUT_NEEDED 1 316 #define JIT_LAYOUT_EXPLICIT_SIZE 2 317 #define JIT_LAYOUT_EXPLICIT_ALIGN 4 318 319 /* 320 * Perform layout on a structure or union type. 321 */ 322 static void perform_layout(jit_type_t type) 323 { 324 jit_nuint size = 0; 325 jit_nuint maxSize = 0; 326 jit_nuint maxAlign = 1; 327 jit_nuint alignLimit; 328 jit_nuint fieldSize; 329 jit_nuint fieldAlign; 330 unsigned int index; 331 332 /* Determine the alignment limit, if there is an override */ 333 #ifdef JIT_ALIGN_OVERRIDES 334 if((type->layout_flags & JIT_LAYOUT_EXPLICIT_ALIGN) != 0) 335 { 336 alignLimit = type->alignment; 337 } 338 else 339 #endif 340 { 341 alignLimit = 0; 342 } 343 344 /* Lay out all of the fields in this structure */ 345 for(index = 0; index < type->num_components; ++index) 346 { 347 /* Get the size and alignment of the field */ 348 fieldSize = jit_type_get_size(type->components[index].type); 349 fieldAlign = jit_type_get_alignment(type->components[index].type); 350 351 /* Clamp the alignment if we have a limit */ 352 if(alignLimit != 0 && fieldAlign > alignLimit) 353 { 354 fieldAlign = alignLimit; 355 } 356 357 /* Update the size and alignment values */ 358 if(type->kind == JIT_TYPE_STRUCT) 359 { 360 /* Perform layout for a struct type */ 361 if((type->components[index].offset & JIT_OFFSET_IS_INTERNAL) != 0) 362 { 363 /* Calculate the offset for the field automatically */ 364 if((size % fieldAlign) != 0) 365 { 366 size += fieldAlign - (size % fieldAlign); 367 } 368 type->components[index].offset = JIT_OFFSET_IS_INTERNAL | size; 369 size += fieldSize; 370 } 371 else 372 { 373 /* Use the explicitly-supplied offset for the field */ 374 size = type->components[index].offset + fieldSize; 375 } 376 if(size > maxSize) 377 { 378 maxSize = size; 379 } 380 } 381 else 382 { 383 /* Perform layout for a union type (offset is always zero) */ 384 type->components[index].offset = JIT_OFFSET_IS_INTERNAL | 0; 385 if((fieldSize % fieldAlign) != 0) 386 { 387 fieldSize += fieldAlign - (fieldSize % fieldAlign); 388 } 389 if(fieldSize > maxSize) 390 { 391 maxSize = fieldSize; 392 } 393 } 394 if(fieldAlign > maxAlign) 395 { 396 maxAlign = fieldAlign; 397 } 398 } 399 400 /* Align the full structure */ 401 if((maxSize % maxAlign) != 0) 402 { 403 maxSize += maxAlign - (maxSize % maxAlign); 404 } 405 406 /* Record the final size and alignment values */ 407 if((type->layout_flags & JIT_LAYOUT_EXPLICIT_SIZE) != 0) 408 { 409 if(maxSize > type->size) 410 { 411 type->size = maxSize; 412 } 413 } 414 else 415 { 416 type->size = maxSize; 417 } 418 if(maxAlign > type->alignment) 419 { 420 type->alignment = maxAlign; 421 } 422 } 423 424 /*@ 425 * @deftypefun jit_type_t jit_type_copy (jit_type_t @var{type}) 426 * Make a copy of the type descriptor @var{type} by increasing 427 * its reference count. 428 * @end deftypefun 429 @*/ 430 jit_type_t jit_type_copy(jit_type_t type) 431 { 432 if(!type || type->is_fixed) 433 { 434 return type; 435 } 436 ++(type->ref_count); 437 return type; 438 } 439 440 /*@ 441 * @deftypefun void jit_type_free (jit_type_t @var{type}) 442 * Free a type descriptor by decreasing its reference count. 443 * This function is safe to use on pre-defined types, which are 444 * never actually freed. 445 * @end deftypefun 446 @*/ 447 void jit_type_free(jit_type_t type) 448 { 449 unsigned int index; 450 if(!type || type->is_fixed) 451 { 452 return; 453 } 454 if(--(type->ref_count) != 0) 455 { 456 return; 457 } 458 jit_type_free(type->sub_type); 459 for(index = 0; index < type->num_components; ++index) 460 { 461 jit_type_free(type->components[index].type); 462 if(type->components[index].name) 463 { 464 jit_free(type->components[index].name); 465 } 466 } 467 if(type->kind >= JIT_TYPE_FIRST_TAGGED) 468 { 469 struct jit_tagged_type *tagged = (struct jit_tagged_type *)type; 470 if(tagged->free_func) 471 { 472 (*(tagged->free_func))(tagged->data); 473 } 474 } 475 jit_free(type); 476 } 477 478 static jit_type_t create_complex(int kind, jit_type_t *types, 479 unsigned int num, int incref) 480 { 481 jit_type_t type; 482 unsigned int index; 483 if(num <= 1) 484 { 485 type = jit_cnew(struct _jit_type); 486 } 487 else 488 { 489 type = (jit_type_t)jit_calloc 490 (1, sizeof(struct _jit_type) + 491 (num - 1) * sizeof(struct jit_component)); 492 } 493 if(!type) 494 { 495 return 0; 496 } 497 type->ref_count = 1; 498 type->kind = kind; 499 type->layout_flags = JIT_LAYOUT_NEEDED; 500 type->num_components = num; 501 for(index = 0; index < num; ++index) 502 { 503 if(incref) 504 { 505 type->components[index].type = jit_type_copy(types[index]); 506 } 507 else 508 { 509 type->components[index].type = types[index]; 510 } 511 type->components[index].offset = JIT_OFFSET_NOT_SET; 512 type->components[index].name = 0; 513 } 514 return type; 515 } 516 517 /*@ 518 * @deftypefun jit_type_t jit_type_create_struct (jit_type_t *@var{fields}, unsigned int @var{num_fields}, int @var{incref}) 519 * Create a type descriptor for a structure. Returns NULL if out of memory. 520 * If there are no fields, then the size of the structure will be zero. 521 * It is necessary to add a padding field if the language does not allow 522 * zero-sized structures. The reference counts on the field types are 523 * incremented if @var{incref} is non-zero. 524 * 525 * The @code{libjit} library does not provide any special support for 526 * implementing structure inheritance, where one structure extends the 527 * definition of another. The effect of inheritance can be achieved 528 * by always allocating the first field of a structure to be an instance 529 * of the inherited structure. Multiple inheritance can be supported 530 * by allocating several special fields at the front of an inheriting 531 * structure. 532 * 533 * Similarly, no special support is provided for vtables. The program 534 * is responsible for allocating an appropriate slot in a structure to 535 * contain the vtable pointer, and dereferencing it wherever necessary. 536 * The vtable will itself be a structure, containing signature types 537 * for each of the method slots. 538 * 539 * The choice not to provide special support for inheritance and vtables 540 * in @code{libjit} was deliberate. The layout of objects and vtables 541 * is highly specific to the language and virtual machine being emulated, 542 * and no single scheme can hope to capture all possibilities. 543 * @end deftypefun 544 @*/ 545 jit_type_t jit_type_create_struct(jit_type_t *fields, unsigned int num_fields, 546 int incref) 547 { 548 return create_complex(JIT_TYPE_STRUCT, fields, num_fields, incref); 549 } 550 551 /*@ 552 * @deftypefun jit_type_t jit_type_create_union (jit_type_t *@var{fields}, unsigned int @var{num_fields}, int @var{incref}) 553 * Create a type descriptor for a union. Returns NULL if out of memory. 554 * If there are no fields, then the size of the union will be zero. 555 * It is necessary to add a padding field if the language does not allow 556 * zero-sized unions. The reference counts on the field types are 557 * incremented if @var{incref} is non-zero. 558 * @end deftypefun 559 @*/ 560 jit_type_t jit_type_create_union(jit_type_t *fields, unsigned int num_fields, 561 int incref) 562 { 563 return create_complex(JIT_TYPE_UNION, fields, num_fields, incref); 564 } 565 566 /*@ 567 * @deftypefun jit_type_t jit_type_create_signature (jit_abi_t @var{abi}, jit_type_t @var{return_type}, jit_type_t *@var{params}, unsigned int @var{num_params}, int @var{incref}) 568 * Create a type descriptor for a function signature. Returns NULL if out 569 * of memory. The reference counts on the component types are incremented 570 * if @var{incref} is non-zero. 571 * 572 * When used as a structure or union field, function signatures are laid 573 * out like pointers. That is, they represent a pointer to a function 574 * that has the specified parameters and return type. 575 * 576 * @tindex jit_abi_t 577 * The @var{abi} parameter specifies the Application Binary Interface (ABI) 578 * that the function uses. It may be one of the following values: 579 * 580 * @table @code 581 * @vindex jit_abi_cdecl 582 * @item jit_abi_cdecl 583 * Use the native C ABI definitions of the underlying platform. 584 * 585 * @vindex jit_abi_vararg 586 * @item jit_abi_vararg 587 * Use the native C ABI definitions of the underlying platform, 588 * and allow for an optional list of variable argument parameters. 589 * 590 * @vindex jit_abi_stdcall 591 * @item jit_abi_stdcall 592 * Use the Win32 STDCALL ABI definitions, whereby the callee pops 593 * its arguments rather than the caller. If the platform does 594 * not support this type of ABI, then @code{jit_abi_stdcall} will be 595 * identical to @code{jit_abi_cdecl}. 596 * 597 * @vindex jit_abi_fastcall 598 * @item jit_abi_fastcall 599 * Use the Win32 FASTCALL ABI definitions, whereby the callee pops 600 * its arguments rather than the caller, and the first two word 601 * arguments are passed in ECX and EDX. If the platform does 602 * not support this type of ABI, then @code{jit_abi_fastcall} will be 603 * identical to @code{jit_abi_cdecl}. 604 * @end table 605 * @end deftypefun 606 @*/ 607 jit_type_t jit_type_create_signature(jit_abi_t abi, jit_type_t return_type, 608 jit_type_t *params, 609 unsigned int num_params, int incref) 610 { 611 jit_type_t type; 612 type = create_complex(JIT_TYPE_SIGNATURE, params, num_params, incref); 613 if(type) 614 { 615 type->abi = (int)abi; 616 type->layout_flags = 0; 617 type->size = 0; 618 type->alignment = JIT_ALIGN_PTR; 619 if(incref) 620 { 621 type->sub_type = jit_type_copy(return_type); 622 } 623 else 624 { 625 type->sub_type = return_type; 626 } 627 } 628 return type; 629 } 630 631 /*@ 632 * @deftypefun jit_type_t jit_type_create_pointer (jit_type_t @var{type}, int @var{incref}) 633 * Create a type descriptor for a pointer to another type. Returns NULL 634 * if out of memory. The reference count on @var{type} is incremented if 635 * @var{incref} is non-zero. 636 * @end deftypefun 637 @*/ 638 jit_type_t jit_type_create_pointer(jit_type_t type, int incref) 639 { 640 jit_type_t ntype; 641 if(type == jit_type_void) 642 { 643 return jit_type_void_ptr; 644 } 645 if((ntype = jit_cnew(struct _jit_type)) == 0) 646 { 647 return 0; 648 } 649 ntype->ref_count = 1; 650 ntype->kind = JIT_TYPE_PTR; 651 ntype->size = sizeof(void *); 652 ntype->alignment = JIT_ALIGN_PTR; 653 if(incref) 654 { 655 ntype->sub_type = jit_type_copy(type); 656 } 657 else 658 { 659 ntype->sub_type = type; 660 } 661 return ntype; 662 } 663 664 /*@ 665 * @deftypefun jit_type_t jit_type_create_tagged (jit_type_t @var{type}, int @var{kind}, void *@var{data}, jit_meta_free_func @var{free_func}, int @var{incref}) 666 * Tag a type with some additional user data. Tagging is typically used by 667 * higher-level programs to embed extra information about a type that 668 * @code{libjit} itself does not support. 669 * 670 * As an example, a language might have a 16-bit Unicode character type 671 * and a 16-bit unsigned integer type that are distinct types, even though 672 * they share the same fundamental representation (@code{jit_ushort}). 673 * Tagging allows the program to distinguish these two types, when 674 * it is necessary to do so, without affecting @code{libjit}'s ability 675 * to compile the code efficiently. 676 * 677 * The @var{kind} is a small positive integer value that the program 678 * can use to distinguish multiple tag types. The @var{data} pointer is 679 * the actual data that you wish to store. And @var{free_func} is a 680 * function that is used to free @var{data} when the type is freed 681 * with @code{jit_type_free}. 682 * 683 * If you need to store more than one piece of information, you can 684 * tag a type multiple times. The order in which multiple tags are 685 * applied is irrelevant to @code{libjit}, although it may be relevant 686 * to the higher-level program. 687 * 688 * Tag kinds of 10000 or greater are reserved for @code{libjit} itself. 689 * The following special tag kinds are currently provided in the 690 * base implementation: 691 * 692 * @table @code 693 * @vindex JIT_TYPETAG_NAME 694 * @item JIT_TYPETAG_NAME 695 * The @var{data} pointer is a @code{char *} string indicating a friendly 696 * name to display for the type. 697 * 698 * @vindex JIT_TYPETAG_STRUCT_NAME 699 * @vindex JIT_TYPETAG_UNION_NAME 700 * @vindex JIT_TYPETAG_ENUM_NAME 701 * @item JIT_TYPETAG_STRUCT_NAME 702 * @itemx JIT_TYPETAG_UNION_NAME 703 * @itemx JIT_TYPETAG_ENUM_NAME 704 * The @var{data} pointer is a @code{char *} string indicating a friendly 705 * name to display for a @code{struct}, @code{union}, or @code{enum} type. 706 * This is for languages like C that have separate naming scopes for 707 * typedef's and structures. 708 * 709 * @vindex JIT_TYPETAG_CONST 710 * @item JIT_TYPETAG_CONST 711 * The underlying value is assumed to have @code{const} semantics. 712 * The @code{libjit} library doesn't enforce such semantics: it is 713 * up to the front-end to only use constant values in appopriate contexts. 714 * 715 * @vindex JIT_TYPETAG_VOLATILE 716 * @item JIT_TYPETAG_VOLATILE 717 * The underlying value is assumed to be volatile. The @code{libjit} 718 * library will automatically call @code{jit_value_set_volatile} when a 719 * value is constructed using this type. 720 * 721 * @vindex JIT_TYPETAG_REFERENCE 722 * @item JIT_TYPETAG_REFERENCE 723 * The underlying value is a pointer, but it is assumed to refer to a 724 * pass-by-reference parameter. 725 * 726 * @vindex JIT_TYPETAG_OUTPUT 727 * @item JIT_TYPETAG_OUTPUT 728 * This is similar to @code{JIT_TYPETAG_REFERENCE}, except that the 729 * underlying parameter is assumed to be output-only. 730 * 731 * @vindex JIT_TYPETAG_RESTRICT 732 * @item JIT_TYPETAG_RESTRICT 733 * The underlying type is marked as @code{restrict}. Normally ignored. 734 * 735 * @vindex JIT_TYPETAG_SYS_BOOL 736 * @vindex JIT_TYPETAG_SYS_CHAR 737 * @vindex JIT_TYPETAG_SYS_SCHAR 738 * @vindex JIT_TYPETAG_SYS_UCHAR 739 * @vindex JIT_TYPETAG_SYS_SHORT 740 * @vindex JIT_TYPETAG_SYS_USHORT 741 * @vindex JIT_TYPETAG_SYS_INT 742 * @vindex JIT_TYPETAG_SYS_UINT 743 * @vindex JIT_TYPETAG_SYS_LONG 744 * @vindex JIT_TYPETAG_SYS_ULONG 745 * @vindex JIT_TYPETAG_SYS_LONGLONG 746 * @vindex JIT_TYPETAG_SYS_ULONGLONG 747 * @vindex JIT_TYPETAG_SYS_FLOAT 748 * @vindex JIT_TYPETAG_SYS_DOUBLE 749 * @vindex JIT_TYPETAG_SYS_LONGDOUBLE 750 * @item JIT_TYPETAG_SYS_BOOL 751 * @itemx JIT_TYPETAG_SYS_CHAR 752 * @itemx JIT_TYPETAG_SYS_SCHAR 753 * @itemx JIT_TYPETAG_SYS_UCHAR 754 * @itemx JIT_TYPETAG_SYS_SHORT 755 * @itemx JIT_TYPETAG_SYS_USHORT 756 * @itemx JIT_TYPETAG_SYS_INT 757 * @itemx JIT_TYPETAG_SYS_UINT 758 * @itemx JIT_TYPETAG_SYS_LONG 759 * @itemx JIT_TYPETAG_SYS_ULONG 760 * @itemx JIT_TYPETAG_SYS_LONGLONG 761 * @itemx JIT_TYPETAG_SYS_ULONGLONG 762 * @itemx JIT_TYPETAG_SYS_FLOAT 763 * @itemx JIT_TYPETAG_SYS_DOUBLE 764 * @itemx JIT_TYPETAG_SYS_LONGDOUBLE 765 * Used to mark types that we know for a fact correspond to the system 766 * C types of the corresponding names. This is primarily used to distinguish 767 * system types like @code{int} and @code{long} types on 32-bit platforms 768 * when it is necessary to do so. The @code{jit_type_sys_xxx} values are 769 * all tagged in this manner. 770 * @end table 771 * @end deftypefun 772 @*/ 773 jit_type_t jit_type_create_tagged(jit_type_t type, int kind, void *data, 774 jit_meta_free_func free_func, int incref) 775 { 776 struct jit_tagged_type *ntype; 777 if((ntype = jit_cnew(struct jit_tagged_type)) == 0) 778 { 779 return 0; 780 } 781 ntype->type.ref_count = 1; 782 ntype->type.kind = JIT_TYPE_FIRST_TAGGED + kind; 783 ntype->type.size = 0; 784 ntype->type.alignment = 1; 785 if(incref) 786 { 787 ntype->type.sub_type = jit_type_copy(type); 788 } 789 else 790 { 791 ntype->type.sub_type = type; 792 } 793 ntype->data = data; 794 ntype->free_func = free_func; 795 return &(ntype->type); 796 } 797 798 /*@ 799 * @deftypefun int jit_type_set_names (jit_type_t @var{type}, char **@var{names}, unsigned int @var{num_names}) 800 * Set the field or parameter names for @var{type}. Returns zero 801 * if there is insufficient memory to set the names. 802 * 803 * Normally fields are accessed via their index. Field names are a 804 * convenience for front ends that prefer to use names to indices. 805 * @end deftypefun 806 @*/ 807 int jit_type_set_names(jit_type_t type, char **names, unsigned int num_names) 808 { 809 char *temp; 810 if(!type || type->is_fixed || !names) 811 { 812 return 1; 813 } 814 if(type->kind == JIT_TYPE_STRUCT || type->kind == JIT_TYPE_UNION || 815 type->kind == JIT_TYPE_SIGNATURE) 816 { 817 if(num_names > type->num_components) 818 { 819 num_names = type->num_components; 820 } 821 while(num_names > 0) 822 { 823 --num_names; 824 if(type->components[num_names].name) 825 { 826 jit_free(type->components[num_names].name); 827 type->components[num_names].name = 0; 828 } 829 if(names[num_names]) 830 { 831 temp = jit_strdup(names[num_names]); 832 if(!temp) 833 { 834 return 0; 835 } 836 type->components[num_names].name = temp; 837 } 838 } 839 } 840 return 1; 841 } 842 843 /*@ 844 * @deftypefun void jit_type_set_size_and_alignment (jit_type_t @var{type}, jit_nint @var{size}, jit_nint @var{alignment}) 845 * Set the size and alignment information for a structure or union 846 * type. Use this for performing explicit type layout. Normally 847 * the size is computed automatically. Ignored if not a 848 * structure or union type. Setting either value to -1 will cause 849 * that value to be computed automatically. 850 * @end deftypefun 851 @*/ 852 void jit_type_set_size_and_alignment(jit_type_t type, jit_nint size, 853 jit_nint alignment) 854 { 855 if(!type) 856 { 857 return; 858 } 859 if(type->kind == JIT_TYPE_STRUCT || type->kind == JIT_TYPE_UNION) 860 { 861 type->size = (jit_nuint)size; 862 type->alignment = (jit_nuint)alignment; 863 if(size != -1) 864 { 865 type->layout_flags |= JIT_LAYOUT_EXPLICIT_SIZE; 866 } 867 if(alignment != -1) 868 { 869 type->layout_flags |= JIT_LAYOUT_EXPLICIT_ALIGN; 870 } 871 type->layout_flags |= JIT_LAYOUT_NEEDED; 872 } 873 } 874 875 /*@ 876 * @deftypefun void jit_type_set_offset (jit_type_t @var{type}, unsigned int @var{field_index}, jit_nuint @var{offset}) 877 * Set the offset of a specific structure field. Use this for 878 * performing explicit type layout. Normally the offset is 879 * computed automatically. Ignored if not a structure type, 880 * or the field index is out of range. 881 * @end deftypefun 882 @*/ 883 void jit_type_set_offset(jit_type_t type, unsigned int field_index, 884 jit_nuint offset) 885 { 886 if(!type || field_index >= type->num_components) 887 { 888 return; 889 } 890 if(type->kind == JIT_TYPE_STRUCT || type->kind == JIT_TYPE_UNION) 891 { 892 type->components[field_index].offset = offset; 893 type->layout_flags |= JIT_LAYOUT_NEEDED; 894 } 895 } 896 897 /*@ 898 * @deftypefun int jit_type_get_kind (jit_type_t @var{type}) 899 * Get a value that indicates the kind of @var{type}. This allows 900 * callers to quickly classify a type to determine how it should be 901 * handled further. 902 * 903 * @table @code 904 * @vindex JIT_TYPE_INVALID 905 * @item JIT_TYPE_INVALID 906 * The value of the @var{type} parameter is NULL. 907 * 908 * @vindex JIT_TYPE_VOID 909 * @item JIT_TYPE_VOID 910 * The type is @code{jit_type_void}. 911 * 912 * @vindex JIT_TYPE_SBYTE 913 * @item JIT_TYPE_SBYTE 914 * The type is @code{jit_type_sbyte}. 915 * 916 * @vindex JIT_TYPE_UBYTE 917 * @item JIT_TYPE_UBYTE 918 * The type is @code{jit_type_ubyte}. 919 * 920 * @vindex JIT_TYPE_SHORT 921 * @item JIT_TYPE_SHORT 922 * The type is @code{jit_type_short}. 923 * 924 * @vindex JIT_TYPE_USHORT 925 * @item JIT_TYPE_USHORT 926 * The type is @code{jit_type_ushort}. 927 * 928 * @vindex JIT_TYPE_INT 929 * @item JIT_TYPE_INT 930 * The type is @code{jit_type_int}. 931 * 932 * @vindex JIT_TYPE_UINT 933 * @item JIT_TYPE_UINT 934 * The type is @code{jit_type_uint}. 935 * 936 * @vindex JIT_TYPE_NINT 937 * @item JIT_TYPE_NINT 938 * The type is @code{jit_type_nint}. 939 * 940 * @vindex JIT_TYPE_NUINT 941 * @item JIT_TYPE_NUINT 942 * The type is @code{jit_type_nuint}. 943 * 944 * @vindex JIT_TYPE_LONG 945 * @item JIT_TYPE_LONG 946 * The type is @code{jit_type_long}. 947 * 948 * @vindex JIT_TYPE_ULONG 949 * @item JIT_TYPE_ULONG 950 * The type is @code{jit_type_ulong}. 951 * 952 * @vindex JIT_TYPE_FLOAT32 953 * @item JIT_TYPE_FLOAT32 954 * The type is @code{jit_type_float32}. 955 * 956 * @vindex JIT_TYPE_FLOAT64 957 * @item JIT_TYPE_FLOAT64 958 * The type is @code{jit_type_float64}. 959 * 960 * @vindex JIT_TYPE_NFLOAT 961 * @item JIT_TYPE_NFLOAT 962 * The type is @code{jit_type_nfloat}. 963 * 964 * @vindex JIT_TYPE_STRUCT 965 * @item JIT_TYPE_STRUCT 966 * The type is the result of calling @code{jit_type_create_struct}. 967 * 968 * @vindex JIT_TYPE_UNION 969 * @item JIT_TYPE_UNION 970 * The type is the result of calling @code{jit_type_create_union}. 971 * 972 * @vindex JIT_TYPE_SIGNATURE 973 * @item JIT_TYPE_SIGNATURE 974 * The type is the result of calling @code{jit_type_create_signature}. 975 * 976 * @vindex JIT_TYPE_PTR 977 * @item JIT_TYPE_PTR 978 * The type is the result of calling @code{jit_type_create_pointer}. 979 * @end table 980 * 981 * @vindex JIT_TYPE_FIRST_TAGGED 982 * If this function returns @code{JIT_TYPE_FIRST_TAGGED} or higher, 983 * then the type is tagged and its tag kind is the return value minus 984 * @code{JIT_TYPE_FIRST_TAGGED}. That is, the following two expressions 985 * will be identical if @var{type} is tagged: 986 * 987 * @example 988 * jit_type_get_tagged_kind(type) 989 * jit_type_get_kind(type) - JIT_TYPE_FIRST_TAGGED 990 * @end example 991 * @end deftypefun 992 @*/ 993 int jit_type_get_kind(jit_type_t type) 994 { 995 if(!type) 996 { 997 return JIT_TYPE_INVALID; 998 } 999 else 1000 { 1001 return type->kind; 1002 } 1003 } 1004 1005 /*@ 1006 * @deftypefun jit_nuint jit_type_get_size (jit_type_t @var{type}) 1007 * Get the size of a type in bytes. 1008 * @end deftypefun 1009 @*/ 1010 jit_nuint jit_type_get_size(jit_type_t type) 1011 { 1012 if(!type) 1013 { 1014 return 0; 1015 } 1016 if(type->kind == JIT_TYPE_SIGNATURE) 1017 { 1018 /* The "size" field is used for argument size, not type size, 1019 so we ignore it and return the real size here */ 1020 return sizeof(void *); 1021 } 1022 else if(type->kind >= JIT_TYPE_FIRST_TAGGED) 1023 { 1024 return jit_type_get_size(type->sub_type); 1025 } 1026 if((type->layout_flags & JIT_LAYOUT_NEEDED) != 0) 1027 { 1028 perform_layout(type); 1029 } 1030 return type->size; 1031 } 1032 1033 /*@ 1034 * @deftypefun jit_nuint jit_type_get_alignment (jit_type_t @var{type}) 1035 * Get the alignment of a type. An alignment value of 2 indicates 1036 * that the type should be aligned on a two-byte boundary, for example. 1037 * @end deftypefun 1038 @*/ 1039 jit_nuint jit_type_get_alignment(jit_type_t type) 1040 { 1041 if(!type) 1042 { 1043 return 0; 1044 } 1045 if(type->kind >= JIT_TYPE_FIRST_TAGGED) 1046 { 1047 return jit_type_get_alignment(type->sub_type); 1048 } 1049 if((type->layout_flags & JIT_LAYOUT_NEEDED) != 0) 1050 { 1051 perform_layout(type); 1052 } 1053 return type->alignment; 1054 } 1055 1056 /*@ 1057 * @deftypefun jit_nuint jit_type_best_alignment (void) 1058 * Get the best alignment value for this platform. 1059 * @end deftypefun 1060 @*/ 1061 jit_nuint 1062 jit_type_best_alignment(void) 1063 { 1064 return JIT_BEST_ALIGNMENT; 1065 } 1066 1067 /*@ 1068 * @deftypefun {unsigned int} jit_type_num_fields (jit_type_t @var{type}) 1069 * Get the number of fields in a structure or union type. 1070 * @end deftypefun 1071 @*/ 1072 unsigned int jit_type_num_fields(jit_type_t type) 1073 { 1074 if(!type || 1075 (type->kind != JIT_TYPE_STRUCT && type->kind != JIT_TYPE_UNION)) 1076 { 1077 return 0; 1078 } 1079 else 1080 { 1081 return type->num_components; 1082 } 1083 } 1084 1085 /*@ 1086 * @deftypefun jit_type_t jit_type_get_field (jit_type_t @var{type}, unsigned int @var{field_index}) 1087 * Get the type of a specific field within a structure or union. 1088 * Returns NULL if not a structure or union, or the index is out of range. 1089 * @end deftypefun 1090 @*/ 1091 jit_type_t jit_type_get_field(jit_type_t type, unsigned int field_index) 1092 { 1093 if(!type || field_index >= type->num_components) 1094 { 1095 return 0; 1096 } 1097 if(type->kind == JIT_TYPE_STRUCT || type->kind == JIT_TYPE_UNION) 1098 { 1099 return type->components[field_index].type; 1100 } 1101 return 0; 1102 } 1103 1104 /*@ 1105 * @deftypefun jit_nuint jit_type_get_offset (jit_type_t @var{type}, unsigned int @var{field_index}) 1106 * Get the offset of a specific field within a structure. 1107 * Returns zero if not a structure, or the index is out of range, 1108 * so this is safe to use on non-structure types. 1109 * @end deftypefun 1110 @*/ 1111 jit_nuint jit_type_get_offset(jit_type_t type, unsigned int field_index) 1112 { 1113 if(!type || field_index >= type->num_components) 1114 { 1115 return 0; 1116 } 1117 if(type->kind != JIT_TYPE_STRUCT && type->kind != JIT_TYPE_UNION) 1118 { 1119 return 0; 1120 } 1121 if((type->layout_flags & JIT_LAYOUT_NEEDED) != 0) 1122 { 1123 perform_layout(type); 1124 } 1125 return type->components[field_index].offset & ~JIT_OFFSET_IS_INTERNAL; 1126 } 1127 1128 /*@ 1129 * @deftypefun {const char *} jit_type_get_name (jit_type_t @var{type}, unsigned int @var{index}) 1130 * Get the name of a structure, union, or signature field/parameter. 1131 * Returns NULL if not a structure, union, or signature, the index 1132 * is out of range, or there is no name associated with the component. 1133 * @end deftypefun 1134 @*/ 1135 const char *jit_type_get_name(jit_type_t type, unsigned int index) 1136 { 1137 if(!type || index >= type->num_components) 1138 { 1139 return 0; 1140 } 1141 else 1142 { 1143 return type->components[index].name; 1144 } 1145 } 1146 1147 /*@ 1148 * @deftypefun {unsigned int} jit_type_find_name (jit_type_t @var{type}, const char *@var{name}) 1149 * Find the field/parameter index for a particular name. Returns 1150 * @code{JIT_INVALID_NAME} if the name was not present. 1151 * @end deftypefun 1152 @*/ 1153 unsigned int jit_type_find_name(jit_type_t type, const char *name) 1154 { 1155 unsigned int index; 1156 if(!type || !name) 1157 { 1158 return JIT_INVALID_NAME; 1159 } 1160 if(type->kind == JIT_TYPE_STRUCT || type->kind == JIT_TYPE_UNION || 1161 type->kind == JIT_TYPE_SIGNATURE) 1162 { 1163 for(index = 0; index < type->num_components; ++index) 1164 { 1165 if(type->components[index].name && 1166 !jit_strcmp(type->components[index].name, name)) 1167 { 1168 return index; 1169 } 1170 } 1171 } 1172 return JIT_INVALID_NAME; 1173 } 1174 1175 /*@ 1176 * @deftypefun {unsigned int} jit_type_num_params (jit_type_t @var{type}) 1177 * Get the number of parameters in a signature type. 1178 * @end deftypefun 1179 @*/ 1180 unsigned int jit_type_num_params(jit_type_t type) 1181 { 1182 if(!type || type->kind != JIT_TYPE_SIGNATURE) 1183 { 1184 return 0; 1185 } 1186 else 1187 { 1188 return type->num_components; 1189 } 1190 } 1191 1192 /*@ 1193 * @deftypefun jit_type_t jit_type_get_return (jit_type_t @var{type}) 1194 * Get the return type from a signature type. Returns NULL if 1195 * not a signature type. 1196 * @end deftypefun 1197 @*/ 1198 jit_type_t jit_type_get_return(jit_type_t type) 1199 { 1200 if(type) 1201 { 1202 if(type->kind == JIT_TYPE_SIGNATURE) 1203 { 1204 return type->sub_type; 1205 } 1206 } 1207 return 0; 1208 } 1209 1210 /*@ 1211 * @deftypefun jit_type_t jit_type_get_param (jit_type_t @var{type}, unsigned int @var{param_index}) 1212 * Get a specific parameter from a signature type. Returns NULL 1213 * if not a signature type or the index is out of range. 1214 * @end deftypefun 1215 @*/ 1216 jit_type_t jit_type_get_param(jit_type_t type, unsigned int param_index) 1217 { 1218 if(!type || param_index >= type->num_components) 1219 { 1220 return 0; 1221 } 1222 if(type->kind == JIT_TYPE_SIGNATURE) 1223 { 1224 return type->components[param_index].type; 1225 } 1226 return 0; 1227 } 1228 1229 /*@ 1230 * @deftypefun jit_abi_t jit_type_get_abi (jit_type_t @var{type}) 1231 * Get the ABI code from a signature type. Returns @code{jit_abi_cdecl} 1232 * if not a signature type. 1233 * @end deftypefun 1234 @*/ 1235 jit_abi_t jit_type_get_abi(jit_type_t type) 1236 { 1237 if(type) 1238 { 1239 return (jit_abi_t)(type->abi); 1240 } 1241 else 1242 { 1243 return jit_abi_cdecl; 1244 } 1245 } 1246 1247 /*@ 1248 * @deftypefun jit_type_t jit_type_get_ref (jit_type_t @var{type}) 1249 * Get the type that is referred to by a pointer type. Returns NULL 1250 * if not a pointer type. 1251 * @end deftypefun 1252 @*/ 1253 jit_type_t jit_type_get_ref(jit_type_t type) 1254 { 1255 if(type) 1256 { 1257 if(type->kind == JIT_TYPE_PTR) 1258 { 1259 return type->sub_type; 1260 } 1261 } 1262 return 0; 1263 } 1264 1265 /*@ 1266 * @deftypefun jit_type_t jit_type_get_tagged_type (jit_type_t @var{type}) 1267 * Get the type that underlies a tagged type. Returns NULL 1268 * if not a tagged type. 1269 * @end deftypefun 1270 @*/ 1271 jit_type_t jit_type_get_tagged_type(jit_type_t type) 1272 { 1273 if(type && type->kind >= JIT_TYPE_FIRST_TAGGED) 1274 { 1275 return type->sub_type; 1276 } 1277 else 1278 { 1279 return 0; 1280 } 1281 } 1282 1283 /*@ 1284 * @deftypefun void jit_type_set_tagged_type (jit_type_t @var{type}, jit_type_t @var{underlying}, int @var{incref}) 1285 * Set the type that underlies a tagged type. Ignored if @var{type} 1286 * is not a tagged type. If @var{type} already has an underlying 1287 * type, then the original is freed. The reference count on @var{underlying} 1288 * is incremented if @var{incref} is non-zero. 1289 * 1290 * This function is typically used to flesh out the body of a 1291 * forward-declared type. The tag is used as a placeholder 1292 * until the definition can be located. 1293 * @end deftypefun 1294 @*/ 1295 void jit_type_set_tagged_type(jit_type_t type, jit_type_t underlying, 1296 int incref) 1297 { 1298 if(type && type->kind >= JIT_TYPE_FIRST_TAGGED) 1299 { 1300 if(type->sub_type != underlying) 1301 { 1302 jit_type_free(type->sub_type); 1303 if(incref) 1304 { 1305 type->sub_type = jit_type_copy(underlying); 1306 } 1307 else 1308 { 1309 type->sub_type = underlying; 1310 } 1311 } 1312 } 1313 } 1314 1315 /*@ 1316 * @deftypefun int jit_type_get_tagged_kind (jit_type_t @var{type}) 1317 * Get the kind of tag that is applied to a tagged type. Returns -1 1318 * if not a tagged type. 1319 * @end deftypefun 1320 @*/ 1321 int jit_type_get_tagged_kind(jit_type_t type) 1322 { 1323 if(type && type->kind >= JIT_TYPE_FIRST_TAGGED) 1324 { 1325 return type->kind - JIT_TYPE_FIRST_TAGGED; 1326 } 1327 else 1328 { 1329 return -1; 1330 } 1331 } 1332 1333 /*@ 1334 * @deftypefun {void *} jit_type_get_tagged_data (jit_type_t @var{type}) 1335 * Get the user data is associated with a tagged type. Returns NULL 1336 * if not a tagged type. 1337 * @end deftypefun 1338 @*/ 1339 void *jit_type_get_tagged_data(jit_type_t type) 1340 { 1341 if(type && type->kind >= JIT_TYPE_FIRST_TAGGED) 1342 { 1343 return ((struct jit_tagged_type *)type)->data; 1344 } 1345 else 1346 { 1347 return 0; 1348 } 1349 } 1350 1351 /*@ 1352 * @deftypefun void jit_type_set_tagged_data (jit_type_t @var{type}, void *@var{data}, jit_meta_free_func @var{free_func}) 1353 * Set the user data is associated with a tagged type. The original data, 1354 * if any, is freed. 1355 * @end deftypefun 1356 @*/ 1357 void jit_type_set_tagged_data(jit_type_t type, void *data, 1358 jit_meta_free_func free_func) 1359 { 1360 if(type && type->kind >= JIT_TYPE_FIRST_TAGGED) 1361 { 1362 struct jit_tagged_type *tagged = (struct jit_tagged_type *)type; 1363 if(tagged->data != data) 1364 { 1365 if(tagged->free_func) 1366 { 1367 (*(tagged->free_func))(tagged->data); 1368 } 1369 tagged->data = data; 1370 tagged->free_func = free_func; 1371 } 1372 } 1373 } 1374 1375 /*@ 1376 * @deftypefun int jit_type_is_primitive (jit_type_t @var{type}) 1377 * Determine if a type is primitive. 1378 * @end deftypefun 1379 @*/ 1380 int jit_type_is_primitive(jit_type_t type) 1381 { 1382 if(type) 1383 { 1384 return (type->kind <= JIT_TYPE_MAX_PRIMITIVE); 1385 } 1386 else 1387 { 1388 return 0; 1389 } 1390 } 1391 1392 /*@ 1393 * @deftypefun int jit_type_is_struct (jit_type_t @var{type}) 1394 * Determine if a type is a structure. 1395 * @end deftypefun 1396 @*/ 1397 int jit_type_is_struct(jit_type_t type) 1398 { 1399 if(type) 1400 { 1401 return (type->kind == JIT_TYPE_STRUCT); 1402 } 1403 else 1404 { 1405 return 0; 1406 } 1407 } 1408 1409 /*@ 1410 * @deftypefun int jit_type_is_union (jit_type_t @var{type}) 1411 * Determine if a type is a union. 1412 * @end deftypefun 1413 @*/ 1414 int jit_type_is_union(jit_type_t type) 1415 { 1416 if(type) 1417 { 1418 return (type->kind == JIT_TYPE_UNION); 1419 } 1420 else 1421 { 1422 return 0; 1423 } 1424 } 1425 1426 /*@ 1427 * @deftypefun int jit_type_is_signature (jit_type_t @var{type}) 1428 * Determine if a type is a function signature. 1429 * @end deftypefun 1430 @*/ 1431 int jit_type_is_signature(jit_type_t type) 1432 { 1433 if(type) 1434 { 1435 return (type->kind == JIT_TYPE_SIGNATURE); 1436 } 1437 else 1438 { 1439 return 0; 1440 } 1441 } 1442 1443 /*@ 1444 * @deftypefun int jit_type_is_pointer (jit_type_t @var{type}) 1445 * Determine if a type is a pointer. 1446 * @end deftypefun 1447 @*/ 1448 int jit_type_is_pointer(jit_type_t type) 1449 { 1450 if(type) 1451 { 1452 return (type->kind == JIT_TYPE_PTR); 1453 } 1454 else 1455 { 1456 return 0; 1457 } 1458 } 1459 1460 /*@ 1461 * @deftypefun int jit_type_is_tagged (jit_type_t @var{type}) 1462 * Determine if a type is a tagged type. 1463 * @end deftypefun 1464 @*/ 1465 int jit_type_is_tagged(jit_type_t type) 1466 { 1467 if(type) 1468 { 1469 return (type->kind >= JIT_TYPE_FIRST_TAGGED); 1470 } 1471 else 1472 { 1473 return 0; 1474 } 1475 } 1476 1477 /*@ 1478 * @deftypefun jit_type_t jit_type_remove_tags (jit_type_t @var{type}) 1479 * Remove tags from a type, and return the underlying type. 1480 * This is different from normalization, which also collapses 1481 * native types to their basic numeric counterparts. 1482 * @end deftypefun 1483 @*/ 1484 jit_type_t 1485 jit_type_remove_tags(jit_type_t type) 1486 { 1487 while(type && type->kind >= JIT_TYPE_FIRST_TAGGED) 1488 { 1489 type = type->sub_type; 1490 } 1491 return type; 1492 } 1493 1494 /*@ 1495 * @deftypefun jit_type_t jit_type_normalize (jit_type_t @var{type}) 1496 * Normalize a type to its basic numeric form. e.g. "jit_type_nint" is 1497 * turned into "jit_type_int" or "jit_type_long", depending upon 1498 * the underlying platform. Pointers are normalized like "jit_type_nint". 1499 * If the type does not have a normalized form, it is left unchanged. 1500 * 1501 * Normalization is typically used prior to applying a binary numeric 1502 * instruction, to make it easier to determine the common type. 1503 * It will also remove tags from the specified type. 1504 * @end deftypefun 1505 @*/ 1506 jit_type_t 1507 jit_type_normalize(jit_type_t type) 1508 { 1509 /* Remove any tags that are attached to the type */ 1510 type = jit_type_remove_tags(type); 1511 if(!type) 1512 { 1513 return type; 1514 } 1515 1516 /* Put a platform-dependent type to its basic form */ 1517 if(type == jit_type_nint || type->kind == JIT_TYPE_PTR || 1518 type->kind == JIT_TYPE_SIGNATURE) 1519 { 1520 #ifdef JIT_NATIVE_INT32 1521 return jit_type_int; 1522 #else 1523 return jit_type_long; 1524 #endif 1525 } 1526 else if(type == jit_type_nuint) 1527 { 1528 #ifdef JIT_NATIVE_INT32 1529 return jit_type_uint; 1530 #else 1531 return jit_type_ulong; 1532 #endif 1533 } 1534 else if(type == jit_type_nfloat) 1535 { 1536 if(sizeof(jit_nfloat) == sizeof(jit_float64)) 1537 { 1538 return jit_type_float64; 1539 } 1540 else if(sizeof(jit_nfloat) == sizeof(jit_float32)) 1541 { 1542 return jit_type_float32; 1543 } 1544 } 1545 return type; 1546 } 1547 1548 /*@ 1549 * @deftypefun jit_type_t jit_type_promote_int (jit_type_t @var{type}) 1550 * If @var{type} is @code{jit_type_sbyte} or @code{jit_type_short}, 1551 * then return @code{jit_type_int}. If @var{type} is 1552 * @code{jit_type_ubyte} or @code{jit_type_ushort}, then return 1553 * @code{jit_type_uint}. Otherwise return @var{type} as-is. 1554 * @end deftypefun 1555 @*/ 1556 jit_type_t jit_type_promote_int(jit_type_t type) 1557 { 1558 if(type == jit_type_sbyte || type == jit_type_short) 1559 { 1560 return jit_type_int; 1561 } 1562 else if(type == jit_type_ubyte || type == jit_type_ushort) 1563 { 1564 return jit_type_uint; 1565 } 1566 else 1567 { 1568 return type; 1569 } 1570 } 1571 1572 /*@ 1573 * @deftypefun int jit_type_return_via_pointer (jit_type_t @var{type}) 1574 * Determine if a type should be returned via a pointer if it appears 1575 * as the return type in a signature. 1576 * @end deftypefun 1577 @*/ 1578 int 1579 jit_type_return_via_pointer(jit_type_t type) 1580 { 1581 extern unsigned char const _jit_apply_return_in_reg[]; 1582 unsigned int size; 1583 1584 /* Remove tags if any */ 1585 type = jit_type_remove_tags(type); 1586 1587 /* Only structure and union types require special handling */ 1588 if(!jit_type_is_struct(type) && !jit_type_is_union(type)) 1589 { 1590 return 0; 1591 } 1592 1593 /* Determine if the structure can be returned in a register */ 1594 size = jit_type_get_size(type); 1595 if(size >= 1 && size <= 64) 1596 { 1597 if((_jit_apply_return_in_reg[(size - 1) / 8] & 1598 (1 << ((size - 1) % 8))) != 0) 1599 { 1600 return 0; 1601 } 1602 } 1603 return 1; 1604 } 1605 1606 /*@ 1607 * @deftypefun int jit_type_has_tag (jit_type_t @var{type}, int @var{kind}) 1608 * Determine if @var{type} has a specific kind of tag. This will 1609 * resolve multiple levels of tagging. 1610 * @end deftypefun 1611 @*/ 1612 int jit_type_has_tag(jit_type_t type, int kind) 1613 { 1614 while(type != 0 && type->kind >= JIT_TYPE_FIRST_TAGGED) 1615 { 1616 if(type->kind == (JIT_TYPE_FIRST_TAGGED + kind)) 1617 { 1618 return 1; 1619 } 1620 type = type->sub_type; 1621 } 1622 return 0; 1623 }