From 54a13135884e0da7c1d1787a3511a16d3518f0ba Mon Sep 17 00:00:00 2001 From: Robin Salen <30937548+Nashtare@users.noreply.github.com> Date: Tue, 9 Jan 2024 15:25:31 +0100 Subject: [PATCH] Improve some calls to `%mstore_rlp` (#1452) * Improve some calls to mstore_rlp * Remove comment --- evm/src/cpu/kernel/asm/memory/core.asm | 3 +- evm/src/cpu/kernel/asm/mpt/hash/hash.asm | 12 ++-- .../asm/mpt/hash/hash_trie_specific.asm | 4 +- evm/src/cpu/kernel/asm/mpt/hex_prefix.asm | 12 ++-- evm/src/cpu/kernel/asm/mpt/util.asm | 4 +- evm/src/cpu/kernel/asm/rlp/encode.asm | 72 +++++++++---------- 6 files changed, 50 insertions(+), 57 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/core.asm b/evm/src/cpu/kernel/asm/memory/core.asm index 3a3a17a500..eef7ee1a49 100644 --- a/evm/src/cpu/kernel/asm/memory/core.asm +++ b/evm/src/cpu/kernel/asm/memory/core.asm @@ -409,8 +409,7 @@ %mstore_u32 %endmacro -// Store a single byte to @SEGMENT_RLP_RAW. -%macro mstore_rlp +%macro swap_mstore // stack: addr, value SWAP1 MSTORE_GENERAL diff --git a/evm/src/cpu/kernel/asm/mpt/hash/hash.asm b/evm/src/cpu/kernel/asm/mpt/hash/hash.asm index 29348f9707..15fdd6a967 100644 --- a/evm/src/cpu/kernel/asm/mpt/hash/hash.asm +++ b/evm/src/cpu/kernel/asm/mpt/hash/hash.asm @@ -151,8 +151,8 @@ global encode_node_branch: // No value; append the empty string (0x80). // stack: value_ptr, rlp_pos', rlp_start, encode_value, cur_len, retdest - %stack (value_ptr, rlp_pos, rlp_start, encode_value) -> (rlp_pos, 0x80, rlp_pos, rlp_start) - %mstore_rlp + %stack (value_ptr, rlp_pos, rlp_start, encode_value) -> (0x80, rlp_pos, rlp_pos, rlp_start) + MSTORE_GENERAL // stack: rlp_pos', rlp_start, cur_len, retdest %increment // stack: rlp_pos'', rlp_start, cur_len, retdest @@ -192,9 +192,9 @@ encode_node_branch_prepend_prefix: SWAP1 DUP1 %sub_const(32) %jumpi(%%unpack) // Otherwise, result is a hash, and we need to add the prefix 0x80 + 32 = 160. // stack: result_len, result, cur_len, rlp_pos, rlp_start, node_payload_ptr, encode_value, retdest + DUP4 // rlp_pos PUSH 160 - DUP5 // rlp_pos - %mstore_rlp + MSTORE_GENERAL SWAP3 %increment SWAP3 // rlp_pos += 1 %%unpack: %stack (result_len, result, cur_len, rlp_pos, rlp_start, node_payload_ptr, encode_value, retdest) @@ -233,9 +233,9 @@ encode_node_extension_after_hex_prefix: // If result_len != 32, result is raw RLP, with an appropriate RLP prefix already. DUP4 %sub_const(32) %jumpi(encode_node_extension_unpack) // Otherwise, result is a hash, and we need to add the prefix 0x80 + 32 = 160. + DUP1 // rlp_pos PUSH 160 - DUP2 // rlp_pos - %mstore_rlp + MSTORE_GENERAL %increment // rlp_pos += 1 encode_node_extension_unpack: %stack (rlp_pos, rlp_start, result, result_len, node_payload_ptr, cur_len) diff --git a/evm/src/cpu/kernel/asm/mpt/hash/hash_trie_specific.asm b/evm/src/cpu/kernel/asm/mpt/hash/hash_trie_specific.asm index 6abca0b687..cd07c01fdc 100644 --- a/evm/src/cpu/kernel/asm/mpt/hash/hash_trie_specific.asm +++ b/evm/src/cpu/kernel/asm/mpt/hash/hash_trie_specific.asm @@ -326,8 +326,8 @@ encode_nonzero_receipt_type: // stack: rlp_receipt_len, txn_type, rlp_addr, value_ptr, retdest DUP3 %encode_rlp_multi_byte_string_prefix // stack: rlp_addr, txn_type, old_rlp_addr, value_ptr, retdest - DUP2 DUP2 - %mstore_rlp + DUP1 DUP3 + MSTORE_GENERAL %increment // stack: rlp_addr, txn_type, old_rlp_addr, value_ptr, retdest %stack (rlp_addr, txn_type, old_rlp_addr, value_ptr, retdest) -> (rlp_addr, value_ptr, retdest) diff --git a/evm/src/cpu/kernel/asm/mpt/hex_prefix.asm b/evm/src/cpu/kernel/asm/mpt/hex_prefix.asm index 7dd02c34f9..cee87deb0a 100644 --- a/evm/src/cpu/kernel/asm/mpt/hex_prefix.asm +++ b/evm/src/cpu/kernel/asm/mpt/hex_prefix.asm @@ -27,7 +27,7 @@ first_byte: // stack: rlp_addr, num_nibbles, packed_nibbles, terminated, retdest // get the first nibble, if num_nibbles is odd, or zero otherwise SWAP2 - // stack: packed_nibbles, num_nibbbles, rlp_addr, terminated, retdest + // stack: packed_nibbles, num_nibbles, rlp_addr, terminated, retdest DUP2 DUP1 %mod_const(2) // stack: parity, num_nibbles, packed_nibbles, num_nibbles, rlp_addr, terminated, retdest @@ -50,7 +50,7 @@ first_byte: ADD // stack: first_byte, rlp_addr, retdest DUP2 - %mstore_rlp + %swap_mstore %increment // stack: rlp_addr', retdest SWAP1 @@ -88,7 +88,7 @@ rlp_header_medium: // stack: hp_len, rlp_addr, num_nibbles, packed_nibbles, terminated, retdest %add_const(0x80) // value = 0x80 + hp_len DUP2 - %mstore_rlp + %swap_mstore // stack: rlp_addr, num_nibbles, packed_nibbles, terminated, retdest // rlp_addr += 1 %increment @@ -108,14 +108,14 @@ rlp_header_large: // In practice hex-prefix length will never exceed 256, so the length of the // length will always be 1 byte in this case. + DUP2 // rlp_addr PUSH 0xb8 // value = 0xb7 + len_of_len = 0xb8 - DUP3 - %mstore_rlp + MSTORE_GENERAL // stack: rlp_addr, value, hp_len, i, rlp_addr, num_nibbles, packed_nibbles, terminated, retdest // stack: hp_len, rlp_addr, num_nibbles, packed_nibbles, terminated, retdest DUP2 %increment - %mstore_rlp + %swap_mstore // stack: rlp_addr, num_nibbles, packed_nibbles, terminated, retdest // rlp_addr += 2 diff --git a/evm/src/cpu/kernel/asm/mpt/util.asm b/evm/src/cpu/kernel/asm/mpt/util.asm index 27418b1704..db13d8890d 100644 --- a/evm/src/cpu/kernel/asm/mpt/util.asm +++ b/evm/src/cpu/kernel/asm/mpt/util.asm @@ -11,9 +11,9 @@ %endmacro %macro initialize_rlp_segment - PUSH 0x80 PUSH @ENCODED_EMPTY_NODE_POS - %mstore_rlp + PUSH 0x80 + MSTORE_GENERAL %endmacro %macro alloc_rlp_block diff --git a/evm/src/cpu/kernel/asm/rlp/encode.asm b/evm/src/cpu/kernel/asm/rlp/encode.asm index 2319e780b9..ccaa9f616c 100644 --- a/evm/src/cpu/kernel/asm/rlp/encode.asm +++ b/evm/src/cpu/kernel/asm/rlp/encode.asm @@ -29,12 +29,11 @@ global encode_rlp_256: // RLP-encode a fixed-length string with the given byte length. Assumes string < 2^(8 * len). global encode_rlp_fixed: // stack: len, rlp_addr, string, retdest - DUP1 + DUP2 + DUP2 %add_const(0x80) - // stack: first_byte, len, rlp_addr, string, retdest - DUP3 - // stack: rlp_addr, first_byte, len, rlp_addr, string, retdest - %mstore_rlp + // stack: first_byte, rlp_addr, len, rlp_addr, string, retdest + MSTORE_GENERAL // stack: len, rlp_addr, string, retdest SWAP1 %increment // increment rlp_addr @@ -51,19 +50,17 @@ encode_rlp_fixed_finish: // I.e. writes encode(encode(string). Assumes string < 2^(8 * len). global doubly_encode_rlp_fixed: // stack: len, rlp_addr, string, retdest - DUP1 + DUP2 + DUP2 %add_const(0x81) - // stack: first_byte, len, rlp_addr, string, retdest - DUP3 - // stack: rlp_addr, first_byte, len, rlp_addr, string, retdest - %mstore_rlp + // stack: first_byte, rlp_addr, len, rlp_addr, string, retdest + MSTORE_GENERAL // stack: len, rlp_addr, string, retdest - DUP1 + DUP2 %increment + DUP2 %add_const(0x80) - // stack: second_byte, len, original_rlp_addr, string, retdest - DUP3 %increment - // stack: rlp_addr', second_byte, len, rlp_addr, string, retdest - %mstore_rlp + // stack: second_byte, rlp_addr', len, original_rlp_addr, string, retdest + MSTORE_GENERAL // stack: len, rlp_addr, string, retdest SWAP1 %add_const(2) // advance past the two prefix bytes @@ -87,11 +84,10 @@ global encode_rlp_multi_byte_string_prefix: %jumpi(encode_rlp_multi_byte_string_prefix_large) // Medium case; prefix is 0x80 + str_len. // stack: rlp_addr, str_len, retdest - SWAP1 %add_const(0x80) - // stack: prefix, rlp_addr, retdest - DUP2 - // stack: rlp_addr, prefix, rlp_addr, retdest - %mstore_rlp + DUP1 + SWAP2 %add_const(0x80) + // stack: prefix, rlp_addr, rlp_addr, retdest + MSTORE_GENERAL // stack: rlp_addr, retdest %increment // stack: rlp_addr', retdest @@ -104,12 +100,11 @@ encode_rlp_multi_byte_string_prefix_large: %num_bytes // stack: len_of_len, rlp_addr, str_len, retdest SWAP1 - DUP2 // len_of_len + DUP1 // rlp_addr + DUP3 // len_of_len %add_const(0xb7) - // stack: first_byte, rlp_addr, len_of_len, str_len, retdest - DUP2 - // stack: rlp_addr, first_byte, rlp_addr, len_of_len, str_len, retdest - %mstore_rlp + // stack: first_byte, rlp_addr, rlp_addr, len_of_len, str_len, retdest + MSTORE_GENERAL // stack: rlp_addr, len_of_len, str_len, retdest %increment // stack: rlp_addr', len_of_len, str_len, retdest @@ -132,12 +127,11 @@ global encode_rlp_list_prefix: %jumpi(encode_rlp_list_prefix_large) // Small case: prefix is just 0xc0 + length. // stack: rlp_addr, payload_len, retdest - SWAP1 + DUP1 + SWAP2 %add_const(0xc0) - // stack: prefix, rlp_addr, retdest - DUP2 - // stack: rlp_addr, prefix, rlp_addr, retdest - %mstore_rlp + // stack: prefix, rlp_addr, rlp_addr, retdest + MSTORE_GENERAL // stack: rlp_addr, retdest %increment SWAP1 @@ -147,10 +141,10 @@ encode_rlp_list_prefix_large: // stack: rlp_addr, payload_len, retdest DUP2 %num_bytes // stack: len_of_len, rlp_addr, payload_len, retdest - DUP1 %add_const(0xf7) - // stack: first_byte, len_of_len, rlp_addr, payload_len, retdest - DUP3 // rlp_addr - %mstore_rlp + DUP2 + DUP2 %add_const(0xf7) + // stack: first_byte, rlp_addr, len_of_len, rlp_addr, payload_len, retdest + MSTORE_GENERAL // stack: len_of_len, rlp_addr, payload_len, retdest SWAP1 %increment // stack: rlp_addr', len_of_len, payload_len, retdest @@ -184,10 +178,10 @@ global prepend_rlp_list_prefix: // If we got here, we have a small list, so we prepend 0xc0 + len at rlp_address 8. // stack: payload_len, end_rlp_addr, start_rlp_addr, retdest - DUP1 %add_const(0xc0) - // stack: prefix_byte, payload_len, end_rlp_addr, start_rlp_addr, retdest - DUP4 %decrement // offset of prefix - %mstore_rlp + DUP3 %decrement // offset of prefix + DUP2 %add_const(0xc0) + // stack: prefix_byte, start_rlp_addr-1, payload_len, end_rlp_addr, start_rlp_addr, retdest + MSTORE_GENERAL // stack: payload_len, end_rlp_addr, start_rlp_addr, retdest %increment // stack: rlp_len, end_rlp_addr, start_rlp_addr, retdest @@ -207,7 +201,7 @@ prepend_rlp_list_prefix_big: DUP5 %decrement // start_rlp_addr - 1 SUB // stack: prefix_start_rlp_addr, len_of_len, payload_len, end_rlp_addr, start_rlp_addr, retdest - DUP2 %add_const(0xf7) DUP2 %mstore_rlp // rlp[prefix_start_rlp_addr] = 0xf7 + len_of_len + DUP2 %add_const(0xf7) DUP2 %swap_mstore // rlp[prefix_start_rlp_addr] = 0xf7 + len_of_len // stack: prefix_start_rlp_addr, len_of_len, payload_len, end_rlp_addr, start_rlp_addr, retdest DUP1 %increment // start_len_rlp_addr = prefix_start_rlp_addr + 1 %stack (start_len_rlp_addr, prefix_start_rlp_addr, len_of_len, payload_len, end_rlp_addr, start_rlp_addr, retdest)