Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: WIP attempt at multi-val #94

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/codegen/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub fn compile_block<'a, 'b, 'c>(
let after_bb = f_ctx.generate_block();

// If the loop breaks out, it will go back to the top of the loop
let loop_breakout_target = BreakoutTarget::new_wrapped(inner_bb, None);
let loop_breakout_target = BreakoutTarget::new_wrapped(inner_bb, Vec::new());
breakout_stack.push(loop_breakout_target);

// If the loop terminates, it will come back to our outer basic block
Expand Down
8 changes: 4 additions & 4 deletions src/codegen/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::codegen::type_conversions::wasm_type_to_zeroed_value;

pub struct BreakoutTarget<'a> {
pub bb: &'a BasicBlock,
result_type: Option<TypeOrFuncType>,
result_type: Vec<TypeOrFuncType>,
jumps: Vec<JumpFrom<'a>>,
}

Expand All @@ -26,7 +26,7 @@ pub struct JumpFrom<'a> {
pub type WBreakoutTarget<'a> = Rc<RefCell<BreakoutTarget<'a>>>;

impl<'a> BreakoutTarget<'a> {
pub fn new(bb: &'a BasicBlock, result_type: Option<TypeOrFuncType>) -> BreakoutTarget<'a> {
pub fn new(bb: &'a BasicBlock, result_type: Vec<TypeOrFuncType>) -> BreakoutTarget<'a> {
BreakoutTarget {
bb,
result_type,
Expand All @@ -36,7 +36,7 @@ impl<'a> BreakoutTarget<'a> {

pub fn new_wrapped(
bb: &'a BasicBlock,
result_type: Option<TypeOrFuncType>,
result_type: Vec<TypeOrFuncType>,
) -> WBreakoutTarget<'a> {
let bt = Self::new(bb, result_type);
Rc::new(RefCell::new(bt))
Expand Down Expand Up @@ -135,7 +135,7 @@ impl<'a> BreakoutTarget<'a> {
locals: &[&'b Value],
stack: &[&'b Value],
) {
let res = if self.result_type.is_some() {
let res = if self.result_type.len() > 0 {
Some(
*stack
.last()
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ pub fn compile_function(ctx: &ModuleCtx, f: &ImplementedFunction) {
let termination_block = llvm_f.append("exit");
let root_breakout_target = BreakoutTarget::new_wrapped(
termination_block,
f.get_return_type().map(TypeOrFuncType::Type),
f.get_return_type()
.iter()
.map(|e| TypeOrFuncType::Type(*e))
.collect(),
);

// In WASM, a break out of the root block is the same as returning from the function
Expand Down
13 changes: 9 additions & 4 deletions src/codegen/type_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ptr;
use llvm::ffi::prelude::LLVMTypeRef;
use llvm::Compile;
use llvm::Context;
use llvm::StructType;
use llvm::Sub;

pub fn llvm_type_to_wasm_type(ctx: &Context, ty: &llvm::Type) -> wasmparser::Type {
Expand Down Expand Up @@ -62,10 +63,14 @@ pub fn wasm_func_type_to_llvm_type<'a>(
f_type: &wasmparser::FuncType,
) -> &'a llvm::Type {
let return_count = f_type.returns.len();
if return_count > 1 {
panic!("aWsm does not support multiple return values");
}
let return_type = if return_count == 0 {
let return_type = if return_count > 1 {
let rets: Vec<&llvm::Type> = f_type
.returns
.into_iter()
.map(|e| wasm_type_to_llvm_type(ctx, *e))
.collect();
StructType::new(ctx, &*rets.into_boxed_slice(), false).to_super()
} else if return_count == 0 {
<()>::get_type(ctx)
} else {
wasm_type_to_llvm_type(ctx, f_type.returns[0])
Expand Down
24 changes: 14 additions & 10 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,19 @@ impl ImplementedFunction {
}
}

pub fn get_return_type(&self) -> Option<Type> {
pub fn get_return_type(&self) -> Vec<Type> {
match self.ty {
Some(ref ty) => {
if ty.returns.len() == 1 {
Some(ty.returns[0])
let mut res = Vec::new();
res.push(ty.returns[0]);
res
} else if ty.returns.is_empty() {
None
Vec::new()
} else {
panic!("Malformed wasm, a function has too many return types")
let mut res: Vec<Type> = ty.returns.iter().map(|e| *e).collect();
res
// panic!("Malformed wasm, a function has too many return types")
}
}
None => panic!("Malformed wasm, a function has no type"),
Expand All @@ -266,9 +270,9 @@ pub struct TableInitializer {
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::upper_case_acronyms)]
pub enum Instruction {
BlockStart { produced_type: Option<TypeOrFuncType> },
LoopStart { produced_type: Option<TypeOrFuncType> },
If { produced_type: Option<TypeOrFuncType> },
BlockStart { produced_type: Vec<TypeOrFuncType> },
LoopStart { produced_type: Vec<TypeOrFuncType> },
If { produced_type: Vec<TypeOrFuncType> },
Else,
End,

Expand Down Expand Up @@ -491,17 +495,17 @@ impl<'a> From<&'a Operator<'a>> for Instruction {
}
Operator::Loop { ty } => {
let produced_type = if ty == TypeOrFuncType::Type(Type::EmptyBlockType) {
None
new Vec()
} else {
Some(ty)
};
Instruction::LoopStart { produced_type }
}
Operator::If { ty } => {
let produced_type = if ty == TypeOrFuncType::Type(Type::EmptyBlockType) {
None
Vec::new()
} else {
Some(ty)
ty
};
Instruction::If { produced_type }
}
Expand Down