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 {
386386 val: Value,
387387 decl_index: Decl.Index,
388388 ) error{ OutOfMemory, AnalysisFail }!void {
389 if (ty.isPtrAtRuntime() and !ty.elemType2().isFnOrHasRuntimeBits()) {
390 return dg.writeCValue(writer, CValue{ .undefined_ptr = ty });
391 }
392
389393 if (ty.isSlice()) {
390394 try writer.writeByte('(');
391395 try dg.renderTypecast(writer, ty);
......@@ -404,23 +408,15 @@ pub const DeclGen = struct {
404408 // them). The analysis until now should ensure that the C function
405409 // pointers are compatible. If they are not, then there is a bug
406410 // somewhere and we should let the C compiler tell us about it.
407 if (ty.castPtrToFn() == null) {
408 // Determine if we must pointer cast.
409 if (ty.eql(decl.ty, dg.module)) {
410 try writer.writeByte('&');
411 try dg.renderDeclName(writer, decl_index);
412 return;
413 }
414
411 const need_typecast = if (ty.castPtrToFn()) |_| false else !ty.eql(decl.ty, dg.module);
412 if (need_typecast) {
415413 try writer.writeAll("((");
416414 try dg.renderTypecast(writer, ty);
417 try writer.writeAll(")&");
418 try dg.renderDeclName(writer, decl_index);
419415 try writer.writeByte(')');
420 return;
421416 }
422
417 try writer.writeByte('&');
423418 try dg.renderDeclName(writer, decl_index);
419 if (need_typecast) try writer.writeByte(')');
424420 }
425421
426422 // Renders a "parent" pointer by recursing to the root decl/variable
......@@ -1830,7 +1826,7 @@ pub const DeclGen = struct {
18301826
18311827 if (dg.module.decl_exports.get(decl_index)) |exports| {
18321828 return writer.writeAll(exports[0].options.name);
1833 } else if (decl.val.tag() == .extern_fn) {
1829 } else if (decl.isExtern()) {
18341830 return writer.writeAll(mem.sliceTo(decl.name, 0));
18351831 } else {
18361832 const gpa = dg.module.gpa;
......@@ -1997,7 +1993,7 @@ pub fn genDecl(o: *Object) !void {
19971993 try o.dg.renderTypeAndName(fwd_decl_writer, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.@"align");
19981994 try fwd_decl_writer.writeAll(";\n");
19991995
2000 if (variable.init.isUndefDeep()) {
1996 if (variable.is_extern or variable.init.isUndefDeep()) {
20011997 return;
20021998 }
20031999
test/behavior/basic.zig-2
......@@ -383,8 +383,6 @@ fn testTakeAddressOfParameter(f: f32) !void {
383383}
384384
385385test "pointer to void return type" {
386 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
387
388386 try testPointerToVoidReturnType();
389387}
390388fn testPointerToVoidReturnType() anyerror!void {
test/behavior/struct.zig-3
......@@ -329,7 +329,6 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {
329329
330330test "pass slice of empty struct to fn" {
331331 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
332 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
333332 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
334333
335334 try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
......@@ -354,8 +353,6 @@ test "self-referencing struct via array member" {
354353}
355354
356355test "empty struct method call" {
357 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
358
359356 const es = EmptyStruct{};
360357 try expect(es.method() == 1234);
361358}