129 lines
4.5 KiB
Rust
129 lines
4.5 KiB
Rust
use prometeu_bytecode::Value;
|
|
|
|
use prometeu_bytecode::assembler::assemble;
|
|
use prometeu_bytecode::load_limits::LoadBudget;
|
|
use prometeu_bytecode::registry::*;
|
|
use prometeu_bytecode::registry_codec::PbxModule;
|
|
use prometeu_hal::syscalls::caps;
|
|
use prometeu_hal::vm_fault::VmFault;
|
|
use prometeu_hal::{HostContext, HostReturn, NativeInterface};
|
|
use prometeu_vm::resource_budget::RuntimeLimits;
|
|
use prometeu_vm::{ValidatedProgramImage, VirtualMachine, VmInitError};
|
|
|
|
pub struct Native;
|
|
impl NativeInterface for Native {
|
|
fn syscall(
|
|
&mut self,
|
|
_: u32,
|
|
_: &[Value],
|
|
_: &mut HostReturn,
|
|
_: &mut HostContext,
|
|
) -> Result<(), VmFault> {
|
|
panic!("no syscall in callable fixtures")
|
|
}
|
|
}
|
|
pub fn param(id: u32) -> NamedType {
|
|
NamedType { type_id: TypeId(id), name: None }
|
|
}
|
|
pub fn sig(parameters: &[u32], results: &[u32]) -> Signature {
|
|
Signature {
|
|
parameters: parameters.iter().map(|&i| param(i)).collect(),
|
|
results: results.iter().map(|&i| TypeId(i)).collect(),
|
|
}
|
|
}
|
|
pub fn types() -> Vec<TypeDecl> {
|
|
vec![
|
|
TypeKind::Builtin(BuiltinId::INT32),
|
|
TypeKind::Builtin(BuiltinId::FLOAT),
|
|
TypeKind::Builtin(BuiltinId::VEC2),
|
|
TypeKind::Builtin(BuiltinId::STRING),
|
|
TypeKind::Fn(sig(&[], &[0])),
|
|
TypeKind::Struct(vec![param(0)]),
|
|
TypeKind::Fn(sig(&[], &[4, 4])),
|
|
TypeKind::Trait(vec![MethodDecl { name: None, signature: sig(&[], &[0]) }]),
|
|
TypeKind::Struct(vec![param(0)]),
|
|
TypeKind::Fn(sig(&[], &[])),
|
|
TypeKind::Array(TypeId(4)),
|
|
TypeKind::Builtin(BuiltinId::DYNAMIC),
|
|
TypeKind::Fn(sig(&[2], &[2])),
|
|
TypeKind::Fn(sig(&[], &[2])),
|
|
TypeKind::Fn(sig(&[], &[1, 1])),
|
|
TypeKind::Struct(vec![param(4)]),
|
|
TypeKind::Fn(sig(&[], &[3])),
|
|
TypeKind::Fn(sig(&[], &[7, 11, 3, 2])),
|
|
TypeKind::Fn(sig(&[4], &[4])),
|
|
]
|
|
.into_iter()
|
|
.map(|kind| TypeDecl { name: None, kind })
|
|
.collect()
|
|
}
|
|
pub fn image(
|
|
bodies: &[(&str, Signature, Vec<TypeId>)],
|
|
implementations: Vec<ImplementationDecl>,
|
|
) -> Result<std::sync::Arc<ValidatedProgramImage>, VmInitError> {
|
|
image_constants(bodies, implementations, &[])
|
|
}
|
|
pub fn image_constants(
|
|
bodies: &[(&str, Signature, Vec<TypeId>)],
|
|
implementations: Vec<ImplementationDecl>,
|
|
constants: &[prometeu_bytecode::model::ConstantPoolEntry],
|
|
) -> Result<std::sync::Arc<ValidatedProgramImage>, VmInitError> {
|
|
let bytes = bytes(bodies, implementations, constants)?;
|
|
ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(Default::default()), caps::NONE)
|
|
}
|
|
pub fn bytes(
|
|
bodies: &[(&str, Signature, Vec<TypeId>)],
|
|
implementations: Vec<ImplementationDecl>,
|
|
constants: &[prometeu_bytecode::model::ConstantPoolEntry],
|
|
) -> Result<Vec<u8>, prometeu_bytecode::load_limits::LoadFailure> {
|
|
bytes_with_syscalls(bodies, implementations, constants, &[])
|
|
}
|
|
pub fn bytes_with_syscalls(
|
|
bodies: &[(&str, Signature, Vec<TypeId>)],
|
|
implementations: Vec<ImplementationDecl>,
|
|
constants: &[prometeu_bytecode::model::ConstantPoolEntry],
|
|
syscalls: &[prometeu_bytecode::model::SyscallDecl],
|
|
) -> Result<Vec<u8>, prometeu_bytecode::load_limits::LoadFailure> {
|
|
let mut code = vec![];
|
|
let mut functions = vec![];
|
|
for (source, signature, captures) in bodies {
|
|
let body = assemble(source).unwrap();
|
|
functions.push(FunctionDecl {
|
|
lifecycle_role: Default::default(),
|
|
signature: signature.clone(),
|
|
captures: captures.clone(),
|
|
code_offset: code.len() as u32,
|
|
code_len: body.len() as u32,
|
|
local_slots: 4,
|
|
max_stack_slots: 32,
|
|
..Default::default()
|
|
});
|
|
code.extend(body);
|
|
}
|
|
let mut budget = LoadBudget::new(Default::default());
|
|
let registry = Registry::resolve(
|
|
RegistryDeclarations { types: types(), functions, implementations },
|
|
code.len(),
|
|
&mut budget,
|
|
)?;
|
|
let bytes = PbxModule {
|
|
registry: ®istry,
|
|
constants,
|
|
code: &code,
|
|
debug: None,
|
|
exports: &[],
|
|
syscalls,
|
|
}
|
|
.serialize(&mut budget)?;
|
|
Ok(bytes)
|
|
}
|
|
pub fn vm(bodies: &[(&str, Signature, Vec<TypeId>)]) -> VirtualMachine {
|
|
let mut vm =
|
|
VirtualMachine::from_image(image(bodies, vec![]).unwrap(), RuntimeLimits::default());
|
|
vm.prepare_boot_call().unwrap();
|
|
vm
|
|
}
|
|
pub fn run(vm: &mut VirtualMachine, budget: u64) -> prometeu_vm::BudgetReport {
|
|
vm.run_budget(budget, &mut Native, &mut HostContext::new(None)).unwrap()
|
|
}
|