-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avoid using Zig std for builtin translation
- Loading branch information
Showing
30 changed files
with
345 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
pub const Builtin = enum { | ||
__builtin_bswap16, | ||
__builtin_bswap32, | ||
__builtin_bswap64, | ||
__builtin_signbit, | ||
__builtin_signbitf, | ||
__builtin_popcount, | ||
__builtin_ctz, | ||
__builtin_clz, | ||
__builtin_sqrt, | ||
__builtin_sqrtf, | ||
__builtin_sin, | ||
__builtin_sinf, | ||
__builtin_cos, | ||
__builtin_cosf, | ||
__builtin_exp, | ||
__builtin_expf, | ||
__builtin_exp2, | ||
__builtin_exp2f, | ||
__builtin_log, | ||
__builtin_logf, | ||
__builtin_log2, | ||
__builtin_log2f, | ||
__builtin_log10, | ||
__builtin_log10f, | ||
__builtin_abs, | ||
__builtin_labs, | ||
__builtin_llabs, | ||
__builtin_fabs, | ||
__builtin_fabsf, | ||
__builtin_floor, | ||
__builtin_floorf, | ||
__builtin_ceil, | ||
__builtin_ceilf, | ||
__builtin_trunc, | ||
__builtin_truncf, | ||
__builtin_round, | ||
__builtin_roundf, | ||
__builtin_strlen, | ||
__builtin_strcmp, | ||
__builtin_object_size, | ||
__builtin___memset_chk, | ||
__builtin_memset, | ||
__builtin___memcpy_chk, | ||
__builtin_memcpy, | ||
__builtin_nanf, | ||
__builtin_huge_valf, | ||
__builtin_inff, | ||
__builtin_isnan, | ||
__builtin_isinf, | ||
__builtin_isinf_sign, | ||
__has_builtin, | ||
__builtin_assume, | ||
__builtin_unreachable, | ||
__builtin_constant_p, | ||
__builtin_mul_overflow, | ||
|
||
// __builtin_alloca_with_align is not currently implemented. | ||
// It is used in a run and a translate test to ensure that non-implemented | ||
// builtins are correctly demoted. If you implement __builtin_alloca_with_align, | ||
// please update the tests to use a different non-implemented builtin. | ||
|
||
pub fn source(b: Builtin) ?[]const u8 { | ||
return switch (b) { | ||
.__builtin_signbit => @embedFile("builtins/signbit.zig"), | ||
.__builtin_signbitf => @embedFile("builtins/signbitf.zig"), | ||
.__builtin_popcount => @embedFile("builtins/popcount.zig"), | ||
.__builtin_ctz => @embedFile("builtins/ctz.zig"), | ||
.__builtin_clz => @embedFile("builtins/clz.zig"), | ||
.__builtin_abs => @embedFile("builtins/abs.zig"), | ||
.__builtin_labs => @embedFile("builtins/labs.zig"), | ||
.__builtin_llabs => @embedFile("builtins/llabs.zig"), | ||
.__builtin_strlen => @embedFile("builtins/strlen.zig"), | ||
.__builtin_strcmp => @embedFile("builtins/strcmp.zig"), | ||
.__builtin_object_size => @embedFile("builtins/object_size.zig"), | ||
.__builtin___memset_chk => @embedFile("builtins/memset_chk.zig"), | ||
.__builtin_memset => @embedFile("builtins/memset.zig"), | ||
.__builtin___memcpy_chk => @embedFile("builtins/memcpy_chk.zig"), | ||
.__builtin_memcpy => @embedFile("builtins/memcpy.zig"), | ||
.__builtin_nanf => @embedFile("builtins/nanf.zig"), | ||
.__builtin_huge_valf => @embedFile("builtins/huge_valf.zig"), | ||
.__builtin_inff => @embedFile("builtins/inff.zig"), | ||
.__builtin_isnan => @embedFile("builtins/isnan.zig"), | ||
.__builtin_isinf => @embedFile("builtins/isinf.zig"), | ||
.__builtin_isinf_sign => @embedFile("builtins/isinf_sign.zig"), | ||
.__has_builtin => @embedFile("builtins/has_builtin.zig"), | ||
.__builtin_assume => @embedFile("builtins/assume.zig"), | ||
.__builtin_constant_p => @embedFile("builtins/constant_p.zig"), | ||
.__builtin_mul_overflow => @embedFile("builtins/mul_overflow.zig"), | ||
else => return null, | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/// Standard C Library bug: The absolute value of the most negative integer remains negative. | ||
pub inline fn __builtin_abs(val: c_int) c_int { | ||
return if (val == @import("std").math.minInt(c_int)) val else @intCast(@abs(val)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub inline fn __builtin_assume(cond: bool) void { | ||
if (!cond) unreachable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/// Returns the number of leading 0-bits in x, starting at the most significant bit position. | ||
/// In C if `val` is 0, the result is undefined; in zig it's the number of bits in a c_uint | ||
pub inline fn __builtin_clz(val: c_uint) c_int { | ||
@setRuntimeSafety(false); | ||
return @as(c_int, @bitCast(@as(c_uint, @clz(val)))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub inline fn __builtin_constant_p(expr: anytype) c_int { | ||
_ = expr; | ||
return @intFromBool(false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/// Returns the number of trailing 0-bits in val, starting at the least significant bit position. | ||
/// In C if `val` is 0, the result is undefined; in zig it's the number of bits in a c_uint | ||
pub inline fn __builtin_ctz(val: c_uint) c_int { | ||
@setRuntimeSafety(false); | ||
return @as(c_int, @bitCast(@as(c_uint, @ctz(val)))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub inline fn __has_builtin(func: anytype) c_int { | ||
_ = func; | ||
return @intFromBool(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub inline fn __builtin_huge_valf() f32 { | ||
return @import("std").math.inf(f32); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub inline fn __builtin_inff() f32 { | ||
return @import("std").math.inf(f32); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub inline fn __builtin_isinf(x: anytype) c_int { | ||
return @intFromBool(@import("std").math.isInf(x)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// Similar to isinf, except the return value is -1 for an argument of -Inf and 1 for an argument of +Inf. | ||
pub inline fn __builtin_isinf_sign(x: anytype) c_int { | ||
if (!@import("std").math.isInf(x)) return 0; | ||
return if (@import("std").math.isPositiveInf(x)) 1 else -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub inline fn __builtin_isnan(x: anytype) c_int { | ||
return @intFromBool(@import("std").math.isNan(x)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/// Standard C Library bug: The absolute value of the most negative integer remains negative. | ||
pub inline fn __builtin_labs(val: c_long) c_long { | ||
return if (val == @import("std").math.minInt(c_long)) val else @intCast(@abs(val)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/// Standard C Library bug: The absolute value of the most negative integer remains negative. | ||
pub inline fn __builtin_llabs(val: c_longlong) c_longlong { | ||
return if (val == @import("std").math.minInt(c_longlong)) val else @intCast(@abs(val)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub inline fn __builtin_memcpy( | ||
noalias dst: ?*anyopaque, | ||
noalias src: ?*const anyopaque, | ||
len: usize, | ||
) ?*anyopaque { | ||
if (len > 0) @memcpy( | ||
@as([*]u8, @ptrCast(dst.?))[0..len], | ||
@as([*]const u8, @ptrCast(src.?)), | ||
); | ||
return dst; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pub inline fn __builtin___memcpy_chk( | ||
noalias dst: ?*anyopaque, | ||
noalias src: ?*const anyopaque, | ||
len: usize, | ||
remaining: usize, | ||
) ?*anyopaque { | ||
if (len > remaining) @panic("__builtin___memcpy_chk called with len > remaining"); | ||
if (len > 0) @memcpy( | ||
@as([*]u8, @ptrCast(dst.?))[0..len], | ||
@as([*]const u8, @ptrCast(src.?)), | ||
); | ||
return dst; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub inline fn __builtin_memset(dst: ?*anyopaque, val: c_int, len: usize) ?*anyopaque { | ||
const dst_cast = @as([*c]u8, @ptrCast(dst)); | ||
@memset(dst_cast[0..len], @as(u8, @bitCast(@as(i8, @truncate(val))))); | ||
return dst; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub inline fn __builtin___memset_chk( | ||
dst: ?*anyopaque, | ||
val: c_int, | ||
len: usize, | ||
remaining: usize, | ||
) ?*anyopaque { | ||
if (len > remaining) @panic("__builtin___memset_chk called with len > remaining"); | ||
const dst_cast = @as([*c]u8, @ptrCast(dst)); | ||
@memset(dst_cast[0..len], @as(u8, @bitCast(@as(i8, @truncate(val))))); | ||
return dst; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub fn __builtin_mul_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) c_int { | ||
const res = @mulWithOverflow(a, b); | ||
result.* = res[0]; | ||
return res[1]; | ||
} |
Oops, something went wrong.