Skip to content

Commit

Permalink
rune: Clean up vm operations
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 5, 2024
1 parent 1671f0f commit 4d7e75a
Show file tree
Hide file tree
Showing 13 changed files with 862 additions and 894 deletions.
2 changes: 1 addition & 1 deletion book/src/field_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl External {
}
let mut module = Module::new();
module.field_function(Protocol::GET, "field", External::field_get)?;
module.field_function(&Protocol::GET, "field", External::field_get)?;
```

Would allow for this in Rune:
Expand Down
10 changes: 5 additions & 5 deletions crates/rune-macros/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl Context {
..
} = g;

let protocol_field = g.tokens.protocol(Protocol::$proto);
let protocol_field = g.tokens.protocol(&Protocol::$proto);

match target {
GenerateTarget::Named { field_ident, field_name } => {
Expand Down Expand Up @@ -391,7 +391,7 @@ impl Context {
quote!(#vm_try!(#try_clone::try_clone(&s.#field_ident)))
};

let protocol = g.tokens.protocol(Protocol::GET);
let protocol = g.tokens.protocol(&Protocol::GET);

quote_spanned! { g.field.span() =>
module.field_function(&#protocol, #field_name, |s: &Self| #vm_result::Ok(#access))?;
Expand All @@ -404,7 +404,7 @@ impl Context {
quote!(#vm_try!(#try_clone::try_clone(&s.#field_index)))
};

let protocol = g.tokens.protocol(Protocol::GET);
let protocol = g.tokens.protocol(&Protocol::GET);

quote_spanned! { g.field.span() =>
module.index_function(&#protocol, #field_index, |s: &Self| #vm_result::Ok(#access))?;
Expand All @@ -427,7 +427,7 @@ impl Context {
..
} = g;

let protocol = g.tokens.protocol(Protocol::SET);
let protocol = g.tokens.protocol(&Protocol::SET);

match target {
GenerateTarget::Named { field_ident, field_name } => {
Expand Down Expand Up @@ -939,7 +939,7 @@ pub(crate) struct Tokens {

impl Tokens {
/// Define a tokenstream for the specified protocol
pub(crate) fn protocol(&self, sym: Protocol) -> TokenStream {
pub(crate) fn protocol(&self, sym: &Protocol) -> TokenStream {
let mut stream = TokenStream::default();
self.protocol.to_tokens(&mut stream);
<Token![::]>::default().to_tokens(&mut stream);
Expand Down
199 changes: 150 additions & 49 deletions crates/rune/src/compile/v1/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,52 +1480,153 @@ fn expr_binary<'a, 'hir>(
return compile_conditional_binop(cx, &hir.lhs, &hir.rhs, &hir.op, span, needs);
}

let op = match hir.op {
ast::BinOp::Eq(..) => InstOp::Eq,
ast::BinOp::Neq(..) => InstOp::Neq,
ast::BinOp::Lt(..) => InstOp::Lt,
ast::BinOp::Gt(..) => InstOp::Gt,
ast::BinOp::Lte(..) => InstOp::Le,
ast::BinOp::Gte(..) => InstOp::Ge,
ast::BinOp::As(..) => InstOp::As,
ast::BinOp::Is(..) => InstOp::Is,
ast::BinOp::IsNot(..) => InstOp::IsNot,
ast::BinOp::And(..) => InstOp::And,
ast::BinOp::Or(..) => InstOp::Or,
ast::BinOp::Add(..) => InstOp::Add,
ast::BinOp::Sub(..) => InstOp::Sub,
ast::BinOp::Div(..) => InstOp::Div,
ast::BinOp::Mul(..) => InstOp::Mul,
ast::BinOp::Rem(..) => InstOp::Rem,
ast::BinOp::BitAnd(..) => InstOp::BitAnd,
ast::BinOp::BitXor(..) => InstOp::BitXor,
ast::BinOp::BitOr(..) => InstOp::BitOr,
ast::BinOp::Shl(..) => InstOp::Shl,
ast::BinOp::Shr(..) => InstOp::Shr,

op => {
return Err(compile::Error::new(
span,
ErrorKind::UnsupportedBinaryOp { op },
));
}
};

let mut a = cx.scopes.defer(span);
let mut b = cx.scopes.defer(span);

let asm = expr_array(cx, span, [(&hir.lhs, &mut a), (&hir.rhs, &mut b)])?;

if let Some([a, b]) = asm.into_converging() {
cx.asm.push(
Inst::Op {
op,
a: a.addr(),
b: b.addr(),
out: needs.alloc_output()?,
let a = a.addr();
let b = b.addr();
let out = needs.alloc_output()?;

let inst = match hir.op {
ast::BinOp::Eq(..) => Inst::Op {
op: InstOp::Eq,
a,
b,
out,
},
span,
)?;
ast::BinOp::Neq(..) => Inst::Op {
op: InstOp::Neq,
a,
b,
out,
},
ast::BinOp::Lt(..) => Inst::Op {
op: InstOp::Lt,
a,
b,
out,
},
ast::BinOp::Gt(..) => Inst::Op {
op: InstOp::Gt,
a,
b,
out,
},
ast::BinOp::Lte(..) => Inst::Op {
op: InstOp::Le,
a,
b,
out,
},
ast::BinOp::Gte(..) => Inst::Op {
op: InstOp::Ge,
a,
b,
out,
},
ast::BinOp::As(..) => Inst::Op {
op: InstOp::As,
a,
b,
out,
},
ast::BinOp::Is(..) => Inst::Op {
op: InstOp::Is,
a,
b,
out,
},
ast::BinOp::IsNot(..) => Inst::Op {
op: InstOp::IsNot,
a,
b,
out,
},
ast::BinOp::And(..) => Inst::Op {
op: InstOp::And,
a,
b,
out,
},
ast::BinOp::Or(..) => Inst::Op {
op: InstOp::Or,
a,
b,
out,
},
ast::BinOp::Add(..) => Inst::Arithmetic {
op: InstArithmeticOp::Add,
a,
b,
out,
},
ast::BinOp::Sub(..) => Inst::Arithmetic {
op: InstArithmeticOp::Sub,
a,
b,
out,
},
ast::BinOp::Div(..) => Inst::Arithmetic {
op: InstArithmeticOp::Div,
a,
b,
out,
},
ast::BinOp::Mul(..) => Inst::Arithmetic {
op: InstArithmeticOp::Mul,
a,
b,
out,
},
ast::BinOp::Rem(..) => Inst::Arithmetic {
op: InstArithmeticOp::Rem,
a,
b,
out,
},
ast::BinOp::BitAnd(..) => Inst::Bitwise {
op: InstBitwiseOp::BitAnd,
a,
b,
out,
},
ast::BinOp::BitXor(..) => Inst::Bitwise {
op: InstBitwiseOp::BitXor,
a,
b,
out,
},
ast::BinOp::BitOr(..) => Inst::Bitwise {
op: InstBitwiseOp::BitOr,
a,
b,
out,
},
ast::BinOp::Shl(..) => Inst::Shift {
op: InstShiftOp::Shl,
a,
b,
out,
},
ast::BinOp::Shr(..) => Inst::Shift {
op: InstShiftOp::Shr,
a,
b,
out,
},

op => {
return Err(compile::Error::new(
span,
ErrorKind::UnsupportedBinaryOp { op },
));
}
};

cx.asm.push(inst, span)?;
}

a.free()?;
Expand Down Expand Up @@ -1623,52 +1724,52 @@ fn compile_assign_binop<'a, 'hir>(
ast::BinOp::AddAssign(..) => Inst::AssignArithmetic {
op: InstArithmeticOp::Add,
target,
value: value.addr(),
rhs: value.addr(),
},
ast::BinOp::SubAssign(..) => Inst::AssignArithmetic {
op: InstArithmeticOp::Sub,
target,
value: value.addr(),
rhs: value.addr(),
},
ast::BinOp::MulAssign(..) => Inst::AssignArithmetic {
op: InstArithmeticOp::Mul,
target,
value: value.addr(),
rhs: value.addr(),
},
ast::BinOp::DivAssign(..) => Inst::AssignArithmetic {
op: InstArithmeticOp::Div,
target,
value: value.addr(),
rhs: value.addr(),
},
ast::BinOp::RemAssign(..) => Inst::AssignArithmetic {
op: InstArithmeticOp::Rem,
target,
value: value.addr(),
rhs: value.addr(),
},
ast::BinOp::BitAndAssign(..) => Inst::AssignBitwise {
op: InstBitwiseOp::BitAnd,
target,
value: value.addr(),
rhs: value.addr(),
},
ast::BinOp::BitXorAssign(..) => Inst::AssignBitwise {
op: InstBitwiseOp::BitXor,
target,
value: value.addr(),
rhs: value.addr(),
},
ast::BinOp::BitOrAssign(..) => Inst::AssignBitwise {
op: InstBitwiseOp::BitOr,
target,
value: value.addr(),
rhs: value.addr(),
},
ast::BinOp::ShlAssign(..) => Inst::AssignShift {
op: InstShiftOp::Shl,
target,
value: value.addr(),
rhs: value.addr(),
},
ast::BinOp::ShrAssign(..) => Inst::AssignShift {
op: InstShiftOp::Shr,
target,
value: value.addr(),
rhs: value.addr(),
},
_ => {
return Err(compile::Error::new(span, ErrorKind::UnsupportedBinaryExpr));
Expand Down
Loading

0 comments on commit 4d7e75a

Please sign in to comment.