MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / handle_module

Function handle_module

cranelift/src/compile.rs:78–145  ·  view source on GitHub ↗
(
    options: &Options,
    path: &Path,
    name: &str,
    fisa: FlagsOrIsa,
    module: Option<&mut impl cranelift_module::Module>,
)

Source from the content-addressed store, hash-verified

76}
77
78fn handle_module(
79 options: &Options,
80 path: &Path,
81 name: &str,
82 fisa: FlagsOrIsa,
83 module: Option<&mut impl cranelift_module::Module>,
84) -> Result<()> {
85 let buffer = read_to_string(&path)?;
86 let test_file = parse_test(&buffer, ParseOptions::default())
87 .with_context(|| format!("failed to parse {name}"))?;
88
89 // If we have an isa from the command-line, use that. Otherwise if the
90 // file contains a unique isa, use that.
91 let isa = fisa.isa.or(test_file.isa_spec.unique_isa());
92
93 let isa = match isa {
94 None => anyhow::bail!("compilation requires a target isa"),
95 Some(isa) => isa,
96 };
97
98 for (func, _) in test_file.functions {
99 let mut context = Context::new();
100 context.func = func;
101
102 // Compile and encode the result to machine code.
103 let compiled_code = context
104 .compile(isa, &mut Default::default())
105 .map_err(|err| anyhow::anyhow!("{}", pretty_error(&err.func, err.inner)))?;
106 let code_info = compiled_code.code_info();
107
108 if let Some(&mut ref mut module) = module {
109 let name = context.func.name.to_string();
110 let fid = module.declare_function(
111 &name,
112 cranelift_module::Linkage::Export,
113 &context.func.signature,
114 )?;
115 module.define_function_with_control_plane(
116 fid,
117 &mut context,
118 &mut Default::default(),
119 )?;
120 }
121
122 if options.print {
123 println!("{}", context.func.display());
124 }
125
126 if options.disasm {
127 let result = context.compiled_code().unwrap();
128 print_all(
129 isa,
130 &context.func,
131 context.compiled_code().unwrap().code_buffer(),
132 code_info.total_size,
133 options.print,
134 result.buffer.relocs(),
135 result.buffer.traps(),

Callers 1

runFunction · 0.85

Calls 15

read_to_stringFunction · 0.85
parse_testFunction · 0.85
print_allFunction · 0.85
OkFunction · 0.85
with_contextMethod · 0.80
unique_isaMethod · 0.80
code_infoMethod · 0.80
compiled_codeMethod · 0.80
code_bufferMethod · 0.80
relocsMethod · 0.80
newFunction · 0.50
orMethod · 0.45

Tested by

no test coverage detected