• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

henrythasler / rust-tiny-wasm / 22976380108

11 Mar 2026 09:56PM UTC coverage: 61.644% (-5.1%) from 66.791%
22976380108

push

github

henrythasler
add control and value stack

1 of 27 new or added lines in 3 files covered. (3.7%)

2 existing lines in 2 files now uncovered.

180 of 292 relevant lines covered (61.64%)

1.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/compiler/function.rs
1
use super::*;
2
use crate::assembler::aarch64::*;
3
use crate::assembler::{emit_epilogue, emit_prologue};
4
use crate::loader::Code;
5
use crate::loader::webassembly::Webassembly_ValTypes;
6

7
enum Opcode {
8
    Func,
9
    Block,
10
    Loop,
11
    If,
12
    Else,
13
}
14

15
struct ControlFrame {
16
    opcode: Opcode,
17
    start_types: Vec<Webassembly_ValTypes>,
18
    end_types: Vec<Webassembly_ValTypes>,
19
    stack_height: usize,
20
}
21

22
struct StackElement {
23
    location: Reg,
24
    val_type: Webassembly_ValTypes,
25
}
26

27
pub fn compile_function(entry: &Code, machinecode: &mut Vec<u32>) {
×
28
    // every functions starts with an epilogue to save the initial state and create a new stack frame
UNCOV
29
    emit_prologue(machinecode);
×
30

31
    // Control stack is initialized with the (implicit) outer func-block
NEW
32
    let mut control_stack: Vec<ControlFrame> = vec![ControlFrame {
×
NEW
33
        opcode: Opcode::Func,
×
NEW
34
        start_types: vec![], // FIXME: insert function parameters
×
NEW
35
        end_types: vec![],   // FIXME: insert result types
×
NEW
36
        stack_height: 0,
×
NEW
37
    }];
×
38

39
    // Value stack starts empty
NEW
40
    let mut value_stack: Vec<StackElement> = vec![];
×
41

42
    // iterate over WebAssembly opcodes and emit machinecode instructions
43
    let mut iter = entry.code.iter();
×
NEW
44
    'expression: while let Some(&opcode) = iter.next() {
×
45
        print!("{:02X?} ", opcode);
×
46

47
        // ret
NEW
48
        if opcode == 0x0f {
×
NEW
49
            // FIXME: Manage Control Stack and add jump entry to backpatch list
×
NEW
50
        }
×
51
        // end
NEW
52
        else if opcode == 0x0b {
×
NEW
53
            let frame = control_stack
×
NEW
54
                .pop()
×
NEW
55
                .expect("control stack should contain at least one element on 'end' opcode");
×
56

NEW
57
            assert_eq!(
×
NEW
58
                value_stack.len(),
×
NEW
59
                frame.stack_height + frame.end_types.len()
×
60
            );
NEW
61
            let mut results = value_stack.split_off(frame.end_types.len());
×
NEW
62
            value_stack.truncate(frame.stack_height);
×
NEW
63
            value_stack.append(&mut results);
×
64

NEW
65
            match frame.opcode {
×
66
                Opcode::Func => {
67
                    // FIXME: validate control frame vs. stack
68
                    // FIXME: backpatch ret instructions here
69
                    // FIXME: if FrameOpcode::Func, exit while-loop and end function
NEW
70
                    break 'expression;
×
71
                }
NEW
72
                _ => {}
×
73
            }
74
            /*
75
               // Validate return values match expected types
76
               assert value_stack.height == frame.height + frame.end_types.length
77
               assert value_stack.top(frame.end_types.length) == frame.end_types
78

79
               // Clean up value stack
80
               results = value_stack.pop(frame.end_types.length)
81
               value_stack.truncate(frame.height)
82
               value_stack.push(results)
83

84
               if frame.opcode == func:
85
                   // Function exit: results stay on stack for caller
86
                   return results
87
            */
NEW
88
        }
×
89
    }
90
    println!();
×
91

92
    // FIXME: move result to r0
93

94
    // restore initial state before returning to the caller
95
    emit_epilogue(machinecode);
×
96
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc