uwu
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
Reference in New Issue
Block a user