authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-11-01 20:43:54-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-11-01 20:43:54-04:00
logd8635af1dcff8dc5fc4874a711d34bd67eeab689
treea2bc96b6b70afea089a1def54a3af6e603b809a0
parent771dadc5e08fc27c5cd4aa3483647b70a9f9cb0b

cbe: correctly implement volatile memset


1 files changed, 28 insertions(+), 8 deletions(-)

src/codegen/c.zig+28-8
......@@ -4930,19 +4930,39 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {
49304930 const dest_ptr = try f.resolveInst(pl_op.operand);
49314931 const value = try f.resolveInst(extra.lhs);
49324932 const len = try f.resolveInst(extra.rhs);
4933 const writer = f.object.writer();
49344933
4935 try writer.writeAll("memset(");
4934 const writer = f.object.writer();
49364935 if (dest_ty.isVolatilePtr()) {
4937 // This is wrong, but good enough for now.
4938 var remove_volatile_pl = dest_ty.ptrInfo();
4939 remove_volatile_pl.data.@"volatile" = false;
4940 const remove_volatile_ty = Type.initPayload(&remove_volatile_pl.base);
4936 var u8_ptr_pl = dest_ty.ptrInfo();
4937 u8_ptr_pl.data.pointee_type = Type.u8;
4938 const u8_ptr_ty = Type.initPayload(&u8_ptr_pl.base);
49414939
4942 try writer.writeByte('(');
4943 try f.renderTypecast(writer, remove_volatile_ty);
4940 try writer.writeAll("for (");
4941 const index = try f.allocLocal(Type.usize, .Mut);
4942 try writer.writeAll(" = ");
4943 try f.object.dg.renderValue(writer, Type.usize, Value.zero, .Initializer);
4944 try writer.writeAll("; ");
4945 try f.writeCValue(writer, index, .Other);
4946 try writer.writeAll(" != ");
4947 try f.writeCValue(writer, len, .Other);
4948 try writer.writeAll("; ");
4949 try f.writeCValue(writer, index, .Other);
4950 try writer.writeAll(" += ");
4951 try f.object.dg.renderValue(writer, Type.usize, Value.one, .Other);
4952 try writer.writeAll(") ((");
4953 try f.renderTypecast(writer, u8_ptr_ty);
49444954 try writer.writeByte(')');
4955 try f.writeCValue(writer, dest_ptr, .FunctionArgument);
4956 try writer.writeAll(")[");
4957 try f.writeCValue(writer, index, .Other);
4958 try writer.writeAll("] = ");
4959 try f.writeCValue(writer, value, .FunctionArgument);
4960 try writer.writeAll(";\n");
4961
4962 return CValue.none;
49454963 }
4964
4965 try writer.writeAll("memset(");
49464966 try f.writeCValue(writer, dest_ptr, .FunctionArgument);
49474967 try writer.writeAll(", ");
49484968 try f.writeCValue(writer, value, .FunctionArgument);