forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bffce37
commit b0e0f27
Showing
23 changed files
with
1,493 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ thiserror = "1.0.4" | |
|
||
[features] | ||
all-arch = ["cranelift-codegen/all-arch"] | ||
component-model = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::compiler::Compiler; | ||
use crate::obj::UnwindInfoBuilder; | ||
use crate::CompiledFunction; | ||
use anyhow::Result; | ||
use cranelift_codegen::ir::{InstBuilder, MemFlags}; | ||
use object::write::{Object, StandardSegment, Symbol, SymbolSection}; | ||
use object::{SectionKind, SymbolFlags, SymbolKind, SymbolScope}; | ||
use std::any::Any; | ||
use wasmtime_environ::component::{ | ||
Component, ComponentCompiler, ComponentTypes, LoweredIndex, TrampolineInfo, VMComponentOffsets, | ||
}; | ||
use wasmtime_environ::{EntityRef, PrimaryMap}; | ||
|
||
impl ComponentCompiler for Compiler { | ||
fn compile_lowered_trampoline( | ||
&self, | ||
component: &Component, | ||
index: LoweredIndex, | ||
types: &ComponentTypes, | ||
) -> Result<Box<dyn Any + Send>> { | ||
let ty = component.lowerings[index].canonical_abi; | ||
let ty = &types[ty]; | ||
let offsets = VMComponentOffsets::new(self.isa.pointer_bytes(), component); | ||
let trampoline: CompiledFunction = | ||
self.wasm_to_host_trampoline(ty, false, |builder, vmctx, pointer_type| { | ||
let offset = offsets.lowering_callee(index); | ||
builder.ins().load( | ||
pointer_type, | ||
MemFlags::trusted(), | ||
vmctx, | ||
i32::try_from(offset).unwrap(), | ||
) | ||
})?; | ||
Ok(Box::new(trampoline)) | ||
} | ||
|
||
fn emit_obj( | ||
&self, | ||
trampolines: PrimaryMap<LoweredIndex, Box<dyn Any + Send>>, | ||
obj: &mut Object<'static>, | ||
) -> Result<PrimaryMap<LoweredIndex, TrampolineInfo>> { | ||
let trampolines: PrimaryMap<LoweredIndex, CompiledFunction> = trampolines | ||
.into_iter() | ||
.map(|(_, f)| *f.downcast().unwrap()) | ||
.collect(); | ||
let mut unwind_info = UnwindInfoBuilder::default(); | ||
let text_section = obj.add_section( | ||
obj.segment_name(StandardSegment::Text).to_vec(), | ||
b".text".to_vec(), | ||
SectionKind::Text, | ||
); | ||
let mut ret = PrimaryMap::new(); | ||
for (idx, trampoline) in trampolines.iter() { | ||
// Currently there's no relocation processing here but it's also | ||
// not used by trampolines at this time. | ||
assert!(trampoline.relocations.is_empty()); | ||
|
||
let len = trampoline.body.len() as u64; | ||
let off = obj.append_section_data(text_section, &trampoline.body, 1); | ||
|
||
// Adding a symbol in normal wasm modules is used to resolve | ||
// relocations, specifically in the dwarf section, but here we | ||
// don't technically need symbols. Add it anyway for now as perhaps | ||
// a debugging convenience one day. | ||
obj.add_symbol(Symbol { | ||
name: format!("wasm_lowered_trampoline{}", idx.index()).into_bytes(), | ||
value: off, | ||
size: len, | ||
kind: SymbolKind::Text, | ||
scope: SymbolScope::Compilation, | ||
weak: false, | ||
section: SymbolSection::Section(text_section), | ||
flags: SymbolFlags::None, | ||
}); | ||
|
||
if let Some(info) = &trampoline.unwind_info { | ||
unwind_info.push(off, len, info, |data, align| { | ||
obj.append_section_data(text_section, data, align.into()) | ||
}); | ||
} | ||
|
||
let i = ret.push(TrampolineInfo { | ||
start: u32::try_from(off).unwrap(), | ||
length: u32::try_from(len).unwrap(), | ||
}); | ||
assert_eq!(i, idx); | ||
} | ||
|
||
Ok(ret) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.