Skip to content

Commit

Permalink
Add truncation prims
Browse files Browse the repository at this point in the history
  • Loading branch information
Kmeakin committed Feb 13, 2023
1 parent ef003d6 commit 23c7521
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fathom/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ def_prims! {
U16Xor => "u16_xor",
U16ExtendU32 => "u16_extend_u32",
U16ExtendU64 => "u16_extend_u64",
U16TruncateU8 => "u16_truncate_u8",

U32Eq => "u32_eq",
U32Neq => "u32_neq",
Expand All @@ -472,6 +473,8 @@ def_prims! {
U32Or => "u32_or",
U32Xor => "u32_xor",
U32ExtendU64 => "u32_extend_u64",
U32TruncateU8 => "u32_truncate_u8",
U32TruncateU16 => "u32_truncate_u16",

U64Eq => "u64_eq",
U64Neq => "u64_neq",
Expand All @@ -489,6 +492,9 @@ def_prims! {
U64And => "u64_and",
U64Or => "u64_or",
U64Xor => "u64_xor",
U64TruncateU8 => "u64_truncate_u8",
U64TruncateU16 => "u64_truncate_u16",
U64TruncateU32 => "u64_truncate_u32",

S8Eq => "s8_eq",
S8Neq => "s8_neq",
Expand Down
12 changes: 12 additions & 0 deletions fathom/src/core/prim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ impl<'arena> Env<'arena> {
env.define_prim_fun(U16And, [&U16_TYPE, &U16_TYPE], &U16_TYPE);
env.define_prim_fun(U16Or, [&U16_TYPE, &U16_TYPE], &U16_TYPE);
env.define_prim_fun(U16Xor, [&U16_TYPE, &U16_TYPE], &U16_TYPE);
env.define_prim_fun(U16TruncateU8, [&U16_TYPE], &U8_TYPE);
env.define_prim_fun(U16ExtendU32, [&U16_TYPE], &U32_TYPE);
env.define_prim_fun(U16ExtendU64, [&U16_TYPE], &U64_TYPE);

Expand All @@ -227,6 +228,8 @@ impl<'arena> Env<'arena> {
env.define_prim_fun(U32And, [&U32_TYPE, &U32_TYPE], &U32_TYPE);
env.define_prim_fun(U32Or, [&U32_TYPE, &U32_TYPE], &U32_TYPE);
env.define_prim_fun(U32Xor, [&U32_TYPE, &U32_TYPE], &U32_TYPE);
env.define_prim_fun(U32TruncateU8, [&U32_TYPE], &U8_TYPE);
env.define_prim_fun(U32TruncateU16, [&U32_TYPE], &U16_TYPE);
env.define_prim_fun(U32ExtendU64, [&U32_TYPE], &U64_TYPE);

env.define_prim_fun(U64Eq, [&U64_TYPE, &U64_TYPE], &BOOL_TYPE);
Expand All @@ -245,6 +248,9 @@ impl<'arena> Env<'arena> {
env.define_prim_fun(U64And, [&U64_TYPE, &U64_TYPE], &U64_TYPE);
env.define_prim_fun(U64Or, [&U64_TYPE, &U64_TYPE], &U64_TYPE);
env.define_prim_fun(U64Xor, [&U64_TYPE, &U64_TYPE], &U64_TYPE);
env.define_prim_fun(U64TruncateU8, [&U64_TYPE], &U8_TYPE);
env.define_prim_fun(U64TruncateU16, [&U64_TYPE], &U16_TYPE);
env.define_prim_fun(U64TruncateU32, [&U64_TYPE], &U32_TYPE);

env.define_prim_fun(S8Eq, [&S8_TYPE, &S8_TYPE], &BOOL_TYPE);
env.define_prim_fun(S8Neq, [&S8_TYPE, &S8_TYPE], &BOOL_TYPE);
Expand Down Expand Up @@ -668,6 +674,7 @@ pub fn step(prim: Prim) -> Step {
Prim::U16And => const_step!([x, xst: U16, y, yst: U16] => Const::U16(u16::bitand(*x, *y), UIntStyle::merge(*xst, *yst))),
Prim::U16Or => const_step!([x, xst: U16, y, yst: U16] => Const::U16(u16::bitor(*x, *y), UIntStyle::merge(*xst, *yst))),
Prim::U16Xor => const_step!([x, xst: U16, y, yst: U16] => Const::U16(u16::bitxor(*x, *y), UIntStyle::merge(*xst, *yst))),
Prim::U16TruncateU8 => const_step!([x, xst: U16] => Const::U8(*x as u8, *xst)),
Prim::U16ExtendU32 => const_step!([x, xst: U16] => Const::U32(u32::from(*x), *xst)),
Prim::U16ExtendU64 => const_step!([x, xst: U16] => Const::U64(u64::from(*x), *xst)),

Expand All @@ -687,6 +694,8 @@ pub fn step(prim: Prim) -> Step {
Prim::U32And => const_step!([x, xst: U32, y, yst: U32] => Const::U32(u32::bitand(*x, *y), UIntStyle::merge(*xst, *yst))),
Prim::U32Or => const_step!([x, xst: U32, y, yst: U32] => Const::U32(u32::bitor(*x, *y), UIntStyle::merge(*xst, *yst))),
Prim::U32Xor => const_step!([x, xst: U32, y, yst: U32] => Const::U32(u32::bitxor(*x, *y), UIntStyle::merge(*xst, *yst))),
Prim::U32TruncateU8 => const_step!([x, xst: U32] => Const::U8(*x as u8, *xst)),
Prim::U32TruncateU16 => const_step!([x, xst: U32] => Const::U16(*x as u16, *xst)),
Prim::U32ExtendU64 => const_step!([x, xst: U32] => Const::U64(u64::from(*x), *xst)),

Prim::U64Eq => const_step!([x: U64, y: U64] => Const::Bool(x == y)),
Expand All @@ -705,6 +714,9 @@ pub fn step(prim: Prim) -> Step {
Prim::U64And => const_step!([x, xst: U64, y, yst: U64] => Const::U64(u64::bitand(*x, *y), UIntStyle::merge(*xst, *yst))),
Prim::U64Or => const_step!([x, xst: U64, y, yst: U64] => Const::U64(u64::bitor(*x, *y), UIntStyle::merge(*xst, *yst))),
Prim::U64Xor => const_step!([x, xst: U64, y, yst: U64] => Const::U64(u64::bitxor(*x, *y), UIntStyle::merge(*xst, *yst))),
Prim::U64TruncateU8 => const_step!([x, xst: U64] => Const::U8(*x as u8, *xst)),
Prim::U64TruncateU16 => const_step!([x, xst: U64] => Const::U16(*x as u16, *xst)),
Prim::U64TruncateU32 => const_step!([x, xst: U64] => Const::U32(*x as u32, *xst)),

Prim::S8Eq => const_step!([x: S8, y: S8] => Const::Bool(x == y)),
Prim::S8Neq => const_step!([x: S8, y: S8] => Const::Bool(x != y)),
Expand Down
6 changes: 6 additions & 0 deletions tests/succeed/primitives.fathom
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ let _ = u16_shr : U16 -> U8 -> U16;
let _ = u16_and : U16 -> U16 -> U16;
let _ = u16_or : U16 -> U16 -> U16;
let _ = u16_xor : U16 -> U16 -> U16;
let _ = u16_truncate_u8 : U16 -> U8;
let _ = u16_extend_u32 : U16 -> U32;
let _ = u16_extend_u64 : U16 -> U64;

Expand All @@ -123,6 +124,8 @@ let _ = u32_shr : U32 -> U8 -> U32;
let _ = u32_and : U32 -> U32 -> U32;
let _ = u32_or : U32 -> U32 -> U32;
let _ = u32_xor : U32 -> U32 -> U32;
let _ = u32_truncate_u8 : U32 -> U8;
let _ = u32_truncate_u16 : U32 -> U16;
let _ = u32_extend_u64 : U32 -> U64;

let _ = u64_eq : U64 -> U64 -> Bool;
Expand All @@ -141,6 +144,9 @@ let _ = u64_shr : U64 -> U8 -> U64;
let _ = u64_and : U64 -> U64 -> U64;
let _ = u64_or : U64 -> U64 -> U64;
let _ = u64_xor : U64 -> U64 -> U64;
let _ = u64_truncate_u8 : U64 -> U8;
let _ = u64_truncate_u16 : U64 -> U16;
let _ = u64_truncate_u32 : U64 -> U32;

let _ = s8_eq : S8 -> S8 -> Bool;
let _ = s8_neq : S8 -> S8 -> Bool;
Expand Down
6 changes: 6 additions & 0 deletions tests/succeed/primitives.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ let _ : U16 -> U8 -> U16 = u16_shr;
let _ : U16 -> U16 -> U16 = u16_and;
let _ : U16 -> U16 -> U16 = u16_or;
let _ : U16 -> U16 -> U16 = u16_xor;
let _ : U16 -> U8 = u16_truncate_u8;
let _ : U16 -> U32 = u16_extend_u32;
let _ : U16 -> U64 = u16_extend_u64;
let _ : U32 -> U32 -> Bool = u32_eq;
Expand All @@ -115,6 +116,8 @@ let _ : U32 -> U8 -> U32 = u32_shr;
let _ : U32 -> U32 -> U32 = u32_and;
let _ : U32 -> U32 -> U32 = u32_or;
let _ : U32 -> U32 -> U32 = u32_xor;
let _ : U32 -> U8 = u32_truncate_u8;
let _ : U32 -> U16 = u32_truncate_u16;
let _ : U32 -> U64 = u32_extend_u64;
let _ : U64 -> U64 -> Bool = u64_eq;
let _ : U64 -> U64 -> Bool = u64_neq;
Expand All @@ -132,6 +135,9 @@ let _ : U64 -> U8 -> U64 = u64_shr;
let _ : U64 -> U64 -> U64 = u64_and;
let _ : U64 -> U64 -> U64 = u64_or;
let _ : U64 -> U64 -> U64 = u64_xor;
let _ : U64 -> U8 = u64_truncate_u8;
let _ : U64 -> U16 = u64_truncate_u16;
let _ : U64 -> U32 = u64_truncate_u32;
let _ : S8 -> S8 -> Bool = s8_eq;
let _ : S8 -> S8 -> Bool = s8_neq;
let _ : S8 -> S8 -> Bool = s8_gt;
Expand Down

0 comments on commit 23c7521

Please sign in to comment.