Skip to content

Commit

Permalink
Merge branch 'master' into err-ret-trace-improvements-1923
Browse files Browse the repository at this point in the history
  • Loading branch information
topolarity authored Oct 18, 2022
2 parents bf9ecd0 + 71f8762 commit fd365e7
Show file tree
Hide file tree
Showing 27 changed files with 3,108 additions and 2,518 deletions.
119 changes: 0 additions & 119 deletions lib/c.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! This is Zig's multi-target implementation of libc.
//! When builtin.link_libc is true, we need to export all the functions and
//! provide an entire C API.
//! Otherwise, only the functions which LLVM generates calls to need to be generated,
//! such as memcpy, memset, and some math functions.

const std = @import("std");
const builtin = @import("builtin");
Expand Down Expand Up @@ -35,13 +33,6 @@ comptime {
@export(clone, .{ .name = "clone" });
}

@export(memset, .{ .name = "memset", .linkage = .Strong });
@export(__memset, .{ .name = "__memset", .linkage = .Strong });
@export(memcpy, .{ .name = "memcpy", .linkage = .Strong });
@export(memmove, .{ .name = "memmove", .linkage = .Strong });
@export(memcmp, .{ .name = "memcmp", .linkage = .Strong });
@export(bcmp, .{ .name = "bcmp", .linkage = .Strong });

if (builtin.link_libc) {
@export(strcmp, .{ .name = "strcmp", .linkage = .Strong });
@export(strncmp, .{ .name = "strncmp", .linkage = .Strong });
Expand Down Expand Up @@ -75,116 +66,6 @@ fn wasm_start() callconv(.C) void {
_ = main(0, undefined);
}

fn memset(dest: ?[*]u8, c: u8, len: usize) callconv(.C) ?[*]u8 {
@setRuntimeSafety(false);

if (len != 0) {
var d = dest.?;
var n = len;
while (true) {
d[0] = c;
n -= 1;
if (n == 0) break;
d += 1;
}
}

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);

if (len != 0) {
var d = dest.?;
var s = src.?;
var n = len;
while (true) {
d[0] = s[0];
n -= 1;
if (n == 0) break;
d += 1;
s += 1;
}
}

return dest;
}

fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8 {
@setRuntimeSafety(false);

if (@ptrToInt(dest) < @ptrToInt(src)) {
var index: usize = 0;
while (index != n) : (index += 1) {
dest.?[index] = src.?[index];
}
} else {
var index = n;
while (index != 0) {
index -= 1;
dest.?[index] = src.?[index];
}
}

return dest;
}

fn memcmp(vl: ?[*]const u8, vr: ?[*]const u8, n: usize) callconv(.C) c_int {
@setRuntimeSafety(false);

var index: usize = 0;
while (index != n) : (index += 1) {
const compare_val = @bitCast(i8, vl.?[index] -% vr.?[index]);
if (compare_val != 0) {
return compare_val;
}
}

return 0;
}

test "memcmp" {
const base_arr = &[_]u8{ 1, 1, 1 };
const arr1 = &[_]u8{ 1, 1, 1 };
const arr2 = &[_]u8{ 1, 0, 1 };
const arr3 = &[_]u8{ 1, 2, 1 };

try std.testing.expect(memcmp(base_arr[0..], arr1[0..], base_arr.len) == 0);
try std.testing.expect(memcmp(base_arr[0..], arr2[0..], base_arr.len) > 0);
try std.testing.expect(memcmp(base_arr[0..], arr3[0..], base_arr.len) < 0);
}

fn bcmp(vl: [*]allowzero const u8, vr: [*]allowzero const u8, n: usize) callconv(.C) c_int {
@setRuntimeSafety(false);

var index: usize = 0;
while (index != n) : (index += 1) {
if (vl[index] != vr[index]) {
return 1;
}
}

return 0;
}

test "bcmp" {
const base_arr = &[_]u8{ 1, 1, 1 };
const arr1 = &[_]u8{ 1, 1, 1 };
const arr2 = &[_]u8{ 1, 0, 1 };
const arr3 = &[_]u8{ 1, 2, 1 };

try std.testing.expect(bcmp(base_arr[0..], arr1[0..], base_arr.len) == 0);
try std.testing.expect(bcmp(base_arr[0..], arr2[0..], base_arr.len) != 0);
try std.testing.expect(bcmp(base_arr[0..], arr3[0..], base_arr.len) != 0);
}

var _fltused: c_int = 1;

fn strcpy(dest: [*:0]u8, src: [*:0]const u8) callconv(.C) [*:0]u8 {
Expand Down
6 changes: 6 additions & 0 deletions lib/compiler_rt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,10 @@ comptime {
_ = @import("compiler_rt/aulldiv.zig");
_ = @import("compiler_rt/aullrem.zig");
_ = @import("compiler_rt/clear_cache.zig");

_ = @import("compiler_rt/memcpy.zig");
_ = @import("compiler_rt/memset.zig");
_ = @import("compiler_rt/memmove.zig");
_ = @import("compiler_rt/cmp.zig");
_ = @import("compiler_rt/bcmp.zig");
}
30 changes: 30 additions & 0 deletions lib/compiler_rt/bcmp.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const std = @import("std");
const common = @import("./common.zig");

comptime {
@export(bcmp, .{ .name = "bcmp", .linkage = common.linkage });
}

pub fn bcmp(vl: [*]allowzero const u8, vr: [*]allowzero const u8, n: usize) callconv(.C) c_int {
@setRuntimeSafety(false);

var index: usize = 0;
while (index != n) : (index += 1) {
if (vl[index] != vr[index]) {
return 1;
}
}

return 0;
}

test "bcmp" {
const base_arr = &[_]u8{ 1, 1, 1 };
const arr1 = &[_]u8{ 1, 1, 1 };
const arr2 = &[_]u8{ 1, 0, 1 };
const arr3 = &[_]u8{ 1, 2, 1 };

try std.testing.expect(bcmp(base_arr[0..], arr1[0..], base_arr.len) == 0);
try std.testing.expect(bcmp(base_arr[0..], arr2[0..], base_arr.len) != 0);
try std.testing.expect(bcmp(base_arr[0..], arr3[0..], base_arr.len) != 0);
}
11 changes: 5 additions & 6 deletions lib/compiler_rt/emutls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const ObjectArray = struct {

if (control.default_value) |value| {
// default value: copy the content to newly allocated object.
@memcpy(data, @ptrCast([*]u8, value), size);
@memcpy(data, @ptrCast([*]const u8, value), size);
} else {
// no default: return zeroed memory.
@memset(data, 0, size);
Expand Down Expand Up @@ -236,7 +236,7 @@ const emutls_control = extern struct {
},

// null or non-zero initial value for the object
default_value: ?*anyopaque,
default_value: ?*const anyopaque,

// global Mutex used to serialize control.index initialization.
var mutex: std.c.pthread_mutex_t = std.c.PTHREAD_MUTEX_INITIALIZER;
Expand Down Expand Up @@ -291,7 +291,7 @@ const emutls_control = extern struct {
}

/// Simple helper for testing purpose.
pub fn init(comptime T: type, default_value: ?*T) emutls_control {
pub fn init(comptime T: type, default_value: ?*const T) emutls_control {
return emutls_control{
.size = @sizeOf(T),
.alignment = @alignOf(T),
Expand Down Expand Up @@ -362,7 +362,7 @@ test "__emutls_get_address zeroed" {
test "__emutls_get_address with default_value" {
if (!builtin.link_libc or builtin.os.tag != .openbsd) return error.SkipZigTest;

var value: usize = 5678; // default value
const value: usize = 5678; // default value
var ctl = emutls_control.init(usize, &value);
try expect(ctl.object.index == 0);

Expand All @@ -384,8 +384,7 @@ test "test default_value with differents sizes" {

const testType = struct {
fn _testType(comptime T: type, value: T) !void {
var def: T = value;
var ctl = emutls_control.init(T, &def);
var ctl = emutls_control.init(T, &value);
var x = ctl.get_typed_pointer(T);
try expect(x.* == value);
}
Expand Down
31 changes: 31 additions & 0 deletions lib/compiler_rt/memcmp.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const std = @import("std");
const common = @import("./common.zig");

comptime {
@export(memcmp, .{ .name = "memcmp", .linkage = common.linkage });
}

pub fn memcmp(vl: ?[*]const u8, vr: ?[*]const u8, n: usize) callconv(.C) c_int {
@setRuntimeSafety(false);

var index: usize = 0;
while (index != n) : (index += 1) {
const compare_val = @bitCast(i8, vl.?[index] -% vr.?[index]);
if (compare_val != 0) {
return compare_val;
}
}

return 0;
}

test "memcmp" {
const base_arr = &[_]u8{ 1, 1, 1 };
const arr1 = &[_]u8{ 1, 1, 1 };
const arr2 = &[_]u8{ 1, 0, 1 };
const arr3 = &[_]u8{ 1, 2, 1 };

try std.testing.expect(memcmp(base_arr[0..], arr1[0..], base_arr.len) == 0);
try std.testing.expect(memcmp(base_arr[0..], arr2[0..], base_arr.len) > 0);
try std.testing.expect(memcmp(base_arr[0..], arr3[0..], base_arr.len) < 0);
}
25 changes: 25 additions & 0 deletions lib/compiler_rt/memcpy.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const std = @import("std");
const common = @import("./common.zig");

comptime {
@export(memcpy, .{ .name = "memcpy", .linkage = common.linkage });
}

pub fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
@setRuntimeSafety(false);

if (len != 0) {
var d = dest.?;
var s = src.?;
var n = len;
while (true) {
d[0] = s[0];
n -= 1;
if (n == 0) break;
d += 1;
s += 1;
}
}

return dest;
}
25 changes: 25 additions & 0 deletions lib/compiler_rt/memmove.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const std = @import("std");
const common = @import("./common.zig");

comptime {
@export(memmove, .{ .name = "memmove", .linkage = common.linkage });
}

pub fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8 {
@setRuntimeSafety(false);

if (@ptrToInt(dest) < @ptrToInt(src)) {
var index: usize = 0;
while (index != n) : (index += 1) {
dest.?[index] = src.?[index];
}
} else {
var index = n;
while (index != 0) {
index -= 1;
dest.?[index] = src.?[index];
}
}

return dest;
}
30 changes: 30 additions & 0 deletions lib/compiler_rt/memset.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const std = @import("std");
const common = @import("./common.zig");

comptime {
@export(memset, .{ .name = "memset", .linkage = common.linkage });
@export(__memset, .{ .name = "__memset", .linkage = common.linkage });
}

pub fn memset(dest: ?[*]u8, c: u8, len: usize) callconv(.C) ?[*]u8 {
@setRuntimeSafety(false);

if (len != 0) {
var d = dest.?;
var n = len;
while (true) {
d[0] = c;
n -= 1;
if (n == 0) break;
d += 1;
}
}

return dest;
}

pub 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);
}
Loading

0 comments on commit fd365e7

Please sign in to comment.