authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-10 05:43:51-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-25 05:11:29-04:00
loge8bda9eb3ae9634a7b434e59df883ada3fdf8d9d
treeab7eac80538c4e6f221471a98384a870b1f86c64
parent87d432328e564ac138a1b773ab025324b5c191ed

cbe: implement ptr slice ptr


4 files changed, 12 insertions(+), 27 deletions(-)

src/codegen/c.zig+12-24
...@@ -791,7 +791,8 @@ pub const DeclGen = struct {...@@ -791,7 +791,8 @@ pub const DeclGen = struct {
791 }791 }
792792
793 if (ty.optionalReprIsPayload()) {793 if (ty.optionalReprIsPayload()) {
794 return dg.renderValue(writer, payload_ty, val, location);794 const payload_val = if (val.castTag(.opt_payload)) |pl| pl.data else val;
795 return dg.renderValue(writer, payload_ty, payload_val, location);
795 }796 }
796797
797 try writer.writeByte('(');798 try writer.writeByte('(');
...@@ -1457,7 +1458,7 @@ pub const DeclGen = struct {...@@ -1457,7 +1458,7 @@ pub const DeclGen = struct {
1457 .c_ulong => try w.writeAll("unsigned long"),1458 .c_ulong => try w.writeAll("unsigned long"),
1458 .c_longlong => try w.writeAll("long long"),1459 .c_longlong => try w.writeAll("long long"),
1459 .c_ulonglong => try w.writeAll("unsigned long long"),1460 .c_ulonglong => try w.writeAll("unsigned long long"),
1460 .int_signed, .int_unsigned => {1461 .u29, .int_signed, .int_unsigned => {
1461 const info = t.intInfo(target);1462 const info = t.intInfo(target);
1462 const sign_prefix = switch (info.signedness) {1463 const sign_prefix = switch (info.signedness) {
1463 .signed => "",1464 .signed => "",
...@@ -2242,11 +2243,11 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO...@@ -2242,11 +2243,11 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
2242 .field_parent_ptr => try airFieldParentPtr(f, inst),2243 .field_parent_ptr => try airFieldParentPtr(f, inst),
22432244
2244 .struct_field_val => try airStructFieldVal(f, inst),2245 .struct_field_val => try airStructFieldVal(f, inst),
2245 .slice_ptr => try airSliceField(f, inst, ".ptr;\n"),2246 .slice_ptr => try airSliceField(f, inst, " = ", ".ptr;\n"),
2246 .slice_len => try airSliceField(f, inst, ".len;\n"),2247 .slice_len => try airSliceField(f, inst, " = ", ".len;\n"),
22472248
2248 .ptr_slice_len_ptr => try airPtrSliceFieldPtr(f, inst, ".len;\n"),2249 .ptr_slice_len_ptr => try airSliceField(f, inst, " = &", ".len;\n"),
2249 .ptr_slice_ptr_ptr => try airPtrSliceFieldPtr(f, inst, ".ptr;\n"),2250 .ptr_slice_ptr_ptr => try airSliceField(f, inst, " = &", ".ptr;\n"),
22502251
2251 .ptr_elem_val => try airPtrElemVal(f, inst),2252 .ptr_elem_val => try airPtrElemVal(f, inst),
2252 .ptr_elem_ptr => try airPtrElemPtr(f, inst),2253 .ptr_elem_ptr => try airPtrElemPtr(f, inst),
...@@ -2306,7 +2307,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO...@@ -2306,7 +2307,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
2306 try writer.writeByte('}');2307 try writer.writeByte('}');
2307}2308}
23082309
2309fn airSliceField(f: *Function, inst: Air.Inst.Index, suffix: []const u8) !CValue {2310fn airSliceField(f: *Function, inst: Air.Inst.Index, prefix: []const u8, suffix: []const u8) !CValue {
2310 if (f.liveness.isUnused(inst)) return CValue.none;2311 if (f.liveness.isUnused(inst)) return CValue.none;
23112312
2312 const inst_ty = f.air.typeOfIndex(inst);2313 const inst_ty = f.air.typeOfIndex(inst);
...@@ -2314,27 +2315,12 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, suffix: []const u8) !CValue...@@ -2314,27 +2315,12 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, suffix: []const u8) !CValue
2314 const operand = try f.resolveInst(ty_op.operand);2315 const operand = try f.resolveInst(ty_op.operand);
2315 const writer = f.object.writer();2316 const writer = f.object.writer();
2316 const local = try f.allocLocal(inst_ty, .Const);2317 const local = try f.allocLocal(inst_ty, .Const);
2317 try writer.writeAll(" = ");2318 try writer.writeAll(prefix);
2318 try f.writeCValue(writer, operand);2319 try f.writeCValue(writer, operand);
2319 try writer.writeAll(suffix);2320 try writer.writeAll(suffix);
2320 return local;2321 return local;
2321}2322}
23222323
2323fn airPtrSliceFieldPtr(f: *Function, inst: Air.Inst.Index, suffix: []const u8) !CValue {
2324 if (f.liveness.isUnused(inst))
2325 return CValue.none;
2326
2327 const ty_op = f.air.instructions.items(.data)[inst].ty_op;
2328 const operand = try f.resolveInst(ty_op.operand);
2329 const writer = f.object.writer();
2330
2331 _ = writer;
2332 _ = operand;
2333 _ = suffix;
2334
2335 return f.fail("TODO: C backend: airPtrSliceFieldPtr", .{});
2336}
2337
2338fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue {2324fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
2339 const bin_op = f.air.instructions.items(.data)[inst].bin_op;2325 const bin_op = f.air.instructions.items(.data)[inst].bin_op;
2340 const ptr_ty = f.air.typeOf(bin_op.lhs);2326 const ptr_ty = f.air.typeOf(bin_op.lhs);
...@@ -3581,7 +3567,9 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3581,7 +3567,9 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
3581 // for the string, we still use the next u32 for the null terminator.3567 // for the string, we still use the next u32 for the null terminator.
3582 extra_i += (constraint.len + name.len + (2 + 3)) / 4;3568 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
35833569
3584 try f.writeCValueDeref(writer, if (output == .none) .{ .local_ref = local.local } else try f.resolveInst(output));3570 try f.writeCValueDeref(writer, if (output == .none) CValue{
3571 .local_ref = local.local,
3572 } else try f.resolveInst(output));
3585 try writer.writeAll(" = ");3573 try writer.writeAll(" = ");
3586 try f.writeCValue(writer, .{ .identifier = name });3574 try f.writeCValue(writer, .{ .identifier = name });
3587 try writer.writeAll(";\n");3575 try writer.writeAll(";\n");
test/behavior/bugs/2578.zig-1
...@@ -15,7 +15,6 @@ test "fixed" {...@@ -15,7 +15,6 @@ test "fixed" {
15 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;15 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
16 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO16 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
17 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO17 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
18 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1918
20 bar(t);19 bar(t);
21}20}
test/behavior/cast.zig-1
...@@ -1260,7 +1260,6 @@ test "cast between [*c]T and ?[*:0]T on fn parameter" {...@@ -1260,7 +1260,6 @@ test "cast between [*c]T and ?[*:0]T on fn parameter" {
1260var global_struct: struct { f0: usize } = undefined;1260var global_struct: struct { f0: usize } = undefined;
1261test "assignment to optional pointer result loc" {1261test "assignment to optional pointer result loc" {
1262 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;1262 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1263 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1264 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO1263 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
12651264
1266 var foo: struct { ptr: ?*anyopaque } = .{ .ptr = &global_struct };1265 var foo: struct { ptr: ?*anyopaque } = .{ .ptr = &global_struct };
test/behavior/fn.zig-1
...@@ -97,7 +97,6 @@ test "discard the result of a function that returns a struct" {...@@ -97,7 +97,6 @@ test "discard the result of a function that returns a struct" {
97}97}
9898
99test "inline function call that calls optional function pointer, return pointer at callsite interacts correctly with callsite return type" {99test "inline function call that calls optional function pointer, return pointer at callsite interacts correctly with callsite return type" {
100 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
101 if (builtin.zig_backend == .stage1) return error.SkipZigTest;100 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
102 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;101 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
103 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;102 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;