authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-13 11:39:32-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-13 11:39:32-04:00
log8a9289996ab8ed7da2f7e0c6df9fe2bd9a3e0b7b
tree28aa8644ce1ea7c3756c88a9948ad2d11a9b24c8
parent24d5ec078355d68e3f1002220fd284b1ff02a465
parent98183e47436699f6e5eab200061c46eec342806e
signaturelock-open Commit is signed but in an unrecognized format.

Merge remote-tracking branch 'origin/master' into rewrite-coroutines


16 files changed, 207 insertions(+), 56 deletions(-)

README.md+2-1
......@@ -1,6 +1,6 @@
11![ZIG](https://ziglang.org/zig-logo.svg)
22
3Zig is an open-source programming language designed for **robustness**,
3A general-purpose programming language designed for **robustness**,
44**optimality**, and **maintainability**.
55
66## Resources
......@@ -10,6 +10,7 @@ Zig is an open-source programming language designed for **robustness**,
1010 * [Community](https://github.com/ziglang/zig/wiki/Community)
1111 * [Contributing](https://github.com/ziglang/zig/blob/master/CONTRIBUTING.md)
1212 * [Frequently Asked Questions](https://github.com/ziglang/zig/wiki/FAQ)
13 * [Community Projects](https://github.com/ziglang/zig/wiki/Community-Projects)
1314
1415## Building from Source
1516
src/all_types.hpp+2-2
......@@ -3745,8 +3745,8 @@ static const size_t slice_len_index = 1;
37453745static const size_t maybe_child_index = 0;
37463746static const size_t maybe_null_index = 1;
37473747
3748static const size_t err_union_err_index = 0;
3749static const size_t err_union_payload_index = 1;
3748static const size_t err_union_payload_index = 0;
3749static const size_t err_union_err_index = 1;
37503750
37513751// label (grep this): [coro_frame_struct_layout]
37523752static const size_t coro_fn_ptr_index = 0;
src/analyze.cpp+7-6
......@@ -7350,20 +7350,21 @@ static void resolve_llvm_types_error_union(CodeGen *g, ZigType *type) {
73507350 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
73517351 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
73527352
7353 ZigLLVMDIType *di_element_types[] = {
7354 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
7353 ZigLLVMDIType *di_element_types[2];
7354 di_element_types[err_union_err_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
7355 ZigLLVMTypeToScope(type->llvm_di_type),
73557356 "tag", di_file, line,
73567357 tag_debug_size_in_bits,
73577358 tag_debug_align_in_bits,
73587359 tag_offset_in_bits,
7359 ZigLLVM_DIFlags_Zero, get_llvm_di_type(g, err_set_type)),
7360 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
7360 ZigLLVM_DIFlags_Zero, get_llvm_di_type(g, err_set_type));
7361 di_element_types[err_union_payload_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
7362 ZigLLVMTypeToScope(type->llvm_di_type),
73617363 "value", di_file, line,
73627364 value_debug_size_in_bits,
73637365 value_debug_align_in_bits,
73647366 value_offset_in_bits,
7365 ZigLLVM_DIFlags_Zero, get_llvm_di_type(g, payload_type)),
7366 };
7367 ZigLLVM_DIFlags_Zero, get_llvm_di_type(g, payload_type));
73677368
73687369 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
73697370 compile_unit_scope,
src/codegen.cpp+10-24
......@@ -6690,29 +6690,12 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
66906690 err_payload_value = gen_const_val(g, payload_val, "");
66916691 make_unnamed_struct = is_llvm_value_unnamed_type(g, payload_val->type, err_payload_value);
66926692 }
6693 LLVMValueRef fields[2];
6694 fields[err_union_err_index] = err_tag_value;
6695 fields[err_union_payload_index] = err_payload_value;
66936696 if (make_unnamed_struct) {
6694 uint64_t payload_off = LLVMOffsetOfElement(g->target_data_ref, get_llvm_type(g, type_entry), 1);
6695 uint64_t err_sz = LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(err_tag_value));
6696 unsigned pad_sz = payload_off - err_sz;
6697 if (pad_sz == 0) {
6698 LLVMValueRef fields[] = {
6699 err_tag_value,
6700 err_payload_value,
6701 };
6702 return LLVMConstStruct(fields, 2, false);
6703 } else {
6704 LLVMValueRef fields[] = {
6705 err_tag_value,
6706 LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_sz)),
6707 err_payload_value,
6708 };
6709 return LLVMConstStruct(fields, 3, false);
6710 }
6697 return LLVMConstStruct(fields, 2, false);
67116698 } else {
6712 LLVMValueRef fields[] = {
6713 err_tag_value,
6714 err_payload_value,
6715 };
67166699 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
67176700 }
67186701 }
......@@ -8627,15 +8610,18 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
86278610 }
86288611 }
86298612
8613 //note(dimenus): appending libc headers before c_headers breaks intrinsics
8614 //and other compiler specific items
8615 // According to Rich Felker libc headers are supposed to go before C language headers.
8616 args.append("-isystem");
8617 args.append(buf_ptr(g->zig_c_headers_dir));
8618
86308619 for (size_t i = 0; i < g->libc_include_dir_len; i += 1) {
86318620 Buf *include_dir = g->libc_include_dir_list[i];
86328621 args.append("-isystem");
86338622 args.append(buf_ptr(include_dir));
86348623 }
86358624
8636 // According to Rich Felker libc headers are supposed to go before C language headers.
8637 args.append("-isystem");
8638 args.append(buf_ptr(g->zig_c_headers_dir));
86398625
86408626 if (g->zig_target->is_native) {
86418627 args.append("-march=native");
src/ir.cpp+12-7
......@@ -9098,6 +9098,9 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
90989098 bool const_val_is_float = (const_val->type->id == ZigTypeIdFloat || const_val->type->id == ZigTypeIdComptimeFloat);
90999099 assert(const_val_is_int || const_val_is_float);
91009100
9101 if (const_val_is_int && other_type->id == ZigTypeIdComptimeFloat) {
9102 return true;
9103 }
91019104 if (other_type->id == ZigTypeIdFloat) {
91029105 if (const_val->type->id == ZigTypeIdComptimeInt || const_val->type->id == ZigTypeIdComptimeFloat) {
91039106 return true;
......@@ -11329,7 +11332,6 @@ static IrInstruction *ir_analyze_enum_to_int(IrAnalyze *ira, IrInstruction *sour
1132911332 if (enum_type->data.enumeration.layout == ContainerLayoutAuto &&
1133011333 enum_type->data.enumeration.src_field_count == 1)
1133111334 {
11332 assert(tag_type == ira->codegen->builtin_types.entry_num_lit_int);
1133311335 IrInstruction *result = ir_const(ira, source_instr, tag_type);
1133411336 init_const_bigint(&result->value, tag_type,
1133511337 &enum_type->data.enumeration.fields[0].value);
......@@ -20697,12 +20699,15 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
2069720699 }
2069820700 for (size_t i = 0; i < errors_len; i += 1) {
2069920701 Stage2ErrorMsg *clang_err = &errors_ptr[i];
20700 ErrorMsg *err_msg = err_msg_create_with_offset(
20701 clang_err->filename_ptr ?
20702 buf_create_from_mem(clang_err->filename_ptr, clang_err->filename_len) : buf_alloc(),
20703 clang_err->line, clang_err->column, clang_err->offset, clang_err->source,
20704 buf_create_from_mem(clang_err->msg_ptr, clang_err->msg_len));
20705 err_msg_add_note(parent_err_msg, err_msg);
20702 // Clang can emit "too many errors, stopping now", in which case `source` and `filename_ptr` are null
20703 if (clang_err->source && clang_err->filename_ptr) {
20704 ErrorMsg *err_msg = err_msg_create_with_offset(
20705 clang_err->filename_ptr ?
20706 buf_create_from_mem(clang_err->filename_ptr, clang_err->filename_len) : buf_alloc(),
20707 clang_err->line, clang_err->column, clang_err->offset, clang_err->source,
20708 buf_create_from_mem(clang_err->msg_ptr, clang_err->msg_len));
20709 err_msg_add_note(parent_err_msg, err_msg);
20710 }
2070620711 }
2070720712
2070820713 return ira->codegen->invalid_instruction;
std/c/freebsd.zig+1
......@@ -6,3 +6,4 @@ pub const _errno = __error;
66
77pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
88pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
9pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int;
std/math.zig+67-3
......@@ -242,12 +242,76 @@ pub fn floatExponentBits(comptime T: type) comptime_int {
242242 };
243243}
244244
245pub fn min(x: var, y: var) @typeOf(x + y) {
246 return if (x < y) x else y;
245/// Given two types, returns the smallest one which is capable of holding the
246/// full range of the minimum value.
247pub fn Min(comptime A: type, comptime B: type) type {
248 switch (@typeInfo(A)) {
249 .Int => |a_info| switch (@typeInfo(B)) {
250 .Int => |b_info| if (!a_info.is_signed and !b_info.is_signed) {
251 if (a_info.bits < b_info.bits) {
252 return A;
253 } else {
254 return B;
255 }
256 },
257 else => {},
258 },
259 else => {},
260 }
261 return @typeOf(A(0) + B(0));
262}
263
264/// Returns the smaller number. When one of the parameter's type's full range fits in the other,
265/// the return type is the smaller type.
266pub fn min(x: var, y: var) Min(@typeOf(x), @typeOf(y)) {
267 const Result = Min(@typeOf(x), @typeOf(y));
268 if (x < y) {
269 // TODO Zig should allow this as an implicit cast because x is immutable and in this
270 // scope it is known to fit in the return type.
271 switch (@typeInfo(Result)) {
272 .Int => return @intCast(Result, x),
273 else => return x,
274 }
275 } else {
276 // TODO Zig should allow this as an implicit cast because y is immutable and in this
277 // scope it is known to fit in the return type.
278 switch (@typeInfo(Result)) {
279 .Int => return @intCast(Result, y),
280 else => return y,
281 }
282 }
247283}
248284
249285test "math.min" {
250286 testing.expect(min(i32(-1), i32(2)) == -1);
287 {
288 var a: u16 = 999;
289 var b: u32 = 10;
290 var result = min(a, b);
291 testing.expect(@typeOf(result) == u16);
292 testing.expect(result == 10);
293 }
294 {
295 var a: f64 = 10.34;
296 var b: f32 = 999.12;
297 var result = min(a, b);
298 testing.expect(@typeOf(result) == f64);
299 testing.expect(result == 10.34);
300 }
301 {
302 var a: i8 = -127;
303 var b: i16 = -200;
304 var result = min(a, b);
305 testing.expect(@typeOf(result) == i16);
306 testing.expect(result == -200);
307 }
308 {
309 const a = 10.34;
310 var b: f32 = 999.12;
311 var result = min(a, b);
312 testing.expect(@typeOf(result) == f32);
313 testing.expect(result == 10.34);
314 }
251315}
252316
253317pub fn max(x: var, y: var) @typeOf(x + y) {
......@@ -309,7 +373,7 @@ test "math.shl" {
309373}
310374
311375/// Shifts right. Overflowed bits are truncated.
312/// A negative shift amount results in a lefft shift.
376/// A negative shift amount results in a left shift.
313377pub fn shr(comptime T: type, a: T, shift_amt: var) T {
314378 const abs_shift_amt = absCast(shift_amt);
315379 const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
std/os.zig+13
......@@ -120,6 +120,19 @@ pub fn getrandom(buf: []u8) GetRandomError!void {
120120 }
121121 }
122122 }
123 if (freebsd.is_the_target) {
124 while (true) {
125 const err = std.c.getErrno(std.c.getrandom(buf.ptr, buf.len, 0));
126
127 switch (err) {
128 0 => return,
129 EINVAL => unreachable,
130 EFAULT => unreachable,
131 EINTR => continue,
132 else => return unexpectedErrno(err),
133 }
134 }
135 }
123136 if (wasi.is_the_target) {
124137 switch (wasi.random_get(buf.ptr, buf.len)) {
125138 0 => return,
std/os/bits/netbsd.zig+59
......@@ -760,3 +760,62 @@ pub const stack_t = extern struct {
760760 ss_size: isize,
761761 ss_flags: i32,
762762};
763
764pub const S_IFMT = 0o170000;
765
766pub const S_IFIFO = 0o010000;
767pub const S_IFCHR = 0o020000;
768pub const S_IFDIR = 0o040000;
769pub const S_IFBLK = 0o060000;
770pub const S_IFREG = 0o100000;
771pub const S_IFLNK = 0o120000;
772pub const S_IFSOCK = 0o140000;
773pub const S_IFWHT = 0o160000;
774
775pub const S_ISUID = 0o4000;
776pub const S_ISGID = 0o2000;
777pub const S_ISVTX = 0o1000;
778pub const S_IRWXU = 0o700;
779pub const S_IRUSR = 0o400;
780pub const S_IWUSR = 0o200;
781pub const S_IXUSR = 0o100;
782pub const S_IRWXG = 0o070;
783pub const S_IRGRP = 0o040;
784pub const S_IWGRP = 0o020;
785pub const S_IXGRP = 0o010;
786pub const S_IRWXO = 0o007;
787pub const S_IROTH = 0o004;
788pub const S_IWOTH = 0o002;
789pub const S_IXOTH = 0o001;
790
791pub fn S_ISFIFO(m: u32) bool {
792 return m & S_IFMT == S_IFIFO;
793}
794
795pub fn S_ISCHR(m: u32) bool {
796 return m & S_IFMT == S_IFCHR;
797}
798
799pub fn S_ISDIR(m: u32) bool {
800 return m & S_IFMT == S_IFDIR;
801}
802
803pub fn S_ISBLK(m: u32) bool {
804 return m & S_IFMT == S_IFBLK;
805}
806
807pub fn S_ISREG(m: u32) bool {
808 return m & S_IFMT == S_IFREG;
809}
810
811pub fn S_ISLNK(m: u32) bool {
812 return m & S_IFMT == S_IFLNK;
813}
814
815pub fn S_ISSOCK(m: u32) bool {
816 return m & S_IFMT == S_IFSOCK;
817}
818
819pub fn S_IWHT(m: u32) bool {
820 return m & S_IFMT == S_IFWHT;
821}
std/os/netbsd.zig+1
......@@ -2,3 +2,4 @@ const builtin = @import("builtin");
22const std = @import("../std.zig");
33pub const is_the_target = builtin.os == .netbsd;
44pub usingnamespace std.c;
5pub usingnamespace @import("bits.zig");
std/os/windows.zig+12-3
......@@ -138,10 +138,19 @@ pub const RtlGenRandomError = error{Unexpected};
138138/// https://github.com/rust-lang-nursery/rand/issues/111
139139/// https://bugzilla.mozilla.org/show_bug.cgi?id=504270
140140pub fn RtlGenRandom(output: []u8) RtlGenRandomError!void {
141 if (advapi32.RtlGenRandom(output.ptr, output.len) == 0) {
142 switch (kernel32.GetLastError()) {
143 else => |err| return unexpectedError(err),
141 var total_read: usize = 0;
142 var buff: []u8 = output[0..];
143 const max_read_size: ULONG = maxInt(ULONG);
144
145 while (total_read < output.len) {
146 const to_read: ULONG = math.min(buff.len, max_read_size);
147
148 if (advapi32.RtlGenRandom(buff.ptr, to_read) == 0) {
149 return unexpectedError(kernel32.GetLastError());
144150 }
151
152 total_read += to_read;
153 buff = buff[to_read..];
145154 }
146155}
147156
std/os/windows/advapi32.zig+1-1
......@@ -19,5 +19,5 @@ pub extern "advapi32" stdcallcc fn RegQueryValueExW(
1919
2020// RtlGenRandom is known as SystemFunction036 under advapi32
2121// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */
22pub extern "advapi32" stdcallcc fn SystemFunction036(output: [*]u8, length: usize) BOOL;
22pub extern "advapi32" stdcallcc fn SystemFunction036(output: [*]u8, length: ULONG) BOOL;
2323pub const RtlGenRandom = SystemFunction036;
test/compile_errors.zig-8
......@@ -3292,14 +3292,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
32923292 "tmp.zig:3:17: note: value 8 cannot fit into type u3",
32933293 );
32943294
3295 cases.add(
3296 "incompatible number literals",
3297 \\const x = 2 == 2.0;
3298 \\export fn entry() usize { return @sizeOf(@typeOf(x)); }
3299 ,
3300 "tmp.zig:1:11: error: integer value 2 cannot be implicitly casted to type 'comptime_float'",
3301 );
3302
33033295 cases.add(
33043296 "missing function call param",
33053297 \\const Foo = struct {
test/stage1/behavior/cast.zig+6-1
......@@ -508,7 +508,7 @@ test "peer type resolution: unreachable, null, slice" {
508508}
509509
510510test "peer type resolution: unreachable, error set, unreachable" {
511 const Error = error {
511 const Error = error{
512512 FileDescriptorAlreadyPresentInSet,
513513 OperationCausesCircularLoop,
514514 FileDescriptorNotRegistered,
......@@ -529,3 +529,8 @@ test "peer type resolution: unreachable, error set, unreachable" {
529529 };
530530 expect(transformed_err == error.SystemResources);
531531}
532
533test "implicit cast comptime_int to comptime_float" {
534 comptime expect(comptime_float(10) == f32(10));
535 expect(2 == 2.0);
536}
test/stage1/behavior/enum.zig+11
......@@ -982,3 +982,14 @@ test "enum literal casting to tagged union" {
982982 else => @panic("fail"),
983983 }
984984}
985
986test "enum with one member and custom tag type" {
987 const E = enum(u2) {
988 One,
989 };
990 expect(@enumToInt(E.One) == 0);
991 const E2 = enum(u2) {
992 One = 2,
993 };
994 expect(@enumToInt(E2.One) == 2);
995}
tools/process_headers.zig+3
......@@ -504,6 +504,9 @@ const Contents = struct {
504504 }
505505};
506506
507comptime {
508 @compileError("the behavior of std.AutoHashMap changed and []const u8 will be treated as a pointer. will need to update the hash maps to actually do some kind of hashing on the slices.");
509}
507510const HashToContents = std.AutoHashMap([]const u8, Contents);
508511const TargetToHash = std.HashMap(DestTarget, []const u8, DestTarget.hash, DestTarget.eql);
509512const PathTable = std.AutoHashMap([]const u8, *TargetToHash);