Skip to content

Commit

Permalink
Don't attempt variable allocation during HIR lowering (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog authored Jun 10, 2023
1 parent 926aa25 commit 0a829c4
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 286 deletions.
3 changes: 1 addition & 2 deletions crates/rune/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ mod prelude;
pub(crate) use self::prelude::Prelude;

pub(crate) mod ir;
pub use self::ir::IrValue;
pub(crate) use self::ir::{IrBudget, IrCompiler, IrEvalOutcome, IrInterpreter};
pub(crate) use self::ir::{IrBudget, IrCompiler, IrEvalOutcome, IrInterpreter, IrValue};

pub use rune_core::{Component, ComponentRef, IntoComponent, Item, ItemBuf};

Expand Down
2 changes: 0 additions & 2 deletions crates/rune/src/compile/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ impl crate::no_std::error::Error for MissingScope {}
pub(crate) enum PopError {
MissingScope(usize),
MissingParentScope(usize),
MissingVariable(usize),
}

impl fmt::Display for PopError {
Expand All @@ -660,7 +659,6 @@ impl fmt::Display for PopError {
match self {
PopError::MissingScope(id) => write!(f, "Missing scope with id {id}"),
PopError::MissingParentScope(id) => write!(f, "Missing parent scope with id {id}"),
PopError::MissingVariable(id) => write!(f, "Missing variable with id {id}"),
}
}
}
Expand Down
72 changes: 36 additions & 36 deletions crates/rune/src/compile/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod interpreter;
pub(crate) use self::interpreter::{IrBudget, IrInterpreter};

mod value;
pub use self::value::IrValue;
pub(crate) use self::value::IrValue;

use core::ops::{AddAssign, MulAssign, ShlAssign, ShrAssign, SubAssign};

Expand All @@ -38,13 +38,13 @@ impl ast::Expr {

let ir = {
// TODO: avoid this arena?
let arena = crate::hir::Arena::new();
let mut hir_ctx = crate::hir::lowering::Ctx::with_const(
let arena = hir::Arena::new();
let mut hir_ctx = hir::lowering::Ctx::with_const(
&arena,
ctx.idx.q.borrow(),
ctx.item_meta.location.source_id,
);
let hir = crate::hir::lowering::expr(&mut hir_ctx, &expr)?;
let hir = hir::lowering::expr(&mut hir_ctx, &expr)?;

let mut c = IrCompiler {
source_id: ctx.item_meta.location.source_id,
Expand Down Expand Up @@ -90,7 +90,7 @@ macro_rules! decl_kind {

/// A single operation in the Rune intermediate language.
#[derive(Debug, Clone, Spanned)]
pub struct Ir {
pub(crate) struct Ir {
#[rune(span)]
pub(crate) span: Span,
pub(crate) kind: IrKind,
Expand All @@ -112,7 +112,7 @@ impl Ir {

/// The target of a set operation.
#[derive(Debug, Clone, Spanned)]
pub struct IrTarget {
pub(crate) struct IrTarget {
/// Span of the target.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -122,9 +122,9 @@ pub struct IrTarget {

/// The kind of the target.
#[derive(Debug, Clone)]
pub enum IrTargetKind {
pub(crate) enum IrTargetKind {
/// A variable.
Name(Box<str>),
Name(hir::OwnedName),
/// A field target.
Field(Box<IrTarget>, Box<str>),
/// An index target.
Expand All @@ -134,7 +134,7 @@ pub enum IrTargetKind {
decl_kind! {
/// The kind of an intermediate operation.
#[derive(Debug, Clone)]
pub enum IrKind {
pub(crate) enum IrKind {
/// Push a scope with the given instructions.
Scope(IrScope),
/// A binary operation.
Expand Down Expand Up @@ -173,7 +173,7 @@ decl_kind! {

/// An interpeted function.
#[derive(Debug, Clone, Spanned)]
pub struct IrFn {
pub(crate) struct IrFn {
/// The span of the function.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -192,15 +192,15 @@ impl IrFn {

for arg in hir.args {
if let hir::FnArg::Pat(hir::Pat {
kind: hir::PatKind::Path(&hir::PatPathKind::Ident(ident, _)),
kind: hir::PatKind::Path(&hir::PatPathKind::Ident(name)),
..
}) = arg
{
args.push(ident.into());
args.push(name.into());
continue;
}

return Err(compile::Error::msg(arg, "unsupported argument in const fn"));
return Err(compile::Error::msg(arg, "Unsupported argument in const fn"));
}

let ir_scope = compiler::block(hir.body, c)?;
Expand All @@ -215,7 +215,7 @@ impl IrFn {

/// Definition of a new variable scope.
#[derive(Debug, Clone, Spanned)]
pub struct IrScope {
pub(crate) struct IrScope {
/// The span of the scope.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -227,7 +227,7 @@ pub struct IrScope {

/// A binary operation.
#[derive(Debug, Clone, Spanned)]
pub struct IrBinary {
pub(crate) struct IrBinary {
/// The span of the binary op.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -241,7 +241,7 @@ pub struct IrBinary {

/// A local variable declaration.
#[derive(Debug, Clone, Spanned)]
pub struct IrDecl {
pub(crate) struct IrDecl {
/// The span of the declaration.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -253,7 +253,7 @@ pub struct IrDecl {

/// Set a target.
#[derive(Debug, Clone, Spanned)]
pub struct IrSet {
pub(crate) struct IrSet {
/// The span of the set operation.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -265,7 +265,7 @@ pub struct IrSet {

/// Assign a target.
#[derive(Debug, Clone, Spanned)]
pub struct IrAssign {
pub(crate) struct IrAssign {
/// The span of the set operation.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -279,7 +279,7 @@ pub struct IrAssign {

/// A string template.
#[derive(Debug, Clone, Spanned)]
pub struct IrTemplate {
pub(crate) struct IrTemplate {
/// The span of the template.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -289,7 +289,7 @@ pub struct IrTemplate {

/// A string template.
#[derive(Debug, Clone)]
pub enum IrTemplateComponent {
pub(crate) enum IrTemplateComponent {
/// An ir expression.
Ir(Ir),
/// A literal string.
Expand All @@ -298,10 +298,10 @@ pub enum IrTemplateComponent {

/// Branch conditions in intermediate representation.
#[derive(Debug, Clone, Spanned)]
pub struct IrBranches {
pub(crate) struct IrBranches {
/// Span associated with branches.
#[rune(span)]
pub span: Span,
pub(crate) span: Span,
/// branches and their associated conditions.
pub(crate) branches: Vec<(IrCondition, IrScope)>,
/// The default fallback branch.
Expand All @@ -310,7 +310,7 @@ pub struct IrBranches {

/// The condition for a branch.
#[derive(Debug, Clone, Spanned)]
pub enum IrCondition {
pub(crate) enum IrCondition {
/// A simple conditional ir expression.
Ir(Ir),
/// A pattern match.
Expand All @@ -319,7 +319,7 @@ pub enum IrCondition {

/// A pattern match.
#[derive(Debug, Clone, Spanned)]
pub struct IrLet {
pub(crate) struct IrLet {
/// The span of the let condition.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -331,7 +331,7 @@ pub struct IrLet {

/// A pattern.
#[derive(Debug, Clone)]
pub enum IrPat {
pub(crate) enum IrPat {
/// An ignore pattern `_`.
Ignore,
/// A named binding.
Expand All @@ -342,8 +342,8 @@ impl IrPat {
fn compile_ast(hir: &hir::Pat<'_>) -> compile::Result<Self> {
match hir.kind {
hir::PatKind::Ignore => return Ok(ir::IrPat::Ignore),
hir::PatKind::Path(&hir::PatPathKind::Ident(ident, _)) => {
return Ok(ir::IrPat::Binding(ident.into()));
hir::PatKind::Path(&hir::PatPathKind::Ident(name)) => {
return Ok(ir::IrPat::Binding(name.into()));
}
_ => (),
}
Expand Down Expand Up @@ -372,7 +372,7 @@ impl IrPat {

/// A loop with an optional condition.
#[derive(Debug, Clone, Spanned)]
pub struct IrLoop {
pub(crate) struct IrLoop {
/// The span of the loop.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -386,7 +386,7 @@ pub struct IrLoop {

/// A break operation.
#[derive(Debug, Clone, Spanned)]
pub struct IrBreak {
pub(crate) struct IrBreak {
/// The span of the break.
#[rune(span)]
pub(crate) span: Span,
Expand Down Expand Up @@ -438,7 +438,7 @@ impl IrBreak {

/// The kind of a break expression.
#[derive(Debug, Clone)]
pub enum IrBreakKind {
pub(crate) enum IrBreakKind {
/// Break to the next loop.
Inherent,
/// Break to the given label.
Expand All @@ -449,7 +449,7 @@ pub enum IrBreakKind {

/// Tuple expression.
#[derive(Debug, Clone, Spanned)]
pub struct IrTuple {
pub(crate) struct IrTuple {
/// Span of the tuple.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -459,7 +459,7 @@ pub struct IrTuple {

/// Object expression.
#[derive(Debug, Clone, Spanned)]
pub struct IrObject {
pub(crate) struct IrObject {
/// Span of the object.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -469,7 +469,7 @@ pub struct IrObject {

/// Call expressions.
#[derive(Debug, Clone, Spanned)]
pub struct IrCall {
pub(crate) struct IrCall {
/// Span of the call.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -481,7 +481,7 @@ pub struct IrCall {

/// Vector expression.
#[derive(Debug, Clone, Spanned)]
pub struct IrVec {
pub(crate) struct IrVec {
/// Span of the vector.
#[rune(span)]
pub(crate) span: Span,
Expand All @@ -491,7 +491,7 @@ pub struct IrVec {

/// A binary operation.
#[derive(Debug, Clone, Copy)]
pub enum IrBinaryOp {
pub(crate) enum IrBinaryOp {
/// Add `+`.
Add,
/// Subtract `-`.
Expand All @@ -518,7 +518,7 @@ pub enum IrBinaryOp {

/// An assign operation.
#[derive(Debug, Clone, Copy)]
pub enum IrAssignOp {
pub(crate) enum IrAssignOp {
/// `+=`.
Add,
/// `-=`.
Expand Down
10 changes: 5 additions & 5 deletions crates/rune/src/compile/ir/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ impl IrCompiler<'_> {
/// Resolve an ir target from an expression.
fn ir_target(&self, expr: &hir::Expr<'_>) -> compile::Result<ir::IrTarget> {
match expr.kind {
hir::ExprKind::Variable(_, name) => {
hir::ExprKind::Variable(name) => {
return Ok(ir::IrTarget {
span: expr.span(),
kind: ir::IrTargetKind::Name(name.into()),
kind: ir::IrTargetKind::Name(name.into_owned()),
});
}
hir::ExprKind::FieldAccess(expr_field_access) => {
Expand Down Expand Up @@ -102,8 +102,8 @@ pub(crate) fn expr(hir: &hir::Expr<'_>, c: &mut IrCompiler<'_>) -> compile::Resu

ir::Ir::new(span, ir::IrValue::from_const(value))
}
hir::ExprKind::Variable(_, name) => {
return Ok(ir::Ir::new(span, <Box<str>>::from(name)));
hir::ExprKind::Variable(name) => {
return Ok(ir::Ir::new(span, <Box<str>>::from(name.as_str())));
}
_ => {
return Err(compile::Error::msg(
Expand Down Expand Up @@ -378,7 +378,7 @@ fn local(hir: &hir::Local<'_>, c: &mut IrCompiler<'_>) -> compile::Result<ir::Ir
hir::PatKind::Ignore => {
return expr(hir.expr, c);
}
hir::PatKind::Path(&hir::PatPathKind::Ident(ident, _)) => ident,
hir::PatKind::Path(&hir::PatPathKind::Ident(name)) => name,
_ => {
return Err(compile::Error::msg(span, "not supported yet"));
}
Expand Down
8 changes: 5 additions & 3 deletions crates/rune/src/compile/ir/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ impl IrScopes {
/// Get the given target as mut.
pub(crate) fn get_target(&mut self, ir_target: &ir::IrTarget) -> compile::Result<IrValue> {
match &ir_target.kind {
ir::IrTargetKind::Name(name) => Ok(self.get_name(name).with_span(ir_target)?.clone()),
ir::IrTargetKind::Name(name) => {
Ok(self.get_name(name.as_str()).with_span(ir_target)?.clone())
}
ir::IrTargetKind::Field(ir_target, field) => {
let value = self.get_target(ir_target)?;

Expand Down Expand Up @@ -250,7 +252,7 @@ impl IrScopes {
) -> compile::Result<()> {
match &ir_target.kind {
ir::IrTargetKind::Name(name) => {
*self.get_name_mut(name.as_ref()).with_span(ir_target)? = value;
*self.get_name_mut(name.as_str()).with_span(ir_target)? = value;
Ok(())
}
ir::IrTargetKind::Field(target, field) => {
Expand Down Expand Up @@ -310,7 +312,7 @@ impl IrScopes {
) -> compile::Result<()> {
match &ir_target.kind {
ir::IrTargetKind::Name(name) => {
let value = self.get_name_mut(name.as_ref()).with_span(ir_target)?;
let value = self.get_name_mut(name.as_str()).with_span(ir_target)?;
op(value)
}
ir::IrTargetKind::Field(target, field) => {
Expand Down
Loading

0 comments on commit 0a829c4

Please sign in to comment.