authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-06 20:07:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-06 20:07:43-07:00
log3bcce5f6d1f48e20dd177a7e440ddea1c451e779
tree0a5dca7cf1037e0fffe177a9c6fffe9605762a1b
parentd4805472c3c98c488c17bce0ee28b6c44e93793c

Sema: implement writing structs to memory at comptime


1 files changed, 14 insertions(+), 1 deletions(-)

src/value.zig+14-1
......@@ -1033,6 +1033,11 @@ pub const Value = extern union {
10331033 }
10341034
10351035 pub fn writeToMemory(val: Value, ty: Type, target: Target, buffer: []u8) void {
1036 if (val.isUndef()) {
1037 const size = @intCast(usize, ty.abiSize(target));
1038 std.mem.set(u8, buffer[0..size], 0xaa);
1039 return;
1040 }
10361041 switch (ty.zigTypeTag()) {
10371042 .Int => {
10381043 var bigint_buffer: BigIntSpace = undefined;
......@@ -1068,6 +1073,14 @@ pub const Value = extern union {
10681073 buf_off += elem_size;
10691074 }
10701075 },
1076 .Struct => {
1077 const fields = ty.structFields().values();
1078 const field_vals = val.castTag(.@"struct").?.data;
1079 for (fields) |field, i| {
1080 const off = @intCast(usize, ty.structFieldOffset(i, target));
1081 writeToMemory(field_vals[i], field.ty, target, buffer[off..]);
1082 }
1083 },
10711084 else => @panic("TODO implement writeToMemory for more types"),
10721085 }
10731086 }
......@@ -1106,7 +1119,7 @@ pub const Value = extern union {
11061119
11071120 fn floatReadFromMemory(comptime F: type, target: Target, buffer: []const u8) F {
11081121 if (F == f80) {
1109 // TODO: use std.math.F80Repr
1122 // TODO: use std.math.F80Repr?
11101123 const big_int = std.mem.readInt(u128, buffer[0..16], target.cpu.arch.endian());
11111124 const int = @truncate(u80, big_int);
11121125 return @bitCast(F, int);