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

Reduce generics #593

Merged
merged 1 commit into from
Jul 30, 2023
Merged
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
28 changes: 19 additions & 9 deletions crates/rune/src/compile/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) struct Assembly {
/// Instructions with spans.
pub(crate) instructions: Vec<(AssemblyInst, Span)>,
/// Comments associated with instructions.
pub(crate) comments: HashMap<usize, Vec<Box<str>>>,
pub(crate) comments: HashMap<usize, String>,
/// The number of labels.
pub(crate) label_count: usize,
/// The collection of functions required by this assembly.
Expand Down Expand Up @@ -166,18 +166,28 @@ impl Assembly {
}

/// Push a raw instruction.
pub(crate) fn push_with_comment<C>(&mut self, raw: Inst, span: &dyn Spanned, comment: C)
where
C: fmt::Display,
{
pub(crate) fn push_with_comment(
&mut self,
raw: Inst,
span: &dyn Spanned,
comment: &dyn fmt::Display,
) -> compile::Result<()> {
use core::fmt::Write;

let pos = self.instructions.len();

self.comments
.entry(pos)
.or_default()
.push(comment.to_string().into());
let c = self.comments.entry(pos).or_default();

if !c.is_empty() {
c.push_str("; ");
}

if let Err(fmt::Error) = write!(c, "{}", comment) {
return Err(compile::Error::msg(span, "Failed to write comment"));
}

self.push(raw, span);
Ok(())
}

fn inner_push(&mut self, inst: AssemblyInst, span: &dyn Spanned) {
Expand Down
87 changes: 71 additions & 16 deletions crates/rune/src/compile/unit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ impl UnitBuilder {
assembly: Assembly,
storage: &mut dyn UnitEncoder,
) -> compile::Result<()> {
use core::fmt::Write;

self.label_count = assembly.label_count;

let base = storage.extend_offsets(assembly.labels.len());
Expand All @@ -696,7 +698,7 @@ impl UnitBuilder {
}

for (pos, (inst, span)) in assembly.instructions.into_iter().enumerate() {
let mut comment = None::<Box<str>>;
let mut comment = String::new();

let mut labels = Vec::new();

Expand Down Expand Up @@ -724,7 +726,14 @@ impl UnitBuilder {
index: label.index,
})
.with_span(location.span)?;
comment = Some(format!("label:{}", label).into());

if let Err(fmt::Error) = write!(comment, "label:{}", label) {
return Err(compile::Error::msg(
location.span,
"Failed to write comment",
));
}

storage
.encode(Inst::Jump { jump })
.with_span(location.span)?;
Expand All @@ -737,7 +746,14 @@ impl UnitBuilder {
index: label.index,
})
.with_span(location.span)?;
comment = Some(format!("label:{}", label).into());

if let Err(fmt::Error) = write!(comment, "label:{}", label) {
return Err(compile::Error::msg(
location.span,
"Failed to write comment",
));
}

storage
.encode(Inst::JumpIf { jump })
.with_span(location.span)?;
Expand All @@ -750,7 +766,14 @@ impl UnitBuilder {
index: label.index,
})
.with_span(location.span)?;
comment = Some(format!("label:{}", label).into());

if let Err(fmt::Error) = write!(comment, "label:{}", label) {
return Err(compile::Error::msg(
location.span,
"Failed to write comment",
));
}

storage
.encode(Inst::JumpIfOrPop { jump })
.with_span(location.span)?;
Expand All @@ -763,7 +786,14 @@ impl UnitBuilder {
index: label.index,
})
.with_span(location.span)?;
comment = Some(format!("label:{}", label).into());

if let Err(fmt::Error) = write!(comment, "label:{}", label) {
return Err(compile::Error::msg(
location.span,
"Failed to write comment",
));
}

storage
.encode(Inst::JumpIfNotOrPop { jump })
.with_span(location.span)?;
Expand All @@ -776,7 +806,14 @@ impl UnitBuilder {
index: label.index,
})
.with_span(location.span)?;
comment = Some(format!("label:{}", label).into());

if let Err(fmt::Error) = write!(comment, "label:{}", label) {
return Err(compile::Error::msg(
location.span,
"Failed to write comment",
));
}

storage
.encode(Inst::JumpIfBranch { branch, jump })
.with_span(location.span)?;
Expand All @@ -789,7 +826,14 @@ impl UnitBuilder {
index: label.index,
})
.with_span(location.span)?;
comment = Some(format!("label:{}", label).into());

if let Err(fmt::Error) = write!(comment, "label:{}", label) {
return Err(compile::Error::msg(
location.span,
"Failed to write comment",
));
}

storage
.encode(Inst::PopAndJumpIfNot { count, jump })
.with_span(location.span)?;
Expand All @@ -802,7 +846,14 @@ impl UnitBuilder {
index: label.index,
})
.with_span(location.span)?;
comment = Some(format!("label:{}", label).into());

if let Err(fmt::Error) = write!(comment, "label:{}", label) {
return Err(compile::Error::msg(
location.span,
"Failed to write comment",
));
}

storage
.encode(Inst::IterNext { offset, jump })
.with_span(location.span)?;
Expand All @@ -812,18 +863,22 @@ impl UnitBuilder {
}
}

if let Some(comments) = assembly.comments.get(&pos) {
let actual = comment
.take()
.into_iter()
.chain(comments.iter().cloned())
.collect::<Vec<_>>()
.join("; ");
comment = Some(actual.into())
if let Some(c) = assembly.comments.get(&pos) {
if !comment.is_empty() {
comment.push_str("; ");
}

comment.push_str(c);
}

let debug = self.debug.get_or_insert_with(Default::default);

let comment = if comment.is_empty() {
None
} else {
Some(comment.into())
};

debug.instructions.insert(
at,
DebugInst::new(location.source_id, span, comment, labels),
Expand Down
52 changes: 27 additions & 25 deletions crates/rune/src/compile/v1/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl<'hir> Asm<'hir> {
/// Assemble into an instruction.
fn apply(self, cx: &mut Ctxt) -> compile::Result<()> {
if let AsmKind::Var(var) = self.kind {
var.copy(cx, &self.span, format_args!("var `{}`", var));
var.copy(cx, &self.span, &format_args!("var `{}`", var))?;
}

Ok(())
Expand Down Expand Up @@ -985,8 +985,8 @@ fn expr_assign<'hir>(
cx.asm.push_with_comment(
Inst::Replace { offset: var.offset },
span,
format_args!("var `{var}`"),
);
&format_args!("var `{var}`"),
)?;
true
}
// <expr>.<field> = <value>
Expand Down Expand Up @@ -1251,10 +1251,10 @@ fn expr_async_block<'hir>(
for capture in hir.captures.iter().copied() {
if hir.do_move {
let var = cx.scopes.take(&mut cx.q, capture, span)?;
var.do_move(cx.asm, span, "capture");
var.do_move(cx.asm, span, &"capture")?;
} else {
let var = cx.scopes.get(&mut cx.q, capture, span)?;
var.copy(cx, span, "capture");
var.copy(cx, span, &"capture")?;
}
}

Expand All @@ -1264,12 +1264,12 @@ fn expr_async_block<'hir>(
args: hir.captures.len(),
},
span,
"async block",
);
&"async block",
)?;

if !needs.value() {
cx.asm
.push_with_comment(Inst::Pop, span, "value is not needed");
.push_with_comment(Inst::Pop, span, &"value is not needed")?;
}

Ok(Asm::top(span))
Expand Down Expand Up @@ -1378,7 +1378,7 @@ fn expr_call<'hir>(
cx.scopes.alloc(span)?;
}

var.copy(cx, span, "call");
var.copy(cx, span, &"call")?;
cx.scopes.alloc(span)?;

cx.asm.push(Inst::CallFn { args }, span);
Expand Down Expand Up @@ -1456,10 +1456,10 @@ fn expr_call_closure<'hir>(
for capture in hir.captures.iter().copied() {
if hir.do_move {
let var = cx.scopes.take(&mut cx.q, capture, span)?;
var.do_move(cx.asm, span, "capture");
var.do_move(cx.asm, span, &"capture")?;
} else {
let var = cx.scopes.get(&mut cx.q, capture, span)?;
var.copy(cx, span, "capture");
var.copy(cx, span, &"capture")?;
}
}

Expand Down Expand Up @@ -1533,8 +1533,8 @@ fn expr_field_access<'hir>(
index,
},
span,
var,
);
&var,
)?;

if !needs.value() {
cx.q.diagnostics.not_used(cx.source_id, span, cx.context());
Expand Down Expand Up @@ -1592,14 +1592,15 @@ fn expr_for<'hir>(
expr(cx, &hir.iter, Needs::Value)?.apply(cx)?;

let iter_offset = cx.scopes.alloc(span)?;

cx.asm.push_with_comment(
Inst::CallAssociated {
hash: *Protocol::INTO_ITER,
args: 0,
},
span,
format_args!("into_iter (offset: {})", iter_offset),
);
&format_args!("into_iter (offset: {})", iter_offset),
)?;

(iter_offset, loop_scope_expected)
};
Expand All @@ -1620,16 +1621,16 @@ fn expr_for<'hir>(
offset: iter_offset,
},
&hir.iter,
"copy iterator (memoize)",
);
&"copy iterator (memoize)",
)?;

cx.asm.push_with_comment(
Inst::LoadInstanceFn {
hash: *Protocol::NEXT,
},
&hir.iter,
"load instance fn (memoize)",
);
&"load instance fn (memoize)",
)?;

Some(offset)
} else {
Expand All @@ -1656,16 +1657,16 @@ fn expr_for<'hir>(
offset: iter_offset,
},
&hir.iter,
"copy iterator",
);
&"copy iterator",
)?;

cx.asm.push_with_comment(
Inst::Copy {
offset: next_offset,
},
&hir.iter,
"copy next",
);
&"copy next",
)?;

cx.asm.push(Inst::CallFn { args: 1 }, span);

Expand All @@ -1691,8 +1692,9 @@ fn expr_for<'hir>(
args: 0,
},
span,
"next",
);
&"next",
)?;

cx.asm.push(
Inst::Replace {
offset: binding_offset,
Expand Down
Loading