Skip to content

Commit 9576ede

Browse files
committed
fixes to constant functions and added some builtins
1 parent 52509a7 commit 9576ede

File tree

3 files changed

+100
-17
lines changed

3 files changed

+100
-17
lines changed

src/cil/call_site.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl CallSite {
7272
if self.class().is_some() {
7373
return false;
7474
};
75-
if self.name.as_ref() != "black_box" {
75+
if self.name.as_ref() != "black_box" && self.name.as_ref() != "assert_inhabited"{
7676
return false;
7777
};
7878
if self.signature.inputs().len() != 1 {

src/constant.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,38 @@ fn create_const_adt_from_bytes<'ctx>(
7676
creator_ops.push(CILOp::FreeTMPLocal);
7777
creator_ops
7878
}
79+
AdtKind::Union => {
80+
let curr_offset = 0;
81+
let cil_ty = crate::utilis::monomorphize(&method_instance, ty, tyctx);
82+
let cil_ty = tycache.type_from_cache(cil_ty, tyctx, Some(method_instance));
83+
let dotnet_ty = cil_ty.as_dotnet().expect("ADT must be a value type!");
84+
let mut creator_ops = vec![CILOp::NewTMPLocal(cil_ty.clone().into())];
85+
for field in adt_def.all_fields() {
86+
let ftype = field.ty(tyctx, subst);
87+
let sizeof = crate::utilis::compiletime_sizeof(ftype, tyctx);
88+
let field_bytes = &bytes[curr_offset..(curr_offset + sizeof)];
89+
let field_ops =
90+
create_const_from_slice(ftype, tyctx, field_bytes, method_instance, tycache);
91+
creator_ops.push(CILOp::LoadAddresOfTMPLocal);
92+
creator_ops.extend(field_ops);
93+
let cil_ftype =
94+
tycache.type_from_cache(field.ty(tyctx, subst), tyctx, Some(method_instance));
95+
let name = field.name.to_string();
96+
let name = crate::r#type::escape_field_name(&name);
97+
creator_ops.push(CILOp::STField(crate::cil::FieldDescriptor::boxed(
98+
dotnet_ty.clone(),
99+
cil_ftype,
100+
name,
101+
)));
102+
rustc_middle::ty::print::with_no_trimmed_paths! {println!(
103+
"Const field {name} of type {ftype} with bytes {field_bytes:?}",
104+
name = field.name
105+
)};
106+
}
107+
creator_ops.push(CILOp::LoadTMPLocal);
108+
creator_ops.push(CILOp::FreeTMPLocal);
109+
creator_ops
110+
}
79111
AdtKind::Enum => {
80112
let variant_size = crate::utilis::enum_tag_size(adt_def.variants().len() as u64);
81113
// This will need to be mutable in order to handle enum fields.
@@ -487,6 +519,31 @@ fn load_const_scalar<'ctx>(
487519
CILOp::FreeTMPLocal,
488520
]
489521
}
522+
AdtKind::Struct => {
523+
//assert!(adt_def.size() < 16);
524+
let low = (scalar_u128 & u128::from(u64::MAX)) as u64;
525+
let high = (scalar_u128 << 64) as u64;
526+
let low = i64::from_ne_bytes(low.to_ne_bytes());
527+
let high = i64::from_ne_bytes(high.to_ne_bytes());
528+
let i128_class = DotnetTypeRef::new(Some("System.Runtime"), "System.Int128");
529+
let ctor_sig =
530+
crate::function_sig::FnSig::new(&[Type::U64, Type::U64], &Type::Void);
531+
vec![
532+
CILOp::LdcI64(high),
533+
CILOp::LdcI64(low),
534+
CILOp::NewObj(CallSite::boxed(
535+
Some(i128_class),
536+
".ctor".into(),
537+
ctor_sig,
538+
true,
539+
)),
540+
CILOp::NewTMPLocal(Type::I128.into()),
541+
CILOp::SetTMPLocal,
542+
CILOp::LoadAddresOfTMPLocal,
543+
CILOp::LdObj(tpe.into()),
544+
CILOp::FreeTMPLocal,
545+
]
546+
}
490547
_ => todo!("Can't load const ADT scalars of type {scalar_type:?}"),
491548
},
492549
TyKind::Char => {
@@ -536,11 +593,11 @@ pub fn load_const_int(value: u128, int_type: &IntTy) -> Vec<CILOp> {
536593
let low = i64::from_ne_bytes(low.to_ne_bytes());
537594
let high = i64::from_ne_bytes(high.to_ne_bytes());
538595
let i128_class = DotnetTypeRef::new(Some("System.Runtime"), "System.Int128");
539-
let ctor_sig = crate::function_sig::FnSig::new(&[Type::U64, Type::U64], &Type::I128);
596+
let ctor_sig = crate::function_sig::FnSig::new(&[Type::U64, Type::U64], &Type::Void);
540597
vec![
541598
CILOp::LdcI64(high),
542599
CILOp::LdcI64(low),
543-
CILOp::Call(CallSite::boxed(
600+
CILOp::NewObj(CallSite::boxed(
544601
Some(i128_class),
545602
".ctor".into(),
546603
ctor_sig,
@@ -578,11 +635,11 @@ pub fn load_const_uint(value: u128, int_type: &UintTy) -> Vec<CILOp> {
578635
let low = i64::from_ne_bytes(low.to_ne_bytes());
579636
let high = i64::from_ne_bytes(high.to_ne_bytes());
580637
let i128_class = DotnetTypeRef::new(Some("System.Runtime"), "System.UInt128");
581-
let ctor_sig = crate::function_sig::FnSig::new(&[Type::U64, Type::U64], &Type::U128);
638+
let ctor_sig = crate::function_sig::FnSig::new(&[Type::U64, Type::U64], &Type::Void);
582639
vec![
583640
CILOp::LdcI64(high),
584641
CILOp::LdcI64(low),
585-
CILOp::Call(CallSite::boxed(
642+
CILOp::NewObj(CallSite::boxed(
586643
Some(i128_class),
587644
".ctor".into(),
588645
ctor_sig,

src/ffi/atomics.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,20 @@ fn add_atomic_cxchgweak_acquire_acquire(asm: &mut Assembly) {
7070
.collect();
7171
for call in atomic_cxchgweak_acquire_acquire_calls.iter() {
7272
let ret_tuple = call.signature().output();
73-
let ret_tuple_dotnet = ret_tuple.as_dotnet().expect("atomic_cxchgweak_acquire_acquire: return tuple invalid!");
73+
let ret_tuple_dotnet = ret_tuple
74+
.as_dotnet()
75+
.expect("atomic_cxchgweak_acquire_acquire: return tuple invalid!");
7476
let mut method = Method::new(
7577
crate::access_modifier::AccessModifer::Private,
7678
true,
7779
call.signature().clone(),
7880
call.name(),
79-
vec![(None, call.signature().inputs()[1].clone()),(Some("return_tuple".into()),ret_tuple.clone())],
81+
vec![
82+
(None, call.signature().inputs()[1].clone()),
83+
(Some("return_tuple".into()), ret_tuple.clone()),
84+
],
8085
);
81-
match call.signature().inputs()[1]{
86+
match call.signature().inputs()[1] {
8287
//Those can't be implemented using System.Threading.Interlocked, since there is no Read(Uintptr).
8388
Type::USize | Type::ISize | Type::Ptr(_) => {
8489
let call_sig = FnSig::new(
@@ -90,7 +95,6 @@ fn add_atomic_cxchgweak_acquire_acquire(asm: &mut Assembly) {
9095
&Type::USize,
9196
);
9297
method.set_ops(vec![
93-
9498
CILOp::LDArg(0),
9599
CILOp::LDArg(1),
96100
CILOp::LDArg(2),
@@ -106,13 +110,21 @@ fn add_atomic_cxchgweak_acquire_acquire(asm: &mut Assembly) {
106110
CILOp::STLoc(0),
107111
CILOp::LDLocA(1),
108112
CILOp::LDLoc(0),
109-
CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet.clone(), call.signature().inputs()[1].clone(),"Item1".into()))),
113+
CILOp::STField(Box::new(FieldDescriptor::new(
114+
ret_tuple_dotnet.clone(),
115+
call.signature().inputs()[1].clone(),
116+
"Item1".into(),
117+
))),
110118
CILOp::LDLocA(1),
111119
CILOp::LDLoc(0),
112120
CILOp::LDArg(1),
113121
CILOp::Eq,
114122
CILOp::Not,
115-
CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet,Type::Bool,"Item2".into()))),
123+
CILOp::STField(Box::new(FieldDescriptor::new(
124+
ret_tuple_dotnet,
125+
Type::Bool,
126+
"Item2".into(),
127+
))),
116128
CILOp::LDLoc(1),
117129
CILOp::Ret,
118130
]);
@@ -127,7 +139,6 @@ fn add_atomic_cxchgweak_acquire_acquire(asm: &mut Assembly) {
127139
&Type::I32,
128140
);
129141
method.set_ops(vec![
130-
131142
CILOp::LDArg(0),
132143
CILOp::LDArg(1),
133144
CILOp::LDArg(2),
@@ -143,13 +154,21 @@ fn add_atomic_cxchgweak_acquire_acquire(asm: &mut Assembly) {
143154
CILOp::STLoc(0),
144155
CILOp::LDLocA(1),
145156
CILOp::LDLoc(0),
146-
CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet.clone(), call.signature().inputs()[1].clone(),"Item1".into()))),
157+
CILOp::STField(Box::new(FieldDescriptor::new(
158+
ret_tuple_dotnet.clone(),
159+
call.signature().inputs()[1].clone(),
160+
"Item1".into(),
161+
))),
147162
CILOp::LDLocA(1),
148163
CILOp::LDLoc(0),
149164
CILOp::LDArg(1),
150165
CILOp::Eq,
151166
CILOp::Not,
152-
CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet,Type::Bool,"Item2".into()))),
167+
CILOp::STField(Box::new(FieldDescriptor::new(
168+
ret_tuple_dotnet,
169+
Type::Bool,
170+
"Item2".into(),
171+
))),
153172
CILOp::LDLoc(1),
154173
CILOp::Ret,
155174
]);
@@ -164,7 +183,6 @@ fn add_atomic_cxchgweak_acquire_acquire(asm: &mut Assembly) {
164183
&Type::U64,
165184
);
166185
method.set_ops(vec![
167-
168186
CILOp::LDArg(0),
169187
CILOp::LDArg(1),
170188
CILOp::LDArg(2),
@@ -180,13 +198,21 @@ fn add_atomic_cxchgweak_acquire_acquire(asm: &mut Assembly) {
180198
CILOp::STLoc(0),
181199
CILOp::LDLocA(1),
182200
CILOp::LDLoc(0),
183-
CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet.clone(), call.signature().inputs()[1].clone(),"Item1".into()))),
201+
CILOp::STField(Box::new(FieldDescriptor::new(
202+
ret_tuple_dotnet.clone(),
203+
call.signature().inputs()[1].clone(),
204+
"Item1".into(),
205+
))),
184206
CILOp::LDLocA(1),
185207
CILOp::LDLoc(0),
186208
CILOp::LDArg(1),
187209
CILOp::Eq,
188210
CILOp::Not,
189-
CILOp::STField(Box::new(FieldDescriptor::new(ret_tuple_dotnet,Type::Bool,"Item2".into()))),
211+
CILOp::STField(Box::new(FieldDescriptor::new(
212+
ret_tuple_dotnet,
213+
Type::Bool,
214+
"Item2".into(),
215+
))),
190216
CILOp::LDLoc(1),
191217
CILOp::Ret,
192218
]);

0 commit comments

Comments
 (0)