Skip to content

Commit

Permalink
[X64] should work now but i get a 0x80000003 exception error when deb…
Browse files Browse the repository at this point in the history
…ugging with VSCode debugging tools and when just running via cargo run --example simple it just hangs
  • Loading branch information
Cr0a3 committed Jul 14, 2024
1 parent 2422dbb commit 0f1810c
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 51 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
lazy_static = "1.5.0"
object = { version = "0.36.1", features = ["write"] }
once_cell = "1.19.0"
12 changes: 0 additions & 12 deletions examples/error.rs

This file was deleted.

4 changes: 2 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::error::Error;
use Ygen::{prelude::*, Target::{initializeX64Target, CallConv}};
use Ygen::{prelude::*, PassManager::Passes::PreComputeValue, Target::{initializeX64Target, CallConv}};

pub fn main() -> Result<(), Box<dyn Error>> {

initializeX64Target();
initializeX64Target(CallConv::WindowsFastCall);

let mut module = Module();

Expand Down
12 changes: 6 additions & 6 deletions src/IR/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Ir for Return<Type> {
}

fn compile(&self) -> Vec<String> {
TARGETS.get().unwrap().lock().unwrap().getCompileFuncRetType()(self)
TARGETS.lock().unwrap().getCompileFuncRetType()(self)
}
}

Expand Down Expand Up @@ -157,7 +157,7 @@ impl Ir for Return<Var> {
}

fn compile(&self) -> Vec<String> {
TARGETS.get().unwrap().lock().unwrap().getCompileFuncRetVar()(self)
TARGETS.lock().unwrap().getCompileFuncRetVar()(self)
}
}

Expand Down Expand Up @@ -207,7 +207,7 @@ impl Ir for Add<Type, Type, Var> {
}

fn compile(&self) -> Vec<String> {
TARGETS.get().unwrap().lock().unwrap().getCompileFuncAddTypeType()(self)
TARGETS.lock().unwrap().getCompileFuncForAddTypeType()(self)
}
}

Expand Down Expand Up @@ -257,7 +257,7 @@ impl Ir for Add<Var, Var, Var> {
}

fn compile(&self) -> Vec<String> {
TARGETS.get().unwrap().lock().unwrap().getCompileFuncAddVarVar()(self)
TARGETS.lock().unwrap().getCompileFuncForAddVarVar()(self)
}
}

Expand Down Expand Up @@ -299,7 +299,7 @@ impl Ir for ConstAssign<Var, Type> {
}

fn compile(&self) -> Vec<String> {
TARGETS.get().unwrap().lock().unwrap().getCompileFuncConstAssign()(self)
TARGETS.lock().unwrap().getCompileFuncForConstAssign()(self)
}
}

Expand Down Expand Up @@ -381,7 +381,7 @@ pub(crate) trait Ir: Debug + Any {
/// Clones the node into a box of `Box<dyn Ir>`
fn clone_box(&self) -> Box<dyn Ir>;

/// Compiles the node based on the initialized TARGETS.get().unwrap().lock().unwrap()
/// Compiles the node based on the initialized TARGETS.lock().unwrap().lock().unwrap()
fn compile(&self) -> Vec<String>;
}

Expand Down
91 changes: 84 additions & 7 deletions src/Target/registry.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
use std::{collections::HashMap, sync::Mutex};
use std::{collections::{HashMap, VecDeque}, fmt::Display, sync::Mutex};

use once_cell::sync::OnceCell;
use lazy_static::lazy_static;

use crate::prelude::{Return, Type, Var};
use crate::prelude::{ir::*, Type, Var};

use super::{Arch, CallConv};

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct BackendInfos {
pub(crate) varsStorage: HashMap<Var, VarStorage>,
pub(crate) currStackOffsetForLocalVars: isize,
pub(crate) openUsableRegisters: VecDeque<String>,
}

impl BackendInfos {
pub(crate) fn new() -> Self {
Self {
varsStorage: HashMap::new(),
currStackOffsetForLocalVars: 0,
openUsableRegisters: VecDeque::new(),
}
}

pub(crate) fn insertVar(&mut self, var: Var, store: VarStorage) {
self.varsStorage.insert(var, store);
}

pub(crate) fn getOpenReg(&mut self) -> Option<String> {
self.openUsableRegisters.pop_front()
}
}


Expand All @@ -23,6 +37,16 @@ pub(crate) enum VarStorage {
Register(String),
Memory(String),
}

impl Display for VarStorage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
VarStorage::Memory(mem) => mem,
VarStorage::Register(reg) => reg,
})
}
}

pub(crate) type CompileFunc<T> = fn(&T/*, &mut BackendInfos*/) -> Vec<String>;

/// The Target Registry: stores if a target was already initialized
Expand All @@ -31,11 +55,16 @@ pub struct TargetRegistry {
pub(crate) inited_targets: Vec<Arch>,
funcForRetType: HashMap<Arch, CompileFunc<Return<Type>>>,
funcForRetVar: HashMap<Arch, CompileFunc<Return<Var>>>,
funcForConstAssign: HashMap<Arch, CompileFunc<ConstAssign<Var, Type>>>,
funcForAddVarVar: HashMap<Arch, CompileFunc<Add<Var, Var, Var>>>,
funcForAddTypeType: HashMap<Arch, CompileFunc<Add<Type, Type, Var>>>,
pub(crate) backend: BackendInfos,
pub(crate) call: CallConv,
}

pub(crate) static TARGETS: OnceCell<Mutex<TargetRegistry>> = OnceCell::new();
lazy_static! {
pub(crate) static ref TARGETS: Mutex<TargetRegistry> = Mutex::new( TargetRegistry::new() );
}

impl TargetRegistry {
/// Creates a new instance
Expand All @@ -44,8 +73,11 @@ impl TargetRegistry {
inited_targets: vec![],
funcForRetType: HashMap::new(),
funcForRetVar: HashMap::new(),
funcForConstAssign: HashMap::new(),
funcForAddVarVar: HashMap::new(),
funcForAddTypeType: HashMap::new(),
call: CallConv::SystemV,
backend: BackendInfos { varsStorage: HashMap::new() },
backend: BackendInfos::new(),
}
}

Expand All @@ -65,8 +97,6 @@ impl TargetRegistry {
} else { todo!()}
}



/// sets the callback for compiling the return ir node into asm
pub(crate) fn setCompileFuncForRetVar(&mut self, arch: Arch, callback: CompileFunc<Return<Var>>) {
if !self.funcForRetVar.contains_key(&arch) {
Expand All @@ -83,6 +113,53 @@ impl TargetRegistry {
} else { todo!()}
}

/// sets the callback for compiling the const assign node into asm
pub(crate) fn setCompileFuncForConstAssign(&mut self, arch: Arch, callback: CompileFunc<ConstAssign<Var, Type>>) {
if !self.funcForConstAssign.contains_key(&arch) {
self.funcForConstAssign.insert(arch, callback);
}
}

/// gets the callback for compiling the const assign into asm
pub(crate) fn getCompileFuncForConstAssign(&self) -> CompileFunc<ConstAssign<Var, Type>> {
if let Some(last_arch) = self.inited_targets.last() {
if let Some(func) = self.funcForConstAssign.get(last_arch) {
*func
} else { todo!() }
} else { todo!()}
}

/// sets the callback for compiling the add var var ir node into asm
pub(crate) fn setCompileFuncForAddVarVar(&mut self, arch: Arch, callback: CompileFunc<Add<Var, Var, Var>>) {
if !self.funcForAddVarVar.contains_key(&arch) {
self.funcForAddVarVar.insert(arch, callback);
}
}

/// gets the callback for compiling the add var var node into into asm
pub(crate) fn getCompileFuncForAddVarVar(&self) -> CompileFunc<Add<Var, Var, Var>> {
if let Some(last_arch) = self.inited_targets.last() {
if let Some(func) = self.funcForAddVarVar.get(last_arch) {
*func
} else { todo!() }
} else { todo!()}
}

/// sets the callback for compiling the add var var ir node into asm
pub(crate) fn setCompileFuncForAddTypeType(&mut self, arch: Arch, callback: CompileFunc<Add<Type, Type, Var>>) {
if !self.funcForAddTypeType.contains_key(&arch) {
self.funcForAddTypeType.insert(arch, callback);
}
}

/// gets the callback for compiling the add var var node into into asm
pub(crate) fn getCompileFuncForAddTypeType(&self) -> CompileFunc<Add<Type, Type, Var>> {
if let Some(last_arch) = self.inited_targets.last() {
if let Some(func) = self.funcForAddTypeType.get(last_arch) {
*func
} else { todo!() }
} else { todo!()}
}

/// Sets an architecture as initialized
pub fn set_inited(&mut self, arch: Arch) {
Expand Down
Loading

0 comments on commit 0f1810c

Please sign in to comment.