From 89a89b7195dfd2856fdb26ced16204e4b90f45bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Mon, 30 Sep 2024 00:22:41 +0200 Subject: [PATCH 01/10] Started moving away from CallSite to MethodRefIdx(new, more optimized representation of methods) --- cilly/src/asm.rs | 83 ++-- cilly/src/bin/interpreter/main.rs | 22 +- cilly/src/bin/linker/main.rs | 6 +- cilly/src/call_site.rs | 184 ------- cilly/src/cil_iter.rs | 75 +-- cilly/src/cil_iter_mut.rs | 63 --- cilly/src/cil_node.rs | 221 +++++---- cilly/src/cil_root.rs | 157 +++--- cilly/src/entrypoint.rs | 118 ++--- cilly/src/method.rs | 48 +- cilly/src/utilis.rs | 250 +++++----- cilly/src/v2/cilnode.rs | 53 +- cilly/src/v2/cilroot.rs | 26 +- cilly/src/v2/method.rs | 54 ++- src/aggregate.rs | 14 +- src/assembly.rs | 36 +- src/binop/bitop.rs | 131 ++--- src/binop/checked/mod.rs | 358 +++++++------- src/binop/cmp.rs | 23 +- src/binop/mod.rs | 38 +- src/binop/shift.rs | 18 +- src/builtin/atomic.rs | 18 +- src/builtin/casts.rs | 66 +-- src/builtin/mod.rs | 70 +-- src/casts.rs | 620 +++++++++++++----------- src/comptime.rs | 2 +- src/constant.rs | 339 +++++++------ src/lib.rs | 16 +- src/place/adress.rs | 6 +- src/place/body.rs | 10 +- src/place/get.rs | 6 +- src/place/set.rs | 8 +- src/rvalue.rs | 62 ++- src/terminator/call.rs | 117 +++-- src/terminator/intrinsics/bswap.rs | 4 +- src/terminator/intrinsics/ints.rs | 106 ++-- src/terminator/intrinsics/mod.rs | 112 ++--- src/terminator/intrinsics/saturating.rs | 30 +- src/terminator/intrinsics/utilis.rs | 84 ++-- src/terminator/mod.rs | 12 +- src/type/tycache.rs | 4 +- src/type/type.rs | 12 +- src/unop.rs | 75 +-- src/utilis/adt.rs | 94 ++-- 44 files changed, 1861 insertions(+), 1990 deletions(-) diff --git a/cilly/src/asm.rs b/cilly/src/asm.rs index 6394b165..69cc706b 100644 --- a/cilly/src/asm.rs +++ b/cilly/src/asm.rs @@ -7,10 +7,9 @@ use serde::{Deserialize, Serialize}; use crate::{ access_modifier::AccessModifer, basic_block::BasicBlock, - call_site::CallSite, cil_root::CILRoot, method::{Method, MethodType}, - v2::{ClassDef, FnSig, Int}, + v2::{cilnode::MethodKind, ClassDef, FnSig, Int, MethodRef, MethodRefIdx}, IString, Type, }; @@ -36,9 +35,9 @@ pub type ExternFnDef = (IString, FnSig, bool); /// Representation of a .NET assembly. pub struct Assembly { /// List of functions defined within this assembly. - functions: FxHashMap, - /// Callsite representing the entrypoint of this assebmly if any present. - entrypoint: Option, + functions: FxHashMap, + /// MethodRefIdx representing the entrypoint of this assebmly if any present. + entrypoint: Option, /// List of references to external assemblies extern_refs: FxHashMap, extern_fns: FxHashMap, @@ -100,33 +99,34 @@ impl Assembly { /// Addds a per-thread static initailzer pub fn add_tcctor(&mut self) -> &mut Method { - self.functions - .entry(CallSite::new( - None, - ".tcctor".into(), + let mref = MethodRef::new( + *self.main_module(), + self.alloc_string(".tcctor"), + self.alloc_sig(FnSig::new(Box::new([]), Type::Void)), + MethodKind::Static, + vec![].into(), + ); + let mref = self.alloc_methodref(mref); + self.functions.entry(mref).or_insert_with(|| { + Method::new( + AccessModifer::Extern, + MethodType::Static, FnSig::new(Box::new([]), Type::Void), - true, - )) - .or_insert_with(|| { - Method::new( - AccessModifer::Extern, - MethodType::Static, - FnSig::new(Box::new([]), Type::Void), - ".tcctor", - vec![ - (None, self.inner.nptr(Type::Int(Int::U8))), - (None, self.inner.nptr(Type::Int(Int::U8))), - ], - vec![BasicBlock::new(vec![CILRoot::VoidRet.into()], 0, None)], - vec![], - ) - }) + ".tcctor", + vec![ + (None, self.inner.nptr(Type::Int(Int::U8))), + (None, self.inner.nptr(Type::Int(Int::U8))), + ], + vec![BasicBlock::new(vec![CILRoot::VoidRet.into()], 0, None)], + vec![], + ) + }) } /// Returns true if assembly contains function named `name` #[must_use] - pub fn contains_fn(&self, site: &CallSite) -> bool { - self.functions.contains_key(site) + pub fn contains_fn(&self, site: MethodRefIdx) -> bool { + self.functions.contains_key(&site) } /// Adds a method to the assebmly. pub fn add_method(&mut self, method: Method) { @@ -144,11 +144,11 @@ impl Assembly { } } - /// Sets the entrypoint of the assembly to the method behind `CallSite`. - pub fn set_entrypoint(&mut self, entrypoint: &CallSite) { + /// Sets the entrypoint of the assembly to the method behind `MethodRefIdx`. + pub fn set_entrypoint(&mut self, entrypoint: MethodRefIdx) { assert!(self.entrypoint.is_none(), "ERROR: Multiple entrypoints"); - let wrapper = crate::entrypoint::wrapper(entrypoint, self.inner_mut()); - self.entrypoint = Some(wrapper.call_site()); + let wrapper = crate::entrypoint::wrapper(self[entrypoint].clone(), self.inner_mut()); + self.entrypoint = Some(wrapper.call_site(self)); self.add_method(wrapper); } @@ -161,15 +161,18 @@ impl Assembly { self.initializers.push(root); } pub fn cctor_mut(&mut self) -> Option<&mut Method> { - self.functions.get_mut(&CallSite::new( - None, - ".cctor".into(), - FnSig::new(Box::new([]), Type::Void), - true, - )) - } - - pub(crate) fn functions(&self) -> &FxHashMap { + let mref = MethodRef::new( + *self.main_module(), + self.alloc_string(".cctor"), + self.sig([], Type::Void), + MethodKind::Static, + vec![].into(), + ); + let mref = self.alloc_methodref(mref); + self.functions.get_mut(&mref) + } + + pub(crate) fn functions(&self) -> &FxHashMap { &self.functions } diff --git a/cilly/src/bin/interpreter/main.rs b/cilly/src/bin/interpreter/main.rs index 21bb8c79..a5217840 100644 --- a/cilly/src/bin/interpreter/main.rs +++ b/cilly/src/bin/interpreter/main.rs @@ -5,7 +5,7 @@ fn main() {} use std::io::Write; use cilly::{ asm::Assembly, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::{CILRoot, SFI}, method::Method, @@ -17,7 +17,7 @@ use fxhash::{FxBuildHasher, FxHashMap}; use value::Value; #[derive(Debug)] enum Exception { - MethodNotFound(CallSite), + MethodNotFound(MethodRefIdx), LocalOutOfRange { loc: usize, lcount: usize }, ArgOutOfRange { arg: usize, lcount: usize }, AllocOffsetOutOfRange, @@ -25,13 +25,13 @@ enum Exception { type AllocID = u32; struct InterpreterState<'asm> { asm: &'asm Assembly, - call_stack: Vec<(&'asm CallSite, usize, usize, cilly::cil_root::SFI)>, + call_stack: Vec<(&'asm MethodRefIdx, usize, usize, cilly::cil_root::SFI)>, locals: Vec>, mem: FxHashMap>, last_alloc: AllocID, fields: FxHashMap, - methods: FxHashMap, - inv_methods: FxHashMap, + methods: FxHashMap, + inv_methods: FxHashMap, last_alloc_method: AllocID, } @@ -341,7 +341,7 @@ fn eval_node<'asm>( } } impl<'asm> InterpreterState<'asm> { - pub fn get_fn_ptr_alloc(&mut self, site: &CallSite) -> AllocID { + pub fn get_fn_ptr_alloc(&mut self, site: &MethodRefIdx) -> AllocID { *self.inv_methods.entry(site.clone()).or_insert_with(|| { let new_method = self.last_alloc_method; self.methods.insert(new_method, site.clone()); @@ -357,7 +357,7 @@ impl<'asm> InterpreterState<'asm> { } pub fn try_call_extern( &mut self, - call: &'asm CallSite, + call: &'asm MethodRefIdx, args: &mut Box<[Value]>, string_map: &AsmStringContainer, ) -> Result { @@ -412,7 +412,7 @@ impl<'asm> InterpreterState<'asm> { pub fn run_cctor(&mut self) -> Result { match self.asm.cctor() { Some(_) => self.run( - Box::::leak(Box::new(CallSite::builtin( + Box::::leak(Box::new(MethodRefIdx::builtin( ".cctor".into(), FnSig::new(&[], Type::Void), true, @@ -426,7 +426,7 @@ impl<'asm> InterpreterState<'asm> { let entry = self.asm.methods().find(|method| method.is_entrypoint()); match entry { Some(entry) => self.run( - Box::::leak(Box::new(entry.call_site())), + Box::::leak(Box::new(entry.call_site())), &mut vec![Value::StringArray( std::env::args().map(|arg| arg.into()).collect(), )] @@ -435,7 +435,7 @@ impl<'asm> InterpreterState<'asm> { None => Ok(Value::Undef), } } - pub fn method(&self, site: &'asm CallSite) -> Result<&'asm Method, Exception> { + pub fn method(&self, site: &'asm MethodRefIdx) -> Result<&'asm Method, Exception> { self.asm .functions() .get(site) @@ -443,7 +443,7 @@ impl<'asm> InterpreterState<'asm> { } pub fn run( &mut self, - call: &'asm CallSite, + call: &'asm MethodRefIdx, args: &mut Box<[Value]>, ) -> Result { assert_eq!(self.locals.len(), self.call_stack.len()); diff --git a/cilly/src/bin/linker/main.rs b/cilly/src/bin/linker/main.rs index b0907bcc..e73baa11 100644 --- a/cilly/src/bin/linker/main.rs +++ b/cilly/src/bin/linker/main.rs @@ -2,7 +2,7 @@ #![allow(clippy::module_name_repetitions)] use cilly::{ asm::DEAD_CODE_ELIMINATION, - call_site::CallSite, + call_site::MethodRefIdx, conv_usize, libc_fns::{self, LIBC_FNS, LIBC_MODIFIES_ERRNO}, v2::{ @@ -267,7 +267,7 @@ fn main() { args: Box::new([conv_usize!( cilly::cil_node::CILNode::LDArg(0) )]), - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(asm.alloc_class_ref(ClassRef::new( rust_exception, None, @@ -608,7 +608,7 @@ fn override_errno(asm: &mut Assembly) { vec![BasicBlock::new( vec![CILRoot::Ret { tree: cilly::call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::marshal()), "GetLastWin32Error".into(), FnSig::new(&[], Type::Int(Int::I32)), diff --git a/cilly/src/call_site.rs b/cilly/src/call_site.rs index a29da8f3..8b137891 100644 --- a/cilly/src/call_site.rs +++ b/cilly/src/call_site.rs @@ -1,185 +1 @@ -use crate::{ - v2::{Assembly, ClassRef, ClassRefIdx, FnSig, Int}, - Type, -}; -use serde::{Deserialize, Serialize}; -use crate::IString; - -/// Represenation of a target of a call. -#[derive(Clone, PartialEq, Serialize, Deserialize, Eq, Hash, Debug)] -pub struct CallSite { - class: Option, - name: IString, - signature: FnSig, - is_static: bool, - generics: Vec, -} - -impl CallSite { - #[must_use] - pub fn mstring_to_ptr(asm: &mut Assembly) -> Self { - Self::new_extern( - ClassRef::marshal(asm), - "StringToCoTaskMemUTF8".into(), - FnSig::new(Box::new([Type::PlatformString]), Type::Int(Int::ISize)), - true, - ) - } - #[must_use] - pub fn aligned_alloc(asm: &mut Assembly) -> Self { - Self::new_extern( - ClassRef::native_mem(asm), - "AlignedAlloc".into(), - FnSig::new( - Box::new([Type::Int(Int::USize), Type::Int(Int::USize)]), - asm.nptr(Type::Void), - ), - true, - ) - } - #[must_use] - pub fn alloc(asm: &mut Assembly) -> Self { - Self::new_extern( - ClassRef::marshal(asm), - "AllocHGlobal".into(), - FnSig::new(Box::new([Type::Int(Int::I32)]), Type::Int(Int::ISize)), - true, - ) - } - #[must_use] - pub fn realloc(asm: &mut Assembly) -> Self { - Self::new( - Some(ClassRef::native_mem(asm)), - "AlignedRealloc".into(), - FnSig::new( - Box::new([ - asm.nptr(Type::Void), - Type::Int(Int::USize), - Type::Int(Int::USize), - ]), - asm.nptr(Type::Void), - ), - true, - ) - } - /// Constructs a new call site targeting method `name`, with signature `signature` and bleonging to class `class`. If `class` is [`None`], then the `` class - /// is assumed. - #[must_use] - pub fn new( - class: Option, - name: IString, - signature: FnSig, - is_static: bool, - ) -> Self { - if *name == *".ctor" { - assert!(!is_static); - } - Self { - class, - name, - signature, - is_static, - generics: vec![], - } - } - #[must_use] - pub fn new_extern( - class: ClassRefIdx, - name: IString, - signature: FnSig, - is_static: bool, - ) -> Self { - Self { - class: Some(class), - name, - signature, - is_static, - generics: vec![], - } - } - #[must_use] - pub fn builtin(name: IString, signature: FnSig, is_static: bool) -> Self { - Self { - class: None, - name, - signature, - is_static, - generics: vec![], - } - } - #[must_use] - pub fn generics(&self) -> &[Type] { - &self.generics - } - pub fn set_generics(&mut self, generics: Vec) { - self.generics = generics; - } - /// The same as [`Self::new`], but boxes the result. - #[must_use] - pub fn boxed( - class: Option, - name: IString, - signature: FnSig, - is_static: bool, - ) -> Box { - Box::new(Self::new(class, name, signature, is_static)) - } - /// Returns the signature of the function this call site targets. - #[must_use] - pub const fn signature(&self) -> &FnSig { - &self.signature - } - - /// Returns the class the targeted method belongs to. - #[must_use] - pub const fn class(&self) -> Option { - self.class - } - /// Returns `true` if the method in question is static. - #[must_use] - pub const fn is_static(&self) -> bool { - self.is_static - } - /// Returns the name of the targteted method. - #[must_use] - pub fn name(&self) -> &str { - &self.name - } - /// Returns true if a call is equivalent to a No-Op. Used to handle `black_box`. - #[must_use] - pub fn is_nop(&self) -> bool { - if !self.is_static() { - return false; - } - if self.class().is_some() { - return false; - }; - if AsRef::::as_ref(&self.name) != "black_box" - && AsRef::::as_ref(&self.name) != "assert_inhabited" - { - return false; - }; - if self.signature.inputs().len() != 1 { - return false; - }; - if self.signature.inputs()[0] != *self.signature.output() { - return false; - }; - true - } - /// All inputs. Includes impilcit `this` argument for instance functions. - #[must_use] - pub fn inputs(&self) -> &[Type] { - self.signature.inputs() - } - /// Inputs, with the implicit `this` skipped if needed. - #[must_use] - pub fn explicit_inputs(&self) -> &[Type] { - if self.is_static || self.inputs().is_empty() { - self.signature.inputs() - } else { - &self.signature.inputs()[1..] - } - } -} diff --git a/cilly/src/cil_iter.rs b/cilly/src/cil_iter.rs index 4658b925..377773bc 100644 --- a/cilly/src/cil_iter.rs +++ b/cilly/src/cil_iter.rs @@ -1,4 +1,4 @@ -use crate::{call_site::CallSite, cil_node::CILNode, cil_root::CILRoot}; +use crate::{cil_node::CILNode, cil_root::CILRoot, v2::MethodRefIdx}; #[derive(Debug, Clone, Copy)] pub enum CILIterElem<'a> { @@ -488,22 +488,22 @@ impl<'a> CILIter<'a> { } } pub trait CILIterTrait<'a> { - fn call_sites(self) -> impl Iterator; + fn call_sites(self) -> impl Iterator; fn nodes(self) -> impl Iterator; fn roots(self) -> impl Iterator; } impl<'a, T: Iterator>> CILIterTrait<'a> for T { - fn call_sites(self) -> impl Iterator { + fn call_sites(self) -> impl Iterator { self.filter_map(|node| match node { CILIterElem::Node( CILNode::Call(call_op_args) | CILNode::CallVirt(call_op_args) | CILNode::NewObj(call_op_args), - ) => Some(call_op_args.site.as_ref()), - CILIterElem::Node(CILNode::LDFtn(site)) => Some(site.as_ref()), + ) => Some(call_op_args.site), + CILIterElem::Node(CILNode::LDFtn(site)) => Some(*site), CILIterElem::Root( CILRoot::Call { site, args: _ } | CILRoot::CallVirt { site, args: _ }, - ) => Some(site), + ) => Some(*site), _ => None, }) } @@ -538,66 +538,3 @@ impl<'a> IntoIterator for &'a CILRoot { CILIter::new_root(self) } } -#[test] -fn iter() { - use crate::{ - call_site::CallSite, - v2::{hashable::HashableF32, Float, FnSig, Int}, - Type, - }; - let node = CILNode::Add( - Box::new(CILNode::Mul( - Box::new(CILNode::LDLoc(0)), - Box::new(CILNode::SizeOf(Box::new(Type::Int(Int::U8)))), - )), - Box::new(CILNode::LDLoc(1)), - ); - let mut iter = node.into_iter(); - assert!(matches!( - iter.next(), - Some(CILIterElem::Node(CILNode::Add(_, _))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElem::Node(CILNode::Mul(_, _))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElem::Node(CILNode::LDLoc(_))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElem::Node(CILNode::SizeOf(_))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElem::Node(CILNode::LDLoc(1))) - )); - assert!(iter.next().is_none()); - let root = CILRoot::Call { - site: Box::new(CallSite::new( - None, - "bob".to_owned().into(), - FnSig::new( - Box::new([Type::Int(Int::I32), Type::Float(Float::F32)]), - Type::Void, - ), - true, - )), - args: [CILNode::LdcI32(-77), CILNode::LdcF32(HashableF32(3.119765))].into(), - }; - let mut iter = root.into_iter(); - assert!(matches!( - iter.next(), - Some(CILIterElem::Root(CILRoot::Call { .. })) - )); - assert!(matches!( - iter.next(), - Some(CILIterElem::Node(CILNode::LdcI32(-77))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElem::Node(CILNode::LdcF32(HashableF32(3.119765)))) - )); - assert!(iter.next().is_none()); -} diff --git a/cilly/src/cil_iter_mut.rs b/cilly/src/cil_iter_mut.rs index 93dcb980..cd20983b 100644 --- a/cilly/src/cil_iter_mut.rs +++ b/cilly/src/cil_iter_mut.rs @@ -575,66 +575,3 @@ impl<'a, T: Iterator>> CILIterMutTrait<'a> for T { }) } } -#[test] -fn iter() { - use crate::{ - call_site::CallSite, - v2::{hashable::HashableF32, Float, FnSig, Int}, - Type, - }; - let mut node = CILNode::Add( - Box::new(CILNode::Mul( - Box::new(CILNode::LDLoc(0)), - Box::new(CILNode::SizeOf(Box::new(Type::Int(Int::U8)))), - )), - Box::new(CILNode::LDLoc(1)), - ); - let mut iter = (&mut node).into_iter(); - assert!(matches!( - iter.next(), - Some(CILIterElemMut::Node(CILNode::Add(_, _))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElemMut::Node(CILNode::Mul(_, _))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElemMut::Node(CILNode::LDLoc(_))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElemMut::Node(CILNode::SizeOf(_))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElemMut::Node(CILNode::LDLoc(1))) - )); - assert!(iter.next().is_none()); - let mut root = CILRoot::Call { - site: Box::new(CallSite::new( - None, - "bob".into(), - FnSig::new( - Box::new([Type::Int(Int::I32), Type::Float(Float::F32)]), - Type::Void, - ), - true, - )), - args: [CILNode::LdcI32(-77), CILNode::LdcF32(HashableF32(3.119765))].into(), - }; - let mut iter = (&mut root).into_iter(); - assert!(matches!( - iter.next(), - Some(CILIterElemMut::Root(CILRoot::Call { .. })) - )); - assert!(matches!( - iter.next(), - Some(CILIterElemMut::Node(CILNode::LdcI32(-77))) - )); - assert!(matches!( - iter.next(), - Some(CILIterElemMut::Node(CILNode::LdcF32(HashableF32(3.119765)))) - )); - assert!(iter.next().is_none()); -} diff --git a/cilly/src/cil_node.rs b/cilly/src/cil_node.rs index 5b71c6f0..63e545b5 100644 --- a/cilly/src/cil_node.rs +++ b/cilly/src/cil_node.rs @@ -1,7 +1,10 @@ -use crate::v2::{Assembly, ClassRef, ClassRefIdx, FieldIdx, FnSig, Int, StaticFieldDesc, Type}; +use crate::v2::cilnode::MethodKind; +use crate::v2::{ + Assembly, ClassRef, ClassRefIdx, FieldIdx, FnSig, Int, MethodRef, MethodRefIdx, + StaticFieldDesc, Type, +}; use crate::{ call, - call_site::CallSite, cil_iter::CILIterTrait, cil_root::CILRoot, v2::hashable::{HashableF32, HashableF64}, @@ -12,7 +15,7 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Hash, Debug)] pub struct CallOpArgs { pub args: Box<[CILNode]>, - pub site: Box, + pub site: MethodRefIdx, } #[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug, Hash)] @@ -170,7 +173,7 @@ pub enum CILNode { SubTrees(Box<(Box<[CILRoot]>, Box)>), LoadAddresOfTMPLocal, LoadTMPLocal, - LDFtn(Box), + LDFtn(MethodRefIdx), LDTypeToken(Box), NewObj(Box), // 24 bytes - too big! @@ -238,21 +241,21 @@ impl CILNode { } let low = u128_low_u64(value); let high = (value >> 64) as u64; - let ctor_sig = FnSig::new( - Box::new([ - asm.nref(Type::Int(Int::U128)), - Type::Int(Int::U64), - Type::Int(Int::U64), - ]), + let ref_u128 = asm.nref(Type::Int(Int::U128)); + let ctor_sig = asm.sig( + [ref_u128, Type::Int(Int::U64), Type::Int(Int::U64)], Type::Void, ); + let uint_128 = ClassRef::uint_128(asm); + let ctor = asm.alloc_string(".ctor"); CILNode::NewObj(Box::new(CallOpArgs { - site: CallSite::boxed( - Some(ClassRef::uint_128(asm)), - ".ctor".into(), + site: asm.alloc_methodref(MethodRef::new( + uint_128, + ctor, ctor_sig, - false, - ), + MethodKind::Constructor, + vec![].into(), + )), args: [ crate::conv_u64!(crate::ldc_u64!(high)), crate::conv_u64!(crate::ldc_u64!(low)), @@ -266,21 +269,21 @@ impl CILNode { } let low = u128_low_u64(value); let high = (value >> 64) as u64; - let ctor_sig = FnSig::new( - Box::new([ - asm.nref(Type::Int(Int::U128)), - Type::Int(Int::U64), - Type::Int(Int::U64), - ]), + let ref_u128 = asm.nref(Type::Int(Int::U128)); + let ctor_sig = asm.sig( + [ref_u128, Type::Int(Int::U64), Type::Int(Int::U64)], Type::Void, ); + let ctor = asm.alloc_string(".ctor"); + let int_128 = ClassRef::int_128(asm); CILNode::NewObj(Box::new(CallOpArgs { - site: CallSite::boxed( - Some(ClassRef::int_128(asm)), - ".ctor".into(), + site: asm.alloc_methodref(MethodRef::new( + int_128, + ctor, ctor_sig, - false, - ), + MethodKind::Constructor, + vec![].into(), + )), args: [ crate::conv_u64!(crate::ldc_u64!(high)), crate::conv_u64!(crate::ldc_u64!(low)), @@ -290,105 +293,103 @@ impl CILNode { } /// Allocates a GC handle to the object, and converts that handle to a nint sized handleID. pub fn managed_ref_to_handle(self, asm: &mut Assembly) -> Self { - let gc_handle = call!( - CallSite::new( - Some(ClassRef::gc_handle(asm)), - "Alloc".to_owned().into(), - FnSig::new( - Box::new([Type::PlatformObject]), - Type::ClassRef(ClassRef::gc_handle(asm)) - ), - true - ), - [self] + let gc_handle_class = Type::ClassRef(ClassRef::gc_handle(asm)); + let mref = MethodRef::new( + ClassRef::gc_handle(asm), + asm.alloc_string("Alloc"), + asm.sig([Type::PlatformObject], gc_handle_class), + MethodKind::Static, + vec![].into(), ); - call!( - CallSite::new( - Some(ClassRef::gc_handle(asm)), - "op_Explicit".to_owned().into(), - FnSig::new( - Box::new([Type::ClassRef(ClassRef::gc_handle(asm))]), - Type::Int(Int::ISize) - ), - true, - ), - [gc_handle] - ) + let gc_handle = call!(asm.alloc_methodref(mref), [self]); + let mref = MethodRef::new( + ClassRef::gc_handle(asm), + asm.alloc_string("op_Explicit"), + asm.sig([gc_handle_class], Type::Int(Int::ISize)), + MethodKind::Instance, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [gc_handle]) } pub fn gc_handle_to_obj(self, obj: ClassRefIdx, asm: &mut Assembly) -> Self { - let gc_handle = call!( - CallSite::new( - Some(ClassRef::gc_handle(asm)), - "FromIntPtr".to_owned().into(), - FnSig::new( - Box::new([Type::Int(Int::ISize)]), - Type::ClassRef(ClassRef::gc_handle(asm)) - ), - true - ), - [self] + let gc_handle_class = Type::ClassRef(ClassRef::gc_handle(asm)); + let mref = MethodRef::new( + ClassRef::gc_handle(asm), + asm.alloc_string("FromIntPtr"), + asm.sig([Type::Int(Int::ISize)], gc_handle_class), + MethodKind::Static, + vec![].into(), ); + let gc_handle = call!(asm.alloc_methodref(mref), [self]); let gc_handle = CILNode::TemporaryLocal(Box::new(( - Type::ClassRef(ClassRef::gc_handle(asm)), + gc_handle_class, [CILRoot::SetTMPLocal { value: gc_handle }].into(), CILNode::LoadAddresOfTMPLocal, ))); - let gc_handle_tpe = Type::ClassRef(ClassRef::gc_handle(asm)); - let object = call!( - CallSite::new( - Some(ClassRef::gc_handle(asm)), - "get_Target".to_owned().into(), - FnSig::new(Box::new([asm.nref(gc_handle_tpe)]), Type::PlatformObject), - false, - ), - [gc_handle] + + let gc_handle_ref = asm.nref(gc_handle_class); + let mref = MethodRef::new( + ClassRef::gc_handle(asm), + asm.alloc_string("get_Target"), + asm.sig([gc_handle_ref], Type::PlatformObject), + MethodKind::Instance, + vec![].into(), ); + let object = call!(asm.alloc_methodref(mref), [gc_handle]); CILNode::CheckedCast(Box::new((object, obj))) } + #[must_use] pub fn select(tpe: Type, a: Self, b: Self, predictate: Self) -> Self { + todo!() + } + /* match tpe { Type::Int(Int::U128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_u128".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::U128), Type::Int(Int::U128), Type::Bool]), Type::Int(Int::U128) ), - true + MethodKind::Static, + vec![].into() ), [a, b, predictate] ), Type::Int(Int::I128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_i128".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::I128), Type::Int(Int::I128), Type::Bool]), Type::Int(Int::I128) ), - true + MethodKind::Static, + vec![].into() ), [a, b, predictate] ), Type::Int(Int::USize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_usize".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::USize), Type::Int(Int::USize), Type::Bool]), Type::Int(Int::USize) ), - true + MethodKind::Static, + vec![].into() ), [a, b, predictate] ), Type::Ptr(_) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_usize".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::USize), Type::Int(Int::USize), Type::Bool]), Type::Int(Int::USize) ), - true + MethodKind::Static, + vec![].into() ), [ a.cast_ptr(Type::Int(Int::USize)), @@ -398,75 +399,81 @@ impl CILNode { ) .cast_ptr(tpe), Type::Int(Int::ISize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_isize".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::ISize), Type::Int(Int::ISize), Type::Bool]), Type::Int(Int::ISize) ), - true + MethodKind::Static, + vec![].into() ), [a, b, predictate] ), Type::Int(Int::U64) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_u64".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::U64), Type::Int(Int::U64), Type::Bool]), Type::Int(Int::U64) ), - true + MethodKind::Static, + vec![].into() ), [a, b, predictate] ), Type::Int(Int::I64) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_i64".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::I64), Type::Int(Int::I64), Type::Bool]), Type::Int(Int::I64) ), - true + MethodKind::Static, + vec![].into() ), [a, b, predictate] ), Type::Int(Int::U32) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_u32".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::U32), Type::Int(Int::U32), Type::Bool]), Type::Int(Int::U32) ), - true + MethodKind::Static, + vec![].into() ), [a, b, predictate] ), Type::Int(Int::I32) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_i32".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::I32), Type::Int(Int::I32), Type::Bool]), Type::Int(Int::I32) ), - true + MethodKind::Static, + vec![].into() ), [a, b, predictate] ), Type::Int(Int::U16) => call!( - CallSite::builtin( + MethodRef::new( "select_u16".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::U16), Type::Int(Int::U16), Type::Bool]), Type::Int(Int::U16) ), - true + MethodKind::Static, + vec![].into() ), [a, b, predictate] ), Type::Int(Int::I16) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_i16".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::I16), Type::Int(Int::I16), Type::Bool]), Type::Int(Int::I16) ), @@ -475,9 +482,9 @@ impl CILNode { [a, b, predictate] ), Type::Int(Int::U8) | Type::Bool => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_u8".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::U8), Type::Int(Int::U8), Type::Bool]), Type::Int(Int::U8) ), @@ -486,9 +493,9 @@ impl CILNode { [a, b, predictate] ), Type::Int(Int::I8) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "select_i8".to_owned().into(), - FnSig::new( + asm.sig( Box::new([Type::Int(Int::I8), Type::Int(Int::I8), Type::Bool]), Type::Int(Int::I8) ), @@ -498,7 +505,7 @@ impl CILNode { ), _ => todo!("Can't select type {tpe:?}"), } - } + }*/ /// Checks if this node may have side effects. `false` means that the node can't have side effects, `true` means that the node *may* have side effects, but it does not have to. pub fn has_side_effects(&self) -> bool { diff --git a/cilly/src/cil_root.rs b/cilly/src/cil_root.rs index 114fe919..4937df44 100644 --- a/cilly/src/cil_root.rs +++ b/cilly/src/cil_root.rs @@ -1,7 +1,9 @@ -use crate::v2::{Assembly, ClassRef, FieldIdx, FnSig, StaticFieldDesc, Type}; +use crate::v2::cilnode::MethodKind; +use crate::v2::{ + Assembly, ClassRef, FieldIdx, FnSig, MethodRef, MethodRefIdx, StaticFieldDesc, Type, +}; use crate::{ call, - call_site::CallSite, cil_node::{CILNode, CallOpArgs}, AsmString, IString, }; @@ -76,7 +78,7 @@ pub enum CILRoot { }, Call { - site: Box, + site: MethodRefIdx, args: Box<[CILNode]>, }, SetField { @@ -118,7 +120,7 @@ pub enum CILRoot { count: Box, }, CallVirt { - site: Box, + site: MethodRefIdx, args: Box<[CILNode]>, }, Ret { @@ -154,10 +156,16 @@ impl CILRoot { pub fn throw(msg: &str, asm: &mut Assembly) -> Self { let class = ClassRef::exception(asm); - let name = ".ctor".to_owned().into(); - let signature = FnSig::new(Box::new([class.into(), Type::PlatformString]), Type::Void); + let name = asm.alloc_string(".ctor"); + let signature = asm.sig([class.into(), Type::PlatformString], Type::Void); Self::Throw(CILNode::NewObj(Box::new(CallOpArgs { - site: CallSite::boxed(Some(class), name, signature, false), + site: asm.alloc_methodref(MethodRef::new( + class, + name, + signature, + MethodKind::Instance, + vec![].into(), + )), args: [CILNode::LdStr(msg.into())].into(), }))) } @@ -165,11 +173,17 @@ impl CILRoot { pub fn debug(msg: &str, asm: &mut Assembly) -> Self { let class = ClassRef::console(asm); - let name = "WriteLine".to_owned().into(); - let signature = FnSig::new(Box::new([Type::PlatformString]), Type::Void); + let name = asm.alloc_string("WriteLine"); + let signature = asm.sig([Type::PlatformString], Type::Void); let message = tiny_message(msg, asm); Self::Call { - site: Box::new(CallSite::new_extern(class, name, signature, true)), + site: asm.alloc_methodref(MethodRef::new( + class, + name, + signature, + MethodKind::Static, + vec![].into(), + )), args: [message].into(), } } @@ -382,83 +396,98 @@ fn tiny_message(msg: &str, asm: &mut Assembly) -> CILNode { runtime_string(&pieces, asm) } fn runtime_string(pieces: &[&str], asm: &mut Assembly) -> CILNode { + // match pieces.len() { 0 => panic!("Incorrect piece count"), 1 => CILNode::LdStr(pieces[0].to_owned().into()), - 2 => call!( - CallSite::new_extern( + 2 => { + let mref = MethodRef::new( ClassRef::string(asm), - "Concat".to_owned().into(), - FnSig::new( - Box::new([Type::PlatformString, Type::PlatformString,]), + asm.alloc_string("Concat"), + asm.sig( + [Type::PlatformString, Type::PlatformString], Type::PlatformString, ), - true - ), - [ - CILNode::LdStr(pieces[0].to_owned().into()), - CILNode::LdStr(pieces[1].to_owned().into()) - ] - ), - 3 => call!( - CallSite::new_extern( + MethodKind::Static, + vec![].into(), + ); + call!( + asm.alloc_methodref(mref), + [ + CILNode::LdStr(pieces[0].to_owned().into()), + CILNode::LdStr(pieces[1].to_owned().into()) + ] + ) + } + 3 => { + let mref = MethodRef::new( ClassRef::string(asm), - "Concat".to_owned().into(), - FnSig::new( - Box::new([ + asm.alloc_string("Concat"), + asm.sig( + [ Type::PlatformString, Type::PlatformString, Type::PlatformString, - ]), + ], Type::PlatformString, ), - true - ), - [ - CILNode::LdStr(pieces[0].to_owned().into()), - CILNode::LdStr(pieces[1].to_owned().into()), - CILNode::LdStr(pieces[2].to_owned().into()) - ] - ), - 4 => call!( - CallSite::new_extern( + MethodKind::Static, + vec![].into(), + ); + call!( + asm.alloc_methodref(mref), + [ + CILNode::LdStr(pieces[0].to_owned().into()), + CILNode::LdStr(pieces[1].to_owned().into()), + CILNode::LdStr(pieces[2].to_owned().into()) + ] + ) + } + 4 => { + let mref = MethodRef::new( ClassRef::string(asm), - "Concat".to_owned().into(), - FnSig::new( - Box::new([ + asm.alloc_string("Concat"), + asm.sig( + [ Type::PlatformString, Type::PlatformString, Type::PlatformString, Type::PlatformString, - ]), + ], Type::PlatformString, ), - true - ), - [ - CILNode::LdStr(pieces[0].to_owned().into()), - CILNode::LdStr(pieces[1].to_owned().into()), - CILNode::LdStr(pieces[2].to_owned().into()), - CILNode::LdStr(pieces[3].to_owned().into()) - ] - ), + MethodKind::Static, + vec![].into(), + ); + call!( + asm.alloc_methodref(mref), + [ + CILNode::LdStr(pieces[0].to_owned().into()), + CILNode::LdStr(pieces[1].to_owned().into()), + CILNode::LdStr(pieces[2].to_owned().into()), + CILNode::LdStr(pieces[3].to_owned().into()) + ] + ) + } _ => { let sub_part = pieces.len() / 4; - call!( - CallSite::new_extern( - ClassRef::string(asm), - "Concat".to_owned().into(), - FnSig::new( - Box::new([ - Type::PlatformString, - Type::PlatformString, - Type::PlatformString, - Type::PlatformString, - ]), + let mref = MethodRef::new( + ClassRef::string(asm), + asm.alloc_string("Concat"), + asm.sig( + [ Type::PlatformString, - ), - true + Type::PlatformString, + Type::PlatformString, + Type::PlatformString, + ], + Type::PlatformString, ), + MethodKind::Static, + vec![].into(), + ); + call!( + asm.alloc_methodref(mref), [ runtime_string(&pieces[..sub_part], asm), runtime_string(&pieces[sub_part..(sub_part * 2)], asm), diff --git a/cilly/src/entrypoint.rs b/cilly/src/entrypoint.rs index 3de3d6b0..61d363f4 100644 --- a/cilly/src/entrypoint.rs +++ b/cilly/src/entrypoint.rs @@ -4,21 +4,21 @@ use crate::{ access_modifier::AccessModifer, basic_block::BasicBlock, call, - call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, conv_isize, conv_usize, ldc_u32, method::{Attribute, Method, MethodType}, - v2::{Assembly, FnSig, Int}, + v2::{cilnode::MethodKind, Assembly, FnSig, Int, MethodRef}, Type, }; -/// Creates a wrapper method around entypoint represented by `CallSite` -pub fn wrapper(entrypoint: &CallSite, asm: &mut Assembly) -> Method { +/// Creates a wrapper method around entypoint represented by `MethodRefIdx` +pub fn wrapper(entrypoint: MethodRef, asm: &mut Assembly) -> Method { let uint8_ptr = asm.nptr(Type::Int(Int::U8)); let uint8_ptr_ptr = asm.nptr(uint8_ptr); - if entrypoint.signature().inputs() == [Type::Int(Int::ISize), uint8_ptr_ptr] - && entrypoint.signature().output() == &Type::Int(Int::ISize) + let entry_sig = &asm[entrypoint.sig()]; + if entry_sig.inputs() == [Type::Int(Int::ISize), uint8_ptr_ptr] + && entry_sig.output() == &Type::Int(Int::ISize) { let sig = FnSig::new( Box::new([Type::PlatformArray { @@ -27,7 +27,20 @@ pub fn wrapper(entrypoint: &CallSite, asm: &mut Assembly) -> Method { }]), Type::Void, ); - + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(".tcctor"), + asm.sig([], Type::Void), + MethodKind::Static, + vec![].into(), + ); + let static_mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("static_init"), + asm.sig([], Type::Void), + MethodKind::Static, + vec![].into(), + ); let mut method = Method::new( AccessModifer::Extern, MethodType::Static, @@ -37,28 +50,18 @@ pub fn wrapper(entrypoint: &CallSite, asm: &mut Assembly) -> Method { vec![BasicBlock::new( vec![ CILRoot::Call { - site: Box::new(CallSite::new( - None, - ".tcctor".into(), - FnSig::new(Box::new([]), Type::Void), - true, - )), + site: asm.alloc_methodref(mref), args: [].into(), } .into(), CILRoot::Call { - site: Box::new(CallSite::new( - None, - "static_init".into(), - FnSig::new(Box::new([]), Type::Void), - true, - )), + site: asm.alloc_methodref(static_mref), args: [].into(), } .into(), CILRoot::Pop { tree: call!( - Box::new(entrypoint.clone()), + (asm.alloc_methodref(entrypoint)), [ conv_isize!(ldc_u32!(0)), conv_usize!(ldc_u32!(0)).cast_ptr(uint8_ptr_ptr) @@ -76,49 +79,52 @@ pub fn wrapper(entrypoint: &CallSite, asm: &mut Assembly) -> Method { //method.set_ops(ops); method.add_attribute(Attribute::EntryPoint); method - } else if entrypoint.signature().inputs().is_empty() - && entrypoint.signature().output() == &Type::Void - { + } else if entry_sig.inputs().is_empty() && entry_sig.output() == &Type::Void { let sig = FnSig::new(Box::new([]), Type::Void); + let tcctor = MethodRef::new( + *asm.main_module(), + asm.alloc_string(".tcctor"), + asm.sig([], Type::Void), + MethodKind::Static, + vec![].into(), + ); + let static_init = MethodRef::new( + *asm.main_module(), + asm.alloc_string("static_init"), + asm.sig([], Type::Void), + MethodKind::Static, + vec![].into(), + ); + let blocks = vec![BasicBlock::new( + vec![ + CILRoot::Call { + site: asm.alloc_methodref(tcctor), + args: [].into(), + } + .into(), + CILRoot::Call { + site: asm.alloc_methodref(static_init), + args: [].into(), + } + .into(), + CILRoot::Call { + site: asm.alloc_methodref(entrypoint.clone()), + args: [].into(), + } + .into(), + //CILRoot::debug(&format!("Preparing to execute the main program.")).into(), + CILRoot::VoidRet.into(), + ], + 0, + None, + )]; let mut method = Method::new( AccessModifer::Extern, MethodType::Static, sig, "entrypoint", vec![], - vec![BasicBlock::new( - vec![ - CILRoot::Call { - site: Box::new(CallSite::new( - None, - ".tcctor".into(), - FnSig::new(Box::new([]), Type::Void), - true, - )), - args: [].into(), - } - .into(), - CILRoot::Call { - site: Box::new(CallSite::new( - None, - "static_init".into(), - FnSig::new(Box::new([]), Type::Void), - true, - )), - args: [].into(), - } - .into(), - CILRoot::Call { - site: Box::new(entrypoint.clone()), - args: [].into(), - } - .into(), - //CILRoot::debug(&format!("Preparing to execute the main program.")).into(), - CILRoot::VoidRet.into(), - ], - 0, - None, - )], + blocks, vec![], ); diff --git a/cilly/src/method.rs b/cilly/src/method.rs index eb6f6c09..d812feab 100644 --- a/cilly/src/method.rs +++ b/cilly/src/method.rs @@ -8,13 +8,12 @@ use std::{ use crate::{ access_modifier::AccessModifer, basic_block::BasicBlock, - call_site::CallSite, cil_iter::{CILIterElem, CILIterTrait}, cil_iter_mut::CILIterElemMut, cil_node::CILNode, cil_root::CILRoot, cil_tree::CILTree, - v2::{Assembly, FnSig}, + v2::{cilnode::MethodKind, Assembly, FnSig, MethodRef, MethodRefIdx}, IString, Type, }; @@ -46,11 +45,11 @@ pub enum Attribute { /// Set if the function is the assemblys entrypoint. EntryPoint, /// This method is nothing more than an alias for another method. - AliasFor(Box), + AliasFor(Box), } impl Attribute { - pub fn as_alias_for(&self) -> Option<&CallSite> { + pub fn as_alias_for(&self) -> Option<&MethodRef> { if let Self::AliasFor(v) = self { Some(v) } else { @@ -60,23 +59,6 @@ impl Attribute { } impl Method { - pub fn alias_for( - access: AccessModifer, - method_type: MethodType, - name: IString, - alias_for: CallSite, - ) -> Self { - Self { - access, - method_type, - sig: alias_for.signature().clone(), - name, - locals: vec![], - blocks: vec![], - attributes: vec![Attribute::AliasFor(Box::new(alias_for))], - arg_names: vec![], - } - } pub fn maxstack(&self) -> usize { let trees = self.blocks().iter().flat_map(|block| block.trees()); let max = trees.map(|tree| tree.root().into_iter().count() + 3).max(); @@ -276,7 +258,7 @@ impl Method { } /// Returns the list of external calls this function preforms. Calls may repeat. // TODO: make this not call `into_ops` - pub fn calls(&self) -> impl Iterator { + pub fn calls(&self) -> impl Iterator + '_ { self.blocks .iter() .flat_map(|block| block.iter_cil()) @@ -298,13 +280,21 @@ impl Method { }*/ /// Returns a call site that describes this method. - pub fn call_site(&self) -> CallSite { - CallSite::new( - None, - self.name().to_owned().into(), - self.sig().clone(), - self.is_static(), - ) + pub fn call_site(&self, asm: &mut crate::v2::Assembly) -> MethodRefIdx { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(self.name()), + asm.alloc_sig(self.sig().clone()), + if self.is_static() { + MethodKind::Static + } else if self.name() == ".ctor" { + MethodKind::Constructor + } else { + MethodKind::Instance + }, + vec![].into(), + ); + asm.alloc_methodref(mref) } /// Alocates all temporary variables within this method. pub fn allocate_temporaries(&mut self) { diff --git a/cilly/src/utilis.rs b/cilly/src/utilis.rs index e1945f8d..5cdadd49 100644 --- a/cilly/src/utilis.rs +++ b/cilly/src/utilis.rs @@ -2,21 +2,22 @@ use std::fmt::Debug; use crate::method::Method; -use crate::v2::{ClassRef, FnSig, Int, StaticFieldDesc}; -use crate::{ - asm::Assembly, call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, eq, lt, size_of, -}; +use crate::v2::cilnode::MethodKind; +use crate::v2::{ClassRef, FnSig, Int, MethodRef, MethodRefIdx, StaticFieldDesc}; +use crate::{asm::Assembly, cil_node::CILNode, cil_root::CILRoot, eq, lt, size_of}; use crate::{call, call_virt, conv_i32, conv_usize, ldc_i32, ldc_u32, mul, Type}; -pub fn argc_argv_init_method(asm: &mut Assembly) -> CallSite { +pub fn argc_argv_init_method(asm: &mut Assembly) -> MethodRefIdx { use std::num::NonZeroU8; - let init_cs = CallSite::new( - None, - "argc_argv_init".into(), - FnSig::new(Box::new([]), Type::Void), - true, + let init_cs = MethodRef::new( + *asm.main_module(), + asm.alloc_string("argc_argv_init"), + asm.sig([], Type::Void), + MethodKind::Static, + vec![].into(), ); - if asm.contains_fn(&init_cs) { + let init_cs = asm.alloc_methodref(init_cs); + if asm.contains_fn(init_cs) { return init_cs; } let mut init_method = Method::new( @@ -46,23 +47,23 @@ pub fn argc_argv_init_method(asm: &mut Assembly) -> CallSite { let arg_idx = u32::try_from(init_method.add_local(Type::Int(Int::I32), Some("arg_idx".into()))).unwrap(); // Get managed args + let string = asm.alloc_type(Type::PlatformString); + let mref = MethodRef::new( + ClassRef::enviroment(asm), + asm.alloc_string("GetCommandLineArgs"), + asm.sig( + [], + Type::PlatformArray { + elem: string, + dims: NonZeroU8::new(1).unwrap(), + }, + ), + MethodKind::Static, + vec![].into(), + ); let margs_init = CILRoot::STLoc { local: managed_args, - tree: call!( - CallSite::new_extern( - ClassRef::enviroment(asm), - "GetCommandLineArgs".into(), - FnSig::new( - Box::new([]), - Type::PlatformArray { - elem: asm.alloc_type(Type::PlatformString), - dims: NonZeroU8::new(1).unwrap() - } - ), - true - ), - [] - ), + tree: call!(asm.alloc_methodref(mref), []), }; // Calculate argc let argc_init = CILRoot::STLoc { @@ -71,10 +72,10 @@ pub fn argc_argv_init_method(asm: &mut Assembly) -> CallSite { arr: CILNode::LDLoc(managed_args).into() }), }; - + let aligned_alloc = MethodRef::aligned_alloc(asm); // Alloc argv let tree = call!( - CallSite::aligned_alloc(asm), + asm.alloc_methodref(aligned_alloc), [ mul!( conv_usize!(CILNode::LDLoc(argc)), @@ -235,28 +236,28 @@ pub fn argc_argv_init_method(asm: &mut Assembly) -> CallSite { init_cs } pub fn mstring_to_utf8ptr(mstring: CILNode, asm: &mut Assembly) -> CILNode { - call!( - CallSite::new_extern( - ClassRef::marshal(asm), - "StringToCoTaskMemUTF8".into(), - FnSig::new(Box::new([Type::PlatformString]), Type::Int(Int::ISize)), - true - ), - [mstring] - ) - .cast_ptr(asm.nptr(Type::Int(Int::U8))) + let mref = MethodRef::new( + ClassRef::marshal(asm), + asm.alloc_string("StringToCoTaskMemUTF8"), + asm.sig([Type::PlatformString], Type::Int(Int::ISize)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [mstring]).cast_ptr(asm.nptr(Type::Int(Int::U8))) } -pub fn get_environ(asm: &mut Assembly) -> CallSite { +pub fn get_environ(asm: &mut Assembly) -> MethodRefIdx { let uint8_ptr = asm.nptr(Type::Int(Int::U8)); let uint8_ptr_ptr = asm.nptr(uint8_ptr); - let init_cs = CallSite::new( - None, - "get_environ".into(), - FnSig::new(Box::new([]), uint8_ptr_ptr), - true, + let init_cs = MethodRef::new( + *asm.main_module(), + asm.alloc_string("get_environ"), + asm.sig([], uint8_ptr_ptr), + MethodKind::Static, + vec![].into(), ); - if asm.contains_fn(&init_cs) { + let init_cs = asm.alloc_methodref(init_cs); + if asm.contains_fn(init_cs) { return init_cs; } @@ -314,34 +315,33 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite { .into(), ); let init = &mut blocks[init_bb as usize]; + let i_dictionary = Type::ClassRef(ClassRef::i_dictionary(asm)); + let mref = MethodRef::new( + ClassRef::enviroment(asm), + asm.alloc_string("GetEnvironmentVariables"), + asm.sig([], i_dictionary), + MethodKind::Static, + vec![].into(), + ); init.trees_mut().push( CILRoot::STLoc { local: dictionary_local, - tree: call!( - CallSite::new( - Some(ClassRef::enviroment(asm)), - "GetEnvironmentVariables".into(), - FnSig::new(Box::new([]), Type::ClassRef(ClassRef::i_dictionary(asm))), - true - ), - [] - ), + tree: call!(asm.alloc_methodref(mref), []), } .into(), ); + let mref = MethodRef::new( + ClassRef::i_collection(asm), + asm.alloc_string("get_Count"), + asm.sig([i_dictionary], Type::Int(Int::I32)), + MethodKind::Instance, + vec![].into(), + ); init.trees_mut().push( CILRoot::STLoc { local: envc, tree: call_virt!( - CallSite::new( - Some(ClassRef::i_collection(asm)), - "get_Count".into(), - FnSig::new( - Box::new([Type::ClassRef(ClassRef::i_dictionary(asm))]), - Type::Int(Int::I32) - ), - false - ), + asm.alloc_methodref(mref), [CILNode::LDLoc(dictionary_local)] ), } @@ -350,10 +350,11 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite { let element_count = CILNode::LDLoc(envc) + ldc_i32!(1); let arr_size = conv_usize!(element_count) * conv_usize!(size_of!(uint8_ptr_ptr)); let arr_align = conv_usize!(size_of!(uint8_ptr_ptr)); + let aligned_alloc = MethodRef::aligned_alloc(asm); init.trees_mut().push( CILRoot::STLoc { local: arr_ptr, - tree: call!(CallSite::aligned_alloc(asm), [arr_size, arr_align]) + tree: call!(asm.alloc_methodref(aligned_alloc), [arr_size, arr_align]) .cast_ptr(uint8_ptr_ptr), } .into(), @@ -365,19 +366,19 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite { } .into(), ); + let dictionary_iterator = ClassRef::dictionary_iterator(asm); + let mref = MethodRef::new( + ClassRef::i_dictionary(asm), + asm.alloc_string("GetEnumerator"), + asm.sig([i_dictionary], Type::ClassRef(dictionary_iterator)), + MethodKind::Instance, + vec![].into(), + ); init.trees_mut().push( CILRoot::STLoc { local: iter_local, tree: call_virt!( - CallSite::new( - Some(ClassRef::i_dictionary(asm)), - "GetEnumerator".into(), - FnSig::new( - Box::new([Type::ClassRef(ClassRef::i_dictionary(asm))]), - Type::ClassRef(ClassRef::dictionary_iterator(asm)) - ), - false - ), + asm.alloc_methodref(mref), [CILNode::LDLoc(dictionary_local)] ), } @@ -403,39 +404,34 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite { .into(), ); let loop_body = &mut blocks[loop_body_bb as usize]; + let move_next = MethodRef::new( + ClassRef::i_enumerator(asm), + asm.alloc_string("MoveNext"), + asm.sig([Type::ClassRef(dictionary_iterator)], Type::Bool), + MethodKind::Instance, + vec![].into(), + ); loop_body.trees_mut().push( CILRoot::BFalse { target: loop_end_bb, sub_target: 0, - cond: call_virt!( - CallSite::new_extern( - ClassRef::i_enumerator(asm), - "MoveNext".into(), - FnSig::new( - Box::new([Type::ClassRef(ClassRef::dictionary_iterator(asm))]), - Type::Bool, - ), - false - ), - [CILNode::LDLoc(iter_local)] - ), + cond: call_virt!(asm.alloc_methodref(move_next), [CILNode::LDLoc(iter_local)]), } .into(), ); + let get_current = MethodRef::new( + ClassRef::i_enumerator(asm), + asm.alloc_string("get_Current"), + asm.sig([Type::ClassRef(dictionary_iterator)], Type::PlatformObject), + MethodKind::Instance, + vec![].into(), + ); loop_body.trees_mut().push( CILRoot::STLoc { local: keyval, tree: CILNode::UnboxAny( Box::new(call_virt!( - CallSite::new_extern( - ClassRef::i_enumerator(asm), - "get_Current".into(), - FnSig::new( - Box::new([Type::ClassRef(ClassRef::dictionary_iterator(asm))]), - Type::PlatformObject, - ), - false - ), + asm.alloc_methodref(get_current), [CILNode::LDLoc(iter_local)] )), Box::new(Type::ClassRef(keyval_tpe)), @@ -443,47 +439,43 @@ pub fn get_environ(asm: &mut Assembly) -> CallSite { } .into(), ); - let key = call!( - CallSite::new_extern( - keyval_tpe, - "get_Key".into(), - FnSig::new( - Box::new([asm.nref(Type::ClassRef(keyval_tpe))]), - Type::PlatformObject - ), - false, + let keyval_tpe_ref = asm.nref(Type::ClassRef(keyval_tpe)); + let sig = asm.sig([keyval_tpe_ref], Type::PlatformObject); + let get_key = MethodRef::new( + keyval_tpe, + asm.alloc_string("get_Key"), + sig, + MethodKind::Instance, + vec![].into(), + ); + let key = call!(asm.alloc_methodref(get_key), [CILNode::LDLocA(keyval)]); + let mref = MethodRef::new( + keyval_tpe, + asm.alloc_string("get_Value"), + sig, + MethodKind::Instance, + vec![].into(), + ); + let value = call!(asm.alloc_methodref(mref), [CILNode::LDLocA(keyval)]); + let concat = MethodRef::new( + ClassRef::string(asm), + asm.alloc_string("Concat"), + asm.sig( + [ + Type::PlatformObject, + Type::PlatformObject, + Type::PlatformObject, + ], + Type::PlatformString, ), - [CILNode::LDLocA(keyval)] - ); - let value = call!( - CallSite::new_extern( - keyval_tpe, - "get_Value".into(), - FnSig::new( - Box::new([asm.nref(Type::ClassRef(keyval_tpe))]), - Type::PlatformObject - ), - false, - ), - [CILNode::LDLocA(keyval)] + MethodKind::Static, + vec![].into(), ); loop_body.trees_mut().push( CILRoot::STLoc { local: encoded_keyval, tree: call!( - CallSite::new_extern( - ClassRef::string(asm), - "Concat".into(), - FnSig::new( - Box::new([ - Type::PlatformObject, - Type::PlatformObject, - Type::PlatformObject - ]), - Type::PlatformString - ), - true - ), + asm.alloc_methodref(concat), [key, CILNode::LdStr("=".into()), value] ), } diff --git a/cilly/src/v2/cilnode.rs b/cilly/src/v2/cilnode.rs index 9e561ce1..df3b799b 100644 --- a/cilly/src/v2/cilnode.rs +++ b/cilly/src/v2/cilnode.rs @@ -6,7 +6,7 @@ use super::field::StaticFieldIdx; use super::{bimap::IntoBiMapIndex, Assembly, Const, Int, MethodRefIdx, SigIdx, TypeIdx}; use super::{ClassRef, FieldIdx, Float}; use crate::cil_node::CILNode as V1Node; -use crate::v2::{MethodRef, Type}; +use crate::v2::Type; #[derive(Hash, PartialEq, Eq, Clone, Copy, Debug, Serialize, Deserialize)] pub struct NodeIdx(BiMapIndex); @@ -736,17 +736,7 @@ impl CILNode { asm.alloc_node(node) }) .collect(); - let sig = asm.alloc_sig(callargs.site.signature().clone()); - let generics: Box<[_]> = (callargs.site.generics()).into(); - let class = callargs.site.class().unwrap_or_else(|| *asm.main_module()); - let name = asm.alloc_string(callargs.site.name()); - let method_ref = if callargs.site.is_static() { - MethodRef::new(class, name, sig, MethodKind::Static, generics) - } else { - MethodRef::new(class, name, sig, MethodKind::Instance, generics) - }; - let method_ref = asm.alloc_methodref(method_ref); - Self::Call(Box::new((method_ref, args))) + Self::Call(Box::new((callargs.site, args))) } V1Node::CallVirt(callargs) => { let args: Box<[_]> = callargs @@ -757,14 +747,8 @@ impl CILNode { asm.alloc_node(node) }) .collect(); - let sig = asm.alloc_sig(callargs.site.signature().clone()); - let generics: Box<[_]> = (callargs.site.generics()).into(); - let class = callargs.site.class().unwrap_or_else(|| *asm.main_module()); - let name = asm.alloc_string(callargs.site.name()); - assert!(!callargs.site.is_static()); - let method_ref = MethodRef::new(class, name, sig, MethodKind::Virtual, generics); - let method_ref = asm.alloc_methodref(method_ref); - Self::Call(Box::new((method_ref, args))) + + Self::Call(Box::new((callargs.site, args))) } V1Node::NewObj(callargs) => { let args: Box<[_]> = callargs @@ -775,19 +759,7 @@ impl CILNode { asm.alloc_node(node) }) .collect(); - let sig = asm.alloc_sig(callargs.site.signature().clone()); - let generics: Box<[_]> = (callargs.site.generics()).into(); - let class = callargs.site.class().unwrap_or_else(|| *asm.main_module()); - let name = asm.alloc_string(callargs.site.name()); - assert!( - !callargs.site.is_static(), - "Newobj site invalid(is static):{:?}", - callargs.site - ); - let method_ref = - MethodRef::new(class, name, sig, MethodKind::Constructor, generics); - let method_ref = asm.alloc_methodref(method_ref); - Self::Call(Box::new((method_ref, args))) + Self::Call(Box::new((callargs.site, args))) } // Special V1Node::GetException => Self::GetException, @@ -851,20 +823,7 @@ impl CILNode { } V1Node::AddressOfStaticField(sfld) => Self::LdStaticFieldAdress(asm.alloc_sfld(**sfld)), V1Node::LDStaticField(sfld) => Self::LdStaticField(asm.alloc_sfld(**sfld)), - V1Node::LDFtn(site) => { - let sig = asm.alloc_sig(site.signature().clone()); - let generics: Box<[_]> = (site.generics()).into(); - let class = site.class().unwrap_or_else(|| *asm.main_module()); - let name = asm.alloc_string(site.name()); - - let method_ref = if site.is_static() { - MethodRef::new(class, name, sig, MethodKind::Static, generics) - } else { - MethodRef::new(class, name, sig, MethodKind::Instance, generics) - }; - let method_ref = asm.alloc_methodref(method_ref); - Self::LdFtn(method_ref) - } + V1Node::LDFtn(method_ref) => Self::LdFtn(*method_ref), V1Node::Volatile(inner) => { let mut tmp = Self::from_v1(inner, asm); if let Self::LdInd { volitale, .. } = &mut tmp { diff --git a/cilly/src/v2/cilroot.rs b/cilly/src/v2/cilroot.rs index 47fd6aff..d61b11ce 100644 --- a/cilly/src/v2/cilroot.rs +++ b/cilly/src/v2/cilroot.rs @@ -2,10 +2,9 @@ use serde::{Deserialize, Serialize}; use super::{ bimap::{BiMapIndex, IntoBiMapIndex}, - cilnode::MethodKind, field::FieldIdx, - Assembly, CILNode, Float, Int, MethodRef, MethodRefIdx, NodeIdx, SigIdx, StaticFieldIdx, - StringIdx, Type, TypeIdx, + Assembly, CILNode, Float, Int, MethodRefIdx, NodeIdx, SigIdx, StaticFieldIdx, StringIdx, Type, + TypeIdx, }; use crate::cil_root::CILRoot as V1Root; //use crate::cil_node::CILNode as V1Node; @@ -322,17 +321,7 @@ impl CILRoot { asm.alloc_node(node) }) .collect(); - let sig = asm.alloc_sig(site.signature().clone()); - let generics: Box<[_]> = (site.generics()).into(); - let class = site.class().unwrap_or_else(|| *asm.main_module()); - let name = asm.alloc_string(site.name()); - let method_ref = if site.is_static() { - MethodRef::new(class, name, sig, MethodKind::Static, generics) - } else { - MethodRef::new(class, name, sig, MethodKind::Instance, generics) - }; - let method_ref = asm.alloc_methodref(method_ref); - Self::Call(Box::new((method_ref, args))) + Self::Call(Box::new((*site, args))) } V1Root::CallVirt { site, args } => { let args: Box<[_]> = args @@ -342,14 +331,7 @@ impl CILRoot { asm.alloc_node(node) }) .collect(); - let sig = asm.alloc_sig(site.signature().clone()); - let generics: Box<[_]> = (site.generics()).into(); - let class = site.class().unwrap_or_else(|| *asm.main_module()); - let name = asm.alloc_string(site.name()); - assert!(!site.is_static()); - let method_ref = MethodRef::new(class, name, sig, MethodKind::Virtual, generics); - let method_ref = asm.alloc_methodref(method_ref); - Self::Call(Box::new((method_ref, args))) + Self::Call(Box::new((*site, args))) } V1Root::SetField { value, addr, desc } => { let value = CILNode::from_v1(value, asm); diff --git a/cilly/src/v2/method.rs b/cilly/src/v2/method.rs index e3e97330..1f19394b 100644 --- a/cilly/src/v2/method.rs +++ b/cilly/src/v2/method.rs @@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize}; use super::{ bimap::{BiMapIndex, IntoBiMapIndex}, cilnode::MethodKind, - Access, Assembly, BasicBlock, CILIterElem, CILNode, ClassDefIdx, ClassRefIdx, SigIdx, - StringIdx, Type, TypeIdx, + Access, Assembly, BasicBlock, CILIterElem, CILNode, ClassDefIdx, ClassRef, ClassRefIdx, Int, + SigIdx, StringIdx, Type, TypeIdx, }; use crate::v2::iter::TpeIter; use crate::v2::CILRoot; @@ -101,6 +101,28 @@ impl MethodRef { MethodKind::Constructor => Type::ClassRef(self.class()), } } + + pub fn aligned_alloc(asm: &mut crate::v2::Assembly) -> MethodRef { + let void_ptr = asm.nptr(Type::Void); + let sig = asm.sig([Type::Int(Int::USize), Type::Int(Int::USize)], void_ptr); + MethodRef::new( + ClassRef::native_mem(asm), + asm.alloc_string("AllignedAlloc"), + sig, + MethodKind::Static, + vec![].into(), + ) + } + pub fn alloc(asm: &mut crate::v2::Assembly) -> MethodRef { + let sig = asm.sig([Type::Int(Int::ISize)], Type::Int(Int::ISize)); + MethodRef::new( + ClassRef::marshal(asm), + asm.alloc_string("AllocHGlobal"), + sig, + MethodKind::Static, + vec![].into(), + ) + } } #[derive(Hash, PartialEq, Eq, Clone, Copy, Debug, Serialize, Deserialize)] @@ -241,21 +263,23 @@ impl MethodDef { asm: &mut super::Assembly, class: ClassDefIdx, ) -> Self { - let sig = asm.alloc_sig(v1.call_site().signature().clone()); + let site = v1.call_site(asm); + let sig = v1.sig().clone(); + let sig_idx = asm.alloc_sig(v1.sig().clone()); let acceess = match v1.access() { crate::access_modifier::AccessModifer::Private => Access::Private, crate::access_modifier::AccessModifer::Public => Access::Public, crate::access_modifier::AccessModifer::Extern => Access::Extern, }; - let kind = if v1.call_site().is_static() { + let kind = if v1.is_static() { MethodKind::Static - } else if v1.call_site().name() == ".ctor" { + } else if v1.name() == ".ctor" { MethodKind::Constructor } else { MethodKind::Instance }; - let name = asm.alloc_string(v1.call_site().name()); + let name = asm.alloc_string(v1.name()); let blocks = v1 .blocks() .iter() @@ -278,14 +302,14 @@ impl MethodDef { .map(|name| name.as_ref().map(|name| asm.alloc_string(name.clone()))) .collect(); let arg_debug_count = arg_names.len(); - let arg_sig_count = v1.call_site().signature().inputs().len(); + let arg_sig_count = sig.inputs().len(); match arg_debug_count.cmp(&arg_sig_count) { std::cmp::Ordering::Less => { println!( "WARNING: argument debug info count invalid(Too few). Expected {}, got {}. fn name:{}", arg_sig_count, arg_debug_count, - v1.call_site().name() + v1.name() ); arg_names.extend((arg_debug_count..arg_sig_count).map(|_| None)); } @@ -295,7 +319,7 @@ impl MethodDef { "WARNING: argument debug info count invalid(Too many). Expected {}, got {}. fn name:{}", arg_sig_count, arg_debug_count, - v1.call_site().name() + v1.name() ); for arg in &arg_names { println!("{:?}", arg.map(|arg| &asm[arg])); @@ -303,8 +327,16 @@ impl MethodDef { arg_names.truncate(arg_sig_count); } } - assert_eq!(arg_names.len(), v1.call_site().signature().inputs().len()); - MethodDef::new(acceess, class, name, sig, kind, implementation, arg_names) + assert_eq!(arg_names.len(), v1.sig().inputs().len()); + MethodDef::new( + acceess, + class, + name, + sig_idx, + kind, + implementation, + arg_names, + ) } #[must_use] diff --git a/src/aggregate.rs b/src/aggregate.rs index 3464cac5..de92d33a 100644 --- a/src/aggregate.rs +++ b/src/aggregate.rs @@ -6,11 +6,10 @@ use crate::{ utilis::{adt::set_discr, field_name}, }; use cilly::{ - call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, conv_usize, ldc_u64, - v2::{ClassRef, FieldDesc, FnSig, Int}, + v2::{cilnode::MethodKind, ClassRef, FieldDesc, FnSig, Int, MethodRef}, Type, }; use rustc_index::IndexVec; @@ -69,7 +68,6 @@ pub fn handle_aggregate<'tcx>( // This array is created from uninitalized data, so it itsefl is uninitialzed, so we can skip initializing it. return super::place::place_get(target_location, ctx); } - let element = ctx.monomorphize(*element); let element = ctx.type_from_cache(element); let array_type = ClassRef::fixed_array(element, value_index.len(), ctx); @@ -78,11 +76,17 @@ pub fn handle_aggregate<'tcx>( Box::new([ctx.nref(array_type.into()), Type::Int(Int::USize), element]), Type::Void, ); - let site = CallSite::new(Some(array_type), "set_Item".into(), sig, false); + let site = MethodRef::new( + array_type, + ctx.alloc_string("set_Item"), + ctx.alloc_sig(sig), + MethodKind::Instance, + vec![].into(), + ); let mut sub_trees = Vec::new(); for value in values { sub_trees.push(CILRoot::Call { - site: Box::new(site.clone()), + site: ctx.alloc_methodref(site.clone()), args: [ array_getter.clone(), conv_usize!(ldc_u64!(u64::from(value.0))), diff --git a/src/assembly.rs b/src/assembly.rs index 46eed435..3ce67343 100644 --- a/src/assembly.rs +++ b/src/assembly.rs @@ -13,14 +13,13 @@ use cilly::{ asm::Assembly, basic_block::BasicBlock, call, - call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, cil_tree::CILTree, conv_usize, ldc_i32, ldc_u32, ldc_u64, method::{Method, MethodType}, utilis::{self, encode}, - v2::{FnSig, Int, MethodDef, StaticFieldDesc}, + v2::{cilnode::MethodKind, FnSig, Int, MethodDef, MethodRef, MethodRefIdx, StaticFieldDesc}, Type, }; use rustc_middle::{ @@ -112,7 +111,7 @@ fn allocation_initializer_method( CILRoot::STLoc { local: 0, tree: Box::new(call!( - CallSite::aligned_alloc(asm), + MethodRef::aligned_alloc(asm), [ conv_usize!(ldc_u64!(bytes.len() as u64)), conv_usize!(ldc_u64!(align)) @@ -127,7 +126,7 @@ fn allocation_initializer_method( CILRoot::STLoc { local: 0, tree: Box::new(call!( - CallSite::alloc(asm), + MethodRef::alloc(asm), [ldc_i32!( i32::try_from(bytes.len()).expect("Static alloc too big") )] @@ -208,16 +207,18 @@ fn allocation_initializer_method( let mut ctx = MethodCompileCtx::new(tcx, None, finstance, asm); let call_info = crate::call_info::CallInfo::sig_from_instance_(finstance, &mut ctx); let function_name = crate::utilis::function_name(tcx.symbol_name(finstance)); - + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(function_name), + asm.alloc_sig(call_info.sig().clone()), + MethodKind::Static, + vec![].into(), + ); trees.push( CILRoot::STIndISize( (CILNode::LDLoc(0) + conv_usize!(ldc_u32!(offset))) .cast_ptr(asm.nptr(Type::Int(Int::USize))), - CILNode::LDFtn( - CallSite::new(None, function_name, call_info.sig().clone(), true) - .into(), - ) - .cast_ptr(Type::Int(Int::USize)), + CILNode::LDFtn(asm.alloc_methodref(mref)).cast_ptr(Type::Int(Int::USize)), ) .into(), ); @@ -565,10 +566,10 @@ pub fn add_item<'tcx>( if section.to_string().contains(".init_array") { let argc = utilis::argc_argv_init_method(asm); asm.add_initialzer(CILRoot::Call { - site: Box::new(argc), + site: argc, args: [].into(), }); - let get_environ: CallSite = utilis::get_environ(asm); + let get_environ: MethodRefIdx = utilis::get_environ(asm); let fn_ptr = alloc.0.provenance().ptrs().iter().next().unwrap(); let fn_ptr = tcx.global_alloc(fn_ptr.1.alloc_id()); let init_call_site = if let GlobalAlloc::Function { @@ -581,7 +582,13 @@ pub fn add_item<'tcx>( crate::call_info::CallInfo::sig_from_instance_(finstance, &mut ctx); let function_name = crate::utilis::function_name(tcx.symbol_name(finstance)); - CallSite::new(None, function_name, call_info.sig().clone(), true) + MethodRef::new( + *asm.main_module(), + asm.alloc_string(function_name), + asm.alloc_sig(call_info.sig().clone()), + MethodKind::Static, + vec![].into(), + ) } else { panic!() }; @@ -589,8 +596,9 @@ pub fn add_item<'tcx>( let argv = asm.alloc_string("argv"); let argc = asm.alloc_string("argc"); let main_module = asm.main_module(); + let mref = asm.alloc_methodref(init_call_site); asm.add_initialzer(CILRoot::Call { - site: Box::new(init_call_site), + site: mref, args: [ CILNode::LDStaticField(Box::new(StaticFieldDesc::new( *main_module, diff --git a/src/binop/bitop.rs b/src/binop/bitop.rs index 7902bf5a..ba73b2c2 100644 --- a/src/binop/bitop.rs +++ b/src/binop/bitop.rs @@ -1,10 +1,9 @@ use crate::assembly::MethodCompileCtx; use cilly::{ and, call, - call_site::CallSite, cil_node::CILNode, or, - v2::{ClassRef, FnSig, Int}, + v2::{cilnode::MethodKind, ClassRef, Int, MethodRef}, xor, Type, }; use rustc_middle::ty::{IntTy, Ty, TyKind, UintTy}; @@ -18,36 +17,44 @@ pub fn bit_and_unchecked<'tcx>( ) -> CILNode { let type_b = ctx.type_from_cache(ty_b); match ty_a.kind() { - TyKind::Uint(UintTy::U128) => call!( - CallSite::boxed( + TyKind::Uint(UintTy::U128) => { + let mref = MethodRef::new( ClassRef::uint_128(ctx).into(), - "op_BitwiseAnd".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Int(Int::U128) + ctx.alloc_string("op_BitwiseAnd"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::U128)], + Type::Int(Int::U128), ), - true, - ), - [ - operand_a, - crate::casts::int_to_int(type_b, Type::Int(Int::U128), operand_b, ctx) - ] - ), - TyKind::Int(IntTy::I128) => call!( - CallSite::boxed( + MethodKind::Static, + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), + [ + operand_a, + crate::casts::int_to_int(type_b, Type::Int(Int::U128), operand_b, ctx) + ] + ) + } + TyKind::Int(IntTy::I128) => { + let mref = MethodRef::new( ClassRef::int_128(ctx).into(), - "op_BitwiseAnd".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Int(Int::I128) + ctx.alloc_string("op_BitwiseAnd"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I128)], + Type::Int(Int::I128), ), - true, - ), - [ - operand_a, - crate::casts::int_to_int(type_b, Type::Int(Int::I128), operand_b, ctx) - ] - ), + MethodKind::Static, + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), + [ + operand_a, + crate::casts::int_to_int(type_b, Type::Int(Int::I128), operand_b, ctx) + ] + ) + } _ => and!(operand_a, operand_b), } } @@ -62,28 +69,26 @@ pub fn bit_or_unchecked<'tcx>( TyKind::Int(IntTy::I128) => { let ty_a = ctx.type_from_cache(ty_a); let ty_b = ctx.type_from_cache(ty_b); - call!( - CallSite::new_extern( - ClassRef::int_128(ctx), - "op_BitwiseOr".into(), - FnSig::new([ty_a, ty_b].into(), ty_a), - true, - ), - [operand_a, operand_b] - ) + let mref = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_BitwiseOr"), + ctx.sig([ty_a, ty_b], ty_a), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [operand_a, operand_b]) } TyKind::Uint(UintTy::U128) => { let ty_a = ctx.type_from_cache(ty_a); let ty_b = ctx.type_from_cache(ty_b); - call!( - CallSite::new_extern( - ClassRef::uint_128(ctx), - "op_BitwiseOr".into(), - FnSig::new([ty_a, ty_b].into(), ty_a), - true, - ), - [operand_a, operand_b] - ) + let mref = MethodRef::new( + ClassRef::uint_128(ctx), + ctx.alloc_string("op_BitwiseOr"), + ctx.sig([ty_a, ty_b], ty_a), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [operand_a, operand_b]) } _ => or!(operand_a, operand_b), } @@ -99,28 +104,26 @@ pub fn bit_xor_unchecked<'tcx>( TyKind::Int(IntTy::I128) => { let ty_a = ctx.type_from_cache(ty_a); let ty_b = ctx.type_from_cache(ty_b); - call!( - CallSite::new_extern( - ClassRef::int_128(ctx), - "op_ExclusiveOr".into(), - FnSig::new([ty_a, ty_b].into(), ty_a), - true, - ), - [ops_a, ops_b] - ) + let mref = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_ExclusiveOr"), + ctx.sig([ty_a, ty_b], ty_a), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) } TyKind::Uint(UintTy::U128) => { let ty_a = ctx.type_from_cache(ty_a); let ty_b = ctx.type_from_cache(ty_b); - call!( - CallSite::new_extern( - ClassRef::uint_128(ctx), - "op_ExclusiveOr".into(), - FnSig::new([ty_a, ty_b].into(), ty_a), - true, - ), - [ops_a, ops_b] - ) + let mref = MethodRef::new( + ClassRef::uint_128(ctx), + ctx.alloc_string("op_ExclusiveOr"), + ctx.sig([ty_a, ty_b], ty_a), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) } _ => xor!(ops_a, ops_b), } diff --git a/src/binop/checked/mod.rs b/src/binop/checked/mod.rs index 5a5b0aeb..cd1bd902 100644 --- a/src/binop/checked/mod.rs +++ b/src/binop/checked/mod.rs @@ -1,12 +1,11 @@ use crate::{assembly::MethodCompileCtx, casts}; use cilly::{ and, call, - call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, conv_i16, conv_i32, conv_i64, conv_i8, conv_isize, conv_u64, conv_usize, gt, gt_un, ldc_i32, ldc_i64, ldc_u32, ldc_u64, lt, mul, or, - v2::{Assembly, ClassRef, FieldDesc, FnSig, Int}, + v2::{cilnode::MethodKind, Assembly, ClassRef, FieldDesc, Int, MethodRef}, Type, }; use rustc_middle::ty::{IntTy, Ty, TyKind, UintTy}; @@ -46,24 +45,26 @@ pub fn zero(ty: Ty, asm: &mut Assembly) -> CILNode { TyKind::Int(IntTy::I64) => ldc_i64!(0), TyKind::Uint(UintTy::Usize) => conv_usize!(ldc_u32!(0)), TyKind::Int(IntTy::Isize) => conv_isize!(ldc_u32!(0)), - TyKind::Uint(UintTy::U128) => call!( - CallSite::new_extern( + TyKind::Uint(UintTy::U128) => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "op_Implicit".into(), - FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U128)), - true - ), - [ldc_u32!(0)] - ), - TyKind::Int(IntTy::I128) => call!( - CallSite::new_extern( + asm.alloc_string("op_Implicit"), + asm.sig([Type::Int(Int::U32)], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [ldc_u32!(0)]) + } + TyKind::Int(IntTy::I128) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_Implicit".into(), - FnSig::new([Type::Int(Int::I32)].into(), Type::Int(Int::I128)), - true - ), - [ldc_i32!(0)] - ), + asm.alloc_string("op_Implicit"), + asm.sig([Type::Int(Int::I32)], Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [ldc_i32!(0)]) + } _ => todo!("Can't get zero of {ty:?}"), } } @@ -77,42 +78,48 @@ fn min(ty: Ty, asm: &mut Assembly) -> CILNode { TyKind::Int(IntTy::I32) => ldc_i32!(i32::MIN), TyKind::Uint(UintTy::U64) => ldc_u64!(u64::MIN), TyKind::Int(IntTy::I64) => ldc_i64!(i64::MIN), - TyKind::Uint(UintTy::Usize) => call!( - CallSite::new_extern( + TyKind::Uint(UintTy::Usize) => { + let mref = MethodRef::new( ClassRef::usize_type(asm), - "get_MinValue".into(), - FnSig::new([].into(), Type::Int(Int::USize)), - true - ), - [] - ), - TyKind::Int(IntTy::Isize) => call!( - CallSite::new_extern( + asm.alloc_string("get_MinValue"), + asm.sig([], Type::Int(Int::USize)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), []); + cilnode + } + TyKind::Int(IntTy::Isize) => { + let mref = MethodRef::new( ClassRef::isize_type(asm), - "get_MinValue".into(), - FnSig::new([].into(), Type::Int(Int::ISize)), - true - ), - [] - ), - TyKind::Uint(UintTy::U128) => call!( - CallSite::new_extern( + asm.alloc_string("get_MinValue"), + asm.sig([], Type::Int(Int::ISize)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), []); + cilnode + } + TyKind::Uint(UintTy::U128) => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "get_MinValue".into(), - FnSig::new([].into(), Type::Int(Int::U128)), - true - ), - [] - ), - TyKind::Int(IntTy::I128) => call!( - CallSite::new_extern( + asm.alloc_string("get_MinValue"), + asm.sig([], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), []) + } + TyKind::Int(IntTy::I128) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "get_MinValue".into(), - FnSig::new([].into(), Type::Int(Int::I128)), - true - ), - [] - ), + asm.alloc_string("get_MinValue"), + asm.sig([], Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), []) + } _ => todo!("Can't get min of {ty:?}"), } } @@ -126,42 +133,46 @@ fn max(ty: Ty, asm: &mut Assembly) -> CILNode { TyKind::Int(IntTy::I32) => ldc_i32!(i32::MAX), TyKind::Uint(UintTy::U64) => ldc_u64!(u64::MAX), TyKind::Int(IntTy::I64) => ldc_i64!(i64::MAX), - TyKind::Uint(UintTy::Usize) => call!( - CallSite::new_extern( + TyKind::Uint(UintTy::Usize) => { + let mref = MethodRef::new( ClassRef::usize_type(asm), - "get_MaxValue".into(), - FnSig::new([].into(), Type::Int(Int::USize)), - true - ), - [] - ), - TyKind::Int(IntTy::Isize) => call!( - CallSite::new_extern( + asm.alloc_string("get_MaxValue"), + asm.sig([], Type::Int(Int::USize)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), []) + } + TyKind::Int(IntTy::Isize) => { + let mref = MethodRef::new( ClassRef::isize_type(asm), - "get_MaxValue".into(), - FnSig::new([].into(), Type::Int(Int::ISize)), - true - ), - [] - ), - TyKind::Uint(UintTy::U128) => call!( - CallSite::new_extern( + asm.alloc_string("get_MaxValue"), + asm.sig([], Type::Int(Int::ISize)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), []) + } + TyKind::Uint(UintTy::U128) => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "get_MaxValue".into(), - FnSig::new([].into(), Type::Int(Int::U128)), - true - ), - [] - ), - TyKind::Int(IntTy::I128) => call!( - CallSite::new_extern( + asm.alloc_string("get_MaxValue"), + asm.sig([], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), []) + } + TyKind::Int(IntTy::I128) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "get_MaxValue".into(), - FnSig::new([].into(), Type::Int(Int::I128)), - true - ), - [] - ), + asm.alloc_string("get_MaxValue"), + asm.sig([], Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), []) + } _ => todo!("Can't get max of {ty:?}"), } } @@ -198,16 +209,18 @@ pub fn mul<'tcx>( } // Use 128 bit ints, not supported in mono. TyKind::Uint(UintTy::U64) => { - let mul = call!( - CallSite::new_extern( - ClassRef::uint_128(ctx), - "op_Multiply".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Int(Int::U128) - ), - true + let op_mul = MethodRef::new( + ClassRef::uint_128(ctx), + ctx.alloc_string("op_Multiply"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::U128)], + Type::Int(Int::U128), ), + MethodKind::Static, + vec![].into(), + ); + let mul = call!( + ctx.alloc_methodref(op_mul), [ casts::int_to_int( Type::Int(Int::U64), @@ -223,16 +236,15 @@ pub fn mul<'tcx>( ) ] ); + let op_gt = MethodRef::new( + ClassRef::uint_128(ctx), + ctx.alloc_string("op_GreaterThan"), + ctx.sig([Type::Int(Int::U128), Type::Int(Int::U128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); call!( - CallSite::new_extern( - ClassRef::uint_128(ctx), - "op_GreaterThan".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Bool - ), - true - ), + ctx.alloc_methodref(op_gt), [ mul.clone(), casts::int_to_int(Type::Int(Int::U64), Type::Int(Int::U128), max(ty, ctx), ctx) @@ -240,16 +252,18 @@ pub fn mul<'tcx>( ) } TyKind::Int(IntTy::I64) => { - let mul = call!( - CallSite::new_extern( - ClassRef::int_128(ctx), - "op_Multiply".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Int(Int::I128) - ), - true + let op_mul = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_Multiply"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I128)], + Type::Int(Int::I128), ), + MethodKind::Static, + vec![].into(), + ); + let mul = call!( + ctx.alloc_methodref(op_mul), [ casts::int_to_int( Type::Int(Int::I64), @@ -265,31 +279,29 @@ pub fn mul<'tcx>( ) ] ); + let op_gt = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_GreaterThan"), + ctx.sig([Type::Int(Int::I128), Type::Int(Int::I128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); let gt = call!( - CallSite::new_extern( - ClassRef::int_128(ctx), - "op_GreaterThan".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Bool - ), - true - ), + ctx.alloc_methodref(op_gt), [ mul.clone(), casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), max(ty, ctx), ctx) ] ); + let op_lt = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_LessThan"), + ctx.sig([Type::Int(Int::I128), Type::Int(Int::I128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); let lt = call!( - CallSite::new_extern( - ClassRef::int_128(ctx), - "op_LessThan".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Bool - ), - true - ), + ctx.alloc_methodref(op_lt), [ mul.clone(), casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), min(ty, ctx), ctx) @@ -299,16 +311,18 @@ pub fn mul<'tcx>( } TyKind::Uint(UintTy::Usize) => { - let mul = call!( - CallSite::new_extern( - ClassRef::uint_128(ctx), - "op_Multiply".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Int(Int::U128) - ), - true + let op_mul = MethodRef::new( + ClassRef::uint_128(ctx), + ctx.alloc_string("op_Multiply"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::U128)], + Type::Int(Int::U128), ), + MethodKind::Static, + vec![].into(), + ); + let mul = call!( + ctx.alloc_methodref(op_mul), [ casts::int_to_int( Type::Int(Int::USize), @@ -324,17 +338,15 @@ pub fn mul<'tcx>( ) ] ); - + let op_gt = MethodRef::new( + ClassRef::uint_128(ctx), + ctx.alloc_string("op_GreaterThan"), + ctx.sig([Type::Int(Int::U128), Type::Int(Int::U128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); call!( - CallSite::new_extern( - ClassRef::uint_128(ctx), - "op_GreaterThan".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Bool - ), - true - ), + ctx.alloc_methodref(op_gt), [ mul.clone(), casts::int_to_int( @@ -347,16 +359,18 @@ pub fn mul<'tcx>( ) } TyKind::Int(IntTy::Isize) => { - let mul = call!( - CallSite::new_extern( - ClassRef::int_128(ctx), - "op_Multiply".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Int(Int::I128) - ), - true + let op_mul = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_Multiply"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I128)], + Type::Int(Int::I128), ), + MethodKind::Static, + vec![].into(), + ); + let mul = call!( + ctx.alloc_methodref(op_mul), [ casts::int_to_int( Type::Int(Int::ISize), @@ -372,16 +386,15 @@ pub fn mul<'tcx>( ) ] ); + let op_gt = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_GreaterThan"), + ctx.sig([Type::Int(Int::I128), Type::Int(Int::I128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); let gt = call!( - CallSite::new_extern( - ClassRef::int_128(ctx), - "op_GreaterThan".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Bool - ), - true - ), + ctx.alloc_methodref(op_gt), [ mul.clone(), casts::int_to_int( @@ -392,16 +405,15 @@ pub fn mul<'tcx>( ) ] ); + let op_lt = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_LessThan"), + ctx.sig([Type::Int(Int::I128), Type::Int(Int::I128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); let lt = call!( - CallSite::new_extern( - ClassRef::int_128(ctx), - "op_LessThan".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Bool - ), - true - ), + ctx.alloc_methodref(op_lt), [ mul.clone(), casts::int_to_int( diff --git a/src/binop/cmp.rs b/src/binop/cmp.rs index ef9e5674..b5007609 100644 --- a/src/binop/cmp.rs +++ b/src/binop/cmp.rs @@ -1,9 +1,8 @@ use cilly::{ call, - call_site::CallSite, cil_node::CILNode, eq, gt, gt_un, lt, lt_un, - v2::{Assembly, ClassRef, Float, FnSig, Int}, + v2::{Assembly, ClassRef, Float, FnSig, Int, MethodRef}, Type, }; use rustc_middle::ty::{FloatTy, IntTy, Ty, TyKind, UintTy}; @@ -30,7 +29,7 @@ pub fn eq_unchecked( match ty_a.kind() { TyKind::Uint(uint) => match uint { UintTy::U128 => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::uint_128(asm), "op_Equality".into(), FnSig::new( @@ -45,7 +44,7 @@ pub fn eq_unchecked( }, TyKind::Int(int) => match int { IntTy::I128 => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::int_128(asm), "op_Equality".into(), FnSig::new( @@ -65,7 +64,7 @@ pub fn eq_unchecked( eq!(operand_a, operand_b) } TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "__eqtf2".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), @@ -76,7 +75,7 @@ pub fn eq_unchecked( [operand_a, operand_b] ), TyKind::Float(FloatTy::F16) => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::half(asm), "op_Equality".into(), FnSig::new( @@ -100,7 +99,7 @@ pub fn lt_unchecked( match ty_a.kind() { TyKind::Uint(uint) => match uint { UintTy::U128 => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::uint_128(asm), "op_LessThan".into(), FnSig::new( @@ -115,7 +114,7 @@ pub fn lt_unchecked( }, TyKind::Int(int) => match int { IntTy::I128 => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::int_128(asm), "op_LessThan".into(), FnSig::new( @@ -134,7 +133,7 @@ pub fn lt_unchecked( } TyKind::RawPtr(_, _) | TyKind::FnPtr(_, _) => lt_un!(operand_a, operand_b), TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "__lttf2".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), @@ -156,7 +155,7 @@ pub fn gt_unchecked( match ty_a.kind() { TyKind::Uint(uint) => match uint { UintTy::U128 => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::uint_128(asm), "op_GreaterThan".into(), FnSig::new( @@ -171,7 +170,7 @@ pub fn gt_unchecked( }, TyKind::Int(int) => match int { IntTy::I128 => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::int_128(asm), "op_GreaterThan".into(), FnSig::new( @@ -189,7 +188,7 @@ pub fn gt_unchecked( gt!(operand_a, operand_b) } TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "__gttf2".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), diff --git a/src/binop/mod.rs b/src/binop/mod.rs index 0b7f1e63..361af507 100644 --- a/src/binop/mod.rs +++ b/src/binop/mod.rs @@ -3,7 +3,7 @@ use crate::assembly::MethodCompileCtx; use bitop::{bit_and_unchecked, bit_or_unchecked, bit_xor_unchecked}; use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_i8, conv_u16, conv_u32, conv_u64, conv_u8, div, eq, gt_un, ld_false, lt_un, rem, rem_un, @@ -73,7 +73,7 @@ pub(crate) fn binop<'tcx>( // Unordered, to handle NaNs propely TyKind::Float(FloatTy::F32 | FloatTy::F64) => eq!(lt_un!(ops_a, ops_b), ld_false!()), TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "__getf2".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), @@ -89,7 +89,7 @@ pub(crate) fn binop<'tcx>( // Unordered, to handle NaNs propely TyKind::Float(FloatTy::F32 | FloatTy::F64) => eq!(gt_un!(ops_a, ops_b), ld_false!()), TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "__letf2".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), @@ -164,7 +164,7 @@ pub fn add_unchecked<'tcx>( TyKind::Int(int_ty) => { if let IntTy::I128 = int_ty { call!( - CallSite::builtin( + MethodRefIdx::builtin( "add_i128".into(), FnSig::new( [Type::Int(Int::I128), Type::Int(Int::I128)].into(), @@ -181,7 +181,7 @@ pub fn add_unchecked<'tcx>( TyKind::Uint(uint_ty) => { if let UintTy::U128 = uint_ty { call!( - CallSite::builtin( + MethodRefIdx::builtin( "add_u128".into(), FnSig::new( [Type::Int(Int::U128), Type::Int(Int::U128)].into(), @@ -203,7 +203,7 @@ pub fn add_unchecked<'tcx>( } TyKind::Float(FloatTy::F32 | FloatTy::F64) => ops_a + ops_b, TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "__addtf3".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), @@ -214,7 +214,7 @@ pub fn add_unchecked<'tcx>( [ops_a, ops_b,] ), TyKind::Float(FloatTy::F16) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "add_f16".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), @@ -239,7 +239,7 @@ pub fn sub_unchecked<'tcx>( TyKind::Int(int_ty) => { if let IntTy::I128 = int_ty { call!( - CallSite::builtin( + MethodRefIdx::builtin( "sub_i128".into(), FnSig::new( [Type::Int(Int::I128), Type::Int(Int::I128)].into(), @@ -256,7 +256,7 @@ pub fn sub_unchecked<'tcx>( TyKind::Uint(uint_ty) => { if let UintTy::U128 = uint_ty { call!( - CallSite::builtin( + MethodRefIdx::builtin( "sub_u128".into(), FnSig::new( [Type::Int(Int::U128), Type::Int(Int::U128)].into(), @@ -272,7 +272,7 @@ pub fn sub_unchecked<'tcx>( } TyKind::Float(FloatTy::F32 | FloatTy::F64) => sub!(ops_a, ops_b), TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "__subtf3".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), @@ -296,7 +296,7 @@ fn rem_unchecked<'tcx>( match ty_a.kind() { TyKind::Int(IntTy::I128) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "mod_i128".into(), FnSig::new( [Type::Int(Int::I128), Type::Int(Int::I128)].into(), @@ -309,7 +309,7 @@ fn rem_unchecked<'tcx>( } TyKind::Uint(UintTy::U128) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "mod_u128".into(), FnSig::new( [Type::Int(Int::U128), Type::Int(Int::U128)].into(), @@ -324,7 +324,7 @@ fn rem_unchecked<'tcx>( rem!(ops_a, ops_b) } TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "fmodl".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), @@ -350,7 +350,7 @@ fn mul_unchecked<'tcx>( match ty_a.kind() { TyKind::Int(IntTy::I128) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "mul_i128".into(), FnSig::new( [Type::Int(Int::I128), Type::Int(Int::I128)].into(), @@ -363,7 +363,7 @@ fn mul_unchecked<'tcx>( } TyKind::Uint(UintTy::U128) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "mul_u128".into(), FnSig::new( [Type::Int(Int::U128), Type::Int(Int::U128)].into(), @@ -375,7 +375,7 @@ fn mul_unchecked<'tcx>( ) } TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "__multf3".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), @@ -398,7 +398,7 @@ fn div_unchecked<'tcx>( match ty_a.kind() { TyKind::Int(IntTy::I128) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "div_i128".into(), FnSig::new( [Type::Int(Int::I128), Type::Int(Int::I128)].into(), @@ -411,7 +411,7 @@ fn div_unchecked<'tcx>( } TyKind::Uint(UintTy::U128) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "div_u128".into(), FnSig::new( [Type::Int(Int::U128), Type::Int(Int::U128)].into(), @@ -427,7 +427,7 @@ fn div_unchecked<'tcx>( div!(operand_a, operand_b) } TyKind::Float(FloatTy::F128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "__divtf3".into(), FnSig::new( [Type::Float(Float::F128), Type::Float(Float::F128)].into(), diff --git a/src/binop/shift.rs b/src/binop/shift.rs index 975ad0a5..0ca62a2a 100644 --- a/src/binop/shift.rs +++ b/src/binop/shift.rs @@ -3,7 +3,7 @@ use crate::utilis::compiletime_sizeof; use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, conv_i32, conv_u32, ldc_u32, rem_un, shl, shr, shr_un, v2::{ClassRef, FnSig, Int}, @@ -22,7 +22,7 @@ pub fn shr_unchecked<'tcx>( match value_type.kind() { TyKind::Uint(UintTy::U128) => { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::uint_128(ctx).into(), "op_RightShift".into(), FnSig::new( @@ -39,7 +39,7 @@ pub fn shr_unchecked<'tcx>( } TyKind::Int(IntTy::I128) => { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::int_128(ctx).into(), "op_RightShift".into(), FnSig::new( @@ -90,7 +90,7 @@ pub fn shr_checked<'tcx>( match value_type.kind() { TyKind::Uint(UintTy::U128) => { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::uint_128(ctx).into(), "op_RightShift".into(), FnSig::new( @@ -110,7 +110,7 @@ pub fn shr_checked<'tcx>( } TyKind::Int(IntTy::I128) => { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::int_128(ctx).into(), "op_RightShift".into(), FnSig::new( @@ -183,7 +183,7 @@ pub fn shl_checked<'tcx>( match value_type.kind() { TyKind::Uint(UintTy::U128) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "shl_u128".into(), FnSig::new( [Type::Int(Int::U128), Type::Int(Int::I32)].into(), @@ -207,7 +207,7 @@ pub fn shl_checked<'tcx>( } TyKind::Int(IntTy::I128) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "shl_i128".into(), FnSig::new( [Type::Int(Int::I128), Type::Int(Int::I32)].into(), @@ -283,7 +283,7 @@ pub fn shl_unchecked<'tcx>( match value_type.kind() { TyKind::Uint(UintTy::U128) => { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::uint_128(ctx).into(), "op_LeftShift".into(), FnSig::new( @@ -300,7 +300,7 @@ pub fn shl_unchecked<'tcx>( } TyKind::Int(IntTy::I128) => { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::int_128(ctx).into(), "op_LeftShift".into(), FnSig::new( diff --git a/src/builtin/atomic.rs b/src/builtin/atomic.rs index f86adece..c7f93746 100644 --- a/src/builtin/atomic.rs +++ b/src/builtin/atomic.rs @@ -3,7 +3,7 @@ use cilly::{ asm::Assembly, basic_block::BasicBlock, call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_u32, conv_u64, conv_usize, size_of, @@ -14,7 +14,7 @@ use cilly::{ macro_rules! monitor_enter { () => {{ CILRoot::Call { - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(ClassRef::monitor()), "Enter".into(), FnSig::new( @@ -38,7 +38,7 @@ macro_rules! monitor_enter { macro_rules! monitor_exit { () => {{ CILRoot::Call { - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(ClassRef::monitor()), "Exit".into(), FnSig::new( @@ -79,7 +79,7 @@ crate::add_method_from_trees!( .into(), CILRoot::Ret { tree: conv_usize!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked()), "Add".into(), FnSig::new( @@ -106,7 +106,7 @@ crate::add_method_from_trees!( BasicBlock::new( vec![CILRoot::Ret { tree: conv_usize!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked()), "Add".into(), FnSig::new( @@ -150,7 +150,7 @@ crate::add_method_from_trees!( .into(), CILRoot::Ret { tree: conv_usize!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked()), "Or".into(), FnSig::new( @@ -177,7 +177,7 @@ crate::add_method_from_trees!( BasicBlock::new( vec![CILRoot::Ret { tree: conv_usize!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked()), "Or".into(), FnSig::new( @@ -221,7 +221,7 @@ crate::add_method_from_trees!( .into(), CILRoot::Ret { tree: conv_usize!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked()), "And".into(), FnSig::new( @@ -248,7 +248,7 @@ crate::add_method_from_trees!( BasicBlock::new( vec![CILRoot::Ret { tree: conv_usize!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked()), "And".into(), FnSig::new( diff --git a/src/builtin/casts.rs b/src/builtin/casts.rs index 898b7647..2e8c38a8 100644 --- a/src/builtin/casts.rs +++ b/src/builtin/casts.rs @@ -6,7 +6,7 @@ use cilly::{ asm::Assembly, basic_block::BasicBlock, call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_f32, conv_f64, conv_f_un, conv_i16, conv_i32, conv_i64, conv_i8, conv_isize, conv_u16, @@ -67,7 +67,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -148,7 +148,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -229,7 +229,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -307,7 +307,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -385,7 +385,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -413,7 +413,7 @@ add_method_from_trees!( cond: lt!( CILNode::LDArg(0), conv_f32!(conv_f_un!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::usize_type()), "get_MaxValue".into(), FnSig::new(&[], Type::Int(Int::USize)), @@ -426,7 +426,7 @@ add_method_from_trees!( .into(), CILRoot::Ret { tree: call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::usize_type()), "get_MaxValue".into(), FnSig::new(&[], Type::Int(Int::USize)), @@ -479,7 +479,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), @@ -507,7 +507,7 @@ add_method_from_trees!( cond: lt!( CILNode::LDArg(0), conv_f64!(conv_f_un!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::usize_type()), "get_MaxValue".into(), FnSig::new(&[], Type::Int(Int::USize)), @@ -520,7 +520,7 @@ add_method_from_trees!( .into(), CILRoot::Ret { tree: call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::usize_type()), "get_MaxValue".into(), FnSig::new(&[], Type::Int(Int::USize)), @@ -573,7 +573,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), @@ -601,7 +601,7 @@ add_method_from_trees!( cond: lt!( CILNode::LDArg(0), conv_f64!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::isize_type()), "get_MaxValue".into(), FnSig::new(&[], Type::Int(Int::ISize)), @@ -614,7 +614,7 @@ add_method_from_trees!( .into(), CILRoot::Ret { tree: call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::isize_type()), "get_MaxValue".into(), FnSig::new(&[], Type::Int(Int::ISize)), @@ -636,7 +636,7 @@ add_method_from_trees!( cond: gt!( CILNode::LDArg(0), conv_f64!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::isize_type()), "get_MinValue".into(), FnSig::new(&[], Type::Int(Int::ISize)), @@ -649,7 +649,7 @@ add_method_from_trees!( .into(), CILRoot::Ret { tree: call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::isize_type()), "get_MinValue".into(), FnSig::new(&[], Type::Int(Int::ISize)), @@ -686,7 +686,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -714,7 +714,7 @@ add_method_from_trees!( cond: lt!( CILNode::LDArg(0), conv_f32!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::isize_type()), "get_MaxValue".into(), FnSig::new(&[], Type::Int(Int::ISize)), @@ -727,7 +727,7 @@ add_method_from_trees!( .into(), CILRoot::Ret { tree: call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::isize_type()), "get_MaxValue".into(), FnSig::new(&[], Type::Int(Int::ISize)), @@ -749,7 +749,7 @@ add_method_from_trees!( cond: gt!( CILNode::LDArg(0), conv_f32!(call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::isize_type()), "get_MinValue".into(), FnSig::new(&[], Type::Int(Int::ISize)), @@ -762,7 +762,7 @@ add_method_from_trees!( .into(), CILRoot::Ret { tree: call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::isize_type()), "get_MinValue".into(), FnSig::new(&[], Type::Int(Int::ISize)), @@ -799,7 +799,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -880,7 +880,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -961,7 +961,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -1039,7 +1039,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F32)], Type::Bool), @@ -1117,7 +1117,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), @@ -1198,7 +1198,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), @@ -1279,7 +1279,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), @@ -1357,7 +1357,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), @@ -1435,7 +1435,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), @@ -1516,7 +1516,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), @@ -1597,7 +1597,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), @@ -1675,7 +1675,7 @@ add_method_from_trees!( sub_target: 0, cond: eq!( call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(), "IsNaN".into(), FnSig::new(&[Type::Float(Float::F64)], Type::Bool), diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index 42f9e8f1..b00cb8e9 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -4,7 +4,7 @@ use cilly::{ asm::Assembly, basic_block::{BasicBlock, Handler}, call, - call_site::CallSite, + call_site::MethodRefIdx, call_virt, cil_node::{CILNode, CallOpArgs}, cil_root::CILRoot, @@ -85,7 +85,7 @@ add_method_from_trees!( } .into(), CILRoot::Call { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::console(), "Write".into(), FnSig::new(&[ClassRef::string_type().into()], Type::Void), @@ -95,7 +95,7 @@ add_method_from_trees!( } .into(), CILRoot::Call { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::console(), "Write".into(), FnSig::new(&[Type::Int(Int::U64)], Type::Void), @@ -105,7 +105,7 @@ add_method_from_trees!( } .into(), CILRoot::Call { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::console(), "Write".into(), FnSig::new(&[ClassRef::string_type().into()], Type::Void), @@ -115,7 +115,7 @@ add_method_from_trees!( } .into(), CILRoot::Call { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::console(), "WriteLine".into(), FnSig::new(&[Type::Int(Int::U64)], Type::Void), @@ -125,7 +125,7 @@ add_method_from_trees!( } .into(), CILRoot::Throw(CILNode::NewObj(Box::new(CallOpArgs { - site: CallSite::boxed( + site: MethodRefIdx::boxed( Some( ClassRef::new( Some("System.Runtime"), @@ -256,7 +256,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { CILRoot::STLoc { local: 0, tree: call!( - CallSite::aligned_alloc(), + MethodRefIdx::aligned_alloc(), [CILNode::LDArg(0), CILNode::LDArg(1)] ) .cast_ptr(ptr!(Type::Int(Int::U8))), @@ -335,7 +335,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { CILRoot::STLoc { local: 0, tree: call!( - CallSite::aligned_alloc(), + MethodRefIdx::aligned_alloc(), [CILNode::LDArg(0), CILNode::LDArg(1)] ), } @@ -367,7 +367,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { vec![BasicBlock::new( vec![CILRoot::Ret { tree: call!( - CallSite::aligned_alloc(), + MethodRefIdx::aligned_alloc(), [CILNode::LDArg(0), CILNode::LDArg(1)] ) .cast_ptr(ptr!(Type::Int(Int::U8))), @@ -397,7 +397,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { vec![BasicBlock::new( vec![ CILRoot::Call { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( native_mem, "AlignedFree".into(), FnSig::new(&[Type::Ptr(Type::Void.into())], Type::Void), @@ -428,7 +428,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { vec![BasicBlock::new( vec![ CILRoot::Call { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( marshal, "FreeHGlobal".into(), FnSig::new(&[Type::Int(Int::ISize)], Type::Void), @@ -473,7 +473,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { BasicBlock::new( vec![CILRoot::Ret { tree: call!( - CallSite::realloc(), + MethodRefIdx::realloc(), [ CILNode::LDArg(0).cast_ptr(ptr!(Type::Void)), CILNode::LDArg(3), @@ -499,7 +499,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { vec![BasicBlock::new( vec![CILRoot::Ret { tree: call!( - CallSite::realloc(), + MethodRefIdx::realloc(), [ CILNode::LDArg(0).cast_ptr(ptr!(Type::Void)), CILNode::LDArg(3), @@ -552,7 +552,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { "thread_results".into(), )), value: CILNode::NewObj(Box::new(CallOpArgs { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::dictionary(Type::Int(Int::I32), Type::Int(Int::ISize)), ".ctor".into(), FnSig::new( @@ -631,7 +631,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { vec![BasicBlock::new( vec![ CILRoot::Call { - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( None, ".tcctor".into(), FnSig::new(&[], Type::Void), @@ -671,7 +671,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { .into(), source_info!(), CILRoot::Call { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::dictionary(Type::Int(Int::I32), Type::Int(Int::ISize)), "set_Item".into(), FnSig::new( @@ -697,7 +697,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { "thread_results".into(), ))), CILNode::CallVirt(Box::new(CallOpArgs { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::thread(), "get_ManagedThreadId".into(), FnSig::new( @@ -707,7 +707,7 @@ pub fn insert_ffi_functions(asm: &mut Assembly, tcx: TyCtxt) { false, )), args: [call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::thread(), "get_CurrentThread".into(), FnSig::new([], Type::ClassRef(Box::new(ClassRef::thread())),), @@ -833,7 +833,7 @@ add_method_from_trees!( vec![BasicBlock::new( vec![CILRoot::Throw(CILNode::NewObj(Box::new(CallOpArgs { args: Box::new([conv_usize!(CILNode::LDArg(0))]), - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(ClassRef::new::<&str, _>(None, "RustException").with_valuetype(false),), ".ctor".into(), FnSig::new( @@ -872,7 +872,7 @@ add_method_from_trees!( args: [CILNode::NewObj(Box::new(CallOpArgs { args: [ CILNode::NewObj(Box::new(CallOpArgs { - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(unmanaged_start()), ".ctor".into(), FnSig::new( @@ -890,7 +890,7 @@ add_method_from_trees!( )), args: [CILNode::LDArg(2), CILNode::LDArg(3),].into() })), - CILNode::LDFtn(Box::new(CallSite::new( + CILNode::LDFtn(Box::new(MethodRefIdx::new( Some(unmanaged_start()), "Start".into(), FnSig::new( @@ -901,7 +901,7 @@ add_method_from_trees!( ))) ] .into(), - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(ClassRef::thread_start()), ".ctor".into(), FnSig::new( @@ -916,7 +916,7 @@ add_method_from_trees!( )), }))] .into(), - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(ClassRef::thread()), ".ctor".into(), FnSig::new( @@ -932,7 +932,7 @@ add_method_from_trees!( } .into(), CILRoot::CallVirt { - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(ClassRef::thread()), "Start".into(), FnSig::new(&[Type::ClassRef(Box::new(ClassRef::thread()))], Type::Void), @@ -1020,7 +1020,7 @@ add_method_from_trees!( CILRoot::STLoc { local: 0, tree: call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::gc_handle(), "FromIntPtr".into(), FnSig::new( @@ -1034,7 +1034,7 @@ add_method_from_trees!( } .into(), CILRoot::Call { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::gc_handle(), "Free".into(), FnSig::new( @@ -1190,7 +1190,7 @@ add_method_from_trees!( BasicBlock::new( vec![ CILRoot::Call { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::console(), "WriteLine".into(), FnSig::new( @@ -1211,7 +1211,7 @@ add_method_from_trees!( args: Box::new([ CILNode::LDArg(1), call!( - CallSite::builtin( + MethodRefIdx::builtin( "exception_to_native".into(), FnSig::new( vec![Type::ClassRef(Box::new( @@ -1272,7 +1272,7 @@ add_method_from_trees!( .into(), source_info!(), CILRoot::CallVirt { - site: Box::new(CallSite::new_extern( + site: Box::new(MethodRefIdx::new_extern( ClassRef::thread(), "Join".into(), FnSig::new([Type::ClassRef(Box::new(ClassRef::thread()))], Type::Void), @@ -1292,7 +1292,7 @@ add_method_from_trees!( CILRoot::STIndPtr( CILNode::LDArg(1), call_virt!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::dictionary(Type::Int(Int::I32), Type::Int(Int::ISize)), "get_Item".into(), FnSig::new( @@ -1317,7 +1317,7 @@ add_method_from_trees!( "thread_results".into() ))), call_virt!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::thread(), "get_ManagedThreadId".into(), FnSig::new( @@ -1351,7 +1351,7 @@ add_method_from_trees!( fn shr_u128(value: CILNode, shift: CILNode) -> CILNode { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::uint_128(asm).into(), "op_RightShift".into(), FnSig::new( @@ -1365,7 +1365,7 @@ fn shr_u128(value: CILNode, shift: CILNode) -> CILNode { } fn or_u128(lhs: CILNode, rhs: CILNode) -> CILNode { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::uint_128(asm).into(), "op_BitwiseOr".into(), FnSig::new( @@ -1379,7 +1379,7 @@ fn or_u128(lhs: CILNode, rhs: CILNode) -> CILNode { } fn and_u128(lhs: CILNode, rhs: CILNode) -> CILNode { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::uint_128(asm).into(), "op_BitwiseAnd".into(), FnSig::new( @@ -1393,7 +1393,7 @@ fn and_u128(lhs: CILNode, rhs: CILNode) -> CILNode { } fn shl_u128(value: CILNode, shift: CILNode) -> CILNode { call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::uint_128(asm).into(), "op_LeftShift".into(), FnSig::new( diff --git a/src/casts.rs b/src/casts.rs index 7ca7feb2..57170ced 100644 --- a/src/casts.rs +++ b/src/casts.rs @@ -1,6 +1,7 @@ use cilly::cil_node::CILNode; -use cilly::v2::{Assembly, ClassRef, Float, FnSig, Int}; -use cilly::{call_site::CallSite, Type}; +use cilly::v2::cilnode::MethodKind; +use cilly::v2::{Assembly, ClassRef, Float, Int, MethodRef}; +use cilly::Type; use cilly::{ call, conv_f32, conv_f64, conv_f_un, conv_i16, conv_i32, conv_i64, conv_i8, conv_isize, @@ -32,51 +33,56 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) conv_i8!(conv_u8!(operand)) } // - (Type::Int(Int::ISize), Type::Int(Int::I128)) => call!( - CallSite::new_extern( + (Type::Int(Int::ISize), Type::Int(Int::I128)) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_Implicit".into(), - FnSig::new(Box::new([Type::Int(Int::ISize)]), Type::Int(Int::I128)), - true, - ), - [operand] - ), - (Type::Int(Int::U32), Type::Int(Int::I128)) => call!( - CallSite::new_extern( + asm.alloc_string("op_Implicit"), + asm.sig(([Type::Int(Int::ISize)]), Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + (Type::Int(Int::U32), Type::Int(Int::I128)) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_Implicit".into(), - FnSig::new(Box::new([Type::Int(Int::U32)]), Type::Int(Int::I128)), - true, - ), - [operand] - ), - (Type::Int(Int::ISize), Type::Int(Int::U128)) => call!( - CallSite::new_extern( + asm.alloc_string("op_Implicit"), + asm.sig(([Type::Int(Int::U32)]), Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + (Type::Int(Int::ISize), Type::Int(Int::U128)) => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "op_Explicit".into(), - FnSig::new(Box::new([Type::Int(Int::I64)]), Type::Int(Int::U128)), - true, - ), - [conv_i64!(operand)] - ), - (Type::Bool, Type::Int(Int::U128)) => call!( - CallSite::new_extern( + asm.alloc_string("op_Explicit"), + asm.sig([Type::Int(Int::I64)], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [conv_i64!(operand)]) + } + (Type::Bool, Type::Int(Int::U128)) => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "op_Explicit".into(), - FnSig::new(Box::new([Type::Int(Int::I32)]), Type::Int(Int::U128)), - true, - ), - [conv_i32!(operand)] - ), - (Type::Bool, Type::Int(Int::I128)) => call!( - CallSite::new_extern( + asm.alloc_string("op_Explicit"), + asm.sig(([Type::Int(Int::I32)]), Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [conv_i32!(operand)]) + } + (Type::Bool, Type::Int(Int::I128)) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_Implicit".into(), - FnSig::new(Box::new([Type::Int(Int::I32)]), Type::Int(Int::I128)), - true, - ), - [conv_i32!(operand)] - ), + asm.alloc_string("op_Implicit"), + asm.sig(([Type::Int(Int::I32)]), Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [conv_i32!(operand)]) + } // Fixes sign casts (Type::Int(Int::I64 | Int::I32 | Int::I16 | Int::I8), Type::Int(Int::USize)) => { CILNode::SignExtendToUSize(operand.into()) @@ -87,61 +93,65 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) // i128 bit casts (Type::Int(Int::U128), Type::Int(Int::I128)) | (Type::Int(Int::I8 | Int::I16 | Int::I32 | Int::I64), Type::Int(Int::U128)) => { - call!( - CallSite::new_extern( - ClassRef::uint_128(asm), - "op_Explicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [operand] - ) + let mref = MethodRef::new( + ClassRef::uint_128(asm), + asm.alloc_string("op_Explicit"), + asm.sig(([src]), target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) } - (_, Type::Int(Int::I128)) => call!( - CallSite::new_extern( + (_, Type::Int(Int::I128)) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_Implicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [operand] - ), - (Type::Int(Int::I128), Type::Int(Int::U128)) => call!( - CallSite::new_extern( + asm.alloc_string("op_Implicit"), + asm.sig(([src]), target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + (Type::Int(Int::I128), Type::Int(Int::U128)) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_Explicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [operand] - ), - (_, Type::Int(Int::U128)) => call!( - CallSite::new_extern( + asm.alloc_string("op_Explicit"), + asm.sig(([src]), target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + (_, Type::Int(Int::U128)) => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "op_Implicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [operand] - ), - (Type::Int(Int::I128), _) => call!( - CallSite::new_extern( + asm.alloc_string("op_Implicit"), + asm.sig(([src]), target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + (Type::Int(Int::I128), _) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_Explicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [operand] - ), - (Type::Int(Int::U128), _) => call!( - CallSite::new_extern( + asm.alloc_string("op_Explicit"), + asm.sig(([src]), target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + (Type::Int(Int::U128), _) => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "op_Explicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [operand] - ), + asm.alloc_string("op_Explicit"), + asm.sig(([src]), target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } //todo!("Casting to 128 bit intiegers is not supported!"), _ => to_int(target, operand), } @@ -149,215 +159,257 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) /// Returns CIL ops required to convert type src to target pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) -> CILNode { match target { - Type::Int(Int::I128) => call!( - CallSite::new_extern( + Type::Int(Int::I128) => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_Explicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [operand] - ), - Type::Int(Int::U128) => call!( - CallSite::new_extern( + asm.alloc_string("op_Explicit"), + asm.sig(([src]), target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Int(Int::U128) => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "op_Explicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [operand] - ), + asm.alloc_string("op_Explicit"), + asm.sig(([src]), target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } Type::Int(Int::U8) => match src { - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_u8".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::U8)), - true - ), - [operand] - ), - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_u8".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::U8)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_u8"), + asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::U8)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_u8"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::U8)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } _ => panic!("Non-float type!"), }, Type::Int(Int::U16) => match src { - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_u16".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::U16)), - true - ), - [operand] - ), - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_u16".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::U16)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_u16"), + asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::U16)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_u16"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::U16)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } _ => panic!("Non-float type!"), }, Type::Int(Int::U32) => match src { - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_u32".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::U32)), - true - ), - [operand] - ), - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_u32".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::U32)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_u32"), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::U32)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_u32"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::U32)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } _ => panic!("Non-float type!"), }, Type::Int(Int::U64) => match src { - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_u64".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::U64)), - true - ), - [operand] - ), - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_u64".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::U64)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_u64"), + asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::U64)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_u64"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::U64)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } _ => panic!("Non-float type!"), }, Type::Int(Int::USize) => match src { //TODO: Check why this caused - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_usize".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::USize)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_usize"), + asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::USize)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_usize".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::USize)), - true - ), - [operand] - ), + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_usize"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::USize)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } //_=> to_int(target, operand), _ => panic!("Non-float type!"), }, Type::Int(Int::ISize) => match src { - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_isize".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::ISize)), - true - ), - [operand] - ), - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_isize".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::ISize)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_isize"), + asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::ISize)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_isize"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::ISize)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } _ => panic!("Non-float type!"), }, Type::Int(Int::I8) => match src { - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_i8".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::I8)), - true - ), - [operand] - ), - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_i8".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::I8)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_i8"), + asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::I8)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_i8"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::I8)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } _ => panic!("Non-float type!"), }, Type::Int(Int::I16) => match src { - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_i16".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::I16)), - true - ), - [operand] - ), - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_i16".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::I16)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_i16"), + asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::I16)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_i16"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::I16)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } _ => panic!("Non-float type!"), }, Type::Int(Int::I32) => match src { - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_i32".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::I32)), - true - ), - [operand] - ), - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_i32".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::I32)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_i32"), + asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_i32"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } _ => panic!("Non-float type!"), }, Type::Int(Int::I64) => match src { - Type::Float(Float::F32) => call!( - CallSite::builtin( - "cast_f32_i64".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Int(Int::I64)), - true - ), - [operand] - ), - Type::Float(Float::F64) => call!( - CallSite::builtin( - "cast_f64_i64".into(), - FnSig::new(Box::new([Type::Float(Float::F64)]), Type::Int(Int::I64)), - true - ), - [operand] - ), + Type::Float(Float::F32) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f32_i64"), + asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::I64)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } + Type::Float(Float::F64) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("cast_f64_i64"), + asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::I64)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand]) + } _ => panic!("Non-float type!"), }, _ => to_int(target, operand), @@ -386,26 +438,24 @@ fn to_int(target: Type, operand: CILNode) -> CILNode { /// Returns CIL ops required to casts from intiger type `src` to `target` MOVE TO CILLY pub fn int_to_float(src: Type, target: Type, parrent: CILNode, asm: &mut Assembly) -> CILNode { if matches!(src, Type::Int(Int::I128)) { - call!( - CallSite::boxed( - ClassRef::int_128(asm).into(), - "op_Explicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [parrent] - ) + let mref = MethodRef::new( + ClassRef::int_128(asm).into(), + asm.alloc_string("op_Explicit"), + asm.sig([src], target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [parrent]) //todo!("Casting from 128 bit intiegers is not supported!") } else if matches!(src, Type::Int(Int::U128)) { - call!( - CallSite::boxed( - ClassRef::uint_128(asm).into(), - "op_Explicit".into(), - FnSig::new(Box::new([src]), target), - true, - ), - [parrent] - ) + let mref = MethodRef::new( + ClassRef::uint_128(asm).into(), + asm.alloc_string("op_Explicit"), + asm.sig([src], target), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [parrent]) } else if matches!(target, Type::Int(Int::I128 | Int::U128)) { todo!("Casting to 128 bit intiegers is not supported!") } else { diff --git a/src/comptime.rs b/src/comptime.rs index 91b78449..939b40e6 100644 --- a/src/comptime.rs +++ b/src/comptime.rs @@ -200,7 +200,7 @@ pub fn interpret<'tcx>( let call_info = CallInfo::sig_from_instance_(def_instance, ctx); let target_function_name = crate::utilis::function_name(ctx.tcx().symbol_name(def_instance)); - let call_site = CallSite::new( + let call_site = MethodRefIdx::new( None, target_function_name, call_info.sig().clone(), diff --git a/src/constant.rs b/src/constant.rs index 29422ef9..3026908a 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -4,13 +4,13 @@ use crate::{assembly::MethodCompileCtx, r#type::get_type}; use cilly::{ call, - call_site::CallSite, cil_node::{CILNode, CallOpArgs}, cil_root::CILRoot, conv_u64, conv_usize, ldc_u64, v2::{ + cilnode::MethodKind, hashable::{HashableF32, HashableF64}, - Assembly, ClassRef, FieldDesc, Float, FnSig, Int, StaticFieldDesc, + Assembly, ClassRef, FieldDesc, Float, FnSig, Int, MethodRef, MethodRefIdx, StaticFieldDesc, }, Type, }; @@ -116,6 +116,7 @@ fn load_scalar_ptr( let (alloc_id, offset) = ptr.into_parts(); let global_alloc = ctx.tcx().global_alloc(alloc_id.alloc_id()); let u8_ptr = ctx.nptr(Type::Int(Int::U8)); + let u8_ptr_ptr = ctx.nptr(u8_ptr); match global_alloc { GlobalAlloc::Static(def_id) => { assert!(ctx.tcx().is_static(def_id)); @@ -146,17 +147,19 @@ fn load_scalar_ptr( ))); } if name == "environ" { + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("get_environ"), + ctx.sig([], u8_ptr_ptr), + MethodKind::Static, + vec![].into(), + ); return CILNode::TemporaryLocal(Box::new(( - ctx.nptr(u8_ptr), + u8_ptr_ptr, [CILRoot::SetTMPLocal { value: CILNode::Call(Box::new(CallOpArgs { args: Box::new([]), - site: Box::new(CallSite::new( - None, - "get_environ".into(), - FnSig::new(Box::new([]), ctx.nptr(u8_ptr)), - true, - )), + site: ctx.alloc_methodref(mref), })), }] .into(), @@ -169,9 +172,9 @@ fn load_scalar_ptr( // TODO: this could cause issues if the pointer to the static is not imediatly dereferenced. let site = get_fn_from_static_name(&name, ctx); return CILNode::TemporaryLocal(Box::new(( - Type::FnPtr(ctx.alloc_sig(site.signature().clone())), + Type::FnPtr(ctx[site].sig()), [CILRoot::SetTMPLocal { - value: CILNode::LDFtn(Box::new(site)), + value: CILNode::LDFtn(site), }] .into(), CILNode::LoadAddresOfTMPLocal, @@ -211,7 +214,14 @@ fn load_scalar_ptr( // If it is a function, patch its pointer up. let call_info = crate::call_info::CallInfo::sig_from_instance_(finstance, ctx); let function_name = crate::utilis::function_name(ctx.tcx().symbol_name(finstance)); - CILNode::LDFtn(CallSite::new(None, function_name, call_info.sig().clone(), true).into()) + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string(function_name), + ctx.alloc_sig(call_info.sig().clone()), + MethodKind::Static, + vec![].into(), + ); + CILNode::LDFtn(ctx.alloc_methodref(mref)) } GlobalAlloc::VTable(..) => todo!("Unhandled global alloc {global_alloc:?}"), } @@ -296,13 +306,15 @@ fn load_const_float(value: u128, float_type: FloatTy, asm: &mut Assembly) -> CIL FloatTy::F16 => { #[cfg(not(target_family = "windows"))] { + let mref = MethodRef::new( + ClassRef::half(asm), + asm.alloc_string("op_Explicit"), + asm.sig([Type::Float(Float::F32)], Type::Float(Float::F16)), + MethodKind::Static, + vec![].into(), + ); call!( - CallSite::new_extern( - ClassRef::half(asm), - "op_Explicit".into(), - FnSig::new(Box::new([Type::Float(Float::F32)]), Type::Float(Float::F16)), - true - ), + asm.alloc_methodref(mref), [CILNode::LdcF32(HashableF32( (f16::from_ne_bytes((u16::try_from(value).unwrap()).to_ne_bytes())) as f32 ),)] @@ -326,24 +338,23 @@ fn load_const_float(value: u128, float_type: FloatTy, asm: &mut Assembly) -> CIL // Int128 is used to emulate f128 let low = u128_low_u64(value); let high = (value >> 64) as u64; - let ctor_sig = FnSig::new( - Box::new([ - asm.nref(Type::Float(Float::F128)), - Type::Int(Int::U64), - Type::Int(Int::U64), - ]), + let f128_ref = asm.nref(Type::Float(Float::F128)); + let ctor_sig = asm.sig( + ([f128_ref, Type::Int(Int::U64), Type::Int(Int::U64)]), Type::Void, ); + let cctor = MethodRef::new( + (ClassRef::int_128(asm)), + asm.alloc_string(".ctor"), + ctor_sig, + MethodKind::Constructor, + vec![].into(), + ); CILNode::TemporaryLocal(Box::new(( Type::Int(Int::I128), Box::new([CILRoot::SetTMPLocal { value: CILNode::NewObj(Box::new(CallOpArgs { - site: CallSite::boxed( - Some(ClassRef::int_128(asm)), - ".ctor".into(), - ctor_sig, - false, - ), + site: asm.alloc_methodref(cctor), args: [conv_u64!(ldc_u64!(high)), conv_u64!(ldc_u64!(low))].into(), })), }]), @@ -385,21 +396,20 @@ pub fn load_const_int(value: u128, int_type: IntTy, asm: &mut Assembly) -> CILNo IntTy::I128 => { let low = u128_low_u64(value); let high = (value >> 64) as u64; - let ctor_sig = FnSig::new( - Box::new([ - asm.nref(Type::Int(Int::I128)), - Type::Int(Int::U64), - Type::Int(Int::U64), - ]), + let int128_ref = asm.nref(Type::Int(Int::I128)); + let ctor_sig = asm.sig( + ([int128_ref, Type::Int(Int::U64), Type::Int(Int::U64)]), Type::Void, ); + let mref = MethodRef::new( + ClassRef::int_128(asm), + asm.alloc_string(".ctor"), + ctor_sig, + MethodKind::Constructor, + vec![].into(), + ); CILNode::NewObj(Box::new(CallOpArgs { - site: CallSite::boxed( - Some(ClassRef::int_128(asm)), - ".ctor".into(), - ctor_sig, - false, - ), + site: asm.alloc_methodref(mref), args: [conv_u64!(ldc_u64!(high)), conv_u64!(ldc_u64!(low))].into(), })) } @@ -425,21 +435,20 @@ pub fn load_const_uint(value: u128, int_type: UintTy, asm: &mut Assembly) -> CIL UintTy::U128 => { let low = u128_low_u64(value); let high = (value >> 64) as u64; - let ctor_sig = FnSig::new( - Box::new([ - asm.nref(Type::Int(Int::U128)), - Type::Int(Int::U64), - Type::Int(Int::U64), - ]), + let u128_ptr = asm.nref(Type::Int(Int::U128)); + let ctor_sig = asm.sig( + ([u128_ptr, Type::Int(Int::U64), Type::Int(Int::U64)]), Type::Void, ); + let mref = MethodRef::new( + ClassRef::uint_128(asm), + asm.alloc_string(".ctor"), + ctor_sig, + MethodKind::Constructor, + vec![].into(), + ); CILNode::NewObj(Box::new(CallOpArgs { - site: CallSite::boxed( - Some(ClassRef::uint_128(asm)), - ".ctor".into(), - ctor_sig, - false, - ), + site: asm.alloc_methodref(mref), args: [conv_u64!(ldc_u64!(high)), conv_u64!(ldc_u64!(low))].into(), })) } @@ -448,113 +457,135 @@ pub fn load_const_uint(value: u128, int_type: UintTy, asm: &mut Assembly) -> CIL fn u128_low_u64(value: u128) -> u64 { u64::try_from(value & u128::from(u64::MAX)).expect("trucating cast error") } -fn get_fn_from_static_name(name: &str, ctx: &mut MethodCompileCtx<'_, '_>) -> CallSite { +fn get_fn_from_static_name(name: &str, ctx: &mut MethodCompileCtx<'_, '_>) -> MethodRefIdx { let int8_ptr = ctx.nptr(Type::Int(Int::I8)); + let int64_ptr = ctx.nptr(Type::Int(Int::I64)); let void_ptr = ctx.nptr(Type::Void); - match name { - "statx" => CallSite::builtin( - "statx".into(), - FnSig::new( - Box::new([ + let int8_ptr_ptr = ctx.nptr(int8_ptr); + let mref = match name { + "statx" => { + (MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("statx"), + ctx.sig( + ([ + Type::Int(Int::I32), + int8_ptr, + Type::Int(Int::I32), + Type::Int(Int::U32), + void_ptr, + ]), Type::Int(Int::I32), - ctx.nptr(Type::Int(Int::U8)), - Type::Int(Int::I32), - Type::Int(Int::U32), - void_ptr, - ]), - Type::Int(Int::I32), - ), - true, - ), - "getrandom" => CallSite::builtin( - "getrandom".into(), - FnSig::new( - Box::new([ - ctx.nptr(Type::Int(Int::U8)), + ), + MethodKind::Static, + vec![].into(), + )) + } + "getrandom" => { + (MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("getrandom"), + ctx.sig( + ([int8_ptr, Type::Int(Int::USize), Type::Int(Int::U32)]), Type::Int(Int::USize), - Type::Int(Int::U32), - ]), - Type::Int(Int::USize), - ), - true, - ), - "posix_spawn" => CallSite::builtin( - "posix_spawn".into(), - FnSig::new( - Box::new([ - ctx.nptr(Type::Int(Int::U8)), - ctx.nptr(Type::Int(Int::U8)), - ctx.nptr(Type::Int(Int::U8)), - ctx.nptr(Type::Int(Int::U8)), - ctx.nptr(Type::Int(Int::U8)), - ctx.nptr(Type::Int(Int::U8)), - ]), - Type::Int(Int::I32), - ), - true, - ), - "posix_spawn_file_actions_addchdir_np" => CallSite::builtin( - "posix_spawn_file_actions_addchdir_np".into(), - FnSig::new( - Box::new([ctx.nptr(Type::Int(Int::U8)), ctx.nptr(Type::Int(Int::U8))]), - Type::Int(Int::I32), - ), - true, - ), - "__dso_handle" => CallSite::builtin( - "__dso_handle".into(), - FnSig::new(Box::new([]), Type::Void), - true, - ), - "__cxa_thread_atexit_impl" => CallSite::builtin( - "__cxa_thread_atexit_impl".into(), - FnSig::new( - Box::new([ - Type::FnPtr(ctx.sig([void_ptr], Type::Void)), - void_ptr, - void_ptr, - ]), - Type::Void, - ), - true, - ), - "copy_file_range" => CallSite::builtin( - "copy_file_range".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I32), - ctx.nptr(Type::Int(Int::I64)), + ), + MethodKind::Static, + vec![].into(), + )) + } + "posix_spawn" => { + (MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("posix_spawn"), + ctx.sig( + ([int8_ptr, int8_ptr, int8_ptr, int8_ptr, int8_ptr, int8_ptr]), Type::Int(Int::I32), - ctx.nptr(Type::Int(Int::I64)), + ), + MethodKind::Static, + vec![].into(), + )) + } + "posix_spawn_file_actions_addchdir_np" => { + (MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("posix_spawn_file_actions_addchdir_np"), + ctx.sig(([int8_ptr, int8_ptr]), Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + )) + } + "__dso_handle" => { + (MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("__dso_handle"), + ctx.sig(([]), Type::Void), + MethodKind::Static, + vec![].into(), + )) + } + "__cxa_thread_atexit_impl" => { + let fn_ptr_sig = Type::FnPtr(ctx.sig([void_ptr], Type::Void)); + (MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("__cxa_thread_atexit_impl"), + ctx.sig([fn_ptr_sig, void_ptr, void_ptr], Type::Void), + MethodKind::Static, + vec![].into(), + )) + } + "copy_file_range" => { + let i64_ptr = ctx.nptr(Type::Int(Int::I64)); + (MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("copy_file_range"), + ctx.sig( + [ + Type::Int(Int::I32), + int64_ptr, + Type::Int(Int::I32), + i64_ptr, + Type::Int(Int::ISize), + Type::Int(Int::U32), + ], Type::Int(Int::ISize), - Type::Int(Int::U32), - ]), - Type::Int(Int::ISize), - ), - true, - ), - "pidfd_spawnp" => CallSite::builtin( - "pidfd_spawnp".into(), - FnSig::new( - Box::new([ - ctx.nptr(Type::Int(Int::I32)), - ctx.nptr(Type::Int(Int::I8)), - void_ptr, - void_ptr, - ctx.nptr(int8_ptr), - ctx.nptr(int8_ptr), - ]), - Type::Int(Int::I32), - ), - true, - ), - "pidfd_getpid" => CallSite::builtin( - "pidfd_getpid".into(), - FnSig::new(Box::new([Type::Int(Int::I32)]), Type::Int(Int::I32)), - true, - ), + ), + MethodKind::Static, + vec![].into(), + )) + } + "pidfd_spawnp" => { + let i32_ptr = ctx.nptr(Type::Int(Int::I32)); + let i8_ptr = ctx.nptr(Type::Int(Int::I8)); + (MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("pidfd_spawnp"), + ctx.sig( + [ + i32_ptr, + i8_ptr, + void_ptr, + void_ptr, + int8_ptr_ptr, + int8_ptr_ptr, + ], + Type::Int(Int::I32), + ), + MethodKind::Static, + vec![].into(), + )) + } + "pidfd_getpid" => { + (MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("pidfd_getpid"), + ctx.sig([Type::Int(Int::I32)], Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + )) + } _ => { todo!("Unsuported function refered to using a weak static. Function name is {name:?}.") } - } + }; + ctx.alloc_methodref(mref) } diff --git a/src/lib.rs b/src/lib.rs index fdf69d49..1853ed03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -161,7 +161,10 @@ pub mod config; mod unsize; // rustc functions used here. use crate::rustc_middle::dep_graph::DepContext; -use cilly::asm::Assembly; +use cilly::{ + asm::Assembly, + v2::{cilnode::MethodKind, MethodRef}, +}; use fn_ctx::MethodCompileCtx; use rustc_codegen_ssa::{ back::archive::{ArArchiveBuilder, ArchiveBuilder, ArchiveBuilderBuilder}, @@ -228,8 +231,15 @@ impl CodegenBackend for MyBackend { .expect("Could not get the signature of the entrypoint."); let symbol = tcx.symbol_name(entrypoint); let symbol = format!("{symbol:?}"); - let cs = cilly::call_site::CallSite::new(None, symbol.into(), sig, true); - asm.set_entrypoint(&cs); + let cs = MethodRef::new( + *asm.main_module(), + asm.alloc_string(symbol), + asm.alloc_sig(sig), + MethodKind::Static, + vec![].into(), + ); + let cs = asm.alloc_methodref(cs); + asm.set_entrypoint(cs); } let ffi_compile_timer = tcx diff --git a/src/place/adress.rs b/src/place/adress.rs index 67361043..63f07ff6 100644 --- a/src/place/adress.rs +++ b/src/place/adress.rs @@ -6,7 +6,7 @@ use crate::{ }; use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, ldc_u32, ldc_u64, size_of, @@ -237,7 +237,7 @@ pub fn place_elem_adress<'tcx>( let array_dotnet = array_type.as_class_ref().expect("Non array type"); call!( - CallSite::new( + MethodRefIdx::new( Some(array_dotnet), "get_Address".into(), FnSig::new( @@ -364,7 +364,7 @@ pub fn place_elem_adress<'tcx>( todo!("Can't index array from end!"); } else { call!( - CallSite::new( + MethodRefIdx::new( Some(array_dotnet), "get_Address".into(), FnSig::new( diff --git a/src/place/body.rs b/src/place/body.rs index 7f6cc526..cea74aac 100644 --- a/src/place/body.rs +++ b/src/place/body.rs @@ -8,7 +8,7 @@ use crate::{ }; use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, size_of, @@ -183,7 +183,7 @@ pub fn place_elem_body<'tcx>( let array_dotnet = array_type.as_class_ref().expect("Non array type"); if body_ty_is_by_adress(element, ctx) { let ops = call!( - CallSite::new( + MethodRefIdx::new( Some(array_dotnet), "get_Address".into(), FnSig::new( @@ -197,7 +197,7 @@ pub fn place_elem_body<'tcx>( ((element).into(), ops) } else { let ops = call!( - CallSite::new( + MethodRefIdx::new( Some(array_dotnet), "get_Item".into(), FnSig::new( @@ -256,7 +256,7 @@ pub fn place_elem_body<'tcx>( let array_dotnet = array_type.as_class_ref().expect("Non array type"); if body_ty_is_by_adress(element_ty, ctx) { let ops = call!( - CallSite::new( + MethodRefIdx::new( Some(array_dotnet), "get_Address".into(), FnSig::new( @@ -270,7 +270,7 @@ pub fn place_elem_body<'tcx>( ((element_ty).into(), ops) } else { let ops = call!( - CallSite::new( + MethodRefIdx::new( Some(array_dotnet), "get_Item".into(), FnSig::new( diff --git a/src/place/get.rs b/src/place/get.rs index 0fc2d108..94b3a8bf 100644 --- a/src/place/get.rs +++ b/src/place/get.rs @@ -1,7 +1,7 @@ use crate::{assembly::MethodCompileCtx, r#type::fat_ptr_to}; use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, conv_usize, ld_field, ldc_u32, ldc_u64, v2::{FieldDesc, FnSig, Int}, @@ -158,7 +158,7 @@ fn place_elem_get<'a>( let array_type = ctx.type_from_cache(curr_ty); let array_dotnet = array_type.as_class_ref().expect("Non array type"); call!( - CallSite::new( + MethodRefIdx::new( Some(array_dotnet), "get_Item".into(), FnSig::new( @@ -227,7 +227,7 @@ fn place_elem_get<'a>( } else { let index = CILNode::LdcU64(*offset); call!( - CallSite::new( + MethodRefIdx::new( Some(array_dotnet), "get_Item".into(), FnSig::new( diff --git a/src/place/set.rs b/src/place/set.rs index d1af183f..2d6180ba 100644 --- a/src/place/set.rs +++ b/src/place/set.rs @@ -5,7 +5,7 @@ use crate::{ }; use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, ldc_u64, size_of, @@ -106,7 +106,7 @@ pub fn place_elem_set<'a>( let array_dotnet = array_type.as_class_ref().expect("Non array type"); CILRoot::Call { - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(array_dotnet), "set_Item".into(), FnSig::new( @@ -154,7 +154,7 @@ pub fn place_elem_set<'a>( let addr = ld_field!(addr_calc.clone(), ctx.alloc_field(desc)) .cast_ptr(ctx.nptr(inner_type)) + call!( - CallSite::new( + MethodRefIdx::new( None, "bounds_check".into(), FnSig::new( @@ -177,7 +177,7 @@ pub fn place_elem_set<'a>( let array_type = ctx.type_from_cache(curr_ty); let array_dotnet = array_type.as_class_ref().expect("Non array type"); CILRoot::Call { - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(array_dotnet), "set_Item".into(), FnSig::new( diff --git a/src/rvalue.rs b/src/rvalue.rs index 74afc0d9..c2c77f00 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -5,11 +5,10 @@ use crate::{ r#type::{fat_ptr_to, get_type, pointer_to_is_fat}, }; use cilly::{ - call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, ldc_i32, ldc_u64, size_of, - v2::{FieldDesc, Float, FnSig, Int}, + v2::{cilnode::MethodKind, FieldDesc, Float, Int, MethodRef}, Type, }; use rustc_middle::{ @@ -124,8 +123,14 @@ pub fn handle_rvalue<'tcx>( let call_info = CallInfo::sig_from_instance_(instance, ctx); let function_name = crate::utilis::function_name(ctx.tcx().symbol_name(instance)); - let call_site = CallSite::new(None, function_name, call_info.sig().clone(), true); - CILNode::LDFtn(Box::new(call_site)) + let call_site = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string(function_name), + ctx.alloc_sig(call_info.sig().clone()), + MethodKind::Static, + vec![].into(), + ); + CILNode::LDFtn(ctx.alloc_methodref(call_site)) } _ => panic!( "{} cannot be cast to a fn ptr", @@ -259,8 +264,14 @@ pub fn handle_rvalue<'tcx>( let function_sig = crate::function_sig::sig_from_instance_(instance, ctx) .expect("Could not get function signature when trying to get a function pointer!"); //FIXME: propely handle `#[track_caller]` - let call_site = CallSite::new(None, function_name, function_sig, true); - CILNode::LDFtn(call_site.into()) + let call_site = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string(function_name), + ctx.alloc_sig(function_sig), + MethodKind::Static, + vec![].into(), + ); + CILNode::LDFtn(ctx.alloc_methodref(call_site)) } Rvalue::Discriminant(place) => { @@ -389,17 +400,18 @@ fn repeat<'tcx>( // Check if there are more than 16 elements. If so, use mecmpy to accelerate initialzation if times > 16 { let mut branches = Vec::new(); + let arr_ref = ctx.nref(array); + let mref = MethodRef::new( + array_dotnet, + ctx.alloc_string("set_Item"), + ctx.sig([arr_ref, Type::Int(Int::USize), element_type], Type::Void), + MethodKind::Instance, + vec![].into(), + ); + let mref = ctx.alloc_methodref(mref); for idx in 0..16 { branches.push(CILRoot::Call { - site: Box::new(CallSite::new( - Some(array_dotnet), - "set_Item".into(), - FnSig::new( - Box::new([ctx.nref(array), Type::Int(Int::USize), element_type]), - Type::Void, - ), - false, - )), + site: mref, args: [ CILNode::LoadAddresOfTMPLocal, conv_usize!(ldc_u64!(idx)), @@ -409,7 +421,6 @@ fn repeat<'tcx>( }); } let mut curr_len = 16; - while curr_len < times { // Copy curr_len elements if possible, otherwise this is the last iteration, so copy the reminder. let curr_copy_size = curr_len.min(times - curr_len); @@ -430,17 +441,18 @@ fn repeat<'tcx>( CILNode::TemporaryLocal(Box::new((array, branches, CILNode::LoadTMPLocal))) } else { let mut branches = Vec::new(); + let arr_ref = ctx.nref(array); + let mref = MethodRef::new( + array_dotnet, + ctx.alloc_string("set_Item"), + ctx.sig([arr_ref, Type::Int(Int::USize), element_type], Type::Void), + MethodKind::Instance, + vec![].into(), + ); + let mref = ctx.alloc_methodref(mref); for idx in 0..times { branches.push(CILRoot::Call { - site: Box::new(CallSite::new( - Some(array_dotnet), - "set_Item".into(), - FnSig::new( - Box::new([ctx.nref(array), Type::Int(Int::USize), element_type]), - Type::Void, - ), - false, - )), + site: mref, args: [ CILNode::LoadAddresOfTMPLocal, conv_usize!(ldc_u64!(idx)), diff --git a/src/terminator/call.rs b/src/terminator/call.rs index ba06f72f..6c7b0d4d 100644 --- a/src/terminator/call.rs +++ b/src/terminator/call.rs @@ -14,9 +14,9 @@ use cilly::{ cil_node::{CILNode, CallOpArgs}, cil_root::CILRoot, conv_usize, ld_field, ldc_i32, size_of, - v2::{ClassRef, FieldDesc, FnSig, Int}, + v2::{cilnode::MethodKind, ClassRef, FieldDesc, FnSig, Int}, }; -use cilly::{call_site::CallSite, Type}; +use cilly::{v2::MethodRef, Type}; use rustc_middle::ty::InstanceKind; use rustc_middle::{ mir::{Operand, Place}, @@ -56,15 +56,16 @@ fn call_managed<'tcx>( if argument_count == 0 { let ret = cilly::Type::Void; - let call_site = CallSite::new( - Some(ctx.alloc_class_ref(tpe)), - managed_fn_name, - FnSig::new(Box::new([]), ret), - true, + let call_site = MethodRef::new( + (ctx.alloc_class_ref(tpe)), + ctx.alloc_string(managed_fn_name), + ctx.sig(([]), ret), + MethodKind::Static, + vec![].into(), ); if *signature.output() == cilly::Type::Void { CILRoot::Call { - site: Box::new(call_site), + site: ctx.alloc_methodref(call_site), args: [].into(), } } else { @@ -77,15 +78,20 @@ fn call_managed<'tcx>( for arg in args { call_args.push(crate::operand::handle_operand(&arg.node, ctx)); } - let call = CallSite::new( - Some(ctx.alloc_class_ref(tpe)), - managed_fn_name, - signature.clone(), - is_static, + let call = MethodRef::new( + (ctx.alloc_class_ref(tpe)), + ctx.alloc_string(managed_fn_name), + ctx.alloc_sig(signature), + if is_static { + MethodKind::Static + } else { + MethodKind::Instance + }, + vec![].into(), ); if *signature.output() == cilly::Type::Void { CILRoot::Call { - site: Box::new(call), + site: ctx.alloc_methodref(call), args: call_args.into(), } } else { @@ -122,15 +128,16 @@ fn callvirt_managed<'tcx>( .expect("Can't get the function signature"); if argument_count == 0 { let ret = cilly::Type::Void; - let call = CallSite::new( - Some(ctx.alloc_class_ref(tpe)), - managed_fn_name, - FnSig::new(Box::new([]), ret), - true, + let call = MethodRef::new( + (ctx.alloc_class_ref(tpe)), + ctx.alloc_string(managed_fn_name), + ctx.sig(([]), ret), + MethodKind::Static, + vec![].into(), ); if *signature.output() == cilly::Type::Void { CILRoot::CallVirt { - site: Box::new(call), + site: ctx.alloc_methodref(call), args: [].into(), } } else { @@ -143,19 +150,28 @@ fn callvirt_managed<'tcx>( for arg in args { call_args.push(crate::operand::handle_operand(&arg.node, ctx)); } - let call = CallSite::new( - Some(ctx.alloc_class_ref(tpe)), - managed_fn_name, - signature.clone(), - is_static, + let call = MethodRef::new( + (ctx.alloc_class_ref(tpe)), + ctx.alloc_string(managed_fn_name), + ctx.alloc_sig(signature), + if is_static { + MethodKind::Static + } else { + MethodKind::Instance + }, + vec![].into(), ); if *signature.output() == cilly::Type::Void { CILRoot::CallVirt { - site: Box::new(call), + site: ctx.alloc_methodref(call), args: call_args.into(), } } else { - crate::place::place_set(destination, call_virt!(call, call_args), ctx) + crate::place::place_set( + destination, + call_virt!(ctx.alloc_methodref(call), call_args), + ctx, + ) } } } @@ -184,15 +200,17 @@ fn call_ctor<'tcx>( let tpe = ctx.alloc_class_ref(tpe); // If no arguments, inputs don't have to be handled, so a simpler call handling is used. if argument_count == 0 { + let mref = MethodRef::new( + tpe, + ctx.alloc_string(".ctor"), + ctx.sig([Type::ClassRef(tpe)], Type::Void), + MethodKind::Constructor, + vec![].into(), + ); crate::place::place_set( destination, CILNode::NewObj(Box::new(CallOpArgs { - site: CallSite::boxed( - Some(tpe), - ".ctor".into(), - FnSig::new(Box::new([Type::ClassRef(tpe)]), Type::Void), - false, - ), + site: ctx.alloc_methodref(mref), args: [].into(), })), ctx, @@ -209,16 +227,22 @@ fn call_ctor<'tcx>( }) .collect(); inputs.insert(0, Type::ClassRef(tpe)); - let sig = FnSig::new(inputs.into(), cilly::Type::Void); + let sig = ctx.sig(inputs, cilly::Type::Void); let mut call = Vec::new(); for arg in args { call.push(crate::operand::handle_operand(&arg.node, ctx)); } - + let ctor = MethodRef::new( + (tpe), + ctx.alloc_string(".ctor"), + sig, + MethodKind::Constructor, + vec![].into(), + ); crate::place::place_set( destination, CILNode::NewObj(Box::new(CallOpArgs { - site: CallSite::boxed(Some(tpe), ".ctor".into(), sig, false), + site: ctx.alloc_methodref(ctor), args: call.into(), })), ctx, @@ -281,12 +305,19 @@ pub fn call_closure<'tcx>( //panic!("Last arg:{last_arg:?}last_arg_type:{last_arg_type:?}"); //assert_eq!(args.len(),signature.inputs().len(),"CALL SIGNATURE ARG COUNT MISMATCH!"); let is_void = matches!(sig.output(), cilly::Type::Void); - let call = CallSite::new(None, function_name.into(), sig, true); + + let call = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string(function_name), + ctx.alloc_sig(sig), + MethodKind::Static, + vec![].into(), + ); // Hande the call itself if is_void { CILRoot::Call { - site: Box::new(call), + site: ctx.alloc_methodref(call), args: call_args.into(), } } else { @@ -554,11 +585,17 @@ pub fn call<'tcx>( if let InstanceKind::DropGlue(_def, None) = instance.def { return CILRoot::Nop; }; - let call_site = CallSite::new(None, function_name, signature, true); + let call_site = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string(function_name), + ctx.alloc_sig(signature), + MethodKind::Static, + vec![].into(), + ); // Hande if is_void { CILRoot::Call { - site: Box::new(call_site), + site: ctx.alloc_methodref(call_site), args: call_args.into(), } } else { diff --git a/src/terminator/intrinsics/bswap.rs b/src/terminator/intrinsics/bswap.rs index 5411d4ae..07933802 100644 --- a/src/terminator/intrinsics/bswap.rs +++ b/src/terminator/intrinsics/bswap.rs @@ -1,7 +1,7 @@ use crate::{assembly::MethodCompileCtx, operand::handle_operand, place::place_set}; use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, v2::{ClassRef, FnSig}, @@ -32,7 +32,7 @@ pub fn bswap<'tcx>( TyKind::Uint(UintTy::U8) => operand, TyKind::Uint(_) | TyKind::Int(_) => { call!( - CallSite::boxed( + MethodRefIdx::boxed( Some(ClassRef::binary_primitives(ctx)), "ReverseEndianness".into(), FnSig::new([tpe].into(), tpe), diff --git a/src/terminator/intrinsics/ints.rs b/src/terminator/intrinsics/ints.rs index 1d175ee2..363cac70 100644 --- a/src/terminator/intrinsics/ints.rs +++ b/src/terminator/intrinsics/ints.rs @@ -1,7 +1,7 @@ use crate::{assembly::MethodCompileCtx, operand::handle_operand, place::place_set}; use cilly::{ and, call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_i16, conv_i32, conv_i8, conv_isize, conv_u16, conv_u32, conv_u64, conv_u8, conv_usize, @@ -40,7 +40,7 @@ pub fn ctpop<'tcx>( destination, match tpe { Type::Int(Int::U64) => conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "PopCount".into(), FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::I32)), @@ -49,7 +49,7 @@ pub fn ctpop<'tcx>( [operand] )), Type::Int(Int::I64) => conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "PopCount".into(), FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::I32)), @@ -58,7 +58,7 @@ pub fn ctpop<'tcx>( [conv_u64!(operand)] )), Type::Int(Int::U32) => conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "PopCount".into(), FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::I32)), @@ -69,7 +69,7 @@ pub fn ctpop<'tcx>( Type::Int(Int::U8 | Int::U16 | Int::I8 | Int::I16 | Int::I32) => { conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "PopCount".into(), FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::I32)), @@ -79,7 +79,7 @@ pub fn ctpop<'tcx>( )) } Type::Int(Int::USize) => conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "PopCount".into(), FnSig::new([Type::Int(Int::USize)].into(), Type::Int(Int::I32)), @@ -88,7 +88,7 @@ pub fn ctpop<'tcx>( [operand] )), Type::Int(Int::ISize) => conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "PopCount".into(), FnSig::new([Type::Int(Int::USize)].into(), Type::Int(Int::I32)), @@ -100,7 +100,7 @@ pub fn ctpop<'tcx>( Type::Int(Int::U128), Type::Int(Int::U32), call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::uint_128(ctx), "PopCount".into(), FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128)), @@ -114,7 +114,7 @@ pub fn ctpop<'tcx>( Type::Int(Int::I128), Type::Int(Int::U32), call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "PopCount".into(), FnSig::new([Type::Int(Int::I128)].into(), Type::Int(Int::I128)), @@ -162,7 +162,7 @@ pub fn ctlz<'tcx>( return place_set( destination, conv_u32!(call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "LeadingZeroCount".into(), FnSig::new([Type::Int(Int::I128)].into(), Type::Int(Int::I128)), @@ -177,7 +177,7 @@ pub fn ctlz<'tcx>( return place_set( destination, conv_u32!(call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::uint_128(ctx), "LeadingZeroCount".into(), FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128)), @@ -194,7 +194,7 @@ pub fn ctlz<'tcx>( destination, conv_u32!(sub!( call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "LeadingZeroCount".into(), FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::I32)), @@ -230,7 +230,7 @@ pub fn cttz<'tcx>( match tpe { Type::Int(Int::I8) => { let value_calc = conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "TrailingZeroCount".into(), FnSig::new([Type::Int(Int::I32)].into(), Type::Int(Int::I32)), @@ -241,7 +241,7 @@ pub fn cttz<'tcx>( place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Min".into(), FnSig::new( @@ -257,7 +257,7 @@ pub fn cttz<'tcx>( } Type::Int(Int::I16) => { let value_calc = conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "TrailingZeroCount".into(), FnSig::new([Type::Int(Int::I32)].into(), Type::Int(Int::I32)), @@ -268,7 +268,7 @@ pub fn cttz<'tcx>( place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Min".into(), FnSig::new( @@ -284,7 +284,7 @@ pub fn cttz<'tcx>( } Type::Int(Int::U8) => { let value_calc = conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "TrailingZeroCount".into(), FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::I32)), @@ -295,7 +295,7 @@ pub fn cttz<'tcx>( place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Min".into(), FnSig::new( @@ -311,7 +311,7 @@ pub fn cttz<'tcx>( } Type::Int(Int::U16) => { let value_calc = conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "TrailingZeroCount".into(), FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::I32)), @@ -322,7 +322,7 @@ pub fn cttz<'tcx>( place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Min".into(), FnSig::new( @@ -339,7 +339,7 @@ pub fn cttz<'tcx>( Type::Int(Int::I128) => place_set( destination, conv_u32!(call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "TrailingZeroCount".into(), FnSig::new([Type::Int(Int::I128)].into(), Type::Int(Int::I128)), @@ -352,7 +352,7 @@ pub fn cttz<'tcx>( Type::Int(Int::U128) => place_set( destination, conv_u32!(call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::uint_128(ctx), "TrailingZeroCount".into(), FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128)), @@ -365,7 +365,7 @@ pub fn cttz<'tcx>( _ => place_set( destination, conv_u32!(call!( - CallSite::boxed( + MethodRefIdx::boxed( bit_operations, "TrailingZeroCount".into(), FnSig::new([tpe].into(), Type::Int(Int::I32)), @@ -400,7 +400,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::U8) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::byte(ctx), "RotateLeft".into(), FnSig::new( @@ -416,7 +416,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::U16) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::uint16(ctx), "RotateLeft".into(), FnSig::new( @@ -432,7 +432,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::U32) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateLeft".into(), FnSig::new( @@ -448,7 +448,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::U64) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateLeft".into(), FnSig::new( @@ -464,7 +464,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::USize) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateLeft".into(), FnSig::new( @@ -480,7 +480,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::I8) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::sbyte(ctx), "RotateLeft".into(), FnSig::new( @@ -496,7 +496,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::I16) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int16(ctx), "RotateLeft".into(), FnSig::new( @@ -512,7 +512,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::I32) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateLeft".into(), FnSig::new( @@ -528,7 +528,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::I64) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateLeft".into(), FnSig::new( @@ -544,7 +544,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::ISize) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateLeft".into(), FnSig::new( @@ -560,7 +560,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::U128) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::uint_128(ctx), "RotateLeft".into(), FnSig::new( @@ -576,7 +576,7 @@ pub fn rotate_left<'tcx>( Type::Int(Int::I128) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "RotateLeft".into(), FnSig::new( @@ -615,7 +615,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::U16) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::uint16(ctx), "RotateRight".into(), FnSig::new( @@ -631,7 +631,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::U8) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::byte(ctx), "RotateRight".into(), FnSig::new( @@ -647,7 +647,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::U32) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateRight".into(), FnSig::new( @@ -663,7 +663,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::U64) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateRight".into(), FnSig::new( @@ -679,7 +679,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::USize) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateRight".into(), FnSig::new( @@ -695,7 +695,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::I8) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::sbyte(ctx), "RotateRight".into(), FnSig::new( @@ -711,7 +711,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::I16) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int16(ctx), "RotateRight".into(), FnSig::new( @@ -727,7 +727,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::I32) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateRight".into(), FnSig::new( @@ -743,7 +743,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::I64) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateRight".into(), FnSig::new( @@ -759,7 +759,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::ISize) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::bit_operations(ctx), "RotateRight".into(), FnSig::new( @@ -775,7 +775,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::U128) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::uint_128(ctx), "RotateRight".into(), FnSig::new( @@ -791,7 +791,7 @@ pub fn rotate_right<'tcx>( Type::Int(Int::I128) => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "RotateRight".into(), FnSig::new( @@ -849,7 +849,7 @@ pub fn bitreverse<'tcx>( Type::Int(Int::U16) => bitreverse_u16(val), Type::Int(Int::I16) => conv_i16!(bitreverse_u16(conv_u16!(val))), Type::Int(Int::U32) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "bitreverse_u32".into(), FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U32)), true @@ -860,7 +860,7 @@ pub fn bitreverse<'tcx>( Type::Int(Int::U32), Type::Int(Int::I32), call!( - CallSite::builtin( + MethodRefIdx::builtin( "bitreverse_u32".into(), FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U32)), true @@ -875,7 +875,7 @@ pub fn bitreverse<'tcx>( ctx, ), Type::Int(Int::U64) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "bitreverse_u64".into(), FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::U64)), true @@ -886,7 +886,7 @@ pub fn bitreverse<'tcx>( Type::Int(Int::U64), Type::Int(Int::I64), call!( - CallSite::builtin( + MethodRefIdx::builtin( "bitreverse_u64".into(), FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::U64)), true @@ -901,7 +901,7 @@ pub fn bitreverse<'tcx>( ctx, ), Type::Int(Int::U128) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "bitreverse_u128".into(), FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128),), true @@ -912,7 +912,7 @@ pub fn bitreverse<'tcx>( Type::Int(Int::U128), Type::Int(Int::I128), call!( - CallSite::builtin( + MethodRefIdx::builtin( "bitreverse_u128".into(), FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128),), true diff --git a/src/terminator/intrinsics/mod.rs b/src/terminator/intrinsics/mod.rs index 696c3d29..3b0670a6 100644 --- a/src/terminator/intrinsics/mod.rs +++ b/src/terminator/intrinsics/mod.rs @@ -6,7 +6,7 @@ use crate::{ }; use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, call_virt, cil_node::CILNode, cil_root::CILRoot, @@ -125,7 +125,7 @@ pub fn handle_intrinsic<'tcx>( "fmaf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "FusedMultiplyAdd".into(), FnSig::new( @@ -150,7 +150,7 @@ pub fn handle_intrinsic<'tcx>( "fmaf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "FusedMultiplyAdd".into(), FnSig::new( @@ -303,21 +303,21 @@ pub fn handle_intrinsic<'tcx>( place_set( destination, call!( - CallSite::boxed( + MethodRefIdx::boxed( Some(ClassRef::uint_128(ctx)), "op_Implicit".into(), FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U128)), true, ), [conv_u32!(call_virt!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::object(ctx).into(), "GetHashCode".into(), gethash_sig, false, ), [call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::type_type(ctx).into(), "GetTypeFromHandle".into(), sig, @@ -440,7 +440,7 @@ pub fn handle_intrinsic<'tcx>( #[allow(clippy::single_match_else)] let exchange_res = match &src_type { Type::Ptr(_) => { - let call_site = CallSite::new( + let call_site = MethodRefIdx::new( Some(interlocked), "CompareExchange".into(), FnSig::new( @@ -467,7 +467,7 @@ pub fn handle_intrinsic<'tcx>( // TODO: this is a bug, on purpose. The 1 byte compare exchange is not supported untill .NET 9. Remove after November, when .NET 9 Releases. Type::Int(Int::U8) => comaprand, _ => { - let call_site = CallSite::new( + let call_site = MethodRefIdx::new( Some(interlocked), "CompareExchange".into(), FnSig::new([ctx.nref(src_type), src_type, src_type].into(), src_type), @@ -582,7 +582,7 @@ pub fn handle_intrinsic<'tcx>( | "atomic_fence_acqrel" => { let thread = ClassRef::thread(ctx); CILRoot::Call { - site: Box::new(CallSite::new( + site: Box::new(MethodRefIdx::new( Some(thread), "MemoryBarrier".into(), FnSig::new([].into(), Type::Void), @@ -684,7 +684,7 @@ pub fn handle_intrinsic<'tcx>( return place_set( destination, call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_xchng_u8".into(), FnSig::new( [ctx.nref(Type::Int(Int::U8)), Type::Int(Int::U8)].into(), @@ -698,7 +698,7 @@ pub fn handle_intrinsic<'tcx>( ) } Type::Ptr(_) => { - let call_site = CallSite::new( + let call_site = MethodRefIdx::new( Some(interlocked), "Exchange".into(), FnSig::new( @@ -725,7 +725,7 @@ pub fn handle_intrinsic<'tcx>( } _ => (), } - let call_site = CallSite::new( + let call_site = MethodRefIdx::new( Some(interlocked), "Exchange".into(), FnSig::new([ctx.nref(src_type), src_type].into(), src_type), @@ -849,7 +849,7 @@ pub fn handle_intrinsic<'tcx>( place_set( destination, call!( - CallSite::boxed( + MethodRefIdx::boxed( Some(ClassRef::mathf(ctx)), "Sqrt".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -871,7 +871,7 @@ pub fn handle_intrinsic<'tcx>( place_set( destination, call!( - CallSite::boxed( + MethodRefIdx::boxed( Some(ClassRef::single(ctx)), "Pow".into(), FnSig::new( @@ -898,7 +898,7 @@ pub fn handle_intrinsic<'tcx>( place_set( destination, call!( - CallSite::boxed( + MethodRefIdx::boxed( Some(ClassRef::double(ctx)), "Pow".into(), FnSig::new( @@ -925,7 +925,7 @@ pub fn handle_intrinsic<'tcx>( let tpe = ctx.monomorphize(pointed_ty); let tpe = ctx.type_from_cache(tpe); CILRoot::Call { - site: Box::new(CallSite::builtin( + site: Box::new(MethodRefIdx::builtin( "swap_at_generic".into(), FnSig::new( [ @@ -988,7 +988,7 @@ pub fn handle_intrinsic<'tcx>( "fabsf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "Abs".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1001,7 +1001,7 @@ pub fn handle_intrinsic<'tcx>( "fabsf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "Abs".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1014,7 +1014,7 @@ pub fn handle_intrinsic<'tcx>( "expf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "Exp".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1027,7 +1027,7 @@ pub fn handle_intrinsic<'tcx>( "expf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "Exp".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1040,7 +1040,7 @@ pub fn handle_intrinsic<'tcx>( "logf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "Log".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1053,7 +1053,7 @@ pub fn handle_intrinsic<'tcx>( "logf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "Log".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1066,7 +1066,7 @@ pub fn handle_intrinsic<'tcx>( "log2f32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "Log2".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1079,7 +1079,7 @@ pub fn handle_intrinsic<'tcx>( "log2f64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "Log2".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1092,7 +1092,7 @@ pub fn handle_intrinsic<'tcx>( "log10f32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "Log10".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1105,7 +1105,7 @@ pub fn handle_intrinsic<'tcx>( "log10f64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "Log10".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1118,7 +1118,7 @@ pub fn handle_intrinsic<'tcx>( "powf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "Pow".into(), FnSig::new( @@ -1137,7 +1137,7 @@ pub fn handle_intrinsic<'tcx>( "powf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "Pow".into(), FnSig::new( @@ -1156,7 +1156,7 @@ pub fn handle_intrinsic<'tcx>( "copysignf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "CopySign".into(), FnSig::new( @@ -1175,7 +1175,7 @@ pub fn handle_intrinsic<'tcx>( "copysignf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "CopySign".into(), FnSig::new( @@ -1194,7 +1194,7 @@ pub fn handle_intrinsic<'tcx>( "sinf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "Sin".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1207,7 +1207,7 @@ pub fn handle_intrinsic<'tcx>( "sinf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "Sin".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1220,7 +1220,7 @@ pub fn handle_intrinsic<'tcx>( "cosf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "Cos".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1233,7 +1233,7 @@ pub fn handle_intrinsic<'tcx>( "cosf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "Cos".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1246,7 +1246,7 @@ pub fn handle_intrinsic<'tcx>( "exp2f32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "Exp2".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1259,7 +1259,7 @@ pub fn handle_intrinsic<'tcx>( "exp2f64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "Exp2".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1272,7 +1272,7 @@ pub fn handle_intrinsic<'tcx>( "truncf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::mathf(ctx), "Truncate".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1285,7 +1285,7 @@ pub fn handle_intrinsic<'tcx>( "truncf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Truncate".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1299,7 +1299,7 @@ pub fn handle_intrinsic<'tcx>( "nearbyintf32" | "rintf32" | "roundevenf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::mathf(ctx), "Round".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1312,7 +1312,7 @@ pub fn handle_intrinsic<'tcx>( "roundf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::mathf(ctx), "Round".into(), FnSig::new( @@ -1339,7 +1339,7 @@ pub fn handle_intrinsic<'tcx>( "nearbyintf64" | "rintf64" | "roundevenf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Round".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1352,7 +1352,7 @@ pub fn handle_intrinsic<'tcx>( "roundf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Round".into(), FnSig::new( @@ -1379,7 +1379,7 @@ pub fn handle_intrinsic<'tcx>( "floorf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::mathf(ctx), "Floor".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1392,7 +1392,7 @@ pub fn handle_intrinsic<'tcx>( "floorf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Floor".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1405,7 +1405,7 @@ pub fn handle_intrinsic<'tcx>( "ceilf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::mathf(ctx), "Ceiling".into(), FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), @@ -1418,7 +1418,7 @@ pub fn handle_intrinsic<'tcx>( "ceilf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Ceiling".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1431,7 +1431,7 @@ pub fn handle_intrinsic<'tcx>( "maxnumf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "MaxNumber".into(), FnSig::new( @@ -1450,7 +1450,7 @@ pub fn handle_intrinsic<'tcx>( "maxnumf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "MaxNumber".into(), FnSig::new( @@ -1469,7 +1469,7 @@ pub fn handle_intrinsic<'tcx>( "minnumf64" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::double(ctx), "MinNumber".into(), FnSig::new( @@ -1488,7 +1488,7 @@ pub fn handle_intrinsic<'tcx>( "minnumf32" => place_set( destination, call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::single(ctx), "MinNumber".into(), FnSig::new( @@ -1527,7 +1527,7 @@ pub fn handle_intrinsic<'tcx>( ); let ops = call!( - CallSite::boxed( + MethodRefIdx::boxed( Some(ClassRef::math(ctx)), "Sqrt".into(), FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), @@ -1551,7 +1551,7 @@ pub fn handle_intrinsic<'tcx>( place_set( destination, call!( - CallSite::builtin( + MethodRefIdx::builtin( "catch_unwind".into(), FnSig::new( [ @@ -1638,21 +1638,21 @@ fn intrinsic_slow<'tcx>( place_set( destination, call!( - CallSite::boxed( + MethodRefIdx::boxed( Some(ClassRef::uint_128(ctx)), "op_Implicit".into(), FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U128)), true, ), [conv_u32!(call_virt!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::object(ctx).into(), "GetHashCode".into(), gethash_sig, false, ), [call!( - CallSite::boxed( + MethodRefIdx::boxed( ClassRef::type_type(ctx).into(), "GetTypeFromHandle".into(), sig, @@ -1693,7 +1693,7 @@ fn intrinsic_slow<'tcx>( let tpe = ctx.monomorphize(pointed_ty); let tpe = ctx.type_from_cache(tpe); CILRoot::Call { - site: Box::new(CallSite::builtin( + site: Box::new(MethodRefIdx::builtin( "swap_at_generic".into(), FnSig::new( [ diff --git a/src/terminator/intrinsics/saturating.rs b/src/terminator/intrinsics/saturating.rs index bb2e0d2a..41d0a9e9 100644 --- a/src/terminator/intrinsics/saturating.rs +++ b/src/terminator/intrinsics/saturating.rs @@ -1,7 +1,7 @@ use crate::{assembly::MethodCompileCtx, operand::handle_operand, place::place_set}; use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_i16, conv_i32, conv_i64, conv_i8, ldc_i32, ldc_i64, @@ -47,7 +47,7 @@ pub fn saturating_add<'tcx>( let b = conv_i64!(b); let diff = a + b; let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Clamp".into(), FnSig::new( @@ -73,7 +73,7 @@ pub fn saturating_add<'tcx>( let a = crate::casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), a, ctx); let b = crate::casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), b, ctx); let diff = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "op_Addition".into(), FnSig::new( @@ -86,7 +86,7 @@ pub fn saturating_add<'tcx>( ); #[allow(clippy::cast_sign_loss)] let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "Clamp".into(), FnSig::new( @@ -112,7 +112,7 @@ pub fn saturating_add<'tcx>( let a = crate::casts::int_to_int(Type::Int(Int::ISize), Type::Int(Int::I128), a, ctx); let b = crate::casts::int_to_int(Type::Int(Int::ISize), Type::Int(Int::I128), b, ctx); let diff = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "op_Addition".into(), FnSig::new( @@ -125,7 +125,7 @@ pub fn saturating_add<'tcx>( ); #[allow(clippy::cast_sign_loss)] let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "Clamp".into(), FnSig::new( @@ -157,7 +157,7 @@ pub fn saturating_add<'tcx>( let b = conv_i32!(b); let diff = a + b; let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Clamp".into(), FnSig::new( @@ -183,7 +183,7 @@ pub fn saturating_add<'tcx>( let b = conv_i32!(b); let diff = a + b; let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Clamp".into(), FnSig::new( @@ -233,7 +233,7 @@ pub fn saturating_sub<'tcx>( let a = crate::casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), a, ctx); let b = crate::casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), b, ctx); let diff = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "op_Subtraction".into(), FnSig::new( @@ -246,7 +246,7 @@ pub fn saturating_sub<'tcx>( ); #[allow(clippy::cast_sign_loss)] let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "Clamp".into(), FnSig::new( @@ -271,7 +271,7 @@ pub fn saturating_sub<'tcx>( let a = crate::casts::int_to_int(Type::Int(Int::ISize), Type::Int(Int::I128), a, ctx); let b = crate::casts::int_to_int(Type::Int(Int::ISize), Type::Int(Int::I128), b, ctx); let diff = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "op_Subtraction".into(), FnSig::new( @@ -284,7 +284,7 @@ pub fn saturating_sub<'tcx>( ); #[allow(clippy::cast_sign_loss)] let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::int_128(ctx), "Clamp".into(), FnSig::new( @@ -316,7 +316,7 @@ pub fn saturating_sub<'tcx>( let b = conv_i64!(b); let diff = a - b; let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Clamp".into(), FnSig::new( @@ -342,7 +342,7 @@ pub fn saturating_sub<'tcx>( let b = conv_i32!(b); let diff = a - b; let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Clamp".into(), FnSig::new( @@ -368,7 +368,7 @@ pub fn saturating_sub<'tcx>( let b = conv_i32!(b); let diff = a - b; let diff_capped = call!( - CallSite::new_extern( + MethodRefIdx::new_extern( ClassRef::math(ctx), "Clamp".into(), FnSig::new( diff --git a/src/terminator/intrinsics/utilis.rs b/src/terminator/intrinsics/utilis.rs index 28d8d77a..d7c1911f 100644 --- a/src/terminator/intrinsics/utilis.rs +++ b/src/terminator/intrinsics/utilis.rs @@ -1,6 +1,6 @@ use cilly::{ call, - call_site::CallSite, + call_site::MethodRefIdx, cil_node::CILNode, v2::{Assembly, ClassRef, FnSig, Int}, Type, @@ -10,7 +10,7 @@ pub fn atomic_add(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) match tpe { Type::Int(Int::U64 | Int::I64) => { call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked(asm)), "Add".into(), FnSig::new( @@ -24,7 +24,7 @@ pub fn atomic_add(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::U32 | Int::I32) => { call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked(asm)), "Add".into(), FnSig::new( @@ -37,7 +37,7 @@ pub fn atomic_add(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) ) } Type::Int(Int::USize | Int::ISize) | Type::Ptr(_) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_add_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -58,7 +58,7 @@ pub fn atomic_or(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) match tpe { Type::Int(Int::U64 | Int::I64) => { call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked(asm)), "Or".into(), FnSig::new( @@ -72,7 +72,7 @@ pub fn atomic_or(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::U32 | Int::I32) => { call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked(asm)), "Or".into(), FnSig::new( @@ -85,7 +85,7 @@ pub fn atomic_or(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) ) } Type::Int(Int::USize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_or_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -96,7 +96,7 @@ pub fn atomic_or(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) [addr, addend] ), Type::Int(Int::ISize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_or_isize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), @@ -107,7 +107,7 @@ pub fn atomic_or(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) [addr, addend] ), Type::Ptr(inner) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_or_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -128,7 +128,7 @@ pub fn atomic_xor(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) match tpe { Type::Int(Int::I32) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_xor_i32".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::I32)), Type::Int(Int::I32)]), @@ -141,7 +141,7 @@ pub fn atomic_xor(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::I64) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_xor_i64".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::I64)), Type::Int(Int::I64)]), @@ -154,7 +154,7 @@ pub fn atomic_xor(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::U32) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_xor_u32".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), @@ -167,7 +167,7 @@ pub fn atomic_xor(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::U64) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_xor_u64".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), @@ -179,7 +179,7 @@ pub fn atomic_xor(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) ) } Type::Int(Int::USize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_xor_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -190,7 +190,7 @@ pub fn atomic_xor(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) [addr, addend] ), Type::Int(Int::ISize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_xor_isize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), @@ -201,7 +201,7 @@ pub fn atomic_xor(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) [addr, addend] ), Type::Ptr(inner) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_xor_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -222,7 +222,7 @@ pub fn atomic_and(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) match tpe { Type::Int(Int::U64 | Int::I64) => { call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked(asm)), "And".into(), FnSig::new( @@ -236,7 +236,7 @@ pub fn atomic_and(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::U32 | Int::I32) => { call!( - CallSite::new( + MethodRefIdx::new( Some(ClassRef::interlocked(asm)), "And".into(), FnSig::new( @@ -249,7 +249,7 @@ pub fn atomic_and(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) ) } Type::Int(Int::USize | Int::ISize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_and_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -260,7 +260,7 @@ pub fn atomic_and(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) [addr, addend] ), Type::Ptr(inner) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_and_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -279,7 +279,7 @@ pub fn atomic_and(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } pub fn compare_bytes(a: CILNode, b: CILNode, len: CILNode, asm: &mut Assembly) -> CILNode { call!( - CallSite::builtin( + MethodRefIdx::builtin( "memcmp".into(), FnSig::new( Box::new([ @@ -298,7 +298,7 @@ pub fn atomic_nand(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly match tpe { Type::Int(Int::I32) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_nand_i32".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::I32)), Type::Int(Int::I32)]), @@ -311,7 +311,7 @@ pub fn atomic_nand(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly } Type::Int(Int::I64) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_nand_i64".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::I64)), Type::Int(Int::I64)]), @@ -324,7 +324,7 @@ pub fn atomic_nand(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly } Type::Int(Int::U32) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_nand_u32".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), @@ -337,7 +337,7 @@ pub fn atomic_nand(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly } Type::Int(Int::U64) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_nand_u64".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), @@ -349,7 +349,7 @@ pub fn atomic_nand(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly ) } Type::Int(Int::USize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_nand_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -360,7 +360,7 @@ pub fn atomic_nand(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly [addr, addend] ), Type::Int(Int::ISize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_nand_isize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), @@ -371,7 +371,7 @@ pub fn atomic_nand(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly [addr, addend] ), Type::Ptr(inner) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_nand_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -392,7 +392,7 @@ pub fn atomic_min(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) match tpe { Type::Int(Int::I32) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_min_i32".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::I32)), Type::Int(Int::I32)]), @@ -405,7 +405,7 @@ pub fn atomic_min(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::I64) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_min_i64".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::I64)), Type::Int(Int::I64)]), @@ -418,7 +418,7 @@ pub fn atomic_min(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::U32) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_min_u32".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), @@ -431,7 +431,7 @@ pub fn atomic_min(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::U64) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_min_u64".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), @@ -443,7 +443,7 @@ pub fn atomic_min(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) ) } Type::Int(Int::USize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_min_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -454,7 +454,7 @@ pub fn atomic_min(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) [addr, addend] ), Type::Int(Int::ISize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_min_isize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), @@ -465,7 +465,7 @@ pub fn atomic_min(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) [addr, addend] ), Type::Ptr(inner) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_min_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -486,7 +486,7 @@ pub fn atomic_max(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) match tpe { Type::Int(Int::I32) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_max_i32".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::I32)), Type::Int(Int::I32)]), @@ -499,7 +499,7 @@ pub fn atomic_max(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::I64) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_max_i64".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::I64)), Type::Int(Int::I64)]), @@ -512,7 +512,7 @@ pub fn atomic_max(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::U32) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_max_u32".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), @@ -525,7 +525,7 @@ pub fn atomic_max(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Int(Int::U64) => { call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_max_u64".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), @@ -537,7 +537,7 @@ pub fn atomic_max(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) ) } Type::Int(Int::USize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_max_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), @@ -548,7 +548,7 @@ pub fn atomic_max(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) [addr, addend] ), Type::Int(Int::ISize) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_max_isize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), @@ -559,7 +559,7 @@ pub fn atomic_max(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) [addr, addend] ), Type::Ptr(inner) => call!( - CallSite::builtin( + MethodRefIdx::builtin( "atomic_max_usize".into(), FnSig::new( Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), diff --git a/src/terminator/mod.rs b/src/terminator/mod.rs index 091d962a..089b9f70 100644 --- a/src/terminator/mod.rs +++ b/src/terminator/mod.rs @@ -1,11 +1,10 @@ use crate::{assembly::MethodCompileCtx, place::place_set}; use cilly::{ - call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, cil_tree::CILTree, conv_usize, ld_field, ldc_u32, - v2::{Assembly, FieldDesc, FnSig, Int}, + v2::{cilnode::MethodKind, Assembly, FieldDesc, FnSig, Int, MethodRef}, Type, }; use rustc_middle::{ @@ -229,9 +228,16 @@ pub fn handle_terminator<'tcx>( crate::function_sig::sig_from_instance_(drop_instance, ctx).unwrap(); let function_name = crate::utilis::function_name(ctx.tcx().symbol_name(drop_instance)); + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string(function_name), + ctx.alloc_sig(sig), + MethodKind::Static, + vec![].into(), + ); vec![ CILRoot::Call { - site: Box::new(CallSite::new(None, function_name, sig, true)), + site: ctx.alloc_methodref(mref), args: [crate::place::place_adress(place, ctx)].into(), } .into(), diff --git a/src/type/tycache.rs b/src/type/tycache.rs index 9142a1a7..a5789fe5 100644 --- a/src/type/tycache.rs +++ b/src/type/tycache.rs @@ -636,7 +636,7 @@ pub fn validity_check<'tcx>( rustc_middle::ty::AdtKind::Struct | rustc_middle::ty::AdtKind::Enum => { if let Some(d_tpe) = tpe.as_class_ref() { cilly::call!( - cilly::call_site::CallSite::new( + cilly::call_site::MethodRefIdx::new( Some(d_tpe), "check_valid".into(), FnSig::new(&[tpe.clone()], tpe), @@ -722,7 +722,7 @@ fn enum_bound_check<'tcx>( let discr = get_discr(layout, addr, enum_tpe.as_class_ref().unwrap(), tcx, ty); let root = cilly::cil_root::CILRoot::Pop { tree: cilly::call!( - cilly::call_site::CallSite::new( + cilly::call_site::MethodRefIdx::new( None, "bounds_check".into(), FnSig::new( diff --git a/src/type/type.rs b/src/type/type.rs index 7aa93c1f..ab96b172 100644 --- a/src/type/type.rs +++ b/src/type/type.rs @@ -5,10 +5,9 @@ use crate::{ }; use cilly::{ call, - call_site::CallSite, cil_node::CILNode, ldc_u32, ldc_u64, - v2::{Assembly, ClassRef, ClassRefIdx, FnSig, Int}, + v2::{cilnode::MethodKind, Assembly, ClassRef, ClassRefIdx, Int, MethodRef}, Type, }; use rustc_middle::ty::{AdtDef, ConstKind, GenericArg, ParamEnv, Ty, TyCtxt, TyKind}; @@ -24,11 +23,12 @@ pub struct DotnetArray { pub fn max_value(tpe: &Type, asm: &mut Assembly) -> CILNode { match tpe { Type::Int(Int::USize) => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::usize_type(asm), - "get_MaxValue".into(), - FnSig::new(Box::new([]), Type::Int(Int::USize)), - true + asm.alloc_string("get_MaxValue"), + asm.sig(([]), Type::Int(Int::USize)), + MethodKind::Static, + vec![].into() ), [] ), diff --git a/src/unop.rs b/src/unop.rs index f772c2d7..93f15f0f 100644 --- a/src/unop.rs +++ b/src/unop.rs @@ -1,9 +1,10 @@ use crate::assembly::MethodCompileCtx; use crate::r#type::get_type; -use cilly::call_site::CallSite; + use cilly::cil_node::CILNode; -use cilly::v2::{ClassRef, FieldDesc, FnSig, Int}; +use cilly::v2::cilnode::MethodKind; +use cilly::v2::{ClassRef, FieldDesc, FnSig, Int, MethodRef}; use cilly::{call, ld_field, Type}; use rustc_middle::mir::{Operand, UnOp}; @@ -19,50 +20,52 @@ pub fn unop<'tcx>( let ty = operand.ty(&ctx.body().local_decls, ctx.tcx()); match unnop { UnOp::Neg => match ty.kind() { - TyKind::Int(IntTy::I128) => call!( - CallSite::boxed( + TyKind::Int(IntTy::I128) => { + let mref = MethodRef::new( ClassRef::int_128(ctx).into(), - "op_UnaryNegation".into(), - FnSig::new(Box::new([Type::Int(Int::I128)]), Type::Int(Int::I128)), - true, - ), - [parrent_node] - ), + ctx.alloc_string("op_UnaryNegation"), + ctx.sig(([Type::Int(Int::I128)]), Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [parrent_node]) + } TyKind::Int(IntTy::I8) => CILNode::Neg(CILNode::ConvI8(parrent_node.into()).into()), TyKind::Int(IntTy::I16) => CILNode::Neg(CILNode::ConvI16(parrent_node.into()).into()), - TyKind::Uint(UintTy::U128) => call!( - CallSite::boxed( + TyKind::Uint(UintTy::U128) => { + let mref = MethodRef::new( ClassRef::uint_128(ctx).into(), - "op_UnaryNegation".into(), - FnSig::new(Box::new([Type::Int(Int::U128)]), Type::Int(Int::U128)), - true, - ), - [parrent_node] - ), + ctx.alloc_string("op_UnaryNegation"), + ctx.sig(([Type::Int(Int::U128)]), Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [parrent_node]) + } _ => CILNode::Neg(parrent_node.into()), }, UnOp::Not => match ty.kind() { TyKind::Bool => CILNode::Eq(CILNode::LdFalse.into(), parrent_node.into()), - TyKind::Uint(UintTy::U128) => call!( - CallSite::boxed( + TyKind::Uint(UintTy::U128) => { + let mref = MethodRef::new( ClassRef::uint_128(ctx).into(), - "op_OnesComplement".into(), - FnSig::new(Box::new([Type::Int(Int::U128)]), Type::Int(Int::U128)), - true, - ), - [parrent_node] - ), - TyKind::Int(IntTy::I128) => call!( - CallSite::boxed( + ctx.alloc_string("op_OnesComplement"), + ctx.sig([Type::Int(Int::U128)], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [parrent_node]) + } + TyKind::Int(IntTy::I128) => { + let mref = MethodRef::new( ClassRef::int_128(ctx).into(), - "op_OnesComplement".into(), - FnSig::new(Box::new([Type::Int(Int::I128)]), Type::Int(Int::I128)), - true, - ), - [parrent_node] - ), - - //TyKind::U128 => ops.extend([CILOp::LdcI32(0), CILOp::Eq]), + ctx.alloc_string("op_OnesComplement"), + ctx.sig([Type::Int(Int::I128)], Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [parrent_node]) + } _ => CILNode::Not(parrent_node.into()), }, rustc_middle::mir::UnOp::PtrMetadata => { diff --git a/src/utilis/adt.rs b/src/utilis/adt.rs index 90202c9a..c07a5b96 100644 --- a/src/utilis/adt.rs +++ b/src/utilis/adt.rs @@ -1,10 +1,12 @@ use cilly::{ call, - call_site::CallSite, cil_node::CILNode, cil_root::CILRoot, eq, gt_un, ldc_u64, sub, - v2::{Assembly, ClassRef, ClassRefIdx, FieldDesc, Float, FnSig, Int}, + v2::{ + cilnode::MethodKind, Assembly, ClassRef, ClassRefIdx, FieldDesc, Float, FnSig, Int, + MethodRef, + }, Type, }; use rustc_middle::ty::{AdtDef, Ty}; @@ -305,36 +307,44 @@ pub fn get_discr<'tcx>( // } let is_niche = match tag_tpe { - Type::Int(Int::U128) => call!( - CallSite::new_extern( + Type::Int(Int::U128) => { + let mref = MethodRef::new( ClassRef::uint_128(ctx), - "op_Equality".into(), - FnSig::new( - Box::new([Type::Int(Int::U128), Type::Int(Int::U128)]), - Type::Bool - ), - true - ), - [ - tag, - CILNode::const_u128(u128::from(niche_variants.start().as_u32(),), ctx) - ] - ), - Type::Int(Int::I128) => call!( - CallSite::new_extern( + ctx.alloc_string("op_Equality"), + ctx.sig(([Type::Int(Int::U128), Type::Int(Int::U128)]), Type::Bool), + MethodKind::Static, + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), + [ + tag, + CILNode::const_u128( + u128::from(niche_variants.start().as_u32(),), + ctx + ) + ] + ) + } + Type::Int(Int::I128) => { + let mref = MethodRef::new( ClassRef::int_128(ctx), - "op_Equality".into(), - FnSig::new( - Box::new([Type::Int(Int::I128), Type::Int(Int::I128)]), - Type::Bool - ), - true - ), - [ - tag, - CILNode::const_i128(u128::from(niche_variants.start().as_u32()), ctx) - ] - ), + ctx.alloc_string("op_Equality"), + ctx.sig(([Type::Int(Int::I128), Type::Int(Int::I128)]), Type::Bool), + MethodKind::Static, + vec![].into(), + ); + call!( + mref, + [ + tag, + CILNode::const_i128( + u128::from(niche_variants.start().as_u32()), + ctx + ) + ] + ) + } _ => eq!( tag, @@ -378,14 +388,12 @@ pub fn get_discr<'tcx>( }; let gt = match tag_tpe { Type::Int(Int::U128) => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::uint_128(ctx), - "op_GreaterThan".into(), - FnSig::new( - Box::new([Type::Int(Int::U128), Type::Int(Int::U128)]), - Type::Bool - ), - true + ctx.alloc_string("op_GreaterThan"), + ctx.sig(([Type::Int(Int::U128), Type::Int(Int::U128)]), Type::Bool), + MethodKind::Static, + vec![].into() ), [ relative_discr.clone(), @@ -393,14 +401,12 @@ pub fn get_discr<'tcx>( ] ), Type::Int(Int::I128) => call!( - CallSite::new_extern( + MethodRef::new( ClassRef::int_128(ctx), - "op_GreaterThan".into(), - FnSig::new( - Box::new([Type::Int(Int::I128), Type::Int(Int::I128)]), - Type::Bool - ), - true + ctx.alloc_string("op_GreaterThan"), + ctx.sig(([Type::Int(Int::I128), Type::Int(Int::I128)]), Type::Bool), + MethodKind::Static, + vec![].into() ), [ relative_discr.clone(), From ce3cbd7540aa215cd718e537e4f631b0560ae797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Tue, 1 Oct 2024 00:10:24 +0200 Subject: [PATCH 02/10] Some more work on porting to the new method reference representation --- src/assembly.rs | 7 +- src/binop/cmp.rs | 209 ++++++++++++++++++++--------------------- src/place/adress.rs | 45 +++++---- src/place/body.rs | 185 +++++++++++++++++++----------------- src/terminator/call.rs | 22 +++-- src/type/type.rs | 12 +-- src/utilis/adt.rs | 48 +++++----- 7 files changed, 272 insertions(+), 256 deletions(-) diff --git a/src/assembly.rs b/src/assembly.rs index 3ce67343..16d38f59 100644 --- a/src/assembly.rs +++ b/src/assembly.rs @@ -107,11 +107,13 @@ fn allocation_initializer_method( let align = const_allocation.align.bytes().max(1); //trees.push(CILRoot::debug(&format!("Preparing to initialize allocation with size {}",bytes.len())).into()); if align > 8 { + let aligned_alloc = MethodRef::aligned_alloc(asm); + trees.push( CILRoot::STLoc { local: 0, tree: Box::new(call!( - MethodRef::aligned_alloc(asm), + asm.alloc_methodref(aligned_alloc), [ conv_usize!(ldc_u64!(bytes.len() as u64)), conv_usize!(ldc_u64!(align)) @@ -122,11 +124,12 @@ fn allocation_initializer_method( .into(), ); } else { + let alloc = MethodRef::alloc(asm); trees.push( CILRoot::STLoc { local: 0, tree: Box::new(call!( - MethodRef::alloc(asm), + asm.alloc_methodref(alloc), [ldc_i32!( i32::try_from(bytes.len()).expect("Static alloc too big") )] diff --git a/src/binop/cmp.rs b/src/binop/cmp.rs index b5007609..24450e87 100644 --- a/src/binop/cmp.rs +++ b/src/binop/cmp.rs @@ -2,7 +2,7 @@ use cilly::{ call, cil_node::CILNode, eq, gt, gt_un, lt, lt_un, - v2::{Assembly, ClassRef, Float, FnSig, Int, MethodRef}, + v2::{cilnode::MethodKind, Assembly, ClassRef, Float, FnSig, Int, MethodRef}, Type, }; use rustc_middle::ty::{FloatTy, IntTy, Ty, TyKind, UintTy}; @@ -28,33 +28,29 @@ pub fn eq_unchecked( //vec![CILOp::Eq] match ty_a.kind() { TyKind::Uint(uint) => match uint { - UintTy::U128 => call!( - MethodRef::new( + UintTy::U128 => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "op_Equality".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Bool - ), - true, - ), - [operand_a, operand_b] - ), + asm.alloc_string("op_Equality"), + asm.sig([Type::Int(Int::U128), Type::Int(Int::U128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } _ => eq!(operand_a, operand_b), }, TyKind::Int(int) => match int { - IntTy::I128 => call!( - MethodRef::new( + IntTy::I128 => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_Equality".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Bool - ), - true, - ), - [operand_a, operand_b] - ), + asm.alloc_string("op_Equality"), + asm.sig([Type::Int(Int::I128), Type::Int(Int::I128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } _ => eq!(operand_a, operand_b), }, TyKind::Bool @@ -63,29 +59,32 @@ pub fn eq_unchecked( | TyKind::RawPtr(_, _) => { eq!(operand_a, operand_b) } - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "__eqtf2".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Bool + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("__eqtf2"), + asm.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Bool, ), - true - ), - [operand_a, operand_b] - ), - TyKind::Float(FloatTy::F16) => call!( - MethodRef::new( + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } + TyKind::Float(FloatTy::F16) => { + let mref = MethodRef::new( ClassRef::half(asm), - "op_Equality".into(), - FnSig::new( - [Type::Float(Float::F16), Type::Float(Float::F16)].into(), - Type::Bool + asm.alloc_string("op_Equality"), + asm.sig( + [Type::Float(Float::F16), Type::Float(Float::F16)], + Type::Bool, ), - true, - ), - [operand_a, operand_b] - ), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } _ => panic!("Can't eq type {ty_a:?}"), } } @@ -98,33 +97,29 @@ pub fn lt_unchecked( //return CILOp::Lt; match ty_a.kind() { TyKind::Uint(uint) => match uint { - UintTy::U128 => call!( - MethodRef::new( + UintTy::U128 => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "op_LessThan".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Bool - ), - true, - ), - [operand_a, operand_b] - ), + asm.alloc_string("op_LessThan"), + asm.sig([Type::Int(Int::U128), Type::Int(Int::U128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } _ => lt_un!(operand_a, operand_b), }, TyKind::Int(int) => match int { - IntTy::I128 => call!( - MethodRef::new( + IntTy::I128 => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_LessThan".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Bool - ), - true, - ), - [operand_a, operand_b] - ), + asm.alloc_string("op_LessThan"), + asm.sig([Type::Int(Int::I128), Type::Int(Int::I128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } _ => lt!(operand_a, operand_b), }, // TODO: are chars considered signed or unsigned? @@ -132,17 +127,19 @@ pub fn lt_unchecked( lt!(operand_a, operand_b) } TyKind::RawPtr(_, _) | TyKind::FnPtr(_, _) => lt_un!(operand_a, operand_b), - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "__lttf2".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Bool + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("__lttf2"), + asm.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Bool, ), - true - ), - [operand_a, operand_b] - ), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } _ => panic!("Can't eq type {ty_a:?}"), } } @@ -154,50 +151,48 @@ pub fn gt_unchecked( ) -> CILNode { match ty_a.kind() { TyKind::Uint(uint) => match uint { - UintTy::U128 => call!( - MethodRef::new( + UintTy::U128 => { + let mref = MethodRef::new( ClassRef::uint_128(asm), - "op_GreaterThan".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Bool - ), - true, - ), - [operand_a, operand_b] - ), + asm.alloc_string("op_GreaterThan"), + asm.sig([Type::Int(Int::U128), Type::Int(Int::U128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } _ => gt_un!(operand_a, operand_b), }, TyKind::Int(int) => match int { - IntTy::I128 => call!( - MethodRef::new( + IntTy::I128 => { + let mref = MethodRef::new( ClassRef::int_128(asm), - "op_GreaterThan".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Bool - ), - true, - ), - [operand_a, operand_b] - ), + asm.alloc_string("op_GreaterThan"), + asm.sig([Type::Int(Int::I128), Type::Int(Int::I128)], Type::Bool), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } _ => gt!(operand_a, operand_b), }, // TODO: are chars considered signed or unsigned? TyKind::Bool | TyKind::Char | TyKind::Float(FloatTy::F32 | FloatTy::F64) => { gt!(operand_a, operand_b) } - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "__gttf2".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Bool + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("__gttf2"), + asm.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Bool, ), - true - ), - [operand_a, operand_b] - ), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [operand_a, operand_b]) + } TyKind::RawPtr(_, _) => gt_un!(operand_a, operand_b), _ => panic!("Can't eq type {ty_a:?}"), } diff --git a/src/place/adress.rs b/src/place/adress.rs index 63f07ff6..701e1540 100644 --- a/src/place/adress.rs +++ b/src/place/adress.rs @@ -6,11 +6,10 @@ use crate::{ }; use cilly::{ call, - call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, ldc_u32, ldc_u64, size_of, - v2::{FieldDesc, FnSig, Int}, + v2::{cilnode::MethodKind, FieldDesc, FnSig, Int, MethodRef}, Type, }; use rustc_middle::{ @@ -235,19 +234,16 @@ pub fn place_elem_adress<'tcx>( let element_type = ctx.type_from_cache(element); let array_type = ctx.type_from_cache(curr_ty); let array_dotnet = array_type.as_class_ref().expect("Non array type"); - - call!( - MethodRefIdx::new( - Some(array_dotnet), - "get_Address".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize)].into(), - ctx.nptr(element_type), - ), - false, - ), - [addr_calc, index] - ) + let arr_ref = ctx.nref(array_type); + let element_ptr = ctx.nptr(element_type); + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("get_Address"), + ctx.sig([arr_ref, Type::Int(Int::USize)], element_ptr), + MethodKind::Instance, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [addr_calc, index]) } _ => { rustc_middle::ty::print::with_no_trimmed_paths! {todo!("Can't index into {curr_ty}!")} @@ -360,19 +356,20 @@ pub fn place_elem_adress<'tcx>( let element = ctx.type_from_cache(element_ty); let array_type = ctx.type_from_cache(curr_ty); let array_dotnet = array_type.as_class_ref().expect("Non array type"); + let arr_ref = ctx.nref(array_type); + let element_ptr = ctx.nptr(element); + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("get_Address"), + ctx.sig([arr_ref, Type::Int(Int::USize)], element_ptr), + MethodKind::Instance, + vec![].into(), + ); if *from_end { todo!("Can't index array from end!"); } else { call!( - MethodRefIdx::new( - Some(array_dotnet), - "get_Address".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize)].into(), - ctx.nptr(element), - ), - false, - ), + ctx.alloc_methodref(mref), [ addr_calc, CILNode::ZeroExtendToUSize(ldc_u64!(*offset).into()), diff --git a/src/place/body.rs b/src/place/body.rs index cea74aac..0f0c9604 100644 --- a/src/place/body.rs +++ b/src/place/body.rs @@ -8,11 +8,10 @@ use crate::{ }; use cilly::{ call, - call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, size_of, - v2::{FieldDesc, FnSig, Int}, + v2::{cilnode::MethodKind, FieldDesc, FnSig, Int, MethodRef}, Type, }; use rustc_middle::mir::PlaceElem; @@ -111,6 +110,77 @@ fn body_field<'a>( } } } +pub fn place_elem_body_index<'tcx>( + place_elem: &PlaceElem<'tcx>, + curr_ty: Ty<'tcx>, + ctx: &mut MethodCompileCtx<'tcx, '_>, + parrent_node: CILNode, + index: rustc_middle::mir::Local, +) -> (PlaceTy<'tcx>, CILNode) { + let index = crate::place::local_get(index.as_usize(), ctx.body()); + match curr_ty.kind() { + TyKind::Slice(inner) => { + let inner = ctx.monomorphize(*inner); + let inner_type = ctx.type_from_cache(inner); + let slice = fat_ptr_to(Ty::new_slice(ctx.tcx(), inner), ctx); + let desc = FieldDesc::new( + slice, + ctx.alloc_string(crate::DATA_PTR), + ctx.nptr(Type::Void), + ); + let addr = ld_field!(parrent_node, ctx.alloc_field(desc)) + .cast_ptr(ctx.nptr(inner_type)) + + (index * CILNode::ZeroExtendToUSize(size_of!(inner_type).into())); + + if body_ty_is_by_adress(inner, ctx) { + (inner.into(), addr) + } else { + ( + inner.into(), + super::deref_op(super::PlaceTy::Ty(inner), ctx, addr), + ) + } + } + TyKind::Array(element, _length) => { + let element = ctx.monomorphize(*element); + let element_type = ctx.type_from_cache(element); + let array_type = ctx.type_from_cache(curr_ty); + let array_dotnet = array_type.as_class_ref().expect("Non array type"); + let arr_ref = ctx.nref(array_type); + if body_ty_is_by_adress(element, ctx) { + let elem_ptr = ctx.nptr(element_type); + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("get_Address"), + ctx.sig([arr_ref, Type::Int(Int::USize)], elem_ptr), + MethodKind::Instance, + vec![].into(), + ); + let ops = call!( + ctx.alloc_methodref(mref), + [parrent_node, CILNode::ZeroExtendToUSize(index.into())] + ); + ((element).into(), ops) + } else { + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("get_Item"), + ctx.sig([arr_ref, Type::Int(Int::USize)], element_type), + MethodKind::Instance, + vec![].into(), + ); + let ops = call!( + ctx.alloc_methodref(mref), + [parrent_node, CILNode::ZeroExtendToUSize(index.into())] + ); + ((element).into(), ops) + } + } + _ => { + rustc_middle::ty::print::with_no_trimmed_paths! {todo!("Can't index into {curr_ty}!")} + } + } +} pub fn place_elem_body<'tcx>( place_elem: &PlaceElem<'tcx>, curr_type: PlaceTy<'tcx>, @@ -148,74 +218,15 @@ pub fn place_elem_body<'tcx>( (variant_type, parrent_node) } - PlaceElem::Index(index) => { - let curr_ty = curr_ty + PlaceElem::Index(index) => place_elem_body_index( + place_elem, + curr_type .as_ty() - .expect("INVALID PLACE: Indexing into enum variant???"); - let index = crate::place::local_get(index.as_usize(), ctx.body()); - match curr_ty.kind() { - TyKind::Slice(inner) => { - let inner = ctx.monomorphize(*inner); - let inner_type = ctx.type_from_cache(inner); - let slice = fat_ptr_to(Ty::new_slice(ctx.tcx(), inner), ctx); - let desc = FieldDesc::new( - slice, - ctx.alloc_string(crate::DATA_PTR), - ctx.nptr(Type::Void), - ); - let addr = ld_field!(parrent_node, ctx.alloc_field(desc)) - .cast_ptr(ctx.nptr(inner_type)) - + (index * CILNode::ZeroExtendToUSize(size_of!(inner_type).into())); - - if body_ty_is_by_adress(inner, ctx) { - (inner.into(), addr) - } else { - ( - inner.into(), - super::deref_op(super::PlaceTy::Ty(inner), ctx, addr), - ) - } - } - TyKind::Array(element, _length) => { - let element = ctx.monomorphize(*element); - let element_type = ctx.type_from_cache(element); - let array_type = ctx.type_from_cache(curr_ty); - let array_dotnet = array_type.as_class_ref().expect("Non array type"); - if body_ty_is_by_adress(element, ctx) { - let ops = call!( - MethodRefIdx::new( - Some(array_dotnet), - "get_Address".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize)].into(), - ctx.nptr(element_type), - ), - false, - ), - [parrent_node, CILNode::ZeroExtendToUSize(index.into())] - ); - ((element).into(), ops) - } else { - let ops = call!( - MethodRefIdx::new( - Some(array_dotnet), - "get_Item".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize)].into(), - element_type, - ), - false, - ), - [parrent_node, CILNode::ZeroExtendToUSize(index.into())] - ); - ((element).into(), ops) - } - } - _ => { - rustc_middle::ty::print::with_no_trimmed_paths! {todo!("Can't index into {curr_ty}!")} - } - } - } + .expect("INVALID PLACE: Indexing into enum variant???"), + ctx, + parrent_node, + *index, + ), PlaceElem::ConstantIndex { offset, min_length: _, @@ -254,31 +265,31 @@ pub fn place_elem_body<'tcx>( let element = ctx.type_from_cache(element_ty); let array_type = ctx.type_from_cache(curr_ty); let array_dotnet = array_type.as_class_ref().expect("Non array type"); + let arr_ref = ctx.nref(array_type); if body_ty_is_by_adress(element_ty, ctx) { + let elem_ptr = ctx.nptr(element); + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("get_Address"), + ctx.sig([arr_ref, Type::Int(Int::USize)], elem_ptr), + MethodKind::Instance, + vec![].into(), + ); let ops = call!( - MethodRefIdx::new( - Some(array_dotnet), - "get_Address".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize)].into(), - ctx.nptr(element), - ), - false, - ), + ctx.alloc_methodref(mref), [parrent_node, CILNode::ZeroExtendToUSize(index.into())] ); ((element_ty).into(), ops) } else { + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("get_Item"), + ctx.sig([arr_ref, Type::Int(Int::USize)], element), + MethodKind::Instance, + vec![].into(), + ); let ops = call!( - MethodRefIdx::new( - Some(array_dotnet), - "get_Item".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize)].into(), - element - ), - false, - ), + ctx.alloc_methodref(mref), [parrent_node, CILNode::ZeroExtendToUSize(index.into())] ); ((element_ty).into(), ops) diff --git a/src/terminator/call.rs b/src/terminator/call.rs index 6c7b0d4d..50a6a2f7 100644 --- a/src/terminator/call.rs +++ b/src/terminator/call.rs @@ -63,9 +63,10 @@ fn call_managed<'tcx>( MethodKind::Static, vec![].into(), ); + let call_site = ctx.alloc_methodref(call_site); if *signature.output() == cilly::Type::Void { CILRoot::Call { - site: ctx.alloc_methodref(call_site), + site: call_site, args: [].into(), } } else { @@ -81,7 +82,7 @@ fn call_managed<'tcx>( let call = MethodRef::new( (ctx.alloc_class_ref(tpe)), ctx.alloc_string(managed_fn_name), - ctx.alloc_sig(signature), + ctx.alloc_sig(signature.clone()), if is_static { MethodKind::Static } else { @@ -89,9 +90,10 @@ fn call_managed<'tcx>( }, vec![].into(), ); + let call = ctx.alloc_methodref(call); if *signature.output() == cilly::Type::Void { CILRoot::Call { - site: ctx.alloc_methodref(call), + site: call, args: call_args.into(), } } else { @@ -135,9 +137,10 @@ fn callvirt_managed<'tcx>( MethodKind::Static, vec![].into(), ); + let call = ctx.alloc_methodref(call); if *signature.output() == cilly::Type::Void { CILRoot::CallVirt { - site: ctx.alloc_methodref(call), + site: call, args: [].into(), } } else { @@ -153,7 +156,7 @@ fn callvirt_managed<'tcx>( let call = MethodRef::new( (ctx.alloc_class_ref(tpe)), ctx.alloc_string(managed_fn_name), - ctx.alloc_sig(signature), + ctx.alloc_sig(signature.clone()), if is_static { MethodKind::Static } else { @@ -314,10 +317,10 @@ pub fn call_closure<'tcx>( vec![].into(), ); // Hande the call itself - + let call = ctx.alloc_methodref(call); if is_void { CILRoot::Call { - site: ctx.alloc_methodref(call), + site: call, args: call_args.into(), } } else { @@ -593,13 +596,14 @@ pub fn call<'tcx>( vec![].into(), ); // Hande + let site = ctx.alloc_methodref(call_site); if is_void { CILRoot::Call { - site: ctx.alloc_methodref(call_site), + site, args: call_args.into(), } } else { - let res_calc = call!(call_site, call_args); + let res_calc = call!(site, call_args); crate::place::place_set(destination, res_calc, ctx) } } diff --git a/src/type/type.rs b/src/type/type.rs index ab96b172..afcec50b 100644 --- a/src/type/type.rs +++ b/src/type/type.rs @@ -22,16 +22,16 @@ pub struct DotnetArray { #[must_use] pub fn max_value(tpe: &Type, asm: &mut Assembly) -> CILNode { match tpe { - Type::Int(Int::USize) => call!( - MethodRef::new( + Type::Int(Int::USize) => { + let mref = MethodRef::new( ClassRef::usize_type(asm), asm.alloc_string("get_MaxValue"), asm.sig(([]), Type::Int(Int::USize)), MethodKind::Static, - vec![].into() - ), - [] - ), + vec![].into(), + ); + call!(asm.alloc_methodref(mref), []) + } Type::Int(Int::U64) => ldc_u64!(u64::MAX), Type::Int(Int::U32) => ldc_u32!(u32::MAX), _ => todo!("Can't get the max value of {tpe:?}"), diff --git a/src/utilis/adt.rs b/src/utilis/adt.rs index c07a5b96..0f62334d 100644 --- a/src/utilis/adt.rs +++ b/src/utilis/adt.rs @@ -335,7 +335,7 @@ pub fn get_discr<'tcx>( vec![].into(), ); call!( - mref, + ctx.alloc_methodref(mref), [ tag, CILNode::const_i128( @@ -387,32 +387,38 @@ pub fn get_discr<'tcx>( ), }; let gt = match tag_tpe { - Type::Int(Int::U128) => call!( - MethodRef::new( + Type::Int(Int::U128) => { + let mref = MethodRef::new( ClassRef::uint_128(ctx), ctx.alloc_string("op_GreaterThan"), - ctx.sig(([Type::Int(Int::U128), Type::Int(Int::U128)]), Type::Bool), + ctx.sig([Type::Int(Int::U128), Type::Int(Int::U128)], Type::Bool), MethodKind::Static, - vec![].into() - ), - [ - relative_discr.clone(), - CILNode::const_u128(u128::from(relative_max), ctx) - ] - ), - Type::Int(Int::I128) => call!( - MethodRef::new( + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), + [ + relative_discr.clone(), + CILNode::const_u128(u128::from(relative_max), ctx) + ] + ) + } + Type::Int(Int::I128) => { + let mref = MethodRef::new( ClassRef::int_128(ctx), ctx.alloc_string("op_GreaterThan"), - ctx.sig(([Type::Int(Int::I128), Type::Int(Int::I128)]), Type::Bool), + ctx.sig([Type::Int(Int::I128), Type::Int(Int::I128)], Type::Bool), MethodKind::Static, - vec![].into() - ), - [ - relative_discr.clone(), - CILNode::const_i128(u128::from(relative_max), ctx) - ] - ), + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), + [ + relative_discr.clone(), + CILNode::const_i128(u128::from(relative_max), ctx) + ] + ) + } _ => gt_un!( relative_discr.clone(), From 5d547f19faca218e18e1160f77feecf283e404b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Wed, 2 Oct 2024 01:03:29 +0200 Subject: [PATCH 03/10] Some more work on porting to the new method references. Refactored certain intrisniscs --- cilly/src/lib.rs | 2 +- cilly/src/v2/class.rs | 24 + cilly/src/v2/int.rs | 67 +- src/binop/mod.rs | 379 +++++------ src/binop/shift.rs | 167 ++--- src/place/get.rs | 39 +- src/place/set.rs | 60 +- src/terminator/intrinsics/bswap.rs | 20 +- src/terminator/intrinsics/ints.rs | 974 ++++++++--------------------- 9 files changed, 694 insertions(+), 1038 deletions(-) diff --git a/cilly/src/lib.rs b/cilly/src/lib.rs index 00509444..49c95b4d 100644 --- a/cilly/src/lib.rs +++ b/cilly/src/lib.rs @@ -54,12 +54,12 @@ pub mod cil_node; pub mod cil_root; pub mod cil_tree; pub mod entrypoint; - pub mod libc_fns; pub mod method; pub mod utilis; pub mod v2; +pub use v2::*; #[must_use] pub fn mem_checks() -> bool { diff --git a/cilly/src/v2/class.rs b/cilly/src/v2/class.rs index 57ddd902..fe9acf23 100644 --- a/cilly/src/v2/class.rs +++ b/cilly/src/v2/class.rs @@ -135,6 +135,30 @@ impl ClassRef { let asm_name = Some(asm.alloc_string("System.Runtime")); asm.alloc_class_ref(ClassRef::new(name, asm_name, true, [].into())) } + /// Returns a reference to the `System.UInt32` type. + pub fn uint32(asm: &mut Assembly) -> ClassRefIdx { + let name = asm.alloc_string("System.UInt32"); + let asm_name = Some(asm.alloc_string("System.Runtime")); + asm.alloc_class_ref(ClassRef::new(name, asm_name, true, [].into())) + } + /// Returns a reference to the `System.Int32` type. + pub fn int32(asm: &mut Assembly) -> ClassRefIdx { + let name = asm.alloc_string("System.Int32"); + let asm_name = Some(asm.alloc_string("System.Runtime")); + asm.alloc_class_ref(ClassRef::new(name, asm_name, true, [].into())) + } + /// Returns a reference to the `System.UInt64` type. + pub fn uint64(asm: &mut Assembly) -> ClassRefIdx { + let name = asm.alloc_string("System.UInt64"); + let asm_name = Some(asm.alloc_string("System.Runtime")); + asm.alloc_class_ref(ClassRef::new(name, asm_name, true, [].into())) + } + /// Returns a reference to the `System.Int64` type. + pub fn int64(asm: &mut Assembly) -> ClassRefIdx { + let name = asm.alloc_string("System.Int64"); + let asm_name = Some(asm.alloc_string("System.Runtime")); + asm.alloc_class_ref(ClassRef::new(name, asm_name, true, [].into())) + } /// Returns a reference to the `System.IntPtr` type. pub fn isize_type(asm: &mut Assembly) -> ClassRefIdx { let name = asm.alloc_string("System.IntPtr"); diff --git a/cilly/src/v2/int.rs b/cilly/src/v2/int.rs index 4cdd6fa2..083bf2ec 100644 --- a/cilly/src/v2/int.rs +++ b/cilly/src/v2/int.rs @@ -25,6 +25,56 @@ impl From for Type { } } impl Int { + /// Returns a reference to a class representing this type. + pub fn class(&self, asm: &mut Assembly) -> ClassRefIdx { + match self { + Int::U8 => ClassRef::byte(asm), + Int::U16 => ClassRef::uint16(asm), + Int::U32 => ClassRef::uint32(asm), + Int::U64 => ClassRef::uint64(asm), + Int::U128 => ClassRef::uint_128(asm), + Int::USize => ClassRef::usize_type(asm), + Int::I8 => ClassRef::sbyte(asm), + Int::I16 => ClassRef::int16(asm), + Int::I32 => ClassRef::int32(asm), + Int::I64 => ClassRef::int64(asm), + Int::I128 => ClassRef::int_128(asm), + Int::ISize => ClassRef::isize_type(asm), + } + } + /// Returns the unsigned version of this type. + /// ``` + /// # use cilly::Int; + /// assert_eq!(Int::I8.as_unsigned(),Int::U8); + /// assert_eq!(Int::I128.as_unsigned(),Int::U128.as_unsigned()); + /// ``` + pub fn as_unsigned(&self) -> Self { + match self { + Int::I8 | Int::U8 => Int::U8, + Int::I16 | Int::U16 => Int::U16, + Int::I32 | Int::U32 => Int::U32, + Int::I64 | Int::U64 => Int::U64, + Int::I128 | Int::U128 => Int::U128, + Int::ISize | Int::USize => Int::USize, + } + } + /// Returns the signed version of this type. + /// ``` + /// # use cilly::Int; + /// assert_eq!(Int::U8.as_signed(),Int::I8); + /// assert_eq!(Int::U128.as_signed(),Int::I128.as_signed()); + /// ``` + pub fn as_signed(&self) -> Self { + match self { + Int::I8 | Int::U8 => Int::I8, + Int::I16 | Int::U16 => Int::I16, + Int::I32 | Int::U32 => Int::I32, + Int::I64 | Int::U64 => Int::I64, + Int::I128 | Int::U128 => Int::I128, + Int::ISize | Int::USize => Int::ISize, + } + } + /// Returns the minimum value of this int. pub fn min(&self, asm: &mut Assembly) -> CILNode { match self { @@ -208,23 +258,6 @@ impl Int { Int::USize | Int::ISize => None, } } - /// Returns a class representing this intiger. - pub fn class(&self, asm: &mut Assembly) -> ClassRefIdx { - match self { - Int::U8 => todo!(), - Int::U16 => todo!(), - Int::U32 => todo!(), - Int::U64 => todo!(), - Int::U128 => ClassRef::uint_128(asm), - Int::USize => ClassRef::usize_type(asm), - Int::I8 => todo!(), - Int::I16 => todo!(), - Int::I32 => todo!(), - Int::I64 => todo!(), - Int::I128 => ClassRef::int_128(asm), - Int::ISize => ClassRef::isize_type(asm), - } - } } #[test] fn is_signed() { diff --git a/src/binop/mod.rs b/src/binop/mod.rs index 361af507..4efdc893 100644 --- a/src/binop/mod.rs +++ b/src/binop/mod.rs @@ -3,12 +3,11 @@ use crate::assembly::MethodCompileCtx; use bitop::{bit_and_unchecked, bit_or_unchecked, bit_xor_unchecked}; use cilly::{ call, - call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_i8, conv_u16, conv_u32, conv_u64, conv_u8, div, eq, gt_un, ld_false, lt_un, rem, rem_un, size_of, sub, - v2::{FieldDesc, Float, FnSig, Int}, + v2::{cilnode::MethodKind, FieldDesc, Float, Int, MethodRef}, Type, }; use cmp::{eq_unchecked, gt_unchecked, lt_unchecked, ne_unchecked}; @@ -72,33 +71,37 @@ pub(crate) fn binop<'tcx>( BinOp::Ge => match ty_a.kind() { // Unordered, to handle NaNs propely TyKind::Float(FloatTy::F32 | FloatTy::F64) => eq!(lt_un!(ops_a, ops_b), ld_false!()), - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "__getf2".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Bool + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("__getf2"), + ctx.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Bool, ), - true - ), - [ops_a, ops_b] - ), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) + } _ => eq!(lt_unchecked(ty_a, ops_a, ops_b, ctx), ld_false!()), }, BinOp::Le => match ty_a.kind() { // Unordered, to handle NaNs propely TyKind::Float(FloatTy::F32 | FloatTy::F64) => eq!(gt_un!(ops_a, ops_b), ld_false!()), - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "__letf2".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Bool + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("__letf2"), + ctx.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Bool, ), - true - ), - [ops_a, ops_b] - ), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) + } _ => eq!(gt_unchecked(ty_a, ops_a, ops_b, ctx), ld_false!()), }, BinOp::Offset => { @@ -163,34 +166,34 @@ pub fn add_unchecked<'tcx>( match ty_a.kind() { TyKind::Int(int_ty) => { if let IntTy::I128 = int_ty { - call!( - MethodRefIdx::builtin( - "add_i128".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Int(Int::I128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("add_i128"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I128)], + Type::Int(Int::I128), ), - [ops_a, ops_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) } else { ops_a + ops_b } } TyKind::Uint(uint_ty) => { if let UintTy::U128 = uint_ty { - call!( - MethodRefIdx::builtin( - "add_u128".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Int(Int::U128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("add_u128"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::U128)], + Type::Int(Int::U128), ), - [ops_a, ops_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) } else { match uint_ty { UintTy::U8 => conv_u8!(ops_a + ops_b), @@ -202,28 +205,32 @@ pub fn add_unchecked<'tcx>( } } TyKind::Float(FloatTy::F32 | FloatTy::F64) => ops_a + ops_b, - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "__addtf3".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Float(Float::F128) + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("__addtf3"), + ctx.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Float(Float::F128), ), - true - ), - [ops_a, ops_b,] - ), - TyKind::Float(FloatTy::F16) => call!( - MethodRefIdx::builtin( - "add_f16".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Float(Float::F128) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b,]) + } + TyKind::Float(FloatTy::F16) => { + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("add_f16"), + ctx.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Float(Float::F128), ), - true - ), - [ops_a, ops_b,] - ), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b,]) + } _ => todo!("can't add numbers of types {ty_a} and {ty_b}"), } } @@ -238,50 +245,52 @@ pub fn sub_unchecked<'tcx>( match ty_a.kind() { TyKind::Int(int_ty) => { if let IntTy::I128 = int_ty { - call!( - MethodRefIdx::builtin( - "sub_i128".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Int(Int::I128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("sub_i128"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I128)], + Type::Int(Int::I128), ), - [ops_a, ops_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) } else { sub!(ops_a, ops_b) } } TyKind::Uint(uint_ty) => { if let UintTy::U128 = uint_ty { - call!( - MethodRefIdx::builtin( - "sub_u128".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Int(Int::U128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("sub_u128"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::U128)], + Type::Int(Int::U128), ), - [ops_a, ops_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) } else { sub!(ops_a, ops_b) } } TyKind::Float(FloatTy::F32 | FloatTy::F64) => sub!(ops_a, ops_b), - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "__subtf3".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Float(Float::F128) + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("__subtf3"), + ctx.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Float(Float::F128), ), - true - ), - [ops_a, ops_b,] - ), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) + } _ => todo!("can't sub numbers of types {ty_a} and {ty_b}"), } } @@ -295,45 +304,47 @@ fn rem_unchecked<'tcx>( ) -> CILNode { match ty_a.kind() { TyKind::Int(IntTy::I128) => { - call!( - MethodRefIdx::builtin( - "mod_i128".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Int(Int::I128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("mod_i128"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I128)], + Type::Int(Int::I128), ), - [ops_a, ops_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) } TyKind::Uint(UintTy::U128) => { - call!( - MethodRefIdx::builtin( - "mod_u128".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Int(Int::U128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("mod_u128"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::U128)], + Type::Int(Int::U128), ), - [ops_a, ops_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) } TyKind::Int(_) | TyKind::Char | TyKind::Float(FloatTy::F32 | FloatTy::F64) => { rem!(ops_a, ops_b) } - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "fmodl".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Float(Float::F128) + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("fmodl"), + ctx.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Float(Float::F128), ), - true - ), - [ops_a, ops_b] - ), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [ops_a, ops_b]) + } TyKind::Uint(_) => rem_un!(ops_a, ops_b), _ => todo!(), @@ -349,42 +360,44 @@ fn mul_unchecked<'tcx>( ) -> CILNode { match ty_a.kind() { TyKind::Int(IntTy::I128) => { - call!( - MethodRefIdx::builtin( - "mul_i128".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Int(Int::I128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("mul_i128"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I128)], + Type::Int(Int::I128), ), - [operand_a, operand_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [operand_a, operand_b]) } TyKind::Uint(UintTy::U128) => { - call!( - MethodRefIdx::builtin( - "mul_u128".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Int(Int::U128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("mul_u128"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::U128)], + Type::Int(Int::U128), ), - [operand_a, operand_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [operand_a, operand_b]) } - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "__multf3".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Float(Float::F128) + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("__multf3"), + ctx.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Float(Float::F128), ), - true - ), - [operand_a, operand_b] - ), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [operand_a, operand_b]) + } _ => operand_a * operand_b, } } @@ -397,46 +410,48 @@ fn div_unchecked<'tcx>( ) -> CILNode { match ty_a.kind() { TyKind::Int(IntTy::I128) => { - call!( - MethodRefIdx::builtin( - "div_i128".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I128)].into(), - Type::Int(Int::I128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("div_i128"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I128)], + Type::Int(Int::I128), ), - [operand_a, operand_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [operand_a, operand_b]) } TyKind::Uint(UintTy::U128) => { - call!( - MethodRefIdx::builtin( - "div_u128".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::U128)].into(), - Type::Int(Int::U128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("div_u128"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::U128)], + Type::Int(Int::U128), ), - [operand_a, operand_b] - ) + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [operand_a, operand_b]) } TyKind::Uint(_) => CILNode::DivUn(operand_a.into(), operand_b.into()), TyKind::Int(_) | TyKind::Char | TyKind::Float(FloatTy::F32 | FloatTy::F64) => { div!(operand_a, operand_b) } - TyKind::Float(FloatTy::F128) => call!( - MethodRefIdx::builtin( - "__divtf3".into(), - FnSig::new( - [Type::Float(Float::F128), Type::Float(Float::F128)].into(), - Type::Float(Float::F128) + TyKind::Float(FloatTy::F128) => { + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("__divtf3"), + ctx.sig( + [Type::Float(Float::F128), Type::Float(Float::F128)], + Type::Float(Float::F128), ), - true - ), - [operand_a, operand_b] - ), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [operand_a, operand_b]) + } _ => todo!(), } } diff --git a/src/binop/shift.rs b/src/binop/shift.rs index 0ca62a2a..d67a64cd 100644 --- a/src/binop/shift.rs +++ b/src/binop/shift.rs @@ -3,10 +3,9 @@ use crate::utilis::compiletime_sizeof; use cilly::{ call, - call_site::MethodRefIdx, cil_node::CILNode, conv_i32, conv_u32, ldc_u32, rem_un, shl, shr, shr_un, - v2::{ClassRef, FnSig, Int}, + v2::{cilnode::MethodKind, ClassRef, FnSig, Int, MethodRef}, Type, }; @@ -21,16 +20,18 @@ pub fn shr_unchecked<'tcx>( let type_b = ctx.type_from_cache(shift_type); match value_type.kind() { TyKind::Uint(UintTy::U128) => { - call!( - MethodRefIdx::boxed( - ClassRef::uint_128(ctx).into(), - "op_RightShift".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::I32)].into(), - Type::Int(Int::U128) - ), - true, + let mref = MethodRef::new( + ClassRef::uint_128(ctx).into(), + ctx.alloc_string("op_RightShift"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::I32)], + Type::Int(Int::U128), ), + MethodKind::Static, + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), [ ops_a, crate::casts::int_to_int(type_b, Type::Int(Int::I32), ops_b, ctx) @@ -38,16 +39,18 @@ pub fn shr_unchecked<'tcx>( ) } TyKind::Int(IntTy::I128) => { - call!( - MethodRefIdx::boxed( - ClassRef::int_128(ctx).into(), - "op_RightShift".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I32)].into(), - Type::Int(Int::I128) - ), - true, + let mref = MethodRef::new( + ClassRef::int_128(ctx).into(), + ctx.alloc_string("op_RightShift"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I32)], + Type::Int(Int::I128), ), + MethodKind::Static, + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), [ ops_a, crate::casts::int_to_int(type_b, Type::Int(Int::I32), ops_b, ctx) @@ -89,16 +92,18 @@ pub fn shr_checked<'tcx>( .expect("Intiger size over 2^32 bits."); match value_type.kind() { TyKind::Uint(UintTy::U128) => { - call!( - MethodRefIdx::boxed( - ClassRef::uint_128(ctx).into(), - "op_RightShift".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::I32)].into(), - Type::Int(Int::U128) - ), - true, + let mref = MethodRef::new( + ClassRef::uint_128(ctx).into(), + ctx.alloc_string("op_RightShift"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::I32)], + Type::Int(Int::U128), ), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!( + ctx.alloc_methodref(mref), [ ops_a, conv_i32!(rem_un!( @@ -106,19 +111,22 @@ pub fn shr_checked<'tcx>( ldc_u32!(128) )) ] - ) + ); + cilnode } TyKind::Int(IntTy::I128) => { - call!( - MethodRefIdx::boxed( - ClassRef::int_128(ctx).into(), - "op_RightShift".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I32)].into(), - Type::Int(Int::I128) - ), - true, + let mref = MethodRef::new( + ClassRef::int_128(ctx).into(), + ctx.alloc_string("op_RightShift"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I32)], + Type::Int(Int::I128), ), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!( + ctx.alloc_methodref(mref), [ ops_a, conv_i32!(rem_un!( @@ -126,7 +134,8 @@ pub fn shr_checked<'tcx>( ldc_u32!(128) )) ] - ) + ); + cilnode } TyKind::Uint(_) => match shift_type.kind() { TyKind::Uint(UintTy::U128 | UintTy::U64) | TyKind::Int(IntTy::I128 | IntTy::I64) => { @@ -182,15 +191,18 @@ pub fn shl_checked<'tcx>( .expect("Intiger has over 2^32 bits."); match value_type.kind() { TyKind::Uint(UintTy::U128) => { - call!( - MethodRefIdx::builtin( - "shl_u128".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::I32)].into(), - Type::Int(Int::U128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("shl_u128"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::I32)], + Type::Int(Int::U128), ), + MethodKind::Static, + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), [ ops_a, conv_i32!(rem_un!( @@ -206,15 +218,18 @@ pub fn shl_checked<'tcx>( ) } TyKind::Int(IntTy::I128) => { - call!( - MethodRefIdx::builtin( - "shl_i128".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I32)].into(), - Type::Int(Int::I128) - ), - true + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("shl_i128"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I32)], + Type::Int(Int::I128), ), + MethodKind::Static, + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), [ ops_a, conv_i32!(rem_un!( @@ -282,16 +297,18 @@ pub fn shl_unchecked<'tcx>( let type_b = ctx.type_from_cache(shift_type); match value_type.kind() { TyKind::Uint(UintTy::U128) => { - call!( - MethodRefIdx::boxed( - ClassRef::uint_128(ctx).into(), - "op_LeftShift".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::I32)].into(), - Type::Int(Int::U128) - ), - true, + let mref = MethodRef::new( + ClassRef::uint_128(ctx).into(), + ctx.alloc_string("op_LeftShift"), + ctx.sig( + [Type::Int(Int::U128), Type::Int(Int::I32)], + Type::Int(Int::U128), ), + MethodKind::Static, + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), [ ops_a, crate::casts::int_to_int(type_b, Type::Int(Int::I32), ops_b, ctx) @@ -299,16 +316,18 @@ pub fn shl_unchecked<'tcx>( ) } TyKind::Int(IntTy::I128) => { - call!( - MethodRefIdx::boxed( - ClassRef::int_128(ctx).into(), - "op_LeftShift".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I32)].into(), - Type::Int(Int::I128) - ), - true, + let mref = MethodRef::new( + ClassRef::int_128(ctx).into(), + ctx.alloc_string("op_LeftShift"), + ctx.sig( + [Type::Int(Int::I128), Type::Int(Int::I32)], + Type::Int(Int::I128), ), + MethodKind::Static, + vec![].into(), + ); + call!( + ctx.alloc_methodref(mref), [ ops_a, crate::casts::int_to_int(type_b, Type::Int(Int::I32), ops_b, ctx) diff --git a/src/place/get.rs b/src/place/get.rs index 94b3a8bf..849bb58a 100644 --- a/src/place/get.rs +++ b/src/place/get.rs @@ -1,10 +1,9 @@ use crate::{assembly::MethodCompileCtx, r#type::fat_ptr_to}; use cilly::{ call, - call_site::MethodRefIdx, cil_node::CILNode, conv_usize, ld_field, ldc_u32, ldc_u64, - v2::{FieldDesc, FnSig, Int}, + v2::{cilnode::MethodKind, FieldDesc, FnSig, Int, MethodRef}, Type, }; use rustc_middle::{ @@ -157,16 +156,16 @@ fn place_elem_get<'a>( let element = ctx.type_from_cache(element); let array_type = ctx.type_from_cache(curr_ty); let array_dotnet = array_type.as_class_ref().expect("Non array type"); + let arr_ref = ctx.nref(array_type); + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("get_Item"), + ctx.sig([arr_ref, Type::Int(Int::USize)], element), + MethodKind::Instance, + vec![].into(), + ); call!( - MethodRefIdx::new( - Some(array_dotnet), - "get_Item".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize)].into(), - element - ), - false, - ), + ctx.alloc_methodref(mref), [addr_calc, CILNode::ZeroExtendToUSize(index.into())] ) } @@ -222,20 +221,20 @@ fn place_elem_get<'a>( let array_type = ctx.type_from_cache(curr_ty); let array_dotnet = array_type.as_class_ref().expect("Non array type"); //eprintln!("WARNING: ConstantIndex has required min_length of {min_length}, but bounds checking on const access not supported yet!"); + let arr_ref = ctx.nref(array_type); if *from_end { todo!("Can't index array from end!"); } else { let index = CILNode::LdcU64(*offset); + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("get_Item"), + ctx.sig([arr_ref, Type::Int(Int::USize)], element), + MethodKind::Instance, + vec![].into(), + ); call!( - MethodRefIdx::new( - Some(array_dotnet), - "get_Item".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize)].into(), - element - ), - false, - ), + ctx.alloc_methodref(mref), [addr_calc, CILNode::ZeroExtendToUSize(index.into())] ) } diff --git a/src/place/set.rs b/src/place/set.rs index 2d6180ba..6e26c82a 100644 --- a/src/place/set.rs +++ b/src/place/set.rs @@ -5,11 +5,10 @@ use crate::{ }; use cilly::{ call, - call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, ldc_u64, size_of, - v2::{ClassRef, FieldDesc, FnSig, Int}, + v2::{cilnode::MethodKind, ClassRef, FieldDesc, FnSig, Int, MethodRef}, Type, }; use rustc_middle::{ @@ -104,17 +103,16 @@ pub fn place_elem_set<'a>( let element_type = ctx.type_from_cache(element); let array_dotnet = array_type.as_class_ref().expect("Non array type"); - + let arr_ref = ctx.nref(array_type); + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("set_Item"), + ctx.sig([arr_ref, Type::Int(Int::USize), element_type], Type::Void), + MethodKind::Instance, + vec![].into(), + ); CILRoot::Call { - site: Box::new(MethodRefIdx::new( - Some(array_dotnet), - "set_Item".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize), element_type].into(), - Type::Void, - ), - false, - )), + site: ctx.alloc_methodref(mref), args: [addr_calc, index, value_calc].into(), } } @@ -151,18 +149,20 @@ pub fn place_elem_set<'a>( ctx.alloc_string(crate::METADATA), Type::Int(Int::USize), ); + let mref = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("bounds_check"), + ctx.sig( + [Type::Int(Int::USize), Type::Int(Int::USize)], + Type::Int(Int::USize), + ), + MethodKind::Static, + vec![].into(), + ); let addr = ld_field!(addr_calc.clone(), ctx.alloc_field(desc)) .cast_ptr(ctx.nptr(inner_type)) + call!( - MethodRefIdx::new( - None, - "bounds_check".into(), - FnSig::new( - [Type::Int(Int::USize), Type::Int(Int::USize)].into(), - Type::Int(Int::USize) - ), - true - ), + ctx.alloc_methodref(mref), [ conv_usize!(index), ld_field!(addr_calc, ctx.alloc_field(metadata)), @@ -176,16 +176,16 @@ pub fn place_elem_set<'a>( let element = ctx.type_from_cache(element); let array_type = ctx.type_from_cache(curr_ty); let array_dotnet = array_type.as_class_ref().expect("Non array type"); + let arr_ref = ctx.nref(array_type); + let mref = MethodRef::new( + (array_dotnet), + ctx.alloc_string("set_Item"), + ctx.sig([arr_ref, Type::Int(Int::USize), element], Type::Void), + MethodKind::Instance, + vec![].into(), + ); CILRoot::Call { - site: Box::new(MethodRefIdx::new( - Some(array_dotnet), - "set_Item".into(), - FnSig::new( - [ctx.nref(array_type), Type::Int(Int::USize), element].into(), - Type::Void, - ), - false, - )), + site: ctx.alloc_methodref(mref), args: [addr_calc, conv_usize!(index), value_calc].into(), } } diff --git a/src/terminator/intrinsics/bswap.rs b/src/terminator/intrinsics/bswap.rs index 07933802..44dda10e 100644 --- a/src/terminator/intrinsics/bswap.rs +++ b/src/terminator/intrinsics/bswap.rs @@ -1,10 +1,9 @@ use crate::{assembly::MethodCompileCtx, operand::handle_operand, place::place_set}; use cilly::{ call, - call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, - v2::{ClassRef, FnSig}, + v2::{cilnode::MethodKind, ClassRef, MethodRef}, }; use rustc_middle::{ mir::{Operand, Place}, @@ -31,15 +30,14 @@ pub fn bswap<'tcx>( match ty.kind() { TyKind::Uint(UintTy::U8) => operand, TyKind::Uint(_) | TyKind::Int(_) => { - call!( - MethodRefIdx::boxed( - Some(ClassRef::binary_primitives(ctx)), - "ReverseEndianness".into(), - FnSig::new([tpe].into(), tpe), - true, - ), - [operand] - ) + let mref = MethodRef::new( + ClassRef::binary_primitives(ctx), + ctx.alloc_string("ReverseEndianness"), + ctx.sig([tpe], tpe), + MethodKind::Static, + vec![].into(), + ); + call!(ctx.alloc_methodref(mref), [operand]) } _ => todo!("Can't bswap {tpe:?}"), diff --git a/src/terminator/intrinsics/ints.rs b/src/terminator/intrinsics/ints.rs index 363cac70..4cd05387 100644 --- a/src/terminator/intrinsics/ints.rs +++ b/src/terminator/intrinsics/ints.rs @@ -1,19 +1,29 @@ use crate::{assembly::MethodCompileCtx, operand::handle_operand, place::place_set}; use cilly::{ and, call, - call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, - conv_i16, conv_i32, conv_i8, conv_isize, conv_u16, conv_u32, conv_u64, conv_u8, conv_usize, - div, ldc_i32, ldc_u32, ldc_u64, rem_un, size_of, sub, - v2::{ClassRef, FnSig, Int}, - Type, + conv_i16, conv_i32, conv_i8, conv_isize, conv_u16, conv_u32, conv_u64, conv_u8, div, ldc_i32, + ldc_u32, ldc_u64, rem_un, size_of, sub, + v2::{cilnode::MethodKind, ClassRef, MethodRef}, + Int, Type, }; use rustc_middle::{ mir::{Operand, Place}, ty::Instance, }; use rustc_span::source_map::Spanned; +fn ctpop_small_int(asm: &mut cilly::v2::Assembly, operand: CILNode, int: Int) -> CILNode { + assert!(int.size().is_none_or(|size| size <= 8)); + let mref = MethodRef::new( + ClassRef::bit_operations(asm), + asm.alloc_string("PopCount"), + asm.sig([Type::Int(int)], Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + ); + conv_u32!(call!(asm.alloc_methodref(mref), [operand])) +} pub fn ctpop<'tcx>( args: &[Spanned>], destination: &Place<'tcx>, @@ -33,97 +43,48 @@ pub fn ctpop<'tcx>( .expect("needs_drop works only on types!"), ), ); - let bit_operations = ClassRef::bit_operations(ctx); - let bit_operations = Some(bit_operations); let operand = handle_operand(&args[0].node, ctx); place_set( destination, match tpe { - Type::Int(Int::U64) => conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "PopCount".into(), - FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::I32)), - true, - ), - [operand] - )), - Type::Int(Int::I64) => conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "PopCount".into(), - FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::I32)), - true, - ), - [conv_u64!(operand)] - )), - Type::Int(Int::U32) => conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "PopCount".into(), - FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::I32)), - true, - ), - [operand] - )), - + Type::Int(Int::U64) => ctpop_small_int(ctx, operand, Int::U64), + Type::Int(Int::I64) => ctpop_small_int(ctx, conv_u64!(operand), Int::U64), + Type::Int(Int::U32) => ctpop_small_int(ctx, operand, Int::U32), Type::Int(Int::U8 | Int::U16 | Int::I8 | Int::I16 | Int::I32) => { - conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "PopCount".into(), - FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::I32)), - true, - ), - [conv_u32!(operand)] - )) + ctpop_small_int(ctx, conv_u32!(operand), Int::U32) + } + Type::Int(Int::USize) => ctpop_small_int(ctx, operand, Int::USize), + Type::Int(Int::ISize) => ctpop_small_int(ctx, conv_isize!(operand), Int::USize), + Type::Int(Int::U128) => { + let mref = MethodRef::new( + ClassRef::uint_128(ctx), + ctx.alloc_string("PopCount"), + ctx.sig([Type::Int(Int::U128)], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); + crate::casts::int_to_int( + Type::Int(Int::U128), + Type::Int(Int::U32), + call!(ctx.alloc_methodref(mref), [operand]), + ctx, + ) + } + Type::Int(Int::I128) => { + let mref = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("PopCount"), + ctx.sig([Type::Int(Int::I128)], Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + crate::casts::int_to_int( + Type::Int(Int::I128), + Type::Int(Int::U32), + call!(ctx.alloc_methodref(mref), [operand]), + ctx, + ) } - Type::Int(Int::USize) => conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "PopCount".into(), - FnSig::new([Type::Int(Int::USize)].into(), Type::Int(Int::I32)), - true, - ), - [operand] - )), - Type::Int(Int::ISize) => conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "PopCount".into(), - FnSig::new([Type::Int(Int::USize)].into(), Type::Int(Int::I32)), - true, - ), - [conv_isize!(operand)] - )), - Type::Int(Int::U128) => crate::casts::int_to_int( - Type::Int(Int::U128), - Type::Int(Int::U32), - call!( - MethodRefIdx::new_extern( - ClassRef::uint_128(ctx), - "PopCount".into(), - FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128)), - true, - ), - [operand] - ), - ctx, - ), - Type::Int(Int::I128) => crate::casts::int_to_int( - Type::Int(Int::I128), - Type::Int(Int::U32), - call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "PopCount".into(), - FnSig::new([Type::Int(Int::I128)].into(), Type::Int(Int::I128)), - true, - ), - [operand] - ), - ctx, - ), _ => todo!("Unsported pop count type {tpe:?}"), }, ctx, @@ -140,15 +101,14 @@ pub fn ctlz<'tcx>( 1, "The intrinsic `ctlz` MUST take in exactly 1 argument!" ); - let bit_operations = ClassRef::bit_operations(ctx); - let bit_operations = Some(bit_operations); - let tpe = ctx.monomorphize( - call_instance.args[0] - .as_type() - .expect("needs_drop works only on types!"), + let tpe = ctx.type_from_cache( + ctx.monomorphize( + call_instance.args[0] + .as_type() + .expect("needs_drop works only on types!"), + ), ); - let tpe = ctx.type_from_cache(tpe); // TODO: this assumes a 64 bit system! let sub = match tpe { Type::Int(Int::ISize | Int::USize) | Type::Ptr(_) => { @@ -159,47 +119,53 @@ pub fn ctlz<'tcx>( Type::Int(Int::I16 | Int::U16) => ldc_i32!(48), Type::Int(Int::I8 | Int::U8) => ldc_i32!(56), Type::Int(Int::I128) => { + let mref = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("LeadingZeroCount"), + ctx.sig([Type::Int(Int::I128)], Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); return place_set( destination, conv_u32!(call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "LeadingZeroCount".into(), - FnSig::new([Type::Int(Int::I128)].into(), Type::Int(Int::I128)), - true - ), + ctx.alloc_methodref(mref), [handle_operand(&args[0].node, ctx)] )), ctx, - ) + ); } Type::Int(Int::U128) => { + let mref = MethodRef::new( + ClassRef::uint_128(ctx), + ctx.alloc_string("LeadingZeroCount"), + ctx.sig([Type::Int(Int::U128)], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); return place_set( destination, conv_u32!(call!( - MethodRefIdx::new_extern( - ClassRef::uint_128(ctx), - "LeadingZeroCount".into(), - FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128)), - true - ), + ctx.alloc_methodref(mref), [handle_operand(&args[0].node, ctx)] )), ctx, - ) + ); } _ => todo!("Can't `ctlz` type {tpe:?} yet!"), }; + let mref = MethodRef::new( + ClassRef::bit_operations(ctx), + ctx.alloc_string("LeadingZeroCount"), + ctx.sig([Type::Int(Int::U64)], Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + ); place_set( destination, conv_u32!(sub!( call!( - MethodRefIdx::boxed( - bit_operations, - "LeadingZeroCount".into(), - FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::I32)), - true, - ), + ctx.alloc_methodref(mref), [conv_u64!(handle_operand(&args[0].node, ctx))] ), sub @@ -225,156 +191,147 @@ pub fn cttz<'tcx>( .expect("needs_drop works only on types!"), ); let tpe = ctx.type_from_cache(tpe); - let bit_operations = Some(bit_operations); let operand = handle_operand(&args[0].node, ctx); match tpe { Type::Int(Int::I8) => { - let value_calc = conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "TrailingZeroCount".into(), - FnSig::new([Type::Int(Int::I32)].into(), Type::Int(Int::I32)), - true, - ), - [conv_i32!(operand)] - )); + let ttc = MethodRef::new( + bit_operations, + ctx.alloc_string("TrailingZeroCount"), + ctx.sig([Type::Int(Int::I32)], Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = conv_u32!(call!(ctx.alloc_methodref(ttc), [conv_i32!(operand)])); + let min = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Min"), + ctx.sig( + [Type::Int(Int::U32), Type::Int(Int::U32)], + Type::Int(Int::U32), + ), + MethodKind::Static, + vec![].into(), + ); place_set( destination, - call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Min".into(), - FnSig::new( - [Type::Int(Int::U32), Type::Int(Int::U32)].into(), - Type::Int(Int::U32) - ), - true - ), - [value_calc, ldc_u32!(i8::BITS)] - ), + call!(ctx.alloc_methodref(min), [value_calc, ldc_u32!(i8::BITS)]), ctx, ) } Type::Int(Int::I16) => { - let value_calc = conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "TrailingZeroCount".into(), - FnSig::new([Type::Int(Int::I32)].into(), Type::Int(Int::I32)), - true, - ), - [conv_i32!(operand)] - )); + let mref = MethodRef::new( + bit_operations, + ctx.alloc_string("TrailingZeroCount"), + ctx.sig([Type::Int(Int::I32)], Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = conv_u32!(call!(ctx.alloc_methodref(mref), [conv_i32!(operand)])); + let min = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Min"), + ctx.sig( + [Type::Int(Int::U32), Type::Int(Int::U32)], + Type::Int(Int::U32), + ), + MethodKind::Static, + vec![].into(), + ); place_set( destination, - call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Min".into(), - FnSig::new( - [Type::Int(Int::U32), Type::Int(Int::U32)].into(), - Type::Int(Int::U32) - ), - true - ), - [value_calc, ldc_u32!(i16::BITS)] - ), + call!(ctx.alloc_methodref(min), [value_calc, ldc_u32!(i16::BITS)]), ctx, ) } Type::Int(Int::U8) => { - let value_calc = conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "TrailingZeroCount".into(), - FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::I32)), - true, - ), - [conv_u32!(operand)] - )); + let mref = MethodRef::new( + bit_operations, + ctx.alloc_string("TrailingZeroCount"), + ctx.sig([Type::Int(Int::U32)], Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = conv_u32!(call!(ctx.alloc_methodref(mref), [conv_u32!(operand)])); + let min = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Min"), + ctx.sig( + [Type::Int(Int::U32), Type::Int(Int::U32)], + Type::Int(Int::U32), + ), + MethodKind::Static, + vec![].into(), + ); place_set( destination, - call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Min".into(), - FnSig::new( - [Type::Int(Int::U32), Type::Int(Int::U32)].into(), - Type::Int(Int::U32) - ), - true - ), - [value_calc, ldc_u32!(u8::BITS)] - ), + call!(ctx.alloc_methodref(min), [value_calc, ldc_u32!(u8::BITS)]), ctx, ) } Type::Int(Int::U16) => { - let value_calc = conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "TrailingZeroCount".into(), - FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::I32)), - true, - ), - [conv_u32!(operand)] - )); + let mref = MethodRef::new( + bit_operations, + ctx.alloc_string("TrailingZeroCount"), + ctx.sig([Type::Int(Int::U32)], Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = conv_u32!(call!(ctx.alloc_methodref(mref), [conv_u32!(operand)])); + let min = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Min"), + ctx.sig( + [Type::Int(Int::U32), Type::Int(Int::U32)], + Type::Int(Int::U32), + ), + MethodKind::Static, + vec![].into(), + ); place_set( destination, - call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Min".into(), - FnSig::new( - [Type::Int(Int::U32), Type::Int(Int::U32)].into(), - Type::Int(Int::U32) - ), - true - ), - [value_calc, ldc_u32!(u16::BITS)] - ), + call!(ctx.alloc_methodref(min), [value_calc, ldc_u32!(u16::BITS)]), ctx, ) } - Type::Int(Int::I128) => place_set( - destination, - conv_u32!(call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "TrailingZeroCount".into(), - FnSig::new([Type::Int(Int::I128)].into(), Type::Int(Int::I128)), - true - ), + Type::Int(Int::I128) => { + let mref = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("TrailingZeroCount"), + ctx.sig([Type::Int(Int::I128)], Type::Int(Int::I128)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = conv_u32!(call!( + ctx.alloc_methodref(mref), [handle_operand(&args[0].node, ctx)] - )), - ctx, - ), - Type::Int(Int::U128) => place_set( - destination, - conv_u32!(call!( - MethodRefIdx::new_extern( - ClassRef::uint_128(ctx), - "TrailingZeroCount".into(), - FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128)), - true - ), + )); + place_set(destination, value_calc, ctx) + } + Type::Int(Int::U128) => { + let mref = MethodRef::new( + ClassRef::uint_128(ctx), + ctx.alloc_string("TrailingZeroCount"), + ctx.sig([Type::Int(Int::U128)], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = conv_u32!(call!( + ctx.alloc_methodref(mref), [handle_operand(&args[0].node, ctx)] - )), - ctx, - ), - _ => place_set( - destination, - conv_u32!(call!( - MethodRefIdx::boxed( - bit_operations, - "TrailingZeroCount".into(), - FnSig::new([tpe].into(), Type::Int(Int::I32)), - true, - ), - [operand] - )), - ctx, - ), + )); + place_set(destination, value_calc, ctx) + } + _ => { + let mref = MethodRef::new( + bit_operations, + ctx.alloc_string("TrailingZeroCount"), + ctx.sig([tpe], Type::Int(Int::I32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = conv_u32!(call!(ctx.alloc_methodref(mref), [operand])); + place_set(destination, value_calc, ctx) + } } } pub fn rotate_left<'tcx>( @@ -386,7 +343,7 @@ pub fn rotate_left<'tcx>( debug_assert_eq!( args.len(), 2, - "The intrinsic `rotate_left` MUST take in exactly 2 arguments!" + "The `rotate_left` MUST take in exactly 2 arguments!" ); let val_tpe = ctx.monomorphize( call_instance.args[0] @@ -397,201 +354,43 @@ pub fn rotate_left<'tcx>( let val = handle_operand(&args[0].node, ctx); let rot = handle_operand(&args[1].node, ctx); match val_tpe { - Type::Int(Int::U8) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::byte(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::U8), Type::Int(Int::I32)].into(), - Type::Int(Int::U8) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::U16) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::uint16(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::U16), Type::Int(Int::I32)].into(), - Type::Int(Int::U16) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::U32) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::U32), Type::Int(Int::I32)].into(), - Type::Int(Int::U32) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::U64) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::U64), Type::Int(Int::I32)].into(), - Type::Int(Int::U64) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::USize) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::USize), Type::Int(Int::I32)].into(), - Type::Int(Int::USize) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I8) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::sbyte(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::I8), Type::Int(Int::I32)].into(), - Type::Int(Int::I8) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I16) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::int16(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::I16), Type::Int(Int::I32)].into(), - Type::Int(Int::I16) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I32) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::U32), Type::Int(Int::I32)].into(), - Type::Int(Int::U32) - ), - true - ), - [conv_u32!(val), conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I64) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::U64), Type::Int(Int::I32)].into(), - Type::Int(Int::U32) - ), - true - ), - [conv_u64!(val), conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::ISize) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::USize), Type::Int(Int::I32)].into(), - Type::Int(Int::U32) - ), - true - ), - [conv_usize!(val), conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::U128) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::uint_128(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::I32)].into(), - Type::Int(Int::U128) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I128) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "RotateLeft".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I32)].into(), - Type::Int(Int::I128) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), + Type::Int( + int @ (Int::U8 + | Int::I8 + | Int::U16 + | Int::I16 + | Int::U32 + | Int::I32 + | Int::U64 + | Int::I64 + | Int::U128 + | Int::I128 + | Int::USize + | Int::ISize), + ) => place_set(destination, rol_int(val, conv_i32!(rot), int, ctx), ctx), _ => todo!("Can't ror {val_tpe:?}"), } } +pub fn rol_int(val: CILNode, rot: CILNode, int: Int, asm: &mut cilly::v2::Assembly) -> CILNode { + let mref = MethodRef::new( + int.class(asm), + asm.alloc_string("RotateLeft"), + asm.sig([Type::Int(int), Type::Int(Int::I32)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [val, rot]) +} +pub fn ror_int(val: CILNode, rot: CILNode, int: Int, asm: &mut cilly::v2::Assembly) -> CILNode { + let mref = MethodRef::new( + int.class(asm), + asm.alloc_string("RotateRight"), + asm.sig([Type::Int(int), Type::Int(Int::I32)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [val, rot]) +} pub fn rotate_right<'tcx>( args: &[Spanned>], destination: &Place<'tcx>, @@ -612,198 +411,20 @@ pub fn rotate_right<'tcx>( let val = handle_operand(&args[0].node, ctx); let rot = handle_operand(&args[1].node, ctx); match val_tpe { - Type::Int(Int::U16) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::uint16(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::U16), Type::Int(Int::I32)].into(), - Type::Int(Int::U16) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::U8) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::byte(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::U8), Type::Int(Int::I32)].into(), - Type::Int(Int::U8) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::U32) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::U32), Type::Int(Int::I32)].into(), - Type::Int(Int::U32) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::U64) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::U64), Type::Int(Int::I32)].into(), - Type::Int(Int::U64) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::USize) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::USize), Type::Int(Int::I32)].into(), - Type::Int(Int::USize) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I8) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::sbyte(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::I8), Type::Int(Int::I32)].into(), - Type::Int(Int::I8) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I16) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::int16(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::I16), Type::Int(Int::I32)].into(), - Type::Int(Int::I16) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I32) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::U32), Type::Int(Int::I32)].into(), - Type::Int(Int::U32) - ), - true - ), - [conv_u32!(val), conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I64) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::U64), Type::Int(Int::I32)].into(), - Type::Int(Int::U32) - ), - true - ), - [conv_u64!(val), conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::ISize) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::bit_operations(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::USize), Type::Int(Int::I32)].into(), - Type::Int(Int::U32) - ), - true - ), - [conv_usize!(val), conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::U128) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::uint_128(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::U128), Type::Int(Int::I32)].into(), - Type::Int(Int::U128) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), - Type::Int(Int::I128) => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "RotateRight".into(), - FnSig::new( - [Type::Int(Int::I128), Type::Int(Int::I32)].into(), - Type::Int(Int::I128) - ), - true - ), - [val, conv_i32!(rot)] - ), - ctx, - ), + Type::Int( + int @ (Int::U8 + | Int::I8 + | Int::U16 + | Int::I16 + | Int::U32 + | Int::I32 + | Int::U64 + | Int::I64 + | Int::U128 + | Int::I128 + | Int::USize + | Int::ISize), + ) => place_set(destination, ror_int(val, conv_i32!(rot), int, ctx), ctx), _ => todo!("Can't ror {val_tpe:?}"), } } @@ -823,6 +444,29 @@ fn bitreverse_u16(ushort: CILNode) -> CILNode { conv_u16!(ldc_u32!(256)) )))) } +pub fn bitreverse_int(val: CILNode, int: Int, asm: &mut cilly::v2::Assembly) -> CILNode { + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("bitreverse{}", int.name())), + asm.sig([Type::Int(int.as_unsigned())], Type::Int(int.as_unsigned())), + MethodKind::Static, + vec![].into(), + ); + crate::casts::int_to_int( + int.as_unsigned().into(), + int.into(), + call!( + asm.alloc_methodref(mref), + [crate::casts::int_to_int( + int.into(), + int.as_unsigned().into(), + val, + asm + )] + ), + asm, + ) +} pub fn bitreverse<'tcx>( args: &[Spanned>], destination: &Place<'tcx>, @@ -848,85 +492,9 @@ pub fn bitreverse<'tcx>( Type::Int(Int::I8) => conv_i8!(bitreverse_u8(val)), Type::Int(Int::U16) => bitreverse_u16(val), Type::Int(Int::I16) => conv_i16!(bitreverse_u16(conv_u16!(val))), - Type::Int(Int::U32) => call!( - MethodRefIdx::builtin( - "bitreverse_u32".into(), - FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U32)), - true - ), - [val] - ), - Type::Int(Int::I32) => crate::casts::int_to_int( - Type::Int(Int::U32), - Type::Int(Int::I32), - call!( - MethodRefIdx::builtin( - "bitreverse_u32".into(), - FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U32)), - true - ), - [crate::casts::int_to_int( - Type::Int(Int::I32), - Type::Int(Int::U32), - val, - ctx - )] - ), - ctx, - ), - Type::Int(Int::U64) => call!( - MethodRefIdx::builtin( - "bitreverse_u64".into(), - FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::U64)), - true - ), - [val] - ), - Type::Int(Int::I64) => crate::casts::int_to_int( - Type::Int(Int::U64), - Type::Int(Int::I64), - call!( - MethodRefIdx::builtin( - "bitreverse_u64".into(), - FnSig::new([Type::Int(Int::U64)].into(), Type::Int(Int::U64)), - true - ), - [crate::casts::int_to_int( - Type::Int(Int::I64), - Type::Int(Int::U64), - val, - ctx - )] - ), - ctx, - ), - Type::Int(Int::U128) => call!( - MethodRefIdx::builtin( - "bitreverse_u128".into(), - FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128),), - true - ), - [val] - ), - Type::Int(Int::I128) => crate::casts::int_to_int( - Type::Int(Int::U128), - Type::Int(Int::I128), - call!( - MethodRefIdx::builtin( - "bitreverse_u128".into(), - FnSig::new([Type::Int(Int::U128)].into(), Type::Int(Int::U128),), - true - ), - [crate::casts::int_to_int( - Type::Int(Int::I128), - Type::Int(Int::U128), - val, - ctx - )] - ), - ctx, - ), - + Type::Int(int @ (Int::I32 | Int::U32 | Int::I64 | Int::U128 | Int::I128)) => { + bitreverse_int(val, int, ctx) + } _ => todo!("can't yet bitreverse {val_tpe:?}"), }, ctx, From a9f37dfa6d12c1305f6da30c85fbce4f244770ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Wed, 2 Oct 2024 13:35:20 +0200 Subject: [PATCH 04/10] More work moving to a new method reference representation --- src/terminator/intrinsics/saturating.rs | 360 +++++++++++++----------- 1 file changed, 188 insertions(+), 172 deletions(-) diff --git a/src/terminator/intrinsics/saturating.rs b/src/terminator/intrinsics/saturating.rs index 41d0a9e9..b2f01b5a 100644 --- a/src/terminator/intrinsics/saturating.rs +++ b/src/terminator/intrinsics/saturating.rs @@ -1,12 +1,12 @@ use crate::{assembly::MethodCompileCtx, operand::handle_operand, place::place_set}; use cilly::{ call, - call_site::MethodRefIdx, cil_node::CILNode, cil_root::CILRoot, + cilnode::MethodKind, conv_i16, conv_i32, conv_i64, conv_i8, ldc_i32, ldc_i64, v2::{ClassRef, FnSig, Int}, - Type, + MethodRef, Type, }; use rustc_middle::{ @@ -46,20 +46,22 @@ pub fn saturating_add<'tcx>( let a = conv_i64!(a); let b = conv_i64!(b); let diff = a + b; - let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I64), - Type::Int(Int::I64), - Type::Int(Int::I64) - ]), - Type::Int(Int::I64) - ), - true + let clamp = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I64), + Type::Int(Int::I64), + Type::Int(Int::I64), + ]), + Type::Int(Int::I64), ), + MethodKind::Static, + vec![].into(), + ); + let diff_capped = call!( + ctx.alloc_methodref(clamp), [ diff, ldc_i64!(i64::from(i32::MIN)), @@ -72,33 +74,34 @@ pub fn saturating_add<'tcx>( Type::Int(Int::I64) => { let a = crate::casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), a, ctx); let b = crate::casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), b, ctx); - let diff = call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "op_Addition".into(), - FnSig::new( - Box::new([Type::Int(Int::I128), Type::Int(Int::I128)]), - Type::Int(Int::I128) - ), - true, + let add = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_Addition"), + ctx.sig( + ([Type::Int(Int::I128), Type::Int(Int::I128)]), + Type::Int(Int::I128), + ), + MethodKind::Static, + vec![].into(), + ); + let diff = call!(ctx.alloc_methodref(add), [a, b]); + let clamp = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I128), + Type::Int(Int::I128), + Type::Int(Int::I128), + ]), + Type::Int(Int::I128), ), - [a, b] + MethodKind::Static, + vec![].into(), ); #[allow(clippy::cast_sign_loss)] let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I128), - Type::Int(Int::I128), - Type::Int(Int::I128) - ]), - Type::Int(Int::I128) - ), - true - ), + ctx.alloc_methodref(clamp), [ diff, CILNode::const_i128(i128::from(i64::MIN) as u128, ctx), @@ -111,33 +114,34 @@ pub fn saturating_add<'tcx>( Type::Int(Int::ISize) => { let a = crate::casts::int_to_int(Type::Int(Int::ISize), Type::Int(Int::I128), a, ctx); let b = crate::casts::int_to_int(Type::Int(Int::ISize), Type::Int(Int::I128), b, ctx); - let diff = call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "op_Addition".into(), - FnSig::new( - Box::new([Type::Int(Int::I128), Type::Int(Int::I128)]), - Type::Int(Int::I128) - ), - true, + let sum = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_Addition"), + ctx.sig( + ([Type::Int(Int::I128), Type::Int(Int::I128)]), + Type::Int(Int::I128), + ), + MethodKind::Static, + vec![].into(), + ); + let diff = call!(ctx.alloc_methodref(sum), [a, b]); + let clamp = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I128), + Type::Int(Int::I128), + Type::Int(Int::I128), + ]), + Type::Int(Int::I128), ), - [a, b] + MethodKind::Static, + vec![].into(), ); #[allow(clippy::cast_sign_loss)] let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I128), - Type::Int(Int::I128), - Type::Int(Int::I128) - ]), - Type::Int(Int::I128) - ), - true - ), + ctx.alloc_methodref(clamp), [ diff, // TODO: this assumes isize::MAX == i64::MAX @@ -156,20 +160,22 @@ pub fn saturating_add<'tcx>( let a = conv_i32!(a); let b = conv_i32!(b); let diff = a + b; - let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I32), - Type::Int(Int::I32), - Type::Int(Int::I32) - ]), - Type::Int(Int::I32) - ), - true + let mref = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I32), + Type::Int(Int::I32), + Type::Int(Int::I32), + ]), + Type::Int(Int::I32), ), + MethodKind::Static, + vec![].into(), + ); + let diff_capped = call!( + ctx.alloc_methodref(mref), [ diff, ldc_i32!(i32::from(i16::MIN)), @@ -182,20 +188,22 @@ pub fn saturating_add<'tcx>( let a = conv_i32!(a); let b = conv_i32!(b); let diff = a + b; - let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I32), - Type::Int(Int::I32), - Type::Int(Int::I32) - ]), - Type::Int(Int::I32) - ), - true + let mref = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I32), + Type::Int(Int::I32), + Type::Int(Int::I32), + ]), + Type::Int(Int::I32), ), + MethodKind::Static, + vec![].into(), + ); + let diff_capped = call!( + ctx.alloc_methodref(mref), [ diff, ldc_i32!(i32::from(i8::MIN)), @@ -232,33 +240,34 @@ pub fn saturating_sub<'tcx>( Type::Int(Int::I64) => { let a = crate::casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), a, ctx); let b = crate::casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), b, ctx); - let diff = call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "op_Subtraction".into(), - FnSig::new( - Box::new([Type::Int(Int::I128), Type::Int(Int::I128)]), - Type::Int(Int::I128) - ), - true, + let sub = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_Subtraction"), + ctx.sig( + ([Type::Int(Int::I128), Type::Int(Int::I128)]), + Type::Int(Int::I128), ), - [a, b] + MethodKind::Static, + vec![].into(), + ); + let diff = call!(ctx.alloc_methodref(sub), [a, b]); + let clamp = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I128), + Type::Int(Int::I128), + Type::Int(Int::I128), + ]), + Type::Int(Int::I128), + ), + MethodKind::Static, + vec![].into(), ); #[allow(clippy::cast_sign_loss)] let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I128), - Type::Int(Int::I128), - Type::Int(Int::I128) - ]), - Type::Int(Int::I128) - ), - true - ), + ctx.alloc_methodref(clamp), [ diff, CILNode::const_i128(i128::from(i64::MIN) as u128, ctx), @@ -270,33 +279,34 @@ pub fn saturating_sub<'tcx>( Type::Int(Int::ISize) => { let a = crate::casts::int_to_int(Type::Int(Int::ISize), Type::Int(Int::I128), a, ctx); let b = crate::casts::int_to_int(Type::Int(Int::ISize), Type::Int(Int::I128), b, ctx); - let diff = call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "op_Subtraction".into(), - FnSig::new( - Box::new([Type::Int(Int::I128), Type::Int(Int::I128)]), - Type::Int(Int::I128) - ), - true, + let sub = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("op_Subtraction"), + ctx.sig( + ([Type::Int(Int::I128), Type::Int(Int::I128)]), + Type::Int(Int::I128), + ), + MethodKind::Static, + vec![].into(), + ); + let diff = call!(ctx.alloc_methodref(sub), [a, b]); + let clamp = MethodRef::new( + ClassRef::int_128(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I128), + Type::Int(Int::I128), + Type::Int(Int::I128), + ]), + Type::Int(Int::I128), ), - [a, b] + MethodKind::Static, + vec![].into(), ); #[allow(clippy::cast_sign_loss)] let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::int_128(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I128), - Type::Int(Int::I128), - Type::Int(Int::I128) - ]), - Type::Int(Int::I128) - ), - true - ), + ctx.alloc_methodref(clamp), [ diff, // TODO: this assumes isize::MAX == i64::MAX @@ -315,20 +325,22 @@ pub fn saturating_sub<'tcx>( let a = conv_i64!(a); let b = conv_i64!(b); let diff = a - b; - let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I64), - Type::Int(Int::I64), - Type::Int(Int::I64) - ]), - Type::Int(Int::I64) - ), - true + let clamp = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I64), + Type::Int(Int::I64), + Type::Int(Int::I64), + ]), + Type::Int(Int::I64), ), + MethodKind::Static, + vec![].into(), + ); + let diff_capped = call!( + ctx.alloc_methodref(clamp), [ diff, ldc_i64!(i64::from(i32::MIN)), @@ -341,20 +353,22 @@ pub fn saturating_sub<'tcx>( let a = conv_i32!(a); let b = conv_i32!(b); let diff = a - b; - let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I32), - Type::Int(Int::I32), - Type::Int(Int::I32) - ]), - Type::Int(Int::I32) - ), - true + let clamp = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I32), + Type::Int(Int::I32), + Type::Int(Int::I32), + ]), + Type::Int(Int::I32), ), + MethodKind::Static, + vec![].into(), + ); + let diff_capped = call!( + ctx.alloc_methodref(clamp), [ diff, ldc_i32!(i32::from(i16::MIN)), @@ -367,20 +381,22 @@ pub fn saturating_sub<'tcx>( let a = conv_i32!(a); let b = conv_i32!(b); let diff = a - b; - let diff_capped = call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Clamp".into(), - FnSig::new( - Box::new([ - Type::Int(Int::I32), - Type::Int(Int::I32), - Type::Int(Int::I32) - ]), - Type::Int(Int::I32) - ), - true + let clamp = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Clamp"), + ctx.sig( + ([ + Type::Int(Int::I32), + Type::Int(Int::I32), + Type::Int(Int::I32), + ]), + Type::Int(Int::I32), ), + MethodKind::Static, + vec![].into(), + ); + let diff_capped = call!( + ctx.alloc_methodref(clamp), [ diff, ldc_i32!(i32::from(i8::MIN)), From 4360232932b1e1e01ccff1d4dbe25099127120fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Wed, 2 Oct 2024 18:35:45 +0200 Subject: [PATCH 05/10] Proted utilis.rs to the new method ref representation --- src/terminator/intrinsics/utilis.rs | 758 ++++++++++------------------ 1 file changed, 255 insertions(+), 503 deletions(-) diff --git a/src/terminator/intrinsics/utilis.rs b/src/terminator/intrinsics/utilis.rs index d7c1911f..8b5d1942 100644 --- a/src/terminator/intrinsics/utilis.rs +++ b/src/terminator/intrinsics/utilis.rs @@ -1,55 +1,53 @@ use cilly::{ call, - call_site::MethodRefIdx, cil_node::CILNode, + cilnode::MethodKind, v2::{Assembly, ClassRef, FnSig, Int}, - Type, + MethodRef, Type, }; pub fn atomic_add(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) -> CILNode { match tpe { Type::Int(Int::U64 | Int::I64) => { - call!( - MethodRefIdx::new( - Some(ClassRef::interlocked(asm)), - "Add".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), - Type::Int(Int::U64) - ), - true - ), - [addr, addend] - ) + let u64_ref = asm.nref(Type::Int(Int::U64)); + let mref = MethodRef::new( + (ClassRef::interlocked(asm)), + asm.alloc_string("Add"), + asm.sig([u64_ref, Type::Int(Int::U64)], Type::Int(Int::U64)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), [addr, addend]); + cilnode } Type::Int(Int::U32 | Int::I32) => { + let u32_ref = asm.nref(Type::Int(Int::U32)); + let mref = MethodRef::new( + (ClassRef::interlocked(asm)), + asm.alloc_string("Add"), + asm.sig(([u32_ref, Type::Int(Int::U32)]), Type::Int(Int::U32)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [addr, addend]) + } + Type::Int(Int::USize | Int::ISize) | Type::Ptr(_) => { + let usize_ref = asm.nref(Type::Int(Int::USize)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("atomic_add_usize"), + asm.sig([usize_ref, Type::Int(Int::USize)], Type::Int(Int::USize)), + MethodKind::Static, + vec![].into(), + ); call!( - MethodRefIdx::new( - Some(ClassRef::interlocked(asm)), - "Add".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), - Type::Int(Int::U32) - ), - true - ), - [addr, addend] + asm.alloc_methodref(mref), + [ + addr.cast_ptr(asm.nptr(Type::Int(Int::USize))), + addend.cast_ptr(Type::Int(Int::USize)) + ] ) } - Type::Int(Int::USize | Int::ISize) | Type::Ptr(_) => call!( - MethodRefIdx::builtin( - "atomic_add_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [ - addr.cast_ptr(asm.nptr(Type::Int(Int::USize))), - addend.cast_ptr(Type::Int(Int::USize)) - ] - ), _ => todo!(), } @@ -57,522 +55,276 @@ pub fn atomic_add(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) pub fn atomic_or(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) -> CILNode { match tpe { Type::Int(Int::U64 | Int::I64) => { - call!( - MethodRefIdx::new( - Some(ClassRef::interlocked(asm)), - "Or".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), - Type::Int(Int::U64) - ), - true - ), - [addr, addend] - ) + let u64_ref = asm.nref(Type::Int(Int::U64)); + let mref = MethodRef::new( + (ClassRef::interlocked(asm)), + asm.alloc_string("Or"), + asm.sig(([u64_ref, Type::Int(Int::U64)]), Type::Int(Int::U64)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [addr, addend]) } Type::Int(Int::U32 | Int::I32) => { - call!( - MethodRefIdx::new( - Some(ClassRef::interlocked(asm)), - "Or".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), - Type::Int(Int::U32) - ), - true - ), - [addr, addend] - ) + let u32_ref = asm.nref(Type::Int(Int::U32)); + let mref = MethodRef::new( + (ClassRef::interlocked(asm)), + asm.alloc_string("Or"), + asm.sig([u32_ref, Type::Int(Int::U32)], Type::Int(Int::U32)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [addr, addend]) + } + Type::Int(int @ (Int::ISize | Int::USize)) => { + let int_ref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_or_{}", int.name())), + asm.sig([int_ref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), [addr, addend]); + cilnode + } + + Type::Ptr(inner) => { + let int = Int::ISize; + let int_ref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_or_{}", int.name())), + asm.sig([int_ref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!( + asm.alloc_methodref(mref), + [ + addr.cast_ptr(asm.nref(Type::Int(Int::USize))), + addend.cast_ptr(Type::Int(Int::USize)) + ] + ); + cilnode.cast_ptr(Type::Ptr(inner)) } - Type::Int(Int::USize) => call!( - MethodRefIdx::builtin( - "atomic_or_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [addr, addend] - ), - Type::Int(Int::ISize) => call!( - MethodRefIdx::builtin( - "atomic_or_isize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), - Type::Int(Int::ISize) - ), - true - ), - [addr, addend] - ), - Type::Ptr(inner) => call!( - MethodRefIdx::builtin( - "atomic_or_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [ - addr.cast_ptr(asm.nref(Type::Int(Int::USize))), - addend.cast_ptr(Type::Int(Int::USize)) - ] - ) - .cast_ptr(Type::Ptr(inner)), _ => todo!(), } } pub fn atomic_xor(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) -> CILNode { match tpe { - Type::Int(Int::I32) => { - call!( - MethodRefIdx::builtin( - "atomic_xor_i32".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::I32)), Type::Int(Int::I32)]), - Type::Int(Int::I32) - ), - true - ), - [addr, addend] - ) + Type::Int(int @ (Int::U32 | Int::I32 | Int::U64 | Int::I64 | Int::USize | Int::ISize)) => { + let iref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_xor_{}", int.name())), + asm.sig([iref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), [addr, addend]); + cilnode } - Type::Int(Int::I64) => { + Type::Ptr(inner) => { + let int = Int::USize; + let iref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_xor_{}", int.name())), + asm.sig([iref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); call!( - MethodRefIdx::builtin( - "atomic_xor_i64".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::I64)), Type::Int(Int::I64)]), - Type::Int(Int::I64) - ), - true - ), - [addr, addend] + asm.alloc_methodref(mref), + [ + addr.cast_ptr(asm.nref(Type::Int(Int::USize))), + addend.cast_ptr(Type::Int(Int::USize)) + ] ) + .cast_ptr(Type::Ptr(inner)) } - Type::Int(Int::U32) => { - call!( - MethodRefIdx::builtin( - "atomic_xor_u32".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), - Type::Int(Int::U32) - ), - true - ), - [addr, addend] - ) - } - Type::Int(Int::U64) => { - call!( - MethodRefIdx::builtin( - "atomic_xor_u64".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), - Type::Int(Int::U64) - ), - true - ), - [addr, addend] - ) - } - Type::Int(Int::USize) => call!( - MethodRefIdx::builtin( - "atomic_xor_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [addr, addend] - ), - Type::Int(Int::ISize) => call!( - MethodRefIdx::builtin( - "atomic_xor_isize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), - Type::Int(Int::ISize) - ), - true - ), - [addr, addend] - ), - Type::Ptr(inner) => call!( - MethodRefIdx::builtin( - "atomic_xor_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [ - addr.cast_ptr(asm.nref(Type::Int(Int::USize))), - addend.cast_ptr(Type::Int(Int::USize)) - ] - ) - .cast_ptr(Type::Ptr(inner)), _ => todo!(), } } pub fn atomic_and(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) -> CILNode { match tpe { Type::Int(Int::U64 | Int::I64) => { - call!( - MethodRefIdx::new( - Some(ClassRef::interlocked(asm)), - "And".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), - Type::Int(Int::U64) - ), - true - ), - [addr, addend] - ) + let u64_ref = asm.nref(Type::Int(Int::U64)); + let mref = MethodRef::new( + (ClassRef::interlocked(asm)), + asm.alloc_string("And"), + asm.sig([u64_ref, Type::Int(Int::U64)], Type::Int(Int::U64)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), [addr, addend]); + cilnode } Type::Int(Int::U32 | Int::I32) => { - call!( - MethodRefIdx::new( - Some(ClassRef::interlocked(asm)), - "And".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), - Type::Int(Int::U32) - ), - true - ), - [addr, addend] - ) + let u32_ref = asm.nref(Type::Int(Int::U32)); + let mref = MethodRef::new( + (ClassRef::interlocked(asm)), + asm.alloc_string("And"), + asm.sig(([u32_ref, Type::Int(Int::U32)]), Type::Int(Int::U32)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), [addr, addend]); + cilnode + } + Type::Int(Int::USize | Int::ISize) => { + let usize_ref = asm.nref(Type::Int(Int::USize)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("atomic_and_usize"), + asm.sig([usize_ref, Type::Int(Int::USize)], Type::Int(Int::USize)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), [addr, addend]); + cilnode + } + Type::Ptr(inner) => { + let usize_ref = asm.nref(Type::Int(Int::USize)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("atomic_and_usize"), + asm.sig([usize_ref, Type::Int(Int::USize)], Type::Int(Int::USize)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!( + asm.alloc_methodref(mref), + [ + addr.cast_ptr(asm.nref(Type::Int(Int::USize))), + addend.cast_ptr(Type::Int(Int::USize)) + ] + ); + cilnode.cast_ptr(Type::Ptr(inner)) } - Type::Int(Int::USize | Int::ISize) => call!( - MethodRefIdx::builtin( - "atomic_and_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [addr, addend] - ), - Type::Ptr(inner) => call!( - MethodRefIdx::builtin( - "atomic_and_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [ - addr.cast_ptr(asm.nref(Type::Int(Int::USize))), - addend.cast_ptr(Type::Int(Int::USize)) - ] - ) - .cast_ptr(Type::Ptr(inner)), _ => todo!(), } } pub fn compare_bytes(a: CILNode, b: CILNode, len: CILNode, asm: &mut Assembly) -> CILNode { - call!( - MethodRefIdx::builtin( - "memcmp".into(), - FnSig::new( - Box::new([ - asm.nptr(Type::Int(Int::U8)), - asm.nptr(Type::Int(Int::U8)), - Type::Int(Int::USize) - ]), - Type::Int(Int::I32) - ), - true + let u8_ref = asm.nptr(Type::Int(Int::U8)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string("memcmp"), + asm.sig( + ([u8_ref, u8_ref, Type::Int(Int::USize)]), + Type::Int(Int::I32), ), - [a, b, len] - ) + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(mref), [a, b, len]) } pub fn atomic_nand(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) -> CILNode { match tpe { - Type::Int(Int::I32) => { - call!( - MethodRefIdx::builtin( - "atomic_nand_i32".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::I32)), Type::Int(Int::I32)]), - Type::Int(Int::I32) - ), - true - ), - [addr, addend] - ) - } - Type::Int(Int::I64) => { - call!( - MethodRefIdx::builtin( - "atomic_nand_i64".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::I64)), Type::Int(Int::I64)]), - Type::Int(Int::I64) - ), - true - ), - [addr, addend] - ) - } - Type::Int(Int::U32) => { - call!( - MethodRefIdx::builtin( - "atomic_nand_u32".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), - Type::Int(Int::U32) - ), - true - ), - [addr, addend] - ) + Type::Int(int @ (Int::U32 | Int::I32 | Int::U64 | Int::I64 | Int::USize | Int::ISize)) => { + let iref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_nand_{}", int.name())), + asm.sig([iref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), [addr, addend]); + cilnode } - Type::Int(Int::U64) => { + Type::Ptr(inner) => { + let int = Int::USize; + let iref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_nand_{}", int.name())), + asm.sig([iref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); call!( - MethodRefIdx::builtin( - "atomic_nand_u64".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), - Type::Int(Int::U64) - ), - true - ), - [addr, addend] + asm.alloc_methodref(mref), + [ + addr.cast_ptr(asm.nref(Type::Int(Int::USize))), + addend.cast_ptr(Type::Int(Int::USize)) + ] ) + .cast_ptr(Type::Ptr(inner)) } - Type::Int(Int::USize) => call!( - MethodRefIdx::builtin( - "atomic_nand_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [addr, addend] - ), - Type::Int(Int::ISize) => call!( - MethodRefIdx::builtin( - "atomic_nand_isize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), - Type::Int(Int::ISize) - ), - true - ), - [addr, addend] - ), - Type::Ptr(inner) => call!( - MethodRefIdx::builtin( - "atomic_nand_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [ - addr.cast_ptr(asm.nref(Type::Int(Int::USize))), - addend.cast_ptr(Type::Int(Int::USize)) - ] - ) - .cast_ptr(Type::Ptr(inner)), _ => todo!(), } } pub fn atomic_min(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) -> CILNode { match tpe { - Type::Int(Int::I32) => { - call!( - MethodRefIdx::builtin( - "atomic_min_i32".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::I32)), Type::Int(Int::I32)]), - Type::Int(Int::I32) - ), - true - ), - [addr, addend] - ) - } - Type::Int(Int::I64) => { - call!( - MethodRefIdx::builtin( - "atomic_min_i64".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::I64)), Type::Int(Int::I64)]), - Type::Int(Int::I64) - ), - true - ), - [addr, addend] - ) + Type::Int(int @ (Int::U32 | Int::I32 | Int::U64 | Int::I64 | Int::USize | Int::ISize)) => { + let iref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_min_{}", int.name())), + asm.sig([iref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), [addr, addend]); + cilnode } - Type::Int(Int::U32) => { + Type::Ptr(inner) => { + let int = Int::USize; + let iref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_min_{}", int.name())), + asm.sig([iref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); call!( - MethodRefIdx::builtin( - "atomic_min_u32".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), - Type::Int(Int::U32) - ), - true - ), - [addr, addend] + asm.alloc_methodref(mref), + [ + addr.cast_ptr(asm.nref(Type::Int(Int::USize))), + addend.cast_ptr(Type::Int(Int::USize)) + ] ) + .cast_ptr(Type::Ptr(inner)) } - Type::Int(Int::U64) => { - call!( - MethodRefIdx::builtin( - "atomic_min_u64".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), - Type::Int(Int::U64) - ), - true - ), - [addr, addend] - ) - } - Type::Int(Int::USize) => call!( - MethodRefIdx::builtin( - "atomic_min_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [addr, addend] - ), - Type::Int(Int::ISize) => call!( - MethodRefIdx::builtin( - "atomic_min_isize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), - Type::Int(Int::ISize) - ), - true - ), - [addr, addend] - ), - Type::Ptr(inner) => call!( - MethodRefIdx::builtin( - "atomic_min_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [ - addr.cast_ptr(asm.nref(Type::Int(Int::USize))), - addend.cast_ptr(Type::Int(Int::USize)) - ] - ) - .cast_ptr(Type::Ptr(inner)), _ => todo!(), } } pub fn atomic_max(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) -> CILNode { match tpe { - Type::Int(Int::I32) => { - call!( - MethodRefIdx::builtin( - "atomic_max_i32".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::I32)), Type::Int(Int::I32)]), - Type::Int(Int::I32) - ), - true - ), - [addr, addend] - ) + Type::Int(int @ (Int::U32 | Int::I32 | Int::U64 | Int::I64 | Int::USize | Int::ISize)) => { + let iref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_max_{}", int.name())), + asm.sig([iref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + let cilnode = call!(asm.alloc_methodref(mref), [addr, addend]); + cilnode } - Type::Int(Int::I64) => { + Type::Ptr(inner) => { + let int = Int::USize; + let iref = asm.nref(Type::Int(int)); + let mref = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("atomic_max_{}", int.name())), + asm.sig([iref, Type::Int(int)], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); call!( - MethodRefIdx::builtin( - "atomic_max_i64".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::I64)), Type::Int(Int::I64)]), - Type::Int(Int::I64) - ), - true - ), - [addr, addend] + asm.alloc_methodref(mref), + [ + addr.cast_ptr(asm.nref(Type::Int(Int::USize))), + addend.cast_ptr(Type::Int(Int::USize)) + ] ) + .cast_ptr(Type::Ptr(inner)) } - Type::Int(Int::U32) => { - call!( - MethodRefIdx::builtin( - "atomic_max_u32".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U32)), Type::Int(Int::U32)]), - Type::Int(Int::U32) - ), - true - ), - [addr, addend] - ) - } - Type::Int(Int::U64) => { - call!( - MethodRefIdx::builtin( - "atomic_max_u64".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::U64)), Type::Int(Int::U64)]), - Type::Int(Int::U64) - ), - true - ), - [addr, addend] - ) - } - Type::Int(Int::USize) => call!( - MethodRefIdx::builtin( - "atomic_max_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [addr, addend] - ), - Type::Int(Int::ISize) => call!( - MethodRefIdx::builtin( - "atomic_max_isize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::ISize)), Type::Int(Int::ISize)]), - Type::Int(Int::ISize) - ), - true - ), - [addr, addend] - ), - Type::Ptr(inner) => call!( - MethodRefIdx::builtin( - "atomic_max_usize".into(), - FnSig::new( - Box::new([asm.nref(Type::Int(Int::USize)), Type::Int(Int::USize)]), - Type::Int(Int::USize) - ), - true - ), - [ - addr.cast_ptr(asm.nref(Type::Int(Int::USize))), - addend.cast_ptr(Type::Int(Int::USize)) - ] - ) - .cast_ptr(Type::Ptr(inner)), _ => todo!(), } } From 0804809cfc7f6a914c20aae44e069964aa7e4356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Thu, 3 Oct 2024 00:00:55 +0200 Subject: [PATCH 06/10] Ported all code to the new methodref repr --- cilly/src/bin/linker/main.rs | 39 +- src/terminator/intrinsics/mod.rs | 1362 +++++++++++++++--------------- 2 files changed, 717 insertions(+), 684 deletions(-) diff --git a/cilly/src/bin/linker/main.rs b/cilly/src/bin/linker/main.rs index e73baa11..24e4e833 100644 --- a/cilly/src/bin/linker/main.rs +++ b/cilly/src/bin/linker/main.rs @@ -2,7 +2,6 @@ #![allow(clippy::module_name_repetitions)] use cilly::{ asm::DEAD_CODE_ELIMINATION, - call_site::MethodRefIdx, conv_usize, libc_fns::{self, LIBC_FNS, LIBC_MODIFIES_ERRNO}, v2::{ @@ -11,6 +10,7 @@ use cilly::{ Assembly, BasicBlock, CILNode, CILRoot, ClassDef, ClassRef, Const, FnSig, IlasmFlavour, Int, MethodImpl, Type, }, + MethodRef, }; //use assembly::Assembly; use lazy_static::lazy_static; @@ -258,6 +258,18 @@ fn main() { final_assembly.alloc_string("_Unwind_RaiseException"), Box::new(|_, asm| { let rust_exception = asm.alloc_string("RustException"); + let exception_class = + asm.alloc_class_ref(ClassRef::new(rust_exception, None, false, [].into())); + let exception_ctor = MethodRef::new( + (asm.alloc_class_ref(ClassRef::new(rust_exception, None, false, [].into()))), + asm.alloc_string(".ctor"), + asm.sig( + ([Type::ClassRef(exception_class), Type::Int(Int::USize)]), + Type::Void, + ), + MethodKind::Constructor, + vec![].into(), + ); MethodImpl::MethodBody { blocks: vec![cilly::v2::BasicBlock::from_v1( &cilly::basic_block::BasicBlock::new( @@ -267,30 +279,7 @@ fn main() { args: Box::new([conv_usize!( cilly::cil_node::CILNode::LDArg(0) )]), - site: Box::new(MethodRefIdx::new( - Some(asm.alloc_class_ref(ClassRef::new( - rust_exception, - None, - false, - [].into(), - ))), - ".ctor".into(), - FnSig::new( - Box::new([ - Type::ClassRef(asm.alloc_class_ref( - ClassRef::new( - rust_exception, - None, - false, - [].into(), - ), - )), - Type::Int(Int::USize), - ]), - Type::Void, - ), - false, - )), + site: asm.alloc_methodref(exception_ctor), }, )), ) diff --git a/src/terminator/intrinsics/mod.rs b/src/terminator/intrinsics/mod.rs index 3b0670a6..18cabe83 100644 --- a/src/terminator/intrinsics/mod.rs +++ b/src/terminator/intrinsics/mod.rs @@ -5,15 +5,14 @@ use crate::{ utilis::field_descrptor, }; use cilly::{ - call, - call_site::MethodRefIdx, - call_virt, + call, call_virt, cil_node::CILNode, cil_root::CILRoot, + cilnode::MethodKind, conv_f32, conv_f64, conv_i16, conv_i32, conv_i64, conv_i8, conv_isize, conv_u16, conv_u32, conv_u64, conv_u8, conv_usize, eq, ld_field, ldc_i32, ldc_u32, ldc_u64, size_of, sub, v2::{ClassRef, Float, FnSig, Int}, - Type, + MethodRef, Type, }; use ints::{ctlz, rotate_left, rotate_right}; use rustc_middle::{ @@ -122,56 +121,56 @@ pub fn handle_intrinsic<'tcx>( let needs_drop = i32::from(needs_drop); place_set(destination, ldc_i32!(needs_drop), ctx) } - "fmaf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "FusedMultiplyAdd".into(), - FnSig::new( - [ - Type::Float(Float::F32), - Type::Float(Float::F32), - Type::Float(Float::F32) - ] - .into(), - Type::Float(Float::F32) - ), - true + "fmaf32" => { + let mref = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("FusedMultiplyAdd"), + ctx.sig( + [ + Type::Float(Float::F32), + Type::Float(Float::F32), + Type::Float(Float::F32), + ], + Type::Float(Float::F32), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(mref), [ handle_operand(&args[0].node, ctx), handle_operand(&args[1].node, ctx), handle_operand(&args[2].node, ctx), ] - ), - ctx, - ), - "fmaf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "FusedMultiplyAdd".into(), - FnSig::new( - [ - Type::Float(Float::F64), - Type::Float(Float::F64), - Type::Float(Float::F64) - ] - .into(), - Type::Float(Float::F64) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "fmaf64" => { + let mref = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("FusedMultiplyAdd"), + ctx.sig( + [ + Type::Float(Float::F64), + Type::Float(Float::F64), + Type::Float(Float::F64), + ], + Type::Float(Float::F64), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(mref), [ handle_operand(&args[0].node, ctx), handle_operand(&args[1].node, ctx), handle_operand(&args[2].node, ctx), ] - ), - ctx, - ), + ); + place_set(destination, value_calc, ctx) + } "raw_eq" => { // Raw eq returns 0 if values are not equal, and 1 if they are, unlike memcmp, which does the oposite. @@ -292,37 +291,39 @@ pub fn handle_intrinsic<'tcx>( .expect("needs_drop works only on types!"), ); let tpe = ctx.type_from_cache(tpe); - let sig = FnSig::new( - [ClassRef::runtime_type_hadle(ctx).into()].into(), - ClassRef::type_type(ctx).into(), + let type_type = ClassRef::type_type(ctx); + let runtime_handle = ClassRef::runtime_type_hadle(ctx); + let sig = ctx.sig([runtime_handle.into()], type_type); + let gethash_sig = ctx.sig([type_type.into()], Type::Int(Int::I32)); + let op_implict = MethodRef::new( + (ClassRef::uint_128(ctx)), + ctx.alloc_string("op_Implicit"), + ctx.sig([Type::Int(Int::U32)], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), ); - let gethash_sig = FnSig::new( - [ClassRef::type_type(ctx).into()].into(), - Type::Int(Int::I32), + let get_hash_code = MethodRef::new( + ClassRef::object(ctx), + ctx.alloc_string("GetHashCode"), + gethash_sig, + MethodKind::Virtual, + vec![].into(), + ); + let get_type_handle = MethodRef::new( + type_type, + ctx.alloc_string("GetTypeFromHandle"), + sig, + MethodKind::Static, + vec![].into(), ); place_set( destination, call!( - MethodRefIdx::boxed( - Some(ClassRef::uint_128(ctx)), - "op_Implicit".into(), - FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U128)), - true, - ), + ctx.alloc_methodref(op_implict), [conv_u32!(call_virt!( - MethodRefIdx::boxed( - ClassRef::object(ctx).into(), - "GetHashCode".into(), - gethash_sig, - false, - ), + ctx.alloc_methodref(get_hash_code), [call!( - MethodRefIdx::boxed( - ClassRef::type_type(ctx).into(), - "GetTypeFromHandle".into(), - sig, - true, - ), + ctx.alloc_methodref(get_type_handle), [CILNode::LDTypeToken(tpe.into())] )] ))] @@ -440,24 +441,21 @@ pub fn handle_intrinsic<'tcx>( #[allow(clippy::single_match_else)] let exchange_res = match &src_type { Type::Ptr(_) => { - let call_site = MethodRefIdx::new( - Some(interlocked), - "CompareExchange".into(), - FnSig::new( - [ - ctx.nref(Type::Int(Int::USize)), - Type::Int(Int::USize), - Type::Int(Int::USize), - ] - .into(), + let usize_ref = ctx.nref(Type::Int(Int::USize)); + let call_site = MethodRef::new( + (interlocked), + ctx.alloc_string("CompareExchange"), + ctx.sig( + [usize_ref, Type::Int(Int::USize), Type::Int(Int::USize)], Type::Int(Int::USize), ), - true, + MethodKind::Static, + vec![].into(), ); call!( - call_site, + ctx.alloc_methodref(call_site), [ - Box::new(dst).cast_ptr(ctx.nref(Type::Int(Int::USize))), + Box::new(dst).cast_ptr(usize_ref), conv_usize!(value), conv_usize!(comaprand) ] @@ -467,13 +465,15 @@ pub fn handle_intrinsic<'tcx>( // TODO: this is a bug, on purpose. The 1 byte compare exchange is not supported untill .NET 9. Remove after November, when .NET 9 Releases. Type::Int(Int::U8) => comaprand, _ => { - let call_site = MethodRefIdx::new( - Some(interlocked), - "CompareExchange".into(), - FnSig::new([ctx.nref(src_type), src_type, src_type].into(), src_type), - true, + let src_ref = ctx.nref(src_type); + let call_site = MethodRef::new( + (interlocked), + ctx.alloc_string("CompareExchange"), + ctx.sig([src_ref, src_type, src_type], src_type), + MethodKind::Static, + vec![].into(), ); - call!(call_site, [dst, value, comaprand]) + call!(ctx.alloc_methodref(call_site), [dst, value, comaprand]) } }; @@ -581,13 +581,15 @@ pub fn handle_intrinsic<'tcx>( | "atomic_fence_release" | "atomic_fence_acqrel" => { let thread = ClassRef::thread(ctx); + let fence = MethodRef::new( + (thread), + ctx.alloc_string("MemoryBarrier"), + ctx.sig([], Type::Void), + MethodKind::Static, + vec![].into(), + ); CILRoot::Call { - site: Box::new(MethodRefIdx::new( - Some(thread), - "MemoryBarrier".into(), - FnSig::new([].into(), Type::Void), - true, - )), + site: ctx.alloc_methodref(fence), args: [].into(), } } @@ -679,38 +681,35 @@ pub fn handle_intrinsic<'tcx>( ); let src_type = ctx.monomorphize(args[1].node.ty(ctx.body(), ctx.tcx())); let src_type = ctx.type_from_cache(src_type); + let uint8_ref = ctx.nref(Type::Int(Int::U8)); + let xchng = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("atomic_xchng_u8"), + ctx.sig([uint8_ref, Type::Int(Int::U8)], Type::Int(Int::U8)), + MethodKind::Static, + vec![].into(), + ); match src_type { Type::Int(Int::U8) => { return place_set( destination, - call!( - MethodRefIdx::builtin( - "atomic_xchng_u8".into(), - FnSig::new( - [ctx.nref(Type::Int(Int::U8)), Type::Int(Int::U8)].into(), - Type::Int(Int::U8) - ), - true - ), - [dst, new] - ), + call!(ctx.alloc_methodref(xchng), [dst, new]), ctx, ) } Type::Ptr(_) => { - let call_site = MethodRefIdx::new( - Some(interlocked), - "Exchange".into(), - FnSig::new( - [ctx.nref(Type::Int(Int::USize)), Type::Int(Int::USize)].into(), - Type::Int(Int::USize), - ), - true, + let usize_ref = ctx.nref(Type::Int(Int::USize)); + let call_site = MethodRef::new( + (interlocked), + ctx.alloc_string("Exchange"), + ctx.sig([usize_ref, Type::Int(Int::USize)], Type::Int(Int::USize)), + MethodKind::Static, + vec![].into(), ); return place_set( destination, call!( - call_site, + ctx.alloc_methodref(call_site), [ Box::new(dst).cast_ptr(ctx.nref(Type::Int(Int::USize))), conv_usize!(new), @@ -725,14 +724,20 @@ pub fn handle_intrinsic<'tcx>( } _ => (), } - let call_site = MethodRefIdx::new( - Some(interlocked), - "Exchange".into(), - FnSig::new([ctx.nref(src_type), src_type].into(), src_type), - true, + let src_ref = ctx.nref(src_type); + let call_site = MethodRef::new( + (interlocked), + ctx.alloc_string("Exchange"), + ctx.sig([src_ref, src_type], src_type), + MethodKind::Static, + vec![].into(), ); // T - place_set(destination, call!(call_site, [dst, new]), ctx) + place_set( + destination, + call!(ctx.alloc_methodref(call_site), [dst, new]), + ctx, + ) } // TODO:Those are not stricly neccessary, but SHOULD be implemented at some point. "assert_inhabited" | "assert_zero_valid" | "const_deallocate" => CILRoot::Nop, @@ -846,15 +851,17 @@ pub fn handle_intrinsic<'tcx>( 1, "The intrinsic `sqrtf32` MUST take in exactly 1 argument!" ); + let sqrt = MethodRef::new( + (ClassRef::mathf(ctx)), + ctx.alloc_string("Sqrt"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); place_set( destination, call!( - MethodRefIdx::boxed( - Some(ClassRef::mathf(ctx)), - "Sqrt".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true, - ), + ctx.alloc_methodref(sqrt), [handle_operand(&args[0].node, ctx)] ), ctx, @@ -867,19 +874,20 @@ pub fn handle_intrinsic<'tcx>( 2, "The intrinsic `powif32` MUST take in exactly 2 arguments!" ); - + let pow = MethodRef::new( + (ClassRef::single(ctx)), + ctx.alloc_string("Pow"), + ctx.sig( + [Type::Float(Float::F32), Type::Float(Float::F32)], + Type::Float(Float::F32), + ), + MethodKind::Static, + vec![].into(), + ); place_set( destination, call!( - MethodRefIdx::boxed( - Some(ClassRef::single(ctx)), - "Pow".into(), - FnSig::new( - [Type::Float(Float::F32), Type::Float(Float::F32)].into(), - Type::Float(Float::F32) - ), - true, - ), + ctx.alloc_methodref(pow), [ handle_operand(&args[0].node, ctx), conv_f32!(handle_operand(&args[1].node, ctx)) @@ -894,19 +902,20 @@ pub fn handle_intrinsic<'tcx>( 2, "The intrinsic `powif64` MUST take in exactly 2 arguments!" ); - + let pow = MethodRef::new( + (ClassRef::double(ctx)), + ctx.alloc_string("Pow"), + ctx.sig( + [Type::Float(Float::F64), Type::Float(Float::F64)], + Type::Float(Float::F64), + ), + MethodKind::Static, + vec![].into(), + ); place_set( destination, call!( - MethodRefIdx::boxed( - Some(ClassRef::double(ctx)), - "Pow".into(), - FnSig::new( - [Type::Float(Float::F64), Type::Float(Float::F64)].into(), - Type::Float(Float::F64) - ), - true, - ), + ctx.alloc_methodref(pow), [ handle_operand(&args[0].node, ctx), conv_f64!(handle_operand(&args[1].node, ctx)) @@ -924,23 +933,19 @@ pub fn handle_intrinsic<'tcx>( ); let tpe = ctx.monomorphize(pointed_ty); let tpe = ctx.type_from_cache(tpe); + let void_ptr = ctx.nptr(Type::Void); + let generic = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("swap_at_generic"), + ctx.sig([void_ptr, void_ptr, Type::Int(Int::USize)], Type::Void), + MethodKind::Static, + vec![].into(), + ); CILRoot::Call { - site: Box::new(MethodRefIdx::builtin( - "swap_at_generic".into(), - FnSig::new( - [ - ctx.nptr(Type::Void), - ctx.nptr(Type::Void), - Type::Int(Int::USize), - ] - .into(), - Type::Void, - ), - true, - )), + site: ctx.alloc_methodref(generic), args: [ - handle_operand(&args[0].node, ctx).cast_ptr(ctx.nptr(Type::Void)), - handle_operand(&args[1].node, ctx).cast_ptr(ctx.nptr(Type::Void)), + handle_operand(&args[0].node, ctx).cast_ptr(void_ptr), + handle_operand(&args[1].node, ctx).cast_ptr(void_ptr), conv_usize!(size_of!(tpe)), ] .into(), @@ -985,525 +990,565 @@ pub fn handle_intrinsic<'tcx>( ctx, ) } - "fabsf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "Abs".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), - [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "fabsf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "Abs".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), - [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "expf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "Exp".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + "fabsf32" => { + let abs = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("Abs"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(abs), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "expf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "Exp".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true + ); + place_set(destination, value_calc, ctx) + } + "fabsf64" => { + let abs = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("Abs"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let place_set = place_set( + destination, + call!( + ctx.alloc_methodref(abs), + [handle_operand(&args[0].node, ctx),] ), + ctx, + ); + place_set + } + "expf32" => { + let exp = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("Exp"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(exp), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "logf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "Log".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "expf64" => { + let exp = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("Exp"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(exp), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "logf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "Log".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "logf32" => { + let log = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("Log"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(log), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "log2f32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "Log2".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true + ); + place_set(destination, value_calc, ctx) + } + "logf64" => { + let log = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("Log"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let place_set = place_set( + destination, + call!( + ctx.alloc_methodref(log), + [handle_operand(&args[0].node, ctx),] ), + ctx, + ); + place_set + } + "log2f32" => { + let log = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("Log2"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(log), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "log2f64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "Log2".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "log2f64" => { + let log = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("Log2"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(log), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "log10f32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "Log10".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "log10f32" => { + let log = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("Log10"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(log), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "log10f64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "Log10".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "log10f64" => { + let log = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("Log10"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(log), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "powf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "Pow".into(), - FnSig::new( - [Type::Float(Float::F32), Type::Float(Float::F32)].into(), - Type::Float(Float::F32) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "powf32" => { + let pow = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("Pow"), + ctx.sig( + [Type::Float(Float::F32), Type::Float(Float::F32)], + Type::Float(Float::F32), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(pow), [ handle_operand(&args[0].node, ctx), handle_operand(&args[1].node, ctx), ] - ), - ctx, - ), - "powf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "Pow".into(), - FnSig::new( - [Type::Float(Float::F64), Type::Float(Float::F64)].into(), - Type::Float(Float::F64) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "powf64" => { + let pow = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("Pow"), + ctx.sig( + [Type::Float(Float::F64), Type::Float(Float::F64)], + Type::Float(Float::F64), ), - [ - handle_operand(&args[0].node, ctx), - handle_operand(&args[1].node, ctx), - ] - ), - ctx, - ), - "copysignf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "CopySign".into(), - FnSig::new( - [Type::Float(Float::F32), Type::Float(Float::F32)].into(), - Type::Float(Float::F32) - ), - true + MethodKind::Static, + vec![].into(), + ); + let place_set = place_set( + destination, + call!( + ctx.alloc_methodref(pow), + [ + handle_operand(&args[0].node, ctx), + handle_operand(&args[1].node, ctx), + ] ), + ctx, + ); + place_set + } + "copysignf32" => { + let copy_sign = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("CopySign"), + ctx.sig( + [Type::Float(Float::F32), Type::Float(Float::F32)], + Type::Float(Float::F32), + ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(copy_sign), [ handle_operand(&args[0].node, ctx), handle_operand(&args[1].node, ctx), ] - ), - ctx, - ), - "copysignf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "CopySign".into(), - FnSig::new( - [Type::Float(Float::F64), Type::Float(Float::F64)].into(), - Type::Float(Float::F64) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "copysignf64" => { + let copy_sign = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("CopySign"), + ctx.sig( + [Type::Float(Float::F64), Type::Float(Float::F64)], + Type::Float(Float::F64), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(copy_sign), [ handle_operand(&args[0].node, ctx), handle_operand(&args[1].node, ctx), ] - ), - ctx, - ), - "sinf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "Sin".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "sinf32" => { + let sin = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("Sin"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(sin), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "sinf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "Sin".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "sinf64" => { + let sin = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("Sin"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(sin), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "cosf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "Cos".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "cosf32" => { + let cos = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("Cos"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(cos), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "cosf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "Cos".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "cosf64" => { + let cos = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("Cos"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(cos), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "exp2f32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "Exp2".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "exp2f32" => { + let exp = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("Exp2"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(exp), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "exp2f64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "Exp2".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "exp2f64" => { + let exp = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("Exp2"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(exp), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "truncf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::mathf(ctx), - "Truncate".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "truncf32" => { + let trunc = MethodRef::new( + ClassRef::mathf(ctx), + ctx.alloc_string("Truncate"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(trunc), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "truncf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Truncate".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "truncf64" => { + let trunc = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Truncate"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(trunc), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), + ); + place_set(destination, value_calc, ctx) + } // `roundf32` should be a differnt intrinsics, but it requires some .NET fuckery to implement(.NET enums are **wierd**) - "nearbyintf32" | "rintf32" | "roundevenf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::mathf(ctx), - "Round".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + "nearbyintf32" | "rintf32" | "roundevenf32" => { + let round = MethodRef::new( + ClassRef::mathf(ctx), + ctx.alloc_string("Round"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(round), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "roundf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::mathf(ctx), - "Round".into(), - FnSig::new( - [ - Type::Float(Float::F32), - Type::ClassRef(ClassRef::midpoint_rounding(ctx)), - ] - .into(), - Type::Float(Float::F32) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "roundf32" => { + let rounding = ClassRef::midpoint_rounding(ctx); + let round = MethodRef::new( + ClassRef::mathf(ctx), + ctx.alloc_string("Round"), + ctx.sig( + [Type::Float(Float::F32), Type::ClassRef(rounding)], + Type::Float(Float::F32), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(round), [ handle_operand(&args[0].node, ctx), ldc_i32!(1).transmute_on_stack( Type::Int(Int::I32), - Type::ClassRef(ClassRef::midpoint_rounding(ctx)), + Type::ClassRef(rounding), ctx ) ] - ), - ctx, - ), - "nearbyintf64" | "rintf64" | "roundevenf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Round".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "nearbyintf64" | "rintf64" | "roundevenf64" => { + let round = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Round"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(round), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "roundf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Round".into(), - FnSig::new( - [ - Type::Float(Float::F64), - Type::ClassRef(ClassRef::midpoint_rounding(ctx)) - ] - .into(), - Type::Float(Float::F64) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "roundf64" => { + let rounding = ClassRef::midpoint_rounding(ctx); + let round = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Round"), + ctx.sig( + [Type::Float(Float::F64), Type::ClassRef(rounding)], + Type::Float(Float::F64), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(round), [ handle_operand(&args[0].node, ctx), ldc_i32!(1).transmute_on_stack( Type::Int(Int::I32), - Type::ClassRef(ClassRef::midpoint_rounding(ctx)), + Type::ClassRef(rounding), ctx ) ] - ), - ctx, - ), - "floorf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::mathf(ctx), - "Floor".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "floorf32" => { + let floor = MethodRef::new( + ClassRef::mathf(ctx), + ctx.alloc_string("Floor"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(floor), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "floorf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Floor".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "floorf64" => { + let floor = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Floor"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(floor), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "ceilf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::mathf(ctx), - "Ceiling".into(), - FnSig::new([Type::Float(Float::F32)].into(), Type::Float(Float::F32)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "ceilf32" => { + let ceil = MethodRef::new( + ClassRef::mathf(ctx), + ctx.alloc_string("Ceiling"), + ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(ceil), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "ceilf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::math(ctx), - "Ceiling".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true - ), + ); + place_set(destination, value_calc, ctx) + } + "ceilf64" => { + let ceil = MethodRef::new( + ClassRef::math(ctx), + ctx.alloc_string("Ceiling"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(ceil), [handle_operand(&args[0].node, ctx),] - ), - ctx, - ), - "maxnumf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "MaxNumber".into(), - FnSig::new( - [Type::Float(Float::F64), Type::Float(Float::F64)].into(), - Type::Float(Float::F64) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "maxnumf64" => { + let max = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("MaxNumber"), + ctx.sig( + [Type::Float(Float::F64), Type::Float(Float::F64)], + Type::Float(Float::F64), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(max), [ handle_operand(&args[0].node, ctx), handle_operand(&args[1].node, ctx), ] - ), - ctx, - ), - "maxnumf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "MaxNumber".into(), - FnSig::new( - [Type::Float(Float::F32), Type::Float(Float::F32)].into(), - Type::Float(Float::F32) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "maxnumf32" => { + let max = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("MaxNumber"), + ctx.sig( + [Type::Float(Float::F32), Type::Float(Float::F32)], + Type::Float(Float::F32), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(max), [ handle_operand(&args[0].node, ctx), handle_operand(&args[1].node, ctx), ] - ), - ctx, - ), - "minnumf64" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::double(ctx), - "MinNumber".into(), - FnSig::new( - [Type::Float(Float::F64), Type::Float(Float::F64)].into(), - Type::Float(Float::F64) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "minnumf64" => { + let min = MethodRef::new( + ClassRef::double(ctx), + ctx.alloc_string("MinNumber"), + ctx.sig( + [Type::Float(Float::F64), Type::Float(Float::F64)], + Type::Float(Float::F64), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(min), [ handle_operand(&args[0].node, ctx), handle_operand(&args[1].node, ctx), ] - ), - ctx, - ), - "minnumf32" => place_set( - destination, - call!( - MethodRefIdx::new_extern( - ClassRef::single(ctx), - "MinNumber".into(), - FnSig::new( - [Type::Float(Float::F32), Type::Float(Float::F32)].into(), - Type::Float(Float::F32) - ), - true + ); + place_set(destination, value_calc, ctx) + } + "minnumf32" => { + let min = MethodRef::new( + ClassRef::single(ctx), + ctx.alloc_string("MinNumber"), + ctx.sig( + [Type::Float(Float::F32), Type::Float(Float::F32)], + Type::Float(Float::F32), ), + MethodKind::Static, + vec![].into(), + ); + let value_calc = call!( + ctx.alloc_methodref(min), [ handle_operand(&args[0].node, ctx), handle_operand(&args[1].node, ctx), ] - ), - ctx, - ), + ); + place_set(destination, value_calc, ctx) + } "variant_count" => { let const_val = ctx .tcx() @@ -1525,14 +1570,15 @@ pub fn handle_intrinsic<'tcx>( 1, "The intrinsic `sqrtf64` MUST take in exactly 1 argument!" ); - + let sqrt = MethodRef::new( + (ClassRef::math(ctx)), + ctx.alloc_string("Sqrt"), + ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), + MethodKind::Static, + vec![].into(), + ); let ops = call!( - MethodRefIdx::boxed( - Some(ClassRef::math(ctx)), - "Sqrt".into(), - FnSig::new([Type::Float(Float::F64)].into(), Type::Float(Float::F64)), - true, - ), + ctx.alloc_methodref(sqrt), [handle_operand(&args[0].node, ctx)] ); place_set(destination, ops, ctx) @@ -1548,22 +1594,22 @@ pub fn handle_intrinsic<'tcx>( let data_ptr = handle_operand(&args[1].node, ctx); let catch_fn = handle_operand(&args[2].node, ctx); let uint8_ptr = ctx.nptr(Type::Int(Int::U8)); + let try_ptr = ctx.sig([uint8_ptr], Type::Void); + let catch_ptr = ctx.sig([uint8_ptr, uint8_ptr], Type::Void); + let catch_unwind = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("catch_unwind"), + ctx.sig( + [Type::FnPtr(try_ptr), uint8_ptr, Type::FnPtr(catch_ptr)], + Type::Int(Int::I32), + ), + MethodKind::Static, + vec![].into(), + ); place_set( destination, call!( - MethodRefIdx::builtin( - "catch_unwind".into(), - FnSig::new( - [ - Type::FnPtr(ctx.sig([uint8_ptr], Type::Void)), - uint8_ptr, - Type::FnPtr(ctx.sig([uint8_ptr, uint8_ptr], Type::Void)), - ] - .into(), - Type::Int(Int::I32), - ), - true - ), + ctx.alloc_methodref(catch_unwind), [try_fn, data_ptr, catch_fn] ), ctx, @@ -1627,37 +1673,39 @@ fn intrinsic_slow<'tcx>( .expect("needs_drop works only on types!"), ); let tpe = ctx.type_from_cache(tpe); - let sig = FnSig::new( - [ClassRef::runtime_type_hadle(ctx).into()].into(), - ClassRef::type_type(ctx).into(), + let type_type = ClassRef::type_type(ctx); + let rt_type_handle = ClassRef::runtime_type_hadle(ctx); + let sig = ctx.sig([rt_type_handle.into()], type_type); + let gethash_sig = ctx.sig([type_type.into()], Type::Int(Int::I32)); + let op_implict = MethodRef::new( + (ClassRef::uint_128(ctx)), + ctx.alloc_string("op_Implicit"), + ctx.sig([Type::Int(Int::U32)], Type::Int(Int::U128)), + MethodKind::Static, + vec![].into(), ); - let gethash_sig = FnSig::new( - [ClassRef::type_type(ctx).into()].into(), - Type::Int(Int::I32), + let hash_code = MethodRef::new( + ClassRef::object(ctx).into(), + ctx.alloc_string("GetHashCode"), + gethash_sig, + MethodKind::Virtual, + vec![].into(), + ); + let type_handle = MethodRef::new( + ClassRef::type_type(ctx).into(), + ctx.alloc_string("GetTypeFromHandle"), + sig, + MethodKind::Static, + vec![].into(), ); place_set( destination, call!( - MethodRefIdx::boxed( - Some(ClassRef::uint_128(ctx)), - "op_Implicit".into(), - FnSig::new([Type::Int(Int::U32)].into(), Type::Int(Int::U128)), - true, - ), + ctx.alloc_methodref(op_implict), [conv_u32!(call_virt!( - MethodRefIdx::boxed( - ClassRef::object(ctx).into(), - "GetHashCode".into(), - gethash_sig, - false, - ), + ctx.alloc_methodref(hash_code), [call!( - MethodRefIdx::boxed( - ClassRef::type_type(ctx).into(), - "GetTypeFromHandle".into(), - sig, - true, - ), + ctx.alloc_methodref(type_handle), [CILNode::LDTypeToken(tpe.into())] )] ))] @@ -1692,23 +1740,19 @@ fn intrinsic_slow<'tcx>( ); let tpe = ctx.monomorphize(pointed_ty); let tpe = ctx.type_from_cache(tpe); + let void_ptr = ctx.nptr(Type::Void); + let swap = MethodRef::new( + *ctx.main_module(), + ctx.alloc_string("swap_at_generic"), + ctx.sig([void_ptr, void_ptr, Type::Int(Int::USize)], Type::Void), + MethodKind::Static, + vec![].into(), + ); CILRoot::Call { - site: Box::new(MethodRefIdx::builtin( - "swap_at_generic".into(), - FnSig::new( - [ - ctx.nptr(Type::Void), - ctx.nptr(Type::Void), - Type::Int(Int::USize), - ] - .into(), - Type::Void, - ), - true, - )), + site: ctx.alloc_methodref(swap), args: [ - handle_operand(&args[0].node, ctx).cast_ptr(ctx.nptr(Type::Void)), - handle_operand(&args[1].node, ctx).cast_ptr(ctx.nptr(Type::Void)), + handle_operand(&args[0].node, ctx).cast_ptr(void_ptr), + handle_operand(&args[1].node, ctx).cast_ptr(void_ptr), conv_usize!(size_of!(tpe)), ] .into(), From 18018fbe81ad80a702c7746793fefa6d90c2ef7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Thu, 3 Oct 2024 00:02:23 +0200 Subject: [PATCH 07/10] Cleanup --- src/binop/cmp.rs | 2 +- src/binop/shift.rs | 2 +- src/casts.rs | 62 ++++++++++++------------- src/constant.rs | 58 +++++++++++------------ src/place/adress.rs | 6 +-- src/place/body.rs | 10 ++-- src/place/get.rs | 6 +-- src/place/set.rs | 6 +-- src/terminator/call.rs | 14 +++--- src/terminator/intrinsics/mod.rs | 24 +++++----- src/terminator/intrinsics/saturating.rs | 50 ++++++++++---------- src/terminator/intrinsics/utilis.rs | 22 ++++----- src/type/type.rs | 2 +- src/unop.rs | 6 +-- src/utilis/adt.rs | 6 +-- 15 files changed, 138 insertions(+), 138 deletions(-) diff --git a/src/binop/cmp.rs b/src/binop/cmp.rs index 24450e87..f6e385a5 100644 --- a/src/binop/cmp.rs +++ b/src/binop/cmp.rs @@ -2,7 +2,7 @@ use cilly::{ call, cil_node::CILNode, eq, gt, gt_un, lt, lt_un, - v2::{cilnode::MethodKind, Assembly, ClassRef, Float, FnSig, Int, MethodRef}, + v2::{cilnode::MethodKind, Assembly, ClassRef, Float, Int, MethodRef}, Type, }; use rustc_middle::ty::{FloatTy, IntTy, Ty, TyKind, UintTy}; diff --git a/src/binop/shift.rs b/src/binop/shift.rs index d67a64cd..22ade659 100644 --- a/src/binop/shift.rs +++ b/src/binop/shift.rs @@ -5,7 +5,7 @@ use cilly::{ call, cil_node::CILNode, conv_i32, conv_u32, ldc_u32, rem_un, shl, shr, shr_un, - v2::{cilnode::MethodKind, ClassRef, FnSig, Int, MethodRef}, + v2::{cilnode::MethodKind, ClassRef, Int, MethodRef}, Type, }; diff --git a/src/casts.rs b/src/casts.rs index 57170ced..2d278613 100644 --- a/src/casts.rs +++ b/src/casts.rs @@ -37,7 +37,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::int_128(asm), asm.alloc_string("op_Implicit"), - asm.sig(([Type::Int(Int::ISize)]), Type::Int(Int::I128)), + asm.sig([Type::Int(Int::ISize)], Type::Int(Int::I128)), MethodKind::Static, vec![].into(), ); @@ -47,7 +47,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::int_128(asm), asm.alloc_string("op_Implicit"), - asm.sig(([Type::Int(Int::U32)]), Type::Int(Int::I128)), + asm.sig([Type::Int(Int::U32)], Type::Int(Int::I128)), MethodKind::Static, vec![].into(), ); @@ -67,7 +67,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::uint_128(asm), asm.alloc_string("op_Explicit"), - asm.sig(([Type::Int(Int::I32)]), Type::Int(Int::U128)), + asm.sig([Type::Int(Int::I32)], Type::Int(Int::U128)), MethodKind::Static, vec![].into(), ); @@ -77,7 +77,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::int_128(asm), asm.alloc_string("op_Implicit"), - asm.sig(([Type::Int(Int::I32)]), Type::Int(Int::I128)), + asm.sig([Type::Int(Int::I32)], Type::Int(Int::I128)), MethodKind::Static, vec![].into(), ); @@ -96,7 +96,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::uint_128(asm), asm.alloc_string("op_Explicit"), - asm.sig(([src]), target), + asm.sig([src], target), MethodKind::Static, vec![].into(), ); @@ -106,7 +106,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::int_128(asm), asm.alloc_string("op_Implicit"), - asm.sig(([src]), target), + asm.sig([src], target), MethodKind::Static, vec![].into(), ); @@ -116,7 +116,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::int_128(asm), asm.alloc_string("op_Explicit"), - asm.sig(([src]), target), + asm.sig([src], target), MethodKind::Static, vec![].into(), ); @@ -126,7 +126,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::uint_128(asm), asm.alloc_string("op_Implicit"), - asm.sig(([src]), target), + asm.sig([src], target), MethodKind::Static, vec![].into(), ); @@ -136,7 +136,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::int_128(asm), asm.alloc_string("op_Explicit"), - asm.sig(([src]), target), + asm.sig([src], target), MethodKind::Static, vec![].into(), ); @@ -146,7 +146,7 @@ pub fn int_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembly) let mref = MethodRef::new( ClassRef::uint_128(asm), asm.alloc_string("op_Explicit"), - asm.sig(([src]), target), + asm.sig([src], target), MethodKind::Static, vec![].into(), ); @@ -163,7 +163,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( ClassRef::int_128(asm), asm.alloc_string("op_Explicit"), - asm.sig(([src]), target), + asm.sig([src], target), MethodKind::Static, vec![].into(), ); @@ -173,7 +173,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( ClassRef::uint_128(asm), asm.alloc_string("op_Explicit"), - asm.sig(([src]), target), + asm.sig([src], target), MethodKind::Static, vec![].into(), ); @@ -184,7 +184,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f32_u8"), - asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::U8)), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::U8)), MethodKind::Static, vec![].into(), ); @@ -194,7 +194,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_u8"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::U8)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::U8)), MethodKind::Static, vec![].into(), ); @@ -207,7 +207,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f32_u16"), - asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::U16)), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::U16)), MethodKind::Static, vec![].into(), ); @@ -217,7 +217,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_u16"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::U16)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::U16)), MethodKind::Static, vec![].into(), ); @@ -240,7 +240,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_u32"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::U32)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::U32)), MethodKind::Static, vec![].into(), ); @@ -253,7 +253,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f32_u64"), - asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::U64)), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::U64)), MethodKind::Static, vec![].into(), ); @@ -263,7 +263,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_u64"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::U64)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::U64)), MethodKind::Static, vec![].into(), ); @@ -277,7 +277,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f32_usize"), - asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::USize)), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::USize)), MethodKind::Static, vec![].into(), ); @@ -288,7 +288,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_usize"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::USize)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::USize)), MethodKind::Static, vec![].into(), ); @@ -302,7 +302,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f32_isize"), - asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::ISize)), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::ISize)), MethodKind::Static, vec![].into(), ); @@ -312,7 +312,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_isize"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::ISize)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::ISize)), MethodKind::Static, vec![].into(), ); @@ -325,7 +325,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f32_i8"), - asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::I8)), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::I8)), MethodKind::Static, vec![].into(), ); @@ -335,7 +335,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_i8"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::I8)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::I8)), MethodKind::Static, vec![].into(), ); @@ -348,7 +348,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f32_i16"), - asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::I16)), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::I16)), MethodKind::Static, vec![].into(), ); @@ -358,7 +358,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_i16"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::I16)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::I16)), MethodKind::Static, vec![].into(), ); @@ -371,7 +371,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f32_i32"), - asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::I32)), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::I32)), MethodKind::Static, vec![].into(), ); @@ -381,7 +381,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_i32"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::I32)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::I32)), MethodKind::Static, vec![].into(), ); @@ -394,7 +394,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f32_i64"), - asm.sig(([Type::Float(Float::F32)]), Type::Int(Int::I64)), + asm.sig([Type::Float(Float::F32)], Type::Int(Int::I64)), MethodKind::Static, vec![].into(), ); @@ -404,7 +404,7 @@ pub fn float_to_int(src: Type, target: Type, operand: CILNode, asm: &mut Assembl let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("cast_f64_i64"), - asm.sig(([Type::Float(Float::F64)]), Type::Int(Int::I64)), + asm.sig([Type::Float(Float::F64)], Type::Int(Int::I64)), MethodKind::Static, vec![].into(), ); diff --git a/src/constant.rs b/src/constant.rs index 3026908a..9b967d34 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -10,7 +10,7 @@ use cilly::{ v2::{ cilnode::MethodKind, hashable::{HashableF32, HashableF64}, - Assembly, ClassRef, FieldDesc, Float, FnSig, Int, MethodRef, MethodRefIdx, StaticFieldDesc, + Assembly, ClassRef, FieldDesc, Float, Int, MethodRef, MethodRefIdx, StaticFieldDesc, }, Type, }; @@ -340,11 +340,11 @@ fn load_const_float(value: u128, float_type: FloatTy, asm: &mut Assembly) -> CIL let high = (value >> 64) as u64; let f128_ref = asm.nref(Type::Float(Float::F128)); let ctor_sig = asm.sig( - ([f128_ref, Type::Int(Int::U64), Type::Int(Int::U64)]), + [f128_ref, Type::Int(Int::U64), Type::Int(Int::U64)], Type::Void, ); let cctor = MethodRef::new( - (ClassRef::int_128(asm)), + ClassRef::int_128(asm), asm.alloc_string(".ctor"), ctor_sig, MethodKind::Constructor, @@ -398,7 +398,7 @@ pub fn load_const_int(value: u128, int_type: IntTy, asm: &mut Assembly) -> CILNo let high = (value >> 64) as u64; let int128_ref = asm.nref(Type::Int(Int::I128)); let ctor_sig = asm.sig( - ([int128_ref, Type::Int(Int::U64), Type::Int(Int::U64)]), + [int128_ref, Type::Int(Int::U64), Type::Int(Int::U64)], Type::Void, ); let mref = MethodRef::new( @@ -437,7 +437,7 @@ pub fn load_const_uint(value: u128, int_type: UintTy, asm: &mut Assembly) -> CIL let high = (value >> 64) as u64; let u128_ptr = asm.nref(Type::Int(Int::U128)); let ctor_sig = asm.sig( - ([u128_ptr, Type::Int(Int::U64), Type::Int(Int::U64)]), + [u128_ptr, Type::Int(Int::U64), Type::Int(Int::U64)], Type::Void, ); let mref = MethodRef::new( @@ -464,78 +464,78 @@ fn get_fn_from_static_name(name: &str, ctx: &mut MethodCompileCtx<'_, '_>) -> Me let int8_ptr_ptr = ctx.nptr(int8_ptr); let mref = match name { "statx" => { - (MethodRef::new( + MethodRef::new( *ctx.main_module(), ctx.alloc_string("statx"), ctx.sig( - ([ + [ Type::Int(Int::I32), int8_ptr, Type::Int(Int::I32), Type::Int(Int::U32), void_ptr, - ]), + ], Type::Int(Int::I32), ), MethodKind::Static, vec![].into(), - )) + ) } "getrandom" => { - (MethodRef::new( + MethodRef::new( *ctx.main_module(), ctx.alloc_string("getrandom"), ctx.sig( - ([int8_ptr, Type::Int(Int::USize), Type::Int(Int::U32)]), + [int8_ptr, Type::Int(Int::USize), Type::Int(Int::U32)], Type::Int(Int::USize), ), MethodKind::Static, vec![].into(), - )) + ) } "posix_spawn" => { - (MethodRef::new( + MethodRef::new( *ctx.main_module(), ctx.alloc_string("posix_spawn"), ctx.sig( - ([int8_ptr, int8_ptr, int8_ptr, int8_ptr, int8_ptr, int8_ptr]), + [int8_ptr, int8_ptr, int8_ptr, int8_ptr, int8_ptr, int8_ptr], Type::Int(Int::I32), ), MethodKind::Static, vec![].into(), - )) + ) } "posix_spawn_file_actions_addchdir_np" => { - (MethodRef::new( + MethodRef::new( *ctx.main_module(), ctx.alloc_string("posix_spawn_file_actions_addchdir_np"), - ctx.sig(([int8_ptr, int8_ptr]), Type::Int(Int::I32)), + ctx.sig([int8_ptr, int8_ptr], Type::Int(Int::I32)), MethodKind::Static, vec![].into(), - )) + ) } "__dso_handle" => { - (MethodRef::new( + MethodRef::new( *ctx.main_module(), ctx.alloc_string("__dso_handle"), - ctx.sig(([]), Type::Void), + ctx.sig([], Type::Void), MethodKind::Static, vec![].into(), - )) + ) } "__cxa_thread_atexit_impl" => { let fn_ptr_sig = Type::FnPtr(ctx.sig([void_ptr], Type::Void)); - (MethodRef::new( + MethodRef::new( *ctx.main_module(), ctx.alloc_string("__cxa_thread_atexit_impl"), ctx.sig([fn_ptr_sig, void_ptr, void_ptr], Type::Void), MethodKind::Static, vec![].into(), - )) + ) } "copy_file_range" => { let i64_ptr = ctx.nptr(Type::Int(Int::I64)); - (MethodRef::new( + MethodRef::new( *ctx.main_module(), ctx.alloc_string("copy_file_range"), ctx.sig( @@ -551,12 +551,12 @@ fn get_fn_from_static_name(name: &str, ctx: &mut MethodCompileCtx<'_, '_>) -> Me ), MethodKind::Static, vec![].into(), - )) + ) } "pidfd_spawnp" => { let i32_ptr = ctx.nptr(Type::Int(Int::I32)); let i8_ptr = ctx.nptr(Type::Int(Int::I8)); - (MethodRef::new( + MethodRef::new( *ctx.main_module(), ctx.alloc_string("pidfd_spawnp"), ctx.sig( @@ -572,16 +572,16 @@ fn get_fn_from_static_name(name: &str, ctx: &mut MethodCompileCtx<'_, '_>) -> Me ), MethodKind::Static, vec![].into(), - )) + ) } "pidfd_getpid" => { - (MethodRef::new( + MethodRef::new( *ctx.main_module(), ctx.alloc_string("pidfd_getpid"), ctx.sig([Type::Int(Int::I32)], Type::Int(Int::I32)), MethodKind::Static, vec![].into(), - )) + ) } _ => { todo!("Unsuported function refered to using a weak static. Function name is {name:?}.") diff --git a/src/place/adress.rs b/src/place/adress.rs index 701e1540..ee82175f 100644 --- a/src/place/adress.rs +++ b/src/place/adress.rs @@ -9,7 +9,7 @@ use cilly::{ cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, ldc_u32, ldc_u64, size_of, - v2::{cilnode::MethodKind, FieldDesc, FnSig, Int, MethodRef}, + v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef}, Type, }; use rustc_middle::{ @@ -237,7 +237,7 @@ pub fn place_elem_adress<'tcx>( let arr_ref = ctx.nref(array_type); let element_ptr = ctx.nptr(element_type); let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("get_Address"), ctx.sig([arr_ref, Type::Int(Int::USize)], element_ptr), MethodKind::Instance, @@ -359,7 +359,7 @@ pub fn place_elem_adress<'tcx>( let arr_ref = ctx.nref(array_type); let element_ptr = ctx.nptr(element); let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("get_Address"), ctx.sig([arr_ref, Type::Int(Int::USize)], element_ptr), MethodKind::Instance, diff --git a/src/place/body.rs b/src/place/body.rs index 0f0c9604..3af9ab03 100644 --- a/src/place/body.rs +++ b/src/place/body.rs @@ -11,7 +11,7 @@ use cilly::{ cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, size_of, - v2::{cilnode::MethodKind, FieldDesc, FnSig, Int, MethodRef}, + v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef}, Type, }; use rustc_middle::mir::PlaceElem; @@ -150,7 +150,7 @@ pub fn place_elem_body_index<'tcx>( if body_ty_is_by_adress(element, ctx) { let elem_ptr = ctx.nptr(element_type); let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("get_Address"), ctx.sig([arr_ref, Type::Int(Int::USize)], elem_ptr), MethodKind::Instance, @@ -163,7 +163,7 @@ pub fn place_elem_body_index<'tcx>( ((element).into(), ops) } else { let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("get_Item"), ctx.sig([arr_ref, Type::Int(Int::USize)], element_type), MethodKind::Instance, @@ -269,7 +269,7 @@ pub fn place_elem_body<'tcx>( if body_ty_is_by_adress(element_ty, ctx) { let elem_ptr = ctx.nptr(element); let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("get_Address"), ctx.sig([arr_ref, Type::Int(Int::USize)], elem_ptr), MethodKind::Instance, @@ -282,7 +282,7 @@ pub fn place_elem_body<'tcx>( ((element_ty).into(), ops) } else { let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("get_Item"), ctx.sig([arr_ref, Type::Int(Int::USize)], element), MethodKind::Instance, diff --git a/src/place/get.rs b/src/place/get.rs index 849bb58a..05aa7a3c 100644 --- a/src/place/get.rs +++ b/src/place/get.rs @@ -3,7 +3,7 @@ use cilly::{ call, cil_node::CILNode, conv_usize, ld_field, ldc_u32, ldc_u64, - v2::{cilnode::MethodKind, FieldDesc, FnSig, Int, MethodRef}, + v2::{cilnode::MethodKind, FieldDesc, Int, MethodRef}, Type, }; use rustc_middle::{ @@ -158,7 +158,7 @@ fn place_elem_get<'a>( let array_dotnet = array_type.as_class_ref().expect("Non array type"); let arr_ref = ctx.nref(array_type); let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("get_Item"), ctx.sig([arr_ref, Type::Int(Int::USize)], element), MethodKind::Instance, @@ -227,7 +227,7 @@ fn place_elem_get<'a>( } else { let index = CILNode::LdcU64(*offset); let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("get_Item"), ctx.sig([arr_ref, Type::Int(Int::USize)], element), MethodKind::Instance, diff --git a/src/place/set.rs b/src/place/set.rs index 6e26c82a..1c6aacae 100644 --- a/src/place/set.rs +++ b/src/place/set.rs @@ -8,7 +8,7 @@ use cilly::{ cil_node::CILNode, cil_root::CILRoot, conv_usize, ld_field, ldc_u64, size_of, - v2::{cilnode::MethodKind, ClassRef, FieldDesc, FnSig, Int, MethodRef}, + v2::{cilnode::MethodKind, ClassRef, FieldDesc, Int, MethodRef}, Type, }; use rustc_middle::{ @@ -105,7 +105,7 @@ pub fn place_elem_set<'a>( let array_dotnet = array_type.as_class_ref().expect("Non array type"); let arr_ref = ctx.nref(array_type); let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("set_Item"), ctx.sig([arr_ref, Type::Int(Int::USize), element_type], Type::Void), MethodKind::Instance, @@ -178,7 +178,7 @@ pub fn place_elem_set<'a>( let array_dotnet = array_type.as_class_ref().expect("Non array type"); let arr_ref = ctx.nref(array_type); let mref = MethodRef::new( - (array_dotnet), + array_dotnet, ctx.alloc_string("set_Item"), ctx.sig([arr_ref, Type::Int(Int::USize), element], Type::Void), MethodKind::Instance, diff --git a/src/terminator/call.rs b/src/terminator/call.rs index 50a6a2f7..9ab43aa0 100644 --- a/src/terminator/call.rs +++ b/src/terminator/call.rs @@ -57,9 +57,9 @@ fn call_managed<'tcx>( if argument_count == 0 { let ret = cilly::Type::Void; let call_site = MethodRef::new( - (ctx.alloc_class_ref(tpe)), + ctx.alloc_class_ref(tpe), ctx.alloc_string(managed_fn_name), - ctx.sig(([]), ret), + ctx.sig([], ret), MethodKind::Static, vec![].into(), ); @@ -80,7 +80,7 @@ fn call_managed<'tcx>( call_args.push(crate::operand::handle_operand(&arg.node, ctx)); } let call = MethodRef::new( - (ctx.alloc_class_ref(tpe)), + ctx.alloc_class_ref(tpe), ctx.alloc_string(managed_fn_name), ctx.alloc_sig(signature.clone()), if is_static { @@ -131,9 +131,9 @@ fn callvirt_managed<'tcx>( if argument_count == 0 { let ret = cilly::Type::Void; let call = MethodRef::new( - (ctx.alloc_class_ref(tpe)), + ctx.alloc_class_ref(tpe), ctx.alloc_string(managed_fn_name), - ctx.sig(([]), ret), + ctx.sig([], ret), MethodKind::Static, vec![].into(), ); @@ -154,7 +154,7 @@ fn callvirt_managed<'tcx>( call_args.push(crate::operand::handle_operand(&arg.node, ctx)); } let call = MethodRef::new( - (ctx.alloc_class_ref(tpe)), + ctx.alloc_class_ref(tpe), ctx.alloc_string(managed_fn_name), ctx.alloc_sig(signature.clone()), if is_static { @@ -236,7 +236,7 @@ fn call_ctor<'tcx>( call.push(crate::operand::handle_operand(&arg.node, ctx)); } let ctor = MethodRef::new( - (tpe), + tpe, ctx.alloc_string(".ctor"), sig, MethodKind::Constructor, diff --git a/src/terminator/intrinsics/mod.rs b/src/terminator/intrinsics/mod.rs index 18cabe83..7158e426 100644 --- a/src/terminator/intrinsics/mod.rs +++ b/src/terminator/intrinsics/mod.rs @@ -11,7 +11,7 @@ use cilly::{ cilnode::MethodKind, conv_f32, conv_f64, conv_i16, conv_i32, conv_i64, conv_i8, conv_isize, conv_u16, conv_u32, conv_u64, conv_u8, conv_usize, eq, ld_field, ldc_i32, ldc_u32, ldc_u64, size_of, sub, - v2::{ClassRef, Float, FnSig, Int}, + v2::{ClassRef, Float, Int}, MethodRef, Type, }; use ints::{ctlz, rotate_left, rotate_right}; @@ -296,7 +296,7 @@ pub fn handle_intrinsic<'tcx>( let sig = ctx.sig([runtime_handle.into()], type_type); let gethash_sig = ctx.sig([type_type.into()], Type::Int(Int::I32)); let op_implict = MethodRef::new( - (ClassRef::uint_128(ctx)), + ClassRef::uint_128(ctx), ctx.alloc_string("op_Implicit"), ctx.sig([Type::Int(Int::U32)], Type::Int(Int::U128)), MethodKind::Static, @@ -443,7 +443,7 @@ pub fn handle_intrinsic<'tcx>( Type::Ptr(_) => { let usize_ref = ctx.nref(Type::Int(Int::USize)); let call_site = MethodRef::new( - (interlocked), + interlocked, ctx.alloc_string("CompareExchange"), ctx.sig( [usize_ref, Type::Int(Int::USize), Type::Int(Int::USize)], @@ -467,7 +467,7 @@ pub fn handle_intrinsic<'tcx>( _ => { let src_ref = ctx.nref(src_type); let call_site = MethodRef::new( - (interlocked), + interlocked, ctx.alloc_string("CompareExchange"), ctx.sig([src_ref, src_type, src_type], src_type), MethodKind::Static, @@ -582,7 +582,7 @@ pub fn handle_intrinsic<'tcx>( | "atomic_fence_acqrel" => { let thread = ClassRef::thread(ctx); let fence = MethodRef::new( - (thread), + thread, ctx.alloc_string("MemoryBarrier"), ctx.sig([], Type::Void), MethodKind::Static, @@ -700,7 +700,7 @@ pub fn handle_intrinsic<'tcx>( Type::Ptr(_) => { let usize_ref = ctx.nref(Type::Int(Int::USize)); let call_site = MethodRef::new( - (interlocked), + interlocked, ctx.alloc_string("Exchange"), ctx.sig([usize_ref, Type::Int(Int::USize)], Type::Int(Int::USize)), MethodKind::Static, @@ -726,7 +726,7 @@ pub fn handle_intrinsic<'tcx>( } let src_ref = ctx.nref(src_type); let call_site = MethodRef::new( - (interlocked), + interlocked, ctx.alloc_string("Exchange"), ctx.sig([src_ref, src_type], src_type), MethodKind::Static, @@ -852,7 +852,7 @@ pub fn handle_intrinsic<'tcx>( "The intrinsic `sqrtf32` MUST take in exactly 1 argument!" ); let sqrt = MethodRef::new( - (ClassRef::mathf(ctx)), + ClassRef::mathf(ctx), ctx.alloc_string("Sqrt"), ctx.sig([Type::Float(Float::F32)], Type::Float(Float::F32)), MethodKind::Static, @@ -875,7 +875,7 @@ pub fn handle_intrinsic<'tcx>( "The intrinsic `powif32` MUST take in exactly 2 arguments!" ); let pow = MethodRef::new( - (ClassRef::single(ctx)), + ClassRef::single(ctx), ctx.alloc_string("Pow"), ctx.sig( [Type::Float(Float::F32), Type::Float(Float::F32)], @@ -903,7 +903,7 @@ pub fn handle_intrinsic<'tcx>( "The intrinsic `powif64` MUST take in exactly 2 arguments!" ); let pow = MethodRef::new( - (ClassRef::double(ctx)), + ClassRef::double(ctx), ctx.alloc_string("Pow"), ctx.sig( [Type::Float(Float::F64), Type::Float(Float::F64)], @@ -1571,7 +1571,7 @@ pub fn handle_intrinsic<'tcx>( "The intrinsic `sqrtf64` MUST take in exactly 1 argument!" ); let sqrt = MethodRef::new( - (ClassRef::math(ctx)), + ClassRef::math(ctx), ctx.alloc_string("Sqrt"), ctx.sig([Type::Float(Float::F64)], Type::Float(Float::F64)), MethodKind::Static, @@ -1678,7 +1678,7 @@ fn intrinsic_slow<'tcx>( let sig = ctx.sig([rt_type_handle.into()], type_type); let gethash_sig = ctx.sig([type_type.into()], Type::Int(Int::I32)); let op_implict = MethodRef::new( - (ClassRef::uint_128(ctx)), + ClassRef::uint_128(ctx), ctx.alloc_string("op_Implicit"), ctx.sig([Type::Int(Int::U32)], Type::Int(Int::U128)), MethodKind::Static, diff --git a/src/terminator/intrinsics/saturating.rs b/src/terminator/intrinsics/saturating.rs index b2f01b5a..756a96ab 100644 --- a/src/terminator/intrinsics/saturating.rs +++ b/src/terminator/intrinsics/saturating.rs @@ -5,7 +5,7 @@ use cilly::{ cil_root::CILRoot, cilnode::MethodKind, conv_i16, conv_i32, conv_i64, conv_i8, ldc_i32, ldc_i64, - v2::{ClassRef, FnSig, Int}, + v2::{ClassRef, Int}, MethodRef, Type, }; @@ -50,11 +50,11 @@ pub fn saturating_add<'tcx>( ClassRef::math(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I64), Type::Int(Int::I64), Type::Int(Int::I64), - ]), + ], Type::Int(Int::I64), ), MethodKind::Static, @@ -78,7 +78,7 @@ pub fn saturating_add<'tcx>( ClassRef::int_128(ctx), ctx.alloc_string("op_Addition"), ctx.sig( - ([Type::Int(Int::I128), Type::Int(Int::I128)]), + [Type::Int(Int::I128), Type::Int(Int::I128)], Type::Int(Int::I128), ), MethodKind::Static, @@ -89,11 +89,11 @@ pub fn saturating_add<'tcx>( ClassRef::int_128(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I128), Type::Int(Int::I128), Type::Int(Int::I128), - ]), + ], Type::Int(Int::I128), ), MethodKind::Static, @@ -118,7 +118,7 @@ pub fn saturating_add<'tcx>( ClassRef::int_128(ctx), ctx.alloc_string("op_Addition"), ctx.sig( - ([Type::Int(Int::I128), Type::Int(Int::I128)]), + [Type::Int(Int::I128), Type::Int(Int::I128)], Type::Int(Int::I128), ), MethodKind::Static, @@ -129,11 +129,11 @@ pub fn saturating_add<'tcx>( ClassRef::int_128(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I128), Type::Int(Int::I128), Type::Int(Int::I128), - ]), + ], Type::Int(Int::I128), ), MethodKind::Static, @@ -164,11 +164,11 @@ pub fn saturating_add<'tcx>( ClassRef::math(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I32), Type::Int(Int::I32), Type::Int(Int::I32), - ]), + ], Type::Int(Int::I32), ), MethodKind::Static, @@ -192,11 +192,11 @@ pub fn saturating_add<'tcx>( ClassRef::math(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I32), Type::Int(Int::I32), Type::Int(Int::I32), - ]), + ], Type::Int(Int::I32), ), MethodKind::Static, @@ -244,7 +244,7 @@ pub fn saturating_sub<'tcx>( ClassRef::int_128(ctx), ctx.alloc_string("op_Subtraction"), ctx.sig( - ([Type::Int(Int::I128), Type::Int(Int::I128)]), + [Type::Int(Int::I128), Type::Int(Int::I128)], Type::Int(Int::I128), ), MethodKind::Static, @@ -255,11 +255,11 @@ pub fn saturating_sub<'tcx>( ClassRef::int_128(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I128), Type::Int(Int::I128), Type::Int(Int::I128), - ]), + ], Type::Int(Int::I128), ), MethodKind::Static, @@ -283,7 +283,7 @@ pub fn saturating_sub<'tcx>( ClassRef::int_128(ctx), ctx.alloc_string("op_Subtraction"), ctx.sig( - ([Type::Int(Int::I128), Type::Int(Int::I128)]), + [Type::Int(Int::I128), Type::Int(Int::I128)], Type::Int(Int::I128), ), MethodKind::Static, @@ -294,11 +294,11 @@ pub fn saturating_sub<'tcx>( ClassRef::int_128(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I128), Type::Int(Int::I128), Type::Int(Int::I128), - ]), + ], Type::Int(Int::I128), ), MethodKind::Static, @@ -329,11 +329,11 @@ pub fn saturating_sub<'tcx>( ClassRef::math(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I64), Type::Int(Int::I64), Type::Int(Int::I64), - ]), + ], Type::Int(Int::I64), ), MethodKind::Static, @@ -357,11 +357,11 @@ pub fn saturating_sub<'tcx>( ClassRef::math(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I32), Type::Int(Int::I32), Type::Int(Int::I32), - ]), + ], Type::Int(Int::I32), ), MethodKind::Static, @@ -385,11 +385,11 @@ pub fn saturating_sub<'tcx>( ClassRef::math(ctx), ctx.alloc_string("Clamp"), ctx.sig( - ([ + [ Type::Int(Int::I32), Type::Int(Int::I32), Type::Int(Int::I32), - ]), + ], Type::Int(Int::I32), ), MethodKind::Static, diff --git a/src/terminator/intrinsics/utilis.rs b/src/terminator/intrinsics/utilis.rs index 8b5d1942..3fbe6444 100644 --- a/src/terminator/intrinsics/utilis.rs +++ b/src/terminator/intrinsics/utilis.rs @@ -2,7 +2,7 @@ use cilly::{ call, cil_node::CILNode, cilnode::MethodKind, - v2::{Assembly, ClassRef, FnSig, Int}, + v2::{Assembly, ClassRef, Int}, MethodRef, Type, }; @@ -11,7 +11,7 @@ pub fn atomic_add(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) Type::Int(Int::U64 | Int::I64) => { let u64_ref = asm.nref(Type::Int(Int::U64)); let mref = MethodRef::new( - (ClassRef::interlocked(asm)), + ClassRef::interlocked(asm), asm.alloc_string("Add"), asm.sig([u64_ref, Type::Int(Int::U64)], Type::Int(Int::U64)), MethodKind::Static, @@ -23,9 +23,9 @@ pub fn atomic_add(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) Type::Int(Int::U32 | Int::I32) => { let u32_ref = asm.nref(Type::Int(Int::U32)); let mref = MethodRef::new( - (ClassRef::interlocked(asm)), + ClassRef::interlocked(asm), asm.alloc_string("Add"), - asm.sig(([u32_ref, Type::Int(Int::U32)]), Type::Int(Int::U32)), + asm.sig([u32_ref, Type::Int(Int::U32)], Type::Int(Int::U32)), MethodKind::Static, vec![].into(), ); @@ -57,9 +57,9 @@ pub fn atomic_or(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) Type::Int(Int::U64 | Int::I64) => { let u64_ref = asm.nref(Type::Int(Int::U64)); let mref = MethodRef::new( - (ClassRef::interlocked(asm)), + ClassRef::interlocked(asm), asm.alloc_string("Or"), - asm.sig(([u64_ref, Type::Int(Int::U64)]), Type::Int(Int::U64)), + asm.sig([u64_ref, Type::Int(Int::U64)], Type::Int(Int::U64)), MethodKind::Static, vec![].into(), ); @@ -68,7 +68,7 @@ pub fn atomic_or(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) Type::Int(Int::U32 | Int::I32) => { let u32_ref = asm.nref(Type::Int(Int::U32)); let mref = MethodRef::new( - (ClassRef::interlocked(asm)), + ClassRef::interlocked(asm), asm.alloc_string("Or"), asm.sig([u32_ref, Type::Int(Int::U32)], Type::Int(Int::U32)), MethodKind::Static, @@ -152,7 +152,7 @@ pub fn atomic_and(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) Type::Int(Int::U64 | Int::I64) => { let u64_ref = asm.nref(Type::Int(Int::U64)); let mref = MethodRef::new( - (ClassRef::interlocked(asm)), + ClassRef::interlocked(asm), asm.alloc_string("And"), asm.sig([u64_ref, Type::Int(Int::U64)], Type::Int(Int::U64)), MethodKind::Static, @@ -164,9 +164,9 @@ pub fn atomic_and(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) Type::Int(Int::U32 | Int::I32) => { let u32_ref = asm.nref(Type::Int(Int::U32)); let mref = MethodRef::new( - (ClassRef::interlocked(asm)), + ClassRef::interlocked(asm), asm.alloc_string("And"), - asm.sig(([u32_ref, Type::Int(Int::U32)]), Type::Int(Int::U32)), + asm.sig([u32_ref, Type::Int(Int::U32)], Type::Int(Int::U32)), MethodKind::Static, vec![].into(), ); @@ -212,7 +212,7 @@ pub fn compare_bytes(a: CILNode, b: CILNode, len: CILNode, asm: &mut Assembly) - *asm.main_module(), asm.alloc_string("memcmp"), asm.sig( - ([u8_ref, u8_ref, Type::Int(Int::USize)]), + [u8_ref, u8_ref, Type::Int(Int::USize)], Type::Int(Int::I32), ), MethodKind::Static, diff --git a/src/type/type.rs b/src/type/type.rs index afcec50b..16cd93b8 100644 --- a/src/type/type.rs +++ b/src/type/type.rs @@ -26,7 +26,7 @@ pub fn max_value(tpe: &Type, asm: &mut Assembly) -> CILNode { let mref = MethodRef::new( ClassRef::usize_type(asm), asm.alloc_string("get_MaxValue"), - asm.sig(([]), Type::Int(Int::USize)), + asm.sig([], Type::Int(Int::USize)), MethodKind::Static, vec![].into(), ); diff --git a/src/unop.rs b/src/unop.rs index 93f15f0f..dcd21760 100644 --- a/src/unop.rs +++ b/src/unop.rs @@ -4,7 +4,7 @@ use crate::r#type::get_type; use cilly::cil_node::CILNode; use cilly::v2::cilnode::MethodKind; -use cilly::v2::{ClassRef, FieldDesc, FnSig, Int, MethodRef}; +use cilly::v2::{ClassRef, FieldDesc, Int, MethodRef}; use cilly::{call, ld_field, Type}; use rustc_middle::mir::{Operand, UnOp}; @@ -24,7 +24,7 @@ pub fn unop<'tcx>( let mref = MethodRef::new( ClassRef::int_128(ctx).into(), ctx.alloc_string("op_UnaryNegation"), - ctx.sig(([Type::Int(Int::I128)]), Type::Int(Int::I128)), + ctx.sig([Type::Int(Int::I128)], Type::Int(Int::I128)), MethodKind::Static, vec![].into(), ); @@ -36,7 +36,7 @@ pub fn unop<'tcx>( let mref = MethodRef::new( ClassRef::uint_128(ctx).into(), ctx.alloc_string("op_UnaryNegation"), - ctx.sig(([Type::Int(Int::U128)]), Type::Int(Int::U128)), + ctx.sig([Type::Int(Int::U128)], Type::Int(Int::U128)), MethodKind::Static, vec![].into(), ); diff --git a/src/utilis/adt.rs b/src/utilis/adt.rs index 0f62334d..c777d1f6 100644 --- a/src/utilis/adt.rs +++ b/src/utilis/adt.rs @@ -4,7 +4,7 @@ use cilly::{ cil_root::CILRoot, eq, gt_un, ldc_u64, sub, v2::{ - cilnode::MethodKind, Assembly, ClassRef, ClassRefIdx, FieldDesc, Float, FnSig, Int, + cilnode::MethodKind, Assembly, ClassRef, ClassRefIdx, FieldDesc, Float, Int, MethodRef, }, Type, @@ -311,7 +311,7 @@ pub fn get_discr<'tcx>( let mref = MethodRef::new( ClassRef::uint_128(ctx), ctx.alloc_string("op_Equality"), - ctx.sig(([Type::Int(Int::U128), Type::Int(Int::U128)]), Type::Bool), + ctx.sig([Type::Int(Int::U128), Type::Int(Int::U128)], Type::Bool), MethodKind::Static, vec![].into(), ); @@ -330,7 +330,7 @@ pub fn get_discr<'tcx>( let mref = MethodRef::new( ClassRef::int_128(ctx), ctx.alloc_string("op_Equality"), - ctx.sig(([Type::Int(Int::I128), Type::Int(Int::I128)]), Type::Bool), + ctx.sig([Type::Int(Int::I128), Type::Int(Int::I128)], Type::Bool), MethodKind::Static, vec![].into(), ); From 97d1ccdef0d35580aa3447e3c4f68a18e80f2e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Thu, 3 Oct 2024 00:18:32 +0200 Subject: [PATCH 08/10] Some misc fixies --- cilly/src/cil_node.rs | 209 +++++------------------- cilly/src/cil_root.rs | 2 +- src/assembly.rs | 7 +- src/terminator/intrinsics/ints.rs | 6 +- src/terminator/intrinsics/mod.rs | 1 + src/terminator/intrinsics/saturating.rs | 4 +- src/utilis/adt.rs | 6 +- 7 files changed, 56 insertions(+), 179 deletions(-) diff --git a/cilly/src/cil_node.rs b/cilly/src/cil_node.rs index 63e545b5..f682f1a6 100644 --- a/cilly/src/cil_node.rs +++ b/cilly/src/cil_node.rs @@ -340,173 +340,52 @@ impl CILNode { } #[must_use] - pub fn select(tpe: Type, a: Self, b: Self, predictate: Self) -> Self { - todo!() - } - /* + pub fn select(tpe: Type, a: Self, b: Self, predictate: Self, asm: &mut Assembly) -> Self { match tpe { - Type::Int(Int::U128) => call!( - MethodRefIdx::builtin( - "select_u128".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::U128), Type::Int(Int::U128), Type::Bool]), - Type::Int(Int::U128) - ), - MethodKind::Static, - vec![].into() - ), - [a, b, predictate] - ), - Type::Int(Int::I128) => call!( - MethodRefIdx::builtin( - "select_i128".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::I128), Type::Int(Int::I128), Type::Bool]), - Type::Int(Int::I128) - ), - MethodKind::Static, - vec![].into() - ), - [a, b, predictate] - ), - Type::Int(Int::USize) => call!( - MethodRefIdx::builtin( - "select_usize".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::USize), Type::Int(Int::USize), Type::Bool]), - Type::Int(Int::USize) - ), - MethodKind::Static, - vec![].into() - ), - [a, b, predictate] - ), - Type::Ptr(_) => call!( - MethodRefIdx::builtin( - "select_usize".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::USize), Type::Int(Int::USize), Type::Bool]), - Type::Int(Int::USize) - ), - MethodKind::Static, - vec![].into() - ), - [ - a.cast_ptr(Type::Int(Int::USize)), - b.cast_ptr(Type::Int(Int::USize)), - predictate - ] - ) - .cast_ptr(tpe), - Type::Int(Int::ISize) => call!( - MethodRefIdx::builtin( - "select_isize".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::ISize), Type::Int(Int::ISize), Type::Bool]), - Type::Int(Int::ISize) - ), - MethodKind::Static, - vec![].into() - ), - [a, b, predictate] - ), - Type::Int(Int::U64) => call!( - MethodRefIdx::builtin( - "select_u64".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::U64), Type::Int(Int::U64), Type::Bool]), - Type::Int(Int::U64) - ), - MethodKind::Static, - vec![].into() - ), - [a, b, predictate] - ), - Type::Int(Int::I64) => call!( - MethodRefIdx::builtin( - "select_i64".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::I64), Type::Int(Int::I64), Type::Bool]), - Type::Int(Int::I64) - ), - MethodKind::Static, - vec![].into() - ), - [a, b, predictate] - ), - Type::Int(Int::U32) => call!( - MethodRefIdx::builtin( - "select_u32".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::U32), Type::Int(Int::U32), Type::Bool]), - Type::Int(Int::U32) - ), - MethodKind::Static, - vec![].into() - ), - [a, b, predictate] - ), - Type::Int(Int::I32) => call!( - MethodRefIdx::builtin( - "select_i32".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::I32), Type::Int(Int::I32), Type::Bool]), - Type::Int(Int::I32) - ), - MethodKind::Static, - vec![].into() - ), - [a, b, predictate] - ), - Type::Int(Int::U16) => call!( - MethodRef::new( - "select_u16".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::U16), Type::Int(Int::U16), Type::Bool]), - Type::Int(Int::U16) - ), - MethodKind::Static, - vec![].into() - ), - [a, b, predictate] - ), - Type::Int(Int::I16) => call!( - MethodRefIdx::builtin( - "select_i16".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::I16), Type::Int(Int::I16), Type::Bool]), - Type::Int(Int::I16) - ), - true - ), - [a, b, predictate] - ), - Type::Int(Int::U8) | Type::Bool => call!( - MethodRefIdx::builtin( - "select_u8".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::U8), Type::Int(Int::U8), Type::Bool]), - Type::Int(Int::U8) - ), - true - ), - [a, b, predictate] - ), - Type::Int(Int::I8) => call!( - MethodRefIdx::builtin( - "select_i8".to_owned().into(), - asm.sig( - Box::new([Type::Int(Int::I8), Type::Int(Int::I8), Type::Bool]), - Type::Int(Int::I8) - ), - true - ), - [a, b, predictate] - ), - _ => todo!("Can't select type {tpe:?}"), + Type::Int( + int @ (Int::I8 + | Int::U8 + | Int::I16 + | Int::U16 + | Int::I32 + | Int::U32 + | Int::I64 + | Int::U64 + | Int::I128 + | Int::U128 + | Int::ISize + | Int::USize), + ) => { + let select = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("select_{}", int.name())), + asm.sig([Type::Int(int), Type::Int(int), Type::Bool], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + call!(asm.alloc_methodref(select), [a, b, predictate]) + } + Type::Ptr(_) => { + let int = Int::USize; + let select = MethodRef::new( + *asm.main_module(), + asm.alloc_string(format!("select_{}", int.name())), + asm.sig([Type::Int(int), Type::Int(int), Type::Bool], Type::Int(int)), + MethodKind::Static, + vec![].into(), + ); + call!( + asm.alloc_methodref(select), + [ + a.cast_ptr(Type::Int(int)), + b.cast_ptr(Type::Int(int)), + predictate + ] + ) + } + _ => todo!(), } - }*/ - + } /// Checks if this node may have side effects. `false` means that the node can't have side effects, `true` means that the node *may* have side effects, but it does not have to. pub fn has_side_effects(&self) -> bool { let contains_calls = self.into_iter().call_sites().next().is_some(); diff --git a/cilly/src/cil_root.rs b/cilly/src/cil_root.rs index 4937df44..ddc1e45a 100644 --- a/cilly/src/cil_root.rs +++ b/cilly/src/cil_root.rs @@ -163,7 +163,7 @@ impl CILRoot { class, name, signature, - MethodKind::Instance, + MethodKind::Constructor, vec![].into(), )), args: [CILNode::LdStr(msg.into())].into(), diff --git a/src/assembly.rs b/src/assembly.rs index 16d38f59..0fa0d8ea 100644 --- a/src/assembly.rs +++ b/src/assembly.rs @@ -16,7 +16,7 @@ use cilly::{ cil_node::CILNode, cil_root::CILRoot, cil_tree::CILTree, - conv_usize, ldc_i32, ldc_u32, ldc_u64, + conv_isize, conv_usize, ldc_i32, ldc_u32, ldc_u64, method::{Method, MethodType}, utilis::{self, encode}, v2::{cilnode::MethodKind, FnSig, Int, MethodDef, MethodRef, MethodRefIdx, StaticFieldDesc}, @@ -108,7 +108,6 @@ fn allocation_initializer_method( //trees.push(CILRoot::debug(&format!("Preparing to initialize allocation with size {}",bytes.len())).into()); if align > 8 { let aligned_alloc = MethodRef::aligned_alloc(asm); - trees.push( CILRoot::STLoc { local: 0, @@ -130,9 +129,9 @@ fn allocation_initializer_method( local: 0, tree: Box::new(call!( asm.alloc_methodref(alloc), - [ldc_i32!( + [conv_isize!(ldc_i32!( i32::try_from(bytes.len()).expect("Static alloc too big") - )] + ))] )) .cast_ptr(asm.nptr(Type::Int(Int::U8))), } diff --git a/src/terminator/intrinsics/ints.rs b/src/terminator/intrinsics/ints.rs index 4cd05387..84a09407 100644 --- a/src/terminator/intrinsics/ints.rs +++ b/src/terminator/intrinsics/ints.rs @@ -492,9 +492,9 @@ pub fn bitreverse<'tcx>( Type::Int(Int::I8) => conv_i8!(bitreverse_u8(val)), Type::Int(Int::U16) => bitreverse_u16(val), Type::Int(Int::I16) => conv_i16!(bitreverse_u16(conv_u16!(val))), - Type::Int(int @ (Int::I32 | Int::U32 | Int::I64 | Int::U128 | Int::I128)) => { - bitreverse_int(val, int, ctx) - } + Type::Int( + int @ (Int::I32 | Int::U32 | Int::I64 | Int::U64 | Int::U128 | Int::I128), + ) => bitreverse_int(val, int, ctx), _ => todo!("can't yet bitreverse {val_tpe:?}"), }, ctx, diff --git a/src/terminator/intrinsics/mod.rs b/src/terminator/intrinsics/mod.rs index 7158e426..48386206 100644 --- a/src/terminator/intrinsics/mod.rs +++ b/src/terminator/intrinsics/mod.rs @@ -1782,6 +1782,7 @@ fn intrinsic_slow<'tcx>( handle_operand(&args[1].node, ctx), handle_operand(&args[2].node, ctx), handle_operand(&args[0].node, ctx), + ctx, ), ctx, ) diff --git a/src/terminator/intrinsics/saturating.rs b/src/terminator/intrinsics/saturating.rs index 756a96ab..772894ff 100644 --- a/src/terminator/intrinsics/saturating.rs +++ b/src/terminator/intrinsics/saturating.rs @@ -40,7 +40,7 @@ pub fn saturating_add<'tcx>( let or = crate::binop::bitop::bit_or_unchecked(a_ty, a_ty, ctx, a.clone(), b.clone()); let flag = crate::binop::cmp::lt_unchecked(a_ty, sum.clone(), or.clone(), ctx); let max = crate::r#type::max_value(&a_type, ctx); - CILNode::select(a_type, max, sum, flag) + CILNode::select(a_type, max, sum, flag, ctx) } Type::Int(Int::I32) => { let a = conv_i64!(a); @@ -235,7 +235,7 @@ pub fn saturating_sub<'tcx>( let undeflow = crate::binop::cmp::lt_unchecked(a_ty, a.clone(), b.clone(), ctx); let diff = crate::binop::sub_unchecked(a_ty, a_ty, ctx, a, b); let zero = crate::binop::checked::zero(a_ty, ctx); - CILNode::select(a_type, zero, diff, undeflow) + CILNode::select(a_type, zero, diff, undeflow, ctx) } Type::Int(Int::I64) => { let a = crate::casts::int_to_int(Type::Int(Int::I64), Type::Int(Int::I128), a, ctx); diff --git a/src/utilis/adt.rs b/src/utilis/adt.rs index c777d1f6..6e0b9576 100644 --- a/src/utilis/adt.rs +++ b/src/utilis/adt.rs @@ -3,10 +3,7 @@ use cilly::{ cil_node::CILNode, cil_root::CILRoot, eq, gt_un, ldc_u64, sub, - v2::{ - cilnode::MethodKind, Assembly, ClassRef, ClassRefIdx, FieldDesc, Float, Int, - MethodRef, - }, + v2::{cilnode::MethodKind, Assembly, ClassRef, ClassRefIdx, FieldDesc, Float, Int, MethodRef}, Type, }; use rustc_middle::ty::{AdtDef, Ty}; @@ -478,6 +475,7 @@ pub fn get_discr<'tcx>( ctx, ), is_niche, + ctx, ) } }; From a640014d261f74ababba9908ae941b85ed553a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Sat, 5 Oct 2024 23:39:50 +0200 Subject: [PATCH 09/10] Fixed the name of AlignedAlloc --- cilly/src/utilis.rs | 4 ++-- cilly/src/v2/method.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cilly/src/utilis.rs b/cilly/src/utilis.rs index 5cdadd49..317dbc9d 100644 --- a/cilly/src/utilis.rs +++ b/cilly/src/utilis.rs @@ -334,7 +334,7 @@ pub fn get_environ(asm: &mut Assembly) -> MethodRefIdx { ClassRef::i_collection(asm), asm.alloc_string("get_Count"), asm.sig([i_dictionary], Type::Int(Int::I32)), - MethodKind::Instance, + MethodKind::Virtual, vec![].into(), ); init.trees_mut().push( @@ -371,7 +371,7 @@ pub fn get_environ(asm: &mut Assembly) -> MethodRefIdx { ClassRef::i_dictionary(asm), asm.alloc_string("GetEnumerator"), asm.sig([i_dictionary], Type::ClassRef(dictionary_iterator)), - MethodKind::Instance, + MethodKind::Virtual, vec![].into(), ); init.trees_mut().push( diff --git a/cilly/src/v2/method.rs b/cilly/src/v2/method.rs index 1f19394b..6b75bbac 100644 --- a/cilly/src/v2/method.rs +++ b/cilly/src/v2/method.rs @@ -107,7 +107,7 @@ impl MethodRef { let sig = asm.sig([Type::Int(Int::USize), Type::Int(Int::USize)], void_ptr); MethodRef::new( ClassRef::native_mem(asm), - asm.alloc_string("AllignedAlloc"), + asm.alloc_string("AlignedAlloc"), sig, MethodKind::Static, vec![].into(), From c0ed0a44d0fd917961d7cc20b32344675582a1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Sun, 6 Oct 2024 00:12:50 +0200 Subject: [PATCH 10/10] Some more fixes to environ --- cilly/src/utilis.rs | 4 ++-- src/terminator/intrinsics/utilis.rs | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/cilly/src/utilis.rs b/cilly/src/utilis.rs index 317dbc9d..f9924eda 100644 --- a/cilly/src/utilis.rs +++ b/cilly/src/utilis.rs @@ -408,7 +408,7 @@ pub fn get_environ(asm: &mut Assembly) -> MethodRefIdx { ClassRef::i_enumerator(asm), asm.alloc_string("MoveNext"), asm.sig([Type::ClassRef(dictionary_iterator)], Type::Bool), - MethodKind::Instance, + MethodKind::Virtual, vec![].into(), ); loop_body.trees_mut().push( @@ -423,7 +423,7 @@ pub fn get_environ(asm: &mut Assembly) -> MethodRefIdx { ClassRef::i_enumerator(asm), asm.alloc_string("get_Current"), asm.sig([Type::ClassRef(dictionary_iterator)], Type::PlatformObject), - MethodKind::Instance, + MethodKind::Virtual, vec![].into(), ); loop_body.trees_mut().push( diff --git a/src/terminator/intrinsics/utilis.rs b/src/terminator/intrinsics/utilis.rs index 3fbe6444..d80c0231 100644 --- a/src/terminator/intrinsics/utilis.rs +++ b/src/terminator/intrinsics/utilis.rs @@ -90,7 +90,7 @@ pub fn atomic_or(addr: CILNode, addend: CILNode, tpe: Type, asm: &mut Assembly) } Type::Ptr(inner) => { - let int = Int::ISize; + let int = Int::USize; let int_ref = asm.nref(Type::Int(int)); let mref = MethodRef::new( *asm.main_module(), @@ -211,10 +211,7 @@ pub fn compare_bytes(a: CILNode, b: CILNode, len: CILNode, asm: &mut Assembly) - let mref = MethodRef::new( *asm.main_module(), asm.alloc_string("memcmp"), - asm.sig( - [u8_ref, u8_ref, Type::Int(Int::USize)], - Type::Int(Int::I32), - ), + asm.sig([u8_ref, u8_ref, Type::Int(Int::USize)], Type::Int(Int::I32)), MethodKind::Static, vec![].into(), );