]> rethought.computer Git - sorel-lang.git/commitdiff
system calls keep/e74dae034b3c4b4869581221cb94a16231c24262
authorBryan English <bryan@rethought.computer>
Thu, 8 Jan 2026 05:44:11 +0000 (00:44 -0500)
committerBryan English <bryan@rethought.computer>
Tue, 10 Feb 2026 04:08:54 +0000 (04:08 +0000)
hylo-lang/Cargo.lock
hylo-lang/README.md
hylo-lang/examples/syscalls.hylo [new file with mode: 0644]
hylo-lang/hylo-interpret/Cargo.toml
hylo-lang/hylo-interpret/src/lib.rs
hylo-lang/hylo-ir/src/lib.rs
hylo-lang/hyloc/src/ir.rs

index 75faf2585bdad3acba903f1a0df23320d7a7f3e7..f782ca88e6abadc3d2f38ddfb078e326adad4fc7 100644 (file)
@@ -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"
index 0f13d52652477b0a70db128f6a0ef5d87e46a26c..6586566f2ca5c55f1cb789c9f94e3b8d608acb6e 100644 (file)
@@ -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 (file)
index 0000000..9aecc52
--- /dev/null
@@ -0,0 +1,6 @@
+: getpid
+  39 sys0
+  drop
+;
+
+getpid putn
index cc703faa972b6f8f2bf05cc7fb8992d99b000985..196666a7cf5e7b5d799b1f9a73801b774b719fe7 100644 (file)
@@ -5,3 +5,4 @@ edition = "2024"
 
 [dependencies]
 hylo-ir = { workspace = true }
+syscalls = "0.7.0"
index aaca00f9eae532da96427ae27bf3be5abe2c82f5..3ccdd29348b50dc57101e0f17bbf5fed243af6d8 100644 (file)
@@ -2,6 +2,8 @@ use hylo_ir::*;
 
 use std::collections::HashMap;
 
+use syscalls::*;
+
 pub struct Interpreter<'a> {
     module: &'a IRModule,
     data_stack: Vec<u64>,
@@ -42,6 +44,19 @@ impl<'a> Interpreter<'a> {
         }
     }
 
+    fn process_syscall_result(&mut self, result: Result<usize, Errno>) {
+        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;
index 9d3e4dbc1cdec36b99f55a75357d3fa978941a4c..906a57c8627fb92cdf35195c0bbec03a5508b557 100644 (file)
@@ -35,6 +35,15 @@ pub enum IR {
     If,
     Else,
     EndIf,
+
+    // System calls
+    Sys0,
+    Sys1,
+    Sys2,
+    Sys3,
+    Sys4,
+    Sys5,
+    Sys6,
 }
 
 #[derive(Serialize, Deserialize, Debug)]
index f1f5edf604094cf5b2b46729195191710ab9faba..7d0374eeac12490590b3a247d0a3d057c86651c8 100644 (file)
@@ -98,6 +98,13 @@ fn generate_internal(path: PathBuf, module: &Module, imported: &mut HashSet<Path
                         "*" => 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))
                     }