| ... | @@ -2,6 +2,7 @@ const std = @import("std"); | ... | @@ -2,6 +2,7 @@ const std = @import("std"); |
| 2 | const Allocator = std.mem.Allocator; | 2 | const Allocator = std.mem.Allocator; |
| 3 | const ArrayList = std.ArrayList; | 3 | const ArrayList = std.ArrayList; |
| 4 | const assert = std.debug.assert; | 4 | const assert = std.debug.assert; |
| | 5 | const testing = std.testing; |
| 5 | const leb = std.leb; | 6 | const leb = std.leb; |
| 6 | const mem = std.mem; | 7 | const mem = std.mem; |
| 7 | const wasm = std.wasm; | 8 | const wasm = std.wasm; |
| ... | @@ -29,6 +30,445 @@ const WValue = union(enum) { | ... | @@ -29,6 +30,445 @@ const WValue = union(enum) { |
| 29 | block_idx: u32, | 30 | block_idx: u32, |
| 30 | }; | 31 | }; |
| 31 | | 32 | |
| | 33 | /// Wasm ops, but without input/output/signedness information |
| | 34 | /// Used for `buildOpcode` |
| | 35 | const Op = enum { |
| | 36 | @"unreachable", |
| | 37 | nop, |
| | 38 | block, |
| | 39 | loop, |
| | 40 | @"if", |
| | 41 | @"else", |
| | 42 | end, |
| | 43 | br, |
| | 44 | br_if, |
| | 45 | br_table, |
| | 46 | @"return", |
| | 47 | call, |
| | 48 | call_indirect, |
| | 49 | drop, |
| | 50 | select, |
| | 51 | local_get, |
| | 52 | local_set, |
| | 53 | local_tee, |
| | 54 | global_get, |
| | 55 | global_set, |
| | 56 | load, |
| | 57 | store, |
| | 58 | memory_size, |
| | 59 | memory_grow, |
| | 60 | @"const", |
| | 61 | eqz, |
| | 62 | eq, |
| | 63 | ne, |
| | 64 | lt, |
| | 65 | gt, |
| | 66 | le, |
| | 67 | ge, |
| | 68 | clz, |
| | 69 | ctz, |
| | 70 | popcnt, |
| | 71 | add, |
| | 72 | sub, |
| | 73 | mul, |
| | 74 | div, |
| | 75 | rem, |
| | 76 | @"and", |
| | 77 | @"or", |
| | 78 | xor, |
| | 79 | shl, |
| | 80 | shr, |
| | 81 | rotl, |
| | 82 | rotr, |
| | 83 | abs, |
| | 84 | neg, |
| | 85 | ceil, |
| | 86 | floor, |
| | 87 | trunc, |
| | 88 | nearest, |
| | 89 | sqrt, |
| | 90 | min, |
| | 91 | max, |
| | 92 | copysign, |
| | 93 | wrap, |
| | 94 | convert, |
| | 95 | demote, |
| | 96 | promote, |
| | 97 | reinterpret, |
| | 98 | extend, |
| | 99 | }; |
| | 100 | |
| | 101 | /// Contains the settings needed to create an `Opcode` using `buildOpcode`. |
| | 102 | /// |
| | 103 | /// The fields correspond to the opcode name. Here is an example |
| | 104 | /// i32_trunc_f32_s |
| | 105 | /// ^ ^ ^ ^ |
| | 106 | /// | | | | |
| | 107 | /// valtype1 | | | |
| | 108 | /// = .i32 | | | |
| | 109 | /// | | | |
| | 110 | /// op | | |
| | 111 | /// = .trunc | | |
| | 112 | /// | | |
| | 113 | /// valtype2 | |
| | 114 | /// = .f32 | |
| | 115 | /// | |
| | 116 | /// width | |
| | 117 | /// = null | |
| | 118 | /// | |
| | 119 | /// signed |
| | 120 | /// = true |
| | 121 | /// |
| | 122 | /// There can be missing fields, here are some more examples: |
| | 123 | /// i64_load8_u |
| | 124 | /// --> .{ .valtype1 = .i64, .op = .load, .width = 8, signed = false } |
| | 125 | /// i32_mul |
| | 126 | /// --> .{ .valtype1 = .i32, .op = .trunc } |
| | 127 | /// nop |
| | 128 | /// --> .{ .op = .nop } |
| | 129 | const OpcodeBuildArguments = struct { |
| | 130 | /// First valtype in the opcode (usually represents the type of the output) |
| | 131 | valtype1: ?wasm.Valtype = null, |
| | 132 | /// The operation (e.g. call, unreachable, div, min, sqrt, etc.) |
| | 133 | op: Op, |
| | 134 | /// Width of the operation (e.g. 8 for i32_load8_s, 16 for i64_extend16_i32_s) |
| | 135 | width: ?u8 = null, |
| | 136 | /// Second valtype in the opcode name (usually represents the type of the input) |
| | 137 | valtype2: ?wasm.Valtype = null, |
| | 138 | /// Signedness of the op |
| | 139 | signedness: ?std.builtin.Signedness = null, |
| | 140 | }; |
| | 141 | |
| | 142 | /// Helper function that builds an Opcode given the arguments needed |
| | 143 | fn buildOpcode(args: OpcodeBuildArguments) wasm.Opcode { |
| | 144 | switch (args.op) { |
| | 145 | .@"unreachable" => return .@"unreachable", |
| | 146 | .nop => return .nop, |
| | 147 | .block => return .block, |
| | 148 | .loop => return .loop, |
| | 149 | .@"if" => return .@"if", |
| | 150 | .@"else" => return .@"else", |
| | 151 | .end => return .end, |
| | 152 | .br => return .br, |
| | 153 | .br_if => return .br_if, |
| | 154 | .br_table => return .br_table, |
| | 155 | .@"return" => return .@"return", |
| | 156 | .call => return .call, |
| | 157 | .call_indirect => return .call_indirect, |
| | 158 | .drop => return .drop, |
| | 159 | .select => return .select, |
| | 160 | .local_get => return .local_get, |
| | 161 | .local_set => return .local_set, |
| | 162 | .local_tee => return .local_tee, |
| | 163 | .global_get => return .global_get, |
| | 164 | .global_set => return .global_set, |
| | 165 | |
| | 166 | .load => if (args.width) |width| |
| | 167 | switch (width) { |
| | 168 | 8 => switch (args.valtype1.?) { |
| | 169 | .i32 => if (args.signedness.? == .signed) return .i32_load8_s else return .i32_load8_u, |
| | 170 | .i64 => if (args.signedness.? == .signed) return .i64_load8_s else return .i64_load8_u, |
| | 171 | .f32, .f64 => unreachable, |
| | 172 | }, |
| | 173 | 16 => switch (args.valtype1.?) { |
| | 174 | .i32 => if (args.signedness.? == .signed) return .i32_load16_s else return .i32_load16_u, |
| | 175 | .i64 => if (args.signedness.? == .signed) return .i64_load16_s else return .i64_load16_u, |
| | 176 | .f32, .f64 => unreachable, |
| | 177 | }, |
| | 178 | 32 => switch (args.valtype1.?) { |
| | 179 | .i64 => if (args.signedness.? == .signed) return .i64_load32_s else return .i64_load32_u, |
| | 180 | .i32, .f32, .f64 => unreachable, |
| | 181 | }, |
| | 182 | else => unreachable, |
| | 183 | } |
| | 184 | else switch (args.valtype1.?) { |
| | 185 | .i32 => return .i32_load, |
| | 186 | .i64 => return .i64_load, |
| | 187 | .f32 => return .f32_load, |
| | 188 | .f64 => return .f64_load, |
| | 189 | }, |
| | 190 | .store => if (args.width) |width| { |
| | 191 | switch (width) { |
| | 192 | 8 => switch (args.valtype1.?) { |
| | 193 | .i32 => return .i32_store8, |
| | 194 | .i64 => return .i64_store8, |
| | 195 | .f32, .f64 => unreachable, |
| | 196 | }, |
| | 197 | 16 => switch (args.valtype1.?) { |
| | 198 | .i32 => return .i32_store16, |
| | 199 | .i64 => return .i64_store16, |
| | 200 | .f32, .f64 => unreachable, |
| | 201 | }, |
| | 202 | 32 => switch (args.valtype1.?) { |
| | 203 | .i64 => return .i64_store32, |
| | 204 | .i32, .f32, .f64 => unreachable, |
| | 205 | }, |
| | 206 | else => unreachable, |
| | 207 | } |
| | 208 | } else { |
| | 209 | switch (args.valtype1.?) { |
| | 210 | .i32 => return .i32_store, |
| | 211 | .i64 => return .i64_store, |
| | 212 | .f32 => return .f32_store, |
| | 213 | .f64 => return .f64_store, |
| | 214 | } |
| | 215 | }, |
| | 216 | |
| | 217 | .memory_size => return .memory_size, |
| | 218 | .memory_grow => return .memory_grow, |
| | 219 | |
| | 220 | .@"const" => switch (args.valtype1.?) { |
| | 221 | .i32 => return .i32_const, |
| | 222 | .i64 => return .i64_const, |
| | 223 | .f32 => return .f32_const, |
| | 224 | .f64 => return .f64_const, |
| | 225 | }, |
| | 226 | |
| | 227 | .eqz => switch (args.valtype1.?) { |
| | 228 | .i32 => return .i32_eqz, |
| | 229 | .i64 => return .i64_eqz, |
| | 230 | .f32, .f64 => unreachable, |
| | 231 | }, |
| | 232 | .eq => switch (args.valtype1.?) { |
| | 233 | .i32 => return .i32_eq, |
| | 234 | .i64 => return .i64_eq, |
| | 235 | .f32 => return .f32_eq, |
| | 236 | .f64 => return .f64_eq, |
| | 237 | }, |
| | 238 | .ne => switch (args.valtype1.?) { |
| | 239 | .i32 => return .i32_ne, |
| | 240 | .i64 => return .i64_ne, |
| | 241 | .f32 => return .f32_ne, |
| | 242 | .f64 => return .f64_ne, |
| | 243 | }, |
| | 244 | |
| | 245 | .lt => switch (args.valtype1.?) { |
| | 246 | .i32 => if (args.signedness.? == .signed) return .i32_lt_s else return .i32_lt_u, |
| | 247 | .i64 => if (args.signedness.? == .signed) return .i64_lt_s else return .i64_lt_u, |
| | 248 | .f32 => return .f32_lt, |
| | 249 | .f64 => return .f64_lt, |
| | 250 | }, |
| | 251 | .gt => switch (args.valtype1.?) { |
| | 252 | .i32 => if (args.signedness.? == .signed) return .i32_gt_s else return .i32_gt_u, |
| | 253 | .i64 => if (args.signedness.? == .signed) return .i64_gt_s else return .i64_gt_u, |
| | 254 | .f32 => return .f32_gt, |
| | 255 | .f64 => return .f64_gt, |
| | 256 | }, |
| | 257 | .le => switch (args.valtype1.?) { |
| | 258 | .i32 => if (args.signedness.? == .signed) return .i32_le_s else return .i32_le_u, |
| | 259 | .i64 => if (args.signedness.? == .signed) return .i64_le_s else return .i64_le_u, |
| | 260 | .f32 => return .f32_le, |
| | 261 | .f64 => return .f64_le, |
| | 262 | }, |
| | 263 | .ge => switch (args.valtype1.?) { |
| | 264 | .i32 => if (args.signedness.? == .signed) return .i32_ge_s else return .i32_ge_u, |
| | 265 | .i64 => if (args.signedness.? == .signed) return .i64_ge_s else return .i64_ge_u, |
| | 266 | .f32 => return .f32_ge, |
| | 267 | .f64 => return .f64_ge, |
| | 268 | }, |
| | 269 | |
| | 270 | .clz => switch (args.valtype1.?) { |
| | 271 | .i32 => return .i32_clz, |
| | 272 | .i64 => return .i64_clz, |
| | 273 | .f32, .f64 => unreachable, |
| | 274 | }, |
| | 275 | .ctz => switch (args.valtype1.?) { |
| | 276 | .i32 => return .i32_ctz, |
| | 277 | .i64 => return .i64_ctz, |
| | 278 | .f32, .f64 => unreachable, |
| | 279 | }, |
| | 280 | .popcnt => switch (args.valtype1.?) { |
| | 281 | .i32 => return .i32_popcnt, |
| | 282 | .i64 => return .i64_popcnt, |
| | 283 | .f32, .f64 => unreachable, |
| | 284 | }, |
| | 285 | |
| | 286 | .add => switch (args.valtype1.?) { |
| | 287 | .i32 => return .i32_add, |
| | 288 | .i64 => return .i64_add, |
| | 289 | .f32 => return .f32_add, |
| | 290 | .f64 => return .f64_add, |
| | 291 | }, |
| | 292 | .sub => switch (args.valtype1.?) { |
| | 293 | .i32 => return .i32_sub, |
| | 294 | .i64 => return .i64_sub, |
| | 295 | .f32 => return .f32_sub, |
| | 296 | .f64 => return .f64_sub, |
| | 297 | }, |
| | 298 | .mul => switch (args.valtype1.?) { |
| | 299 | .i32 => return .i32_mul, |
| | 300 | .i64 => return .i64_mul, |
| | 301 | .f32 => return .f32_mul, |
| | 302 | .f64 => return .f64_mul, |
| | 303 | }, |
| | 304 | |
| | 305 | .div => switch (args.valtype1.?) { |
| | 306 | .i32 => if (args.signedness.? == .signed) return .i32_div_s else return .i32_div_u, |
| | 307 | .i64 => if (args.signedness.? == .signed) return .i64_div_s else return .i64_div_u, |
| | 308 | .f32 => return .f32_div, |
| | 309 | .f64 => return .f64_div, |
| | 310 | }, |
| | 311 | .rem => switch (args.valtype1.?) { |
| | 312 | .i32 => if (args.signedness.? == .signed) return .i32_rem_s else return .i32_rem_u, |
| | 313 | .i64 => if (args.signedness.? == .signed) return .i64_rem_s else return .i64_rem_u, |
| | 314 | .f32, .f64 => unreachable, |
| | 315 | }, |
| | 316 | |
| | 317 | .@"and" => switch (args.valtype1.?) { |
| | 318 | .i32 => return .i32_and, |
| | 319 | .i64 => return .i64_and, |
| | 320 | .f32, .f64 => unreachable, |
| | 321 | }, |
| | 322 | .@"or" => switch (args.valtype1.?) { |
| | 323 | .i32 => return .i32_or, |
| | 324 | .i64 => return .i64_or, |
| | 325 | .f32, .f64 => unreachable, |
| | 326 | }, |
| | 327 | .xor => switch (args.valtype1.?) { |
| | 328 | .i32 => return .i32_xor, |
| | 329 | .i64 => return .i64_xor, |
| | 330 | .f32, .f64 => unreachable, |
| | 331 | }, |
| | 332 | |
| | 333 | .shl => switch (args.valtype1.?) { |
| | 334 | .i32 => return .i32_shl, |
| | 335 | .i64 => return .i64_shl, |
| | 336 | .f32, .f64 => unreachable, |
| | 337 | }, |
| | 338 | .shr => switch (args.valtype1.?) { |
| | 339 | .i32 => if (args.signedness.? == .signed) return .i32_shr_s else return .i32_shr_u, |
| | 340 | .i64 => if (args.signedness.? == .signed) return .i64_shr_s else return .i64_shr_u, |
| | 341 | .f32, .f64 => unreachable, |
| | 342 | }, |
| | 343 | .rotl => switch (args.valtype1.?) { |
| | 344 | .i32 => return .i32_rotl, |
| | 345 | .i64 => return .i64_rotl, |
| | 346 | .f32, .f64 => unreachable, |
| | 347 | }, |
| | 348 | .rotr => switch (args.valtype1.?) { |
| | 349 | .i32 => return .i32_rotr, |
| | 350 | .i64 => return .i64_rotr, |
| | 351 | .f32, .f64 => unreachable, |
| | 352 | }, |
| | 353 | |
| | 354 | .abs => switch (args.valtype1.?) { |
| | 355 | .i32, .i64 => unreachable, |
| | 356 | .f32 => return .f32_abs, |
| | 357 | .f64 => return .f64_abs, |
| | 358 | }, |
| | 359 | .neg => switch (args.valtype1.?) { |
| | 360 | .i32, .i64 => unreachable, |
| | 361 | .f32 => return .f32_neg, |
| | 362 | .f64 => return .f64_neg, |
| | 363 | }, |
| | 364 | .ceil => switch (args.valtype1.?) { |
| | 365 | .i32, .i64 => unreachable, |
| | 366 | .f32 => return .f32_ceil, |
| | 367 | .f64 => return .f64_ceil, |
| | 368 | }, |
| | 369 | .floor => switch (args.valtype1.?) { |
| | 370 | .i32, .i64 => unreachable, |
| | 371 | .f32 => return .f32_floor, |
| | 372 | .f64 => return .f64_floor, |
| | 373 | }, |
| | 374 | .trunc => switch (args.valtype1.?) { |
| | 375 | .i32 => switch (args.valtype2.?) { |
| | 376 | .i32 => unreachable, |
| | 377 | .i64 => unreachable, |
| | 378 | .f32 => if (args.signedness.? == .signed) return .i32_trunc_f32_s else return .i32_trunc_f32_u, |
| | 379 | .f64 => if (args.signedness.? == .signed) return .i32_trunc_f64_s else return .i32_trunc_f64_u, |
| | 380 | }, |
| | 381 | .i64 => unreachable, |
| | 382 | .f32 => return .f32_trunc, |
| | 383 | .f64 => return .f64_trunc, |
| | 384 | }, |
| | 385 | .nearest => switch (args.valtype1.?) { |
| | 386 | .i32, .i64 => unreachable, |
| | 387 | .f32 => return .f32_nearest, |
| | 388 | .f64 => return .f64_nearest, |
| | 389 | }, |
| | 390 | .sqrt => switch (args.valtype1.?) { |
| | 391 | .i32, .i64 => unreachable, |
| | 392 | .f32 => return .f32_sqrt, |
| | 393 | .f64 => return .f64_sqrt, |
| | 394 | }, |
| | 395 | .min => switch (args.valtype1.?) { |
| | 396 | .i32, .i64 => unreachable, |
| | 397 | .f32 => return .f32_min, |
| | 398 | .f64 => return .f64_min, |
| | 399 | }, |
| | 400 | .max => switch (args.valtype1.?) { |
| | 401 | .i32, .i64 => unreachable, |
| | 402 | .f32 => return .f32_max, |
| | 403 | .f64 => return .f64_max, |
| | 404 | }, |
| | 405 | .copysign => switch (args.valtype1.?) { |
| | 406 | .i32, .i64 => unreachable, |
| | 407 | .f32 => return .f32_copysign, |
| | 408 | .f64 => return .f64_copysign, |
| | 409 | }, |
| | 410 | |
| | 411 | .wrap => switch (args.valtype1.?) { |
| | 412 | .i32 => switch (args.valtype2.?) { |
| | 413 | .i32 => unreachable, |
| | 414 | .i64 => return .i32_wrap_i64, |
| | 415 | .f32, .f64 => unreachable, |
| | 416 | }, |
| | 417 | .i64, .f32, .f64 => unreachable, |
| | 418 | }, |
| | 419 | .convert => switch (args.valtype1.?) { |
| | 420 | .i32, .i64 => unreachable, |
| | 421 | .f32 => switch (args.valtype2.?) { |
| | 422 | .i32 => if (args.signedness.? == .signed) return .f32_convert_i32_s else return .f32_convert_i32_u, |
| | 423 | .i64 => if (args.signedness.? == .signed) return .f32_convert_i64_s else return .f32_convert_i64_u, |
| | 424 | .f32, .f64 => unreachable, |
| | 425 | }, |
| | 426 | .f64 => switch (args.valtype2.?) { |
| | 427 | .i32 => if (args.signedness.? == .signed) return .f64_convert_i32_s else return .f64_convert_i32_u, |
| | 428 | .i64 => if (args.signedness.? == .signed) return .f64_convert_i64_s else return .f64_convert_i64_u, |
| | 429 | .f32, .f64 => unreachable, |
| | 430 | }, |
| | 431 | }, |
| | 432 | .demote => if (args.valtype1.? == .f32 and args.valtype2.? == .f64) return .f32_demote_f64 else unreachable, |
| | 433 | .promote => if (args.valtype1.? == .f64 and args.valtype2.? == .f32) return .f64_promote_f32 else unreachable, |
| | 434 | .reinterpret => switch (args.valtype1.?) { |
| | 435 | .i32 => if (args.valtype2.? == .f32) return .i32_reinterpret_f32 else unreachable, |
| | 436 | .i64 => if (args.valtype2.? == .f64) return .i64_reinterpret_f64 else unreachable, |
| | 437 | .f32 => if (args.valtype2.? == .i32) return .f32_reinterpret_i32 else unreachable, |
| | 438 | .f64 => if (args.valtype2.? == .i64) return .f64_reinterpret_i64 else unreachable, |
| | 439 | }, |
| | 440 | .extend => switch (args.valtype1.?) { |
| | 441 | .i32 => switch (args.width.?) { |
| | 442 | 8 => if (args.signedness.? == .signed) return .i32_extend8_s else unreachable, |
| | 443 | 16 => if (args.signedness.? == .signed) return .i32_extend16_s else unreachable, |
| | 444 | else => unreachable, |
| | 445 | }, |
| | 446 | .i64 => switch (args.width.?) { |
| | 447 | 8 => if (args.signedness.? == .signed) return .i64_extend8_s else unreachable, |
| | 448 | 16 => if (args.signedness.? == .signed) return .i64_extend16_s else unreachable, |
| | 449 | 32 => if (args.signedness.? == .signed) return .i64_extend32_s else unreachable, |
| | 450 | else => unreachable, |
| | 451 | }, |
| | 452 | .f32, .f64 => unreachable, |
| | 453 | }, |
| | 454 | } |
| | 455 | } |
| | 456 | |
| | 457 | test "Wasm - buildOpcode" { |
| | 458 | // Make sure buildOpcode is referenced, and test some examples |
| | 459 | const i32_const = buildOpcode(.{ .op = .@"const", .valtype1 = .i32 }); |
| | 460 | const end = buildOpcode(.{ .op = .end }); |
| | 461 | const local_get = buildOpcode(.{ .op = .local_get }); |
| | 462 | const i64_extend32_s = buildOpcode(.{ .op = .extend, .valtype1 = .i64, .width = 32, .signedness = .signed }); |
| | 463 | const f64_reinterpret_i64 = buildOpcode(.{ .op = .reinterpret, .valtype1 = .f64, .valtype2 = .i64 }); |
| | 464 | |
| | 465 | testing.expectEqual(@as(wasm.Opcode, .i32_const), i32_const); |
| | 466 | testing.expectEqual(@as(wasm.Opcode, .end), end); |
| | 467 | testing.expectEqual(@as(wasm.Opcode, .local_get), local_get); |
| | 468 | testing.expectEqual(@as(wasm.Opcode, .i64_extend32_s), i64_extend32_s); |
| | 469 | testing.expectEqual(@as(wasm.Opcode, .f64_reinterpret_i64), f64_reinterpret_i64); |
| | 470 | } |
| | 471 | |
| 32 | /// Hashmap to store generated `WValue` for each `Inst` | 472 | /// Hashmap to store generated `WValue` for each `Inst` |
| 33 | pub const ValueTable = std.AutoHashMapUnmanaged(*Inst, WValue); | 473 | pub const ValueTable = std.AutoHashMapUnmanaged(*Inst, WValue); |
| 34 | | 474 | |