diff --git a/Cargo.lock b/Cargo.lock index f5136a8f..c5ef07b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1712,7 +1712,7 @@ dependencies = [ [[package]] name = "rue-ast" -version = "0.8.2" +version = "0.8.3" dependencies = [ "paste", "rue-parser", @@ -1720,7 +1720,7 @@ dependencies = [ [[package]] name = "rue-cli" -version = "0.8.2" +version = "0.8.3" dependencies = [ "anyhow", "chialisp", @@ -1738,7 +1738,7 @@ dependencies = [ [[package]] name = "rue-compiler" -version = "0.8.2" +version = "0.8.3" dependencies = [ "clvmr", "expect-test", @@ -1763,14 +1763,14 @@ dependencies = [ [[package]] name = "rue-diagnostic" -version = "0.8.2" +version = "0.8.3" dependencies = [ "thiserror 2.0.14", ] [[package]] name = "rue-hir" -version = "0.8.2" +version = "0.8.3" dependencies = [ "derive_more", "expect-test", @@ -1787,14 +1787,14 @@ dependencies = [ [[package]] name = "rue-lexer" -version = "0.8.2" +version = "0.8.3" dependencies = [ "expect-test", ] [[package]] name = "rue-lir" -version = "0.8.2" +version = "0.8.3" dependencies = [ "chialisp", "clvm-traits", @@ -1811,7 +1811,7 @@ dependencies = [ [[package]] name = "rue-lsp" -version = "0.8.2" +version = "0.8.3" dependencies = [ "indexmap", "rowan", @@ -1827,7 +1827,7 @@ dependencies = [ [[package]] name = "rue-options" -version = "0.8.2" +version = "0.8.3" dependencies = [ "serde", "thiserror 2.0.14", @@ -1836,7 +1836,7 @@ dependencies = [ [[package]] name = "rue-parser" -version = "0.8.2" +version = "0.8.3" dependencies = [ "derive_more", "expect-test", @@ -1851,7 +1851,7 @@ dependencies = [ [[package]] name = "rue-tests" -version = "0.8.2" +version = "0.8.3" dependencies = [ "anyhow", "chialisp", @@ -1868,7 +1868,7 @@ dependencies = [ [[package]] name = "rue-types" -version = "0.8.2" +version = "0.8.3" dependencies = [ "clvmr", "derive_more", @@ -1883,7 +1883,7 @@ dependencies = [ [[package]] name = "rue-wasm" -version = "0.8.2" +version = "0.8.3" dependencies = [ "chialisp", "clvmr", diff --git a/Cargo.toml b/Cargo.toml index f4f73d13..3a9a9e91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,15 +42,15 @@ cast_possible_truncation = "allow" case_sensitive_file_extension_comparisons = "allow" [workspace.dependencies] -rue-lexer = { path = "crates/rue-lexer", version = "0.8.2" } -rue-parser = { path = "crates/rue-parser", version = "0.8.2" } -rue-diagnostic = { path = "crates/rue-diagnostic", version = "0.8.2" } -rue-ast = { path = "crates/rue-ast", version = "0.8.2" } -rue-compiler = { path = "crates/rue-compiler", version = "0.8.2" } -rue-options = { path = "crates/rue-options", version = "0.8.2" } -rue-lir = { path = "crates/rue-lir", version = "0.8.2" } -rue-hir = { path = "crates/rue-hir", version = "0.8.2" } -rue-types = { path = "crates/rue-types", version = "0.8.2" } +rue-lexer = { path = "crates/rue-lexer", version = "0.8.3" } +rue-parser = { path = "crates/rue-parser", version = "0.8.3" } +rue-diagnostic = { path = "crates/rue-diagnostic", version = "0.8.3" } +rue-ast = { path = "crates/rue-ast", version = "0.8.3" } +rue-compiler = { path = "crates/rue-compiler", version = "0.8.3" } +rue-options = { path = "crates/rue-options", version = "0.8.3" } +rue-lir = { path = "crates/rue-lir", version = "0.8.3" } +rue-hir = { path = "crates/rue-hir", version = "0.8.3" } +rue-types = { path = "crates/rue-types", version = "0.8.3" } anyhow = "1.0.98" clvm-traits = "0.28.1" clvm-utils = "0.28.1" diff --git a/crates/rue-ast/Cargo.toml b/crates/rue-ast/Cargo.toml index c42a58d1..dee291e6 100644 --- a/crates/rue-ast/Cargo.toml +++ b/crates/rue-ast/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-ast" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "An implementation of the Abstract Syntax Tree for the Rue compiler." diff --git a/crates/rue-cli/Cargo.toml b/crates/rue-cli/Cargo.toml index db55cfca..7b0a7c07 100644 --- a/crates/rue-cli/Cargo.toml +++ b/crates/rue-cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-cli" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "A CLI tool for invoking the Rue compiler." diff --git a/crates/rue-compiler/Cargo.toml b/crates/rue-compiler/Cargo.toml index 49cd3a57..9cbe9a60 100644 --- a/crates/rue-compiler/Cargo.toml +++ b/crates/rue-compiler/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-compiler" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "A compiler for the Rue programming language." diff --git a/crates/rue-diagnostic/Cargo.toml b/crates/rue-diagnostic/Cargo.toml index 0460a172..54b2f262 100644 --- a/crates/rue-diagnostic/Cargo.toml +++ b/crates/rue-diagnostic/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-diagnostic" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "All of the potential diagnostics that can be emitted by the Rue compiler." diff --git a/crates/rue-hir/Cargo.toml b/crates/rue-hir/Cargo.toml index 8dad6c7c..8d5409f0 100644 --- a/crates/rue-hir/Cargo.toml +++ b/crates/rue-hir/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-hir" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "Provides a high-level intermediate representation of the Rue programming language." diff --git a/crates/rue-lexer/Cargo.toml b/crates/rue-lexer/Cargo.toml index 0ad6cd11..2df0ad7b 100644 --- a/crates/rue-lexer/Cargo.toml +++ b/crates/rue-lexer/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-lexer" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "A lexer for the Rue programming language." diff --git a/crates/rue-lir/Cargo.toml b/crates/rue-lir/Cargo.toml index 8c1fd017..403b50c8 100644 --- a/crates/rue-lir/Cargo.toml +++ b/crates/rue-lir/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-lir" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "Provides a low-level intermediate representation that compiles to CLVM." diff --git a/crates/rue-lir/src/optimize/ops.rs b/crates/rue-lir/src/optimize/ops.rs index 367b4994..26e7d012 100644 --- a/crates/rue-lir/src/optimize/ops.rs +++ b/crates/rue-lir/src/optimize/ops.rs @@ -576,11 +576,61 @@ pub fn opt_substr( } pub fn opt_logand(arena: &mut Arena, args: Vec) -> LirId { - arena.alloc(Lir::Logand(args)) + let mut args = ArgList::new(args); + let mut result = Vec::new(); + let mut value = BigInt::from(-1); + + while let Some(arg) = args.next() { + match arena[arg].clone() { + Lir::Atom(atom) => value &= atom_bigint(atom), + Lir::Logand(items) => args.prepend(items), + Lir::Raise(_) => return arg, + _ => result.push(arg), + } + } + + if value != BigInt::from(-1) { + result.push(arena.alloc(Lir::Atom(bigint_atom(value)))); + } + + if result.is_empty() { + return arena.alloc(Lir::Atom(bigint_atom(BigInt::from(-1)))); + } + + if result.len() == 1 && matches!(arena[result[0]], Lir::Atom(_)) { + return result[0]; + } + + arena.alloc(Lir::Logand(result)) } pub fn opt_logior(arena: &mut Arena, args: Vec) -> LirId { - arena.alloc(Lir::Logior(args)) + let mut args = ArgList::new(args); + let mut result = Vec::new(); + let mut value = BigInt::from(0); + + while let Some(arg) = args.next() { + match arena[arg].clone() { + Lir::Atom(atom) => value |= atom_bigint(atom), + Lir::Logior(items) => args.prepend(items), + Lir::Raise(_) => return arg, + _ => result.push(arg), + } + } + + if value != BigInt::from(0) { + result.push(arena.alloc(Lir::Atom(bigint_atom(value)))); + } + + if result.is_empty() { + return arena.alloc(Lir::Atom(vec![])); + } + + if result.len() == 1 && matches!(arena[result[0]], Lir::Atom(_)) { + return result[0]; + } + + arena.alloc(Lir::Logior(result)) } pub fn opt_logxor(arena: &mut Arena, args: Vec) -> LirId { diff --git a/crates/rue-lsp/Cargo.toml b/crates/rue-lsp/Cargo.toml index a51fab54..e1af12b5 100644 --- a/crates/rue-lsp/Cargo.toml +++ b/crates/rue-lsp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-lsp" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "A language server protocol (LSP) implementation for the Rue programming language." diff --git a/crates/rue-options/Cargo.toml b/crates/rue-options/Cargo.toml index 40b46a89..9e74db4f 100644 --- a/crates/rue-options/Cargo.toml +++ b/crates/rue-options/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-options" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "Provides a way to configure the Rue compiler." diff --git a/crates/rue-parser/Cargo.toml b/crates/rue-parser/Cargo.toml index 36a51d7a..d27bbeaa 100644 --- a/crates/rue-parser/Cargo.toml +++ b/crates/rue-parser/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-parser" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "A parser for the Rue programming language." diff --git a/crates/rue-tests/Cargo.toml b/crates/rue-tests/Cargo.toml index efd7a9a6..fcec076b 100644 --- a/crates/rue-tests/Cargo.toml +++ b/crates/rue-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-tests" -version = "0.8.2" +version = "0.8.3" edition = "2024" publish = false license = "Apache-2.0" diff --git a/crates/rue-types/Cargo.toml b/crates/rue-types/Cargo.toml index 6d7df07f..61d4ff96 100644 --- a/crates/rue-types/Cargo.toml +++ b/crates/rue-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-types" -version = "0.8.2" +version = "0.8.3" edition = "2024" license = "Apache-2.0" description = "A type system for the Rue programming language." diff --git a/tests/operators.yaml b/tests/operators.yaml index bfea31df..fbfb4731 100644 --- a/tests/operators.yaml +++ b/tests/operators.yaml @@ -14,12 +14,12 @@ tests: byte_cost: 9636000 total_cost: 9655280 - name: bitwise - program: (a (i (= (lognot (q . 10)) (q . -11)) (q 2 (i (= (ash (q . 50) (q . -2)) (q . 12)) (q 2 (i (= (ash (q . 50) (q . 2)) (q . 200)) (q 2 (i (= (lsh (q . -50) (q . -1)) (q . 103)) (q 2 (i (= (lsh (q . 50) (q . -1)) (q . 25)) (q 2 (i (= (lsh (q . -50) (q . 1)) (q . 412)) (q 2 (i (= (lsh (q . 50) (q . 1)) (q . 100)) (q 2 (i (logand (q . 10) (q . 5)) (q 8) (q 2 (i (= (logior (q . 10) (q . 5)) (q . 15)) (q 2 (i (= (logxor (q . 10) (q . 5)) (q . 15)) (q) (q 8)) 1) (q 8)) 1)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) + program: (a (i (= (lognot (q . 10)) (q . -11)) (q 2 (i (= (ash (q . 50) (q . -2)) (q . 12)) (q 2 (i (= (ash (q . 50) (q . 2)) (q . 200)) (q 2 (i (= (lsh (q . -50) (q . -1)) (q . 103)) (q 2 (i (= (lsh (q . 50) (q . -1)) (q . 25)) (q 2 (i (= (lsh (q . -50) (q . 1)) (q . 412)) (q 2 (i (= (lsh (q . 50) (q . 1)) (q . 100)) (q 2 (i (= (q . 15) (q . 15)) (q 2 (i (= (logxor (q . 10) (q . 5)) (q . 15)) (q) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) (q 8)) 1) debug_program: (a (i (= (lognot (q . 10)) (- () (q . 11))) (q 2 (i (= (ash (q . 50) (- () (q . 2))) (q . 12)) (q 2 (i (= (ash (q . 50) (q . 2)) (q . 200)) (q 2 (i (= (lsh (- () (q . 50)) (- () (q . 1))) (q . 103)) (q 2 (i (= (lsh (q . 50) (- () (q . 1))) (q . 25)) (q 2 (i (= (lsh (- () (q . 50)) (- () (- () (q . 1)))) (q . 412)) (q 2 (i (= (lsh (q . 50) (- () (- () (q . 1)))) (q . 100)) (q 2 (i (= (logand (q . 10) (q . 5)) ()) (q 2 (i (= (logior (q . 10) (q . 5)) (q . 15)) (q 2 (i (= (logxor (q . 10) (q . 5)) (q . 15)) (q) (q 8 (q . "assertion failed at operators.rue:71:5"))) 1) (q 8 (q . "assertion failed at operators.rue:70:5"))) 1) (q 8 (q . "assertion failed at operators.rue:69:5"))) 1) (q 8 (q . "assertion failed at operators.rue:68:5"))) 1) (q 8 (q . "assertion failed at operators.rue:67:5"))) 1) (q 8 (q . "assertion failed at operators.rue:66:5"))) 1) (q 8 (q . "assertion failed at operators.rue:65:5"))) 1) (q 8 (q . "assertion failed at operators.rue:64:5"))) 1) (q 8 (q . "assertion failed at operators.rue:63:5"))) 1) (q 8 (q . "assertion failed at operators.rue:62:5"))) 1) output: () - runtime_cost: 8473 - byte_cost: 4548000 - total_cost: 4556473 + runtime_cost: 6924 + byte_cost: 4092000 + total_cost: 4098924 diagnostics: - Unnecessary `+` operator, since it has no effect at operators.rue:2:12 - Condition always evaluates to `true` at operators.rue:51:12 diff --git a/tests/optimizer/bitwise_constant.rue b/tests/optimizer/bitwise_constant.rue new file mode 100644 index 00000000..570d8201 --- /dev/null +++ b/tests/optimizer/bitwise_constant.rue @@ -0,0 +1,7 @@ +test fn test_bitwise_or() -> Int { + SENDER_PUZZLE | RECEIVER_PUZZLE +} + +test fn test_bitwise_and() -> Int { + 0b110_000 & 0b010_000 +} diff --git a/tests/optimizer/bitwise_constant.yaml b/tests/optimizer/bitwise_constant.yaml new file mode 100644 index 00000000..c36580fa --- /dev/null +++ b/tests/optimizer/bitwise_constant.yaml @@ -0,0 +1,15 @@ +tests: +- name: test_bitwise_or + program: (q . 18) + debug_program: (logior (q . 16) (q . 2)) + output: '18' + runtime_cost: 20 + byte_cost: 36000 + total_cost: 36020 +- name: test_bitwise_and + program: (q . 16) + debug_program: (logand (q . 48) (q . 16)) + output: '16' + runtime_cost: 20 + byte_cost: 36000 + total_cost: 36020 diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 38b81839..207e78e1 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rue-wasm" -version = "0.8.2" +version = "0.8.3" edition = "2024" publish = false license = "Apache-2.0"