| ... | ... | @@ -39,8 +39,6 @@ llvm_object: ?*LlvmObject = null, |
| 39 | 39 | /// to support existing code. |
| 40 | 40 | /// TODO: Allow setting this through a flag? |
| 41 | 41 | host_name: []const u8 = "env", |
| 42 | | /// The last `DeclBlock` that was initialized will be saved here. |
| 43 | | last_atom: ?*Atom = null, |
| 44 | 42 | /// List of all `Decl` that are currently alive. |
| 45 | 43 | /// This is ment for bookkeeping so we can safely cleanup all codegen memory |
| 46 | 44 | /// when calling `deinit` |
| ... | ... | @@ -57,10 +55,8 @@ code_section_index: ?u32 = null, |
| 57 | 55 | /// The count of imported functions. This number will be appended |
| 58 | 56 | /// to the function indexes as their index starts at the lowest non-extern function. |
| 59 | 57 | imported_functions_count: u32 = 0, |
| 60 | | /// List of all 'extern' declarations |
| 61 | | imports: std.ArrayListUnmanaged(wasm.Import) = .{}, |
| 62 | | /// List of indexes of symbols representing extern declarations. |
| 63 | | import_symbols: std.ArrayListUnmanaged(u32) = .{}, |
| 58 | /// Map of symbol indexes, represented by its `wasm.Import` |
| 59 | imports: std.AutoHashMapUnmanaged(u32, wasm.Import) = .{}, |
| 64 | 60 | /// Represents non-synthetic section entries. |
| 65 | 61 | /// Used for code, data and custom sections. |
| 66 | 62 | segments: std.ArrayListUnmanaged(Segment) = .{}, |
| ... | ... | @@ -77,6 +73,8 @@ func_types: std.ArrayListUnmanaged(wasm.Type) = .{}, |
| 77 | 73 | functions: std.ArrayListUnmanaged(wasm.Func) = .{}, |
| 78 | 74 | /// Output global section |
| 79 | 75 | globals: std.ArrayListUnmanaged(wasm.Global) = .{}, |
| 76 | /// Memory section |
| 77 | memories: wasm.Memory = .{ .limits = .{ .min = 0, .max = null } }, |
| 80 | 78 | |
| 81 | 79 | /// Indirect function table, used to call function pointers |
| 82 | 80 | /// When this is non-zero, we must emit a table entry, |
| ... | ... | @@ -180,7 +178,6 @@ pub fn deinit(self: *Wasm) void { |
| 180 | 178 | |
| 181 | 179 | // free output sections |
| 182 | 180 | self.imports.deinit(self.base.allocator); |
| 183 | | self.import_symbols.deinit(self.base.allocator); |
| 184 | 181 | self.func_types.deinit(self.base.allocator); |
| 185 | 182 | self.functions.deinit(self.base.allocator); |
| 186 | 183 | self.globals.deinit(self.base.allocator); |
| ... | ... | @@ -221,6 +218,8 @@ pub fn updateFunc(self: *Wasm, module: *Module, func: *Module.Fn, air: Air, live |
| 221 | 218 | const decl = func.owner_decl; |
| 222 | 219 | assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes() |
| 223 | 220 | |
| 221 | decl.link.wasm.clear(); |
| 222 | |
| 224 | 223 | var codegen: CodeGen = .{ |
| 225 | 224 | .gpa = self.base.allocator, |
| 226 | 225 | .air = air, |
| ... | ... | @@ -259,6 +258,8 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { |
| 259 | 258 | } |
| 260 | 259 | assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes() |
| 261 | 260 | |
| 261 | decl.link.wasm.clear(); |
| 262 | |
| 262 | 263 | var codegen: CodeGen = .{ |
| 263 | 264 | .gpa = self.base.allocator, |
| 264 | 265 | .air = undefined, |
| ... | ... | @@ -293,19 +294,14 @@ fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, result: CodeGen.Result, cod |
| 293 | 294 | .externally_managed => |payload| payload, |
| 294 | 295 | }; |
| 295 | 296 | |
| 297 | if (decl.isExtern()) { |
| 298 | try self.addOrUpdateImport(decl); |
| 299 | } |
| 300 | |
| 301 | if (code.len == 0) return; |
| 296 | 302 | const atom: *Atom = &decl.link.wasm; |
| 297 | 303 | atom.size = @intCast(u32, code.len); |
| 298 | 304 | try atom.code.appendSlice(self.base.allocator, code); |
| 299 | | |
| 300 | | // If we're updating an existing decl, unplug it first |
| 301 | | // to avoid infinite loops due to earlier links |
| 302 | | atom.unplug(); |
| 303 | | |
| 304 | | if (decl.isExtern()) { |
| 305 | | try self.createUndefinedSymbol(decl, atom.sym_index); |
| 306 | | } else { |
| 307 | | try self.createDefinedSymbol(decl, atom.sym_index, atom); |
| 308 | | } |
| 309 | 305 | } |
| 310 | 306 | |
| 311 | 307 | pub fn updateDeclExports( |
| ... | ... | @@ -326,60 +322,62 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { |
| 326 | 322 | if (build_options.have_llvm) { |
| 327 | 323 | if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl); |
| 328 | 324 | } |
| 329 | | |
| 330 | 325 | const atom = &decl.link.wasm; |
| 331 | | |
| 332 | | if (self.last_atom == atom) { |
| 333 | | self.last_atom = atom.prev; |
| 334 | | } |
| 335 | | |
| 336 | | atom.unplug(); |
| 337 | 326 | self.symbols_free_list.append(self.base.allocator, atom.sym_index) catch {}; |
| 338 | 327 | atom.deinit(self.base.allocator); |
| 339 | 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 | } |
| 340 | 337 | } |
| 341 | 338 | |
| 342 | | fn createUndefinedSymbol(self: *Wasm, decl: *Module.Decl, symbol_index: u32) !void { |
| 343 | | var symbol: *Symbol = &self.symbols.items[symbol_index]; |
| 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]; |
| 344 | 342 | symbol.name = decl.name; |
| 345 | 343 | symbol.setUndefined(true); |
| 346 | 344 | switch (decl.ty.zigTypeTag()) { |
| 347 | 345 | .Fn => { |
| 348 | | symbol.index = self.imported_functions_count; |
| 349 | | self.imported_functions_count += 1; |
| 350 | | try self.import_symbols.append(self.base.allocator, symbol_index); |
| 351 | | try self.imports.append(self.base.allocator, .{ |
| 352 | | .module_name = self.host_name, |
| 353 | | .name = std.mem.span(decl.name), |
| 354 | | .kind = .{ .function = decl.fn_link.wasm.type_index }, |
| 355 | | }); |
| 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 | } |
| 356 | 355 | }, |
| 357 | 356 | else => @panic("TODO: Implement undefined symbols for non-function declarations"), |
| 358 | 357 | } |
| 359 | 358 | } |
| 360 | 359 | |
| 361 | | /// Creates a defined symbol, as well as inserts the given `atom` into the chain |
| 362 | | fn createDefinedSymbol(self: *Wasm, decl: *Module.Decl, symbol_index: u32, atom: *Atom) !void { |
| 363 | | const symbol: *Symbol = &self.symbols.items[symbol_index]; |
| 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]; |
| 364 | 363 | symbol.name = decl.name; |
| 365 | | const final_index = switch (decl.ty.zigTypeTag()) { |
| 364 | atom.alignment = decl.ty.abiAlignment(self.base.options.target); |
| 365 | const final_index: u32 = switch (decl.ty.zigTypeTag()) { |
| 366 | 366 | .Fn => result: { |
| 367 | | const type_index = decl.fn_link.wasm.type_index; |
| 368 | | const index = @intCast(u32, self.functions.items.len); |
| 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); |
| 369 | 370 | try self.functions.append(self.base.allocator, .{ .type_index = type_index }); |
| 370 | 371 | symbol.tag = .function; |
| 371 | 372 | symbol.index = index; |
| 372 | | atom.alignment = 1; |
| 373 | 373 | |
| 374 | 374 | if (self.code_section_index == null) { |
| 375 | 375 | self.code_section_index = @intCast(u32, self.segments.items.len); |
| 376 | 376 | try self.segments.append(self.base.allocator, .{ |
| 377 | 377 | .alignment = atom.alignment, |
| 378 | 378 | .size = atom.size, |
| 379 | | .offset = atom.offset, |
| 379 | .offset = 0, |
| 380 | 380 | }); |
| 381 | | } else { |
| 382 | | self.segments.items[self.code_section_index.?].size += atom.size; |
| 383 | 381 | } |
| 384 | 382 | |
| 385 | 383 | break :result self.code_section_index.?; |
| ... | ... | @@ -390,11 +388,11 @@ fn createDefinedSymbol(self: *Wasm, decl: *Module.Decl, symbol_index: u32, atom: |
| 390 | 388 | self.segments.items[gop.value_ptr.*].size += atom.size; |
| 391 | 389 | break :blk gop.value_ptr.*; |
| 392 | 390 | } else blk: { |
| 393 | | const index = @intCast(u32, self.segments.items.len) - @boolToInt(self.code_section_index != null); |
| 391 | const index = @intCast(u32, self.segments.items.len); |
| 394 | 392 | try self.segments.append(self.base.allocator, .{ |
| 395 | 393 | .alignment = atom.alignment, |
| 396 | | .size = atom.size, |
| 397 | | .offset = atom.offset, |
| 394 | .size = 0, |
| 395 | .offset = 0, |
| 398 | 396 | }); |
| 399 | 397 | gop.value_ptr.* = index; |
| 400 | 398 | break :blk index; |
| ... | ... | @@ -413,20 +411,153 @@ fn createDefinedSymbol(self: *Wasm, decl: *Module.Decl, symbol_index: u32, atom: |
| 413 | 411 | symbol.tag = .data; |
| 414 | 412 | symbol.index = info_index; |
| 415 | 413 | atom.alignment = decl.ty.abiAlignment(self.base.options.target); |
| 414 | |
| 416 | 415 | break :result atom_index; |
| 417 | 416 | }, |
| 418 | 417 | }; |
| 419 | 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 | |
| 420 | 427 | if (self.atoms.getPtr(final_index)) |last| { |
| 421 | 428 | last.*.next = atom; |
| 422 | 429 | atom.prev = last.*; |
| 423 | | atom.offset = last.*.offset + last.*.size; |
| 424 | 430 | last.* = atom; |
| 425 | 431 | } else { |
| 426 | 432 | try self.atoms.putNoClobber(self.base.allocator, final_index, atom); |
| 427 | 433 | } |
| 428 | 434 | } |
| 429 | 435 | |
| 436 | fn allocateAtoms(self: *Wasm) !void { |
| 437 | var it = self.atoms.iterator(); |
| 438 | while (it.next()) |entry| { |
| 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 | }, |
| 467 | else => unreachable, |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /// Sets up the memory section of the wasm module, as well as the stack. |
| 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; |
| 491 | } |
| 492 | |
| 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; |
| 499 | |
| 500 | if (self.base.options.initial_memory) |initial_memory| { |
| 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 | } |
| 515 | |
| 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}); |
| 520 | |
| 521 | if (self.base.options.max_memory) |max_memory| { |
| 522 | if (!std.mem.isAlignedGeneric(u64, max_memory, page_size)) { |
| 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; |
| 559 | } |
| 560 | |
| 430 | 561 | pub fn flush(self: *Wasm, comp: *Compilation) !void { |
| 431 | 562 | if (build_options.have_llvm and self.base.options.use_lld) { |
| 432 | 563 | return self.linkWithLLD(comp); |
| ... | ... | @@ -440,22 +571,21 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 440 | 571 | const tracy = trace(@src()); |
| 441 | 572 | defer tracy.end(); |
| 442 | 573 | |
| 443 | | const file = self.base.file.?; |
| 444 | | const header_size = 5 + 1; |
| 445 | | // The size of the emulated stack |
| 446 | | const stack_size = @intCast(u32, self.base.options.stack_size_override orelse std.wasm.page_size); |
| 447 | | |
| 448 | | var data_size: u32 = 0; |
| 449 | | for (self.segments.items) |segment, index| { |
| 450 | | // skip 'code' segments as they do not count towards data section size |
| 451 | | if (self.code_section_index) |code_index| { |
| 452 | | if (index == code_index) continue; |
| 453 | | } |
| 454 | | data_size += segment.size; |
| 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.*); |
| 455 | 582 | } |
| 456 | 583 | |
| 457 | | // set the stack size on the global |
| 458 | | self.globals.items[0].init.i32_const = @bitCast(i32, data_size + stack_size); |
| 584 | try self.setupMemory(); |
| 585 | try self.allocateAtoms(); |
| 586 | |
| 587 | const file = self.base.file.?; |
| 588 | const header_size = 5 + 1; |
| 459 | 589 | |
| 460 | 590 | // No need to rewrite the magic/version header |
| 461 | 591 | try file.setEndPos(@sizeOf(@TypeOf(wasm.magic ++ wasm.version))); |
| ... | ... | @@ -484,42 +614,34 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 484 | 614 | } |
| 485 | 615 | |
| 486 | 616 | // Import section |
| 487 | | if (self.import_symbols.items.len > 0) { |
| 617 | const import_mem = self.base.options.import_memory; |
| 618 | if (self.imports.count() != 0 or import_mem) { |
| 488 | 619 | const header_offset = try reserveVecSectionHeader(file); |
| 489 | 620 | const writer = file.writer(); |
| 490 | | for (self.import_symbols.items) |symbol_index| { |
| 491 | | const import_symbol = self.symbols.items[symbol_index]; |
| 621 | |
| 622 | var it = self.imports.iterator(); |
| 623 | while (it.next()) |entry| { |
| 624 | const import_symbol = self.symbols.items[entry.key_ptr.*]; |
| 492 | 625 | std.debug.assert(import_symbol.isUndefined()); |
| 493 | | try leb.writeULEB128(writer, @intCast(u32, self.host_name.len)); |
| 494 | | try writer.writeAll(self.host_name); |
| 495 | | |
| 496 | | const name = std.mem.span(import_symbol.name); |
| 497 | | try leb.writeULEB128(writer, @intCast(u32, name.len)); |
| 498 | | try writer.writeAll(name); |
| 499 | | |
| 500 | | try writer.writeByte(wasm.externalKind(import_symbol.tag.externalType())); |
| 501 | | const import = self.findImport(import_symbol.index, import_symbol.tag.externalType()).?; |
| 502 | | switch (import.kind) { |
| 503 | | .function => |type_index| try leb.writeULEB128(writer, type_index), |
| 504 | | .global => |global_type| { |
| 505 | | try leb.writeULEB128(writer, wasm.valtype(global_type.valtype)); |
| 506 | | try writer.writeByte(@boolToInt(global_type.mutable)); |
| 507 | | }, |
| 508 | | .table => |table| { |
| 509 | | try leb.writeULEB128(writer, wasm.reftype(table.reftype)); |
| 510 | | try emitLimits(writer, table.limits); |
| 511 | | }, |
| 512 | | .memory => |limits| { |
| 513 | | try emitLimits(writer, limits); |
| 514 | | }, |
| 515 | | } |
| 626 | const import = entry.value_ptr.*; |
| 627 | try emitImport(writer, import); |
| 516 | 628 | } |
| 629 | |
| 630 | if (import_mem) { |
| 631 | const mem_imp: wasm.Import = .{ |
| 632 | .module_name = self.host_name, |
| 633 | .name = "memory", |
| 634 | .kind = .{ .memory = self.memories.limits }, |
| 635 | }; |
| 636 | try emitImport(writer, mem_imp); |
| 637 | } |
| 638 | |
| 517 | 639 | try writeVecSectionHeader( |
| 518 | 640 | file, |
| 519 | 641 | header_offset, |
| 520 | 642 | .import, |
| 521 | 643 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 522 | | @intCast(u32, self.imports.items.len), |
| 644 | @intCast(u32, self.imports.count() + @boolToInt(import_mem)), |
| 523 | 645 | ); |
| 524 | 646 | } |
| 525 | 647 | |
| ... | ... | @@ -541,21 +663,11 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 541 | 663 | } |
| 542 | 664 | |
| 543 | 665 | // Memory section |
| 544 | | { |
| 666 | if (!self.base.options.import_memory) { |
| 545 | 667 | const header_offset = try reserveVecSectionHeader(file); |
| 546 | 668 | const writer = file.writer(); |
| 547 | 669 | |
| 548 | | try leb.writeULEB128(writer, @as(u32, 0)); |
| 549 | | // Calculate the amount of memory pages are required and write them. |
| 550 | | // Wasm uses 64kB page sizes. Round up to ensure the data segments fit into the memory |
| 551 | | try leb.writeULEB128( |
| 552 | | writer, |
| 553 | | try std.math.divCeil( |
| 554 | | u32, |
| 555 | | data_size + stack_size, |
| 556 | | std.wasm.page_size, |
| 557 | | ), |
| 558 | | ); |
| 670 | try emitLimits(writer, self.memories.limits); |
| 559 | 671 | try writeVecSectionHeader( |
| 560 | 672 | file, |
| 561 | 673 | header_offset, |
| ... | ... | @@ -590,7 +702,6 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 590 | 702 | const header_offset = try reserveVecSectionHeader(file); |
| 591 | 703 | const writer = file.writer(); |
| 592 | 704 | var count: u32 = 0; |
| 593 | | var func_index: u32 = self.imported_functions_count; |
| 594 | 705 | for (module.decl_exports.values()) |exports| { |
| 595 | 706 | for (exports) |exprt| { |
| 596 | 707 | // Export name length + name |
| ... | ... | @@ -599,11 +710,13 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 599 | 710 | |
| 600 | 711 | switch (exprt.exported_decl.ty.zigTypeTag()) { |
| 601 | 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); |
| 602 | 716 | // Type of the export |
| 603 | 717 | try writer.writeByte(wasm.externalKind(.function)); |
| 604 | 718 | // Exported function index |
| 605 | | try leb.writeULEB128(writer, func_index); |
| 606 | | func_index += 1; |
| 719 | try leb.writeULEB128(writer, target_symbol.index); |
| 607 | 720 | }, |
| 608 | 721 | else => return error.TODOImplementNonFnDeclsForWasm, |
| 609 | 722 | } |
| ... | ... | @@ -613,7 +726,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 613 | 726 | } |
| 614 | 727 | |
| 615 | 728 | // export memory if size is not 0 |
| 616 | | if (data_size != 0) { |
| 729 | if (!self.base.options.import_memory) { |
| 617 | 730 | try leb.writeULEB128(writer, @intCast(u32, "memory".len)); |
| 618 | 731 | try writer.writeAll("memory"); |
| 619 | 732 | try writer.writeByte(wasm.externalKind(.memory)); |
| ... | ... | @@ -639,7 +752,6 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 639 | 752 | try atom.resolveRelocs(self); |
| 640 | 753 | try leb.writeULEB128(writer, atom.size); |
| 641 | 754 | try writer.writeAll(atom.code.items); |
| 642 | | |
| 643 | 755 | atom = atom.next orelse break; |
| 644 | 756 | } |
| 645 | 757 | try writeVecSectionHeader( |
| ... | ... | @@ -657,37 +769,47 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 657 | 769 | const writer = file.writer(); |
| 658 | 770 | |
| 659 | 771 | var it = self.data_segments.iterator(); |
| 772 | var segment_count: u32 = 0; |
| 660 | 773 | while (it.next()) |entry| { |
| 661 | 774 | // do not output 'bss' section |
| 662 | 775 | if (std.mem.eql(u8, entry.key_ptr.*, ".bss")) continue; |
| 776 | segment_count += 1; |
| 663 | 777 | const atom_index = entry.value_ptr.*; |
| 664 | | var atom = self.atoms.getPtr(atom_index).?.*.getFirst(); |
| 778 | var atom: *Atom = self.atoms.getPtr(atom_index).?.*.getFirst(); |
| 665 | 779 | var segment = self.segments.items[atom_index]; |
| 666 | 780 | |
| 667 | 781 | // flag and index to memory section (currently, there can only be 1 memory section in wasm) |
| 668 | 782 | try leb.writeULEB128(writer, @as(u32, 0)); |
| 669 | | |
| 670 | 783 | // offset into data section |
| 671 | | try writer.writeByte(wasm.opcode(.i32_const)); |
| 672 | | try leb.writeILEB128(writer, @as(i32, 0)); |
| 673 | | try writer.writeByte(wasm.opcode(.end)); |
| 674 | | |
| 675 | | // offset table + data size |
| 784 | try emitInit(writer, .{ .i32_const = @bitCast(i32, segment.offset) }); |
| 676 | 785 | try leb.writeULEB128(writer, segment.size); |
| 677 | 786 | |
| 678 | 787 | // fill in the offset table and the data segments |
| 679 | 788 | var current_offset: u32 = 0; |
| 680 | 789 | while (true) { |
| 681 | 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 | } |
| 682 | 798 | std.debug.assert(current_offset == atom.offset); |
| 683 | 799 | std.debug.assert(atom.code.items.len == atom.size); |
| 684 | | |
| 685 | 800 | try writer.writeAll(atom.code.items); |
| 686 | 801 | |
| 687 | 802 | current_offset += atom.size; |
| 688 | 803 | if (atom.next) |next| { |
| 689 | 804 | atom = next; |
| 690 | | } else break; |
| 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 | } |
| 691 | 813 | } |
| 692 | 814 | } |
| 693 | 815 | |
| ... | ... | @@ -696,7 +818,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 696 | 818 | header_offset, |
| 697 | 819 | .data, |
| 698 | 820 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 699 | | @intCast(u32, 1), // only 1 data section |
| 821 | @intCast(u32, segment_count), |
| 700 | 822 | ); |
| 701 | 823 | } |
| 702 | 824 | } |
| ... | ... | @@ -735,6 +857,30 @@ fn emitInit(writer: anytype, init_expr: wasm.InitExpression) !void { |
| 735 | 857 | try writer.writeByte(wasm.opcode(.end)); |
| 736 | 858 | } |
| 737 | 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 | |
| 738 | 884 | fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { |
| 739 | 885 | const tracy = trace(@src()); |
| 740 | 886 | defer tracy.end(); |