From 8906315af70ff22a767b41c669b87eff1fb10ce7 Mon Sep 17 00:00:00 2001 From: MCorange Date: Mon, 27 May 2024 01:57:17 +0300 Subject: [PATCH] uwu --- .gitignore | 1 + Cargo.lock | 6 +++ bfox-asmgen/Cargo.toml | 4 ++ bfox-asmgen/src/lib.rs | 67 ++++++++++++++++++++++++++ bfox-asmgen/src/targets/mod.rs | 2 + bfox-asmgen/src/targets/x86_64_nasm.rs | 24 +++++++++ bfox-parser/src/lib.rs | 2 +- bfox/src/cli.rs | 4 ++ bfox/src/main.rs | 9 +++- 9 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 bfox-asmgen/src/targets/mod.rs create mode 100644 bfox-asmgen/src/targets/x86_64_nasm.rs diff --git a/.gitignore b/.gitignore index 2f7896d..e9747d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ target/ +*.s \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 93b068a..7f8f352 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -82,6 +82,12 @@ dependencies = [ [[package]] name = "bfox-asmgen" version = "0.1.0" +dependencies = [ + "anyhow", + "bfox-parser", + "clap", + "log", +] [[package]] name = "bfox-parser" diff --git a/bfox-asmgen/Cargo.toml b/bfox-asmgen/Cargo.toml index fb6a097..849ee6f 100644 --- a/bfox-asmgen/Cargo.toml +++ b/bfox-asmgen/Cargo.toml @@ -6,3 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.86" +log = "0.4.21" +bfox-parser = { path = "../bfox-parser" } +clap = { version = "4.5.4", features = ["derive"] } diff --git a/bfox-asmgen/src/lib.rs b/bfox-asmgen/src/lib.rs index e69de29..393e924 100644 --- a/bfox-asmgen/src/lib.rs +++ b/bfox-asmgen/src/lib.rs @@ -0,0 +1,67 @@ +use std::{fmt::Display, io::{BufWriter, Write}, path::PathBuf}; +use anyhow::bail; +use bfox_parser::types::token::Token; +use clap::ValueEnum; + +mod targets; + +#[allow(non_camel_case_types)] +#[derive(Debug, Default, Clone, ValueEnum)] +pub enum AsmType { + #[default] + X86_64_NASM +} + +impl Display for AsmType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{self:?}") + } +} + +pub struct AsmGen { + asm_type: AsmType +} + + +impl AsmGen { + pub fn new() -> Self { + Self { + asm_type: Default::default(), + } + } + + pub fn set_asm_type(&mut self, t: AsmType) { + self.asm_type = t; + } + + pub fn generate(&mut self, tokens: Vec, obj_path: PathBuf) -> anyhow::Result<()> { + let asm_path = obj_path.with_extension("s"); + + let fd_ = std::fs::File::options() + .read(true) + .write(true) + .truncate(true) + .create(true) + .open(&asm_path); + + let fd; + match fd_ { + Ok(fd_) => fd = fd_, + Err(e) => { + log::error!("Failed to open file {asm_path:?}: {e}"); + bail!(""); + }, + } + + let mut bw = BufWriter::new(fd); + + match self.asm_type { + AsmType::X86_64_NASM => targets::compile_x86_64_nasm(self, tokens, &mut bw)?, + } + + bw.flush()?; + + Ok(()) + } +} + diff --git a/bfox-asmgen/src/targets/mod.rs b/bfox-asmgen/src/targets/mod.rs new file mode 100644 index 0000000..3d8aa68 --- /dev/null +++ b/bfox-asmgen/src/targets/mod.rs @@ -0,0 +1,2 @@ +mod x86_64_nasm; +pub use x86_64_nasm::compile_x86_64_nasm; \ No newline at end of file diff --git a/bfox-asmgen/src/targets/x86_64_nasm.rs b/bfox-asmgen/src/targets/x86_64_nasm.rs new file mode 100644 index 0000000..8caccc8 --- /dev/null +++ b/bfox-asmgen/src/targets/x86_64_nasm.rs @@ -0,0 +1,24 @@ +use std::{fs::File, io::{BufWriter, Write}}; +use anyhow::Result; +use bfox_parser::types::token::Token; + +use crate::AsmGen; + + +pub fn compile_x86_64_nasm(_asm_gen: &mut AsmGen, tokens: Vec, bw: &mut BufWriter) -> Result<()>{ + + writeln!(bw, "BITS 64")?; + writeln!(bw, "section .text")?; + writeln!(bw, "_start:")?; + + for t in tokens { + log::debug!("{t:?}"); + } + + // Exit + writeln!(bw, " mov rax, 60")?; + writeln!(bw, " mov rdi, 0")?; + writeln!(bw, " syscall")?; + + Ok(()) +} \ No newline at end of file diff --git a/bfox-parser/src/lib.rs b/bfox-parser/src/lib.rs index 67fc0ec..50e611b 100644 --- a/bfox-parser/src/lib.rs +++ b/bfox-parser/src/lib.rs @@ -2,7 +2,7 @@ use types::{token::Token, token::TokenType, Loc}; -mod types; +pub mod types; pub struct Parser { tokens: Vec, diff --git a/bfox/src/cli.rs b/bfox/src/cli.rs index 4d7fbcb..18b4a5c 100644 --- a/bfox/src/cli.rs +++ b/bfox/src/cli.rs @@ -7,6 +7,10 @@ pub(crate) struct CliArgs { #[arg(long, short)] pub assemly: bool, + + #[arg(long, short, default_value="x86-64-nasm")] + pub target: bfox_asmgen::AsmType, + /// Output file #[arg(long, short, default_value="./a.out")] pub output: camino::Utf8PathBuf, diff --git a/bfox/src/main.rs b/bfox/src/main.rs index 3a5c287..d4dc6b3 100644 --- a/bfox/src/main.rs +++ b/bfox/src/main.rs @@ -21,8 +21,15 @@ fn main() -> anyhow::Result<()> { log::debug!("{tokens:?}"); - if !cli.assemly { + let mut asmgen = bfox_asmgen::AsmGen::new(); + + asmgen.set_asm_type(cli.target); + + asmgen.generate(tokens.to_vec(), cli.input.into_std_path_buf())?; + + if cli.assemly { //TODO: Compile here + } Ok(())