|
| 1 | +use crate::{ |
| 2 | + assembly::Assembly, |
| 3 | + cil::{CILOp, CallSite, FieldDescriptor}, |
| 4 | + function_sig::FnSig, |
| 5 | + method::Method, |
| 6 | + r#type::{DotnetTypeRef, Type}, |
| 7 | +}; |
| 8 | +//TODO: propely mimic the semantics used in atomics! |
| 9 | +pub fn add_atomics(asm: &mut Assembly) { |
| 10 | + add_atomic_load_acquire(asm); |
| 11 | + add_atomic_cxchgweak_acquire_acquire(asm); |
| 12 | +} |
| 13 | +fn add_atomic_load_acquire(asm: &mut Assembly) { |
| 14 | + let atomic_load_acquire_calls: Box<[_]> = asm |
| 15 | + .call_sites() |
| 16 | + .filter(|call_site| { |
| 17 | + call_site.signature().inputs().len() == 1 |
| 18 | + && matches!(call_site.signature().inputs()[0], Type::Ptr(_)) |
| 19 | + && call_site.name().contains("atomic_load_acquire") |
| 20 | + }) |
| 21 | + .cloned() |
| 22 | + .collect(); |
| 23 | + for call in atomic_load_acquire_calls.iter() { |
| 24 | + let mut method = Method::new( |
| 25 | + crate::access_modifier::AccessModifer::Private, |
| 26 | + true, |
| 27 | + call.signature().clone(), |
| 28 | + call.name(), |
| 29 | + vec![], |
| 30 | + ); |
| 31 | + match call.signature().output() { |
| 32 | + //Those can't be implemented using System.Threading.Interlocked, since there is no Read(Uintptr). |
| 33 | + Type::Ptr(_) => method.set_ops(vec![CILOp::LDArg(0), CILOp::LDIndISize, CILOp::Ret]), |
| 34 | + Type::ISize => method.set_ops(vec![CILOp::LDArg(0), CILOp::LDIndISize, CILOp::Ret]), |
| 35 | + Type::USize => method.set_ops(vec![CILOp::LDArg(0), CILOp::LDIndISize, CILOp::Ret]), |
| 36 | + //Those can be implemented using System.Threading.Interlocked. |
| 37 | + Type::I64 => method.set_ops(vec![CILOp::LDArg(0), CILOp::LDIndI64, CILOp::Ret]), |
| 38 | + Type::U64 => method.set_ops(vec![CILOp::LDArg(0), CILOp::LDIndI64, CILOp::Ret]), |
| 39 | + //Those can't be implemented using System.Threading.Interlocked. |
| 40 | + Type::U8 => method.set_ops(vec![CILOp::LDArg(0), CILOp::LDIndI8, CILOp::Ret]), |
| 41 | + Type::I8 => method.set_ops(vec![CILOp::LDArg(0), CILOp::LDIndI8, CILOp::Ret]), |
| 42 | + Type::U32 => method.set_ops(vec![CILOp::LDArg(0), CILOp::LDIndI32, CILOp::Ret]), |
| 43 | + Type::I32 => method.set_ops(vec![CILOp::LDArg(0), CILOp::LDIndI32, CILOp::Ret]), |
| 44 | + _ => { |
| 45 | + eprintln!( |
| 46 | + "Warning: `atomic_load_acquire` used with unsuported type({:?})!", |
| 47 | + call.signature().output() |
| 48 | + ); |
| 49 | + continue; |
| 50 | + } |
| 51 | + } |
| 52 | + asm.add_method(method); |
| 53 | + } |
| 54 | +} |
| 55 | +fn interlocked() -> DotnetTypeRef { |
| 56 | + DotnetTypeRef::new(Some("System.Threading"), "System.Threading.Interlocked") |
| 57 | + .with_valuetype(false) |
| 58 | +} |
| 59 | + |
| 60 | +fn add_atomic_cxchgweak_acquire_acquire(asm: &mut Assembly) { |
| 61 | + let atomic_cxchgweak_acquire_acquire_calls: Box<[_]> = asm |
| 62 | + .call_sites() |
| 63 | + .filter(|call_site| { |
| 64 | + call_site.signature().inputs().len() == 3 |
| 65 | + && call_site |
| 66 | + .name() |
| 67 | + .contains("atomic_cxchgweak_acquire_acquire") |
| 68 | + }) |
| 69 | + .cloned() |
| 70 | + .collect(); |
| 71 | + for call in atomic_cxchgweak_acquire_acquire_calls.iter() { |
| 72 | + let ret_tuple = call.signature().output(); |
| 73 | + let ret_tuple_dotnet = ret_tuple.as_dotnet().expect("atomic_cxchgweak_acquire_acquire: return tuple invalid!"); |
| 74 | + let mut method = Method::new( |
| 75 | + crate::access_modifier::AccessModifer::Private, |
| 76 | + true, |
| 77 | + call.signature().clone(), |
| 78 | + call.name(), |
| 79 | + vec![(None, call.signature().inputs()[1].clone()),(Some("return_tuple".into()),ret_tuple.clone())], |
| 80 | + ); |
| 81 | + match call.signature().inputs()[1]{ |
| 82 | + //Those can't be implemented using System.Threading.Interlocked, since there is no Read(Uintptr). |
| 83 | + Type::USize | Type::ISize | Type::Ptr(_) => { |
| 84 | + let call_sig = FnSig::new( |
| 85 | + &[ |
| 86 | + Type::ManagedReference(Type::USize.into()), |
| 87 | + Type::USize, |
| 88 | + Type::USize, |
| 89 | + ], |
| 90 | + &Type::USize, |
| 91 | + ); |
| 92 | + method.set_ops(vec![ |
| 93 | + |
| 94 | + CILOp::LDArg(0), |
| 95 | + CILOp::LDArg(1), |
| 96 | + CILOp::LDArg(2), |
| 97 | + CILOp::Call( |
| 98 | + CallSite::new( |
| 99 | + Some(interlocked()), |
| 100 | + "CompareExchange".into(), |
| 101 | + call_sig, |
| 102 | + true, |
| 103 | + ) |
| 104 | + .into(), |
| 105 | + ), |
| 106 | + CILOp::STLoc(0), |
| 107 | + CILOp::LDLocA(1), |
| 108 | + CILOp::LDLoc(0), |
| 109 | + CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet.clone(), call.signature().inputs()[1].clone(),"Item1".into()))), |
| 110 | + CILOp::LDLocA(1), |
| 111 | + CILOp::LDLoc(0), |
| 112 | + CILOp::LDArg(1), |
| 113 | + CILOp::Eq, |
| 114 | + CILOp::Not, |
| 115 | + CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet,Type::Bool,"Item2".into()))), |
| 116 | + CILOp::LDLoc(1), |
| 117 | + CILOp::Ret, |
| 118 | + ]); |
| 119 | + } |
| 120 | + Type::U32 | Type::I32 => { |
| 121 | + let call_sig = FnSig::new( |
| 122 | + &[ |
| 123 | + Type::ManagedReference(Type::I32.into()), |
| 124 | + Type::I32, |
| 125 | + Type::I32, |
| 126 | + ], |
| 127 | + &Type::I32, |
| 128 | + ); |
| 129 | + method.set_ops(vec![ |
| 130 | + |
| 131 | + CILOp::LDArg(0), |
| 132 | + CILOp::LDArg(1), |
| 133 | + CILOp::LDArg(2), |
| 134 | + CILOp::Call( |
| 135 | + CallSite::new( |
| 136 | + Some(interlocked()), |
| 137 | + "CompareExchange".into(), |
| 138 | + call_sig, |
| 139 | + true, |
| 140 | + ) |
| 141 | + .into(), |
| 142 | + ), |
| 143 | + CILOp::STLoc(0), |
| 144 | + CILOp::LDLocA(1), |
| 145 | + CILOp::LDLoc(0), |
| 146 | + CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet.clone(), call.signature().inputs()[1].clone(),"Item1".into()))), |
| 147 | + CILOp::LDLocA(1), |
| 148 | + CILOp::LDLoc(0), |
| 149 | + CILOp::LDArg(1), |
| 150 | + CILOp::Eq, |
| 151 | + CILOp::Not, |
| 152 | + CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet,Type::Bool,"Item2".into()))), |
| 153 | + CILOp::LDLoc(1), |
| 154 | + CILOp::Ret, |
| 155 | + ]); |
| 156 | + } |
| 157 | + Type::U64 | Type::I64 => { |
| 158 | + let call_sig = FnSig::new( |
| 159 | + &[ |
| 160 | + Type::ManagedReference(Type::U64.into()), |
| 161 | + Type::U64, |
| 162 | + Type::U64, |
| 163 | + ], |
| 164 | + &Type::U64, |
| 165 | + ); |
| 166 | + method.set_ops(vec![ |
| 167 | + |
| 168 | + CILOp::LDArg(0), |
| 169 | + CILOp::LDArg(1), |
| 170 | + CILOp::LDArg(2), |
| 171 | + CILOp::Call( |
| 172 | + CallSite::new( |
| 173 | + Some(interlocked()), |
| 174 | + "CompareExchange".into(), |
| 175 | + call_sig, |
| 176 | + true, |
| 177 | + ) |
| 178 | + .into(), |
| 179 | + ), |
| 180 | + CILOp::STLoc(0), |
| 181 | + CILOp::LDLocA(1), |
| 182 | + CILOp::LDLoc(0), |
| 183 | + CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet.clone(), call.signature().inputs()[1].clone(),"Item1".into()))), |
| 184 | + CILOp::LDLocA(1), |
| 185 | + CILOp::LDLoc(0), |
| 186 | + CILOp::LDArg(1), |
| 187 | + CILOp::Eq, |
| 188 | + CILOp::Not, |
| 189 | + CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet,Type::Bool,"Item2".into()))), |
| 190 | + CILOp::LDLoc(1), |
| 191 | + CILOp::Ret, |
| 192 | + ]); |
| 193 | + } |
| 194 | + //Type::ISize=> method.set_ops(vec![CILOp::LDArg(0),CILOp::LDIndISize,CILOp::Ret]), |
| 195 | + // Type::USize=> method.set_ops(vec![CILOp::LDArg(0),CILOp::LDIndISize,CILOp::Ret]), |
| 196 | + //Those can be implemented using System.Threading.Interlocked. |
| 197 | + //Type::I64=> method.set_ops(vec![CILOp::LDArg(0),CILOp::LDIndI64,CILOp::Ret]), |
| 198 | + // Type::U64=> method.set_ops(vec![CILOp::LDArg(0),CILOp::LDIndI64,CILOp::Ret]), |
| 199 | + //Those can't be implemented using System.Threading.Interlocked. |
| 200 | + //Type::U8=> method.set_ops(vec![CILOp::LDArg(0),CILOp::LDIndI8,CILOp::Ret]), |
| 201 | + // Type::I8=> method.set_ops(vec![CILOp::LDArg(0),CILOp::LDIndI8,CILOp::Ret]), |
| 202 | + //Type::U32=> method.set_ops(vec![CILOp::LDArg(0),CILOp::LDIndI32,CILOp::Ret]), |
| 203 | + //Type::I32=> method.set_ops(vec![CILOp::LDArg(0),CILOp::LDIndI32,CILOp::Ret]), |
| 204 | + _ => { |
| 205 | + eprintln!( |
| 206 | + "Warning: `atomic_cxchgweak_acquire_acquire` used with unsuported type({:?})!", |
| 207 | + call.signature().inputs()[1] |
| 208 | + ); |
| 209 | + continue; |
| 210 | + } |
| 211 | + } |
| 212 | + asm.add_method(method); |
| 213 | + } |
| 214 | +} |
| 215 | +//TODO: implement cmp by using System.Threading.Interlocked.CompareExchange<T>(T, T, T) |
0 commit comments