authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-09 19:13:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-09 19:13:53-07:00
log65c812842dfb0db9a4c6f5af04719ebf18f2d169
tree2f13df583b2d8425f24fbccef3a6abc4d702188c
parentf2f1c63daff8e47b6b901d53fd58805e54fcdf78

freestanding libc: fix missing functions

In the previous commit I got mixed up and cut-pasted instead of copy-pasting. In this commit I made c_stage1.zig additionally included for stage1 and everything else included for both. So moving forward we move stuff over from c_stage1.zig to c.zig instead of copying.

2 files changed, 19 insertions(+), 53 deletions(-)

lib/std/special/c.zig+19-12
......@@ -14,19 +14,20 @@ comptime {
1414 // When the self-hosted compiler is further along, all the logic from c_stage1.zig will
1515 // be migrated to this file and then c_stage1.zig will be deleted. Until then we have a
1616 // simpler implementation of c.zig that only uses features already implemented in self-hosted.
17 if (builtin.zig_backend != .stage1) {
18 @export(memset, .{ .name = "memset", .linkage = .Strong });
19 @export(memcpy, .{ .name = "memcpy", .linkage = .Strong });
20
21 @export(trunc, .{ .name = "trunc", .linkage = .Strong });
22 @export(truncf, .{ .name = "truncf", .linkage = .Strong });
23 @export(truncl, .{ .name = "truncl", .linkage = .Strong });
24
25 @export(log, .{ .name = "log", .linkage = .Strong });
26 @export(logf, .{ .name = "logf", .linkage = .Strong });
27 } else {
17 if (builtin.zig_backend == .stage1) {
2818 _ = @import("c_stage1.zig");
2919 }
20
21 @export(memset, .{ .name = "memset", .linkage = .Strong });
22 @export(__memset, .{ .name = "__memset", .linkage = .Strong });
23 @export(memcpy, .{ .name = "memcpy", .linkage = .Strong });
24
25 @export(trunc, .{ .name = "trunc", .linkage = .Strong });
26 @export(truncf, .{ .name = "truncf", .linkage = .Strong });
27 @export(truncl, .{ .name = "truncl", .linkage = .Strong });
28
29 @export(log, .{ .name = "log", .linkage = .Strong });
30 @export(logf, .{ .name = "logf", .linkage = .Strong });
3031}
3132
3233// Avoid dragging in the runtime safety mechanisms into this .o file,
......@@ -65,6 +66,12 @@ fn memset(dest: ?[*]u8, c: u8, len: usize) callconv(.C) ?[*]u8 {
6566 return dest;
6667}
6768
69fn __memset(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
70 if (dest_n < n)
71 @panic("buffer overflow");
72 return memset(dest, c, n);
73}
74
6875fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
6976 @setRuntimeSafety(false);
7077
......@@ -73,7 +80,7 @@ fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(
7380 var s = src.?;
7481 var n = len;
7582 while (true) {
76 d.* = s.*;
83 d[0] = s[0];
7784 n -= 1;
7885 if (n == 0) break;
7986 d += 1;
lib/std/special/c_stage1.zig-41
......@@ -161,32 +161,6 @@ test "strncmp" {
161161 try std.testing.expect(strncmp("\xff", "\x02", 1) == 253);
162162}
163163
164export fn memset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8 {
165 @setRuntimeSafety(false);
166
167 var index: usize = 0;
168 while (index != n) : (index += 1)
169 dest.?[index] = c;
170
171 return dest;
172}
173
174export fn __memset(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
175 if (dest_n < n)
176 @panic("buffer overflow");
177 return memset(dest, c, n);
178}
179
180export fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8 {
181 @setRuntimeSafety(false);
182
183 var index: usize = 0;
184 while (index != n) : (index += 1)
185 dest.?[index] = src.?[index];
186
187 return dest;
188}
189
190164export fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8 {
191165 @setRuntimeSafety(false);
192166
......@@ -732,21 +706,6 @@ export fn fabsf(a: f32) f32 {
732706 return math.fabs(a);
733707}
734708
735export fn trunc(a: f64) f64 {
736 return math.trunc(a);
737}
738
739export fn truncf(a: f32) f32 {
740 return math.trunc(a);
741}
742
743export fn truncl(a: c_longdouble) c_longdouble {
744 if (!long_double_is_f128) {
745 @panic("TODO implement this");
746 }
747 return math.trunc(a);
748}
749
750709export fn round(a: f64) f64 {
751710 return math.round(a);
752711}