Skip to main content

Crate vihaco_cpu

Crate vihaco_cpu 

Source
Expand description

Checked word-stack CPU component.

Enable vihaco-cpu/typed-declarations to use the cpu runtime dialect and per-instruction Execute implementations. This forwards to the lower runtime crates; facade applications can instead enable vihaco/typed-declarations. Feature-off use retains the legacy instruction enums and CPUMessage.

The nondefault program-image-execution feature implies typed-declarations and switches CPU handlers to typed operands and stack result effects. vihaco_stdlib::ExecutionState owns locals, operands, frames, and the program counter. Heap storage remains on CPU and uses the same typed transfers.

With this feature, program instructions and their lowering helpers live in vihaco_stdlib::program; CPU exposes only computation and heap instructions. The following syntax description and example describe the typed-only route.

Typed instruction syntax retains readable literals until explicit lowering encodes them into runtime Word payloads. Branch and call targets accept numeric addresses or @name symbols; function references accept numeric indices or function symbols. String constants accept indices or quoted text. Loaders supply lower::LabelLookup, lower::FunctionLookup, and lower::StringIntern to resolve them. vihaco_stdlib::ProgramImage implements string interning using its section string table. Heap references remain numeric. For explicit metadata, vihaco_module::loader::MetadataLoweringContext implements all three lookup capabilities and rejects strings absent from the supplied table. It works with ParsedModule::lower_with_metadata in vihaco-syntax. label @name is retained in the parsed function’s label metadata without an executable slot. Labels, function_start, and function_end have no runtime instruction leaves. Composite source uses device::dialect::mnemonic, for example host::cpu::const_i64 -42. Generated aggregate bytecode codecs are separate work.

use vihaco_runtime::{Execute, NoMessage, Lower, Parse, Syntax, chumsky::Parser as _};
use vihaco_module::host::StackMemory;
use vihaco_cpu::{CPU, cpu, encode_i64, StepOutcome};
let instruction = cpu::ConstI64::<Syntax>::parser().parse("const_i64 -42")
    .into_result().unwrap();
let instruction = instruction.lower(&mut ()).unwrap();
assert_eq!(instruction, cpu::ConstI64(encode_i64(-42)));
let mut cpu = CPU::default();
assert_eq!(cpu.execute(&instruction, NoMessage::new()).unwrap(), StepOutcome::Continue);
assert_eq!(cpu.stack(), &[encode_i64(-42)]);

Modules§

cpu
word
The CPU’s raw word representation and its typed bit conversions.

Structs§

CPU

Functions§

decode_bool
Decodes a boolean word. Zero is false and every nonzero word is true.
decode_f32
Decodes an f32 from its IEEE-754 representation in the low 32 bits.
decode_f64
Decodes an f64 from its IEEE-754 representation.
decode_function_ref
Decodes a numeric function reference.
decode_heap_ref
Decodes a numeric heap reference.
decode_i32
Decodes an i32 from the low 32 bits of a word.
decode_i64
Decodes an i64 without changing its bit pattern.
decode_string_id
Decodes an interned string identifier.
decode_u32
Decodes a u32 from the low 32 bits of a word.
decode_u64
Decodes a u64 without changing its bit pattern.
encode_bool
Encodes a boolean as its canonical word representation, 0 or 1.
encode_f32
Encodes an f32 using its IEEE-754 representation in the low 32 bits.
encode_f64
Encodes an f64 using its IEEE-754 representation.
encode_function_ref
Encodes a numeric function reference.
encode_heap_ref
Encodes a numeric heap reference.
encode_i32
Encodes an i32 in the low 32 bits of a word.
encode_i64
Encodes an i64 without changing its bit pattern.
encode_string_id
Encodes an interned string identifier.
encode_u32
Encodes a u32 in the low 32 bits of a word.
encode_u64
Encodes a u64 without changing its bit pattern.

Type Aliases§

Word
One raw value in word-backed execution storage.