zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 3bcce5f6d1f48e20dd177a7e440ddea1c451e779 (tree)
parent d4805472c3c98c488c17bce0ee28b6c44e93793c
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Sun,  6 Feb 2022 20:07:43 -0700

Sema: implement writing structs to memory at comptime

Diffstat:
Msrc/value.zig | 15++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/value.zig b/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);