authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-27 22:04:00-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-07-27 22:04:00-07:00
logc650ccfca719b695fe7752f9126e8dbcc2ab4d6d
treedc112b2472f53e44e377dee68d30db02caed8c03
parentdfc7493dcb049788b92137ca09b8bd47cee23865
parent3ccb6a0cd4d4f436a6009ed614436bb3e2e27a7c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12265 from ziglang/stage3-run-translated-c

CI: test-run-translated-c with stage3

8 files changed, 194 insertions(+), 88 deletions(-)

ci/zinc/linux_test.sh+13-13
......@@ -52,22 +52,22 @@ stage2/bin/zig build -p stage3 -Dstatic-llvm -Dtarget=native-native-musl --searc
5252stage3/bin/zig build # test building self-hosted without LLVM
5353stage3/bin/zig build -Dtarget=arm-linux-musleabihf # test building self-hosted for 32-bit arm
5454
55stage3/bin/zig build test-compiler-rt -fqemu -fwasmtime -Denable-llvm
56stage3/bin/zig build test-behavior -fqemu -fwasmtime -Denable-llvm
57stage3/bin/zig build test-std -fqemu -fwasmtime -Denable-llvm
58stage3/bin/zig build test-universal-libc -fqemu -fwasmtime -Denable-llvm
59stage3/bin/zig build test-compare-output -fqemu -fwasmtime -Denable-llvm
60stage3/bin/zig build test-asm-link -fqemu -fwasmtime -Denable-llvm
61stage3/bin/zig build test-fmt -fqemu -fwasmtime -Denable-llvm
62stage3/bin/zig build test-translate-c -fqemu -fwasmtime -Denable-llvm
63stage3/bin/zig build test-standalone -fqemu -fwasmtime -Denable-llvm
64stage3/bin/zig build test-cli -fqemu -fwasmtime -Denable-llvm
55stage3/bin/zig build test-compiler-rt -fqemu -fwasmtime -Denable-llvm
56stage3/bin/zig build test-behavior -fqemu -fwasmtime -Denable-llvm
57stage3/bin/zig build test-std -fqemu -fwasmtime -Denable-llvm
58stage3/bin/zig build test-universal-libc -fqemu -fwasmtime -Denable-llvm
59stage3/bin/zig build test-compare-output -fqemu -fwasmtime -Denable-llvm
60stage3/bin/zig build test-asm-link -fqemu -fwasmtime -Denable-llvm
61stage3/bin/zig build test-fmt -fqemu -fwasmtime -Denable-llvm
62stage3/bin/zig build test-translate-c -fqemu -fwasmtime -Denable-llvm
63stage3/bin/zig build test-run-translated-c -fqemu -fwasmtime -Denable-llvm
64stage3/bin/zig build test-standalone -fqemu -fwasmtime -Denable-llvm
65stage3/bin/zig build test-cli -fqemu -fwasmtime -Denable-llvm
6566# https://github.com/ziglang/zig/issues/12144
66stage3/bin/zig build test-cases -fqemu -fwasmtime
67stage3/bin/zig build test-link -fqemu -fwasmtime -Denable-llvm
67stage3/bin/zig build test-cases -fqemu -fwasmtime
68stage3/bin/zig build test-link -fqemu -fwasmtime -Denable-llvm
6869
6970$STAGE1_ZIG build test-stack-traces -fqemu -fwasmtime
70$STAGE1_ZIG build test-run-translated-c -fqemu -fwasmtime
7171$STAGE1_ZIG build docs -fqemu -fwasmtime
7272
7373# Produce the experimental std lib documentation.
src/AstGen.zig+21-20
......@@ -10282,11 +10282,11 @@ const GenZir = struct {
1028210282 try astgen.extra.ensureUnusedCapacity(
1028310283 gpa,
1028410284 @typeInfo(Zir.Inst.FuncFancy).Struct.fields.len +
10285 fancyFnExprExtraLen(align_body, args.align_ref) +
10286 fancyFnExprExtraLen(addrspace_body, args.addrspace_ref) +
10287 fancyFnExprExtraLen(section_body, args.section_ref) +
10288 fancyFnExprExtraLen(cc_body, args.cc_ref) +
10289 fancyFnExprExtraLen(ret_body, ret_ref) +
10285 fancyFnExprExtraLen(astgen, align_body, args.align_ref) +
10286 fancyFnExprExtraLen(astgen, addrspace_body, args.addrspace_ref) +
10287 fancyFnExprExtraLen(astgen, section_body, args.section_ref) +
10288 fancyFnExprExtraLen(astgen, cc_body, args.cc_ref) +
10289 fancyFnExprExtraLen(astgen, ret_body, ret_ref) +
1029010290 body_len + src_locs.len +
1029110291 @boolToInt(args.lib_name != 0) +
1029210292 @boolToInt(args.noalias_bits != 0),
......@@ -10322,36 +10322,36 @@ const GenZir = struct {
1032210322
1032310323 const zir_datas = astgen.instructions.items(.data);
1032410324 if (align_body.len != 0) {
10325 astgen.extra.appendAssumeCapacity(@intCast(u32, align_body.len));
10326 astgen.extra.appendSliceAssumeCapacity(align_body);
10325 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, align_body));
10326 astgen.appendBodyWithFixups(align_body);
1032710327 zir_datas[align_body[align_body.len - 1]].@"break".block_inst = new_index;
1032810328 } else if (args.align_ref != .none) {
1032910329 astgen.extra.appendAssumeCapacity(@enumToInt(args.align_ref));
1033010330 }
1033110331 if (addrspace_body.len != 0) {
10332 astgen.extra.appendAssumeCapacity(@intCast(u32, addrspace_body.len));
10333 astgen.extra.appendSliceAssumeCapacity(addrspace_body);
10332 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, addrspace_body));
10333 astgen.appendBodyWithFixups(addrspace_body);
1033410334 zir_datas[addrspace_body[addrspace_body.len - 1]].@"break".block_inst = new_index;
1033510335 } else if (args.addrspace_ref != .none) {
1033610336 astgen.extra.appendAssumeCapacity(@enumToInt(args.addrspace_ref));
1033710337 }
1033810338 if (section_body.len != 0) {
10339 astgen.extra.appendAssumeCapacity(@intCast(u32, section_body.len));
10340 astgen.extra.appendSliceAssumeCapacity(section_body);
10339 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, section_body));
10340 astgen.appendBodyWithFixups(section_body);
1034110341 zir_datas[section_body[section_body.len - 1]].@"break".block_inst = new_index;
1034210342 } else if (args.section_ref != .none) {
1034310343 astgen.extra.appendAssumeCapacity(@enumToInt(args.section_ref));
1034410344 }
1034510345 if (cc_body.len != 0) {
10346 astgen.extra.appendAssumeCapacity(@intCast(u32, cc_body.len));
10347 astgen.extra.appendSliceAssumeCapacity(cc_body);
10346 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, cc_body));
10347 astgen.appendBodyWithFixups(cc_body);
1034810348 zir_datas[cc_body[cc_body.len - 1]].@"break".block_inst = new_index;
1034910349 } else if (args.cc_ref != .none) {
1035010350 astgen.extra.appendAssumeCapacity(@enumToInt(args.cc_ref));
1035110351 }
1035210352 if (ret_body.len != 0) {
10353 astgen.extra.appendAssumeCapacity(@intCast(u32, ret_body.len));
10354 astgen.extra.appendSliceAssumeCapacity(ret_body);
10353 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, ret_body));
10354 astgen.appendBodyWithFixups(ret_body);
1035510355 zir_datas[ret_body[ret_body.len - 1]].@"break".block_inst = new_index;
1035610356 } else if (ret_ref != .none) {
1035710357 astgen.extra.appendAssumeCapacity(@enumToInt(ret_ref));
......@@ -10389,11 +10389,12 @@ const GenZir = struct {
1038910389 try astgen.extra.ensureUnusedCapacity(
1039010390 gpa,
1039110391 @typeInfo(Zir.Inst.Func).Struct.fields.len + 1 +
10392 @maximum(ret_body.len, @boolToInt(ret_ref != .none)) +
10392 fancyFnExprExtraLen(astgen, ret_body, ret_ref) +
1039310393 body_len + src_locs.len,
1039410394 );
10395
1039510396 const ret_body_len = if (ret_body.len != 0)
10396 @intCast(u32, ret_body.len)
10397 countBodyLenAfterFixups(astgen, ret_body)
1039710398 else
1039810399 @boolToInt(ret_ref != .none);
1039910400
......@@ -10404,7 +10405,7 @@ const GenZir = struct {
1040410405 });
1040510406 const zir_datas = astgen.instructions.items(.data);
1040610407 if (ret_body.len != 0) {
10407 astgen.extra.appendSliceAssumeCapacity(ret_body);
10408 astgen.appendBodyWithFixups(ret_body);
1040810409 zir_datas[ret_body[ret_body.len - 1]].@"break".block_inst = new_index;
1040910410 } else if (ret_ref != .none) {
1041010411 astgen.extra.appendAssumeCapacity(@enumToInt(ret_ref));
......@@ -10435,10 +10436,10 @@ const GenZir = struct {
1043510436 }
1043610437 }
1043710438
10438 fn fancyFnExprExtraLen(body: []Zir.Inst.Index, ref: Zir.Inst.Ref) usize {
10439 fn fancyFnExprExtraLen(astgen: *AstGen, body: []Zir.Inst.Index, ref: Zir.Inst.Ref) u32 {
1043910440 // In the case of non-empty body, there is one for the body length,
1044010441 // and then one for each instruction.
10441 return body.len + @boolToInt(ref != .none);
10442 return countBodyLenAfterFixups(astgen, body) + @boolToInt(ref != .none);
1044210443 }
1044310444
1044410445 fn addVar(gz: *GenZir, args: struct {
src/codegen/llvm.zig+33-3
......@@ -3646,6 +3646,24 @@ pub const DeclGen = struct {
36463646 },
36473647 .Struct => {
36483648 const field_ty = parent_ty.structFieldType(field_index);
3649 if (parent_ty.containerLayout() == .Packed) {
3650 const llvm_usize = dg.context.intType(target.cpu.arch.ptrBitWidth());
3651 const base_addr = parent_llvm_ptr.constPtrToInt(llvm_usize);
3652 // count bits of fields before this one
3653 const prev_bits = b: {
3654 var b: usize = 0;
3655 for (parent_ty.structFields().values()[0..field_index]) |field| {
3656 if (field.is_comptime or !field.ty.hasRuntimeBitsIgnoreComptime()) continue;
3657 b += field.ty.bitSize(target);
3658 }
3659 break :b b;
3660 };
3661 const byte_offset = llvm_usize.constInt((prev_bits + 7) / 8, .False);
3662 const field_addr = base_addr.constAdd(byte_offset);
3663 bitcast_needed = false;
3664 const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0);
3665 break :blk field_addr.constIntToPtr(final_llvm_ty);
3666 }
36493667 bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module);
36503668
36513669 var ty_buf: Type.Payload.Pointer = undefined;
......@@ -4473,17 +4491,29 @@ pub const FuncGen = struct {
44734491 }
44744492 return null;
44754493 }
4494
44764495 const abi_ret_ty = try lowerFnRetTy(self.dg, fn_info);
4496 const ptr_abi_ty = abi_ret_ty.pointerType(0);
44774497 const operand = try self.resolveInst(un_op);
4498 const target = self.dg.module.getTarget();
4499 const alignment = ret_ty.abiAlignment(target);
4500
4501 if (isByRef(ret_ty)) {
4502 // operand is a pointer however self.ret_ptr is null so that means
4503 // we need to return a value.
4504 const casted_ptr = self.builder.buildBitCast(operand, ptr_abi_ty, "");
4505 const load_inst = self.builder.buildLoad(casted_ptr, "");
4506 load_inst.setAlignment(alignment);
4507 _ = self.builder.buildRet(load_inst);
4508 return null;
4509 }
4510
44784511 const llvm_ret_ty = operand.typeOf();
44794512 if (abi_ret_ty == llvm_ret_ty) {
44804513 _ = self.builder.buildRet(operand);
44814514 return null;
44824515 }
44834516
4484 const target = self.dg.module.getTarget();
4485 const alignment = ret_ty.abiAlignment(target);
4486 const ptr_abi_ty = abi_ret_ty.pointerType(0);
44874517 const rp = self.buildAlloca(llvm_ret_ty);
44884518 rp.setAlignment(alignment);
44894519 const store_inst = self.builder.buildStore(operand, rp);
src/codegen/llvm/bindings.zig+3
......@@ -169,6 +169,9 @@ pub const Value = opaque {
169169 pub const constNot = LLVMConstNot;
170170 extern fn LLVMConstNot(ConstantVal: *const Value) *const Value;
171171
172 pub const constAdd = LLVMConstAdd;
173 extern fn LLVMConstAdd(LHSConstant: *const Value, RHSConstant: *const Value) *const Value;
174
172175 pub const setWeak = LLVMSetWeak;
173176 extern fn LLVMSetWeak(CmpXchgInst: *const Value, IsWeak: Bool) void;
174177
test/behavior/fn.zig+14
......@@ -408,3 +408,17 @@ test "function with inferred error set but returning no error" {
408408 const return_ty = @typeInfo(@TypeOf(S.foo)).Fn.return_type.?;
409409 try expectEqual(0, @typeInfo(@typeInfo(return_ty).ErrorUnion.error_set).ErrorSet.?.len);
410410}
411
412test "import passed byref to function in return type" {
413 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
414 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
415
416 const S = struct {
417 fn get() @import("std").ArrayListUnmanaged(i32) {
418 var x: @import("std").ArrayListUnmanaged(i32) = .{};
419 return x;
420 }
421 };
422 var list = S.get();
423 try expect(list.items.len == 0);
424}
test/behavior/packed-struct.zig+22
......@@ -436,3 +436,25 @@ test "load pointer from packed struct" {
436436 try expect(i == 123);
437437 }
438438}
439
440test "@ptrToInt on a packed struct field" {
441 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
442 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
443 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
444 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
445 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
446
447 const S = struct {
448 const P = packed struct {
449 x: u8,
450 y: u8,
451 z: u32,
452 };
453 var p0: P = P{
454 .x = 1,
455 .y = 2,
456 .z = 0,
457 };
458 };
459 try expect(@ptrToInt(&S.p0.z) - @ptrToInt(&S.p0.x) == 2);
460}
test/behavior/union.zig+30
......@@ -1226,3 +1226,33 @@ test "extern union most-aligned field is smaller" {
12261226 var a: ?U = .{ .un = [_]u8{0} ** 110 };
12271227 try expect(a != null);
12281228}
1229
1230test "return an extern union from C calling convention" {
1231 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1232 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1233 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1234
1235 const namespace = struct {
1236 const S = extern struct {
1237 x: c_int,
1238 };
1239 const U = extern union {
1240 l: c_long,
1241 d: f64,
1242 s: S,
1243 };
1244
1245 fn bar(arg_u: U) callconv(.C) U {
1246 var u = arg_u;
1247 return u;
1248 }
1249 };
1250
1251 var u: namespace.U = namespace.U{
1252 .l = @as(c_long, 42),
1253 };
1254 u = namespace.bar(namespace.U{
1255 .d = 4.0,
1256 });
1257 try expect(u.d == 4.0);
1258}
test/run_translated_c.zig+58-52
......@@ -891,39 +891,42 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
891891 \\}
892892 , "");
893893
894 cases.add("Obscure ways of calling functions; issue #4124",
895 \\#include <stdlib.h>
896 \\static int add(int a, int b) {
897 \\ return a + b;
898 \\}
899 \\typedef int (*adder)(int, int);
900 \\typedef void (*funcptr)(void);
901 \\int main() {
902 \\ if ((add)(1, 2) != 3) abort();
903 \\ if ((&add)(1, 2) != 3) abort();
904 \\ if (add(3, 1) != 4) abort();
905 \\ if ((*add)(2, 3) != 5) abort();
906 \\ if ((**add)(7, -1) != 6) abort();
907 \\ if ((***add)(-2, 9) != 7) abort();
908 \\
909 \\ int (*ptr)(int a, int b);
910 \\ ptr = add;
911 \\
912 \\ if (ptr(1, 2) != 3) abort();
913 \\ if ((*ptr)(3, 1) != 4) abort();
914 \\ if ((**ptr)(2, 3) != 5) abort();
915 \\ if ((***ptr)(7, -1) != 6) abort();
916 \\ if ((****ptr)(-2, 9) != 7) abort();
917 \\
918 \\ funcptr addr1 = (funcptr)(add);
919 \\ funcptr addr2 = (funcptr)(&add);
920 \\
921 \\ if (addr1 != addr2) abort();
922 \\ if (((int(*)(int, int))addr1)(1, 2) != 3) abort();
923 \\ if (((adder)addr2)(1, 2) != 3) abort();
924 \\ return 0;
925 \\}
926 , "");
894 if (@import("builtin").zig_backend == .stage1) {
895 // https://github.com/ziglang/zig/issues/12263
896 cases.add("Obscure ways of calling functions; issue #4124",
897 \\#include <stdlib.h>
898 \\static int add(int a, int b) {
899 \\ return a + b;
900 \\}
901 \\typedef int (*adder)(int, int);
902 \\typedef void (*funcptr)(void);
903 \\int main() {
904 \\ if ((add)(1, 2) != 3) abort();
905 \\ if ((&add)(1, 2) != 3) abort();
906 \\ if (add(3, 1) != 4) abort();
907 \\ if ((*add)(2, 3) != 5) abort();
908 \\ if ((**add)(7, -1) != 6) abort();
909 \\ if ((***add)(-2, 9) != 7) abort();
910 \\
911 \\ int (*ptr)(int a, int b);
912 \\ ptr = add;
913 \\
914 \\ if (ptr(1, 2) != 3) abort();
915 \\ if ((*ptr)(3, 1) != 4) abort();
916 \\ if ((**ptr)(2, 3) != 5) abort();
917 \\ if ((***ptr)(7, -1) != 6) abort();
918 \\ if ((****ptr)(-2, 9) != 7) abort();
919 \\
920 \\ funcptr addr1 = (funcptr)(add);
921 \\ funcptr addr2 = (funcptr)(&add);
922 \\
923 \\ if (addr1 != addr2) abort();
924 \\ if (((int(*)(int, int))addr1)(1, 2) != 3) abort();
925 \\ if (((adder)addr2)(1, 2) != 3) abort();
926 \\ return 0;
927 \\}
928 , "");
929 }
927930
928931 cases.add("Return boolean expression as int; issue #6215",
929932 \\#include <stdlib.h>
......@@ -1319,25 +1322,28 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
13191322 \\}
13201323 , "");
13211324
1322 cases.add("basic vector expressions",
1323 \\#include <stdlib.h>
1324 \\#include <stdint.h>
1325 \\typedef int16_t __v8hi __attribute__((__vector_size__(16)));
1326 \\int main(int argc, char**argv) {
1327 \\ __v8hi uninitialized;
1328 \\ __v8hi empty_init = {};
1329 \\ __v8hi partial_init = {0, 1, 2, 3};
1330 \\
1331 \\ __v8hi a = {0, 1, 2, 3, 4, 5, 6, 7};
1332 \\ __v8hi b = (__v8hi) {100, 200, 300, 400, 500, 600, 700, 800};
1333 \\
1334 \\ __v8hi sum = a + b;
1335 \\ for (int i = 0; i < 8; i++) {
1336 \\ if (sum[i] != a[i] + b[i]) abort();
1337 \\ }
1338 \\ return 0;
1339 \\}
1340 , "");
1325 if (@import("builtin").zig_backend == .stage1) {
1326 // https://github.com/ziglang/zig/issues/12264
1327 cases.add("basic vector expressions",
1328 \\#include <stdlib.h>
1329 \\#include <stdint.h>
1330 \\typedef int16_t __v8hi __attribute__((__vector_size__(16)));
1331 \\int main(int argc, char**argv) {
1332 \\ __v8hi uninitialized;
1333 \\ __v8hi empty_init = {};
1334 \\ __v8hi partial_init = {0, 1, 2, 3};
1335 \\
1336 \\ __v8hi a = {0, 1, 2, 3, 4, 5, 6, 7};
1337 \\ __v8hi b = (__v8hi) {100, 200, 300, 400, 500, 600, 700, 800};
1338 \\
1339 \\ __v8hi sum = a + b;
1340 \\ for (int i = 0; i < 8; i++) {
1341 \\ if (sum[i] != a[i] + b[i]) abort();
1342 \\ }
1343 \\ return 0;
1344 \\}
1345 , "");
1346 }
13411347
13421348 cases.add("__builtin_shufflevector",
13431349 \\#include <stdlib.h>