2022-02-14 03:17:53 +08:00
|
|
|
/* This D file is implicitly imported by all ImportC source files.
|
|
|
|
* It provides definitions for C compiler builtin functions and declarations.
|
|
|
|
* The purpose is to make it unnecessary to hardwire them into the compiler.
|
|
|
|
* As the leading double underscore suggests, this is for internal use only.
|
|
|
|
*
|
|
|
|
* Copyright: Copyright Digital Mars 2022
|
|
|
|
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
|
|
|
|
* Authors: Walter Bright
|
|
|
|
* Source: $(DRUNTIMESRC __builtins.d)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
module __builtins;
|
|
|
|
|
|
|
|
/* gcc relies on internal __builtin_xxxx functions and templates to
|
|
|
|
* accomplish <stdarg.h>. D does the same thing with templates in core.stdc.stdarg.
|
|
|
|
* Here, we redirect the gcc builtin declarations to the equivalent
|
2022-09-27 16:43:32 +08:00
|
|
|
* ones in core.stdc.stdarg, thereby avoiding having to hardwire them
|
2022-02-14 03:17:53 +08:00
|
|
|
* into the D compiler.
|
|
|
|
*/
|
|
|
|
|
2022-05-17 00:30:46 +08:00
|
|
|
alias va_list = imported!"core.stdc.stdarg".va_list;
|
2022-02-14 03:17:53 +08:00
|
|
|
|
|
|
|
version (Posix)
|
|
|
|
{
|
|
|
|
version (X86_64)
|
2022-05-17 00:30:46 +08:00
|
|
|
alias __va_list_tag = imported!"core.stdc.stdarg".__va_list_tag;
|
2022-02-14 03:17:53 +08:00
|
|
|
}
|
|
|
|
|
2022-05-17 00:30:46 +08:00
|
|
|
alias __builtin_va_start = imported!"core.stdc.stdarg".va_start;
|
2022-02-14 03:17:53 +08:00
|
|
|
|
2022-05-17 00:30:46 +08:00
|
|
|
alias __builtin_va_end = imported!"core.stdc.stdarg".va_end;
|
2022-02-14 03:17:53 +08:00
|
|
|
|
2022-05-17 00:30:46 +08:00
|
|
|
alias __builtin_va_copy = imported!"core.stdc.stdarg".va_copy;
|
2022-02-14 03:17:53 +08:00
|
|
|
|
|
|
|
/* dmd's ImportC rewrites __builtin_va_arg into an instantiation of va_arg
|
|
|
|
*/
|
2022-05-17 00:30:46 +08:00
|
|
|
alias va_arg = imported!"core.stdc.stdarg".va_arg;
|
2022-02-21 03:02:23 +08:00
|
|
|
|
|
|
|
version (CRuntime_Microsoft)
|
|
|
|
{
|
|
|
|
//https://docs.microsoft.com/en-us/cpp/cpp/int8-int16-int32-int64?view=msvc-170
|
|
|
|
alias __int8 = byte;
|
|
|
|
alias __int16 = short;
|
|
|
|
alias __int32 = int;
|
|
|
|
alias __int64 = long;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********** floating point *************/
|
|
|
|
|
|
|
|
/* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
version (DigitalMars)
|
|
|
|
{
|
2024-01-18 06:49:05 +08:00
|
|
|
immutable float __nan = float.nan;
|
|
|
|
|
|
|
|
float __builtin_nanf()(char*) { return float.nan; }
|
|
|
|
|
2022-02-21 03:02:23 +08:00
|
|
|
double __builtin_inf()() { return double.infinity; }
|
|
|
|
float __builtin_inff()() { return float.infinity; }
|
|
|
|
real __builtin_infl()() { return real.infinity; }
|
|
|
|
|
|
|
|
alias __builtin_huge_val = __builtin_inf;
|
|
|
|
alias __builtin_huge_valf = __builtin_inff;
|
|
|
|
alias __builtin_huge_vall = __builtin_infl;
|
|
|
|
|
2022-05-17 00:30:46 +08:00
|
|
|
alias __builtin_fabs = imported!"core.stdc.math".fabs;
|
|
|
|
alias __builtin_fabsf = imported!"core.stdc.math".fabsf;
|
|
|
|
alias __builtin_fabsl = imported!"core.stdc.math".fabsl;
|
2022-02-21 03:02:23 +08:00
|
|
|
|
|
|
|
ushort __builtin_bswap16()(ushort value)
|
|
|
|
{
|
2023-07-10 09:07:41 +08:00
|
|
|
return cast(ushort) (((value >> 8) & 0xFF) | ((value << 8) & 0xFF00U));
|
2022-02-21 03:02:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint __builtin_bswap32()(uint value)
|
|
|
|
{
|
|
|
|
import core.bitop;
|
|
|
|
return core.bitop.bswap(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong __builtin_bswap64()(ulong value)
|
|
|
|
{
|
|
|
|
import core.bitop;
|
|
|
|
return core.bitop.bswap(value);
|
|
|
|
}
|
|
|
|
|
2022-05-17 00:30:46 +08:00
|
|
|
// Lazily imported on first use
|
|
|
|
private alias c_long = imported!"core.stdc.config".c_long;
|
|
|
|
|
2022-02-21 03:02:23 +08:00
|
|
|
// Stub these out to no-ops
|
2022-05-17 00:30:46 +08:00
|
|
|
int __builtin_constant_p(T)(T exp) { return 0; } // should be something like __traits(compiles, enum X = expr)
|
|
|
|
c_long __builtin_expect()(c_long exp, c_long c) { return exp; }
|
|
|
|
void* __builtin_assume_aligned()(const void* p, size_t align_, ...) { return cast(void*)p; }
|
2022-02-21 03:02:23 +08:00
|
|
|
|
|
|
|
// https://releases.llvm.org/13.0.0/tools/clang/docs/LanguageExtensions.html#builtin-assume
|
|
|
|
void __builtin_assume(T)(lazy T arg) { }
|
|
|
|
|
|
|
|
/* Header on macOS for arm64 references this.
|
|
|
|
* Don't need to implement it, it just needs to compile
|
|
|
|
*/
|
|
|
|
align (16) struct __uint128_t
|
|
|
|
{
|
|
|
|
ulong a, b;
|
|
|
|
}
|
|
|
|
}
|