authorgravatar for reserveblue@protonmail.comdrew <reserveblue@protonmail.com> 2021-11-14 23:27:13-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-16 16:51:31-07:00
logcf99afc52568ab121db2db8aa3ba94b97109f396
treee7ed54a202c88c7a04753b24850308768d12dac8
parent3896de307888d598cca1baba8e559debc81d2080

add generics behavior test

-airLoad and airStore now properly report an error if they are used with an array, instead of having the C compiler emit a vague error -airStoreUndefined now works with array types -structFieldPtr now works with array types, allowing generics' tests to pass

2 files changed, 17 insertions(+), 6 deletions(-)

src/codegen/c.zig+16-5
......@@ -1431,6 +1431,8 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
14311431 if (!is_volatile and f.liveness.isUnused(inst))
14321432 return CValue.none;
14331433 const inst_ty = f.air.typeOfIndex(inst);
1434 if (inst_ty.zigTypeTag() == .Array)
1435 return f.fail("TODO: C backend: implement airLoad for arrays", .{});
14341436 const operand = try f.resolveInst(ty_op.operand);
14351437 const writer = f.object.writer();
14361438 const local = try f.allocLocal(inst_ty, .Const);
......@@ -1557,7 +1559,7 @@ fn airBoolToInt(f: *Function, inst: Air.Inst.Index) !CValue {
15571559 return local;
15581560}
15591561
1560fn airStoreUndefined(f: *Function, dest_ptr: CValue) !CValue {
1562fn airStoreUndefined(f: *Function, dest_ptr: CValue, dest_type: Type) !CValue {
15611563 const is_debug_build = f.object.dg.module.optimizeMode() == .Debug;
15621564 if (!is_debug_build)
15631565 return CValue.none;
......@@ -1581,9 +1583,11 @@ fn airStoreUndefined(f: *Function, dest_ptr: CValue) !CValue {
15811583 try writer.writeAll("));\n");
15821584 },
15831585 else => {
1586 const indirection = if (dest_type.zigTypeTag() == .Array) "" else "*";
1587
15841588 try writer.writeAll("memset(");
15851589 try f.writeCValue(writer, dest_ptr);
1586 try writer.writeAll(", 0xaa, sizeof(*");
1590 try writer.print(", 0xaa, sizeof({s}", .{indirection});
15871591 try f.writeCValue(writer, dest_ptr);
15881592 try writer.writeAll("));\n");
15891593 },
......@@ -1596,11 +1600,16 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue {
15961600 const bin_op = f.air.instructions.items(.data)[inst].bin_op;
15971601 const dest_ptr = try f.resolveInst(bin_op.lhs);
15981602 const src_val = try f.resolveInst(bin_op.rhs);
1603 const lhs_type = f.air.typeOf(bin_op.lhs);
15991604
16001605 const src_val_is_undefined =
16011606 if (f.air.value(bin_op.rhs)) |v| v.isUndef() else false;
16021607 if (src_val_is_undefined)
1603 return try airStoreUndefined(f, dest_ptr);
1608 return try airStoreUndefined(f, dest_ptr, lhs_type);
1609
1610 // Don't check this for airStoreUndefined as that will work for arrays already
1611 if (lhs_type.zigTypeTag() == .Array)
1612 return f.fail("TODO: C backend: implement airStore for arrays", .{});
16041613
16051614 const writer = f.object.writer();
16061615 switch (dest_ptr) {
......@@ -2420,15 +2429,17 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc
24202429 const writer = f.object.writer();
24212430 const struct_obj = struct_ptr_ty.elemType().castTag(.@"struct").?.data;
24222431 const field_name = struct_obj.fields.keys()[index];
2432 const field_val = struct_obj.fields.values()[index];
2433 const addrof = if (field_val.ty.zigTypeTag() == .Array) "" else "&";
24232434
24242435 const inst_ty = f.air.typeOfIndex(inst);
24252436 const local = try f.allocLocal(inst_ty, .Const);
24262437 switch (struct_ptr) {
24272438 .local_ref => |i| {
2428 try writer.print(" = &t{d}.{};\n", .{ i, fmtIdent(field_name) });
2439 try writer.print(" = {s}t{d}.{};\n", .{ addrof, i, fmtIdent(field_name) });
24292440 },
24302441 else => {
2431 try writer.writeAll(" = &");
2442 try writer.print(" = {s}", .{addrof});
24322443 try f.writeCValue(writer, struct_ptr);
24332444 try writer.print("->{};\n", .{fmtIdent(field_name)});
24342445 },
test/behavior.zig+1-1
......@@ -38,6 +38,7 @@ test {
3838 _ = @import("behavior/this.zig");
3939 _ = @import("behavior/member_func.zig");
4040 _ = @import("behavior/translate_c_macros.zig");
41 _ = @import("behavior/generics.zig");
4142
4243 if (builtin.object_format != .c) {
4344 // Tests that pass for stage1 and stage2 but not the C backend.
......@@ -57,7 +58,6 @@ test {
5758 _ = @import("behavior/floatop.zig");
5859 _ = @import("behavior/fn.zig");
5960 _ = @import("behavior/for.zig");
60 _ = @import("behavior/generics.zig");
6161 _ = @import("behavior/math.zig");
6262 _ = @import("behavior/maximum_minimum.zig");
6363 _ = @import("behavior/null_llvm.zig");