uwu
This commit is contained in:
parent
93b1fe5979
commit
8906315af7
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
target/
|
||||
*.s
|
6
Cargo.lock
generated
6
Cargo.lock
generated
|
@ -82,6 +82,12 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "bfox-asmgen"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bfox-parser",
|
||||
"clap",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bfox-parser"
|
||||
|
|
|
@ -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"] }
|
||||
|
|
|
@ -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<Token>, 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(())
|
||||
}
|
||||
}
|
||||
|
2
bfox-asmgen/src/targets/mod.rs
Normal file
2
bfox-asmgen/src/targets/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
mod x86_64_nasm;
|
||||
pub use x86_64_nasm::compile_x86_64_nasm;
|
24
bfox-asmgen/src/targets/x86_64_nasm.rs
Normal file
24
bfox-asmgen/src/targets/x86_64_nasm.rs
Normal file
|
@ -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<Token>, bw: &mut BufWriter<File>) -> 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(())
|
||||
}
|
|
@ -2,7 +2,7 @@ use types::{token::Token, token::TokenType, Loc};
|
|||
|
||||
|
||||
|
||||
mod types;
|
||||
pub mod types;
|
||||
|
||||
pub struct Parser {
|
||||
tokens: Vec<Token>,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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(())
|
||||
|
|
Loading…
Reference in New Issue
Block a user