authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2024-01-19 18:04:40+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2024-02-29 15:23:03+01:00
log94f3a18c88eaee9a36a08a1b00c9df0584a01b05
treea2ed825cde298807071e7d03c9078c8391d15a06
parentcbc8d330622c597527397d98b14b6d298b1b981e
signaturelock-open Commit is signed but in an unrecognized format.

wasm: Add `File` abstraction


1 files changed, 127 insertions(+), 0 deletions(-)

src/link/Wasm/file.zig created+127
......@@ -0,0 +1,127 @@
1pub const File = union(enum) {
2 zig_object: *ZigObject,
3 object: *Object,
4
5 pub const Index = enum(u16) {
6 null = std.math.maxInt(u16),
7 _,
8 };
9
10 pub fn path(file: File) []const u8 {
11 return switch (file) {
12 inline else => |obj| obj.path,
13 };
14 }
15
16 pub fn segmentInfo(file: File) []const types.Segment {
17 return switch (file) {
18 .zig_object => |obj| obj.segment_info.items,
19 .object => |obj| obj.segment_info,
20 };
21 }
22
23 pub fn symbol(file: File, index: u32) *Symbol {
24 return switch (file) {
25 .zig_object => |obj| &obj.symbols.items[index],
26 .object => |obj| &obj.symtable[index],
27 };
28 }
29
30 pub fn symbols(file: File) []const Symbol {
31 return switch (file) {
32 .zig_object => |obj| obj.symbols.items,
33 .object => |obj| obj.symtable,
34 };
35 }
36
37 pub fn symbolName(file: File, index: u32) []const u8 {
38 switch (file) {
39 .zig_object => |obj| {
40 const sym = obj.symbols.items[index];
41 return obj.string_table.get(sym.name).?;
42 },
43 .object => |obj| {
44 const sym = obj.symtable[index];
45 return obj.string_table.get(sym.name);
46 },
47 }
48 }
49
50 pub fn parseSymbolIntoAtom(file: File, wasm_file: *Wasm, index: u32) !AtomIndex {
51 return switch (file) {
52 inline else => |obj| obj.parseSymbolIntoAtom(wasm_file, index),
53 };
54 }
55
56 /// For a given symbol index, find its corresponding import.
57 /// Asserts import exists.
58 pub fn import(file: File, symbol_index: u32) types.Import {
59 return switch (file) {
60 .zig_object => |obj| obj.imports.get(symbol_index).?,
61 .object => |obj| obj.findImport(obj.symtable[symbol_index]),
62 };
63 }
64
65 /// For a given offset, returns its string value.
66 /// Asserts string exists in the object string table.
67 pub fn string(file: File, offset: u32) []const u8 {
68 return switch (file) {
69 .zig_object => |obj| obj.string_table.get(offset).?,
70 .object => |obj| obj.string_table.get(offset),
71 };
72 }
73
74 pub fn importedGlobals(file: File) u32 {
75 return switch (file) {
76 inline else => |obj| obj.imported_globals_count,
77 };
78 }
79
80 pub fn importedFunctions(file: File) u32 {
81 return switch (file) {
82 inline else => |obj| obj.imported_functions_count,
83 };
84 }
85
86 pub fn importedTables(file: File) u32 {
87 return switch (file) {
88 inline else => |obj| obj.imported_tables_count,
89 };
90 }
91
92 pub fn functions(file: File) []const std.wasm.Func {
93 return switch (file) {
94 .zig_object => |obj| obj.functions.items,
95 .object => |obj| obj.functions,
96 };
97 }
98
99 pub fn globals(file: File) []const std.wasm.Global {
100 return switch (file) {
101 .zig_object => |obj| obj.globals.items,
102 .object => |obj| obj.globals,
103 };
104 }
105
106 pub fn funcTypes(file: File) []const std.wasm.Type {
107 return switch (file) {
108 .zig_object => |obj| obj.func_types.items,
109 .object => |obj| obj.func_types,
110 };
111 }
112
113 pub const Entry = union(enum) {
114 null: void,
115 zig_object: ZigObject,
116 object: Object,
117 };
118};
119
120const std = @import("std");
121const types = @import("types.zig");
122
123const AtomIndex = @import("Atom.zig").Index;
124const Object = @import("Object.zig");
125const Symbol = @import("Symbol.zig");
126const Wasm = @import("../Wasm.zig");
127const ZigObject = @import("ZigObject.zig");