@zigc/lib 0.16.0-test.1 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (241) hide show
  1. package/LICENSE +19 -0
  2. package/c/math.zig +135 -35
  3. package/c/stropts.zig +17 -0
  4. package/c.zig +1 -0
  5. package/compiler/aro/aro/Attribute/names.zig +604 -589
  6. package/compiler/aro/aro/Attribute.zig +202 -116
  7. package/compiler/aro/aro/Builtins/common.zig +874 -863
  8. package/compiler/aro/aro/Builtins/eval.zig +15 -7
  9. package/compiler/aro/aro/Builtins.zig +0 -1
  10. package/compiler/aro/aro/CodeGen.zig +3 -1
  11. package/compiler/aro/aro/Compilation.zig +120 -97
  12. package/compiler/aro/aro/Diagnostics.zig +21 -17
  13. package/compiler/aro/aro/Driver/GCCDetector.zig +635 -0
  14. package/compiler/aro/aro/Driver.zig +124 -50
  15. package/compiler/aro/aro/LangOpts.zig +12 -2
  16. package/compiler/aro/aro/Parser/Diagnostic.zig +79 -19
  17. package/compiler/aro/aro/Parser.zig +336 -142
  18. package/compiler/aro/aro/Preprocessor/Diagnostic.zig +21 -0
  19. package/compiler/aro/aro/Preprocessor.zig +127 -56
  20. package/compiler/aro/aro/Target.zig +17 -12
  21. package/compiler/aro/aro/Tokenizer.zig +31 -14
  22. package/compiler/aro/aro/Toolchain.zig +4 -7
  23. package/compiler/aro/aro/Tree.zig +178 -148
  24. package/compiler/aro/aro/TypeStore.zig +82 -24
  25. package/compiler/aro/aro/Value.zig +13 -17
  26. package/compiler/aro/aro/features.zig +1 -0
  27. package/compiler/aro/aro/pragmas/once.zig +0 -1
  28. package/compiler/aro/aro/record_layout.zig +3 -3
  29. package/compiler/aro/assembly_backend/x86_64.zig +3 -4
  30. package/compiler/aro/backend/Assembly.zig +1 -2
  31. package/compiler/aro/backend/Interner.zig +2 -2
  32. package/compiler/aro/backend/Ir.zig +100 -92
  33. package/compiler/aro/include/ptrcheck.h +49 -0
  34. package/compiler/aro/main.zig +26 -10
  35. package/compiler/build_runner.zig +1 -0
  36. package/compiler/objdump.zig +93 -0
  37. package/compiler/reduce.zig +5 -1
  38. package/compiler/resinator/compile.zig +2 -2
  39. package/compiler/resinator/main.zig +7 -1
  40. package/compiler/resinator/preprocess.zig +1 -3
  41. package/compiler/std-docs.zig +8 -1
  42. package/compiler/test_runner.zig +193 -61
  43. package/compiler/translate-c/MacroTranslator.zig +80 -11
  44. package/compiler/translate-c/PatternList.zig +1 -9
  45. package/compiler/translate-c/Scope.zig +43 -6
  46. package/compiler/translate-c/Translator.zig +364 -126
  47. package/compiler/translate-c/ast.zig +19 -11
  48. package/compiler/translate-c/main.zig +75 -16
  49. package/compiler_rt/cos.zig +141 -52
  50. package/compiler_rt/limb64.zig +266 -0
  51. package/compiler_rt/long_double.zig +37 -0
  52. package/compiler_rt/mulo.zig +6 -1
  53. package/compiler_rt/rem_pio2l.zig +173 -0
  54. package/compiler_rt/sin.zig +140 -55
  55. package/compiler_rt/sincos.zig +279 -72
  56. package/compiler_rt/tan.zig +118 -47
  57. package/compiler_rt/trig.zig +256 -6
  58. package/compiler_rt.zig +2 -0
  59. package/fuzzer.zig +855 -307
  60. package/libc/musl/src/math/pow.c +343 -0
  61. package/package.json +1 -1
  62. package/std/Build/Fuzz.zig +6 -19
  63. package/std/Build/Module.zig +1 -1
  64. package/std/Build/Step/CheckObject.zig +3 -3
  65. package/std/Build/Step/Compile.zig +18 -0
  66. package/std/Build/Step/ConfigHeader.zig +49 -33
  67. package/std/Build/Step/InstallArtifact.zig +18 -0
  68. package/std/Build/Step/Run.zig +536 -87
  69. package/std/Build/Step/TranslateC.zig +0 -6
  70. package/std/Build/Step.zig +8 -15
  71. package/std/Build/WebServer.zig +29 -17
  72. package/std/Build/abi.zig +47 -11
  73. package/std/Build.zig +17 -14
  74. package/std/Io/Dispatch.zig +2 -0
  75. package/std/Io/File/Reader.zig +3 -1
  76. package/std/Io/File.zig +1 -0
  77. package/std/Io/Kqueue.zig +2 -2
  78. package/std/Io/Threaded.zig +181 -143
  79. package/std/Io/Uring.zig +2 -1
  80. package/std/Io.zig +970 -2
  81. package/std/Target.zig +3 -2
  82. package/std/Thread.zig +8 -3
  83. package/std/array_hash_map.zig +96 -555
  84. package/std/array_list.zig +22 -31
  85. package/std/bit_set.zig +22 -6
  86. package/std/builtin/assembly.zig +68 -0
  87. package/std/c.zig +17 -17
  88. package/std/compress/flate/Compress.zig +3 -3
  89. package/std/crypto/Certificate/Bundle.zig +15 -1
  90. package/std/crypto/codecs/asn1.zig +33 -18
  91. package/std/crypto/codecs/base64_hex_ct.zig +14 -4
  92. package/std/debug/Dwarf.zig +29 -9
  93. package/std/debug/Info.zig +4 -0
  94. package/std/debug/MachOFile.zig +46 -8
  95. package/std/debug/Pdb.zig +539 -36
  96. package/std/debug/SelfInfo/Elf.zig +19 -18
  97. package/std/debug/SelfInfo/MachO.zig +18 -7
  98. package/std/debug/SelfInfo/Windows.zig +138 -36
  99. package/std/debug.zig +179 -65
  100. package/std/enums.zig +25 -19
  101. package/std/heap/ArenaAllocator.zig +145 -154
  102. package/std/heap/debug_allocator.zig +7 -7
  103. package/std/http/Client.zig +10 -6
  104. package/std/http.zig +11 -9
  105. package/std/json/Stringify.zig +3 -3
  106. package/std/json/dynamic.zig +4 -4
  107. package/std/math/big/int.zig +16 -17
  108. package/std/mem/Allocator.zig +4 -5
  109. package/std/mem.zig +48 -0
  110. package/std/os/emscripten.zig +1 -17
  111. package/std/os/linux.zig +7 -2
  112. package/std/os/windows.zig +2 -2
  113. package/std/pdb.zig +143 -4
  114. package/std/posix.zig +6 -12
  115. package/std/priority_dequeue.zig +13 -12
  116. package/std/priority_queue.zig +5 -4
  117. package/std/process/Child.zig +1 -1
  118. package/std/process/Environ.zig +1 -1
  119. package/std/start.zig +17 -4
  120. package/std/std.zig +19 -6
  121. package/std/testing/FailingAllocator.zig +4 -4
  122. package/std/testing/Smith.zig +37 -2
  123. package/std/zig/Ast/Render.zig +186 -458
  124. package/std/zig/Ast.zig +0 -4
  125. package/std/zig/AstGen.zig +44 -7
  126. package/std/zig/AstSmith.zig +2602 -0
  127. package/std/zig/Client.zig +8 -3
  128. package/std/zig/Parse.zig +83 -74
  129. package/std/zig/Server.zig +26 -0
  130. package/std/zig/Zir.zig +17 -0
  131. package/std/zig/c_translation/helpers.zig +14 -9
  132. package/std/zig/llvm/Builder.zig +107 -48
  133. package/std/zig/system.zig +20 -4
  134. package/std/zig/tokenizer.zig +2 -1
  135. package/std/zig.zig +6 -0
  136. package/compiler/aro/aro/Driver/Filesystem.zig +0 -241
  137. package/libc/mingw/complex/cabs.c +0 -48
  138. package/libc/mingw/complex/cabsf.c +0 -48
  139. package/libc/mingw/complex/cacos.c +0 -50
  140. package/libc/mingw/complex/cacosf.c +0 -50
  141. package/libc/mingw/complex/carg.c +0 -48
  142. package/libc/mingw/complex/cargf.c +0 -48
  143. package/libc/mingw/complex/casin.c +0 -50
  144. package/libc/mingw/complex/casinf.c +0 -50
  145. package/libc/mingw/complex/catan.c +0 -50
  146. package/libc/mingw/complex/catanf.c +0 -50
  147. package/libc/mingw/complex/ccos.c +0 -50
  148. package/libc/mingw/complex/ccosf.c +0 -50
  149. package/libc/mingw/complex/cexp.c +0 -48
  150. package/libc/mingw/complex/cexpf.c +0 -48
  151. package/libc/mingw/complex/cimag.c +0 -48
  152. package/libc/mingw/complex/cimagf.c +0 -48
  153. package/libc/mingw/complex/clog.c +0 -48
  154. package/libc/mingw/complex/clog10.c +0 -49
  155. package/libc/mingw/complex/clog10f.c +0 -49
  156. package/libc/mingw/complex/clogf.c +0 -48
  157. package/libc/mingw/complex/conj.c +0 -48
  158. package/libc/mingw/complex/conjf.c +0 -48
  159. package/libc/mingw/complex/cpow.c +0 -48
  160. package/libc/mingw/complex/cpowf.c +0 -48
  161. package/libc/mingw/complex/cproj.c +0 -48
  162. package/libc/mingw/complex/cprojf.c +0 -48
  163. package/libc/mingw/complex/creal.c +0 -48
  164. package/libc/mingw/complex/crealf.c +0 -48
  165. package/libc/mingw/complex/csin.c +0 -50
  166. package/libc/mingw/complex/csinf.c +0 -50
  167. package/libc/mingw/complex/csqrt.c +0 -48
  168. package/libc/mingw/complex/csqrtf.c +0 -48
  169. package/libc/mingw/complex/ctan.c +0 -50
  170. package/libc/mingw/complex/ctanf.c +0 -50
  171. package/libc/mingw/math/arm/s_rint.c +0 -86
  172. package/libc/mingw/math/arm/s_rintf.c +0 -51
  173. package/libc/mingw/math/arm/sincos.S +0 -30
  174. package/libc/mingw/math/arm-common/sincosl.c +0 -13
  175. package/libc/mingw/math/arm64/rint.c +0 -12
  176. package/libc/mingw/math/arm64/rintf.c +0 -12
  177. package/libc/mingw/math/arm64/sincos.S +0 -32
  178. package/libc/mingw/math/bsd_private_base.h +0 -148
  179. package/libc/mingw/math/frexpf.c +0 -13
  180. package/libc/mingw/math/frexpl.c +0 -71
  181. package/libc/mingw/math/x86/acosf.c +0 -29
  182. package/libc/mingw/math/x86/atanf.c +0 -23
  183. package/libc/mingw/math/x86/atanl.c +0 -18
  184. package/libc/mingw/math/x86/cos.def.h +0 -65
  185. package/libc/mingw/math/x86/cosl.c +0 -46
  186. package/libc/mingw/math/x86/cosl_internal.S +0 -55
  187. package/libc/mingw/math/x86/ldexp.c +0 -23
  188. package/libc/mingw/math/x86/scalbn.S +0 -41
  189. package/libc/mingw/math/x86/scalbnf.S +0 -40
  190. package/libc/mingw/math/x86/sin.def.h +0 -65
  191. package/libc/mingw/math/x86/sinl.c +0 -46
  192. package/libc/mingw/math/x86/sinl_internal.S +0 -58
  193. package/libc/mingw/math/x86/tanl.S +0 -62
  194. package/libc/mingw/misc/btowc.c +0 -28
  195. package/libc/mingw/misc/wcstof.c +0 -66
  196. package/libc/mingw/misc/wcstoimax.c +0 -132
  197. package/libc/mingw/misc/wcstoumax.c +0 -126
  198. package/libc/mingw/misc/wctob.c +0 -29
  199. package/libc/mingw/misc/winbs_uint64.c +0 -6
  200. package/libc/mingw/misc/winbs_ulong.c +0 -6
  201. package/libc/mingw/misc/winbs_ushort.c +0 -6
  202. package/libc/mingw/stdio/_Exit.c +0 -10
  203. package/libc/mingw/stdio/_findfirst64i32.c +0 -21
  204. package/libc/mingw/stdio/_findnext64i32.c +0 -21
  205. package/libc/mingw/stdio/_fstat64i32.c +0 -37
  206. package/libc/mingw/stdio/_stat64i32.c +0 -37
  207. package/libc/mingw/stdio/_wfindfirst64i32.c +0 -21
  208. package/libc/mingw/stdio/_wfindnext64i32.c +0 -21
  209. package/libc/mingw/stdio/_wstat64i32.c +0 -37
  210. package/libc/musl/src/legacy/isastream.c +0 -7
  211. package/libc/musl/src/legacy/valloc.c +0 -8
  212. package/libc/musl/src/math/__cosl.c +0 -96
  213. package/libc/musl/src/math/__sinl.c +0 -78
  214. package/libc/musl/src/math/__tanl.c +0 -143
  215. package/libc/musl/src/math/aarch64/lrint.c +0 -10
  216. package/libc/musl/src/math/aarch64/lrintf.c +0 -10
  217. package/libc/musl/src/math/aarch64/rintf.c +0 -7
  218. package/libc/musl/src/math/cosl.c +0 -39
  219. package/libc/musl/src/math/fdim.c +0 -10
  220. package/libc/musl/src/math/finite.c +0 -7
  221. package/libc/musl/src/math/finitef.c +0 -7
  222. package/libc/musl/src/math/frexp.c +0 -23
  223. package/libc/musl/src/math/frexpf.c +0 -23
  224. package/libc/musl/src/math/frexpl.c +0 -29
  225. package/libc/musl/src/math/i386/lrint.c +0 -8
  226. package/libc/musl/src/math/i386/lrintf.c +0 -8
  227. package/libc/musl/src/math/i386/rintf.c +0 -7
  228. package/libc/musl/src/math/lrint.c +0 -72
  229. package/libc/musl/src/math/lrintf.c +0 -8
  230. package/libc/musl/src/math/powerpc64/lrint.c +0 -16
  231. package/libc/musl/src/math/powerpc64/lrintf.c +0 -16
  232. package/libc/musl/src/math/rintf.c +0 -30
  233. package/libc/musl/src/math/s390x/rintf.c +0 -15
  234. package/libc/musl/src/math/sincosl.c +0 -60
  235. package/libc/musl/src/math/sinl.c +0 -41
  236. package/libc/musl/src/math/tanl.c +0 -29
  237. package/libc/musl/src/math/x32/lrint.s +0 -5
  238. package/libc/musl/src/math/x32/lrintf.s +0 -5
  239. package/libc/musl/src/math/x86_64/lrint.c +0 -8
  240. package/libc/musl/src/math/x86_64/lrintf.c +0 -8
  241. package/libc/wasi/libc-bottom-half/sources/reallocarray.c +0 -14
@@ -50,6 +50,7 @@ pub const Node = extern union {
50
50
  break_val,
51
51
  @"return",
52
52
  field_access,
53
+ field_builtin,
53
54
  array_access,
54
55
  call,
55
56
  var_decl,
@@ -371,6 +372,7 @@ pub const Node = extern union {
371
372
  .div_exact,
372
373
  .offset_of,
373
374
  .static_assert,
375
+ .field_builtin,
374
376
  => Payload.BinOp,
375
377
 
376
378
  .integer_literal,
@@ -455,14 +457,14 @@ pub const Node = extern union {
455
457
  return .{ .ptr_otherwise = payload };
456
458
  }
457
459
 
458
- pub fn isNoreturn(node: Node, break_counts: bool) bool {
459
- switch (node.tag()) {
460
+ pub fn isNoreturn(node: Node) bool {
461
+ return switch (node.tag()) {
460
462
  .block => {
461
463
  const block_node = node.castTag(.block).?;
462
464
  if (block_node.data.stmts.len == 0) return false;
463
465
 
464
466
  const last = block_node.data.stmts[block_node.data.stmts.len - 1];
465
- return last.isNoreturn(break_counts);
467
+ return last.isNoreturn();
466
468
  },
467
469
  .@"switch" => {
468
470
  const switch_node = node.castTag(.@"switch").?;
@@ -475,15 +477,16 @@ pub const Node = extern union {
475
477
  else
476
478
  unreachable;
477
479
 
478
- if (!body.isNoreturn(break_counts)) return false;
480
+ if (!body.isNoreturn()) return false;
479
481
  }
480
482
  return true;
481
483
  },
482
- .@"return", .return_void => return true,
483
- .@"break" => if (break_counts) return true,
484
- else => {},
485
- }
486
- return false;
484
+ .@"return", .return_void => true,
485
+ .@"break" => true,
486
+ .@"continue" => true,
487
+ .@"unreachable" => true,
488
+ else => false,
489
+ };
487
490
  }
488
491
 
489
492
  pub fn isBoolRes(res: Node) bool {
@@ -2015,6 +2018,10 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
2015
2018
  const lhs = try renderNodeGrouped(c, payload.lhs);
2016
2019
  return renderFieldAccess(c, lhs, payload.field_name);
2017
2020
  },
2021
+ .field_builtin => {
2022
+ const payload = node.castTag(.field_builtin).?.data;
2023
+ return renderBuiltinCall(c, "@field", &.{ payload.lhs, payload.rhs });
2024
+ },
2018
2025
  .@"struct", .@"union", .@"opaque" => return renderContainer(c, node),
2019
2026
  .enum_constant => {
2020
2027
  const payload = node.castTag(.enum_constant).?.data;
@@ -2424,7 +2431,7 @@ fn renderNullSentinelArrayType(c: *Context, len: u64, elem_type: Node) !NodeInde
2424
2431
  fn addSemicolonIfNeeded(c: *Context, node: Node) !void {
2425
2432
  switch (node.tag()) {
2426
2433
  .warning => unreachable,
2427
- .var_decl, .var_simple, .arg_redecl, .alias, .block, .empty_block, .block_single, .@"switch", .wrapped_local, .mut_str => {},
2434
+ .static_assert, .var_decl, .var_simple, .arg_redecl, .alias, .block, .empty_block, .block_single, .@"switch", .wrapped_local, .mut_str => {},
2428
2435
  .while_true => {
2429
2436
  const payload = node.castTag(.while_true).?.data;
2430
2437
  return addSemicolonIfNotBlock(c, payload);
@@ -2532,6 +2539,8 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
2532
2539
  .trunc,
2533
2540
  .floor,
2534
2541
  .root_ref,
2542
+ .field_builtin,
2543
+ .@"switch",
2535
2544
  => {
2536
2545
  // no grouping needed
2537
2546
  return renderNode(c, node);
@@ -2594,7 +2603,6 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
2594
2603
  .pub_var_simple,
2595
2604
  .enum_constant,
2596
2605
  .@"while",
2597
- .@"switch",
2598
2606
  .@"break",
2599
2607
  .break_val,
2600
2608
  .pub_inline_fn,
@@ -1,15 +1,17 @@
1
1
  const std = @import("std");
2
- const Io = std.Io;
3
2
  const assert = std.debug.assert;
4
3
  const mem = std.mem;
5
4
  const process = std.process;
5
+ const Io = std.Io;
6
+
6
7
  const aro = @import("aro");
7
8
  const compiler_util = @import("../util.zig");
9
+
8
10
  const Translator = @import("Translator.zig");
9
11
 
10
12
  const fast_exit = @import("builtin").mode != .Debug;
11
13
 
12
- pub fn main(init: std.process.Init) u8 {
14
+ pub fn main(init: process.Init) u8 {
13
15
  const gpa = init.gpa;
14
16
  const arena = init.arena.allocator();
15
17
  const io = init.io;
@@ -33,16 +35,20 @@ pub fn main(init: std.process.Init) u8 {
33
35
  var stderr = Io.File.stderr().writer(io, &stderr_buf);
34
36
  var diagnostics: aro.Diagnostics = switch (zig_integration) {
35
37
  false => .{ .output = .{ .to_writer = .{
36
- .mode = Io.Terminal.Mode.detect(io, stderr.file, NO_COLOR, CLICOLOR_FORCE) catch unreachable,
38
+ .mode = Io.Terminal.Mode.detect(io, stderr.file, NO_COLOR, CLICOLOR_FORCE) catch .no_color,
37
39
  .writer = &stderr.interface,
38
40
  } } },
39
- true => .{ .output = .{ .to_list = .{
40
- .arena = .init(gpa),
41
- } } },
41
+ true => .{ .output = .{ .to_list = .{ .arena = .init(gpa) } } },
42
42
  };
43
43
  defer diagnostics.deinit();
44
44
 
45
- var comp = aro.Compilation.initDefault(gpa, arena, io, &diagnostics, .cwd(), environ_map) catch |err| switch (err) {
45
+ var comp = aro.Compilation.init(.{
46
+ .gpa = gpa,
47
+ .arena = arena,
48
+ .io = io,
49
+ .diagnostics = &diagnostics,
50
+ .environ_map = environ_map,
51
+ }) catch |err| switch (err) {
46
52
  error.OutOfMemory => {
47
53
  std.debug.print("ran out of memory initializing C compilation\n", .{});
48
54
  if (fast_exit) process.exit(1);
@@ -82,7 +88,6 @@ pub fn main(init: std.process.Init) u8 {
82
88
  return 1;
83
89
  },
84
90
  };
85
-
86
91
  assert(comp.diagnostics.errors == 0 or !zig_integration);
87
92
  if (fast_exit) process.exit(@intFromBool(comp.diagnostics.errors != 0));
88
93
  return @intFromBool(comp.diagnostics.errors != 0);
@@ -107,10 +112,23 @@ pub const usage =
107
112
  \\Usage {s}: [options] file [CC options]
108
113
  \\
109
114
  \\Options:
110
- \\ --help Print this message
111
- \\ --version Print translate-c version
112
- \\ -fmodule-libs Import libraries as modules
113
- \\ -fno-module-libs (default) Install libraries next to output file
115
+ \\ --help Print this message
116
+ \\ --version Print translate-c version
117
+ \\ -fmodule-libs Import libraries as modules
118
+ \\ -fno-module-libs (default) Install libraries next to output file
119
+ \\ -fpub-static (default) Translate static functions as pub
120
+ \\ -fno-pub-static Do not translate static functions as pub
121
+ \\ -ffunc-bodies (default) Translate function bodies
122
+ \\ -fno-func-bodies Do not translate function bodies
123
+ \\ -fkeep-macro-literals (default) Preserve macro names for literals
124
+ \\ -fno-keep-macro-literals Do not preserve macro names for literals
125
+ \\ -fdefault-init Default initialize struct fields
126
+ \\ -fno-default-init (default) Do not default initialize struct fields
127
+ \\ -fstrict-flex-arrays=<n> Control when to treat a trailing array as a flexible array member (default: 2)
128
+ \\ 0: any trailing array
129
+ \\ 1: size [0]/[1]/[]
130
+ \\ 2: size [0]/[]
131
+ \\ 3: [] only
114
132
  \\
115
133
  \\
116
134
  ;
@@ -119,7 +137,14 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: []const [:0]const u8, zig
119
137
  const gpa = d.comp.gpa;
120
138
  const io = d.comp.io;
121
139
 
122
- var aro_args: std.ArrayList([:0]const u8) = .empty;
140
+ var module_libs = true;
141
+ var pub_static = true;
142
+ var func_bodies = true;
143
+ var keep_macro_literals = true;
144
+ var default_init = true;
145
+ var strict_flex_arrays: Translator.StrictFlexArraysLevel = .@"2";
146
+
147
+ var aro_args: std.ArrayList([:0]const u8) = try .initCapacity(gpa, args.len);
123
148
  defer aro_args.deinit(gpa);
124
149
 
125
150
  for (args, 0..) |arg, i| {
@@ -139,8 +164,34 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: []const [:0]const u8, zig
139
164
  } else if (mem.eql(u8, arg, "--zig-integration")) {
140
165
  if (i != 1 or !zig_integration)
141
166
  return d.fatal("--zig-integration must be the first argument", .{});
167
+ } else if (mem.eql(u8, arg, "-fmodule-libs")) {
168
+ module_libs = true;
169
+ } else if (mem.eql(u8, arg, "-fno-module-libs")) {
170
+ module_libs = false;
171
+ } else if (mem.eql(u8, arg, "-fpub-static")) {
172
+ pub_static = true;
173
+ } else if (mem.eql(u8, arg, "-fno-pub-static")) {
174
+ pub_static = false;
175
+ } else if (mem.eql(u8, arg, "-ffunc-bodies")) {
176
+ func_bodies = true;
177
+ } else if (mem.eql(u8, arg, "-fno-func-bodies")) {
178
+ func_bodies = false;
179
+ } else if (mem.eql(u8, arg, "-fkeep-macro-literals")) {
180
+ keep_macro_literals = true;
181
+ } else if (mem.eql(u8, arg, "-fno-keep-macro-literals")) {
182
+ keep_macro_literals = false;
183
+ } else if (mem.eql(u8, arg, "-fdefault-init")) {
184
+ default_init = true;
185
+ } else if (mem.eql(u8, arg, "-fno-default-init")) {
186
+ default_init = false;
187
+ } else if (mem.startsWith(u8, arg, "-fstrict-flex-arrays=")) {
188
+ const val_str = arg["-fstrict-flex-arrays=".len..];
189
+ if (val_str.len != 1 or val_str[0] < '0' or val_str[0] > '3') {
190
+ return d.fatal("-fstrict-flex-arrays= requires a value of '0', '1', '2', or '3'", .{});
191
+ }
192
+ strict_flex_arrays = @enumFromInt(val_str[0] - '0');
142
193
  } else {
143
- try aro_args.append(gpa, arg);
194
+ aro_args.appendAssumeCapacity(arg);
144
195
  }
145
196
  }
146
197
  const user_macros = macros: {
@@ -148,7 +199,7 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: []const [:0]const u8, zig
148
199
  defer macro_buf.deinit(gpa);
149
200
 
150
201
  var discard_buf: [256]u8 = undefined;
151
- var discarding: std.Io.Writer.Discarding = .init(&discard_buf);
202
+ var discarding: Io.Writer.Discarding = .init(&discard_buf);
152
203
  assert(!try d.parseArgs(&discarding.writer, &macro_buf, aro_args.items));
153
204
  if (macro_buf.items.len > std.math.maxInt(u32)) {
154
205
  return d.fatal("user provided macro source exceeded max size", .{});
@@ -185,7 +236,9 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: []const [:0]const u8, zig
185
236
  else => |e| return e,
186
237
  };
187
238
 
188
- var pp = try aro.Preprocessor.initDefault(d.comp);
239
+ var pp = try aro.Preprocessor.init(d.comp, .{
240
+ .base_file = source.id,
241
+ });
189
242
  defer pp.deinit();
190
243
 
191
244
  var name_buf: [std.fs.max_name_bytes]u8 = undefined;
@@ -235,6 +288,12 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: []const [:0]const u8, zig
235
288
  .comp = d.comp,
236
289
  .pp = &pp,
237
290
  .tree = &c_tree,
291
+ .module_libs = module_libs,
292
+ .pub_static = pub_static,
293
+ .func_bodies = func_bodies,
294
+ .keep_macro_literals = keep_macro_literals,
295
+ .default_init = default_init,
296
+ .strict_flex_arrays = strict_flex_arrays,
238
297
  });
239
298
  defer gpa.free(rendered_zig);
240
299
 
@@ -1,19 +1,30 @@
1
+ //! Ported from musl, which is licensed under the MIT license:
2
+ //! https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
3
+ //!
4
+ //! https://git.musl-libc.org/cgit/musl/tree/src/math/cosf.c
5
+ //! https://git.musl-libc.org/cgit/musl/tree/src/math/cos.c
6
+ //! https://git.musl-libc.org/cgit/musl/tree/src/math/cosl.c
7
+
1
8
  const std = @import("std");
2
9
  const math = std.math;
3
10
  const mem = std.mem;
4
11
  const expect = std.testing.expect;
12
+ const expectApproxEqAbs = std.testing.expectApproxEqAbs;
5
13
 
6
14
  const compiler_rt = @import("../compiler_rt.zig");
7
15
  const symbol = @import("../compiler_rt.zig").symbol;
8
16
  const trig = @import("trig.zig");
9
17
  const rem_pio2 = @import("rem_pio2.zig").rem_pio2;
10
18
  const rem_pio2f = @import("rem_pio2f.zig").rem_pio2f;
19
+ const rem_pio2l = @import("rem_pio2l.zig").rem_pio2l;
20
+ const ld = @import("long_double.zig");
11
21
 
12
22
  comptime {
13
- symbol(&__cosh, "__cosh");
23
+ symbol(&cosh, "__cosh");
24
+ symbol(&cosl, "__cosl");
14
25
  symbol(&cosf, "cosf");
15
26
  symbol(&cos, "cos");
16
- symbol(&__cosx, "__cosx");
27
+ symbol(&cosx, "__cosx");
17
28
  if (compiler_rt.want_ppc_abi) {
18
29
  symbol(&cosq, "cosf128");
19
30
  }
@@ -21,7 +32,7 @@ comptime {
21
32
  symbol(&cosl, "cosl");
22
33
  }
23
34
 
24
- pub fn __cosh(a: f16) callconv(.c) f16 {
35
+ pub fn cosh(a: f16) callconv(.c) f16 {
25
36
  // TODO: more efficient implementation
26
37
  return @floatCast(cosf(a));
27
38
  }
@@ -43,27 +54,27 @@ pub fn cosf(x: f32) callconv(.c) f32 {
43
54
  if (compiler_rt.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
44
55
  return 1.0;
45
56
  }
46
- return trig.__cosdf(x);
57
+ return trig.cosdf(x);
47
58
  }
48
59
  if (ix <= 0x407b53d1) { // |x| ~<= 5*pi/4
49
60
  if (ix > 0x4016cbe3) { // |x| ~> 3*pi/4
50
- return -trig.__cosdf(if (sign) x + c2pio2 else x - c2pio2);
61
+ return -trig.cosdf(if (sign) x + c2pio2 else x - c2pio2);
51
62
  } else {
52
63
  if (sign) {
53
- return trig.__sindf(x + c1pio2);
64
+ return trig.sindf(x + c1pio2);
54
65
  } else {
55
- return trig.__sindf(c1pio2 - x);
66
+ return trig.sindf(c1pio2 - x);
56
67
  }
57
68
  }
58
69
  }
59
70
  if (ix <= 0x40e231d5) { // |x| ~<= 9*pi/4
60
71
  if (ix > 0x40afeddf) { // |x| ~> 7*pi/4
61
- return trig.__cosdf(if (sign) x + c4pio2 else x - c4pio2);
72
+ return trig.cosdf(if (sign) x + c4pio2 else x - c4pio2);
62
73
  } else {
63
74
  if (sign) {
64
- return trig.__sindf(-x - c3pio2);
75
+ return trig.sindf(-x - c3pio2);
65
76
  } else {
66
- return trig.__sindf(x - c3pio2);
77
+ return trig.sindf(x - c3pio2);
67
78
  }
68
79
  }
69
80
  }
@@ -76,10 +87,10 @@ pub fn cosf(x: f32) callconv(.c) f32 {
76
87
  var y: f64 = undefined;
77
88
  const n = rem_pio2f(x, &y);
78
89
  return switch (n & 3) {
79
- 0 => trig.__cosdf(y),
80
- 1 => trig.__sindf(-y),
81
- 2 => -trig.__cosdf(y),
82
- else => trig.__sindf(y),
90
+ 0 => trig.cosdf(y),
91
+ 1 => trig.sindf(-y),
92
+ 2 => -trig.cosdf(y),
93
+ else => trig.sindf(y),
83
94
  };
84
95
  }
85
96
 
@@ -94,7 +105,7 @@ pub fn cos(x: f64) callconv(.c) f64 {
94
105
  if (compiler_rt.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
95
106
  return 1.0;
96
107
  }
97
- return trig.__cos(x, 0);
108
+ return trig.cos(x, 0);
98
109
  }
99
110
 
100
111
  // cos(Inf or NaN) is NaN
@@ -105,66 +116,144 @@ pub fn cos(x: f64) callconv(.c) f64 {
105
116
  var y: [2]f64 = undefined;
106
117
  const n = rem_pio2(x, &y);
107
118
  return switch (n & 3) {
108
- 0 => trig.__cos(y[0], y[1]),
109
- 1 => -trig.__sin(y[0], y[1], 1),
110
- 2 => -trig.__cos(y[0], y[1]),
111
- else => trig.__sin(y[0], y[1], 1),
119
+ 0 => trig.cos(y[0], y[1]),
120
+ 1 => -trig.sin(y[0], y[1], 1),
121
+ 2 => -trig.cos(y[0], y[1]),
122
+ else => trig.sin(y[0], y[1], 1),
112
123
  };
113
124
  }
114
125
 
115
- pub fn __cosx(a: f80) callconv(.c) f80 {
116
- // TODO: more efficient implementation
117
- return @floatCast(cosq(a));
126
+ pub fn cosx(x: f80) callconv(.c) f80 {
127
+ const se = ld.signExponent(x) & 0x7fff;
128
+ if (se == 0x7fff) {
129
+ return x - x;
130
+ }
131
+
132
+ if (@abs(x) < trig.pi_4) {
133
+ if (se < 0x3fff - math.floatMantissaBits(f80)) {
134
+ // raise inexact if x!=0
135
+ return 1.0 + x;
136
+ }
137
+ return trig.cosx(x, 0.0);
138
+ }
139
+
140
+ var y: [2]f80 = undefined;
141
+ const n = rem_pio2l(f80, x, &y);
142
+ return switch (n & 3) {
143
+ 0 => trig.cosx(y[0], y[1]),
144
+ 1 => -trig.sinx(y[0], y[1], 1),
145
+ 2 => -trig.cosx(y[0], y[1]),
146
+ else => trig.sinx(y[0], y[1], 1),
147
+ };
118
148
  }
119
149
 
120
- pub fn cosq(a: f128) callconv(.c) f128 {
121
- // TODO: more correct implementation
122
- return cos(@floatCast(a));
150
+ pub fn cosq(x: f128) callconv(.c) f128 {
151
+ const se = ld.signExponent(x) & 0x7fff;
152
+ if (se == 0x7fff) {
153
+ return x - x;
154
+ }
155
+
156
+ if (@abs(x) < trig.pi_4) {
157
+ if (se < 0x3fff - math.floatMantissaBits(f128)) {
158
+ // raise inexact if x!=0
159
+ return 1.0 + x;
160
+ }
161
+ return trig.cosq(x, 0.0);
162
+ }
163
+
164
+ var y: [2]f128 = undefined;
165
+ const n = rem_pio2l(f128, x, &y);
166
+ return switch (n & 3) {
167
+ 0 => trig.cosq(y[0], y[1]),
168
+ 1 => -trig.sinq(y[0], y[1], 1),
169
+ 2 => -trig.cosq(y[0], y[1]),
170
+ else => trig.sinq(y[0], y[1], 1),
171
+ };
123
172
  }
124
173
 
125
174
  pub fn cosl(x: c_longdouble) callconv(.c) c_longdouble {
126
175
  switch (@typeInfo(c_longdouble).float.bits) {
127
- 16 => return __cosh(x),
176
+ 16 => return cosh(x),
128
177
  32 => return cosf(x),
129
178
  64 => return cos(x),
130
- 80 => return __cosx(x),
179
+ 80 => return cosx(x),
131
180
  128 => return cosq(x),
132
181
  else => @compileError("unreachable"),
133
182
  }
134
183
  }
135
184
 
136
- test "cos32" {
137
- const epsilon = 0.00001;
185
+ fn testCosSpecial(comptime T: type) !void {
186
+ const f = switch (T) {
187
+ f32 => cosf,
188
+ f64 => cos,
189
+ f80 => cosx,
190
+ f128 => cosq,
191
+ else => @compileError("unimplemented"),
192
+ };
138
193
 
139
- try expect(math.approxEqAbs(f32, cosf(0.0), 1.0, epsilon));
140
- try expect(math.approxEqAbs(f32, cosf(0.2), 0.980067, epsilon));
141
- try expect(math.approxEqAbs(f32, cosf(0.8923), 0.627623, epsilon));
142
- try expect(math.approxEqAbs(f32, cosf(1.5), 0.070737, epsilon));
143
- try expect(math.approxEqAbs(f32, cosf(-1.5), 0.070737, epsilon));
144
- try expect(math.approxEqAbs(f32, cosf(37.45), 0.969132, epsilon));
145
- try expect(math.approxEqAbs(f32, cosf(89.123), 0.400798, epsilon));
194
+ try expect(f(0.0) == 1.0);
195
+ try expect(f(-0.0) == 1.0);
196
+ try expect(math.isNan(f(math.inf(T))));
197
+ try expect(math.isNan(f(-math.inf(T))));
198
+ try expect(math.isNan(f(math.nan(T))));
146
199
  }
147
200
 
148
- test "cos64" {
149
- const epsilon = 0.000001;
150
-
151
- try expect(math.approxEqAbs(f64, cos(0.0), 1.0, epsilon));
152
- try expect(math.approxEqAbs(f64, cos(0.2), 0.980067, epsilon));
153
- try expect(math.approxEqAbs(f64, cos(0.8923), 0.627623, epsilon));
154
- try expect(math.approxEqAbs(f64, cos(1.5), 0.070737, epsilon));
155
- try expect(math.approxEqAbs(f64, cos(-1.5), 0.070737, epsilon));
156
- try expect(math.approxEqAbs(f64, cos(37.45), 0.969132, epsilon));
157
- try expect(math.approxEqAbs(f64, cos(89.123), 0.40080, epsilon));
201
+ test "cos32.normal" {
202
+ const epsilon = math.floatEps(f32);
203
+ try expectApproxEqAbs(@as(f32, 1.0), cosf(0.0), epsilon);
204
+ try expectApproxEqAbs(@as(f32, 0.9800666), cosf(0.2), epsilon);
205
+ try expectApproxEqAbs(@as(f32, 0.6276231), cosf(0.8923), epsilon);
206
+ try expectApproxEqAbs(@as(f32, 0.0707372), cosf(1.5), epsilon);
207
+ try expectApproxEqAbs(@as(f32, 0.0707372), cosf(-1.5), epsilon);
208
+ try expectApproxEqAbs(@as(f32, 0.96913195), cosf(37.45), epsilon);
209
+ try expectApproxEqAbs(@as(f32, 0.40079966), cosf(89.123), epsilon);
158
210
  }
159
211
 
160
212
  test "cos32.special" {
161
- try expect(math.isNan(cosf(math.inf(f32))));
162
- try expect(math.isNan(cosf(-math.inf(f32))));
163
- try expect(math.isNan(cosf(math.nan(f32))));
213
+ try testCosSpecial(f32);
214
+ }
215
+
216
+ test "cos64.normal" {
217
+ const epsilon = math.floatEps(f64);
218
+ try expectApproxEqAbs(@as(f64, 1.0), cos(0.0), epsilon);
219
+ try expectApproxEqAbs(@as(f64, 0.9800665778412416), cos(0.2), epsilon);
220
+ try expectApproxEqAbs(@as(f64, 0.6276230983360804), cos(0.8923), epsilon);
221
+ try expectApproxEqAbs(@as(f64, 0.0707372016677029), cos(1.5), epsilon);
222
+ try expectApproxEqAbs(@as(f64, 0.0707372016677029), cos(-1.5), epsilon);
223
+ try expectApproxEqAbs(@as(f64, 0.9691317730707778), cos(37.45), epsilon);
224
+ try expectApproxEqAbs(@as(f64, 0.4008006809354791), cos(89.123), epsilon);
164
225
  }
165
226
 
166
227
  test "cos64.special" {
167
- try expect(math.isNan(cos(math.inf(f64))));
168
- try expect(math.isNan(cos(-math.inf(f64))));
169
- try expect(math.isNan(cos(math.nan(f64))));
228
+ try testCosSpecial(f64);
229
+ }
230
+
231
+ test "cos80.normal" {
232
+ const epsilon = math.floatEps(f80);
233
+ try expectApproxEqAbs(@as(f80, 1.0), cosx(0.0), epsilon);
234
+ try expectApproxEqAbs(@as(f80, 0.98006657784124163112419651674816888), cosx(0.2), epsilon);
235
+ try expectApproxEqAbs(@as(f80, 0.62762309833608037003563995939286067), cosx(0.8923), epsilon);
236
+ try expectApproxEqAbs(@as(f80, 0.070737201667702910088189851434268747), cosx(1.5), epsilon);
237
+ try expectApproxEqAbs(@as(f80, 0.070737201667702910088189851434268747), cosx(-1.5), epsilon);
238
+ try expectApproxEqAbs(@as(f80, 0.9691317730707771246), cosx(37.45), epsilon);
239
+ try expectApproxEqAbs(@as(f80, 0.4008006809354834001), cosx(89.123), epsilon);
240
+ }
241
+
242
+ test "cos80.special" {
243
+ try testCosSpecial(f80);
244
+ }
245
+
246
+ test "cos128.normal" {
247
+ const epsilon = math.floatEps(f128);
248
+ try expectApproxEqAbs(@as(f128, 1.0), cosq(0.0), epsilon);
249
+ try expectApproxEqAbs(@as(f128, 0.98006657784124163112419651674816888), cosq(0.2), epsilon);
250
+ try expectApproxEqAbs(@as(f128, 0.62762309833608037003563995939286067), cosq(0.8923), epsilon);
251
+ try expectApproxEqAbs(@as(f128, 0.070737201667702910088189851434268747), cosq(1.5), epsilon);
252
+ try expectApproxEqAbs(@as(f128, 0.070737201667702910088189851434268747), cosq(-1.5), epsilon);
253
+ try expectApproxEqAbs(@as(f128, 0.96913177307077712443149563847233230), cosq(37.45), epsilon);
254
+ try expectApproxEqAbs(@as(f128, 0.40080068093548339848199454493704702), cosq(89.123), epsilon);
255
+ }
256
+
257
+ test "cos128.special" {
258
+ try testCosSpecial(f128);
170
259
  }