diff options
| author | Robin Voetter <robin@voetter.nl> | 2021-10-21 16:17:21 +0200 |
|---|---|---|
| committer | Robin Voetter <robin@voetter.nl> | 2021-10-21 16:24:18 +0200 |
| commit | 09c7d5aebc4f7fe96a89f93c0cf99cc03977ea5f (patch) | |
| tree | 7877d79040b38f43aea7fc192546b19d07821bc0 /src/codegen/c.zig | |
| parent | 84876fec582e13707a809c8136bb1eaf92b5da09 (diff) | |
| download | zig-09c7d5aebc4f7fe96a89f93c0cf99cc03977ea5f.tar.gz zig-09c7d5aebc4f7fe96a89f93c0cf99cc03977ea5f.zip | |
stage2: elemPtr for slices
* Restructure elemPtr a bit
* New AIR instruction: slice_elem_ptr, which returns a pointer to an element of a slice
* Value: adapt elemPtr to work on slices
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index f09bb574af..9050499fce 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1083,6 +1083,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO .ptr_elem_val => try airPtrElemVal(f, inst, "["), .ptr_elem_ptr => try airPtrElemPtr(f, inst), .slice_elem_val => try airSliceElemVal(f, inst, "["), + .slice_elem_ptr => try airSliceElemPtr(f, inst), .array_elem_val => try airArrayElemVal(f, inst), .unwrap_errunion_payload => try airUnwrapErrUnionPay(f, inst), @@ -1165,6 +1166,24 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index, prefix: []const u8) !CVal return local; } +fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { + if (f.liveness.isUnused(inst)) + return CValue.none; + const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; + const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; + + const slice = try f.resolveInst(bin_op.lhs); + const index = try f.resolveInst(bin_op.rhs); + const writer = f.object.writer(); + const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); + try writer.writeAll(" = &"); + try f.writeCValue(writer, slice); + try writer.writeByte('['); + try f.writeCValue(writer, index); + try writer.writeAll("];\n"); + return local; +} + fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { if (f.liveness.isUnused(inst)) return CValue.none; |
