aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-09-24 17:33:06 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-09-24 17:33:06 -0700
commit42aa1ea115eca3dcc704eddf020ce87271a41174 (patch)
tree3242b53425c599af929446e6776a2f6b3a0c6966 /src/codegen/c.zig
parent87fd502fb68f8f488e6eba6b1f7d70902d6bfe5a (diff)
downloadzig-42aa1ea115eca3dcc704eddf020ce87271a41174.tar.gz
zig-42aa1ea115eca3dcc704eddf020ce87271a41174.zip
stage2: implement `@memset` and `@memcpy` builtins
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig46
1 files changed, 45 insertions, 1 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index a82f0e57f7..16b13db292 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -953,6 +953,8 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
.cmpxchg_strong => try airCmpxchg(f, inst, "strong"),
.atomic_rmw => try airAtomicRmw(f, inst),
.atomic_load => try airAtomicLoad(f, inst),
+ .memset => try airMemset(f, inst),
+ .memcpy => try airMemcpy(f, inst),
.int_to_float,
.float_to_int,
@@ -2005,8 +2007,12 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue {
fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue {
const atomic_load = f.air.instructions.items(.data)[inst].atomic_load;
- const inst_ty = f.air.typeOfIndex(inst);
const ptr = try f.resolveInst(atomic_load.ptr);
+ const ptr_ty = f.air.typeOf(atomic_load.ptr);
+ if (!ptr_ty.isVolatilePtr() and f.liveness.isUnused(inst))
+ return CValue.none;
+
+ const inst_ty = f.air.typeOfIndex(inst);
const local = try f.allocLocal(inst_ty, .Const);
const writer = f.object.writer();
@@ -2036,6 +2042,44 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa
return local;
}
+fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {
+ const pl_op = f.air.instructions.items(.data)[inst].pl_op;
+ const extra = f.air.extraData(Air.Bin, pl_op.payload).data;
+ const dest_ptr = try f.resolveInst(pl_op.operand);
+ const value = try f.resolveInst(extra.lhs);
+ const len = try f.resolveInst(extra.rhs);
+ const writer = f.object.writer();
+
+ try writer.writeAll("memset(");
+ try f.writeCValue(writer, dest_ptr);
+ try writer.writeAll(", ");
+ try f.writeCValue(writer, value);
+ try writer.writeAll(", ");
+ try f.writeCValue(writer, len);
+ try writer.writeAll(");\n");
+
+ return CValue.none;
+}
+
+fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue {
+ const pl_op = f.air.instructions.items(.data)[inst].pl_op;
+ const extra = f.air.extraData(Air.Bin, pl_op.payload).data;
+ const dest_ptr = try f.resolveInst(pl_op.operand);
+ const src_ptr = try f.resolveInst(extra.lhs);
+ const len = try f.resolveInst(extra.rhs);
+ const writer = f.object.writer();
+
+ try writer.writeAll("memcpy(");
+ try f.writeCValue(writer, dest_ptr);
+ try writer.writeAll(", ");
+ try f.writeCValue(writer, src_ptr);
+ try writer.writeAll(", ");
+ try f.writeCValue(writer, len);
+ try writer.writeAll(");\n");
+
+ return CValue.none;
+}
+
fn toMemoryOrder(order: std.builtin.AtomicOrder) [:0]const u8 {
return switch (order) {
.Unordered => "memory_order_relaxed",