From 6f2976296909fa04396ff810126efd3cef750798 Mon Sep 17 00:00:00 2001 From: Logan Nguyen Date: Mon, 23 Oct 2023 16:44:28 -0500 Subject: [PATCH 1/3] feat: added DataAllocation solidity example contract Signed-off-by: Logan Nguyen --- .../yul/data-allocation/DataAllocation.sol | 42 +++++++++++ test/yul/data-allocation/DataAllocation.js | 69 +++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 contracts/yul/data-allocation/DataAllocation.sol create mode 100644 test/yul/data-allocation/DataAllocation.js diff --git a/contracts/yul/data-allocation/DataAllocation.sol b/contracts/yul/data-allocation/DataAllocation.sol new file mode 100644 index 000000000..5afeb5e34 --- /dev/null +++ b/contracts/yul/data-allocation/DataAllocation.sol @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +contract DataAllocation { + uint256 a = 0; + uint256 b = 12; + + /// mstore - mem[p…(p+32)) := v + /// mload - mem[p…(p+32)) + function allocateMemory(uint256 p, uint256 v) external pure returns (uint256 n) { + assembly { + mstore(p, v) + n := mload(p) + } + return n; + } + + /// mstore8 - mem[p] := v & 0xff (only modifies a single byte) + /// mload - mem[p…(p+32)) + function allocateMemory8(uint256 p, uint8 v) external pure returns (uint8 n) { + bytes1 value; + assembly { + mstore8(p, v) + value := mload(p) + } + n = uint8(value); + } + + /// sload - storage[p] + function sload(uint256 p) external view returns (uint256 n) { + assembly { + n := sload(p) + } + } + + /// sstore - storage[p] := v + function sstore(uint256 p, uint256 v) external { + assembly { + sstore(p, v) + } + } +} \ No newline at end of file diff --git a/test/yul/data-allocation/DataAllocation.js b/test/yul/data-allocation/DataAllocation.js new file mode 100644 index 000000000..fbc75bc79 --- /dev/null +++ b/test/yul/data-allocation/DataAllocation.js @@ -0,0 +1,69 @@ +/*- + * + * Hedera Smart Contracts + * + * Copyright (C) 2023 Hedera Hashgraph, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +const { expect } = require('chai') +const { ethers } = require('hardhat') + +describe('@solidityequiv5 Data Allocation Tests', () => { + let dataAllocationContract + const P = 32 + const V = 72 + const SLOT_0_KEY = 0 + const SLOT_1_KEY = 1 + + before(async () => { + const dataAllocationContractFactory = await ethers.getContractFactory( + 'DataAllocation' + ) + + dataAllocationContract = await dataAllocationContractFactory.deploy() + }) + + it('Should execute allocateMemory', async () => { + const result = await dataAllocationContract.allocateMemory(P, V) + + expect(result).to.eq(V) + }) + + it('Should execute allocateMemory8', async () => { + const result = await dataAllocationContract.allocateMemory8(P, V) + + expect(result).to.eq(V) + }) + + it('Should execute sload', async () => { + const EXPECTED_SLOT_0_VALUE = 0 // state variable `a` + const EXPECTED_SLOT_1_VALUE = 12 // state variable `b` + + const result0 = await dataAllocationContract.sload(SLOT_0_KEY) + const result1 = await dataAllocationContract.sload(SLOT_1_KEY) + + expect(result0).to.eq(EXPECTED_SLOT_0_VALUE) + expect(result1).to.eq(EXPECTED_SLOT_1_VALUE) + }) + + it('Should execute sstore', async () => { + await dataAllocationContract.sstore(SLOT_0_KEY, V) + + const result = await dataAllocationContract.sload(SLOT_0_KEY) + + expect(result).to.eq(V) + }) +}) From 6252a1876ae8af8bf2b41919b8b3ce09c9cd521a Mon Sep 17 00:00:00 2001 From: Logan Nguyen Date: Wed, 25 Oct 2023 11:40:29 -0500 Subject: [PATCH 2/3] update: added blank line Signed-off-by: Logan Nguyen --- contracts/yul/data-allocation/DataAllocation.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contracts/yul/data-allocation/DataAllocation.sol b/contracts/yul/data-allocation/DataAllocation.sol index 5afeb5e34..3bfda3055 100644 --- a/contracts/yul/data-allocation/DataAllocation.sol +++ b/contracts/yul/data-allocation/DataAllocation.sol @@ -39,4 +39,5 @@ contract DataAllocation { sstore(p, v) } } -} \ No newline at end of file +} + From 8d5412430d2a5403b8e0014eae88b0d45cf458e1 Mon Sep 17 00:00:00 2001 From: Logan Nguyen Date: Wed, 25 Oct 2023 22:42:57 -0500 Subject: [PATCH 3/3] update: updated license identifier Signed-off-by: Logan Nguyen --- contracts/yul/data-allocation/DataAllocation.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/yul/data-allocation/DataAllocation.sol b/contracts/yul/data-allocation/DataAllocation.sol index 3bfda3055..e50714209 100644 --- a/contracts/yul/data-allocation/DataAllocation.sol +++ b/contracts/yul/data-allocation/DataAllocation.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: UNLICENSED +// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.20; contract DataAllocation {