Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use self hosted translate-c for cImport #4025

Merged
merged 23 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fceda07
use self hosted translate-c for cimports
Vexu Dec 21, 2019
3f7bab7
fix translate-c taking ages
Vexu Dec 21, 2019
1a3633d
fix stage2 build
Vexu Dec 22, 2019
59cc707
translate-c-2 use `intCast` in most places
Vexu Dec 23, 2019
f5e7d2d
translate-c fix bugs
Vexu Dec 29, 2019
621629e
translate-c-2 fix assertion failure rendering do while
Vexu Dec 29, 2019
55348c9
translate-c-2 bug fixes
Vexu Dec 29, 2019
fcc82a2
Add macro ops
frmdstryr Dec 27, 2019
6df9e9f
Cleanup c_tokenizer.zig tests
frmdstryr Dec 29, 2019
ca21161
update c_tokenzier tests to new api
Vexu Dec 29, 2019
950eb81
translate-c-2 macro comparision ops
Vexu Dec 29, 2019
39ee3bc
Merge branch 'stage-2-cimport' of https://github.com/Vexu/zig into Ve…
andrewrk Dec 31, 2019
5749f70
translate-c: non-wrapping operator for pointer arithmetic
andrewrk Dec 31, 2019
42945a2
translate-c: better mangling strategy
andrewrk Jan 1, 2020
7b62d5d
translate-c: propagate scope properly in nested assignment
andrewrk Jan 1, 2020
5575e2a
std.mem.compare: breaking API changes
andrewrk Jan 1, 2020
dc28526
translate-c: improve support of integer casting
andrewrk Jan 1, 2020
9298b9a
translate-c: prevent name clashing of globals declared after locals
andrewrk Jan 1, 2020
ec09b9e
translate-c: prevent name clashing of macros declared after locals
andrewrk Jan 2, 2020
365a612
translate-c: fix regression from previous commit
andrewrk Jan 2, 2020
a3f741e
translate-c: avoid producing duplicate macro errors
andrewrk Jan 2, 2020
5ba143e
avoid trying to translate microsoft's stdio.h inline functions
andrewrk Jan 2, 2020
88c5e2a
translate-c: don't export inline functions
andrewrk Jan 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ set(ZIG_SOURCES
"${CMAKE_SOURCE_DIR}/src/bigfloat.cpp"
"${CMAKE_SOURCE_DIR}/src/bigint.cpp"
"${CMAKE_SOURCE_DIR}/src/buffer.cpp"
"${CMAKE_SOURCE_DIR}/src/c_tokenizer.cpp"
"${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"
"${CMAKE_SOURCE_DIR}/src/codegen.cpp"
"${CMAKE_SOURCE_DIR}/src/compiler.cpp"
Expand All @@ -465,7 +464,6 @@ set(ZIG_SOURCES
"${CMAKE_SOURCE_DIR}/src/range_set.cpp"
"${CMAKE_SOURCE_DIR}/src/target.cpp"
"${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
"${CMAKE_SOURCE_DIR}/src/translate_c.cpp"
"${CMAKE_SOURCE_DIR}/src/util.cpp"
"${ZIG_SOURCES_MEM_PROFILE}"
)
Expand Down
2 changes: 1 addition & 1 deletion lib/std/crypto/chacha20.zig
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ test "crypto.chacha20 test vector sunscreen" {
// Chacha20 is self-reversing.
var plaintext: [114]u8 = undefined;
chaCha20IETF(plaintext[0..], result[0..], 1, key, nonce);
testing.expect(mem.compare(u8, input, &plaintext) == mem.Compare.Equal);
testing.expect(mem.order(u8, input, &plaintext) == .eq);
}

// https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
Expand Down
4 changes: 2 additions & 2 deletions lib/std/http/headers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ const HeaderEntry = struct {
}

// Sort lexicographically on header name
return mem.compare(u8, a.name, b.name) == mem.Compare.LessThan;
return mem.order(u8, a.name, b.name) == .lt;
}

// Sort lexicographically on header value
if (!mem.eql(u8, a.value, b.value)) {
return mem.compare(u8, a.value, b.value) == mem.Compare.LessThan;
return mem.order(u8, a.value, b.value) == .lt;
}

// Doesn't matter here; need to pick something for sort consistency
Expand Down
32 changes: 27 additions & 5 deletions lib/std/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -926,9 +926,6 @@ test "minInt and maxInt" {
}

test "max value type" {
// If the type of maxInt(i32) was i32 then this implicit cast to
// u32 would not work. But since the value is a number literal,
// it works fine.
const x: u32 = maxInt(i32);
testing.expect(x == 2147483647);
}
Expand All @@ -944,7 +941,32 @@ test "math.mulWide" {
testing.expect(mulWide(u8, 100, 100) == 10000);
}

/// Not to be confused with `std.mem.Compare`.
/// See also `CompareOperator`.
pub const Order = enum {
/// Less than (`<`)
lt,

/// Equal (`==`)
eq,

/// Greater than (`>`)
gt,
};

/// Given two numbers, this function returns the order they are with respect to each other.
pub fn order(a: var, b: var) Order {
if (a == b) {
return .eq;
} else if (a < b) {
return .lt;
} else if (a > b) {
return .gt;
} else {
unreachable;
}
}

/// See also `Order`.
pub const CompareOperator = enum {
/// Less than (`<`)
lt,
Expand Down Expand Up @@ -979,7 +1001,7 @@ pub fn compare(a: var, op: CompareOperator, b: var) bool {
};
}

test "math.lt, et al < <= > >= between signed and unsigned" {
test "compare between signed and unsigned" {
testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255)));
testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1)));
testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255)));
Expand Down
48 changes: 13 additions & 35 deletions lib/std/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,6 @@ pub const Allocator = struct {
}
};

pub const Compare = enum {
LessThan,
Equal,
GreaterThan,
};

/// Copy all of source into dest at position 0.
/// dest.len must be >= source.len.
/// dest.ptr must be <= src.ptr.
Expand Down Expand Up @@ -297,46 +291,30 @@ test "mem.secureZero" {
testing.expectEqualSlices(u8, a[0..], b[0..]);
}

pub fn compare(comptime T: type, lhs: []const T, rhs: []const T) Compare {
pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order {
const n = math.min(lhs.len, rhs.len);
var i: usize = 0;
while (i < n) : (i += 1) {
if (lhs[i] == rhs[i]) {
continue;
} else if (lhs[i] < rhs[i]) {
return Compare.LessThan;
} else if (lhs[i] > rhs[i]) {
return Compare.GreaterThan;
} else {
unreachable;
switch (math.order(lhs[i], rhs[i])) {
.eq => continue,
.lt => return .lt,
.gt => return .gt,
}
}

if (lhs.len == rhs.len) {
return Compare.Equal;
} else if (lhs.len < rhs.len) {
return Compare.LessThan;
} else if (lhs.len > rhs.len) {
return Compare.GreaterThan;
}
unreachable;
return math.order(lhs.len, rhs.len);
}

test "mem.compare" {
testing.expect(compare(u8, "abcd", "bee") == Compare.LessThan);
testing.expect(compare(u8, "abc", "abc") == Compare.Equal);
testing.expect(compare(u8, "abc", "abc0") == Compare.LessThan);
testing.expect(compare(u8, "", "") == Compare.Equal);
testing.expect(compare(u8, "", "a") == Compare.LessThan);
test "order" {
testing.expect(order(u8, "abcd", "bee") == .lt);
testing.expect(order(u8, "abc", "abc") == .eq);
testing.expect(order(u8, "abc", "abc0") == .lt);
testing.expect(order(u8, "", "") == .eq);
testing.expect(order(u8, "", "a") == .lt);
}

/// Returns true if lhs < rhs, false otherwise
pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool {
var result = compare(T, lhs, rhs);
if (result == Compare.LessThan) {
return true;
} else
return false;
return order(T, lhs, rhs) == .lt;
}

test "mem.lessThan" {
Expand Down
38 changes: 20 additions & 18 deletions lib/std/rb.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std.zig");
const assert = std.debug.assert;
const testing = std.testing;
const mem = std.mem; // For mem.Compare
const Order = std.math.Order;

const Color = enum(u1) {
Black,
Expand Down Expand Up @@ -132,7 +132,7 @@ pub const Node = struct {

pub const Tree = struct {
root: ?*Node,
compareFn: fn (*Node, *Node) mem.Compare,
compareFn: fn (*Node, *Node) Order,

/// If you have a need for a version that caches this, please file a bug.
pub fn first(tree: *Tree) ?*Node {
Expand Down Expand Up @@ -389,7 +389,7 @@ pub const Tree = struct {
var new = newconst;

// I assume this can get optimized out if the caller already knows.
if (tree.compareFn(old, new) != mem.Compare.Equal) return ReplaceError.NotEqual;
if (tree.compareFn(old, new) != .eq) return ReplaceError.NotEqual;

if (old.getParent()) |parent| {
parent.setChild(new, parent.left == old);
Expand All @@ -404,7 +404,7 @@ pub const Tree = struct {
new.* = old.*;
}

pub fn init(tree: *Tree, f: fn (*Node, *Node) mem.Compare) void {
pub fn init(tree: *Tree, f: fn (*Node, *Node) Order) void {
tree.root = null;
tree.compareFn = f;
}
Expand Down Expand Up @@ -469,19 +469,21 @@ fn doLookup(key: *Node, tree: *Tree, pparent: *?*Node, is_left: *bool) ?*Node {
is_left.* = false;

while (maybe_node) |node| {
var res: mem.Compare = tree.compareFn(node, key);
if (res == mem.Compare.Equal) {
const res = tree.compareFn(node, key);
if (res == .eq) {
return node;
}
pparent.* = node;
if (res == mem.Compare.GreaterThan) {
is_left.* = true;
maybe_node = node.left;
} else if (res == mem.Compare.LessThan) {
is_left.* = false;
maybe_node = node.right;
} else {
unreachable;
switch (res) {
.gt => {
is_left.* = true;
maybe_node = node.left;
},
.lt => {
is_left.* = false;
maybe_node = node.right;
},
.eq => unreachable, // handled above
}
}
return null;
Expand All @@ -496,16 +498,16 @@ fn testGetNumber(node: *Node) *testNumber {
return @fieldParentPtr(testNumber, "node", node);
}

fn testCompare(l: *Node, r: *Node) mem.Compare {
fn testCompare(l: *Node, r: *Node) Order {
var left = testGetNumber(l);
var right = testGetNumber(r);

if (left.value < right.value) {
return mem.Compare.LessThan;
return .lt;
} else if (left.value == right.value) {
return mem.Compare.Equal;
return .eq;
} else if (left.value > right.value) {
return mem.Compare.GreaterThan;
return .gt;
}
unreachable;
}
Expand Down
Loading