| ... | ... | @@ -33,9 +33,18 @@ base: link.File, |
| 33 | 33 | |
| 34 | 34 | /// List of all function Decls to be written to the output file. The index of |
| 35 | 35 | /// each Decl in this list at the time of writing the binary is used as the |
| 36 | | /// function index. |
| 36 | /// function index. In the event where ext_funcs' size is not 0, the index of |
| 37 | /// each function is added on top of the ext_funcs' length. |
| 37 | 38 | /// TODO: can/should we access some data structure in Module directly? |
| 38 | 39 | funcs: std.ArrayListUnmanaged(*Module.Decl) = .{}, |
| 40 | /// List of all extern function Decls to be written to the `import` section of the |
| 41 | /// wasm binary. The positin in the list defines the function index |
| 42 | ext_funcs: std.ArrayListUnmanaged(*Module.Decl) = .{}, |
| 43 | /// When importing objects from the host environment, a name must be supplied. |
| 44 | /// LLVM uses "env" by default when none is given. This would be a good default for Zig |
| 45 | /// to support existing code. |
| 46 | /// TODO: Allow setting this through a flag? |
| 47 | host_name: []const u8 = "env", |
| 39 | 48 | |
| 40 | 49 | pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Options) !*Wasm { |
| 41 | 50 | assert(options.object_format == .wasm); |
| ... | ... | @@ -76,7 +85,13 @@ pub fn deinit(self: *Wasm) void { |
| 76 | 85 | decl.fn_link.wasm.?.code.deinit(self.base.allocator); |
| 77 | 86 | decl.fn_link.wasm.?.idx_refs.deinit(self.base.allocator); |
| 78 | 87 | } |
| 88 | for (self.ext_funcs.items) |decl| { |
| 89 | decl.fn_link.wasm.?.functype.deinit(self.base.allocator); |
| 90 | decl.fn_link.wasm.?.code.deinit(self.base.allocator); |
| 91 | decl.fn_link.wasm.?.idx_refs.deinit(self.base.allocator); |
| 92 | } |
| 79 | 93 | self.funcs.deinit(self.base.allocator); |
| 94 | self.ext_funcs.deinit(self.base.allocator); |
| 80 | 95 | } |
| 81 | 96 | |
| 82 | 97 | // Generate code for the Decl, storing it in memory to be later written to |
| ... | ... | @@ -85,8 +100,6 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { |
| 85 | 100 | const typed_value = decl.typed_value.most_recent.typed_value; |
| 86 | 101 | if (typed_value.ty.zigTypeTag() != .Fn) |
| 87 | 102 | return error.TODOImplementNonFnDeclsForWasm; |
| 88 | | if (typed_value.val.tag() == .extern_fn) |
| 89 | | return error.TODOImplementExternFnDeclsForWasm; |
| 90 | 103 | |
| 91 | 104 | if (decl.fn_link.wasm) |*fn_data| { |
| 92 | 105 | fn_data.functype.items.len = 0; |
| ... | ... | @@ -94,7 +107,12 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { |
| 94 | 107 | fn_data.idx_refs.items.len = 0; |
| 95 | 108 | } else { |
| 96 | 109 | decl.fn_link.wasm = .{}; |
| 97 | | try self.funcs.append(self.base.allocator, decl); |
| 110 | // dependent on function type, appends it to the correct list |
| 111 | switch (decl.typed_value.most_recent.typed_value.val.tag()) { |
| 112 | .function => try self.funcs.append(self.base.allocator, decl), |
| 113 | .extern_fn => try self.ext_funcs.append(self.base.allocator, decl), |
| 114 | else => return error.TODOImplementNonFnDeclsForWasm, |
| 115 | } |
| 98 | 116 | } |
| 99 | 117 | const fn_data = &decl.fn_link.wasm.?; |
| 100 | 118 | |
| ... | ... | @@ -143,7 +161,12 @@ pub fn updateDeclExports( |
| 143 | 161 | pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { |
| 144 | 162 | // TODO: remove this assert when non-function Decls are implemented |
| 145 | 163 | assert(decl.typed_value.most_recent.typed_value.ty.zigTypeTag() == .Fn); |
| 146 | | _ = self.funcs.swapRemove(self.getFuncidx(decl).?); |
| 164 | const func_idx = self.getFuncidx(decl).?; |
| 165 | switch (decl.typed_value.most_recent.typed_value.val.tag()) { |
| 166 | .function => _ = self.funcs.swapRemove(func_idx), |
| 167 | .extern_fn => _ = self.ext_funcs.swapRemove(func_idx), |
| 168 | else => unreachable, |
| 169 | } |
| 147 | 170 | decl.fn_link.wasm.?.functype.deinit(self.base.allocator); |
| 148 | 171 | decl.fn_link.wasm.?.code.deinit(self.base.allocator); |
| 149 | 172 | decl.fn_link.wasm.?.idx_refs.deinit(self.base.allocator); |
| ... | ... | @@ -172,15 +195,46 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 172 | 195 | // Type section |
| 173 | 196 | { |
| 174 | 197 | const header_offset = try reserveVecSectionHeader(file); |
| 175 | | for (self.funcs.items) |decl| { |
| 176 | | try file.writeAll(decl.fn_link.wasm.?.functype.items); |
| 177 | | } |
| 198 | |
| 199 | // extern functions are defined in the wasm binary first through the `import` |
| 200 | // section, so define their func types first |
| 201 | for (self.ext_funcs.items) |decl| try file.writeAll(decl.fn_link.wasm.?.functype.items); |
| 202 | for (self.funcs.items) |decl| try file.writeAll(decl.fn_link.wasm.?.functype.items); |
| 203 | |
| 178 | 204 | try writeVecSectionHeader( |
| 179 | 205 | file, |
| 180 | 206 | header_offset, |
| 181 | 207 | .type, |
| 182 | 208 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 183 | | @intCast(u32, self.funcs.items.len), |
| 209 | @intCast(u32, self.ext_funcs.items.len + self.funcs.items.len), |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | // Import section |
| 214 | { |
| 215 | // TODO: implement non-functions imports |
| 216 | const header_offset = try reserveVecSectionHeader(file); |
| 217 | const writer = file.writer(); |
| 218 | for (self.ext_funcs.items) |decl, typeidx| { |
| 219 | try leb.writeULEB128(writer, @intCast(u32, self.host_name.len)); |
| 220 | try writer.writeAll(self.host_name); |
| 221 | |
| 222 | // wasm requires the length of the import name with no null-termination |
| 223 | const decl_len = mem.len(decl.name); |
| 224 | try leb.writeULEB128(writer, @intCast(u32, decl_len)); |
| 225 | try writer.writeAll(decl.name[0..decl_len]); |
| 226 | |
| 227 | // emit kind and the function type |
| 228 | try writer.writeByte(wasm.externalKind(.function)); |
| 229 | try leb.writeULEB128(writer, @intCast(u32, typeidx)); |
| 230 | } |
| 231 | |
| 232 | try writeVecSectionHeader( |
| 233 | file, |
| 234 | header_offset, |
| 235 | .import, |
| 236 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 237 | @intCast(u32, self.ext_funcs.items.len), |
| 184 | 238 | ); |
| 185 | 239 | } |
| 186 | 240 | |
| ... | ... | @@ -188,7 +242,11 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 188 | 242 | { |
| 189 | 243 | const header_offset = try reserveVecSectionHeader(file); |
| 190 | 244 | const writer = file.writer(); |
| 191 | | for (self.funcs.items) |_, typeidx| try leb.writeULEB128(writer, @intCast(u32, typeidx)); |
| 245 | for (self.funcs.items) |_, typeidx| { |
| 246 | const func_idx = @intCast(u32, self.getFuncIdxOffset() + typeidx); |
| 247 | try leb.writeULEB128(writer, func_idx); |
| 248 | } |
| 249 | |
| 192 | 250 | try writeVecSectionHeader( |
| 193 | 251 | file, |
| 194 | 252 | header_offset, |
| ... | ... | @@ -212,7 +270,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 212 | 270 | switch (exprt.exported_decl.typed_value.most_recent.typed_value.ty.zigTypeTag()) { |
| 213 | 271 | .Fn => { |
| 214 | 272 | // Type of the export |
| 215 | | try writer.writeByte(0x00); |
| 273 | try writer.writeByte(wasm.externalKind(.function)); |
| 216 | 274 | // Exported function index |
| 217 | 275 | try leb.writeULEB128(writer, self.getFuncidx(exprt.exported_decl).?); |
| 218 | 276 | }, |
| ... | ... | @@ -523,13 +581,31 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { |
| 523 | 581 | } |
| 524 | 582 | |
| 525 | 583 | /// Get the current index of a given Decl in the function list |
| 526 | | /// TODO: we could maintain a hash map to potentially make this |
| 584 | /// This will correctly provide the index, regardless whether the function is extern or not |
| 585 | /// TODO: we could maintain a hash map to potentially make this simpler |
| 527 | 586 | fn getFuncidx(self: Wasm, decl: *Module.Decl) ?u32 { |
| 528 | | return for (self.funcs.items) |func, idx| { |
| 529 | | if (func == decl) break @intCast(u32, idx); |
| 587 | var offset: u32 = 0; |
| 588 | const slice = switch (decl.typed_value.most_recent.typed_value.val.tag()) { |
| 589 | .function => blk: { |
| 590 | // when the target is a regular function, we have to calculate |
| 591 | // the offset of where the index starts |
| 592 | offset += self.getFuncIdxOffset(); |
| 593 | break :blk self.funcs.items; |
| 594 | }, |
| 595 | .extern_fn => self.ext_funcs.items, |
| 596 | else => return null, |
| 597 | }; |
| 598 | return for (slice) |func, idx| { |
| 599 | if (func == decl) break @intCast(u32, offset + idx); |
| 530 | 600 | } else null; |
| 531 | 601 | } |
| 532 | 602 | |
| 603 | /// Based on the size of `ext_funcs` returns the |
| 604 | /// offset of the function indices |
| 605 | fn getFuncIdxOffset(self: Wasm) u32 { |
| 606 | return @intCast(u32, self.ext_funcs.items.len); |
| 607 | } |
| 608 | |
| 533 | 609 | fn reserveVecSectionHeader(file: fs.File) !u64 { |
| 534 | 610 | // section id + fixed leb contents size + fixed leb vector length |
| 535 | 611 | const header_size = 1 + 5 + 5; |