| author | |
| committer | |
| log | c0685458a2f9463bf3c2276f9b5d9ca4b3157cd7 |
| tree | 6b42f4e7b522f6a65d72bcbf551eae6bef1b3870 |
| parent | 683d3f72427b39bea5827a0a1c1fc4c74dbe246e |
Update link.Wasm.zig to use std.wasm for its constants
Make opcodes u8 and non-exhaustive
Update test and rename 'spec' to 'wasm'3 files changed, 284 insertions(+), 35 deletions(-)
lib/std/std.zig+1| ... | ... | @@ -77,6 +77,7 @@ pub const testing = @import("testing.zig"); |
| 77 | 77 | pub const time = @import("time.zig"); |
| 78 | 78 | pub const unicode = @import("unicode.zig"); |
| 79 | 79 | pub const valgrind = @import("valgrind.zig"); |
| 80 | pub const wasm = @import("wasm.zig"); | |
| 80 | 81 | pub const zig = @import("zig.zig"); |
| 81 | 82 | pub const start = @import("start.zig"); |
| 82 | 83 |
lib/std/wasm.zig created+266| ... | ... | @@ -0,0 +1,266 @@ |
| 1 | // SPDX-License-Identifier: MIT | |
| 2 | // Copyright (c) 2015-2021 Zig Contributors | |
| 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed. | |
| 4 | // The MIT license requires this copyright notice to be included in all copies | |
| 5 | // and substantial portions of the software. | |
| 6 | const testing = @import("std.zig").testing; | |
| 7 | ||
| 8 | /// Wasm instruction opcodes | |
| 9 | /// | |
| 10 | /// All instructions are defined as per spec: | |
| 11 | /// https://webassembly.github.io/spec/core/appendix/index-instructions.html | |
| 12 | pub const Opcode = enum(u8) { | |
| 13 | @"unreachable" = 0x00, | |
| 14 | nop = 0x01, | |
| 15 | block = 0x02, | |
| 16 | loop = 0x03, | |
| 17 | @"if" = 0x04, | |
| 18 | @"else" = 0x05, | |
| 19 | end = 0x0B, | |
| 20 | br = 0x0C, | |
| 21 | br_if = 0x0D, | |
| 22 | br_table = 0x0E, | |
| 23 | @"return" = 0x0F, | |
| 24 | call = 0x10, | |
| 25 | call_indirect = 0x11, | |
| 26 | drop = 0x1A, | |
| 27 | select = 0x1B, | |
| 28 | local_get = 0x20, | |
| 29 | local_set = 0x21, | |
| 30 | local_tee = 0x22, | |
| 31 | global_get = 0x23, | |
| 32 | global_set = 0x24, | |
| 33 | i32_load = 0x28, | |
| 34 | i64_load = 0x29, | |
| 35 | f32_load = 0x2A, | |
| 36 | f64_load = 0x2B, | |
| 37 | i32_load8_s = 0x2C, | |
| 38 | i32_load8_u = 0x2D, | |
| 39 | i32_load16_s = 0x2E, | |
| 40 | i32_load16_u = 0x2F, | |
| 41 | i64_load8_s = 0x30, | |
| 42 | i64_load8_u = 0x31, | |
| 43 | i64_load16_s = 0x32, | |
| 44 | i64_load16_u = 0x33, | |
| 45 | i64_load32_s = 0x34, | |
| 46 | i64_load32_u = 0x35, | |
| 47 | i32_store = 0x36, | |
| 48 | i64_store = 0x37, | |
| 49 | f32_store = 0x38, | |
| 50 | f64_store = 0x39, | |
| 51 | i32_store8 = 0x3A, | |
| 52 | i32_store16 = 0x3B, | |
| 53 | i64_store8 = 0x3C, | |
| 54 | i64_store16 = 0x3D, | |
| 55 | i64_store32 = 0x3E, | |
| 56 | memory_size = 0x3F, | |
| 57 | memory_grow = 0x40, | |
| 58 | i32_const = 0x41, | |
| 59 | i64_const = 0x42, | |
| 60 | f32_const = 0x43, | |
| 61 | f64_const = 0x44, | |
| 62 | i32_eqz = 0x45, | |
| 63 | i32_eq = 0x46, | |
| 64 | i32_ne = 0x47, | |
| 65 | i32_lt_s = 0x48, | |
| 66 | i32_lt_u = 0x49, | |
| 67 | i32_gt_s = 0x4A, | |
| 68 | i32_gt_u = 0x4B, | |
| 69 | i32_le_s = 0x4C, | |
| 70 | i32_le_u = 0x4D, | |
| 71 | i32_ge_s = 0x4E, | |
| 72 | i32_ge_u = 0x4F, | |
| 73 | i64_eqz = 0x50, | |
| 74 | i64_eq = 0x51, | |
| 75 | i64_ne = 0x52, | |
| 76 | i64_lt_s = 0x53, | |
| 77 | i64_lt_u = 0x54, | |
| 78 | i64_gt_s = 0x55, | |
| 79 | i64_gt_u = 0x56, | |
| 80 | i64_le_s = 0x57, | |
| 81 | i64_le_u = 0x58, | |
| 82 | i64_ge_s = 0x59, | |
| 83 | i64_ge_u = 0x5A, | |
| 84 | f32_eq = 0x5B, | |
| 85 | f32_ne = 0x5C, | |
| 86 | f32_lt = 0x5D, | |
| 87 | f32_gt = 0x5E, | |
| 88 | f32_le = 0x5F, | |
| 89 | f32_ge = 0x60, | |
| 90 | f64_eq = 0x61, | |
| 91 | f64_ne = 0x62, | |
| 92 | f64_lt = 0x63, | |
| 93 | f64_gt = 0x64, | |
| 94 | f64_le = 0x65, | |
| 95 | f64_ge = 0x66, | |
| 96 | i32_clz = 0x67, | |
| 97 | i32_ctz = 0x68, | |
| 98 | i32_popcnt = 0x69, | |
| 99 | i32_add = 0x6A, | |
| 100 | i32_sub = 0x6B, | |
| 101 | i32_mul = 0x6C, | |
| 102 | i32_div_s = 0x6D, | |
| 103 | i32_div_u = 0x6E, | |
| 104 | i32_rem_s = 0x6F, | |
| 105 | i32_rem_u = 0x70, | |
| 106 | i32_and = 0x71, | |
| 107 | i32_or = 0x72, | |
| 108 | i32_xor = 0x73, | |
| 109 | i32_shl = 0x74, | |
| 110 | i32_shr_s = 0x75, | |
| 111 | i32_shr_u = 0x76, | |
| 112 | i32_rotl = 0x77, | |
| 113 | i32_rotr = 0x78, | |
| 114 | i64_clz = 0x79, | |
| 115 | i64_ctz = 0x7A, | |
| 116 | i64_popcnt = 0x7B, | |
| 117 | i64_add = 0x7C, | |
| 118 | i64_sub = 0x7D, | |
| 119 | i64_mul = 0x7E, | |
| 120 | i64_div_s = 0x7F, | |
| 121 | i64_div_u = 0x80, | |
| 122 | i64_rem_s = 0x81, | |
| 123 | i64_rem_u = 0x82, | |
| 124 | i64_and = 0x83, | |
| 125 | i64_or = 0x84, | |
| 126 | i64_xor = 0x85, | |
| 127 | i64_shl = 0x86, | |
| 128 | i64_shr_s = 0x87, | |
| 129 | i64_shr_u = 0x88, | |
| 130 | i64_rotl = 0x89, | |
| 131 | i64_rotr = 0x8A, | |
| 132 | f32_abs = 0x8B, | |
| 133 | f32_neg = 0x8C, | |
| 134 | f32_ceil = 0x8D, | |
| 135 | f32_floor = 0x8E, | |
| 136 | f32_trunc = 0x8F, | |
| 137 | f32_nearest = 0x90, | |
| 138 | f32_sqrt = 0x91, | |
| 139 | f32_add = 0x92, | |
| 140 | f32_sub = 0x93, | |
| 141 | f32_mul = 0x94, | |
| 142 | f32_div = 0x95, | |
| 143 | f32_min = 0x96, | |
| 144 | f32_max = 0x97, | |
| 145 | f32_copysign = 0x98, | |
| 146 | f64_abs = 0x99, | |
| 147 | f64_neg = 0x9A, | |
| 148 | f64_ceil = 0x9B, | |
| 149 | f64_floor = 0x9C, | |
| 150 | f64_trunc = 0x9D, | |
| 151 | f64_nearest = 0x9E, | |
| 152 | f64_sqrt = 0x9F, | |
| 153 | f64_add = 0xA0, | |
| 154 | f64_sub = 0xA1, | |
| 155 | f64_mul = 0xA2, | |
| 156 | f64_div = 0xA3, | |
| 157 | f64_min = 0xA4, | |
| 158 | f64_max = 0xA5, | |
| 159 | f64_copysign = 0xA6, | |
| 160 | i32_wrap_i64 = 0xA7, | |
| 161 | i32_trunc_f32_s = 0xA8, | |
| 162 | i32_trunc_f32_u = 0xA9, | |
| 163 | i32_trunc_f64_s = 0xB0, | |
| 164 | i32_trunc_f64_u = 0xB1, | |
| 165 | f32_convert_i32_s = 0xB2, | |
| 166 | f32_convert_i32_u = 0xB3, | |
| 167 | f32_convert_i64_s = 0xB4, | |
| 168 | f32_convert_i64_u = 0xB5, | |
| 169 | f32_demote_f64 = 0xB6, | |
| 170 | f64_convert_i32_s = 0xB7, | |
| 171 | f64_convert_i32_u = 0xB8, | |
| 172 | f64_convert_i64_s = 0xB9, | |
| 173 | f64_convert_i64_u = 0xBA, | |
| 174 | f64_promote_f32 = 0xBB, | |
| 175 | i32_reinterpret_f32 = 0xBC, | |
| 176 | i64_reinterpret_f64 = 0xBD, | |
| 177 | f32_reinterpret_i32 = 0xBE, | |
| 178 | i64_reinterpret_i64 = 0xBF, | |
| 179 | i32_extend8_s = 0xC0, | |
| 180 | i32_extend16_s = 0xC1, | |
| 181 | i64_extend8_s = 0xC2, | |
| 182 | i64_extend16_s = 0xC3, | |
| 183 | i64_extend32_s = 0xC4, | |
| 184 | _, | |
| 185 | }; | |
| 186 | ||
| 187 | /// Returns the integer value of an `Opcode`. Used by the Zig compiler | |
| 188 | /// to write instructions to the wasm binary file | |
| 189 | pub fn opcode(op: Opcode) u8 { | |
| 190 | return @enumToInt(op); | |
| 191 | } | |
| 192 | ||
| 193 | test "Wasm - opcodes" { | |
| 194 | // Ensure our opcodes values remain intact as certain values are skipped due to them being reserved | |
| 195 | const i32_const = opcode(.i32_const); | |
| 196 | const end = opcode(.end); | |
| 197 | const drop = opcode(.drop); | |
| 198 | const local_get = opcode(.local_get); | |
| 199 | const i64_extend32_s = opcode(.i64_extend32_s); | |
| 200 | ||
| 201 | testing.expectEqual(@as(u16, 0x41), i32_const); | |
| 202 | testing.expectEqual(@as(u16, 0x0B), end); | |
| 203 | testing.expectEqual(@as(u16, 0x1A), drop); | |
| 204 | testing.expectEqual(@as(u16, 0x20), local_get); | |
| 205 | testing.expectEqual(@as(u16, 0xC4), i64_extend32_s); | |
| 206 | } | |
| 207 | ||
| 208 | /// Enum representing all Wasm value types as per spec: | |
| 209 | /// https://webassembly.github.io/spec/core/binary/types.html | |
| 210 | pub const Valtype = enum(u8) { | |
| 211 | i32 = 0x7F, | |
| 212 | i64 = 0x7E, | |
| 213 | f32 = 0x7D, | |
| 214 | f64 = 0x7C, | |
| 215 | }; | |
| 216 | ||
| 217 | /// Returns the integer value of a `Valtype` | |
| 218 | pub fn valtype(value: Valtype) u8 { | |
| 219 | return @enumToInt(value); | |
| 220 | } | |
| 221 | ||
| 222 | test "Wasm - valtypes" { | |
| 223 | const _i32 = valtype(.i32); | |
| 224 | const _i64 = valtype(.i64); | |
| 225 | const _f32 = valtype(.f32); | |
| 226 | const _f64 = valtype(.f64); | |
| 227 | ||
| 228 | testing.expectEqual(@as(u8, 0x7F), _i32); | |
| 229 | testing.expectEqual(@as(u8, 0x7E), _i64); | |
| 230 | testing.expectEqual(@as(u8, 0x7D), _f32); | |
| 231 | testing.expectEqual(@as(u8, 0x7C), _f64); | |
| 232 | } | |
| 233 | ||
| 234 | /// Wasm module sections as per spec: | |
| 235 | /// https://webassembly.github.io/spec/core/binary/modules.html | |
| 236 | pub const Section = enum(u8) { | |
| 237 | custom, | |
| 238 | type, | |
| 239 | import, | |
| 240 | function, | |
| 241 | table, | |
| 242 | memory, | |
| 243 | global, | |
| 244 | @"export", | |
| 245 | start, | |
| 246 | element, | |
| 247 | code, | |
| 248 | data, | |
| 249 | }; | |
| 250 | ||
| 251 | /// Returns the integer value of a given `Section` | |
| 252 | pub fn section(val: Section) u8 { | |
| 253 | return @enumToInt(val); | |
| 254 | } | |
| 255 | ||
| 256 | // types | |
| 257 | pub const element_type: u8 = 0x70; | |
| 258 | pub const function_type: u8 = 0x60; | |
| 259 | pub const result_type: u8 = 0x40; | |
| 260 | ||
| 261 | /// Represents a block which will not return a value | |
| 262 | pub const block_empty: u8 = 0x40; | |
| 263 | ||
| 264 | // binary constants | |
| 265 | pub const magic = [_]u8{ 0x00, 0x61, 0x73, 0x6D }; // \0asm | |
| 266 | pub const version = [_]u8{ 0x01, 0x00, 0x00, 0x00 }; // version 1 |
src/link/Wasm.zig+17-35| ... | ... | @@ -7,6 +7,7 @@ const assert = std.debug.assert; |
| 7 | 7 | const fs = std.fs; |
| 8 | 8 | const leb = std.leb; |
| 9 | 9 | const log = std.log.scoped(.link); |
| 10 | const wasm = std.wasm; | |
| 10 | 11 | |
| 11 | 12 | const Module = @import("../Module.zig"); |
| 12 | 13 | const Compilation = @import("../Compilation.zig"); |
| ... | ... | @@ -16,25 +17,6 @@ const trace = @import("../tracy.zig").trace; |
| 16 | 17 | const build_options = @import("build_options"); |
| 17 | 18 | const Cache = @import("../Cache.zig"); |
| 18 | 19 | |
| 19 | /// Various magic numbers defined by the wasm spec | |
| 20 | const spec = struct { | |
| 21 | const magic = [_]u8{ 0x00, 0x61, 0x73, 0x6D }; // \0asm | |
| 22 | const version = [_]u8{ 0x01, 0x00, 0x00, 0x00 }; // version 1 | |
| 23 | ||
| 24 | const custom_id = 0; | |
| 25 | const types_id = 1; | |
| 26 | const imports_id = 2; | |
| 27 | const funcs_id = 3; | |
| 28 | const tables_id = 4; | |
| 29 | const memories_id = 5; | |
| 30 | const globals_id = 6; | |
| 31 | const exports_id = 7; | |
| 32 | const start_id = 8; | |
| 33 | const elements_id = 9; | |
| 34 | const code_id = 10; | |
| 35 | const data_id = 11; | |
| 36 | }; | |
| 37 | ||
| 38 | 20 | pub const base_tag = link.File.Tag.wasm; |
| 39 | 21 | |
| 40 | 22 | pub const FnData = struct { |
| ... | ... | @@ -65,19 +47,19 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio |
| 65 | 47 | const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true }); |
| 66 | 48 | errdefer file.close(); |
| 67 | 49 | |
| 68 | const wasm = try createEmpty(allocator, options); | |
| 69 | errdefer wasm.base.destroy(); | |
| 50 | const wasm_bin = try createEmpty(allocator, options); | |
| 51 | errdefer wasm_bin.base.destroy(); | |
| 70 | 52 | |
| 71 | wasm.base.file = file; | |
| 53 | wasm_bin.base.file = file; | |
| 72 | 54 | |
| 73 | try file.writeAll(&(spec.magic ++ spec.version)); | |
| 55 | try file.writeAll(&(wasm.magic ++ wasm.version)); | |
| 74 | 56 | |
| 75 | return wasm; | |
| 57 | return wasm_bin; | |
| 76 | 58 | } |
| 77 | 59 | |
| 78 | 60 | pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Wasm { |
| 79 | const wasm = try gpa.create(Wasm); | |
| 80 | wasm.* = .{ | |
| 61 | const wasm_bin = try gpa.create(Wasm); | |
| 62 | wasm_bin.* = .{ | |
| 81 | 63 | .base = .{ |
| 82 | 64 | .tag = .wasm, |
| 83 | 65 | .options = options, |
| ... | ... | @@ -85,7 +67,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Wasm { |
| 85 | 67 | .allocator = gpa, |
| 86 | 68 | }, |
| 87 | 69 | }; |
| 88 | return wasm; | |
| 70 | return wasm_bin; | |
| 89 | 71 | } |
| 90 | 72 | |
| 91 | 73 | pub fn deinit(self: *Wasm) void { |
| ... | ... | @@ -176,8 +158,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 176 | 158 | const header_size = 5 + 1; |
| 177 | 159 | |
| 178 | 160 | // No need to rewrite the magic/version header |
| 179 | try file.setEndPos(@sizeOf(@TypeOf(spec.magic ++ spec.version))); | |
| 180 | try file.seekTo(@sizeOf(@TypeOf(spec.magic ++ spec.version))); | |
| 161 | try file.setEndPos(@sizeOf(@TypeOf(wasm.magic ++ wasm.version))); | |
| 162 | try file.seekTo(@sizeOf(@TypeOf(wasm.magic ++ wasm.version))); | |
| 181 | 163 | |
| 182 | 164 | // Type section |
| 183 | 165 | { |
| ... | ... | @@ -188,7 +170,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 188 | 170 | try writeVecSectionHeader( |
| 189 | 171 | file, |
| 190 | 172 | header_offset, |
| 191 | spec.types_id, | |
| 173 | .type, | |
| 192 | 174 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 193 | 175 | @intCast(u32, self.funcs.items.len), |
| 194 | 176 | ); |
| ... | ... | @@ -202,7 +184,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 202 | 184 | try writeVecSectionHeader( |
| 203 | 185 | file, |
| 204 | 186 | header_offset, |
| 205 | spec.funcs_id, | |
| 187 | .function, | |
| 206 | 188 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 207 | 189 | @intCast(u32, self.funcs.items.len), |
| 208 | 190 | ); |
| ... | ... | @@ -235,7 +217,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 235 | 217 | try writeVecSectionHeader( |
| 236 | 218 | file, |
| 237 | 219 | header_offset, |
| 238 | spec.exports_id, | |
| 220 | .@"export", | |
| 239 | 221 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 240 | 222 | count, |
| 241 | 223 | ); |
| ... | ... | @@ -266,7 +248,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 266 | 248 | try writeVecSectionHeader( |
| 267 | 249 | file, |
| 268 | 250 | header_offset, |
| 269 | spec.code_id, | |
| 251 | .code, | |
| 270 | 252 | @intCast(u32, (try file.getPos()) - header_offset - header_size), |
| 271 | 253 | @intCast(u32, self.funcs.items.len), |
| 272 | 254 | ); |
| ... | ... | @@ -549,9 +531,9 @@ fn reserveVecSectionHeader(file: fs.File) !u64 { |
| 549 | 531 | return (try file.getPos()) - header_size; |
| 550 | 532 | } |
| 551 | 533 | |
| 552 | fn writeVecSectionHeader(file: fs.File, offset: u64, section: u8, size: u32, items: u32) !void { | |
| 534 | fn writeVecSectionHeader(file: fs.File, offset: u64, section: wasm.Section, size: u32, items: u32) !void { | |
| 553 | 535 | var buf: [1 + 5 + 5]u8 = undefined; |
| 554 | buf[0] = section; | |
| 536 | buf[0] = @enumToInt(section); | |
| 555 | 537 | leb.writeUnsignedFixed(5, buf[1..6], size); |
| 556 | 538 | leb.writeUnsignedFixed(5, buf[6..], items); |
| 557 | 539 | try file.pwriteAll(&buf, offset); |