Skip to content

Commit ef0f5f8

Browse files
committed
New tests + documentation updates
1 parent 4e2e8c6 commit ef0f5f8

File tree

14 files changed

+181
-104
lines changed

14 files changed

+181
-104
lines changed

cilly/src/v2/asm.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ impl Assembly {
10071007
Type::PlatformGeneric(_, _) => 8,
10081008
Type::Bool => 1,
10091009
Type::Void => 0,
1010-
Type::SMIDVector(simdvector) => match simdvector.elem() {
1010+
Type::SIMDVector(simdvector) => match simdvector.elem() {
10111011
super::tpe::simd::SIMDElem::Int(int) => int.size().unwrap_or(8) as u64, // ASSUMES alignof<usize>() = 8.
10121012
super::tpe::simd::SIMDElem::Float(float) => float.size() as u64,
10131013
},
@@ -1077,9 +1077,14 @@ pub static ILASM_PATH: std::sync::LazyLock<String> = std::sync::LazyLock::new(||
10771077
});
10781078

10791079
#[cfg(not(target_os = "windows"))]
1080+
/// Finds the default instance of the IL assembler.
10801081
fn get_default_ilasm() -> String {
10811082
"ilasm".into()
10821083
}
1084+
#[test]
1085+
fn test_get_default_ilasm() {
1086+
assert!(get_default_ilasm().contains("ilasm"));
1087+
}
10831088
#[cfg(target_os = "windows")]
10841089
fn get_default_ilasm() -> String {
10851090
if std::process::Command::new("ilasm")

cilly/src/v2/asm_link.rs

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Assembly {
2222
| Type::Void
2323
| Type::PlatformObject
2424
| Type::PlatformGeneric(_, _)
25-
| Type::SMIDVector(_) => tpe,
25+
| Type::SIMDVector(_) => tpe,
2626
Type::ClassRef(class_ref) => {
2727
Type::ClassRef(self.translate_class_ref(source, class_ref))
2828
}
@@ -616,48 +616,39 @@ impl Assembly {
616616
}
617617
}
618618

619-
def
620-
.methods()
621-
.iter()
622-
.for_each(|mdef| {
623-
let mut method_definition = self.translate_method_def(source, source.method_def(*mdef));
624-
let method_ref = self.alloc_methodref(method_definition.ref_to());
625-
// 1st Take the orignal method, if it exists(we need this to be able to mutate methods)
626-
let original = self.method_defs().get(&MethodDefIdx(method_ref));
627-
let method_definition = match original {
628-
Some(original) => {
629-
assert_eq!(method_definition.name(), original.name());
630-
// Check if this method has a special name, and needs merging.
631-
let name = &self[method_definition.name()];
632-
if SPECIAL_METHOD_NAMES.iter().any(|val| **val == *name) {
633-
// Not special, proly does not need merging, so we can check if it matches and go on our merry way.
634-
assert_eq!(method_definition.access(), original.access());
635-
assert_eq!(method_definition.class(), original.class());
636-
assert_eq!(method_definition.sig(), original.sig());
637-
assert_eq!(method_definition.kind(), original.kind());
638-
method_definition
639-
.implementation_mut()
640-
.merge_cctor_impls(original.implementation(), self);
641-
method_definition
642-
} else {
643-
// Not special, proly does not need merging, so we can check if it matches and go on our merry way.
644-
assert_eq!(method_definition.access(), original.access());
645-
assert_eq!(method_definition.class(), original.class());
646-
assert_eq!(method_definition.sig(), original.sig());
647-
assert_eq!(method_definition.kind(), original.kind());
648-
// EXPENSIVE, consider making this check debug only.
649-
let (left_val,right_val) = (&(method_definition.implementation()), &(original.implementation()));
650-
if!(*left_val== *right_val){
651-
eprintln!("WARNING: linking methods with diveriging implmenentations. This is usualy a sign of a bug. {left_val:?} {right_val:?}");
652-
};
653-
method_definition
654-
}
619+
def.methods().iter().for_each(|mdef| {
620+
let mut method_definition = self.translate_method_def(source, source.method_def(*mdef));
621+
let method_ref = self.alloc_methodref(method_definition.ref_to());
622+
// 1st Take the orignal method, if it exists(we need this to be able to mutate methods)
623+
let original = self.method_defs().get(&MethodDefIdx(method_ref));
624+
let method_definition = match original {
625+
Some(original) => {
626+
assert_eq!(method_definition.name(), original.name());
627+
// Check if this method has a special name, and needs merging.
628+
let name = &self[method_definition.name()];
629+
if SPECIAL_METHOD_NAMES.iter().any(|val| **val == *name) {
630+
// Needs special handling.
631+
assert_eq!(method_definition.access(), original.access());
632+
assert_eq!(method_definition.class(), original.class());
633+
assert_eq!(method_definition.sig(), original.sig());
634+
assert_eq!(method_definition.kind(), original.kind());
635+
method_definition
636+
.implementation_mut()
637+
.merge_cctor_impls(original.implementation(), self);
638+
method_definition
639+
} else {
640+
// Not special, proly does not need merging, so we can check if it matches and go on our merry way.
641+
assert_eq!(method_definition.access(), original.access());
642+
assert_eq!(method_definition.class(), original.class());
643+
assert_eq!(method_definition.sig(), original.sig());
644+
assert_eq!(method_definition.kind(), original.kind());
645+
method_definition
655646
}
656-
None => method_definition,
657-
};
658-
self.new_method(method_definition);
659-
})
660-
;
647+
}
648+
None => method_definition,
649+
};
650+
self.new_method(method_definition);
651+
});
661652
translated
662653
}
663654
}

cilly/src/v2/builtins/int128/mod.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,3 @@ pub fn i128_mul_ovf_check(asm: &mut Assembly, patcher: &mut MissingMethodPatcher
161161
};
162162
patcher.insert(name, Box::new(generator));
163163
}
164-
#[test]
165-
fn test() {
166-
let op = BinOp::Eq;
167-
let lhs_type = Int::I128;
168-
panic!(
169-
"{op}_{lhs_type}",
170-
op = op.name(),
171-
lhs_type = lhs_type.name()
172-
);
173-
}

cilly/src/v2/c_exporter/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn c_tpe(field_tpe: Type, asm: &Assembly) -> String {
100100
dims = "*".repeat(dims.get() as usize)
101101
),
102102
Type::FnPtr(_) => "void*".into(),
103-
Type::SMIDVector(vec) => {
103+
Type::SIMDVector(vec) => {
104104
format!(
105105
"__simdvec{elem}{count}",
106106
elem = std::convert::Into::<Type>::into(vec.elem()).mangle(asm),
@@ -114,11 +114,11 @@ fn mref_to_name(mref: &MethodRef, asm: &Assembly) -> String {
114114
let class_name = escape_ident(&asm[class.name()]);
115115
let mname = escape_ident(&asm[mref.name()]);
116116
if class.asm().is_some()
117-
|| matches!(mref.output(asm), Type::SMIDVector(_))
117+
|| matches!(mref.output(asm), Type::SIMDVector(_))
118118
|| mref
119119
.stack_inputs(asm)
120120
.iter()
121-
.any(|tpe| matches!(tpe, Type::SMIDVector(_)))
121+
.any(|tpe| matches!(tpe, Type::SIMDVector(_)))
122122
{
123123
let mangled = escape_ident(
124124
&asm[mref.sig()]

cilly/src/v2/cillyir_exporter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn tpe_to(tpe: &Type, asm: &Assembly) -> String {
9494
| Type::Void => format!("{tpe:?}"),
9595
Type::PlatformArray { .. } => todo!(),
9696
Type::FnPtr(sig) => format!("Type::FnPtr({sig})", sig = sig_to(asm[*sig].clone(), asm)),
97-
Type::SMIDVector(_) => panic!("SMID is not supported when dumping cilly IR"),
97+
Type::SIMDVector(_) => panic!("SMID is not supported when dumping cilly IR"),
9898
}
9999
}
100100
fn sig_to(sig: FnSig, asm: &Assembly) -> String {

cilly/src/v2/cilroot.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ impl CILRoot {
228228
let col_end = u16::try_from(sfi.1.end.min(u64::from(u16::MAX))).unwrap();
229229
let col_len = col_end - col_start;
230230
let file = asm.alloc_string(sfi.2.clone());
231+
231232
Self::SourceFileInfo {
232233
line_start,
233234
line_len,
@@ -663,8 +664,16 @@ impl CILRoot {
663664
}
664665
}
665666
}
666-
667-
pub(crate) fn display(
667+
/// Returns a debug string, representing this root. This debug repr contains additional info not included by std::fmt::Debug.
668+
/// ```
669+
/// # use cilly::v2::cilroot::CILRoot;
670+
/// # let mut asm = cilly::Assembly::default();
671+
/// # let sig = asm.sig([],cilly::Type::Void);
672+
/// # let locals = [];
673+
/// let root = CILRoot::Nop;
674+
/// assert_eq!(root.display(&mut asm,sig,&locals),"Nop");
675+
/// ```
676+
pub fn display(
668677
&self,
669678
asm: &mut Assembly,
670679
sig: SigIdx,
@@ -720,11 +729,19 @@ fn many_mut<T>(input: &mut [T]) -> Vec<&mut T> {
720729
assert_eq!(res.len(), input_len);
721730
res
722731
}
723-
/// Changes a mutable reference to a slice to an vec of mutable references to the elements.
732+
/// Changes a reference to a slice to an vec of references to the elements.
724733
fn many_ref<T>(inputs: &[T]) -> Vec<&T> {
725734
inputs.iter().collect()
726735
}
727736
#[test]
737+
fn test_many_ref() {
738+
let inputs = [0, 1, 2, 3, 4];
739+
let res = many_ref(&inputs);
740+
assert_eq!(res.len(), inputs.len());
741+
assert_eq!(res[0], &0);
742+
assert_eq!(res[4], &4);
743+
}
744+
#[test]
728745
fn test_many_mut() {
729746
// 0 elements
730747
many_mut::<i32>(&mut []);
@@ -748,4 +765,8 @@ fn test_many_mut() {
748765
*many_mut(&mut [1, 2, 3, 4, 5])[2] = 3;
749766
*many_mut(&mut [1, 2, 3, 4, 5])[3] = 4;
750767
*many_mut(&mut [1, 2, 3, 4, 5])[4] = 5;
768+
for i in 0..100 {
769+
let mut vec = vec![0; i];
770+
assert_eq!(many_mut(&mut vec).len(), i);
771+
}
751772
}

cilly/src/v2/il_exporter/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,10 @@ impl ILExporter {
646646
(Type::PlatformArray { .. }, false) => writeln!(out, "ldind.ref"),
647647
(Type::FnPtr(_), true) => writeln!(out, "volatile. ldind.i"),
648648
(Type::FnPtr(_), false) => writeln!(out, "ldind.i"),
649-
(Type::SMIDVector(_), true) => {
649+
(Type::SIMDVector(_), true) => {
650650
writeln!(out, "volatile. ldobj {}", type_il(&tpe, asm))
651651
}
652-
(Type::SMIDVector(_), false) => {
652+
(Type::SIMDVector(_), false) => {
653653
writeln!(out, "ldobj {}", type_il(&tpe, asm))
654654
}
655655
}
@@ -1132,7 +1132,7 @@ impl ILExporter {
11321132
Type::Void => writeln!(out, "pop pop ldstr \"Attempted to wrtie to a zero-sized type(void).\" newobj void [System.Runtime]System.Exception::.ctor(string) throw"), // TODO: forbid this, since this is NEVER valid.
11331133
Type::PlatformArray { .. } => writeln!(out, "{is_volitale} stind.ref"),
11341134
Type::FnPtr(_) => writeln!(out, "{is_volitale} stind.i"),
1135-
Type::SMIDVector(_)=>writeln!(out, "stobj {}", type_il(&tpe, asm)),
1135+
Type::SIMDVector(_)=>writeln!(out, "stobj {}", type_il(&tpe, asm)),
11361136
}
11371137
}
11381138
super::CILRoot::InitBlk(blk) => {
@@ -1350,7 +1350,7 @@ fn non_void_type_il(tpe: &Type, asm: &Assembly) -> String {
13501350
}
13511351
fn type_il(tpe: &Type, asm: &Assembly) -> String {
13521352
match tpe {
1353-
Type::SMIDVector(simdvec) => {
1353+
Type::SIMDVector(simdvec) => {
13541354
let vec_bits = simdvec.bits();
13551355
assert!(
13561356
vec_bits == 64 || vec_bits == 128 || vec_bits == 256 || vec_bits == 512,

cilly/src/v2/java_exporter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ fn type_string(tpe: Type, asm: &Assembly) -> String {
181181
elem = type_string(asm[elem], asm)
182182
),
183183
Type::FnPtr(_) => "J".into(),
184-
Type::SMIDVector(_) => panic!("SMID is not supported in Java"),
184+
Type::SIMDVector(_) => panic!("SMID is not supported in Java"),
185185
}
186186
}
187187
/*

cilly/src/v2/mod.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,6 @@ pub trait Exporter {
9090
fn export(&self, asm: &Assembly, target: &Path) -> Result<(), Self::Error>;
9191
}
9292

93-
#[test]
94-
fn no_collision() {
95-
fn test_binop(asm: &mut Assembly, op: BinOp) -> CILNode {
96-
let mut curr: NodeIdx = IntoAsmIndex::into_idx(Const::I8(1), asm);
97-
for _ in 0..100 {
98-
curr = IntoAsmIndex::into_idx(asm.biop(curr, curr, op), asm);
99-
}
100-
asm[curr]
101-
.clone()
102-
.typecheck(asm.sig(vec![], Type::Void), &[], asm)
103-
.unwrap();
104-
asm[curr].clone()
105-
}
106-
let mut asm = Assembly::default();
107-
test_binop(&mut asm, BinOp::Add);
108-
}
10993
#[test]
11094
fn test_binops() {
11195
fn test_binop(asm: &mut Assembly, op: BinOp) -> CILNode {

cilly/src/v2/tpe/float.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ impl Float {
2424
Float::F128 => todo!(),
2525
}
2626
}
27-
/// Returns the size of this floating-point number
27+
/// Returns the size of this floating-point number, in bytes
28+
/// ```
29+
/// # use cilly::Float;
30+
/// assert_eq!(Float::F16.size(),2);
31+
/// assert_eq!(Float::F128.size(),16);
32+
/// ```
2833
#[must_use]
2934
pub fn size(&self) -> u8 {
3035
match self {
@@ -153,13 +158,14 @@ impl Float {
153158
));
154159
asm.alloc_node(CILNode::Call(Box::new((mref, Box::new([val])))))
155160
}
161+
/// Counts the number of bits this number has.
162+
/// ```
163+
/// # use cilly::Float;
164+
/// assert_eq!(Float::F16.bits(),16);
165+
/// assert_eq!(Float::F128.bits(),128);
166+
/// ```
156167
pub fn bits(&self) -> u8 {
157-
match self {
158-
Float::F16 => 16,
159-
Float::F32 => 32,
160-
Float::F64 => 64,
161-
Float::F128 => 128,
162-
}
168+
self.size() * 8
163169
}
164170
}
165171
impl From<Float> for Type {

cilly/src/v2/tpe/int.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ impl Int {
7474
Int::ISize | Int::USize => Int::ISize,
7575
}
7676
}
77-
7877
/// Returns the minimum value of this int.
7978
pub fn min(&self, asm: &mut Assembly) -> CILNode {
8079
match self {
@@ -258,9 +257,16 @@ impl Int {
258257
Int::USize | Int::ISize => None,
259258
}
260259
}
261-
262-
pub fn bits(&self) -> u8 {
263-
self.size().unwrap() * 8
260+
/// Counts the number of bits this intiger has. Returns None when this information is not known till runtime(for `usize` and `isize`).
261+
/// ```
262+
/// # use cilly::Int;
263+
/// assert_eq!(Int::U8.bits(),Some(8));
264+
/// assert_eq!(Int::I16.bits(),Some(16));
265+
/// assert_eq!(Int::U128.bits(),Some(128));
266+
/// assert_eq!(Int::USize.bits(),None);
267+
/// ```
268+
pub fn bits(&self) -> Option<u8> {
269+
self.size().map(|size| size * 8)
264270
}
265271
}
266272
#[test]

0 commit comments

Comments
 (0)