wasmer_compiler_cranelift/
address_map.rs

1// This file contains code from external sources.
2// Attributions: https://github.com/wasmerio/wasmer/blob/main/docs/ATTRIBUTIONS.md
3
4use cranelift_codegen::{Context, MachSrcLoc};
5use std::ops::Range;
6use wasmer_compiler::types::address_map::{FunctionAddressMap, InstructionAddressMap};
7use wasmer_types::SourceLoc;
8
9pub fn get_function_address_map(
10    context: &Context,
11    range: Range<usize>,
12    body_len: usize,
13) -> FunctionAddressMap {
14    let mut instructions = Vec::new();
15
16    // New-style backend: we have a `MachCompileResult` that will give us `MachSrcLoc` mapping
17    // tuples.
18    let mcr = context.compiled_code().unwrap();
19    for &MachSrcLoc { start, end, loc } in mcr.buffer.get_srclocs_sorted() {
20        instructions.push(InstructionAddressMap {
21            srcloc: SourceLoc::new(loc.bits()),
22            code_offset: start as usize,
23            code_len: (end - start) as usize,
24        });
25    }
26
27    // Generate artificial srcloc for function start/end to identify boundary
28    // within module. Similar to FuncTranslator::cur_srcloc(): it will wrap around
29    // if byte code is larger than 4 GB.
30    let start_srcloc = SourceLoc::new(range.start as u32);
31    let end_srcloc = SourceLoc::new(range.end as u32);
32
33    FunctionAddressMap {
34        instructions,
35        start_srcloc,
36        end_srcloc,
37        body_offset: 0,
38        body_len,
39    }
40}