authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-09 01:48:26-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-25 05:11:28-04:00
loga12535f5014cb2d4878581871287f17a31f28961
tree7bbb5199f9797d19d1c3bb66e391c5d142fec65c
parentf399dd107a2b0387ca54688741d6c3fa0109ed39

cbe: fix global access


3 files changed, 10 insertions(+), 19 deletions(-)

src/codegen/c.zig+10-14
...@@ -386,6 +386,10 @@ pub const DeclGen = struct {...@@ -386,6 +386,10 @@ pub const DeclGen = struct {
386 val: Value,386 val: Value,
387 decl_index: Decl.Index,387 decl_index: Decl.Index,
388 ) error{ OutOfMemory, AnalysisFail }!void {388 ) error{ OutOfMemory, AnalysisFail }!void {
389 if (ty.isPtrAtRuntime() and !ty.elemType2().isFnOrHasRuntimeBits()) {
390 return dg.writeCValue(writer, CValue{ .undefined_ptr = ty });
391 }
392
389 if (ty.isSlice()) {393 if (ty.isSlice()) {
390 try writer.writeByte('(');394 try writer.writeByte('(');
391 try dg.renderTypecast(writer, ty);395 try dg.renderTypecast(writer, ty);
...@@ -404,23 +408,15 @@ pub const DeclGen = struct {...@@ -404,23 +408,15 @@ pub const DeclGen = struct {
404 // them). The analysis until now should ensure that the C function408 // them). The analysis until now should ensure that the C function
405 // pointers are compatible. If they are not, then there is a bug409 // pointers are compatible. If they are not, then there is a bug
406 // somewhere and we should let the C compiler tell us about it.410 // somewhere and we should let the C compiler tell us about it.
407 if (ty.castPtrToFn() == null) {411 const need_typecast = if (ty.castPtrToFn()) |_| false else !ty.eql(decl.ty, dg.module);
408 // Determine if we must pointer cast.412 if (need_typecast) {
409 if (ty.eql(decl.ty, dg.module)) {
410 try writer.writeByte('&');
411 try dg.renderDeclName(writer, decl_index);
412 return;
413 }
414
415 try writer.writeAll("((");413 try writer.writeAll("((");
416 try dg.renderTypecast(writer, ty);414 try dg.renderTypecast(writer, ty);
417 try writer.writeAll(")&");
418 try dg.renderDeclName(writer, decl_index);
419 try writer.writeByte(')');415 try writer.writeByte(')');
420 return;
421 }416 }
422417 try writer.writeByte('&');
423 try dg.renderDeclName(writer, decl_index);418 try dg.renderDeclName(writer, decl_index);
419 if (need_typecast) try writer.writeByte(')');
424 }420 }
425421
426 // Renders a "parent" pointer by recursing to the root decl/variable422 // Renders a "parent" pointer by recursing to the root decl/variable
...@@ -1830,7 +1826,7 @@ pub const DeclGen = struct {...@@ -1830,7 +1826,7 @@ pub const DeclGen = struct {
18301826
1831 if (dg.module.decl_exports.get(decl_index)) |exports| {1827 if (dg.module.decl_exports.get(decl_index)) |exports| {
1832 return writer.writeAll(exports[0].options.name);1828 return writer.writeAll(exports[0].options.name);
1833 } else if (decl.val.tag() == .extern_fn) {1829 } else if (decl.isExtern()) {
1834 return writer.writeAll(mem.sliceTo(decl.name, 0));1830 return writer.writeAll(mem.sliceTo(decl.name, 0));
1835 } else {1831 } else {
1836 const gpa = dg.module.gpa;1832 const gpa = dg.module.gpa;
...@@ -1997,7 +1993,7 @@ pub fn genDecl(o: *Object) !void {...@@ -1997,7 +1993,7 @@ pub fn genDecl(o: *Object) !void {
1997 try o.dg.renderTypeAndName(fwd_decl_writer, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.@"align");1993 try o.dg.renderTypeAndName(fwd_decl_writer, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.@"align");
1998 try fwd_decl_writer.writeAll(";\n");1994 try fwd_decl_writer.writeAll(";\n");
19991995
2000 if (variable.init.isUndefDeep()) {1996 if (variable.is_extern or variable.init.isUndefDeep()) {
2001 return;1997 return;
2002 }1998 }
20031999
test/behavior/basic.zig-2
...@@ -383,8 +383,6 @@ fn testTakeAddressOfParameter(f: f32) !void {...@@ -383,8 +383,6 @@ fn testTakeAddressOfParameter(f: f32) !void {
383}383}
384384
385test "pointer to void return type" {385test "pointer to void return type" {
386 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
387
388 try testPointerToVoidReturnType();386 try testPointerToVoidReturnType();
389}387}
390fn testPointerToVoidReturnType() anyerror!void {388fn testPointerToVoidReturnType() anyerror!void {
test/behavior/struct.zig-3
...@@ -329,7 +329,6 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {...@@ -329,7 +329,6 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {
329329
330test "pass slice of empty struct to fn" {330test "pass slice of empty struct to fn" {
331 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;331 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
332 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
333 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO332 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
334333
335 try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);334 try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
...@@ -354,8 +353,6 @@ test "self-referencing struct via array member" {...@@ -354,8 +353,6 @@ test "self-referencing struct via array member" {
354}353}
355354
356test "empty struct method call" {355test "empty struct method call" {
357 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
358
359 const es = EmptyStruct{};356 const es = EmptyStruct{};
360 try expect(es.method() == 1234);357 try expect(es.method() == 1234);
361}358}