diff --git a/crates/rue-compiler/src/optimizer.rs b/crates/rue-compiler/src/optimizer.rs index fa7707b..88e61e2 100644 --- a/crates/rue-compiler/src/optimizer.rs +++ b/crates/rue-compiler/src/optimizer.rs @@ -1,6 +1,7 @@ use num_bigint::BigInt; use num_traits::One; use rue_clvm::{first_path, rest_path}; +use rue_typing::bigint_to_bytes; use crate::{ database::{Database, LirId, SymbolId}, @@ -187,12 +188,21 @@ impl<'a> Optimizer<'a> { fn opt_listp(&mut self, env_id: EnvironmentId, mir_id: MirId) -> LirId { let lir_id = self.opt_mir(env_id, mir_id); - self.db.alloc_lir(Lir::Listp(lir_id)) + match self.db.lir(lir_id) { + Lir::Pair(..) => self.db.alloc_lir(Lir::Atom(vec![1])), + Lir::Atom(..) => self.db.alloc_lir(Lir::Atom(Vec::new())), + _ => self.db.alloc_lir(Lir::Listp(lir_id)), + } } fn opt_strlen(&mut self, env_id: EnvironmentId, mir_id: MirId) -> LirId { let lir_id = self.opt_mir(env_id, mir_id); - self.db.alloc_lir(Lir::Strlen(lir_id)) + match self.db.lir(lir_id) { + Lir::Atom(atom) => self + .db + .alloc_lir(Lir::Atom(bigint_to_bytes(BigInt::from(atom.len())))), + _ => self.db.alloc_lir(Lir::Strlen(lir_id)), + } } fn opt_pubkey_for_exp(&mut self, env_id: EnvironmentId, mir_id: MirId) -> LirId { @@ -415,7 +425,10 @@ impl<'a> Optimizer<'a> { fn opt_not(&mut self, env_id: EnvironmentId, value: MirId) -> LirId { let value = self.opt_mir(env_id, value); - self.db.alloc_lir(Lir::Not(value)) + match self.db.lir(value) { + Lir::Not(value) => *value, + _ => self.db.alloc_lir(Lir::Not(value)), + } } fn opt_divmod(&mut self, env_id: EnvironmentId, lhs: MirId, rhs: MirId) -> LirId { diff --git a/tests.toml b/tests.toml index 536f961..7754515 100644 --- a/tests.toml +++ b/tests.toml @@ -213,18 +213,18 @@ output = "()" hash = "1a9674474efa85b2616b28fc2ae4f1b6a199273973e482ea74498fcc507141cf" [enum_fields] -bytes = 65 -cost = 1004 +bytes = 53 +cost = 889 input = "()" output = "1000" -hash = "6f486106a324ee36d5b3e63bd65d7e69c8a95278d4f405307492e0c4960b08f2" +hash = "443b5ea65804535fbdd7232183d4dd0a9cc7a8507fe39ec33e3388d211e6a9ea" [enum_numeric] -bytes = 103 -cost = 2002 +bytes = 95 +cost = 1600 input = "()" output = "()" -hash = "a904e8177e07f7f7c2454b8cb53017f85181b0b7841ba9275950e4d6151c9b84" +hash = "33ff837cd597167b097846ba8681da6cf7265079be7051bb5287d9ef79d5ffe6" [enum_mixed] bytes = 275 @@ -372,11 +372,11 @@ output = "()" hash = "a11c0ef207b9839e5c23ed85799012b79ed88c6b92957d0e117471e192a6de9d" [literal_bytes] -bytes = 367 -cost = 15830 +bytes = 359 +cost = 15428 input = "()" output = "0xf1fb6ca14066f4236b3cc44a79a78fe90d44f1d45e04f9b9939863db71b99560" -hash = "14c7dd45e2f1dba24d5941e5b5ca95eb44dcaee423bf45a8edf8f8a1737ae674" +hash = "2572b331f315153b90aba120bf296f492ba013086ac9cf199dd1f5c95b74bc2d" [concat_bytes] bytes = 30 @@ -435,11 +435,11 @@ output = "0x064cc0" hash = "96a314da556b577d15a2e0a2db42e3b7f22610ab4336ed86713716c5a029e787" [boolean_arithmetic] -bytes = 47 -cost = 3975 +bytes = 39 +cost = 3573 input = "()" output = "1" -hash = "7c0bf0b36503c7abd7e264b2d63cc829162cd1fdff59d1f34792cc043a24e9a5" +hash = "141778a0f5fec809d4262fa568ce905a597ae3c43e59c0681243eb13d381da38" [bitwise_shift] bytes = 41 @@ -535,11 +535,11 @@ output = "((g1_multiply 0xc00000000000000000000000000000000000000000000000000000 hash = "21f96d7bb1b15b83ce81dff3525d4c98793f906f6cc7ebba52a76524a7db6943" [singleton] -bytes = 1531 -cost = 54718 +bytes = 1523 +cost = 54316 input = "((0x42840c6aebec47ce2e01629ce381b461c19695264281a7b1aab5d4ff54506775 0x4696e7a2b7682e2df01ab47e6e002d0dca895f99c6172e4a55a3e033499532b7 0x291e4594b43d58e833cab95e4b165c5fac6b4d8391c81ebfd20efdd8d58b92d8) 1 (0x9b1c580707ca8282534c02c1a055427e0954818b6195a29f4442ac3e7ea8e8ee () 1) 1 ((51 0x173385b87af5d8940767c328026fe5f8e76bc238d2a3aaddf4f55e844f400fca 1)))" output = "((73 1) (71 0xf92f0ebbd0e5ecb1334331d98c1f3b3e41cfce2c15f1053ffd1e2151b361e909) (g1_negate 0x07d534114dd68436cb7a4026abade359cd9c9f28b253c60e305535c781bbc7ed 1))" -hash = "3d8892dcdac1a32aab1034fa9489b7b7dc3b6aabf512f38e73d54574b1f6b62c" +hash = "24b8661391d18c36e6a2f0f68a10d076be30b357d21a5884b55629ca18f662f7" [enum_type_guard] bytes = 101