From f0524ed45a407ff9b1b59ee04edbfb9489a4f8a1 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 11 Oct 2016 12:25:18 +0100 Subject: Document memory layout --- docs/miscellaneous.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'docs') diff --git a/docs/miscellaneous.rst b/docs/miscellaneous.rst index c4a954ad..cdc82c78 100644 --- a/docs/miscellaneous.rst +++ b/docs/miscellaneous.rst @@ -56,6 +56,20 @@ So for the following contract snippet:: The position of ``data[4][9].b`` is at ``keccak256(uint256(9) . keccak256(uint256(4) . uint256(1))) + 1``. +**************** +Layout in Memory +**************** + +EVM memory is linear. Reading is limited to 256-bit width, but both 8-bit and 256-bit writes are allowed. Memory size is 0 at the beginning of execution. Memory is expanded by accessing (either reading or writing) a previously untouched memory offset. At the time of expansion, the cost in gas must be paid. + +Solidity reserves three 256-bit slots: +- 0 - 64: scratch space for hashing methods +- 64 - 96: currently allocated memory size (aka. free memory pointer) + +Solidity always places new objects at the free memory pointer and memory is never freed. + +.. index: memory layout + ***************** Esoteric Features ***************** -- cgit v1.2.3 From 3d12c2ddc72d15dfcf599a089f104a2a5c224f41 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 11 Oct 2016 23:41:37 +0100 Subject: Merge the two EVM memory introduction sections --- docs/introduction-to-smart-contracts.rst | 8 +++++--- docs/miscellaneous.rst | 2 -- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'docs') diff --git a/docs/introduction-to-smart-contracts.rst b/docs/introduction-to-smart-contracts.rst index 922056ec..bfc342d9 100644 --- a/docs/introduction-to-smart-contracts.rst +++ b/docs/introduction-to-smart-contracts.rst @@ -348,9 +348,11 @@ storage. A contract can neither read nor write to any storage apart from its own. The second memory area is called **memory**, of which a contract obtains -a freshly cleared instance for each message call. Memory can be -addressed at byte level, but read and written to in 32 byte (256-bit) -chunks. Memory is more costly the larger it grows (it scales +a freshly cleared instance for each message call. Memory is linear and can be +addressed at byte level, but reads are limited to a width of 256 bits, while writes +can be either 8 bits or 256 bits wide. Memory is expanded by accessing (either +reading or writing) a previously untouched memory offset. At the time of expansion, +the cost in gas must be paid. Memory is more costly the larger it grows (it scales quadratically). The EVM is not a register machine but a stack machine, so all diff --git a/docs/miscellaneous.rst b/docs/miscellaneous.rst index cdc82c78..47da33b8 100644 --- a/docs/miscellaneous.rst +++ b/docs/miscellaneous.rst @@ -60,8 +60,6 @@ The position of ``data[4][9].b`` is at ``keccak256(uint256(9) . keccak256(uint25 Layout in Memory **************** -EVM memory is linear. Reading is limited to 256-bit width, but both 8-bit and 256-bit writes are allowed. Memory size is 0 at the beginning of execution. Memory is expanded by accessing (either reading or writing) a previously untouched memory offset. At the time of expansion, the cost in gas must be paid. - Solidity reserves three 256-bit slots: - 0 - 64: scratch space for hashing methods - 64 - 96: currently allocated memory size (aka. free memory pointer) -- cgit v1.2.3 From 66c6c746998b853cde52225ce73818b5496c79c5 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 11 Oct 2016 23:51:09 +0100 Subject: Explain how scratch space affects the free memory pointer --- docs/miscellaneous.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'docs') diff --git a/docs/miscellaneous.rst b/docs/miscellaneous.rst index 47da33b8..b631a2e4 100644 --- a/docs/miscellaneous.rst +++ b/docs/miscellaneous.rst @@ -64,7 +64,12 @@ Solidity reserves three 256-bit slots: - 0 - 64: scratch space for hashing methods - 64 - 96: currently allocated memory size (aka. free memory pointer) -Solidity always places new objects at the free memory pointer and memory is never freed. +Scratch space can be used between statements (ie. within inline assembly). + +Solidity always places new objects at the free memory pointer and memory is never freed (this might change in the future). + +.. warning:: + There are some operations in Solidity that need a temporary memory area larger than 64 bytes and therefore will not fit into the scratch space. They will be placed where the free memory points to, but given their short lifecycle, the pointer is not updated. The memory may or may not be zeroed out. Because of this, one shouldn't expect the free memory to be zeroed out. .. index: memory layout -- cgit v1.2.3 From 9521188bde4385e5131446b3e3eb7a9bdda37504 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 13 Oct 2016 19:00:57 +0100 Subject: Mention that memory is organised as 256-bit words --- docs/introduction-to-smart-contracts.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs') diff --git a/docs/introduction-to-smart-contracts.rst b/docs/introduction-to-smart-contracts.rst index bfc342d9..ad0a9650 100644 --- a/docs/introduction-to-smart-contracts.rst +++ b/docs/introduction-to-smart-contracts.rst @@ -350,10 +350,10 @@ from its own. The second memory area is called **memory**, of which a contract obtains a freshly cleared instance for each message call. Memory is linear and can be addressed at byte level, but reads are limited to a width of 256 bits, while writes -can be either 8 bits or 256 bits wide. Memory is expanded by accessing (either -reading or writing) a previously untouched memory offset. At the time of expansion, -the cost in gas must be paid. Memory is more costly the larger it grows (it scales -quadratically). +can be either 8 bits or 256 bits wide. Memory is expanded by a word (256-bit), when +accessing (either reading or writing) a previously untouched memory word (ie. any offset +within a word). At the time of expansion, the cost in gas must be paid. Memory is more +costly the larger it grows (it scales quadratically). The EVM is not a register machine but a stack machine, so all computations are performed on an area called the **stack**. It has a maximum size of -- cgit v1.2.3