Skip to content

Commit

Permalink
Clean up jump instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Jul 16, 2024
1 parent c6748e3 commit 1bb6c5b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 50 deletions.
17 changes: 10 additions & 7 deletions crates/rune/src/compile/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,6 @@ impl Assembly {

/// Push a raw instruction.
pub(crate) fn push(&mut self, raw: Inst, span: &dyn Spanned) -> compile::Result<()> {
if let Inst::Call { hash, .. } = raw {
self.required_functions
.entry(hash)
.or_try_default()?
.try_push((span.span(), self.location.source_id))?;
}

self.inner_push(AssemblyInst::Raw { raw }, span)?;
Ok(())
}
Expand All @@ -192,6 +185,16 @@ impl Assembly {
}

fn inner_push(&mut self, inst: AssemblyInst, span: &dyn Spanned) -> compile::Result<()> {
if let AssemblyInst::Raw {
raw: Inst::Call { hash, .. },
} = &inst
{
self.required_functions
.entry(*hash)
.or_try_default()?
.try_push((span.span(), self.location.source_id))?;
}

self.instructions.try_push((inst, span.span()))?;
Ok(())
}
Expand Down
54 changes: 16 additions & 38 deletions crates/rune/src/compile/unit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::query::QueryInner;
use crate::runtime::debug::{DebugArgs, DebugSignature};
use crate::runtime::unit::UnitEncoder;
use crate::runtime::{
Call, ConstValue, DebugInfo, DebugInst, Inst, Protocol, Rtti, StaticString, Unit, UnitFn,
VariantRtti,
Call, ConstValue, DebugInfo, DebugInst, Inst, Label, Protocol, Rtti, StaticString, Unit,
UnitFn, VariantRtti,
};
use crate::{Context, Diagnostics, Hash, SourceId};

Expand Down Expand Up @@ -813,61 +813,39 @@ impl UnitBuilder {
labels.try_push(label.to_debug_label())?;
}

let build_label = |label: Label| {
label
.jump()
.ok_or(ErrorKind::MissingLabelLocation {
name: label.name,
index: label.index,
})
.with_span(span)
};

match inst {
AssemblyInst::Jump { label } => {
let jump = label
.jump()
.ok_or(ErrorKind::MissingLabelLocation {
name: label.name,
index: label.index,
})
.with_span(span)?;

write!(comment, "label:{}", label)?;

let jump = build_label(label)?;
storage.encode(Inst::Jump { jump }).with_span(span)?;
}
AssemblyInst::JumpIf { addr, label } => {
let jump = label
.jump()
.ok_or(ErrorKind::MissingLabelLocation {
name: label.name,
index: label.index,
})
.with_span(span)?;

write!(comment, "label:{}", label)?;

let jump = build_label(label)?;
storage
.encode(Inst::JumpIf { cond: addr, jump })
.with_span(span)?;
}
AssemblyInst::JumpIfNot { addr, label } => {
let jump = label
.jump()
.ok_or(ErrorKind::MissingLabelLocation {
name: label.name,
index: label.index,
})
.with_span(span)?;

write!(comment, "label:{}", label)?;

let jump = build_label(label)?;
storage
.encode(Inst::JumpIfNot { cond: addr, jump })
.with_span(span)?;
}
AssemblyInst::IterNext { addr, label, out } => {
let jump = label
.jump()
.ok_or(ErrorKind::MissingLabelLocation {
name: label.name,
index: label.index,
})
.with_span(span)?;

write!(comment, "label:{}", label)?;

let jump = build_label(label)?;
storage
.encode(Inst::IterNext { addr, jump, out })
.with_span(span)?;
Expand Down
7 changes: 2 additions & 5 deletions crates/rune/src/compile/v1/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,9 @@ fn pat<'a, 'hir>(
converge!(load(cx, &mut needs)?, free(needs));

let cond = cx.scopes.alloc(hir)?;
let inst = pat_sequence_kind_to_inst(*kind, needs.addr()?.addr(), cond.output());

cx.asm.push(
pat_sequence_kind_to_inst(*kind, needs.addr()?.addr(), cond.output()),
hir,
)?;

cx.asm.push(inst, hir)?;
cx.asm.jump_if_not(cond.addr(), false_label, hir)?;

cond.free()?;
Expand Down

0 comments on commit 1bb6c5b

Please sign in to comment.