| 1 | use craterust-analyzer cargo wasmparser 0.239.0 crate/::{BinaryReaderrust-analyzer cargo wasmparser 0.239.0 binary_reader/BinaryReader#, FromReaderrust-analyzer cargo wasmparser 0.239.0 readers/FromReader#, Resultrust-analyzer cargo wasmparser 0.239.0 binary_reader/Result#, SectionLimitedrust-analyzer cargo wasmparser 0.239.0 readers/SectionLimited#}; |
| 2 | |
| 3 | /// A reader for the `metadata.code.branch_hint` custom section. |
| 4 | pub type BranchHintSectionReaderrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHintSectionReader#<'a> = SectionLimitedrust-analyzer cargo wasmparser 0.239.0 readers/SectionLimited#<'a, BranchHintFunctionrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHintFunction#<'a>>; |
| 5 | |
| 6 | /// Branch hints for a single function. |
| 7 | /// |
| 8 | /// Produced from [`BranchHintSectionReader`]. |
| 9 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 10 | pub struct BranchHintFunctionrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHintFunction#<'a> { |
| 11 | /// The function that these branch hints apply to. |
| 12 | pub funcrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHintFunction#func.: u32, |
| 13 | /// The branch hints available for this function. |
| 14 | pub hintsrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHintFunction#hints.: SectionLimitedrust-analyzer cargo wasmparser 0.239.0 readers/SectionLimited#<'a, BranchHintrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHint#>, |
| 15 | } |
| 16 | |
| 17 | impl<'a> FromReaderrust-analyzer cargo wasmparser 0.239.0 readers/FromReader#<'a> for BranchHintFunctionrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHintFunction#<'a> { |
| 18 | fn from_readerrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/impl#[`BranchHintFunction<'a>`][`FromReader<'a>`]from_reader().(reader: &mut BinaryReaderrust-analyzer cargo wasmparser 0.239.0 binary_reader/BinaryReader#<'a>) -> Resultrust-analyzer cargo wasmparser 0.239.0 binary_reader/Result#<Selfrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/impl#[`BranchHintFunction<'a>`][`FromReader<'a>`]> { |
| 19 | let func = reader.read_var_u32rust-analyzer cargo wasmparser 0.239.0 binary_reader/impl#[`BinaryReader<'a>`]read_var_u32().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 20 | // FIXME(#188) ideally wouldn't have to do skips here |
| 21 | let hints = reader.skiprust-analyzer cargo wasmparser 0.239.0 binary_reader/impl#[`BinaryReader<'a>`]skip().(|reader| { |
| 22 | let items_count = reader.read_var_u32rust-analyzer cargo wasmparser 0.239.0 binary_reader/impl#[`BinaryReader<'a>`]read_var_u32().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 23 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#items_count { |
| 24 | reader.readrust-analyzer cargo wasmparser 0.239.0 binary_reader/impl#[`BinaryReader<'a>`]read().::<BranchHintrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHint#>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 25 | } |
| 26 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 27 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 28 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(BranchHintFunctionrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHintFunction# { |
| 29 | funcrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHintFunction#func., |
| 30 | hintsrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHintFunction#hints.: SectionLimitedrust-analyzer cargo wasmparser 0.239.0 readers/SectionLimited#::newrust-analyzer cargo wasmparser 0.239.0 readers/impl#[`SectionLimited<'a, T>`]new().(hints)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 31 | }) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /// A hint for a single branch. |
| 36 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 37 | pub struct BranchHintrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHint# { |
| 38 | /// The byte offset, from the start of the function's body, of where the |
| 39 | /// hinted instruction lives. |
| 40 | pub func_offsetrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHint#func_offset.: u32, |
| 41 | /// Whether or not the branch is hinted to be taken or not. |
| 42 | pub takenrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHint#taken.: bool, |
| 43 | } |
| 44 | |
| 45 | impl<'a> FromReaderrust-analyzer cargo wasmparser 0.239.0 readers/FromReader#<'a> for BranchHintrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHint# { |
| 46 | fn from_readerrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/impl#[BranchHint][`FromReader<'a>`]from_reader().(reader: &mut BinaryReaderrust-analyzer cargo wasmparser 0.239.0 binary_reader/BinaryReader#<'a>) -> Resultrust-analyzer cargo wasmparser 0.239.0 binary_reader/Result#<Selfrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/impl#[BranchHint][`FromReader<'a>`]> { |
| 47 | let func_offset = reader.read_var_u32rust-analyzer cargo wasmparser 0.239.0 binary_reader/impl#[`BinaryReader<'a>`]read_var_u32().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 48 | match reader.read_u8rust-analyzer cargo wasmparser 0.239.0 binary_reader/impl#[`BinaryReader<'a>`]read_u8().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 49 | 1 => {} |
| 50 | n => reader.invalid_leading_byterust-analyzer cargo wasmparser 0.239.0 binary_reader/impl#[`BinaryReader<'a>`]invalid_leading_byte().(n, "invalid branch hint byte")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 51 | } |
| 52 | let taken = match reader.read_u8rust-analyzer cargo wasmparser 0.239.0 binary_reader/impl#[`BinaryReader<'a>`]read_u8().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 53 | 0 => false, |
| 54 | 1 => true, |
| 55 | n => reader.invalid_leading_byterust-analyzer cargo wasmparser 0.239.0 binary_reader/impl#[`BinaryReader<'a>`]invalid_leading_byte().(n, "invalid branch hint taken byte")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 56 | }; |
| 57 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(BranchHintrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHint# { func_offsetrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHint#func_offset., takenrust-analyzer cargo wasmparser 0.239.0 readers/core/branch_hinting/BranchHint#taken. }) |
| 58 | } |
| 59 | } |