Commit 3bcce5f6d1

Andrew Kelley <andrew@ziglang.org>
2022-02-07 04:07:43
Sema: implement writing structs to memory at comptime
1 parent d480547
Changed files (1)
src/value.zig
@@ -1033,6 +1033,11 @@ pub const Value = extern union {
     }
 
     pub fn writeToMemory(val: Value, ty: Type, target: Target, buffer: []u8) void {
+        if (val.isUndef()) {
+            const size = @intCast(usize, ty.abiSize(target));
+            std.mem.set(u8, buffer[0..size], 0xaa);
+            return;
+        }
         switch (ty.zigTypeTag()) {
             .Int => {
                 var bigint_buffer: BigIntSpace = undefined;
@@ -1068,6 +1073,14 @@ pub const Value = extern union {
                     buf_off += elem_size;
                 }
             },
+            .Struct => {
+                const fields = ty.structFields().values();
+                const field_vals = val.castTag(.@"struct").?.data;
+                for (fields) |field, i| {
+                    const off = @intCast(usize, ty.structFieldOffset(i, target));
+                    writeToMemory(field_vals[i], field.ty, target, buffer[off..]);
+                }
+            },
             else => @panic("TODO implement writeToMemory for more types"),
         }
     }
@@ -1106,7 +1119,7 @@ pub const Value = extern union {
 
     fn floatReadFromMemory(comptime F: type, target: Target, buffer: []const u8) F {
         if (F == f80) {
-            // TODO: use std.math.F80Repr
+            // TODO: use std.math.F80Repr?
             const big_int = std.mem.readInt(u128, buffer[0..16], target.cpu.arch.endian());
             const int = @truncate(u80, big_int);
             return @bitCast(F, int);