Merge pull request #11945 from ziglang/stage2-std

stage2 fixes towards standard library tests passing
This commit is contained in:
Andrew Kelley 2022-06-28 01:34:46 -04:00 committed by GitHub
commit 975a660807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 116 additions and 31 deletions

View File

@ -341,7 +341,7 @@ pub const Fe = struct {
}
/// Square a field element `n` times
inline fn sqn(a: Fe, comptime n: comptime_int) Fe {
fn sqn(a: Fe, n: usize) Fe {
var i: usize = 0;
var fe = a;
while (i < n) : (i += 1) {
@ -390,13 +390,12 @@ pub const Fe = struct {
const _11 = a.mul(a.sq());
const _1111 = _11.mul(_11.sq().sq());
const _11111111 = _1111.mul(_1111.sq().sq().sq().sq());
var t = _11111111.sqn(2).mul(_11);
const u = t;
t = t.sqn(10).mul(u).sqn(10).mul(u);
t = t.sqn(30).mul(t);
t = t.sqn(60).mul(t);
t = t.sqn(120).mul(t).sqn(10).mul(u).sqn(3).mul(_11).sq();
return @bitCast(bool, @truncate(u1, ~(t.toBytes()[1] & 1)));
const u = _11111111.sqn(2).mul(_11);
const t = u.sqn(10).mul(u).sqn(10).mul(u);
const t2 = t.sqn(30).mul(t);
const t3 = t2.sqn(60).mul(t2);
const t4 = t3.sqn(120).mul(t3).sqn(10).mul(u).sqn(3).mul(_11).sq();
return @bitCast(bool, @truncate(u1, ~(t4.toBytes()[1] & 1)));
}
fn uncheckedSqrt(x2: Fe) Fe {

View File

@ -98,8 +98,13 @@ pub fn syscall6(
);
}
const CloneFn = switch (@import("builtin").zig_backend) {
.stage1 => fn (arg: usize) callconv(.C) u8,
else => *const fn (arg: usize) callconv(.C) u8,
};
/// This matches the libc clone function.
pub extern fn clone(func: fn (arg: usize) callconv(.C) u8, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub const restore = restore_rt;

View File

@ -118,8 +118,13 @@ pub fn socketcall(call: usize, args: [*]usize) usize {
);
}
const CloneFn = switch (@import("builtin").zig_backend) {
.stage1 => fn (arg: usize) callconv(.C) u8,
else => *const fn (arg: usize) callconv(.C) u8,
};
/// This matches the libc clone function.
pub extern fn clone(func: fn (arg: usize) callconv(.C) u8, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub fn restore() callconv(.Naked) void {
return asm volatile ("int $0x80"

View File

@ -190,8 +190,13 @@ pub fn syscall7(
);
}
const CloneFn = switch (@import("builtin").zig_backend) {
.stage1 => fn (arg: usize) callconv(.C) u8,
else => *const fn (arg: usize) callconv(.C) u8,
};
/// This matches the libc clone function.
pub extern fn clone(func: fn (arg: usize) callconv(.C) u8, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub fn restore() callconv(.Naked) void {
return asm volatile ("syscall"

View File

@ -126,8 +126,13 @@ pub fn syscall6(
);
}
const CloneFn = switch (@import("builtin").zig_backend) {
.stage1 => fn (arg: usize) callconv(.C) u8,
else => *const fn (arg: usize) callconv(.C) u8,
};
/// This matches the libc clone function.
pub extern fn clone(func: fn (arg: usize) callconv(.C) u8, stack: usize, flags: usize, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub extern fn clone(func: CloneFn, stack: usize, flags: usize, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub const restore = restore_rt;

View File

@ -126,8 +126,13 @@ pub fn syscall6(
);
}
const CloneFn = switch (@import("builtin").zig_backend) {
.stage1 => fn (arg: usize) callconv(.C) u8,
else => *const fn (arg: usize) callconv(.C) u8,
};
/// This matches the libc clone function.
pub extern fn clone(func: fn (arg: usize) callconv(.C) u8, stack: usize, flags: usize, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub extern fn clone(func: CloneFn, stack: usize, flags: usize, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub const restore = restore_rt;

View File

@ -95,7 +95,12 @@ pub fn syscall6(
);
}
pub extern fn clone(func: fn (arg: usize) callconv(.C) u8, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
const CloneFn = switch (@import("builtin").zig_backend) {
.stage1 => fn (arg: usize) callconv(.C) u8,
else => *const fn (arg: usize) callconv(.C) u8,
};
pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub const restore = restore_rt;

View File

@ -178,8 +178,13 @@ pub fn syscall6(
);
}
const CloneFn = switch (@import("builtin").zig_backend) {
.stage1 => fn (arg: usize) callconv(.C) u8,
else => *const fn (arg: usize) callconv(.C) u8,
};
/// This matches the libc clone function.
pub extern fn clone(func: fn (arg: usize) callconv(.C) u8, stack: usize, flags: usize, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub extern fn clone(func: CloneFn, stack: usize, flags: usize, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub const restore = restore_rt;

View File

@ -9,10 +9,25 @@ const testing = std.testing;
const native_os = builtin.os;
const have_ifnamesize = @hasDecl(os.system, "IFNAMESIZE");
pub const ResolveScopeIdError = error{
NameTooLong,
PermissionDenied,
AddressFamilyNotSupported,
ProtocolFamilyNotAvailable,
ProcessFdQuotaExceeded,
SystemFdQuotaExceeded,
SystemResources,
ProtocolNotSupported,
SocketTypeNotSupported,
InterfaceNotFound,
FileSystem,
Unexpected,
};
/// Resolves a network interface name into a scope/zone ID. It returns
/// an error if either resolution fails, or if the interface name is
/// too long.
pub fn resolveScopeId(name: []const u8) !u32 {
pub fn resolveScopeId(name: []const u8) ResolveScopeIdError!u32 {
if (have_ifnamesize) {
if (name.len >= os.IFNAMESIZE) return error.NameTooLong;

View File

@ -4607,6 +4607,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
DeclAdapter{ .mod = mod },
Namespace.DeclContext{ .module = mod },
);
const comp = mod.comp;
if (!gop.found_existing) {
const new_decl_index = try mod.allocateNewDecl(namespace, decl_node, iter.parent_decl.src_scope);
const new_decl = mod.declPtr(new_decl_index);
@ -4625,7 +4626,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
1 => blk: {
// test decl with no name. Skip the part where we check against
// the test name filter.
if (!mod.comp.bin_file.options.is_test) break :blk false;
if (!comp.bin_file.options.is_test) break :blk false;
if (decl_pkg != mod.main_pkg) {
if (!mod.main_pkg_in_std) break :blk false;
const std_pkg = mod.main_pkg.table.get("std").?;
@ -4636,19 +4637,23 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
},
else => blk: {
if (!is_named_test) break :blk false;
if (!mod.comp.bin_file.options.is_test) break :blk false;
if (!comp.bin_file.options.is_test) break :blk false;
if (decl_pkg != mod.main_pkg) {
if (!mod.main_pkg_in_std) break :blk false;
const std_pkg = mod.main_pkg.table.get("std").?;
if (std_pkg != decl_pkg) break :blk false;
}
// TODO check the name against --test-filter
if (comp.test_filter) |test_filter| {
if (mem.indexOf(u8, decl_name, test_filter) == null) {
break :blk false;
}
}
try mod.test_functions.put(gpa, new_decl_index, {});
break :blk true;
},
};
if (want_analysis) {
mod.comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl_index });
comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = new_decl_index });
}
new_decl.is_pub = is_pub;
new_decl.is_exported = is_exported;
@ -4675,24 +4680,24 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
decl.has_linksection_or_addrspace = has_linksection_or_addrspace;
decl.zir_decl_index = @intCast(u32, decl_sub_index);
if (decl.getFunction()) |_| {
switch (mod.comp.bin_file.tag) {
switch (comp.bin_file.tag) {
.coff => {
// TODO Implement for COFF
},
.elf => if (decl.fn_link.elf.len != 0) {
// TODO Look into detecting when this would be unnecessary by storing enough state
// in `Decl` to notice that the line number did not change.
mod.comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index });
comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index });
},
.macho => if (decl.fn_link.macho.len != 0) {
// TODO Look into detecting when this would be unnecessary by storing enough state
// in `Decl` to notice that the line number did not change.
mod.comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index });
comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index });
},
.plan9 => {
// TODO Look into detecting when this would be unnecessary by storing enough state
// in `Decl` to notice that the line number did not change.
mod.comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index });
comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index });
},
.c, .wasm, .spirv, .nvptx => {},
}

View File

@ -4197,7 +4197,7 @@ pub const FuncGen = struct {
}
var it = iterateParamTypes(self.dg, fn_info);
while (it.next()) |lowering| switch (lowering) {
while (it.nextCall(self, args)) |lowering| switch (lowering) {
.no_bits => continue,
.byval => {
const arg = args[it.zig_index - 1];
@ -7212,11 +7212,17 @@ pub const FuncGen = struct {
fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
if (self.liveness.isUnused(inst)) return null;
const llvm_usize = try self.dg.lowerType(Type.usize);
const target = self.dg.module.getTarget();
if (!target_util.supportsReturnAddress(target)) {
// https://github.com/ziglang/zig/issues/11946
return llvm_usize.constNull();
}
const llvm_i32 = self.context.intType(32);
const llvm_fn = self.getIntrinsic("llvm.returnaddress", &.{});
const params = [_]*const llvm.Value{llvm_i32.constNull()};
const ptr_val = self.builder.buildCall(llvm_fn, &params, params.len, .Fast, .Auto, "");
const llvm_usize = try self.dg.lowerType(Type.usize);
return self.builder.buildPtrToInt(ptr_val, llvm_usize, "");
}
@ -8021,7 +8027,6 @@ pub const FuncGen = struct {
assert(union_obj.haveFieldTypes());
const field = union_obj.fields.values()[extra.field_index];
const field_llvm_ty = try self.dg.lowerType(field.ty);
const tag_llvm_ty = try self.dg.lowerType(union_obj.tag_ty);
const field_size = field.ty.abiSize(target);
const field_align = field.normalAlignment(target);
@ -8044,6 +8049,7 @@ pub const FuncGen = struct {
const fields: [1]*const llvm.Type = .{payload};
break :t self.context.structType(&fields, fields.len, .False);
}
const tag_llvm_ty = try self.dg.lowerType(union_obj.tag_ty);
var fields: [3]*const llvm.Type = undefined;
var fields_len: c_uint = 2;
if (layout.tag_align >= layout.payload_align) {
@ -8100,6 +8106,7 @@ pub const FuncGen = struct {
index_type.constInt(@boolToInt(layout.tag_align < layout.payload_align), .False),
};
const field_ptr = self.builder.buildInBoundsGEP(casted_ptr, &indices, indices.len, "");
const tag_llvm_ty = try self.dg.lowerType(union_obj.tag_ty);
const llvm_tag = tag_llvm_ty.constInt(extra.field_index, .False);
const store_inst = self.builder.buildStore(llvm_tag, field_ptr);
store_inst.setAlignment(union_obj.tag_ty.abiAlignment(target));
@ -9004,10 +9011,26 @@ const ParamTypeIterator = struct {
slice,
};
fn next(it: *ParamTypeIterator) ?Lowering {
pub fn next(it: *ParamTypeIterator) ?Lowering {
if (it.zig_index >= it.fn_info.param_types.len) return null;
const ty = it.fn_info.param_types[it.zig_index];
return nextInner(it, ty);
}
/// `airCall` uses this instead of `next` so that it can take into account variadic functions.
pub fn nextCall(it: *ParamTypeIterator, fg: *FuncGen, args: []const Air.Inst.Ref) ?Lowering {
if (it.zig_index >= it.fn_info.param_types.len) {
if (it.zig_index >= args.len) {
return null;
} else {
return nextInner(it, fg.air.typeOf(args[it.zig_index]));
}
} else {
return nextInner(it, it.fn_info.param_types[it.zig_index]);
}
}
fn nextInner(it: *ParamTypeIterator, ty: Type) ?Lowering {
if (!ty.hasRuntimeBitsIgnoreComptime()) {
it.zig_index += 1;
return .no_bits;

View File

@ -6764,8 +6764,8 @@ static LLVMValueRef ir_render_return_address(CodeGen *g, Stage1Air *executable,
Stage1AirInstReturnAddress *instruction)
{
if ((target_is_wasm(g->zig_target) && g->zig_target->os != OsEmscripten) || target_is_bpf(g->zig_target)) {
// I got this error from LLVM 10:
// "Non-Emscripten WebAssembly hasn't implemented __builtin_return_address"
// LLVM 13 reports "Non-Emscripten WebAssembly hasn't implemented __builtin_return_address"
// https://github.com/ziglang/zig/issues/11946
return LLVMConstNull(get_llvm_type(g, instruction->base.value->type));
}

View File

@ -283,6 +283,14 @@ pub fn supportsStackProbing(target: std.Target) bool {
(target.cpu.arch == .i386 or target.cpu.arch == .x86_64);
}
pub fn supportsReturnAddress(target: std.Target) bool {
return switch (target.cpu.arch) {
.wasm32, .wasm64 => target.os.tag == .emscripten,
.bpfel, .bpfeb => false,
else => true,
};
}
pub fn osToLLVM(os_tag: std.Target.Os.Tag) llvm.OSType {
return switch (os_tag) {
.freestanding, .other, .opencl, .glsl450, .vulkan, .plan9 => .UnknownOS,