fork/rust
git clone https://git.julia.blue/fork/rust
49cae55760d
1.44.0
1// Helper functions used only in tests 2 3#include <stdint.h> 4#include <assert.h> 5#include <stdarg.h> 6 7// These functions are used in the unit tests for C ABI calls. 8 9uint32_t 10rust_dbg_extern_identity_u32(uint32_t u) { 11 return u; 12} 13 14uint64_t 15rust_dbg_extern_identity_u64(uint64_t u) { 16 return u; 17} 18 19double 20rust_dbg_extern_identity_double(double u) { 21 return u; 22} 23 24char 25rust_dbg_extern_identity_u8(char u) { 26 return u; 27} 28 29typedef void *(*dbg_callback)(void*); 30 31void * 32rust_dbg_call(dbg_callback cb, void *data) { 33 return cb(data); 34} 35 36void rust_dbg_do_nothing() { } 37 38struct TwoU8s { 39 uint8_t one; 40 uint8_t two; 41}; 42 43struct TwoU8s 44rust_dbg_extern_return_TwoU8s() { 45 struct TwoU8s s; 46 s.one = 10; 47 s.two = 20; 48 return s; 49} 50 51struct TwoU8s 52rust_dbg_extern_identity_TwoU8s(struct TwoU8s u) { 53 return u; 54} 55 56struct TwoU16s { 57 uint16_t one; 58 uint16_t two; 59}; 60 61struct TwoU16s 62rust_dbg_extern_return_TwoU16s() { 63 struct TwoU16s s; 64 s.one = 10; 65 s.two = 20; 66 return s; 67} 68 69struct TwoU16s 70rust_dbg_extern_identity_TwoU16s(struct TwoU16s u) { 71 return u; 72} 73 74struct TwoU32s { 75 uint32_t one; 76 uint32_t two; 77}; 78 79struct TwoU32s 80rust_dbg_extern_return_TwoU32s() { 81 struct TwoU32s s; 82 s.one = 10; 83 s.two = 20; 84 return s; 85} 86 87struct TwoU32s 88rust_dbg_extern_identity_TwoU32s(struct TwoU32s u) { 89 return u; 90} 91 92struct TwoU64s { 93 uint64_t one; 94 uint64_t two; 95}; 96 97struct TwoU64s 98rust_dbg_extern_return_TwoU64s() { 99 struct TwoU64s s; 100 s.one = 10; 101 s.two = 20; 102 return s; 103} 104 105struct TwoU64s 106rust_dbg_extern_identity_TwoU64s(struct TwoU64s u) { 107 return u; 108} 109 110struct TwoDoubles { 111 double one; 112 double two; 113}; 114 115struct TwoDoubles 116rust_dbg_extern_identity_TwoDoubles(struct TwoDoubles u) { 117 return u; 118} 119 120struct ManyInts { 121 int8_t arg1; 122 int16_t arg2; 123 int32_t arg3; 124 int16_t arg4; 125 int8_t arg5; 126 struct TwoU8s arg6; 127}; 128 129// MSVC doesn't allow empty structs or unions 130#ifndef _MSC_VER 131struct Empty { 132}; 133 134void 135rust_dbg_extern_empty_struct(struct ManyInts v1, struct Empty e, struct ManyInts v2) { 136 assert(v1.arg1 == v2.arg1 + 1); 137 assert(v1.arg2 == v2.arg2 + 1); 138 assert(v1.arg3 == v2.arg3 + 1); 139 assert(v1.arg4 == v2.arg4 + 1); 140 assert(v1.arg5 == v2.arg5 + 1); 141 assert(v1.arg6.one == v2.arg6.one + 1); 142 assert(v1.arg6.two == v2.arg6.two + 1); 143} 144#endif 145 146intptr_t 147rust_get_test_int() { 148 return 1; 149} 150 151char * 152rust_get_null_ptr() { 153 return 0; 154} 155 156// Debug helpers strictly to verify ABI conformance. 157 158struct quad { 159 uint64_t a; 160 uint64_t b; 161 uint64_t c; 162 uint64_t d; 163}; 164 165struct floats { 166 double a; 167 uint8_t b; 168 double c; 169}; 170 171struct char_char_double { 172 uint8_t a; 173 uint8_t b; 174 double c; 175}; 176 177struct char_char_float { 178 uint8_t a; 179 uint8_t b; 180 float c; 181}; 182 183struct quad 184rust_dbg_abi_1(struct quad q) { 185 struct quad qq = { q.c + 1, 186 q.d - 1, 187 q.a + 1, 188 q.b - 1 }; 189 return qq; 190} 191 192struct floats 193rust_dbg_abi_2(struct floats f) { 194 struct floats ff = { f.c + 1.0, 195 0xff, 196 f.a - 1.0 }; 197 return ff; 198} 199 200struct char_char_double 201rust_dbg_abi_3(struct char_char_double a) { 202 struct char_char_double ccd = { a.a + 1, 203 a.b - 1, 204 a.c + 1.0 }; 205 return ccd; 206} 207 208struct char_char_float 209rust_dbg_abi_4(struct char_char_float a) { 210 struct char_char_float ccd = { a.a + 1, 211 a.b - 1, 212 a.c + 1.0 }; 213 return ccd; 214} 215 216 217int 218rust_dbg_static_mut = 3; 219 220void 221rust_dbg_static_mut_check_four() { 222 assert(rust_dbg_static_mut == 4); 223} 224 225struct S { 226 uint64_t x; 227 uint64_t y; 228 uint64_t z; 229}; 230 231uint64_t get_x(struct S s) { 232 return s.x; 233} 234 235uint64_t get_y(struct S s) { 236 return s.y; 237} 238 239uint64_t get_z(struct S s) { 240 return s.z; 241} 242 243uint64_t get_c_many_params(void *a, void *b, void *c, void *d, struct quad f) { 244 return f.c; 245} 246 247struct quad_floats { 248 float a; 249 float b; 250 float c; 251 float d; 252}; 253 254float get_c_exhaust_sysv64_ints( 255 void *a, 256 void *b, 257 void *c, 258 void *d, 259 void *e, 260 void *f, 261 // `f` used the last integer register, so `g` goes on the stack. 262 // It also used to bring the "count of available integer registers" down to 263 // `-1` which broke the next SSE-only aggregate argument (`h`) - see #62350. 264 void *g, 265 struct quad_floats h 266) { 267 return h.c; 268} 269 270// Calculates the average of `(x + y) / n` where x: i64, y: f64. There must be exactly n pairs 271// passed as variadic arguments. There are two versions of this function: the 272// variadic one, and the one that takes a `va_list`. 273double rust_valist_interesting_average(uint64_t n, va_list pairs) { 274 double sum = 0.0; 275 int i; 276 for(i = 0; i < n; i += 1) { 277 sum += (double)va_arg(pairs, int64_t); 278 sum += va_arg(pairs, double); 279 } 280 return sum / n; 281} 282 283double rust_interesting_average(uint64_t n, ...) { 284 double sum; 285 va_list pairs; 286 va_start(pairs, n); 287 sum = rust_valist_interesting_average(n, pairs); 288 va_end(pairs); 289 return sum; 290} 291 292int32_t rust_int8_to_int32(int8_t x) { 293 return (int32_t)x; 294} 295 296typedef union LARGE_INTEGER { 297 struct { 298 uint32_t LowPart; 299 uint32_t HighPart; 300 }; 301 struct { 302 uint32_t LowPart; 303 uint32_t HighPart; 304 } u; 305 uint64_t QuadPart; 306} LARGE_INTEGER; 307 308LARGE_INTEGER increment_all_parts(LARGE_INTEGER li) { 309 li.LowPart += 1; 310 li.HighPart += 1; 311 li.u.LowPart += 1; 312 li.u.HighPart += 1; 313 li.QuadPart += 1; 314 return li; 315} 316 317#if __SIZEOF_INT128__ == 16 318 319unsigned __int128 identity(unsigned __int128 a) { 320 return a; 321} 322 323__int128 square(__int128 a) { 324 return a * a; 325} 326 327__int128 sub(__int128 a, __int128 b) { 328 return a - b; 329} 330 331#endif 332 333#define OPTION_TAG_NONE (0) 334#define OPTION_TAG_SOME (1) 335 336struct U8TaggedEnumOptionU64 { 337 uint8_t tag; 338 union { 339 uint64_t some; 340 }; 341}; 342 343struct U8TaggedEnumOptionU64 344rust_dbg_new_some_u64(uint64_t some) { 345 struct U8TaggedEnumOptionU64 r = { 346 .tag = OPTION_TAG_SOME, 347 .some = some, 348 }; 349 return r; 350} 351 352struct U8TaggedEnumOptionU64 353rust_dbg_new_none_u64(void) { 354 struct U8TaggedEnumOptionU64 r = { 355 .tag = OPTION_TAG_NONE, 356 }; 357 return r; 358} 359 360int32_t 361rust_dbg_unpack_option_u64(struct U8TaggedEnumOptionU64 o, uint64_t *into) { 362 assert(into); 363 switch (o.tag) { 364 case OPTION_TAG_SOME: 365 *into = o.some; 366 return 1; 367 case OPTION_TAG_NONE: 368 return 0; 369 default: 370 assert(0 && "unexpected tag"); 371 } 372} 373 374struct U8TaggedEnumOptionU64U64 { 375 uint8_t tag; 376 union { 377 struct { 378 uint64_t a; 379 uint64_t b; 380 } some; 381 }; 382}; 383 384struct U8TaggedEnumOptionU64U64 385rust_dbg_new_some_u64u64(uint64_t a, uint64_t b) { 386 struct U8TaggedEnumOptionU64U64 r = { 387 .tag = OPTION_TAG_SOME, 388 .some = { .a = a, .b = b }, 389 }; 390 return r; 391} 392 393struct U8TaggedEnumOptionU64U64 394rust_dbg_new_none_u64u64(void) { 395 struct U8TaggedEnumOptionU64U64 r = { 396 .tag = OPTION_TAG_NONE, 397 }; 398 return r; 399} 400 401int32_t 402rust_dbg_unpack_option_u64u64(struct U8TaggedEnumOptionU64U64 o, uint64_t *a, uint64_t *b) { 403 assert(a); 404 assert(b); 405 switch (o.tag) { 406 case OPTION_TAG_SOME: 407 *a = o.some.a; 408 *b = o.some.b; 409 return 1; 410 case OPTION_TAG_NONE: 411 return 0; 412 default: 413 assert(0 && "unexpected tag"); 414 } 415}