From 81ac895e8ad5cf58e8756fa7d48d3598e649f199 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 26 Jun 2024 21:08:53 +0100 Subject: [PATCH 01/57] Adds files and first function --- crates/compiler/builtins/bitcode/src/crypt.zig | 7 +++++++ crates/compiler/builtins/roc/Crypt.roc | 0 2 files changed, 7 insertions(+) create mode 100644 crates/compiler/builtins/bitcode/src/crypt.zig create mode 100644 crates/compiler/builtins/roc/Crypt.roc diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig new file mode 100644 index 00000000000..03d307e5430 --- /dev/null +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -0,0 +1,7 @@ +const std = @import("std"); +const crypto = std.crypto; +const sha2 = crypto.hash.sha2; + +pub fn sha256( )sha2.Sha256{ + return sha2.Sha256.init(.{}); +} diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc new file mode 100644 index 00000000000..e69de29bb2d From 48fffa0e777d71d65cb030823fdad265af2e8582 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 1 Jul 2024 22:28:48 +0100 Subject: [PATCH 02/57] zig functions --- crates/compiler/builtins/bitcode/src/crypt.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 03d307e5430..109263b3072 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -5,3 +5,17 @@ const sha2 = crypto.hash.sha2; pub fn sha256( )sha2.Sha256{ return sha2.Sha256.init(.{}); } + +pub fn addBytes(hasher: sha2.Sha256, bytes:[] const u8)sha2.Sha256{ + var out = hasher; + out.update(bytes); + return out; +} + +const sha256_digest_length = 32; + +pub const sha256_digest = [sha256_digest_length2]u8; + +pub fn digest(hasher: sha2.Sha256 ): sha256_digest{ + return hasher.peek(); +} \ No newline at end of file From fff57ae553aa03589b6b0a932d3ce2e033e5712a Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 2 Jul 2024 20:22:56 +0100 Subject: [PATCH 03/57] Adds crypt to main.zig --- crates/compiler/builtins/bitcode/src/crypt.zig | 4 ++-- crates/compiler/builtins/bitcode/src/main.zig | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 109263b3072..9dd5f72b633 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -14,8 +14,8 @@ pub fn addBytes(hasher: sha2.Sha256, bytes:[] const u8)sha2.Sha256{ const sha256_digest_length = 32; -pub const sha256_digest = [sha256_digest_length2]u8; +pub const sha256_digest = [sha256_digest_length]u8; -pub fn digest(hasher: sha2.Sha256 ): sha256_digest{ +pub fn digest(hasher: sha2.Sha256 ) sha256_digest{ return hasher.peek(); } \ No newline at end of file diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index 9923a8566f1..8265186aaaf 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -10,6 +10,15 @@ const ROC_BUILTINS = "roc_builtins"; const NUM = "num"; const STR = "str"; +// Crypt Module +const crypt = @import("crypt.zig"); +comptime{ + // exportCryptFn(crypt.sha256); + // exportCryptFn(crypt.addBytes); + // exportCryptFn(crypt.digest); +} + + // Dec Module const dec = @import("dec.zig"); @@ -389,6 +398,9 @@ fn exportListFn(comptime func: anytype, comptime func_name: []const u8) void { fn exportDecFn(comptime func: anytype, comptime func_name: []const u8) void { exportBuiltinFn(func, "dec." ++ func_name); } +fn exportCryptFn(comptime func: anytype, comptime func_name: []const u8) void { + exportBuiltinFn(func, "crypt." ++ func_name); +} fn exportUtilsFn(comptime func: anytype, comptime func_name: []const u8) void { exportBuiltinFn(func, "utils." ++ func_name); From 1bde7801cd02ef5d6aa002aa2ee1148d16ddd1cc Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Thu, 4 Jul 2024 19:31:02 +0100 Subject: [PATCH 04/57] Converts to pointers --- .../compiler/builtins/bitcode/src/crypt.zig | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 9dd5f72b633..1b7e5ece15b 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -1,21 +1,35 @@ const std = @import("std"); const crypto = std.crypto; const sha2 = crypto.hash.sha2; +const list = @importg("list.zig") +const utils = @import("utils.zig"); -pub fn sha256( )sha2.Sha256{ - return sha2.Sha256.init(.{}); +const Sha256 = extern struct{ + location:usize, + pub fn pointer(self : Sha256) *sha2.Sha256{ + return @ptrFromInt(self.location); + } + } + +pub fn sha256() callconv(.C) Sha256{ + const allocation = utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256)); + const ptr:*sha2.Sha256 = @ptrCast(allocation); + ptr.* = sha2.Sha256.init(.{}); + return Sha256{.location = @intFromPtr(ptr)),}; } -pub fn addBytes(hasher: sha2.Sha256, bytes:[] const u8)sha2.Sha256{ - var out = hasher; - out.update(bytes); +pub fn addBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256{ + var out = sha256(); + if(data.bytes)|bytes|{ + out.pointer.*.update(bytes); + } return out; } const sha256_digest_length = 32; -pub const sha256_digest = [sha256_digest_length]u8; +pub const Digest256 = [sha256_digest_length]u8; -pub fn digest(hasher: sha2.Sha256 ) sha256_digest{ - return hasher.peek(); +pub fn digest(sha: Sha256 ) callCov(.C) Digest256{ + return sha.pointer.*.peek(); } \ No newline at end of file From 8168545511db2edf4679e0b3eb660cdac14258d1 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Fri, 5 Jul 2024 22:52:38 +0100 Subject: [PATCH 05/57] Zig functions export --- .../compiler/builtins/bitcode/src/crypt.zig | 26 +++++++++---------- crates/compiler/builtins/bitcode/src/main.zig | 6 ++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 1b7e5ece15b..73417679f85 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -1,35 +1,35 @@ const std = @import("std"); const crypto = std.crypto; const sha2 = crypto.hash.sha2; -const list = @importg("list.zig") +const list = @import("list.zig"); const utils = @import("utils.zig"); const Sha256 = extern struct{ location:usize, - pub fn pointer(self : Sha256) *sha2.Sha256{ + fn pointer(self : Sha256) *sha2.Sha256{ return @ptrFromInt(self.location); } - } + }; -pub fn sha256() callconv(.C) Sha256{ +pub fn emptySha256() callconv(.C) Sha256{ const allocation = utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256)); - const ptr:*sha2.Sha256 = @ptrCast(allocation); + const ptr:*sha2.Sha256 = @alignCast(@ptrCast(allocation)); ptr.* = sha2.Sha256.init(.{}); - return Sha256{.location = @intFromPtr(ptr)),}; + return Sha256{.location = @intFromPtr(ptr),}; } pub fn addBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256{ - var out = sha256(); + var out = emptySha256(); + out.pointer().* = sha.pointer().*; if(data.bytes)|bytes|{ - out.pointer.*.update(bytes); + const byteSlice : []u8 = bytes[0..data.length]; + out.pointer().*.update(byteSlice); } return out; } -const sha256_digest_length = 32; - -pub const Digest256 = [sha256_digest_length]u8; +pub const Digest256 = extern struct{firstHalf:u128, secondHalf:u128,}; -pub fn digest(sha: Sha256 ) callCov(.C) Digest256{ - return sha.pointer.*.peek(); +pub fn digest(sha: Sha256 ) callconv(.C) Digest256{ + return @bitCast(sha.pointer().*.peek()); } \ No newline at end of file diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index 8265186aaaf..ee9c90ad742 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -13,9 +13,9 @@ const STR = "str"; // Crypt Module const crypt = @import("crypt.zig"); comptime{ - // exportCryptFn(crypt.sha256); - // exportCryptFn(crypt.addBytes); - // exportCryptFn(crypt.digest); + exportCryptFn(crypt.emptySha256, "empty_sha256"); + exportCryptFn(crypt.addBytes, "add_bytes"); + exportCryptFn(crypt.digest, "digest"); } From 881b1be674a3de7108ae4979b416399e37256a45 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sat, 6 Jul 2024 16:38:37 +0100 Subject: [PATCH 06/57] WIP broken. trying to do plumbing --- crates/compiler/builtins/bitcode/src/crypt.zig | 6 ++++-- crates/compiler/builtins/src/bitcode.rs | 4 ++++ crates/compiler/can/src/builtins.rs | 4 ++++ crates/compiler/module/src/symbol.rs | 12 +++++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 73417679f85..fd89e4b35e9 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -11,7 +11,9 @@ const Sha256 = extern struct{ } }; -pub fn emptySha256() callconv(.C) Sha256{ +const EmptyStruct = extern struct{}; + +pub fn emptySha256(_: EmptyStruct) callconv(.C) Sha256{ const allocation = utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256)); const ptr:*sha2.Sha256 = @alignCast(@ptrCast(allocation)); ptr.* = sha2.Sha256.init(.{}); @@ -19,7 +21,7 @@ pub fn emptySha256() callconv(.C) Sha256{ } pub fn addBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256{ - var out = emptySha256(); + var out = emptySha256(undefined); out.pointer().* = sha.pointer().*; if(data.bytes)|bytes|{ const byteSlice : []u8 = bytes[0..data.length]; diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index cbf1ca73ee1..948536cd808 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -417,6 +417,10 @@ pub const DEC_ROUND: IntrinsicName = int_intrinsic!("roc_builtins.dec.round"); pub const DEC_FLOOR: IntrinsicName = int_intrinsic!("roc_builtins.dec.floor"); pub const DEC_CEILING: IntrinsicName = int_intrinsic!("roc_builtins.dec.ceiling"); +pub const CRYPT_EMPTY_SHA256: &str = "roc_builins.crypt.emptySha256"; +pub const CRYPT_ADD_BYTES: &str = "roc_builtins.crypt.addBytes"; +pub const CRYPT_DIGEST: &str = "roc_builtins.crypt.digest"; + pub const UTILS_DBG_IMPL: &str = "roc_builtins.utils.dbg_impl"; pub const UTILS_TEST_PANIC: &str = "roc_builtins.utils.test_panic"; pub const UTILS_ALLOCATE_WITH_REFCOUNT: &str = "roc_builtins.utils.allocate_with_refcount"; diff --git a/crates/compiler/can/src/builtins.rs b/crates/compiler/can/src/builtins.rs index 6b4c99ee5e3..3d9bdf320cc 100644 --- a/crates/compiler/can/src/builtins.rs +++ b/crates/compiler/can/src/builtins.rs @@ -210,6 +210,10 @@ map_symbol_to_lowlevel_and_arity! { NumF32FromParts; NUM_F32_FROM_PARTS; 1, NumF64FromParts; NUM_F64_FROM_PARTS; 1, + CryptEmptySha256; CRYPT_EMPTY_SHA256; 1, + CryptAddBytes; CRYPT_ADD_BYTES; 2, + CryptDigest; CRYPT_DIGEST; 1, + Eq; BOOL_STRUCTURAL_EQ; 2, NotEq; BOOL_STRUCTURAL_NOT_EQ; 2, And; BOOL_AND; 2, diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index 47a2e5f8e25..8daf6424527 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1716,7 +1716,7 @@ define_builtins! { 32 INSPECT_TO_INSPECTOR: "toInspector" 33 INSPECT_TO_STR: "toStr" } - 15 TASK: "Task" => { + 16 TASK: "Task" => { 0 TASK_TASK: "Task" exposed_type=true // the Task.Task opaque type 1 TASK_FOREVER: "forever" 2 TASK_LOOP: "loop" @@ -1735,4 +1735,14 @@ define_builtins! { } num_modules: 16 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) + 15 CRYPT: "Crypt" =>{ + 0 CRYPT_SHA256: "Sha256" exposed_type=true + 1 CRYPT_EMPTY_STRUCT: "EmptyStruct" exposed_type=true + 3 CRYPT_DIGEST256: "Digest256" exposed_type + 4 CRYPT_EMPTY_SHA256: "emptySha256" + 5 CRYPT_ADD_BYTES: "addBytes" + 6 CRYPT_DIGEST: "digest" + } + + num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) } From cb294ee41f042012f386f605b8789781521cdb6f Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sat, 6 Jul 2024 23:07:01 +0100 Subject: [PATCH 07/57] WIP filling in missing match cases --- crates/compiler/gen_wasm/src/low_level.rs | 7 ++++++- crates/compiler/module/src/low_level.rs | 6 ++++++ crates/compiler/module/src/symbol.rs | 2 +- crates/compiler/mono/src/drop_specialization.rs | 1 + crates/compiler/mono/src/inc_dec.rs | 3 +++ 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index 1e4c05c3db6..0af0685a6aa 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2163,6 +2163,11 @@ impl<'a> LowLevelCall<'a> { NumF64ToParts => self.load_args_and_call_zig(backend, bitcode::NUM_F64_TO_PARTS), NumF32FromParts => self.load_args_and_call_zig(backend, bitcode::NUM_F32_FROM_PARTS), NumF64FromParts => self.load_args_and_call_zig(backend, bitcode::NUM_F64_FROM_PARTS), + // Crypt + CryptEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPT_EMPTY_SHA256), + CryptAddBytes => self.load_args_and_call_zig(backend, bitcode::CRYPT_ADD_BYTES), + CryptAddBytes => self.load_args_and_call_zig(backend, bitcode::CRYPT_ADD_BYTES), + And => { self.load_args(backend); backend.code_builder.i32_and(); @@ -2229,7 +2234,7 @@ impl<'a> LowLevelCall<'a> { BoxExpr | UnboxExpr => { unreachable!("The {:?} operation is turned into mono Expr", self.lowlevel) - } + }, Unreachable => match self.ret_storage { StoredValue::Local { value_type, .. } => match value_type { diff --git a/crates/compiler/module/src/low_level.rs b/crates/compiler/module/src/low_level.rs index 2f40c002a8f..f1e79ac21a4 100644 --- a/crates/compiler/module/src/low_level.rs +++ b/crates/compiler/module/src/low_level.rs @@ -129,6 +129,9 @@ pub enum LowLevel { SetJmp, LongJmp, SetLongJmpBuffer, + CryptEmptySha256, + CryptAddBytes, + CryptDigest, } macro_rules! higher_order { @@ -348,4 +351,7 @@ map_symbol_to_lowlevel! { Not <= BOOL_NOT; Unreachable <= LIST_UNREACHABLE; DictPseudoSeed <= DICT_PSEUDO_SEED; + CryptEmptySha256 <= CRYPT_EMPTY_SHA256; + CryptAddBytes <= CRYPT_ADD_BYTES; + CryptDigest <= CRYPT_DIGEST; } diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index 8daf6424527..7cdadc7f9ec 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1738,7 +1738,7 @@ define_builtins! { 15 CRYPT: "Crypt" =>{ 0 CRYPT_SHA256: "Sha256" exposed_type=true 1 CRYPT_EMPTY_STRUCT: "EmptyStruct" exposed_type=true - 3 CRYPT_DIGEST256: "Digest256" exposed_type + 3 CRYPT_DIGEST256: "Digest256" exposed_type=true 4 CRYPT_EMPTY_SHA256: "emptySha256" 5 CRYPT_ADD_BYTES: "addBytes" 6 CRYPT_DIGEST: "digest" diff --git a/crates/compiler/mono/src/drop_specialization.rs b/crates/compiler/mono/src/drop_specialization.rs index 2728fcbc07d..9d2f98d6a9a 100644 --- a/crates/compiler/mono/src/drop_specialization.rs +++ b/crates/compiler/mono/src/drop_specialization.rs @@ -1609,6 +1609,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC { ListIsUnique => RC::Rc, ListClone => RC::Rc, + CryptEmptySha256 | CryptAddBytes | CryptDigest => RC::NoRc, BoxExpr | UnboxExpr => { unreachable!("These lowlevel operations are turned into mono Expr's") diff --git a/crates/compiler/mono/src/inc_dec.rs b/crates/compiler/mono/src/inc_dec.rs index 1d9cf29a67d..87b133b046f 100644 --- a/crates/compiler/mono/src/inc_dec.rs +++ b/crates/compiler/mono/src/inc_dec.rs @@ -1256,6 +1256,9 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] { ListSwap => &[OWNED, IRRELEVANT, IRRELEVANT], ListReleaseExcessCapacity => &[OWNED], StrReleaseExcessCapacity => &[OWNED], + CryptEmptySha256 => &[IRRELEVANT], + CryptAddBytes => &[OWNED, BORROWED], + CryptDigest => &[BORROWED], ListIncref => &[OWNED], ListDecref => &[OWNED], From 07e0c28efd067e84bd41d44e67103bf1ddff08ca Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sun, 7 Jul 2024 18:57:56 +0100 Subject: [PATCH 08/57] Filling in missing matches before rebase --- crates/compiler/gen_wasm/src/low_level.rs | 2 +- crates/compiler/module/src/symbol.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index 0af0685a6aa..d3dce52a030 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2166,7 +2166,7 @@ impl<'a> LowLevelCall<'a> { // Crypt CryptEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPT_EMPTY_SHA256), CryptAddBytes => self.load_args_and_call_zig(backend, bitcode::CRYPT_ADD_BYTES), - CryptAddBytes => self.load_args_and_call_zig(backend, bitcode::CRYPT_ADD_BYTES), + CryptDigest => self.load_args_and_call_zig(backend, bitcode::CRYPT_DIGEST), And => { self.load_args(backend); diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index 7cdadc7f9ec..aa4ca65f78e 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1738,10 +1738,10 @@ define_builtins! { 15 CRYPT: "Crypt" =>{ 0 CRYPT_SHA256: "Sha256" exposed_type=true 1 CRYPT_EMPTY_STRUCT: "EmptyStruct" exposed_type=true - 3 CRYPT_DIGEST256: "Digest256" exposed_type=true - 4 CRYPT_EMPTY_SHA256: "emptySha256" - 5 CRYPT_ADD_BYTES: "addBytes" - 6 CRYPT_DIGEST: "digest" + 2 CRYPT_DIGEST256: "Digest256" exposed_type=true + 3 CRYPT_EMPTY_SHA256: "emptySha256" + 4 CRYPT_ADD_BYTES: "addBytes" + 5 CRYPT_DIGEST: "digest" } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) From 883d580f89372242b83809ddc727bb9f8094784a Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sun, 7 Jul 2024 19:11:55 +0100 Subject: [PATCH 09/57] Fixed rebase --- crates/compiler/module/src/symbol.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index aa4ca65f78e..7cdadc7f9ec 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1738,10 +1738,10 @@ define_builtins! { 15 CRYPT: "Crypt" =>{ 0 CRYPT_SHA256: "Sha256" exposed_type=true 1 CRYPT_EMPTY_STRUCT: "EmptyStruct" exposed_type=true - 2 CRYPT_DIGEST256: "Digest256" exposed_type=true - 3 CRYPT_EMPTY_SHA256: "emptySha256" - 4 CRYPT_ADD_BYTES: "addBytes" - 5 CRYPT_DIGEST: "digest" + 3 CRYPT_DIGEST256: "Digest256" exposed_type=true + 4 CRYPT_EMPTY_SHA256: "emptySha256" + 5 CRYPT_ADD_BYTES: "addBytes" + 6 CRYPT_DIGEST: "digest" } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) From 0ab4f458e81bb9aab28137cd09e4e2fc88bee63e Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sun, 7 Jul 2024 22:37:17 +0100 Subject: [PATCH 10/57] Adds case staements for lowlevel to compile --- crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 11 +++++++++++ crates/compiler/module/src/symbol.rs | 8 ++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 95659e66b68..ead1067637c 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1431,6 +1431,17 @@ pub(crate) fn run_low_level<'a, 'ctx>( call_bitcode_fn(env, &[], bitcode::UTILS_DICT_PSEUDO_SEED) } + CryptEmptySha256 => { + call_bitcode_fn(env, &[], bitcode::CRYPT_EMPTY_SHA256) + } + CryptAddBytes => { + arguments!(sha, data); + call_bitcode_fn(env, &[sha, data], bitcode::CRYPT_ADD_BYTES) + } + CryptDigest => { + arguments!(sha); + call_bitcode_fn(env, &[sha], bitcode::CRYPT_DIGEST) + } ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { unreachable!("only inserted in dev backend codegen") diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index 7cdadc7f9ec..aa4ca65f78e 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1738,10 +1738,10 @@ define_builtins! { 15 CRYPT: "Crypt" =>{ 0 CRYPT_SHA256: "Sha256" exposed_type=true 1 CRYPT_EMPTY_STRUCT: "EmptyStruct" exposed_type=true - 3 CRYPT_DIGEST256: "Digest256" exposed_type=true - 4 CRYPT_EMPTY_SHA256: "emptySha256" - 5 CRYPT_ADD_BYTES: "addBytes" - 6 CRYPT_DIGEST: "digest" + 2 CRYPT_DIGEST256: "Digest256" exposed_type=true + 3 CRYPT_EMPTY_SHA256: "emptySha256" + 4 CRYPT_ADD_BYTES: "addBytes" + 5 CRYPT_DIGEST: "digest" } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) From 315c396b5158f261f3e3ec61046f191bdfbd4d3f Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Fri, 9 Aug 2024 18:09:09 +0100 Subject: [PATCH 11/57] switch out bad pointer rep --- crates/compiler/builtins/bitcode/src/crypt.zig | 8 ++++---- crates/compiler/builtins/roc/Crypt.roc | 16 ++++++++++++++++ crates/compiler/builtins/roc/main.roc | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index fd89e4b35e9..49cceef0b9b 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -5,19 +5,19 @@ const list = @import("list.zig"); const utils = @import("utils.zig"); const Sha256 = extern struct{ - location:usize, + location:[*]u8, fn pointer(self : Sha256) *sha2.Sha256{ - return @ptrFromInt(self.location); + return @alignCast(@ptrCast(self.location)); } }; const EmptyStruct = extern struct{}; pub fn emptySha256(_: EmptyStruct) callconv(.C) Sha256{ - const allocation = utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256)); + const allocation = utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256), false); const ptr:*sha2.Sha256 = @alignCast(@ptrCast(allocation)); ptr.* = sha2.Sha256.init(.{}); - return Sha256{.location = @intFromPtr(ptr),}; + return Sha256{.location = @alignCast(@ptrCast(ptr)),}; } pub fn addBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256{ diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index e69de29bb2d..89117ae8bdd 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -0,0 +1,16 @@ +module[ + emptySha256, + addBytes, + digest, + hashSha256, +] +import List +emptySha256 : {} -> Sha256 + +addBytes : Sha256, List.List u8 -> Sha256 + +digest : Sha256 -> Digest256 + +hashSha256 : List u8 -> Digest256 +hashSha256 = \bytes -> + digest ( addBytes (emptySha256 {}) bytes) diff --git a/crates/compiler/builtins/roc/main.roc b/crates/compiler/builtins/roc/main.roc index 36edf4ed49b..13ef4eb07c8 100644 --- a/crates/compiler/builtins/roc/main.roc +++ b/crates/compiler/builtins/roc/main.roc @@ -12,4 +12,5 @@ package [ Box, Inspect, Task, + Crypt, ] {} From 0824029d41d5cb97c9f1462165d1b6dbab3cd05f Mon Sep 17 00:00:00 2001 From: Sam Mohr Date: Fri, 30 Aug 2024 13:50:47 -0700 Subject: [PATCH 12/57] Fix Crypt builtin --- .../compiler/builtins/bitcode/src/crypt.zig | 35 ++++++++++--------- crates/compiler/builtins/bitcode/src/main.zig | 7 ++-- crates/compiler/builtins/roc/Crypt.roc | 30 +++++++++++----- crates/compiler/builtins/src/bitcode.rs | 2 +- crates/compiler/builtins/src/roc.rs | 2 ++ crates/compiler/can/src/builtins.rs | 2 +- crates/compiler/gen_dev/src/lib.rs | 15 ++++++++ crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 8 ++--- crates/compiler/load/build.rs | 1 + crates/compiler/load/src/lib.rs | 3 ++ crates/compiler/load_internal/src/file.rs | 3 ++ crates/compiler/load_internal/src/lib.rs | 1 + .../load_internal/src/module_cache.rs | 1 + crates/compiler/module/src/ident.rs | 1 + crates/compiler/module/src/low_level.rs | 2 +- crates/compiler/module/src/symbol.rs | 18 +++++----- 16 files changed, 85 insertions(+), 46 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 49cceef0b9b..10e7484b88f 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -4,34 +4,37 @@ const sha2 = crypto.hash.sha2; const list = @import("list.zig"); const utils = @import("utils.zig"); -const Sha256 = extern struct{ - location:[*]u8, - fn pointer(self : Sha256) *sha2.Sha256{ +const Sha256 = extern struct { + location: [*]u8, + fn pointer(self: Sha256) *sha2.Sha256 { return @alignCast(@ptrCast(self.location)); } - }; - -const EmptyStruct = extern struct{}; +}; -pub fn emptySha256(_: EmptyStruct) callconv(.C) Sha256{ +pub fn emptySha256() callconv(.C) Sha256 { const allocation = utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256), false); - const ptr:*sha2.Sha256 = @alignCast(@ptrCast(allocation)); + const ptr: *sha2.Sha256 = @alignCast(@ptrCast(allocation)); ptr.* = sha2.Sha256.init(.{}); - return Sha256{.location = @alignCast(@ptrCast(ptr)),}; + return Sha256{ + .location = @alignCast(@ptrCast(ptr)), + }; } -pub fn addBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256{ - var out = emptySha256(undefined); +pub fn addBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { + var out = emptySha256(); out.pointer().* = sha.pointer().*; - if(data.bytes)|bytes|{ - const byteSlice : []u8 = bytes[0..data.length]; + if (data.bytes) |bytes| { + const byteSlice: []u8 = bytes[0..data.length]; out.pointer().*.update(byteSlice); } return out; } -pub const Digest256 = extern struct{firstHalf:u128, secondHalf:u128,}; +pub const Digest256 = extern struct { + firstHalf: u128, + secondHalf: u128, +}; -pub fn digest(sha: Sha256 ) callconv(.C) Digest256{ +pub fn digest(sha: Sha256) callconv(.C) Digest256 { return @bitCast(sha.pointer().*.peek()); -} \ No newline at end of file +} diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index ee9c90ad742..04a02f7ab97 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -12,13 +12,12 @@ const STR = "str"; // Crypt Module const crypt = @import("crypt.zig"); -comptime{ - exportCryptFn(crypt.emptySha256, "empty_sha256"); - exportCryptFn(crypt.addBytes, "add_bytes"); +comptime { + exportCryptFn(crypt.emptySha256, "emptySha256"); + exportCryptFn(crypt.addBytes, "addBytes"); exportCryptFn(crypt.digest, "digest"); } - // Dec Module const dec = @import("dec.zig"); diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index 89117ae8bdd..e19cb81bf25 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -1,16 +1,30 @@ -module[ - emptySha256, - addBytes, - digest, +module [ + emptySha256, + addBytes, + digest, hashSha256, + Sha256, + Digest256, ] + import List +import Num exposing [U8, U64, U128] + +Sha256 := { + location : U64, +} + +Digest256 := { + firstHalf : U128, + secondHalf : U128, +} + emptySha256 : {} -> Sha256 -addBytes : Sha256, List.List u8 -> Sha256 +addBytes : Sha256, List.List U8 -> Sha256 digest : Sha256 -> Digest256 -hashSha256 : List u8 -> Digest256 -hashSha256 = \bytes -> - digest ( addBytes (emptySha256 {}) bytes) +hashSha256 : List U8 -> Digest256 +hashSha256 = \bytes -> + digest (addBytes (emptySha256 {}) bytes) diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index 948536cd808..5dcd6634461 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -417,7 +417,7 @@ pub const DEC_ROUND: IntrinsicName = int_intrinsic!("roc_builtins.dec.round"); pub const DEC_FLOOR: IntrinsicName = int_intrinsic!("roc_builtins.dec.floor"); pub const DEC_CEILING: IntrinsicName = int_intrinsic!("roc_builtins.dec.ceiling"); -pub const CRYPT_EMPTY_SHA256: &str = "roc_builins.crypt.emptySha256"; +pub const CRYPT_EMPTY_SHA256: &str = "roc_builtins.crypt.emptySha256"; pub const CRYPT_ADD_BYTES: &str = "roc_builtins.crypt.addBytes"; pub const CRYPT_DIGEST: &str = "roc_builtins.crypt.digest"; diff --git a/crates/compiler/builtins/src/roc.rs b/crates/compiler/builtins/src/roc.rs index 2093c8e4cb2..6701d207eb3 100644 --- a/crates/compiler/builtins/src/roc.rs +++ b/crates/compiler/builtins/src/roc.rs @@ -17,6 +17,7 @@ pub fn module_source(module_id: ModuleId) -> &'static str { ModuleId::HASH => HASH, ModuleId::INSPECT => INSPECT, ModuleId::TASK => TASK, + ModuleId::CRYPT => CRYPT, _ => internal_error!( "ModuleId {:?} is not part of the standard library", module_id @@ -37,3 +38,4 @@ const DECODE: &str = include_str!("../roc/Decode.roc"); const HASH: &str = include_str!("../roc/Hash.roc"); const INSPECT: &str = include_str!("../roc/Inspect.roc"); const TASK: &str = include_str!("../roc/Task.roc"); +const CRYPT: &str = include_str!("../roc/Crypt.roc"); diff --git a/crates/compiler/can/src/builtins.rs b/crates/compiler/can/src/builtins.rs index 3d9bdf320cc..5b3e570ec52 100644 --- a/crates/compiler/can/src/builtins.rs +++ b/crates/compiler/can/src/builtins.rs @@ -210,7 +210,7 @@ map_symbol_to_lowlevel_and_arity! { NumF32FromParts; NUM_F32_FROM_PARTS; 1, NumF64FromParts; NUM_F64_FROM_PARTS; 1, - CryptEmptySha256; CRYPT_EMPTY_SHA256; 1, + CryptEmptySha256; CRYPT_EMPTY_SHA_256; 1, CryptAddBytes; CRYPT_ADD_BYTES; 2, CryptDigest; CRYPT_DIGEST; 1, diff --git a/crates/compiler/gen_dev/src/lib.rs b/crates/compiler/gen_dev/src/lib.rs index 8eca9aa5ee3..e21bd10686e 100644 --- a/crates/compiler/gen_dev/src/lib.rs +++ b/crates/compiler/gen_dev/src/lib.rs @@ -2216,6 +2216,21 @@ trait Backend<'a> { self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout) } + LowLevel::CryptEmptySha256 => { + let intrinsic = bitcode::CRYPT_EMPTY_SHA256.to_string(); + self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); + } + + LowLevel::CryptAddBytes => { + let intrinsic = bitcode::CRYPT_ADD_BYTES.to_string(); + self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); + } + + LowLevel::CryptDigest => { + let intrinsic = bitcode::CRYPT_DIGEST.to_string(); + self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); + } + x => todo!("low level, {:?}", x), } } diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index ead1067637c..bcb57b4842e 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1431,14 +1431,12 @@ pub(crate) fn run_low_level<'a, 'ctx>( call_bitcode_fn(env, &[], bitcode::UTILS_DICT_PSEUDO_SEED) } - CryptEmptySha256 => { - call_bitcode_fn(env, &[], bitcode::CRYPT_EMPTY_SHA256) - } - CryptAddBytes => { + CryptEmptySha256 => call_bitcode_fn(env, &[], bitcode::CRYPT_EMPTY_SHA256), + CryptAddBytes => { arguments!(sha, data); call_bitcode_fn(env, &[sha, data], bitcode::CRYPT_ADD_BYTES) } - CryptDigest => { + CryptDigest => { arguments!(sha); call_bitcode_fn(env, &[sha], bitcode::CRYPT_DIGEST) } diff --git a/crates/compiler/load/build.rs b/crates/compiler/load/build.rs index 6f0956bf7ec..85b9d32f8cf 100644 --- a/crates/compiler/load/build.rs +++ b/crates/compiler/load/build.rs @@ -26,6 +26,7 @@ const MODULES: &[(ModuleId, &str)] = &[ (ModuleId::HASH, "Hash.roc"), (ModuleId::INSPECT, "Inspect.roc"), (ModuleId::TASK, "Task.roc"), + (ModuleId::CRYPT, "Crypt.roc"), ]; fn main() { diff --git a/crates/compiler/load/src/lib.rs b/crates/compiler/load/src/lib.rs index 179a7d1da5d..b3653a59af7 100644 --- a/crates/compiler/load/src/lib.rs +++ b/crates/compiler/load/src/lib.rs @@ -257,6 +257,7 @@ fn read_cached_types() -> MutMap { let mod_hash = include_bytes_align_as!(u128, concat!(env!("OUT_DIR"), "/Hash.dat")); let mod_inspect = include_bytes_align_as!(u128, concat!(env!("OUT_DIR"), "/Inspect.dat")); let mod_task = include_bytes_align_as!(u128, concat!(env!("OUT_DIR"), "/Task.dat")); + let mod_crypt = include_bytes_align_as!(u128, concat!(env!("OUT_DIR"), "/Crypt.dat")); let mut output = MutMap::default(); @@ -282,6 +283,8 @@ fn read_cached_types() -> MutMap { output.insert(ModuleId::INSPECT, deserialize_help(mod_inspect)); output.insert(ModuleId::TASK, deserialize_help(mod_task)); + + output.insert(ModuleId::CRYPT, deserialize_help(mod_crypt)); } output diff --git a/crates/compiler/load_internal/src/file.rs b/crates/compiler/load_internal/src/file.rs index 0a47cd22eba..0f7844ae195 100644 --- a/crates/compiler/load_internal/src/file.rs +++ b/crates/compiler/load_internal/src/file.rs @@ -2338,6 +2338,7 @@ fn update<'a>( extend_module_with_builtin_import(parsed, ModuleId::HASH); extend_module_with_builtin_import(parsed, ModuleId::INSPECT); extend_module_with_builtin_import(parsed, ModuleId::TASK); + extend_module_with_builtin_import(parsed, ModuleId::CRYPT); } state .module_cache @@ -3659,6 +3660,7 @@ fn load_module<'a>( "Hash", ModuleId::HASH "Inspect", ModuleId::INSPECT "Task", ModuleId::TASK + "Crypt", ModuleId::CRYPT } let (filename, opt_shorthand) = module_name_to_path(src_dir, &module_name, arc_shorthands); @@ -5231,6 +5233,7 @@ fn canonicalize_and_constrain<'a>( | ModuleId::HASH | ModuleId::INSPECT | ModuleId::TASK + | ModuleId::CRYPT ); if !name.is_builtin() || should_include_builtin { diff --git a/crates/compiler/load_internal/src/lib.rs b/crates/compiler/load_internal/src/lib.rs index a19a0a445f5..e86bdaf1ee7 100644 --- a/crates/compiler/load_internal/src/lib.rs +++ b/crates/compiler/load_internal/src/lib.rs @@ -26,4 +26,5 @@ pub const BUILTIN_MODULES: &[(ModuleId, &str)] = &[ (ModuleId::HASH, "Hash"), (ModuleId::INSPECT, "Inspect"), (ModuleId::TASK, "Task"), + (ModuleId::CRYPT, "Crypt"), ]; diff --git a/crates/compiler/load_internal/src/module_cache.rs b/crates/compiler/load_internal/src/module_cache.rs index e53c4ee5cc3..165c508b725 100644 --- a/crates/compiler/load_internal/src/module_cache.rs +++ b/crates/compiler/load_internal/src/module_cache.rs @@ -94,6 +94,7 @@ impl Default for ModuleCache<'_> { HASH, INSPECT, TASK, + CRYPT, } Self { diff --git a/crates/compiler/module/src/ident.rs b/crates/compiler/module/src/ident.rs index 7571c0f57f8..b98d72b179f 100644 --- a/crates/compiler/module/src/ident.rs +++ b/crates/compiler/module/src/ident.rs @@ -131,6 +131,7 @@ impl ModuleName { pub const HASH: &'static str = "Hash"; pub const INSPECT: &'static str = "Inspect"; pub const TASK: &'static str = "Task"; + pub const CRYPT: &'static str = "Crypt"; pub fn as_str(&self) -> &str { self.0.as_str() diff --git a/crates/compiler/module/src/low_level.rs b/crates/compiler/module/src/low_level.rs index f1e79ac21a4..cbec1b42735 100644 --- a/crates/compiler/module/src/low_level.rs +++ b/crates/compiler/module/src/low_level.rs @@ -351,7 +351,7 @@ map_symbol_to_lowlevel! { Not <= BOOL_NOT; Unreachable <= LIST_UNREACHABLE; DictPseudoSeed <= DICT_PSEUDO_SEED; - CryptEmptySha256 <= CRYPT_EMPTY_SHA256; + CryptEmptySha256 <= CRYPT_EMPTY_SHA_256; CryptAddBytes <= CRYPT_ADD_BYTES; CryptDigest <= CRYPT_DIGEST; } diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index aa4ca65f78e..e1b05100dcf 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1716,7 +1716,7 @@ define_builtins! { 32 INSPECT_TO_INSPECTOR: "toInspector" 33 INSPECT_TO_STR: "toStr" } - 16 TASK: "Task" => { + 15 TASK: "Task" => { 0 TASK_TASK: "Task" exposed_type=true // the Task.Task opaque type 1 TASK_FOREVER: "forever" 2 TASK_LOOP: "loop" @@ -1733,15 +1733,13 @@ define_builtins! { 13 TASK_FOR_EACH: "forEach" 14 TASK_RESULT: "result" } - - num_modules: 16 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) - 15 CRYPT: "Crypt" =>{ - 0 CRYPT_SHA256: "Sha256" exposed_type=true - 1 CRYPT_EMPTY_STRUCT: "EmptyStruct" exposed_type=true - 2 CRYPT_DIGEST256: "Digest256" exposed_type=true - 3 CRYPT_EMPTY_SHA256: "emptySha256" - 4 CRYPT_ADD_BYTES: "addBytes" - 5 CRYPT_DIGEST: "digest" + 16 CRYPT: "Crypt" =>{ + 0 CRYPT_SHA_256: "Sha256" exposed_type=true + 1 CRYPT_DIGEST_256: "Digest256" exposed_type=true + 2 CRYPT_EMPTY_SHA_256: "emptySha256" + 3 CRYPT_ADD_BYTES: "addBytes" + 4 CRYPT_DIGEST: "digest" + 5 CRYPT_HASH_SHA_256: "hashSha256" } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) From 277bc0be646ee09eb7794d7a1ac754408438e56b Mon Sep 17 00:00:00 2001 From: Sam Mohr Date: Fri, 30 Aug 2024 13:54:49 -0700 Subject: [PATCH 13/57] Fix formatting --- crates/compiler/gen_wasm/src/low_level.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index d3dce52a030..6320c2ea660 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2234,7 +2234,7 @@ impl<'a> LowLevelCall<'a> { BoxExpr | UnboxExpr => { unreachable!("The {:?} operation is turned into mono Expr", self.lowlevel) - }, + } Unreachable => match self.ret_storage { StoredValue::Local { value_type, .. } => match value_type { From 2f986f6f5f75a4fdda87770949adf3af6ea336c5 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 2 Sep 2024 21:36:38 +0100 Subject: [PATCH 14/57] Adding digest inspectors --- crates/compiler/builtins/bitcode/src/crypt.zig | 14 +++++++++++--- crates/compiler/builtins/bitcode/src/main.zig | 2 ++ crates/compiler/builtins/roc/Crypt.roc | 6 ++++++ crates/compiler/builtins/src/bitcode.rs | 2 ++ crates/compiler/can/src/builtins.rs | 2 ++ crates/compiler/gen_dev/src/lib.rs | 12 ++++++++++++ crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 9 +++++++++ crates/compiler/gen_wasm/src/low_level.rs | 2 ++ crates/compiler/module/src/low_level.rs | 5 +++++ crates/compiler/module/src/symbol.rs | 2 ++ crates/compiler/mono/src/drop_specialization.rs | 2 +- crates/compiler/mono/src/inc_dec.rs | 2 ++ 12 files changed, 56 insertions(+), 4 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 10e7484b88f..e320db7cf12 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -1,6 +1,7 @@ const std = @import("std"); const crypto = std.crypto; const sha2 = crypto.hash.sha2; +const mem = std.mem; const list = @import("list.zig"); const utils = @import("utils.zig"); @@ -24,17 +25,24 @@ pub fn addBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { var out = emptySha256(); out.pointer().* = sha.pointer().*; if (data.bytes) |bytes| { - const byteSlice: []u8 = bytes[0..data.length]; + const byteSlice = bytes[0..data.length]; out.pointer().*.update(byteSlice); } return out; } pub const Digest256 = extern struct { - firstHalf: u128, - secondHalf: u128, + bytes:[32]u8, }; pub fn digest(sha: Sha256) callconv(.C) Digest256 { return @bitCast(sha.pointer().*.peek()); } + +pub fn digest256Eq(left : Digest256, right : Digest256) callconv(.C) bool{ + return mem.eql(u8, &left.bytes, &right.bytes); +} + +pub fn digest256ByteList(dig : Digest256) callconv(.C) list.RocList{ + return list.RocList.fromSlice(u8, dig.bytes[0..32],false); +} \ No newline at end of file diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index 04a02f7ab97..d447fbf8f02 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -16,6 +16,8 @@ comptime { exportCryptFn(crypt.emptySha256, "emptySha256"); exportCryptFn(crypt.addBytes, "addBytes"); exportCryptFn(crypt.digest, "digest"); + exportCryptFn(crypt.digest256Eq, "digest256Eq"); + exportCryptFn(crypt.digest256ByteList, "digest256ByteList"); } // Dec Module diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index e19cb81bf25..cb6da751104 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -3,6 +3,8 @@ module [ addBytes, digest, hashSha256, + digest256Eq, + digest256ByteList, Sha256, Digest256, ] @@ -25,6 +27,10 @@ addBytes : Sha256, List.List U8 -> Sha256 digest : Sha256 -> Digest256 +digest256Eq : Digest256, Digest256 -> Bool.Bool + +digest256ByteList: Digest256 -> List.List U8 + hashSha256 : List U8 -> Digest256 hashSha256 = \bytes -> digest (addBytes (emptySha256 {}) bytes) diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index 5dcd6634461..dc170932e6a 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -420,6 +420,8 @@ pub const DEC_CEILING: IntrinsicName = int_intrinsic!("roc_builtins.dec.ceiling" pub const CRYPT_EMPTY_SHA256: &str = "roc_builtins.crypt.emptySha256"; pub const CRYPT_ADD_BYTES: &str = "roc_builtins.crypt.addBytes"; pub const CRYPT_DIGEST: &str = "roc_builtins.crypt.digest"; +pub const CRYPT_DIGEST256_EQ: &str = "roc_builtins.crypt.digest256Eq"; +pub const CRYPT_DIGEST256_BYTE_LIST: &str = "roc_builtins.crypt.digest256Eq"; pub const UTILS_DBG_IMPL: &str = "roc_builtins.utils.dbg_impl"; pub const UTILS_TEST_PANIC: &str = "roc_builtins.utils.test_panic"; diff --git a/crates/compiler/can/src/builtins.rs b/crates/compiler/can/src/builtins.rs index 5b3e570ec52..24dc73b89c6 100644 --- a/crates/compiler/can/src/builtins.rs +++ b/crates/compiler/can/src/builtins.rs @@ -213,6 +213,8 @@ map_symbol_to_lowlevel_and_arity! { CryptEmptySha256; CRYPT_EMPTY_SHA_256; 1, CryptAddBytes; CRYPT_ADD_BYTES; 2, CryptDigest; CRYPT_DIGEST; 1, + CryptDigest256Eq; CRYPT_DIGEST_256_EQ; 2, + CryptDigest256ByteList; CRYPT_DIGEST_256_BYTE_LIST; 1, Eq; BOOL_STRUCTURAL_EQ; 2, NotEq; BOOL_STRUCTURAL_NOT_EQ; 2, diff --git a/crates/compiler/gen_dev/src/lib.rs b/crates/compiler/gen_dev/src/lib.rs index e21bd10686e..ff6c475eeeb 100644 --- a/crates/compiler/gen_dev/src/lib.rs +++ b/crates/compiler/gen_dev/src/lib.rs @@ -2231,6 +2231,18 @@ trait Backend<'a> { self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } + LowLevel::CryptDigest256Eq => { + let intrinsic = bitcode::CRYPT_DIGEST256_EQ.to_string(); + self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); + } + + LowLevel::CryptDigest256ByteList => { + let intrinsic = bitcode::CRYPT_DIGEST256_BYTE_LIST.to_string(); + self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); + } + + + x => todo!("low level, {:?}", x), } } diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index bcb57b4842e..42ce455235b 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1440,6 +1440,15 @@ pub(crate) fn run_low_level<'a, 'ctx>( arguments!(sha); call_bitcode_fn(env, &[sha], bitcode::CRYPT_DIGEST) } + CryptDigest256Eq => { + arguments!(sha); + call_bitcode_fn(env, &[sha], bitcode::CRYPT_DIGEST256_EQ) + } + CryptDigest256ByteList => { + arguments!(sha); + call_bitcode_fn(env, &[sha], bitcode::CRYPT_DIGEST256_BYTE_LIST) + } + ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { unreachable!("only inserted in dev backend codegen") diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index 6320c2ea660..3421f7c7b62 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2167,6 +2167,8 @@ impl<'a> LowLevelCall<'a> { CryptEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPT_EMPTY_SHA256), CryptAddBytes => self.load_args_and_call_zig(backend, bitcode::CRYPT_ADD_BYTES), CryptDigest => self.load_args_and_call_zig(backend, bitcode::CRYPT_DIGEST), + CryptDigest256Eq => self.load_args_and_call_zig(backend, bitcode::CRYPT_DIGEST256_EQ), + CryptDigest256ByteList => self.load_args_and_call_zig(backend, bitcode::CRYPT_DIGEST256_BYTE_LIST), And => { self.load_args(backend); diff --git a/crates/compiler/module/src/low_level.rs b/crates/compiler/module/src/low_level.rs index cbec1b42735..3d504d0446a 100644 --- a/crates/compiler/module/src/low_level.rs +++ b/crates/compiler/module/src/low_level.rs @@ -132,6 +132,8 @@ pub enum LowLevel { CryptEmptySha256, CryptAddBytes, CryptDigest, + CryptDigest256Eq, + CryptDigest256ByteList } macro_rules! higher_order { @@ -354,4 +356,7 @@ map_symbol_to_lowlevel! { CryptEmptySha256 <= CRYPT_EMPTY_SHA_256; CryptAddBytes <= CRYPT_ADD_BYTES; CryptDigest <= CRYPT_DIGEST; + CryptDigest256Eq <= CRYPT_DIGEST_256_EQ; + CryptDigest256ByteList <= CRYPT_DIGEST_256_BYTE_LIST; + } diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index e1b05100dcf..5af2620e532 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1740,6 +1740,8 @@ define_builtins! { 3 CRYPT_ADD_BYTES: "addBytes" 4 CRYPT_DIGEST: "digest" 5 CRYPT_HASH_SHA_256: "hashSha256" + 6 CRYPT_DIGEST_256_EQ: "digest256Eq" + 7 CRYPT_DIGEST_256_BYTE_LIST: "digest256ByteList" } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) diff --git a/crates/compiler/mono/src/drop_specialization.rs b/crates/compiler/mono/src/drop_specialization.rs index 9d2f98d6a9a..ba01e2ee8a8 100644 --- a/crates/compiler/mono/src/drop_specialization.rs +++ b/crates/compiler/mono/src/drop_specialization.rs @@ -1609,7 +1609,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC { ListIsUnique => RC::Rc, ListClone => RC::Rc, - CryptEmptySha256 | CryptAddBytes | CryptDigest => RC::NoRc, + CryptEmptySha256 | CryptAddBytes | CryptDigest | CryptDigest256Eq | CryptDigest256ByteList => RC::NoRc, BoxExpr | UnboxExpr => { unreachable!("These lowlevel operations are turned into mono Expr's") diff --git a/crates/compiler/mono/src/inc_dec.rs b/crates/compiler/mono/src/inc_dec.rs index 87b133b046f..8a87531f069 100644 --- a/crates/compiler/mono/src/inc_dec.rs +++ b/crates/compiler/mono/src/inc_dec.rs @@ -1259,6 +1259,8 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] { CryptEmptySha256 => &[IRRELEVANT], CryptAddBytes => &[OWNED, BORROWED], CryptDigest => &[BORROWED], + CryptDigest256Eq => &[BORROWED], + CryptDigest256ByteList => &[BORROWED], ListIncref => &[OWNED], ListDecref => &[OWNED], From 2ef3af39ca52216875fc69460081ca11c8c209ba Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 3 Sep 2024 18:38:40 +0100 Subject: [PATCH 15/57] Revert "Adding digest inspectors" This reverts commit 77b9223bfc9b036106d8ff6e519708140057e0c1. --- crates/compiler/builtins/bitcode/src/crypt.zig | 14 +++----------- crates/compiler/builtins/bitcode/src/main.zig | 2 -- crates/compiler/builtins/roc/Crypt.roc | 6 ------ crates/compiler/builtins/src/bitcode.rs | 2 -- crates/compiler/can/src/builtins.rs | 2 -- crates/compiler/gen_dev/src/lib.rs | 12 ------------ crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 9 --------- crates/compiler/gen_wasm/src/low_level.rs | 2 -- crates/compiler/module/src/low_level.rs | 5 ----- crates/compiler/module/src/symbol.rs | 2 -- crates/compiler/mono/src/drop_specialization.rs | 2 +- crates/compiler/mono/src/inc_dec.rs | 2 -- 12 files changed, 4 insertions(+), 56 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index e320db7cf12..10e7484b88f 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -1,7 +1,6 @@ const std = @import("std"); const crypto = std.crypto; const sha2 = crypto.hash.sha2; -const mem = std.mem; const list = @import("list.zig"); const utils = @import("utils.zig"); @@ -25,24 +24,17 @@ pub fn addBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { var out = emptySha256(); out.pointer().* = sha.pointer().*; if (data.bytes) |bytes| { - const byteSlice = bytes[0..data.length]; + const byteSlice: []u8 = bytes[0..data.length]; out.pointer().*.update(byteSlice); } return out; } pub const Digest256 = extern struct { - bytes:[32]u8, + firstHalf: u128, + secondHalf: u128, }; pub fn digest(sha: Sha256) callconv(.C) Digest256 { return @bitCast(sha.pointer().*.peek()); } - -pub fn digest256Eq(left : Digest256, right : Digest256) callconv(.C) bool{ - return mem.eql(u8, &left.bytes, &right.bytes); -} - -pub fn digest256ByteList(dig : Digest256) callconv(.C) list.RocList{ - return list.RocList.fromSlice(u8, dig.bytes[0..32],false); -} \ No newline at end of file diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index d447fbf8f02..04a02f7ab97 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -16,8 +16,6 @@ comptime { exportCryptFn(crypt.emptySha256, "emptySha256"); exportCryptFn(crypt.addBytes, "addBytes"); exportCryptFn(crypt.digest, "digest"); - exportCryptFn(crypt.digest256Eq, "digest256Eq"); - exportCryptFn(crypt.digest256ByteList, "digest256ByteList"); } // Dec Module diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index cb6da751104..e19cb81bf25 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -3,8 +3,6 @@ module [ addBytes, digest, hashSha256, - digest256Eq, - digest256ByteList, Sha256, Digest256, ] @@ -27,10 +25,6 @@ addBytes : Sha256, List.List U8 -> Sha256 digest : Sha256 -> Digest256 -digest256Eq : Digest256, Digest256 -> Bool.Bool - -digest256ByteList: Digest256 -> List.List U8 - hashSha256 : List U8 -> Digest256 hashSha256 = \bytes -> digest (addBytes (emptySha256 {}) bytes) diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index dc170932e6a..5dcd6634461 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -420,8 +420,6 @@ pub const DEC_CEILING: IntrinsicName = int_intrinsic!("roc_builtins.dec.ceiling" pub const CRYPT_EMPTY_SHA256: &str = "roc_builtins.crypt.emptySha256"; pub const CRYPT_ADD_BYTES: &str = "roc_builtins.crypt.addBytes"; pub const CRYPT_DIGEST: &str = "roc_builtins.crypt.digest"; -pub const CRYPT_DIGEST256_EQ: &str = "roc_builtins.crypt.digest256Eq"; -pub const CRYPT_DIGEST256_BYTE_LIST: &str = "roc_builtins.crypt.digest256Eq"; pub const UTILS_DBG_IMPL: &str = "roc_builtins.utils.dbg_impl"; pub const UTILS_TEST_PANIC: &str = "roc_builtins.utils.test_panic"; diff --git a/crates/compiler/can/src/builtins.rs b/crates/compiler/can/src/builtins.rs index 24dc73b89c6..5b3e570ec52 100644 --- a/crates/compiler/can/src/builtins.rs +++ b/crates/compiler/can/src/builtins.rs @@ -213,8 +213,6 @@ map_symbol_to_lowlevel_and_arity! { CryptEmptySha256; CRYPT_EMPTY_SHA_256; 1, CryptAddBytes; CRYPT_ADD_BYTES; 2, CryptDigest; CRYPT_DIGEST; 1, - CryptDigest256Eq; CRYPT_DIGEST_256_EQ; 2, - CryptDigest256ByteList; CRYPT_DIGEST_256_BYTE_LIST; 1, Eq; BOOL_STRUCTURAL_EQ; 2, NotEq; BOOL_STRUCTURAL_NOT_EQ; 2, diff --git a/crates/compiler/gen_dev/src/lib.rs b/crates/compiler/gen_dev/src/lib.rs index ff6c475eeeb..e21bd10686e 100644 --- a/crates/compiler/gen_dev/src/lib.rs +++ b/crates/compiler/gen_dev/src/lib.rs @@ -2231,18 +2231,6 @@ trait Backend<'a> { self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } - LowLevel::CryptDigest256Eq => { - let intrinsic = bitcode::CRYPT_DIGEST256_EQ.to_string(); - self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); - } - - LowLevel::CryptDigest256ByteList => { - let intrinsic = bitcode::CRYPT_DIGEST256_BYTE_LIST.to_string(); - self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); - } - - - x => todo!("low level, {:?}", x), } } diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 42ce455235b..bcb57b4842e 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1440,15 +1440,6 @@ pub(crate) fn run_low_level<'a, 'ctx>( arguments!(sha); call_bitcode_fn(env, &[sha], bitcode::CRYPT_DIGEST) } - CryptDigest256Eq => { - arguments!(sha); - call_bitcode_fn(env, &[sha], bitcode::CRYPT_DIGEST256_EQ) - } - CryptDigest256ByteList => { - arguments!(sha); - call_bitcode_fn(env, &[sha], bitcode::CRYPT_DIGEST256_BYTE_LIST) - } - ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { unreachable!("only inserted in dev backend codegen") diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index 3421f7c7b62..6320c2ea660 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2167,8 +2167,6 @@ impl<'a> LowLevelCall<'a> { CryptEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPT_EMPTY_SHA256), CryptAddBytes => self.load_args_and_call_zig(backend, bitcode::CRYPT_ADD_BYTES), CryptDigest => self.load_args_and_call_zig(backend, bitcode::CRYPT_DIGEST), - CryptDigest256Eq => self.load_args_and_call_zig(backend, bitcode::CRYPT_DIGEST256_EQ), - CryptDigest256ByteList => self.load_args_and_call_zig(backend, bitcode::CRYPT_DIGEST256_BYTE_LIST), And => { self.load_args(backend); diff --git a/crates/compiler/module/src/low_level.rs b/crates/compiler/module/src/low_level.rs index 3d504d0446a..cbec1b42735 100644 --- a/crates/compiler/module/src/low_level.rs +++ b/crates/compiler/module/src/low_level.rs @@ -132,8 +132,6 @@ pub enum LowLevel { CryptEmptySha256, CryptAddBytes, CryptDigest, - CryptDigest256Eq, - CryptDigest256ByteList } macro_rules! higher_order { @@ -356,7 +354,4 @@ map_symbol_to_lowlevel! { CryptEmptySha256 <= CRYPT_EMPTY_SHA_256; CryptAddBytes <= CRYPT_ADD_BYTES; CryptDigest <= CRYPT_DIGEST; - CryptDigest256Eq <= CRYPT_DIGEST_256_EQ; - CryptDigest256ByteList <= CRYPT_DIGEST_256_BYTE_LIST; - } diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index 5af2620e532..e1b05100dcf 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1740,8 +1740,6 @@ define_builtins! { 3 CRYPT_ADD_BYTES: "addBytes" 4 CRYPT_DIGEST: "digest" 5 CRYPT_HASH_SHA_256: "hashSha256" - 6 CRYPT_DIGEST_256_EQ: "digest256Eq" - 7 CRYPT_DIGEST_256_BYTE_LIST: "digest256ByteList" } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) diff --git a/crates/compiler/mono/src/drop_specialization.rs b/crates/compiler/mono/src/drop_specialization.rs index ba01e2ee8a8..9d2f98d6a9a 100644 --- a/crates/compiler/mono/src/drop_specialization.rs +++ b/crates/compiler/mono/src/drop_specialization.rs @@ -1609,7 +1609,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC { ListIsUnique => RC::Rc, ListClone => RC::Rc, - CryptEmptySha256 | CryptAddBytes | CryptDigest | CryptDigest256Eq | CryptDigest256ByteList => RC::NoRc, + CryptEmptySha256 | CryptAddBytes | CryptDigest => RC::NoRc, BoxExpr | UnboxExpr => { unreachable!("These lowlevel operations are turned into mono Expr's") diff --git a/crates/compiler/mono/src/inc_dec.rs b/crates/compiler/mono/src/inc_dec.rs index 8a87531f069..87b133b046f 100644 --- a/crates/compiler/mono/src/inc_dec.rs +++ b/crates/compiler/mono/src/inc_dec.rs @@ -1259,8 +1259,6 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] { CryptEmptySha256 => &[IRRELEVANT], CryptAddBytes => &[OWNED, BORROWED], CryptDigest => &[BORROWED], - CryptDigest256Eq => &[BORROWED], - CryptDigest256ByteList => &[BORROWED], ListIncref => &[OWNED], ListDecref => &[OWNED], From 44243bae89a9e8e50e05a4f056e23e333d7321a4 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Wed, 4 Sep 2024 21:12:08 +0100 Subject: [PATCH 16/57] Adds functions to access digest --- crates/compiler/builtins/roc/Crypt.roc | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index e19cb81bf25..91476be8ca5 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -3,12 +3,14 @@ module [ addBytes, digest, hashSha256, + digest256ToBytes, Sha256, Digest256, ] import List -import Num exposing [U8, U64, U128] +import Num exposing [U8, U64, U128, Int] +import Num Sha256 := { location : U64, @@ -17,14 +19,27 @@ Sha256 := { Digest256 := { firstHalf : U128, secondHalf : U128, -} +} implements[Eq, Hash] emptySha256 : {} -> Sha256 -addBytes : Sha256, List.List U8 -> Sha256 +addBytes : Sha256, List U8 -> Sha256 digest : Sha256 -> Digest256 hashSha256 : List U8 -> Digest256 hashSha256 = \bytes -> digest (addBytes (emptySha256 {}) bytes) + +u128Bytes : U128 -> List U8 +u128Bytes = \ number -> + loop = \ n, bytes, place -> + if place == 16 then bytes + else + newByte = n |> Num.bitwiseAnd 255 |> Num.toU8 + loop (Num.shiftRightBy n 8) (List.append bytes newByte) (place + 1) + loop number [] 0 + +digest256ToBytes : Digest256 -> List U8 +digest256ToBytes \ digest256 -> + List.append (u128Bytes digest256.firstHalf) (u128Bytes digest256.secondHalf) From a37dc00942cffc2005ceedff305ad271ae728ad3 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Wed, 4 Sep 2024 22:11:25 +0100 Subject: [PATCH 17/57] add missing module import --- crates/compiler/builtins/roc/Crypt.roc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index 91476be8ca5..6e9c729c579 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -8,9 +8,10 @@ module [ Digest256, ] +import Bool exposing [Eq] import List -import Num exposing [U8, U64, U128, Int] -import Num +import Num exposing [U8, U64, U128] + Sha256 := { location : U64, @@ -19,7 +20,7 @@ Sha256 := { Digest256 := { firstHalf : U128, secondHalf : U128, -} implements[Eq, Hash] +} implements[Eq] emptySha256 : {} -> Sha256 @@ -37,7 +38,7 @@ u128Bytes = \ number -> if place == 16 then bytes else newByte = n |> Num.bitwiseAnd 255 |> Num.toU8 - loop (Num.shiftRightBy n 8) (List.append bytes newByte) (place + 1) + loop (Num.shiftRightBy n 8) (List.prepend bytes newByte) (place + 1) loop number [] 0 digest256ToBytes : Digest256 -> List U8 From ebbc4409d1fb88e3ba38311e93de1c0101d78f55 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 9 Sep 2024 22:03:28 +0100 Subject: [PATCH 18/57] Fixes structured binding in digest256ToBytes --- crates/compiler/builtins/roc/Crypt.roc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index 6e9c729c579..fdf68ef6b55 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -3,7 +3,7 @@ module [ addBytes, digest, hashSha256, - digest256ToBytes, + digest256ToBytes, Sha256, Digest256, ] @@ -33,7 +33,7 @@ hashSha256 = \bytes -> digest (addBytes (emptySha256 {}) bytes) u128Bytes : U128 -> List U8 -u128Bytes = \ number -> +u128Bytes = \number -> loop = \ n, bytes, place -> if place == 16 then bytes else @@ -41,6 +41,9 @@ u128Bytes = \ number -> loop (Num.shiftRightBy n 8) (List.prepend bytes newByte) (place + 1) loop number [] 0 + + digest256ToBytes : Digest256 -> List U8 -digest256ToBytes \ digest256 -> - List.append (u128Bytes digest256.firstHalf) (u128Bytes digest256.secondHalf) +digest256ToBytes = \@Digest256{firstHalf, secondHalf} -> + List.concat (u128Bytes firstHalf) (u128Bytes secondHalf) + From eae0c5eceef2452bfbce91ce2093085852b73192 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 10 Sep 2024 18:00:49 +0100 Subject: [PATCH 19/57] Name changes --- crates/compiler/builtins/bitcode/src/crypt.zig | 4 ++-- crates/compiler/builtins/bitcode/src/main.zig | 4 ++-- crates/compiler/builtins/roc/Crypt.roc | 15 ++++++--------- crates/compiler/builtins/src/bitcode.rs | 4 ++-- crates/compiler/can/src/builtins.rs | 4 ++-- crates/compiler/gen_dev/src/lib.rs | 8 ++++---- crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 8 ++++---- crates/compiler/gen_wasm/src/low_level.rs | 4 ++-- crates/compiler/module/src/low_level.rs | 8 ++++---- crates/compiler/module/src/symbol.rs | 4 ++-- crates/compiler/mono/src/drop_specialization.rs | 2 +- crates/compiler/mono/src/inc_dec.rs | 4 ++-- 12 files changed, 33 insertions(+), 36 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 10e7484b88f..93cba4d2cdc 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -20,7 +20,7 @@ pub fn emptySha256() callconv(.C) Sha256 { }; } -pub fn addBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { +pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { var out = emptySha256(); out.pointer().* = sha.pointer().*; if (data.bytes) |bytes| { @@ -35,6 +35,6 @@ pub const Digest256 = extern struct { secondHalf: u128, }; -pub fn digest(sha: Sha256) callconv(.C) Digest256 { +pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { return @bitCast(sha.pointer().*.peek()); } diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index 04a02f7ab97..42800503d3e 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -14,8 +14,8 @@ const STR = "str"; const crypt = @import("crypt.zig"); comptime { exportCryptFn(crypt.emptySha256, "emptySha256"); - exportCryptFn(crypt.addBytes, "addBytes"); - exportCryptFn(crypt.digest, "digest"); + exportCryptFn(crypt.sha256AddBytes, "sha256AddBytes"); + exportCryptFn(crypt.sha256Digest, "sha256Digest"); } // Dec Module diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index fdf68ef6b55..588f2bd2bcd 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -1,9 +1,9 @@ module [ emptySha256, - addBytes, - digest, + sha256AddBytes, + sha256Digest, hashSha256, - digest256ToBytes, + digest256ToBytes, Sha256, Digest256, ] @@ -24,13 +24,13 @@ Digest256 := { emptySha256 : {} -> Sha256 -addBytes : Sha256, List U8 -> Sha256 +sha256AddBytes : Sha256, List U8 -> Sha256 -digest : Sha256 -> Digest256 +sha256Digest : Sha256 -> Digest256 hashSha256 : List U8 -> Digest256 hashSha256 = \bytes -> - digest (addBytes (emptySha256 {}) bytes) + sha256Digest (sha256AddBytes (emptySha256 {}) bytes) u128Bytes : U128 -> List U8 u128Bytes = \number -> @@ -41,9 +41,6 @@ u128Bytes = \number -> loop (Num.shiftRightBy n 8) (List.prepend bytes newByte) (place + 1) loop number [] 0 - - digest256ToBytes : Digest256 -> List U8 digest256ToBytes = \@Digest256{firstHalf, secondHalf} -> List.concat (u128Bytes firstHalf) (u128Bytes secondHalf) - diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index 5dcd6634461..9a1b3f33e85 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -418,8 +418,8 @@ pub const DEC_FLOOR: IntrinsicName = int_intrinsic!("roc_builtins.dec.floor"); pub const DEC_CEILING: IntrinsicName = int_intrinsic!("roc_builtins.dec.ceiling"); pub const CRYPT_EMPTY_SHA256: &str = "roc_builtins.crypt.emptySha256"; -pub const CRYPT_ADD_BYTES: &str = "roc_builtins.crypt.addBytes"; -pub const CRYPT_DIGEST: &str = "roc_builtins.crypt.digest"; +pub const CRYPT_SHA256_ADD_BYTES: &str = "roc_builtins.crypt.sha256AddBytes"; +pub const CRYPT_SHA256_DIGEST: &str = "roc_builtins.crypt.sha156Digest"; pub const UTILS_DBG_IMPL: &str = "roc_builtins.utils.dbg_impl"; pub const UTILS_TEST_PANIC: &str = "roc_builtins.utils.test_panic"; diff --git a/crates/compiler/can/src/builtins.rs b/crates/compiler/can/src/builtins.rs index 5b3e570ec52..edfbeff9579 100644 --- a/crates/compiler/can/src/builtins.rs +++ b/crates/compiler/can/src/builtins.rs @@ -211,8 +211,8 @@ map_symbol_to_lowlevel_and_arity! { NumF64FromParts; NUM_F64_FROM_PARTS; 1, CryptEmptySha256; CRYPT_EMPTY_SHA_256; 1, - CryptAddBytes; CRYPT_ADD_BYTES; 2, - CryptDigest; CRYPT_DIGEST; 1, + CryptSha256AddBytes; CRYPT_SHA256_ADD_BYTES; 2, + CryptSha256Digest; CRYPT_SHA256_DIGEST; 1, Eq; BOOL_STRUCTURAL_EQ; 2, NotEq; BOOL_STRUCTURAL_NOT_EQ; 2, diff --git a/crates/compiler/gen_dev/src/lib.rs b/crates/compiler/gen_dev/src/lib.rs index e21bd10686e..9eaa9813851 100644 --- a/crates/compiler/gen_dev/src/lib.rs +++ b/crates/compiler/gen_dev/src/lib.rs @@ -2221,13 +2221,13 @@ trait Backend<'a> { self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } - LowLevel::CryptAddBytes => { - let intrinsic = bitcode::CRYPT_ADD_BYTES.to_string(); + LowLevel::CryptSha256AddBytes => { + let intrinsic = bitcode::CRYPT_SHA256_ADD_BYTES.to_string(); self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } - LowLevel::CryptDigest => { - let intrinsic = bitcode::CRYPT_DIGEST.to_string(); + LowLevel::CryptSha256Digest => { + let intrinsic = bitcode::CRYPT_SHA256_DIGEST.to_string(); self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index bcb57b4842e..6e9c4f76b4d 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1432,13 +1432,13 @@ pub(crate) fn run_low_level<'a, 'ctx>( call_bitcode_fn(env, &[], bitcode::UTILS_DICT_PSEUDO_SEED) } CryptEmptySha256 => call_bitcode_fn(env, &[], bitcode::CRYPT_EMPTY_SHA256), - CryptAddBytes => { + CryptSha256AddBytes => { arguments!(sha, data); - call_bitcode_fn(env, &[sha, data], bitcode::CRYPT_ADD_BYTES) + call_bitcode_fn(env, &[sha, data], bitcode::CRYPT_SHA256_ADD_BYTES) } - CryptDigest => { + CryptSha256Digest => { arguments!(sha); - call_bitcode_fn(env, &[sha], bitcode::CRYPT_DIGEST) + call_bitcode_fn(env, &[sha], bitcode::CRYPT_SHA256_DIGEST) } ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index 6320c2ea660..4ec6e3433da 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2165,8 +2165,8 @@ impl<'a> LowLevelCall<'a> { NumF64FromParts => self.load_args_and_call_zig(backend, bitcode::NUM_F64_FROM_PARTS), // Crypt CryptEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPT_EMPTY_SHA256), - CryptAddBytes => self.load_args_and_call_zig(backend, bitcode::CRYPT_ADD_BYTES), - CryptDigest => self.load_args_and_call_zig(backend, bitcode::CRYPT_DIGEST), + CryptSha256AddBytes => self.load_args_and_call_zig(backend, bitcode::CRYPT_SHA256_ADD_BYTES), + CryptSha256Digest => self.load_args_and_call_zig(backend, bitcode::CRYPT_SHA256_DIGEST), And => { self.load_args(backend); diff --git a/crates/compiler/module/src/low_level.rs b/crates/compiler/module/src/low_level.rs index cbec1b42735..2fdadc78a4d 100644 --- a/crates/compiler/module/src/low_level.rs +++ b/crates/compiler/module/src/low_level.rs @@ -130,8 +130,8 @@ pub enum LowLevel { LongJmp, SetLongJmpBuffer, CryptEmptySha256, - CryptAddBytes, - CryptDigest, + CryptSha256AddBytes, + CryptSha256Digest, } macro_rules! higher_order { @@ -352,6 +352,6 @@ map_symbol_to_lowlevel! { Unreachable <= LIST_UNREACHABLE; DictPseudoSeed <= DICT_PSEUDO_SEED; CryptEmptySha256 <= CRYPT_EMPTY_SHA_256; - CryptAddBytes <= CRYPT_ADD_BYTES; - CryptDigest <= CRYPT_DIGEST; + CryptSha256AddBytes <= CRYPT_SHA256_ADD_BYTES; + CryptSha256Digest <= CRYPT_SHA256_DIGEST; } diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index e1b05100dcf..acbdc77c23e 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1737,8 +1737,8 @@ define_builtins! { 0 CRYPT_SHA_256: "Sha256" exposed_type=true 1 CRYPT_DIGEST_256: "Digest256" exposed_type=true 2 CRYPT_EMPTY_SHA_256: "emptySha256" - 3 CRYPT_ADD_BYTES: "addBytes" - 4 CRYPT_DIGEST: "digest" + 3 CRYPT_SHA256_ADD_BYTES: "sha256AddBytes" + 4 CRYPT_SHA256_DIGEST: "sha256Digest" 5 CRYPT_HASH_SHA_256: "hashSha256" } diff --git a/crates/compiler/mono/src/drop_specialization.rs b/crates/compiler/mono/src/drop_specialization.rs index 9d2f98d6a9a..e9821fbef4d 100644 --- a/crates/compiler/mono/src/drop_specialization.rs +++ b/crates/compiler/mono/src/drop_specialization.rs @@ -1609,7 +1609,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC { ListIsUnique => RC::Rc, ListClone => RC::Rc, - CryptEmptySha256 | CryptAddBytes | CryptDigest => RC::NoRc, + CryptEmptySha256 | CryptSha256AddBytes | CryptSha256Digest => RC::NoRc, BoxExpr | UnboxExpr => { unreachable!("These lowlevel operations are turned into mono Expr's") diff --git a/crates/compiler/mono/src/inc_dec.rs b/crates/compiler/mono/src/inc_dec.rs index 87b133b046f..87d663c7400 100644 --- a/crates/compiler/mono/src/inc_dec.rs +++ b/crates/compiler/mono/src/inc_dec.rs @@ -1257,8 +1257,8 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] { ListReleaseExcessCapacity => &[OWNED], StrReleaseExcessCapacity => &[OWNED], CryptEmptySha256 => &[IRRELEVANT], - CryptAddBytes => &[OWNED, BORROWED], - CryptDigest => &[BORROWED], + CryptSha256AddBytes => &[OWNED, BORROWED], + CryptSha256Digest => &[BORROWED], ListIncref => &[OWNED], ListDecref => &[OWNED], From 61db15370857519a5af6de6e989fd3a921905a07 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 10 Sep 2024 18:20:40 +0100 Subject: [PATCH 20/57] Tidy formatting --- crates/compiler/builtins/roc/Crypt.roc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index 588f2bd2bcd..f429197166d 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -30,14 +30,18 @@ sha256Digest : Sha256 -> Digest256 hashSha256 : List U8 -> Digest256 hashSha256 = \bytes -> - sha256Digest (sha256AddBytes (emptySha256 {}) bytes) + {} |> emptySha256 + |> sha256AddBytes bytes + |> sha256Digest u128Bytes : U128 -> List U8 u128Bytes = \number -> loop = \ n, bytes, place -> if place == 16 then bytes else - newByte = n |> Num.bitwiseAnd 255 |> Num.toU8 + newByte = n + |> Num.bitwiseAnd 255 + |> Num.toU8 loop (Num.shiftRightBy n 8) (List.prepend bytes newByte) (place + 1) loop number [] 0 From 302d5c3fa015b784ae71e65b4faee1e76743dcf8 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 10 Sep 2024 21:41:35 +0100 Subject: [PATCH 21/57] Attempt to fix formatting --- crates/compiler/builtins/roc/Crypt.roc | 23 ++++++----------------- crates/compiler/gen_wasm/src/low_level.rs | 4 +++- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index f429197166d..679fb418c50 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -12,15 +12,9 @@ import Bool exposing [Eq] import List import Num exposing [U8, U64, U128] +Sha256 := { location : U64, } -Sha256 := { - location : U64, -} - -Digest256 := { - firstHalf : U128, - secondHalf : U128, -} implements[Eq] +Digest256 := { firstHalf : U128, secondHalf : U128, } implements [Eq] emptySha256 : {} -> Sha256 @@ -29,22 +23,17 @@ sha256AddBytes : Sha256, List U8 -> Sha256 sha256Digest : Sha256 -> Digest256 hashSha256 : List U8 -> Digest256 -hashSha256 = \bytes -> - {} |> emptySha256 - |> sha256AddBytes bytes - |> sha256Digest +hashSha256 = \bytes -> {} |> emptySha256 |> sha256AddBytes bytes |> sha256Digest u128Bytes : U128 -> List U8 u128Bytes = \number -> - loop = \ n, bytes, place -> + loop = \n, bytes, place -> if place == 16 then bytes else - newByte = n - |> Num.bitwiseAnd 255 - |> Num.toU8 + newByte = n |> Num.bitwiseAnd 255 |> Num.toU8 loop (Num.shiftRightBy n 8) (List.prepend bytes newByte) (place + 1) loop number [] 0 digest256ToBytes : Digest256 -> List U8 -digest256ToBytes = \@Digest256{firstHalf, secondHalf} -> +digest256ToBytes = \@Digest256{ firstHalf, secondHalf } -> List.concat (u128Bytes firstHalf) (u128Bytes secondHalf) diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index 4ec6e3433da..4770562255c 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2165,7 +2165,9 @@ impl<'a> LowLevelCall<'a> { NumF64FromParts => self.load_args_and_call_zig(backend, bitcode::NUM_F64_FROM_PARTS), // Crypt CryptEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPT_EMPTY_SHA256), - CryptSha256AddBytes => self.load_args_and_call_zig(backend, bitcode::CRYPT_SHA256_ADD_BYTES), + CryptSha256AddBytes => { + self.load_args_and_call_zig(backend, bitcode::CRYPT_SHA256_ADD_BYTES) + } CryptSha256Digest => self.load_args_and_call_zig(backend, bitcode::CRYPT_SHA256_DIGEST), And => { From 43bfd8a912db6098315f26e367edbbc6a4ec4ac8 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 10 Sep 2024 21:54:27 +0100 Subject: [PATCH 22/57] Ran fomatter --- crates/compiler/builtins/roc/Crypt.roc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index 679fb418c50..d7adb9990fd 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -12,9 +12,9 @@ import Bool exposing [Eq] import List import Num exposing [U8, U64, U128] -Sha256 := { location : U64, } +Sha256 := { location : U64 } -Digest256 := { firstHalf : U128, secondHalf : U128, } implements [Eq] +Digest256 := { firstHalf : U128, secondHalf : U128 } implements [Eq] emptySha256 : {} -> Sha256 @@ -24,16 +24,17 @@ sha256Digest : Sha256 -> Digest256 hashSha256 : List U8 -> Digest256 hashSha256 = \bytes -> {} |> emptySha256 |> sha256AddBytes bytes |> sha256Digest - -u128Bytes : U128 -> List U8 + +u128Bytes : U128 -> List U8 u128Bytes = \number -> loop = \n, bytes, place -> - if place == 16 then bytes - else + if place == 16 then + bytes + else newByte = n |> Num.bitwiseAnd 255 |> Num.toU8 loop (Num.shiftRightBy n 8) (List.prepend bytes newByte) (place + 1) loop number [] 0 digest256ToBytes : Digest256 -> List U8 -digest256ToBytes = \@Digest256{ firstHalf, secondHalf } -> +digest256ToBytes = \@Digest256 { firstHalf, secondHalf } -> List.concat (u128Bytes firstHalf) (u128Bytes secondHalf) From 34fb4cb9323091bf9ecadb88806191608dd48137 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Thu, 12 Sep 2024 19:53:24 +0100 Subject: [PATCH 23/57] tests --- .../compiler/builtins/bitcode/src/crypt.zig | 1 + crates/compiler/builtins/roc/Crypt.roc | 77 ++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 93cba4d2cdc..086faf4007b 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -38,3 +38,4 @@ pub const Digest256 = extern struct { pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { return @bitCast(sha.pointer().*.peek()); } + \ No newline at end of file diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index d7adb9990fd..f96857ab9fb 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -11,6 +11,8 @@ module [ import Bool exposing [Eq] import List import Num exposing [U8, U64, U128] +import Result +import Str Sha256 := { location : U64 } @@ -23,7 +25,7 @@ sha256AddBytes : Sha256, List U8 -> Sha256 sha256Digest : Sha256 -> Digest256 hashSha256 : List U8 -> Digest256 -hashSha256 = \bytes -> {} |> emptySha256 |> sha256AddBytes bytes |> sha256Digest +hashSha256 = \bytes -> emptySha256 {} |> sha256AddBytes bytes |> sha256Digest u128Bytes : U128 -> List U8 u128Bytes = \number -> @@ -38,3 +40,76 @@ u128Bytes = \number -> digest256ToBytes : Digest256 -> List U8 digest256ToBytes = \@Digest256 { firstHalf, secondHalf } -> List.concat (u128Bytes firstHalf) (u128Bytes secondHalf) + +# test data taken from https://ziglang.org/documentation/0.11.0/std/src/std/crypto/sha2.zig.html#L434 +digestBytesOfEmpty : List U8 +digestBytesOfEmpty = fromHexString "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + +digestBytesOfAbc : List U8 +digestBytesOfAbc = fromHexString "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + +digestBytesOfLong : List U8 +digestBytesOfLong = fromHexString "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1" + +expect + data : List U8 + data = [] + want = digestBytesOfEmpty + got = data |> hashSha256 |> digest256ToBytes + want == got + +expect + data = ['a', 'b', 'c'] + want = digestBytesOfAbc + got = data |> hashSha256 |> digest256ToBytes + want == got + +expect + data = Str.toUtf8 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" + want = digestBytesOfLong + got = data |> hashSha256 |> digest256ToBytes + want == got + +expect + want = digestBytesOfEmpty + got = emptySha256 {} |> sha256Digest |> digest256ToBytes + want == got + +expect + data = Str.toUtf8 "abc" + want = digestBytesOfAbc + got = + emptySha256 {} + |> sha256AddBytes data + |> sha256Digest + |> digest256ToBytes + want == got + +expect + want = digestBytesOfAbc + got = + emptySha256 {} + |> sha256AddBytes ['a'] + |> sha256AddBytes ['b'] + |> sha256AddBytes ['c'] + |> sha256Digest + |> digest256ToBytes + want == got + +fromHexString : Str -> List U8 +fromHexString = \hex -> + fromHexDigit = \smallNumber -> + if smallNumber <= '9' then + smallNumber - '0' + else + smallNumber - 'a' + + fromHexDigits = \pair -> + first = pair |> List.first |> Result.withDefault 0 + second = pair |> List.get 1 |> Result.withDefault 0 + 16 * (fromHexDigit first) + (fromHexDigit second) + + hex + |> Str.toUtf8 + |> List.chunksOf 2 + |> List.map fromHexDigits From 75f3420cf15c2b059a1a8597a2a15700d2d4e477 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Thu, 12 Sep 2024 21:26:19 +0100 Subject: [PATCH 24/57] Docs for exposed functions and types --- crates/compiler/builtins/roc/Crypt.roc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index f96857ab9fb..10b24348cfb 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -14,16 +14,23 @@ import Num exposing [U8, U64, U128] import Result import Str +## Represents, as an opaque type, the state of a SHA256 cryptographic hashing function, after some (or no) data have been added to the hash. Sha256 := { location : U64 } +## Represents the digest of soem data produced by the SHA256 cryptographic hashing function as an opaque type. +## `Digest256`implements the `Eq` ability. Digest256 := { firstHalf : U128, secondHalf : U128 } implements [Eq] +## Returns a `Sha256` to which no data have been added. emptySha256 : {} -> Sha256 +## Adds bytes of data to be hashed in the `Sha256`. sha256AddBytes : Sha256, List U8 -> Sha256 +## Returns the digest of the cryptographic hashing function represted by a`Sha256`. sha256Digest : Sha256 -> Digest256 +## Applies the SHA256 crytographic hashing function to some bytes. hashSha256 : List U8 -> Digest256 hashSha256 = \bytes -> emptySha256 {} |> sha256AddBytes bytes |> sha256Digest @@ -37,6 +44,7 @@ u128Bytes = \number -> loop (Num.shiftRightBy n 8) (List.prepend bytes newByte) (place + 1) loop number [] 0 +## Returns the bytes of a `Digest256`as a list. digest256ToBytes : Digest256 -> List U8 digest256ToBytes = \@Digest256 { firstHalf, secondHalf } -> List.concat (u128Bytes firstHalf) (u128Bytes secondHalf) From 32fa02bebe54dd351ae174042c27c051a9efdc0e Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Thu, 12 Sep 2024 22:06:51 +0100 Subject: [PATCH 25/57] Fix spelling and zig fmt --- crates/compiler/builtins/bitcode/src/crypt.zig | 1 - crates/compiler/builtins/roc/Crypt.roc | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypt.zig index 086faf4007b..93cba4d2cdc 100644 --- a/crates/compiler/builtins/bitcode/src/crypt.zig +++ b/crates/compiler/builtins/bitcode/src/crypt.zig @@ -38,4 +38,3 @@ pub const Digest256 = extern struct { pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { return @bitCast(sha.pointer().*.peek()); } - \ No newline at end of file diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index 10b24348cfb..3a0a1ed445f 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -17,7 +17,7 @@ import Str ## Represents, as an opaque type, the state of a SHA256 cryptographic hashing function, after some (or no) data have been added to the hash. Sha256 := { location : U64 } -## Represents the digest of soem data produced by the SHA256 cryptographic hashing function as an opaque type. +## Represents the digest of some data produced by the SHA256 cryptographic hashing function as an opaque type. ## `Digest256`implements the `Eq` ability. Digest256 := { firstHalf : U128, secondHalf : U128 } implements [Eq] From 2acbbd280a583ee2485a166525ca9c353154dc81 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Fri, 13 Sep 2024 12:48:11 +0100 Subject: [PATCH 26/57] Response to review on comment --- crates/compiler/builtins/roc/Crypt.roc | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index 3a0a1ed445f..d4d1699c0bf 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -14,23 +14,24 @@ import Num exposing [U8, U64, U128] import Result import Str -## Represents, as an opaque type, the state of a SHA256 cryptographic hashing function, after some (or no) data have been added to the hash. +## Represents the state of a SHA-256 cryptographic hashing function, after some (or no) data has been added to the hash. Sha256 := { location : U64 } -## Represents the digest of some data produced by the SHA256 cryptographic hashing function as an opaque type. -## `Digest256`implements the `Eq` ability. +## Represents the digest of some data produced by the SHA-256 cryptographic hashing function as an opaque type. + +## `Digest256` implements the `Eq` ability. Digest256 := { firstHalf : U128, secondHalf : U128 } implements [Eq] -## Returns a `Sha256` to which no data have been added. +## Returns an empty SHA-256 hasher. emptySha256 : {} -> Sha256 -## Adds bytes of data to be hashed in the `Sha256`. +## Adds bytes of data to be hashed by a SHA-256 hasher.. sha256AddBytes : Sha256, List U8 -> Sha256 -## Returns the digest of the cryptographic hashing function represted by a`Sha256`. +## Returns the digest of the cryptographic hashing function represented by a SHA-256 hasher.. sha256Digest : Sha256 -> Digest256 -## Applies the SHA256 crytographic hashing function to some bytes. +## Applies the SHA-256 crytographic hashing function to some bytes. hashSha256 : List U8 -> Digest256 hashSha256 = \bytes -> emptySha256 {} |> sha256AddBytes bytes |> sha256Digest @@ -44,7 +45,7 @@ u128Bytes = \number -> loop (Num.shiftRightBy n 8) (List.prepend bytes newByte) (place + 1) loop number [] 0 -## Returns the bytes of a `Digest256`as a list. +## Returns the bytes of a SHA-256 digest as a list. digest256ToBytes : Digest256 -> List U8 digest256ToBytes = \@Digest256 { firstHalf, secondHalf } -> List.concat (u128Bytes firstHalf) (u128Bytes secondHalf) @@ -84,7 +85,7 @@ expect want == got expect - data = Str.toUtf8 "abc" + data = ['a', 'b', 'c'] want = digestBytesOfAbc got = emptySha256 {} From 5fe4d61ff076847d8933d34140dffa0c9e5ced1a Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Fri, 13 Sep 2024 23:11:08 +0100 Subject: [PATCH 27/57] Fixes typo in test --- crates/compiler/builtins/roc/Crypt.roc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypt.roc index d4d1699c0bf..5f2a0557297 100644 --- a/crates/compiler/builtins/roc/Crypt.roc +++ b/crates/compiler/builtins/roc/Crypt.roc @@ -111,7 +111,7 @@ fromHexString = \hex -> if smallNumber <= '9' then smallNumber - '0' else - smallNumber - 'a' + smallNumber - 'a' + 10 fromHexDigits = \pair -> first = pair |> List.first |> Result.withDefault 0 From 638d2141b11162501a04069d4202fc73465db0c2 Mon Sep 17 00:00:00 2001 From: Sam Mohr Date: Mon, 7 Oct 2024 21:44:51 -0700 Subject: [PATCH 28/57] update mono tests --- crates/compiler/load/tests/test_reporting.rs | 4 ++-- .../compiler/load_internal/tests/test_load.rs | 4 ++-- .../test_mono/generated/issue_4557.txt | 2 +- .../test_mono/generated/issue_4770.txt | 2 +- ..._4772_weakened_monomorphic_destructure.txt | 2 +- ...ist_map_take_capturing_or_noncapturing.txt | 2 +- .../generated/monomorphized_list.txt | 2 +- ...e_wrapped_with_nullable_not_last_index.txt | 2 +- .../test_mono/generated/pattern_as_nested.txt | 2 +- .../generated/pattern_as_toplevel.txt | 2 +- .../polymorphic_expression_unification.txt | 13 ++++------ ..._set_resolved_only_upon_specialization.txt | 8 +++---- .../generated/specialize_after_match.txt | 24 +++++++++---------- 13 files changed, 33 insertions(+), 36 deletions(-) diff --git a/crates/compiler/load/tests/test_reporting.rs b/crates/compiler/load/tests/test_reporting.rs index 2ae8c985fd5..ce85d671f7e 100644 --- a/crates/compiler/load/tests/test_reporting.rs +++ b/crates/compiler/load/tests/test_reporting.rs @@ -658,9 +658,9 @@ mod test_reporting { Did you mean one of these? - Str Frac Num + Str U8 "### ); @@ -8160,7 +8160,7 @@ All branches in an `if` must have the same type! Encode Inspect Dict - List + Crypt " ); diff --git a/crates/compiler/load_internal/tests/test_load.rs b/crates/compiler/load_internal/tests/test_load.rs index b95d9319901..3aec18b2bb4 100644 --- a/crates/compiler/load_internal/tests/test_load.rs +++ b/crates/compiler/load_internal/tests/test_load.rs @@ -1934,9 +1934,9 @@ fn issue_2863_module_type_does_not_exist() { Did you mean one of these? Decoding - Result Dict - DecodeError + Result + Digest256 " ) ) diff --git a/crates/compiler/test_mono/generated/issue_4557.txt b/crates/compiler/test_mono/generated/issue_4557.txt index 45ace9c5326..ac27c966f73 100644 --- a/crates/compiler/test_mono/generated/issue_4557.txt +++ b/crates/compiler/test_mono/generated/issue_4557.txt @@ -16,8 +16,8 @@ procedure Test.1 (Test.2, Test.3): let Test.23 : {} = Struct {}; joinpoint Test.24 Test.22: let Test.20 : Int1 = CallByName Bool.11 Test.21 Test.22; - dec Test.21; dec Test.22; + dec Test.21; let Test.18 : Int1 = CallByName Bool.4 Test.19 Test.20; ret Test.18; in diff --git a/crates/compiler/test_mono/generated/issue_4770.txt b/crates/compiler/test_mono/generated/issue_4770.txt index 272442c9dbe..1966dbe7eb2 100644 --- a/crates/compiler/test_mono/generated/issue_4770.txt +++ b/crates/compiler/test_mono/generated/issue_4770.txt @@ -199,8 +199,8 @@ procedure Test.1 (#Derived_gen.5): let Test.30 : Int1 = CallByName Num.22 Test.31 Test.32; ret Test.30; else - dec Test.12; dec Test.14; + dec Test.12; let Test.28 : Int1 = CallByName Bool.1; ret Test.28; in diff --git a/crates/compiler/test_mono/generated/issue_4772_weakened_monomorphic_destructure.txt b/crates/compiler/test_mono/generated/issue_4772_weakened_monomorphic_destructure.txt index 12dc47df8fa..68b16ce5806 100644 --- a/crates/compiler/test_mono/generated/issue_4772_weakened_monomorphic_destructure.txt +++ b/crates/compiler/test_mono/generated/issue_4772_weakened_monomorphic_destructure.txt @@ -91,8 +91,8 @@ procedure Test.19 (): let Test.120 : [C Str, C {List U8, I64}] = TagId(0) Test.122; ret Test.120; else - dec Test.93; dec Test.92; + dec Test.93; let Test.128 : Str = "not a number"; let Test.126 : [C Str, C {List U8, I64}] = TagId(0) Test.128; ret Test.126; diff --git a/crates/compiler/test_mono/generated/list_map_take_capturing_or_noncapturing.txt b/crates/compiler/test_mono/generated/list_map_take_capturing_or_noncapturing.txt index 982d5a13551..fa758eb311a 100644 --- a/crates/compiler/test_mono/generated/list_map_take_capturing_or_noncapturing.txt +++ b/crates/compiler/test_mono/generated/list_map_take_capturing_or_noncapturing.txt @@ -108,8 +108,8 @@ procedure Test.0 (): else let Test.22 : Str = "B"; let Test.23 : Int1 = lowlevel Eq Test.22 Test.12; - dec Test.12; dec Test.22; + dec Test.12; if Test.23 then let Test.17 : [C U8, C U8, C ] = TagId(1) Test.2; jump Test.13 Test.17; diff --git a/crates/compiler/test_mono/generated/monomorphized_list.txt b/crates/compiler/test_mono/generated/monomorphized_list.txt index a48816e97e3..40879f418d4 100644 --- a/crates/compiler/test_mono/generated/monomorphized_list.txt +++ b/crates/compiler/test_mono/generated/monomorphized_list.txt @@ -16,6 +16,6 @@ procedure Test.0 (): let Test.10 : {} = Struct {}; let Test.8 : List U16 = CallByName Test.1 Test.10; let Test.6 : U64 = CallByName Test.2 Test.7 Test.8; - dec Test.8; dec Test.7; + dec Test.8; ret Test.6; diff --git a/crates/compiler/test_mono/generated/nullable_wrapped_with_nullable_not_last_index.txt b/crates/compiler/test_mono/generated/nullable_wrapped_with_nullable_not_last_index.txt index 100ee770493..d5d7c89fa82 100644 --- a/crates/compiler/test_mono/generated/nullable_wrapped_with_nullable_not_last_index.txt +++ b/crates/compiler/test_mono/generated/nullable_wrapped_with_nullable_not_last_index.txt @@ -24,6 +24,6 @@ procedure Test.0 (): let Test.8 : Str = CallByName Test.2 Test.10; let Test.9 : Str = "c"; let Test.7 : Int1 = CallByName Bool.11 Test.8 Test.9; - dec Test.8; dec Test.9; + dec Test.8; ret Test.7; diff --git a/crates/compiler/test_mono/generated/pattern_as_nested.txt b/crates/compiler/test_mono/generated/pattern_as_nested.txt index 994f2de2f1d..72a98e1ea49 100644 --- a/crates/compiler/test_mono/generated/pattern_as_nested.txt +++ b/crates/compiler/test_mono/generated/pattern_as_nested.txt @@ -24,8 +24,8 @@ procedure Test.0 (): let Test.13 : {I64, Str} = StructAtIndex 0 Test.5; let Test.7 : {I64, Str} = CallByName Test.1; let Test.6 : Int1 = CallByName Bool.11 Test.7 Test.13; - dec Test.13; dec Test.7; + dec Test.13; ret Test.6; else dec Test.14; diff --git a/crates/compiler/test_mono/generated/pattern_as_toplevel.txt b/crates/compiler/test_mono/generated/pattern_as_toplevel.txt index a124668cf09..4a70501f514 100644 --- a/crates/compiler/test_mono/generated/pattern_as_toplevel.txt +++ b/crates/compiler/test_mono/generated/pattern_as_toplevel.txt @@ -20,9 +20,9 @@ procedure Test.0 (): if Test.13 then let Test.6 : {I64, Str} = CallByName Test.1; let Test.5 : Int1 = CallByName Bool.11 Test.6 Test.4; + dec Test.6; let #Derived_gen.0 : Str = StructAtIndex 1 Test.4; dec #Derived_gen.0; - dec Test.6; ret Test.5; else let #Derived_gen.1 : Str = StructAtIndex 1 Test.4; diff --git a/crates/compiler/test_mono/generated/polymorphic_expression_unification.txt b/crates/compiler/test_mono/generated/polymorphic_expression_unification.txt index 9a8ece21892..1e5d7515869 100644 --- a/crates/compiler/test_mono/generated/polymorphic_expression_unification.txt +++ b/crates/compiler/test_mono/generated/polymorphic_expression_unification.txt @@ -30,15 +30,12 @@ procedure Test.0 (): let Test.16 : Str = ""; let Test.15 : [C List *self, C Str] = TagId(1) Test.16; let Test.13 : Int1 = CallByName Bool.11 Test.14 Test.15; - joinpoint #Derived_gen.0: - dec Test.14; - ret Test.13; - in - let #Derived_gen.1 : Int1 = lowlevel RefCountIsUnique Test.15; - if #Derived_gen.1 then + dec Test.14; + let #Derived_gen.0 : Int1 = lowlevel RefCountIsUnique Test.15; + if #Derived_gen.0 then dec Test.16; free Test.15; - jump #Derived_gen.0; + ret Test.13; else decref Test.15; - jump #Derived_gen.0; + ret Test.13; diff --git a/crates/compiler/test_mono/generated/recursive_lambda_set_resolved_only_upon_specialization.txt b/crates/compiler/test_mono/generated/recursive_lambda_set_resolved_only_upon_specialization.txt index 06f48f983f6..f679a2e5d04 100644 --- a/crates/compiler/test_mono/generated/recursive_lambda_set_resolved_only_upon_specialization.txt +++ b/crates/compiler/test_mono/generated/recursive_lambda_set_resolved_only_upon_specialization.txt @@ -10,7 +10,7 @@ procedure Num.21 (#Attr.2, #Attr.3): let Num.281 : U8 = lowlevel NumMul #Attr.2 #Attr.3; ret Num.281; -procedure Test.1 (#Derived_gen.2, #Derived_gen.3): +procedure Test.1 (#Derived_gen.0, #Derived_gen.1): joinpoint Test.11 Test.2 Test.3: let Test.26 : U8 = 0i64; let Test.22 : Int1 = CallByName Bool.11 Test.2 Test.26; @@ -33,9 +33,9 @@ procedure Test.1 (#Derived_gen.2, #Derived_gen.3): let Test.14 : [, C *self U8] = TagId(0) Test.3 Test.2; jump Test.11 Test.13 Test.14; in - jump Test.11 #Derived_gen.2 #Derived_gen.3; + jump Test.11 #Derived_gen.0 #Derived_gen.1; -procedure Test.4 (#Derived_gen.0, #Derived_gen.1): +procedure Test.4 (#Derived_gen.2, #Derived_gen.3): joinpoint Test.15 Test.5 #Attr.12: let Test.20 : U8 = UnionAtIndex (Id 0) (Index 1) #Attr.12; let Test.19 : [, C *self U8] = UnionAtIndex (Id 0) (Index 0) #Attr.12; @@ -61,7 +61,7 @@ procedure Test.4 (#Derived_gen.0, #Derived_gen.1): decref #Attr.12; jump #Derived_gen.4; in - jump Test.15 #Derived_gen.0 #Derived_gen.1; + jump Test.15 #Derived_gen.2 #Derived_gen.3; procedure Test.6 (Test.7): ret Test.7; diff --git a/crates/compiler/test_mono/generated/specialize_after_match.txt b/crates/compiler/test_mono/generated/specialize_after_match.txt index 081cbdc8406..cb95b4a71a7 100644 --- a/crates/compiler/test_mono/generated/specialize_after_match.txt +++ b/crates/compiler/test_mono/generated/specialize_after_match.txt @@ -23,7 +23,7 @@ procedure Test.2 (Test.9, Test.10): let Test.29 : U64 = CallByName Test.3 Test.9; ret Test.29; else - joinpoint #Derived_gen.1: + joinpoint #Derived_gen.4: let Test.13 : Str = UnionAtIndex (Id 0) (Index 0) Test.10; let Test.14 : [, C Str *self] = UnionAtIndex (Id 0) (Index 1) Test.10; let Test.33 : U64 = CallByName Test.3 Test.12; @@ -36,15 +36,15 @@ procedure Test.2 (Test.9, Test.10): else ret Test.16; in - let #Derived_gen.2 : Int1 = lowlevel RefCountIsUnique Test.9; - if #Derived_gen.2 then + let #Derived_gen.5 : Int1 = lowlevel RefCountIsUnique Test.9; + if #Derived_gen.5 then dec Test.11; free Test.9; - jump #Derived_gen.1; + jump #Derived_gen.4; else inc Test.12; decref Test.9; - jump #Derived_gen.1; + jump #Derived_gen.4; procedure Test.3 (Test.17): let Test.26 : U8 = 1i64; @@ -55,22 +55,22 @@ procedure Test.3 (Test.17): ret Test.22; else let Test.18 : [, C Str *self] = UnionAtIndex (Id 0) (Index 1) Test.17; - joinpoint #Derived_gen.3: + joinpoint #Derived_gen.1: let Test.24 : U64 = 1i64; let Test.25 : U64 = CallByName Test.3 Test.18; let Test.23 : U64 = CallByName Num.19 Test.24 Test.25; ret Test.23; in - let #Derived_gen.5 : Int1 = lowlevel RefCountIsUnique Test.17; - if #Derived_gen.5 then - let #Derived_gen.4 : Str = UnionAtIndex (Id 0) (Index 0) Test.17; - dec #Derived_gen.4; + let #Derived_gen.3 : Int1 = lowlevel RefCountIsUnique Test.17; + if #Derived_gen.3 then + let #Derived_gen.2 : Str = UnionAtIndex (Id 0) (Index 0) Test.17; + dec #Derived_gen.2; free Test.17; - jump #Derived_gen.3; + jump #Derived_gen.1; else inc Test.18; decref Test.17; - jump #Derived_gen.3; + jump #Derived_gen.1; procedure Test.0 (): let Test.5 : [, C Str *self] = TagId(1) ; From 5e3f50298268decfa299102e8011d85c63b8df5d Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Wed, 20 Nov 2024 09:57:02 +1100 Subject: [PATCH 29/57] rename Crypt to Crypto --- .../builtins/bitcode/src/{crypt.zig => crypto.zig} | 0 crates/compiler/builtins/bitcode/src/main.zig | 12 ++++++------ .../builtins/roc/{Crypt.roc => Crypto.roc} | 0 crates/compiler/builtins/roc/main.roc | 2 +- crates/compiler/builtins/src/bitcode.rs | 6 +++--- crates/compiler/builtins/src/roc.rs | 4 ++-- crates/compiler/can/src/builtins.rs | 6 +++--- crates/compiler/gen_dev/src/lib.rs | 12 ++++++------ crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 10 +++++----- crates/compiler/gen_wasm/src/low_level.rs | 12 +++++++----- crates/compiler/load/build.rs | 2 +- crates/compiler/load/src/lib.rs | 4 ++-- crates/compiler/load/tests/test_reporting.rs | 6 +++--- crates/compiler/load_internal/src/file.rs | 6 +++--- crates/compiler/load_internal/src/lib.rs | 2 +- crates/compiler/load_internal/src/module_cache.rs | 2 +- crates/compiler/module/src/ident.rs | 2 +- crates/compiler/module/src/low_level.rs | 12 ++++++------ crates/compiler/module/src/symbol.rs | 14 +++++++------- crates/compiler/mono/src/drop_specialization.rs | 2 +- crates/compiler/mono/src/inc_dec.rs | 6 +++--- 21 files changed, 62 insertions(+), 60 deletions(-) rename crates/compiler/builtins/bitcode/src/{crypt.zig => crypto.zig} (100%) rename crates/compiler/builtins/roc/{Crypt.roc => Crypto.roc} (100%) diff --git a/crates/compiler/builtins/bitcode/src/crypt.zig b/crates/compiler/builtins/bitcode/src/crypto.zig similarity index 100% rename from crates/compiler/builtins/bitcode/src/crypt.zig rename to crates/compiler/builtins/bitcode/src/crypto.zig diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index 42800503d3e..39cf60bc550 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -11,11 +11,11 @@ const NUM = "num"; const STR = "str"; // Crypt Module -const crypt = @import("crypt.zig"); +const crypto = @import("crypto.zig"); comptime { - exportCryptFn(crypt.emptySha256, "emptySha256"); - exportCryptFn(crypt.sha256AddBytes, "sha256AddBytes"); - exportCryptFn(crypt.sha256Digest, "sha256Digest"); + exportCryptoFn(crypto.emptySha256, "emptySha256"); + exportCryptoFn(crypto.sha256AddBytes, "sha256AddBytes"); + exportCryptoFn(crypto.sha256Digest, "sha256Digest"); } // Dec Module @@ -397,8 +397,8 @@ fn exportListFn(comptime func: anytype, comptime func_name: []const u8) void { fn exportDecFn(comptime func: anytype, comptime func_name: []const u8) void { exportBuiltinFn(func, "dec." ++ func_name); } -fn exportCryptFn(comptime func: anytype, comptime func_name: []const u8) void { - exportBuiltinFn(func, "crypt." ++ func_name); +fn exportCryptoFn(comptime func: anytype, comptime func_name: []const u8) void { + exportBuiltinFn(func, "crypto." ++ func_name); } fn exportUtilsFn(comptime func: anytype, comptime func_name: []const u8) void { diff --git a/crates/compiler/builtins/roc/Crypt.roc b/crates/compiler/builtins/roc/Crypto.roc similarity index 100% rename from crates/compiler/builtins/roc/Crypt.roc rename to crates/compiler/builtins/roc/Crypto.roc diff --git a/crates/compiler/builtins/roc/main.roc b/crates/compiler/builtins/roc/main.roc index 13ef4eb07c8..ccb36d7e701 100644 --- a/crates/compiler/builtins/roc/main.roc +++ b/crates/compiler/builtins/roc/main.roc @@ -12,5 +12,5 @@ package [ Box, Inspect, Task, - Crypt, + Crypto, ] {} diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index 9a1b3f33e85..0c4ef8f351b 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -417,9 +417,9 @@ pub const DEC_ROUND: IntrinsicName = int_intrinsic!("roc_builtins.dec.round"); pub const DEC_FLOOR: IntrinsicName = int_intrinsic!("roc_builtins.dec.floor"); pub const DEC_CEILING: IntrinsicName = int_intrinsic!("roc_builtins.dec.ceiling"); -pub const CRYPT_EMPTY_SHA256: &str = "roc_builtins.crypt.emptySha256"; -pub const CRYPT_SHA256_ADD_BYTES: &str = "roc_builtins.crypt.sha256AddBytes"; -pub const CRYPT_SHA256_DIGEST: &str = "roc_builtins.crypt.sha156Digest"; +pub const CRYPTO_EMPTY_SHA256: &str = "roc_builtins.crypto.emptySha256"; +pub const CRYPTO_SHA256_ADD_BYTES: &str = "roc_builtins.crypto.sha256AddBytes"; +pub const CRYPTO_SHA256_DIGEST: &str = "roc_builtins.crypto.sha156Digest"; pub const UTILS_DBG_IMPL: &str = "roc_builtins.utils.dbg_impl"; pub const UTILS_TEST_PANIC: &str = "roc_builtins.utils.test_panic"; diff --git a/crates/compiler/builtins/src/roc.rs b/crates/compiler/builtins/src/roc.rs index 6701d207eb3..ea4f7bd2813 100644 --- a/crates/compiler/builtins/src/roc.rs +++ b/crates/compiler/builtins/src/roc.rs @@ -17,7 +17,7 @@ pub fn module_source(module_id: ModuleId) -> &'static str { ModuleId::HASH => HASH, ModuleId::INSPECT => INSPECT, ModuleId::TASK => TASK, - ModuleId::CRYPT => CRYPT, + ModuleId::CRYPTO => CRYPTO, _ => internal_error!( "ModuleId {:?} is not part of the standard library", module_id @@ -38,4 +38,4 @@ const DECODE: &str = include_str!("../roc/Decode.roc"); const HASH: &str = include_str!("../roc/Hash.roc"); const INSPECT: &str = include_str!("../roc/Inspect.roc"); const TASK: &str = include_str!("../roc/Task.roc"); -const CRYPT: &str = include_str!("../roc/Crypt.roc"); +const CRYPTO: &str = include_str!("../roc/Crypto.roc"); diff --git a/crates/compiler/can/src/builtins.rs b/crates/compiler/can/src/builtins.rs index edfbeff9579..7f63a7d2136 100644 --- a/crates/compiler/can/src/builtins.rs +++ b/crates/compiler/can/src/builtins.rs @@ -210,9 +210,9 @@ map_symbol_to_lowlevel_and_arity! { NumF32FromParts; NUM_F32_FROM_PARTS; 1, NumF64FromParts; NUM_F64_FROM_PARTS; 1, - CryptEmptySha256; CRYPT_EMPTY_SHA_256; 1, - CryptSha256AddBytes; CRYPT_SHA256_ADD_BYTES; 2, - CryptSha256Digest; CRYPT_SHA256_DIGEST; 1, + CryptoEmptySha256; CRYPTO_EMPTY_SHA_256; 1, + CryptoSha256AddBytes; CRYPTO_SHA256_ADD_BYTES; 2, + CryptoSha256Digest; CRYPTO_SHA256_DIGEST; 1, Eq; BOOL_STRUCTURAL_EQ; 2, NotEq; BOOL_STRUCTURAL_NOT_EQ; 2, diff --git a/crates/compiler/gen_dev/src/lib.rs b/crates/compiler/gen_dev/src/lib.rs index 9eaa9813851..03b5f423508 100644 --- a/crates/compiler/gen_dev/src/lib.rs +++ b/crates/compiler/gen_dev/src/lib.rs @@ -2216,18 +2216,18 @@ trait Backend<'a> { self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout) } - LowLevel::CryptEmptySha256 => { - let intrinsic = bitcode::CRYPT_EMPTY_SHA256.to_string(); + LowLevel::CryptoEmptySha256 => { + let intrinsic = bitcode::CRYPTO_EMPTY_SHA256.to_string(); self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } - LowLevel::CryptSha256AddBytes => { - let intrinsic = bitcode::CRYPT_SHA256_ADD_BYTES.to_string(); + LowLevel::CryptoSha256AddBytes => { + let intrinsic = bitcode::CRYPTO_SHA256_ADD_BYTES.to_string(); self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } - LowLevel::CryptSha256Digest => { - let intrinsic = bitcode::CRYPT_SHA256_DIGEST.to_string(); + LowLevel::CryptoSha256Digest => { + let intrinsic = bitcode::CRYPTO_SHA256_DIGEST.to_string(); self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 6e9c4f76b4d..be62dbaa2fe 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1431,14 +1431,14 @@ pub(crate) fn run_low_level<'a, 'ctx>( call_bitcode_fn(env, &[], bitcode::UTILS_DICT_PSEUDO_SEED) } - CryptEmptySha256 => call_bitcode_fn(env, &[], bitcode::CRYPT_EMPTY_SHA256), - CryptSha256AddBytes => { + CryptoEmptySha256 => call_bitcode_fn(env, &[], bitcode::CRYPTO_EMPTY_SHA256), + CryptoSha256AddBytes => { arguments!(sha, data); - call_bitcode_fn(env, &[sha, data], bitcode::CRYPT_SHA256_ADD_BYTES) + call_bitcode_fn(env, &[sha, data], bitcode::CRYPTO_SHA256_ADD_BYTES) } - CryptSha256Digest => { + CryptoSha256Digest => { arguments!(sha); - call_bitcode_fn(env, &[sha], bitcode::CRYPT_SHA256_DIGEST) + call_bitcode_fn(env, &[sha], bitcode::CRYPTO_SHA256_DIGEST) } ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index 4770562255c..5fa98dbf949 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2163,12 +2163,14 @@ impl<'a> LowLevelCall<'a> { NumF64ToParts => self.load_args_and_call_zig(backend, bitcode::NUM_F64_TO_PARTS), NumF32FromParts => self.load_args_and_call_zig(backend, bitcode::NUM_F32_FROM_PARTS), NumF64FromParts => self.load_args_and_call_zig(backend, bitcode::NUM_F64_FROM_PARTS), - // Crypt - CryptEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPT_EMPTY_SHA256), - CryptSha256AddBytes => { - self.load_args_and_call_zig(backend, bitcode::CRYPT_SHA256_ADD_BYTES) + // Crypto + CryptoEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPTO_EMPTY_SHA256), + CryptoSha256AddBytes => { + self.load_args_and_call_zig(backend, bitcode::CRYPTO_SHA256_ADD_BYTES) + } + CryptoSha256Digest => { + self.load_args_and_call_zig(backend, bitcode::CRYPTO_SHA256_DIGEST) } - CryptSha256Digest => self.load_args_and_call_zig(backend, bitcode::CRYPT_SHA256_DIGEST), And => { self.load_args(backend); diff --git a/crates/compiler/load/build.rs b/crates/compiler/load/build.rs index 85b9d32f8cf..64115c1f43b 100644 --- a/crates/compiler/load/build.rs +++ b/crates/compiler/load/build.rs @@ -26,7 +26,7 @@ const MODULES: &[(ModuleId, &str)] = &[ (ModuleId::HASH, "Hash.roc"), (ModuleId::INSPECT, "Inspect.roc"), (ModuleId::TASK, "Task.roc"), - (ModuleId::CRYPT, "Crypt.roc"), + (ModuleId::CRYPTO, "Crypto.roc"), ]; fn main() { diff --git a/crates/compiler/load/src/lib.rs b/crates/compiler/load/src/lib.rs index b3653a59af7..ae6d11874a9 100644 --- a/crates/compiler/load/src/lib.rs +++ b/crates/compiler/load/src/lib.rs @@ -257,7 +257,7 @@ fn read_cached_types() -> MutMap { let mod_hash = include_bytes_align_as!(u128, concat!(env!("OUT_DIR"), "/Hash.dat")); let mod_inspect = include_bytes_align_as!(u128, concat!(env!("OUT_DIR"), "/Inspect.dat")); let mod_task = include_bytes_align_as!(u128, concat!(env!("OUT_DIR"), "/Task.dat")); - let mod_crypt = include_bytes_align_as!(u128, concat!(env!("OUT_DIR"), "/Crypt.dat")); + let mod_crypto = include_bytes_align_as!(u128, concat!(env!("OUT_DIR"), "/Crypto.dat")); let mut output = MutMap::default(); @@ -284,7 +284,7 @@ fn read_cached_types() -> MutMap { output.insert(ModuleId::TASK, deserialize_help(mod_task)); - output.insert(ModuleId::CRYPT, deserialize_help(mod_crypt)); + output.insert(ModuleId::CRYPTO, deserialize_help(mod_crypto)); } output diff --git a/crates/compiler/load/tests/test_reporting.rs b/crates/compiler/load/tests/test_reporting.rs index ce85d671f7e..ff94fe595ff 100644 --- a/crates/compiler/load/tests/test_reporting.rs +++ b/crates/compiler/load/tests/test_reporting.rs @@ -5421,9 +5421,9 @@ mod test_reporting { 6│ 2 -> 2 ^^ - Looks like you are trying to define a function. + Looks like you are trying to define a function. - In Roc, functions are always written as a lambda, like + In Roc, functions are always written as a lambda, like increment = \n -> n + 1 "### @@ -8160,7 +8160,7 @@ All branches in an `if` must have the same type! Encode Inspect Dict - Crypt + Crypto " ); diff --git a/crates/compiler/load_internal/src/file.rs b/crates/compiler/load_internal/src/file.rs index 0f7844ae195..c7a48df8d97 100644 --- a/crates/compiler/load_internal/src/file.rs +++ b/crates/compiler/load_internal/src/file.rs @@ -2338,7 +2338,7 @@ fn update<'a>( extend_module_with_builtin_import(parsed, ModuleId::HASH); extend_module_with_builtin_import(parsed, ModuleId::INSPECT); extend_module_with_builtin_import(parsed, ModuleId::TASK); - extend_module_with_builtin_import(parsed, ModuleId::CRYPT); + extend_module_with_builtin_import(parsed, ModuleId::CRYPTO); } state .module_cache @@ -3660,7 +3660,7 @@ fn load_module<'a>( "Hash", ModuleId::HASH "Inspect", ModuleId::INSPECT "Task", ModuleId::TASK - "Crypt", ModuleId::CRYPT + "Crypto", ModuleId::CRYPTO } let (filename, opt_shorthand) = module_name_to_path(src_dir, &module_name, arc_shorthands); @@ -5233,7 +5233,7 @@ fn canonicalize_and_constrain<'a>( | ModuleId::HASH | ModuleId::INSPECT | ModuleId::TASK - | ModuleId::CRYPT + | ModuleId::CRYPTO ); if !name.is_builtin() || should_include_builtin { diff --git a/crates/compiler/load_internal/src/lib.rs b/crates/compiler/load_internal/src/lib.rs index e86bdaf1ee7..ea7c058002c 100644 --- a/crates/compiler/load_internal/src/lib.rs +++ b/crates/compiler/load_internal/src/lib.rs @@ -26,5 +26,5 @@ pub const BUILTIN_MODULES: &[(ModuleId, &str)] = &[ (ModuleId::HASH, "Hash"), (ModuleId::INSPECT, "Inspect"), (ModuleId::TASK, "Task"), - (ModuleId::CRYPT, "Crypt"), + (ModuleId::CRYPTO, "Crypto"), ]; diff --git a/crates/compiler/load_internal/src/module_cache.rs b/crates/compiler/load_internal/src/module_cache.rs index 165c508b725..6f05b3d4885 100644 --- a/crates/compiler/load_internal/src/module_cache.rs +++ b/crates/compiler/load_internal/src/module_cache.rs @@ -94,7 +94,7 @@ impl Default for ModuleCache<'_> { HASH, INSPECT, TASK, - CRYPT, + CRYPTO, } Self { diff --git a/crates/compiler/module/src/ident.rs b/crates/compiler/module/src/ident.rs index b98d72b179f..8f48f6042dc 100644 --- a/crates/compiler/module/src/ident.rs +++ b/crates/compiler/module/src/ident.rs @@ -131,7 +131,7 @@ impl ModuleName { pub const HASH: &'static str = "Hash"; pub const INSPECT: &'static str = "Inspect"; pub const TASK: &'static str = "Task"; - pub const CRYPT: &'static str = "Crypt"; + pub const CRYPTO: &'static str = "Crypto"; pub fn as_str(&self) -> &str { self.0.as_str() diff --git a/crates/compiler/module/src/low_level.rs b/crates/compiler/module/src/low_level.rs index 2fdadc78a4d..775d58fd680 100644 --- a/crates/compiler/module/src/low_level.rs +++ b/crates/compiler/module/src/low_level.rs @@ -129,9 +129,9 @@ pub enum LowLevel { SetJmp, LongJmp, SetLongJmpBuffer, - CryptEmptySha256, - CryptSha256AddBytes, - CryptSha256Digest, + CryptoEmptySha256, + CryptoSha256AddBytes, + CryptoSha256Digest, } macro_rules! higher_order { @@ -351,7 +351,7 @@ map_symbol_to_lowlevel! { Not <= BOOL_NOT; Unreachable <= LIST_UNREACHABLE; DictPseudoSeed <= DICT_PSEUDO_SEED; - CryptEmptySha256 <= CRYPT_EMPTY_SHA_256; - CryptSha256AddBytes <= CRYPT_SHA256_ADD_BYTES; - CryptSha256Digest <= CRYPT_SHA256_DIGEST; + CryptoEmptySha256 <= CRYPTO_EMPTY_SHA_256; + CryptoSha256AddBytes <= CRYPTO_SHA256_ADD_BYTES; + CryptoSha256Digest <= CRYPTO_SHA256_DIGEST; } diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index acbdc77c23e..09e078395f3 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1733,13 +1733,13 @@ define_builtins! { 13 TASK_FOR_EACH: "forEach" 14 TASK_RESULT: "result" } - 16 CRYPT: "Crypt" =>{ - 0 CRYPT_SHA_256: "Sha256" exposed_type=true - 1 CRYPT_DIGEST_256: "Digest256" exposed_type=true - 2 CRYPT_EMPTY_SHA_256: "emptySha256" - 3 CRYPT_SHA256_ADD_BYTES: "sha256AddBytes" - 4 CRYPT_SHA256_DIGEST: "sha256Digest" - 5 CRYPT_HASH_SHA_256: "hashSha256" + 16 CRYPTO: "Crypto" =>{ + 0 CRYPTO_SHA_256: "Sha256" exposed_type=true + 1 CRYPTO_DIGEST_256: "Digest256" exposed_type=true + 2 CRYPTO_EMPTY_SHA_256: "emptySha256" + 3 CRYPTO_SHA256_ADD_BYTES: "sha256AddBytes" + 4 CRYPTO_SHA256_DIGEST: "sha256Digest" + 5 CRYPTO_HASH_SHA_256: "hashSha256" } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) diff --git a/crates/compiler/mono/src/drop_specialization.rs b/crates/compiler/mono/src/drop_specialization.rs index e9821fbef4d..f8fb0d1cd87 100644 --- a/crates/compiler/mono/src/drop_specialization.rs +++ b/crates/compiler/mono/src/drop_specialization.rs @@ -1609,7 +1609,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC { ListIsUnique => RC::Rc, ListClone => RC::Rc, - CryptEmptySha256 | CryptSha256AddBytes | CryptSha256Digest => RC::NoRc, + CryptoEmptySha256 | CryptoSha256AddBytes | CryptoSha256Digest => RC::NoRc, BoxExpr | UnboxExpr => { unreachable!("These lowlevel operations are turned into mono Expr's") diff --git a/crates/compiler/mono/src/inc_dec.rs b/crates/compiler/mono/src/inc_dec.rs index 87d663c7400..5f842d3bfd0 100644 --- a/crates/compiler/mono/src/inc_dec.rs +++ b/crates/compiler/mono/src/inc_dec.rs @@ -1256,9 +1256,9 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] { ListSwap => &[OWNED, IRRELEVANT, IRRELEVANT], ListReleaseExcessCapacity => &[OWNED], StrReleaseExcessCapacity => &[OWNED], - CryptEmptySha256 => &[IRRELEVANT], - CryptSha256AddBytes => &[OWNED, BORROWED], - CryptSha256Digest => &[BORROWED], + CryptoEmptySha256 => &[IRRELEVANT], + CryptoSha256AddBytes => &[OWNED, BORROWED], + CryptoSha256Digest => &[BORROWED], ListIncref => &[OWNED], ListDecref => &[OWNED], From d9208538f1cfdf476e86216527ff52b289f7b47b Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Wed, 20 Nov 2024 11:37:04 +1100 Subject: [PATCH 30/57] fix LLVM issues --- crates/compiler/builtins/src/bitcode.rs | 2 +- crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index 0c4ef8f351b..a6e10060d9a 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -419,7 +419,7 @@ pub const DEC_CEILING: IntrinsicName = int_intrinsic!("roc_builtins.dec.ceiling" pub const CRYPTO_EMPTY_SHA256: &str = "roc_builtins.crypto.emptySha256"; pub const CRYPTO_SHA256_ADD_BYTES: &str = "roc_builtins.crypto.sha256AddBytes"; -pub const CRYPTO_SHA256_DIGEST: &str = "roc_builtins.crypto.sha156Digest"; +pub const CRYPTO_SHA256_DIGEST: &str = "roc_builtins.crypto.sha256Digest"; pub const UTILS_DBG_IMPL: &str = "roc_builtins.utils.dbg_impl"; pub const UTILS_TEST_PANIC: &str = "roc_builtins.utils.test_panic"; diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index be62dbaa2fe..03d77df2fec 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1433,12 +1433,30 @@ pub(crate) fn run_low_level<'a, 'ctx>( } CryptoEmptySha256 => call_bitcode_fn(env, &[], bitcode::CRYPTO_EMPTY_SHA256), CryptoSha256AddBytes => { + // Crypto.sha256AddBytes : Sha256, List U8 -> Sha256 arguments!(sha, data); - call_bitcode_fn(env, &[sha, data], bitcode::CRYPTO_SHA256_ADD_BYTES) + + let list_ptr = create_entry_block_alloca(env, data.get_type(), "list_alloca"); + env.builder.new_build_store(list_ptr, data); + + call_bitcode_fn( + env, + &[sha, list_ptr.into()], + bitcode::CRYPTO_SHA256_ADD_BYTES, + ) } CryptoSha256Digest => { + // Crypto.sha256Digest : Sha256 -> Digest256 arguments!(sha); - call_bitcode_fn(env, &[sha], bitcode::CRYPTO_SHA256_DIGEST) + + call_bitcode_fn_fixing_for_convention( + env, + layout_interner, + env.module.get_struct_type("crypto.Digest256").unwrap(), + &[sha], + layout, + bitcode::CRYPTO_SHA256_DIGEST, + ) } ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { From e54ff61ed6488d4628448b3512b6688b9747b525 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Fri, 29 Nov 2024 16:13:54 +0000 Subject: [PATCH 31/57] add test --- crates/compiler/builtins/bitcode/src/crypto.zig | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index 93cba4d2cdc..9a68d19f148 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -3,6 +3,7 @@ const crypto = std.crypto; const sha2 = crypto.hash.sha2; const list = @import("list.zig"); const utils = @import("utils.zig"); +const testing = std.testing; const Sha256 = extern struct { location: [*]u8, @@ -20,8 +21,14 @@ pub fn emptySha256() callconv(.C) Sha256 { }; } +test "emptySha256" { + const emptyHash = emptySha256().pointer().*.peek(); + try std.testing.expectEqualSlices(u8, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", emptyHash[0..emptyHash.len]); + } + pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { var out = emptySha256(); + std.debug.print("Here!", .{}); out.pointer().* = sha.pointer().*; if (data.bytes) |bytes| { const byteSlice: []u8 = bytes[0..data.length]; From 0f37191a634f59ca7d03e9b4ddcbe06b23a2c9cb Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Fri, 29 Nov 2024 16:48:56 +0000 Subject: [PATCH 32/57] Removed unwanted line --- crates/compiler/builtins/bitcode/src/crypto.zig | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index 9a68d19f148..714355564c4 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -28,7 +28,6 @@ test "emptySha256" { pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { var out = emptySha256(); - std.debug.print("Here!", .{}); out.pointer().* = sha.pointer().*; if (data.bytes) |bytes| { const byteSlice: []u8 = bytes[0..data.length]; From 6a07cb418a30ed878a9a2bbbd76c45dbcf2a3731 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Fri, 29 Nov 2024 17:55:28 +0000 Subject: [PATCH 33/57] correct test --- crates/compiler/builtins/bitcode/src/crypto.zig | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index 714355564c4..ee9f8f2e276 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -23,7 +23,7 @@ pub fn emptySha256() callconv(.C) Sha256 { test "emptySha256" { const emptyHash = emptySha256().pointer().*.peek(); - try std.testing.expectEqualSlices(u8, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", emptyHash[0..emptyHash.len]); + try std.testing.expect( sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", emptyHash[0..emptyHash.len])); } pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { @@ -44,3 +44,11 @@ pub const Digest256 = extern struct { pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { return @bitCast(sha.pointer().*.peek()); } + +fn sameBytesAsHex(comptime comptime expected_hex: [:0]const u8, input: []const u8) bool{ + for (input, 0..) |input_byte, i| { + const hex_byte = fmt.parseInt(u8, expected_hex[2 * i .. 2 * i + 2], 16) catch unreachable; + if hex_byte != input_byte{ return false;} + } + true +} \ No newline at end of file From 307d6a7ed9e13b049f593ed04bd8ef67416f5f78 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Fri, 29 Nov 2024 22:14:32 +0000 Subject: [PATCH 34/57] test doing its job. alignment is broken --- crates/compiler/builtins/bitcode/src/crypto.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index ee9f8f2e276..fdcf173bc67 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -45,10 +45,10 @@ pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { return @bitCast(sha.pointer().*.peek()); } -fn sameBytesAsHex(comptime comptime expected_hex: [:0]const u8, input: []const u8) bool{ +fn sameBytesAsHex(comptime expected_hex: [:0]const u8, input: []const u8) bool{ for (input, 0..) |input_byte, i| { - const hex_byte = fmt.parseInt(u8, expected_hex[2 * i .. 2 * i + 2], 16) catch unreachable; - if hex_byte != input_byte{ return false;} + const hex_byte = std.fmt.parseInt(u8, expected_hex[2 * i .. 2 * i + 2], 16) catch unreachable; + if (hex_byte != input_byte) { return false;} } - true + return true; } \ No newline at end of file From 00280622588bace7027632f207da37f42b369885 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sat, 30 Nov 2024 22:59:49 +0000 Subject: [PATCH 35/57] location type switched. local test added --- .../compiler/builtins/bitcode/src/crypto.zig | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index fdcf173bc67..631b0a67b37 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const crypto = std.crypto; const sha2 = crypto.hash.sha2; const list = @import("list.zig"); @@ -6,32 +7,30 @@ const utils = @import("utils.zig"); const testing = std.testing; const Sha256 = extern struct { - location: [*]u8, - fn pointer(self: Sha256) *sha2.Sha256 { - return @alignCast(@ptrCast(self.location)); - } + location: *sha2.Sha256, }; pub fn emptySha256() callconv(.C) Sha256 { - const allocation = utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256), false); - const ptr: *sha2.Sha256 = @alignCast(@ptrCast(allocation)); - ptr.* = sha2.Sha256.init(.{}); + const location: *sha2.Sha256 = create(sha2.Sha256); + location.* = sha2.Sha256.init(.{}); return Sha256{ - .location = @alignCast(@ptrCast(ptr)), + .location = location, }; } test "emptySha256" { - const emptyHash = emptySha256().pointer().*.peek(); - try std.testing.expect( sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", emptyHash[0..emptyHash.len])); - } + const emptySha = emptySha256(); + defer std.testing.allocator.destroy(emptySha.location); + const emptyHash = emptySha.location.*.peek(); + try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", emptyHash[0..emptyHash.len])); +} pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { var out = emptySha256(); - out.pointer().* = sha.pointer().*; + out.location.* = sha.location.*; if (data.bytes) |bytes| { const byteSlice: []u8 = bytes[0..data.length]; - out.pointer().*.update(byteSlice); + out.location.*.update(byteSlice); } return out; } @@ -42,13 +41,22 @@ pub const Digest256 = extern struct { }; pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { - return @bitCast(sha.pointer().*.peek()); + return @bitCast(sha.location.*.peek()); } -fn sameBytesAsHex(comptime expected_hex: [:0]const u8, input: []const u8) bool{ - for (input, 0..) |input_byte, i| { +fn sameBytesAsHex(comptime expected_hex: [:0]const u8, input: []const u8) bool { + for (input, 0..) |input_byte, i| { const hex_byte = std.fmt.parseInt(u8, expected_hex[2 * i .. 2 * i + 2], 16) catch unreachable; - if (hex_byte != input_byte) { return false;} + if (hex_byte != input_byte) { + return false; + } } return true; -} \ No newline at end of file +} + +fn create(comptime T: type) *T { + if (builtin.is_test) { + return std.testing.allocator.create(T) catch unreachable; + } + return @alignCast(@ptrCast(utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256), false))); +} From 1cc7a8e85656f675908da75d3ef7b0281608aac2 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sat, 30 Nov 2024 23:21:06 +0000 Subject: [PATCH 36/57] test add bytes --- .../compiler/builtins/bitcode/src/crypto.zig | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index 631b0a67b37..ad160a68d53 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -10,6 +10,13 @@ const Sha256 = extern struct { location: *sha2.Sha256, }; +fn create(comptime T: type) *T { + if (builtin.is_test) { + return std.testing.allocator.create(T) catch unreachable; + } + return @alignCast(@ptrCast(utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256), false))); +} + pub fn emptySha256() callconv(.C) Sha256 { const location: *sha2.Sha256 = create(sha2.Sha256); location.* = sha2.Sha256.init(.{}); @@ -19,10 +26,10 @@ pub fn emptySha256() callconv(.C) Sha256 { } test "emptySha256" { - const emptySha = emptySha256(); - defer std.testing.allocator.destroy(emptySha.location); - const emptyHash = emptySha.location.*.peek(); - try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", emptyHash[0..emptyHash.len])); + const empty_sha = emptySha256(); + defer std.testing.allocator.destroy(empty_sha.location); + const empty_hash = empty_sha.location.*.peek(); + try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", empty_hash[0..empty_hash.len])); } pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { @@ -35,6 +42,19 @@ pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { return out; } +fn rcNone(_: ?[*]u8) callconv(.C) void {} + +test "sha256AddBytes" { + const empty_sha = emptySha256(); + defer std.testing.allocator.destroy(empty_sha.location); + const abc = list.RocList.fromSlice(u8, "abc", false); + defer abc.decref(@alignOf(u8), @sizeOf(u8), false, rcNone); + const abc_sha = sha256AddBytes(empty_sha, abc); + defer std.testing.allocator.destroy(abc_sha.location); + const abc_hash = abc_sha.location.*.peek(); + try std.testing.expect(sameBytesAsHex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", abc_hash[0..abc_hash.len])); +} + pub const Digest256 = extern struct { firstHalf: u128, secondHalf: u128, @@ -53,10 +73,3 @@ fn sameBytesAsHex(comptime expected_hex: [:0]const u8, input: []const u8) bool { } return true; } - -fn create(comptime T: type) *T { - if (builtin.is_test) { - return std.testing.allocator.create(T) catch unreachable; - } - return @alignCast(@ptrCast(utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256), false))); -} From 070a3123f6ece90d188bbbbfad3e50d90455d5b6 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sat, 30 Nov 2024 23:30:27 +0000 Subject: [PATCH 37/57] comment --- crates/compiler/builtins/bitcode/src/crypto.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index ad160a68d53..1b100d1726a 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -11,6 +11,7 @@ const Sha256 = extern struct { }; fn create(comptime T: type) *T { + //test_roc_alloc ignores alignment if (builtin.is_test) { return std.testing.allocator.create(T) catch unreachable; } From 5e7cc8d47198ab872cb7e1d3c757b63e4cc13a38 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sun, 1 Dec 2024 09:32:21 +0000 Subject: [PATCH 38/57] added length check to sameBytesAsHex --- crates/compiler/builtins/bitcode/src/crypto.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index 1b100d1726a..bca34768e2b 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -66,11 +66,16 @@ pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { } fn sameBytesAsHex(comptime expected_hex: [:0]const u8, input: []const u8) bool { + if (expected_hex.len != 2 * input.len) { + return false; + } + for (input, 0..) |input_byte, i| { const hex_byte = std.fmt.parseInt(u8, expected_hex[2 * i .. 2 * i + 2], 16) catch unreachable; if (hex_byte != input_byte) { return false; } } + return true; } From 463d6948f837eb4d1b9e9d03fb72a55b78fe67d4 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sun, 1 Dec 2024 11:29:47 +0000 Subject: [PATCH 39/57] Test for digest --- .../compiler/builtins/bitcode/src/crypto.zig | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index bca34768e2b..df2c563cfa0 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -43,8 +43,6 @@ pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { return out; } -fn rcNone(_: ?[*]u8) callconv(.C) void {} - test "sha256AddBytes" { const empty_sha = emptySha256(); defer std.testing.allocator.destroy(empty_sha.location); @@ -57,14 +55,26 @@ test "sha256AddBytes" { } pub const Digest256 = extern struct { - firstHalf: u128, - secondHalf: u128, + first_half: u128, + second_half: u128, }; pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { return @bitCast(sha.location.*.peek()); } +test "sha256Digest" { + const empty_sha = emptySha256(); + defer std.testing.allocator.destroy(empty_sha.location); + const digest = sha256Digest(empty_sha); + const first_half_bytes: [16]u8 = @bitCast(digest.first_half); + const second_half_bytes: [16]u8 = @bitCast(digest.second_half); + try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb924", first_half_bytes[0..first_half_bytes.len])); + try std.testing.expect(sameBytesAsHex("27ae41e4649b934ca495991b7852b855", second_half_bytes[0..second_half_bytes.len])); +} +//----------------test utilities ------------------------ +fn rcNone(_: ?[*]u8) callconv(.C) void {} + fn sameBytesAsHex(comptime expected_hex: [:0]const u8, input: []const u8) bool { if (expected_hex.len != 2 * input.len) { return false; From c034933e717a8af92379a75ba401f22232680c01 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sun, 1 Dec 2024 11:31:49 +0000 Subject: [PATCH 40/57] Applied hint --- crates/compiler/builtins/bitcode/src/crypto.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index df2c563cfa0..1d8c04d2ccd 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -34,7 +34,7 @@ test "emptySha256" { } pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { - var out = emptySha256(); + const out = emptySha256(); out.location.* = sha.location.*; if (data.bytes) |bytes| { const byteSlice: []u8 = bytes[0..data.length]; From 403595346a63f52ba91f3c2e6d7eaa24b06d0d86 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sun, 1 Dec 2024 15:34:21 +0000 Subject: [PATCH 41/57] Switches u128Bytes to little-endian --- crates/compiler/builtins/roc/Crypto.roc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/compiler/builtins/roc/Crypto.roc b/crates/compiler/builtins/roc/Crypto.roc index 5f2a0557297..cc78d7ac50c 100644 --- a/crates/compiler/builtins/roc/Crypto.roc +++ b/crates/compiler/builtins/roc/Crypto.roc @@ -31,10 +31,11 @@ sha256AddBytes : Sha256, List U8 -> Sha256 ## Returns the digest of the cryptographic hashing function represented by a SHA-256 hasher.. sha256Digest : Sha256 -> Digest256 -## Applies the SHA-256 crytographic hashing function to some bytes. +## Applies the SHA-256 cryptographic hashing function to some bytes. hashSha256 : List U8 -> Digest256 hashSha256 = \bytes -> emptySha256 {} |> sha256AddBytes bytes |> sha256Digest +# Assumes little-endian. Probably shouldn't. u128Bytes : U128 -> List U8 u128Bytes = \number -> loop = \n, bytes, place -> @@ -42,9 +43,17 @@ u128Bytes = \number -> bytes else newByte = n |> Num.bitwiseAnd 255 |> Num.toU8 - loop (Num.shiftRightBy n 8) (List.prepend bytes newByte) (place + 1) + loop (Num.shiftRightBy n 8) (List.append bytes newByte) (place + 1) loop number [] 0 +expect + bytes1 = u128Bytes 1 + bytes1 == [1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0] + +expect + bytes257 = u128Bytes 0x000102030405060708090a0b0c0d0e0f + bytes257 == [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] + ## Returns the bytes of a SHA-256 digest as a list. digest256ToBytes : Digest256 -> List U8 digest256ToBytes = \@Digest256 { firstHalf, secondHalf } -> From 861c75f3c28951c29ff39059cffea4772d6ceb30 Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Sat, 7 Dec 2024 19:58:01 +1100 Subject: [PATCH 42/57] roc format Crypto.roc --- crates/compiler/builtins/roc/Crypto.roc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/compiler/builtins/roc/Crypto.roc b/crates/compiler/builtins/roc/Crypto.roc index cc78d7ac50c..32d47361486 100644 --- a/crates/compiler/builtins/roc/Crypto.roc +++ b/crates/compiler/builtins/roc/Crypto.roc @@ -48,7 +48,7 @@ u128Bytes = \number -> expect bytes1 = u128Bytes 1 - bytes1 == [1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0] + bytes1 == [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] expect bytes257 = u128Bytes 0x000102030405060708090a0b0c0d0e0f From 608f787cc089bf2aaf443dfab4abebae6a2a235d Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Sat, 7 Dec 2024 20:25:49 +1100 Subject: [PATCH 43/57] syntax and mono updates --- ...tests__cli_tests__transitive_expects_verbose.snap | 6 +++--- crates/compiler/load/tests/test_reporting.rs | 10 +++++----- .../test_mono/generated/capture_void_layout_task.txt | 6 +++--- .../choose_correct_recursion_var_under_record.txt | 6 +++--- ...ursive_lambda_set_productive_nullable_wrapped.txt | 4 ++-- .../encode_derived_nested_record_string.txt | 12 ++++++------ .../encode_derived_record_one_field_string.txt | 6 +++--- .../encode_derived_record_two_field_strings.txt | 6 +++--- .../encode_derived_tag_two_payloads_string.txt | 6 +++--- ...n_does_not_duplicate_identical_concrete_types.txt | 6 +++--- ...ncrete_types_without_unification_of_unifiable.txt | 12 ++++++------ 11 files changed, 40 insertions(+), 40 deletions(-) diff --git a/crates/cli/tests/snapshots/cli_tests__cli_tests__transitive_expects_verbose.snap b/crates/cli/tests/snapshots/cli_tests__cli_tests__transitive_expects_verbose.snap index 2c3bb24398e..54dacebc38c 100644 --- a/crates/cli/tests/snapshots/cli_tests__cli_tests__transitive_expects_verbose.snap +++ b/crates/cli/tests/snapshots/cli_tests__cli_tests__transitive_expects_verbose.snap @@ -4,8 +4,8 @@ expression: cli_test_out.normalize_stdout_and_stderr() --- Compiled in ms. -Direct.roc: - 0 failed and 2 passed in ms. - Transitive.roc: 0 failed and 1 passed in ms. + +Direct.roc: + 0 failed and 2 passed in ms. diff --git a/crates/compiler/load/tests/test_reporting.rs b/crates/compiler/load/tests/test_reporting.rs index ff94fe595ff..88b0c9719ee 100644 --- a/crates/compiler/load/tests/test_reporting.rs +++ b/crates/compiler/load/tests/test_reporting.rs @@ -5421,9 +5421,9 @@ mod test_reporting { 6│ 2 -> 2 ^^ - Looks like you are trying to define a function. + Looks like you are trying to define a function. - In Roc, functions are always written as a lambda, like + In Roc, functions are always written as a lambda, like increment = \n -> n + 1 "### @@ -8146,7 +8146,7 @@ All branches in an `if` must have the same type! alt "# ), - @r" + @r###" ── MODULE NOT IMPORTED in /code/proj/Main.roc ────────────────────────────────── The `Unimported` module is not imported: @@ -8160,8 +8160,8 @@ All branches in an `if` must have the same type! Encode Inspect Dict - Crypto - " + List + "### ); test_report!( diff --git a/crates/compiler/test_mono/generated/capture_void_layout_task.txt b/crates/compiler/test_mono/generated/capture_void_layout_task.txt index 0487b5e2cfd..41fec32f840 100644 --- a/crates/compiler/test_mono/generated/capture_void_layout_task.txt +++ b/crates/compiler/test_mono/generated/capture_void_layout_task.txt @@ -1,4 +1,4 @@ -procedure List.100 (#Derived_gen.13, #Derived_gen.14, #Derived_gen.15, #Derived_gen.16, #Derived_gen.17): +procedure List.100 (#Derived_gen.10, #Derived_gen.11, #Derived_gen.12, #Derived_gen.13, #Derived_gen.14): joinpoint List.662 List.174 List.175 List.176 List.177 List.178: let List.664 : Int1 = CallByName Num.22 List.177 List.178; if List.664 then @@ -11,8 +11,8 @@ procedure List.100 (#Derived_gen.13, #Derived_gen.14, #Derived_gen.15, #Derived_ dec List.174; ret List.175; in - inc #Derived_gen.13; - jump List.662 #Derived_gen.13 #Derived_gen.14 #Derived_gen.15 #Derived_gen.16 #Derived_gen.17; + inc #Derived_gen.10; + jump List.662 #Derived_gen.10 #Derived_gen.11 #Derived_gen.12 #Derived_gen.13 #Derived_gen.14; procedure List.18 (List.171, List.172, List.173): let List.660 : U64 = 0i64; diff --git a/crates/compiler/test_mono/generated/choose_correct_recursion_var_under_record.txt b/crates/compiler/test_mono/generated/choose_correct_recursion_var_under_record.txt index 75323ee1c07..3dd9c09f635 100644 --- a/crates/compiler/test_mono/generated/choose_correct_recursion_var_under_record.txt +++ b/crates/compiler/test_mono/generated/choose_correct_recursion_var_under_record.txt @@ -2,7 +2,7 @@ procedure Bool.1 (): let Bool.24 : Int1 = false; ret Bool.24; -procedure List.100 (#Derived_gen.0, #Derived_gen.1, #Derived_gen.2, #Derived_gen.3, #Derived_gen.4): +procedure List.100 (#Derived_gen.1, #Derived_gen.2, #Derived_gen.3, #Derived_gen.4, #Derived_gen.5): joinpoint List.681 List.174 List.175 List.176 List.177 List.178: let List.683 : Int1 = CallByName Num.22 List.177 List.178; if List.683 then @@ -16,8 +16,8 @@ procedure List.100 (#Derived_gen.0, #Derived_gen.1, #Derived_gen.2, #Derived_gen dec List.174; ret List.175; in - inc #Derived_gen.0; - jump List.681 #Derived_gen.0 #Derived_gen.1 #Derived_gen.2 #Derived_gen.3 #Derived_gen.4; + inc #Derived_gen.1; + jump List.681 #Derived_gen.1 #Derived_gen.2 #Derived_gen.3 #Derived_gen.4 #Derived_gen.5; procedure List.18 (List.171, List.172, List.173): let List.679 : U64 = 0i64; diff --git a/crates/compiler/test_mono/generated/compose_recursive_lambda_set_productive_nullable_wrapped.txt b/crates/compiler/test_mono/generated/compose_recursive_lambda_set_productive_nullable_wrapped.txt index 84df43f8d94..9ffbe418f85 100644 --- a/crates/compiler/test_mono/generated/compose_recursive_lambda_set_productive_nullable_wrapped.txt +++ b/crates/compiler/test_mono/generated/compose_recursive_lambda_set_productive_nullable_wrapped.txt @@ -47,7 +47,7 @@ procedure Str.3 (#Attr.2, #Attr.3): procedure Test.1 (Test.5): ret Test.5; -procedure Test.11 (#Derived_gen.8, #Derived_gen.9): +procedure Test.11 (#Derived_gen.5, #Derived_gen.6): joinpoint Test.27 Test.12 #Attr.12: let Test.34 : Int1 = UnionAtIndex (Id 2) (Index 1) #Attr.12; let Test.33 : [, C *self Int1, C *self Int1] = UnionAtIndex (Id 2) (Index 0) #Attr.12; @@ -87,7 +87,7 @@ procedure Test.11 (#Derived_gen.8, #Derived_gen.9): decref #Attr.12; jump #Derived_gen.12; in - jump Test.27 #Derived_gen.8 #Derived_gen.9; + jump Test.27 #Derived_gen.5 #Derived_gen.6; procedure Test.2 (Test.13): ret Test.13; diff --git a/crates/compiler/test_mono/generated/encode_derived_nested_record_string.txt b/crates/compiler/test_mono/generated/encode_derived_nested_record_string.txt index cbe4549671c..143c3a6a61f 100644 --- a/crates/compiler/test_mono/generated/encode_derived_nested_record_string.txt +++ b/crates/compiler/test_mono/generated/encode_derived_nested_record_string.txt @@ -67,7 +67,7 @@ procedure Encode.26 (Encode.107, Encode.108): let Encode.110 : List U8 = CallByName Encode.24 Encode.111 Encode.112 Encode.108; ret Encode.110; -procedure List.100 (#Derived_gen.29, #Derived_gen.30, #Derived_gen.31, #Derived_gen.32, #Derived_gen.33): +procedure List.100 (#Derived_gen.26, #Derived_gen.27, #Derived_gen.28, #Derived_gen.29, #Derived_gen.30): joinpoint List.688 List.174 List.175 List.176 List.177 List.178: let List.690 : Int1 = CallByName Num.22 List.177 List.178; if List.690 then @@ -81,10 +81,10 @@ procedure List.100 (#Derived_gen.29, #Derived_gen.30, #Derived_gen.31, #Derived_ dec List.174; ret List.175; in - inc #Derived_gen.29; - jump List.688 #Derived_gen.29 #Derived_gen.30 #Derived_gen.31 #Derived_gen.32 #Derived_gen.33; + inc #Derived_gen.26; + jump List.688 #Derived_gen.26 #Derived_gen.27 #Derived_gen.28 #Derived_gen.29 #Derived_gen.30; -procedure List.100 (#Derived_gen.37, #Derived_gen.38, #Derived_gen.39, #Derived_gen.40, #Derived_gen.41): +procedure List.100 (#Derived_gen.34, #Derived_gen.35, #Derived_gen.36, #Derived_gen.37, #Derived_gen.38): joinpoint List.662 List.174 List.175 List.176 List.177 List.178: let List.664 : Int1 = CallByName Num.22 List.177 List.178; if List.664 then @@ -98,8 +98,8 @@ procedure List.100 (#Derived_gen.37, #Derived_gen.38, #Derived_gen.39, #Derived_ dec List.174; ret List.175; in - inc #Derived_gen.37; - jump List.662 #Derived_gen.37 #Derived_gen.38 #Derived_gen.39 #Derived_gen.40 #Derived_gen.41; + inc #Derived_gen.34; + jump List.662 #Derived_gen.34 #Derived_gen.35 #Derived_gen.36 #Derived_gen.37 #Derived_gen.38; procedure List.18 (List.171, List.172, List.173): let List.660 : U64 = 0i64; diff --git a/crates/compiler/test_mono/generated/encode_derived_record_one_field_string.txt b/crates/compiler/test_mono/generated/encode_derived_record_one_field_string.txt index f97f1c8bdc8..e4b7c2b15f2 100644 --- a/crates/compiler/test_mono/generated/encode_derived_record_one_field_string.txt +++ b/crates/compiler/test_mono/generated/encode_derived_record_one_field_string.txt @@ -39,7 +39,7 @@ procedure Encode.26 (Encode.107, Encode.108): let Encode.110 : List U8 = CallByName Encode.24 Encode.111 Encode.112 Encode.108; ret Encode.110; -procedure List.100 (#Derived_gen.16, #Derived_gen.17, #Derived_gen.18, #Derived_gen.19, #Derived_gen.20): +procedure List.100 (#Derived_gen.19, #Derived_gen.20, #Derived_gen.21, #Derived_gen.22, #Derived_gen.23): joinpoint List.662 List.174 List.175 List.176 List.177 List.178: let List.664 : Int1 = CallByName Num.22 List.177 List.178; if List.664 then @@ -53,8 +53,8 @@ procedure List.100 (#Derived_gen.16, #Derived_gen.17, #Derived_gen.18, #Derived_ dec List.174; ret List.175; in - inc #Derived_gen.16; - jump List.662 #Derived_gen.16 #Derived_gen.17 #Derived_gen.18 #Derived_gen.19 #Derived_gen.20; + inc #Derived_gen.19; + jump List.662 #Derived_gen.19 #Derived_gen.20 #Derived_gen.21 #Derived_gen.22 #Derived_gen.23; procedure List.18 (List.171, List.172, List.173): let List.660 : U64 = 0i64; diff --git a/crates/compiler/test_mono/generated/encode_derived_record_two_field_strings.txt b/crates/compiler/test_mono/generated/encode_derived_record_two_field_strings.txt index 311b88e7b25..e44e9b35303 100644 --- a/crates/compiler/test_mono/generated/encode_derived_record_two_field_strings.txt +++ b/crates/compiler/test_mono/generated/encode_derived_record_two_field_strings.txt @@ -46,7 +46,7 @@ procedure Encode.26 (Encode.107, Encode.108): let Encode.110 : List U8 = CallByName Encode.24 Encode.111 Encode.112 Encode.108; ret Encode.110; -procedure List.100 (#Derived_gen.20, #Derived_gen.21, #Derived_gen.22, #Derived_gen.23, #Derived_gen.24): +procedure List.100 (#Derived_gen.23, #Derived_gen.24, #Derived_gen.25, #Derived_gen.26, #Derived_gen.27): joinpoint List.662 List.174 List.175 List.176 List.177 List.178: let List.664 : Int1 = CallByName Num.22 List.177 List.178; if List.664 then @@ -60,8 +60,8 @@ procedure List.100 (#Derived_gen.20, #Derived_gen.21, #Derived_gen.22, #Derived_ dec List.174; ret List.175; in - inc #Derived_gen.20; - jump List.662 #Derived_gen.20 #Derived_gen.21 #Derived_gen.22 #Derived_gen.23 #Derived_gen.24; + inc #Derived_gen.23; + jump List.662 #Derived_gen.23 #Derived_gen.24 #Derived_gen.25 #Derived_gen.26 #Derived_gen.27; procedure List.18 (List.171, List.172, List.173): let List.660 : U64 = 0i64; diff --git a/crates/compiler/test_mono/generated/encode_derived_tag_two_payloads_string.txt b/crates/compiler/test_mono/generated/encode_derived_tag_two_payloads_string.txt index e08ecc31d55..18ec24c91b6 100644 --- a/crates/compiler/test_mono/generated/encode_derived_tag_two_payloads_string.txt +++ b/crates/compiler/test_mono/generated/encode_derived_tag_two_payloads_string.txt @@ -43,7 +43,7 @@ procedure Encode.26 (Encode.107, Encode.108): let Encode.110 : List U8 = CallByName Encode.24 Encode.111 Encode.112 Encode.108; ret Encode.110; -procedure List.100 (#Derived_gen.23, #Derived_gen.24, #Derived_gen.25, #Derived_gen.26, #Derived_gen.27): +procedure List.100 (#Derived_gen.20, #Derived_gen.21, #Derived_gen.22, #Derived_gen.23, #Derived_gen.24): joinpoint List.662 List.174 List.175 List.176 List.177 List.178: let List.664 : Int1 = CallByName Num.22 List.177 List.178; if List.664 then @@ -57,8 +57,8 @@ procedure List.100 (#Derived_gen.23, #Derived_gen.24, #Derived_gen.25, #Derived_ dec List.174; ret List.175; in - inc #Derived_gen.23; - jump List.662 #Derived_gen.23 #Derived_gen.24 #Derived_gen.25 #Derived_gen.26 #Derived_gen.27; + inc #Derived_gen.20; + jump List.662 #Derived_gen.20 #Derived_gen.21 #Derived_gen.22 #Derived_gen.23 #Derived_gen.24; procedure List.13 (#Attr.2, #Attr.3): let List.685 : List Str = lowlevel ListPrepend #Attr.2 #Attr.3; diff --git a/crates/compiler/test_mono/generated/unspecialized_lambda_set_unification_does_not_duplicate_identical_concrete_types.txt b/crates/compiler/test_mono/generated/unspecialized_lambda_set_unification_does_not_duplicate_identical_concrete_types.txt index e5fc4dc70bb..3eab6b11d4f 100644 --- a/crates/compiler/test_mono/generated/unspecialized_lambda_set_unification_does_not_duplicate_identical_concrete_types.txt +++ b/crates/compiler/test_mono/generated/unspecialized_lambda_set_unification_does_not_duplicate_identical_concrete_types.txt @@ -29,7 +29,7 @@ procedure Encode.26 (Encode.107, Encode.108): let Encode.110 : List U8 = CallByName Encode.24 Encode.111 Encode.112 Encode.108; ret Encode.110; -procedure List.100 (#Derived_gen.0, #Derived_gen.1, #Derived_gen.2, #Derived_gen.3, #Derived_gen.4): +procedure List.100 (#Derived_gen.6, #Derived_gen.7, #Derived_gen.8, #Derived_gen.9, #Derived_gen.10): joinpoint List.662 List.174 List.175 List.176 List.177 List.178: let List.664 : Int1 = CallByName Num.22 List.177 List.178; if List.664 then @@ -43,8 +43,8 @@ procedure List.100 (#Derived_gen.0, #Derived_gen.1, #Derived_gen.2, #Derived_gen dec List.174; ret List.175; in - inc #Derived_gen.0; - jump List.662 #Derived_gen.0 #Derived_gen.1 #Derived_gen.2 #Derived_gen.3 #Derived_gen.4; + inc #Derived_gen.6; + jump List.662 #Derived_gen.6 #Derived_gen.7 #Derived_gen.8 #Derived_gen.9 #Derived_gen.10; procedure List.13 (#Attr.2, #Attr.3): let List.685 : List Str = lowlevel ListPrepend #Attr.2 #Attr.3; diff --git a/crates/compiler/test_mono/generated/unspecialized_lambda_set_unification_keeps_all_concrete_types_without_unification_of_unifiable.txt b/crates/compiler/test_mono/generated/unspecialized_lambda_set_unification_keeps_all_concrete_types_without_unification_of_unifiable.txt index c1812728370..d7ec1c80c64 100644 --- a/crates/compiler/test_mono/generated/unspecialized_lambda_set_unification_keeps_all_concrete_types_without_unification_of_unifiable.txt +++ b/crates/compiler/test_mono/generated/unspecialized_lambda_set_unification_keeps_all_concrete_types_without_unification_of_unifiable.txt @@ -87,7 +87,7 @@ procedure Encode.26 (Encode.107, Encode.108): let Encode.110 : List U8 = CallByName Encode.24 Encode.111 Encode.112 Encode.108; ret Encode.110; -procedure List.100 (#Derived_gen.44, #Derived_gen.45, #Derived_gen.46, #Derived_gen.47, #Derived_gen.48): +procedure List.100 (#Derived_gen.38, #Derived_gen.39, #Derived_gen.40, #Derived_gen.41, #Derived_gen.42): joinpoint List.689 List.174 List.175 List.176 List.177 List.178: let List.691 : Int1 = CallByName Num.22 List.177 List.178; if List.691 then @@ -101,10 +101,10 @@ procedure List.100 (#Derived_gen.44, #Derived_gen.45, #Derived_gen.46, #Derived_ dec List.174; ret List.175; in - inc #Derived_gen.44; - jump List.689 #Derived_gen.44 #Derived_gen.45 #Derived_gen.46 #Derived_gen.47 #Derived_gen.48; + inc #Derived_gen.38; + jump List.689 #Derived_gen.38 #Derived_gen.39 #Derived_gen.40 #Derived_gen.41 #Derived_gen.42; -procedure List.100 (#Derived_gen.49, #Derived_gen.50, #Derived_gen.51, #Derived_gen.52, #Derived_gen.53): +procedure List.100 (#Derived_gen.46, #Derived_gen.47, #Derived_gen.48, #Derived_gen.49, #Derived_gen.50): joinpoint List.662 List.174 List.175 List.176 List.177 List.178: let List.664 : Int1 = CallByName Num.22 List.177 List.178; if List.664 then @@ -118,8 +118,8 @@ procedure List.100 (#Derived_gen.49, #Derived_gen.50, #Derived_gen.51, #Derived_ dec List.174; ret List.175; in - inc #Derived_gen.49; - jump List.662 #Derived_gen.49 #Derived_gen.50 #Derived_gen.51 #Derived_gen.52 #Derived_gen.53; + inc #Derived_gen.46; + jump List.662 #Derived_gen.46 #Derived_gen.47 #Derived_gen.48 #Derived_gen.49 #Derived_gen.50; procedure List.13 (#Attr.2, #Attr.3): let List.685 : List [C {}, C {}, C Str] = lowlevel ListPrepend #Attr.2 #Attr.3; From 056b918701ed9b0a3cb6aaa1b90aa76c6edc60f2 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sun, 8 Dec 2024 18:44:34 +0000 Subject: [PATCH 44/57] Removed alloc calls from zig code --- .../compiler/builtins/bitcode/src/crypto.zig | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index 1d8c04d2ccd..cded331af5a 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -7,50 +7,45 @@ const utils = @import("utils.zig"); const testing = std.testing; const Sha256 = extern struct { - location: *sha2.Sha256, + data: [@sizeOf(sha2.Sha256)]u8 align(@alignOf(sha2.Sha256)), }; -fn create(comptime T: type) *T { - //test_roc_alloc ignores alignment - if (builtin.is_test) { - return std.testing.allocator.create(T) catch unreachable; - } - return @alignCast(@ptrCast(utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256), false))); +test "Sha256 size and alignment" { + try std.testing.expectEqual(@sizeOf(Sha256), 128); + try std.testing.expectEqual(@alignOf(Sha256), 16); } pub fn emptySha256() callconv(.C) Sha256 { - const location: *sha2.Sha256 = create(sha2.Sha256); - location.* = sha2.Sha256.init(.{}); - return Sha256{ - .location = location, - }; + var sha: Sha256 = undefined; + const ptr: *sha2.Sha256 = @constCast(@ptrCast(&sha)); + ptr.* = sha2.Sha256.init(.{}); + return sha; } test "emptySha256" { const empty_sha = emptySha256(); - defer std.testing.allocator.destroy(empty_sha.location); - const empty_hash = empty_sha.location.*.peek(); + const ptr: *sha2.Sha256 = @constCast(@ptrCast(&empty_sha)); + const empty_hash = ptr.*.peek(); try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", empty_hash[0..empty_hash.len])); } pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { - const out = emptySha256(); - out.location.* = sha.location.*; + var out = sha; + const out_ptr: *sha2.Sha256 = @constCast(@ptrCast(&out)); if (data.bytes) |bytes| { const byteSlice: []u8 = bytes[0..data.length]; - out.location.*.update(byteSlice); + out_ptr.*.update(byteSlice); } return out; } test "sha256AddBytes" { const empty_sha = emptySha256(); - defer std.testing.allocator.destroy(empty_sha.location); const abc = list.RocList.fromSlice(u8, "abc", false); defer abc.decref(@alignOf(u8), @sizeOf(u8), false, rcNone); const abc_sha = sha256AddBytes(empty_sha, abc); - defer std.testing.allocator.destroy(abc_sha.location); - const abc_hash = abc_sha.location.*.peek(); + const abc_ptr: *sha2.Sha256 = @constCast(@ptrCast(&abc_sha)); + const abc_hash = abc_ptr.*.peek(); try std.testing.expect(sameBytesAsHex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", abc_hash[0..abc_hash.len])); } @@ -60,19 +55,19 @@ pub const Digest256 = extern struct { }; pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { - return @bitCast(sha.location.*.peek()); + const ptr: *sha2.Sha256 = @constCast(@ptrCast(&sha)); + return @bitCast(ptr.*.peek()); } test "sha256Digest" { const empty_sha = emptySha256(); - defer std.testing.allocator.destroy(empty_sha.location); const digest = sha256Digest(empty_sha); const first_half_bytes: [16]u8 = @bitCast(digest.first_half); const second_half_bytes: [16]u8 = @bitCast(digest.second_half); try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb924", first_half_bytes[0..first_half_bytes.len])); try std.testing.expect(sameBytesAsHex("27ae41e4649b934ca495991b7852b855", second_half_bytes[0..second_half_bytes.len])); } -//----------------test utilities ------------------------ +// ----------------test utilities ------------------------ fn rcNone(_: ?[*]u8) callconv(.C) void {} fn sameBytesAsHex(comptime expected_hex: [:0]const u8, input: []const u8) bool { From cd51cdba7c7f5877a9daf357695573af715d44b5 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Sun, 8 Dec 2024 18:51:40 +0000 Subject: [PATCH 45/57] Switched type in Roc --- crates/compiler/builtins/roc/Crypto.roc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/compiler/builtins/roc/Crypto.roc b/crates/compiler/builtins/roc/Crypto.roc index 32d47361486..f494a78ec01 100644 --- a/crates/compiler/builtins/roc/Crypto.roc +++ b/crates/compiler/builtins/roc/Crypto.roc @@ -10,12 +10,12 @@ module [ import Bool exposing [Eq] import List -import Num exposing [U8, U64, U128] +import Num exposing [U8, U128] import Result import Str ## Represents the state of a SHA-256 cryptographic hashing function, after some (or no) data has been added to the hash. -Sha256 := { location : U64 } +Sha256 := { block0 : U128, block1 : U128, block2 : U128, block3 : U128, block4 : U128, block5 : U128, block6 : U128, block7 : U128} ## Represents the digest of some data produced by the SHA-256 cryptographic hashing function as an opaque type. @@ -25,10 +25,10 @@ Digest256 := { firstHalf : U128, secondHalf : U128 } implements [Eq] ## Returns an empty SHA-256 hasher. emptySha256 : {} -> Sha256 -## Adds bytes of data to be hashed by a SHA-256 hasher.. +## Adds bytes of data to be hashed by a SHA-256 hasher. sha256AddBytes : Sha256, List U8 -> Sha256 -## Returns the digest of the cryptographic hashing function represented by a SHA-256 hasher.. +## Returns the digest of the cryptographic hashing function represented by a SHA-256 hasher. sha256Digest : Sha256 -> Digest256 ## Applies the SHA-256 cryptographic hashing function to some bytes. From 8c47be12fb49123c11890f8d1f6735aae2c79b68 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 9 Dec 2024 09:18:50 +0000 Subject: [PATCH 46/57] removed unwrap from rust glue --- crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 03d77df2fec..3872dd3879a 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1449,12 +1449,9 @@ pub(crate) fn run_low_level<'a, 'ctx>( // Crypto.sha256Digest : Sha256 -> Digest256 arguments!(sha); - call_bitcode_fn_fixing_for_convention( + call_bitcode_fn( env, - layout_interner, - env.module.get_struct_type("crypto.Digest256").unwrap(), &[sha], - layout, bitcode::CRYPTO_SHA256_DIGEST, ) } From e614ffa49a763c5395dd87ae3c1145e49e291fa3 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 9 Dec 2024 09:29:04 +0000 Subject: [PATCH 47/57] switched hasher ot be passed as owned --- crates/compiler/mono/src/inc_dec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/compiler/mono/src/inc_dec.rs b/crates/compiler/mono/src/inc_dec.rs index 5f842d3bfd0..6c52327d027 100644 --- a/crates/compiler/mono/src/inc_dec.rs +++ b/crates/compiler/mono/src/inc_dec.rs @@ -1257,7 +1257,7 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] { ListReleaseExcessCapacity => &[OWNED], StrReleaseExcessCapacity => &[OWNED], CryptoEmptySha256 => &[IRRELEVANT], - CryptoSha256AddBytes => &[OWNED, BORROWED], + CryptoSha256AddBytes => &[OWNED, OWNED], CryptoSha256Digest => &[BORROWED], ListIncref => &[OWNED], ListDecref => &[OWNED], From 19ffe1acc4cd243280e67a84730574bde473ac24 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 9 Dec 2024 09:45:00 +0000 Subject: [PATCH 48/57] cargo fmt --- crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 3872dd3879a..6181e022b46 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1449,11 +1449,7 @@ pub(crate) fn run_low_level<'a, 'ctx>( // Crypto.sha256Digest : Sha256 -> Digest256 arguments!(sha); - call_bitcode_fn( - env, - &[sha], - bitcode::CRYPTO_SHA256_DIGEST, - ) + call_bitcode_fn(env, &[sha], bitcode::CRYPTO_SHA256_DIGEST) } ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { From 60379ce3cb35b269335cafbe27c83d33f9fdc64a Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 9 Dec 2024 11:32:38 +0000 Subject: [PATCH 49/57] roc format --- crates/compiler/builtins/roc/Crypto.roc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/compiler/builtins/roc/Crypto.roc b/crates/compiler/builtins/roc/Crypto.roc index f494a78ec01..719420325a7 100644 --- a/crates/compiler/builtins/roc/Crypto.roc +++ b/crates/compiler/builtins/roc/Crypto.roc @@ -15,7 +15,7 @@ import Result import Str ## Represents the state of a SHA-256 cryptographic hashing function, after some (or no) data has been added to the hash. -Sha256 := { block0 : U128, block1 : U128, block2 : U128, block3 : U128, block4 : U128, block5 : U128, block6 : U128, block7 : U128} +Sha256 := { block0 : U128, block1 : U128, block2 : U128, block3 : U128, block4 : U128, block5 : U128, block6 : U128, block7 : U128 } ## Represents the digest of some data produced by the SHA-256 cryptographic hashing function as an opaque type. From b8f429feea03920408db6cf300e5cb98b57904ab Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 9 Dec 2024 17:24:52 +0000 Subject: [PATCH 50/57] back to pointer version --- .../compiler/builtins/bitcode/src/crypto.zig | 41 +++++++++++-------- crates/compiler/builtins/roc/Crypto.roc | 8 ++-- crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 9 +++- crates/compiler/mono/src/inc_dec.rs | 2 +- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index cded331af5a..1d8c04d2ccd 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -7,45 +7,50 @@ const utils = @import("utils.zig"); const testing = std.testing; const Sha256 = extern struct { - data: [@sizeOf(sha2.Sha256)]u8 align(@alignOf(sha2.Sha256)), + location: *sha2.Sha256, }; -test "Sha256 size and alignment" { - try std.testing.expectEqual(@sizeOf(Sha256), 128); - try std.testing.expectEqual(@alignOf(Sha256), 16); +fn create(comptime T: type) *T { + //test_roc_alloc ignores alignment + if (builtin.is_test) { + return std.testing.allocator.create(T) catch unreachable; + } + return @alignCast(@ptrCast(utils.allocateWithRefcount(@sizeOf(sha2.Sha256), @alignOf(sha2.Sha256), false))); } pub fn emptySha256() callconv(.C) Sha256 { - var sha: Sha256 = undefined; - const ptr: *sha2.Sha256 = @constCast(@ptrCast(&sha)); - ptr.* = sha2.Sha256.init(.{}); - return sha; + const location: *sha2.Sha256 = create(sha2.Sha256); + location.* = sha2.Sha256.init(.{}); + return Sha256{ + .location = location, + }; } test "emptySha256" { const empty_sha = emptySha256(); - const ptr: *sha2.Sha256 = @constCast(@ptrCast(&empty_sha)); - const empty_hash = ptr.*.peek(); + defer std.testing.allocator.destroy(empty_sha.location); + const empty_hash = empty_sha.location.*.peek(); try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", empty_hash[0..empty_hash.len])); } pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { - var out = sha; - const out_ptr: *sha2.Sha256 = @constCast(@ptrCast(&out)); + const out = emptySha256(); + out.location.* = sha.location.*; if (data.bytes) |bytes| { const byteSlice: []u8 = bytes[0..data.length]; - out_ptr.*.update(byteSlice); + out.location.*.update(byteSlice); } return out; } test "sha256AddBytes" { const empty_sha = emptySha256(); + defer std.testing.allocator.destroy(empty_sha.location); const abc = list.RocList.fromSlice(u8, "abc", false); defer abc.decref(@alignOf(u8), @sizeOf(u8), false, rcNone); const abc_sha = sha256AddBytes(empty_sha, abc); - const abc_ptr: *sha2.Sha256 = @constCast(@ptrCast(&abc_sha)); - const abc_hash = abc_ptr.*.peek(); + defer std.testing.allocator.destroy(abc_sha.location); + const abc_hash = abc_sha.location.*.peek(); try std.testing.expect(sameBytesAsHex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", abc_hash[0..abc_hash.len])); } @@ -55,19 +60,19 @@ pub const Digest256 = extern struct { }; pub fn sha256Digest(sha: Sha256) callconv(.C) Digest256 { - const ptr: *sha2.Sha256 = @constCast(@ptrCast(&sha)); - return @bitCast(ptr.*.peek()); + return @bitCast(sha.location.*.peek()); } test "sha256Digest" { const empty_sha = emptySha256(); + defer std.testing.allocator.destroy(empty_sha.location); const digest = sha256Digest(empty_sha); const first_half_bytes: [16]u8 = @bitCast(digest.first_half); const second_half_bytes: [16]u8 = @bitCast(digest.second_half); try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb924", first_half_bytes[0..first_half_bytes.len])); try std.testing.expect(sameBytesAsHex("27ae41e4649b934ca495991b7852b855", second_half_bytes[0..second_half_bytes.len])); } -// ----------------test utilities ------------------------ +//----------------test utilities ------------------------ fn rcNone(_: ?[*]u8) callconv(.C) void {} fn sameBytesAsHex(comptime expected_hex: [:0]const u8, input: []const u8) bool { diff --git a/crates/compiler/builtins/roc/Crypto.roc b/crates/compiler/builtins/roc/Crypto.roc index 719420325a7..32d47361486 100644 --- a/crates/compiler/builtins/roc/Crypto.roc +++ b/crates/compiler/builtins/roc/Crypto.roc @@ -10,12 +10,12 @@ module [ import Bool exposing [Eq] import List -import Num exposing [U8, U128] +import Num exposing [U8, U64, U128] import Result import Str ## Represents the state of a SHA-256 cryptographic hashing function, after some (or no) data has been added to the hash. -Sha256 := { block0 : U128, block1 : U128, block2 : U128, block3 : U128, block4 : U128, block5 : U128, block6 : U128, block7 : U128 } +Sha256 := { location : U64 } ## Represents the digest of some data produced by the SHA-256 cryptographic hashing function as an opaque type. @@ -25,10 +25,10 @@ Digest256 := { firstHalf : U128, secondHalf : U128 } implements [Eq] ## Returns an empty SHA-256 hasher. emptySha256 : {} -> Sha256 -## Adds bytes of data to be hashed by a SHA-256 hasher. +## Adds bytes of data to be hashed by a SHA-256 hasher.. sha256AddBytes : Sha256, List U8 -> Sha256 -## Returns the digest of the cryptographic hashing function represented by a SHA-256 hasher. +## Returns the digest of the cryptographic hashing function represented by a SHA-256 hasher.. sha256Digest : Sha256 -> Digest256 ## Applies the SHA-256 cryptographic hashing function to some bytes. diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 6181e022b46..03d77df2fec 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1449,7 +1449,14 @@ pub(crate) fn run_low_level<'a, 'ctx>( // Crypto.sha256Digest : Sha256 -> Digest256 arguments!(sha); - call_bitcode_fn(env, &[sha], bitcode::CRYPTO_SHA256_DIGEST) + call_bitcode_fn_fixing_for_convention( + env, + layout_interner, + env.module.get_struct_type("crypto.Digest256").unwrap(), + &[sha], + layout, + bitcode::CRYPTO_SHA256_DIGEST, + ) } ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { diff --git a/crates/compiler/mono/src/inc_dec.rs b/crates/compiler/mono/src/inc_dec.rs index 6c52327d027..5f842d3bfd0 100644 --- a/crates/compiler/mono/src/inc_dec.rs +++ b/crates/compiler/mono/src/inc_dec.rs @@ -1257,7 +1257,7 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] { ListReleaseExcessCapacity => &[OWNED], StrReleaseExcessCapacity => &[OWNED], CryptoEmptySha256 => &[IRRELEVANT], - CryptoSha256AddBytes => &[OWNED, OWNED], + CryptoSha256AddBytes => &[OWNED, BORROWED], CryptoSha256Digest => &[BORROWED], ListIncref => &[OWNED], ListDecref => &[OWNED], From bbc7b5852d5cb8442d102d5165567e98fe58c0c1 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 9 Dec 2024 18:05:55 +0000 Subject: [PATCH 51/57] changes produced in test suite --- .../generalization_among_large_recursive_group.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt b/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt index cb79cd47420..e5a08b57eb3 100644 --- a/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt +++ b/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt @@ -3,22 +3,22 @@ app "test" provides [main] to "./platform" f = \{} -> -#^{-1} <3219><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3228>{}]<80>* +#^{-1} <3238><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3247>{}]<80>* when g {} is -# ^ <3209><3228>{} -<3217>[[g(2)]]<125>-> <71>[Ok <3228>{}]<104>* +# ^ <3228><3247>{} -<3236>[[g(2)]]<125>-> <71>[Ok <3247>{}]<104>* _ -> Ok {} g = \{} -> -#^{-1} <3209><3228>{} -<3217>[[g(2)]]<125>-> <71>[Ok <3228>{}]<104>* +#^{-1} <3228><3247>{} -<3236>[[g(2)]]<125>-> <71>[Ok <3247>{}]<104>* when h {} is -# ^ <3214><3228>{} -<3222>[[h(3)]]<125>-> <95>[Ok <3228>{}]<128>* +# ^ <3233><3247>{} -<3241>[[h(3)]]<125>-> <95>[Ok <3247>{}]<128>* _ -> Ok {} h = \{} -> -#^{-1} <3214><3228>{} -<3222>[[h(3)]]<125>-> <95>[Ok <3228>{}]<128>* +#^{-1} <3233><3247>{} -<3241>[[h(3)]]<125>-> <95>[Ok <3247>{}]<128>* when f {} is -# ^ <3219><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3228>{}]<80>* +# ^ <3238><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3247>{}]<80>* _ -> Ok {} main = f {} -# ^ <3230><138>{} -<141>[[f(1)]]<143>-> <137>[Ok <3228>{}]<3229>w_a +# ^ <3249><138>{} -<141>[[f(1)]]<143>-> <137>[Ok <3247>{}]<3248>w_a From 40eb6fc20d80b7901ba0efed232d7c0b792f7044 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 9 Dec 2024 19:15:43 +0000 Subject: [PATCH 52/57] removes unwrap --- crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 03d77df2fec..871c0c149d1 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1449,14 +1449,7 @@ pub(crate) fn run_low_level<'a, 'ctx>( // Crypto.sha256Digest : Sha256 -> Digest256 arguments!(sha); - call_bitcode_fn_fixing_for_convention( - env, - layout_interner, - env.module.get_struct_type("crypto.Digest256").unwrap(), - &[sha], - layout, - bitcode::CRYPTO_SHA256_DIGEST, - ) + call_bitcode_fn(env, &[sha], bitcode::CRYPTO_SHA256_DIGEST,) } ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { From 83ae431c009c7626a6eed3941370f707f7745b35 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Mon, 9 Dec 2024 23:10:00 +0000 Subject: [PATCH 53/57] both versions of zig --- .../compiler/builtins/bitcode/src/crypto.zig | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index 1d8c04d2ccd..6d8370b0628 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -10,6 +10,18 @@ const Sha256 = extern struct { location: *sha2.Sha256, }; +const Dummy = extern struct { + data: [@sizeOf(sha2.Sha256)]u8 align(@alignOf(sha2.Sha256)), + const len = @sizeOf(sha2.Sha256); +}; + +test "Dummy size and alignment" { + try std.testing.expectEqual(@sizeOf(Dummy), 128); + try std.testing.expectEqual(@sizeOf(Dummy), @sizeOf(sha2.Sha256)); + try std.testing.expectEqual(@alignOf(Dummy), 16); + try std.testing.expectEqual(@alignOf(Dummy), @alignOf(sha2.Sha256)); +} + fn create(comptime T: type) *T { //test_roc_alloc ignores alignment if (builtin.is_test) { @@ -33,6 +45,20 @@ test "emptySha256" { try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", empty_hash[0..empty_hash.len])); } +pub fn emptyDummy() callconv(.C) Dummy { + var sha: Dummy = undefined; + const ptr: *sha2.Sha256 = @ptrCast(sha.data[0..Dummy.len]); + ptr.* = sha2.Sha256.init(.{}); + return sha; +} + +test "emptyDummy" { + const empty_sha = emptyDummy(); + const ptr: *sha2.Sha256 = @constCast(@ptrCast(&empty_sha)); + const empty_hash = ptr.*.peek(); + try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", empty_hash[0..empty_hash.len])); +} + pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { const out = emptySha256(); out.location.* = sha.location.*; @@ -54,6 +80,26 @@ test "sha256AddBytes" { try std.testing.expect(sameBytesAsHex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", abc_hash[0..abc_hash.len])); } +pub fn dummyAddBytes(sha: Dummy, data: list.RocList) callconv(.C) Dummy { + var out = sha; + const out_ptr: *sha2.Sha256 = @ptrCast(out.data[0..Dummy.len]); + if (data.bytes) |bytes| { + const byteSlice: []u8 = bytes[0..data.length]; + out_ptr.*.update(byteSlice); + } + return out; +} + +test "dummy256AddBytes" { + const empty_sha = emptyDummy(); + const abc = list.RocList.fromSlice(u8, "abc", false); + defer abc.decref(@alignOf(u8), @sizeOf(u8), false, rcNone); + const abc_sha = dummyAddBytes(empty_sha, abc); + const abc_ptr: *sha2.Sha256 = @constCast(@ptrCast(&abc_sha)); + const abc_hash = abc_ptr.*.peek(); + try std.testing.expect(sameBytesAsHex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", abc_hash[0..abc_hash.len])); +} + pub const Digest256 = extern struct { first_half: u128, second_half: u128, @@ -72,6 +118,21 @@ test "sha256Digest" { try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb924", first_half_bytes[0..first_half_bytes.len])); try std.testing.expect(sameBytesAsHex("27ae41e4649b934ca495991b7852b855", second_half_bytes[0..second_half_bytes.len])); } + +pub fn dummyDigest(sha: Dummy) callconv(.C) Digest256 { + const ptr: *const sha2.Sha256 = @ptrCast(sha.data[0..Dummy.len]); + return @bitCast(ptr.*.peek()); +} + +test "dummy256Digest" { + const empty_sha = emptyDummy(); + const digest = dummyDigest(empty_sha); + const first_half_bytes: [16]u8 = @bitCast(digest.first_half); + const second_half_bytes: [16]u8 = @bitCast(digest.second_half); + try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb924", first_half_bytes[0..first_half_bytes.len])); + try std.testing.expect(sameBytesAsHex("27ae41e4649b934ca495991b7852b855", second_half_bytes[0..second_half_bytes.len])); +} + //----------------test utilities ------------------------ fn rcNone(_: ?[*]u8) callconv(.C) void {} From f80f00973bfc3715d9e5cf9838968617bc48f03e Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 10 Dec 2024 16:21:54 +0000 Subject: [PATCH 54/57] WIP threading through in place version --- crates/compiler/builtins/bitcode/src/main.zig | 1 + crates/compiler/builtins/roc/Crypto.roc | 11 +++++++++++ crates/compiler/builtins/src/bitcode.rs | 1 + crates/compiler/can/src/builtins.rs | 1 + crates/compiler/gen_dev/src/lib.rs | 5 +++++ crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 4 ++++ crates/compiler/gen_wasm/src/low_level.rs | 1 + crates/compiler/module/src/low_level.rs | 2 ++ crates/compiler/module/src/symbol.rs | 2 ++ crates/compiler/mono/src/drop_specialization.rs | 2 +- crates/compiler/mono/src/inc_dec.rs | 1 + .../generalization_among_large_recursive_group.txt | 14 +++++++------- 12 files changed, 37 insertions(+), 8 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index 39cf60bc550..268beeccaf0 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -14,6 +14,7 @@ const STR = "str"; const crypto = @import("crypto.zig"); comptime { exportCryptoFn(crypto.emptySha256, "emptySha256"); + exportCryptoFn(crypto.emptyDummy, "emptyDummy"); exportCryptoFn(crypto.sha256AddBytes, "sha256AddBytes"); exportCryptoFn(crypto.sha256Digest, "sha256Digest"); } diff --git a/crates/compiler/builtins/roc/Crypto.roc b/crates/compiler/builtins/roc/Crypto.roc index 32d47361486..4dbad071e71 100644 --- a/crates/compiler/builtins/roc/Crypto.roc +++ b/crates/compiler/builtins/roc/Crypto.roc @@ -1,11 +1,13 @@ module [ emptySha256, + emptyDummy, sha256AddBytes, sha256Digest, hashSha256, digest256ToBytes, Sha256, Digest256, + Dummy, ] import Bool exposing [Eq] @@ -17,6 +19,8 @@ import Str ## Represents the state of a SHA-256 cryptographic hashing function, after some (or no) data has been added to the hash. Sha256 := { location : U64 } +Dummy := { block0 : U128, block1 : U128, block2 : U128, block3 : U128, block4 : U128, block5 : U128, block6 : U128, block7 : U128 } implements [Eq] + ## Represents the digest of some data produced by the SHA-256 cryptographic hashing function as an opaque type. ## `Digest256` implements the `Eq` ability. @@ -25,6 +29,13 @@ Digest256 := { firstHalf : U128, secondHalf : U128 } implements [Eq] ## Returns an empty SHA-256 hasher. emptySha256 : {} -> Sha256 +emptyDummy : {} -> Dummy + +expect + firstEmpty = emptyDummy {} + secondEmpty = emptyDummy {} + firstEmpty == secondEmpty + ## Adds bytes of data to be hashed by a SHA-256 hasher.. sha256AddBytes : Sha256, List U8 -> Sha256 diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index a6e10060d9a..af176db2c93 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -418,6 +418,7 @@ pub const DEC_FLOOR: IntrinsicName = int_intrinsic!("roc_builtins.dec.floor"); pub const DEC_CEILING: IntrinsicName = int_intrinsic!("roc_builtins.dec.ceiling"); pub const CRYPTO_EMPTY_SHA256: &str = "roc_builtins.crypto.emptySha256"; +pub const CRYPTO_EMPTY_DUMMY: &str = "roc_builtins.crypto.emptyDummy"; pub const CRYPTO_SHA256_ADD_BYTES: &str = "roc_builtins.crypto.sha256AddBytes"; pub const CRYPTO_SHA256_DIGEST: &str = "roc_builtins.crypto.sha256Digest"; diff --git a/crates/compiler/can/src/builtins.rs b/crates/compiler/can/src/builtins.rs index 7f63a7d2136..fa7fef5de68 100644 --- a/crates/compiler/can/src/builtins.rs +++ b/crates/compiler/can/src/builtins.rs @@ -211,6 +211,7 @@ map_symbol_to_lowlevel_and_arity! { NumF64FromParts; NUM_F64_FROM_PARTS; 1, CryptoEmptySha256; CRYPTO_EMPTY_SHA_256; 1, + CryptoEmptyDummy; CRYPTO_EMPTY_DUMMY; 1, CryptoSha256AddBytes; CRYPTO_SHA256_ADD_BYTES; 2, CryptoSha256Digest; CRYPTO_SHA256_DIGEST; 1, diff --git a/crates/compiler/gen_dev/src/lib.rs b/crates/compiler/gen_dev/src/lib.rs index 03b5f423508..b2087711804 100644 --- a/crates/compiler/gen_dev/src/lib.rs +++ b/crates/compiler/gen_dev/src/lib.rs @@ -2221,6 +2221,11 @@ trait Backend<'a> { self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } + LowLevel::CryptoEmptyDummy => { + let intrinsic = bitcode::CRYPTO_EMPTY_DUMMY.to_string(); + self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); + } + LowLevel::CryptoSha256AddBytes => { let intrinsic = bitcode::CRYPTO_SHA256_ADD_BYTES.to_string(); self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 871c0c149d1..7af1293789a 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1431,7 +1431,11 @@ pub(crate) fn run_low_level<'a, 'ctx>( call_bitcode_fn(env, &[], bitcode::UTILS_DICT_PSEUDO_SEED) } + CryptoEmptySha256 => call_bitcode_fn(env, &[], bitcode::CRYPTO_EMPTY_SHA256), + + CryptoEmptyDummy => call_bitcode_fn(env, &[], bitcode::CRYPTO_EMPTY_DUMMY), + CryptoSha256AddBytes => { // Crypto.sha256AddBytes : Sha256, List U8 -> Sha256 arguments!(sha, data); diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index 5fa98dbf949..14ed6e5e747 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2165,6 +2165,7 @@ impl<'a> LowLevelCall<'a> { NumF64FromParts => self.load_args_and_call_zig(backend, bitcode::NUM_F64_FROM_PARTS), // Crypto CryptoEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPTO_EMPTY_SHA256), + CryptoEmptyDummy => self.load_args_and_call_zig(backend, bitcode::CRYPTO_EMPTY_DUMMY), CryptoSha256AddBytes => { self.load_args_and_call_zig(backend, bitcode::CRYPTO_SHA256_ADD_BYTES) } diff --git a/crates/compiler/module/src/low_level.rs b/crates/compiler/module/src/low_level.rs index 775d58fd680..afcf282f098 100644 --- a/crates/compiler/module/src/low_level.rs +++ b/crates/compiler/module/src/low_level.rs @@ -130,6 +130,7 @@ pub enum LowLevel { LongJmp, SetLongJmpBuffer, CryptoEmptySha256, + CryptoEmptyDummy, CryptoSha256AddBytes, CryptoSha256Digest, } @@ -352,6 +353,7 @@ map_symbol_to_lowlevel! { Unreachable <= LIST_UNREACHABLE; DictPseudoSeed <= DICT_PSEUDO_SEED; CryptoEmptySha256 <= CRYPTO_EMPTY_SHA_256; + CryptoEmptyDummy <= CRYPTO_EMPTY_DUMMY; CryptoSha256AddBytes <= CRYPTO_SHA256_ADD_BYTES; CryptoSha256Digest <= CRYPTO_SHA256_DIGEST; } diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index 09e078395f3..1b91ad26e6d 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1740,6 +1740,8 @@ define_builtins! { 3 CRYPTO_SHA256_ADD_BYTES: "sha256AddBytes" 4 CRYPTO_SHA256_DIGEST: "sha256Digest" 5 CRYPTO_HASH_SHA_256: "hashSha256" + 6 CRYPTO_DUMMY: "Dummy" exposed_type=true + 7 CRYPTO_EMPTY_DUMMY: "emptyDummy" } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) diff --git a/crates/compiler/mono/src/drop_specialization.rs b/crates/compiler/mono/src/drop_specialization.rs index f8fb0d1cd87..33eb0ae1c91 100644 --- a/crates/compiler/mono/src/drop_specialization.rs +++ b/crates/compiler/mono/src/drop_specialization.rs @@ -1609,7 +1609,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC { ListIsUnique => RC::Rc, ListClone => RC::Rc, - CryptoEmptySha256 | CryptoSha256AddBytes | CryptoSha256Digest => RC::NoRc, + CryptoEmptySha256 | CryptoEmptyDummy| CryptoSha256AddBytes | CryptoSha256Digest => RC::NoRc, BoxExpr | UnboxExpr => { unreachable!("These lowlevel operations are turned into mono Expr's") diff --git a/crates/compiler/mono/src/inc_dec.rs b/crates/compiler/mono/src/inc_dec.rs index 5f842d3bfd0..5f200c3cd1b 100644 --- a/crates/compiler/mono/src/inc_dec.rs +++ b/crates/compiler/mono/src/inc_dec.rs @@ -1257,6 +1257,7 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] { ListReleaseExcessCapacity => &[OWNED], StrReleaseExcessCapacity => &[OWNED], CryptoEmptySha256 => &[IRRELEVANT], + CryptoEmptyDummy => &[IRRELEVANT], CryptoSha256AddBytes => &[OWNED, BORROWED], CryptoSha256Digest => &[BORROWED], ListIncref => &[OWNED], diff --git a/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt b/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt index e5a08b57eb3..676d0e3bbae 100644 --- a/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt +++ b/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt @@ -3,22 +3,22 @@ app "test" provides [main] to "./platform" f = \{} -> -#^{-1} <3238><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3247>{}]<80>* +#^{-1} <3287><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3296>{}]<80>* when g {} is -# ^ <3228><3247>{} -<3236>[[g(2)]]<125>-> <71>[Ok <3247>{}]<104>* +# ^ <3277><3296>{} -<3285>[[g(2)]]<125>-> <71>[Ok <3296>{}]<104>* _ -> Ok {} g = \{} -> -#^{-1} <3228><3247>{} -<3236>[[g(2)]]<125>-> <71>[Ok <3247>{}]<104>* +#^{-1} <3277><3296>{} -<3285>[[g(2)]]<125>-> <71>[Ok <3296>{}]<104>* when h {} is -# ^ <3233><3247>{} -<3241>[[h(3)]]<125>-> <95>[Ok <3247>{}]<128>* +# ^ <3282><3296>{} -<3290>[[h(3)]]<125>-> <95>[Ok <3296>{}]<128>* _ -> Ok {} h = \{} -> -#^{-1} <3233><3247>{} -<3241>[[h(3)]]<125>-> <95>[Ok <3247>{}]<128>* +#^{-1} <3282><3296>{} -<3290>[[h(3)]]<125>-> <95>[Ok <3296>{}]<128>* when f {} is -# ^ <3238><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3247>{}]<80>* +# ^ <3287><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3296>{}]<80>* _ -> Ok {} main = f {} -# ^ <3249><138>{} -<141>[[f(1)]]<143>-> <137>[Ok <3247>{}]<3248>w_a +# ^ <3298><138>{} -<141>[[f(1)]]<143>-> <137>[Ok <3296>{}]<3297>w_a From bed0497c42e162804c6fbe57673d4476b90e7f7c Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 10 Dec 2024 16:58:50 +0000 Subject: [PATCH 55/57] re added call with unwrap of struct type --- crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 7af1293789a..734a91fcc3b 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1449,11 +1449,19 @@ pub(crate) fn run_low_level<'a, 'ctx>( bitcode::CRYPTO_SHA256_ADD_BYTES, ) } + CryptoSha256Digest => { // Crypto.sha256Digest : Sha256 -> Digest256 arguments!(sha); - call_bitcode_fn(env, &[sha], bitcode::CRYPTO_SHA256_DIGEST,) + call_bitcode_fn_fixing_for_convention( + env, + layout_interner, + env.module.get_struct_type("crypto.Digest256").unwrap(), + &[sha], + layout, + bitcode::CRYPTO_SHA256_DIGEST, + ) } ListIncref | ListDecref | SetJmp | LongJmp | SetLongJmpBuffer => { From cc9be6d45569c1c92fa6cf2459be9baab2565417 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 10 Dec 2024 21:52:38 +0000 Subject: [PATCH 56/57] more "dummy" experiment nonsense --- .../compiler/builtins/bitcode/src/crypto.zig | 38 +++++++++++++------ crates/compiler/builtins/roc/Crypto.roc | 7 ++-- crates/compiler/module/src/symbol.rs | 1 + 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index 6d8370b0628..dec5f57c08c 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -10,17 +10,30 @@ const Sha256 = extern struct { location: *sha2.Sha256, }; +const ThirtyTwoBytes = extern struct { + first: u128, + second: u128, + const zero = .{ .first = 0, .second = 0 }; +}; + const Dummy = extern struct { - data: [@sizeOf(sha2.Sha256)]u8 align(@alignOf(sha2.Sha256)), - const len = @sizeOf(sha2.Sha256); + first: ThirtyTwoBytes, + second: ThirtyTwoBytes, + const zero = .{ .first = ThirtyTwoBytes.zero, .second = ThirtyTwoBytes.zero }; }; -test "Dummy size and alignment" { - try std.testing.expectEqual(@sizeOf(Dummy), 128); - try std.testing.expectEqual(@sizeOf(Dummy), @sizeOf(sha2.Sha256)); - try std.testing.expectEqual(@alignOf(Dummy), 16); - try std.testing.expectEqual(@alignOf(Dummy), @alignOf(sha2.Sha256)); -} +// const Dummy = extern struct { +// first: SixtyFourBytes, +// second: SixtyFourBytes, +// const zero = .{ .first = SixtyFourBytes.zero, .second = SixtyFourBytes.zero }; +// }; + +// test "Dummy size and alignment" { +// try std.testing.expectEqual(@sizeOf(Dummy), 128); +// try std.testing.expectEqual(@sizeOf(Dummy), @sizeOf(sha2.Sha256)); +// try std.testing.expectEqual(@alignOf(Dummy), 16); +// try std.testing.expectEqual(@alignOf(Dummy), @alignOf(sha2.Sha256)); +// } fn create(comptime T: type) *T { //test_roc_alloc ignores alignment @@ -46,10 +59,11 @@ test "emptySha256" { } pub fn emptyDummy() callconv(.C) Dummy { - var sha: Dummy = undefined; - const ptr: *sha2.Sha256 = @ptrCast(sha.data[0..Dummy.len]); - ptr.* = sha2.Sha256.init(.{}); - return sha; + // var sha: Dummy = undefined; + // const ptr: *sha2.Sha256 = @ptrCast(sha.data[0..Dummy.len]); + // ptr.* = sha2.Sha256.init(.{}); + // return sha; + return Dummy.zero; } test "emptyDummy" { diff --git a/crates/compiler/builtins/roc/Crypto.roc b/crates/compiler/builtins/roc/Crypto.roc index 4dbad071e71..c6929530464 100644 --- a/crates/compiler/builtins/roc/Crypto.roc +++ b/crates/compiler/builtins/roc/Crypto.roc @@ -18,9 +18,10 @@ import Str ## Represents the state of a SHA-256 cryptographic hashing function, after some (or no) data has been added to the hash. Sha256 := { location : U64 } - -Dummy := { block0 : U128, block1 : U128, block2 : U128, block3 : U128, block4 : U128, block5 : U128, block6 : U128, block7 : U128 } implements [Eq] - +ThirtyTwoBytes := { first : U128, second : U128 } implements [Eq] +##SixtyFourBytes := { first : ThirtyTwoBytes, second : ThirtyTwoBytes } implements [Eq] +## Dummy := { block0 : U128, block1 : U128, block2 : U128, block3 : U128, block4 : U128, block5 : U128, block6 : U128, block7 : U128 } implements [Eq] +Dummy := { first : ThirtyTwoBytes, second : ThirtyTwoBytes } implements [Eq] ## Represents the digest of some data produced by the SHA-256 cryptographic hashing function as an opaque type. ## `Digest256` implements the `Eq` ability. diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index 1b91ad26e6d..ff58fa975c1 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1742,6 +1742,7 @@ define_builtins! { 5 CRYPTO_HASH_SHA_256: "hashSha256" 6 CRYPTO_DUMMY: "Dummy" exposed_type=true 7 CRYPTO_EMPTY_DUMMY: "emptyDummy" + 8 CRYPTO_THIRTY_TWO_BYTES: "ThirtyTwoBytes" exposed_type=true } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) From 1f30fe05e1f617605f8c17c92c2d1ac5290cc667 Mon Sep 17 00:00:00 2001 From: Mattthew Heath Date: Tue, 10 Dec 2024 22:05:44 +0000 Subject: [PATCH 57/57] roll back to pointer only --- .../compiler/builtins/bitcode/src/crypto.zig | 75 ------------------- crates/compiler/builtins/bitcode/src/main.zig | 1 - crates/compiler/builtins/roc/Crypto.roc | 14 +--- crates/compiler/builtins/src/bitcode.rs | 1 - crates/compiler/can/src/builtins.rs | 1 - crates/compiler/gen_dev/src/lib.rs | 5 -- crates/compiler/gen_llvm/src/llvm/lowlevel.rs | 5 -- crates/compiler/gen_wasm/src/low_level.rs | 1 - crates/compiler/module/src/low_level.rs | 2 - crates/compiler/module/src/symbol.rs | 3 - .../compiler/mono/src/drop_specialization.rs | 2 +- crates/compiler/mono/src/inc_dec.rs | 1 - ...ralization_among_large_recursive_group.txt | 14 ++-- 13 files changed, 9 insertions(+), 116 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/crypto.zig b/crates/compiler/builtins/bitcode/src/crypto.zig index dec5f57c08c..1d8c04d2ccd 100644 --- a/crates/compiler/builtins/bitcode/src/crypto.zig +++ b/crates/compiler/builtins/bitcode/src/crypto.zig @@ -10,31 +10,6 @@ const Sha256 = extern struct { location: *sha2.Sha256, }; -const ThirtyTwoBytes = extern struct { - first: u128, - second: u128, - const zero = .{ .first = 0, .second = 0 }; -}; - -const Dummy = extern struct { - first: ThirtyTwoBytes, - second: ThirtyTwoBytes, - const zero = .{ .first = ThirtyTwoBytes.zero, .second = ThirtyTwoBytes.zero }; -}; - -// const Dummy = extern struct { -// first: SixtyFourBytes, -// second: SixtyFourBytes, -// const zero = .{ .first = SixtyFourBytes.zero, .second = SixtyFourBytes.zero }; -// }; - -// test "Dummy size and alignment" { -// try std.testing.expectEqual(@sizeOf(Dummy), 128); -// try std.testing.expectEqual(@sizeOf(Dummy), @sizeOf(sha2.Sha256)); -// try std.testing.expectEqual(@alignOf(Dummy), 16); -// try std.testing.expectEqual(@alignOf(Dummy), @alignOf(sha2.Sha256)); -// } - fn create(comptime T: type) *T { //test_roc_alloc ignores alignment if (builtin.is_test) { @@ -58,21 +33,6 @@ test "emptySha256" { try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", empty_hash[0..empty_hash.len])); } -pub fn emptyDummy() callconv(.C) Dummy { - // var sha: Dummy = undefined; - // const ptr: *sha2.Sha256 = @ptrCast(sha.data[0..Dummy.len]); - // ptr.* = sha2.Sha256.init(.{}); - // return sha; - return Dummy.zero; -} - -test "emptyDummy" { - const empty_sha = emptyDummy(); - const ptr: *sha2.Sha256 = @constCast(@ptrCast(&empty_sha)); - const empty_hash = ptr.*.peek(); - try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", empty_hash[0..empty_hash.len])); -} - pub fn sha256AddBytes(sha: Sha256, data: list.RocList) callconv(.C) Sha256 { const out = emptySha256(); out.location.* = sha.location.*; @@ -94,26 +54,6 @@ test "sha256AddBytes" { try std.testing.expect(sameBytesAsHex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", abc_hash[0..abc_hash.len])); } -pub fn dummyAddBytes(sha: Dummy, data: list.RocList) callconv(.C) Dummy { - var out = sha; - const out_ptr: *sha2.Sha256 = @ptrCast(out.data[0..Dummy.len]); - if (data.bytes) |bytes| { - const byteSlice: []u8 = bytes[0..data.length]; - out_ptr.*.update(byteSlice); - } - return out; -} - -test "dummy256AddBytes" { - const empty_sha = emptyDummy(); - const abc = list.RocList.fromSlice(u8, "abc", false); - defer abc.decref(@alignOf(u8), @sizeOf(u8), false, rcNone); - const abc_sha = dummyAddBytes(empty_sha, abc); - const abc_ptr: *sha2.Sha256 = @constCast(@ptrCast(&abc_sha)); - const abc_hash = abc_ptr.*.peek(); - try std.testing.expect(sameBytesAsHex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", abc_hash[0..abc_hash.len])); -} - pub const Digest256 = extern struct { first_half: u128, second_half: u128, @@ -132,21 +72,6 @@ test "sha256Digest" { try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb924", first_half_bytes[0..first_half_bytes.len])); try std.testing.expect(sameBytesAsHex("27ae41e4649b934ca495991b7852b855", second_half_bytes[0..second_half_bytes.len])); } - -pub fn dummyDigest(sha: Dummy) callconv(.C) Digest256 { - const ptr: *const sha2.Sha256 = @ptrCast(sha.data[0..Dummy.len]); - return @bitCast(ptr.*.peek()); -} - -test "dummy256Digest" { - const empty_sha = emptyDummy(); - const digest = dummyDigest(empty_sha); - const first_half_bytes: [16]u8 = @bitCast(digest.first_half); - const second_half_bytes: [16]u8 = @bitCast(digest.second_half); - try std.testing.expect(sameBytesAsHex("e3b0c44298fc1c149afbf4c8996fb924", first_half_bytes[0..first_half_bytes.len])); - try std.testing.expect(sameBytesAsHex("27ae41e4649b934ca495991b7852b855", second_half_bytes[0..second_half_bytes.len])); -} - //----------------test utilities ------------------------ fn rcNone(_: ?[*]u8) callconv(.C) void {} diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index 268beeccaf0..39cf60bc550 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -14,7 +14,6 @@ const STR = "str"; const crypto = @import("crypto.zig"); comptime { exportCryptoFn(crypto.emptySha256, "emptySha256"); - exportCryptoFn(crypto.emptyDummy, "emptyDummy"); exportCryptoFn(crypto.sha256AddBytes, "sha256AddBytes"); exportCryptoFn(crypto.sha256Digest, "sha256Digest"); } diff --git a/crates/compiler/builtins/roc/Crypto.roc b/crates/compiler/builtins/roc/Crypto.roc index c6929530464..32d47361486 100644 --- a/crates/compiler/builtins/roc/Crypto.roc +++ b/crates/compiler/builtins/roc/Crypto.roc @@ -1,13 +1,11 @@ module [ emptySha256, - emptyDummy, sha256AddBytes, sha256Digest, hashSha256, digest256ToBytes, Sha256, Digest256, - Dummy, ] import Bool exposing [Eq] @@ -18,10 +16,7 @@ import Str ## Represents the state of a SHA-256 cryptographic hashing function, after some (or no) data has been added to the hash. Sha256 := { location : U64 } -ThirtyTwoBytes := { first : U128, second : U128 } implements [Eq] -##SixtyFourBytes := { first : ThirtyTwoBytes, second : ThirtyTwoBytes } implements [Eq] -## Dummy := { block0 : U128, block1 : U128, block2 : U128, block3 : U128, block4 : U128, block5 : U128, block6 : U128, block7 : U128 } implements [Eq] -Dummy := { first : ThirtyTwoBytes, second : ThirtyTwoBytes } implements [Eq] + ## Represents the digest of some data produced by the SHA-256 cryptographic hashing function as an opaque type. ## `Digest256` implements the `Eq` ability. @@ -30,13 +25,6 @@ Digest256 := { firstHalf : U128, secondHalf : U128 } implements [Eq] ## Returns an empty SHA-256 hasher. emptySha256 : {} -> Sha256 -emptyDummy : {} -> Dummy - -expect - firstEmpty = emptyDummy {} - secondEmpty = emptyDummy {} - firstEmpty == secondEmpty - ## Adds bytes of data to be hashed by a SHA-256 hasher.. sha256AddBytes : Sha256, List U8 -> Sha256 diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index af176db2c93..a6e10060d9a 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -418,7 +418,6 @@ pub const DEC_FLOOR: IntrinsicName = int_intrinsic!("roc_builtins.dec.floor"); pub const DEC_CEILING: IntrinsicName = int_intrinsic!("roc_builtins.dec.ceiling"); pub const CRYPTO_EMPTY_SHA256: &str = "roc_builtins.crypto.emptySha256"; -pub const CRYPTO_EMPTY_DUMMY: &str = "roc_builtins.crypto.emptyDummy"; pub const CRYPTO_SHA256_ADD_BYTES: &str = "roc_builtins.crypto.sha256AddBytes"; pub const CRYPTO_SHA256_DIGEST: &str = "roc_builtins.crypto.sha256Digest"; diff --git a/crates/compiler/can/src/builtins.rs b/crates/compiler/can/src/builtins.rs index fa7fef5de68..7f63a7d2136 100644 --- a/crates/compiler/can/src/builtins.rs +++ b/crates/compiler/can/src/builtins.rs @@ -211,7 +211,6 @@ map_symbol_to_lowlevel_and_arity! { NumF64FromParts; NUM_F64_FROM_PARTS; 1, CryptoEmptySha256; CRYPTO_EMPTY_SHA_256; 1, - CryptoEmptyDummy; CRYPTO_EMPTY_DUMMY; 1, CryptoSha256AddBytes; CRYPTO_SHA256_ADD_BYTES; 2, CryptoSha256Digest; CRYPTO_SHA256_DIGEST; 1, diff --git a/crates/compiler/gen_dev/src/lib.rs b/crates/compiler/gen_dev/src/lib.rs index b2087711804..03b5f423508 100644 --- a/crates/compiler/gen_dev/src/lib.rs +++ b/crates/compiler/gen_dev/src/lib.rs @@ -2221,11 +2221,6 @@ trait Backend<'a> { self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); } - LowLevel::CryptoEmptyDummy => { - let intrinsic = bitcode::CRYPTO_EMPTY_DUMMY.to_string(); - self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); - } - LowLevel::CryptoSha256AddBytes => { let intrinsic = bitcode::CRYPTO_SHA256_ADD_BYTES.to_string(); self.build_fn_call(sym, intrinsic, args, arg_layouts, ret_layout); diff --git a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs index 734a91fcc3b..03d77df2fec 100644 --- a/crates/compiler/gen_llvm/src/llvm/lowlevel.rs +++ b/crates/compiler/gen_llvm/src/llvm/lowlevel.rs @@ -1431,11 +1431,7 @@ pub(crate) fn run_low_level<'a, 'ctx>( call_bitcode_fn(env, &[], bitcode::UTILS_DICT_PSEUDO_SEED) } - CryptoEmptySha256 => call_bitcode_fn(env, &[], bitcode::CRYPTO_EMPTY_SHA256), - - CryptoEmptyDummy => call_bitcode_fn(env, &[], bitcode::CRYPTO_EMPTY_DUMMY), - CryptoSha256AddBytes => { // Crypto.sha256AddBytes : Sha256, List U8 -> Sha256 arguments!(sha, data); @@ -1449,7 +1445,6 @@ pub(crate) fn run_low_level<'a, 'ctx>( bitcode::CRYPTO_SHA256_ADD_BYTES, ) } - CryptoSha256Digest => { // Crypto.sha256Digest : Sha256 -> Digest256 arguments!(sha); diff --git a/crates/compiler/gen_wasm/src/low_level.rs b/crates/compiler/gen_wasm/src/low_level.rs index 14ed6e5e747..5fa98dbf949 100644 --- a/crates/compiler/gen_wasm/src/low_level.rs +++ b/crates/compiler/gen_wasm/src/low_level.rs @@ -2165,7 +2165,6 @@ impl<'a> LowLevelCall<'a> { NumF64FromParts => self.load_args_and_call_zig(backend, bitcode::NUM_F64_FROM_PARTS), // Crypto CryptoEmptySha256 => self.load_args_and_call_zig(backend, bitcode::CRYPTO_EMPTY_SHA256), - CryptoEmptyDummy => self.load_args_and_call_zig(backend, bitcode::CRYPTO_EMPTY_DUMMY), CryptoSha256AddBytes => { self.load_args_and_call_zig(backend, bitcode::CRYPTO_SHA256_ADD_BYTES) } diff --git a/crates/compiler/module/src/low_level.rs b/crates/compiler/module/src/low_level.rs index afcf282f098..775d58fd680 100644 --- a/crates/compiler/module/src/low_level.rs +++ b/crates/compiler/module/src/low_level.rs @@ -130,7 +130,6 @@ pub enum LowLevel { LongJmp, SetLongJmpBuffer, CryptoEmptySha256, - CryptoEmptyDummy, CryptoSha256AddBytes, CryptoSha256Digest, } @@ -353,7 +352,6 @@ map_symbol_to_lowlevel! { Unreachable <= LIST_UNREACHABLE; DictPseudoSeed <= DICT_PSEUDO_SEED; CryptoEmptySha256 <= CRYPTO_EMPTY_SHA_256; - CryptoEmptyDummy <= CRYPTO_EMPTY_DUMMY; CryptoSha256AddBytes <= CRYPTO_SHA256_ADD_BYTES; CryptoSha256Digest <= CRYPTO_SHA256_DIGEST; } diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index ff58fa975c1..09e078395f3 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1740,9 +1740,6 @@ define_builtins! { 3 CRYPTO_SHA256_ADD_BYTES: "sha256AddBytes" 4 CRYPTO_SHA256_DIGEST: "sha256Digest" 5 CRYPTO_HASH_SHA_256: "hashSha256" - 6 CRYPTO_DUMMY: "Dummy" exposed_type=true - 7 CRYPTO_EMPTY_DUMMY: "emptyDummy" - 8 CRYPTO_THIRTY_TWO_BYTES: "ThirtyTwoBytes" exposed_type=true } num_modules: 17 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro) diff --git a/crates/compiler/mono/src/drop_specialization.rs b/crates/compiler/mono/src/drop_specialization.rs index 33eb0ae1c91..f8fb0d1cd87 100644 --- a/crates/compiler/mono/src/drop_specialization.rs +++ b/crates/compiler/mono/src/drop_specialization.rs @@ -1609,7 +1609,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC { ListIsUnique => RC::Rc, ListClone => RC::Rc, - CryptoEmptySha256 | CryptoEmptyDummy| CryptoSha256AddBytes | CryptoSha256Digest => RC::NoRc, + CryptoEmptySha256 | CryptoSha256AddBytes | CryptoSha256Digest => RC::NoRc, BoxExpr | UnboxExpr => { unreachable!("These lowlevel operations are turned into mono Expr's") diff --git a/crates/compiler/mono/src/inc_dec.rs b/crates/compiler/mono/src/inc_dec.rs index 5f200c3cd1b..5f842d3bfd0 100644 --- a/crates/compiler/mono/src/inc_dec.rs +++ b/crates/compiler/mono/src/inc_dec.rs @@ -1257,7 +1257,6 @@ pub(crate) fn lowlevel_borrow_signature(op: LowLevel) -> &'static [Ownership] { ListReleaseExcessCapacity => &[OWNED], StrReleaseExcessCapacity => &[OWNED], CryptoEmptySha256 => &[IRRELEVANT], - CryptoEmptyDummy => &[IRRELEVANT], CryptoSha256AddBytes => &[OWNED, BORROWED], CryptoSha256Digest => &[BORROWED], ListIncref => &[OWNED], diff --git a/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt b/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt index 676d0e3bbae..e5a08b57eb3 100644 --- a/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt +++ b/crates/compiler/uitest/tests/recursion/generalization_among_large_recursive_group.txt @@ -3,22 +3,22 @@ app "test" provides [main] to "./platform" f = \{} -> -#^{-1} <3287><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3296>{}]<80>* +#^{-1} <3238><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3247>{}]<80>* when g {} is -# ^ <3277><3296>{} -<3285>[[g(2)]]<125>-> <71>[Ok <3296>{}]<104>* +# ^ <3228><3247>{} -<3236>[[g(2)]]<125>-> <71>[Ok <3247>{}]<104>* _ -> Ok {} g = \{} -> -#^{-1} <3277><3296>{} -<3285>[[g(2)]]<125>-> <71>[Ok <3296>{}]<104>* +#^{-1} <3228><3247>{} -<3236>[[g(2)]]<125>-> <71>[Ok <3247>{}]<104>* when h {} is -# ^ <3282><3296>{} -<3290>[[h(3)]]<125>-> <95>[Ok <3296>{}]<128>* +# ^ <3233><3247>{} -<3241>[[h(3)]]<125>-> <95>[Ok <3247>{}]<128>* _ -> Ok {} h = \{} -> -#^{-1} <3282><3296>{} -<3290>[[h(3)]]<125>-> <95>[Ok <3296>{}]<128>* +#^{-1} <3233><3247>{} -<3241>[[h(3)]]<125>-> <95>[Ok <3247>{}]<128>* when f {} is -# ^ <3287><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3296>{}]<80>* +# ^ <3238><120>{} -<123>[[f(1)]]<125>-> <119>[Ok <3247>{}]<80>* _ -> Ok {} main = f {} -# ^ <3298><138>{} -<141>[[f(1)]]<143>-> <137>[Ok <3296>{}]<3297>w_a +# ^ <3249><138>{} -<141>[[f(1)]]<143>-> <137>[Ok <3247>{}]<3248>w_a