From e74dae034b3c4b4869581221cb94a16231c24262 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Thu, 8 Jan 2026 00:44:11 -0500 Subject: [PATCH] system calls --- hylo-lang/Cargo.lock | 23 ++++++++++ hylo-lang/README.md | 2 +- hylo-lang/examples/syscalls.hylo | 6 +++ hylo-lang/hylo-interpret/Cargo.toml | 1 + hylo-lang/hylo-interpret/src/lib.rs | 66 ++++++++++++++++++++++++++++- hylo-lang/hylo-ir/src/lib.rs | 9 ++++ hylo-lang/hyloc/src/ir.rs | 7 +++ 7 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 hylo-lang/examples/syscalls.hylo diff --git a/hylo-lang/Cargo.lock b/hylo-lang/Cargo.lock index 75faf25..f782ca8 100644 --- a/hylo-lang/Cargo.lock +++ b/hylo-lang/Cargo.lock @@ -19,6 +19,7 @@ name = "hylo-interpret" version = "0.1.0" dependencies = [ "hylo-ir", + "syscalls", ] [[package]] @@ -85,6 +86,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", + "serde_derive", ] [[package]] @@ -107,6 +109,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_repr" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175ee3e80ae9982737ca543e96133087cbd9a485eecc3bc4de9c1a37b47ea59c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "serde_yaml" version = "0.9.34+deprecated" @@ -131,6 +144,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syscalls" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90db46b5b4962319605d435986c775ea45a0ad2561c09e1d5372b89afeb49cf4" +dependencies = [ + "serde", + "serde_repr", +] + [[package]] name = "unicode-ident" version = "1.0.22" diff --git a/hylo-lang/README.md b/hylo-lang/README.md index 0f13d52..6586566 100644 --- a/hylo-lang/README.md +++ b/hylo-lang/README.md @@ -6,6 +6,6 @@ The name means "it's high-level and low-level at the same time". ## TODO * [x] Imports -* [ ] Syscalls +* [x] Syscalls * [ ] Structs * [ ] many, many more things diff --git a/hylo-lang/examples/syscalls.hylo b/hylo-lang/examples/syscalls.hylo new file mode 100644 index 0000000..9aecc52 --- /dev/null +++ b/hylo-lang/examples/syscalls.hylo @@ -0,0 +1,6 @@ +: getpid + 39 sys0 + drop +; + +getpid putn diff --git a/hylo-lang/hylo-interpret/Cargo.toml b/hylo-lang/hylo-interpret/Cargo.toml index cc703fa..196666a 100644 --- a/hylo-lang/hylo-interpret/Cargo.toml +++ b/hylo-lang/hylo-interpret/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" [dependencies] hylo-ir = { workspace = true } +syscalls = "0.7.0" diff --git a/hylo-lang/hylo-interpret/src/lib.rs b/hylo-lang/hylo-interpret/src/lib.rs index aaca00f..3ccdd29 100644 --- a/hylo-lang/hylo-interpret/src/lib.rs +++ b/hylo-lang/hylo-interpret/src/lib.rs @@ -2,6 +2,8 @@ use hylo_ir::*; use std::collections::HashMap; +use syscalls::*; + pub struct Interpreter<'a> { module: &'a IRModule, data_stack: Vec, @@ -42,6 +44,19 @@ impl<'a> Interpreter<'a> { } } + fn process_syscall_result(&mut self, result: Result) { + match result { + Ok(result) => { + self.data_stack.push(result as u64); + self.data_stack.push(0); + } + Err(err) => { + self.data_stack.push(0); + self.data_stack.push(err.into_raw() as u64); + } + } + } + pub fn run(&mut self) { let mut looking_for_endif = false; loop { @@ -122,9 +137,58 @@ impl<'a> Interpreter<'a> { } }, IR::EndIf => {}, + IR::Sys0 => { + let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32); + self.process_syscall_result(unsafe { syscall!(call_num) }); + }, + IR::Sys1 => { + let a1 = self.data_stack.pop().unwrap(); + let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32); + self.process_syscall_result(unsafe { syscall!(call_num, a1) }); + }, + IR::Sys2 => { + let a2 = self.data_stack.pop().unwrap(); + let a1 = self.data_stack.pop().unwrap(); + let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32); + self.process_syscall_result(unsafe { syscall!(call_num, a1, a2) }); + }, + IR::Sys3 => { + let a3 = self.data_stack.pop().unwrap(); + let a2 = self.data_stack.pop().unwrap(); + let a1 = self.data_stack.pop().unwrap(); + let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32); + self.process_syscall_result(unsafe { syscall!(call_num, a1, a2, a3) }); + }, + IR::Sys4 => { + let a4 = self.data_stack.pop().unwrap(); + let a3 = self.data_stack.pop().unwrap(); + let a2 = self.data_stack.pop().unwrap(); + let a1 = self.data_stack.pop().unwrap(); + let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32); + self.process_syscall_result(unsafe { syscall!(call_num, a1, a2, a3, a4) }); + }, + IR::Sys5 => { + let a5 = self.data_stack.pop().unwrap(); + let a4 = self.data_stack.pop().unwrap(); + let a3 = self.data_stack.pop().unwrap(); + let a2 = self.data_stack.pop().unwrap(); + let a1 = self.data_stack.pop().unwrap(); + let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32); + self.process_syscall_result(unsafe { syscall!(call_num, a1, a2, a3, a4, a5) }); + }, + IR::Sys6 => { + let a6 = self.data_stack.pop().unwrap(); + let a5 = self.data_stack.pop().unwrap(); + let a4 = self.data_stack.pop().unwrap(); + let a3 = self.data_stack.pop().unwrap(); + let a2 = self.data_stack.pop().unwrap(); + let a1 = self.data_stack.pop().unwrap(); + let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32); + self.process_syscall_result(unsafe { syscall!(call_num, a1, a2, a3, a4, a5, a6) }); + }, _ => { println!("Instruction not implemented."); - } + }, } } self.instruction_pointer += 1; diff --git a/hylo-lang/hylo-ir/src/lib.rs b/hylo-lang/hylo-ir/src/lib.rs index 9d3e4db..906a57c 100644 --- a/hylo-lang/hylo-ir/src/lib.rs +++ b/hylo-lang/hylo-ir/src/lib.rs @@ -35,6 +35,15 @@ pub enum IR { If, Else, EndIf, + + // System calls + Sys0, + Sys1, + Sys2, + Sys3, + Sys4, + Sys5, + Sys6, } #[derive(Serialize, Deserialize, Debug)] diff --git a/hylo-lang/hyloc/src/ir.rs b/hylo-lang/hyloc/src/ir.rs index f1f5edf..7d0374e 100644 --- a/hylo-lang/hyloc/src/ir.rs +++ b/hylo-lang/hyloc/src/ir.rs @@ -98,6 +98,13 @@ fn generate_internal(path: PathBuf, module: &Module, imported: &mut HashSet IR::MultiplyU64, "/" => IR::DivideU64, "import" => IR::Import, + "sys0" => IR::Sys0, + "sys1" => IR::Sys1, + "sys2" => IR::Sys2, + "sys3" => IR::Sys3, + "sys4" => IR::Sys4, + "sys5" => IR::Sys5, + "sys6" => IR::Sys6, // TODO num type specfic math like `+:i32`, etc. _ => IR::Call(String::from(*word)) } -- 2.43.0