aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-08-07 15:41:52 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-08-07 15:46:53 -0700
commitf81b2531cb4904064446f84a06f6e09e4120e28a (patch)
treee4bac7a1a13c56b728309086d983091e9e009a65 /src/codegen/c.zig
parentade85471e2cdab466ba685a38c2c7949c9dd1632 (diff)
downloadzig-f81b2531cb4904064446f84a06f6e09e4120e28a.tar.gz
zig-f81b2531cb4904064446f84a06f6e09e4120e28a.zip
stage2: pass some pointer tests
* New AIR instructions: ptr_add, ptr_sub, ptr_elem_val, ptr_ptr_elem_val - See the doc comments for details. * Sema: implement runtime pointer arithmetic. * Sema: implement elem_val for many-pointers. * Sema: support coercion from `*[N:s]T` to `[*]T`. * Type: isIndexable handles many-pointers.
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index 22420aca45..65ad4bac8e 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -850,19 +850,19 @@ fn genBody(o: *Object, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfM
// TODO use a different strategy for add that communicates to the optimizer
// that wrapping is UB.
- .add => try airBinOp( o, inst, " + "),
- .addwrap => try airWrapOp(o, inst, " + ", "addw_"),
+ .add, .ptr_add => try airBinOp( o, inst, " + "),
+ .addwrap => try airWrapOp(o, inst, " + ", "addw_"),
// TODO use a different strategy for sub that communicates to the optimizer
// that wrapping is UB.
- .sub => try airBinOp( o, inst, " - "),
- .subwrap => try airWrapOp(o, inst, " - ", "subw_"),
+ .sub, .ptr_sub => try airBinOp( o, inst, " - "),
+ .subwrap => try airWrapOp(o, inst, " - ", "subw_"),
// TODO use a different strategy for mul that communicates to the optimizer
// that wrapping is UB.
- .mul => try airBinOp( o, inst, " * "),
- .mulwrap => try airWrapOp(o, inst, " * ", "mulw_"),
+ .mul => try airBinOp( o, inst, " * "),
+ .mulwrap => try airWrapOp(o, inst, " * ", "mulw_"),
// TODO use a different strategy for div that communicates to the optimizer
// that wrapping is UB.
- .div => try airBinOp( o, inst, " / "),
+ .div => try airBinOp( o, inst, " / "),
.cmp_eq => try airBinOp(o, inst, " == "),
.cmp_gt => try airBinOp(o, inst, " > "),
@@ -915,6 +915,8 @@ fn genBody(o: *Object, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfM
.slice_ptr => try airSliceField(o, inst, ".ptr;\n"),
.slice_len => try airSliceField(o, inst, ".len;\n"),
+ .ptr_elem_val => try airPtrElemVal(o, inst, "["),
+ .ptr_ptr_elem_val => try airPtrElemVal(o, inst, "[0]["),
.slice_elem_val => try airSliceElemVal(o, inst, "["),
.ptr_slice_elem_val => try airSliceElemVal(o, inst, "[0]["),
@@ -953,8 +955,18 @@ fn airSliceField(o: *Object, inst: Air.Inst.Index, suffix: []const u8) !CValue {
return local;
}
+fn airPtrElemVal(o: *Object, inst: Air.Inst.Index, prefix: []const u8) !CValue {
+ const is_volatile = false; // TODO
+ if (!is_volatile and o.liveness.isUnused(inst))
+ return CValue.none;
+
+ _ = prefix;
+ return o.dg.fail("TODO: C backend: airPtrElemVal", .{});
+}
+
fn airSliceElemVal(o: *Object, inst: Air.Inst.Index, prefix: []const u8) !CValue {
- if (o.liveness.isUnused(inst))
+ const is_volatile = false; // TODO
+ if (!is_volatile and o.liveness.isUnused(inst))
return CValue.none;
const bin_op = o.air.instructions.items(.data)[inst].bin_op;