| ... | @@ -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 | |
| ... | @@ -58,6 +498,8 @@ pub const Context = struct { | ... | @@ -58,6 +498,8 @@ pub const Context = struct { |
| 58 | /// List of all locals' types generated throughout this declaration | 498 | /// List of all locals' types generated throughout this declaration |
| 59 | /// used to emit locals count at start of 'code' section. | 499 | /// used to emit locals count at start of 'code' section. |
| 60 | locals: std.ArrayListUnmanaged(u8), | 500 | locals: std.ArrayListUnmanaged(u8), |
| | 501 | /// The Target we're emitting (used to call intInfo) |
| | 502 | target: std.Target, |
| 61 | | 503 | |
| 62 | const InnerError = error{ | 504 | const InnerError = error{ |
| 63 | OutOfMemory, | 505 | OutOfMemory, |
| ... | @@ -89,17 +531,22 @@ pub const Context = struct { | ... | @@ -89,17 +531,22 @@ pub const Context = struct { |
| 89 | return self.values.get(inst).?; // Instruction does not dominate all uses! | 531 | return self.values.get(inst).?; // Instruction does not dominate all uses! |
| 90 | } | 532 | } |
| 91 | | 533 | |
| 92 | /// Using a given `Type`, returns the corresponding wasm value type | 534 | /// Using a given `Type`, returns the corresponding wasm Valtype |
| 93 | fn genValtype(self: *Context, src: LazySrcLoc, ty: Type) InnerError!u8 { | 535 | fn typeToValtype(self: *Context, src: LazySrcLoc, ty: Type) InnerError!wasm.Valtype { |
| 94 | return switch (ty.tag()) { | 536 | return switch (ty.tag()) { |
| 95 | .f32 => wasm.valtype(.f32), | 537 | .f32 => .f32, |
| 96 | .f64 => wasm.valtype(.f64), | 538 | .f64 => .f64, |
| 97 | .u32, .i32, .bool => wasm.valtype(.i32), | 539 | .u32, .i32, .bool => .i32, |
| 98 | .u64, .i64 => wasm.valtype(.i64), | 540 | .u64, .i64 => .i64, |
| 99 | else => self.fail(src, "TODO - Wasm genValtype for type '{s}'", .{ty.tag()}), | 541 | else => self.fail(src, "TODO - Wasm valtype for type '{s}'", .{ty.tag()}), |
| 100 | }; | 542 | }; |
| 101 | } | 543 | } |
| 102 | | 544 | |
| | 545 | /// Using a given `Type`, returns the byte representation of its wasm value type |
| | 546 | fn genValtype(self: *Context, src: LazySrcLoc, ty: Type) InnerError!u8 { |
| | 547 | return wasm.valtype(try self.typeToValtype(src, ty)); |
| | 548 | } |
| | 549 | |
| 103 | /// Using a given `Type`, returns the corresponding wasm value type | 550 | /// Using a given `Type`, returns the corresponding wasm value type |
| 104 | /// Differently from `genValtype` this also allows `void` to create a block | 551 | /// Differently from `genValtype` this also allows `void` to create a block |
| 105 | /// with no return type | 552 | /// with no return type |
| ... | @@ -203,7 +650,7 @@ pub const Context = struct { | ... | @@ -203,7 +650,7 @@ pub const Context = struct { |
| 203 | | 650 | |
| 204 | fn genInst(self: *Context, inst: *Inst) InnerError!WValue { | 651 | fn genInst(self: *Context, inst: *Inst) InnerError!WValue { |
| 205 | return switch (inst.tag) { | 652 | return switch (inst.tag) { |
| 206 | .add => self.genAdd(inst.castTag(.add).?), | 653 | .add => self.genBinOp(inst.castTag(.add).?, .add), |
| 207 | .alloc => self.genAlloc(inst.castTag(.alloc).?), | 654 | .alloc => self.genAlloc(inst.castTag(.alloc).?), |
| 208 | .arg => self.genArg(inst.castTag(.arg).?), | 655 | .arg => self.genArg(inst.castTag(.arg).?), |
| 209 | .block => self.genBlock(inst.castTag(.block).?), | 656 | .block => self.genBlock(inst.castTag(.block).?), |
| ... | @@ -221,10 +668,12 @@ pub const Context = struct { | ... | @@ -221,10 +668,12 @@ pub const Context = struct { |
| 221 | .dbg_stmt => WValue.none, | 668 | .dbg_stmt => WValue.none, |
| 222 | .load => self.genLoad(inst.castTag(.load).?), | 669 | .load => self.genLoad(inst.castTag(.load).?), |
| 223 | .loop => self.genLoop(inst.castTag(.loop).?), | 670 | .loop => self.genLoop(inst.castTag(.loop).?), |
| | 671 | .mul => self.genBinOp(inst.castTag(.mul).?, .mul), |
| 224 | .not => self.genNot(inst.castTag(.not).?), | 672 | .not => self.genNot(inst.castTag(.not).?), |
| 225 | .ret => self.genRet(inst.castTag(.ret).?), | 673 | .ret => self.genRet(inst.castTag(.ret).?), |
| 226 | .retvoid => WValue.none, | 674 | .retvoid => WValue.none, |
| 227 | .store => self.genStore(inst.castTag(.store).?), | 675 | .store => self.genStore(inst.castTag(.store).?), |
| | 676 | .sub => self.genBinOp(inst.castTag(.sub).?, .sub), |
| 228 | .unreach => self.genUnreachable(inst.castTag(.unreach).?), | 677 | .unreach => self.genUnreachable(inst.castTag(.unreach).?), |
| 229 | else => self.fail(inst.src, "TODO: Implement wasm inst: {s}", .{inst.tag}), | 678 | else => self.fail(inst.src, "TODO: Implement wasm inst: {s}", .{inst.tag}), |
| 230 | }; | 679 | }; |
| ... | @@ -305,56 +754,59 @@ pub const Context = struct { | ... | @@ -305,56 +754,59 @@ pub const Context = struct { |
| 305 | return WValue{ .local = self.local_index }; | 754 | return WValue{ .local = self.local_index }; |
| 306 | } | 755 | } |
| 307 | | 756 | |
| 308 | fn genAdd(self: *Context, inst: *Inst.BinOp) InnerError!WValue { | 757 | fn genBinOp(self: *Context, inst: *Inst.BinOp, op: Op) InnerError!WValue { |
| 309 | const lhs = self.resolveInst(inst.lhs); | 758 | const lhs = self.resolveInst(inst.lhs); |
| 310 | const rhs = self.resolveInst(inst.rhs); | 759 | const rhs = self.resolveInst(inst.rhs); |
| 311 | | 760 | |
| 312 | try self.emitWValue(lhs); | 761 | try self.emitWValue(lhs); |
| 313 | try self.emitWValue(rhs); | 762 | try self.emitWValue(rhs); |
| 314 | | 763 | |
| 315 | const opcode: wasm.Opcode = switch (inst.base.ty.tag()) { | 764 | const opcode: wasm.Opcode = buildOpcode(.{ |
| 316 | .u32, .i32 => .i32_add, | 765 | .op = op, |
| 317 | .u64, .i64 => .i64_add, | 766 | .valtype1 = try self.typeToValtype(inst.base.src, inst.base.ty), |
| 318 | .f32 => .f32_add, | 767 | }); |
| 319 | .f64 => .f64_add, | | |
| 320 | else => return self.fail(inst.base.src, "TODO - Implement wasm genAdd for type '{s}'", .{inst.base.ty.tag()}), | | |
| 321 | }; | | |
| 322 | | | |
| 323 | try self.code.append(wasm.opcode(opcode)); | 768 | try self.code.append(wasm.opcode(opcode)); |
| 324 | return .none; | 769 | return .none; |
| 325 | } | 770 | } |
| 326 | | 771 | |
| 327 | fn emitConstant(self: *Context, inst: *Inst.Constant) InnerError!void { | 772 | fn emitConstant(self: *Context, inst: *Inst.Constant) InnerError!void { |
| 328 | const writer = self.code.writer(); | 773 | const writer = self.code.writer(); |
| 329 | switch (inst.base.ty.tag()) { | 774 | switch (inst.base.ty.zigTypeTag()) { |
| 330 | .u32 => { | 775 | .Int => { |
| 331 | try writer.writeByte(wasm.opcode(.i32_const)); | 776 | // write opcode |
| 332 | try leb.writeILEB128(writer, inst.val.toUnsignedInt()); | 777 | const opcode: wasm.Opcode = buildOpcode(.{ |
| | 778 | .op = .@"const", |
| | 779 | .valtype1 = try self.typeToValtype(inst.base.src, inst.base.ty), |
| | 780 | }); |
| | 781 | try writer.writeByte(wasm.opcode(opcode)); |
| | 782 | // write constant |
| | 783 | switch (inst.base.ty.intInfo(self.target).signedness) { |
| | 784 | .signed => try leb.writeILEB128(writer, inst.val.toSignedInt()), |
| | 785 | .unsigned => try leb.writeILEB128(writer, inst.val.toUnsignedInt()), |
| | 786 | } |
| 333 | }, | 787 | }, |
| 334 | .i32, .bool => { | 788 | .Bool => { |
| | 789 | // write opcode |
| 335 | try writer.writeByte(wasm.opcode(.i32_const)); | 790 | try writer.writeByte(wasm.opcode(.i32_const)); |
| | 791 | // write constant |
| 336 | try leb.writeILEB128(writer, inst.val.toSignedInt()); | 792 | try leb.writeILEB128(writer, inst.val.toSignedInt()); |
| 337 | }, | 793 | }, |
| 338 | .u64 => { | 794 | .Float => { |
| 339 | try writer.writeByte(wasm.opcode(.i64_const)); | 795 | // write opcode |
| 340 | try leb.writeILEB128(writer, inst.val.toUnsignedInt()); | 796 | const opcode: wasm.Opcode = buildOpcode(.{ |
| | 797 | .op = .@"const", |
| | 798 | .valtype1 = try self.typeToValtype(inst.base.src, inst.base.ty), |
| | 799 | }); |
| | 800 | try writer.writeByte(wasm.opcode(opcode)); |
| | 801 | // write constant |
| | 802 | switch (inst.base.ty.floatBits(self.target)) { |
| | 803 | 0...32 => try writer.writeIntLittle(u32, @bitCast(u32, inst.val.toFloat(f32))), |
| | 804 | 64 => try writer.writeIntLittle(u64, @bitCast(u64, inst.val.toFloat(f64))), |
| | 805 | else => |bits| return self.fail(inst.base.src, "Wasm TODO: emitConstant for float with {d} bits", .{bits}), |
| | 806 | } |
| 341 | }, | 807 | }, |
| 342 | .i64 => { | 808 | .Void => {}, |
| 343 | try writer.writeByte(wasm.opcode(.i64_const)); | 809 | else => |ty| return self.fail(inst.base.src, "Wasm TODO: emitConstant for zigTypeTag {s}", .{ty}), |
| 344 | try leb.writeILEB128(writer, inst.val.toSignedInt()); | | |
| 345 | }, | | |
| 346 | .f32 => { | | |
| 347 | try writer.writeByte(wasm.opcode(.f32_const)); | | |
| 348 | // TODO: enforce LE byte order | | |
| 349 | try writer.writeAll(mem.asBytes(&inst.val.toFloat(f32))); | | |
| 350 | }, | | |
| 351 | .f64 => { | | |
| 352 | try writer.writeByte(wasm.opcode(.f64_const)); | | |
| 353 | // TODO: enforce LE byte order | | |
| 354 | try writer.writeAll(mem.asBytes(&inst.val.toFloat(f64))); | | |
| 355 | }, | | |
| 356 | .void => {}, | | |
| 357 | else => |ty| return self.fail(inst.base.src, "Wasm TODO: emitConstant for type {s}", .{ty}), | | |
| 358 | } | 810 | } |
| 359 | } | 811 | } |
| 360 | | 812 | |
| ... | @@ -455,62 +907,18 @@ pub const Context = struct { | ... | @@ -455,62 +907,18 @@ pub const Context = struct { |
| 455 | try self.emitWValue(lhs); | 907 | try self.emitWValue(lhs); |
| 456 | try self.emitWValue(rhs); | 908 | try self.emitWValue(rhs); |
| 457 | | 909 | |
| 458 | const opcode_maybe: ?wasm.Opcode = switch (op) { | 910 | const opcode: wasm.Opcode = buildOpcode(.{ |
| 459 | .lt => @as(?wasm.Opcode, switch (ty) { | 911 | .valtype1 = try self.typeToValtype(inst.base.src, inst.lhs.ty), |
| 460 | .i32 => .i32_lt_s, | 912 | .op = switch (op) { |
| 461 | .u32 => .i32_lt_u, | 913 | .lt => .lt, |
| 462 | .i64 => .i64_lt_s, | 914 | .lte => .le, |
| 463 | .u64 => .i64_lt_u, | 915 | .eq => .eq, |
| 464 | .f32 => .f32_lt, | 916 | .neq => .ne, |
| 465 | .f64 => .f64_lt, | 917 | .gte => .ge, |
| 466 | else => null, | 918 | .gt => .gt, |
| 467 | }), | 919 | }, |
| 468 | .lte => @as(?wasm.Opcode, switch (ty) { | 920 | .signedness = inst.lhs.ty.intInfo(self.target).signedness, |
| 469 | .i32 => .i32_le_s, | 921 | }); |
| 470 | .u32 => .i32_le_u, | | |
| 471 | .i64 => .i64_le_s, | | |
| 472 | .u64 => .i64_le_u, | | |
| 473 | .f32 => .f32_le, | | |
| 474 | .f64 => .f64_le, | | |
| 475 | else => null, | | |
| 476 | }), | | |
| 477 | .eq => @as(?wasm.Opcode, switch (ty) { | | |
| 478 | .i32, .u32 => .i32_eq, | | |
| 479 | .i64, .u64 => .i64_eq, | | |
| 480 | .f32 => .f32_eq, | | |
| 481 | .f64 => .f64_eq, | | |
| 482 | else => null, | | |
| 483 | }), | | |
| 484 | .gte => @as(?wasm.Opcode, switch (ty) { | | |
| 485 | .i32 => .i32_ge_s, | | |
| 486 | .u32 => .i32_ge_u, | | |
| 487 | .i64 => .i64_ge_s, | | |
| 488 | .u64 => .i64_ge_u, | | |
| 489 | .f32 => .f32_ge, | | |
| 490 | .f64 => .f64_ge, | | |
| 491 | else => null, | | |
| 492 | }), | | |
| 493 | .gt => @as(?wasm.Opcode, switch (ty) { | | |
| 494 | .i32 => .i32_gt_s, | | |
| 495 | .u32 => .i32_gt_u, | | |
| 496 | .i64 => .i64_gt_s, | | |
| 497 | .u64 => .i64_gt_u, | | |
| 498 | .f32 => .f32_gt, | | |
| 499 | .f64 => .f64_gt, | | |
| 500 | else => null, | | |
| 501 | }), | | |
| 502 | .neq => @as(?wasm.Opcode, switch (ty) { | | |
| 503 | .i32, .u32 => .i32_ne, | | |
| 504 | .i64, .u64 => .i64_ne, | | |
| 505 | .f32 => .f32_ne, | | |
| 506 | .f64 => .f64_ne, | | |
| 507 | else => null, | | |
| 508 | }), | | |
| 509 | }; | | |
| 510 | | | |
| 511 | const opcode = opcode_maybe orelse | | |
| 512 | return self.fail(inst.base.src, "TODO - Wasm genCmp for type '{s}' and operator '{s}'", .{ ty, @tagName(op) }); | | |
| 513 | | | |
| 514 | try self.code.append(wasm.opcode(opcode)); | 922 | try self.code.append(wasm.opcode(opcode)); |
| 515 | return WValue{ .code_offset = offset }; | 923 | return WValue{ .code_offset = offset }; |
| 516 | } | 924 | } |