1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
//! Traits that defines the injection behaviour for wasm opcodes
// note: this should be implemented by FunctionBuilder, ModuleIterator, and ComponentIterator
// note that the location of the injection is handled specific implementation
// for iterators, we inject at the location the iterator is pointing at (curr_loc)
// for FunctionBuilder, we inject at the end of the function
use crate::ir::id::{FunctionID, GlobalID, LocalID};
use crate::ir::types::{BlockType, FuncInstrMode, InstrumentationMode};
use crate::Location;
use wasmparser::MemArg;
use wasmparser::Operator;
/// Defines instrumentation behaviour
pub trait Instrumenter<'a> {
/// Get the InstrumentType of the current location
fn curr_instrument_mode(&self) -> &Option<InstrumentationMode>;
/// Sets the type of Instrumentation Type of the specified location
fn set_instrument_mode_at(&mut self, mode: InstrumentationMode, loc: Location);
/// Get the InstrumentType of the current function
fn curr_func_instrument_mode(&self) -> &Option<FuncInstrMode>;
/// Sets the type of Instrumentation Type of the current function
fn set_func_instrument_mode(&mut self, mode: FuncInstrMode);
// ==== FUNC INSTR INJECTION ====
/// Mark the current function to InstrumentFuncEntry
fn func_entry(&mut self) -> &mut Self {
self.set_func_instrument_mode(FuncInstrMode::Entry);
self
}
/// Mark the current function to InstrumentFuncExit
fn func_exit(&mut self) -> &mut Self {
self.set_func_instrument_mode(FuncInstrMode::Exit);
self
}
// ==== INSTR INJECTION ====
/// Clears the instruction at a given Location
fn clear_instr_at(&mut self, loc: Location, mode: InstrumentationMode);
/// Splice a new instruction into a specific location
fn add_instr_at(&mut self, loc: Location, instr: Operator<'a>);
/// Injects an Instruction with InstrumentationMode `Before` at a given location
fn before_at(&mut self, loc: Location) -> &mut Self {
self.set_instrument_mode_at(InstrumentationMode::Before, loc);
self
}
/// Injects an Instruction with InstrumentationMode `After` at a given location
fn after_at(&mut self, loc: Location) -> &mut Self {
self.set_instrument_mode_at(InstrumentationMode::After, loc);
self
}
/// Injects an Instruction with InstrumentationMode `Alternate` at a given location
fn alternate_at(&mut self, loc: Location) -> &mut Self {
self.set_instrument_mode_at(InstrumentationMode::Alternate, loc);
self
}
/// Injects an empty InstrumentationMode `Alternate` at a given location
fn empty_alternate_at(&mut self, loc: Location) -> &mut Self;
/// Injects a Semantic After at a given location
fn semantic_after_at(&mut self, loc: Location) -> &mut Self {
self.set_instrument_mode_at(InstrumentationMode::SemanticAfter, loc);
self
}
/// Injects a block entry at a given location
fn block_entry_at(&mut self, loc: Location) -> &mut Self {
self.set_instrument_mode_at(InstrumentationMode::BlockEntry, loc);
self
}
/// Injects a block exit at a given location
fn block_exit_at(&mut self, loc: Location) -> &mut Self {
self.set_instrument_mode_at(InstrumentationMode::BlockExit, loc);
self
}
/// Injects a block alternate at a given location
fn block_alt_at(&mut self, loc: Location) -> &mut Self {
self.set_instrument_mode_at(InstrumentationMode::BlockAlt, loc);
self
}
/// Injects an empty block alternate at a given location
fn empty_block_alt_at(&mut self, loc: Location) -> &mut Self;
/// Get the instruction injected at index idx
fn get_injected_val(&self, idx: usize) -> &Operator;
}
/// Defines Injection behaviour at the current location of the Iterator
pub trait Inject<'a> {
/// Inject an operator at the current location
fn inject(&mut self, instr: Operator<'a>);
/// Inject multiple operators at the current location
fn inject_all(&mut self, instrs: &[Operator<'a>]) -> &mut Self {
instrs.iter().for_each(|instr| {
self.inject(instr.to_owned());
});
self
}
}
/// Defines Injection Behaviour at a given location
pub trait InjectAt<'a> {
/// Inject an Instruction at a given Location with a given `InstrumentationMode`
fn inject_at(&mut self, idx: usize, mode: InstrumentationMode, instr: Operator<'a>);
}
#[allow(dead_code)]
/// Defines injection behaviour. Takes a [`wasmparser::Operator`] and instructions are defined [here].
///
/// [`wasmparser::Operator`]: https://docs.rs/wasmparser/latest/wasmparser/enum.Operator.html
/// [here]: https://webassembly.github.io/spec/core/binary/instructions.html
pub trait Opcode<'a>: Inject<'a> {
// Control Flow
/// Inject a call instruction
fn call(&mut self, idx: FunctionID) -> &mut Self {
self.inject(Operator::Call {
function_index: *idx,
});
self
}
/// Inject a return statement
fn return_stmt(&mut self) -> &mut Self {
self.inject(Operator::Return);
self
}
/// Inject a no op instruction
fn nop(&mut self) -> &mut Self {
self.inject(Operator::Nop);
self
}
/// Inject an unreachable instruction
fn unreachable(&mut self) -> &mut Self {
self.inject(Operator::Unreachable);
self
}
/// Inject an if statement
fn if_stmt(&mut self, block_type: BlockType) -> &mut Self {
self.inject(Operator::If {
blockty: wasmparser::BlockType::from(block_type),
});
self
}
/// Inject an else statement
fn else_stmt(&mut self) -> &mut Self {
self.inject(Operator::Else);
self
}
/// Inject an end statement. Indicates the end of the current scope
fn end(&mut self) -> &mut Self {
self.inject(Operator::End);
self
}
/// Inject a block statement. Indicates the start of a block
fn block(&mut self, block_type: BlockType) -> &mut Self {
self.inject(Operator::Block {
blockty: wasmparser::BlockType::from(block_type),
});
self
}
/// Inject a loop statement
fn loop_stmt(&mut self, block_type: BlockType) -> &mut Self {
self.inject(Operator::Loop {
blockty: wasmparser::BlockType::from(block_type),
});
self
}
/// Inject a break statement
fn br(&mut self, relative_depth: u32) -> &mut Self {
self.inject(Operator::Br { relative_depth });
self
}
/// Inject a conditional break statement
fn br_if(&mut self, relative_depth: u32) -> &mut Self {
self.inject(Operator::BrIf { relative_depth });
self
}
// Numerics
/// Inject a local.get
fn local_get(&mut self, idx: LocalID) -> &mut Self {
self.inject(Operator::LocalGet { local_index: *idx });
self
}
/// Inject a local.set
fn local_set(&mut self, idx: LocalID) -> &mut Self {
self.inject(Operator::LocalSet { local_index: *idx });
self
}
fn local_tee(&mut self, idx: LocalID) -> &mut Self {
self.inject(Operator::LocalTee { local_index: *idx });
self
}
// Integers
/// Inject an i32.const instruction
fn i32_const(&mut self, value: i32) -> &mut Self {
self.inject(Operator::I32Const { value });
self
}
/// Inject an i32.add instruction
fn i32_add(&mut self) -> &mut Self {
self.inject(Operator::I32Add);
self
}
/// Inject an i32.sub instruction
fn i32_sub(&mut self) -> &mut Self {
self.inject(Operator::I32Sub);
self
}
/// Inject an i32.mul instruction
fn i32_mul(&mut self) -> &mut Self {
self.inject(Operator::I32Mul);
self
}
/// Inject an i32.divs instruction
fn i32_div_signed(&mut self) -> &mut Self {
self.inject(Operator::I32DivS);
self
}
/// Inject an i32.divu instruction
fn i32_div_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I32DivU);
self
}
/// Inject an i32.remu instruction
fn i32_rem_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I32RemU);
self
}
/// Inject an i32.rems instruction
fn i32_rem_signed(&mut self) -> &mut Self {
self.inject(Operator::I32RemS);
self
}
/// Inject an i32.and instruction
fn i32_and(&mut self) -> &mut Self {
self.inject(Operator::I32And);
self
}
/// Inject an i32.or instruction
fn i32_or(&mut self) -> &mut Self {
self.inject(Operator::I32Or);
self
}
/// Inject an i32.xor instruction
fn i32_xor(&mut self) -> &mut Self {
self.inject(Operator::I32Xor);
self
}
/// Inject an i32.shl instruction
fn i32_shl(&mut self) -> &mut Self {
self.inject(Operator::I32Shl);
self
}
/// Inject an i32.shrs instruction
fn i32_shr_signed(&mut self) -> &mut Self {
self.inject(Operator::I32ShrS);
self
}
/// Inject an i32.shru instruction
fn i32_shr_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I32ShrU);
self
}
/// Inject an i32.rotl instruction
fn i32_rotl(&mut self) -> &mut Self {
self.inject(Operator::I32Rotl);
self
}
/// Inject and i32.rotr instruction
fn i32_rotr(&mut self) -> &mut Self {
self.inject(Operator::I32Rotr);
self
}
/// Inject an i32.eq instruction
fn i32_eq(&mut self) -> &mut Self {
self.inject(Operator::I32Eq);
self
}
/// Inject an i32.eqz instruction
fn i32_eqz(&mut self) -> &mut Self {
self.inject(Operator::I32Eqz);
self
}
/// Inject an i32.ne instruction
fn i32_ne(&mut self) -> &mut Self {
self.inject(Operator::I32Ne);
self
}
/// Inject an i32.ltu instruction
fn i32_lt_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I32LtU);
self
}
/// Inject an i32.lts instruction
fn i32_lt_signed(&mut self) -> &mut Self {
self.inject(Operator::I32LtS);
self
}
/// Inject an i32.gtu instruction
fn i32_gt_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I32GtU);
self
}
/// Inject an i32.gts instruction
fn i32_gt_signed(&mut self) -> &mut Self {
self.inject(Operator::I32GtS);
self
}
/// Inject an i32.lteu instruction
fn i32_lte_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I32LeU);
self
}
/// Inject an i32.ltes instruction
fn i32_lte_signed(&mut self) -> &mut Self {
self.inject(Operator::I32LeS);
self
}
/// Inject an i32.gteu instruction
fn i32_gte_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I32GeU);
self
}
/// Inject an i32.gtes instruction
fn i32_gte_signed(&mut self) -> &mut Self {
self.inject(Operator::I32GeS);
self
}
fn i32_wrap_i64(&mut self) -> &mut Self {
self.inject(Operator::I32WrapI64);
self
}
/// Inject an i64.const
fn i64_const(&mut self, value: i64) -> &mut Self {
self.inject(Operator::I64Const { value });
self
}
/// Inject an i64.add instruction
fn i64_add(&mut self) -> &mut Self {
self.inject(Operator::I64Add);
self
}
/// Inject an i64.sub instruction
fn i64_sub(&mut self) -> &mut Self {
self.inject(Operator::I64Sub);
self
}
/// Inject an i64.mul instruction
fn i64_mul(&mut self) -> &mut Self {
self.inject(Operator::I64Mul);
self
}
/// Inject an i64.divs instruction
fn i64_div_signed(&mut self) -> &mut Self {
self.inject(Operator::I64DivS);
self
}
/// Inject an i64.divu instruction
fn i64_div_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I64DivU);
self
}
/// Inject an i64.remu instruction
fn i64_rem_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I64RemU);
self
}
/// Inject an i64.rems instruction
fn i64_rem_signed(&mut self) -> &mut Self {
self.inject(Operator::I64RemS);
self
}
/// Inject an i64.and instruction
fn i64_and(&mut self) -> &mut Self {
self.inject(Operator::I64And);
self
}
/// Inject an i64.or instruction
fn i64_or(&mut self) -> &mut Self {
self.inject(Operator::I64Or);
self
}
/// Inject an i64.xor instruction
fn i64_xor(&mut self) -> &mut Self {
self.inject(Operator::I64Xor);
self
}
/// Inject an i64.shl instruction
fn i64_shl(&mut self) -> &mut Self {
self.inject(Operator::I64Shl);
self
}
/// Inject an i64.shrs instruction
fn i64_shr_signed(&mut self) -> &mut Self {
self.inject(Operator::I64ShrS);
self
}
/// Inject an i64.shru instruction
fn i64_shr_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I64ShrU);
self
}
/// Inject an i64.rotl instruction
fn i64_rotl(&mut self) -> &mut Self {
self.inject(Operator::I64Rotl);
self
}
/// Inject an i64.rotr instruction
fn i64_rotr(&mut self) -> &mut Self {
self.inject(Operator::I64Rotr);
self
}
/// Inject an i64.eq instruction
fn i64_eq(&mut self) -> &mut Self {
self.inject(Operator::I64Eq);
self
}
/// Inject an i64.eqz instruction
fn i64_eqz(&mut self) -> &mut Self {
self.inject(Operator::I64Eqz);
self
}
/// Inject an i64.ne instruction
fn i64_ne(&mut self) -> &mut Self {
self.inject(Operator::I64Ne);
self
}
/// Inject an i64.ltu instruction
fn i64_lt_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I64LtU);
self
}
/// Inject an i64.lts instruction
fn i64_lt_signed(&mut self) -> &mut Self {
self.inject(Operator::I64LtS);
self
}
/// Inject an i64.gtu instruction
fn i64_gt_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I64GtU);
self
}
/// Inject an i64.gts instruction
fn i64_gt_signed(&mut self) -> &mut Self {
self.inject(Operator::I64GtS);
self
}
/// Inject an i64.lteu instruction
fn i64_lte_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I64LeU);
self
}
/// Inject an i64.ltes instruction
fn i64_lte_signed(&mut self) -> &mut Self {
self.inject(Operator::I64LeS);
self
}
/// Inject an i64.gteu instruction
fn i64_gte_unsigned(&mut self) -> &mut Self {
self.inject(Operator::I64GeU);
self
}
/// Inject an i64.gtes instruction
fn i64_gte_signed(&mut self) -> &mut Self {
self.inject(Operator::I64GeS);
self
}
fn i64_extend_i32u(&mut self) -> &mut Self {
self.inject(Operator::I64ExtendI32U);
self
}
// Floating point
/// Inject a f32.const instruction
fn f32_const(&mut self, val: f32) -> &mut Self {
self.inject(Operator::F32Const {
value: wasmparser::Ieee32::from(val),
});
self
}
/// Inject a f32.abs instruction
fn f32_abs(&mut self) -> &mut Self {
self.inject(Operator::F32Abs);
self
}
/// Inject a f32.ceil instruction
fn f32_ceil(&mut self) -> &mut Self {
self.inject(Operator::F32Ceil);
self
}
/// Inject a f32.floor instruction
fn f32_floor(&mut self) -> &mut Self {
self.inject(Operator::F32Floor);
self
}
/// Inject a f32.trunc instruction
fn f32_trunc(&mut self) -> &mut Self {
self.inject(Operator::F32Trunc);
self
}
/// Inject a f32.sqrt instruction
fn f32_sqrt(&mut self) -> &mut Self {
self.inject(Operator::F32Sqrt);
self
}
/// Inject a f32.add instruction
fn f32_add(&mut self) -> &mut Self {
self.inject(Operator::F32Add);
self
}
/// Inject a f32.sub instruction
fn f32_sub(&mut self) -> &mut Self {
self.inject(Operator::F32Sub);
self
}
/// Inject a f32.mul instruction
fn f32_mul(&mut self) -> &mut Self {
self.inject(Operator::F32Mul);
self
}
/// Inject a f32.div instruction
fn f32_div(&mut self) -> &mut Self {
self.inject(Operator::F32Div);
self
}
/// Inject a f32.min instruction
fn f32_min(&mut self) -> &mut Self {
self.inject(Operator::F32Min);
self
}
/// Inject a f32.max instruction
fn f32_max(&mut self) -> &mut Self {
self.inject(Operator::F32Max);
self
}
/// Inject a f32.eq instruction
fn f32_eq(&mut self) -> &mut Self {
self.inject(Operator::F32Eq);
self
}
/// Inject a f32.ne instruction
fn f32_ne(&mut self) -> &mut Self {
self.inject(Operator::F32Ne);
self
}
/// Inject a f32.gt instruction
fn f32_gt(&mut self) -> &mut Self {
self.inject(Operator::F32Gt);
self
}
/// Inject a f32.ge instruction
fn f32_ge(&mut self) -> &mut Self {
self.inject(Operator::F32Ge);
self
}
/// Inject a f32.lt instruction
fn f32_lt(&mut self) -> &mut Self {
self.inject(Operator::F32Lt);
self
}
/// Inject a f32.le instruction
fn f32_le(&mut self) -> &mut Self {
self.inject(Operator::F32Le);
self
}
/// Inject a f32_convert_i32s instruction
fn f32_convert_i32s(&mut self) -> &mut Self {
self.inject(Operator::F32ConvertI32S);
self
}
/// Inject a f32_demote_f64 instruction
fn f32_demote_f64(&mut self) -> &mut Self {
self.inject(Operator::F32DemoteF64);
self
}
/// Inject a f64.const instruction
fn f64_const(&mut self, val: f64) -> &mut Self {
self.inject(Operator::F64Const {
value: wasmparser::Ieee64::from(val),
});
self
}
/// Inject a f64.abs instruction
fn f64_abs(&mut self) -> &mut Self {
self.inject(Operator::F64Abs);
self
}
/// Inject a f64.ceil instruction
fn f64_ceil(&mut self) -> &mut Self {
self.inject(Operator::F64Ceil);
self
}
/// Inject a f64.floor instruction
fn f64_floor(&mut self) -> &mut Self {
self.inject(Operator::F64Floor);
self
}
/// Inject a f64.trunc instruction
fn f64_trunc(&mut self) -> &mut Self {
self.inject(Operator::F64Trunc);
self
}
/// Inject a f64.sqrt instruction
fn f64_sqrt(&mut self) -> &mut Self {
self.inject(Operator::F64Sqrt);
self
}
/// Inject a f64.add instruction
fn f64_add(&mut self) -> &mut Self {
self.inject(Operator::F64Add);
self
}
/// Inject a f64.sub instruction
fn f64_sub(&mut self) -> &mut Self {
self.inject(Operator::F64Sub);
self
}
/// Inject a f64.mul instruction
fn f64_mul(&mut self) -> &mut Self {
self.inject(Operator::F64Mul);
self
}
/// Inject a f64.div instruction
fn f64_div(&mut self) -> &mut Self {
self.inject(Operator::F64Div);
self
}
/// Inject a f64.min instruction
fn f64_min(&mut self) -> &mut Self {
self.inject(Operator::F64Min);
self
}
/// Inject a f64.max instruction
fn f64_max(&mut self) -> &mut Self {
self.inject(Operator::F64Max);
self
}
/// Inject a f64.eq instruction
fn f64_eq(&mut self) -> &mut Self {
self.inject(Operator::F64Eq);
self
}
/// Inject a f64.ne instruction
fn f64_ne(&mut self) -> &mut Self {
self.inject(Operator::F64Ne);
self
}
/// Inject a f64.gt instruction
fn f64_gt(&mut self) -> &mut Self {
self.inject(Operator::F64Gt);
self
}
/// Inject a f64.ge instruction
fn f64_ge(&mut self) -> &mut Self {
self.inject(Operator::F64Ge);
self
}
/// Inject a f64.lt instruction
fn f64_lt(&mut self) -> &mut Self {
self.inject(Operator::F64Lt);
self
}
/// Inject a f64.le instruction
fn f64_le(&mut self) -> &mut Self {
self.inject(Operator::F64Le);
self
}
/// Inject a f64_reinterpret_i64 instruction
fn f64_reinterpret_i64(&mut self) -> &mut Self {
self.inject(Operator::F64ReinterpretI64);
self
}
/// Inject a f64_promote_f32 instruction
fn f64_promote_f32(&mut self) -> &mut Self {
self.inject(Operator::F64PromoteF32);
self
}
/// Inject a f64_convert_i32s instruction
fn f64_convert_i32s(&mut self) -> &mut Self {
self.inject(Operator::F64ConvertI32S);
self
}
// Memory Instructions
/// Inject a memory.init instruction
fn memory_init(&mut self, data_index: u32, mem: u32) -> &mut Self {
self.inject(Operator::MemoryInit { data_index, mem });
self
}
/// Inject a memory.size instruction
fn memory_size(&mut self, mem: u32) -> &mut Self {
self.inject(Operator::MemorySize { mem });
self
}
/// Inject a memory.grow instruction
fn memory_grow(&mut self, mem: u32) -> &mut Self {
self.inject(Operator::MemoryGrow { mem });
self
}
/// Inject a memory.fill instruction
fn memory_fill(&mut self, mem: u32) -> &mut Self {
self.inject(Operator::MemoryFill { mem });
self
}
/// Inject a memory.copy instruction
fn memory_copy(&mut self, dst_mem: u32, src_mem: u32) -> &mut Self {
self.inject(Operator::MemoryCopy { dst_mem, src_mem });
self
}
/// Inject a memory.discard instruction
fn memory_discard(&mut self, mem: u32) -> &mut Self {
self.inject(Operator::MemoryDiscard { mem });
self
}
/// Inject a data drop instruction
fn data_drop(&mut self, data_index: u32) -> &mut Self {
self.inject(Operator::DataDrop { data_index });
self
}
// Parametric Instructions
/// Inject a drop instruction
fn drop(&mut self) -> &mut Self {
self.inject(Operator::Drop);
self
}
// Linear Memory Access
// note: walrus does not specify max_align (probably it's the same as align)
/// load 1 byte and sign-extend i8 to i32
fn i32_load8_s(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I32Load8S { memarg });
self
}
/// load 1 byte and zero-extend i8 to i32
fn i32_load8_u(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I32Load8U { memarg });
self
}
/// load 2 bytes and sign-extend i16 to i32
fn i32_load16_s(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I32Load16S { memarg });
self
}
/// load 2 bytes and zero-extend i16 to i32
fn i32_load16_u(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I32Load16U { memarg });
self
}
/// load 4 bytes as i32
fn i32_load(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I32Load { memarg });
self
}
fn i32_store(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I32Store { memarg });
self
}
/// load 1 byte and sign-extend i8 to i64
fn i64_load8_s(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I64Load8S { memarg });
self
}
/// load 1 byte and zero-extend i8 to i64
fn i64_load8_u(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I64Load8U { memarg });
self
}
/// load 2 bytes and sign-extend i16 to i64
fn i64_load16_s(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I64Load16S { memarg });
self
}
/// load 2 bytes and zero-extend i16 to i64
fn i64_load16_u(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I64Load16U { memarg });
self
}
/// load 4 bytes and sign-extend i32 to i64
fn i64_load32_s(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I64Load32S { memarg });
self
}
/// load 4 bytes and zero-extend i32 to i64
fn i64_load32_u(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I64Load32U { memarg });
self
}
/// load 4 bytes as i64
fn i64_load(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I64Load { memarg });
self
}
fn i64_store(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::I64Store { memarg });
self
}
/// load 4 bytes as f32
fn f32_load(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::F32Load { memarg });
self
}
fn f32_store(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::F32Store { memarg });
self
}
/// load 8 bytes as f64
fn f64_load(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::F64Load { memarg });
self
}
/// Inject an f64_store instruction
fn f64_store(&mut self, memarg: MemArg) -> &mut Self {
self.inject(Operator::F64Store { memarg });
self
}
/// Inject a global.get
fn global_get(&mut self, idx: GlobalID) -> &mut Self {
self.inject(Operator::GlobalGet { global_index: *idx });
self
}
/// Inject a global.set
fn global_set(&mut self, idx: GlobalID) -> &mut Self {
self.inject(Operator::GlobalSet { global_index: *idx });
self
}
}
#[allow(dead_code)]
/// Defines injection behaviour. Takes a [`wasmparser::Operator`] and instructions are defined [here].
///
/// [`wasmparser::Operator`]: https://docs.rs/wasmparser/latest/wasmparser/enum.Operator.html
/// [here]: https://webassembly.github.io/spec/core/binary/instructions.html
pub trait MacroOpcode<'a>: Inject<'a> {
/// Helper function to reinterpret an u32 as an i32 and inject an i32.const instruction with that reinterpreted value.
/// (Useful to emitting memory addresses.)
/// We cast using the `as` keyword to accomplish this.
/// See <https://github.com/thesuhas/orca/issues/133> for an explanation.
fn u32_const(&mut self, value: u32) -> &mut Self {
let i32_val = value as i32;
self.inject(Operator::I32Const { value: i32_val });
self
}
/// Helper function to reinterpret an u64 as an i64 and inject an i64.const instruction with that reinterpreted value.
/// (Useful to emitting memory addresses.)
/// We cast using the `as` keyword to accomplish this.
/// See <https://github.com/thesuhas/orca/issues/133> for an explanation.
fn u64_const(&mut self, value: u64) -> &mut Self {
let i64_val = value as i64;
self.inject(Operator::I64Const { value: i64_val });
self
}
}