TwoHalves & trying to get cowsay to compile again

This commit is contained in:
losfair
2019-07-25 02:44:28 +08:00
parent 0133b92bec
commit cc4f0e31a6
4 changed files with 170 additions and 39 deletions

View File

@ -1,4 +1,4 @@
use super::stackmap::{self, StackmapRegistry};
use super::stackmap::{self, StackmapRegistry, StkMapRecord};
use crate::intrinsics::Intrinsics;
use inkwell::{
memory_buffer::MemoryBuffer,
@ -18,6 +18,7 @@ use std::{
ptr::{self, NonNull},
slice, str,
sync::{Arc, Once},
collections::BTreeMap,
};
use wasmer_runtime_core::{
backend::{
@ -304,7 +305,7 @@ impl LLVMBackend {
};
if raw_stackmap.len() > 0 {
let map = stackmap::StackMap::parse(raw_stackmap).unwrap();
eprintln!("{:?}", map);
println!("{:?}", map);
let (code_ptr, code_size) = unsafe {
(
@ -316,10 +317,16 @@ impl LLVMBackend {
local_functions: Default::default(),
total_size: code_size,
};
let mut map_records: BTreeMap<usize, &StkMapRecord> = BTreeMap::new();
for r in &map.stk_map_records {
map_records.insert(r.patchpoint_id as usize, r);
}
let mut map_record_idx: usize = 0;
for size_record in &map.stk_size_records {
for _ in 0..size_record.record_count {
let map_record = &map.stk_map_records[map_record_idx];
for _ in 0..size_record.record_count as usize {
let map_record = map_records.get(&map_record_idx).expect("map record not found");
let map_entry = &stackmaps.entries[map_record_idx];
assert_eq!(map_record.patchpoint_id, map_record_idx as u64);
map_record_idx += 1;

View File

@ -447,6 +447,26 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
let ctx = CtxType::new(module_info, function, cache_builder);
self.ctx = Some(ctx);
{
let mut state = &mut self.state;
let builder = self.builder.as_ref().unwrap();
let intrinsics = self.intrinsics.as_ref().unwrap();
let mut stackmaps = self.stackmaps.borrow_mut();
emit_stack_map(
&intrinsics,
&builder,
self.index,
&mut *stackmaps,
StackmapEntryKind::FunctionHeader,
&self.locals,
&state,
::std::usize::MAX,
);
}
Ok(())
}
@ -2582,20 +2602,6 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
let local_func_index = self.functions.len();
{
let mut stackmaps = self.stackmaps.borrow_mut();
emit_stack_map(
&intrinsics,
&builder,
local_func_index,
&mut *stackmaps,
StackmapEntryKind::FunctionHeader,
&locals,
&state,
::std::usize::MAX,
);
}
let code = LLVMFunctionCodeGenerator {
state,
context: Some(context),

View File

@ -74,7 +74,7 @@ impl StackmapEntry {
map_record: &StkMapRecord,
msm: &mut ModuleStateMap,
) {
#[derive(Copy, Clone, Debug)]
#[derive(Clone, Debug)]
enum RuntimeOrConstant {
Runtime(MachineValue),
Constant(u64),
@ -88,10 +88,9 @@ impl StackmapEntry {
});
assert_eq!(self.value_semantics.len(), map_record.locations.len());
assert!(size_record.stack_size % 8 == 0);
//assert!(size_record.stack_size % 8 == 0); // is this also aligned to 16 bytes?
let mut machine_stack_layout: Vec<MachineValue> =
vec![MachineValue::Undefined; (size_record.stack_size as usize) / 8];
let mut machine_stack_half_layout: Vec<MachineValue> = Vec::new();
let mut regs: Vec<(RegisterIndex, MachineValue)> = vec![];
let mut stack_constants: HashMap<usize, u64> = HashMap::new();
@ -145,30 +144,38 @@ impl StackmapEntry {
}
LocationType::Direct => match mv {
MachineValue::WasmLocal(_) => {
assert_eq!(loc.location_size, 8);
assert!(loc.offset_or_small_constant < 0);
assert_eq!(loc.location_size, 8); // the pointer itself
assert!(
X64Register::from_dwarf_regnum(loc.dwarf_regnum).unwrap()
== X64Register::GPR(GPR::RBP)
);
let stack_offset = ((-loc.offset_or_small_constant) % 8) as usize;
assert!(stack_offset > 0 && stack_offset <= machine_stack_layout.len());
machine_stack_layout[stack_offset - 1] = mv;
if loc.offset_or_small_constant >= 0 {
eprintln!("XXX: {}", loc.offset_or_small_constant);
}
assert!(loc.offset_or_small_constant < 0);
let stack_offset = ((-loc.offset_or_small_constant) / 4) as usize;
while stack_offset > machine_stack_half_layout.len() {
machine_stack_half_layout.push(MachineValue::Undefined);
}
assert!(stack_offset > 0 && stack_offset <= machine_stack_half_layout.len());
machine_stack_half_layout[stack_offset - 1] = mv;
}
_ => unreachable!(
"Direct location type is not expected for values other than local"
),
},
LocationType::Indirect => {
assert_eq!(loc.location_size, 8);
assert!(loc.offset_or_small_constant < 0);
assert!(
X64Register::from_dwarf_regnum(loc.dwarf_regnum).unwrap()
== X64Register::GPR(GPR::RBP)
);
let stack_offset = ((-loc.offset_or_small_constant) % 8) as usize;
assert!(stack_offset > 0 && stack_offset <= machine_stack_layout.len());
machine_stack_layout[stack_offset - 1] = mv;
let stack_offset = ((-loc.offset_or_small_constant) / 4) as usize;
while stack_offset > machine_stack_half_layout.len() {
machine_stack_half_layout.push(MachineValue::Undefined);
}
assert!(stack_offset > 0 && stack_offset <= machine_stack_half_layout.len());
machine_stack_half_layout[stack_offset - 1] = mv;
}
}
}
@ -176,6 +183,26 @@ impl StackmapEntry {
assert_eq!(wasm_stack.len(), self.stack_count);
assert_eq!(wasm_locals.len(), self.local_count);
if machine_stack_half_layout.len() % 2 != 0 {
machine_stack_half_layout.push(MachineValue::Undefined);
}
let mut machine_stack_layout: Vec<MachineValue> = Vec::with_capacity(machine_stack_half_layout.len() / 2);
for i in 0..machine_stack_half_layout.len() / 2 {
let left = &machine_stack_half_layout[i * 2];
let right = &machine_stack_half_layout[i * 2 + 1];
let only_left = match *right {
MachineValue::Undefined => true,
_ => false,
};
if only_left {
machine_stack_layout.push(left.clone());
} else {
machine_stack_layout.push(MachineValue::TwoHalves(Box::new((right.clone(), left.clone()))));
}
}
let diff = MachineStateDiff {
last: None,
stack_push: machine_stack_layout,