Skip to content

Commit

Permalink
Improvements to interop, rewrote the C exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Sep 17, 2024
1 parent f6e4308 commit a628dc3
Show file tree
Hide file tree
Showing 16 changed files with 1,105 additions and 63 deletions.
22 changes: 11 additions & 11 deletions cilly/src/bin/linker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn get_out_path(args: &[String]) -> &str {
&args[1 + args
.iter()
.position(|arg| *arg == "-o")
.expect(&format!("No output file! {args:?}"))]
.unwrap_or_else(|| panic!("No output file! {args:?}"))]
}
#[cfg(target_os = "windows")]
fn get_out_path<'a>(args: &'a [String]) -> &'a str {
Expand Down Expand Up @@ -374,14 +374,17 @@ fn main() {
}
}),
);
cilly::v2::builtins::atomics::generate_all_atomics(&mut final_assembly, &mut overrides);
cilly::v2::builtins::casts::insert_casts(&mut final_assembly, &mut overrides);
cilly::v2::builtins::select::generate_int_selects(&mut final_assembly, &mut overrides);
cilly::v2::builtins::insert_heap(&mut final_assembly, &mut overrides);
cilly::v2::builtins::instert_threading(&mut final_assembly, &mut overrides);
cilly::v2::builtins::insert_swap_at_generic(&mut final_assembly, &mut overrides);
cilly::v2::builtins::insert_bounds_check(&mut final_assembly, &mut overrides);
cilly::v2::builtins::math::math(&mut final_assembly, &mut overrides);
if !*C_MODE {
cilly::v2::builtins::atomics::generate_all_atomics(&mut final_assembly, &mut overrides);
cilly::v2::builtins::casts::insert_casts(&mut final_assembly, &mut overrides);
cilly::v2::builtins::insert_heap(&mut final_assembly, &mut overrides);
cilly::v2::builtins::instert_threading(&mut final_assembly, &mut overrides);

cilly::v2::builtins::math::math(&mut final_assembly, &mut overrides);
}
// Ensure the cctor and tcctor exist!
let _ = final_assembly.tcctor();
let _ = final_assembly.cctor();
Expand All @@ -407,15 +410,12 @@ fn main() {
let mut fuel = final_assembly.fuel_from_env().fraction(0.5);
final_assembly.opt(&mut fuel);
final_assembly.eliminate_dead_code();
//final_assembly.typecheck();

final_assembly
.save_tmp(&mut std::fs::File::create(path.with_extension("cilly2")).unwrap())
.unwrap();
if *C_MODE {
final_assembly.export(
&path,
cilly::v2::il_exporter::ILExporter::new(*ILASM_FLAVOUR, is_lib),
);
final_assembly.export(&path, cilly::v2::c_exporter::CExporter::new(is_lib));
} else if *JS_MODE {
todo!();
} else if *JAVA_MODE {
Expand Down
1 change: 1 addition & 0 deletions cilly/src/utilis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite {
if asm.contains_fn(&init_cs) {
return init_cs;
}

let mut get_environ = Method::new(
crate::access_modifier::AccessModifer::Extern,
crate::method::MethodType::Static,
Expand Down
88 changes: 83 additions & 5 deletions cilly/src/v2/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ use crate::{asm::Assembly as V1Asm, v2::MethodImpl};
use fxhash::{FxHashMap, FxHashSet};
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use std::{any::type_name, path::PathBuf};
use std::{any::type_name, ops::Index, path::PathBuf};

pub type MissingMethodPatcher =
FxHashMap<StringIdx, Box<dyn Fn(MethodRefIdx, &mut Assembly) -> MethodImpl>>;
type StringMap = BiMap<StringIdx, IString>;
type TypeMap = BiMap<TypeIdx, Type>;
#[derive(Default, Serialize, Deserialize, Clone)]
pub struct Assembly {
/// A list of strings used in this assembly
strings: BiMap<StringIdx, IString>,
strings: StringMap,
/// A list of all types in this assembly
types: BiMap<TypeIdx, Type>,
types: TypeMap,
class_refs: BiMap<ClassRefIdx, ClassRef>,
class_defs: FxHashMap<ClassDefIdx, ClassDef>,
nodes: BiMap<NodeIdx, CILNode>,
Expand All @@ -34,13 +36,89 @@ pub struct Assembly {
//#[serde(skip)]
//cache: CachedAssemblyInfo<NodeIdx, NonMaxU32, StackUsage>,
}
impl Index<StringIdx> for Assembly {
type Output = str;

fn index(&self, index: StringIdx) -> &Self::Output {
&self.strings[index]
}
}
impl Index<ClassDefIdx> for Assembly {
type Output = ClassDef;

fn index(&self, index: ClassDefIdx) -> &Self::Output {
&self.class_defs[&index]
}
}
impl Index<MethodRefIdx> for Assembly {
type Output = MethodRef;

fn index(&self, index: MethodRefIdx) -> &Self::Output {
&self.method_refs[index]
}
}
impl Index<MethodDefIdx> for Assembly {
type Output = MethodDef;

fn index(&self, index: MethodDefIdx) -> &Self::Output {
&self.method_defs[&index]
}
}
impl Index<ClassRefIdx> for Assembly {
type Output = ClassRef;

fn index(&self, index: ClassRefIdx) -> &Self::Output {
&self.class_refs[index]
}
}
impl Index<TypeIdx> for Assembly {
type Output = Type;

fn index(&self, index: TypeIdx) -> &Self::Output {
&self.types[index]
}
}
impl Index<SigIdx> for Assembly {
type Output = FnSig;

fn index(&self, index: SigIdx) -> &Self::Output {
&self.sigs[index]
}
}
impl Index<RootIdx> for Assembly {
type Output = CILRoot;

fn index(&self, index: RootIdx) -> &Self::Output {
&self.roots[index]
}
}
impl Index<NodeIdx> for Assembly {
type Output = CILNode;

fn index(&self, index: NodeIdx) -> &Self::Output {
&self.nodes[index]
}
}
impl Index<StaticFieldIdx> for Assembly {
type Output = StaticFieldDesc;

fn index(&self, index: StaticFieldIdx) -> &Self::Output {
&self.statics[index]
}
}
impl Index<FieldIdx> for Assembly {
type Output = FieldDesc;

fn index(&self, index: FieldIdx) -> &Self::Output {
&self.fields[index]
}
}
impl Assembly {
pub fn typecheck(&mut self) {
let method_def_idxs: Box<[_]> = self.method_defs.keys().copied().collect();
for method in method_def_idxs {
let mut tmp_method = self.borrow_methoddef(method);
let mut tmp_method = self.method_def(method).clone();
tmp_method.typecheck(self);
self.return_methoddef(method, tmp_method);
}
}
#[must_use]
Expand Down
12 changes: 11 additions & 1 deletion cilly/src/v2/bimap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fxhash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::{collections::hash_map::Entry, fmt::Debug, hash::Hash, num::NonZeroU32};
use std::{collections::hash_map::Entry, fmt::Debug, hash::Hash, num::NonZeroU32, ops::Index};

#[derive(Serialize, Deserialize, Clone)]
pub struct BiMap<Key, Value: Eq + Hash>(pub Vec<Value>, pub FxHashMap<Value, Key>);
Expand All @@ -11,6 +11,16 @@ impl<Key: IntoBiMapIndex + Eq + Hash + Clone, Value: Eq + Hash + Clone> Default
Self(Vec::default(), FxHashMap::default())
}
}
impl<Key: IntoBiMapIndex + Eq + Hash + Clone + Debug, Value: Eq + Hash + Clone + Debug> Index<Key>
for BiMap<Key, Value>
{
type Output = Value;

fn index(&self, index: Key) -> &Self::Output {
self.get(index)
}
}

impl<Key: IntoBiMapIndex + Eq + Hash + Clone + Debug, Value: Eq + Hash + Clone + Debug>
BiMap<Key, Value>
{
Expand Down
Loading

1 comment on commit a628dc3

@1Tiphereth
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey c_header.h is missing which causes the build to fail.

error: couldn't read `cilly\src\v2\c_exporter\c_header.h`: The system cannot find the file specified. (os error 2)
   --> cilly\src\v2\c_exporter\mod.rs:764:23
    |
764 |         out.write_all(include_bytes!("c_header.h"))?;
    |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)

Please sign in to comment.