| ... | @@ -10,6 +10,7 @@ const leb = std.leb; | ... | @@ -10,6 +10,7 @@ const leb = std.leb; |
| 10 | const log = std.log.scoped(.link); | 10 | const log = std.log.scoped(.link); |
| 11 | const wasm = std.wasm; | 11 | const wasm = std.wasm; |
| 12 | | 12 | |
| | 13 | const Atom = @import("Wasm/Atom.zig"); |
| 13 | const Module = @import("../Module.zig"); | 14 | const Module = @import("../Module.zig"); |
| 14 | const Compilation = @import("../Compilation.zig"); | 15 | const Compilation = @import("../Compilation.zig"); |
| 15 | const CodeGen = @import("../arch/wasm/CodeGen.zig"); | 16 | const CodeGen = @import("../arch/wasm/CodeGen.zig"); |
| ... | @@ -22,101 +23,78 @@ const TypedValue = @import("../TypedValue.zig"); | ... | @@ -22,101 +23,78 @@ const TypedValue = @import("../TypedValue.zig"); |
| 22 | const LlvmObject = @import("../codegen/llvm.zig").Object; | 23 | const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 23 | const Air = @import("../Air.zig"); | 24 | const Air = @import("../Air.zig"); |
| 24 | const Liveness = @import("../Liveness.zig"); | 25 | const Liveness = @import("../Liveness.zig"); |
| | 26 | const Symbol = @import("Wasm/Symbol.zig"); |
| | 27 | const types = @import("Wasm/types.zig"); |
| 25 | | 28 | |
| 26 | pub const base_tag = link.File.Tag.wasm; | 29 | pub const base_tag = link.File.Tag.wasm; |
| 27 | | 30 | |
| | 31 | /// deprecated: Use `@import("Wasm/Atom.zig");` |
| | 32 | pub const DeclBlock = Atom; |
| | 33 | |
| 28 | base: link.File, | 34 | base: link.File, |
| 29 | /// If this is not null, an object file is created by LLVM and linked with LLD afterwards. | 35 | /// If this is not null, an object file is created by LLVM and linked with LLD afterwards. |
| 30 | llvm_object: ?*LlvmObject = null, | 36 | llvm_object: ?*LlvmObject = null, |
| 31 | /// List of all function Decls to be written to the output file. The index of | | |
| 32 | /// each Decl in this list at the time of writing the binary is used as the | | |
| 33 | /// function index. In the event where ext_funcs' size is not 0, the index of | | |
| 34 | /// each function is added on top of the ext_funcs' length. | | |
| 35 | /// TODO: can/should we access some data structure in Module directly? | | |
| 36 | funcs: std.ArrayListUnmanaged(*Module.Decl) = .{}, | | |
| 37 | /// List of all extern function Decls to be written to the `import` section of the | | |
| 38 | /// wasm binary. The position in the list defines the function index | | |
| 39 | ext_funcs: std.ArrayListUnmanaged(*Module.Decl) = .{}, | | |
| 40 | /// When importing objects from the host environment, a name must be supplied. | 37 | /// When importing objects from the host environment, a name must be supplied. |
| 41 | /// LLVM uses "env" by default when none is given. This would be a good default for Zig | 38 | /// LLVM uses "env" by default when none is given. This would be a good default for Zig |
| 42 | /// to support existing code. | 39 | /// to support existing code. |
| 43 | /// TODO: Allow setting this through a flag? | 40 | /// TODO: Allow setting this through a flag? |
| 44 | host_name: []const u8 = "env", | 41 | host_name: []const u8 = "env", |
| 45 | /// The last `DeclBlock` that was initialized will be saved here. | | |
| 46 | last_block: ?*DeclBlock = null, | | |
| 47 | /// Table with offsets, each element represents an offset with the value being | | |
| 48 | /// the offset into the 'data' section where the data lives | | |
| 49 | offset_table: std.ArrayListUnmanaged(u32) = .{}, | | |
| 50 | /// List of offset indexes which are free to be used for new decl's. | | |
| 51 | /// Each element's value points to an index into the offset_table. | | |
| 52 | offset_table_free_list: std.ArrayListUnmanaged(u32) = .{}, | | |
| 53 | /// List of all `Decl` that are currently alive. | 42 | /// List of all `Decl` that are currently alive. |
| 54 | /// This is ment for bookkeeping so we can safely cleanup all codegen memory | 43 | /// This is ment for bookkeeping so we can safely cleanup all codegen memory |
| 55 | /// when calling `deinit` | 44 | /// when calling `deinit` |
| 56 | symbols: std.ArrayListUnmanaged(*Module.Decl) = .{}, | 45 | decls: std.AutoHashMapUnmanaged(*Module.Decl, void) = .{}, |
| | 46 | /// List of all symbols. |
| | 47 | symbols: std.ArrayListUnmanaged(Symbol) = .{}, |
| 57 | /// List of symbol indexes which are free to be used. | 48 | /// List of symbol indexes which are free to be used. |
| 58 | symbols_free_list: std.ArrayListUnmanaged(u32) = .{}, | 49 | symbols_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| | 50 | /// Maps atoms to their segment index |
| | 51 | atoms: std.AutoHashMapUnmanaged(u32, *Atom) = .{}, |
| | 52 | /// Represents the index into `segments` where the 'code' section |
| | 53 | /// lives. |
| | 54 | code_section_index: ?u32 = null, |
| | 55 | /// The count of imported functions. This number will be appended |
| | 56 | /// to the function indexes as their index starts at the lowest non-extern function. |
| | 57 | imported_functions_count: u32 = 0, |
| | 58 | /// Map of symbol indexes, represented by its `wasm.Import` |
| | 59 | imports: std.AutoHashMapUnmanaged(u32, wasm.Import) = .{}, |
| | 60 | /// Represents non-synthetic section entries. |
| | 61 | /// Used for code, data and custom sections. |
| | 62 | segments: std.ArrayListUnmanaged(Segment) = .{}, |
| | 63 | /// Maps a data segment key (such as .rodata) to the index into `segments`. |
| | 64 | data_segments: std.StringArrayHashMapUnmanaged(u32) = .{}, |
| | 65 | /// A list of `types.Segment` which provide meta data |
| | 66 | /// about a data symbol such as its name |
| | 67 | segment_info: std.ArrayListUnmanaged(types.Segment) = .{}, |
| | 68 | |
| | 69 | // Output sections |
| | 70 | /// Output type section |
| | 71 | func_types: std.ArrayListUnmanaged(wasm.Type) = .{}, |
| | 72 | /// Output function section |
| | 73 | functions: std.ArrayListUnmanaged(wasm.Func) = .{}, |
| | 74 | /// Output global section |
| | 75 | globals: std.ArrayListUnmanaged(wasm.Global) = .{}, |
| | 76 | /// Memory section |
| | 77 | memories: wasm.Memory = .{ .limits = .{ .min = 0, .max = null } }, |
| | 78 | |
| | 79 | /// Indirect function table, used to call function pointers |
| | 80 | /// When this is non-zero, we must emit a table entry, |
| | 81 | /// as well as an 'elements' section. |
| | 82 | function_table: std.ArrayListUnmanaged(Symbol) = .{}, |
| | 83 | |
| | 84 | pub const Segment = struct { |
| | 85 | alignment: u32, |
| | 86 | size: u32, |
| | 87 | offset: u32, |
| | 88 | }; |
| 59 | | 89 | |
| 60 | pub const FnData = struct { | 90 | pub const FnData = struct { |
| 61 | /// Generated code for the type of the function | 91 | type_index: u32, |
| 62 | functype: std.ArrayListUnmanaged(u8), | | |
| 63 | /// Generated code for the body of the function | | |
| 64 | code: std.ArrayListUnmanaged(u8), | | |
| 65 | /// Locations in the generated code where function indexes must be filled in. | | |
| 66 | /// This must be kept ordered by offset. | | |
| 67 | /// `decl` is the symbol_index of the target. | | |
| 68 | idx_refs: std.ArrayListUnmanaged(struct { offset: u32, decl: u32 }), | | |
| 69 | | 92 | |
| 70 | pub const empty: FnData = .{ | 93 | pub const empty: FnData = .{ |
| 71 | .functype = .{}, | 94 | .type_index = undefined, |
| 72 | .code = .{}, | | |
| 73 | .idx_refs = .{}, | | |
| 74 | }; | 95 | }; |
| 75 | }; | 96 | }; |
| 76 | | 97 | |
| 77 | pub const DeclBlock = struct { | | |
| 78 | /// Determines whether the `DeclBlock` has been initialized for codegen. | | |
| 79 | init: bool, | | |
| 80 | /// Index into the `symbols` list. | | |
| 81 | symbol_index: u32, | | |
| 82 | /// Index into the offset table | | |
| 83 | offset_index: u32, | | |
| 84 | /// The size of the block and how large part of the data section it occupies. | | |
| 85 | /// Will be 0 when the Decl will not live inside the data section and `data` will be undefined. | | |
| 86 | size: u32, | | |
| 87 | /// Points to the previous and next blocks. | | |
| 88 | /// Can be used to find the total size, and used to calculate the `offset` based on the previous block. | | |
| 89 | prev: ?*DeclBlock, | | |
| 90 | next: ?*DeclBlock, | | |
| 91 | /// Pointer to data that will be written to the 'data' section. | | |
| 92 | /// This data either lives in `FnData.code` or is externally managed. | | |
| 93 | /// For data that does not live inside the 'data' section, this field will be undefined. (size == 0). | | |
| 94 | data: [*]const u8, | | |
| 95 | | | |
| 96 | pub const empty: DeclBlock = .{ | | |
| 97 | .init = false, | | |
| 98 | .symbol_index = 0, | | |
| 99 | .offset_index = 0, | | |
| 100 | .size = 0, | | |
| 101 | .prev = null, | | |
| 102 | .next = null, | | |
| 103 | .data = undefined, | | |
| 104 | }; | | |
| 105 | | | |
| 106 | /// Unplugs the `DeclBlock` from the chain | | |
| 107 | fn unplug(self: *DeclBlock) void { | | |
| 108 | if (self.prev) |prev| { | | |
| 109 | prev.next = self.next; | | |
| 110 | } | | |
| 111 | | | |
| 112 | if (self.next) |next| { | | |
| 113 | next.prev = self.prev; | | |
| 114 | } | | |
| 115 | self.next = null; | | |
| 116 | self.prev = null; | | |
| 117 | } | | |
| 118 | }; | | |
| 119 | | | |
| 120 | pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Options) !*Wasm { | 98 | pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Options) !*Wasm { |
| 121 | assert(options.object_format == .wasm); | 99 | assert(options.object_format == .wasm); |
| 122 | | 100 | |
| ... | @@ -139,6 +117,22 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio | ... | @@ -139,6 +117,22 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio |
| 139 | | 117 | |
| 140 | try file.writeAll(&(wasm.magic ++ wasm.version)); | 118 | try file.writeAll(&(wasm.magic ++ wasm.version)); |
| 141 | | 119 | |
| | 120 | // As sym_index '0' is reserved, we use it for our stack pointer symbol |
| | 121 | const global = try wasm_bin.globals.addOne(allocator); |
| | 122 | global.* = .{ |
| | 123 | .global_type = .{ |
| | 124 | .valtype = .i32, |
| | 125 | .mutable = true, |
| | 126 | }, |
| | 127 | .init = .{ .i32_const = 0 }, |
| | 128 | }; |
| | 129 | const symbol = try wasm_bin.symbols.addOne(allocator); |
| | 130 | symbol.* = .{ |
| | 131 | .name = "__stack_pointer", |
| | 132 | .tag = .global, |
| | 133 | .flags = 0, |
| | 134 | .index = 0, |
| | 135 | }; |
| 142 | return wasm_bin; | 136 | return wasm_bin; |
| 143 | } | 137 | } |
| 144 | | 138 | |
| ... | @@ -160,63 +154,57 @@ pub fn deinit(self: *Wasm) void { | ... | @@ -160,63 +154,57 @@ pub fn deinit(self: *Wasm) void { |
| 160 | if (self.llvm_object) |llvm_object| llvm_object.destroy(self.base.allocator); | 154 | if (self.llvm_object) |llvm_object| llvm_object.destroy(self.base.allocator); |
| 161 | } | 155 | } |
| 162 | | 156 | |
| 163 | for (self.symbols.items) |decl, symbol_index| { | 157 | var decl_it = self.decls.keyIterator(); |
| 164 | // Check if we already freed all memory for the symbol | 158 | while (decl_it.next()) |decl_ptr| { |
| 165 | // TODO: Audit this when we refactor the linker. | 159 | const decl = decl_ptr.*; |
| 166 | var already_freed = false; | 160 | decl.link.wasm.deinit(self.base.allocator); |
| 167 | for (self.symbols_free_list.items) |index| { | | |
| 168 | if (symbol_index == index) { | | |
| 169 | already_freed = true; | | |
| 170 | break; | | |
| 171 | } | | |
| 172 | } | | |
| 173 | if (already_freed) continue; | | |
| 174 | decl.fn_link.wasm.functype.deinit(self.base.allocator); | | |
| 175 | decl.fn_link.wasm.code.deinit(self.base.allocator); | | |
| 176 | decl.fn_link.wasm.idx_refs.deinit(self.base.allocator); | | |
| 177 | } | 161 | } |
| 178 | | 162 | |
| 179 | self.funcs.deinit(self.base.allocator); | 163 | for (self.func_types.items) |func_type| { |
| 180 | self.ext_funcs.deinit(self.base.allocator); | 164 | self.base.allocator.free(func_type.params); |
| 181 | self.offset_table.deinit(self.base.allocator); | 165 | self.base.allocator.free(func_type.returns); |
| 182 | self.offset_table_free_list.deinit(self.base.allocator); | 166 | } |
| | 167 | for (self.segment_info.items) |segment_info| { |
| | 168 | self.base.allocator.free(segment_info.name); |
| | 169 | } |
| | 170 | |
| | 171 | self.decls.deinit(self.base.allocator); |
| 183 | self.symbols.deinit(self.base.allocator); | 172 | self.symbols.deinit(self.base.allocator); |
| 184 | self.symbols_free_list.deinit(self.base.allocator); | 173 | self.symbols_free_list.deinit(self.base.allocator); |
| | 174 | self.atoms.deinit(self.base.allocator); |
| | 175 | self.segments.deinit(self.base.allocator); |
| | 176 | self.data_segments.deinit(self.base.allocator); |
| | 177 | self.segment_info.deinit(self.base.allocator); |
| | 178 | |
| | 179 | // free output sections |
| | 180 | self.imports.deinit(self.base.allocator); |
| | 181 | self.func_types.deinit(self.base.allocator); |
| | 182 | self.functions.deinit(self.base.allocator); |
| | 183 | self.globals.deinit(self.base.allocator); |
| | 184 | self.function_table.deinit(self.base.allocator); |
| 185 | } | 185 | } |
| 186 | | 186 | |
| 187 | pub fn allocateDeclIndexes(self: *Wasm, decl: *Module.Decl) !void { | 187 | pub fn allocateDeclIndexes(self: *Wasm, decl: *Module.Decl) !void { |
| 188 | if (decl.link.wasm.init) return; | 188 | if (decl.link.wasm.sym_index != 0) return; |
| 189 | | 189 | |
| 190 | try self.offset_table.ensureUnusedCapacity(self.base.allocator, 1); | | |
| 191 | try self.symbols.ensureUnusedCapacity(self.base.allocator, 1); | 190 | try self.symbols.ensureUnusedCapacity(self.base.allocator, 1); |
| | 191 | try self.decls.putNoClobber(self.base.allocator, decl, {}); |
| 192 | | 192 | |
| 193 | const block = &decl.link.wasm; | 193 | const atom = &decl.link.wasm; |
| 194 | block.init = true; | | |
| 195 | | 194 | |
| 196 | if (self.offset_table_free_list.popOrNull()) |index| { | 195 | var symbol: Symbol = .{ |
| 197 | block.offset_index = index; | 196 | .name = undefined, // will be set after updateDecl |
| 198 | } else { | 197 | .flags = 0, |
| 199 | block.offset_index = @intCast(u32, self.offset_table.items.len); | 198 | .tag = undefined, // will be set after updateDecl |
| 200 | _ = self.offset_table.addOneAssumeCapacity(); | 199 | .index = undefined, // will be set after updateDecl |
| 201 | } | 200 | }; |
| 202 | | 201 | |
| 203 | if (self.symbols_free_list.popOrNull()) |index| { | 202 | if (self.symbols_free_list.popOrNull()) |index| { |
| 204 | block.symbol_index = index; | 203 | atom.sym_index = index; |
| 205 | self.symbols.items[block.symbol_index] = decl; | 204 | self.symbols.items[index] = symbol; |
| 206 | } else { | 205 | } else { |
| 207 | block.symbol_index = @intCast(u32, self.symbols.items.len); | 206 | atom.sym_index = @intCast(u32, self.symbols.items.len); |
| 208 | self.symbols.appendAssumeCapacity(decl); | 207 | self.symbols.appendAssumeCapacity(symbol); |
| 209 | } | | |
| 210 | | | |
| 211 | self.offset_table.items[block.offset_index] = 0; | | |
| 212 | | | |
| 213 | if (decl.ty.zigTypeTag() == .Fn) { | | |
| 214 | switch (decl.val.tag()) { | | |
| 215 | // dependent on function type, appends it to the correct list | | |
| 216 | .function => try self.funcs.append(self.base.allocator, decl), | | |
| 217 | .extern_fn => try self.ext_funcs.append(self.base.allocator, decl), | | |
| 218 | else => unreachable, | | |
| 219 | } | | |
| 220 | } | 208 | } |
| 221 | } | 209 | } |
| 222 | | 210 | |
| ... | @@ -228,25 +216,21 @@ pub fn updateFunc(self: *Wasm, module: *Module, func: *Module.Fn, air: Air, live | ... | @@ -228,25 +216,21 @@ pub fn updateFunc(self: *Wasm, module: *Module, func: *Module.Fn, air: Air, live |
| 228 | if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(module, func, air, liveness); | 216 | if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(module, func, air, liveness); |
| 229 | } | 217 | } |
| 230 | const decl = func.owner_decl; | 218 | const decl = func.owner_decl; |
| 231 | assert(decl.link.wasm.init); // Must call allocateDeclIndexes() | 219 | assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes() |
| 232 | | 220 | |
| 233 | const fn_data = &decl.fn_link.wasm; | 221 | decl.link.wasm.clear(); |
| 234 | fn_data.functype.items.len = 0; | | |
| 235 | fn_data.code.items.len = 0; | | |
| 236 | fn_data.idx_refs.items.len = 0; | | |
| 237 | | 222 | |
| 238 | var codegen: CodeGen = .{ | 223 | var codegen: CodeGen = .{ |
| 239 | .gpa = self.base.allocator, | 224 | .gpa = self.base.allocator, |
| 240 | .air = air, | 225 | .air = air, |
| 241 | .liveness = liveness, | 226 | .liveness = liveness, |
| 242 | .values = .{}, | 227 | .values = .{}, |
| 243 | .code = fn_data.code.toManaged(self.base.allocator), | 228 | .code = std.ArrayList(u8).init(self.base.allocator), |
| 244 | .func_type_data = fn_data.functype.toManaged(self.base.allocator), | | |
| 245 | .decl = decl, | 229 | .decl = decl, |
| 246 | .err_msg = undefined, | 230 | .err_msg = undefined, |
| 247 | .locals = .{}, | 231 | .locals = .{}, |
| 248 | .target = self.base.options.target, | 232 | .target = self.base.options.target, |
| 249 | .bin_file = &self.base, | 233 | .bin_file = self, |
| 250 | .global_error_set = self.base.options.module.?.global_error_set, | 234 | .global_error_set = self.base.options.module.?.global_error_set, |
| 251 | }; | 235 | }; |
| 252 | defer codegen.deinit(); | 236 | defer codegen.deinit(); |
| ... | @@ -272,26 +256,21 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { | ... | @@ -272,26 +256,21 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { |
| 272 | if (build_options.have_llvm) { | 256 | if (build_options.have_llvm) { |
| 273 | if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(module, decl); | 257 | if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(module, decl); |
| 274 | } | 258 | } |
| 275 | assert(decl.link.wasm.init); // Must call allocateDeclIndexes() | 259 | assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes() |
| 276 | | 260 | |
| 277 | // TODO don't use this for non-functions | 261 | decl.link.wasm.clear(); |
| 278 | const fn_data = &decl.fn_link.wasm; | | |
| 279 | fn_data.functype.items.len = 0; | | |
| 280 | fn_data.code.items.len = 0; | | |
| 281 | fn_data.idx_refs.items.len = 0; | | |
| 282 | | 262 | |
| 283 | var codegen: CodeGen = .{ | 263 | var codegen: CodeGen = .{ |
| 284 | .gpa = self.base.allocator, | 264 | .gpa = self.base.allocator, |
| 285 | .air = undefined, | 265 | .air = undefined, |
| 286 | .liveness = undefined, | 266 | .liveness = undefined, |
| 287 | .values = .{}, | 267 | .values = .{}, |
| 288 | .code = fn_data.code.toManaged(self.base.allocator), | 268 | .code = std.ArrayList(u8).init(self.base.allocator), |
| 289 | .func_type_data = fn_data.functype.toManaged(self.base.allocator), | | |
| 290 | .decl = decl, | 269 | .decl = decl, |
| 291 | .err_msg = undefined, | 270 | .err_msg = undefined, |
| 292 | .locals = .{}, | 271 | .locals = .{}, |
| 293 | .target = self.base.options.target, | 272 | .target = self.base.options.target, |
| 294 | .bin_file = &self.base, | 273 | .bin_file = self, |
| 295 | .global_error_set = self.base.options.module.?.global_error_set, | 274 | .global_error_set = self.base.options.module.?.global_error_set, |
| 296 | }; | 275 | }; |
| 297 | defer codegen.deinit(); | 276 | defer codegen.deinit(); |
| ... | @@ -310,33 +289,19 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { | ... | @@ -310,33 +289,19 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { |
| 310 | } | 289 | } |
| 311 | | 290 | |
| 312 | fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, result: CodeGen.Result, codegen: *CodeGen) !void { | 291 | fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, result: CodeGen.Result, codegen: *CodeGen) !void { |
| 313 | const fn_data: *FnData = &decl.fn_link.wasm; | | |
| 314 | | | |
| 315 | fn_data.code = codegen.code.toUnmanaged(); | | |
| 316 | fn_data.functype = codegen.func_type_data.toUnmanaged(); | | |
| 317 | | | |
| 318 | const code: []const u8 = switch (result) { | 292 | const code: []const u8 = switch (result) { |
| 319 | .appended => @as([]const u8, fn_data.code.items), | 293 | .appended => @as([]const u8, codegen.code.items), |
| 320 | .externally_managed => |payload| payload, | 294 | .externally_managed => |payload| payload, |
| 321 | }; | 295 | }; |
| 322 | | 296 | |
| 323 | const block = &decl.link.wasm; | 297 | if (decl.isExtern()) { |
| 324 | if (decl.ty.zigTypeTag() != .Fn) { | 298 | try self.addOrUpdateImport(decl); |
| 325 | block.size = @intCast(u32, code.len); | | |
| 326 | block.data = code.ptr; | | |
| 327 | } | 299 | } |
| 328 | | 300 | |
| 329 | // If we're updating an existing decl, unplug it first | 301 | if (code.len == 0) return; |
| 330 | // to avoid infinite loops due to earlier links | 302 | const atom: *Atom = &decl.link.wasm; |
| 331 | block.unplug(); | 303 | atom.size = @intCast(u32, code.len); |
| 332 | | 304 | try atom.code.appendSlice(self.base.allocator, code); |
| 333 | if (self.last_block) |last| { | | |
| 334 | if (last != block) { | | |
| 335 | last.next = block; | | |
| 336 | block.prev = last; | | |
| 337 | } | | |
| 338 | } | | |
| 339 | self.last_block = block; | | |
| 340 | } | 305 | } |
| 341 | | 306 | |
| 342 | pub fn updateDeclExports( | 307 | pub fn updateDeclExports( |
| ... | @@ -357,30 +322,240 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { | ... | @@ -357,30 +322,240 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { |
| 357 | if (build_options.have_llvm) { | 322 | if (build_options.have_llvm) { |
| 358 | if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl); | 323 | if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl); |
| 359 | } | 324 | } |
| | 325 | const atom = &decl.link.wasm; |
| | 326 | self.symbols_free_list.append(self.base.allocator, atom.sym_index) catch {}; |
| | 327 | atom.deinit(self.base.allocator); |
| | 328 | _ = self.decls.remove(decl); |
| | 329 | |
| | 330 | if (decl.isExtern()) { |
| | 331 | const import = self.imports.fetchRemove(decl.link.wasm.sym_index).?.value; |
| | 332 | switch (import.kind) { |
| | 333 | .function => self.imported_functions_count -= 1, |
| | 334 | else => unreachable, |
| | 335 | } |
| | 336 | } |
| | 337 | } |
| | 338 | |
| | 339 | fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void { |
| | 340 | const symbol_index = decl.link.wasm.sym_index; |
| | 341 | const symbol: *Symbol = &self.symbols.items[symbol_index]; |
| | 342 | symbol.name = decl.name; |
| | 343 | symbol.setUndefined(true); |
| | 344 | switch (decl.ty.zigTypeTag()) { |
| | 345 | .Fn => { |
| | 346 | const gop = try self.imports.getOrPut(self.base.allocator, symbol_index); |
| | 347 | if (!gop.found_existing) { |
| | 348 | self.imported_functions_count += 1; |
| | 349 | gop.value_ptr.* = .{ |
| | 350 | .module_name = self.host_name, |
| | 351 | .name = std.mem.span(symbol.name), |
| | 352 | .kind = .{ .function = decl.fn_link.wasm.type_index }, |
| | 353 | }; |
| | 354 | } |
| | 355 | }, |
| | 356 | else => @panic("TODO: Implement undefined symbols for non-function declarations"), |
| | 357 | } |
| | 358 | } |
| | 359 | |
| | 360 | fn parseDeclIntoAtom(self: *Wasm, decl: *Module.Decl) !void { |
| | 361 | const atom: *Atom = &decl.link.wasm; |
| | 362 | const symbol: *Symbol = &self.symbols.items[atom.sym_index]; |
| | 363 | symbol.name = decl.name; |
| | 364 | atom.alignment = decl.ty.abiAlignment(self.base.options.target); |
| | 365 | const final_index: u32 = switch (decl.ty.zigTypeTag()) { |
| | 366 | .Fn => result: { |
| | 367 | const fn_data = decl.fn_link.wasm; |
| | 368 | const type_index = fn_data.type_index; |
| | 369 | const index = @intCast(u32, self.functions.items.len + self.imported_functions_count); |
| | 370 | try self.functions.append(self.base.allocator, .{ .type_index = type_index }); |
| | 371 | symbol.tag = .function; |
| | 372 | symbol.index = index; |
| | 373 | |
| | 374 | if (self.code_section_index == null) { |
| | 375 | self.code_section_index = @intCast(u32, self.segments.items.len); |
| | 376 | try self.segments.append(self.base.allocator, .{ |
| | 377 | .alignment = atom.alignment, |
| | 378 | .size = atom.size, |
| | 379 | .offset = 0, |
| | 380 | }); |
| | 381 | } |
| | 382 | |
| | 383 | break :result self.code_section_index.?; |
| | 384 | }, |
| | 385 | else => result: { |
| | 386 | const gop = try self.data_segments.getOrPut(self.base.allocator, ".rodata"); |
| | 387 | const atom_index = if (gop.found_existing) blk: { |
| | 388 | self.segments.items[gop.value_ptr.*].size += atom.size; |
| | 389 | break :blk gop.value_ptr.*; |
| | 390 | } else blk: { |
| | 391 | const index = @intCast(u32, self.segments.items.len); |
| | 392 | try self.segments.append(self.base.allocator, .{ |
| | 393 | .alignment = atom.alignment, |
| | 394 | .size = 0, |
| | 395 | .offset = 0, |
| | 396 | }); |
| | 397 | gop.value_ptr.* = index; |
| | 398 | break :blk index; |
| | 399 | }; |
| | 400 | const info_index = @intCast(u32, self.segment_info.items.len); |
| | 401 | const segment_name = try std.mem.concat(self.base.allocator, u8, &.{ |
| | 402 | ".rodata.", |
| | 403 | std.mem.span(symbol.name), |
| | 404 | }); |
| | 405 | errdefer self.base.allocator.free(segment_name); |
| | 406 | try self.segment_info.append(self.base.allocator, .{ |
| | 407 | .name = segment_name, |
| | 408 | .alignment = atom.alignment, |
| | 409 | .flags = 0, |
| | 410 | }); |
| | 411 | symbol.tag = .data; |
| | 412 | symbol.index = info_index; |
| | 413 | atom.alignment = decl.ty.abiAlignment(self.base.options.target); |
| | 414 | |
| | 415 | break :result atom_index; |
| | 416 | }, |
| | 417 | }; |
| | 418 | |
| | 419 | const segment: *Segment = &self.segments.items[final_index]; |
| | 420 | segment.alignment = std.math.max(segment.alignment, atom.alignment); |
| | 421 | segment.size = std.mem.alignForwardGeneric( |
| | 422 | u32, |
| | 423 | std.mem.alignForwardGeneric(u32, segment.size, atom.alignment) + atom.size, |
| | 424 | segment.alignment, |
| | 425 | ); |
| | 426 | |
| | 427 | if (self.atoms.getPtr(final_index)) |last| { |
| | 428 | last.*.next = atom; |
| | 429 | atom.prev = last.*; |
| | 430 | last.* = atom; |
| | 431 | } else { |
| | 432 | try self.atoms.putNoClobber(self.base.allocator, final_index, atom); |
| | 433 | } |
| | 434 | } |
| 360 | | 435 | |
| 361 | if (self.getFuncidx(decl)) |func_idx| { | 436 | fn allocateAtoms(self: *Wasm) !void { |
| 362 | switch (decl.val.tag()) { | 437 | var it = self.atoms.iterator(); |
| 363 | .function => _ = self.funcs.swapRemove(func_idx), | 438 | while (it.next()) |entry| { |
| 364 | .extern_fn => _ = self.ext_funcs.swapRemove(func_idx), | 439 | var atom: *Atom = entry.value_ptr.*.getFirst(); |
| | 440 | var offset: u32 = 0; |
| | 441 | while (true) { |
| | 442 | offset = std.mem.alignForwardGeneric(u32, offset, atom.alignment); |
| | 443 | atom.offset = offset; |
| | 444 | log.debug("Atom '{s}' allocated from 0x{x:0>8} to 0x{x:0>8} size={d}", .{ |
| | 445 | self.symbols.items[atom.sym_index].name, |
| | 446 | offset, |
| | 447 | offset + atom.size, |
| | 448 | atom.size, |
| | 449 | }); |
| | 450 | offset += atom.size; |
| | 451 | atom = atom.next orelse break; |
| | 452 | } |
| | 453 | } |
| | 454 | } |
| | 455 | |
| | 456 | fn setupImports(self: *Wasm) void { |
| | 457 | var function_index: u32 = 0; |
| | 458 | var it = self.imports.iterator(); |
| | 459 | while (it.next()) |entry| { |
| | 460 | const symbol = &self.symbols.items[entry.key_ptr.*]; |
| | 461 | const import: wasm.Import = entry.value_ptr.*; |
| | 462 | switch (import.kind) { |
| | 463 | .function => { |
| | 464 | symbol.index = function_index; |
| | 465 | function_index += 1; |
| | 466 | }, |
| 365 | else => unreachable, | 467 | else => unreachable, |
| 366 | } | 468 | } |
| 367 | } | 469 | } |
| 368 | const block = &decl.link.wasm; | 470 | } |
| 369 | | 471 | |
| 370 | if (self.last_block == block) { | 472 | /// Sets up the memory section of the wasm module, as well as the stack. |
| 371 | self.last_block = block.prev; | 473 | fn setupMemory(self: *Wasm) !void { |
| | 474 | log.debug("Setting up memory layout", .{}); |
| | 475 | const page_size = 64 * 1024; |
| | 476 | const stack_size = self.base.options.stack_size_override orelse page_size * 1; |
| | 477 | const stack_alignment = 16; |
| | 478 | var memory_ptr: u64 = self.base.options.global_base orelse 1024; |
| | 479 | memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment); |
| | 480 | |
| | 481 | var offset: u32 = @intCast(u32, memory_ptr); |
| | 482 | for (self.segments.items) |*segment, i| { |
| | 483 | // skip 'code' segments |
| | 484 | if (self.code_section_index) |index| { |
| | 485 | if (index == i) continue; |
| | 486 | } |
| | 487 | memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, segment.alignment); |
| | 488 | memory_ptr += segment.size; |
| | 489 | segment.offset = offset; |
| | 490 | offset += segment.size; |
| 372 | } | 491 | } |
| 373 | | 492 | |
| 374 | block.unplug(); | 493 | memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment); |
| | 494 | memory_ptr += stack_size; |
| | 495 | |
| | 496 | // Setup the max amount of pages |
| | 497 | // For now we only support wasm32 by setting the maximum allowed memory size 2^32-1 |
| | 498 | const max_memory_allowed: u64 = (1 << 32) - 1; |
| 375 | | 499 | |
| 376 | self.offset_table_free_list.append(self.base.allocator, decl.link.wasm.offset_index) catch {}; | 500 | if (self.base.options.initial_memory) |initial_memory| { |
| 377 | self.symbols_free_list.append(self.base.allocator, block.symbol_index) catch {}; | 501 | if (!std.mem.isAlignedGeneric(u64, initial_memory, page_size)) { |
| | 502 | log.err("Initial memory must be {d}-byte aligned", .{page_size}); |
| | 503 | return error.MissAlignment; |
| | 504 | } |
| | 505 | if (memory_ptr > initial_memory) { |
| | 506 | log.err("Initial memory too small, must be at least {d} bytes", .{memory_ptr}); |
| | 507 | return error.MemoryTooSmall; |
| | 508 | } |
| | 509 | if (initial_memory > max_memory_allowed) { |
| | 510 | log.err("Initial memory exceeds maximum memory {d}", .{max_memory_allowed}); |
| | 511 | return error.MemoryTooBig; |
| | 512 | } |
| | 513 | memory_ptr = initial_memory; |
| | 514 | } |
| 378 | | 515 | |
| 379 | block.init = false; | 516 | // In case we do not import memory, but define it ourselves, |
| | 517 | // set the minimum amount of pages on the memory section. |
| | 518 | self.memories.limits.min = @intCast(u32, std.mem.alignForwardGeneric(u64, memory_ptr, page_size) / page_size); |
| | 519 | log.debug("Total memory pages: {d}", .{self.memories.limits.min}); |
| 380 | | 520 | |
| 381 | decl.fn_link.wasm.functype.deinit(self.base.allocator); | 521 | if (self.base.options.max_memory) |max_memory| { |
| 382 | decl.fn_link.wasm.code.deinit(self.base.allocator); | 522 | if (!std.mem.isAlignedGeneric(u64, max_memory, page_size)) { |
| 383 | decl.fn_link.wasm.idx_refs.deinit(self.base.allocator); | 523 | log.err("Maximum memory must be {d}-byte aligned", .{page_size}); |
| | 524 | return error.MissAlignment; |
| | 525 | } |
| | 526 | if (memory_ptr > max_memory) { |
| | 527 | log.err("Maxmimum memory too small, must be at least {d} bytes", .{memory_ptr}); |
| | 528 | return error.MemoryTooSmall; |
| | 529 | } |
| | 530 | if (max_memory > max_memory_allowed) { |
| | 531 | log.err("Maximum memory exceeds maxmium amount {d}", .{max_memory_allowed}); |
| | 532 | return error.MemoryTooBig; |
| | 533 | } |
| | 534 | self.memories.limits.max = @intCast(u32, max_memory / page_size); |
| | 535 | log.debug("Maximum memory pages: {d}", .{self.memories.limits.max}); |
| | 536 | } |
| | 537 | |
| | 538 | // We always put the stack pointer global at index 0 |
| | 539 | self.globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr)); |
| | 540 | } |
| | 541 | |
| | 542 | fn resetState(self: *Wasm) void { |
| | 543 | for (self.segment_info.items) |*segment_info| { |
| | 544 | self.base.allocator.free(segment_info.name); |
| | 545 | } |
| | 546 | var decl_it = self.decls.keyIterator(); |
| | 547 | while (decl_it.next()) |decl| { |
| | 548 | const atom = &decl.*.link.wasm; |
| | 549 | atom.next = null; |
| | 550 | atom.prev = null; |
| | 551 | } |
| | 552 | self.functions.clearRetainingCapacity(); |
| | 553 | self.segments.clearRetainingCapacity(); |
| | 554 | self.segment_info.clearRetainingCapacity(); |
| | 555 | self.data_segments.clearRetainingCapacity(); |
| | 556 | self.function_table.clearRetainingCapacity(); |
| | 557 | self.atoms.clearRetainingCapacity(); |
| | 558 | self.code_section_index = null; |
| 384 | } | 559 | } |
| 385 | | 560 | |
| 386 | pub fn flush(self: *Wasm, comp: *Compilation) !void { | 561 | pub fn flush(self: *Wasm, comp: *Compilation) !void { |
| ... | @@ -396,29 +571,21 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -396,29 +571,21 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 396 | const tracy = trace(@src()); | 571 | const tracy = trace(@src()); |
| 397 | defer tracy.end(); | 572 | defer tracy.end(); |
| 398 | | 573 | |
| | 574 | // When we finish/error we reset the state of the linker |
| | 575 | // So we can rebuild the binary file on each incremental update |
| | 576 | defer self.resetState(); |
| | 577 | self.setupImports(); |
| | 578 | var decl_it = self.decls.keyIterator(); |
| | 579 | while (decl_it.next()) |decl| { |
| | 580 | if (decl.*.isExtern()) continue; |
| | 581 | try self.parseDeclIntoAtom(decl.*); |
| | 582 | } |
| | 583 | |
| | 584 | try self.setupMemory(); |
| | 585 | try self.allocateAtoms(); |
| | 586 | |
| 399 | const file = self.base.file.?; | 587 | const file = self.base.file.?; |
| 400 | const header_size = 5 + 1; | 588 | const header_size = 5 + 1; |
| 401 | // ptr_width in bytes | | |
| 402 | const ptr_width = self.base.options.target.cpu.arch.ptrBitWidth() / 8; | | |
| 403 | // The size of the offset table in bytes | | |
| 404 | // The table contains all decl's with its corresponding offset into | | |
| 405 | // the 'data' section | | |
| 406 | const offset_table_size = @intCast(u32, self.offset_table.items.len * ptr_width); | | |
| 407 | // The size of the emulated stack | | |
| 408 | const stack_size = @intCast(u32, self.base.options.stack_size_override orelse std.wasm.page_size); | | |
| 409 | | | |
| 410 | // The size of the data, this together with `offset_table_size` amounts to the | | |
| 411 | // total size of the 'data' section | | |
| 412 | var first_decl: ?*DeclBlock = null; | | |
| 413 | const data_size: u32 = if (self.last_block) |last| blk: { | | |
| 414 | var size = last.size; | | |
| 415 | var cur = last; | | |
| 416 | while (cur.prev) |prev| : (cur = prev) { | | |
| 417 | size += prev.size; | | |
| 418 | } | | |
| 419 | first_decl = cur; | | |
| 420 | break :blk size; | | |
| 421 | } else 0; | | |
| 422 | | 589 | |
| 423 | // No need to rewrite the magic/version header | 590 | // No need to rewrite the magic/version header |
| 424 | try file.setEndPos(@sizeOf(@TypeOf(wasm.magic ++ wasm.version))); | 591 | try file.setEndPos(@sizeOf(@TypeOf(wasm.magic ++ wasm.version))); |
| ... | @@ -427,38 +594,46 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -427,38 +594,46 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 427 | // Type section | 594 | // Type section |
| 428 | { | 595 | { |
| 429 | const header_offset = try reserveVecSectionHeader(file); | 596 | const header_offset = try reserveVecSectionHeader(file); |
| | 597 | const writer = file.writer(); |
| 430 | | 598 | |
| 431 | // extern functions are defined in the wasm binary first through the `import` | 599 | for (self.func_types.items) |func_type| { |
| 432 | // section, so define their func types first | 600 | try leb.writeULEB128(writer, wasm.function_type); |
| 433 | for (self.ext_funcs.items) |decl| try file.writeAll(decl.fn_link.wasm.functype.items); | 601 | try leb.writeULEB128(writer, @intCast(u32, func_type.params.len)); |
| 434 | for (self.funcs.items) |decl| try file.writeAll(decl.fn_link.wasm.functype.items); | 602 | for (func_type.params) |param_ty| try leb.writeULEB128(writer, wasm.valtype(param_ty)); |
| | 603 | try leb.writeULEB128(writer, @intCast(u32, func_type.returns.len)); |
| | 604 | for (func_type.returns) |ret_ty| try leb.writeULEB128(writer, wasm.valtype(ret_ty)); |
| | 605 | } |
| 435 | | 606 | |
| 436 | try writeVecSectionHeader( | 607 | try writeVecSectionHeader( |
| 437 | file, | 608 | file, |
| 438 | header_offset, | 609 | header_offset, |
| 439 | .type, | 610 | .type, |
| 440 | @intCast(u32, (try file.getPos()) - header_offset - header_size), | 611 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 441 | @intCast(u32, self.ext_funcs.items.len + self.funcs.items.len), | 612 | @intCast(u32, self.func_types.items.len), |
| 442 | ); | 613 | ); |
| 443 | } | 614 | } |
| 444 | | 615 | |
| 445 | // Import section | 616 | // Import section |
| 446 | { | 617 | const import_mem = self.base.options.import_memory; |
| 447 | // TODO: implement non-functions imports | 618 | if (self.imports.count() != 0 or import_mem) { |
| 448 | const header_offset = try reserveVecSectionHeader(file); | 619 | const header_offset = try reserveVecSectionHeader(file); |
| 449 | const writer = file.writer(); | 620 | const writer = file.writer(); |
| 450 | for (self.ext_funcs.items) |decl, typeidx| { | | |
| 451 | try leb.writeULEB128(writer, @intCast(u32, self.host_name.len)); | | |
| 452 | try writer.writeAll(self.host_name); | | |
| 453 | | 621 | |
| 454 | // wasm requires the length of the import name with no null-termination | 622 | var it = self.imports.iterator(); |
| 455 | const decl_len = mem.len(decl.name); | 623 | while (it.next()) |entry| { |
| 456 | try leb.writeULEB128(writer, @intCast(u32, decl_len)); | 624 | const import_symbol = self.symbols.items[entry.key_ptr.*]; |
| 457 | try writer.writeAll(decl.name[0..decl_len]); | 625 | std.debug.assert(import_symbol.isUndefined()); |
| | 626 | const import = entry.value_ptr.*; |
| | 627 | try emitImport(writer, import); |
| | 628 | } |
| 458 | | 629 | |
| 459 | // emit kind and the function type | 630 | if (import_mem) { |
| 460 | try writer.writeByte(wasm.externalKind(.function)); | 631 | const mem_imp: wasm.Import = .{ |
| 461 | try leb.writeULEB128(writer, @intCast(u32, typeidx)); | 632 | .module_name = self.host_name, |
| | 633 | .name = "memory", |
| | 634 | .kind = .{ .memory = self.memories.limits }, |
| | 635 | }; |
| | 636 | try emitImport(writer, mem_imp); |
| 462 | } | 637 | } |
| 463 | | 638 | |
| 464 | try writeVecSectionHeader( | 639 | try writeVecSectionHeader( |
| ... | @@ -466,7 +641,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -466,7 +641,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 466 | header_offset, | 641 | header_offset, |
| 467 | .import, | 642 | .import, |
| 468 | @intCast(u32, (try file.getPos()) - header_offset - header_size), | 643 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 469 | @intCast(u32, self.ext_funcs.items.len), | 644 | @intCast(u32, self.imports.count() + @boolToInt(import_mem)), |
| 470 | ); | 645 | ); |
| 471 | } | 646 | } |
| 472 | | 647 | |
| ... | @@ -474,9 +649,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -474,9 +649,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 474 | { | 649 | { |
| 475 | const header_offset = try reserveVecSectionHeader(file); | 650 | const header_offset = try reserveVecSectionHeader(file); |
| 476 | const writer = file.writer(); | 651 | const writer = file.writer(); |
| 477 | for (self.funcs.items) |_, typeidx| { | 652 | for (self.functions.items) |function| { |
| 478 | const func_idx = @intCast(u32, self.getFuncIdxOffset() + typeidx); | 653 | try leb.writeULEB128(writer, function.type_index); |
| 479 | try leb.writeULEB128(writer, func_idx); | | |
| 480 | } | 654 | } |
| 481 | | 655 | |
| 482 | try writeVecSectionHeader( | 656 | try writeVecSectionHeader( |
| ... | @@ -484,26 +658,16 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -484,26 +658,16 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 484 | header_offset, | 658 | header_offset, |
| 485 | .function, | 659 | .function, |
| 486 | @intCast(u32, (try file.getPos()) - header_offset - header_size), | 660 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 487 | @intCast(u32, self.funcs.items.len), | 661 | @intCast(u32, self.functions.items.len), |
| 488 | ); | 662 | ); |
| 489 | } | 663 | } |
| 490 | | 664 | |
| 491 | // Memory section | 665 | // Memory section |
| 492 | { | 666 | if (!self.base.options.import_memory) { |
| 493 | const header_offset = try reserveVecSectionHeader(file); | 667 | const header_offset = try reserveVecSectionHeader(file); |
| 494 | const writer = file.writer(); | 668 | const writer = file.writer(); |
| 495 | | 669 | |
| 496 | try leb.writeULEB128(writer, @as(u32, 0)); | 670 | try emitLimits(writer, self.memories.limits); |
| 497 | // Calculate the amount of memory pages are required and write them. | | |
| 498 | // Wasm uses 64kB page sizes. Round up to ensure the data segments fit into the memory | | |
| 499 | try leb.writeULEB128( | | |
| 500 | writer, | | |
| 501 | try std.math.divCeil( | | |
| 502 | u32, | | |
| 503 | offset_table_size + data_size + stack_size, | | |
| 504 | std.wasm.page_size, | | |
| 505 | ), | | |
| 506 | ); | | |
| 507 | try writeVecSectionHeader( | 671 | try writeVecSectionHeader( |
| 508 | file, | 672 | file, |
| 509 | header_offset, | 673 | header_offset, |
| ... | @@ -515,29 +679,21 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -515,29 +679,21 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 515 | | 679 | |
| 516 | // Global section (used to emit stack pointer) | 680 | // Global section (used to emit stack pointer) |
| 517 | { | 681 | { |
| 518 | // We emit the emulated stack at the end of the data section, | | |
| 519 | // 'growing' downwards towards the program memory. | | |
| 520 | // TODO: Have linker resolve the offset table, so we can emit the stack | | |
| 521 | // at the start so we can't overwrite program memory with the stack. | | |
| 522 | const sp_value = offset_table_size + data_size + std.wasm.page_size; | | |
| 523 | const mutable = true; // stack pointer MUST be mutable | | |
| 524 | const header_offset = try reserveVecSectionHeader(file); | 682 | const header_offset = try reserveVecSectionHeader(file); |
| 525 | const writer = file.writer(); | 683 | const writer = file.writer(); |
| 526 | | 684 | |
| 527 | try writer.writeByte(wasm.valtype(.i32)); | 685 | for (self.globals.items) |global| { |
| 528 | try writer.writeByte(@boolToInt(mutable)); | 686 | try writer.writeByte(wasm.valtype(global.global_type.valtype)); |
| 529 | | 687 | try writer.writeByte(@boolToInt(global.global_type.mutable)); |
| 530 | // set the initial value of the stack pointer to the data size + stack size | 688 | try emitInit(writer, global.init); |
| 531 | try writer.writeByte(wasm.opcode(.i32_const)); | 689 | } |
| 532 | try leb.writeILEB128(writer, @bitCast(i32, sp_value)); | | |
| 533 | try writer.writeByte(wasm.opcode(.end)); | | |
| 534 | | 690 | |
| 535 | try writeVecSectionHeader( | 691 | try writeVecSectionHeader( |
| 536 | file, | 692 | file, |
| 537 | header_offset, | 693 | header_offset, |
| 538 | .global, | 694 | .global, |
| 539 | @intCast(u32, (try file.getPos()) - header_offset - header_size), | 695 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 540 | @as(u32, 1), | 696 | @intCast(u32, self.globals.items.len), |
| 541 | ); | 697 | ); |
| 542 | } | 698 | } |
| 543 | | 699 | |
| ... | @@ -554,10 +710,13 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -554,10 +710,13 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 554 | | 710 | |
| 555 | switch (exprt.exported_decl.ty.zigTypeTag()) { | 711 | switch (exprt.exported_decl.ty.zigTypeTag()) { |
| 556 | .Fn => { | 712 | .Fn => { |
| | 713 | const target = exprt.exported_decl.link.wasm.sym_index; |
| | 714 | const target_symbol = self.symbols.items[target]; |
| | 715 | std.debug.assert(target_symbol.tag == .function); |
| 557 | // Type of the export | 716 | // Type of the export |
| 558 | try writer.writeByte(wasm.externalKind(.function)); | 717 | try writer.writeByte(wasm.externalKind(.function)); |
| 559 | // Exported function index | 718 | // Exported function index |
| 560 | try leb.writeULEB128(writer, self.getFuncidx(exprt.exported_decl).?); | 719 | try leb.writeULEB128(writer, target_symbol.index); |
| 561 | }, | 720 | }, |
| 562 | else => return error.TODOImplementNonFnDeclsForWasm, | 721 | else => return error.TODOImplementNonFnDeclsForWasm, |
| 563 | } | 722 | } |
| ... | @@ -567,7 +726,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -567,7 +726,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 567 | } | 726 | } |
| 568 | | 727 | |
| 569 | // export memory if size is not 0 | 728 | // export memory if size is not 0 |
| 570 | if (data_size != 0) { | 729 | if (!self.base.options.import_memory) { |
| 571 | try leb.writeULEB128(writer, @intCast(u32, "memory".len)); | 730 | try leb.writeULEB128(writer, @intCast(u32, "memory".len)); |
| 572 | try writer.writeAll("memory"); | 731 | try writer.writeAll("memory"); |
| 573 | try writer.writeByte(wasm.externalKind(.memory)); | 732 | try writer.writeByte(wasm.externalKind(.memory)); |
| ... | @@ -585,75 +744,143 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -585,75 +744,143 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 585 | } | 744 | } |
| 586 | | 745 | |
| 587 | // Code section | 746 | // Code section |
| 588 | { | 747 | if (self.code_section_index) |code_index| { |
| 589 | const header_offset = try reserveVecSectionHeader(file); | 748 | const header_offset = try reserveVecSectionHeader(file); |
| 590 | const writer = file.writer(); | 749 | const writer = file.writer(); |
| 591 | for (self.funcs.items) |decl| { | 750 | var atom: *Atom = self.atoms.get(code_index).?.getFirst(); |
| 592 | const fn_data = &decl.fn_link.wasm; | 751 | while (true) { |
| 593 | | 752 | try atom.resolveRelocs(self); |
| 594 | // Write the already generated code to the file, inserting | 753 | try leb.writeULEB128(writer, atom.size); |
| 595 | // function indexes where required. | 754 | try writer.writeAll(atom.code.items); |
| 596 | for (fn_data.idx_refs.items) |idx_ref| { | 755 | atom = atom.next orelse break; |
| 597 | const relocatable_decl = self.symbols.items[idx_ref.decl]; | | |
| 598 | const index = self.getFuncidx(relocatable_decl).?; | | |
| 599 | leb.writeUnsignedFixed(5, fn_data.code.items[idx_ref.offset..][0..5], index); | | |
| 600 | } | | |
| 601 | try writer.writeAll(fn_data.code.items); | | |
| 602 | } | 756 | } |
| 603 | try writeVecSectionHeader( | 757 | try writeVecSectionHeader( |
| 604 | file, | 758 | file, |
| 605 | header_offset, | 759 | header_offset, |
| 606 | .code, | 760 | .code, |
| 607 | @intCast(u32, (try file.getPos()) - header_offset - header_size), | 761 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 608 | @intCast(u32, self.funcs.items.len), | 762 | @intCast(u32, self.functions.items.len), |
| 609 | ); | 763 | ); |
| 610 | } | 764 | } |
| 611 | | 765 | |
| 612 | // Data section | 766 | // Data section |
| 613 | if (data_size != 0) { | 767 | if (self.data_segments.count() != 0) { |
| 614 | const header_offset = try reserveVecSectionHeader(file); | 768 | const header_offset = try reserveVecSectionHeader(file); |
| 615 | const writer = file.writer(); | 769 | const writer = file.writer(); |
| 616 | // index to memory section (currently, there can only be 1 memory section in wasm) | | |
| 617 | try leb.writeULEB128(writer, @as(u32, 0)); | | |
| 618 | | | |
| 619 | // offset into data section | | |
| 620 | try writer.writeByte(wasm.opcode(.i32_const)); | | |
| 621 | try leb.writeILEB128(writer, @as(i32, 0)); | | |
| 622 | try writer.writeByte(wasm.opcode(.end)); | | |
| 623 | | | |
| 624 | const total_size = offset_table_size + data_size; | | |
| 625 | | | |
| 626 | // offset table + data size | | |
| 627 | try leb.writeULEB128(writer, total_size); | | |
| 628 | | 770 | |
| 629 | // fill in the offset table and the data segments | 771 | var it = self.data_segments.iterator(); |
| 630 | const file_offset = try file.getPos(); | 772 | var segment_count: u32 = 0; |
| 631 | var cur = first_decl; | 773 | while (it.next()) |entry| { |
| 632 | var data_offset = offset_table_size; | 774 | // do not output 'bss' section |
| 633 | while (cur) |cur_block| : (cur = cur_block.next) { | 775 | if (std.mem.eql(u8, entry.key_ptr.*, ".bss")) continue; |
| 634 | if (cur_block.size == 0) continue; | 776 | segment_count += 1; |
| 635 | assert(cur_block.init); | 777 | const atom_index = entry.value_ptr.*; |
| 636 | | 778 | var atom: *Atom = self.atoms.getPtr(atom_index).?.*.getFirst(); |
| 637 | const offset = (cur_block.offset_index) * ptr_width; | 779 | var segment = self.segments.items[atom_index]; |
| 638 | var buf: [4]u8 = undefined; | 780 | |
| 639 | std.mem.writeIntLittle(u32, &buf, data_offset); | 781 | // flag and index to memory section (currently, there can only be 1 memory section in wasm) |
| 640 | | 782 | try leb.writeULEB128(writer, @as(u32, 0)); |
| 641 | try file.pwriteAll(&buf, file_offset + offset); | 783 | // offset into data section |
| 642 | try file.pwriteAll(cur_block.data[0..cur_block.size], file_offset + data_offset); | 784 | try emitInit(writer, .{ .i32_const = @bitCast(i32, segment.offset) }); |
| 643 | data_offset += cur_block.size; | 785 | try leb.writeULEB128(writer, segment.size); |
| | 786 | |
| | 787 | // fill in the offset table and the data segments |
| | 788 | var current_offset: u32 = 0; |
| | 789 | while (true) { |
| | 790 | try atom.resolveRelocs(self); |
| | 791 | |
| | 792 | // Pad with zeroes to ensure all segments are aligned |
| | 793 | if (current_offset != atom.offset) { |
| | 794 | const diff = atom.offset - current_offset; |
| | 795 | try writer.writeByteNTimes(0, diff); |
| | 796 | current_offset += diff; |
| | 797 | } |
| | 798 | std.debug.assert(current_offset == atom.offset); |
| | 799 | std.debug.assert(atom.code.items.len == atom.size); |
| | 800 | try writer.writeAll(atom.code.items); |
| | 801 | |
| | 802 | current_offset += atom.size; |
| | 803 | if (atom.next) |next| { |
| | 804 | atom = next; |
| | 805 | } else { |
| | 806 | // also pad with zeroes when last atom to ensure |
| | 807 | // segments are aligned. |
| | 808 | if (current_offset != segment.size) { |
| | 809 | try writer.writeByteNTimes(0, segment.size - current_offset); |
| | 810 | } |
| | 811 | break; |
| | 812 | } |
| | 813 | } |
| 644 | } | 814 | } |
| 645 | | 815 | |
| 646 | try file.seekTo(file_offset + data_offset); | | |
| 647 | try writeVecSectionHeader( | 816 | try writeVecSectionHeader( |
| 648 | file, | 817 | file, |
| 649 | header_offset, | 818 | header_offset, |
| 650 | .data, | 819 | .data, |
| 651 | @intCast(u32, (file_offset + data_offset) - header_offset - header_size), | 820 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 652 | @intCast(u32, 1), // only 1 data section | 821 | @intCast(u32, segment_count), |
| 653 | ); | 822 | ); |
| 654 | } | 823 | } |
| 655 | } | 824 | } |
| 656 | | 825 | |
| | 826 | fn emitLimits(writer: anytype, limits: wasm.Limits) !void { |
| | 827 | try leb.writeULEB128(writer, @boolToInt(limits.max != null)); |
| | 828 | try leb.writeULEB128(writer, limits.min); |
| | 829 | if (limits.max) |max| { |
| | 830 | try leb.writeULEB128(writer, max); |
| | 831 | } |
| | 832 | } |
| | 833 | |
| | 834 | fn emitInit(writer: anytype, init_expr: wasm.InitExpression) !void { |
| | 835 | switch (init_expr) { |
| | 836 | .i32_const => |val| { |
| | 837 | try writer.writeByte(wasm.opcode(.i32_const)); |
| | 838 | try leb.writeILEB128(writer, val); |
| | 839 | }, |
| | 840 | .i64_const => |val| { |
| | 841 | try writer.writeByte(wasm.opcode(.i64_const)); |
| | 842 | try leb.writeILEB128(writer, val); |
| | 843 | }, |
| | 844 | .f32_const => |val| { |
| | 845 | try writer.writeByte(wasm.opcode(.f32_const)); |
| | 846 | try writer.writeIntLittle(u32, @bitCast(u32, val)); |
| | 847 | }, |
| | 848 | .f64_const => |val| { |
| | 849 | try writer.writeByte(wasm.opcode(.f64_const)); |
| | 850 | try writer.writeIntLittle(u64, @bitCast(u64, val)); |
| | 851 | }, |
| | 852 | .global_get => |val| { |
| | 853 | try writer.writeByte(wasm.opcode(.global_get)); |
| | 854 | try leb.writeULEB128(writer, val); |
| | 855 | }, |
| | 856 | } |
| | 857 | try writer.writeByte(wasm.opcode(.end)); |
| | 858 | } |
| | 859 | |
| | 860 | fn emitImport(writer: anytype, import: wasm.Import) !void { |
| | 861 | try leb.writeULEB128(writer, @intCast(u32, import.module_name.len)); |
| | 862 | try writer.writeAll(import.module_name); |
| | 863 | |
| | 864 | try leb.writeULEB128(writer, @intCast(u32, import.name.len)); |
| | 865 | try writer.writeAll(import.name); |
| | 866 | |
| | 867 | try writer.writeByte(@enumToInt(import.kind)); |
| | 868 | switch (import.kind) { |
| | 869 | .function => |type_index| try leb.writeULEB128(writer, type_index), |
| | 870 | .global => |global_type| { |
| | 871 | try leb.writeULEB128(writer, wasm.valtype(global_type.valtype)); |
| | 872 | try writer.writeByte(@boolToInt(global_type.mutable)); |
| | 873 | }, |
| | 874 | .table => |table| { |
| | 875 | try leb.writeULEB128(writer, wasm.reftype(table.reftype)); |
| | 876 | try emitLimits(writer, table.limits); |
| | 877 | }, |
| | 878 | .memory => |limits| { |
| | 879 | try emitLimits(writer, limits); |
| | 880 | }, |
| | 881 | } |
| | 882 | } |
| | 883 | |
| 657 | fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { | 884 | fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { |
| 658 | const tracy = trace(@src()); | 885 | const tracy = trace(@src()); |
| 659 | defer tracy.end(); | 886 | defer tracy.end(); |
| ... | @@ -970,32 +1197,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { | ... | @@ -970,32 +1197,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { |
| 970 | } | 1197 | } |
| 971 | } | 1198 | } |
| 972 | | 1199 | |
| 973 | /// Get the current index of a given Decl in the function list | | |
| 974 | /// This will correctly provide the index, regardless whether the function is extern or not | | |
| 975 | /// TODO: we could maintain a hash map to potentially make this simpler | | |
| 976 | fn getFuncidx(self: Wasm, decl: *Module.Decl) ?u32 { | | |
| 977 | var offset: u32 = 0; | | |
| 978 | const slice = switch (decl.val.tag()) { | | |
| 979 | .function => blk: { | | |
| 980 | // when the target is a regular function, we have to calculate | | |
| 981 | // the offset of where the index starts | | |
| 982 | offset += self.getFuncIdxOffset(); | | |
| 983 | break :blk self.funcs.items; | | |
| 984 | }, | | |
| 985 | .extern_fn => self.ext_funcs.items, | | |
| 986 | else => return null, | | |
| 987 | }; | | |
| 988 | return for (slice) |func, idx| { | | |
| 989 | if (func == decl) break @intCast(u32, offset + idx); | | |
| 990 | } else null; | | |
| 991 | } | | |
| 992 | | | |
| 993 | /// Based on the size of `ext_funcs` returns the | | |
| 994 | /// offset of the function indices | | |
| 995 | fn getFuncIdxOffset(self: Wasm) u32 { | | |
| 996 | return @intCast(u32, self.ext_funcs.items.len); | | |
| 997 | } | | |
| 998 | | | |
| 999 | fn reserveVecSectionHeader(file: fs.File) !u64 { | 1200 | fn reserveVecSectionHeader(file: fs.File) !u64 { |
| 1000 | // section id + fixed leb contents size + fixed leb vector length | 1201 | // section id + fixed leb contents size + fixed leb vector length |
| 1001 | const header_size = 1 + 5 + 5; | 1202 | const header_size = 1 + 5 + 5; |
| ... | @@ -1012,3 +1213,36 @@ fn writeVecSectionHeader(file: fs.File, offset: u64, section: wasm.Section, size | ... | @@ -1012,3 +1213,36 @@ fn writeVecSectionHeader(file: fs.File, offset: u64, section: wasm.Section, size |
| 1012 | leb.writeUnsignedFixed(5, buf[6..], items); | 1213 | leb.writeUnsignedFixed(5, buf[6..], items); |
| 1013 | try file.pwriteAll(&buf, offset); | 1214 | try file.pwriteAll(&buf, offset); |
| 1014 | } | 1215 | } |
| | 1216 | |
| | 1217 | /// Searches for an a matching function signature, when not found |
| | 1218 | /// a new entry will be made. The index of the existing/new signature will be returned. |
| | 1219 | pub fn putOrGetFuncType(self: *Wasm, func_type: wasm.Type) !u32 { |
| | 1220 | var index: u32 = 0; |
| | 1221 | while (index < self.func_types.items.len) : (index += 1) { |
| | 1222 | if (self.func_types.items[index].eql(func_type)) return index; |
| | 1223 | } |
| | 1224 | |
| | 1225 | // functype does not exist. |
| | 1226 | const params = try self.base.allocator.dupe(wasm.Valtype, func_type.params); |
| | 1227 | errdefer self.base.allocator.free(params); |
| | 1228 | const returns = try self.base.allocator.dupe(wasm.Valtype, func_type.returns); |
| | 1229 | errdefer self.base.allocator.free(returns); |
| | 1230 | try self.func_types.append(self.base.allocator, .{ |
| | 1231 | .params = params, |
| | 1232 | .returns = returns, |
| | 1233 | }); |
| | 1234 | return index; |
| | 1235 | } |
| | 1236 | |
| | 1237 | /// From a given index and an `ExternalKind`, finds the corresponding Import. |
| | 1238 | /// This is due to indexes for imports being unique per type, rather than across all imports. |
| | 1239 | fn findImport(self: Wasm, index: u32, external_type: wasm.ExternalKind) ?*wasm.Import { |
| | 1240 | var current_index: u32 = 0; |
| | 1241 | for (self.imports.items) |*import| { |
| | 1242 | if (import.kind == external_type) { |
| | 1243 | if (current_index == index) return import; |
| | 1244 | current_index += 1; |
| | 1245 | } |
| | 1246 | } |
| | 1247 | return null; |
| | 1248 | } |