aboutsummaryrefslogtreecommitdiff
path: root/src/link/Wasm/file.zig
blob: e0ff12132265dcaab4ca7923045ad7681d9bf148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
pub const File = union(enum) {
    zig_object: *ZigObject,
    object: *Object,

    pub const Index = enum(u16) {
        null = std.math.maxInt(u16),
        _,
    };

    pub fn path(file: File) []const u8 {
        return switch (file) {
            inline else => |obj| obj.path,
        };
    }

    pub fn segmentInfo(file: File) []const types.Segment {
        return switch (file) {
            .zig_object => |obj| obj.segment_info.items,
            .object => |obj| obj.segment_info,
        };
    }

    pub fn symbol(file: File, index: Symbol.Index) *Symbol {
        return switch (file) {
            .zig_object => |obj| &obj.symbols.items[@intFromEnum(index)],
            .object => |obj| &obj.symtable[@intFromEnum(index)],
        };
    }

    pub fn symbols(file: File) []const Symbol {
        return switch (file) {
            .zig_object => |obj| obj.symbols.items,
            .object => |obj| obj.symtable,
        };
    }

    pub fn symbolName(file: File, index: Symbol.Index) []const u8 {
        switch (file) {
            .zig_object => |obj| {
                const sym = obj.symbols.items[@intFromEnum(index)];
                return obj.string_table.get(sym.name).?;
            },
            .object => |obj| {
                const sym = obj.symtable[@intFromEnum(index)];
                return obj.string_table.get(sym.name);
            },
        }
    }

    pub fn parseSymbolIntoAtom(file: File, wasm_file: *Wasm, index: Symbol.Index) !AtomIndex {
        return switch (file) {
            inline else => |obj| obj.parseSymbolIntoAtom(wasm_file, index),
        };
    }

    /// For a given symbol index, find its corresponding import.
    /// Asserts import exists.
    pub fn import(file: File, symbol_index: Symbol.Index) types.Import {
        return switch (file) {
            .zig_object => |obj| obj.imports.get(symbol_index).?,
            .object => |obj| obj.findImport(obj.symtable[@intFromEnum(symbol_index)]),
        };
    }

    /// For a given offset, returns its string value.
    /// Asserts string exists in the object string table.
    pub fn string(file: File, offset: u32) []const u8 {
        return switch (file) {
            .zig_object => |obj| obj.string_table.get(offset).?,
            .object => |obj| obj.string_table.get(offset),
        };
    }

    pub fn importedGlobals(file: File) u32 {
        return switch (file) {
            inline else => |obj| obj.imported_globals_count,
        };
    }

    pub fn importedFunctions(file: File) u32 {
        return switch (file) {
            inline else => |obj| obj.imported_functions_count,
        };
    }

    pub fn importedTables(file: File) u32 {
        return switch (file) {
            inline else => |obj| obj.imported_tables_count,
        };
    }

    pub fn function(file: File, sym_index: Symbol.Index) std.wasm.Func {
        switch (file) {
            .zig_object => |obj| {
                const sym = obj.symbols.items[@intFromEnum(sym_index)];
                return obj.functions.items[sym.index];
            },
            .object => |obj| {
                const sym = obj.symtable[@intFromEnum(sym_index)];
                return obj.functions[sym.index - obj.imported_functions_count];
            },
        }
    }

    pub fn globals(file: File) []const std.wasm.Global {
        return switch (file) {
            .zig_object => |obj| obj.globals.items,
            .object => |obj| obj.globals,
        };
    }

    pub fn funcTypes(file: File) []const std.wasm.Type {
        return switch (file) {
            .zig_object => |obj| obj.func_types.items,
            .object => |obj| obj.func_types,
        };
    }

    pub const Entry = union(enum) {
        zig_object: ZigObject,
        object: Object,
    };
};

const std = @import("std");
const types = @import("types.zig");

const AtomIndex = @import("Atom.zig").Index;
const Object = @import("Object.zig");
const Symbol = @import("Symbol.zig");
const Wasm = @import("../Wasm.zig");
const ZigObject = @import("ZigObject.zig");