Skip to content

Commit

Permalink
Moved constants to v2 nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Oct 22, 2024
1 parent 95caa83 commit 9925ea9
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 124 deletions.
2 changes: 2 additions & 0 deletions cilly/src/v2/c_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,13 @@ impl CExporter {
Const::I16(v) => format!("(int16_t)0x{v:x}"),
Const::I32(v) => format!("((int32_t)0x{v:x})"),
Const::I64(v) => format!("0x{v:x}L"),
Const::I128(v) => todo!("can't load const i128"),
Const::ISize(v) => format!("(intptr_t)0x{v:x}L"),
Const::U8(v) => format!("(uint8_t)0x{v:x}"),
Const::U16(v) => format!("(uint16_t)0x{v:x}"),
Const::U32(v) => format!("0x{v:x}u"),
Const::U64(v) => format!("0x{v:x}uL"),
Const::U128(v) => todo!("can't load const u128"),
Const::USize(v) => format!("(uintptr_t)0x{v:x}uL"),
Const::PlatformString(string_idx) => format!("{:?}", &asm[*string_idx]),
Const::Bool(val) => {
Expand Down
39 changes: 30 additions & 9 deletions cilly/src/v2/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ pub enum Const {
I16(i16),
I32(i32),
I64(i64),
I128(i128),
ISize(i64),
U8(u8),
U16(u16),
U32(u32),
U64(u64),
U128(u128),
USize(u64),
PlatformString(StringIdx),
Bool(bool),
Expand All @@ -30,12 +32,14 @@ impl Const {
Const::I16(_) => Type::Int(Int::I16),
Const::I32(_) => Type::Int(Int::I32),
Const::I64(_) => Type::Int(Int::I64),
Const::I128(_) => Type::Int(Int::I128),
Const::ISize(_) => Type::Int(Int::ISize),
Const::U8(_) => Type::Int(Int::U8),
Const::U16(_) => Type::Int(Int::U16),
Const::U32(_) => Type::Int(Int::U32),
Const::U64(_) => Type::Int(Int::U64),
Const::USize(_) => Type::Int(Int::USize),
Const::U128(_) => Type::Int(Int::U128),
Const::PlatformString(_) => Type::PlatformString,
Const::Bool(_) => Type::Bool,
Const::F32(_) => Type::Float(Float::F32),
Expand All @@ -50,13 +54,30 @@ impl From<Const> for CILNode {
Self::Const(Box::new(value))
}
}
impl From<bool> for Const {
fn from(value: bool) -> Self {
Const::Bool(value)
}
}
impl From<bool> for CILNode {
fn from(value: bool) -> Self {
Const::Bool(value).into()
}
macro_rules! const_impl {
($ty:ty,$name:ident) => {
impl From<$ty> for Const {
fn from(value: $ty) -> Self {
Const::$name(value.try_into().unwrap())
}
}
impl From<$ty> for CILNode {
fn from(value: $ty) -> Self {
Const::$name(value.try_into().unwrap()).into()
}
}
};
}
const_impl! {bool, Bool}
const_impl! {u8, U8}
const_impl! {u16, U16}
const_impl! {u32, U32}
const_impl! {u64, U64}
const_impl! {u128, U128}
const_impl! {usize, USize}
const_impl! {i8, I8}
const_impl! {i16, I16}
const_impl! {i32, I32}
const_impl! {i64, I64}
const_impl! {i128, I128}
const_impl! {isize, ISize}
31 changes: 29 additions & 2 deletions cilly/src/v2/il_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ impl ILExporter {
}
_ => writeln!(out, "ldc.i8 {val}"),
},
super::Const::I128(val) => match val {
-1 => writeln!(out, "ldc.i4.m1 call valuetype [System.Runtime]System.Int128 [System.Runtime]System.Int128::op_Implicit(int32)"),
0..=8 => writeln!(out, "ldc.i4.{val} call valuetype [System.Runtime]System.Int128 [System.Runtime]System.Int128::op_Implicit(int32)"),
9..=127 => writeln!(out, "ldc.i4.s {val} call valuetype [System.Runtime]System.Int128 [System.Runtime]System.Int128::op_Implicit(int32)"),
-2_147_483_648i128..0 | 128..=2_147_483_647i128 => {
writeln!(out, "ldc.i4 {val} call valuetype [System.Runtime]System.Int128 [System.Runtime]System.Int128::op_Implicit(int32)")
}
-9_223_372_036_854_775_808_i128..-2_147_483_648i128 | 2_147_483_648i128..=9_223_372_036_854_775_807i128 => {
writeln!(out, "ldc.i8 {val} call valuetype [System.Runtime]System.Int128 [System.Runtime]System.Int128::op_Implicit(int64)")
}
_ => {
let low = u64::try_from((*val as u128) & u128::from(u64::MAX)).expect("trucating cast error");
let high = ((*val as u128) >> 64) as u64;
writeln!(out, "ldc.i8 {high} ldc.i8 {low} newobj instance void valuetype [System.Runtime]System.Int128::.ctor(uint64,uint64)")
},
},
super::Const::ISize(val) => match val {
-1 => writeln!(out, "ldc.i4.m1 conv.i"),
0..=8 => writeln!(out, "ldc.i4.{val} conv.i"),
Expand Down Expand Up @@ -296,7 +312,7 @@ impl ILExporter {
super::Const::U64(val) => match val {
0..=8 => writeln!(out, "ldc.i4.{val} conv.u8"),
9..=127 => writeln!(out, "ldc.i4.s {val} conv.u8"),
128..=2_147_483_647u64 => writeln!(out, "ldc.i4 {val} conv.u8"),
128..=4_294_967_295u64 => writeln!(out, "ldc.i4 {val} conv.u8"),
_ => writeln!(out, "ldc.i8 {val}"),
},
super::Const::USize(val) => match val {
Expand All @@ -305,6 +321,17 @@ impl ILExporter {
128..=2_147_483_647u64 => writeln!(out, "ldc.i4 {val} conv.u"),
_ => writeln!(out, "ldc.i8 {val} conv.u"),
},
super::Const::U128(val)=>match val {
0..=8 => writeln!(out, "ldc.i4.{val} call valuetype [System.Runtime]System.UInt128 [System.Runtime]System.UInt128::op_Implicit(uint32)"),
9..=127 => writeln!(out, "ldc.i4.s {val} call valuetype [System.Runtime]System.UInt128 [System.Runtime]System.UInt128::op_Implicit(uint32)"),
128..=4_294_967_295u128 => writeln!(out, "ldc.i4 {val} call valuetype [System.Runtime]System.UInt128 [System.Runtime]System.UInt128::op_Implicit(uint32)"),
4_294_967_296u128..=18_446_744_073_709_551_615u128 => writeln!(out, "ldc.i8 {val} call valuetype [System.Runtime]System.UInt128 [System.Runtime]System.UInt128::op_Implicit(uint64)"),
_ => {
let low = u64::try_from({ *val } & u128::from(u64::MAX)).expect("trucating cast error");
let high = ({ *val } >> 64) as u64;
writeln!(out, "ldc.i8 {high} ldc.i8 {low} newobj instance void valuetype [System.Runtime]System.UInt128::.ctor(uint64,uint64)")
},
}
super::Const::PlatformString(msg) => {
let msg = &asm[*msg];
writeln!(out, "ldstr {msg:?}")
Expand Down Expand Up @@ -567,7 +594,7 @@ impl ILExporter {
(Int::I128, false) => {
writeln!(
out,
"volatile. ldobj valuetype [System.Runtime]System.Int128"
"ldobj valuetype [System.Runtime]System.Int128"
)
}
(Int::ISize, true) => writeln!(out, "volatile. ldind.i"),
Expand Down
6 changes: 2 additions & 4 deletions cilly/src/v2/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#[macro_export]
macro_rules! binop {
// |closure| + |closure|
Expand Down Expand Up @@ -325,9 +323,9 @@ macro_rules! ld_arg {
fn macro_test() {
let sum = add!(
zero_extend!(size_of!(usize), usize),
zero_extend!(size_of!(Type::Int(Int::U8)), usize)
zero_extend!(size_of!(crate::Type::Int(crate::Int::U8)), usize)
);
let mut asm = Assembly::default();
let mut asm = super::Assembly::default();
sum(&mut asm);
}
/*
Expand Down
2 changes: 1 addition & 1 deletion cilly/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ into_asm_index_closure! {StringIdx}

#[test]
fn add_macro() {
let sum = binop!(
let sum = crate::binop!(
CILNode::LdLoc(0),
|asm| { asm.alloc_node(CILNode::LdLoc(0)) },
crate::BinOp::Add
Expand Down
52 changes: 26 additions & 26 deletions src/binop/checked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ pub fn result_tuple(tpe: Type, out_of_range: CILNode, val: CILNode, asm: &mut As
}
pub fn zero(ty: Ty, asm: &mut Assembly) -> CILNode {
match ty.kind() {
TyKind::Uint(UintTy::U16) => CILNode::LdcU16(0),
TyKind::Uint(UintTy::U8) => CILNode::LdcU8(0),
TyKind::Uint(UintTy::U32) => ldc_u32!(0),
TyKind::Int(IntTy::I16) => CILNode::LdcI16(0),
TyKind::Int(IntTy::I32) => ldc_i32!(0),
TyKind::Int(IntTy::I8) => CILNode::LdcI8(0),
TyKind::Uint(UintTy::U64) => ldc_u64!(0),
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::U8) => CILNode::V2(asm.alloc_node(0_u8)),
TyKind::Uint(UintTy::U16) => CILNode::V2(asm.alloc_node(0_u16)),
TyKind::Uint(UintTy::U32) => CILNode::V2(asm.alloc_node(0_u32)),
TyKind::Uint(UintTy::U64) => CILNode::V2(asm.alloc_node(0_u64)),
TyKind::Uint(UintTy::Usize) => CILNode::V2(asm.alloc_node(0_usize)),
TyKind::Int(IntTy::I8) => CILNode::V2(asm.alloc_node(0_i8)),
TyKind::Int(IntTy::I16) => CILNode::V2(asm.alloc_node(0_i16)),
TyKind::Int(IntTy::I32) => CILNode::V2(asm.alloc_node(0_i32)),
TyKind::Int(IntTy::I64) => CILNode::V2(asm.alloc_node(0_i64)),
TyKind::Int(IntTy::Isize) => CILNode::V2(asm.alloc_node(0_isize)),
TyKind::Uint(UintTy::U128) => {
let mref = MethodRef::new(
ClassRef::uint_128(asm),
Expand All @@ -70,14 +70,14 @@ pub fn zero(ty: Ty, asm: &mut Assembly) -> CILNode {
}
fn min(ty: Ty, asm: &mut Assembly) -> CILNode {
match ty.kind() {
TyKind::Uint(UintTy::U8) => CILNode::LdcU8(u8::MIN),
TyKind::Uint(UintTy::U16) => CILNode::LdcU16(u16::MIN),
TyKind::Uint(UintTy::U32) => ldc_u32!(u32::MIN),
TyKind::Int(IntTy::I8) => CILNode::LdcI8(i8::MIN),
TyKind::Int(IntTy::I16) => CILNode::LdcI16(i16::MIN),
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::U8) => CILNode::V2(asm.alloc_node(u8::MIN)),
TyKind::Uint(UintTy::U16) => CILNode::V2(asm.alloc_node(u16::MIN)),
TyKind::Uint(UintTy::U32) => CILNode::V2(asm.alloc_node(u32::MIN)),
TyKind::Uint(UintTy::U64) => CILNode::V2(asm.alloc_node(u64::MIN)),
TyKind::Int(IntTy::I8) => CILNode::V2(asm.alloc_node(i8::MIN)),
TyKind::Int(IntTy::I16) => CILNode::V2(asm.alloc_node(i16::MIN)),
TyKind::Int(IntTy::I32) => CILNode::V2(asm.alloc_node(i32::MIN)),
TyKind::Int(IntTy::I64) => CILNode::V2(asm.alloc_node(i64::MIN)),
TyKind::Uint(UintTy::Usize) => {
let mref = MethodRef::new(
ClassRef::usize_type(asm),
Expand Down Expand Up @@ -125,14 +125,14 @@ fn min(ty: Ty, asm: &mut Assembly) -> CILNode {
}
fn max(ty: Ty, asm: &mut Assembly) -> CILNode {
match ty.kind() {
TyKind::Uint(UintTy::U8) => CILNode::LdcU8(u8::MAX),
TyKind::Uint(UintTy::U16) => CILNode::LdcU16(u16::MAX),
TyKind::Uint(UintTy::U32) => ldc_u32!(u32::MAX),
TyKind::Int(IntTy::I8) => CILNode::LdcI8(i8::MAX),
TyKind::Int(IntTy::I16) => CILNode::LdcI16(i16::MAX),
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::U8) => CILNode::V2(asm.alloc_node(u8::MAX)),
TyKind::Uint(UintTy::U16) => CILNode::V2(asm.alloc_node(u16::MAX)),
TyKind::Uint(UintTy::U32) => CILNode::V2(asm.alloc_node(u32::MAX)),
TyKind::Uint(UintTy::U64) => CILNode::V2(asm.alloc_node(u64::MAX)),
TyKind::Int(IntTy::I8) => CILNode::V2(asm.alloc_node(i8::MAX)),
TyKind::Int(IntTy::I16) => CILNode::V2(asm.alloc_node(i16::MAX)),
TyKind::Int(IntTy::I32) => CILNode::V2(asm.alloc_node(i32::MAX)),
TyKind::Int(IntTy::I64) => CILNode::V2(asm.alloc_node(i64::MAX)),
TyKind::Uint(UintTy::Usize) => {
let mref = MethodRef::new(
ClassRef::usize_type(asm),
Expand Down
107 changes: 29 additions & 78 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use cilly::{
hashable::{HashableF32, HashableF64},
Assembly, ClassRef, FieldDesc, Float, Int, MethodRef, MethodRefIdx, StaticFieldDesc,
},
Type,
NodeIdx, Type,
};

use rustc_middle::{
Expand Down Expand Up @@ -264,8 +264,8 @@ fn load_const_scalar<'tcx>(
};

match scalar_ty.kind() {
TyKind::Int(int_type) => load_const_int(scalar_u128, *int_type, ctx),
TyKind::Uint(uint_type) => load_const_uint(scalar_u128, *uint_type, ctx),
TyKind::Int(int_type) => CILNode::V2(load_const_int(scalar_u128, *int_type, ctx)),
TyKind::Uint(uint_type) => CILNode::V2(load_const_uint(scalar_u128, *uint_type, ctx)),
TyKind::Float(ftype) => load_const_float(scalar_u128, *ftype, ctx),
TyKind::Bool => CILNode::V2(ctx.alloc_node(scalar_u128 != 0)),
TyKind::RawPtr(_, _) => conv_usize!(ldc_u64!(
Expand Down Expand Up @@ -370,90 +370,41 @@ fn load_const_float(value: u128, float_type: FloatTy, asm: &mut Assembly) -> CIL
}
}
}
pub fn load_const_int(value: u128, int_type: IntTy, asm: &mut Assembly) -> CILNode {
pub fn load_const_int(value: u128, int_type: IntTy, asm: &mut Assembly) -> NodeIdx {
match int_type {
IntTy::I8 => {
let value = i8::from_ne_bytes([u8::try_from(value).unwrap()]);
CILNode::LdcI8(value)
}
IntTy::I8 => (asm.alloc_node(i8::from_ne_bytes([u8::try_from(value).unwrap()]))),
IntTy::I16 => {
let value = i16::from_ne_bytes((u16::try_from(value).unwrap()).to_ne_bytes());
CILNode::LdcI16(value)
(asm.alloc_node(i16::from_ne_bytes(
(u16::try_from(value).unwrap()).to_ne_bytes(),
)))
}
IntTy::I32 => CILNode::LdcI32(i32::from_ne_bytes(
(u32::try_from(value).unwrap()).to_ne_bytes(),
)),
IntTy::I64 => CILNode::SignExtendToI64(
CILNode::LdcI64(i64::from_ne_bytes(
IntTy::I32 => {
(asm.alloc_node(i32::from_ne_bytes(
(u32::try_from(value).unwrap()).to_ne_bytes(),
)))
}
IntTy::I64 => {
(asm.alloc_node(i64::from_ne_bytes(
(u64::try_from(value).unwrap()).to_ne_bytes(),
))
.into(),
),
IntTy::Isize => CILNode::SignExtendToISize(
CILNode::LdcI64(i64::from_ne_bytes(
)))
}
IntTy::Isize => {
(asm.alloc_node(cilly::Const::ISize(i64::from_ne_bytes(
(u64::try_from(value).unwrap()).to_ne_bytes(),
))
.into(),
),
IntTy::I128 => {
let low = u128_low_u64(value);
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)],
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: asm.alloc_methodref(mref),
args: [conv_u64!(ldc_u64!(high)), conv_u64!(ldc_u64!(low))].into(),
}))
))))
}
#[allow(clippy::cast_possible_wrap)]
IntTy::I128 => (asm.alloc_node(value as i128)),
}
}
pub fn load_const_uint(value: u128, int_type: UintTy, asm: &mut Assembly) -> CILNode {
pub fn load_const_uint(value: u128, int_type: UintTy, asm: &mut Assembly) -> NodeIdx {
match int_type {
UintTy::U8 => {
let value = u8::try_from(value).unwrap();
CILNode::ConvU8(CILNode::LdcU32(u32::from(value)).into())
}
UintTy::U16 => {
let value = u16::try_from(value).unwrap();
CILNode::ConvU16(CILNode::LdcU32(u32::from(value)).into())
}
UintTy::U32 => CILNode::ConvU32(CILNode::LdcU32(u32::try_from(value).unwrap()).into()),
UintTy::U64 => {
CILNode::ZeroExtendToU64(CILNode::LdcU64(u64::try_from(value).unwrap()).into())
}
UintTy::Usize => {
CILNode::ZeroExtendToUSize(CILNode::LdcU64(u64::try_from(value).unwrap()).into())
}
UintTy::U128 => {
let low = u128_low_u64(value);
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)],
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: asm.alloc_methodref(mref),
args: [conv_u64!(ldc_u64!(high)), conv_u64!(ldc_u64!(low))].into(),
}))
}
UintTy::U8 => (asm.alloc_node(u8::try_from(value).unwrap())),
UintTy::U16 => (asm.alloc_node(u16::try_from(value).unwrap())),
UintTy::U32 => (asm.alloc_node(u32::try_from(value).unwrap())),
UintTy::U64 => (asm.alloc_node(u64::try_from(value).unwrap())),
UintTy::Usize => (asm.alloc_node(cilly::Const::USize(u64::try_from(value).unwrap()))),
UintTy::U128 => (asm.alloc_node(value)),
}
}
fn u128_low_u64(value: u128) -> u64 {
Expand Down
Loading

0 comments on commit 9925ea9

Please sign in to comment.