lib/choir/src/backends/artifact/serialization/metadata.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 const artifact = @import("../root.zig");
 3 const format = @import("root.zig").format;
 4 
 5 pub const Decoded = struct {
 6     allocator: std.mem.Allocator,
 7     value: artifact.Metadata,
 8 
 9     pub fn deinit(self: *Decoded) void {
10         self.allocator.free(@constCast(self.value.target.features));
11         self.* = undefined;
12     }
13 };
14 
15 pub fn write(writer: *format.Writer, value: artifact.Metadata) format.WriteError!void {
16     try writer.writeTag(value.kind);
17     try writer.writeOptionalString(value.producer);
18     try writer.writeTag(value.target.architecture);
19     try writer.writeOptionalString(value.target.triple);
20     try writer.writeOptionalString(value.target.vendor);
21     try writer.writeOptionalString(value.target.os);
22     try writer.writeOptionalString(value.target.environment);
23     try writer.writeOptionalString(value.target.cpu);
24     try writer.writeCount(value.target.features.len);
25     for (value.target.features) |feature| try writer.writeString(feature);
26     try writer.writeOptionalString(value.abi.name);
27     try writer.writeOptionalString(value.abi.calling_convention);
28     try writer.writeOptionalString(value.abi.object_format);
29     try writer.writeOptionalU16(value.abi.pointer_width_bits);
30     try writer.writeBool(value.abi.endianness != null);
31     if (value.abi.endianness) |endianness| try writer.writeTag(endianness);
32 }
33 
34 pub fn read(allocator: std.mem.Allocator, reader: *format.Reader) (format.Error || std.mem.Allocator.Error)!Decoded {
35     const kind = try reader.readTag(artifact.ArtifactKind);
36     const producer = try reader.readOptionalString();
37     const architecture = try reader.readTag(artifact.Architecture);
38     const triple = try reader.readOptionalString();
39     const vendor = try reader.readOptionalString();
40     const os = try reader.readOptionalString();
41     const environment = try reader.readOptionalString();
42     const cpu = try reader.readOptionalString();
43     const feature_count = try reader.readCount();
44     const features = try allocator.alloc([]const u8, feature_count);
45     errdefer allocator.free(features);
46     for (features) |*feature| feature.* = try reader.readString();
47     const abi_name = try reader.readOptionalString();
48     const calling_convention = try reader.readOptionalString();
49     const object_format = try reader.readOptionalString();
50     const pointer_width_bits = try reader.readOptionalU16();
51     const endianness = if (try reader.readBool()) try reader.readTag(artifact.Endianness) else null;
52     return .{
53         .allocator = allocator,
54         .value = .{
55             .kind = kind,
56             .producer = producer,
57             .target = .{
58                 .architecture = architecture,
59                 .triple = triple,
60                 .vendor = vendor,
61                 .os = os,
62                 .environment = environment,
63                 .cpu = cpu,
64                 .features = features,
65             },
66             .abi = .{
67                 .name = abi_name,
68                 .calling_convention = calling_convention,
69                 .object_format = object_format,
70                 .pointer_width_bits = pointer_width_bits,
71                 .endianness = endianness,
72             },
73         },
74     };
75 }