| 1 |
/* vim: ts=4 sw=4 sts=4 et tw=78 |
| 2 |
* Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved. |
| 3 |
* Portions copyright (c) 2011 James R. McKaskill. |
| 4 |
* |
| 5 |
* This source code is licensed under the BSD-style license found in the |
| 6 |
* LICENSE file in the root directory of this source tree. An additional grant |
| 7 |
* of patent rights can be found in the PATENTS file in the same directory. |
| 8 |
*/ |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdint.h> |
| 11 |
#include <inttypes.h> |
| 12 |
#include <stddef.h> |
| 13 |
#include <stdbool.h> |
| 14 |
#include <stdarg.h> |
| 15 |
|
| 16 |
#ifdef _WIN32 |
| 17 |
#include <windows.h> |
| 18 |
#else |
| 19 |
#include <errno.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifndef _MSC_VER |
| 23 |
#include <complex.h> |
| 24 |
#define HAVE_COMPLEX |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifdef __cplusplus |
| 28 |
# define EXTERN_C extern "C" |
| 29 |
#else |
| 30 |
# define EXTERN_C extern |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef _WIN32 |
| 34 |
#define EXPORT EXTERN_C __declspec(dllexport) |
| 35 |
#elif defined __GNUC__ |
| 36 |
#define EXPORT EXTERN_C __attribute__((visibility("default"))) |
| 37 |
#else |
| 38 |
#define EXPORT EXTERN_C |
| 39 |
#endif |
| 40 |
|
| 41 |
enum e8 { |
| 42 |
FOO8, |
| 43 |
BAR8, |
| 44 |
}; |
| 45 |
enum e16 { |
| 46 |
FOO16 = 1 << 8, |
| 47 |
BAR16, |
| 48 |
BIG16 = 1 << 14, |
| 49 |
}; |
| 50 |
enum e32 { |
| 51 |
FOO32 = 1 << 16, |
| 52 |
BAR32, |
| 53 |
BIG32 = 1 << 30, |
| 54 |
}; |
| 55 |
|
| 56 |
EXPORT bool have_complex(); |
| 57 |
|
| 58 |
bool have_complex() |
| 59 |
{ |
| 60 |
#ifdef HAVE_COMPLEX |
| 61 |
return 1; |
| 62 |
#else |
| 63 |
return 0; |
| 64 |
#endif |
| 65 |
} |
| 66 |
|
| 67 |
EXPORT bool is_msvc; |
| 68 |
|
| 69 |
bool is_msvc = |
| 70 |
#ifdef _MSC_VER |
| 71 |
1; |
| 72 |
#else |
| 73 |
0; |
| 74 |
#endif |
| 75 |
|
| 76 |
EXPORT int test_pow(int v); |
| 77 |
int test_pow(int v) |
| 78 |
{ return v * v; } |
| 79 |
|
| 80 |
#define ADD(TYPE, NAME) \ |
| 81 |
EXPORT TYPE NAME(TYPE a, TYPE b); \ |
| 82 |
TYPE NAME(TYPE a, TYPE b) { return a + b; } |
| 83 |
|
| 84 |
ADD(int8_t, add_i8) |
| 85 |
ADD(uint8_t, add_u8) |
| 86 |
ADD(int16_t, add_i16) |
| 87 |
ADD(uint16_t, add_u16) |
| 88 |
ADD(int32_t, add_i32) |
| 89 |
ADD(uint32_t, add_u32) |
| 90 |
ADD(int64_t, add_i64) |
| 91 |
ADD(uint64_t, add_u64) |
| 92 |
ADD(double, add_d) |
| 93 |
ADD(float, add_f) |
| 94 |
#ifdef HAVE_COMPLEX |
| 95 |
ADD(double complex, add_dc) |
| 96 |
ADD(float complex, add_fc) |
| 97 |
#endif |
| 98 |
|
| 99 |
EXPORT enum e8 inc_e8(enum e8 v); |
| 100 |
EXPORT enum e16 inc_e16(enum e16 v); |
| 101 |
EXPORT enum e32 inc_e32(enum e32 v); |
| 102 |
enum e8 inc_e8(enum e8 v) {return v+1;} |
| 103 |
enum e16 inc_e16(enum e16 v) {return v+1;} |
| 104 |
enum e32 inc_e32(enum e32 v) {return v+1;} |
| 105 |
|
| 106 |
EXPORT _Bool not_b(_Bool v); |
| 107 |
EXPORT _Bool not_b2(_Bool v); |
| 108 |
|
| 109 |
_Bool not_b(_Bool v) {return !v;} |
| 110 |
_Bool not_b2(_Bool v) {return !v;} |
| 111 |
|
| 112 |
#define PRINT(TYPE, NAME, FORMAT) \ |
| 113 |
EXPORT int NAME(char* buf, TYPE val); \ |
| 114 |
int NAME(char* buf, TYPE val) {return sprintf(buf, "%" FORMAT, val);} |
| 115 |
|
| 116 |
PRINT(int8_t, print_i8, PRId8) |
| 117 |
PRINT(uint8_t, print_u8, PRIu8) |
| 118 |
PRINT(int16_t, print_i16, PRId16) |
| 119 |
PRINT(uint16_t, print_u16, PRIu16) |
| 120 |
PRINT(int32_t, print_i32, PRId32) |
| 121 |
PRINT(uint32_t, print_u32, PRIu32) |
| 122 |
PRINT(int64_t, print_i64, PRId64) |
| 123 |
PRINT(uint64_t, print_u64, PRIu64) |
| 124 |
PRINT(double, print_d, "g") |
| 125 |
PRINT(float, print_f, "g") |
| 126 |
PRINT(const char*, print_s, "s") |
| 127 |
PRINT(void*, print_p, "p") |
| 128 |
PRINT(enum e8, print_e8, "d") |
| 129 |
PRINT(enum e16, print_e16, "d") |
| 130 |
PRINT(enum e32, print_e32, "d") |
| 131 |
|
| 132 |
#ifdef HAVE_COMPLEX |
| 133 |
EXPORT int print_dc(char* buf, double complex val); |
| 134 |
EXPORT int print_fc(char* buf, float complex val); |
| 135 |
int print_dc(char* buf, double complex val) {return sprintf(buf, "%g+%gi", creal(val), cimag(val));} |
| 136 |
int print_fc(char* buf, float complex val) {return sprintf(buf, "%g+%gi", creal(val), cimag(val));} |
| 137 |
#endif |
| 138 |
|
| 139 |
EXPORT int print_b(char* buf, _Bool val); |
| 140 |
EXPORT int print_b2(char* buf, _Bool val); |
| 141 |
int print_b(char* buf, _Bool val) {return sprintf(buf, "%s", val ? "true" : "false");} |
| 142 |
int print_b2(char* buf, _Bool val) {return sprintf(buf, "%s", val ? "true" : "false");} |
| 143 |
|
| 144 |
EXPORT bool (*ret_fp(bool (*val)(bool)))(bool); |
| 145 |
bool (*ret_fp(bool (*val)(bool)))(bool) |
| 146 |
{return val;} |
| 147 |
|
| 148 |
#define OFFSETOF(STRUCT, MEMBER) ((int) ((char*) &STRUCT.MEMBER - (char*) &S - 1)) |
| 149 |
|
| 150 |
#define ALIGN_UP(VALUE, ALIGNMENT, SUFFIX) \ |
| 151 |
struct align_##ALIGNMENT##_##SUFFIX { \ |
| 152 |
char pad; \ |
| 153 |
VALUE; \ |
| 154 |
}; \ |
| 155 |
EXPORT int print_align_##ALIGNMENT##_##SUFFIX(char* buf, struct align_##ALIGNMENT##_##SUFFIX* p); \ |
| 156 |
int print_align_##ALIGNMENT##_##SUFFIX(char* buf, struct align_##ALIGNMENT##_##SUFFIX* p) { \ |
| 157 |
struct {char ch; struct align_##ALIGNMENT##_##SUFFIX v;} s; \ |
| 158 |
int off = sprintf(buf, "size %d offset %d align %d value ", \ |
| 159 |
(int) sizeof(s.v), \ |
| 160 |
(int) (((char*) &p->v) - (char*) p), \ |
| 161 |
(int) (((char*) &s.v) - (char*) &s)); \ |
| 162 |
return print_##SUFFIX(buf+off, p->v); \ |
| 163 |
} |
| 164 |
|
| 165 |
#ifdef HAVE_COMPLEX |
| 166 |
#define COMPLEX_ALIGN(ALIGNMENT, ATTR) \ |
| 167 |
ALIGN_UP(ATTR(double complex), ALIGNMENT, dc) \ |
| 168 |
ALIGN_UP(ATTR(float complex), ALIGNMENT, fc) |
| 169 |
#else |
| 170 |
#define COMPLEX_ALIGN(ALIGNMENT, ATTR) |
| 171 |
#endif |
| 172 |
|
| 173 |
/* MSVC doesn't support __declspec(aligned(#)) on enums see C4329 */ |
| 174 |
#define ENUM_ALIGN2(ALIGNMENT, ATTR) \ |
| 175 |
ALIGN_UP(ATTR(enum e8), ALIGNMENT, e8) \ |
| 176 |
ALIGN_UP(ATTR(enum e16), ALIGNMENT, e16) \ |
| 177 |
ALIGN_UP(ATTR(enum e32), ALIGNMENT, e32) \ |
| 178 |
|
| 179 |
#ifdef _MSC_VER |
| 180 |
#define ENUM_ALIGN(ALIGNMENT, ATTR) |
| 181 |
#else |
| 182 |
#define ENUM_ALIGN(ALIGNMENT, ATTR) ENUM_ALIGN2(ALIGNMENT, ATTR) |
| 183 |
#endif |
| 184 |
|
| 185 |
#define ALIGN2(ALIGNMENT, ATTR) \ |
| 186 |
ALIGN_UP(ATTR(uint16_t), ALIGNMENT, u16) \ |
| 187 |
ALIGN_UP(ATTR(uint32_t), ALIGNMENT, u32) \ |
| 188 |
ALIGN_UP(ATTR(uint64_t), ALIGNMENT, u64) \ |
| 189 |
ALIGN_UP(ATTR(float), ALIGNMENT, f) \ |
| 190 |
ALIGN_UP(ATTR(double), ALIGNMENT, d) \ |
| 191 |
ALIGN_UP(ATTR(const char*), ALIGNMENT, s) \ |
| 192 |
ALIGN_UP(ATTR(void*), ALIGNMENT, p) \ |
| 193 |
ALIGN_UP(ATTR(_Bool), ALIGNMENT, b) \ |
| 194 |
ALIGN_UP(ATTR(_Bool), ALIGNMENT, b2) \ |
| 195 |
ENUM_ALIGN(ALIGNMENT, ATTR) \ |
| 196 |
COMPLEX_ALIGN(ALIGNMENT, ATTR) |
| 197 |
|
| 198 |
#define NO_ATTR(TYPE) TYPE v |
| 199 |
|
| 200 |
#ifdef _MSC_VER |
| 201 |
#define ALIGN_NO_ATTR(ALIGNMENT) \ |
| 202 |
ALIGN2(ALIGNMENT, NO_ATTR) \ |
| 203 |
ENUM_ALIGN2(ALIGNMENT, NO_ATTR) |
| 204 |
#else |
| 205 |
#define ALIGN_NO_ATTR(ALIGNMENT) \ |
| 206 |
ALIGN2(ALIGNMENT, NO_ATTR) |
| 207 |
#endif |
| 208 |
|
| 209 |
ALIGN_NO_ATTR(0) |
| 210 |
|
| 211 |
#pragma pack(push) |
| 212 |
#pragma pack(1) |
| 213 |
ALIGN_NO_ATTR(1) |
| 214 |
#pragma pack(2) |
| 215 |
ALIGN_NO_ATTR(2) |
| 216 |
#pragma pack(4) |
| 217 |
ALIGN_NO_ATTR(4) |
| 218 |
#pragma pack(8) |
| 219 |
ALIGN_NO_ATTR(8) |
| 220 |
#pragma pack(16) |
| 221 |
ALIGN_NO_ATTR(16) |
| 222 |
#pragma pack(pop) |
| 223 |
|
| 224 |
#ifdef _MSC_VER |
| 225 |
#define ATTR_(TYPE, ALIGN) __declspec(align(ALIGN)) TYPE v |
| 226 |
#else |
| 227 |
#define ATTR_(TYPE, ALIGN) TYPE v __attribute__((aligned(ALIGN))) |
| 228 |
#endif |
| 229 |
|
| 230 |
#define ATTR1(TYPE) ATTR_(TYPE, 1) |
| 231 |
#define ATTR2(TYPE) ATTR_(TYPE, 2) |
| 232 |
#define ATTR4(TYPE) ATTR_(TYPE, 4) |
| 233 |
#define ATTR8(TYPE) ATTR_(TYPE, 8) |
| 234 |
#define ATTR16(TYPE) ATTR_(TYPE, 16) |
| 235 |
|
| 236 |
#define ATTR_DEF(TYPE) TYPE v __attribute__((aligned)) |
| 237 |
|
| 238 |
ALIGN2(attr_1, ATTR1) |
| 239 |
ALIGN2(attr_2, ATTR2) |
| 240 |
ALIGN2(attr_4, ATTR4) |
| 241 |
ALIGN2(attr_8, ATTR8) |
| 242 |
ALIGN2(attr_16, ATTR16) |
| 243 |
|
| 244 |
#ifndef _MSC_VER |
| 245 |
ALIGN2(attr_def, ATTR_DEF) |
| 246 |
#endif |
| 247 |
|
| 248 |
#ifdef _MSC_VER |
| 249 |
#define alignof(type) __alignof(type) |
| 250 |
#else |
| 251 |
#define alignof(type) __alignof__(type) |
| 252 |
#endif |
| 253 |
|
| 254 |
EXPORT int max_alignment(); |
| 255 |
|
| 256 |
int max_alignment() |
| 257 |
{ return alignof(struct align_attr_16_p); } |
| 258 |
|
| 259 |
/* bit_fields1.cpp */ |
| 260 |
/* compile with: /LD */ |
| 261 |
struct Date { |
| 262 |
unsigned short nWeekDay : 3; /* 0..7 (3 bits) */ |
| 263 |
unsigned short nMonthDay : 6; /* 0..31 (6 bits) */ |
| 264 |
unsigned short nMonth : 5; /* 0..12 (5 bits) */ |
| 265 |
unsigned short nYear : 8; /* 0..100 (8 bits) */ |
| 266 |
}; |
| 267 |
|
| 268 |
EXPORT int print_date(size_t* sz, size_t* align, char* buf, struct Date* d); |
| 269 |
|
| 270 |
int print_date(size_t* sz, size_t* align, char* buf, struct Date* d) { |
| 271 |
*sz = sizeof(struct Date); |
| 272 |
*align = alignof(struct Date); |
| 273 |
return sprintf(buf, "%d %d %d %d", d->nWeekDay, d->nMonthDay, d->nMonth, d->nYear); |
| 274 |
} |
| 275 |
|
| 276 |
/* bit_fields2.cpp */ |
| 277 |
/* compile with: /LD */ |
| 278 |
struct Date2 { |
| 279 |
unsigned nWeekDay : 3; /* 0..7 (3 bits) */ |
| 280 |
unsigned nMonthDay : 6; /* 0..31 (6 bits) */ |
| 281 |
unsigned : 0; /* Force alignment to next boundary. */ |
| 282 |
unsigned nMonth : 5; /* 0..12 (5 bits) */ |
| 283 |
unsigned nYear : 8; /* 0..100 (8 bits) */ |
| 284 |
}; |
| 285 |
|
| 286 |
EXPORT int print_date2(size_t* sz, size_t* align, char* buf, struct Date2* d); |
| 287 |
|
| 288 |
int print_date2(size_t* sz, size_t* align, char* buf, struct Date2* d) { |
| 289 |
*sz = sizeof(struct Date2); |
| 290 |
*align = alignof(struct Date2); |
| 291 |
return sprintf(buf, "%d %d %d %d", d->nWeekDay, d->nMonthDay, d->nMonth, d->nYear); |
| 292 |
} |
| 293 |
|
| 294 |
// Examples from SysV X86 ABI |
| 295 |
struct sysv1 { |
| 296 |
int j:5; |
| 297 |
int k:6; |
| 298 |
int m:7; |
| 299 |
}; |
| 300 |
|
| 301 |
EXPORT int print_sysv1(size_t* sz, size_t* align, char* buf, struct sysv1* s); |
| 302 |
|
| 303 |
int print_sysv1(size_t* sz, size_t* align, char* buf, struct sysv1* s) { |
| 304 |
*sz = sizeof(struct sysv1); |
| 305 |
*align = alignof(struct sysv1); |
| 306 |
return sprintf(buf, "%d %d %d", s->j, s->k, s->m); |
| 307 |
} |
| 308 |
|
| 309 |
struct sysv2 { |
| 310 |
short s:9; |
| 311 |
int j:9; |
| 312 |
char c; |
| 313 |
short t:9; |
| 314 |
short u:9; |
| 315 |
char d; |
| 316 |
}; |
| 317 |
|
| 318 |
EXPORT int print_sysv2(size_t* sz, size_t* align, char* buf, struct sysv2* s); |
| 319 |
|
| 320 |
int print_sysv2(size_t* sz, size_t* align, char* buf, struct sysv2* s) { |
| 321 |
*sz = sizeof(struct sysv2); |
| 322 |
*align = alignof(struct sysv2); |
| 323 |
return sprintf(buf, "%d %d %d %d %d %d", s->s, s->j, s->c, s->t, s->u, s->d); |
| 324 |
} |
| 325 |
|
| 326 |
struct sysv3 { |
| 327 |
char c; |
| 328 |
short s:8; |
| 329 |
}; |
| 330 |
|
| 331 |
EXPORT int print_sysv3(size_t* sz, size_t* align, char* buf, struct sysv3* s); |
| 332 |
|
| 333 |
int print_sysv3(size_t* sz, size_t* align, char* buf, struct sysv3* s) { |
| 334 |
*sz = sizeof(struct sysv3); |
| 335 |
*align = alignof(struct sysv3); |
| 336 |
return sprintf(buf, "%d %d", s->c, s->s); |
| 337 |
} |
| 338 |
|
| 339 |
union sysv4 { |
| 340 |
char c; |
| 341 |
short s:8; |
| 342 |
}; |
| 343 |
|
| 344 |
EXPORT int print_sysv4(size_t* sz, size_t* align, char* buf, union sysv4* s); |
| 345 |
|
| 346 |
int print_sysv4(size_t* sz, size_t* align, char* buf, union sysv4* s) { |
| 347 |
*sz = sizeof(union sysv4); |
| 348 |
*align = alignof(union sysv4); |
| 349 |
return sprintf(buf, "%d", s->s); |
| 350 |
} |
| 351 |
|
| 352 |
struct sysv5 { |
| 353 |
char c; |
| 354 |
int :0; |
| 355 |
char d; |
| 356 |
short :9; |
| 357 |
char e; |
| 358 |
char :0; |
| 359 |
}; |
| 360 |
|
| 361 |
EXPORT int print_sysv5(size_t* sz, size_t* align, char* buf, struct sysv5* s); |
| 362 |
|
| 363 |
int print_sysv5(size_t* sz, size_t* align, char* buf, struct sysv5* s) { |
| 364 |
*sz = sizeof(struct sysv5); |
| 365 |
*align = alignof(struct sysv5); |
| 366 |
return sprintf(buf, "%d %d %d", s->c, s->d, s->e); |
| 367 |
} |
| 368 |
|
| 369 |
struct sysv6 { |
| 370 |
char c; |
| 371 |
int :0; |
| 372 |
char d; |
| 373 |
int :9; |
| 374 |
char e; |
| 375 |
}; |
| 376 |
|
| 377 |
EXPORT int print_sysv6(size_t* sz, size_t* align, char* buf, struct sysv6* s); |
| 378 |
|
| 379 |
int print_sysv6(size_t* sz, size_t* align, char* buf, struct sysv6* s) { |
| 380 |
*sz = sizeof(struct sysv6); |
| 381 |
*align = alignof(struct sysv6); |
| 382 |
return sprintf(buf, "%d %d %d", s->c, s->d, s->e); |
| 383 |
} |
| 384 |
|
| 385 |
struct sysv7 { |
| 386 |
int j:9; |
| 387 |
short s:9; |
| 388 |
char c; |
| 389 |
short t:9; |
| 390 |
short u:9; |
| 391 |
}; |
| 392 |
|
| 393 |
EXPORT int print_sysv7(size_t* sz, size_t* align, char* buf, struct sysv7* s); |
| 394 |
|
| 395 |
int print_sysv7(size_t* sz, size_t* align, char* buf, struct sysv7* s) { |
| 396 |
*sz = sizeof(struct sysv7); |
| 397 |
*align = alignof(struct sysv7); |
| 398 |
return sprintf(buf, "%d %d %d %d %d", s->j, s->s, s->c, s->t, s->u); |
| 399 |
} |
| 400 |
|
| 401 |
/* Now some targeting bitfield tests */ |
| 402 |
|
| 403 |
/* Bitfield alignment */ |
| 404 |
#define BITALIGN(TNUM,BNUM) \ |
| 405 |
struct ba_##TNUM##_##BNUM { \ |
| 406 |
char a; \ |
| 407 |
uint##TNUM##_t b : BNUM; \ |
| 408 |
}; \ |
| 409 |
EXPORT int print_ba_##TNUM##_##BNUM(size_t* sz, size_t* align, char* buf, struct ba_##TNUM##_##BNUM* s); \ |
| 410 |
int print_ba_##TNUM##_##BNUM(size_t* sz, size_t* align, char* buf, struct ba_##TNUM##_##BNUM* s) { \ |
| 411 |
*sz = sizeof(struct ba_##TNUM##_##BNUM); \ |
| 412 |
*align = alignof(struct ba_##TNUM##_##BNUM); \ |
| 413 |
return sprintf(buf, "%d %d", (int) s->a, (int) s->b); \ |
| 414 |
} |
| 415 |
|
| 416 |
BITALIGN(8,7) |
| 417 |
BITALIGN(16,7) |
| 418 |
BITALIGN(16,15) |
| 419 |
BITALIGN(32,7) |
| 420 |
BITALIGN(32,15) |
| 421 |
BITALIGN(32,31) |
| 422 |
BITALIGN(64,7) |
| 423 |
BITALIGN(64,15) |
| 424 |
BITALIGN(64,31) |
| 425 |
BITALIGN(64,63) |
| 426 |
|
| 427 |
/* Do unsigned and signed coallesce */ |
| 428 |
#define BITCOALESCE(NUM) \ |
| 429 |
struct bc##NUM { \ |
| 430 |
uint##NUM##_t a : 3; \ |
| 431 |
int##NUM##_t b : 3; \ |
| 432 |
}; \ |
| 433 |
EXPORT int print_bc##NUM(size_t* sz, size_t* align, char* buf, struct bc##NUM* s); \ |
| 434 |
int print_bc##NUM(size_t* sz, size_t* align, char* buf, struct bc##NUM* s) { \ |
| 435 |
*sz = sizeof(struct bc##NUM); \ |
| 436 |
*align = alignof(struct bc##NUM); \ |
| 437 |
return sprintf(buf, "%d %d", (int) s->a, (int) s->b); \ |
| 438 |
} |
| 439 |
|
| 440 |
BITCOALESCE(8) |
| 441 |
BITCOALESCE(16) |
| 442 |
BITCOALESCE(32) |
| 443 |
BITCOALESCE(64) |
| 444 |
|
| 445 |
// Do different sizes coallesce |
| 446 |
struct bdsz { |
| 447 |
uint8_t a : 3; |
| 448 |
uint16_t b : 3; |
| 449 |
uint32_t c : 3; |
| 450 |
uint64_t d : 3; |
| 451 |
}; |
| 452 |
|
| 453 |
EXPORT int print_bdsz(size_t* sz, size_t* align, char* buf, struct bdsz* s); |
| 454 |
int print_bdsz(size_t* sz, size_t* align, char* buf, struct bdsz* s) { |
| 455 |
*sz = sizeof(struct bdsz); |
| 456 |
*align = alignof(struct bdsz); |
| 457 |
return sprintf(buf, "%d %d %d %d", (int) s->a, (int) s->b, (int) s->c, (int) s->d); |
| 458 |
} |
| 459 |
|
| 460 |
// Does coallesence upgrade the storage unit |
| 461 |
struct bcup { |
| 462 |
uint8_t a : 7; |
| 463 |
uint16_t b : 9; |
| 464 |
uint32_t c : 17; |
| 465 |
uint64_t d : 33; |
| 466 |
}; |
| 467 |
|
| 468 |
EXPORT int print_bcup(size_t* sz, size_t* align, char* buf, struct bcup* s); |
| 469 |
int print_bcup(size_t* sz, size_t* align, char* buf, struct bcup* s) { |
| 470 |
*sz = sizeof(struct bcup); |
| 471 |
*align = alignof(struct bcup); |
| 472 |
return sprintf(buf, "%d %d %d %"PRIu64, (int) s->a, (int) s->b, (int) s->c, (uint64_t) s->d); |
| 473 |
} |
| 474 |
|
| 475 |
// Is unaligned access allowed |
| 476 |
struct buna { |
| 477 |
uint32_t a : 31; |
| 478 |
uint32_t b : 31; |
| 479 |
}; |
| 480 |
|
| 481 |
EXPORT int print_buna(size_t* sz, size_t* align, char* buf, struct buna* s); |
| 482 |
int print_buna(size_t* sz, size_t* align, char* buf, struct buna* s) { |
| 483 |
*sz = sizeof(struct buna); |
| 484 |
*align = alignof(struct buna); |
| 485 |
return sprintf(buf, "%d %d", (int) s->a, (int) s->b); |
| 486 |
} |
| 487 |
|
| 488 |
/* What does a lone :0 do */ |
| 489 |
#define BITLONEZERO(NUM) \ |
| 490 |
struct blz##NUM { \ |
| 491 |
uint##NUM##_t a; \ |
| 492 |
uint##NUM##_t :0; \ |
| 493 |
uint##NUM##_t b; \ |
| 494 |
}; \ |
| 495 |
EXPORT int print_##blz##NUM(size_t* sz, size_t* align, char* buf, struct blz##NUM* s); \ |
| 496 |
int print_blz##NUM(size_t* sz, size_t* align, char* buf, struct blz##NUM* s) { \ |
| 497 |
*sz = sizeof(struct blz##NUM); \ |
| 498 |
*align = alignof(struct blz##NUM); \ |
| 499 |
return sprintf(buf, "%d %d", (int) s->a, (int) s->b); \ |
| 500 |
} |
| 501 |
|
| 502 |
BITLONEZERO(8) |
| 503 |
BITLONEZERO(16) |
| 504 |
BITLONEZERO(32) |
| 505 |
BITLONEZERO(64) |
| 506 |
|
| 507 |
/* What does a :0 or unnamed :# of the same or different type do */ |
| 508 |
#define BITZERO(NUM, ZNUM, BNUM) \ |
| 509 |
struct bz_##NUM##_##ZNUM##_##BNUM { \ |
| 510 |
uint8_t a; \ |
| 511 |
uint##NUM##_t b : 3; \ |
| 512 |
uint##ZNUM##_t :BNUM; \ |
| 513 |
uint##NUM##_t c : 3; \ |
| 514 |
}; \ |
| 515 |
EXPORT int print_bz_##NUM##_##ZNUM##_##BNUM(size_t* sz, size_t* align, char* buf, struct bz_##NUM##_##ZNUM##_##BNUM* s); \ |
| 516 |
int print_bz_##NUM##_##ZNUM##_##BNUM(size_t* sz, size_t* align, char* buf, struct bz_##NUM##_##ZNUM##_##BNUM* s) { \ |
| 517 |
*sz = sizeof(struct bz_##NUM##_##ZNUM##_##BNUM); \ |
| 518 |
*align = alignof(struct bz_##NUM##_##ZNUM##_##BNUM); \ |
| 519 |
return sprintf(buf, "%d %d %d", (int) s->a, (int) s->b, (int) s->c); \ |
| 520 |
} |
| 521 |
|
| 522 |
BITZERO(8,8,0) |
| 523 |
BITZERO(8,8,7) |
| 524 |
BITZERO(8,16,0) |
| 525 |
BITZERO(8,16,7) |
| 526 |
BITZERO(8,16,15) |
| 527 |
BITZERO(8,32,0) |
| 528 |
BITZERO(8,32,7) |
| 529 |
BITZERO(8,32,15) |
| 530 |
BITZERO(8,32,31) |
| 531 |
BITZERO(8,64,0) |
| 532 |
BITZERO(8,64,7) |
| 533 |
BITZERO(8,64,15) |
| 534 |
BITZERO(8,64,31) |
| 535 |
BITZERO(8,64,63) |
| 536 |
BITZERO(16,8,0) |
| 537 |
BITZERO(16,8,7) |
| 538 |
BITZERO(16,16,0) |
| 539 |
BITZERO(16,16,7) |
| 540 |
BITZERO(16,16,15) |
| 541 |
BITZERO(16,32,0) |
| 542 |
BITZERO(16,32,7) |
| 543 |
BITZERO(16,32,15) |
| 544 |
BITZERO(16,32,31) |
| 545 |
BITZERO(16,64,0) |
| 546 |
BITZERO(16,64,7) |
| 547 |
BITZERO(16,64,15) |
| 548 |
BITZERO(16,64,31) |
| 549 |
BITZERO(16,64,63) |
| 550 |
BITZERO(32,8,0) |
| 551 |
BITZERO(32,8,7) |
| 552 |
BITZERO(32,16,0) |
| 553 |
BITZERO(32,16,7) |
| 554 |
BITZERO(32,16,15) |
| 555 |
BITZERO(32,32,0) |
| 556 |
BITZERO(32,32,7) |
| 557 |
BITZERO(32,32,15) |
| 558 |
BITZERO(32,32,31) |
| 559 |
BITZERO(32,64,0) |
| 560 |
BITZERO(32,64,7) |
| 561 |
BITZERO(32,64,15) |
| 562 |
BITZERO(32,64,31) |
| 563 |
BITZERO(32,64,63) |
| 564 |
BITZERO(64,8,0) |
| 565 |
BITZERO(64,8,7) |
| 566 |
BITZERO(64,16,0) |
| 567 |
BITZERO(64,16,7) |
| 568 |
BITZERO(64,16,15) |
| 569 |
BITZERO(64,32,0) |
| 570 |
BITZERO(64,32,7) |
| 571 |
BITZERO(64,32,15) |
| 572 |
BITZERO(64,32,31) |
| 573 |
BITZERO(64,64,0) |
| 574 |
BITZERO(64,64,7) |
| 575 |
BITZERO(64,64,15) |
| 576 |
BITZERO(64,64,31) |
| 577 |
BITZERO(64,64,63) |
| 578 |
|
| 579 |
#define CALL(TYPE, SUFFIX) \ |
| 580 |
EXPORT TYPE call_##SUFFIX(TYPE (*func)(TYPE), TYPE arg); \ |
| 581 |
TYPE call_##SUFFIX(TYPE (*func)(TYPE), TYPE arg) { \ |
| 582 |
return func(arg); \ |
| 583 |
} |
| 584 |
|
| 585 |
CALL(int, i) |
| 586 |
CALL(float, f) |
| 587 |
CALL(double, d) |
| 588 |
CALL(const char*, s) |
| 589 |
CALL(_Bool, b) |
| 590 |
CALL(enum e8, e8) |
| 591 |
CALL(enum e16, e16) |
| 592 |
CALL(enum e32, e32) |
| 593 |
#ifdef HAVE_COMPLEX |
| 594 |
CALL(double complex, dc) |
| 595 |
CALL(float complex, fc) |
| 596 |
#endif |
| 597 |
|
| 598 |
struct fptr { |
| 599 |
#ifdef _MSC_VER |
| 600 |
int (__cdecl *p)(int); |
| 601 |
#else |
| 602 |
int (*p)(int); |
| 603 |
#endif |
| 604 |
}; |
| 605 |
|
| 606 |
EXPORT int call_fptr(struct fptr* s, int val); |
| 607 |
|
| 608 |
int call_fptr(struct fptr* s, int val) { |
| 609 |
return (s->p)(val); |
| 610 |
} |
| 611 |
|
| 612 |
EXPORT bool g_b; |
| 613 |
EXPORT int8_t g_i8; |
| 614 |
EXPORT int16_t g_i16; |
| 615 |
EXPORT int32_t g_i32; |
| 616 |
EXPORT int64_t g_i64; |
| 617 |
EXPORT uint8_t g_u8; |
| 618 |
EXPORT uint16_t g_u16; |
| 619 |
EXPORT uint32_t g_u32; |
| 620 |
EXPORT uint64_t g_u64; |
| 621 |
EXPORT float g_f; |
| 622 |
EXPORT double g_d; |
| 623 |
#ifdef HAVE_COMPLEX |
| 624 |
EXPORT double complex g_dc; |
| 625 |
EXPORT float complex g_fc; |
| 626 |
#endif |
| 627 |
EXPORT bool (*g_fp)(bool); |
| 628 |
EXPORT const char g_s[]; |
| 629 |
EXPORT const char* g_sp; |
| 630 |
EXPORT void* g_p; |
| 631 |
EXPORT enum e8 g_e8; |
| 632 |
EXPORT enum e16 g_e16; |
| 633 |
EXPORT enum e32 g_e32; |
| 634 |
EXPORT struct Date g_date; |
| 635 |
|
| 636 |
bool g_b = true; |
| 637 |
int8_t g_i8 = -8; |
| 638 |
int16_t g_i16 = -16; |
| 639 |
int32_t g_i32 = -32; |
| 640 |
int64_t g_i64 = -64; |
| 641 |
uint8_t g_u8 = 8; |
| 642 |
uint16_t g_u16 = 16; |
| 643 |
uint32_t g_u32 = 32; |
| 644 |
uint64_t g_u64 = 64; |
| 645 |
float g_f = 3; |
| 646 |
double g_d = 5; |
| 647 |
#ifdef HAVE_COMPLEX |
| 648 |
double complex g_dc = 7+8i; |
| 649 |
float complex g_fc = 6+9i; |
| 650 |
#endif |
| 651 |
bool (*g_fp)(bool) = ¬_b; |
| 652 |
void* g_p = (void*) ¬_b; |
| 653 |
const char g_s[] = "g_s"; |
| 654 |
const char* g_sp = "g_sp"; |
| 655 |
enum e8 g_e8 = FOO8; |
| 656 |
enum e16 g_e16 = FOO16; |
| 657 |
enum e32 g_e32 = FOO32; |
| 658 |
struct Date g_date = {1,2,3,4}; |
| 659 |
|
| 660 |
EXPORT void set_errno(int val); |
| 661 |
EXPORT int get_errno(void); |
| 662 |
|
| 663 |
void set_errno(int val) { |
| 664 |
#ifdef _WIN32 |
| 665 |
SetLastError(val); |
| 666 |
#else |
| 667 |
errno = val; |
| 668 |
#endif |
| 669 |
} |
| 670 |
|
| 671 |
int get_errno(void) { |
| 672 |
#ifdef _WIN32 |
| 673 |
return GetLastError(); |
| 674 |
#else |
| 675 |
return errno; |
| 676 |
#endif |
| 677 |
} |
| 678 |
|
| 679 |
EXPORT int va_list_size, va_list_align; |
| 680 |
int va_list_size = sizeof(va_list); |
| 681 |
int va_list_align = alignof(va_list); |
| 682 |
|
| 683 |
|
| 684 |
EXPORT char buf[512]; |
| 685 |
char buf[512]; |
| 686 |
|
| 687 |
EXPORT void test_call_echo(char* c); |
| 688 |
void test_call_echo(char* c) |
| 689 |
{ |
| 690 |
sprintf(buf, "%s", c); |
| 691 |
} |
| 692 |
|
| 693 |
EXPORT void test_call_pppppii(void* a, void* b, void* c, void* d, void* e, int f, int g); |
| 694 |
void test_call_pppppii(void* a, void* b, void* c, void* d, void* e, int f, int g) |
| 695 |
{ |
| 696 |
sprintf(buf, "%p %p %p %p %p %d %d", a, b, c, d, e, f, g); |
| 697 |
} |
| 698 |
|
| 699 |
EXPORT void test_call_pppppiiiiii(void* p1, void* p2, void* p3, void* p4, void* p5, int i1, int i2, int i3, int i4, int i5, int i6); |
| 700 |
void test_call_pppppiiiiii(void* p1, void* p2, void* p3, void* p4, void* p5, int i1, int i2, int i3, int i4, int i5, int i6) |
| 701 |
{ |
| 702 |
sprintf(buf, "%p %p %p %p %p %d %d %d %d %d %d", p1, p2, p3, p4, p5, i1, i2, i3, i4, i5, i6); |
| 703 |
} |
| 704 |
|
| 705 |
EXPORT void test_call_pppppffffff(void* p1, void* p2, void* p3, void* p4, void* p5, float f1, float f2, float f3, float f4, float f5, float f6); |
| 706 |
void test_call_pppppffffff(void* p1, void* p2, void* p3, void* p4, void* p5, float f1, float f2, float f3, float f4, float f5, float f6) |
| 707 |
{ |
| 708 |
sprintf(buf, "%p %p %p %p %p %0.1f %0.1f %0.1f %0.1f %0.1f %0.1f", p1, p2, p3, p4, p5, f1, f2, f3, f4, f5, f6); |
| 709 |
} |
| 710 |
|
| 711 |
EXPORT void test_call_pppppffffffffff(void* p1, void* p2, void* p3, void* p4, void* p5, float f1, float f2, float f3, float f4, float f5, float f6, float f7, float f8, float f9, float f10); |
| 712 |
void test_call_pppppffffffffff(void* p1, void* p2, void* p3, void* p4, void* p5, float f1, float f2, float f3, float f4, float f5, float f6, float f7, float f8, float f9, float f10) |
| 713 |
{ |
| 714 |
sprintf(buf, "%p %p %p %p %p %0.1f %0.1f %0.1f %0.1f %0.1f %0.1f %0.1f %0.1f %0.1f %0.1f", p1, p2, p3, p4, p5, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10); |
| 715 |
} |
| 716 |
|
| 717 |
EXPORT void test_call_pppppiifiii(void* p1, void* p2, void* p3, void* p4, void* p5, int i1, int i2, float f3, int i4, int i5, int i6); |
| 718 |
void test_call_pppppiifiii(void* p1, void* p2, void* p3, void* p4, void* p5, int i1, int i2, float f3, int i4, int i5, int i6) |
| 719 |
{ |
| 720 |
sprintf(buf, "%p %p %p %p %p %d %d %0.1f %d %d %d", p1, p2, p3, p4, p5, i1, i2, f3, i4, i5, i6); |
| 721 |
} |
| 722 |
|
| 723 |
EXPORT void test_call_pppppiiifii(void* p1, void* p2, void* p3, void* p4, void* p5, int i1, int i2, int i3, float f4, int i5, int i6); |
| 724 |
void test_call_pppppiiifii(void* p1, void* p2, void* p3, void* p4, void* p5, int i1, int i2, int i3, float f4, int i5, int i6) |
| 725 |
{ |
| 726 |
sprintf(buf, "%p %p %p %p %p %d %d %d %0.1f %d %d", p1, p2, p3, p4, p5, i1, i2, i3, f4, i5, i6); |
| 727 |
} |
| 728 |
|