| 1 |
/* This D file is implicitly imported by all ImportC source files. |
| 2 |
* It provides definitions for C compiler builtin functions and declarations. |
| 3 |
* The purpose is to make it unnecessary to hardwire them into the compiler. |
| 4 |
* As the leading double underscore suggests, this is for internal use only. |
| 5 |
* |
| 6 |
* Copyright: Copyright D Language Foundation 2022-2025 |
| 7 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 8 |
* Authors: Walter Bright |
| 9 |
* Source: $(DRUNTIMESRC __importc_builtins.di) |
| 10 |
*/ |
| 11 |
|
| 12 |
|
| 13 |
module __builtins; |
| 14 |
|
| 15 |
/* gcc relies on internal __builtin_xxxx functions and templates to |
| 16 |
* accomplish <stdarg.h>. D does the same thing with templates in core.stdc.stdarg. |
| 17 |
* Here, we redirect the gcc builtin declarations to the equivalent |
| 18 |
* ones in core.stdc.stdarg, thereby avoiding having to hardwire them |
| 19 |
* into the D compiler. |
| 20 |
*/ |
| 21 |
|
| 22 |
alias va_list = imported!"core.stdc.stdarg".va_list; |
| 23 |
|
| 24 |
version (Posix) |
| 25 |
{ |
| 26 |
version (X86_64) |
| 27 |
alias __va_list_tag = imported!"core.stdc.stdarg".__va_list_tag; |
| 28 |
} |
| 29 |
|
| 30 |
alias __builtin_va_start = imported!"core.stdc.stdarg".va_start; |
| 31 |
|
| 32 |
alias __builtin_va_end = imported!"core.stdc.stdarg".va_end; |
| 33 |
|
| 34 |
alias __builtin_va_copy = imported!"core.stdc.stdarg".va_copy; |
| 35 |
|
| 36 |
/* dmd's ImportC rewrites __builtin_va_arg into an instantiation of va_arg |
| 37 |
*/ |
| 38 |
alias va_arg = imported!"core.stdc.stdarg".va_arg; |
| 39 |
|
| 40 |
version (CRuntime_Microsoft) |
| 41 |
{ |
| 42 |
//https://docs.microsoft.com/en-us/cpp/cpp/int8-int16-int32-int64?view=msvc-170 |
| 43 |
alias __int8 = byte; |
| 44 |
alias __int16 = short; |
| 45 |
alias __int32 = int; |
| 46 |
alias __int64 = long; |
| 47 |
} |
| 48 |
|
| 49 |
/*********** floating point *************/ |
| 50 |
|
| 51 |
/* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html |
| 52 |
*/ |
| 53 |
|
| 54 |
version (DigitalMars) |
| 55 |
{ |
| 56 |
immutable float __nan = float.nan; |
| 57 |
|
| 58 |
float __builtin_nanf()(char*) { return float.nan; } |
| 59 |
|
| 60 |
double __builtin_inf()() { return double.infinity; } |
| 61 |
float __builtin_inff()() { return float.infinity; } |
| 62 |
real __builtin_infl()() { return real.infinity; } |
| 63 |
|
| 64 |
alias __builtin_huge_val = __builtin_inf; |
| 65 |
alias __builtin_huge_valf = __builtin_inff; |
| 66 |
alias __builtin_huge_vall = __builtin_infl; |
| 67 |
|
| 68 |
alias __builtin_fabs = imported!"core.stdc.math".fabs; |
| 69 |
alias __builtin_fabsf = imported!"core.stdc.math".fabsf; |
| 70 |
alias __builtin_fabsl = imported!"core.stdc.math".fabsl; |
| 71 |
|
| 72 |
ushort __builtin_bswap16()(ushort value) |
| 73 |
{ |
| 74 |
return cast(ushort) (((value >> 8) & 0xFF) | ((value << 8) & 0xFF00U)); |
| 75 |
} |
| 76 |
|
| 77 |
uint __builtin_bswap32()(uint value) |
| 78 |
{ |
| 79 |
import core.bitop; |
| 80 |
return core.bitop.bswap(value); |
| 81 |
} |
| 82 |
|
| 83 |
ulong __builtin_bswap64()(ulong value) |
| 84 |
{ |
| 85 |
import core.bitop; |
| 86 |
return core.bitop.bswap(value); |
| 87 |
} |
| 88 |
|
| 89 |
uint __builtin__popcount()(ulong value) |
| 90 |
{ |
| 91 |
import core.bitop; |
| 92 |
return core.bitop._popcnt(value); |
| 93 |
} |
| 94 |
|
| 95 |
// Lazily imported on first use |
| 96 |
private alias c_long = imported!"core.stdc.config".c_long; |
| 97 |
|
| 98 |
// Stub these out to no-ops |
| 99 |
int __builtin_constant_p(T)(T exp) { return 0; } // should be something like __traits(compiles, enum X = expr) |
| 100 |
c_long __builtin_expect()(c_long exp, c_long c) { return exp; } |
| 101 |
void* __builtin_assume_aligned()(const void* p, size_t align_, ...) { return cast(void*)p; } |
| 102 |
|
| 103 |
// https://releases.llvm.org/13.0.0/tools/clang/docs/LanguageExtensions.html#builtin-assume |
| 104 |
void __builtin_assume(T)(lazy T arg) { } |
| 105 |
|
| 106 |
/* Header on macOS for arm64 references this. |
| 107 |
* Don't need to implement it, it just needs to compile |
| 108 |
*/ |
| 109 |
align (16) struct __uint128_t |
| 110 |
{ |
| 111 |
ulong a, b; |
| 112 |
} |
| 113 |
} |