__builtins.di 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Digital Mars 2022
  7. * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
  8. * Authors: Walter Bright
  9. * Source: $(DRUNTIMESRC __builtins.d)
  10. */
  11. module __builtins;
  12. /* gcc relies on internal __builtin_xxxx functions and templates to
  13. * accomplish <stdarg.h>. D does the same thing with templates in core.stdc.stdarg.
  14. * Here, we redirect the gcc builtin declarations to the equivalent
  15. * ones in core.stdc.stdarg, thereby avoiding having to hardware them
  16. * into the D compiler.
  17. */
  18. import core.stdc.stdarg;
  19. alias va_list = core.stdc.stdarg.va_list;
  20. version (Posix)
  21. {
  22. version (X86_64)
  23. alias __va_list_tag = core.stdc.stdarg.__va_list_tag;
  24. }
  25. alias __builtin_va_start = core.stdc.stdarg.va_start;
  26. alias __builtin_va_end = core.stdc.stdarg.va_end;
  27. alias __builtin_va_copy = core.stdc.stdarg.va_copy;
  28. /* dmd's ImportC rewrites __builtin_va_arg into an instantiation of va_arg
  29. */
  30. alias va_arg = core.stdc.stdarg.va_arg;
  31. version (CRuntime_Microsoft)
  32. {
  33. //https://docs.microsoft.com/en-us/cpp/cpp/int8-int16-int32-int64?view=msvc-170
  34. alias __int8 = byte;
  35. alias __int16 = short;
  36. alias __int32 = int;
  37. alias __int64 = long;
  38. }
  39. /*********** floating point *************/
  40. /* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
  41. */
  42. version (DigitalMars)
  43. {
  44. double __builtin_inf()() { return double.infinity; }
  45. float __builtin_inff()() { return float.infinity; }
  46. real __builtin_infl()() { return real.infinity; }
  47. alias __builtin_huge_val = __builtin_inf;
  48. alias __builtin_huge_valf = __builtin_inff;
  49. alias __builtin_huge_vall = __builtin_infl;
  50. import core.stdc.math;
  51. alias __builtin_fabs = core.stdc.math.fabs;
  52. alias __builtin_fabsf = core.stdc.math.fabsf;
  53. alias __builtin_fabsl = core.stdc.math.fabsl;
  54. ushort __builtin_bswap16()(ushort value)
  55. {
  56. import core.bitop;
  57. return core.bitop.byteswap(value);
  58. }
  59. uint __builtin_bswap32()(uint value)
  60. {
  61. import core.bitop;
  62. return core.bitop.bswap(value);
  63. }
  64. ulong __builtin_bswap64()(ulong value)
  65. {
  66. import core.bitop;
  67. return core.bitop.bswap(value);
  68. }
  69. // Stub these out to no-ops
  70. int __builtin_constant_p(T)(T exp) { return 0; } // should be something like __traits(compiles, enum X = expr)
  71. long __builtin_expect()(long exp, long c) { return exp; }
  72. void* __builtin_assume_aligned()(const void* p, size_t align_, ...) { return cast(void*)p; }
  73. // https://releases.llvm.org/13.0.0/tools/clang/docs/LanguageExtensions.html#builtin-assume
  74. void __builtin_assume(T)(lazy T arg) { }
  75. /* Header on macOS for arm64 references this.
  76. * Don't need to implement it, it just needs to compile
  77. */
  78. align (16) struct __uint128_t
  79. {
  80. ulong a, b;
  81. }
  82. }