diff --git a/kernel32.def b/kernel32.def new file mode 100644 index 0000000..92014a5 --- /dev/null +++ b/kernel32.def @@ -0,0 +1,5 @@ +LIBRARY KERNEL32.DLL +EXPORTS + WriteConsoleA + GetStdHandle + ExitProcess \ No newline at end of file diff --git a/libkernel32.a b/libkernel32.a new file mode 100644 index 0000000..d367433 Binary files /dev/null and b/libkernel32.a differ diff --git a/src/compile/win32_x86_64/commands.rs b/src/compile/win32_x86_64/commands.rs index 2b20a41..59f402a 100644 --- a/src/compile/win32_x86_64/commands.rs +++ b/src/compile/win32_x86_64/commands.rs @@ -4,7 +4,7 @@ use color_eyre::Result; use crate::compile::Folders; use crate::info; -pub fn linux_x86_64_compile_and_link(folders: &Folders, quiet: bool) -> Result<()> { +pub fn cmpile_and_link(folders: &Folders, quiet: bool) -> Result<()> { let nasm_args = [ "-fwin32", @@ -17,14 +17,12 @@ pub fn linux_x86_64_compile_and_link(folders: &Folders, quiet: bool) -> Result<( folders.of_o.to_str().unwrap(), "-o", folders.of_c.to_str().unwrap(), - "-L", - "C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0\\Lib", - "-l", - "kernel32" + "-nostdlib", "-nostartfiles", + "-lC:\\Users\\gvida\\@Projects\\rust\\mclang2\\libkernel32.a", ]; - let mut proc = if cfg!(target_os = "windows") { + let mut proc = if !cfg!(target_os = "windows") { return Ok(()); } else { Command::new("nasm") @@ -43,21 +41,21 @@ pub fn linux_x86_64_compile_and_link(folders: &Folders, quiet: bool) -> Result<( } - let mut proc2 = if cfg!(target_os = "windows") { + let mut proc2 = if !cfg!(target_os = "windows") { return Ok(()); } else { - Command::new("ld") + Command::new("gcc") .args(ld_args) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .spawn()? }; if !quiet { - info!("running 'ld {}'", ld_args.join(" ")); + info!("running 'gcc {}'", ld_args.join(" ")); } let exit2 = proc2.wait()?; if !quiet { - info!("ld process exited with code {}", exit2); + info!("gcc process exited with code {}", exit2); } @@ -65,13 +63,13 @@ pub fn linux_x86_64_compile_and_link(folders: &Folders, quiet: bool) -> Result<( Ok(()) } -pub fn linux_x86_64_run(bin: &Path, args: &[String], quiet: bool) -> Result { +pub fn run(bin: &Path, args: &[String], quiet: bool) -> Result { let bin = PathBuf::from( format!("./{}", bin.to_string_lossy()) ); - let mut proc = if cfg!(target_os = "windows") { + let mut proc = if !cfg!(target_os = "windows") { return Ok(0); } else { Command::new(bin.clone()) diff --git a/src/compile/win32_x86_64/mod.rs b/src/compile/win32_x86_64/mod.rs index 3e1526a..61aa9e7 100644 --- a/src/compile/win32_x86_64/mod.rs +++ b/src/compile/win32_x86_64/mod.rs @@ -2,8 +2,8 @@ mod commands; use std::{fs, io::{Write, BufWriter}}; use crate::{constants::{Operator, OpType, KeywordType}, Args}; use color_eyre::Result; -use commands::linux_x86_64_compile_and_link; -use commands::linux_x86_64_run; +use commands::cmpile_and_link; +use commands::run; use crate::constants::InstructionType; use super::Folders; @@ -18,6 +18,10 @@ pub fn compile(tokens: &[Operator], args: &Args, folders: &Folders) -> Result Result Result>(); @@ -409,9 +427,9 @@ pub fn compile(tokens: &[Operator], args: &Args, folders: &Folders) -> Result panic!("Unknown syscall {n}") + } + + } \ No newline at end of file diff --git a/test.mcl b/test.mcl index c23c68e..1400dd2 100644 --- a/test.mcl +++ b/test.mcl @@ -1,3 +1,3 @@ #ifdef __win32__ - 69 + 10 print #endif \ No newline at end of file diff --git a/test.nasm b/test.nasm new file mode 100644 index 0000000..e08930e --- /dev/null +++ b/test.nasm @@ -0,0 +1,62 @@ +BITS 64 +segment .text +extern WriteConsoleA +extern GetStdHandle +extern ExitProcess +print: + mov r9, -3689348814741910323 + sub rsp, 40 + mov BYTE [rsp+31], 10 + lea rcx, [rsp+30] +.L2: + mov rax, rdi + lea r8, [rsp+32] + mul r9 + mov rax, rdi + sub r8, rcx + shr rdx, 3 + lea rsi, [rdx+rdx*4] + add rsi, rsi + sub rax, rsi + add eax, 48 + mov BYTE [rcx], al + mov rax, rdi + mov rdi, rdx + mov rdx, rcx + sub rcx, 1 + cmp rax, 9 + ja .L2 + lea rax, [rsp+32] + mov edi, 1 + sub rdx, rax + xor eax, eax + lea rsi, [rsp+32+rdx] + mov rdx, r8 + sub rsp, 8+8+32 + mov ecx, -11 + call GetStdHandle + mov rcx, rax + mov r8, rdx + mov rdx, rsi + lea r9, [rsp-16] + mov qword [rsp-56], 0 + add rsp, 8+8+32+40 + call WriteConsoleA + ret +global _start +_start: +sub rsp, 8 +addr_0: + ;; -- push int 10 + mov rax, 10 + push rax +addr_1: + ;; -- print + pop rdi + call print +addr_2: + mov rcx, 0 + call ExitProcess +segment .data +segment .bss +mem: resb 640000 diff --git a/test.o b/test.o new file mode 100644 index 0000000..f78909a Binary files /dev/null and b/test.o differ