Add hacky reinterpret instructions and pop used arguments off the value stack

This commit is contained in:
Lachlan Sneff
2019-03-04 16:44:46 -08:00
parent 25c0cef5f7
commit 2969409ca7
2 changed files with 136 additions and 47 deletions

View File

@ -581,13 +581,23 @@ fn parse_function(
state.push1(i); state.push1(i);
} }
Operator::F32Const { value } => { Operator::F32Const { value } => {
let f = intrinsics let bits = intrinsics.i32_ty.const_int(value.bits() as u64, false);
.f32_ty let space =
.const_float(f32::from_bits(value.bits()) as f64); builder.build_alloca(intrinsics.f32_ty.as_basic_type_enum(), "const_space");
let i32_space =
builder.build_pointer_cast(space, intrinsics.i32_ptr_ty, "i32_space");
builder.build_store(i32_space, bits);
let f = builder.build_load(space, "f");
state.push1(f); state.push1(f);
} }
Operator::F64Const { value } => { Operator::F64Const { value } => {
let f = intrinsics.f64_ty.const_float(f64::from_bits(value.bits())); let bits = intrinsics.i64_ty.const_int(value.bits(), false);
let space =
builder.build_alloca(intrinsics.f64_ty.as_basic_type_enum(), "const_space");
let i64_space =
builder.build_pointer_cast(space, intrinsics.i64_ptr_ty, "i32_space");
builder.build_store(i64_space, bits);
let f = builder.build_load(space, "f");
state.push1(f); state.push1(f);
} }
@ -683,6 +693,8 @@ fn parse_function(
} }
}; };
state.popn(func_sig.params().len())?;
if let Some(basic_value) = call_site.try_as_basic_value().left() { if let Some(basic_value) = call_site.try_as_basic_value().left() {
match func_sig.returns().len() { match func_sig.returns().len() {
1 => state.push1(basic_value), 1 => state.push1(basic_value),
@ -1521,11 +1533,45 @@ fn parse_function(
builder.build_unsigned_int_to_float(v1, intrinsics.f64_ty, &state.var_name()); builder.build_unsigned_int_to_float(v1, intrinsics.f64_ty, &state.var_name());
state.push1(res); state.push1(res);
} }
Operator::I32ReinterpretF32 Operator::I32ReinterpretF32 => {
| Operator::F32ReinterpretI32 let v = state.pop1()?;
| Operator::I64ReinterpretF64 let space =
| Operator::F64ReinterpretI64 => { builder.build_alloca(intrinsics.i32_ty.as_basic_type_enum(), &state.var_name());
unimplemented!("waiting on better bitcasting support in inkwell") let f32_space =
builder.build_pointer_cast(space, intrinsics.f32_ptr_ty, &state.var_name());
builder.build_store(f32_space, v);
let int = builder.build_load(space, &state.var_name());
state.push1(int);
}
Operator::I64ReinterpretF64 => {
let v = state.pop1()?;
let space =
builder.build_alloca(intrinsics.i64_ty.as_basic_type_enum(), &state.var_name());
let f64_space =
builder.build_pointer_cast(space, intrinsics.f64_ptr_ty, &state.var_name());
builder.build_store(f64_space, v);
let int = builder.build_load(space, &state.var_name());
state.push1(int);
}
Operator::F32ReinterpretI32 => {
let v = state.pop1()?;
let space =
builder.build_alloca(intrinsics.f32_ty.as_basic_type_enum(), &state.var_name());
let i32_space =
builder.build_pointer_cast(space, intrinsics.i32_ptr_ty, &state.var_name());
builder.build_store(i32_space, v);
let f = builder.build_load(space, &state.var_name());
state.push1(f);
}
Operator::F64ReinterpretI64 => {
let v = state.pop1()?;
let space =
builder.build_alloca(intrinsics.f64_ty.as_basic_type_enum(), &state.var_name());
let i64_space =
builder.build_pointer_cast(space, intrinsics.i64_ptr_ty, &state.var_name());
builder.build_store(i64_space, v);
let f = builder.build_load(space, &state.var_name());
state.push1(f);
} }
/*************************** /***************************

View File

@ -4,45 +4,88 @@ use wabt::wat2wasm;
static WAT: &'static str = r#" static WAT: &'static str = r#"
(module (module
(type (;0;) (func)) (type (;0;) (func (param i32 i32)))
(type (;1;) (func)) (type (;1;) (func (param i32 i64)))
(type (;2;) (func)) (type (;2;) (func (param i32) (result i32)))
(type (;3;) (func (result i32))) (type (;3;) (func (param i32) (result i64)))
(type (;4;) (func (result i32))) (type (;4;) (func (param i64) (result i64)))
(type (;5;) (func (param i32) (result i32))) (type (;5;) (func (param f32) (result f32)))
(type (;6;) (func (param i32))) (type (;6;) (func (param f64) (result f64)))
(import "spectest" "print_i32" (func (;0;) (type 6))) (func (;0;) (type 0) (param i32 i32)
(func (;1;) (type 0)) local.get 0
(func (;2;) (type 1)) local.get 1
(func (;3;) (type 4) (result i32) i32.store8
i32.const 13)
(func (;4;) (type 5) (param i32) (result i32)
local.get 0 local.get 0
i32.const 1 i32.const 1
i32.add) i32.add
(func (;5;) (type 5) (param i32) (result i32) local.get 1
i32.const 8
i32.shr_u
i32.store8)
(func (;1;) (type 0) (param i32 i32)
local.get 0
local.get 1
call 0
local.get 0 local.get 0
i32.const 2 i32.const 2
i32.sub) i32.add
(func (;6;) (type 6) (param i32) local.get 1
local.get 0 i32.const 16
i32.shr_u
call 0) call 0)
(export "one" (func 3)) (func (;2;) (type 1) (param i32 i64)
(export "two" (func 4)) local.get 0
(export "three" (func 5)) local.get 1
(export "four" (func 6))) i32.wrap_i64
call 1
local.get 0
i32.const 4
i32.add
local.get 1
i64.const 32
i64.shr_u
i32.wrap_i64
call 1)
(func (;3;) (type 2) (param i32) (result i32)
local.get 0
i32.load8_u
local.get 0
i32.const 1
i32.add
i32.load8_u
i32.const 8
i32.shl
i32.or)
(func (;4;) (type 2) (param i32) (result i32)
local.get 0
call 3
local.get 0
i32.const 2
i32.add
call 3
i32.const 16
i32.shl
i32.or)
(func (;5;) (type 3) (param i32) (result i64)
local.get 0
i64.extend_i32_u
local.get 0
call 4
i64.extend_i32_u
i64.or)
(memory (;0;) 1))
"#; "#;
static WAT2: &'static str = r#" // static WAT2: &'static str = r#"
(module // (module
(type $t0 (func (param i32))) // (type $t0 (func (param i32)))
(type $t1 (func)) // (type $t1 (func))
(func $print_i32 (export "print_i32") (type $t0) (param $lhs i32)) // (func $print_i32 (export "print_i32") (type $t0) (param $lhs i32))
(func $print (export "print") (type $t1)) // (func $print (export "print") (type $t1))
(table $table (export "table") 10 20 anyfunc) // (table $table (export "table") 10 20 anyfunc)
(memory $memory (export "memory") 1 2) // (memory $memory (export "memory") 1 2)
(global $global_i32 (export "global_i32") i32 (i32.const 666))) // (global $global_i32 (export "global_i32") i32 (i32.const 666)))
"#; // "#;
fn get_wasm() -> Vec<u8> { fn get_wasm() -> Vec<u8> {
wat2wasm(WAT).unwrap() wat2wasm(WAT).unwrap()
@ -57,15 +100,15 @@ fn main() -> Result<(), error::Error> {
let module = compile(&wasm)?; let module = compile(&wasm)?;
let import_module = compile(&wat2wasm(WAT2).unwrap())?; // let import_module = compile(&wat2wasm(WAT2).unwrap())?;
let import_instance = import_module.instantiate(&imports! {})?; // let import_instance = import_module.instantiate(&imports! {})?;
let imports = imports! { // let imports = imports! {
"spectest" => import_instance, // "spectest" => import_instance,
}; // };
println!("instantiating"); println!("instantiating");
let instance = module.instantiate(&imports)?; let instance = module.instantiate(&imports! {})?;
let foo = instance.dyn_func("four")?; let foo = instance.dyn_func("four")?;