Skip to content

Commit 0212977

Browse files
committed
add and use ControlFrameBase trait
1 parent 83fc21b commit 0212977

File tree

4 files changed

+124
-96
lines changed

4 files changed

+124
-96
lines changed

crates/wasmi/src/engine/translator/func2/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use self::{
1515
stack::{
1616
BlockControlFrame,
1717
ControlFrame,
18+
ControlFrameBase,
1819
ControlFrameKind,
1920
ElseControlFrame,
2021
IfControlFrame,

crates/wasmi/src/engine/translator/func2/stack/control.rs

Lines changed: 121 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,54 @@ impl From<ControlFrameKind> for ControlFrame {
329329
}
330330
}
331331

332-
impl ControlFrame {
333-
/// Makes the [`ControlFrame`] aware that there is a branch to it.
334-
pub fn branch_to(&mut self) {
332+
/// Trait implemented by control frame types that share a common API.
333+
pub trait ControlFrameBase {
334+
/// Returns the branch label of `self`.
335+
fn label(&self) -> LabelRef;
336+
337+
/// Returns `true` if there exists a branch to `self.`
338+
fn is_branched_to(&self) -> bool;
339+
340+
/// Makes `self` aware that there is a branch to it.
341+
fn branch_to(&mut self);
342+
343+
/// Returns the number of operands required for branching to `self`.
344+
fn len_branch_params(&self, engine: &Engine) -> u16;
345+
346+
/// Returns a reference to the [`Instruction::ConsumeFuel`] of `self`.
347+
///
348+
/// Returns `None` if fuel metering is disabled.
349+
fn consume_fuel_instr(&self) -> Option<Instr>;
350+
}
351+
352+
impl ControlFrameBase for ControlFrame {
353+
fn label(&self) -> LabelRef {
354+
match self {
355+
ControlFrame::Block(frame) => frame.label(),
356+
ControlFrame::Loop(frame) => frame.label(),
357+
ControlFrame::If(frame) => frame.label(),
358+
ControlFrame::Else(frame) => frame.label(),
359+
ControlFrame::Unreachable(_) => {
360+
panic!("invalid query for unreachable control frame: `ControlFrame::label`")
361+
}
362+
}
363+
}
364+
365+
fn is_branched_to(&self) -> bool {
366+
match self {
367+
ControlFrame::Block(frame) => frame.is_branched_to(),
368+
ControlFrame::Loop(frame) => frame.is_branched_to(),
369+
ControlFrame::If(frame) => frame.is_branched_to(),
370+
ControlFrame::Else(frame) => frame.is_branched_to(),
371+
ControlFrame::Unreachable(_) => {
372+
panic!(
373+
"invalid query for unreachable control frame: `ControlFrame::is_branched_to`"
374+
)
375+
}
376+
}
377+
}
378+
379+
fn branch_to(&mut self) {
335380
match self {
336381
ControlFrame::Block(frame) => frame.branch_to(),
337382
ControlFrame::Loop(frame) => frame.branch_to(),
@@ -343,8 +388,7 @@ impl ControlFrame {
343388
}
344389
}
345390

346-
/// Returns the number of operands required for branching to the [`ControlFrame`].
347-
pub fn len_branch_params(&self, engine: &Engine) -> u16 {
391+
fn len_branch_params(&self, engine: &Engine) -> u16 {
348392
match self {
349393
ControlFrame::Block(frame) => frame.len_branch_params(engine),
350394
ControlFrame::Loop(frame) => frame.len_branch_params(engine),
@@ -356,10 +400,7 @@ impl ControlFrame {
356400
}
357401
}
358402

359-
/// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`ControlFrame`] if any.
360-
///
361-
/// Returns `None` if fuel metering is disabled.
362-
pub fn consume_fuel_instr(&self) -> Option<Instr> {
403+
fn consume_fuel_instr(&self) -> Option<Instr> {
363404
match self {
364405
ControlFrame::Block(frame) => frame.consume_fuel_instr(),
365406
ControlFrame::Loop(frame) => frame.consume_fuel_instr(),
@@ -397,36 +438,31 @@ impl BlockControlFrame {
397438
self.ty
398439
}
399440

400-
/// Returns the number of operands required for branching to the [`BlockControlFrame`].
401-
pub fn len_branch_params(&self, engine: &Engine) -> u16 {
402-
self.ty.len_results(engine)
403-
}
404-
405441
/// Returns the height of the [`BlockControlFrame`].
406442
pub fn height(&self) -> usize {
407443
self.height.into()
408444
}
445+
}
446+
447+
impl ControlFrameBase for BlockControlFrame {
448+
fn label(&self) -> LabelRef {
449+
self.label
450+
}
409451

410-
/// Returns `true` if there are branches to this [`BlockControlFrame`].
411-
pub fn is_branched_to(&self) -> bool {
452+
fn is_branched_to(&self) -> bool {
412453
self.is_branched_to
413454
}
414455

415-
/// Makes the [`BlockControlFrame`] aware that there is a branch to it.
416-
pub fn branch_to(&mut self) {
456+
fn branch_to(&mut self) {
417457
self.is_branched_to = true;
418458
}
419459

420-
/// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`BlockControlFrame`] if any.
421-
///
422-
/// Returns `None` if fuel metering is disabled.
423-
pub fn consume_fuel_instr(&self) -> Option<Instr> {
424-
self.consume_fuel
460+
fn len_branch_params(&self, engine: &Engine) -> u16 {
461+
self.ty.len_results(engine)
425462
}
426463

427-
/// Returns the branch label of the [`BlockControlFrame`].
428-
pub fn label(&self) -> LabelRef {
429-
self.label
464+
fn consume_fuel_instr(&self) -> Option<Instr> {
465+
self.consume_fuel
430466
}
431467
}
432468

@@ -455,36 +491,31 @@ impl LoopControlFrame {
455491
self.ty
456492
}
457493

458-
/// Returns the number of operands required for branching to the [`LoopControlFrame`].
459-
pub fn len_branch_params(&self, engine: &Engine) -> u16 {
460-
self.ty.len_params(engine)
461-
}
462-
463494
/// Returns the height of the [`LoopControlFrame`].
464495
pub fn height(&self) -> usize {
465496
self.height.into()
466497
}
498+
}
467499

468-
/// Returns `true` if there are branches to this [`LoopControlFrame`].
469-
pub fn is_branched_to(&self) -> bool {
500+
impl ControlFrameBase for LoopControlFrame {
501+
fn label(&self) -> LabelRef {
502+
self.label
503+
}
504+
505+
fn is_branched_to(&self) -> bool {
470506
self.is_branched_to
471507
}
472508

473-
/// Makes the [`LoopControlFrame`] aware that there is a branch to it.
474-
pub fn branch_to(&mut self) {
509+
fn branch_to(&mut self) {
475510
self.is_branched_to = true;
476511
}
477512

478-
/// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`LoopControlFrame`] if any.
479-
///
480-
/// Returns `None` if fuel metering is disabled.
481-
pub fn consume_fuel_instr(&self) -> Option<Instr> {
482-
self.consume_fuel
513+
fn len_branch_params(&self, engine: &Engine) -> u16 {
514+
self.ty.len_params(engine)
483515
}
484516

485-
/// Returns the branch label of the [`LoopControlFrame`].
486-
pub fn label(&self) -> LabelRef {
487-
self.label
517+
fn consume_fuel_instr(&self) -> Option<Instr> {
518+
self.consume_fuel
488519
}
489520
}
490521

@@ -515,38 +546,11 @@ impl IfControlFrame {
515546
self.ty
516547
}
517548

518-
/// Returns the number of operands required for branching to the [`IfControlFrame`].
519-
pub fn len_branch_params(&self, engine: &Engine) -> u16 {
520-
self.ty.len_results(engine)
521-
}
522-
523549
/// Returns the height of the [`IfControlFrame`].
524550
pub fn height(&self) -> usize {
525551
self.height.into()
526552
}
527553

528-
/// Returns `true` if there are branches to this [`IfControlFrame`].
529-
pub fn is_branched_to(&self) -> bool {
530-
self.is_branched_to
531-
}
532-
533-
/// Makes the [`IfControlFrame`] aware that there is a branch to it.
534-
pub fn branch_to(&mut self) {
535-
self.is_branched_to = true;
536-
}
537-
538-
/// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`IfControlFrame`] if any.
539-
///
540-
/// Returns `None` if fuel metering is disabled.
541-
pub fn consume_fuel_instr(&self) -> Option<Instr> {
542-
self.consume_fuel
543-
}
544-
545-
/// Returns the branch label of the [`IfControlFrame`].
546-
pub fn label(&self) -> LabelRef {
547-
self.label
548-
}
549-
550554
/// Returns the [`IfReachability`] of the [`IfControlFrame`].
551555
pub fn reachability(&self) -> IfReachability {
552556
self.reachability
@@ -585,6 +589,28 @@ impl IfControlFrame {
585589
}
586590
}
587591

592+
impl ControlFrameBase for IfControlFrame {
593+
fn label(&self) -> LabelRef {
594+
self.label
595+
}
596+
597+
fn is_branched_to(&self) -> bool {
598+
self.is_branched_to
599+
}
600+
601+
fn branch_to(&mut self) {
602+
self.is_branched_to = true;
603+
}
604+
605+
fn len_branch_params(&self, engine: &Engine) -> u16 {
606+
self.ty.len_results(engine)
607+
}
608+
609+
fn consume_fuel_instr(&self) -> Option<Instr> {
610+
self.consume_fuel
611+
}
612+
}
613+
588614
/// The reachability of the `if` control flow frame.
589615
#[derive(Debug, Copy, Clone)]
590616
pub enum IfReachability {
@@ -678,36 +704,14 @@ impl ElseControlFrame {
678704
self.ty
679705
}
680706

681-
/// Returns the number of operands required for branching to the [`ElseControlFrame`].
682-
pub fn len_branch_params(&self, engine: &Engine) -> u16 {
683-
self.ty.len_results(engine)
684-
}
685-
686707
/// Returns the height of the [`ElseControlFrame`].
687708
pub fn height(&self) -> usize {
688709
self.height.into()
689710
}
690711

691-
/// Returns `true` if there are branches to this [`ElseControlFrame`].
692-
pub fn is_branched_to(&self) -> bool {
693-
self.is_branched_to
694-
}
695-
696-
/// Makes the [`ElseControlFrame`] aware that there is a branch to it.
697-
pub fn branch_to(&mut self) {
698-
self.is_branched_to = true;
699-
}
700-
701-
/// Returns a reference to the [`Instruction::ConsumeFuel`] of the [`ElseControlFrame`] if any.
702-
///
703-
/// Returns `None` if fuel metering is disabled.
704-
pub fn consume_fuel_instr(&self) -> Option<Instr> {
705-
self.consume_fuel
706-
}
707-
708-
/// Returns the branch label of the [`ElseControlFrame`].
709-
pub fn label(&self) -> LabelRef {
710-
self.label
712+
/// Returns the [`ElseReachability`] of the [`ElseReachability`].
713+
pub fn reachability(&self) -> ElseReachability {
714+
self.reachability
711715
}
712716

713717
/// Returns `true` if the `then` branch is reachable.
@@ -740,6 +744,28 @@ impl ElseControlFrame {
740744
}
741745
}
742746

747+
impl ControlFrameBase for ElseControlFrame {
748+
fn label(&self) -> LabelRef {
749+
self.label
750+
}
751+
752+
fn is_branched_to(&self) -> bool {
753+
self.is_branched_to
754+
}
755+
756+
fn branch_to(&mut self) {
757+
self.is_branched_to = true;
758+
}
759+
760+
fn len_branch_params(&self, engine: &Engine) -> u16 {
761+
self.ty.len_results(engine)
762+
}
763+
764+
fn consume_fuel_instr(&self) -> Option<Instr> {
765+
self.consume_fuel
766+
}
767+
}
768+
743769
/// The kind of a Wasm control frame.
744770
#[derive(Debug, Copy, Clone)]
745771
pub enum ControlFrameKind {

crates/wasmi/src/engine/translator/func2/stack/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub use self::{
1212
control::{
1313
BlockControlFrame,
1414
ControlFrame,
15+
ControlFrameBase,
1516
ControlFrameKind,
1617
ElseControlFrame,
1718
ElseReachability,

crates/wasmi/src/engine/translator/func2/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{ControlFrame, ControlFrameKind, FuncTranslator, LocalIdx};
22
use crate::{
33
core::{wasm, FuelCostsProvider, TrapCode},
44
engine::{
5-
translator::func2::{stack::IfReachability, Operand},
5+
translator::func2::{stack::IfReachability, ControlFrameBase, Operand},
66
BlockType,
77
},
88
ir::Instruction,

0 commit comments

Comments
 (0)