Skip to content

Commit

Permalink
📝 Complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfps committed May 21, 2024
1 parent 99d632f commit 9636c41
Show file tree
Hide file tree
Showing 102 changed files with 198 additions and 6 deletions.
2 changes: 1 addition & 1 deletion book/book/1-introduction/1-1-introduction.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ <h1 class="menu-title">Yul By Example</h1>

<div id="content" class="content">
<main>
<h1 id="introduction-what-is-this-book-about"><a class="header" href="#introduction-what-is-this-book-about">Introduction <em>(What is this book about?)</em></a></h1>
<h1 id="introduction"><a class="header" href="#introduction">Introduction</a></h1>
<hr />
<p>Hello there!</p>
<p>This is a book about Yul, a low-level, intermediate language written alongside Solidity that can be compiled to bytecode for different backends [<a href="https://docs.soliditylang.org/en/latest/yul.html#yul">1</a>].</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="soliditys-storage-and-memory"><a class="header" href="#soliditys-storage-and-memory">Solidity's Storage And Memory</a></h1>
<hr />
<p>Solidity offers us two locations for data storage:</p>
<ul>
<li>Storage</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="soliditys-storage-and-memory-layout"><a class="header" href="#soliditys-storage-and-memory-layout">Solidity's Storage And Memory Layout</a></h1>
<hr />
<p>We have understood what the storage and memory are. Persistent and temporary, respectively. Now we are going to take a look at how these two data storage locations are laid out.</p>
<h3 id="storage"><a class="header" href="#storage">Storage</a></h3>
<p>Solidity's storage layout has a finite amount of space, which is broken down into 32-byte groups called <code>slots</code>, and each slot can hold a 256-bit value. These slots start from index 0 and can stretch to a theoretical index limit of somewhere around <code>(2^256) - 1</code>. It is safe to say that the storage can never get full. Cool, isn't it?</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="using-yul-to-read-and-write-directly-to-storage-and-memory"><a class="header" href="#using-yul-to-read-and-write-directly-to-storage-and-memory">Using Yul To Read And Write Directly To Storage And Memory</a></h1>
<hr />
<p>Remember when we said keep <a href="https://evm.codes">evm.codes</a> handy? You can go to the website and take a look at some Yul commands. Take note of the <code>mload</code>, <code>mstore</code>, <code>mstore8</code>, <code>sload</code> and <code>sstore</code>.</p>
<p><code>mload</code> is short for Memory Load. <br>
<code>mstore</code> is short for Memory Store. <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="starting-yul-in-a-solidity-contract"><a class="header" href="#starting-yul-in-a-solidity-contract">Starting Yul In A Solidity Contract</a></h1>
<hr />
<p>Yul, as you know by now, is written inside Solidity functions. To start off a Yul code in a Solidity function, you simply declare the <code>assembly</code> keyword, followed by curly braces. Your Yul (Inline Assembly) code can then come inside the curly braces.</p>
<pre><code class="language-solidity">assembly {
// You can now write Yul here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="variable-storage-in-storage"><a class="header" href="#variable-storage-in-storage">Variable Storage In Storage</a></h1>
<hr />
<p>Solidity stores variables declared globally in storage. The storage is made up of slots, as we've discussed earlier. In this section we will look at how different data types are stored in Solidity's storage. Some are packed and some are greedy enough to take up a full slot without sharing.</p>
<p>You can head into the start of the section at <a href="4-2-1-uint8-uint128-uint256.html">uint8, uint128, uint256</a>.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="uint8-uint128-uint256"><a class="header" href="#uint8-uint128-uint256"><code>uint8</code>, <code>uint128</code>, <code>uint256</code></a></h1>
<hr />
<p>Unsigned integers are stored in memory based on the size of bytes they have. The size of <code>uint[n]</code> bytes can be realized by dividing <code>[n]</code> by 8, meaning that, while small <code>uint[n]</code> values like <code>uint8</code> have 1 byte, <code>uint256</code> has 32 bytes. Solidity's storage is designed in such a way that it can contain up to 32 bytes of value in one slot. In a situation where a variable doesn't contain up to 32 bytes, the value is stored and the next variable will be <strong>packed</strong> into the same slot, on the condition that when the bytes are added to the slot, it doesn't exceed 32 bytes.</p>
<p><code>uint256</code> =&gt; 32 bytes =&gt; Slot 0.</p>
<p>This is the maximum, hence it occupies an entire slot of its own. Whatever bytes we are going to add will exceed 32 and hence, will pick up the next slot, slot 1.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="custom-types"><a class="header" href="#custom-types">Custom Types</a></h1>
<hr />
<p>A custom type or a user-defined value type allows you to create an alias of a native type in Solidity. This alias, will inherit and act as if it is the original type. It is defined by using the <code>type C is V syntax</code>, where <code>C</code> is the custom type, and <code>V</code> is the native type. To convert from the underlying type to the custom type, <code>C.wrap(&lt;value&gt;)</code> is used, and to convert from the custom type to the native underlying type, <code>C.unwrap(&lt;value&gt;)</code> is used [<a href="https://docs.soliditylang.org/en/latest/types.html#user-defined-value-types">5</a>].</p>
<p>Custom types behave like their underlying types.</p>
<pre><code class="language-solidity">// SPDX-License-Identifier: GPL-3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="arrays"><a class="header" href="#arrays">Arrays</a></h1>
<hr />
<h2 id="general-array-storage"><a class="header" href="#general-array-storage">General Array Storage</a></h2>
<p>Up till now, we've learnt about individual data types in Solidity and how they are stored in storage. Before we proceed to their array counterparts, we would want to go over how arrays are viewed in Solidity storage generally. This view is applied to all other types.</p>
<p>Solidity recognizes two types of arrays, the fixed length array and the dynamic array. These two array types are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="int8-int128-int256"><a class="header" href="#int8-int128-int256"><code>int8</code>, <code>int128</code>, <code>int256</code></a></h1>
<hr />
<p>While <code>uint[n]</code> types can only contain positive integers, the <code>int[n]</code> types can contain both positive and negative numbers. As you know, <code>int[n]</code> variables store numbers ranging from <code>-1 -&gt; -(2 ^ (n-1))</code> on the negative side, and <code>0 -&gt; (2^n) - 1</code> on the positive side. This means that for the three <code>int[n]</code> types specified above, this would be their minimum and maximum values.</p>
<div class="table-wrapper"><table><thead><tr><th><strong><code>int[n]</code> type</strong></th><th><strong>Minimum Value</strong></th><th><strong>Maximum Value</strong></th></tr></thead><tbody>
<tr><td><code>int8</code></td><td>-128</td><td>127</td></tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="bytes1-bytes16-bytes32"><a class="header" href="#bytes1-bytes16-bytes32"><code>bytes1</code>, <code>bytes16</code>, <code>bytes32</code></a></h1>
<hr />
<p><code>bytes[n]</code> are fixed length byte arrays, as opposed to <code>bytes</code> that are variable length byte arrays. The storage of <code>bytes[n]</code> in storage is such that it takes up a slot when it is of <code>bytes32</code> and is packed in cases of bytes below <code>32</code>. It is very similar to the <code>uint</code> type.</p>
<pre><code class="language-solidity">// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="bytes"><a class="header" href="#bytes"><code>bytes</code></a></h1>
<hr />
<p><code>bytes</code> are dynamic byte arrays. While the length of a <code>bytes[n]</code> is known as <code>n</code>, bytes do not have a specific
length and can stretch up to <code>128</code> or even more.</p>
<p>For example, try running the <code>getByteCode()</code> in the <code>ByteCodeGenerator</code> contract below on Remix.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="string"><a class="header" href="#string"><code>string</code></a></h1>
<hr />
<p><code>string</code> is first converted into its <code>bytes</code> type by converting each individual ASCII character to its hexadecimal
value. It is then stored in the exact way <code>bytes</code> are stored.</p>
<p>Example, a <code>string</code> type, <code>"hello"</code> would be first converted into <code>0x68656c6c6f</code>, a concatenation of each hexadecimal value of each character, and then stored just the way
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="address"><a class="header" href="#address"><code>address</code></a></h1>
<hr />
<p><code>address</code> is a <code>bytes20</code> or <code>uint160</code> value, it holds 20 bytes of data. Since <code>address</code> take up 20 bytes of a
storage slot, they can be packed with any number of types that can sum up to 12 bytes.</p>
<pre><code class="language-solidity">// SPDX-License-Identifier: GPL-3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="struct"><a class="header" href="#struct"><code>struct</code></a></h1>
<hr />
<p>A <code>struct</code> is a group of data. The layout of a <code>struct</code> is identical to the layout of the data within a <code>struct</code>. The slots a <code>struct</code> would occupy is dependent on the types of variables within the struct. A struct with two uint256 types would occupy two slots.</p>
<pre><code class="language-solidity">// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="mapping"><a class="header" href="#mapping"><code>mapping</code></a></h1>
<hr />
<p>A typical <code>mapping</code> in Solidity is accessed using the <code>key</code>. There are two types of <code>mapping</code> as you know, single
<code>mapping</code> and nested <code>mapping</code>. We will look at how to retrieve data from a single and a nested <code>mapping</code>.</p>
<h3 id="single-mapping"><a class="header" href="#single-mapping">Single <code>mapping</code></a></h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="enum"><a class="header" href="#enum"><code>enum</code></a></h1>
<hr />
<p><code>enum</code> is equivalent to <code>uint8</code>, the value of an <code>enum</code> variable cannot exceed <code>256</code>. <code>enum</code> shares the same characteristic as the <code>uint8</code> type. They can be packed in storage.</p>
<p>Refer to <a href="4-2-1-uint8-uint128-uint256.html"><code>uint8</code>, <code>uint128</code>, <code>uint256</code></a>.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="variable-storage-in-memory"><a class="header" href="#variable-storage-in-memory">Variable Storage In Memory</a></h1>
<hr />
<p>Variables declared inside a function's body, or returned by a function are stored in memory, except in special cases with libraries, internal and private functions that can return variables stored in storage. The memory, unlike the storage is not divided up into slots, but rather is a vast area of data grouped into 32 bytes. The retrieval of data from memory is even more tricky than that of storage, because, unlike storage that restricts us to read from and write to one slot at a time, we can read from anywhere in memory and write to anywhere in memory arbitrarily. We can start our data reading from location <code>0x00</code>, or <code>0x78</code>, or <code>0x0120</code>, or <code>0x3454</code>. The memory is very similar to the <code>calldata</code> in terms of data storage. They both have the same layout, the only difference being that the <code>calldata</code> is prepended with <code>4 bytes</code> of data that we know as the <code>function signature</code>.</p>
<p>When a value is stored in memory, Solidity automatically stores the highest variant of its type in memory. Meaning that storing a data of type <code>uint8</code> will store a <code>uint256</code> type of the same number in memory, occupying an entire 32-byte memory space.</p>
<p>Data in the memory and <code>calldata</code> are stored according to the standard ABI specification [<a href="https://docs.soliditylang.org/en/latest/internals/layout_in_memory.html#">3</a>], <strong>and they are not packed</strong>.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="uint8-uint128-uint256"><a class="header" href="#uint8-uint128-uint256"><code>uint8</code>, <code>uint128</code>, <code>uint256</code></a></h1>
<hr />
<p>The storage of <code>uint8</code>, <code>uint128</code>, <code>uint256</code> in memory are very simple, they are written directly at our chosen
memory location and are returned the same.</p>
<pre><code class="language-solidity">// SPDX-License-Identifier: GPL-3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="struct"><a class="header" href="#struct"><code>struct</code></a></h1>
<hr />
<p>Struct in memory is stored the using the same three-step method.</p>
<ol>
<li>The pointer.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="int8-int128-int256"><a class="header" href="#int8-int128-int256"><code>int8</code>, <code>int128</code>, <code>int256</code></a></h1>
<hr />
<p>The storage of <code>int8</code>, <code>int128</code>, <code>int256</code> in memory are very simple, they are written directly at our chosen
memory location and are returned the same. Yul wraps around negative <code>int[n]</code> values.</p>
<pre><code class="language-solidity">// SPDX-License-Identifier: GPL-3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="bytes1-bytes16-bytes32"><a class="header" href="#bytes1-bytes16-bytes32"><code>bytes1</code>, <code>bytes16</code>, <code>bytes32</code></a></h1>
<hr />
<p>The storage of <code>bytes1</code>, <code>bytes16</code>, <code>bytes32</code> in memory are quite different, they are right-padded to 32 bytes if they're not up to 32 bytes, and then, they are written directly at our chosen memory location and are returned with the padding.</p>
<h2 id="padding"><a class="header" href="#padding">Padding?</a></h2>
<p>Padding is the process of adding a number of <code>0</code> to a data type's hexadecimal value to make it up to 32 bytes so that it can be written to a location in storage or memory. If the value is up to 32 bytes, it is not padded.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="bytes"><a class="header" href="#bytes"><code>bytes</code></a></h1>
<hr />
<p>The storage of <code>bytes</code> in memory is very tricky. Normally, declaring a <code>bytes</code> variable in memory will be followed by the <code>memory</code> keyword as in <code>bytes memory &lt;variableName&gt;</code> this is because, <code>bytes</code> values are encoded in memory. The encoding has three parts.</p>
<ol>
<li>The pointer.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="string"><a class="header" href="#string"><code>string</code></a></h1>
<hr />
<p><code>string</code> is first converted into its <code>bytes</code> type by converting each individual ASCII character to its hexadecimal
value. It is then stored in the exact way <code>bytes</code> are stored.</p>
<p>Example, a <code>string</code> type, <code>"hello"</code> would be first converted into <code>0x68656c6c6f</code>, a concatenation of each hexadecimal value of each character, and then stored just the way <code>bytes</code> are stored.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="address"><a class="header" href="#address"><code>address</code></a></h1>
<hr />
<p>The storage of <code>address</code> in memory are very simple, they are written directly at our chosen memory location and are returned the same.</p>
<pre><code class="language-solidity">// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="custom-types"><a class="header" href="#custom-types">Custom Types</a></h1>
<hr />
<p>Custom types behave like their underlying types.</p>
<pre><code class="language-solidity">// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ <h1 class="menu-title">Yul By Example</h1>
<div id="content" class="content">
<main>
<h1 id="arrays"><a class="header" href="#arrays">Arrays</a></h1>
<hr />
<h2 id="fixed-arrays-typen"><a class="header" href="#fixed-arrays-typen">Fixed Arrays, <code>&lt;type&gt;[n]</code></a></h2>
<p>The storage of fixed arrays in memory is simply a concatenation of the storage of its individual elements.</p>
<h2 id="dynamic-arrays-type"><a class="header" href="#dynamic-arrays-type">Dynamic Arrays, <code>&lt;type&gt;[]</code></a></h2>
Expand Down
Loading

0 comments on commit 9636c41

Please sign in to comment.