some docs for devs, added a few examples, implemented syscalls
This commit is contained in:
@@ -196,6 +196,77 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
|
||||
writeln!(writer, " jmp addr_{}", token.jmp)?;
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall0 => {
|
||||
writeln!(writer, " ; -- SYSCALL0")?;
|
||||
writeln!(writer, " pop rax")?;
|
||||
writeln!(writer, " syscall")?;
|
||||
writeln!(writer, " push rax")?;
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall1 => {
|
||||
writeln!(writer, " ; -- SYSCALL1")?;
|
||||
writeln!(writer, " pop rax")?;
|
||||
writeln!(writer, " pop rdi")?;
|
||||
writeln!(writer, " syscall")?;
|
||||
writeln!(writer, " push rax")?;
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall2 => {
|
||||
writeln!(writer, " ; -- SYSCALL2")?;
|
||||
writeln!(writer, " pop rax")?;
|
||||
writeln!(writer, " pop rdi")?;
|
||||
writeln!(writer, " pop rsi")?;
|
||||
writeln!(writer, " syscall")?;
|
||||
writeln!(writer, " push rax")?;
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall3 => {
|
||||
writeln!(writer, " ; -- SYSCALL3")?;
|
||||
writeln!(writer, " pop rax")?;
|
||||
writeln!(writer, " pop rdi")?;
|
||||
writeln!(writer, " pop rsi")?;
|
||||
writeln!(writer, " pop rdx")?;
|
||||
writeln!(writer, " syscall")?;
|
||||
writeln!(writer, " push rax")?;
|
||||
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall4 => {
|
||||
writeln!(writer, " ; -- SYSCALL4")?;
|
||||
writeln!(writer, " pop rax")?;
|
||||
writeln!(writer, " pop rdi")?;
|
||||
writeln!(writer, " pop rsi")?;
|
||||
writeln!(writer, " pop rdx")?;
|
||||
writeln!(writer, " pop r10")?;
|
||||
writeln!(writer, " syscall")?;
|
||||
writeln!(writer, " push rax")?;
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall5 => {
|
||||
writeln!(writer, " ; -- SYSCALL5")?;
|
||||
writeln!(writer, " pop rax")?;
|
||||
writeln!(writer, " pop rdi")?;
|
||||
writeln!(writer, " pop rsi")?;
|
||||
writeln!(writer, " pop rdx")?;
|
||||
writeln!(writer, " pop r10")?;
|
||||
writeln!(writer, " pop r8")?;
|
||||
writeln!(writer, " syscall")?;
|
||||
writeln!(writer, " push rax")?;
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall6 => {
|
||||
writeln!(writer, " ; -- SYSCALL6")?;
|
||||
writeln!(writer, " pop rax")?;
|
||||
writeln!(writer, " pop rdi")?;
|
||||
writeln!(writer, " pop rsi")?;
|
||||
writeln!(writer, " pop rdx")?;
|
||||
writeln!(writer, " pop r10")?;
|
||||
writeln!(writer, " pop r8")?;
|
||||
writeln!(writer, " pop r9")?;
|
||||
writeln!(writer, " syscall")?;
|
||||
writeln!(writer, " push rax")?;
|
||||
ti += 1;
|
||||
},
|
||||
}
|
||||
}
|
||||
writeln!(writer, "addr_{}:", ti)?;
|
||||
|
||||
@@ -17,7 +17,14 @@ pub enum OpType {
|
||||
Do,
|
||||
Mem,
|
||||
Load8,
|
||||
Store8
|
||||
Store8,
|
||||
Syscall0,
|
||||
Syscall1,
|
||||
Syscall2,
|
||||
Syscall3,
|
||||
Syscall4,
|
||||
Syscall5,
|
||||
Syscall6
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use crate::constants::OpType;
|
||||
use std::fmt::format;
|
||||
|
||||
use crate::{constants::OpType, util::logger};
|
||||
// use crate::util::logger;
|
||||
use color_eyre::Result;
|
||||
|
||||
mod syscalls;
|
||||
|
||||
fn stack_pop(stack: &mut Vec<u64>) -> Result<u64, &'static str> {
|
||||
match stack.pop() {
|
||||
Some(i) => Ok(i),
|
||||
@@ -12,7 +16,8 @@ fn stack_pop(stack: &mut Vec<u64>) -> Result<u64, &'static str> {
|
||||
pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
|
||||
let mut stack: Vec<u64> = Vec::new();
|
||||
let mut ti = 0;
|
||||
let mut mem: Vec<u64> = Vec::with_capacity(crate::compile::MEM_SZ as usize);
|
||||
let mut mem: Vec<u8> = vec![0; crate::compile::MEM_SZ as usize];
|
||||
|
||||
while ti < tokens.len() {
|
||||
let token = &tokens[ti];
|
||||
|
||||
@@ -48,14 +53,14 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
|
||||
OpType::Load8 => {
|
||||
let a = stack_pop(&mut stack)?;
|
||||
|
||||
stack.push(mem[a as usize]);
|
||||
stack.push(mem[a as usize] as u64);
|
||||
ti += 1;
|
||||
}
|
||||
OpType::Store8 => {
|
||||
let a = stack_pop(&mut stack)?;
|
||||
let b = stack_pop(&mut stack)?;
|
||||
|
||||
mem[b as usize] = a;
|
||||
mem[b as usize] = a as u8;
|
||||
ti += 1;
|
||||
}
|
||||
|
||||
@@ -118,6 +123,46 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
|
||||
OpType::End => {
|
||||
ti = token.jmp as usize;
|
||||
}
|
||||
OpType::Syscall0 => {
|
||||
todo!();
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall1 => {
|
||||
todo!();
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall2 => {
|
||||
todo!();
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall3 => {
|
||||
let rax = stack_pop(&mut stack)?;
|
||||
let rdi = stack_pop(&mut stack)?;
|
||||
let rsi = stack_pop(&mut stack)?;
|
||||
let rdx = stack_pop(&mut stack)?;
|
||||
|
||||
let ret = match rax {
|
||||
1 => syscalls::sys_write(rax, rdi, rsi, rdx, &mem),
|
||||
_ => {
|
||||
logger::error(format!("Syscall(3) #{} is not implemented", rax).as_str());
|
||||
return Err("Exiting");
|
||||
}
|
||||
};
|
||||
stack.push(ret);
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall4 => {
|
||||
todo!();
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall5 => {
|
||||
todo!();
|
||||
ti += 1;
|
||||
},
|
||||
OpType::Syscall6 => {
|
||||
todo!();
|
||||
ti += 1;
|
||||
},
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
13
src/interpret/linux_x86_64/syscalls.rs
Normal file
13
src/interpret/linux_x86_64/syscalls.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
pub fn sys_write(rax: u64, rdi: u64, rsi: u64, rdx: u64, mem: &Vec<u8> ) -> u64 {
|
||||
for c in rsi..rdx {
|
||||
match rdi {
|
||||
1 => print!("{}", unsafe { char::from_u32_unchecked((mem[(rsi + c) as usize]) as u32) }),
|
||||
2 => eprint!("{}", unsafe { char::from_u32_unchecked((mem[(rsi + c) as usize]) as u32) }),
|
||||
_ => panic!("Unknown file {}", rdi)
|
||||
};
|
||||
let _ = std::io::Write::flush(&mut std::io::stdout());
|
||||
let _ = std::io::Write::flush(&mut std::io::stderr());
|
||||
}
|
||||
rax
|
||||
}
|
||||
@@ -112,6 +112,14 @@ impl Parser {
|
||||
"!8" => tokens.push(Operator::new(OpType::Load8, 0, token.file.clone(), token.line, token.col)),
|
||||
"@8" => tokens.push(Operator::new(OpType::Store8, 0, token.file.clone(), token.line, token.col)),
|
||||
|
||||
"syscall0" => tokens.push(Operator::new(OpType::Syscall0, 0, token.file.clone(), token.line, token.col)),
|
||||
"syscall1" => tokens.push(Operator::new(OpType::Syscall1, 0, token.file.clone(), token.line, token.col)),
|
||||
"syscall2" => tokens.push(Operator::new(OpType::Syscall2, 0, token.file.clone(), token.line, token.col)),
|
||||
"syscall3" => tokens.push(Operator::new(OpType::Syscall3, 0, token.file.clone(), token.line, token.col)),
|
||||
"syscall4" => tokens.push(Operator::new(OpType::Syscall4, 0, token.file.clone(), token.line, token.col)),
|
||||
"syscall5" => tokens.push(Operator::new(OpType::Syscall5, 0, token.file.clone(), token.line, token.col)),
|
||||
"syscall6" => tokens.push(Operator::new(OpType::Syscall6, 0, token.file.clone(), token.line, token.col)),
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user