Commit 65c812842d

Andrew Kelley <andrew@ziglang.org>
2022-02-10 03:13:53
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.
1 parent f2f1c63
Changed files (2)
lib
std
lib/std/special/c.zig
@@ -14,19 +14,20 @@ comptime {
     // When the self-hosted compiler is further along, all the logic from c_stage1.zig will
     // be migrated to this file and then c_stage1.zig will be deleted. Until then we have a
     // simpler implementation of c.zig that only uses features already implemented in self-hosted.
-    if (builtin.zig_backend != .stage1) {
-        @export(memset, .{ .name = "memset", .linkage = .Strong });
-        @export(memcpy, .{ .name = "memcpy", .linkage = .Strong });
-
-        @export(trunc, .{ .name = "trunc", .linkage = .Strong });
-        @export(truncf, .{ .name = "truncf", .linkage = .Strong });
-        @export(truncl, .{ .name = "truncl", .linkage = .Strong });
-
-        @export(log, .{ .name = "log", .linkage = .Strong });
-        @export(logf, .{ .name = "logf", .linkage = .Strong });
-    } else {
+    if (builtin.zig_backend == .stage1) {
         _ = @import("c_stage1.zig");
     }
+
+    @export(memset, .{ .name = "memset", .linkage = .Strong });
+    @export(__memset, .{ .name = "__memset", .linkage = .Strong });
+    @export(memcpy, .{ .name = "memcpy", .linkage = .Strong });
+
+    @export(trunc, .{ .name = "trunc", .linkage = .Strong });
+    @export(truncf, .{ .name = "truncf", .linkage = .Strong });
+    @export(truncl, .{ .name = "truncl", .linkage = .Strong });
+
+    @export(log, .{ .name = "log", .linkage = .Strong });
+    @export(logf, .{ .name = "logf", .linkage = .Strong });
 }
 
 // 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 {
     return dest;
 }
 
+fn __memset(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
+    if (dest_n < n)
+        @panic("buffer overflow");
+    return memset(dest, c, n);
+}
+
 fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
     @setRuntimeSafety(false);
 
@@ -73,7 +80,7 @@ fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(
         var s = src.?;
         var n = len;
         while (true) {
-            d.* = s.*;
+            d[0] = s[0];
             n -= 1;
             if (n == 0) break;
             d += 1;
lib/std/special/c_stage1.zig
@@ -161,32 +161,6 @@ test "strncmp" {
     try std.testing.expect(strncmp("\xff", "\x02", 1) == 253);
 }
 
-export fn memset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8 {
-    @setRuntimeSafety(false);
-
-    var index: usize = 0;
-    while (index != n) : (index += 1)
-        dest.?[index] = c;
-
-    return dest;
-}
-
-export fn __memset(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
-    if (dest_n < n)
-        @panic("buffer overflow");
-    return memset(dest, c, n);
-}
-
-export fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8 {
-    @setRuntimeSafety(false);
-
-    var index: usize = 0;
-    while (index != n) : (index += 1)
-        dest.?[index] = src.?[index];
-
-    return dest;
-}
-
 export fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8 {
     @setRuntimeSafety(false);
 
@@ -732,21 +706,6 @@ export fn fabsf(a: f32) f32 {
     return math.fabs(a);
 }
 
-export fn trunc(a: f64) f64 {
-    return math.trunc(a);
-}
-
-export fn truncf(a: f32) f32 {
-    return math.trunc(a);
-}
-
-export fn truncl(a: c_longdouble) c_longdouble {
-    if (!long_double_is_f128) {
-        @panic("TODO implement this");
-    }
-    return math.trunc(a);
-}
-
 export fn round(a: f64) f64 {
     return math.round(a);
 }