| ... | ... | @@ -19,6 +19,7 @@ const mem = std.mem; |
| 19 | 19 | const leb = std.leb; |
| 20 | 20 | const log = std.log.scoped(.link); |
| 21 | 21 | const assert = std.debug.assert; |
| 22 | const ArrayList = std.ArrayList; |
| 22 | 23 | |
| 23 | 24 | /// Ordered list of data segments that will appear in the final binary. |
| 24 | 25 | /// When sorted, to-be-merged segments will be made adjacent. |
| ... | ... | @@ -27,9 +28,9 @@ data_segments: std.AutoArrayHashMapUnmanaged(Wasm.DataSegmentId, u32) = .empty, |
| 27 | 28 | /// Each time a `data_segment` offset equals zero it indicates a new group, and |
| 28 | 29 | /// the next element in this array will contain the total merged segment size. |
| 29 | 30 | /// Value is the virtual memory address of the end of the segment. |
| 30 | | data_segment_groups: std.ArrayListUnmanaged(DataSegmentGroup) = .empty, |
| 31 | data_segment_groups: ArrayList(DataSegmentGroup) = .empty, |
| 31 | 32 | |
| 32 | | binary_bytes: std.ArrayListUnmanaged(u8) = .empty, |
| 33 | binary_bytes: ArrayList(u8) = .empty, |
| 33 | 34 | missing_exports: std.AutoArrayHashMapUnmanaged(String, void) = .empty, |
| 34 | 35 | function_imports: std.AutoArrayHashMapUnmanaged(String, Wasm.FunctionImportId) = .empty, |
| 35 | 36 | global_imports: std.AutoArrayHashMapUnmanaged(String, Wasm.GlobalImportId) = .empty, |
| ... | ... | @@ -563,8 +564,6 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 563 | 564 | try binary_bytes.appendSlice(gpa, &std.wasm.magic ++ &std.wasm.version); |
| 564 | 565 | assert(binary_bytes.items.len == 8); |
| 565 | 566 | |
| 566 | | const binary_writer = binary_bytes.writer(gpa); |
| 567 | | |
| 568 | 567 | // Type section. |
| 569 | 568 | for (f.function_imports.values()) |id| { |
| 570 | 569 | try f.func_types.put(gpa, id.functionType(wasm), {}); |
| ... | ... | @@ -576,16 +575,16 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 576 | 575 | const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); |
| 577 | 576 | for (f.func_types.keys()) |func_type_index| { |
| 578 | 577 | const func_type = func_type_index.ptr(wasm); |
| 579 | | try leb.writeUleb128(binary_writer, std.wasm.function_type); |
| 578 | try appendLeb128(gpa, binary_bytes, std.wasm.function_type); |
| 580 | 579 | const params = func_type.params.slice(wasm); |
| 581 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(params.len))); |
| 580 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(params.len))); |
| 582 | 581 | for (params) |param_ty| { |
| 583 | | try leb.writeUleb128(binary_writer, @intFromEnum(param_ty)); |
| 582 | try appendLeb128(gpa, binary_bytes, @intFromEnum(param_ty)); |
| 584 | 583 | } |
| 585 | 584 | const returns = func_type.returns.slice(wasm); |
| 586 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(returns.len))); |
| 585 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(returns.len))); |
| 587 | 586 | for (returns) |ret_ty| { |
| 588 | | try leb.writeUleb128(binary_writer, @intFromEnum(ret_ty)); |
| 587 | try appendLeb128(gpa, binary_bytes, @intFromEnum(ret_ty)); |
| 589 | 588 | } |
| 590 | 589 | } |
| 591 | 590 | replaceVecSectionHeader(binary_bytes, header_offset, .type, @intCast(f.func_types.entries.len)); |
| ... | ... | @@ -605,31 +604,31 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 605 | 604 | |
| 606 | 605 | for (f.function_imports.values()) |id| { |
| 607 | 606 | const module_name = id.moduleName(wasm).slice(wasm).?; |
| 608 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(module_name.len))); |
| 609 | | try binary_writer.writeAll(module_name); |
| 607 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(module_name.len))); |
| 608 | try binary_bytes.appendSlice(gpa, module_name); |
| 610 | 609 | |
| 611 | 610 | const name = id.importName(wasm).slice(wasm); |
| 612 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); |
| 613 | | try binary_writer.writeAll(name); |
| 611 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 612 | try binary_bytes.appendSlice(gpa, name); |
| 614 | 613 | |
| 615 | | try binary_writer.writeByte(@intFromEnum(std.wasm.ExternalKind.function)); |
| 614 | try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.function)); |
| 616 | 615 | const type_index: FuncTypeIndex = .fromTypeIndex(id.functionType(wasm), f); |
| 617 | | try leb.writeUleb128(binary_writer, @intFromEnum(type_index)); |
| 616 | try appendLeb128(gpa, binary_bytes, @intFromEnum(type_index)); |
| 618 | 617 | } |
| 619 | 618 | total_imports += f.function_imports.entries.len; |
| 620 | 619 | |
| 621 | 620 | for (wasm.table_imports.values()) |id| { |
| 622 | 621 | const table_import = id.value(wasm); |
| 623 | 622 | const module_name = table_import.module_name.slice(wasm); |
| 624 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(module_name.len))); |
| 625 | | try binary_writer.writeAll(module_name); |
| 623 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(module_name.len))); |
| 624 | try binary_bytes.appendSlice(gpa, module_name); |
| 626 | 625 | |
| 627 | 626 | const name = table_import.name.slice(wasm); |
| 628 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); |
| 629 | | try binary_writer.writeAll(name); |
| 627 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 628 | try binary_bytes.appendSlice(gpa, name); |
| 630 | 629 | |
| 631 | | try binary_writer.writeByte(@intFromEnum(std.wasm.ExternalKind.table)); |
| 632 | | try leb.writeUleb128(binary_writer, @intFromEnum(@as(std.wasm.RefType, table_import.flags.ref_type.to()))); |
| 630 | try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.table)); |
| 631 | try appendLeb128(gpa, binary_bytes, @intFromEnum(@as(std.wasm.RefType, table_import.flags.ref_type.to()))); |
| 633 | 632 | try emitLimits(gpa, binary_bytes, table_import.limits()); |
| 634 | 633 | } |
| 635 | 634 | total_imports += wasm.table_imports.entries.len; |
| ... | ... | @@ -650,17 +649,17 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 650 | 649 | |
| 651 | 650 | for (f.global_imports.values()) |id| { |
| 652 | 651 | const module_name = id.moduleName(wasm).slice(wasm).?; |
| 653 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(module_name.len))); |
| 654 | | try binary_writer.writeAll(module_name); |
| 652 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(module_name.len))); |
| 653 | try binary_bytes.appendSlice(gpa, module_name); |
| 655 | 654 | |
| 656 | 655 | const name = id.importName(wasm).slice(wasm); |
| 657 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); |
| 658 | | try binary_writer.writeAll(name); |
| 656 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 657 | try binary_bytes.appendSlice(gpa, name); |
| 659 | 658 | |
| 660 | | try binary_writer.writeByte(@intFromEnum(std.wasm.ExternalKind.global)); |
| 659 | try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.global)); |
| 661 | 660 | const global_type = id.globalType(wasm); |
| 662 | | try leb.writeUleb128(binary_writer, @intFromEnum(@as(std.wasm.Valtype, global_type.valtype))); |
| 663 | | try binary_writer.writeByte(@intFromBool(global_type.mutable)); |
| 661 | try appendLeb128(gpa, binary_bytes, @intFromEnum(@as(std.wasm.Valtype, global_type.valtype))); |
| 662 | try binary_bytes.append(gpa, @intFromBool(global_type.mutable)); |
| 664 | 663 | } |
| 665 | 664 | total_imports += f.global_imports.entries.len; |
| 666 | 665 | |
| ... | ... | @@ -677,7 +676,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 677 | 676 | const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); |
| 678 | 677 | for (wasm.functions.keys()) |function| { |
| 679 | 678 | const index: FuncTypeIndex = .fromTypeIndex(function.typeIndex(wasm), f); |
| 680 | | try leb.writeUleb128(binary_writer, @intFromEnum(index)); |
| 679 | try appendLeb128(gpa, binary_bytes, @intFromEnum(index)); |
| 681 | 680 | } |
| 682 | 681 | |
| 683 | 682 | replaceVecSectionHeader(binary_bytes, header_offset, .function, @intCast(wasm.functions.count())); |
| ... | ... | @@ -689,7 +688,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 689 | 688 | const header_offset = try reserveVecSectionHeader(gpa, binary_bytes); |
| 690 | 689 | |
| 691 | 690 | for (wasm.tables.keys()) |table| { |
| 692 | | try leb.writeUleb128(binary_writer, @intFromEnum(@as(std.wasm.RefType, table.refType(wasm)))); |
| 691 | try appendLeb128(gpa, binary_bytes, @intFromEnum(@as(std.wasm.RefType, table.refType(wasm)))); |
| 693 | 692 | try emitLimits(gpa, binary_bytes, table.limits(wasm)); |
| 694 | 693 | } |
| 695 | 694 | |
| ... | ... | @@ -743,39 +742,39 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 743 | 742 | |
| 744 | 743 | for (wasm.function_exports.keys(), wasm.function_exports.values()) |exp_name, function_index| { |
| 745 | 744 | const name = exp_name.slice(wasm); |
| 746 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); |
| 745 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 747 | 746 | try binary_bytes.appendSlice(gpa, name); |
| 748 | 747 | try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.function)); |
| 749 | 748 | const func_index = Wasm.OutputFunctionIndex.fromFunctionIndex(wasm, function_index); |
| 750 | | try leb.writeUleb128(binary_writer, @intFromEnum(func_index)); |
| 749 | try appendLeb128(gpa, binary_bytes, @intFromEnum(func_index)); |
| 751 | 750 | } |
| 752 | 751 | exports_len += wasm.function_exports.entries.len; |
| 753 | 752 | |
| 754 | 753 | if (wasm.export_table and f.indirect_function_table.entries.len > 0) { |
| 755 | 754 | const name = "__indirect_function_table"; |
| 756 | 755 | const index: u32 = @intCast(wasm.tables.getIndex(.__indirect_function_table).?); |
| 757 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); |
| 756 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 758 | 757 | try binary_bytes.appendSlice(gpa, name); |
| 759 | 758 | try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.table)); |
| 760 | | try leb.writeUleb128(binary_writer, index); |
| 759 | try appendLeb128(gpa, binary_bytes, index); |
| 761 | 760 | exports_len += 1; |
| 762 | 761 | } |
| 763 | 762 | |
| 764 | 763 | if (export_memory) { |
| 765 | 764 | const name = "memory"; |
| 766 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); |
| 765 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 767 | 766 | try binary_bytes.appendSlice(gpa, name); |
| 768 | 767 | try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.memory)); |
| 769 | | try leb.writeUleb128(binary_writer, @as(u32, 0)); |
| 768 | try appendLeb128(gpa, binary_bytes, @as(u32, 0)); |
| 770 | 769 | exports_len += 1; |
| 771 | 770 | } |
| 772 | 771 | |
| 773 | 772 | for (wasm.global_exports.items) |exp| { |
| 774 | 773 | const name = exp.name.slice(wasm); |
| 775 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len))); |
| 774 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 776 | 775 | try binary_bytes.appendSlice(gpa, name); |
| 777 | 776 | try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.global)); |
| 778 | | try leb.writeUleb128(binary_writer, @intFromEnum(exp.global_index)); |
| 777 | try appendLeb128(gpa, binary_bytes, @intFromEnum(exp.global_index)); |
| 779 | 778 | } |
| 780 | 779 | exports_len += wasm.global_exports.items.len; |
| 781 | 780 | |
| ... | ... | @@ -802,18 +801,22 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 802 | 801 | const table_index: u32 = @intCast(wasm.tables.getIndex(.__indirect_function_table).?); |
| 803 | 802 | // passive with implicit 0-index table or set table index manually |
| 804 | 803 | const flags: u32 = if (table_index == 0) 0x0 else 0x02; |
| 805 | | try leb.writeUleb128(binary_writer, flags); |
| 804 | try appendLeb128(gpa, binary_bytes, flags); |
| 806 | 805 | if (flags == 0x02) { |
| 807 | | try leb.writeUleb128(binary_writer, table_index); |
| 806 | try appendLeb128(gpa, binary_bytes, table_index); |
| 808 | 807 | } |
| 809 | 808 | // We start at index 1, so unresolved function pointers are invalid |
| 810 | | try emitInit(binary_writer, .{ .i32_const = 1 }); |
| 809 | { |
| 810 | var aw: std.Io.Writer.Allocating = .fromArrayList(gpa, binary_bytes); |
| 811 | defer binary_bytes.* = aw.toArrayList(); |
| 812 | try emitInit(&aw.writer, .{ .i32_const = 1 }); |
| 813 | } |
| 811 | 814 | if (flags == 0x02) { |
| 812 | | try leb.writeUleb128(binary_writer, @as(u8, 0)); // represents funcref |
| 815 | try appendLeb128(gpa, binary_bytes, @as(u8, 0)); // represents funcref |
| 813 | 816 | } |
| 814 | | try leb.writeUleb128(binary_writer, @as(u32, @intCast(f.indirect_function_table.entries.len))); |
| 817 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(f.indirect_function_table.entries.len))); |
| 815 | 818 | for (f.indirect_function_table.keys()) |func_index| { |
| 816 | | try leb.writeUleb128(binary_writer, @intFromEnum(func_index)); |
| 819 | try appendLeb128(gpa, binary_bytes, @intFromEnum(func_index)); |
| 817 | 820 | } |
| 818 | 821 | |
| 819 | 822 | replaceVecSectionHeader(binary_bytes, header_offset, .element, 1); |
| ... | ... | @@ -851,7 +854,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 851 | 854 | .object_function => |i| { |
| 852 | 855 | const ptr = i.ptr(wasm); |
| 853 | 856 | const code = ptr.code.slice(wasm); |
| 854 | | try leb.writeUleb128(binary_writer, code.len); |
| 857 | try appendLeb128(gpa, binary_bytes, code.len); |
| 855 | 858 | const code_start = binary_bytes.items.len; |
| 856 | 859 | try binary_bytes.appendSlice(gpa, code); |
| 857 | 860 | if (!is_obj) applyRelocs(binary_bytes.items[code_start..], ptr.offset, ptr.relocations(wasm), wasm); |
| ... | ... | @@ -946,12 +949,14 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void { |
| 946 | 949 | const group_size = group_end_addr - group_start_addr; |
| 947 | 950 | log.debug("emit data section group, {d} bytes", .{group_size}); |
| 948 | 951 | const flags: Object.DataSegmentFlags = if (segment_id.isPassive(wasm)) .passive else .active; |
| 949 | | try leb.writeUleb128(binary_writer, @intFromEnum(flags)); |
| 952 | try appendLeb128(gpa, binary_bytes, @intFromEnum(flags)); |
| 950 | 953 | // Passive segments are initialized at runtime. |
| 951 | 954 | if (flags != .passive) { |
| 952 | | try emitInit(binary_writer, .{ .i32_const = @as(i32, @bitCast(group_start_addr)) }); |
| 955 | var aw: std.Io.Writer.Allocating = .fromArrayList(gpa, binary_bytes); |
| 956 | defer binary_bytes.* = aw.toArrayList(); |
| 957 | try emitInit(&aw.writer, .{ .i32_const = @as(i32, @bitCast(group_start_addr)) }); |
| 953 | 958 | } |
| 954 | | try leb.writeUleb128(binary_writer, group_size); |
| 959 | try appendLeb128(gpa, binary_bytes, group_size); |
| 955 | 960 | } |
| 956 | 961 | if (segment_id.isEmpty(wasm)) { |
| 957 | 962 | // It counted for virtual memory but it does not go into the binary. |
| ... | ... | @@ -1077,7 +1082,7 @@ const VirtualAddrs = struct { |
| 1077 | 1082 | fn emitNameSection( |
| 1078 | 1083 | wasm: *Wasm, |
| 1079 | 1084 | data_segment_groups: []const DataSegmentGroup, |
| 1080 | | binary_bytes: *std.ArrayListUnmanaged(u8), |
| 1085 | binary_bytes: *ArrayList(u8), |
| 1081 | 1086 | ) !void { |
| 1082 | 1087 | const f = &wasm.flush_buffer; |
| 1083 | 1088 | const comp = wasm.base.comp; |
| ... | ... | @@ -1087,7 +1092,7 @@ fn emitNameSection( |
| 1087 | 1092 | defer writeCustomSectionHeader(binary_bytes, header_offset); |
| 1088 | 1093 | |
| 1089 | 1094 | const name_name = "name"; |
| 1090 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, name_name.len)); |
| 1095 | try appendLeb128(gpa, binary_bytes, @as(u32, name_name.len)); |
| 1091 | 1096 | try binary_bytes.appendSlice(gpa, name_name); |
| 1092 | 1097 | |
| 1093 | 1098 | { |
| ... | ... | @@ -1095,18 +1100,18 @@ fn emitNameSection( |
| 1095 | 1100 | defer replaceHeader(binary_bytes, sub_offset, @intFromEnum(std.wasm.NameSubsection.function)); |
| 1096 | 1101 | |
| 1097 | 1102 | const total_functions: u32 = @intCast(f.function_imports.entries.len + wasm.functions.entries.len); |
| 1098 | | try leb.writeUleb128(binary_bytes.writer(gpa), total_functions); |
| 1103 | try appendLeb128(gpa, binary_bytes, total_functions); |
| 1099 | 1104 | |
| 1100 | 1105 | for (f.function_imports.keys(), 0..) |name_index, function_index| { |
| 1101 | 1106 | const name = name_index.slice(wasm); |
| 1102 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(function_index))); |
| 1103 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); |
| 1107 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(function_index))); |
| 1108 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 1104 | 1109 | try binary_bytes.appendSlice(gpa, name); |
| 1105 | 1110 | } |
| 1106 | 1111 | for (wasm.functions.keys(), f.function_imports.entries.len..) |resolution, function_index| { |
| 1107 | 1112 | const name = resolution.name(wasm).?; |
| 1108 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(function_index))); |
| 1109 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); |
| 1113 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(function_index))); |
| 1114 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 1110 | 1115 | try binary_bytes.appendSlice(gpa, name); |
| 1111 | 1116 | } |
| 1112 | 1117 | } |
| ... | ... | @@ -1116,18 +1121,18 @@ fn emitNameSection( |
| 1116 | 1121 | defer replaceHeader(binary_bytes, sub_offset, @intFromEnum(std.wasm.NameSubsection.global)); |
| 1117 | 1122 | |
| 1118 | 1123 | const total_globals: u32 = @intCast(f.global_imports.entries.len + wasm.globals.entries.len); |
| 1119 | | try leb.writeUleb128(binary_bytes.writer(gpa), total_globals); |
| 1124 | try appendLeb128(gpa, binary_bytes, total_globals); |
| 1120 | 1125 | |
| 1121 | 1126 | for (f.global_imports.keys(), 0..) |name_index, global_index| { |
| 1122 | 1127 | const name = name_index.slice(wasm); |
| 1123 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(global_index))); |
| 1124 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); |
| 1128 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(global_index))); |
| 1129 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 1125 | 1130 | try binary_bytes.appendSlice(gpa, name); |
| 1126 | 1131 | } |
| 1127 | 1132 | for (wasm.globals.keys(), f.global_imports.entries.len..) |resolution, global_index| { |
| 1128 | 1133 | const name = resolution.name(wasm).?; |
| 1129 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(global_index))); |
| 1130 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); |
| 1134 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(global_index))); |
| 1135 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 1131 | 1136 | try binary_bytes.appendSlice(gpa, name); |
| 1132 | 1137 | } |
| 1133 | 1138 | } |
| ... | ... | @@ -1137,12 +1142,12 @@ fn emitNameSection( |
| 1137 | 1142 | defer replaceHeader(binary_bytes, sub_offset, @intFromEnum(std.wasm.NameSubsection.data_segment)); |
| 1138 | 1143 | |
| 1139 | 1144 | const total_data_segments: u32 = @intCast(data_segment_groups.len); |
| 1140 | | try leb.writeUleb128(binary_bytes.writer(gpa), total_data_segments); |
| 1145 | try appendLeb128(gpa, binary_bytes, total_data_segments); |
| 1141 | 1146 | |
| 1142 | 1147 | for (data_segment_groups, 0..) |group, i| { |
| 1143 | 1148 | const name, _ = splitSegmentName(group.first_segment.name(wasm)); |
| 1144 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(i))); |
| 1145 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); |
| 1149 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(i))); |
| 1150 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 1146 | 1151 | try binary_bytes.appendSlice(gpa, name); |
| 1147 | 1152 | } |
| 1148 | 1153 | } |
| ... | ... | @@ -1150,7 +1155,7 @@ fn emitNameSection( |
| 1150 | 1155 | |
| 1151 | 1156 | fn emitFeaturesSection( |
| 1152 | 1157 | gpa: Allocator, |
| 1153 | | binary_bytes: *std.ArrayListUnmanaged(u8), |
| 1158 | binary_bytes: *ArrayList(u8), |
| 1154 | 1159 | target: *const std.Target, |
| 1155 | 1160 | ) Allocator.Error!void { |
| 1156 | 1161 | const feature_count = target.cpu.features.count(); |
| ... | ... | @@ -1159,87 +1164,84 @@ fn emitFeaturesSection( |
| 1159 | 1164 | const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes); |
| 1160 | 1165 | defer writeCustomSectionHeader(binary_bytes, header_offset); |
| 1161 | 1166 | |
| 1162 | | const writer = binary_bytes.writer(gpa); |
| 1163 | 1167 | const target_features = "target_features"; |
| 1164 | | try leb.writeUleb128(writer, @as(u32, @intCast(target_features.len))); |
| 1165 | | try writer.writeAll(target_features); |
| 1168 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(target_features.len))); |
| 1169 | try binary_bytes.appendSlice(gpa, target_features); |
| 1166 | 1170 | |
| 1167 | | try leb.writeUleb128(writer, @as(u32, @intCast(feature_count))); |
| 1171 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(feature_count))); |
| 1168 | 1172 | |
| 1169 | 1173 | var safety_count = feature_count; |
| 1170 | 1174 | for (target.cpu.arch.allFeaturesList(), 0..) |*feature, i| { |
| 1171 | 1175 | if (!target.cpu.has(.wasm, @as(std.Target.wasm.Feature, @enumFromInt(i)))) continue; |
| 1172 | 1176 | safety_count -= 1; |
| 1173 | 1177 | |
| 1174 | | try leb.writeUleb128(writer, @as(u32, '+')); |
| 1178 | try appendLeb128(gpa, binary_bytes, @as(u32, '+')); |
| 1175 | 1179 | // Depends on llvm_name for the hyphenated version that matches wasm tooling conventions. |
| 1176 | 1180 | const name = feature.llvm_name.?; |
| 1177 | | try leb.writeUleb128(writer, @as(u32, @intCast(name.len))); |
| 1178 | | try writer.writeAll(name); |
| 1181 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 1182 | try binary_bytes.appendSlice(gpa, name); |
| 1179 | 1183 | } |
| 1180 | 1184 | assert(safety_count == 0); |
| 1181 | 1185 | } |
| 1182 | 1186 | |
| 1183 | | fn emitBuildIdSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8), build_id: []const u8) !void { |
| 1187 | fn emitBuildIdSection(gpa: Allocator, binary_bytes: *ArrayList(u8), build_id: []const u8) !void { |
| 1184 | 1188 | const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes); |
| 1185 | 1189 | defer writeCustomSectionHeader(binary_bytes, header_offset); |
| 1186 | 1190 | |
| 1187 | | const writer = binary_bytes.writer(gpa); |
| 1188 | 1191 | const hdr_build_id = "build_id"; |
| 1189 | | try leb.writeUleb128(writer, @as(u32, @intCast(hdr_build_id.len))); |
| 1190 | | try writer.writeAll(hdr_build_id); |
| 1192 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(hdr_build_id.len))); |
| 1193 | try binary_bytes.appendSlice(gpa, hdr_build_id); |
| 1191 | 1194 | |
| 1192 | | try leb.writeUleb128(writer, @as(u32, 1)); |
| 1193 | | try leb.writeUleb128(writer, @as(u32, @intCast(build_id.len))); |
| 1194 | | try writer.writeAll(build_id); |
| 1195 | try appendLeb128(gpa, binary_bytes, @as(u32, 1)); |
| 1196 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(build_id.len))); |
| 1197 | try binary_bytes.appendSlice(gpa, build_id); |
| 1195 | 1198 | } |
| 1196 | 1199 | |
| 1197 | | fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)) !void { |
| 1200 | fn emitProducerSection(gpa: Allocator, binary_bytes: *ArrayList(u8)) !void { |
| 1198 | 1201 | const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes); |
| 1199 | 1202 | defer writeCustomSectionHeader(binary_bytes, header_offset); |
| 1200 | 1203 | |
| 1201 | | const writer = binary_bytes.writer(gpa); |
| 1202 | 1204 | const producers = "producers"; |
| 1203 | | try leb.writeUleb128(writer, @as(u32, @intCast(producers.len))); |
| 1204 | | try writer.writeAll(producers); |
| 1205 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(producers.len))); |
| 1206 | try binary_bytes.appendSlice(gpa, producers); |
| 1205 | 1207 | |
| 1206 | | try leb.writeUleb128(writer, @as(u32, 2)); // 2 fields: Language + processed-by |
| 1208 | try appendLeb128(gpa, binary_bytes, @as(u32, 2)); // 2 fields: Language + processed-by |
| 1207 | 1209 | |
| 1208 | 1210 | // language field |
| 1209 | 1211 | { |
| 1210 | 1212 | const language = "language"; |
| 1211 | | try leb.writeUleb128(writer, @as(u32, @intCast(language.len))); |
| 1212 | | try writer.writeAll(language); |
| 1213 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(language.len))); |
| 1214 | try binary_bytes.appendSlice(gpa, language); |
| 1213 | 1215 | |
| 1214 | 1216 | // field_value_count (TODO: Parse object files for producer sections to detect their language) |
| 1215 | | try leb.writeUleb128(writer, @as(u32, 1)); |
| 1217 | try appendLeb128(gpa, binary_bytes, @as(u32, 1)); |
| 1216 | 1218 | |
| 1217 | 1219 | // versioned name |
| 1218 | 1220 | { |
| 1219 | | try leb.writeUleb128(writer, @as(u32, 3)); // len of "Zig" |
| 1220 | | try writer.writeAll("Zig"); |
| 1221 | try appendLeb128(gpa, binary_bytes, @as(u32, 3)); // len of "Zig" |
| 1222 | try binary_bytes.appendSlice(gpa, "Zig"); |
| 1221 | 1223 | |
| 1222 | | try leb.writeUleb128(writer, @as(u32, @intCast(build_options.version.len))); |
| 1223 | | try writer.writeAll(build_options.version); |
| 1224 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(build_options.version.len))); |
| 1225 | try binary_bytes.appendSlice(gpa, build_options.version); |
| 1224 | 1226 | } |
| 1225 | 1227 | } |
| 1226 | 1228 | |
| 1227 | 1229 | // processed-by field |
| 1228 | 1230 | { |
| 1229 | 1231 | const processed_by = "processed-by"; |
| 1230 | | try leb.writeUleb128(writer, @as(u32, @intCast(processed_by.len))); |
| 1231 | | try writer.writeAll(processed_by); |
| 1232 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(processed_by.len))); |
| 1233 | try binary_bytes.appendSlice(gpa, processed_by); |
| 1232 | 1234 | |
| 1233 | 1235 | // field_value_count (TODO: Parse object files for producer sections to detect other used tools) |
| 1234 | | try leb.writeUleb128(writer, @as(u32, 1)); |
| 1236 | try appendLeb128(gpa, binary_bytes, @as(u32, 1)); |
| 1235 | 1237 | |
| 1236 | 1238 | // versioned name |
| 1237 | 1239 | { |
| 1238 | | try leb.writeUleb128(writer, @as(u32, 3)); // len of "Zig" |
| 1239 | | try writer.writeAll("Zig"); |
| 1240 | try appendLeb128(gpa, binary_bytes, @as(u32, 3)); // len of "Zig" |
| 1241 | try binary_bytes.appendSlice(gpa, "Zig"); |
| 1240 | 1242 | |
| 1241 | | try leb.writeUleb128(writer, @as(u32, @intCast(build_options.version.len))); |
| 1242 | | try writer.writeAll(build_options.version); |
| 1243 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(build_options.version.len))); |
| 1244 | try binary_bytes.appendSlice(gpa, build_options.version); |
| 1243 | 1245 | } |
| 1244 | 1246 | } |
| 1245 | 1247 | } |
| ... | ... | @@ -1280,99 +1282,97 @@ fn wantSegmentMerge( |
| 1280 | 1282 | const section_header_reserve_size = 1 + 5 + 5; |
| 1281 | 1283 | const section_header_size = 5 + 1; |
| 1282 | 1284 | |
| 1283 | | fn reserveVecSectionHeader(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 { |
| 1285 | fn reserveVecSectionHeader(gpa: Allocator, bytes: *ArrayList(u8)) Allocator.Error!u32 { |
| 1284 | 1286 | try bytes.appendNTimes(gpa, 0, section_header_reserve_size); |
| 1285 | 1287 | return @intCast(bytes.items.len - section_header_reserve_size); |
| 1286 | 1288 | } |
| 1287 | 1289 | |
| 1288 | 1290 | fn replaceVecSectionHeader( |
| 1289 | | bytes: *std.ArrayListUnmanaged(u8), |
| 1291 | bytes: *ArrayList(u8), |
| 1290 | 1292 | offset: u32, |
| 1291 | 1293 | section: std.wasm.Section, |
| 1292 | 1294 | n_items: u32, |
| 1293 | 1295 | ) void { |
| 1294 | 1296 | const size: u32 = @intCast(bytes.items.len - offset - section_header_reserve_size + uleb128size(n_items)); |
| 1295 | 1297 | var buf: [section_header_reserve_size]u8 = undefined; |
| 1296 | | var fbw = std.io.fixedBufferStream(&buf); |
| 1297 | | const w = fbw.writer(); |
| 1298 | var w: std.Io.Writer = .fixed(&buf); |
| 1298 | 1299 | w.writeByte(@intFromEnum(section)) catch unreachable; |
| 1299 | | leb.writeUleb128(w, size) catch unreachable; |
| 1300 | | leb.writeUleb128(w, n_items) catch unreachable; |
| 1301 | | bytes.replaceRangeAssumeCapacity(offset, section_header_reserve_size, fbw.getWritten()); |
| 1300 | w.writeUleb128(size) catch unreachable; |
| 1301 | w.writeUleb128(n_items) catch unreachable; |
| 1302 | bytes.replaceRangeAssumeCapacity(offset, section_header_reserve_size, w.buffered()); |
| 1302 | 1303 | } |
| 1303 | 1304 | |
| 1304 | | fn reserveCustomSectionHeader(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 { |
| 1305 | fn reserveCustomSectionHeader(gpa: Allocator, bytes: *ArrayList(u8)) Allocator.Error!u32 { |
| 1305 | 1306 | try bytes.appendNTimes(gpa, 0, section_header_size); |
| 1306 | 1307 | return @intCast(bytes.items.len - section_header_size); |
| 1307 | 1308 | } |
| 1308 | 1309 | |
| 1309 | | fn writeCustomSectionHeader(bytes: *std.ArrayListUnmanaged(u8), offset: u32) void { |
| 1310 | fn writeCustomSectionHeader(bytes: *ArrayList(u8), offset: u32) void { |
| 1310 | 1311 | return replaceHeader(bytes, offset, 0); // 0 = 'custom' section |
| 1311 | 1312 | } |
| 1312 | 1313 | |
| 1313 | | fn replaceHeader(bytes: *std.ArrayListUnmanaged(u8), offset: u32, tag: u8) void { |
| 1314 | fn replaceHeader(bytes: *ArrayList(u8), offset: u32, tag: u8) void { |
| 1314 | 1315 | const size: u32 = @intCast(bytes.items.len - offset - section_header_size); |
| 1315 | 1316 | var buf: [section_header_size]u8 = undefined; |
| 1316 | | var fbw = std.io.fixedBufferStream(&buf); |
| 1317 | | const w = fbw.writer(); |
| 1317 | var w: std.Io.Writer = .fixed(&buf); |
| 1318 | 1318 | w.writeByte(tag) catch unreachable; |
| 1319 | | leb.writeUleb128(w, size) catch unreachable; |
| 1320 | | bytes.replaceRangeAssumeCapacity(offset, section_header_size, fbw.getWritten()); |
| 1319 | w.writeUleb128(size) catch unreachable; |
| 1320 | bytes.replaceRangeAssumeCapacity(offset, section_header_size, w.buffered()); |
| 1321 | 1321 | } |
| 1322 | 1322 | |
| 1323 | 1323 | const max_size_encoding = 5; |
| 1324 | 1324 | |
| 1325 | | fn reserveSize(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 { |
| 1325 | fn reserveSize(gpa: Allocator, bytes: *ArrayList(u8)) Allocator.Error!u32 { |
| 1326 | 1326 | try bytes.appendNTimes(gpa, 0, max_size_encoding); |
| 1327 | 1327 | return @intCast(bytes.items.len - max_size_encoding); |
| 1328 | 1328 | } |
| 1329 | 1329 | |
| 1330 | | fn replaceSize(bytes: *std.ArrayListUnmanaged(u8), offset: u32) void { |
| 1330 | fn replaceSize(bytes: *ArrayList(u8), offset: u32) void { |
| 1331 | 1331 | const size: u32 = @intCast(bytes.items.len - offset - max_size_encoding); |
| 1332 | 1332 | var buf: [max_size_encoding]u8 = undefined; |
| 1333 | | var fbw = std.io.fixedBufferStream(&buf); |
| 1334 | | leb.writeUleb128(fbw.writer(), size) catch unreachable; |
| 1335 | | bytes.replaceRangeAssumeCapacity(offset, max_size_encoding, fbw.getWritten()); |
| 1333 | var w: std.Io.Writer = .fixed(&buf); |
| 1334 | w.writeUleb128(size) catch unreachable; |
| 1335 | bytes.replaceRangeAssumeCapacity(offset, max_size_encoding, w.buffered()); |
| 1336 | 1336 | } |
| 1337 | 1337 | |
| 1338 | 1338 | fn emitLimits( |
| 1339 | 1339 | gpa: Allocator, |
| 1340 | | binary_bytes: *std.ArrayListUnmanaged(u8), |
| 1340 | binary_bytes: *ArrayList(u8), |
| 1341 | 1341 | limits: std.wasm.Limits, |
| 1342 | 1342 | ) Allocator.Error!void { |
| 1343 | 1343 | try binary_bytes.append(gpa, @bitCast(limits.flags)); |
| 1344 | | try leb.writeUleb128(binary_bytes.writer(gpa), limits.min); |
| 1345 | | if (limits.flags.has_max) try leb.writeUleb128(binary_bytes.writer(gpa), limits.max); |
| 1344 | try appendLeb128(gpa, binary_bytes, limits.min); |
| 1345 | if (limits.flags.has_max) try appendLeb128(gpa, binary_bytes, limits.max); |
| 1346 | 1346 | } |
| 1347 | 1347 | |
| 1348 | 1348 | fn emitMemoryImport( |
| 1349 | 1349 | wasm: *Wasm, |
| 1350 | | binary_bytes: *std.ArrayListUnmanaged(u8), |
| 1350 | binary_bytes: *ArrayList(u8), |
| 1351 | 1351 | name_index: String, |
| 1352 | 1352 | memory_import: *const Wasm.MemoryImport, |
| 1353 | 1353 | ) Allocator.Error!void { |
| 1354 | 1354 | const gpa = wasm.base.comp.gpa; |
| 1355 | 1355 | const module_name = memory_import.module_name.slice(wasm); |
| 1356 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(module_name.len))); |
| 1356 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(module_name.len))); |
| 1357 | 1357 | try binary_bytes.appendSlice(gpa, module_name); |
| 1358 | 1358 | |
| 1359 | 1359 | const name = name_index.slice(wasm); |
| 1360 | | try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len))); |
| 1360 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(name.len))); |
| 1361 | 1361 | try binary_bytes.appendSlice(gpa, name); |
| 1362 | 1362 | |
| 1363 | 1363 | try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.memory)); |
| 1364 | 1364 | try emitLimits(gpa, binary_bytes, memory_import.limits()); |
| 1365 | 1365 | } |
| 1366 | 1366 | |
| 1367 | | pub fn emitInit(writer: anytype, init_expr: std.wasm.InitExpression) !void { |
| 1367 | fn emitInit(writer: *std.Io.Writer, init_expr: std.wasm.InitExpression) !void { |
| 1368 | 1368 | switch (init_expr) { |
| 1369 | 1369 | .i32_const => |val| { |
| 1370 | 1370 | try writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)); |
| 1371 | | try leb.writeIleb128(writer, val); |
| 1371 | try writer.writeSleb128(val); |
| 1372 | 1372 | }, |
| 1373 | 1373 | .i64_const => |val| { |
| 1374 | 1374 | try writer.writeByte(@intFromEnum(std.wasm.Opcode.i64_const)); |
| 1375 | | try leb.writeIleb128(writer, val); |
| 1375 | try writer.writeSleb128(val); |
| 1376 | 1376 | }, |
| 1377 | 1377 | .f32_const => |val| { |
| 1378 | 1378 | try writer.writeByte(@intFromEnum(std.wasm.Opcode.f32_const)); |
| ... | ... | @@ -1384,13 +1384,13 @@ pub fn emitInit(writer: anytype, init_expr: std.wasm.InitExpression) !void { |
| 1384 | 1384 | }, |
| 1385 | 1385 | .global_get => |val| { |
| 1386 | 1386 | try writer.writeByte(@intFromEnum(std.wasm.Opcode.global_get)); |
| 1387 | | try leb.writeUleb128(writer, val); |
| 1387 | try writer.writeUleb128(val); |
| 1388 | 1388 | }, |
| 1389 | 1389 | } |
| 1390 | 1390 | try writer.writeByte(@intFromEnum(std.wasm.Opcode.end)); |
| 1391 | 1391 | } |
| 1392 | 1392 | |
| 1393 | | pub fn emitExpr(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8), expr: Wasm.Expr) Allocator.Error!void { |
| 1393 | pub fn emitExpr(wasm: *const Wasm, binary_bytes: *ArrayList(u8), expr: Wasm.Expr) Allocator.Error!void { |
| 1394 | 1394 | const gpa = wasm.base.comp.gpa; |
| 1395 | 1395 | const slice = expr.slice(wasm); |
| 1396 | 1396 | try binary_bytes.appendSlice(gpa, slice[0 .. slice.len + 1]); // +1 to include end opcode |
| ... | ... | @@ -1398,21 +1398,20 @@ pub fn emitExpr(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8), ex |
| 1398 | 1398 | |
| 1399 | 1399 | fn emitSegmentInfo(wasm: *Wasm, binary_bytes: *std.array_list.Managed(u8)) !void { |
| 1400 | 1400 | const gpa = wasm.base.comp.gpa; |
| 1401 | | const writer = binary_bytes.writer(gpa); |
| 1402 | | try leb.writeUleb128(writer, @intFromEnum(Wasm.SubsectionType.segment_info)); |
| 1401 | try appendLeb128(gpa, binary_bytes, @intFromEnum(Wasm.SubsectionType.segment_info)); |
| 1403 | 1402 | const segment_offset = binary_bytes.items.len; |
| 1404 | 1403 | |
| 1405 | | try leb.writeUleb128(writer, @as(u32, @intCast(wasm.segment_info.count()))); |
| 1404 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(wasm.segment_info.count()))); |
| 1406 | 1405 | for (wasm.segment_info.values()) |segment_info| { |
| 1407 | 1406 | log.debug("Emit segment: {s} align({d}) flags({b})", .{ |
| 1408 | 1407 | segment_info.name, |
| 1409 | 1408 | segment_info.alignment, |
| 1410 | 1409 | segment_info.flags, |
| 1411 | 1410 | }); |
| 1412 | | try leb.writeUleb128(writer, @as(u32, @intCast(segment_info.name.len))); |
| 1413 | | try writer.writeAll(segment_info.name); |
| 1414 | | try leb.writeUleb128(writer, segment_info.alignment.toLog2Units()); |
| 1415 | | try leb.writeUleb128(writer, segment_info.flags); |
| 1411 | try appendLeb128(gpa, binary_bytes, @as(u32, @intCast(segment_info.name.len))); |
| 1412 | try binary_bytes.appendSlice(gpa, segment_info.name); |
| 1413 | try appendLeb128(gpa, binary_bytes, segment_info.alignment.toLog2Units()); |
| 1414 | try appendLeb128(gpa, binary_bytes, segment_info.flags); |
| 1416 | 1415 | } |
| 1417 | 1416 | |
| 1418 | 1417 | var buf: [5]u8 = undefined; |
| ... | ... | @@ -1429,7 +1428,7 @@ fn uleb128size(x: u32) u32 { |
| 1429 | 1428 | |
| 1430 | 1429 | fn emitTagNameTable( |
| 1431 | 1430 | gpa: Allocator, |
| 1432 | | code: *std.ArrayListUnmanaged(u8), |
| 1431 | code: *ArrayList(u8), |
| 1433 | 1432 | tag_name_offs: []const u32, |
| 1434 | 1433 | tag_name_bytes: []const u8, |
| 1435 | 1434 | base: u32, |
| ... | ... | @@ -1604,7 +1603,7 @@ fn reloc_leb_type(code: []u8, index: FuncTypeIndex) void { |
| 1604 | 1603 | leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(index)); |
| 1605 | 1604 | } |
| 1606 | 1605 | |
| 1607 | | fn emitCallCtorsFunction(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!void { |
| 1606 | fn emitCallCtorsFunction(wasm: *const Wasm, binary_bytes: *ArrayList(u8)) Allocator.Error!void { |
| 1608 | 1607 | const gpa = wasm.base.comp.gpa; |
| 1609 | 1608 | |
| 1610 | 1609 | try binary_bytes.ensureUnusedCapacity(gpa, 5 + 1); |
| ... | ... | @@ -1631,7 +1630,7 @@ fn emitCallCtorsFunction(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanage |
| 1631 | 1630 | |
| 1632 | 1631 | fn emitInitMemoryFunction( |
| 1633 | 1632 | wasm: *const Wasm, |
| 1634 | | binary_bytes: *std.ArrayListUnmanaged(u8), |
| 1633 | binary_bytes: *ArrayList(u8), |
| 1635 | 1634 | virtual_addrs: *const VirtualAddrs, |
| 1636 | 1635 | ) Allocator.Error!void { |
| 1637 | 1636 | const comp = wasm.base.comp; |
| ... | ... | @@ -1734,7 +1733,7 @@ fn emitInitMemoryFunction( |
| 1734 | 1733 | // notify any waiters for segment initialization completion |
| 1735 | 1734 | appendReservedI32Const(binary_bytes, flag_address); |
| 1736 | 1735 | binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const)); |
| 1737 | | leb.writeIleb128(binary_bytes.fixedWriter(), @as(i32, -1)) catch unreachable; // number of waiters |
| 1736 | appendReservedLeb128(binary_bytes, @as(i32, -1)); // number of waiters |
| 1738 | 1737 | binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix)); |
| 1739 | 1738 | appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.memory_atomic_notify)); |
| 1740 | 1739 | appendReservedUleb32(binary_bytes, @as(u32, 2)); // alignment |
| ... | ... | @@ -1750,7 +1749,7 @@ fn emitInitMemoryFunction( |
| 1750 | 1749 | appendReservedI32Const(binary_bytes, flag_address); |
| 1751 | 1750 | appendReservedI32Const(binary_bytes, 1); // expected flag value |
| 1752 | 1751 | binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const)); |
| 1753 | | leb.writeIleb128(binary_bytes.fixedWriter(), @as(i64, -1)) catch unreachable; // timeout |
| 1752 | appendReservedLeb128(binary_bytes, @as(i64, -1)); // timeout |
| 1754 | 1753 | binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix)); |
| 1755 | 1754 | appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.memory_atomic_wait32)); |
| 1756 | 1755 | appendReservedUleb32(binary_bytes, @as(u32, 2)); // alignment |
| ... | ... | @@ -1779,7 +1778,7 @@ fn emitInitMemoryFunction( |
| 1779 | 1778 | binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); |
| 1780 | 1779 | } |
| 1781 | 1780 | |
| 1782 | | fn emitInitTlsFunction(wasm: *const Wasm, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!void { |
| 1781 | fn emitInitTlsFunction(wasm: *const Wasm, bytes: *ArrayList(u8)) Allocator.Error!void { |
| 1783 | 1782 | const comp = wasm.base.comp; |
| 1784 | 1783 | const gpa = comp.gpa; |
| 1785 | 1784 | |
| ... | ... | @@ -1840,14 +1839,14 @@ fn emitInitTlsFunction(wasm: *const Wasm, bytes: *std.ArrayListUnmanaged(u8)) Al |
| 1840 | 1839 | bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); |
| 1841 | 1840 | } |
| 1842 | 1841 | |
| 1843 | | fn emitStartSection(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8), i: Wasm.OutputFunctionIndex) !void { |
| 1842 | fn emitStartSection(gpa: Allocator, bytes: *ArrayList(u8), i: Wasm.OutputFunctionIndex) !void { |
| 1844 | 1843 | const header_offset = try reserveVecSectionHeader(gpa, bytes); |
| 1845 | 1844 | replaceVecSectionHeader(bytes, header_offset, .start, @intFromEnum(i)); |
| 1846 | 1845 | } |
| 1847 | 1846 | |
| 1848 | 1847 | fn emitTagNameFunction( |
| 1849 | 1848 | wasm: *Wasm, |
| 1850 | | code: *std.ArrayListUnmanaged(u8), |
| 1849 | code: *ArrayList(u8), |
| 1851 | 1850 | table_base_addr: u32, |
| 1852 | 1851 | table_index: u32, |
| 1853 | 1852 | enum_type_ip: InternPool.Index, |
| ... | ... | @@ -1959,22 +1958,34 @@ fn emitTagNameFunction( |
| 1959 | 1958 | } |
| 1960 | 1959 | |
| 1961 | 1960 | /// Writes an unsigned 32-bit integer as a LEB128-encoded 'i32.const' value. |
| 1962 | | fn appendReservedI32Const(bytes: *std.ArrayListUnmanaged(u8), val: u32) void { |
| 1961 | fn appendReservedI32Const(bytes: *ArrayList(u8), val: u32) void { |
| 1963 | 1962 | bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const)); |
| 1964 | | leb.writeIleb128(bytes.fixedWriter(), @as(i32, @bitCast(val))) catch unreachable; |
| 1963 | var w: std.Io.Writer = .fromArrayList(bytes); |
| 1964 | defer bytes.* = w.toArrayList(); |
| 1965 | return w.writeSleb128(val) catch |err| switch (err) { |
| 1966 | error.WriteFailed => unreachable, |
| 1967 | }; |
| 1965 | 1968 | } |
| 1966 | 1969 | |
| 1967 | 1970 | /// Writes an unsigned 64-bit integer as a LEB128-encoded 'i64.const' value. |
| 1968 | | fn appendReservedI64Const(bytes: *std.ArrayListUnmanaged(u8), val: u64) void { |
| 1971 | fn appendReservedI64Const(bytes: *ArrayList(u8), val: u64) void { |
| 1969 | 1972 | bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const)); |
| 1970 | | leb.writeIleb128(bytes.fixedWriter(), @as(i64, @bitCast(val))) catch unreachable; |
| 1973 | var w: std.Io.Writer = .fromArrayList(bytes); |
| 1974 | defer bytes.* = w.toArrayList(); |
| 1975 | return w.writeSleb128(val) catch |err| switch (err) { |
| 1976 | error.WriteFailed => unreachable, |
| 1977 | }; |
| 1971 | 1978 | } |
| 1972 | 1979 | |
| 1973 | | fn appendReservedUleb32(bytes: *std.ArrayListUnmanaged(u8), val: u32) void { |
| 1974 | | leb.writeUleb128(bytes.fixedWriter(), val) catch unreachable; |
| 1980 | fn appendReservedUleb32(bytes: *ArrayList(u8), val: u32) void { |
| 1981 | var w: std.Io.Writer = .fromArrayList(bytes); |
| 1982 | defer bytes.* = w.toArrayList(); |
| 1983 | return w.writeUleb128(val) catch |err| switch (err) { |
| 1984 | error.WriteFailed => unreachable, |
| 1985 | }; |
| 1975 | 1986 | } |
| 1976 | 1987 | |
| 1977 | | fn appendGlobal(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8), mutable: u8, val: u32) Allocator.Error!void { |
| 1988 | fn appendGlobal(gpa: Allocator, bytes: *ArrayList(u8), mutable: u8, val: u32) Allocator.Error!void { |
| 1978 | 1989 | try bytes.ensureUnusedCapacity(gpa, 9); |
| 1979 | 1990 | bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Valtype.i32)); |
| 1980 | 1991 | bytes.appendAssumeCapacity(mutable); |
| ... | ... | @@ -1982,3 +1993,19 @@ fn appendGlobal(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8), mutable: u8, |
| 1982 | 1993 | appendReservedUleb32(bytes, val); |
| 1983 | 1994 | bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); |
| 1984 | 1995 | } |
| 1996 | |
| 1997 | fn appendLeb128(gpa: Allocator, bytes: *ArrayList(u8), value: anytype) Allocator.Error!void { |
| 1998 | var aw: std.Io.Writer.Allocating = .fromArrayList(gpa, bytes); |
| 1999 | defer bytes.* = aw.toArrayList(); |
| 2000 | return aw.writer.writeLeb128(value) catch |err| switch (err) { |
| 2001 | error.WriteFailed => return error.OutOfMemory, |
| 2002 | }; |
| 2003 | } |
| 2004 | |
| 2005 | fn appendReservedLeb128(bytes: *ArrayList(u8), value: anytype) void { |
| 2006 | var w: std.Io.Writer = .fromArrayList(bytes); |
| 2007 | defer bytes.* = w.toArrayList(); |
| 2008 | return w.writeLeb128(value) catch |err| switch (err) { |
| 2009 | error.WriteFailed => unreachable, |
| 2010 | }; |
| 2011 | } |