mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-06-14 22:41:07 +00:00
Compare commits
46 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 93788e72e9 | |||
| 296a226d0b | |||
| 84deb3a29d | |||
| ee064671e0 | |||
| 63dfd046e5 | |||
| 08f341ccc1 | |||
| a07968205b | |||
| 7ffe64ed7c | |||
| f1bce4fe3f | |||
| 9efbb4c0b4 | |||
| bcc8fa892c | |||
| 89ec25da65 | |||
| 75a83af4da | |||
| 9c330ef8fc | |||
| 687cec31ef | |||
| 48a019e0ad | |||
| 17a2d2f9f2 | |||
| 6ad7908c5e | |||
| 840a736fc5 | |||
| 89cdfefab4 | |||
| 6c2c633651 | |||
| a73b0925c6 | |||
| ee650cf03a | |||
| c2210442b6 | |||
| cb268850a9 | |||
| f3a86588f3 | |||
| 7233738f45 | |||
| 79ec4dd04b | |||
| 374563bbe5 | |||
| a921e425b4 | |||
| 60fc09f787 | |||
| 10b8ff989c | |||
| 157647d5c1 | |||
| 35419e8202 | |||
| de3e7bf253 | |||
| 9fb24b607d | |||
| ba7310fdff | |||
| 4ef495beea | |||
| 8ed689e7ec | |||
| bfda465c32 | |||
| ab90af49df | |||
| 8201401fef | |||
| 1a8a7926e9 | |||
| bec5d60b7c | |||
| 3608a5a143 | |||
| 888723eb0d |
+2
-3
@@ -12,7 +12,6 @@ rustflags = [
|
|||||||
"-Clink-arg=-sALLOW_TABLE_GROWTH=1",
|
"-Clink-arg=-sALLOW_TABLE_GROWTH=1",
|
||||||
"-Clink-arg=--js-library=js/embed/soljson_interface.js",
|
"-Clink-arg=--js-library=js/embed/soljson_interface.js",
|
||||||
"-Clink-arg=--pre-js=js/embed/pre.js",
|
"-Clink-arg=--pre-js=js/embed/pre.js",
|
||||||
"-Clink-arg=-sNODEJS_CATCH_EXIT=0",
|
"-Clink-arg=-sSTACK_SIZE=128kb",
|
||||||
"-Clink-arg=-sDISABLE_EXCEPTION_CATCHING=0",
|
"-Clink-arg=-sNODEJS_CATCH_EXIT=0"
|
||||||
"-Copt-level=3"
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
name: "get emsdk"
|
||||||
|
inputs:
|
||||||
|
version:
|
||||||
|
description: ""
|
||||||
|
required: false
|
||||||
|
default: "3.1.64"
|
||||||
|
|
||||||
|
|
||||||
|
runs:
|
||||||
|
using: "composite"
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: install emsdk
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
git clone https://github.com/emscripten-core/emsdk.git ./emsdk/
|
||||||
|
cd emsdk
|
||||||
|
git checkout tags/${{ inputs.version }}
|
||||||
|
./emsdk install ${{ inputs.version }}
|
||||||
|
./emsdk activate ${{ inputs.version }}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
# example:
|
||||||
|
#
|
||||||
|
# - name: get llvm
|
||||||
|
# uses: ./.github/actions/get-llvm
|
||||||
|
# with:
|
||||||
|
# releasePrefix: llvm-
|
||||||
|
# artifactArch: macos-arm64
|
||||||
|
# dir: target-llvm/macos
|
||||||
|
|
||||||
|
name: "get llvm"
|
||||||
|
inputs:
|
||||||
|
artifactArch:
|
||||||
|
required: true
|
||||||
|
releasePrefix:
|
||||||
|
description: "LLVM release tag prefix to search"
|
||||||
|
required: false
|
||||||
|
default: "llvm-"
|
||||||
|
dir:
|
||||||
|
description: "Archive extract path (`tar -C`)"
|
||||||
|
required: false
|
||||||
|
default: "./"
|
||||||
|
stripComponents:
|
||||||
|
description: "Strip UMBER leading components from file names on extraction (`tar --strip-components`)"
|
||||||
|
required: false
|
||||||
|
default: 0
|
||||||
|
|
||||||
|
|
||||||
|
runs:
|
||||||
|
using: "composite"
|
||||||
|
steps:
|
||||||
|
- name: find asset
|
||||||
|
id: find
|
||||||
|
uses: actions/github-script@v7
|
||||||
|
env:
|
||||||
|
releasePrefix: ${{ inputs.releasePrefix }}
|
||||||
|
artifactArch: ${{ inputs.artifactArch }}
|
||||||
|
with:
|
||||||
|
result-encoding: string
|
||||||
|
script: |
|
||||||
|
let page = 1;
|
||||||
|
let releases = [];
|
||||||
|
|
||||||
|
let releasePrefix = process.env.releasePrefix
|
||||||
|
let artifactArch = process.env.artifactArch
|
||||||
|
|
||||||
|
do {
|
||||||
|
const res = await github.rest.repos.listReleases({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
per_page: 50,
|
||||||
|
page,
|
||||||
|
});
|
||||||
|
|
||||||
|
releases = res.data
|
||||||
|
releases.sort((a, b) => {
|
||||||
|
return (a.published_at < b.published_at) ? 1 : ((a.published_at > b.published_at) ? -1 : 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
let llvmLatestRelease = releases.find(release => {
|
||||||
|
return release.tag_name.startsWith(releasePrefix);
|
||||||
|
});
|
||||||
|
if (llvmLatestRelease){
|
||||||
|
let asset = llvmLatestRelease.assets.find(asset =>{
|
||||||
|
return asset.name.includes(artifactArch);
|
||||||
|
});
|
||||||
|
if (!asset){
|
||||||
|
core.setFailed(`Artifact for '${artifactArch}' not found in release ${llvmLatestRelease.tag_name} (${llvmLatestRelease.html_url})`);
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
return asset.browser_download_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
page++;
|
||||||
|
} while(releases.length > 0);
|
||||||
|
|
||||||
|
core.setFailed(`No LLVM releases with '${releasePrefix}' atifacts found! Please release LLVM before running this workflow.`);
|
||||||
|
process.exit();
|
||||||
|
|
||||||
|
- name: download
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
mkdir -p ${{ inputs.dir }}
|
||||||
|
curl -sSLo llvm.tar.gz ${{ steps.find.outputs.result }}
|
||||||
|
ls -al
|
||||||
|
|
||||||
|
- name: unpack
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
tar -xf llvm.tar.gz -C ${{ inputs.dir }} --strip-components=${{ inputs.stripComponents }}
|
||||||
|
rm llvm.tar.gz
|
||||||
|
ls -al ${{ inputs.dir }}
|
||||||
@@ -6,63 +6,48 @@ on:
|
|||||||
branches: ["main"]
|
branches: ["main"]
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
REVIVE_WASM_INSTALL_DIR: ${{ github.workspace }}/target/wasm32-unknown-emscripten/release
|
REVIVE_WASM_INSTALL_DIR: ${{ github.workspace }}/target/wasm32-unknown-emscripten/release
|
||||||
BUN_VERSION: 1.1.43
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-revive-wasm:
|
build-revive-wasm:
|
||||||
runs-on: parity-large
|
runs-on: ubuntu-24.04
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
shell: bash
|
shell: bash
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Cache LLVM build
|
|
||||||
id: cache-llvm
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
target-llvm/emscripten/target-final
|
|
||||||
target-llvm/gnu/target-final
|
|
||||||
# Use a unique key based on LLVM version or configuration files to avoid cache invalidation
|
|
||||||
key: llvm-build-${{ runner.os }}-${{ hashFiles('LLVM.lock', 'Cargo.toml', 'Cargo.lock', 'crates/llvm-builder/**', '.github/workflows/build-revive-wasm.yml') }}
|
|
||||||
|
|
||||||
- name: Install system dependencies
|
|
||||||
run: |
|
|
||||||
sudo apt-get update && sudo apt-get install -y cmake ninja-build curl git libssl-dev pkg-config clang lld
|
|
||||||
|
|
||||||
- name: Install Rust stable toolchain
|
- name: Install Rust stable toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
profile: minimal
|
|
||||||
toolchain: stable
|
toolchain: stable
|
||||||
components: rust-src
|
components: rust-src
|
||||||
target: wasm32-unknown-emscripten
|
target: wasm32-unknown-emscripten
|
||||||
|
rustflags: ""
|
||||||
|
|
||||||
- name: Install LLVM build dependencies
|
- name: get llvm gnu
|
||||||
run: |
|
uses: ./.github/actions/get-llvm
|
||||||
make install-llvm-builder
|
with:
|
||||||
revive-llvm --target-env emscripten clone
|
artifactArch: x86_64-linux-gnu
|
||||||
|
- name: get llvm emscripten
|
||||||
|
uses: ./.github/actions/get-llvm
|
||||||
|
with:
|
||||||
|
artifactArch: emscripten
|
||||||
|
|
||||||
|
- name: install emsdk
|
||||||
|
uses: ./.github/actions/get-emsdk
|
||||||
|
|
||||||
- name: Setup revive environment variables
|
- name: Setup revive environment variables
|
||||||
run: |
|
run: |
|
||||||
echo "LLVM_SYS_181_PREFIX=$(pwd)/target-llvm/gnu/target-final" >> $GITHUB_ENV
|
echo "LLVM_SYS_181_PREFIX=$(pwd)/target-llvm/gnu/target-final" >> $GITHUB_ENV
|
||||||
echo "REVIVE_LLVM_TARGET_PREFIX=$(pwd)/target-llvm/emscripten/target-final" >> $GITHUB_ENV
|
echo "REVIVE_LLVM_TARGET_PREFIX=$(pwd)/target-llvm/emscripten/target-final" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Build host LLVM
|
|
||||||
if: steps.cache-llvm.outputs.cache-hit != 'true'
|
|
||||||
run: |
|
|
||||||
revive-llvm build --llvm-projects lld --llvm-projects clang
|
|
||||||
|
|
||||||
- name: Build target LLVM
|
|
||||||
if: steps.cache-llvm.outputs.cache-hit != 'true'
|
|
||||||
run: |
|
|
||||||
source emsdk/emsdk_env.sh
|
|
||||||
revive-llvm --target-env emscripten build --llvm-projects lld
|
|
||||||
|
|
||||||
- run: |
|
- run: |
|
||||||
rustup show
|
rustup show
|
||||||
cargo --version
|
cargo --version
|
||||||
@@ -71,11 +56,6 @@ jobs:
|
|||||||
cmake --version
|
cmake --version
|
||||||
bash --version
|
bash --version
|
||||||
|
|
||||||
- name: Use Cached LLVM
|
|
||||||
if: steps.cache-llvm.outputs.cache-hit == 'true'
|
|
||||||
run: |
|
|
||||||
echo "Using cached LLVM"
|
|
||||||
|
|
||||||
- name: Build revive
|
- name: Build revive
|
||||||
run: |
|
run: |
|
||||||
source emsdk/emsdk_env.sh
|
source emsdk/emsdk_env.sh
|
||||||
@@ -87,6 +67,7 @@ jobs:
|
|||||||
path: |
|
path: |
|
||||||
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc.js
|
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc.js
|
||||||
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc.wasm
|
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc.wasm
|
||||||
|
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc_web.js
|
||||||
retention-days: 1
|
retention-days: 1
|
||||||
|
|
||||||
test-revive-wasm:
|
test-revive-wasm:
|
||||||
@@ -112,21 +93,6 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
node-version: "20"
|
node-version: "20"
|
||||||
|
|
||||||
- name: Install Bun on Windows
|
|
||||||
if: runner.os == 'Windows'
|
|
||||||
run: |
|
|
||||||
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
|
|
||||||
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
|
|
||||||
scoop install bun@${{ env.BUN_VERSION }}
|
|
||||||
scoop install wget
|
|
||||||
Join-Path (Resolve-Path ~).Path "scoop\shims" >> $Env:GITHUB_PATH
|
|
||||||
|
|
||||||
- name: Install Bun on macOS and Linux
|
|
||||||
if: runner.os != 'Windows'
|
|
||||||
run: |
|
|
||||||
curl -fsSL https://bun.sh/install | bash -s bun-v${{ env.BUN_VERSION }}
|
|
||||||
echo "$HOME/.bun/bin" >> $GITHUB_PATH
|
|
||||||
|
|
||||||
- name: Install packages
|
- name: Install packages
|
||||||
run: npm install
|
run: npm install
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,171 @@
|
|||||||
|
name: Release LLVM
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
llvm_version:
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
description: llvm version in "x.x.x" format, e.g. "18.1.8"
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
env:
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
create-release:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
outputs:
|
||||||
|
version: ${{ steps.resolve-version.outputs.version }}
|
||||||
|
steps:
|
||||||
|
- id: resolve-version
|
||||||
|
run: |
|
||||||
|
echo "version=llvm-${{ inputs.llvm_version }}-revive.${GITHUB_SHA:0:7}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: create release
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
name: "LLVM binaries release: ${{ steps.resolve-version.outputs.version }}"
|
||||||
|
body: "This release includes binaries of LLVM, used to compile revive itself"
|
||||||
|
make_latest: "false"
|
||||||
|
tag_name: ${{ steps.resolve-version.outputs.version }}
|
||||||
|
|
||||||
|
build-macos:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [macos-14, macos-13]
|
||||||
|
include:
|
||||||
|
- os: macos-13
|
||||||
|
arch: x64
|
||||||
|
- os: macos-14
|
||||||
|
arch: arm64
|
||||||
|
needs: create-release
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
name: "build-macos-${{ matrix.arch }}"
|
||||||
|
env:
|
||||||
|
RUST_LOG: trace
|
||||||
|
permissions:
|
||||||
|
contents: write # for uploading assets to release
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: install macos deps
|
||||||
|
run: |
|
||||||
|
brew install ninja
|
||||||
|
|
||||||
|
- name: versions
|
||||||
|
run: |
|
||||||
|
rustup show
|
||||||
|
cargo --version
|
||||||
|
cmake --version
|
||||||
|
echo "bash:" && bash --version
|
||||||
|
echo "ninja:" && ninja --version
|
||||||
|
echo "clang:" && clang --version
|
||||||
|
|
||||||
|
- name: Build LLVM
|
||||||
|
run: |
|
||||||
|
make install-llvm
|
||||||
|
|
||||||
|
- name: clean
|
||||||
|
# check removed files
|
||||||
|
run: |
|
||||||
|
cd target-llvm/gnu/target-final/bin/
|
||||||
|
rm diagtool llvm-libtool-darwin llvm-lipo llvm-pdbutil llvm-dwarfdump llvm-nm llvm-readobj llvm-cfi-verify \
|
||||||
|
sancov llvm-debuginfo-analyzer llvm-objdump llvm-profgen llvm-extract llvm-jitlink llvm-c-test llvm-gsymutil llvm-dwp \
|
||||||
|
dsymutil llvm-dwarfutil llvm-exegesis lli clang-rename bugpoint clang-extdef-mapping clang-refactor c-index-test \
|
||||||
|
llvm-reduce llvm-lto clang-linker-wrapper llc llvm-lto2
|
||||||
|
|
||||||
|
- name: package artifacts
|
||||||
|
run: |
|
||||||
|
tar -czf "${{ needs.create-release.outputs.version }}-macos-${{ matrix.arch }}.tar.gz" target-llvm/gnu/target-final
|
||||||
|
|
||||||
|
- name: upload archive to release
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
make_latest: "false"
|
||||||
|
tag_name: ${{ needs.create-release.outputs.version }}
|
||||||
|
files: |
|
||||||
|
${{ needs.create-release.outputs.version }}-macos-${{ matrix.arch }}.tar.gz
|
||||||
|
|
||||||
|
|
||||||
|
build-linux-all:
|
||||||
|
needs: create-release
|
||||||
|
runs-on: parity-large
|
||||||
|
env:
|
||||||
|
RUST_LOG: trace
|
||||||
|
permissions:
|
||||||
|
contents: write # for uploading assets to release
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: install linux deps
|
||||||
|
run: |
|
||||||
|
sudo apt-get update && sudo apt-get install -y cmake ninja-build curl git libssl-dev pkg-config clang lld musl
|
||||||
|
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
components: rust-src
|
||||||
|
target: wasm32-unknown-emscripten
|
||||||
|
rustflags: ""
|
||||||
|
|
||||||
|
- name: versions
|
||||||
|
run: |
|
||||||
|
rustup show
|
||||||
|
cargo --version
|
||||||
|
cmake --version
|
||||||
|
echo "bash:" && bash --version
|
||||||
|
echo "ninja:" && ninja --version
|
||||||
|
echo "clang:" && clang --version
|
||||||
|
|
||||||
|
- name: Build host LLVM
|
||||||
|
run: |
|
||||||
|
make install-llvm
|
||||||
|
|
||||||
|
- name: Build gnu LLVM
|
||||||
|
run: |
|
||||||
|
revive-llvm clone
|
||||||
|
revive-llvm build --llvm-projects lld --llvm-projects clang
|
||||||
|
|
||||||
|
- name: Build musl LLVM
|
||||||
|
run: |
|
||||||
|
revive-llvm --target-env musl build --llvm-projects lld --llvm-projects clang
|
||||||
|
|
||||||
|
- name: Build emscripten LLVM
|
||||||
|
run: |
|
||||||
|
revive-llvm --target-env emscripten clone
|
||||||
|
source emsdk/emsdk_env.sh
|
||||||
|
revive-llvm --target-env emscripten build --llvm-projects lld
|
||||||
|
|
||||||
|
- name: clean
|
||||||
|
# check removed files
|
||||||
|
run: |
|
||||||
|
for target in gnu emscripten musl; do
|
||||||
|
cd target-llvm/${target}/target-final/bin/
|
||||||
|
rm -rf diagtool llvm-libtool-darwin llvm-lipo llvm-pdbutil llvm-dwarfdump llvm-nm llvm-readobj llvm-cfi-verify \
|
||||||
|
sancov llvm-debuginfo-analyzer llvm-objdump llvm-profgen llvm-extract llvm-jitlink llvm-c-test llvm-gsymutil llvm-dwp \
|
||||||
|
dsymutil llvm-dwarfutil llvm-exegesis lli clang-rename bugpoint clang-extdef-mapping clang-refactor c-index-test \
|
||||||
|
llvm-reduce llvm-lto clang-linker-wrapper llc llvm-lto2 llvm-otool llvm-readelf
|
||||||
|
cd -
|
||||||
|
done
|
||||||
|
|
||||||
|
- name: package artifacts
|
||||||
|
run: |
|
||||||
|
tar -czf "${{ needs.create-release.outputs.version }}-x86_64-linux-gnu-linux.tar.gz" target-llvm/gnu/target-final
|
||||||
|
tar -czf "${{ needs.create-release.outputs.version }}-x86_64-linux-musl.tar.gz" target-llvm/musl/target-final
|
||||||
|
tar -czf "${{ needs.create-release.outputs.version }}-wasm32-unknown-emscripten.tar.gz" target-llvm/emscripten/target-final
|
||||||
|
|
||||||
|
- name: upload archive to release
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
make_latest: "false"
|
||||||
|
tag_name: ${{ needs.create-release.outputs.version }}
|
||||||
|
files: |
|
||||||
|
${{ needs.create-release.outputs.version }}-x86_64-linux-gnu-linux.tar.gz
|
||||||
|
${{ needs.create-release.outputs.version }}-x86_64-linux-musl.tar.gz
|
||||||
|
${{ needs.create-release.outputs.version }}-wasm32-unknown-emscripten.tar.gz
|
||||||
@@ -0,0 +1,339 @@
|
|||||||
|
name: Release
|
||||||
|
run-name: Release ${{ github.ref_name }}
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- "main"
|
||||||
|
pull_request:
|
||||||
|
types: [opened, synchronize, reopened, ready_for_review, labeled]
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
env:
|
||||||
|
#rust-musl-cross:x86_64-musl
|
||||||
|
RUST_MUSL_CROSS_IMAGE: messense/rust-musl-cross@sha256:68b86bc7cb2867259e6b233415a665ff4469c28b57763e78c3bfea1c68091561
|
||||||
|
RUST_LOG: trace
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tag:
|
||||||
|
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'release-test')
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
outputs:
|
||||||
|
TAG: ${{ steps.versions.outputs.TAG }}
|
||||||
|
PKG_VER: ${{ steps.versions.outputs.PKG_VER }}
|
||||||
|
RELEASE_NOTES: ${{ steps.versions.outputs.RELEASE_NOTES }}
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-tags: "true"
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Versions
|
||||||
|
id: versions
|
||||||
|
run: |
|
||||||
|
export CURRENT_TAG=$(git describe --tags --abbrev=0 --exclude "llvm-*")
|
||||||
|
export PKG_VER=v$(cat Cargo.toml | grep -A 5 package] | grep version | cut -d '=' -f 2 | tr -d '"' | tr -d " ")
|
||||||
|
echo "Current tag $CURRENT_TAG"
|
||||||
|
echo "Package version $PKG_VER"
|
||||||
|
#
|
||||||
|
echo "PKG_VER=$PKG_VER" >> $GITHUB_OUTPUT
|
||||||
|
if [[ $CURRENT_TAG == $PKG_VER ]];
|
||||||
|
then
|
||||||
|
echo "Tag is up to date. Nothing to do.";
|
||||||
|
export TAG=old;
|
||||||
|
else
|
||||||
|
echo "Tag was updated.";
|
||||||
|
export TAG=new;
|
||||||
|
fi
|
||||||
|
echo "TAG=$TAG" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
# Generating release notes early, in order to avoid checkout at the last step
|
||||||
|
export RELEASE_NOTES="$(sed '/^## '${PKG_VER}'/,/^## v/!d' CHANGELOG.md | sed -e '1d' -e '$d')"
|
||||||
|
|
||||||
|
echo "Release notes:"
|
||||||
|
echo "$RELEASE_NOTES"
|
||||||
|
|
||||||
|
echo 'RELEASE_NOTES<<EOF' >> $GITHUB_OUTPUT
|
||||||
|
echo "$RELEASE_NOTES" >> $GITHUB_OUTPUT
|
||||||
|
echo 'EOF' >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
build-macos:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [macos-14, macos-13]
|
||||||
|
include:
|
||||||
|
- os: macos-13
|
||||||
|
arch: x64
|
||||||
|
- os: macos-14
|
||||||
|
arch: arm64
|
||||||
|
if: ${{ needs.tag.outputs.TAG == 'new' }}
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
name: build-macos
|
||||||
|
needs: [tag]
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: get llvm
|
||||||
|
uses: ./.github/actions/get-llvm
|
||||||
|
with:
|
||||||
|
releasePrefix: llvm-
|
||||||
|
artifactArch: macos-${{ matrix.arch }}
|
||||||
|
dir: ./
|
||||||
|
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
components: rust-src
|
||||||
|
target: wasm32-unknown-emscripten
|
||||||
|
rustflags: ""
|
||||||
|
|
||||||
|
- name: install macos deps
|
||||||
|
run: |
|
||||||
|
brew install ninja
|
||||||
|
|
||||||
|
- name: versions
|
||||||
|
run: |
|
||||||
|
rustup show
|
||||||
|
cargo --version
|
||||||
|
cmake --version
|
||||||
|
echo "bash:" && bash --version
|
||||||
|
echo "ninja:" && ninja --version
|
||||||
|
echo "clang:" && clang --version
|
||||||
|
|
||||||
|
- name: build revive
|
||||||
|
run: |
|
||||||
|
export LLVM_SYS_181_PREFIX=$PWD/target-llvm/gnu/target-final
|
||||||
|
make install-bin
|
||||||
|
cp ./target/release/resolc ./target/release/resolc-${{ matrix.arch }}
|
||||||
|
|
||||||
|
- name: check revive
|
||||||
|
run: |
|
||||||
|
mkdir solc
|
||||||
|
curl -sSLo solc/solc https://github.com/ethereum/solidity/releases/download/v0.8.28/solc-macos
|
||||||
|
chmod +x solc/solc
|
||||||
|
PATH=$PWD/solc:$PATH
|
||||||
|
result=$(./target/release/resolc-${{ matrix.arch }} --bin crates/integration/contracts/flipper.sol)
|
||||||
|
echo $result
|
||||||
|
if [[ $result == *'0x50564d'* ]]; then exit 0; else exit 1; fi
|
||||||
|
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: "revive-macos-${{ matrix.arch }}"
|
||||||
|
path: |
|
||||||
|
./target/release/resolc-${{ matrix.arch }}
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
macos-universal-binary:
|
||||||
|
runs-on: macos-14
|
||||||
|
needs: [build-macos]
|
||||||
|
steps:
|
||||||
|
- uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
pattern: revive-macos-*
|
||||||
|
path: revive-macos
|
||||||
|
|
||||||
|
- name: run lipo
|
||||||
|
run: |
|
||||||
|
lipo revive-macos/revive-macos-arm64/resolc-arm64 revive-macos/revive-macos-x64/resolc-x64 -create -output resolc-macos
|
||||||
|
chmod +x resolc-macos
|
||||||
|
|
||||||
|
- name: compress macos artifact
|
||||||
|
run: |
|
||||||
|
tar -czf resolc-macos.tar.gz ./resolc-macos
|
||||||
|
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: resolc-macos.tar.gz
|
||||||
|
path: |
|
||||||
|
resolc-macos.tar.gz
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
build-linux-all:
|
||||||
|
if: ${{ needs.tag.outputs.TAG == 'new' }}
|
||||||
|
runs-on: parity-large
|
||||||
|
needs: [tag]
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: install linux deps
|
||||||
|
run: |
|
||||||
|
sudo apt-get update && sudo apt-get install -y cmake ninja-build \
|
||||||
|
curl git libssl-dev pkg-config clang lld musl
|
||||||
|
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
components: rust-src
|
||||||
|
target: wasm32-unknown-emscripten
|
||||||
|
rustflags: ""
|
||||||
|
|
||||||
|
- name: versions
|
||||||
|
run: |
|
||||||
|
rustup show
|
||||||
|
cargo --version
|
||||||
|
cmake --version
|
||||||
|
echo "bash:" && bash --version
|
||||||
|
echo "ninja:" && ninja --version
|
||||||
|
echo "clang:" && clang --version
|
||||||
|
|
||||||
|
- name: get llvm musl
|
||||||
|
uses: ./.github/actions/get-llvm
|
||||||
|
with:
|
||||||
|
releasePrefix: llvm-
|
||||||
|
artifactArch: x86_64-linux-musl
|
||||||
|
dir: ./
|
||||||
|
|
||||||
|
# Build revive
|
||||||
|
|
||||||
|
- name: build musl
|
||||||
|
run: |
|
||||||
|
mkdir resolc-out
|
||||||
|
docker run -v $PWD:/opt/revive $RUST_MUSL_CROSS_IMAGE /bin/bash -c "
|
||||||
|
cd /opt/revive
|
||||||
|
apt update && apt upgrade -y && apt install -y pkg-config
|
||||||
|
export LLVM_SYS_181_PREFIX=/opt/revive/target-llvm/musl/target-final
|
||||||
|
make install-bin
|
||||||
|
cp /root/.cargo/bin/resolc /opt/revive/resolc-out/resolc-static-linux
|
||||||
|
"
|
||||||
|
|
||||||
|
- name: check musl
|
||||||
|
run: |
|
||||||
|
mkdir solc
|
||||||
|
curl -sSLo solc/solc https://github.com/ethereum/solidity/releases/download/v0.8.28/solc-static-linux
|
||||||
|
chmod +x solc/solc
|
||||||
|
PATH=$PWD/solc:$PATH
|
||||||
|
result=$(./resolc-out/resolc-static-linux --bin crates/integration/contracts/flipper.sol)
|
||||||
|
echo $result
|
||||||
|
if [[ $result == *'0x50564d'* ]]; then exit 0; else exit 1; fi
|
||||||
|
|
||||||
|
- name: compress musl artifact
|
||||||
|
run: |
|
||||||
|
tar -czf $(pwd)/resolc-static-linux.tar.gz -C ./resolc-out resolc-static-linux
|
||||||
|
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: resolc-static-linux.tar.gz
|
||||||
|
path: |
|
||||||
|
resolc-static-linux.tar.gz
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
- name: Set Up Node.js
|
||||||
|
uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
node-version: "20"
|
||||||
|
|
||||||
|
- name: get llvm emscripten
|
||||||
|
uses: ./.github/actions/get-llvm
|
||||||
|
with:
|
||||||
|
artifactArch: emscripten
|
||||||
|
|
||||||
|
- name: install emsdk
|
||||||
|
uses: ./.github/actions/get-emsdk
|
||||||
|
|
||||||
|
- name: build wasm
|
||||||
|
run: |
|
||||||
|
export LLVM_SYS_181_PREFIX=$PWD/target-llvm/musl/target-final
|
||||||
|
export REVIVE_LLVM_TARGET_PREFIX=$PWD/target-llvm/emscripten/target-final
|
||||||
|
source emsdk/emsdk_env.sh
|
||||||
|
rustup target add wasm32-unknown-emscripten
|
||||||
|
make install-wasm
|
||||||
|
chmod -x ./target/wasm32-unknown-emscripten/release/resolc.wasm
|
||||||
|
|
||||||
|
- name: check wasm
|
||||||
|
run: |
|
||||||
|
curl -sSLo solc/soljson.js https://github.com/ethereum/solidity/releases/download/v0.8.28/soljson.js
|
||||||
|
node -e "
|
||||||
|
const soljson = require('solc/soljson');
|
||||||
|
const createRevive = require('./target/wasm32-unknown-emscripten/release/resolc.js');
|
||||||
|
|
||||||
|
const compiler = createRevive();
|
||||||
|
compiler.soljson = soljson;
|
||||||
|
|
||||||
|
const standardJsonInput =
|
||||||
|
{
|
||||||
|
language: 'Solidity',
|
||||||
|
sources: {
|
||||||
|
'MyContract.sol': {
|
||||||
|
content: 'pragma solidity ^0.8.0; contract MyContract { function greet() public pure returns (string memory) { return \'Hello\'; } }',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
settings: { optimizer: { enabled: false } }
|
||||||
|
};
|
||||||
|
|
||||||
|
compiler.writeToStdin(JSON.stringify(standardJsonInput));
|
||||||
|
compiler.callMain(['--standard-json']);
|
||||||
|
|
||||||
|
// Collect output
|
||||||
|
const stdout = compiler.readFromStdout();
|
||||||
|
const stderr = compiler.readFromStderr();
|
||||||
|
|
||||||
|
if (stderr) { console.error(stderr); process.exit(1); }
|
||||||
|
|
||||||
|
let out = JSON.parse(stdout);
|
||||||
|
let bytecode = out.contracts['MyContract.sol']['MyContract'].evm.bytecode.object
|
||||||
|
console.log(bytecode);
|
||||||
|
|
||||||
|
if(!bytecode.startsWith('50564d')) { process.exit(1); }
|
||||||
|
"
|
||||||
|
|
||||||
|
- name: compress wasm artifact
|
||||||
|
run: |
|
||||||
|
tar -czf $(pwd)/resolc-wasm.tar.gz -C ./target/wasm32-unknown-emscripten/release/ \
|
||||||
|
resolc.js \
|
||||||
|
resolc.wasm \
|
||||||
|
resolc_web.js
|
||||||
|
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: resolc-wasm.tar.gz
|
||||||
|
path: |
|
||||||
|
resolc-wasm.tar.gz
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
create-release:
|
||||||
|
if: github.event_name != 'pull_request'
|
||||||
|
needs: [tag, build-linux-all, macos-universal-binary]
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
steps:
|
||||||
|
- name: Download revive-wasm
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: resolc-wasm.tar.gz
|
||||||
|
path: resolc-wasm/
|
||||||
|
|
||||||
|
- name: Download revive-linux
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: resolc-static-linux.tar.gz
|
||||||
|
path: resolc-linux/
|
||||||
|
|
||||||
|
- name: Download revive-macos
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: resolc-macos.tar.gz
|
||||||
|
path: resolc-macos/
|
||||||
|
|
||||||
|
- name: create-release
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
body: |
|
||||||
|
${{ needs.tag.outputs.RELEASE_NOTES }}
|
||||||
|
|
||||||
|
# Note for macOS Users
|
||||||
|
The macOS binary is unsigned and it needs to be made runnable using `xattr -c resolc-macos`.
|
||||||
|
tag_name: ${{ needs.tag.outputs.PKG_VER }}
|
||||||
|
name: ${{ needs.tag.outputs.PKG_VER }}
|
||||||
|
draft: true
|
||||||
|
target_commitish: ${{ github.sha }}
|
||||||
|
files: |
|
||||||
|
./resolc-linux/resolc-static-linux.tar.gz
|
||||||
|
./resolc-macos/resolc-macos.tar.gz
|
||||||
|
./resolc-wasm/resolc-wasm.tar.gz
|
||||||
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [assigned, opened, synchronize, reopened]
|
||||||
|
paths:
|
||||||
|
- 'LLVM.lock'
|
||||||
|
- 'crates/llvm-builder/**'
|
||||||
|
- '.github/workflows/revive-llvm-test.yml'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
runner: [parity-large, macos-14, macos-13]
|
||||||
|
runs-on: ${{ matrix.runner }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install apt dependencies
|
||||||
|
if: matrix.runner == 'parity-large'
|
||||||
|
run: |
|
||||||
|
sudo apt update && sudo apt-get install -y cmake ninja-build curl git libssl-dev pkg-config clang lld musl
|
||||||
|
|
||||||
|
- name: Install macos dependencies
|
||||||
|
if: matrix.runner == 'macos-14' || matrix.runner == 'macos-13'
|
||||||
|
run: |
|
||||||
|
brew install ninja
|
||||||
|
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
components: rust-src
|
||||||
|
rustflags: ""
|
||||||
|
|
||||||
|
- run: |
|
||||||
|
rustup show
|
||||||
|
cargo --version
|
||||||
|
cmake --version
|
||||||
|
bash --version
|
||||||
|
|
||||||
|
- name: Test llvm-builder
|
||||||
|
run: make test-llvm-builder
|
||||||
|
env:
|
||||||
|
RUST_LOG: trace
|
||||||
@@ -6,6 +6,10 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches: ["main"]
|
branches: ["main"]
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
@@ -30,14 +34,16 @@ jobs:
|
|||||||
tar Jxf llvm.tar.xz -C llvm18/
|
tar Jxf llvm.tar.xz -C llvm18/
|
||||||
echo "LLVM_SYS_181_PREFIX=$(pwd)/llvm18" >> $GITHUB_ENV
|
echo "LLVM_SYS_181_PREFIX=$(pwd)/llvm18" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Install apt dependencies
|
- name: Install geth
|
||||||
run: |
|
run: |
|
||||||
sudo add-apt-repository -y ppa:ethereum/ethereum
|
sudo add-apt-repository -y ppa:ethereum/ethereum
|
||||||
sudo apt update
|
sudo apt update
|
||||||
sudo apt install -y ethereum
|
sudo apt install -y ethereum
|
||||||
|
|
||||||
- name: Machete
|
# Disabled for now (always install the latest version despite setting it):
|
||||||
uses: bnjbvr/cargo-machete@main
|
# https://github.com/bnjbvr/cargo-machete/issues/156
|
||||||
|
#- name: Machete
|
||||||
|
# uses: bnjbvr/cargo-machete@v0.7.0
|
||||||
|
|
||||||
- name: Format
|
- name: Format
|
||||||
run: make format
|
run: make format
|
||||||
|
|||||||
@@ -2,6 +2,77 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
This is a development pre-release.
|
||||||
|
|
||||||
|
Supported `polkadot-sdk` rev:`c29e72a8628835e34deb6aa7db9a78a2e4eabcee`
|
||||||
|
|
||||||
|
## v0.1.0-dev.12
|
||||||
|
|
||||||
|
This is a development pre-release.
|
||||||
|
|
||||||
|
Supported `polkadot-sdk` rev: `21f6f0705e53c15aa2b8a5706b208200447774a9`
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Per file output selection for `--standard-json` mode.
|
||||||
|
- The `ir` output selection option for `--standard-json` mode.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Improved code size: Large contracts compile to smaller code blobs when enabling aggressive size optimizations (`-Oz`).
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
## v0.1.0-dev.11
|
||||||
|
|
||||||
|
This is a development pre-release.
|
||||||
|
|
||||||
|
Supported `polkadot-sdk` rev: `274a781e8ca1a9432c7ec87593bd93214abbff50`
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- A bug causing incorrect loads from the emulated EVM linear memory.
|
||||||
|
- A missing integer truncate after switching to 64bit.
|
||||||
|
|
||||||
|
## v0.1.0-dev.10
|
||||||
|
|
||||||
|
This is a development pre-release.
|
||||||
|
|
||||||
|
Supported `polkadot-sdk` rev: `274a781e8ca1a9432c7ec87593bd93214abbff50`
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Support for the `coinbase` opcode.
|
||||||
|
- The resolc web JS version.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Missing the `--overwrite` flag emits an error instead of a warning.
|
||||||
|
- The `resolc` executable prints the help by default.
|
||||||
|
- Removed support for legacy EVM assembly (EVMLA) translation.
|
||||||
|
- integration: identify cached code blobs on source code to fix potential confusions.
|
||||||
|
- Setting base, include or allow paths in emscripten is now a hard error.
|
||||||
|
- Employ a heuristic to detect `address.transfer` and `address.send` calls.
|
||||||
|
If detected, the re-entrant call flag is not set and 0 deposit limit is endowed.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Solidity: Add the solc `--libraries` files to sources.
|
||||||
|
- A data race in tests.
|
||||||
|
- Fix `broken pipe` errors.
|
||||||
|
- llvm-builder: Allow warnings.
|
||||||
|
- solidity: Fix the custom compiler warning messages.
|
||||||
|
|
||||||
|
## v0.1.0-dev.9
|
||||||
|
|
||||||
|
This is a development pre-release.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Syscalls with more than 6 arguments now pack them into registers.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Remove reloading of the resolc.js file (fix issue with relative path in web worker)
|
||||||
|
|
||||||
## v0.1.0-dev.8
|
## v0.1.0-dev.8
|
||||||
|
|
||||||
This is a development pre-release.
|
This is a development pre-release.
|
||||||
|
|||||||
Generated
+978
-937
File diff suppressed because it is too large
Load Diff
+33
-34
@@ -3,7 +3,7 @@ resolver = "2"
|
|||||||
members = ["crates/*"]
|
members = ["crates/*"]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.1.0-dev.8"
|
version = "0.1.0-dev.12"
|
||||||
authors = [
|
authors = [
|
||||||
"Cyrill Leutwiler <cyrill@parity.io>",
|
"Cyrill Leutwiler <cyrill@parity.io>",
|
||||||
"Parity Technologies <admin@parity.io>",
|
"Parity Technologies <admin@parity.io>",
|
||||||
@@ -14,48 +14,47 @@ repository = "https://github.com/paritytech/revive"
|
|||||||
rust-version = "1.81.0"
|
rust-version = "1.81.0"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
revive-benchmarks = { version = "0.1.0-dev.8", path = "crates/benchmarks" }
|
revive-benchmarks = { version = "0.1.0-dev.12", path = "crates/benchmarks" }
|
||||||
revive-builtins = { version = "0.1.0-dev.8", path = "crates/builtins" }
|
revive-builtins = { version = "0.1.0-dev.12", path = "crates/builtins" }
|
||||||
revive-common = { version = "0.1.0-dev.8", path = "crates/common" }
|
revive-common = { version = "0.1.0-dev.12", path = "crates/common" }
|
||||||
revive-differential = { version = "0.1.0-dev.8", path = "crates/differential" }
|
revive-differential = { version = "0.1.0-dev.12", path = "crates/differential" }
|
||||||
revive-integration = { version = "0.1.0-dev.8", path = "crates/integration" }
|
revive-integration = { version = "0.1.0-dev.12", path = "crates/integration" }
|
||||||
revive-linker = { version = "0.1.0-dev.8", path = "crates/linker" }
|
revive-linker = { version = "0.1.0-dev.12", path = "crates/linker" }
|
||||||
lld-sys = { version = "0.1.0-dev.8", path = "crates/lld-sys" }
|
lld-sys = { version = "0.1.0-dev.12", path = "crates/lld-sys" }
|
||||||
revive-llvm-context = { version = "0.1.0-dev.8", path = "crates/llvm-context" }
|
revive-llvm-context = { version = "0.1.0-dev.12", path = "crates/llvm-context" }
|
||||||
revive-runtime-api = { version = "0.1.0-dev.8", path = "crates/runtime-api" }
|
revive-runtime-api = { version = "0.1.0-dev.12", path = "crates/runtime-api" }
|
||||||
revive-runner = { version = "0.1.0-dev.8", path = "crates/runner" }
|
revive-runner = { version = "0.1.0-dev.12", path = "crates/runner" }
|
||||||
revive-solidity = { version = "0.1.0-dev.8", path = "crates/solidity" }
|
revive-solidity = { version = "0.1.0-dev.12", path = "crates/solidity" }
|
||||||
revive-stdlib = { version = "0.1.0-dev.8", path = "crates/stdlib" }
|
revive-stdlib = { version = "0.1.0-dev.12", path = "crates/stdlib" }
|
||||||
revive-build-utils = { version = "0.1.0-dev.8", path = "crates/build-utils" }
|
revive-build-utils = { version = "0.1.0-dev.12", path = "crates/build-utils" }
|
||||||
|
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
cc = "1.0"
|
cc = "1.2"
|
||||||
libc = "0.2.169"
|
libc = "0.2.169"
|
||||||
tempfile = "3.8"
|
tempfile = "3.17"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
semver = { version = "1.0", features = [ "serde" ] }
|
semver = { version = "1.0", features = ["serde"] }
|
||||||
itertools = "0.14"
|
itertools = "0.14"
|
||||||
serde = { version = "1.0", features = [ "derive" ] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = { version = "1.0", features = [ "arbitrary_precision" ] }
|
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
|
||||||
regex = "1.10"
|
regex = "1.10"
|
||||||
once_cell = "1.19"
|
once_cell = "1.20"
|
||||||
num = "0.4.3"
|
num = "0.4.3"
|
||||||
sha1 = "0.10"
|
sha1 = "0.10"
|
||||||
sha3 = "0.10"
|
sha3 = "0.10"
|
||||||
md5 = "0.7.0"
|
|
||||||
thiserror = "2.0"
|
thiserror = "2.0"
|
||||||
which = "7.0"
|
which = "7.0"
|
||||||
path-slash = "0.2"
|
path-slash = "0.2"
|
||||||
rayon = "1.8"
|
rayon = "1.8"
|
||||||
clap = { version = "4", default-features = false, features = ["derive"] }
|
clap = { version = "4", default-features = false, features = ["derive"] }
|
||||||
polkavm-common = "0.19.0"
|
polkavm-common = "0.21.0"
|
||||||
polkavm-linker = "0.19.0"
|
polkavm-linker = "0.21.0"
|
||||||
polkavm-disassembler = "0.19.0"
|
polkavm-disassembler = "0.21.0"
|
||||||
polkavm = "0.19.0"
|
polkavm = "0.21.0"
|
||||||
alloy-primitives = { version = "0.8.19", features = ["serde"] }
|
alloy-primitives = { version = "0.8.21", features = ["serde"] }
|
||||||
alloy-sol-types = "0.8.19"
|
alloy-sol-types = "0.8.21"
|
||||||
alloy-genesis = "0.9.2"
|
alloy-genesis = "0.11.1"
|
||||||
alloy-serde = "0.9.2"
|
alloy-serde = "0.11.1"
|
||||||
env_logger = { version = "0.11.6", default-features = false }
|
env_logger = { version = "0.11.6", default-features = false }
|
||||||
serde_stacker = "0.1.11"
|
serde_stacker = "0.1.11"
|
||||||
criterion = { version = "0.5.1", features = ["html_reports"] }
|
criterion = { version = "0.5.1", features = ["html_reports"] }
|
||||||
@@ -65,15 +64,15 @@ downloader = "0.2.8"
|
|||||||
flate2 = "1.0.35"
|
flate2 = "1.0.35"
|
||||||
fs_extra = "1.3.0"
|
fs_extra = "1.3.0"
|
||||||
num_cpus = "1"
|
num_cpus = "1"
|
||||||
tar = "0.4.43"
|
tar = "0.4"
|
||||||
toml = "0.8.19"
|
toml = "0.8"
|
||||||
assert_cmd = "2.0.16"
|
assert_cmd = "2.0"
|
||||||
assert_fs = "1.1.2"
|
assert_fs = "1.1"
|
||||||
|
|
||||||
# polkadot-sdk and friends
|
# polkadot-sdk and friends
|
||||||
codec = { version = "3.6.12", default-features = false, package = "parity-scale-codec" }
|
codec = { version = "3.6.12", default-features = false, package = "parity-scale-codec" }
|
||||||
scale-info = { version = "2.11.6", default-features = false }
|
scale-info = { version = "2.11.6", default-features = false }
|
||||||
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", rev = "d62a90c8c729acd98c7e9a5cab9803b8b211ffc5" }
|
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c29e72a8628835e34deb6aa7db9a78a2e4eabcee" }
|
||||||
|
|
||||||
# llvm
|
# llvm
|
||||||
[workspace.dependencies.inkwell]
|
[workspace.dependencies.inkwell]
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ install-npm:
|
|||||||
|
|
||||||
install-wasm: install-npm
|
install-wasm: install-npm
|
||||||
cargo build --target wasm32-unknown-emscripten -p revive-solidity --release --no-default-features
|
cargo build --target wasm32-unknown-emscripten -p revive-solidity --release --no-default-features
|
||||||
|
npm run build:package
|
||||||
|
|
||||||
install-llvm-builder:
|
install-llvm-builder:
|
||||||
cargo install --path crates/llvm-builder
|
cargo install --path crates/llvm-builder
|
||||||
@@ -42,7 +43,7 @@ format:
|
|||||||
cargo fmt --all --check
|
cargo fmt --all --check
|
||||||
|
|
||||||
clippy:
|
clippy:
|
||||||
cargo clippy --all-features --workspace --tests --benches -- --deny warnings --allow dead_code
|
cargo clippy --all-features --workspace --tests --benches -- --deny warnings
|
||||||
|
|
||||||
machete:
|
machete:
|
||||||
cargo install cargo-machete
|
cargo install cargo-machete
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||

|

|
||||||
[](https://contracts.polkadot.io)
|
[](https://contracts.polkadot.io/revive_compiler/)
|
||||||
|
|
||||||
# revive
|
# revive
|
||||||
|
|
||||||
YUL and EVM assembly recompiler to LLVM, targetting RISC-V on [PolkaVM](https://github.com/koute/polkavm).
|
YUL recompiler to LLVM, targetting RISC-V on [PolkaVM](https://github.com/koute/polkavm).
|
||||||
|
|
||||||
Visit [contracts.polkadot.io](https://contracts.polkadot.io) to learn more about contracts on Polkadot!
|
Visit [contracts.polkadot.io](https://contracts.polkadot.io) to learn more about contracts on Polkadot!
|
||||||
|
|
||||||
@@ -14,10 +14,7 @@ This is experimental software in active development and not ready just yet for p
|
|||||||
Discussion around the development is hosted on the [Polkadot Forum](https://forum.polkadot.network/t/contracts-update-solidity-on-polkavm/6949#a-new-solidity-compiler-1).
|
Discussion around the development is hosted on the [Polkadot Forum](https://forum.polkadot.network/t/contracts-update-solidity-on-polkavm/6949#a-new-solidity-compiler-1).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
Please consult [the documentation](https://contracts.polkadot.io/revive_compiler/installation) for installation instructions.
|
||||||
`resolc` depends on the [solc](https://github.com/ethereum/solidity) binary installed on your system.
|
|
||||||
|
|
||||||
Download and install the `resolc` frontend executable for your platform from our [releases](https://github.com/paritytech/revive/releases).
|
|
||||||
|
|
||||||
## Building from source
|
## Building from source
|
||||||
|
|
||||||
@@ -25,22 +22,46 @@ Building revive requires a [stable Rust installation](https://rustup.rs/) and a
|
|||||||
|
|
||||||
### LLVM
|
### LLVM
|
||||||
|
|
||||||
`revive` depends on a custom build of LLVM `v18.1.8` with the RISC-V _embedded_ target, including the `compiler-rt` builtins. Use the provided [revive-llvm](crates/llvm-builder/README.md) utility to compile a compatible LLVM build locally and point `$LLVM_SYS_181_PREFIX` to the installation afterwards.
|
`revive` depends on a custom build of LLVM `v18.1.8` with the RISC-V _embedded_ target, including the `compiler-rt` builtins. You can either download a build from our releases (recommended for older hardware) or build it from source.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Download from our LLVM releases</summary>
|
||||||
|
|
||||||
|
Download the [latest LLVM build](https://github.com/paritytech/revive/releases?q=LLVM+binaries+release&expanded=true) from our releases.
|
||||||
|
|
||||||
|
> **MacOS** users need to clear the `downloaded` attribute from all binaries after extracting the archive:
|
||||||
|
> ```sh
|
||||||
|
> xattr -rc </path/to/the/extracted/archive>/target-llvm/gnu/target-final/bin/*
|
||||||
|
> ```
|
||||||
|
|
||||||
|
After extracting the archive, point `$LLVM_SYS_181_PREFIX` to it:
|
||||||
|
```sh
|
||||||
|
export LLVM_SYS_181_PREFIX=</path/to/the/extracted/archive>/target-llvm/gnu/target-final
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Building from source</summary>
|
||||||
|
|
||||||
|
Use the provided [revive-llvm](crates/llvm-builder/README.md) utility to compile a compatible LLVM build locally and point `$LLVM_SYS_181_PREFIX` to the installation afterwards.
|
||||||
|
|
||||||
The `Makefile` provides a shortcut target to obtain a compatible LLVM build:
|
The `Makefile` provides a shortcut target to obtain a compatible LLVM build:
|
||||||
|
|
||||||
```bash
|
```sh
|
||||||
make install-llvm
|
make install-llvm
|
||||||
export LLVM_SYS_181_PREFIX=${PWD}/target-llvm/gnu/target-final
|
export LLVM_SYS_181_PREFIX=${PWD}/target-llvm/gnu/target-final
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
### The `resolc` Solidity frontend
|
### The `resolc` Solidity frontend
|
||||||
|
|
||||||
To build the `resolc` Solidity frontend executable, make sure you have obtained a compatible LLVM build using [revive-llvm](crates/llvm-builder/README.md) and did export the `LLVM_SYS_181_PREFIX` environment variable pointing to it (see [above](#LLVM)).
|
To build the `resolc` Solidity frontend executable, make sure you have obtained a compatible LLVM build and did export the `LLVM_SYS_181_PREFIX` environment variable pointing to it (see [above](#LLVM)).
|
||||||
|
|
||||||
To install the `resolc` Solidity frontend executable:
|
To install the `resolc` Solidity frontend executable:
|
||||||
|
|
||||||
```bash
|
```sh
|
||||||
make install-bin
|
make install-bin
|
||||||
resolc --version
|
resolc --version
|
||||||
```
|
```
|
||||||
@@ -49,7 +70,10 @@ resolc --version
|
|||||||
|
|
||||||
Cross-compile the `resolc.js` frontend executable to Wasm for running it in a Node.js or browser environment. The `REVIVE_LLVM_TARGET_PREFIX` environment variable is used to control the target environment LLVM dependency.
|
Cross-compile the `resolc.js` frontend executable to Wasm for running it in a Node.js or browser environment. The `REVIVE_LLVM_TARGET_PREFIX` environment variable is used to control the target environment LLVM dependency.
|
||||||
|
|
||||||
```bash
|
<details>
|
||||||
|
<summary>Instructions for cross-compilation to wasm32-unknown-emscripten</summary>
|
||||||
|
|
||||||
|
```sh
|
||||||
# Build the host LLVM dependency with PolkaVM target support
|
# Build the host LLVM dependency with PolkaVM target support
|
||||||
make install-llvm
|
make install-llvm
|
||||||
export LLVM_SYS_181_PREFIX=${PWD}/target-llvm/gnu/target-final
|
export LLVM_SYS_181_PREFIX=${PWD}/target-llvm/gnu/target-final
|
||||||
@@ -65,21 +89,23 @@ make install-wasm
|
|||||||
make test-wasm
|
make test-wasm
|
||||||
```
|
```
|
||||||
|
|
||||||
### Development
|
</details>
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
Please consult the [Makefile](Makefile) targets to learn how to run tests and benchmarks.
|
Please consult the [Makefile](Makefile) targets to learn how to run tests and benchmarks.
|
||||||
Ensure that your branch passes `make test` locally when submitting a pull request.
|
Ensure that your branch passes `make test` locally when submitting a pull request.
|
||||||
|
|
||||||
## Design overview
|
### Design overview
|
||||||
|
See the [relevant section in our documentation](https://contracts.polkadot.io/revive_compiler/architecture) to learn more about how the compiler works.
|
||||||
|
|
||||||
`revive` uses [solc](https://github.com/ethereum/solidity/), the Ethereum Solidity compiler, as the [Solidity frontend](crates/solidity/src/lib.rs) to process smart contracts written in Solidity. The YUL IR code (or legacy EVM assembly as a fallback for older `solc` versions) emitted by `solc` is then translated to LLVM IR, targetting [Polkadots `revive` pallet](https://docs.rs/pallet-revive/latest/pallet_revive/trait.SyscallDoc.html).
|
[Frontend](https://github.com/matter-labs/era-compiler-solidity) and [code generator](https://github.com/matter-labs/era-compiler-llvm-context) are based of ZKSync `zksolc` (the project started as a fork of the era compiler).
|
||||||
[Frontend](https://github.com/matter-labs/era-compiler-solidity) and [code generator](https://github.com/matter-labs/era-compiler-llvm-context) are based of ZKSync `zksolc`.
|
|
||||||
|
|
||||||
## Tests
|
### Tests
|
||||||
|
|
||||||
Before running the tests, ensure that Geth (Go Ethereum) is installed on your system. Follow the installation guide here: [Installing Geth](https://geth.ethereum.org/docs/getting-started/installing-geth).
|
Before running the tests, ensure that Geth (Go Ethereum) is installed on your system. Follow the installation guide here: [Installing Geth](https://geth.ethereum.org/docs/getting-started/installing-geth).
|
||||||
Once Geth is installed, you can run the tests using the following command:
|
Once Geth is installed, you can run the tests using the following command:
|
||||||
|
|
||||||
```bash
|
```sh
|
||||||
make test
|
make test
|
||||||
```
|
```
|
||||||
|
|||||||
+11
-4
@@ -4,8 +4,15 @@ Prior to the first stable release we neither have formal release processes nor d
|
|||||||
|
|
||||||
To create a new pre-release:
|
To create a new pre-release:
|
||||||
|
|
||||||
1. Merge a release PR which updates the `-dev.X` versions in the workspace `Cargo.toml` and updates the `CHANGELOG.md` accordingly
|
1. Create a release PR which updates the `-dev.X` versions in the workspace `Cargo.toml` and updates the `CHANGELOG.md` accordingly. Add the `release-test` label to trigger the release workflows.
|
||||||
2. Push a release tag to `main`
|
2. If the CI passes, merge the release PR. The release workflow will attempt to build and publish a new release whenever the latest git tag does not match the cargo package version.
|
||||||
3. Create a __pre-release__ from the tag and manually upload the `resolc` binary from docker image
|
3. Wait for the `Release` workflow to finish. If the workflow fails after the `build-linux-all` step, check if a tag has been created and delete it before restarting or pushing updates. Note: It's more convenient to debug the release workflow in a fork (the fork has to be under the `paritytech` org to access `parity-large` runners).
|
||||||
4. Manually upload `resolc.js` and `resolc.wasm` from the `build-revive-wasm` action artifacts.
|
4. Check draft release on [Releases page](https://github.com/paritytech/revive/releases) and publish (should contain `resolc.js`, `resolc.wasm`, `resolc-web.js`, and `resolc-static-linux` release assets)
|
||||||
5. Update the [contract-docs](https://github.com/paritytech/contract-docs/) accordingly
|
5. Update the [contract-docs](https://github.com/paritytech/contract-docs/) accordingly
|
||||||
|
|
||||||
|
# LLVM release
|
||||||
|
|
||||||
|
To create a new LLVM release, run "Release LLVM" workflow. Use current LLVM version as parameter, e.g. `18.1.8`.
|
||||||
|
Version suffix will be resolved automatically.
|
||||||
|
The workflows will create new GitHub release, and upload LLVM binaries.
|
||||||
|
Next release of resolc will use newly created binaries.
|
||||||
|
|||||||
@@ -12,12 +12,6 @@ pub static EXTENSION_ABI: &str = "abi";
|
|||||||
/// The Yul IR file extension.
|
/// The Yul IR file extension.
|
||||||
pub static EXTENSION_YUL: &str = "yul";
|
pub static EXTENSION_YUL: &str = "yul";
|
||||||
|
|
||||||
/// The EVM legacy assembly IR file extension.
|
|
||||||
pub static EXTENSION_EVMLA: &str = "evmla";
|
|
||||||
|
|
||||||
/// The Ethereal IR file extension.
|
|
||||||
pub static EXTENSION_ETHIR: &str = "ethir";
|
|
||||||
|
|
||||||
/// The EVM file extension.
|
/// The EVM file extension.
|
||||||
pub static EXTENSION_EVM: &str = "evm";
|
pub static EXTENSION_EVM: &str = "evm";
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,16 @@
|
|||||||
"shanghaiTime": 0,
|
"shanghaiTime": 0,
|
||||||
"cancunTime": 0,
|
"cancunTime": 0,
|
||||||
"terminalTotalDifficulty": 0,
|
"terminalTotalDifficulty": 0,
|
||||||
"terminalTotalDifficultyPassed": true
|
"terminalTotalDifficultyPassed": true,
|
||||||
|
"blobSchedule": {
|
||||||
|
"cancun": {
|
||||||
|
"target": 3,
|
||||||
|
"max": 6,
|
||||||
|
"baseFeeUpdateFraction": 3338477
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"coinbase": "0x0000000000000000000000000000000000000000",
|
"coinbase": "0xffffffffffffffffffffffffffffffffffffffff",
|
||||||
"difficulty": "0x20000",
|
"difficulty": "0x20000",
|
||||||
"extraData": "",
|
"extraData": "",
|
||||||
"gasLimit": "0xffffffff",
|
"gasLimit": "0xffffffff",
|
||||||
|
|||||||
@@ -413,7 +413,7 @@ impl Evm {
|
|||||||
let stderr = str::from_utf8(output.stderr.as_slice())
|
let stderr = str::from_utf8(output.stderr.as_slice())
|
||||||
.unwrap_or_else(|err| panic!("{EXECUTABLE_NAME} stderr failed to parse: {err}"));
|
.unwrap_or_else(|err| panic!("{EXECUTABLE_NAME} stderr failed to parse: {err}"));
|
||||||
|
|
||||||
let mut log: EvmLog = stdout.into();
|
let mut log: EvmLog = format!("{stdout}{stderr}").as_str().into();
|
||||||
log.stderr = stderr.into();
|
log.stderr = stderr.into();
|
||||||
if self.bench {
|
if self.bench {
|
||||||
log.parse_gas_used_from_bench();
|
log.parse_gas_used_from_bench();
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ serde_json = { workspace = true }
|
|||||||
|
|
||||||
revive-solidity = { workspace = true }
|
revive-solidity = { workspace = true }
|
||||||
revive-runner = { workspace = true }
|
revive-runner = { workspace = true }
|
||||||
|
revive-llvm-context = { workspace = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
sha1 = { workspace = true }
|
sha1 = { workspace = true }
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"Baseline": 1110,
|
"Baseline": 1443,
|
||||||
"Computation": 2389,
|
"Computation": 2788,
|
||||||
"DivisionArithmetics": 14822,
|
"DivisionArithmetics": 9748,
|
||||||
"ERC20": 23973,
|
"ERC20": 19203,
|
||||||
"Events": 1605,
|
"Events": 2201,
|
||||||
"FibonacciIterative": 2023,
|
"FibonacciIterative": 2041,
|
||||||
"Flipper": 1989,
|
"Flipper": 2632,
|
||||||
"SHA1": 17026
|
"SHA1": 8958
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.8;
|
||||||
|
|
||||||
|
/* runner.json
|
||||||
|
{
|
||||||
|
"differential": true,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"Upload": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "BalanceReceiver"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Instantiate": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "Balance"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"value": 24
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "1c8d16b30000000000000000000000000303030303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000a"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "6ada15d90000000000000000000000000303030303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000a"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
contract BalanceReceiver {
|
||||||
|
constructor() payable {}
|
||||||
|
|
||||||
|
fallback() external payable {}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract Balance {
|
||||||
|
constructor() payable {
|
||||||
|
// 0 to EOA
|
||||||
|
transfer_to(payable(address(0xdeadbeef)), 0);
|
||||||
|
send_to(payable(address(0xdeadbeef)), 0);
|
||||||
|
|
||||||
|
// 1 to EOA
|
||||||
|
transfer_to(payable(address(0xcafebabe)), 1);
|
||||||
|
send_to(payable(address(0xcafebabe)), 1);
|
||||||
|
|
||||||
|
BalanceReceiver balanceReceiver = new BalanceReceiver();
|
||||||
|
|
||||||
|
// 0 to contract
|
||||||
|
transfer_to(payable(address(balanceReceiver)), 0);
|
||||||
|
send_to(payable(address(balanceReceiver)), 0);
|
||||||
|
|
||||||
|
// 1 to contract
|
||||||
|
transfer_to(payable(address(balanceReceiver)), 1);
|
||||||
|
send_to(payable(address(balanceReceiver)), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function transfer_to(address payable _dest, uint _amount) public payable {
|
||||||
|
_dest.transfer(_amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
function send_to(address payable _dest, uint _amount) public payable {
|
||||||
|
require(_dest.send(_amount));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
pragma solidity ^0.8;
|
||||||
|
|
||||||
|
/* runner.json
|
||||||
|
{
|
||||||
|
"differential": true,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"Instantiate": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "Coinbase"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
contract Coinbase {
|
||||||
|
constructor() payable {
|
||||||
|
address coinbase = address(0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF);
|
||||||
|
assert(block.coinbase == coinbase);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
pragma solidity ^0.8;
|
||||||
|
|
||||||
|
/* runner.json
|
||||||
|
{
|
||||||
|
"differential": true,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"Upload": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "CreateA"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Instantiate": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "CreateB"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"value": 100000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
contract CreateA {
|
||||||
|
constructor() payable {}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract CreateB {
|
||||||
|
constructor() payable {
|
||||||
|
bytes32 salt = hex"ff";
|
||||||
|
|
||||||
|
try new CreateA{salt: salt}() returns (CreateA) {} catch {
|
||||||
|
revert("the first instantiation should succeed");
|
||||||
|
}
|
||||||
|
|
||||||
|
try new CreateA{salt: salt}() returns (CreateA) {} catch {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
revert("the second instantiation should have failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
pragma solidity ^0.8.28;
|
||||||
|
|
||||||
|
/* runner.json
|
||||||
|
{
|
||||||
|
"differential": true,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"Instantiate": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "DelegateCaller"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "e466c6c9"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
contract DelegateCaller {
|
||||||
|
function delegateNoContract() external returns (bool) {
|
||||||
|
address testAddress = 0x0000000000000000000000000000000000000000;
|
||||||
|
(bool success, ) = testAddress.delegatecall(
|
||||||
|
abi.encodeWithSignature("test()")
|
||||||
|
);
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
pragma solidity ^0.8;
|
||||||
|
|
||||||
|
/* runner.json
|
||||||
|
{
|
||||||
|
"differential": true,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"Instantiate": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "FunctionPointer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "26121ff0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
contract FunctionPointer {
|
||||||
|
bool public flag = false;
|
||||||
|
|
||||||
|
function f0() public {
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function f() public returns (bool) {
|
||||||
|
function() internal x = f0;
|
||||||
|
x();
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
pragma solidity ^0.8.28;
|
||||||
|
|
||||||
|
/* runner.json
|
||||||
|
{
|
||||||
|
"differential": true,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"Instantiate": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "FunctionType"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "b8c9d365"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
contract FunctionType {
|
||||||
|
uint public immutable x = 42;
|
||||||
|
|
||||||
|
function h() public view returns (function() external view returns (uint)) {
|
||||||
|
return this.x;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
pragma solidity ^0.8;
|
||||||
|
|
||||||
|
/* runner.json
|
||||||
|
{
|
||||||
|
"differential": true,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"Instantiate": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "MCopyOverlap"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "afdce848"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
function copy(
|
||||||
|
uint dstOffset,
|
||||||
|
uint srcOffset,
|
||||||
|
uint length
|
||||||
|
) pure returns (bytes memory out) {
|
||||||
|
out = hex"2222222222222222333333333333333344444444444444445555555555555555"
|
||||||
|
hex"6666666666666666777777777777777788888888888888889999999999999999"
|
||||||
|
hex"aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccdddddddddddddddd";
|
||||||
|
assembly {
|
||||||
|
mcopy(
|
||||||
|
add(add(out, 0x20), dstOffset),
|
||||||
|
add(add(out, 0x20), srcOffset),
|
||||||
|
length
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract MCopyOverlap {
|
||||||
|
function mcopy_to_right_overlap() public pure returns (bytes memory) {
|
||||||
|
return copy(0x20, 0x10, 0x30);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
pragma solidity ^0.8.28;
|
||||||
|
|
||||||
|
/* runner.json
|
||||||
|
{
|
||||||
|
"differential": true,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"Instantiate": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "MLoad"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "e2179b8e"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
contract MLoad {
|
||||||
|
constructor() payable {
|
||||||
|
assert(g() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function g() public payable returns (uint m) {
|
||||||
|
assembly {
|
||||||
|
m := mload(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.8;
|
||||||
|
|
||||||
|
/* runner.json
|
||||||
|
{
|
||||||
|
"differential": false,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"Instantiate": {
|
||||||
|
"code": {
|
||||||
|
"Solidity": {
|
||||||
|
"contract": "Send"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"value": 211
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "1c8d16b30000000000000000000000000303030303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000a"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"VerifyCall": {
|
||||||
|
"success": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "fb9e8d050000000000000000000000000000000000000000000000000000000000000001"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"VerifyCall": {
|
||||||
|
"success": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "fb9e8d050000000000000000000000000000000000000000000000000000000000000000"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"VerifyCall": {
|
||||||
|
"success": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
contract Send {
|
||||||
|
constructor() payable {}
|
||||||
|
|
||||||
|
function transfer_self(uint _amount) public payable {
|
||||||
|
transfer_to(payable(address(this)), _amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
function transfer_to(address payable _dest, uint _amount) public payable {
|
||||||
|
if (_dest.send(_amount)) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fallback() external {}
|
||||||
|
|
||||||
|
receive() external payable {}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ pragma solidity ^0.8;
|
|||||||
|
|
||||||
/* runner.json
|
/* runner.json
|
||||||
{
|
{
|
||||||
"differential": true,
|
"differential": false,
|
||||||
"actions": [
|
"actions": [
|
||||||
{
|
{
|
||||||
"Instantiate": {
|
"Instantiate": {
|
||||||
@@ -12,7 +12,7 @@ pragma solidity ^0.8;
|
|||||||
"contract": "Transfer"
|
"contract": "Transfer"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"value": 11
|
"value": 211
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -23,12 +23,35 @@ pragma solidity ^0.8;
|
|||||||
"data": "1c8d16b30000000000000000000000000303030303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000a"
|
"data": "1c8d16b30000000000000000000000000303030303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000a"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"VerifyCall": {
|
||||||
|
"success": true
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"Call": {
|
"Call": {
|
||||||
"dest": {
|
"dest": {
|
||||||
"Instantiated": 0
|
"Instantiated": 0
|
||||||
},
|
},
|
||||||
"data": "fb9e8d0500000000000000000000000003030303030303030303030303030303030303030000000000000000000000000000000000000000000000000000000000000001"
|
"data": "fb9e8d050000000000000000000000000000000000000000000000000000000000000001"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"VerifyCall": {
|
||||||
|
"success": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Call": {
|
||||||
|
"dest": {
|
||||||
|
"Instantiated": 0
|
||||||
|
},
|
||||||
|
"data": "fb9e8d050000000000000000000000000000000000000000000000000000000000000000"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"VerifyCall": {
|
||||||
|
"success": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -36,19 +59,17 @@ pragma solidity ^0.8;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
contract Transfer {
|
contract Transfer {
|
||||||
constructor() payable {
|
constructor() payable {}
|
||||||
transfer_self(msg.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
function address_self() internal view returns (address payable) {
|
|
||||||
return payable(address(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
function transfer_self(uint _amount) public payable {
|
function transfer_self(uint _amount) public payable {
|
||||||
transfer_to(address_self(), _amount);
|
transfer_to(payable(address(this)), _amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
function transfer_to(address payable _dest, uint _amount) public payable {
|
function transfer_to(address payable _dest, uint _amount) public payable {
|
||||||
_dest.transfer(_amount);
|
_dest.transfer(_amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fallback() external {}
|
||||||
|
|
||||||
|
receive() external payable {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use alloy_primitives::{Address, Bytes, I256, U256};
|
use alloy_primitives::{Address, Bytes, I256, U256};
|
||||||
use alloy_sol_types::{sol, SolCall, SolConstructor};
|
use alloy_sol_types::{sol, SolCall, SolConstructor};
|
||||||
|
|
||||||
|
use revive_llvm_context::OptimizerSettings;
|
||||||
use revive_solidity::test_utils::*;
|
use revive_solidity::test_utils::*;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -156,6 +157,20 @@ case!("DivisionArithmetics.sol", DivisionArithmetics, sdivCall, division_arithme
|
|||||||
case!("DivisionArithmetics.sol", DivisionArithmetics, modCall, division_arithmetics_mod, n: U256, d: U256);
|
case!("DivisionArithmetics.sol", DivisionArithmetics, modCall, division_arithmetics_mod, n: U256, d: U256);
|
||||||
case!("DivisionArithmetics.sol", DivisionArithmetics, smodCall, division_arithmetics_smod, n: I256, d: I256);
|
case!("DivisionArithmetics.sol", DivisionArithmetics, smodCall, division_arithmetics_smod, n: I256, d: I256);
|
||||||
|
|
||||||
|
sol!(
|
||||||
|
contract Send {
|
||||||
|
function transfer_self(uint _amount) public payable;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
case!("Send.sol", Send, transfer_selfCall, send_self, amount: U256);
|
||||||
|
|
||||||
|
sol!(
|
||||||
|
contract Transfer {
|
||||||
|
function transfer_self(uint _amount) public payable;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
case!("Transfer.sol", Transfer, transfer_selfCall, transfer_self, amount: U256);
|
||||||
|
|
||||||
sol!(
|
sol!(
|
||||||
contract MStore8 {
|
contract MStore8 {
|
||||||
function mStore8(uint value) public pure returns (uint256 word);
|
function mStore8(uint value) public pure returns (uint256 word);
|
||||||
@@ -236,7 +251,7 @@ sol!(
|
|||||||
case!("Storage.sol", Storage, transientCall, storage_transient, value: U256);
|
case!("Storage.sol", Storage, transientCall, storage_transient, value: U256);
|
||||||
|
|
||||||
impl Contract {
|
impl Contract {
|
||||||
fn build(calldata: Vec<u8>, name: &'static str, code: &str) -> Self {
|
pub fn build(calldata: Vec<u8>, name: &'static str, code: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name,
|
name,
|
||||||
evm_runtime: compile_evm_bin_runtime(name, code),
|
evm_runtime: compile_evm_bin_runtime(name, code),
|
||||||
@@ -244,11 +259,19 @@ impl Contract {
|
|||||||
calldata,
|
calldata,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn build_size_opt(calldata: Vec<u8>, name: &'static str, code: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
name,
|
||||||
|
evm_runtime: compile_evm_bin_runtime(name, code),
|
||||||
|
pvm_runtime: compile_blob_with_options(name, code, true, OptimizerSettings::size()),
|
||||||
|
calldata,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use alloy_primitives::{Bytes, U256};
|
|
||||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||||
use serde::{de::Deserialize, Serialize};
|
use serde::{de::Deserialize, Serialize};
|
||||||
use std::{collections::BTreeMap, fs::File};
|
use std::{collections::BTreeMap, fs::File};
|
||||||
@@ -288,14 +311,47 @@ mod tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
[
|
[
|
||||||
Contract::baseline as fn() -> Contract,
|
(|| {
|
||||||
Contract::flipper as fn() -> Contract,
|
Contract::build_size_opt(
|
||||||
(|| Contract::odd_product(0)) as fn() -> Contract,
|
vec![],
|
||||||
(|| Contract::fib_iterative(U256::ZERO)) as fn() -> Contract,
|
"Baseline",
|
||||||
Contract::erc20 as fn() -> Contract,
|
include_str!("../contracts/Baseline.sol"),
|
||||||
(|| Contract::sha1(Bytes::new())) as fn() -> Contract,
|
)
|
||||||
(|| Contract::division_arithmetics_div(U256::ZERO, U256::ZERO)) as fn() -> Contract,
|
}) as _,
|
||||||
(|| Contract::event(U256::ZERO)) as fn() -> Contract,
|
(|| {
|
||||||
|
Contract::build_size_opt(
|
||||||
|
vec![],
|
||||||
|
"Flipper",
|
||||||
|
include_str!("../contracts/flipper.sol"),
|
||||||
|
)
|
||||||
|
}) as _,
|
||||||
|
(|| {
|
||||||
|
Contract::build_size_opt(
|
||||||
|
vec![],
|
||||||
|
"Computation",
|
||||||
|
include_str!("../contracts/Computation.sol"),
|
||||||
|
)
|
||||||
|
}) as _,
|
||||||
|
(|| {
|
||||||
|
Contract::build_size_opt(
|
||||||
|
vec![],
|
||||||
|
"FibonacciIterative",
|
||||||
|
include_str!("../contracts/Fibonacci.sol"),
|
||||||
|
)
|
||||||
|
}) as _,
|
||||||
|
(|| Contract::build_size_opt(vec![], "ERC20", include_str!("../contracts/ERC20.sol")))
|
||||||
|
as _,
|
||||||
|
(|| Contract::build_size_opt(vec![], "SHA1", include_str!("../contracts/SHA1.sol")))
|
||||||
|
as _,
|
||||||
|
(|| {
|
||||||
|
Contract::build_size_opt(
|
||||||
|
vec![],
|
||||||
|
"DivisionArithmetics",
|
||||||
|
include_str!("../contracts/DivisionArithmetics.sol"),
|
||||||
|
)
|
||||||
|
}) as _,
|
||||||
|
(|| Contract::build_size_opt(vec![], "Events", include_str!("../contracts/Events.sol")))
|
||||||
|
as _,
|
||||||
]
|
]
|
||||||
.into_par_iter()
|
.into_par_iter()
|
||||||
.map(extract_code_size)
|
.map(extract_code_size)
|
||||||
|
|||||||
@@ -33,14 +33,15 @@ test_spec!(msize, "MSize", "MSize.sol");
|
|||||||
test_spec!(sha1, "SHA1", "SHA1.sol");
|
test_spec!(sha1, "SHA1", "SHA1.sol");
|
||||||
test_spec!(block, "Block", "Block.sol");
|
test_spec!(block, "Block", "Block.sol");
|
||||||
test_spec!(mcopy, "MCopy", "MCopy.sol");
|
test_spec!(mcopy, "MCopy", "MCopy.sol");
|
||||||
|
test_spec!(mcopy_overlap, "MCopyOverlap", "MCopyOverlap.sol");
|
||||||
test_spec!(events, "Events", "Events.sol");
|
test_spec!(events, "Events", "Events.sol");
|
||||||
test_spec!(storage, "Storage", "Storage.sol");
|
test_spec!(storage, "Storage", "Storage.sol");
|
||||||
test_spec!(mstore8, "MStore8", "MStore8.sol");
|
test_spec!(mstore8, "MStore8", "MStore8.sol");
|
||||||
test_spec!(address, "Context", "Context.sol");
|
test_spec!(address, "Context", "Context.sol");
|
||||||
test_spec!(balance, "Value", "Value.sol");
|
test_spec!(value, "Value", "Value.sol");
|
||||||
test_spec!(create, "CreateB", "Create.sol");
|
test_spec!(create, "CreateB", "Create.sol");
|
||||||
test_spec!(call, "Caller", "Call.sol");
|
test_spec!(call, "Caller", "Call.sol");
|
||||||
test_spec!(transfer, "Transfer", "Transfer.sol");
|
test_spec!(balance, "Balance", "Balance.sol");
|
||||||
test_spec!(return_data_oob, "ReturnDataOob", "ReturnDataOob.sol");
|
test_spec!(return_data_oob, "ReturnDataOob", "ReturnDataOob.sol");
|
||||||
test_spec!(immutables, "Immutables", "Immutables.sol");
|
test_spec!(immutables, "Immutables", "Immutables.sol");
|
||||||
test_spec!(transaction, "Transaction", "Transaction.sol");
|
test_spec!(transaction, "Transaction", "Transaction.sol");
|
||||||
@@ -50,6 +51,14 @@ test_spec!(gas_price, "GasPrice", "GasPrice.sol");
|
|||||||
test_spec!(gas_left, "GasLeft", "GasLeft.sol");
|
test_spec!(gas_left, "GasLeft", "GasLeft.sol");
|
||||||
test_spec!(gas_limit, "GasLimit", "GasLimit.sol");
|
test_spec!(gas_limit, "GasLimit", "GasLimit.sol");
|
||||||
test_spec!(base_fee, "BaseFee", "BaseFee.sol");
|
test_spec!(base_fee, "BaseFee", "BaseFee.sol");
|
||||||
|
test_spec!(coinbase, "Coinbase", "Coinbase.sol");
|
||||||
|
test_spec!(create2, "CreateB", "Create2.sol");
|
||||||
|
test_spec!(transfer, "Transfer", "Transfer.sol");
|
||||||
|
test_spec!(send, "Send", "Send.sol");
|
||||||
|
test_spec!(function_pointer, "FunctionPointer", "FunctionPointer.sol");
|
||||||
|
test_spec!(mload, "MLoad", "MLoad.sol");
|
||||||
|
test_spec!(delegate_no_contract, "DelegateCaller", "DelegateCaller.sol");
|
||||||
|
test_spec!(function_type, "FunctionType", "FunctionType.sol");
|
||||||
|
|
||||||
fn instantiate(path: &str, contract: &str) -> Vec<SpecsAction> {
|
fn instantiate(path: &str, contract: &str) -> Vec<SpecsAction> {
|
||||||
vec![Instantiate {
|
vec![Instantiate {
|
||||||
@@ -61,7 +70,6 @@ fn instantiate(path: &str, contract: &str) -> Vec<SpecsAction> {
|
|||||||
path: Some(path.into()),
|
path: Some(path.into()),
|
||||||
contract: contract.to_string(),
|
contract: contract.to_string(),
|
||||||
solc_optimizer: None,
|
solc_optimizer: None,
|
||||||
pipeline: None,
|
|
||||||
},
|
},
|
||||||
data: vec![],
|
data: vec![],
|
||||||
salt: OptionalHex::default(),
|
salt: OptionalHex::default(),
|
||||||
@@ -354,7 +362,6 @@ fn ext_code_size() {
|
|||||||
path: Some("contracts/Baseline.sol".into()),
|
path: Some("contracts/Baseline.sol".into()),
|
||||||
contract: "Baseline".to_string(),
|
contract: "Baseline".to_string(),
|
||||||
solc_optimizer: None,
|
solc_optimizer: None,
|
||||||
pipeline: None,
|
|
||||||
},
|
},
|
||||||
data: vec![],
|
data: vec![],
|
||||||
salt: OptionalHex::from([0; 32]),
|
salt: OptionalHex::from([0; 32]),
|
||||||
@@ -435,32 +442,46 @@ fn ext_code_size() {
|
|||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[test]
|
||||||
// These test were implement for the mock-runtime and need to be ported yet.
|
#[should_panic(expected = "ReentranceDenied")]
|
||||||
|
fn send_denies_reentrancy() {
|
||||||
|
let value = 1000;
|
||||||
|
Specs {
|
||||||
|
actions: vec![
|
||||||
|
instantiate("contracts/Send.sol", "Send").remove(0),
|
||||||
|
Call {
|
||||||
|
origin: TestAddress::Alice,
|
||||||
|
dest: TestAddress::Instantiated(0),
|
||||||
|
value,
|
||||||
|
gas_limit: None,
|
||||||
|
storage_deposit_limit: None,
|
||||||
|
data: Contract::send_self(U256::from(value)).calldata,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
differential: false,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create2_failure() {
|
#[should_panic(expected = "ReentranceDenied")]
|
||||||
let mut state = State::default();
|
fn transfer_denies_reentrancy() {
|
||||||
let contract_a = Contract::create_a();
|
let value = 1000;
|
||||||
state.upload_code(&contract_a.pvm_runtime);
|
Specs {
|
||||||
|
actions: vec![
|
||||||
let contract = Contract::create_b();
|
instantiate("contracts/Transfer.sol", "Transfer").remove(0),
|
||||||
let (state, output) = state
|
Call {
|
||||||
.transaction()
|
origin: TestAddress::Alice,
|
||||||
.with_default_account(&contract.pvm_runtime)
|
dest: TestAddress::Instantiated(0),
|
||||||
.calldata(contract.calldata.clone())
|
value,
|
||||||
.call();
|
gas_limit: None,
|
||||||
|
storage_deposit_limit: None,
|
||||||
assert_eq!(output.flags, ReturnFlags::Success);
|
data: Contract::transfer_self(U256::from(value)).calldata,
|
||||||
|
},
|
||||||
// The address already exists, which should cause the contract to revert
|
],
|
||||||
|
differential: false,
|
||||||
let (_, output) = state
|
..Default::default()
|
||||||
.transaction()
|
}
|
||||||
.with_default_account(&contract.pvm_runtime)
|
.run();
|
||||||
.calldata(contract.calldata)
|
|
||||||
.call();
|
|
||||||
|
|
||||||
assert_eq!(output.flags, ReturnFlags::Revert);
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|||||||
@@ -52,7 +52,6 @@ pub fn link<T: AsRef<[u8]>>(input: T) -> anyhow::Result<Vec<u8>> {
|
|||||||
|
|
||||||
let ld_args = [
|
let ld_args = [
|
||||||
"ld.lld",
|
"ld.lld",
|
||||||
"--lto=full",
|
|
||||||
"--error-limit=0",
|
"--error-limit=0",
|
||||||
"--relocatable",
|
"--relocatable",
|
||||||
"--emit-relocs",
|
"--emit-relocs",
|
||||||
|
|||||||
@@ -40,17 +40,9 @@ pub const SHARED_BUILD_OPTS_NOT_MUSL: [&str; 4] = [
|
|||||||
|
|
||||||
/// The shared build options to treat warnings as errors.
|
/// The shared build options to treat warnings as errors.
|
||||||
///
|
///
|
||||||
/// Disabled on Windows due to the following upstream issue with MSYS2 with mingw-w64:
|
/// Disabled because it makes the build very brittle.
|
||||||
/// ProgramTest.cpp:23:15: error: '__p__environ' redeclared without 'dllimport' attribute
|
pub fn shared_build_opts_werror(_target_env: TargetEnv) -> Vec<String> {
|
||||||
pub fn shared_build_opts_werror(target_env: TargetEnv) -> Vec<String> {
|
vec!["-DLLVM_ENABLE_WERROR='Off'".to_string()]
|
||||||
vec![format!(
|
|
||||||
"-DLLVM_ENABLE_WERROR='{}'",
|
|
||||||
if cfg!(target_os = "windows") || target_env == TargetEnv::Emscripten {
|
|
||||||
"Off"
|
|
||||||
} else {
|
|
||||||
"On"
|
|
||||||
},
|
|
||||||
)]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The build options to set the default target.
|
/// The build options to set the default target.
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ fn build_target(
|
|||||||
Command::new("emcmake")
|
Command::new("emcmake")
|
||||||
.env("EMCC_DEBUG", "2")
|
.env("EMCC_DEBUG", "2")
|
||||||
.env("CXXFLAGS", "-Dwait4=__syscall_wait4")
|
.env("CXXFLAGS", "-Dwait4=__syscall_wait4")
|
||||||
.env("LDFLAGS", "-lnodefs.js -s NO_INVOKE_RUN -s EXIT_RUNTIME -s INITIAL_MEMORY=64MB -s ALLOW_MEMORY_GROWTH -s EXPORTED_RUNTIME_METHODS=FS,callMain,NODEFS -s MODULARIZE -s EXPORT_ES6 -s WASM_BIGINT")
|
.env("LDFLAGS", "-lnodefs.js -s NO_INVOKE_RUN=1 -s EXIT_RUNTIME=1 -s ALLOW_MEMORY_GROWTH=1 -s EXPORTED_RUNTIME_METHODS=FS,callMain,NODEFS -s MODULARIZE=1 -s WASM_BIGINT=1 -s ALLOW_TABLE_GROWTH=1 -s NODEJS_CATCH_EXIT=0 -sDYNAMIC_EXECUTION=0")
|
||||||
.arg("cmake")
|
.arg("cmake")
|
||||||
.args([
|
.args([
|
||||||
"-S",
|
"-S",
|
||||||
|
|||||||
@@ -53,6 +53,16 @@ fn clone_build_and_clean_musl() -> anyhow::Result<()> {
|
|||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
|
|
||||||
|
Command::cargo_bin(common::REVIVE_LLVM)?
|
||||||
|
.current_dir(test_dir.path())
|
||||||
|
.arg("build")
|
||||||
|
.arg("--llvm-projects")
|
||||||
|
.arg("clang")
|
||||||
|
.arg("--llvm-projects")
|
||||||
|
.arg("lld")
|
||||||
|
.assert()
|
||||||
|
.success();
|
||||||
|
|
||||||
Command::cargo_bin(common::REVIVE_LLVM)?
|
Command::cargo_bin(common::REVIVE_LLVM)?
|
||||||
.arg("--target-env")
|
.arg("--target-env")
|
||||||
.arg("musl")
|
.arg("musl")
|
||||||
@@ -65,12 +75,6 @@ fn clone_build_and_clean_musl() -> anyhow::Result<()> {
|
|||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
|
|
||||||
Command::cargo_bin(common::REVIVE_LLVM)?
|
|
||||||
.current_dir(test_dir.path())
|
|
||||||
.arg("builtins")
|
|
||||||
.assert()
|
|
||||||
.success();
|
|
||||||
|
|
||||||
Command::cargo_bin(common::REVIVE_LLVM)?
|
Command::cargo_bin(common::REVIVE_LLVM)?
|
||||||
.current_dir(test_dir.path())
|
.current_dir(test_dir.path())
|
||||||
.arg("clean")
|
.arg("clean")
|
||||||
@@ -83,6 +87,7 @@ fn clone_build_and_clean_musl() -> anyhow::Result<()> {
|
|||||||
/// This test verifies that the LLVM repository can be successfully cloned and built in debug mode
|
/// This test verifies that the LLVM repository can be successfully cloned and built in debug mode
|
||||||
/// with tests and coverage enabled.
|
/// with tests and coverage enabled.
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
fn debug_build_with_tests_coverage() -> anyhow::Result<()> {
|
fn debug_build_with_tests_coverage() -> anyhow::Result<()> {
|
||||||
let test_dir = common::TestDir::with_lockfile(None)?;
|
let test_dir = common::TestDir::with_lockfile(None)?;
|
||||||
|
|
||||||
@@ -99,6 +104,10 @@ fn debug_build_with_tests_coverage() -> anyhow::Result<()> {
|
|||||||
.arg("--enable-tests")
|
.arg("--enable-tests")
|
||||||
.arg("--build-type")
|
.arg("--build-type")
|
||||||
.arg("Debug")
|
.arg("Debug")
|
||||||
|
.arg("--llvm-projects")
|
||||||
|
.arg("clang")
|
||||||
|
.arg("--llvm-projects")
|
||||||
|
.arg("lld")
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
|
|
||||||
@@ -107,6 +116,7 @@ fn debug_build_with_tests_coverage() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
/// This test verifies that the LLVM repository can be successfully built with address sanitizer.
|
/// This test verifies that the LLVM repository can be successfully built with address sanitizer.
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
fn build_with_sanitizers() -> anyhow::Result<()> {
|
fn build_with_sanitizers() -> anyhow::Result<()> {
|
||||||
let test_dir = common::TestDir::with_lockfile(None)?;
|
let test_dir = common::TestDir::with_lockfile(None)?;
|
||||||
|
|
||||||
@@ -121,6 +131,10 @@ fn build_with_sanitizers() -> anyhow::Result<()> {
|
|||||||
.arg("build")
|
.arg("build")
|
||||||
.arg("--sanitizer")
|
.arg("--sanitizer")
|
||||||
.arg("Address")
|
.arg("Address")
|
||||||
|
.arg("--llvm-projects")
|
||||||
|
.arg("lld")
|
||||||
|
.arg("--llvm-projects")
|
||||||
|
.arg("clang")
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
|
|
||||||
@@ -129,15 +143,36 @@ fn build_with_sanitizers() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
/// Tests the clone, build, and clean process of the LLVM repository for the emscripten target.
|
/// Tests the clone, build, and clean process of the LLVM repository for the emscripten target.
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
#[cfg(target_os = "linux")]
|
||||||
fn clone_build_and_clean_emscripten() -> anyhow::Result<()> {
|
fn clone_build_and_clean_emscripten() -> anyhow::Result<()> {
|
||||||
let test_dir = common::TestDir::with_lockfile(None)?;
|
let test_dir = common::TestDir::with_lockfile(None)?;
|
||||||
let command = Command::cargo_bin(common::REVIVE_LLVM)?;
|
let command = Command::cargo_bin(common::REVIVE_LLVM)?;
|
||||||
let program = command.get_program().to_string_lossy();
|
let program = command.get_program().to_string_lossy();
|
||||||
|
|
||||||
|
Command::cargo_bin(common::REVIVE_LLVM)?
|
||||||
|
.current_dir(test_dir.path())
|
||||||
|
.arg("clone")
|
||||||
|
.assert()
|
||||||
|
.success();
|
||||||
|
|
||||||
|
Command::cargo_bin(common::REVIVE_LLVM)?
|
||||||
|
.current_dir(test_dir.path())
|
||||||
|
.arg("build")
|
||||||
|
.arg("--llvm-projects")
|
||||||
|
.arg("lld")
|
||||||
|
.arg("--llvm-projects")
|
||||||
|
.arg("clang")
|
||||||
|
.assert()
|
||||||
|
.success();
|
||||||
|
|
||||||
|
// Two little shell-dependent things here:
|
||||||
|
// Doing `. ./emsdk_env.sh` instead of `source`, as `source` might be missing in some shells
|
||||||
|
// `cd {} && . ./emsdk_env.sh && cd ..` helps the script to locate `emsdk.py`
|
||||||
|
// @see https://github.com/emscripten-core/emsdk/blob/9dbdc4b3437750b85d16931c7c801bb71a782122/emsdk_env.sh#L61-L69
|
||||||
let emsdk_wrapped_build_command = format!(
|
let emsdk_wrapped_build_command = format!(
|
||||||
"{program} --target-env emscripten clone && \
|
"{program} --target-env emscripten clone && \
|
||||||
source {}emsdk_env.sh && \
|
cd {} && . ./emsdk_env.sh && cd .. && \
|
||||||
{program} --target-env emscripten build --llvm-projects clang --llvm-projects lld",
|
{program} --target-env emscripten build --llvm-projects lld",
|
||||||
revive_llvm_builder::LLVMPath::DIRECTORY_EMSDK_SOURCE,
|
revive_llvm_builder::LLVMPath::DIRECTORY_EMSDK_SOURCE,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ serde = { workspace = true, features = ["derive"] }
|
|||||||
num = { workspace = true }
|
num = { workspace = true }
|
||||||
hex = { workspace = true }
|
hex = { workspace = true }
|
||||||
sha3 = { workspace = true }
|
sha3 = { workspace = true }
|
||||||
md5 = { workspace = true }
|
|
||||||
inkwell = { workspace = true }
|
inkwell = { workspace = true }
|
||||||
polkavm-disassembler = { workspace = true }
|
polkavm-disassembler = { workspace = true }
|
||||||
polkavm-common = { workspace = true }
|
polkavm-common = { workspace = true }
|
||||||
|
|||||||
@@ -1,16 +1,11 @@
|
|||||||
//! The debug IR type.
|
//! The debug IR type.
|
||||||
|
|
||||||
/// The debug IR type.
|
/// The debug IR type.
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
#[allow(clippy::upper_case_acronyms)]
|
#[allow(clippy::upper_case_acronyms)]
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub enum IRType {
|
pub enum IRType {
|
||||||
/// Whether to dump the Yul code.
|
/// Whether to dump the Yul code.
|
||||||
Yul,
|
Yul,
|
||||||
/// Whether to dump the EVM legacy assembly code.
|
|
||||||
EVMLA,
|
|
||||||
/// Whether to dump the Ethereal IR code.
|
|
||||||
EthIR,
|
|
||||||
/// Whether to dump the LLVM IR code.
|
/// Whether to dump the LLVM IR code.
|
||||||
LLVM,
|
LLVM,
|
||||||
/// Whether to dump the assembly code.
|
/// Whether to dump the assembly code.
|
||||||
@@ -27,8 +22,6 @@ impl IRType {
|
|||||||
pub fn file_extension(&self) -> &'static str {
|
pub fn file_extension(&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
Self::Yul => revive_common::EXTENSION_YUL,
|
Self::Yul => revive_common::EXTENSION_YUL,
|
||||||
Self::EthIR => revive_common::EXTENSION_ETHIR,
|
|
||||||
Self::EVMLA => revive_common::EXTENSION_EVMLA,
|
|
||||||
Self::LLVM => revive_common::EXTENSION_LLVM_SOURCE,
|
Self::LLVM => revive_common::EXTENSION_LLVM_SOURCE,
|
||||||
Self::Assembly => revive_common::EXTENSION_POLKAVM_ASSEMBLY,
|
Self::Assembly => revive_common::EXTENSION_POLKAVM_ASSEMBLY,
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
|||||||
@@ -39,30 +39,6 @@ impl DebugConfig {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Dumps the EVM legacy assembly IR.
|
|
||||||
pub fn dump_evmla(&self, contract_path: &str, code: &str) -> anyhow::Result<()> {
|
|
||||||
if let Some(output_directory) = self.output_directory.as_ref() {
|
|
||||||
let mut file_path = output_directory.to_owned();
|
|
||||||
let full_file_name = Self::full_file_name(contract_path, None, IRType::EVMLA);
|
|
||||||
file_path.push(full_file_name);
|
|
||||||
std::fs::write(file_path, code)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Dumps the Ethereal IR.
|
|
||||||
pub fn dump_ethir(&self, contract_path: &str, code: &str) -> anyhow::Result<()> {
|
|
||||||
if let Some(output_directory) = self.output_directory.as_ref() {
|
|
||||||
let mut file_path = output_directory.to_owned();
|
|
||||||
let full_file_name = Self::full_file_name(contract_path, None, IRType::EthIR);
|
|
||||||
file_path.push(full_file_name);
|
|
||||||
std::fs::write(file_path, code)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Dumps the unoptimized LLVM IR.
|
/// Dumps the unoptimized LLVM IR.
|
||||||
pub fn dump_llvm_ir_unoptimized(
|
pub fn dump_llvm_ir_unoptimized(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
@@ -17,26 +17,29 @@ pub use self::polkavm::context::attribute::Attribute as PolkaVMAttribute;
|
|||||||
pub use self::polkavm::context::build::Build as PolkaVMBuild;
|
pub use self::polkavm::context::build::Build as PolkaVMBuild;
|
||||||
pub use self::polkavm::context::code_type::CodeType as PolkaVMCodeType;
|
pub use self::polkavm::context::code_type::CodeType as PolkaVMCodeType;
|
||||||
pub use self::polkavm::context::debug_info::DebugInfo;
|
pub use self::polkavm::context::debug_info::DebugInfo;
|
||||||
pub use self::polkavm::context::evmla_data::EVMLAData as PolkaVMContextEVMLAData;
|
|
||||||
pub use self::polkavm::context::function::block::evmla_data::key::Key as PolkaVMFunctionBlockKey;
|
|
||||||
pub use self::polkavm::context::function::block::evmla_data::EVMLAData as PolkaVMFunctionBlockEVMLAData;
|
|
||||||
pub use self::polkavm::context::function::block::Block as PolkaVMFunctionBlock;
|
|
||||||
pub use self::polkavm::context::function::declaration::Declaration as PolkaVMFunctionDeclaration;
|
pub use self::polkavm::context::function::declaration::Declaration as PolkaVMFunctionDeclaration;
|
||||||
pub use self::polkavm::context::function::evmla_data::EVMLAData as PolkaVMFunctionEVMLAData;
|
|
||||||
pub use self::polkavm::context::function::intrinsics::Intrinsics as PolkaVMIntrinsicFunction;
|
pub use self::polkavm::context::function::intrinsics::Intrinsics as PolkaVMIntrinsicFunction;
|
||||||
pub use self::polkavm::context::function::llvm_runtime::LLVMRuntime as PolkaVMLLVMRuntime;
|
pub use self::polkavm::context::function::llvm_runtime::LLVMRuntime as PolkaVMLLVMRuntime;
|
||||||
pub use self::polkavm::context::function::r#return::Return as PolkaVMFunctionReturn;
|
pub use self::polkavm::context::function::r#return::Return as PolkaVMFunctionReturn;
|
||||||
|
pub use self::polkavm::context::function::runtime::arithmetics::Division as PolkaVMDivisionFunction;
|
||||||
|
pub use self::polkavm::context::function::runtime::arithmetics::Remainder as PolkaVMRemainderFunction;
|
||||||
|
pub use self::polkavm::context::function::runtime::arithmetics::SignedDivision as PolkaVMSignedDivisionFunction;
|
||||||
|
pub use self::polkavm::context::function::runtime::arithmetics::SignedRemainder as PolkaVMSignedRemainderFunction;
|
||||||
pub use self::polkavm::context::function::runtime::deploy_code::DeployCode as PolkaVMDeployCodeFunction;
|
pub use self::polkavm::context::function::runtime::deploy_code::DeployCode as PolkaVMDeployCodeFunction;
|
||||||
pub use self::polkavm::context::function::runtime::entry::Entry as PolkaVMEntryFunction;
|
pub use self::polkavm::context::function::runtime::entry::Entry as PolkaVMEntryFunction;
|
||||||
pub use self::polkavm::context::function::runtime::immutable_data_load::ImmutableDataLoad as PolkaVMImmutableDataLoadFunction;
|
pub use self::polkavm::context::function::runtime::revive::Exit as PolkaVMExitFunction;
|
||||||
|
pub use self::polkavm::context::function::runtime::revive::WordToPointer as PolkaVMWordToPointerFunction;
|
||||||
pub use self::polkavm::context::function::runtime::runtime_code::RuntimeCode as PolkaVMRuntimeCodeFunction;
|
pub use self::polkavm::context::function::runtime::runtime_code::RuntimeCode as PolkaVMRuntimeCodeFunction;
|
||||||
pub use self::polkavm::context::function::runtime::FUNCTION_DEPLOY_CODE as PolkaVMFunctionDeployCode;
|
pub use self::polkavm::context::function::runtime::FUNCTION_DEPLOY_CODE as PolkaVMFunctionDeployCode;
|
||||||
pub use self::polkavm::context::function::runtime::FUNCTION_ENTRY as PolkaVMFunctionEntry;
|
pub use self::polkavm::context::function::runtime::FUNCTION_ENTRY as PolkaVMFunctionEntry;
|
||||||
pub use self::polkavm::context::function::runtime::FUNCTION_LOAD_IMMUTABLE_DATA as PolkaVMFunctionImmutableDataLoad;
|
|
||||||
pub use self::polkavm::context::function::runtime::FUNCTION_RUNTIME_CODE as PolkaVMFunctionRuntimeCode;
|
pub use self::polkavm::context::function::runtime::FUNCTION_RUNTIME_CODE as PolkaVMFunctionRuntimeCode;
|
||||||
pub use self::polkavm::context::function::yul_data::YulData as PolkaVMFunctionYulData;
|
pub use self::polkavm::context::function::yul_data::YulData as PolkaVMFunctionYulData;
|
||||||
pub use self::polkavm::context::function::Function as PolkaVMFunction;
|
pub use self::polkavm::context::function::Function as PolkaVMFunction;
|
||||||
pub use self::polkavm::context::global::Global as PolkaVMGlobal;
|
pub use self::polkavm::context::global::Global as PolkaVMGlobal;
|
||||||
|
pub use self::polkavm::context::pointer::heap::LoadWord as PolkaVMLoadHeapWordFunction;
|
||||||
|
pub use self::polkavm::context::pointer::heap::StoreWord as PolkaVMStoreHeapWordFunction;
|
||||||
|
pub use self::polkavm::context::pointer::storage::LoadWord as PolkaVMLoadStorageWordFunction;
|
||||||
|
pub use self::polkavm::context::pointer::storage::StoreWord as PolkaVMStoreStorageWordFunction;
|
||||||
pub use self::polkavm::context::pointer::Pointer as PolkaVMPointer;
|
pub use self::polkavm::context::pointer::Pointer as PolkaVMPointer;
|
||||||
pub use self::polkavm::context::r#loop::Loop as PolkaVMLoop;
|
pub use self::polkavm::context::r#loop::Loop as PolkaVMLoop;
|
||||||
pub use self::polkavm::context::solidity_data::SolidityData as PolkaVMContextSolidityData;
|
pub use self::polkavm::context::solidity_data::SolidityData as PolkaVMContextSolidityData;
|
||||||
@@ -52,8 +55,12 @@ pub use self::polkavm::evm::create as polkavm_evm_create;
|
|||||||
pub use self::polkavm::evm::crypto as polkavm_evm_crypto;
|
pub use self::polkavm::evm::crypto as polkavm_evm_crypto;
|
||||||
pub use self::polkavm::evm::ether_gas as polkavm_evm_ether_gas;
|
pub use self::polkavm::evm::ether_gas as polkavm_evm_ether_gas;
|
||||||
pub use self::polkavm::evm::event as polkavm_evm_event;
|
pub use self::polkavm::evm::event as polkavm_evm_event;
|
||||||
|
pub use self::polkavm::evm::event::EventLog as PolkaVMEventLogFunction;
|
||||||
pub use self::polkavm::evm::ext_code as polkavm_evm_ext_code;
|
pub use self::polkavm::evm::ext_code as polkavm_evm_ext_code;
|
||||||
pub use self::polkavm::evm::immutable as polkavm_evm_immutable;
|
pub use self::polkavm::evm::immutable as polkavm_evm_immutable;
|
||||||
|
pub use self::polkavm::evm::immutable::Load as PolkaVMLoadImmutableDataFunction;
|
||||||
|
pub use self::polkavm::evm::immutable::Store as PolkaVMStoreImmutableDataFunction;
|
||||||
|
|
||||||
pub use self::polkavm::evm::math as polkavm_evm_math;
|
pub use self::polkavm::evm::math as polkavm_evm_math;
|
||||||
pub use self::polkavm::evm::memory as polkavm_evm_memory;
|
pub use self::polkavm::evm::memory as polkavm_evm_memory;
|
||||||
pub use self::polkavm::evm::r#return as polkavm_evm_return;
|
pub use self::polkavm::evm::r#return as polkavm_evm_return;
|
||||||
|
|||||||
@@ -6,15 +6,9 @@ pub const LLVM_VERSION: semver::Version = semver::Version::new(18, 1, 4);
|
|||||||
/// The pointer width sized type.
|
/// The pointer width sized type.
|
||||||
pub static XLEN: usize = revive_common::BIT_LENGTH_X32;
|
pub static XLEN: usize = revive_common::BIT_LENGTH_X32;
|
||||||
|
|
||||||
/// The heap memory pointer pointer global variable name.
|
|
||||||
pub static GLOBAL_HEAP_MEMORY_POINTER: &str = "memory_pointer";
|
|
||||||
|
|
||||||
/// The calldata size global variable name.
|
/// The calldata size global variable name.
|
||||||
pub static GLOBAL_CALLDATA_SIZE: &str = "calldatasize";
|
pub static GLOBAL_CALLDATA_SIZE: &str = "calldatasize";
|
||||||
|
|
||||||
/// The call flags global variable name.
|
|
||||||
pub static GLOBAL_CALL_FLAGS: &str = "call_flags";
|
|
||||||
|
|
||||||
/// The deployer call header size that consists of:
|
/// The deployer call header size that consists of:
|
||||||
/// - bytecode hash (32 bytes)
|
/// - bytecode hash (32 bytes)
|
||||||
pub const DEPLOYER_CALL_HEADER_SIZE: usize = revive_common::BYTE_LENGTH_WORD;
|
pub const DEPLOYER_CALL_HEADER_SIZE: usize = revive_common::BYTE_LENGTH_WORD;
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
//! The LLVM IR generator EVM legacy assembly data.
|
|
||||||
|
|
||||||
use crate::polkavm::context::argument::Argument;
|
|
||||||
|
|
||||||
/// The LLVM IR generator EVM legacy assembly data.
|
|
||||||
/// Describes some data that is only relevant to the EVM legacy assembly.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct EVMLAData<'ctx> {
|
|
||||||
/// The Solidity compiler version.
|
|
||||||
/// Some instruction behave differenly depending on the version.
|
|
||||||
pub version: semver::Version,
|
|
||||||
/// The static stack allocated for the current function.
|
|
||||||
pub stack: Vec<Argument<'ctx>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EVMLAData<'_> {
|
|
||||||
/// The default stack size.
|
|
||||||
pub const DEFAULT_STACK_SIZE: usize = 64;
|
|
||||||
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new(version: semver::Version) -> Self {
|
|
||||||
Self {
|
|
||||||
version,
|
|
||||||
stack: Vec::with_capacity(Self::DEFAULT_STACK_SIZE),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
//! The LLVM IR generator function block key.
|
|
||||||
|
|
||||||
use crate::polkavm::context::code_type::CodeType;
|
|
||||||
|
|
||||||
/// The LLVM IR generator function block key.
|
|
||||||
/// Is only relevant to the EVM legacy assembly.
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
||||||
pub struct Key {
|
|
||||||
/// The block code type.
|
|
||||||
pub code_type: CodeType,
|
|
||||||
/// The block tag.
|
|
||||||
pub tag: num::BigUint,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Key {
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new(code_type: CodeType, tag: num::BigUint) -> Self {
|
|
||||||
Self { code_type, tag }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Key {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"{}_{}",
|
|
||||||
match self.code_type {
|
|
||||||
CodeType::Deploy => "dt",
|
|
||||||
CodeType::Runtime => "rt",
|
|
||||||
},
|
|
||||||
self.tag
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
//! The LLVM function block EVM legacy assembly data.
|
|
||||||
|
|
||||||
pub mod key;
|
|
||||||
|
|
||||||
/// The LLVM function block EVM legacy assembly data.
|
|
||||||
/// Describes some data that is only relevant to the EVM legacy assembly.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct EVMLAData {
|
|
||||||
/// The initial hashes of the allowed stack states.
|
|
||||||
pub stack_hashes: Vec<md5::Digest>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EVMLAData {
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new(stack_hashes: Vec<md5::Digest>) -> Self {
|
|
||||||
Self { stack_hashes }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
//! The LLVM IR generator function block.
|
|
||||||
|
|
||||||
pub mod evmla_data;
|
|
||||||
|
|
||||||
use self::evmla_data::EVMLAData;
|
|
||||||
|
|
||||||
/// The LLVM IR generator function block.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Block<'ctx> {
|
|
||||||
/// The inner block.
|
|
||||||
inner: inkwell::basic_block::BasicBlock<'ctx>,
|
|
||||||
/// The EVM legacy assembly compiler data.
|
|
||||||
evmla_data: Option<EVMLAData>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'ctx> Block<'ctx> {
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new(inner: inkwell::basic_block::BasicBlock<'ctx>) -> Self {
|
|
||||||
Self {
|
|
||||||
inner,
|
|
||||||
evmla_data: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the EVM legacy assembly data.
|
|
||||||
pub fn set_evmla_data(&mut self, data: EVMLAData) {
|
|
||||||
self.evmla_data = Some(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The LLVM object reference.
|
|
||||||
pub fn inner(&self) -> inkwell::basic_block::BasicBlock<'ctx> {
|
|
||||||
self.inner
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the EVM data reference.
|
|
||||||
/// # Panics
|
|
||||||
/// If the EVM data has not been initialized.
|
|
||||||
pub fn evm(&self) -> &EVMLAData {
|
|
||||||
self.evmla_data
|
|
||||||
.as_ref()
|
|
||||||
.expect("The EVM data must have been initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the EVM data mutable reference.
|
|
||||||
/// # Panics
|
|
||||||
/// If the EVM data has not been initialized.
|
|
||||||
pub fn evm_mut(&mut self) -> &mut EVMLAData {
|
|
||||||
self.evmla_data
|
|
||||||
.as_mut()
|
|
||||||
.expect("The EVM data must have been initialized")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
//! The LLVM function EVM legacy assembly data.
|
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
use crate::polkavm::context::function::block::evmla_data::key::Key as BlockKey;
|
|
||||||
use crate::polkavm::context::function::block::Block;
|
|
||||||
|
|
||||||
/// The LLVM function EVM legacy assembly data.
|
|
||||||
/// Describes some data that is only relevant to the EVM legacy assembly.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct EVMLAData<'ctx> {
|
|
||||||
/// The ordinary blocks with numeric tags.
|
|
||||||
/// Is only used by the Solidity EVM compiler.
|
|
||||||
pub blocks: BTreeMap<BlockKey, Vec<Block<'ctx>>>,
|
|
||||||
/// The function stack size.
|
|
||||||
pub stack_size: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'ctx> EVMLAData<'ctx> {
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new(stack_size: usize) -> Self {
|
|
||||||
Self {
|
|
||||||
blocks: BTreeMap::new(),
|
|
||||||
stack_size,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Inserts a function block.
|
|
||||||
pub fn insert_block(&mut self, key: BlockKey, block: Block<'ctx>) {
|
|
||||||
if let Some(blocks) = self.blocks.get_mut(&key) {
|
|
||||||
blocks.push(block);
|
|
||||||
} else {
|
|
||||||
self.blocks.insert(key, vec![block]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the block with the specified tag and initial stack pattern.
|
|
||||||
/// If there is only one block, it is returned unconditionally.
|
|
||||||
pub fn find_block(
|
|
||||||
&self,
|
|
||||||
key: &BlockKey,
|
|
||||||
stack_hash: &md5::Digest,
|
|
||||||
) -> anyhow::Result<Block<'ctx>> {
|
|
||||||
if self
|
|
||||||
.blocks
|
|
||||||
.get(key)
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key))?
|
|
||||||
.len()
|
|
||||||
== 1
|
|
||||||
{
|
|
||||||
return self
|
|
||||||
.blocks
|
|
||||||
.get(key)
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key))?
|
|
||||||
.first()
|
|
||||||
.cloned()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key));
|
|
||||||
}
|
|
||||||
|
|
||||||
self.blocks
|
|
||||||
.get(key)
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key))?
|
|
||||||
.iter()
|
|
||||||
.find(|block| {
|
|
||||||
block
|
|
||||||
.evm()
|
|
||||||
.stack_hashes
|
|
||||||
.iter()
|
|
||||||
.any(|hash| hash == stack_hash)
|
|
||||||
})
|
|
||||||
.cloned()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Undeclared function block {}", key))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -91,7 +91,7 @@ impl<'ctx> LLVMRuntime<'ctx> {
|
|||||||
llvm,
|
llvm,
|
||||||
sha3,
|
sha3,
|
||||||
//vec![Attribute::ArgMemOnly, Attribute::ReadOnly],
|
//vec![Attribute::ArgMemOnly, Attribute::ReadOnly],
|
||||||
vec![],
|
&[],
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
//! The LLVM IR generator function.
|
//! The LLVM IR generator function.
|
||||||
|
|
||||||
pub mod block;
|
|
||||||
pub mod declaration;
|
pub mod declaration;
|
||||||
pub mod evmla_data;
|
|
||||||
pub mod intrinsics;
|
pub mod intrinsics;
|
||||||
pub mod llvm_runtime;
|
pub mod llvm_runtime;
|
||||||
pub mod r#return;
|
pub mod r#return;
|
||||||
@@ -19,7 +17,6 @@ use crate::polkavm::context::attribute::Attribute;
|
|||||||
use crate::polkavm::context::pointer::Pointer;
|
use crate::polkavm::context::pointer::Pointer;
|
||||||
|
|
||||||
use self::declaration::Declaration;
|
use self::declaration::Declaration;
|
||||||
use self::evmla_data::EVMLAData;
|
|
||||||
use self::r#return::Return;
|
use self::r#return::Return;
|
||||||
use self::yul_data::YulData;
|
use self::yul_data::YulData;
|
||||||
|
|
||||||
@@ -45,8 +42,6 @@ pub struct Function<'ctx> {
|
|||||||
|
|
||||||
/// The Yul compiler data.
|
/// The Yul compiler data.
|
||||||
yul_data: Option<YulData>,
|
yul_data: Option<YulData>,
|
||||||
/// The EVM legacy assembly compiler data.
|
|
||||||
evmla_data: Option<EVMLAData<'ctx>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ctx> Function<'ctx> {
|
impl<'ctx> Function<'ctx> {
|
||||||
@@ -72,7 +67,6 @@ impl<'ctx> Function<'ctx> {
|
|||||||
return_block,
|
return_block,
|
||||||
|
|
||||||
yul_data: None,
|
yul_data: None,
|
||||||
evmla_data: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,8 +81,7 @@ impl<'ctx> Function<'ctx> {
|
|||||||
|| (name.starts_with("__")
|
|| (name.starts_with("__")
|
||||||
&& name != self::runtime::FUNCTION_ENTRY
|
&& name != self::runtime::FUNCTION_ENTRY
|
||||||
&& name != self::runtime::FUNCTION_DEPLOY_CODE
|
&& name != self::runtime::FUNCTION_DEPLOY_CODE
|
||||||
&& name != self::runtime::FUNCTION_RUNTIME_CODE
|
&& name != self::runtime::FUNCTION_RUNTIME_CODE)
|
||||||
&& name != self::runtime::FUNCTION_LOAD_IMMUTABLE_DATA)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the LLVM function declaration.
|
/// Returns the LLVM function declaration.
|
||||||
@@ -116,30 +109,21 @@ impl<'ctx> Function<'ctx> {
|
|||||||
pub fn set_attributes(
|
pub fn set_attributes(
|
||||||
llvm: &'ctx inkwell::context::Context,
|
llvm: &'ctx inkwell::context::Context,
|
||||||
declaration: Declaration<'ctx>,
|
declaration: Declaration<'ctx>,
|
||||||
attributes: Vec<Attribute>,
|
attributes: &[Attribute],
|
||||||
force: bool,
|
force: bool,
|
||||||
) {
|
) {
|
||||||
for attribute_kind in attributes.into_iter() {
|
for attribute_kind in attributes {
|
||||||
match attribute_kind {
|
match attribute_kind {
|
||||||
Attribute::Memory => unimplemented!("`memory` attributes are not implemented"),
|
Attribute::Memory => unimplemented!("`memory` attributes are not implemented"),
|
||||||
attribute_kind @ Attribute::AlwaysInline if force => {
|
attribute_kind @ Attribute::AlwaysInline if force => {
|
||||||
let is_optimize_none_set = declaration
|
declaration.value.remove_enum_attribute(
|
||||||
.value
|
inkwell::attributes::AttributeLoc::Function,
|
||||||
.get_enum_attribute(
|
Attribute::NoInline as u32,
|
||||||
inkwell::attributes::AttributeLoc::Function,
|
);
|
||||||
Attribute::OptimizeNone as u32,
|
declaration.value.add_attribute(
|
||||||
)
|
inkwell::attributes::AttributeLoc::Function,
|
||||||
.is_some();
|
llvm.create_enum_attribute(*attribute_kind as u32, 0),
|
||||||
if !is_optimize_none_set {
|
);
|
||||||
declaration.value.remove_enum_attribute(
|
|
||||||
inkwell::attributes::AttributeLoc::Function,
|
|
||||||
Attribute::NoInline as u32,
|
|
||||||
);
|
|
||||||
declaration.value.add_attribute(
|
|
||||||
inkwell::attributes::AttributeLoc::Function,
|
|
||||||
llvm.create_enum_attribute(attribute_kind as u32, 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
attribute_kind @ Attribute::NoInline if force => {
|
attribute_kind @ Attribute::NoInline if force => {
|
||||||
declaration.value.remove_enum_attribute(
|
declaration.value.remove_enum_attribute(
|
||||||
@@ -148,12 +132,12 @@ impl<'ctx> Function<'ctx> {
|
|||||||
);
|
);
|
||||||
declaration.value.add_attribute(
|
declaration.value.add_attribute(
|
||||||
inkwell::attributes::AttributeLoc::Function,
|
inkwell::attributes::AttributeLoc::Function,
|
||||||
llvm.create_enum_attribute(attribute_kind as u32, 0),
|
llvm.create_enum_attribute(*attribute_kind as u32, 0),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
attribute_kind => declaration.value.add_attribute(
|
attribute_kind => declaration.value.add_attribute(
|
||||||
inkwell::attributes::AttributeLoc::Function,
|
inkwell::attributes::AttributeLoc::Function,
|
||||||
llvm.create_enum_attribute(attribute_kind as u32, 0),
|
llvm.create_enum_attribute(*attribute_kind as u32, 0),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,27 +168,16 @@ impl<'ctx> Function<'ctx> {
|
|||||||
declaration: Declaration<'ctx>,
|
declaration: Declaration<'ctx>,
|
||||||
optimizer: &Optimizer,
|
optimizer: &Optimizer,
|
||||||
) {
|
) {
|
||||||
if optimizer.settings().level_middle_end == inkwell::OptimizationLevel::None {
|
if optimizer.settings().level_middle_end_size == SizeLevel::Z {
|
||||||
Self::remove_attributes(
|
|
||||||
declaration,
|
|
||||||
&[Attribute::OptimizeForSize, Attribute::AlwaysInline],
|
|
||||||
);
|
|
||||||
Self::set_attributes(
|
Self::set_attributes(
|
||||||
llvm,
|
llvm,
|
||||||
declaration,
|
declaration,
|
||||||
vec![Attribute::OptimizeNone, Attribute::NoInline],
|
&[Attribute::OptimizeForSize, Attribute::MinSize],
|
||||||
false,
|
|
||||||
);
|
|
||||||
} else if optimizer.settings().level_middle_end_size == SizeLevel::Z {
|
|
||||||
Self::set_attributes(
|
|
||||||
llvm,
|
|
||||||
declaration,
|
|
||||||
vec![Attribute::OptimizeForSize, Attribute::MinSize],
|
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::set_attributes(llvm, declaration, vec![Attribute::NoFree], false);
|
Self::set_attributes(llvm, declaration, &[Attribute::NoFree], false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the front-end runtime attributes.
|
/// Sets the front-end runtime attributes.
|
||||||
@@ -214,26 +187,10 @@ impl<'ctx> Function<'ctx> {
|
|||||||
optimizer: &Optimizer,
|
optimizer: &Optimizer,
|
||||||
) {
|
) {
|
||||||
if optimizer.settings().level_middle_end_size == SizeLevel::Z {
|
if optimizer.settings().level_middle_end_size == SizeLevel::Z {
|
||||||
Self::set_attributes(llvm, declaration, vec![Attribute::NoInline], false);
|
Self::set_attributes(llvm, declaration, &[Attribute::NoInline], false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the exception handler attributes.
|
|
||||||
pub fn set_exception_handler_attributes(
|
|
||||||
llvm: &'ctx inkwell::context::Context,
|
|
||||||
declaration: Declaration<'ctx>,
|
|
||||||
) {
|
|
||||||
Self::set_attributes(llvm, declaration, vec![Attribute::NoInline], false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the CXA-throw attributes.
|
|
||||||
pub fn set_cxa_throw_attributes(
|
|
||||||
llvm: &'ctx inkwell::context::Context,
|
|
||||||
declaration: Declaration<'ctx>,
|
|
||||||
) {
|
|
||||||
Self::set_attributes(llvm, declaration, vec![Attribute::NoProfile], false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the pure function attributes.
|
/// Sets the pure function attributes.
|
||||||
pub fn set_pure_function_attributes(
|
pub fn set_pure_function_attributes(
|
||||||
llvm: &'ctx inkwell::context::Context,
|
llvm: &'ctx inkwell::context::Context,
|
||||||
@@ -242,7 +199,7 @@ impl<'ctx> Function<'ctx> {
|
|||||||
Self::set_attributes(
|
Self::set_attributes(
|
||||||
llvm,
|
llvm,
|
||||||
declaration,
|
declaration,
|
||||||
vec![
|
&[
|
||||||
Attribute::MustProgress,
|
Attribute::MustProgress,
|
||||||
Attribute::NoUnwind,
|
Attribute::NoUnwind,
|
||||||
Attribute::WillReturn,
|
Attribute::WillReturn,
|
||||||
@@ -300,29 +257,6 @@ impl<'ctx> Function<'ctx> {
|
|||||||
self.return_block
|
self.return_block
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the EVM legacy assembly data.
|
|
||||||
pub fn set_evmla_data(&mut self, data: EVMLAData<'ctx>) {
|
|
||||||
self.evmla_data = Some(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the EVM legacy assembly data reference.
|
|
||||||
/// # Panics
|
|
||||||
/// If the EVM data has not been initialized.
|
|
||||||
pub fn evmla(&self) -> &EVMLAData<'ctx> {
|
|
||||||
self.evmla_data
|
|
||||||
.as_ref()
|
|
||||||
.expect("The EVM data must have been initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the EVM legacy assembly data mutable reference.
|
|
||||||
/// # Panics
|
|
||||||
/// If the EVM data has not been initialized.
|
|
||||||
pub fn evmla_mut(&mut self) -> &mut EVMLAData<'ctx> {
|
|
||||||
self.evmla_data
|
|
||||||
.as_mut()
|
|
||||||
.expect("The EVM data must have been initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the Yul data.
|
/// Sets the Yul data.
|
||||||
pub fn set_yul_data(&mut self, data: YulData) {
|
pub fn set_yul_data(&mut self, data: YulData) {
|
||||||
self.yul_data = Some(data);
|
self.yul_data = Some(data);
|
||||||
|
|||||||
@@ -0,0 +1,269 @@
|
|||||||
|
//! Translates the arithmetic operations.
|
||||||
|
|
||||||
|
use inkwell::values::BasicValue;
|
||||||
|
|
||||||
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
||||||
|
use crate::polkavm::context::Context;
|
||||||
|
use crate::polkavm::Dependency;
|
||||||
|
use crate::polkavm::WriteLLVM;
|
||||||
|
|
||||||
|
/// Implements the division operator according to the EVM specification.
|
||||||
|
pub struct Division;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for Division
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_division";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.word_type().fn_type(
|
||||||
|
&[context.word_type().into(), context.word_type().into()],
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
|
||||||
|
let operand_1 = Self::paramater(context, 0).into_int_value();
|
||||||
|
let operand_2 = Self::paramater(context, 1).into_int_value();
|
||||||
|
|
||||||
|
wrapped_division(context, operand_2, || {
|
||||||
|
Ok(context
|
||||||
|
.builder()
|
||||||
|
.build_int_unsigned_div(operand_1, operand_2, "DIV")?)
|
||||||
|
})
|
||||||
|
.map(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for Division
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implements the signed division operator according to the EVM specification.
|
||||||
|
pub struct SignedDivision;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for SignedDivision
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_signed_division";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.word_type().fn_type(
|
||||||
|
&[context.word_type().into(), context.word_type().into()],
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
|
||||||
|
let operand_1 = Self::paramater(context, 0).into_int_value();
|
||||||
|
let operand_2 = Self::paramater(context, 1).into_int_value();
|
||||||
|
|
||||||
|
let block_calculate = context.append_basic_block("calculate");
|
||||||
|
let block_overflow = context.append_basic_block("overflow");
|
||||||
|
let block_select = context.append_basic_block("select_result");
|
||||||
|
let block_origin = context.basic_block();
|
||||||
|
context.builder().build_switch(
|
||||||
|
operand_2,
|
||||||
|
block_calculate,
|
||||||
|
&[
|
||||||
|
(context.word_type().const_zero(), block_select),
|
||||||
|
(context.word_type().const_all_ones(), block_overflow),
|
||||||
|
],
|
||||||
|
)?;
|
||||||
|
|
||||||
|
context.set_basic_block(block_calculate);
|
||||||
|
let quotient = context
|
||||||
|
.builder()
|
||||||
|
.build_int_signed_div(operand_1, operand_2, "SDIV")?;
|
||||||
|
context.build_unconditional_branch(block_select);
|
||||||
|
|
||||||
|
context.set_basic_block(block_overflow);
|
||||||
|
let max_uint = context.builder().build_int_z_extend(
|
||||||
|
context
|
||||||
|
.integer_type(revive_common::BIT_LENGTH_WORD - 1)
|
||||||
|
.const_all_ones(),
|
||||||
|
context.word_type(),
|
||||||
|
"max_uint",
|
||||||
|
)?;
|
||||||
|
let is_operand_1_overflow = context.builder().build_int_compare(
|
||||||
|
inkwell::IntPredicate::EQ,
|
||||||
|
operand_1,
|
||||||
|
context.builder().build_int_neg(max_uint, "min_uint")?,
|
||||||
|
"is_operand_1_overflow",
|
||||||
|
)?;
|
||||||
|
context.build_conditional_branch(is_operand_1_overflow, block_select, block_calculate)?;
|
||||||
|
|
||||||
|
context.set_basic_block(block_select);
|
||||||
|
let result = context.builder().build_phi(context.word_type(), "result")?;
|
||||||
|
result.add_incoming(&[
|
||||||
|
(&operand_1, block_overflow),
|
||||||
|
(&context.word_const(0), block_origin),
|
||||||
|
("ient.as_basic_value_enum(), block_calculate),
|
||||||
|
]);
|
||||||
|
Ok(Some(result.as_basic_value()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for SignedDivision
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implements the remainder operator according to the EVM specification.
|
||||||
|
pub struct Remainder;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for Remainder
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_remainder";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.word_type().fn_type(
|
||||||
|
&[context.word_type().into(), context.word_type().into()],
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
|
||||||
|
let operand_1 = Self::paramater(context, 0).into_int_value();
|
||||||
|
let operand_2 = Self::paramater(context, 1).into_int_value();
|
||||||
|
|
||||||
|
wrapped_division(context, operand_2, || {
|
||||||
|
Ok(context
|
||||||
|
.builder()
|
||||||
|
.build_int_unsigned_rem(operand_1, operand_2, "MOD")?)
|
||||||
|
})
|
||||||
|
.map(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for Remainder
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implements the signed remainder operator according to the EVM specification.
|
||||||
|
pub struct SignedRemainder;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for SignedRemainder
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_signed_remainder";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.word_type().fn_type(
|
||||||
|
&[context.word_type().into(), context.word_type().into()],
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
|
||||||
|
let operand_1 = Self::paramater(context, 0).into_int_value();
|
||||||
|
let operand_2 = Self::paramater(context, 1).into_int_value();
|
||||||
|
|
||||||
|
wrapped_division(context, operand_2, || {
|
||||||
|
Ok(context
|
||||||
|
.builder()
|
||||||
|
.build_int_signed_rem(operand_1, operand_2, "SMOD")?)
|
||||||
|
})
|
||||||
|
.map(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for SignedRemainder
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wrap division operations so that zero will be returned if the
|
||||||
|
/// denominator is zero (see also Ethereum YP Appendix H.2).
|
||||||
|
///
|
||||||
|
/// The closure is expected to calculate and return the quotient.
|
||||||
|
///
|
||||||
|
/// The result is either the calculated quotient or zero,
|
||||||
|
/// selected at runtime.
|
||||||
|
fn wrapped_division<'ctx, D, F, T>(
|
||||||
|
context: &Context<'ctx, D>,
|
||||||
|
denominator: inkwell::values::IntValue<'ctx>,
|
||||||
|
f: F,
|
||||||
|
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
F: FnOnce() -> anyhow::Result<T>,
|
||||||
|
T: inkwell::values::IntMathValue<'ctx>,
|
||||||
|
{
|
||||||
|
assert_eq!(
|
||||||
|
denominator.get_type().get_bit_width(),
|
||||||
|
revive_common::BIT_LENGTH_WORD as u32
|
||||||
|
);
|
||||||
|
|
||||||
|
let block_calculate = context.append_basic_block("calculate");
|
||||||
|
let block_select = context.append_basic_block("select");
|
||||||
|
let block_origin = context.basic_block();
|
||||||
|
context.builder().build_switch(
|
||||||
|
denominator,
|
||||||
|
block_calculate,
|
||||||
|
&[(context.word_const(0), block_select)],
|
||||||
|
)?;
|
||||||
|
|
||||||
|
context.set_basic_block(block_calculate);
|
||||||
|
let calculated_value = f()?.as_basic_value_enum();
|
||||||
|
context.build_unconditional_branch(block_select);
|
||||||
|
|
||||||
|
context.set_basic_block(block_select);
|
||||||
|
let result = context.builder().build_phi(context.word_type(), "result")?;
|
||||||
|
result.add_incoming(&[
|
||||||
|
(&context.word_const(0), block_origin),
|
||||||
|
(&calculated_value, block_calculate),
|
||||||
|
]);
|
||||||
|
Ok(result.as_basic_value())
|
||||||
|
}
|
||||||
@@ -24,22 +24,6 @@ impl Entry {
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
context.set_global(
|
|
||||||
crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER,
|
|
||||||
context.llvm().ptr_type(AddressSpace::Heap.into()),
|
|
||||||
AddressSpace::Stack,
|
|
||||||
context.xlen_type().get_undef(),
|
|
||||||
);
|
|
||||||
context.build_store(
|
|
||||||
context
|
|
||||||
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
|
|
||||||
.into(),
|
|
||||||
context.build_sbrk(
|
|
||||||
context.xlen_type().const_zero(),
|
|
||||||
context.xlen_type().const_zero(),
|
|
||||||
)?,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
context.set_global(
|
context.set_global(
|
||||||
crate::polkavm::GLOBAL_CALLDATA_SIZE,
|
crate::polkavm::GLOBAL_CALLDATA_SIZE,
|
||||||
context.xlen_type(),
|
context.xlen_type(),
|
||||||
@@ -47,13 +31,6 @@ impl Entry {
|
|||||||
context.xlen_type().get_undef(),
|
context.xlen_type().get_undef(),
|
||||||
);
|
);
|
||||||
|
|
||||||
context.set_global(
|
|
||||||
crate::polkavm::GLOBAL_CALL_FLAGS,
|
|
||||||
context.word_type(),
|
|
||||||
AddressSpace::Stack,
|
|
||||||
context.word_const(0),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,6 +47,11 @@ impl Entry {
|
|||||||
.build_runtime_call(revive_runtime_api::polkavm_imports::CALL_DATA_SIZE, &[])
|
.build_runtime_call(revive_runtime_api::polkavm_imports::CALL_DATA_SIZE, &[])
|
||||||
.expect("the call_data_size syscall method should return a value")
|
.expect("the call_data_size syscall method should return a value")
|
||||||
.into_int_value();
|
.into_int_value();
|
||||||
|
let call_data_size_value = context.builder().build_int_truncate(
|
||||||
|
call_data_size_value,
|
||||||
|
context.xlen_type(),
|
||||||
|
"call_data_size_truncated",
|
||||||
|
)?;
|
||||||
context
|
context
|
||||||
.builder()
|
.builder()
|
||||||
.build_store(call_data_size_pointer, call_data_size_value)?;
|
.build_store(call_data_size_pointer, call_data_size_value)?;
|
||||||
@@ -90,13 +72,6 @@ impl Entry {
|
|||||||
.borrow()
|
.borrow()
|
||||||
.get_nth_param(Self::ARGUMENT_INDEX_CALL_FLAGS);
|
.get_nth_param(Self::ARGUMENT_INDEX_CALL_FLAGS);
|
||||||
|
|
||||||
context.set_global(
|
|
||||||
crate::polkavm::GLOBAL_CALL_FLAGS,
|
|
||||||
is_deploy.get_type(),
|
|
||||||
AddressSpace::Stack,
|
|
||||||
is_deploy.into_int_value(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let deploy_code_call_block = context.append_basic_block("deploy_code_call_block");
|
let deploy_code_call_block = context.append_basic_block("deploy_code_call_block");
|
||||||
let runtime_code_call_block = context.append_basic_block("runtime_code_call_block");
|
let runtime_code_call_block = context.append_basic_block("runtime_code_call_block");
|
||||||
|
|
||||||
@@ -170,7 +145,7 @@ where
|
|||||||
crate::PolkaVMFunction::set_attributes(
|
crate::PolkaVMFunction::set_attributes(
|
||||||
context.llvm(),
|
context.llvm(),
|
||||||
entry,
|
entry,
|
||||||
vec![crate::PolkaVMAttribute::NoReturn],
|
&[crate::PolkaVMAttribute::NoReturn],
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,118 +0,0 @@
|
|||||||
//! The immutable data runtime function.
|
|
||||||
|
|
||||||
use crate::polkavm::context::address_space::AddressSpace;
|
|
||||||
use crate::polkavm::context::function::runtime;
|
|
||||||
use crate::polkavm::context::pointer::Pointer;
|
|
||||||
use crate::polkavm::context::Context;
|
|
||||||
use crate::polkavm::Dependency;
|
|
||||||
use crate::polkavm::WriteLLVM;
|
|
||||||
|
|
||||||
/// A function for requesting the immutable data from the runtime.
|
|
||||||
/// This is a special function that is only used by the front-end generated code.
|
|
||||||
///
|
|
||||||
/// The runtime API is called lazily and subsequent calls are no-ops.
|
|
||||||
///
|
|
||||||
/// The bytes written is asserted to match the expected length.
|
|
||||||
/// This should never fail; the length is known.
|
|
||||||
/// However, this is a one time assertion, hence worth it.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ImmutableDataLoad;
|
|
||||||
|
|
||||||
impl<D> WriteLLVM<D> for ImmutableDataLoad
|
|
||||||
where
|
|
||||||
D: Dependency + Clone,
|
|
||||||
{
|
|
||||||
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
|
||||||
context.add_function(
|
|
||||||
runtime::FUNCTION_LOAD_IMMUTABLE_DATA,
|
|
||||||
context.void_type().fn_type(Default::default(), false),
|
|
||||||
0,
|
|
||||||
Some(inkwell::module::Linkage::External),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
|
||||||
context.set_current_function(runtime::FUNCTION_LOAD_IMMUTABLE_DATA, None)?;
|
|
||||||
context.set_basic_block(context.current_function().borrow().entry_block());
|
|
||||||
|
|
||||||
let immutable_data_size_pointer = context
|
|
||||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE)?
|
|
||||||
.value
|
|
||||||
.as_pointer_value();
|
|
||||||
let immutable_data_size = context.build_load(
|
|
||||||
Pointer::new(
|
|
||||||
context.xlen_type(),
|
|
||||||
AddressSpace::Stack,
|
|
||||||
immutable_data_size_pointer,
|
|
||||||
),
|
|
||||||
"immutable_data_size_load",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let load_immutable_data_block = context.append_basic_block("load_immutables_block");
|
|
||||||
let return_block = context.current_function().borrow().return_block();
|
|
||||||
let immutable_data_size_is_zero = context.builder().build_int_compare(
|
|
||||||
inkwell::IntPredicate::EQ,
|
|
||||||
context.xlen_type().const_zero(),
|
|
||||||
immutable_data_size.into_int_value(),
|
|
||||||
"immutable_data_size_is_zero",
|
|
||||||
)?;
|
|
||||||
context.build_conditional_branch(
|
|
||||||
immutable_data_size_is_zero,
|
|
||||||
return_block,
|
|
||||||
load_immutable_data_block,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
context.set_basic_block(load_immutable_data_block);
|
|
||||||
let output_pointer = context
|
|
||||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
|
|
||||||
.value
|
|
||||||
.as_pointer_value();
|
|
||||||
context.build_runtime_call(
|
|
||||||
revive_runtime_api::polkavm_imports::GET_IMMUTABLE_DATA,
|
|
||||||
&[
|
|
||||||
context
|
|
||||||
.builder()
|
|
||||||
.build_ptr_to_int(output_pointer, context.xlen_type(), "ptr_to_xlen")?
|
|
||||||
.into(),
|
|
||||||
context
|
|
||||||
.builder()
|
|
||||||
.build_ptr_to_int(
|
|
||||||
immutable_data_size_pointer,
|
|
||||||
context.xlen_type(),
|
|
||||||
"ptr_to_xlen",
|
|
||||||
)?
|
|
||||||
.into(),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
let bytes_written = context.builder().build_load(
|
|
||||||
context.xlen_type(),
|
|
||||||
immutable_data_size_pointer,
|
|
||||||
"bytes_written",
|
|
||||||
)?;
|
|
||||||
context.builder().build_store(
|
|
||||||
immutable_data_size_pointer,
|
|
||||||
context.xlen_type().const_zero(),
|
|
||||||
)?;
|
|
||||||
let overflow_block = context.append_basic_block("immutable_data_overflow");
|
|
||||||
let is_overflow = context.builder().build_int_compare(
|
|
||||||
inkwell::IntPredicate::UGT,
|
|
||||||
immutable_data_size.into_int_value(),
|
|
||||||
bytes_written.into_int_value(),
|
|
||||||
"is_overflow",
|
|
||||||
)?;
|
|
||||||
context.build_conditional_branch(is_overflow, overflow_block, return_block)?;
|
|
||||||
|
|
||||||
context.set_basic_block(overflow_block);
|
|
||||||
context.build_call(context.intrinsics().trap, &[], "invalid_trap");
|
|
||||||
context.build_unreachable();
|
|
||||||
|
|
||||||
context.set_basic_block(return_block);
|
|
||||||
context.build_return(None);
|
|
||||||
|
|
||||||
context.pop_debug_scope();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
//! The front-end runtime functions.
|
//! The front-end runtime functions.
|
||||||
|
|
||||||
|
pub mod arithmetics;
|
||||||
pub mod deploy_code;
|
pub mod deploy_code;
|
||||||
pub mod entry;
|
pub mod entry;
|
||||||
pub mod immutable_data_load;
|
pub mod revive;
|
||||||
pub mod runtime_code;
|
pub mod runtime_code;
|
||||||
|
|
||||||
/// The main entry function name.
|
/// The main entry function name.
|
||||||
@@ -13,6 +14,3 @@ pub const FUNCTION_DEPLOY_CODE: &str = "__deploy";
|
|||||||
|
|
||||||
/// The runtime code function name.
|
/// The runtime code function name.
|
||||||
pub const FUNCTION_RUNTIME_CODE: &str = "__runtime";
|
pub const FUNCTION_RUNTIME_CODE: &str = "__runtime";
|
||||||
|
|
||||||
/// The immutable data load function name.
|
|
||||||
pub const FUNCTION_LOAD_IMMUTABLE_DATA: &str = "__immutable_data_load";
|
|
||||||
|
|||||||
@@ -0,0 +1,147 @@
|
|||||||
|
//! The revive compiler runtime functions.
|
||||||
|
|
||||||
|
use inkwell::values::BasicValue;
|
||||||
|
|
||||||
|
use crate::polkavm::context::function::Attribute;
|
||||||
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
||||||
|
use crate::polkavm::context::Context;
|
||||||
|
use crate::polkavm::Dependency;
|
||||||
|
use crate::polkavm::WriteLLVM;
|
||||||
|
|
||||||
|
/// Pointers are represented as opaque 256 bit integer values in EVM.
|
||||||
|
/// In practice, they should never exceed a register sized bit value.
|
||||||
|
/// However, we still protect against this possibility here: Heap index
|
||||||
|
/// offsets are generally untrusted and potentially represent valid
|
||||||
|
/// (but wrong) pointers when truncated.
|
||||||
|
pub struct WordToPointer;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for WordToPointer
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_int_truncate";
|
||||||
|
|
||||||
|
const ATTRIBUTES: &'static [Attribute] = &[
|
||||||
|
Attribute::WillReturn,
|
||||||
|
Attribute::NoFree,
|
||||||
|
Attribute::AlwaysInline,
|
||||||
|
];
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context
|
||||||
|
.xlen_type()
|
||||||
|
.fn_type(&[context.word_type().into()], false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
|
||||||
|
let value = Self::paramater(context, 0).into_int_value();
|
||||||
|
let truncated =
|
||||||
|
context
|
||||||
|
.builder()
|
||||||
|
.build_int_truncate(value, context.xlen_type(), "offset_truncated")?;
|
||||||
|
let extended = context.builder().build_int_z_extend(
|
||||||
|
truncated,
|
||||||
|
context.word_type(),
|
||||||
|
"offset_extended",
|
||||||
|
)?;
|
||||||
|
let is_overflow = context.builder().build_int_compare(
|
||||||
|
inkwell::IntPredicate::NE,
|
||||||
|
value,
|
||||||
|
extended,
|
||||||
|
"compare_truncated_extended",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let block_continue = context.append_basic_block("offset_pointer_ok");
|
||||||
|
let block_trap = context.append_basic_block("offset_pointer_overflow");
|
||||||
|
context.build_conditional_branch(is_overflow, block_trap, block_continue)?;
|
||||||
|
|
||||||
|
context.set_basic_block(block_trap);
|
||||||
|
context.build_call(context.intrinsics().trap, &[], "invalid_trap");
|
||||||
|
context.build_unreachable();
|
||||||
|
|
||||||
|
context.set_basic_block(block_continue);
|
||||||
|
Ok(Some(truncated.as_basic_value_enum()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for WordToPointer
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The revive runtime exit function.
|
||||||
|
pub struct Exit;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for Exit
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_exit";
|
||||||
|
|
||||||
|
const ATTRIBUTES: &'static [Attribute] = &[
|
||||||
|
Attribute::NoReturn,
|
||||||
|
Attribute::NoFree,
|
||||||
|
Attribute::AlwaysInline,
|
||||||
|
];
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.void_type().fn_type(
|
||||||
|
&[
|
||||||
|
context.xlen_type().into(),
|
||||||
|
context.word_type().into(),
|
||||||
|
context.word_type().into(),
|
||||||
|
],
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
|
||||||
|
let flags = Self::paramater(context, 0).into_int_value();
|
||||||
|
let offset = Self::paramater(context, 1).into_int_value();
|
||||||
|
let length = Self::paramater(context, 2).into_int_value();
|
||||||
|
|
||||||
|
let offset_truncated = context.safe_truncate_int_to_xlen(offset)?;
|
||||||
|
let length_truncated = context.safe_truncate_int_to_xlen(length)?;
|
||||||
|
let heap_pointer = context.build_heap_gep(offset_truncated, length_truncated)?;
|
||||||
|
let offset_pointer = context.builder().build_ptr_to_int(
|
||||||
|
heap_pointer.value,
|
||||||
|
context.xlen_type(),
|
||||||
|
"return_data_ptr_to_int",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
context.build_runtime_call(
|
||||||
|
revive_runtime_api::polkavm_imports::RETURN,
|
||||||
|
&[flags.into(), offset_pointer.into(), length_truncated.into()],
|
||||||
|
);
|
||||||
|
context.build_unreachable();
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for Exit
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,11 +6,11 @@ pub mod attribute;
|
|||||||
pub mod build;
|
pub mod build;
|
||||||
pub mod code_type;
|
pub mod code_type;
|
||||||
pub mod debug_info;
|
pub mod debug_info;
|
||||||
pub mod evmla_data;
|
|
||||||
pub mod function;
|
pub mod function;
|
||||||
pub mod global;
|
pub mod global;
|
||||||
pub mod r#loop;
|
pub mod r#loop;
|
||||||
pub mod pointer;
|
pub mod pointer;
|
||||||
|
pub mod runtime;
|
||||||
pub mod solidity_data;
|
pub mod solidity_data;
|
||||||
pub mod yul_data;
|
pub mod yul_data;
|
||||||
|
|
||||||
@@ -32,21 +32,25 @@ use crate::polkavm::DebugConfig;
|
|||||||
use crate::polkavm::Dependency;
|
use crate::polkavm::Dependency;
|
||||||
use crate::target_machine::target::Target;
|
use crate::target_machine::target::Target;
|
||||||
use crate::target_machine::TargetMachine;
|
use crate::target_machine::TargetMachine;
|
||||||
|
use crate::PolkaVMLoadHeapWordFunction;
|
||||||
|
use crate::PolkaVMStoreHeapWordFunction;
|
||||||
|
|
||||||
use self::address_space::AddressSpace;
|
use self::address_space::AddressSpace;
|
||||||
use self::attribute::Attribute;
|
use self::attribute::Attribute;
|
||||||
use self::build::Build;
|
use self::build::Build;
|
||||||
use self::code_type::CodeType;
|
use self::code_type::CodeType;
|
||||||
use self::debug_info::DebugInfo;
|
use self::debug_info::DebugInfo;
|
||||||
use self::evmla_data::EVMLAData;
|
|
||||||
use self::function::declaration::Declaration as FunctionDeclaration;
|
use self::function::declaration::Declaration as FunctionDeclaration;
|
||||||
use self::function::intrinsics::Intrinsics;
|
use self::function::intrinsics::Intrinsics;
|
||||||
use self::function::llvm_runtime::LLVMRuntime;
|
use self::function::llvm_runtime::LLVMRuntime;
|
||||||
use self::function::r#return::Return as FunctionReturn;
|
use self::function::r#return::Return as FunctionReturn;
|
||||||
|
use self::function::runtime::revive::Exit;
|
||||||
|
use self::function::runtime::revive::WordToPointer;
|
||||||
use self::function::Function;
|
use self::function::Function;
|
||||||
use self::global::Global;
|
use self::global::Global;
|
||||||
use self::pointer::Pointer;
|
use self::pointer::Pointer;
|
||||||
use self::r#loop::Loop;
|
use self::r#loop::Loop;
|
||||||
|
use self::runtime::RuntimeFunction;
|
||||||
use self::solidity_data::SolidityData;
|
use self::solidity_data::SolidityData;
|
||||||
use self::yul_data::YulData;
|
use self::yul_data::YulData;
|
||||||
|
|
||||||
@@ -95,8 +99,6 @@ where
|
|||||||
solidity_data: Option<SolidityData>,
|
solidity_data: Option<SolidityData>,
|
||||||
/// The Yul data.
|
/// The Yul data.
|
||||||
yul_data: Option<YulData>,
|
yul_data: Option<YulData>,
|
||||||
/// The EVM legacy assembly data.
|
|
||||||
evmla_data: Option<EVMLAData<'ctx>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ctx, D> Context<'ctx, D>
|
impl<'ctx, D> Context<'ctx, D>
|
||||||
@@ -257,7 +259,6 @@ where
|
|||||||
|
|
||||||
solidity_data: None,
|
solidity_data: None,
|
||||||
yul_data: None,
|
yul_data: None,
|
||||||
evmla_data: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -726,6 +727,7 @@ where
|
|||||||
name: &str,
|
name: &str,
|
||||||
) -> Pointer<'ctx> {
|
) -> Pointer<'ctx> {
|
||||||
let pointer = self.builder.build_alloca(r#type, name).unwrap();
|
let pointer = self.builder.build_alloca(r#type, name).unwrap();
|
||||||
|
|
||||||
pointer
|
pointer
|
||||||
.as_instruction()
|
.as_instruction()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -773,60 +775,21 @@ where
|
|||||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
|
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
|
||||||
match pointer.address_space {
|
match pointer.address_space {
|
||||||
AddressSpace::Heap => {
|
AddressSpace::Heap => {
|
||||||
let heap_pointer = self.build_heap_gep(
|
let name = <PolkaVMLoadHeapWordFunction as RuntimeFunction<D>>::NAME;
|
||||||
self.builder().build_ptr_to_int(
|
let declaration =
|
||||||
pointer.value,
|
<PolkaVMLoadHeapWordFunction as RuntimeFunction<D>>::declaration(self);
|
||||||
self.xlen_type(),
|
let arguments = [self
|
||||||
"offset_ptrtoint",
|
|
||||||
)?,
|
|
||||||
pointer
|
|
||||||
.r#type
|
|
||||||
.size_of()
|
|
||||||
.expect("should be IntValue")
|
|
||||||
.const_truncate(self.xlen_type()),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let value = self
|
|
||||||
.builder()
|
.builder()
|
||||||
.build_load(pointer.r#type, heap_pointer.value, name)?;
|
.build_ptr_to_int(pointer.value, self.xlen_type(), "offset_ptrtoint")?
|
||||||
self.basic_block()
|
.as_basic_value_enum()];
|
||||||
.get_last_instruction()
|
Ok(self
|
||||||
.expect("Always exists")
|
.build_call(declaration, &arguments, "heap_load")
|
||||||
.set_alignment(revive_common::BYTE_LENGTH_BYTE as u32)
|
.unwrap_or_else(|| {
|
||||||
.expect("Alignment is valid");
|
panic!("revive runtime function {name} should return a value")
|
||||||
|
}))
|
||||||
self.build_byte_swap(value)
|
|
||||||
}
|
}
|
||||||
AddressSpace::Storage | AddressSpace::TransientStorage => {
|
AddressSpace::Storage | AddressSpace::TransientStorage => {
|
||||||
let storage_value_pointer =
|
unreachable!("should use the runtime function")
|
||||||
self.build_alloca(self.word_type(), "storage_value_pointer");
|
|
||||||
self.build_store(storage_value_pointer, self.word_const(0))?;
|
|
||||||
|
|
||||||
let storage_value_length_pointer =
|
|
||||||
self.build_alloca(self.xlen_type(), "storage_value_length_pointer");
|
|
||||||
self.build_store(
|
|
||||||
storage_value_length_pointer,
|
|
||||||
self.word_const(revive_common::BIT_LENGTH_WORD as u64),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let transient = pointer.address_space == AddressSpace::TransientStorage;
|
|
||||||
|
|
||||||
self.build_runtime_call(
|
|
||||||
revive_runtime_api::polkavm_imports::GET_STORAGE,
|
|
||||||
&[
|
|
||||||
self.xlen_type().const_int(transient as u64, false).into(),
|
|
||||||
pointer.to_int(self).into(),
|
|
||||||
self.xlen_type().const_all_ones().into(),
|
|
||||||
storage_value_pointer.to_int(self).into(),
|
|
||||||
storage_value_length_pointer.to_int(self).into(),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
// We do not to check the return value.
|
|
||||||
// Solidity assumes infallible SLOAD.
|
|
||||||
// If a key doesn't exist the "zero" value is returned.
|
|
||||||
|
|
||||||
self.build_load(storage_value_pointer, "storage_value_load")
|
|
||||||
}
|
}
|
||||||
AddressSpace::Stack => {
|
AddressSpace::Stack => {
|
||||||
let value = self
|
let value = self
|
||||||
@@ -852,60 +815,16 @@ where
|
|||||||
{
|
{
|
||||||
match pointer.address_space {
|
match pointer.address_space {
|
||||||
AddressSpace::Heap => {
|
AddressSpace::Heap => {
|
||||||
let heap_pointer = self.build_heap_gep(
|
let declaration =
|
||||||
self.builder().build_ptr_to_int(
|
<PolkaVMStoreHeapWordFunction as RuntimeFunction<D>>::declaration(self);
|
||||||
pointer.value,
|
let arguments = [
|
||||||
self.xlen_type(),
|
pointer.to_int(self).as_basic_value_enum(),
|
||||||
"offset_ptrtoint",
|
value.as_basic_value_enum(),
|
||||||
)?,
|
];
|
||||||
value
|
self.build_call(declaration, &arguments, "heap_store");
|
||||||
.as_basic_value_enum()
|
|
||||||
.get_type()
|
|
||||||
.size_of()
|
|
||||||
.expect("should be IntValue")
|
|
||||||
.const_truncate(self.xlen_type()),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let value = value.as_basic_value_enum();
|
|
||||||
let value = match value.get_type().into_int_type().get_bit_width() as usize {
|
|
||||||
revive_common::BIT_LENGTH_WORD => self.build_byte_swap(value)?,
|
|
||||||
revive_common::BIT_LENGTH_BYTE => value,
|
|
||||||
_ => unreachable!("Only word and byte sized values can be stored on EVM heap"),
|
|
||||||
};
|
|
||||||
|
|
||||||
self.builder
|
|
||||||
.build_store(heap_pointer.value, value)?
|
|
||||||
.set_alignment(revive_common::BYTE_LENGTH_BYTE as u32)
|
|
||||||
.expect("Alignment is valid");
|
|
||||||
}
|
}
|
||||||
AddressSpace::Storage | AddressSpace::TransientStorage => {
|
AddressSpace::Storage | AddressSpace::TransientStorage => {
|
||||||
assert_eq!(
|
unreachable!("should use the runtime function")
|
||||||
value.as_basic_value_enum().get_type(),
|
|
||||||
self.word_type().as_basic_type_enum()
|
|
||||||
);
|
|
||||||
|
|
||||||
let storage_value_pointer = self.build_alloca(self.word_type(), "storage_value");
|
|
||||||
let storage_value_pointer_casted = self.builder().build_ptr_to_int(
|
|
||||||
storage_value_pointer.value,
|
|
||||||
self.xlen_type(),
|
|
||||||
"storage_value_pointer_casted",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
self.builder()
|
|
||||||
.build_store(storage_value_pointer.value, value)?;
|
|
||||||
|
|
||||||
let transient = pointer.address_space == AddressSpace::TransientStorage;
|
|
||||||
|
|
||||||
self.build_runtime_call(
|
|
||||||
revive_runtime_api::polkavm_imports::SET_STORAGE,
|
|
||||||
&[
|
|
||||||
self.xlen_type().const_int(transient as u64, false).into(),
|
|
||||||
pointer.to_int(self).into(),
|
|
||||||
self.xlen_type().const_all_ones().into(),
|
|
||||||
storage_value_pointer_casted.into(),
|
|
||||||
self.integer_const(crate::polkavm::XLEN, 32).into(),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
AddressSpace::Stack => {
|
AddressSpace::Stack => {
|
||||||
let instruction = self.builder.build_store(pointer.value, value).unwrap();
|
let instruction = self.builder.build_store(pointer.value, value).unwrap();
|
||||||
@@ -1120,38 +1039,16 @@ where
|
|||||||
offset: inkwell::values::IntValue<'ctx>,
|
offset: inkwell::values::IntValue<'ctx>,
|
||||||
length: inkwell::values::IntValue<'ctx>,
|
length: inkwell::values::IntValue<'ctx>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let offset_truncated = self.safe_truncate_int_to_xlen(offset)?;
|
self.build_call(
|
||||||
let length_truncated = self.safe_truncate_int_to_xlen(length)?;
|
<Exit as RuntimeFunction<D>>::declaration(self),
|
||||||
let offset_into_heap = self.build_heap_gep(offset_truncated, length_truncated)?;
|
&[flags.into(), offset.into(), length.into()],
|
||||||
|
"exit",
|
||||||
let length_pointer = self.safe_truncate_int_to_xlen(length)?;
|
|
||||||
let offset_pointer = self.builder().build_ptr_to_int(
|
|
||||||
offset_into_heap.value,
|
|
||||||
self.xlen_type(),
|
|
||||||
"return_data_ptr_to_int",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
self.build_runtime_call(
|
|
||||||
revive_runtime_api::polkavm_imports::RETURN,
|
|
||||||
&[flags.into(), offset_pointer.into(), length_pointer.into()],
|
|
||||||
);
|
);
|
||||||
self.build_unreachable();
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Truncate a memory offset to register size, trapping if it doesn't fit.
|
/// Truncate a memory offset to register size, trapping if it doesn't fit.
|
||||||
/// Pointers are represented as opaque 256 bit integer values in EVM.
|
|
||||||
/// In practice, they should never exceed a register sized bit value.
|
|
||||||
/// However, we still protect against this possibility here. Heap index
|
|
||||||
/// offsets are generally untrusted and potentially represent valid
|
|
||||||
/// (but wrong) pointers when truncated.
|
|
||||||
///
|
|
||||||
/// TODO: Splitting up into a dedicated function
|
|
||||||
/// could potentially decrease code sizes (LLVM can still decide to inline).
|
|
||||||
/// However, passing i256 parameters is counter productive and
|
|
||||||
/// I've found that splitting it up actualy increases code size.
|
|
||||||
/// Should be reviewed after 64bit support.
|
|
||||||
pub fn safe_truncate_int_to_xlen(
|
pub fn safe_truncate_int_to_xlen(
|
||||||
&self,
|
&self,
|
||||||
value: inkwell::values::IntValue<'ctx>,
|
value: inkwell::values::IntValue<'ctx>,
|
||||||
@@ -1165,29 +1062,19 @@ where
|
|||||||
"expected XLEN or WORD sized int type for memory offset",
|
"expected XLEN or WORD sized int type for memory offset",
|
||||||
);
|
);
|
||||||
|
|
||||||
let truncated =
|
Ok(self
|
||||||
self.builder()
|
.build_call(
|
||||||
.build_int_truncate(value, self.xlen_type(), "offset_truncated")?;
|
<WordToPointer as RuntimeFunction<D>>::declaration(self),
|
||||||
let extended =
|
&[value.into()],
|
||||||
self.builder()
|
"word_to_pointer",
|
||||||
.build_int_z_extend(truncated, self.word_type(), "offset_extended")?;
|
)
|
||||||
let is_overflow = self.builder().build_int_compare(
|
.unwrap_or_else(|| {
|
||||||
inkwell::IntPredicate::NE,
|
panic!(
|
||||||
value,
|
"revive runtime function {} should return a value",
|
||||||
extended,
|
<WordToPointer as RuntimeFunction<D>>::NAME,
|
||||||
"compare_truncated_extended",
|
)
|
||||||
)?;
|
})
|
||||||
|
.into_int_value())
|
||||||
let block_continue = self.append_basic_block("offset_pointer_ok");
|
|
||||||
let block_trap = self.append_basic_block("offset_pointer_overflow");
|
|
||||||
self.build_conditional_branch(is_overflow, block_trap, block_continue)?;
|
|
||||||
|
|
||||||
self.set_basic_block(block_trap);
|
|
||||||
self.build_call(self.intrinsics().trap, &[], "invalid_trap");
|
|
||||||
self.build_unreachable();
|
|
||||||
|
|
||||||
self.set_basic_block(block_continue);
|
|
||||||
Ok(truncated)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build a call to PolkaVM `sbrk` for extending the heap from offset by `size`.
|
/// Build a call to PolkaVM `sbrk` for extending the heap from offset by `size`.
|
||||||
@@ -1214,45 +1101,17 @@ where
|
|||||||
|
|
||||||
/// Build a call to PolkaVM `msize` for querying the linear memory size.
|
/// Build a call to PolkaVM `msize` for querying the linear memory size.
|
||||||
pub fn build_msize(&self) -> anyhow::Result<inkwell::values::IntValue<'ctx>> {
|
pub fn build_msize(&self) -> anyhow::Result<inkwell::values::IntValue<'ctx>> {
|
||||||
Ok(self
|
let memory_size_pointer = self
|
||||||
.builder()
|
.module()
|
||||||
.build_call(
|
.get_global(revive_runtime_api::polkavm_imports::MEMORY_SIZE)
|
||||||
self.runtime_api_method(revive_runtime_api::polkavm_imports::MEMORY_SIZE),
|
.expect("the memory size symbol should have been declared")
|
||||||
&[],
|
.as_pointer_value();
|
||||||
"call_msize",
|
let memory_size_value = self.builder().build_load(
|
||||||
)?
|
self.xlen_type(),
|
||||||
.try_as_basic_value()
|
memory_size_pointer,
|
||||||
.left()
|
"memory_size_value",
|
||||||
.expect("sbrk returns an int")
|
|
||||||
.into_int_value())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Call PolkaVM `sbrk` for extending the heap by `offset` + `size`,
|
|
||||||
/// trapping the contract if the call failed.
|
|
||||||
pub fn build_heap_alloc(
|
|
||||||
&self,
|
|
||||||
offset: inkwell::values::IntValue<'ctx>,
|
|
||||||
size: inkwell::values::IntValue<'ctx>,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
let end_of_memory = self.build_sbrk(offset, size)?;
|
|
||||||
let return_is_nil = self.builder().build_int_compare(
|
|
||||||
inkwell::IntPredicate::EQ,
|
|
||||||
end_of_memory,
|
|
||||||
self.llvm().ptr_type(Default::default()).const_null(),
|
|
||||||
"compare_end_of_memory_nil",
|
|
||||||
)?;
|
)?;
|
||||||
|
Ok(memory_size_value.into_int_value())
|
||||||
let continue_block = self.append_basic_block("sbrk_not_nil");
|
|
||||||
let trap_block = self.append_basic_block("sbrk_nil");
|
|
||||||
self.build_conditional_branch(return_is_nil, trap_block, continue_block)?;
|
|
||||||
|
|
||||||
self.set_basic_block(trap_block);
|
|
||||||
self.build_call(self.intrinsics().trap, &[], "invalid_trap");
|
|
||||||
self.build_unreachable();
|
|
||||||
|
|
||||||
self.set_basic_block(continue_block);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a pointer to `offset` into the heap, allocating
|
/// Returns a pointer to `offset` into the heap, allocating
|
||||||
@@ -1267,18 +1126,8 @@ where
|
|||||||
assert_eq!(offset.get_type(), self.xlen_type());
|
assert_eq!(offset.get_type(), self.xlen_type());
|
||||||
assert_eq!(length.get_type(), self.xlen_type());
|
assert_eq!(length.get_type(), self.xlen_type());
|
||||||
|
|
||||||
self.build_heap_alloc(offset, length)?;
|
let pointer = self.build_sbrk(offset, length)?;
|
||||||
|
Ok(Pointer::new(self.byte_type(), AddressSpace::Stack, pointer))
|
||||||
let heap_start = self
|
|
||||||
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
|
|
||||||
.value
|
|
||||||
.as_pointer_value();
|
|
||||||
Ok(self.build_gep(
|
|
||||||
Pointer::new(self.byte_type(), AddressSpace::Stack, heap_start),
|
|
||||||
&[offset],
|
|
||||||
self.byte_type(),
|
|
||||||
"heap_offset_via_gep",
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a boolean type constant.
|
/// Returns a boolean type constant.
|
||||||
@@ -1339,11 +1188,17 @@ where
|
|||||||
self.llvm.custom_width_int_type(bit_length as u32)
|
self.llvm.custom_width_int_type(bit_length as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the register witdh sized type.
|
/// Returns the XLEN witdh sized type.
|
||||||
pub fn xlen_type(&self) -> inkwell::types::IntType<'ctx> {
|
pub fn xlen_type(&self) -> inkwell::types::IntType<'ctx> {
|
||||||
self.llvm.custom_width_int_type(crate::polkavm::XLEN as u32)
|
self.llvm.custom_width_int_type(crate::polkavm::XLEN as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the PolkaVM native register width sized type.
|
||||||
|
pub fn register_type(&self) -> inkwell::types::IntType<'ctx> {
|
||||||
|
self.llvm
|
||||||
|
.custom_width_int_type(revive_common::BIT_LENGTH_X64 as u32)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the sentinel pointer value.
|
/// Returns the sentinel pointer value.
|
||||||
pub fn sentinel_pointer(&self) -> Pointer<'ctx> {
|
pub fn sentinel_pointer(&self) -> Pointer<'ctx> {
|
||||||
let sentinel_pointer = self
|
let sentinel_pointer = self
|
||||||
@@ -1568,29 +1423,6 @@ where
|
|||||||
.expect("The Yul data must have been initialized")
|
.expect("The Yul data must have been initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the EVM legacy assembly data.
|
|
||||||
pub fn set_evmla_data(&mut self, data: EVMLAData<'ctx>) {
|
|
||||||
self.evmla_data = Some(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the EVM legacy assembly data reference.
|
|
||||||
/// # Panics
|
|
||||||
/// If the EVM data has not been initialized.
|
|
||||||
pub fn evmla(&self) -> &EVMLAData<'ctx> {
|
|
||||||
self.evmla_data
|
|
||||||
.as_ref()
|
|
||||||
.expect("The EVMLA data must have been initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the EVM legacy assembly data mutable reference.
|
|
||||||
/// # Panics
|
|
||||||
/// If the EVM data has not been initialized.
|
|
||||||
pub fn evmla_mut(&mut self) -> &mut EVMLAData<'ctx> {
|
|
||||||
self.evmla_data
|
|
||||||
.as_mut()
|
|
||||||
.expect("The EVMLA data must have been initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current number of immutables values in the contract.
|
/// Returns the current number of immutables values in the contract.
|
||||||
/// If the size is set manually, then it is returned. Otherwise, the number of elements in
|
/// If the size is set manually, then it is returned. Otherwise, the number of elements in
|
||||||
/// the identifier-to-offset mapping tree is returned.
|
/// the identifier-to-offset mapping tree is returned.
|
||||||
@@ -1601,4 +1433,8 @@ where
|
|||||||
anyhow::bail!("The immutable size data is not available");
|
anyhow::bail!("The immutable size data is not available");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn optimizer_settings(&self) -> &OptimizerSettings {
|
||||||
|
self.optimizer.settings()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,110 @@
|
|||||||
|
//! The revive simulated EVM linear memory pointer functions.
|
||||||
|
|
||||||
|
use inkwell::values::BasicValueEnum;
|
||||||
|
|
||||||
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
||||||
|
use crate::polkavm::context::Context;
|
||||||
|
use crate::polkavm::Dependency;
|
||||||
|
use crate::polkavm::WriteLLVM;
|
||||||
|
|
||||||
|
/// Load a word size value from a heap pointer.
|
||||||
|
pub struct LoadWord;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for LoadWord
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_load_heap_word";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context
|
||||||
|
.word_type()
|
||||||
|
.fn_type(&[context.xlen_type().into()], false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<BasicValueEnum<'ctx>>> {
|
||||||
|
let offset = Self::paramater(context, 0).into_int_value();
|
||||||
|
let length = context
|
||||||
|
.xlen_type()
|
||||||
|
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false);
|
||||||
|
let pointer = context.build_heap_gep(offset, length)?;
|
||||||
|
let value = context
|
||||||
|
.builder()
|
||||||
|
.build_load(context.word_type(), pointer.value, "value")?;
|
||||||
|
context
|
||||||
|
.basic_block()
|
||||||
|
.get_last_instruction()
|
||||||
|
.expect("Always exists")
|
||||||
|
.set_alignment(revive_common::BYTE_LENGTH_BYTE as u32)
|
||||||
|
.expect("Alignment is valid");
|
||||||
|
|
||||||
|
let swapped_value = context.build_byte_swap(value)?;
|
||||||
|
Ok(Some(swapped_value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for LoadWord
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Store a word size value through a heap pointer.
|
||||||
|
pub struct StoreWord;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for StoreWord
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_store_heap_word";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.void_type().fn_type(
|
||||||
|
&[context.xlen_type().into(), context.word_type().into()],
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<BasicValueEnum<'ctx>>> {
|
||||||
|
let offset = Self::paramater(context, 0).into_int_value();
|
||||||
|
let length = context
|
||||||
|
.xlen_type()
|
||||||
|
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false);
|
||||||
|
let pointer = context.build_heap_gep(offset, length)?;
|
||||||
|
|
||||||
|
let value = context.build_byte_swap(Self::paramater(context, 1))?;
|
||||||
|
|
||||||
|
context
|
||||||
|
.builder()
|
||||||
|
.build_store(pointer.value, value)?
|
||||||
|
.set_alignment(revive_common::BYTE_LENGTH_BYTE as u32)
|
||||||
|
.expect("Alignment is valid");
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for StoreWord
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
@@ -7,6 +7,9 @@ use crate::polkavm::context::global::Global;
|
|||||||
use crate::polkavm::context::Context;
|
use crate::polkavm::context::Context;
|
||||||
use crate::polkavm::Dependency;
|
use crate::polkavm::Dependency;
|
||||||
|
|
||||||
|
pub mod heap;
|
||||||
|
pub mod storage;
|
||||||
|
|
||||||
/// The LLVM pointer.
|
/// The LLVM pointer.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Pointer<'ctx> {
|
pub struct Pointer<'ctx> {
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
//! The revive storage pointer functions.
|
||||||
|
|
||||||
|
use inkwell::values::BasicValueEnum;
|
||||||
|
|
||||||
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
||||||
|
use crate::polkavm::context::Context;
|
||||||
|
use crate::polkavm::Dependency;
|
||||||
|
use crate::polkavm::WriteLLVM;
|
||||||
|
|
||||||
|
/// Load a word size value from a storage pointer.
|
||||||
|
pub struct LoadWord;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for LoadWord
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_load_storage_word";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.word_type().fn_type(
|
||||||
|
&[context.xlen_type().into(), context.word_type().into()],
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<BasicValueEnum<'ctx>>> {
|
||||||
|
let is_transient = Self::paramater(context, 0);
|
||||||
|
let key_value = Self::paramater(context, 1);
|
||||||
|
|
||||||
|
let key_pointer = context.build_alloca_at_entry(context.word_type(), "key_pointer");
|
||||||
|
let value_pointer = context.build_alloca_at_entry(context.word_type(), "value_pointer");
|
||||||
|
let length_pointer = context.build_alloca_at_entry(context.xlen_type(), "length_pointer");
|
||||||
|
|
||||||
|
context
|
||||||
|
.builder()
|
||||||
|
.build_store(key_pointer.value, key_value)?;
|
||||||
|
context.build_store(value_pointer, context.word_const(0))?;
|
||||||
|
context.build_store(
|
||||||
|
length_pointer,
|
||||||
|
context
|
||||||
|
.xlen_type()
|
||||||
|
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let arguments = [
|
||||||
|
is_transient,
|
||||||
|
key_pointer.to_int(context).into(),
|
||||||
|
context.xlen_type().const_all_ones().into(),
|
||||||
|
value_pointer.to_int(context).into(),
|
||||||
|
length_pointer.to_int(context).into(),
|
||||||
|
];
|
||||||
|
context.build_runtime_call(revive_runtime_api::polkavm_imports::GET_STORAGE, &arguments);
|
||||||
|
|
||||||
|
// We do not to check the return value: Solidity assumes infallible loads.
|
||||||
|
// If a key doesn't exist the "zero" value is returned (ensured by above write).
|
||||||
|
|
||||||
|
Ok(Some(context.build_load(value_pointer, "storage_value")?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for LoadWord
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Store a word size value through a storage pointer.
|
||||||
|
pub struct StoreWord;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for StoreWord
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_store_storage_word";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.void_type().fn_type(
|
||||||
|
&[
|
||||||
|
context.xlen_type().into(),
|
||||||
|
context.word_type().into(),
|
||||||
|
context.word_type().into(),
|
||||||
|
],
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<BasicValueEnum<'ctx>>> {
|
||||||
|
let is_transient = Self::paramater(context, 0);
|
||||||
|
let key = Self::paramater(context, 1);
|
||||||
|
let value = Self::paramater(context, 2);
|
||||||
|
|
||||||
|
let key_pointer = context.build_alloca_at_entry(context.word_type(), "key_pointer");
|
||||||
|
let value_pointer = context.build_alloca_at_entry(context.word_type(), "value_pointer");
|
||||||
|
|
||||||
|
context.build_store(key_pointer, key)?;
|
||||||
|
context.build_store(value_pointer, value)?;
|
||||||
|
|
||||||
|
let arguments = [
|
||||||
|
is_transient,
|
||||||
|
key_pointer.to_int(context).into(),
|
||||||
|
context.xlen_type().const_all_ones().into(),
|
||||||
|
value_pointer.to_int(context).into(),
|
||||||
|
context.integer_const(crate::polkavm::XLEN, 32).into(),
|
||||||
|
];
|
||||||
|
context.build_runtime_call(revive_runtime_api::polkavm_imports::SET_STORAGE, &arguments);
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for StoreWord
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
//! The revive compiler runtime function interface definition.
|
||||||
|
//!
|
||||||
|
//! Common routines should not be inlined but extracted into smaller functions.
|
||||||
|
//! This benefits contract code size.
|
||||||
|
|
||||||
|
use crate::optimizer::settings::size_level::SizeLevel;
|
||||||
|
use crate::polkavm::context::function::declaration::Declaration;
|
||||||
|
use crate::polkavm::context::function::Function;
|
||||||
|
use crate::polkavm::context::Attribute;
|
||||||
|
use crate::polkavm::context::Context;
|
||||||
|
use crate::polkavm::Dependency;
|
||||||
|
|
||||||
|
/// The revive runtime function interface simplifies declaring runtime functions
|
||||||
|
/// and code emitting by providing helpful default implementations.
|
||||||
|
pub trait RuntimeFunction<D>
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
/// The function name.
|
||||||
|
const NAME: &'static str;
|
||||||
|
|
||||||
|
const ATTRIBUTES: &'static [Attribute] = &[
|
||||||
|
Attribute::NoFree,
|
||||||
|
Attribute::NoRecurse,
|
||||||
|
Attribute::WillReturn,
|
||||||
|
];
|
||||||
|
|
||||||
|
/// The function type.
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx>;
|
||||||
|
|
||||||
|
/// Declare the function.
|
||||||
|
fn declare(&self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
let function = context.add_function(
|
||||||
|
Self::NAME,
|
||||||
|
Self::r#type(context),
|
||||||
|
0,
|
||||||
|
Some(inkwell::module::Linkage::External),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let mut attributes = Self::ATTRIBUTES.to_vec();
|
||||||
|
attributes.extend_from_slice(match context.optimizer_settings().level_middle_end_size {
|
||||||
|
SizeLevel::Zero => &[],
|
||||||
|
_ => &[Attribute::OptimizeForSize, Attribute::MinSize],
|
||||||
|
});
|
||||||
|
Function::set_attributes(
|
||||||
|
context.llvm(),
|
||||||
|
function.borrow().declaration(),
|
||||||
|
&attributes,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the function declaration.
|
||||||
|
fn declaration<'ctx>(context: &Context<'ctx, D>) -> Declaration<'ctx> {
|
||||||
|
context
|
||||||
|
.get_function(Self::NAME)
|
||||||
|
.unwrap_or_else(|| panic!("runtime function {} should be declared", Self::NAME))
|
||||||
|
.borrow()
|
||||||
|
.declaration()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Emit the function.
|
||||||
|
fn emit(&self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
context.set_current_function(Self::NAME, None)?;
|
||||||
|
context.set_basic_block(context.current_function().borrow().entry_block());
|
||||||
|
|
||||||
|
let return_value = self.emit_body(context)?;
|
||||||
|
self.emit_epilogue(context, return_value);
|
||||||
|
|
||||||
|
context.pop_debug_scope();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Emit the function body.
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>>;
|
||||||
|
|
||||||
|
/// Emit the function return instructions.
|
||||||
|
fn emit_epilogue<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
return_value: Option<inkwell::values::BasicValueEnum<'ctx>>,
|
||||||
|
) {
|
||||||
|
let return_block = context.current_function().borrow().return_block();
|
||||||
|
context.build_unconditional_branch(return_block);
|
||||||
|
context.set_basic_block(return_block);
|
||||||
|
match return_value {
|
||||||
|
Some(value) => context.build_return(Some(&value)),
|
||||||
|
None => context.build_return(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the nth function paramater.
|
||||||
|
fn paramater<'ctx>(
|
||||||
|
context: &Context<'ctx, D>,
|
||||||
|
index: usize,
|
||||||
|
) -> inkwell::values::BasicValueEnum<'ctx> {
|
||||||
|
let name = Self::NAME;
|
||||||
|
context
|
||||||
|
.get_function(name)
|
||||||
|
.unwrap_or_else(|| panic!("runtime function {name} should be declared"))
|
||||||
|
.borrow()
|
||||||
|
.declaration()
|
||||||
|
.function_value()
|
||||||
|
.get_nth_param(index as u32)
|
||||||
|
.unwrap_or_else(|| panic!("runtime function {name} should have parameter #{index}"))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,13 @@
|
|||||||
|
|
||||||
use inkwell::values::BasicValue;
|
use inkwell::values::BasicValue;
|
||||||
|
|
||||||
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
||||||
use crate::polkavm::context::Context;
|
use crate::polkavm::context::Context;
|
||||||
use crate::polkavm::Dependency;
|
use crate::polkavm::Dependency;
|
||||||
|
use crate::PolkaVMDivisionFunction;
|
||||||
|
use crate::PolkaVMRemainderFunction;
|
||||||
|
use crate::PolkaVMSignedDivisionFunction;
|
||||||
|
use crate::PolkaVMSignedRemainderFunction;
|
||||||
|
|
||||||
/// Translates the arithmetic addition.
|
/// Translates the arithmetic addition.
|
||||||
pub fn addition<'ctx, D>(
|
pub fn addition<'ctx, D>(
|
||||||
@@ -59,11 +64,11 @@ pub fn division<'ctx, D>(
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
wrapped_division(context, operand_2, || {
|
let name = <PolkaVMDivisionFunction as RuntimeFunction<D>>::NAME;
|
||||||
Ok(context
|
let declaration = <PolkaVMDivisionFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
.builder()
|
Ok(context
|
||||||
.build_int_unsigned_div(operand_1, operand_2, "DIV")?)
|
.build_call(declaration, &[operand_1.into(), operand_2.into()], "div")
|
||||||
})
|
.unwrap_or_else(|| panic!("revive runtime function {name} should return a value",)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translates the arithmetic remainder.
|
/// Translates the arithmetic remainder.
|
||||||
@@ -75,11 +80,11 @@ pub fn remainder<'ctx, D>(
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
wrapped_division(context, operand_2, || {
|
let name = <PolkaVMRemainderFunction as RuntimeFunction<D>>::NAME;
|
||||||
Ok(context
|
let declaration = <PolkaVMRemainderFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
.builder()
|
Ok(context
|
||||||
.build_int_unsigned_rem(operand_1, operand_2, "MOD")?)
|
.build_call(declaration, &[operand_1.into(), operand_2.into()], "rem")
|
||||||
})
|
.unwrap_or_else(|| panic!("revive runtime function {name} should return a value",)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translates the signed arithmetic division.
|
/// Translates the signed arithmetic division.
|
||||||
@@ -94,54 +99,11 @@ pub fn division_signed<'ctx, D>(
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
assert_eq!(
|
let name = <PolkaVMSignedDivisionFunction as RuntimeFunction<D>>::NAME;
|
||||||
operand_2.get_type().get_bit_width(),
|
let declaration = <PolkaVMSignedDivisionFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
revive_common::BIT_LENGTH_WORD as u32
|
Ok(context
|
||||||
);
|
.build_call(declaration, &[operand_1.into(), operand_2.into()], "sdiv")
|
||||||
|
.unwrap_or_else(|| panic!("revive runtime function {name} should return a value",)))
|
||||||
let block_calculate = context.append_basic_block("calculate");
|
|
||||||
let block_overflow = context.append_basic_block("overflow");
|
|
||||||
let block_select = context.append_basic_block("select_result");
|
|
||||||
let block_origin = context.basic_block();
|
|
||||||
context.builder().build_switch(
|
|
||||||
operand_2,
|
|
||||||
block_calculate,
|
|
||||||
&[
|
|
||||||
(context.word_type().const_zero(), block_select),
|
|
||||||
(context.word_type().const_all_ones(), block_overflow),
|
|
||||||
],
|
|
||||||
)?;
|
|
||||||
|
|
||||||
context.set_basic_block(block_calculate);
|
|
||||||
let quotient = context
|
|
||||||
.builder()
|
|
||||||
.build_int_signed_div(operand_1, operand_2, "SDIV")?;
|
|
||||||
context.build_unconditional_branch(block_select);
|
|
||||||
|
|
||||||
context.set_basic_block(block_overflow);
|
|
||||||
let max_uint = context.builder().build_int_z_extend(
|
|
||||||
context
|
|
||||||
.integer_type(revive_common::BIT_LENGTH_WORD - 1)
|
|
||||||
.const_all_ones(),
|
|
||||||
context.word_type(),
|
|
||||||
"max_uint",
|
|
||||||
)?;
|
|
||||||
let is_operand_1_overflow = context.builder().build_int_compare(
|
|
||||||
inkwell::IntPredicate::EQ,
|
|
||||||
operand_1,
|
|
||||||
context.builder().build_int_neg(max_uint, "min_uint")?,
|
|
||||||
"is_operand_1_overflow",
|
|
||||||
)?;
|
|
||||||
context.build_conditional_branch(is_operand_1_overflow, block_select, block_calculate)?;
|
|
||||||
|
|
||||||
context.set_basic_block(block_select);
|
|
||||||
let result = context.builder().build_phi(context.word_type(), "result")?;
|
|
||||||
result.add_incoming(&[
|
|
||||||
(&operand_1, block_overflow),
|
|
||||||
(&context.word_const(0), block_origin),
|
|
||||||
("ient.as_basic_value_enum(), block_calculate),
|
|
||||||
]);
|
|
||||||
Ok(result.as_basic_value())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translates the signed arithmetic remainder.
|
/// Translates the signed arithmetic remainder.
|
||||||
@@ -153,53 +115,9 @@ pub fn remainder_signed<'ctx, D>(
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
wrapped_division(context, operand_2, || {
|
let name = <PolkaVMSignedRemainderFunction as RuntimeFunction<D>>::NAME;
|
||||||
Ok(context
|
let declaration = <PolkaVMSignedRemainderFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
.builder()
|
Ok(context
|
||||||
.build_int_signed_rem(operand_1, operand_2, "SMOD")?)
|
.build_call(declaration, &[operand_1.into(), operand_2.into()], "srem")
|
||||||
})
|
.unwrap_or_else(|| panic!("revive runtime function {name} should return a value",)))
|
||||||
}
|
|
||||||
|
|
||||||
/// Wrap division operations so that zero will be returned if the
|
|
||||||
/// denominator is zero (see also Ethereum YP Appendix H.2).
|
|
||||||
///
|
|
||||||
/// The closure is expected to calculate and return the quotient.
|
|
||||||
///
|
|
||||||
/// The result is either the calculated quotient or zero,
|
|
||||||
/// selected at runtime.
|
|
||||||
fn wrapped_division<'ctx, D, F, T>(
|
|
||||||
context: &Context<'ctx, D>,
|
|
||||||
denominator: inkwell::values::IntValue<'ctx>,
|
|
||||||
f: F,
|
|
||||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
|
||||||
where
|
|
||||||
D: Dependency + Clone,
|
|
||||||
F: FnOnce() -> anyhow::Result<T>,
|
|
||||||
T: inkwell::values::IntMathValue<'ctx>,
|
|
||||||
{
|
|
||||||
assert_eq!(
|
|
||||||
denominator.get_type().get_bit_width(),
|
|
||||||
revive_common::BIT_LENGTH_WORD as u32
|
|
||||||
);
|
|
||||||
|
|
||||||
let block_calculate = context.append_basic_block("calculate");
|
|
||||||
let block_select = context.append_basic_block("select");
|
|
||||||
let block_origin = context.basic_block();
|
|
||||||
context.builder().build_switch(
|
|
||||||
denominator,
|
|
||||||
block_calculate,
|
|
||||||
&[(context.word_const(0), block_select)],
|
|
||||||
)?;
|
|
||||||
|
|
||||||
context.set_basic_block(block_calculate);
|
|
||||||
let calculated_value = f()?.as_basic_value_enum();
|
|
||||||
context.build_unconditional_branch(block_select);
|
|
||||||
|
|
||||||
context.set_basic_block(block_select);
|
|
||||||
let result = context.builder().build_phi(context.word_type(), "result")?;
|
|
||||||
result.add_incoming(&[
|
|
||||||
(&context.word_const(0), block_origin),
|
|
||||||
(&calculated_value, block_calculate),
|
|
||||||
]);
|
|
||||||
Ok(result.as_basic_value())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use crate::polkavm::Dependency;
|
|||||||
|
|
||||||
const STATIC_CALL_FLAG: u32 = 0b0001_0000;
|
const STATIC_CALL_FLAG: u32 = 0b0001_0000;
|
||||||
const REENTRANT_CALL_FLAG: u32 = 0b0000_1000;
|
const REENTRANT_CALL_FLAG: u32 = 0b0000_1000;
|
||||||
|
const SOLIDITY_TRANSFER_GAS_STIPEND_THRESHOLD: u64 = 2300;
|
||||||
|
|
||||||
/// Translates a contract call.
|
/// Translates a contract call.
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
@@ -37,60 +38,67 @@ where
|
|||||||
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?;
|
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?;
|
||||||
let output_length = context.safe_truncate_int_to_xlen(output_length)?;
|
let output_length = context.safe_truncate_int_to_xlen(output_length)?;
|
||||||
|
|
||||||
// TODO: What to supply here? Is there a weight to gas?
|
|
||||||
let _gas = context
|
|
||||||
.builder()
|
|
||||||
.build_int_truncate(gas, context.integer_type(64), "gas")?;
|
|
||||||
|
|
||||||
let input_pointer = context.build_heap_gep(input_offset, input_length)?;
|
let input_pointer = context.build_heap_gep(input_offset, input_length)?;
|
||||||
let output_pointer = context.build_heap_gep(output_offset, output_length)?;
|
let output_pointer = context.build_heap_gep(output_offset, output_length)?;
|
||||||
|
|
||||||
let output_length_pointer = context.build_alloca_at_entry(context.xlen_type(), "output_length");
|
let output_length_pointer = context.build_alloca_at_entry(context.xlen_type(), "output_length");
|
||||||
context.build_store(output_length_pointer, output_length)?;
|
context.build_store(output_length_pointer, output_length)?;
|
||||||
|
|
||||||
let deposit_pointer = context.build_alloca_at_entry(context.word_type(), "deposit_pointer");
|
let (flags, deposit_limit_value) = if static_call {
|
||||||
context.build_store(deposit_pointer, context.word_type().const_all_ones())?;
|
let flags = REENTRANT_CALL_FLAG | STATIC_CALL_FLAG;
|
||||||
|
(
|
||||||
let flags = if static_call {
|
context.xlen_type().const_int(flags as u64, false),
|
||||||
REENTRANT_CALL_FLAG | STATIC_CALL_FLAG
|
context.word_type().const_zero(),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
REENTRANT_CALL_FLAG
|
call_reentrancy_heuristic(context, gas, input_length, output_length)?
|
||||||
};
|
};
|
||||||
let flags = context.xlen_type().const_int(flags as u64, false);
|
|
||||||
|
|
||||||
let argument_type = revive_runtime_api::calling_convention::call(context.llvm());
|
let deposit_pointer = context.build_alloca_at_entry(context.word_type(), "deposit_pointer");
|
||||||
let argument_pointer = context.build_alloca_at_entry(argument_type, "call_arguments");
|
context.build_store(deposit_pointer, deposit_limit_value)?;
|
||||||
let arguments = &[
|
|
||||||
flags.as_basic_value_enum(),
|
let flags_and_callee = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
address_pointer.value.as_basic_value_enum(),
|
|
||||||
context
|
|
||||||
.integer_const(revive_common::BIT_LENGTH_X64, u64::MAX)
|
|
||||||
.as_basic_value_enum(),
|
|
||||||
context
|
|
||||||
.integer_const(revive_common::BIT_LENGTH_X64, u64::MAX)
|
|
||||||
.as_basic_value_enum(),
|
|
||||||
deposit_pointer.value.as_basic_value_enum(),
|
|
||||||
value_pointer.value.as_basic_value_enum(),
|
|
||||||
input_pointer.value.as_basic_value_enum(),
|
|
||||||
input_length.as_basic_value_enum(),
|
|
||||||
output_pointer.value.as_basic_value_enum(),
|
|
||||||
output_length_pointer.value.as_basic_value_enum(),
|
|
||||||
];
|
|
||||||
revive_runtime_api::calling_convention::spill(
|
|
||||||
context.builder(),
|
context.builder(),
|
||||||
argument_pointer.value,
|
context.llvm(),
|
||||||
argument_type,
|
flags,
|
||||||
arguments,
|
address_pointer.to_int(context),
|
||||||
|
"address_and_callee",
|
||||||
|
)?;
|
||||||
|
let deposit_and_value = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
|
context.builder(),
|
||||||
|
context.llvm(),
|
||||||
|
deposit_pointer.to_int(context),
|
||||||
|
value_pointer.to_int(context),
|
||||||
|
"deposit_and_value",
|
||||||
|
)?;
|
||||||
|
let input_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
|
context.builder(),
|
||||||
|
context.llvm(),
|
||||||
|
input_length,
|
||||||
|
input_pointer.to_int(context),
|
||||||
|
"input_data",
|
||||||
|
)?;
|
||||||
|
let output_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
|
context.builder(),
|
||||||
|
context.llvm(),
|
||||||
|
output_length_pointer.to_int(context),
|
||||||
|
output_pointer.to_int(context),
|
||||||
|
"output_data",
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let name = revive_runtime_api::polkavm_imports::CALL;
|
let name = revive_runtime_api::polkavm_imports::CALL;
|
||||||
let argument_pointer = context.builder().build_ptr_to_int(
|
|
||||||
argument_pointer.value,
|
|
||||||
context.xlen_type(),
|
|
||||||
"call_argument_pointer",
|
|
||||||
)?;
|
|
||||||
let success = context
|
let success = context
|
||||||
.build_runtime_call(name, &[argument_pointer.into()])
|
.build_runtime_call(
|
||||||
|
name,
|
||||||
|
&[
|
||||||
|
flags_and_callee.into(),
|
||||||
|
context.register_type().const_all_ones().into(),
|
||||||
|
context.register_type().const_all_ones().into(),
|
||||||
|
deposit_and_value.into(),
|
||||||
|
input_data.into(),
|
||||||
|
output_data.into(),
|
||||||
|
],
|
||||||
|
)
|
||||||
.unwrap_or_else(|| panic!("{name} should return a value"))
|
.unwrap_or_else(|| panic!("{name} should return a value"))
|
||||||
.into_int_value();
|
.into_int_value();
|
||||||
|
|
||||||
@@ -110,7 +118,7 @@ where
|
|||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn delegate_call<'ctx, D>(
|
pub fn delegate_call<'ctx, D>(
|
||||||
context: &mut Context<'ctx, D>,
|
context: &mut Context<'ctx, D>,
|
||||||
gas: inkwell::values::IntValue<'ctx>,
|
_gas: inkwell::values::IntValue<'ctx>,
|
||||||
address: inkwell::values::IntValue<'ctx>,
|
address: inkwell::values::IntValue<'ctx>,
|
||||||
input_offset: inkwell::values::IntValue<'ctx>,
|
input_offset: inkwell::values::IntValue<'ctx>,
|
||||||
input_length: inkwell::values::IntValue<'ctx>,
|
input_length: inkwell::values::IntValue<'ctx>,
|
||||||
@@ -128,11 +136,6 @@ where
|
|||||||
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?;
|
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?;
|
||||||
let output_length = context.safe_truncate_int_to_xlen(output_length)?;
|
let output_length = context.safe_truncate_int_to_xlen(output_length)?;
|
||||||
|
|
||||||
// TODO: What to supply here? Is there a weight to gas?
|
|
||||||
let _gas = context
|
|
||||||
.builder()
|
|
||||||
.build_int_truncate(gas, context.integer_type(64), "gas")?;
|
|
||||||
|
|
||||||
let input_pointer = context.build_heap_gep(input_offset, input_length)?;
|
let input_pointer = context.build_heap_gep(input_offset, input_length)?;
|
||||||
let output_pointer = context.build_heap_gep(output_offset, output_length)?;
|
let output_pointer = context.build_heap_gep(output_offset, output_length)?;
|
||||||
|
|
||||||
@@ -144,38 +147,41 @@ where
|
|||||||
|
|
||||||
let flags = context.xlen_type().const_int(0u64, false);
|
let flags = context.xlen_type().const_int(0u64, false);
|
||||||
|
|
||||||
let argument_type = revive_runtime_api::calling_convention::delegate_call(context.llvm());
|
let flags_and_callee = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
let argument_pointer = context.build_alloca_at_entry(argument_type, "delegate_call_arguments");
|
|
||||||
let arguments = &[
|
|
||||||
flags.as_basic_value_enum(),
|
|
||||||
address_pointer.value.as_basic_value_enum(),
|
|
||||||
context
|
|
||||||
.integer_const(revive_common::BIT_LENGTH_X64, u64::MAX)
|
|
||||||
.as_basic_value_enum(),
|
|
||||||
context
|
|
||||||
.integer_const(revive_common::BIT_LENGTH_X64, u64::MAX)
|
|
||||||
.as_basic_value_enum(),
|
|
||||||
deposit_pointer.value.as_basic_value_enum(),
|
|
||||||
input_pointer.value.as_basic_value_enum(),
|
|
||||||
input_length.as_basic_value_enum(),
|
|
||||||
output_pointer.value.as_basic_value_enum(),
|
|
||||||
output_length_pointer.value.as_basic_value_enum(),
|
|
||||||
];
|
|
||||||
revive_runtime_api::calling_convention::spill(
|
|
||||||
context.builder(),
|
context.builder(),
|
||||||
argument_pointer.value,
|
context.llvm(),
|
||||||
argument_type,
|
flags,
|
||||||
arguments,
|
address_pointer.to_int(context),
|
||||||
|
"address_and_callee",
|
||||||
|
)?;
|
||||||
|
let input_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
|
context.builder(),
|
||||||
|
context.llvm(),
|
||||||
|
input_length,
|
||||||
|
input_pointer.to_int(context),
|
||||||
|
"input_data",
|
||||||
|
)?;
|
||||||
|
let output_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
|
context.builder(),
|
||||||
|
context.llvm(),
|
||||||
|
output_length_pointer.to_int(context),
|
||||||
|
output_pointer.to_int(context),
|
||||||
|
"output_data",
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let name = revive_runtime_api::polkavm_imports::DELEGATE_CALL;
|
let name = revive_runtime_api::polkavm_imports::DELEGATE_CALL;
|
||||||
let argument_pointer = context.builder().build_ptr_to_int(
|
|
||||||
argument_pointer.value,
|
|
||||||
context.xlen_type(),
|
|
||||||
"delegate_call_argument_pointer",
|
|
||||||
)?;
|
|
||||||
let success = context
|
let success = context
|
||||||
.build_runtime_call(name, &[argument_pointer.into()])
|
.build_runtime_call(
|
||||||
|
name,
|
||||||
|
&[
|
||||||
|
flags_and_callee.into(),
|
||||||
|
context.register_type().const_all_ones().into(),
|
||||||
|
context.register_type().const_all_ones().into(),
|
||||||
|
deposit_pointer.to_int(context).into(),
|
||||||
|
input_data.into(),
|
||||||
|
output_data.into(),
|
||||||
|
],
|
||||||
|
)
|
||||||
.unwrap_or_else(|| panic!("{name} should return a value"))
|
.unwrap_or_else(|| panic!("{name} should return a value"))
|
||||||
.into_int_value();
|
.into_int_value();
|
||||||
|
|
||||||
@@ -209,3 +215,86 @@ where
|
|||||||
.resolve_library(path.as_str())?
|
.resolve_library(path.as_str())?
|
||||||
.as_basic_value_enum())
|
.as_basic_value_enum())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The Solidity `address.transfer` and `address.send` call detection heuristic.
|
||||||
|
///
|
||||||
|
/// # Why
|
||||||
|
/// This heuristic is an additional security feature to guard against re-entrancy attacks
|
||||||
|
/// in case contract authors violate Solidity best practices and use `address.transfer` or
|
||||||
|
/// `address.send`.
|
||||||
|
/// While contract authors are supposed to never use `address.transfer` or `address.send`,
|
||||||
|
/// for a small cost we can be extra defensive about it.
|
||||||
|
///
|
||||||
|
/// # How
|
||||||
|
/// The gas stipend emitted by solc for `transfer` and `send` is not static, thus:
|
||||||
|
/// - Dynamically allow re-entrancy only for calls considered not transfer or send.
|
||||||
|
/// - Detected balance transfers will supply 0 deposit limit instead of `u256::MAX`.
|
||||||
|
///
|
||||||
|
/// Calls are considered transfer or send if:
|
||||||
|
/// - (Input length | Output lenght) == 0;
|
||||||
|
/// - Gas <= 2300;
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
/// The call flags xlen `IntValue` and the deposit limit word `IntValue`.
|
||||||
|
fn call_reentrancy_heuristic<'ctx, D>(
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
gas: inkwell::values::IntValue<'ctx>,
|
||||||
|
input_length: inkwell::values::IntValue<'ctx>,
|
||||||
|
output_length: inkwell::values::IntValue<'ctx>,
|
||||||
|
) -> anyhow::Result<(
|
||||||
|
inkwell::values::IntValue<'ctx>,
|
||||||
|
inkwell::values::IntValue<'ctx>,
|
||||||
|
)>
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
// Branch-free SSA implementation: First derive the heuristic boolean (int1) value.
|
||||||
|
let input_length_or_output_length =
|
||||||
|
context
|
||||||
|
.builder()
|
||||||
|
.build_or(input_length, output_length, "input_length_or_output_length")?;
|
||||||
|
let is_no_input_no_output = context.builder().build_int_compare(
|
||||||
|
inkwell::IntPredicate::EQ,
|
||||||
|
context.xlen_type().const_zero(),
|
||||||
|
input_length_or_output_length,
|
||||||
|
"is_no_input_no_output",
|
||||||
|
)?;
|
||||||
|
let gas_stipend = context
|
||||||
|
.word_type()
|
||||||
|
.const_int(SOLIDITY_TRANSFER_GAS_STIPEND_THRESHOLD, false);
|
||||||
|
let is_gas_stipend_for_transfer_or_send = context.builder().build_int_compare(
|
||||||
|
inkwell::IntPredicate::ULE,
|
||||||
|
gas,
|
||||||
|
gas_stipend,
|
||||||
|
"is_gas_stipend_for_transfer_or_send",
|
||||||
|
)?;
|
||||||
|
let is_balance_transfer = context.builder().build_and(
|
||||||
|
is_no_input_no_output,
|
||||||
|
is_gas_stipend_for_transfer_or_send,
|
||||||
|
"is_balance_transfer",
|
||||||
|
)?;
|
||||||
|
let is_regular_call = context
|
||||||
|
.builder()
|
||||||
|
.build_not(is_balance_transfer, "is_balance_transfer_inverted")?;
|
||||||
|
|
||||||
|
// Call flag: Left shift the heuristic boolean value.
|
||||||
|
let is_regular_call_xlen = context.builder().build_int_z_extend(
|
||||||
|
is_regular_call,
|
||||||
|
context.xlen_type(),
|
||||||
|
"is_balance_transfer_xlen",
|
||||||
|
)?;
|
||||||
|
let call_flags = context.builder().build_left_shift(
|
||||||
|
is_regular_call_xlen,
|
||||||
|
context.xlen_type().const_int(3, false),
|
||||||
|
"flags",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// Deposit limit value: Sign-extended the heuristic boolean value.
|
||||||
|
let deposit_limit_value = context.builder().build_int_s_extend(
|
||||||
|
is_regular_call,
|
||||||
|
context.word_type(),
|
||||||
|
"deposit_limit_value",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok((call_flags, deposit_limit_value))
|
||||||
|
}
|
||||||
|
|||||||
@@ -122,12 +122,20 @@ where
|
|||||||
|
|
||||||
/// Translates the `coinbase` instruction.
|
/// Translates the `coinbase` instruction.
|
||||||
pub fn coinbase<'ctx, D>(
|
pub fn coinbase<'ctx, D>(
|
||||||
_context: &mut Context<'ctx, D>,
|
context: &mut Context<'ctx, D>,
|
||||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
todo!()
|
let pointer = context.build_alloca_at_entry(
|
||||||
|
context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS),
|
||||||
|
"coinbase_output",
|
||||||
|
);
|
||||||
|
context.build_runtime_call(
|
||||||
|
revive_runtime_api::polkavm_imports::BLOCK_AUTHOR,
|
||||||
|
&[pointer.to_int(context).into()],
|
||||||
|
);
|
||||||
|
context.build_load_address(pointer)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translates the `basefee` instruction.
|
/// Translates the `basefee` instruction.
|
||||||
|
|||||||
@@ -26,15 +26,6 @@ where
|
|||||||
|
|
||||||
let code_hash_pointer = context.build_heap_gep(input_offset, input_length)?;
|
let code_hash_pointer = context.build_heap_gep(input_offset, input_length)?;
|
||||||
|
|
||||||
let input_data_pointer = context.build_gep(
|
|
||||||
code_hash_pointer,
|
|
||||||
&[context
|
|
||||||
.xlen_type()
|
|
||||||
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false)],
|
|
||||||
context.byte_type(),
|
|
||||||
"input_ptr_parameter_offset",
|
|
||||||
);
|
|
||||||
|
|
||||||
let value_pointer = context.build_alloca_at_entry(context.value_type(), "transferred_value");
|
let value_pointer = context.build_alloca_at_entry(context.value_type(), "transferred_value");
|
||||||
context.build_store(value_pointer, value)?;
|
context.build_store(value_pointer, value)?;
|
||||||
|
|
||||||
@@ -56,40 +47,38 @@ where
|
|||||||
let deposit_pointer = context.build_alloca_at_entry(context.word_type(), "deposit_pointer");
|
let deposit_pointer = context.build_alloca_at_entry(context.word_type(), "deposit_pointer");
|
||||||
context.build_store(deposit_pointer, context.word_type().const_all_ones())?;
|
context.build_store(deposit_pointer, context.word_type().const_all_ones())?;
|
||||||
|
|
||||||
let argument_type = revive_runtime_api::calling_convention::instantiate(context.llvm());
|
let deposit_and_value = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
let argument_pointer = context.build_alloca_at_entry(argument_type, "instantiate_arguments");
|
|
||||||
let arguments = &[
|
|
||||||
code_hash_pointer.value.as_basic_value_enum(),
|
|
||||||
context
|
|
||||||
.integer_const(revive_common::BIT_LENGTH_X64, u64::MAX)
|
|
||||||
.as_basic_value_enum(),
|
|
||||||
context
|
|
||||||
.integer_const(revive_common::BIT_LENGTH_X64, u64::MAX)
|
|
||||||
.as_basic_value_enum(),
|
|
||||||
deposit_pointer.value.as_basic_value_enum(),
|
|
||||||
value_pointer.value.as_basic_value_enum(),
|
|
||||||
input_data_pointer.value.as_basic_value_enum(),
|
|
||||||
input_length.as_basic_value_enum(),
|
|
||||||
address_pointer.value.as_basic_value_enum(),
|
|
||||||
context.sentinel_pointer().value.as_basic_value_enum(),
|
|
||||||
context.sentinel_pointer().value.as_basic_value_enum(),
|
|
||||||
salt_pointer.value.as_basic_value_enum(),
|
|
||||||
];
|
|
||||||
revive_runtime_api::calling_convention::spill(
|
|
||||||
context.builder(),
|
context.builder(),
|
||||||
argument_pointer.value,
|
context.llvm(),
|
||||||
argument_type,
|
deposit_pointer.to_int(context),
|
||||||
arguments,
|
value_pointer.to_int(context),
|
||||||
|
"deposit_and_value",
|
||||||
|
)?;
|
||||||
|
let input_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
|
context.builder(),
|
||||||
|
context.llvm(),
|
||||||
|
input_length,
|
||||||
|
code_hash_pointer.to_int(context),
|
||||||
|
"input_data",
|
||||||
|
)?;
|
||||||
|
let address_and_salt = revive_runtime_api::calling_convention::pack_hi_lo_reg(
|
||||||
|
context.builder(),
|
||||||
|
context.llvm(),
|
||||||
|
address_pointer.to_int(context),
|
||||||
|
salt_pointer.to_int(context),
|
||||||
|
"output_data",
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let argument_pointer = context.builder().build_ptr_to_int(
|
|
||||||
argument_pointer.value,
|
|
||||||
context.xlen_type(),
|
|
||||||
"instantiate_argument_pointer",
|
|
||||||
)?;
|
|
||||||
context.build_runtime_call(
|
context.build_runtime_call(
|
||||||
revive_runtime_api::polkavm_imports::INSTANTIATE,
|
revive_runtime_api::polkavm_imports::INSTANTIATE,
|
||||||
&[argument_pointer.into()],
|
&[
|
||||||
|
context.register_type().const_all_ones().into(),
|
||||||
|
context.register_type().const_all_ones().into(),
|
||||||
|
deposit_and_value.into(),
|
||||||
|
input_data.into(),
|
||||||
|
context.register_type().const_all_ones().into(),
|
||||||
|
address_and_salt.into(),
|
||||||
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
let address = context.build_byte_swap(context.build_load(address_pointer, "address")?)?;
|
let address = context.build_byte_swap(context.build_load(address_pointer, "address")?)?;
|
||||||
|
|||||||
@@ -2,84 +2,183 @@
|
|||||||
|
|
||||||
use inkwell::values::BasicValue;
|
use inkwell::values::BasicValue;
|
||||||
|
|
||||||
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
||||||
use crate::polkavm::context::Context;
|
use crate::polkavm::context::Context;
|
||||||
use crate::polkavm::Dependency;
|
use crate::polkavm::Dependency;
|
||||||
|
use crate::polkavm::WriteLLVM;
|
||||||
|
|
||||||
|
/// A function for emitting EVM event logs from contract code.
|
||||||
|
pub struct EventLog<const N: usize>;
|
||||||
|
|
||||||
|
impl<D, const N: usize> RuntimeFunction<D> for EventLog<N>
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = match N {
|
||||||
|
0 => "__revive_log_0",
|
||||||
|
1 => "__revive_log_1",
|
||||||
|
2 => "__revive_log_2",
|
||||||
|
3 => "__revive_log_3",
|
||||||
|
4 => "__revive_log_4",
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
let mut parameter_types = vec![context.xlen_type().into(), context.xlen_type().into()];
|
||||||
|
parameter_types.extend_from_slice(&[context.word_type().into(); N]);
|
||||||
|
context.void_type().fn_type(¶meter_types, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
|
||||||
|
let input_offset = Self::paramater(context, 0).into_int_value();
|
||||||
|
let input_length = Self::paramater(context, 1).into_int_value();
|
||||||
|
let input_pointer = context.builder().build_ptr_to_int(
|
||||||
|
context.build_heap_gep(input_offset, input_length)?.value,
|
||||||
|
context.xlen_type(),
|
||||||
|
"event_input_offset",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let arguments = if N == 0 {
|
||||||
|
[
|
||||||
|
context.xlen_type().const_zero().as_basic_value_enum(),
|
||||||
|
context.xlen_type().const_zero().as_basic_value_enum(),
|
||||||
|
input_pointer.as_basic_value_enum(),
|
||||||
|
input_length.as_basic_value_enum(),
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
let topics_buffer_size = N * revive_common::BYTE_LENGTH_WORD;
|
||||||
|
let topics_buffer_pointer = context.build_alloca_at_entry(
|
||||||
|
context.byte_type().array_type(topics_buffer_size as u32),
|
||||||
|
"topics_buffer",
|
||||||
|
);
|
||||||
|
|
||||||
|
for n in 0..N {
|
||||||
|
let topic = Self::paramater(context, n + 2);
|
||||||
|
let topic_buffer_offset = context
|
||||||
|
.xlen_type()
|
||||||
|
.const_int((n * revive_common::BYTE_LENGTH_WORD) as u64, false);
|
||||||
|
context.build_store(
|
||||||
|
context.build_gep(
|
||||||
|
topics_buffer_pointer,
|
||||||
|
&[context.xlen_type().const_zero(), topic_buffer_offset],
|
||||||
|
context.byte_type(),
|
||||||
|
&format!("topic_buffer_{N}_gep"),
|
||||||
|
),
|
||||||
|
context.build_byte_swap(topic.as_basic_value_enum())?,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
[
|
||||||
|
context
|
||||||
|
.builder()
|
||||||
|
.build_ptr_to_int(
|
||||||
|
topics_buffer_pointer.value,
|
||||||
|
context.xlen_type(),
|
||||||
|
"event_topics_offset",
|
||||||
|
)?
|
||||||
|
.as_basic_value_enum(),
|
||||||
|
context
|
||||||
|
.xlen_type()
|
||||||
|
.const_int(N as u64, false)
|
||||||
|
.as_basic_value_enum(),
|
||||||
|
input_pointer.as_basic_value_enum(),
|
||||||
|
input_length.as_basic_value_enum(),
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
context.build_runtime_call(
|
||||||
|
revive_runtime_api::polkavm_imports::DEPOSIT_EVENT,
|
||||||
|
&arguments,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for EventLog<0>
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for EventLog<1>
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for EventLog<2>
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for EventLog<3>
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for EventLog<4>
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Translates a log or event call.
|
/// Translates a log or event call.
|
||||||
///
|
pub fn log<'ctx, D, const N: usize>(
|
||||||
/// TODO: Splitting up into dedicated functions (log0..log4)
|
|
||||||
/// could potentially decrease code sizes (LLVM can still decide to inline).
|
|
||||||
/// However, passing i256 parameters is counter productive and
|
|
||||||
/// I've found that splitting it up actualy increases code size.
|
|
||||||
/// Should be reviewed after 64bit support.
|
|
||||||
pub fn log<'ctx, D>(
|
|
||||||
context: &mut Context<'ctx, D>,
|
context: &mut Context<'ctx, D>,
|
||||||
input_offset: inkwell::values::IntValue<'ctx>,
|
input_offset: inkwell::values::IntValue<'ctx>,
|
||||||
input_length: inkwell::values::IntValue<'ctx>,
|
input_length: inkwell::values::IntValue<'ctx>,
|
||||||
topics: Vec<inkwell::values::IntValue<'ctx>>,
|
topics: [inkwell::values::BasicValueEnum<'ctx>; N],
|
||||||
) -> anyhow::Result<()>
|
) -> anyhow::Result<()>
|
||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
let input_offset = context.safe_truncate_int_to_xlen(input_offset)?;
|
let declaration = <EventLog<N> as RuntimeFunction<D>>::declaration(context);
|
||||||
let input_length = context.safe_truncate_int_to_xlen(input_length)?;
|
let mut arguments = vec![
|
||||||
let input_pointer = context.builder().build_ptr_to_int(
|
context.safe_truncate_int_to_xlen(input_offset)?.into(),
|
||||||
context.build_heap_gep(input_offset, input_length)?.value,
|
context.safe_truncate_int_to_xlen(input_length)?.into(),
|
||||||
context.xlen_type(),
|
];
|
||||||
"event_input_offset",
|
arguments.extend_from_slice(&topics);
|
||||||
)?;
|
context.build_call(declaration, &arguments, "log");
|
||||||
|
|
||||||
let arguments = if topics.is_empty() {
|
|
||||||
[
|
|
||||||
context.xlen_type().const_zero().as_basic_value_enum(),
|
|
||||||
context.xlen_type().const_zero().as_basic_value_enum(),
|
|
||||||
input_pointer.as_basic_value_enum(),
|
|
||||||
input_length.as_basic_value_enum(),
|
|
||||||
]
|
|
||||||
} else {
|
|
||||||
let topics_buffer_size = topics.len() * revive_common::BYTE_LENGTH_WORD;
|
|
||||||
let topics_buffer_pointer = context.build_alloca(
|
|
||||||
context.byte_type().array_type(topics_buffer_size as u32),
|
|
||||||
"topics_buffer",
|
|
||||||
);
|
|
||||||
|
|
||||||
for (n, topic) in topics.iter().enumerate() {
|
|
||||||
let topic_buffer_offset = context
|
|
||||||
.xlen_type()
|
|
||||||
.const_int((n * revive_common::BYTE_LENGTH_WORD) as u64, false);
|
|
||||||
context.build_store(
|
|
||||||
context.build_gep(
|
|
||||||
topics_buffer_pointer,
|
|
||||||
&[context.xlen_type().const_zero(), topic_buffer_offset],
|
|
||||||
context.byte_type(),
|
|
||||||
"topic_buffer_gep",
|
|
||||||
),
|
|
||||||
context.build_byte_swap(topic.as_basic_value_enum())?,
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
[
|
|
||||||
context
|
|
||||||
.builder()
|
|
||||||
.build_ptr_to_int(
|
|
||||||
topics_buffer_pointer.value,
|
|
||||||
context.xlen_type(),
|
|
||||||
"event_topics_offset",
|
|
||||||
)?
|
|
||||||
.as_basic_value_enum(),
|
|
||||||
context
|
|
||||||
.xlen_type()
|
|
||||||
.const_int(topics.len() as u64, false)
|
|
||||||
.as_basic_value_enum(),
|
|
||||||
input_pointer.as_basic_value_enum(),
|
|
||||||
input_length.as_basic_value_enum(),
|
|
||||||
]
|
|
||||||
};
|
|
||||||
|
|
||||||
let _ = context.build_runtime_call(
|
|
||||||
revive_runtime_api::polkavm_imports::DEPOSIT_EVENT,
|
|
||||||
&arguments,
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,206 @@ use inkwell::types::BasicType;
|
|||||||
|
|
||||||
use crate::polkavm::context::address_space::AddressSpace;
|
use crate::polkavm::context::address_space::AddressSpace;
|
||||||
use crate::polkavm::context::code_type::CodeType;
|
use crate::polkavm::context::code_type::CodeType;
|
||||||
use crate::polkavm::context::function::runtime;
|
|
||||||
use crate::polkavm::context::pointer::Pointer;
|
use crate::polkavm::context::pointer::Pointer;
|
||||||
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
||||||
use crate::polkavm::context::Context;
|
use crate::polkavm::context::Context;
|
||||||
use crate::polkavm::Dependency;
|
use crate::polkavm::Dependency;
|
||||||
|
use crate::polkavm::WriteLLVM;
|
||||||
|
|
||||||
|
/// A function for requesting the immutable data from the runtime.
|
||||||
|
/// This is a special function that is only used by the front-end generated code.
|
||||||
|
///
|
||||||
|
/// The runtime API is called lazily and subsequent calls are no-ops.
|
||||||
|
///
|
||||||
|
/// The bytes written is asserted to match the expected length.
|
||||||
|
/// This should never fail; the length is known.
|
||||||
|
/// However, this is a one time assertion, hence worth it.
|
||||||
|
pub struct Load;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for Load
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_load_immutable_data";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.void_type().fn_type(Default::default(), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
|
||||||
|
let immutable_data_size_pointer = context
|
||||||
|
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE)?
|
||||||
|
.value
|
||||||
|
.as_pointer_value();
|
||||||
|
let immutable_data_size = context.build_load(
|
||||||
|
Pointer::new(
|
||||||
|
context.xlen_type(),
|
||||||
|
AddressSpace::Stack,
|
||||||
|
immutable_data_size_pointer,
|
||||||
|
),
|
||||||
|
"immutable_data_size_load",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let load_immutable_data_block = context.append_basic_block("load_immutables_block");
|
||||||
|
let return_block = context.current_function().borrow().return_block();
|
||||||
|
let immutable_data_size_is_zero = context.builder().build_int_compare(
|
||||||
|
inkwell::IntPredicate::EQ,
|
||||||
|
context.xlen_type().const_zero(),
|
||||||
|
immutable_data_size.into_int_value(),
|
||||||
|
"immutable_data_size_is_zero",
|
||||||
|
)?;
|
||||||
|
context.build_conditional_branch(
|
||||||
|
immutable_data_size_is_zero,
|
||||||
|
return_block,
|
||||||
|
load_immutable_data_block,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
context.set_basic_block(load_immutable_data_block);
|
||||||
|
let output_pointer = context
|
||||||
|
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
|
||||||
|
.value
|
||||||
|
.as_pointer_value();
|
||||||
|
context.build_runtime_call(
|
||||||
|
revive_runtime_api::polkavm_imports::GET_IMMUTABLE_DATA,
|
||||||
|
&[
|
||||||
|
context
|
||||||
|
.builder()
|
||||||
|
.build_ptr_to_int(output_pointer, context.xlen_type(), "ptr_to_xlen")?
|
||||||
|
.into(),
|
||||||
|
context
|
||||||
|
.builder()
|
||||||
|
.build_ptr_to_int(
|
||||||
|
immutable_data_size_pointer,
|
||||||
|
context.xlen_type(),
|
||||||
|
"ptr_to_xlen",
|
||||||
|
)?
|
||||||
|
.into(),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
let bytes_written = context.builder().build_load(
|
||||||
|
context.xlen_type(),
|
||||||
|
immutable_data_size_pointer,
|
||||||
|
"bytes_written",
|
||||||
|
)?;
|
||||||
|
context.builder().build_store(
|
||||||
|
immutable_data_size_pointer,
|
||||||
|
context.xlen_type().const_zero(),
|
||||||
|
)?;
|
||||||
|
let overflow_block = context.append_basic_block("immutable_data_overflow");
|
||||||
|
let is_overflow = context.builder().build_int_compare(
|
||||||
|
inkwell::IntPredicate::UGT,
|
||||||
|
immutable_data_size.into_int_value(),
|
||||||
|
bytes_written.into_int_value(),
|
||||||
|
"is_overflow",
|
||||||
|
)?;
|
||||||
|
context.build_conditional_branch(is_overflow, overflow_block, return_block)?;
|
||||||
|
|
||||||
|
context.set_basic_block(overflow_block);
|
||||||
|
context.build_call(context.intrinsics().trap, &[], "invalid_trap");
|
||||||
|
context.build_unreachable();
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for Load
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Store the immutable data from the constructor code.
|
||||||
|
pub struct Store;
|
||||||
|
|
||||||
|
impl<D> RuntimeFunction<D> for Store
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
const NAME: &'static str = "__revive_store_immutable_data";
|
||||||
|
|
||||||
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
||||||
|
context.void_type().fn_type(Default::default(), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit_body<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &mut Context<'ctx, D>,
|
||||||
|
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
|
||||||
|
let immutable_data_size_pointer = context
|
||||||
|
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE)?
|
||||||
|
.value
|
||||||
|
.as_pointer_value();
|
||||||
|
let immutable_data_size = context.build_load(
|
||||||
|
Pointer::new(
|
||||||
|
context.xlen_type(),
|
||||||
|
AddressSpace::Stack,
|
||||||
|
immutable_data_size_pointer,
|
||||||
|
),
|
||||||
|
"immutable_data_size_load",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let write_immutable_data_block = context.append_basic_block("write_immutables_block");
|
||||||
|
let join_return_block = context.append_basic_block("join_return_block");
|
||||||
|
let immutable_data_size_is_zero = context.builder().build_int_compare(
|
||||||
|
inkwell::IntPredicate::EQ,
|
||||||
|
context.xlen_type().const_zero(),
|
||||||
|
immutable_data_size.into_int_value(),
|
||||||
|
"immutable_data_size_is_zero",
|
||||||
|
)?;
|
||||||
|
context.build_conditional_branch(
|
||||||
|
immutable_data_size_is_zero,
|
||||||
|
join_return_block,
|
||||||
|
write_immutable_data_block,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
context.set_basic_block(write_immutable_data_block);
|
||||||
|
let immutable_data_pointer = context
|
||||||
|
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
|
||||||
|
.value
|
||||||
|
.as_pointer_value();
|
||||||
|
context.build_runtime_call(
|
||||||
|
revive_runtime_api::polkavm_imports::SET_IMMUTABLE_DATA,
|
||||||
|
&[
|
||||||
|
context
|
||||||
|
.builder()
|
||||||
|
.build_ptr_to_int(
|
||||||
|
immutable_data_pointer,
|
||||||
|
context.xlen_type(),
|
||||||
|
"immutable_data_pointer_to_xlen",
|
||||||
|
)?
|
||||||
|
.into(),
|
||||||
|
immutable_data_size,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
context.build_unconditional_branch(join_return_block);
|
||||||
|
|
||||||
|
context.set_basic_block(join_return_block);
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> WriteLLVM<D> for Store
|
||||||
|
where
|
||||||
|
D: Dependency + Clone,
|
||||||
|
{
|
||||||
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||||
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Translates the contract immutable load.
|
/// Translates the contract immutable load.
|
||||||
///
|
///
|
||||||
@@ -27,14 +223,15 @@ where
|
|||||||
}
|
}
|
||||||
Some(CodeType::Deploy) => load_from_memory(context, index),
|
Some(CodeType::Deploy) => load_from_memory(context, index),
|
||||||
Some(CodeType::Runtime) => {
|
Some(CodeType::Runtime) => {
|
||||||
|
let name = <Load as RuntimeFunction<D>>::NAME;
|
||||||
context.build_call(
|
context.build_call(
|
||||||
context
|
context
|
||||||
.get_function(runtime::FUNCTION_LOAD_IMMUTABLE_DATA)
|
.get_function(name)
|
||||||
.expect("is always declared for runtime code")
|
.expect("is always declared for runtime code")
|
||||||
.borrow()
|
.borrow()
|
||||||
.declaration(),
|
.declaration(),
|
||||||
&[],
|
&[],
|
||||||
runtime::FUNCTION_LOAD_IMMUTABLE_DATA,
|
name,
|
||||||
);
|
);
|
||||||
load_from_memory(context, index)
|
load_from_memory(context, index)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,5 +85,14 @@ where
|
|||||||
offset,
|
offset,
|
||||||
"mstore8_destination",
|
"mstore8_destination",
|
||||||
);
|
);
|
||||||
context.build_store(pointer, value)
|
let pointer = context.build_sbrk(
|
||||||
|
pointer.to_int(context),
|
||||||
|
context.xlen_type().const_int(1, false),
|
||||||
|
)?;
|
||||||
|
context
|
||||||
|
.builder()
|
||||||
|
.build_store(pointer, value)?
|
||||||
|
.set_alignment(revive_common::BYTE_LENGTH_BYTE as u32)
|
||||||
|
.expect("Alignment is valid");
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
//! Translates the transaction return operations.
|
//! Translates the transaction return operations.
|
||||||
|
|
||||||
use crate::polkavm::context::address_space::AddressSpace;
|
|
||||||
use crate::polkavm::context::code_type::CodeType;
|
use crate::polkavm::context::code_type::CodeType;
|
||||||
use crate::polkavm::context::pointer::Pointer;
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
||||||
use crate::polkavm::context::Context;
|
use crate::polkavm::context::Context;
|
||||||
|
use crate::polkavm::evm::immutable::Store;
|
||||||
use crate::polkavm::Dependency;
|
use crate::polkavm::Dependency;
|
||||||
|
|
||||||
/// Translates the `return` instruction.
|
/// Translates the `return` instruction.
|
||||||
@@ -18,55 +18,11 @@ where
|
|||||||
match context.code_type() {
|
match context.code_type() {
|
||||||
None => anyhow::bail!("Return is not available if the contract part is undefined"),
|
None => anyhow::bail!("Return is not available if the contract part is undefined"),
|
||||||
Some(CodeType::Deploy) => {
|
Some(CodeType::Deploy) => {
|
||||||
let immutable_data_size_pointer = context
|
context.build_call(
|
||||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE)?
|
<Store as RuntimeFunction<D>>::declaration(context),
|
||||||
.value
|
Default::default(),
|
||||||
.as_pointer_value();
|
"store_immutable_data",
|
||||||
let immutable_data_size = context.build_load(
|
|
||||||
Pointer::new(
|
|
||||||
context.xlen_type(),
|
|
||||||
AddressSpace::Stack,
|
|
||||||
immutable_data_size_pointer,
|
|
||||||
),
|
|
||||||
"immutable_data_size_load",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let write_immutable_data_block = context.append_basic_block("write_immutables_block");
|
|
||||||
let join_return_block = context.append_basic_block("join_return_block");
|
|
||||||
let immutable_data_size_is_zero = context.builder().build_int_compare(
|
|
||||||
inkwell::IntPredicate::EQ,
|
|
||||||
context.xlen_type().const_zero(),
|
|
||||||
immutable_data_size.into_int_value(),
|
|
||||||
"immutable_data_size_is_zero",
|
|
||||||
)?;
|
|
||||||
context.build_conditional_branch(
|
|
||||||
immutable_data_size_is_zero,
|
|
||||||
join_return_block,
|
|
||||||
write_immutable_data_block,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
context.set_basic_block(write_immutable_data_block);
|
|
||||||
let immutable_data_pointer = context
|
|
||||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
|
|
||||||
.value
|
|
||||||
.as_pointer_value();
|
|
||||||
context.build_runtime_call(
|
|
||||||
revive_runtime_api::polkavm_imports::SET_IMMUTABLE_DATA,
|
|
||||||
&[
|
|
||||||
context
|
|
||||||
.builder()
|
|
||||||
.build_ptr_to_int(
|
|
||||||
immutable_data_pointer,
|
|
||||||
context.xlen_type(),
|
|
||||||
"immutable_data_pointer_to_xlen",
|
|
||||||
)?
|
|
||||||
.into(),
|
|
||||||
immutable_data_size,
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
context.build_unconditional_branch(join_return_block);
|
|
||||||
|
|
||||||
context.set_basic_block(join_return_block);
|
|
||||||
}
|
}
|
||||||
Some(CodeType::Runtime) => {}
|
Some(CodeType::Runtime) => {}
|
||||||
}
|
}
|
||||||
@@ -100,11 +56,7 @@ pub fn stop<D>(context: &mut Context<D>) -> anyhow::Result<()>
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
r#return(
|
r#return(context, context.word_const(0), context.word_const(0))
|
||||||
context,
|
|
||||||
context.integer_const(crate::polkavm::XLEN, 0),
|
|
||||||
context.integer_const(crate::polkavm::XLEN, 0),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translates the `invalid` instruction.
|
/// Translates the `invalid` instruction.
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
//! Translates the storage operations.
|
//! Translates the storage operations.
|
||||||
|
|
||||||
use crate::polkavm::context::address_space::AddressSpace;
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
||||||
use crate::polkavm::context::Context;
|
use crate::polkavm::context::Context;
|
||||||
use crate::polkavm::Dependency;
|
use crate::polkavm::Dependency;
|
||||||
|
use crate::PolkaVMLoadStorageWordFunction;
|
||||||
|
use crate::PolkaVMStoreStorageWordFunction;
|
||||||
|
|
||||||
/// Translates the storage load.
|
/// Translates the storage load.
|
||||||
pub fn load<'ctx, D>(
|
pub fn load<'ctx, D>(
|
||||||
@@ -12,10 +14,12 @@ pub fn load<'ctx, D>(
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer");
|
let name = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::NAME;
|
||||||
slot_ptr.address_space = AddressSpace::Storage;
|
let declaration = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
context.builder().build_store(slot_ptr.value, position)?;
|
let arguments = [context.xlen_type().const_zero().into(), position.into()];
|
||||||
context.build_load(slot_ptr, "storage_load_value")
|
Ok(context
|
||||||
|
.build_call(declaration, &arguments, "storage_load")
|
||||||
|
.unwrap_or_else(|| panic!("runtime function {name} should return a value")))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translates the storage store.
|
/// Translates the storage store.
|
||||||
@@ -27,10 +31,13 @@ pub fn store<'ctx, D>(
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer");
|
let declaration = <PolkaVMStoreStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
slot_ptr.address_space = AddressSpace::Storage;
|
let arguments = [
|
||||||
context.builder().build_store(slot_ptr.value, position)?;
|
context.xlen_type().const_zero().into(),
|
||||||
context.build_store(slot_ptr, value)?;
|
position.into(),
|
||||||
|
value.into(),
|
||||||
|
];
|
||||||
|
context.build_call(declaration, &arguments, "storage_store");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,10 +49,15 @@ pub fn transient_load<'ctx, D>(
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer");
|
let name = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::NAME;
|
||||||
slot_ptr.address_space = AddressSpace::TransientStorage;
|
let declaration = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
context.builder().build_store(slot_ptr.value, position)?;
|
let arguments = [
|
||||||
context.build_load(slot_ptr, "transient_storage_load_value")
|
context.xlen_type().const_int(1, false).into(),
|
||||||
|
position.into(),
|
||||||
|
];
|
||||||
|
Ok(context
|
||||||
|
.build_call(declaration, &arguments, "storage_load")
|
||||||
|
.unwrap_or_else(|| panic!("runtime function {name} should return a value")))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translates the transient storage store.
|
/// Translates the transient storage store.
|
||||||
@@ -57,9 +69,12 @@ pub fn transient_store<'ctx, D>(
|
|||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer");
|
let declaration = <PolkaVMStoreStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
slot_ptr.address_space = AddressSpace::TransientStorage;
|
let arguments = [
|
||||||
context.builder().build_store(slot_ptr.value, position)?;
|
context.xlen_type().const_int(1, false).into(),
|
||||||
context.build_store(slot_ptr, value)?;
|
position.into(),
|
||||||
|
value.into(),
|
||||||
|
];
|
||||||
|
context.build_call(declaration, &arguments, "storage_store");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ pub fn build_assembly_text(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Implemented by items which are translated into LLVM IR.
|
/// Implemented by items which are translated into LLVM IR.
|
||||||
#[allow(clippy::upper_case_acronyms)]
|
|
||||||
pub trait WriteLLVM<D>
|
pub trait WriteLLVM<D>
|
||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
|
|||||||
@@ -34,3 +34,4 @@ polkadot-sdk.features = [
|
|||||||
|
|
||||||
revive-solidity = { workspace = true, optional = true }
|
revive-solidity = { workspace = true, optional = true }
|
||||||
revive-differential = { workspace = true, optional = true }
|
revive-differential = { workspace = true, optional = true }
|
||||||
|
revive-llvm-context = { workspace = true }
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ impl ExtBuilder {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
pallet_balances::GenesisConfig::<Runtime> {
|
pallet_balances::GenesisConfig::<Runtime> {
|
||||||
balances: self.balance_genesis_config,
|
balances: self.balance_genesis_config,
|
||||||
|
dev_accounts: None,
|
||||||
}
|
}
|
||||||
.assimilate_storage(&mut t)
|
.assimilate_storage(&mut t)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -238,7 +239,6 @@ pub enum Code {
|
|||||||
Solidity {
|
Solidity {
|
||||||
path: Option<std::path::PathBuf>,
|
path: Option<std::path::PathBuf>,
|
||||||
solc_optimizer: Option<bool>,
|
solc_optimizer: Option<bool>,
|
||||||
pipeline: Option<revive_solidity::SolcPipeline>,
|
|
||||||
contract: String,
|
contract: String,
|
||||||
},
|
},
|
||||||
/// Read the contract blob from disk
|
/// Read the contract blob from disk
|
||||||
@@ -263,7 +263,6 @@ impl From<Code> for pallet_revive::Code {
|
|||||||
path,
|
path,
|
||||||
contract,
|
contract,
|
||||||
solc_optimizer,
|
solc_optimizer,
|
||||||
pipeline,
|
|
||||||
} => {
|
} => {
|
||||||
let Some(path) = path else {
|
let Some(path) = path else {
|
||||||
panic!("Solidity source of contract '{contract}' missing path");
|
panic!("Solidity source of contract '{contract}' missing path");
|
||||||
@@ -275,7 +274,7 @@ impl From<Code> for pallet_revive::Code {
|
|||||||
&contract,
|
&contract,
|
||||||
&source_code,
|
&source_code,
|
||||||
solc_optimizer.unwrap_or(true),
|
solc_optimizer.unwrap_or(true),
|
||||||
pipeline.unwrap_or(revive_solidity::SolcPipeline::Yul),
|
revive_llvm_context::OptimizerSettings::cycles(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
Code::Path(path) => pallet_revive::Code::Upload(std::fs::read(path).unwrap()),
|
Code::Path(path) => pallet_revive::Code::Upload(std::fs::read(path).unwrap()),
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use frame_support::{runtime, weights::constants::WEIGHT_REF_TIME_PER_SECOND};
|
use frame_support::{runtime, traits::FindAuthor, weights::constants::WEIGHT_REF_TIME_PER_SECOND};
|
||||||
|
|
||||||
use pallet_revive::AccountId32Mapper;
|
use pallet_revive::AccountId32Mapper;
|
||||||
use polkadot_sdk::*;
|
use polkadot_sdk::*;
|
||||||
use polkadot_sdk::{
|
use polkadot_sdk::{
|
||||||
@@ -11,8 +10,6 @@ pub type Balance = u128;
|
|||||||
pub type AccountId = pallet_revive::AccountId32Mapper<Runtime>;
|
pub type AccountId = pallet_revive::AccountId32Mapper<Runtime>;
|
||||||
pub type Block = frame_system::mocking::MockBlock<Runtime>;
|
pub type Block = frame_system::mocking::MockBlock<Runtime>;
|
||||||
pub type Hash = <Runtime as frame_system::Config>::Hash;
|
pub type Hash = <Runtime as frame_system::Config>::Hash;
|
||||||
pub type EventRecord =
|
|
||||||
frame_system::EventRecord<<Runtime as frame_system::Config>::RuntimeEvent, Hash>;
|
|
||||||
|
|
||||||
#[runtime]
|
#[runtime]
|
||||||
mod runtime {
|
mod runtime {
|
||||||
@@ -26,7 +23,8 @@ mod runtime {
|
|||||||
RuntimeHoldReason,
|
RuntimeHoldReason,
|
||||||
RuntimeSlashReason,
|
RuntimeSlashReason,
|
||||||
RuntimeLockId,
|
RuntimeLockId,
|
||||||
RuntimeTask
|
RuntimeTask,
|
||||||
|
RuntimeViewFunction
|
||||||
)]
|
)]
|
||||||
pub struct Runtime;
|
pub struct Runtime;
|
||||||
|
|
||||||
@@ -87,6 +85,15 @@ impl pallet_revive::Config for Runtime {
|
|||||||
type UploadOrigin = EnsureSigned<AccountId32>;
|
type UploadOrigin = EnsureSigned<AccountId32>;
|
||||||
type InstantiateOrigin = EnsureSigned<AccountId32>;
|
type InstantiateOrigin = EnsureSigned<AccountId32>;
|
||||||
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
|
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
|
||||||
type Debug = ();
|
|
||||||
type ChainId = ConstU64<420_420_420>;
|
type ChainId = ConstU64<420_420_420>;
|
||||||
|
type FindAuthor = Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FindAuthor<<Runtime as frame_system::Config>::AccountId> for Runtime {
|
||||||
|
fn find_author<'a, I>(_digests: I) -> Option<<Runtime as frame_system::Config>::AccountId>
|
||||||
|
where
|
||||||
|
I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,
|
||||||
|
{
|
||||||
|
Some([0xff; 32].into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -282,17 +282,11 @@ impl Specs {
|
|||||||
let Code::Solidity {
|
let Code::Solidity {
|
||||||
path: Some(path),
|
path: Some(path),
|
||||||
solc_optimizer,
|
solc_optimizer,
|
||||||
pipeline,
|
|
||||||
contract,
|
contract,
|
||||||
} = code
|
} = code
|
||||||
else {
|
else {
|
||||||
panic!("the differential runner requires Code::Solidity source");
|
panic!("the differential runner requires Code::Solidity source");
|
||||||
};
|
};
|
||||||
assert_ne!(
|
|
||||||
pipeline,
|
|
||||||
Some(revive_solidity::SolcPipeline::EVMLA),
|
|
||||||
"yul pipeline must be enabled in differential mode"
|
|
||||||
);
|
|
||||||
assert!(
|
assert!(
|
||||||
salt.0.is_none(),
|
salt.0.is_none(),
|
||||||
"salt is not supported in differential mode"
|
"salt is not supported in differential mode"
|
||||||
|
|||||||
@@ -1,10 +1,4 @@
|
|||||||
use inkwell::{
|
use inkwell::{builder::Builder, context::Context, module::Module, values::IntValue};
|
||||||
builder::Builder,
|
|
||||||
context::Context,
|
|
||||||
module::Module,
|
|
||||||
types::{BasicType, StructType},
|
|
||||||
values::{BasicValueEnum, PointerValue},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Creates a module that sets the PolkaVM minimum stack size to [`size`] if linked in.
|
/// Creates a module that sets the PolkaVM minimum stack size to [`size`] if linked in.
|
||||||
pub fn min_stack_size<'context>(
|
pub fn min_stack_size<'context>(
|
||||||
@@ -21,115 +15,23 @@ pub fn min_stack_size<'context>(
|
|||||||
module
|
module
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper for building function calls with stack spilled arguments.
|
/// Helper for packing two 32 bit integer values into a 64 bit integer value.
|
||||||
/// - `pointer`: points to a struct of the packed argument struct type
|
pub fn pack_hi_lo_reg<'ctx>(
|
||||||
/// - `type`: the packed argument struct type
|
|
||||||
/// - `arguments`: a correctly ordered list of the struct field values
|
|
||||||
pub fn spill<'ctx>(
|
|
||||||
builder: &Builder<'ctx>,
|
builder: &Builder<'ctx>,
|
||||||
pointer: PointerValue<'ctx>,
|
context: &'ctx Context,
|
||||||
r#type: StructType<'ctx>,
|
hi: IntValue<'ctx>,
|
||||||
arguments: &[BasicValueEnum<'ctx>],
|
lo: IntValue<'ctx>,
|
||||||
) -> anyhow::Result<()> {
|
name: &str,
|
||||||
for index in 0..r#type.get_field_types().len() {
|
) -> anyhow::Result<IntValue<'ctx>> {
|
||||||
let field_pointer = builder.build_struct_gep(
|
assert_eq!(hi.get_type(), context.i32_type());
|
||||||
r#type,
|
assert_eq!(lo.get_type(), context.i32_type());
|
||||||
pointer,
|
|
||||||
index as u32,
|
|
||||||
&format!("spill_parameter_{}", index),
|
|
||||||
)?;
|
|
||||||
let field_value = arguments
|
|
||||||
.get(index)
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("invalid index {index} for struct type {}", r#type))?;
|
|
||||||
builder.build_store(field_pointer, *field_value)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
let lo_part = builder.build_int_z_extend(lo, context.i64_type(), &format!("{name}_lo_part"))?;
|
||||||
}
|
let hi_part = builder.build_int_z_extend(hi, context.i64_type(), &format!("{name}_hi_part"))?;
|
||||||
|
let hi_part_shifted = builder.build_left_shift(
|
||||||
/// Returns a packed struct argument type for the `instantiate` API.
|
hi_part,
|
||||||
pub fn instantiate(context: &Context) -> StructType {
|
context.i64_type().const_int(32, false),
|
||||||
context.struct_type(
|
&format!("{name}_hi_part_shifted"),
|
||||||
&[
|
)?;
|
||||||
// code_hash_ptr: u32,
|
Ok(builder.build_or(hi_part_shifted, lo_part, name)?)
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// ref_time_limit: u64,
|
|
||||||
context.i64_type().as_basic_type_enum(),
|
|
||||||
// proof_size_limit: u64,
|
|
||||||
context.i64_type().as_basic_type_enum(),
|
|
||||||
// deposit_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// value_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// input_data_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// input_data_len: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// address_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// output_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// output_len_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// salt_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
],
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a packed struct argument type for the `call` API.
|
|
||||||
pub fn call(context: &Context) -> StructType {
|
|
||||||
context.struct_type(
|
|
||||||
&[
|
|
||||||
// flags: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// address_ptr:
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// ref_time_limit: u64,
|
|
||||||
context.i64_type().as_basic_type_enum(),
|
|
||||||
// proof_size_limit: u64,
|
|
||||||
context.i64_type().as_basic_type_enum(),
|
|
||||||
// deposit_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// value_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// input_data_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// input_data_len: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// output_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// output_len_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
],
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a packed struct argument type for the `delegate_call` API.
|
|
||||||
pub fn delegate_call(context: &Context) -> StructType {
|
|
||||||
context.struct_type(
|
|
||||||
&[
|
|
||||||
// flags: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// address_ptr:
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// ref_time_limit: u64,
|
|
||||||
context.i64_type().as_basic_type_enum(),
|
|
||||||
// proof_size_limit: u64,
|
|
||||||
context.i64_type().as_basic_type_enum(),
|
|
||||||
// deposit_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// input_data_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// input_data_len: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// output_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
// output_len_ptr: u32,
|
|
||||||
context.i32_type().as_basic_type_enum(),
|
|
||||||
],
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,27 +8,23 @@
|
|||||||
#define EVM_WORD_SIZE 32
|
#define EVM_WORD_SIZE 32
|
||||||
#define ALIGN(size) ((size + EVM_WORD_SIZE - 1) & ~(EVM_WORD_SIZE - 1))
|
#define ALIGN(size) ((size + EVM_WORD_SIZE - 1) & ~(EVM_WORD_SIZE - 1))
|
||||||
#define MAX_MEMORY_SIZE (64 * 1024)
|
#define MAX_MEMORY_SIZE (64 * 1024)
|
||||||
static char __memory[MAX_MEMORY_SIZE];
|
char __memory[MAX_MEMORY_SIZE];
|
||||||
static uint32_t __memory_size = 0;
|
uint32_t __memory_size = 0;
|
||||||
|
|
||||||
void * __sbrk_internal(uint32_t offset, uint32_t size) {
|
void * __sbrk_internal(uint32_t offset, uint32_t size) {
|
||||||
if (offset >= MAX_MEMORY_SIZE || size > MAX_MEMORY_SIZE) {
|
if (offset >= MAX_MEMORY_SIZE || size > MAX_MEMORY_SIZE) {
|
||||||
return NULL;
|
POLKAVM_TRAP();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t new_size = ALIGN(offset + size);
|
uint32_t new_size = ALIGN(offset + size);
|
||||||
if (new_size > MAX_MEMORY_SIZE) {
|
if (new_size > MAX_MEMORY_SIZE) {
|
||||||
return NULL;
|
POLKAVM_TRAP();
|
||||||
}
|
}
|
||||||
if (new_size > __memory_size) {
|
if (new_size > __memory_size) {
|
||||||
__memory_size = new_size;
|
__memory_size = new_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (void *)&__memory[__memory_size];
|
return (void *)&__memory[offset];
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t __msize() {
|
|
||||||
return __memory_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void * memset(void *b, int c, size_t len) {
|
void * memset(void *b, int c, size_t len) {
|
||||||
@@ -72,11 +68,13 @@ POLKAVM_IMPORT(void, balance_of, uint32_t, uint32_t)
|
|||||||
|
|
||||||
POLKAVM_IMPORT(void, base_fee, uint32_t)
|
POLKAVM_IMPORT(void, base_fee, uint32_t)
|
||||||
|
|
||||||
|
POLKAVM_IMPORT(void, block_author, uint32_t)
|
||||||
|
|
||||||
POLKAVM_IMPORT(void, block_hash, uint32_t, uint32_t)
|
POLKAVM_IMPORT(void, block_hash, uint32_t, uint32_t)
|
||||||
|
|
||||||
POLKAVM_IMPORT(void, block_number, uint32_t)
|
POLKAVM_IMPORT(void, block_number, uint32_t)
|
||||||
|
|
||||||
POLKAVM_IMPORT(uint64_t, call, uint32_t)
|
POLKAVM_IMPORT(uint64_t, call, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t)
|
||||||
|
|
||||||
POLKAVM_IMPORT(uint64_t, call_data_copy, uint32_t, uint32_t, uint32_t)
|
POLKAVM_IMPORT(uint64_t, call_data_copy, uint32_t, uint32_t, uint32_t)
|
||||||
|
|
||||||
@@ -92,7 +90,7 @@ POLKAVM_IMPORT(uint64_t, code_size, uint32_t)
|
|||||||
|
|
||||||
POLKAVM_IMPORT(void, code_hash, uint32_t, uint32_t)
|
POLKAVM_IMPORT(void, code_hash, uint32_t, uint32_t)
|
||||||
|
|
||||||
POLKAVM_IMPORT(uint64_t, delegate_call, uint32_t)
|
POLKAVM_IMPORT(uint64_t, delegate_call, uint64_t, uint64_t, uint64_t, uint32_t, uint64_t, uint64_t)
|
||||||
|
|
||||||
POLKAVM_IMPORT(void, deposit_event, uint32_t, uint32_t, uint32_t, uint32_t)
|
POLKAVM_IMPORT(void, deposit_event, uint32_t, uint32_t, uint32_t, uint32_t)
|
||||||
|
|
||||||
@@ -106,7 +104,7 @@ POLKAVM_IMPORT(uint64_t, get_storage, uint32_t, uint32_t, uint32_t, uint32_t, ui
|
|||||||
|
|
||||||
POLKAVM_IMPORT(void, hash_keccak_256, uint32_t, uint32_t, uint32_t)
|
POLKAVM_IMPORT(void, hash_keccak_256, uint32_t, uint32_t, uint32_t)
|
||||||
|
|
||||||
POLKAVM_IMPORT(uint64_t, instantiate, uint32_t)
|
POLKAVM_IMPORT(uint64_t, instantiate, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t)
|
||||||
|
|
||||||
POLKAVM_IMPORT(void, now, uint32_t)
|
POLKAVM_IMPORT(void, now, uint32_t)
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,14 @@
|
|||||||
//! This crate vendors the [PolkaVM][0] C API and provides a LLVM module for interacting
|
|
||||||
//! with the `pallet-revive` runtime API.
|
|
||||||
//! At present, the revive pallet requires blobs to export `call` and `deploy`,
|
|
||||||
//! and offers a bunch of [runtime API methods][1]. The provided [module] implements
|
|
||||||
//! those exports and imports.
|
|
||||||
//! [0]: [https://crates.io/crates/polkavm]
|
|
||||||
//! [1]: [https://docs.rs/pallet-contracts/26.0.0/pallet_contracts/api_doc/index.html]
|
|
||||||
|
|
||||||
use inkwell::{context::Context, memory_buffer::MemoryBuffer, module::Module, support::LLVMString};
|
use inkwell::{context::Context, memory_buffer::MemoryBuffer, module::Module, support::LLVMString};
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/polkavm_imports.rs"));
|
include!(concat!(env!("OUT_DIR"), "/polkavm_imports.rs"));
|
||||||
|
|
||||||
pub static SBRK: &str = "__sbrk_internal";
|
/// The emulated EVM heap memory global symbol.
|
||||||
|
pub static MEMORY: &str = "__memory";
|
||||||
|
|
||||||
pub static MEMORY_SIZE: &str = "__msize";
|
/// The emulated EVM heap memory size global symbol.
|
||||||
|
pub static MEMORY_SIZE: &str = "__memory_size";
|
||||||
|
|
||||||
|
pub static SBRK: &str = "__sbrk_internal";
|
||||||
|
|
||||||
pub static ADDRESS: &str = "address";
|
pub static ADDRESS: &str = "address";
|
||||||
|
|
||||||
@@ -22,6 +18,8 @@ pub static BALANCE_OF: &str = "balance_of";
|
|||||||
|
|
||||||
pub static BASE_FEE: &str = "base_fee";
|
pub static BASE_FEE: &str = "base_fee";
|
||||||
|
|
||||||
|
pub static BLOCK_AUTHOR: &str = "block_author";
|
||||||
|
|
||||||
pub static BLOCK_HASH: &str = "block_hash";
|
pub static BLOCK_HASH: &str = "block_hash";
|
||||||
|
|
||||||
pub static BLOCK_NUMBER: &str = "block_number";
|
pub static BLOCK_NUMBER: &str = "block_number";
|
||||||
@@ -82,11 +80,11 @@ pub static WEIGHT_TO_FEE: &str = "weight_to_fee";
|
|||||||
/// Useful for configuring common attributes and linkage.
|
/// Useful for configuring common attributes and linkage.
|
||||||
pub static IMPORTS: [&str; 34] = [
|
pub static IMPORTS: [&str; 34] = [
|
||||||
SBRK,
|
SBRK,
|
||||||
MEMORY_SIZE,
|
|
||||||
ADDRESS,
|
ADDRESS,
|
||||||
BALANCE,
|
BALANCE,
|
||||||
BALANCE_OF,
|
BALANCE_OF,
|
||||||
BASE_FEE,
|
BASE_FEE,
|
||||||
|
BLOCK_AUTHOR,
|
||||||
BLOCK_HASH,
|
BLOCK_HASH,
|
||||||
BLOCK_NUMBER,
|
BLOCK_NUMBER,
|
||||||
CALL,
|
CALL,
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ regex = { workspace = true }
|
|||||||
hex = { workspace = true }
|
hex = { workspace = true }
|
||||||
num = { workspace = true }
|
num = { workspace = true }
|
||||||
sha3 = { workspace = true }
|
sha3 = { workspace = true }
|
||||||
md5 = { workspace = true }
|
|
||||||
inkwell = { workspace = true }
|
inkwell = { workspace = true }
|
||||||
|
|
||||||
revive-common = { workspace = true }
|
revive-common = { workspace = true }
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ impl Contract {
|
|||||||
file_path.push(file_name);
|
file_path.push(file_name);
|
||||||
|
|
||||||
if file_path.exists() && !overwrite {
|
if file_path.exists() && !overwrite {
|
||||||
eprintln!(
|
anyhow::bail!(
|
||||||
"Refusing to overwrite an existing file {file_path:?} (use --overwrite to force)."
|
"Refusing to overwrite an existing file {file_path:?} (use --overwrite to force)."
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
@@ -87,7 +87,7 @@ impl Contract {
|
|||||||
file_path.push(file_name);
|
file_path.push(file_name);
|
||||||
|
|
||||||
if file_path.exists() && !overwrite {
|
if file_path.exists() && !overwrite {
|
||||||
eprintln!(
|
anyhow::bail!(
|
||||||
"Refusing to overwrite an existing file {file_path:?} (use --overwrite to force)."
|
"Refusing to overwrite an existing file {file_path:?} (use --overwrite to force)."
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
//! Solidity to PolkaVM compiler constants.
|
//! Solidity to PolkaVM compiler constants.
|
||||||
|
|
||||||
#![allow(dead_code)]
|
|
||||||
|
|
||||||
/// The default executable name.
|
/// The default executable name.
|
||||||
pub static DEFAULT_EXECUTABLE_NAME: &str = "resolc";
|
pub static DEFAULT_EXECUTABLE_NAME: &str = "resolc";
|
||||||
|
|
||||||
|
|||||||
@@ -1,67 +0,0 @@
|
|||||||
//! The inner JSON legacy assembly code element.
|
|
||||||
|
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
use crate::evmla::assembly::Assembly;
|
|
||||||
|
|
||||||
/// The inner JSON legacy assembly code element.
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
pub enum Data {
|
|
||||||
/// The assembly code wrapper.
|
|
||||||
Assembly(Assembly),
|
|
||||||
/// The hash.
|
|
||||||
Hash(String),
|
|
||||||
/// The full contract path after the factory dependencies replacing pass.
|
|
||||||
Path(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Data {
|
|
||||||
/// Returns the inner assembly reference if it is present.
|
|
||||||
pub fn get_assembly(&self) -> Option<&Assembly> {
|
|
||||||
match self {
|
|
||||||
Self::Assembly(ref assembly) => Some(assembly),
|
|
||||||
Self::Hash(_) => None,
|
|
||||||
Self::Path(_) => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Returns the inner assembly mutable reference if it is present.
|
|
||||||
pub fn get_assembly_mut(&mut self) -> Option<&mut Assembly> {
|
|
||||||
match self {
|
|
||||||
Self::Assembly(ref mut assembly) => Some(assembly),
|
|
||||||
Self::Hash(_) => None,
|
|
||||||
Self::Path(_) => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the list of missing deployable libraries.
|
|
||||||
pub fn get_missing_libraries(&self) -> HashSet<String> {
|
|
||||||
match self {
|
|
||||||
Self::Assembly(assembly) => assembly.get_missing_libraries(),
|
|
||||||
Self::Hash(_) => HashSet::new(),
|
|
||||||
Self::Path(_) => HashSet::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the contract `keccak256` hash.
|
|
||||||
pub fn keccak256(&self) -> String {
|
|
||||||
match self {
|
|
||||||
Self::Assembly(assembly) => assembly.keccak256(),
|
|
||||||
Self::Hash(hash) => panic!("Expected assembly, found hash `{hash}`"),
|
|
||||||
Self::Path(path) => panic!("Expected assembly, found path `{path}`"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Data {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::Assembly(inner) => writeln!(f, "{inner}"),
|
|
||||||
Self::Hash(inner) => writeln!(f, "Hash `{inner}`"),
|
|
||||||
Self::Path(inner) => writeln!(f, "Path `{inner}`"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
//! Translates the CODECOPY use cases.
|
|
||||||
|
|
||||||
/// Translates the contract hash copying.
|
|
||||||
pub fn contract_hash<'ctx, D>(
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>,
|
|
||||||
offset: inkwell::values::IntValue<'ctx>,
|
|
||||||
value: inkwell::values::IntValue<'ctx>,
|
|
||||||
) -> anyhow::Result<()>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
let offset = context.builder().build_int_add(
|
|
||||||
offset,
|
|
||||||
context
|
|
||||||
.word_const((revive_common::BYTE_LENGTH_X32 + revive_common::BYTE_LENGTH_WORD) as u64),
|
|
||||||
"datacopy_contract_hash_offset",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
revive_llvm_context::polkavm_evm_memory::store(context, offset, value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Translates the library marker copying.
|
|
||||||
pub fn library_marker<D>(
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<D>,
|
|
||||||
offset: u64,
|
|
||||||
value: u64,
|
|
||||||
) -> anyhow::Result<()>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
revive_llvm_context::polkavm_evm_memory::store_byte(
|
|
||||||
context,
|
|
||||||
context.word_const(offset),
|
|
||||||
context.word_const(value),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Translates the static data copying.
|
|
||||||
pub fn static_data<'ctx, D>(
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>,
|
|
||||||
destination: inkwell::values::IntValue<'ctx>,
|
|
||||||
source: &str,
|
|
||||||
) -> anyhow::Result<()>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
let mut offset = 0;
|
|
||||||
for (index, chunk) in source
|
|
||||||
.chars()
|
|
||||||
.collect::<Vec<char>>()
|
|
||||||
.chunks(revive_common::BYTE_LENGTH_WORD * 2)
|
|
||||||
.enumerate()
|
|
||||||
{
|
|
||||||
let mut value_string = chunk.iter().collect::<String>();
|
|
||||||
value_string.push_str(
|
|
||||||
"0".repeat((revive_common::BYTE_LENGTH_WORD * 2) - chunk.len())
|
|
||||||
.as_str(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let datacopy_destination = context.builder().build_int_add(
|
|
||||||
destination,
|
|
||||||
context.word_const(offset as u64),
|
|
||||||
format!("datacopy_destination_index_{index}").as_str(),
|
|
||||||
)?;
|
|
||||||
let datacopy_value = context.word_const_str_hex(value_string.as_str());
|
|
||||||
revive_llvm_context::polkavm_evm_memory::store(
|
|
||||||
context,
|
|
||||||
datacopy_destination,
|
|
||||||
datacopy_value,
|
|
||||||
)?;
|
|
||||||
offset += chunk.len() / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
//! Translates the jump operations.
|
|
||||||
|
|
||||||
/// Translates the unconditional jump.
|
|
||||||
pub fn unconditional<D>(
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<D>,
|
|
||||||
destination: num::BigUint,
|
|
||||||
stack_hash: md5::Digest,
|
|
||||||
) -> anyhow::Result<()>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
let code_type = context
|
|
||||||
.code_type()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("The contract code part type is undefined"))?;
|
|
||||||
let block_key = revive_llvm_context::PolkaVMFunctionBlockKey::new(code_type, destination);
|
|
||||||
|
|
||||||
let block = context
|
|
||||||
.current_function()
|
|
||||||
.borrow()
|
|
||||||
.evmla()
|
|
||||||
.find_block(&block_key, &stack_hash)?;
|
|
||||||
context.build_unconditional_branch(block.inner());
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Translates the conditional jump.
|
|
||||||
pub fn conditional<D>(
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<D>,
|
|
||||||
destination: num::BigUint,
|
|
||||||
stack_hash: md5::Digest,
|
|
||||||
stack_height: usize,
|
|
||||||
) -> anyhow::Result<()>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
let code_type = context
|
|
||||||
.code_type()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("The contract code part type is undefined"))?;
|
|
||||||
let block_key = revive_llvm_context::PolkaVMFunctionBlockKey::new(code_type, destination);
|
|
||||||
|
|
||||||
let condition_pointer = context.evmla().stack[stack_height]
|
|
||||||
.to_llvm()
|
|
||||||
.into_pointer_value();
|
|
||||||
let condition = context.build_load(
|
|
||||||
revive_llvm_context::PolkaVMPointer::new_stack_field(context, condition_pointer),
|
|
||||||
format!("conditional_{block_key}_condition").as_str(),
|
|
||||||
)?;
|
|
||||||
let condition = context.builder().build_int_compare(
|
|
||||||
inkwell::IntPredicate::NE,
|
|
||||||
condition.into_int_value(),
|
|
||||||
context.word_const(0),
|
|
||||||
format!("conditional_{block_key}_condition_compared").as_str(),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let then_block = context
|
|
||||||
.current_function()
|
|
||||||
.borrow()
|
|
||||||
.evmla()
|
|
||||||
.find_block(&block_key, &stack_hash)?;
|
|
||||||
let join_block =
|
|
||||||
context.append_basic_block(format!("conditional_{block_key}_join_block").as_str());
|
|
||||||
|
|
||||||
context.build_conditional_branch(condition, then_block.inner(), join_block)?;
|
|
||||||
|
|
||||||
context.set_basic_block(join_block);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -1,382 +0,0 @@
|
|||||||
//! The EVM instruction.
|
|
||||||
|
|
||||||
pub mod codecopy;
|
|
||||||
pub mod jump;
|
|
||||||
pub mod name;
|
|
||||||
pub mod stack;
|
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
use self::name::Name;
|
|
||||||
|
|
||||||
/// The EVM instruction.
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
||||||
pub struct Instruction {
|
|
||||||
/// The opcode or tag identifier.
|
|
||||||
pub name: Name,
|
|
||||||
/// The optional value argument.
|
|
||||||
pub value: Option<String>,
|
|
||||||
|
|
||||||
/// The source code identifier.
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
pub source: Option<isize>,
|
|
||||||
/// The source code location begin.
|
|
||||||
pub begin: isize,
|
|
||||||
/// The source code location end.
|
|
||||||
pub end: isize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Instruction {
|
|
||||||
/// Returns the number of input stack arguments.
|
|
||||||
pub const fn input_size(&self, version: &semver::Version) -> usize {
|
|
||||||
match self.name {
|
|
||||||
Name::POP => 1,
|
|
||||||
|
|
||||||
Name::JUMP => 1,
|
|
||||||
Name::JUMPI => 2,
|
|
||||||
|
|
||||||
Name::ADD => 2,
|
|
||||||
Name::SUB => 2,
|
|
||||||
Name::MUL => 2,
|
|
||||||
Name::DIV => 2,
|
|
||||||
Name::MOD => 2,
|
|
||||||
Name::SDIV => 2,
|
|
||||||
Name::SMOD => 2,
|
|
||||||
|
|
||||||
Name::LT => 2,
|
|
||||||
Name::GT => 2,
|
|
||||||
Name::EQ => 2,
|
|
||||||
Name::ISZERO => 1,
|
|
||||||
Name::SLT => 2,
|
|
||||||
Name::SGT => 2,
|
|
||||||
|
|
||||||
Name::OR => 2,
|
|
||||||
Name::XOR => 2,
|
|
||||||
Name::NOT => 1,
|
|
||||||
Name::AND => 2,
|
|
||||||
Name::SHL => 2,
|
|
||||||
Name::SHR => 2,
|
|
||||||
Name::SAR => 2,
|
|
||||||
Name::BYTE => 2,
|
|
||||||
|
|
||||||
Name::ADDMOD => 3,
|
|
||||||
Name::MULMOD => 3,
|
|
||||||
Name::EXP => 2,
|
|
||||||
Name::SIGNEXTEND => 2,
|
|
||||||
Name::SHA3 => 2,
|
|
||||||
Name::KECCAK256 => 2,
|
|
||||||
|
|
||||||
Name::MLOAD => 1,
|
|
||||||
Name::MSTORE => 2,
|
|
||||||
Name::MSTORE8 => 2,
|
|
||||||
Name::MCOPY => 3,
|
|
||||||
|
|
||||||
Name::SLOAD => 1,
|
|
||||||
Name::SSTORE => 2,
|
|
||||||
Name::TLOAD => 1,
|
|
||||||
Name::TSTORE => 2,
|
|
||||||
Name::PUSHIMMUTABLE => 0,
|
|
||||||
Name::ASSIGNIMMUTABLE => {
|
|
||||||
if version.minor >= 8 {
|
|
||||||
2
|
|
||||||
} else {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Name::CALLDATALOAD => 1,
|
|
||||||
Name::CALLDATACOPY => 3,
|
|
||||||
Name::CODECOPY => 3,
|
|
||||||
Name::RETURNDATACOPY => 3,
|
|
||||||
Name::EXTCODESIZE => 1,
|
|
||||||
Name::EXTCODEHASH => 1,
|
|
||||||
|
|
||||||
Name::CALL => 7,
|
|
||||||
Name::CALLCODE => 7,
|
|
||||||
Name::STATICCALL => 6,
|
|
||||||
Name::DELEGATECALL => 6,
|
|
||||||
|
|
||||||
Name::RETURN => 2,
|
|
||||||
Name::REVERT => 2,
|
|
||||||
Name::SELFDESTRUCT => 1,
|
|
||||||
|
|
||||||
Name::LOG0 => 2,
|
|
||||||
Name::LOG1 => 3,
|
|
||||||
Name::LOG2 => 4,
|
|
||||||
Name::LOG3 => 5,
|
|
||||||
Name::LOG4 => 6,
|
|
||||||
|
|
||||||
Name::CREATE => 3,
|
|
||||||
Name::CREATE2 => 4,
|
|
||||||
|
|
||||||
Name::ZK_CREATE => 3,
|
|
||||||
Name::ZK_CREATE2 => 4,
|
|
||||||
|
|
||||||
Name::BALANCE => 1,
|
|
||||||
|
|
||||||
Name::BLOCKHASH => 1,
|
|
||||||
Name::BLOBHASH => 1,
|
|
||||||
|
|
||||||
Name::EXTCODECOPY => 4,
|
|
||||||
|
|
||||||
Name::RecursiveCall { input_size, .. } => input_size,
|
|
||||||
Name::RecursiveReturn { input_size } => input_size,
|
|
||||||
|
|
||||||
_ => 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the number of output stack arguments.
|
|
||||||
pub const fn output_size(&self) -> usize {
|
|
||||||
match self.name {
|
|
||||||
Name::PUSH => 1,
|
|
||||||
Name::PUSH_Data => 1,
|
|
||||||
Name::PUSH_Tag => 1,
|
|
||||||
Name::PUSH_ContractHash => 1,
|
|
||||||
Name::PUSH_ContractHashSize => 1,
|
|
||||||
Name::PUSHLIB => 1,
|
|
||||||
Name::PUSHDEPLOYADDRESS => 1,
|
|
||||||
|
|
||||||
Name::PUSH1 => 1,
|
|
||||||
Name::PUSH2 => 1,
|
|
||||||
Name::PUSH3 => 1,
|
|
||||||
Name::PUSH4 => 1,
|
|
||||||
Name::PUSH5 => 1,
|
|
||||||
Name::PUSH6 => 1,
|
|
||||||
Name::PUSH7 => 1,
|
|
||||||
Name::PUSH8 => 1,
|
|
||||||
Name::PUSH9 => 1,
|
|
||||||
Name::PUSH10 => 1,
|
|
||||||
Name::PUSH11 => 1,
|
|
||||||
Name::PUSH12 => 1,
|
|
||||||
Name::PUSH13 => 1,
|
|
||||||
Name::PUSH14 => 1,
|
|
||||||
Name::PUSH15 => 1,
|
|
||||||
Name::PUSH16 => 1,
|
|
||||||
Name::PUSH17 => 1,
|
|
||||||
Name::PUSH18 => 1,
|
|
||||||
Name::PUSH19 => 1,
|
|
||||||
Name::PUSH20 => 1,
|
|
||||||
Name::PUSH21 => 1,
|
|
||||||
Name::PUSH22 => 1,
|
|
||||||
Name::PUSH23 => 1,
|
|
||||||
Name::PUSH24 => 1,
|
|
||||||
Name::PUSH25 => 1,
|
|
||||||
Name::PUSH26 => 1,
|
|
||||||
Name::PUSH27 => 1,
|
|
||||||
Name::PUSH28 => 1,
|
|
||||||
Name::PUSH29 => 1,
|
|
||||||
Name::PUSH30 => 1,
|
|
||||||
Name::PUSH31 => 1,
|
|
||||||
Name::PUSH32 => 1,
|
|
||||||
|
|
||||||
Name::DUP1 => 1,
|
|
||||||
Name::DUP2 => 1,
|
|
||||||
Name::DUP3 => 1,
|
|
||||||
Name::DUP4 => 1,
|
|
||||||
Name::DUP5 => 1,
|
|
||||||
Name::DUP6 => 1,
|
|
||||||
Name::DUP7 => 1,
|
|
||||||
Name::DUP8 => 1,
|
|
||||||
Name::DUP9 => 1,
|
|
||||||
Name::DUP10 => 1,
|
|
||||||
Name::DUP11 => 1,
|
|
||||||
Name::DUP12 => 1,
|
|
||||||
Name::DUP13 => 1,
|
|
||||||
Name::DUP14 => 1,
|
|
||||||
Name::DUP15 => 1,
|
|
||||||
Name::DUP16 => 1,
|
|
||||||
|
|
||||||
Name::ADD => 1,
|
|
||||||
Name::SUB => 1,
|
|
||||||
Name::MUL => 1,
|
|
||||||
Name::DIV => 1,
|
|
||||||
Name::MOD => 1,
|
|
||||||
Name::SDIV => 1,
|
|
||||||
Name::SMOD => 1,
|
|
||||||
|
|
||||||
Name::LT => 1,
|
|
||||||
Name::GT => 1,
|
|
||||||
Name::EQ => 1,
|
|
||||||
Name::ISZERO => 1,
|
|
||||||
Name::SLT => 1,
|
|
||||||
Name::SGT => 1,
|
|
||||||
|
|
||||||
Name::OR => 1,
|
|
||||||
Name::XOR => 1,
|
|
||||||
Name::NOT => 1,
|
|
||||||
Name::AND => 1,
|
|
||||||
Name::SHL => 1,
|
|
||||||
Name::SHR => 1,
|
|
||||||
Name::SAR => 1,
|
|
||||||
Name::BYTE => 1,
|
|
||||||
|
|
||||||
Name::ADDMOD => 1,
|
|
||||||
Name::MULMOD => 1,
|
|
||||||
Name::EXP => 1,
|
|
||||||
Name::SIGNEXTEND => 1,
|
|
||||||
Name::SHA3 => 1,
|
|
||||||
Name::KECCAK256 => 1,
|
|
||||||
|
|
||||||
Name::MLOAD => 1,
|
|
||||||
|
|
||||||
Name::SLOAD => 1,
|
|
||||||
Name::TLOAD => 1,
|
|
||||||
Name::PUSHIMMUTABLE => 1,
|
|
||||||
|
|
||||||
Name::CALLDATALOAD => 1,
|
|
||||||
Name::CALLDATASIZE => 1,
|
|
||||||
Name::CODESIZE => 1,
|
|
||||||
Name::PUSHSIZE => 1,
|
|
||||||
Name::RETURNDATASIZE => 1,
|
|
||||||
Name::EXTCODESIZE => 1,
|
|
||||||
Name::EXTCODEHASH => 1,
|
|
||||||
|
|
||||||
Name::CALL => 1,
|
|
||||||
Name::CALLCODE => 1,
|
|
||||||
Name::STATICCALL => 1,
|
|
||||||
Name::DELEGATECALL => 1,
|
|
||||||
|
|
||||||
Name::CREATE => 1,
|
|
||||||
Name::CREATE2 => 1,
|
|
||||||
|
|
||||||
Name::ZK_CREATE => 1,
|
|
||||||
Name::ZK_CREATE2 => 1,
|
|
||||||
|
|
||||||
Name::ADDRESS => 1,
|
|
||||||
Name::CALLER => 1,
|
|
||||||
Name::TIMESTAMP => 1,
|
|
||||||
Name::NUMBER => 1,
|
|
||||||
|
|
||||||
Name::CALLVALUE => 1,
|
|
||||||
Name::GAS => 1,
|
|
||||||
Name::BALANCE => 1,
|
|
||||||
Name::SELFBALANCE => 1,
|
|
||||||
|
|
||||||
Name::GASLIMIT => 1,
|
|
||||||
Name::GASPRICE => 1,
|
|
||||||
Name::ORIGIN => 1,
|
|
||||||
Name::CHAINID => 1,
|
|
||||||
Name::BLOCKHASH => 1,
|
|
||||||
Name::BLOBHASH => 1,
|
|
||||||
Name::DIFFICULTY => 1,
|
|
||||||
Name::PREVRANDAO => 1,
|
|
||||||
Name::COINBASE => 1,
|
|
||||||
Name::MSIZE => 1,
|
|
||||||
|
|
||||||
Name::BASEFEE => 1,
|
|
||||||
Name::BLOBBASEFEE => 1,
|
|
||||||
Name::PC => 1,
|
|
||||||
|
|
||||||
Name::RecursiveCall { output_size, .. } => output_size,
|
|
||||||
|
|
||||||
_ => 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Replaces the instruction data aliases with the actual data.
|
|
||||||
pub fn replace_data_aliases(
|
|
||||||
instructions: &mut [Self],
|
|
||||||
mapping: &BTreeMap<String, String>,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
for instruction in instructions.iter_mut() {
|
|
||||||
match instruction {
|
|
||||||
Instruction {
|
|
||||||
name: Name::PUSH_ContractHash | Name::PUSH_ContractHashSize,
|
|
||||||
value: Some(value),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
*value = mapping.get(value.as_str()).cloned().ok_or_else(|| {
|
|
||||||
anyhow::anyhow!("Contract alias `{}` data not found", value)
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
Instruction {
|
|
||||||
name: Name::PUSH_Data,
|
|
||||||
value: Some(value),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
let mut key_extended =
|
|
||||||
"0".repeat(revive_common::BYTE_LENGTH_WORD * 2 - value.len());
|
|
||||||
key_extended.push_str(value.as_str());
|
|
||||||
|
|
||||||
*value = mapping.get(key_extended.as_str()).cloned().ok_or_else(|| {
|
|
||||||
anyhow::anyhow!("Data chunk alias `{}` data not found", key_extended)
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Initializes an `INVALID` instruction to terminate an invalid unreachable block part.
|
|
||||||
pub fn invalid(previous: &Self) -> Self {
|
|
||||||
Self {
|
|
||||||
name: Name::INVALID,
|
|
||||||
value: None,
|
|
||||||
|
|
||||||
source: previous.source,
|
|
||||||
begin: previous.begin,
|
|
||||||
end: previous.end,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Initializes a recursive function `Call` instruction.
|
|
||||||
pub fn recursive_call(
|
|
||||||
name: String,
|
|
||||||
entry_key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
stack_hash: md5::Digest,
|
|
||||||
input_size: usize,
|
|
||||||
output_size: usize,
|
|
||||||
return_address: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
previous: &Self,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
name: Name::RecursiveCall {
|
|
||||||
name,
|
|
||||||
entry_key,
|
|
||||||
stack_hash,
|
|
||||||
input_size,
|
|
||||||
output_size,
|
|
||||||
return_address,
|
|
||||||
},
|
|
||||||
value: None,
|
|
||||||
|
|
||||||
source: previous.source,
|
|
||||||
begin: previous.begin,
|
|
||||||
end: previous.end,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Initializes a recursive function `Return` instruction.
|
|
||||||
pub fn recursive_return(input_size: usize, previous: &Self) -> Self {
|
|
||||||
Self {
|
|
||||||
name: Name::RecursiveReturn { input_size },
|
|
||||||
value: None,
|
|
||||||
|
|
||||||
source: previous.source,
|
|
||||||
begin: previous.begin,
|
|
||||||
end: previous.end,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Instruction {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let name = self.name.to_string();
|
|
||||||
match self.name {
|
|
||||||
Name::Tag => write!(f, "{:4}", name),
|
|
||||||
_ => write!(f, "{:15}", name),
|
|
||||||
}?;
|
|
||||||
match self.value {
|
|
||||||
Some(ref value) if value.len() <= 64 => write!(f, "{}", value)?,
|
|
||||||
Some(ref value) => write!(f, "... {}", &value[value.len() - 60..])?,
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,417 +0,0 @@
|
|||||||
//! The EVM instruction name.
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
/// The EVM instruction name.
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
#[allow(clippy::upper_case_acronyms)]
|
|
||||||
pub enum Name {
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH,
|
|
||||||
/// Pushes a constant tag index.
|
|
||||||
#[serde(rename = "PUSH [tag]")]
|
|
||||||
PUSH_Tag,
|
|
||||||
/// Pushes an unknown `data` value.
|
|
||||||
#[serde(rename = "PUSH data")]
|
|
||||||
PUSH_Data,
|
|
||||||
/// Pushes a contract hash size.
|
|
||||||
#[serde(rename = "PUSH #[$]")]
|
|
||||||
PUSH_ContractHashSize,
|
|
||||||
/// Pushes a contract hash.
|
|
||||||
#[serde(rename = "PUSH [$]")]
|
|
||||||
PUSH_ContractHash,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH1,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH2,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH3,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH4,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH5,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH6,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH7,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH8,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH9,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH10,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH11,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH12,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH13,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH14,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH15,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH16,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH17,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH18,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH19,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH20,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH21,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH22,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH23,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH24,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH25,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH26,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH27,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH28,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH29,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH30,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH31,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSH32,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP1,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP2,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP3,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP4,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP5,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP6,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP7,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP8,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP9,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP10,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP11,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP12,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP13,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP14,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP15,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DUP16,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP1,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP2,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP3,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP4,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP5,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP6,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP7,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP8,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP9,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP10,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP11,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP12,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP13,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP14,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP15,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SWAP16,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
POP,
|
|
||||||
|
|
||||||
/// Sets the current basic code block.
|
|
||||||
#[serde(rename = "tag")]
|
|
||||||
Tag,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
JUMP,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
JUMPI,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
JUMPDEST,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
ADD,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SUB,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
MUL,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DIV,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
MOD,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SDIV,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SMOD,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
LT,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
GT,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
EQ,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
ISZERO,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SLT,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SGT,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
OR,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
XOR,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
NOT,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
AND,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SHL,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SHR,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SAR,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
BYTE,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
ADDMOD,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
MULMOD,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
EXP,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SIGNEXTEND,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SHA3,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
KECCAK256,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
MLOAD,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
MSTORE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
MSTORE8,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
MCOPY,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SLOAD,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SSTORE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
TLOAD,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
TSTORE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSHIMMUTABLE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
ASSIGNIMMUTABLE,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CALLDATALOAD,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CALLDATASIZE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CALLDATACOPY,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CODESIZE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CODECOPY,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSHSIZE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
EXTCODESIZE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
EXTCODEHASH,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
RETURNDATASIZE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
RETURNDATACOPY,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
RETURN,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
REVERT,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
STOP,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
INVALID,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
LOG0,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
LOG1,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
LOG2,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
LOG3,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
LOG4,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CALL,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
STATICCALL,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DELEGATECALL,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CREATE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CREATE2,
|
|
||||||
|
|
||||||
/// The eponymous PolkaVM instruction.
|
|
||||||
#[serde(rename = "$ZK_CREATE")]
|
|
||||||
ZK_CREATE,
|
|
||||||
/// The eponymous PolkaVM instruction.
|
|
||||||
#[serde(rename = "$ZK_CREATE2")]
|
|
||||||
ZK_CREATE2,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
ADDRESS,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CALLER,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CALLVALUE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
GAS,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
BALANCE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SELFBALANCE,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSHLIB,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PUSHDEPLOYADDRESS,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
GASLIMIT,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
GASPRICE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
ORIGIN,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CHAINID,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
TIMESTAMP,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
NUMBER,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
BLOCKHASH,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
BLOBHASH,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
DIFFICULTY,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PREVRANDAO,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
COINBASE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
BASEFEE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
BLOBBASEFEE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
MSIZE,
|
|
||||||
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
CALLCODE,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
PC,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
EXTCODECOPY,
|
|
||||||
/// The eponymous EVM instruction.
|
|
||||||
SELFDESTRUCT,
|
|
||||||
|
|
||||||
/// The recursive function call instruction.
|
|
||||||
#[serde(skip)]
|
|
||||||
RecursiveCall {
|
|
||||||
/// The called function name.
|
|
||||||
name: String,
|
|
||||||
/// The called function key.
|
|
||||||
entry_key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
/// The stack state hash after return.
|
|
||||||
stack_hash: md5::Digest,
|
|
||||||
/// The input size.
|
|
||||||
input_size: usize,
|
|
||||||
/// The output size.
|
|
||||||
output_size: usize,
|
|
||||||
/// The return address.
|
|
||||||
return_address: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
},
|
|
||||||
/// The recursive function return instruction.
|
|
||||||
#[serde(skip)]
|
|
||||||
RecursiveReturn {
|
|
||||||
/// The output size.
|
|
||||||
input_size: usize,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Name {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::Tag => write!(f, "Tag"),
|
|
||||||
Self::RecursiveCall {
|
|
||||||
name,
|
|
||||||
entry_key,
|
|
||||||
input_size,
|
|
||||||
output_size,
|
|
||||||
return_address,
|
|
||||||
..
|
|
||||||
} => write!(
|
|
||||||
f,
|
|
||||||
"RECURSIVE_CALL({}_{}, {}, {}, {})",
|
|
||||||
name, entry_key, input_size, output_size, return_address
|
|
||||||
),
|
|
||||||
Self::RecursiveReturn { input_size } => write!(f, "RECURSIVE_RETURN({})", input_size),
|
|
||||||
_ => write!(
|
|
||||||
f,
|
|
||||||
"{}",
|
|
||||||
serde_json::to_string(self)
|
|
||||||
.expect("Always valid")
|
|
||||||
.trim_matches('\"')
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
//! Translates the stack memory operations.
|
|
||||||
|
|
||||||
use inkwell::values::BasicValue;
|
|
||||||
|
|
||||||
/// Translates the ordinar value push.
|
|
||||||
pub fn push<'ctx, D>(
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>,
|
|
||||||
value: String,
|
|
||||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
let result = context
|
|
||||||
.word_type()
|
|
||||||
.const_int_from_string(
|
|
||||||
value.to_ascii_uppercase().as_str(),
|
|
||||||
inkwell::types::StringRadix::Hexadecimal,
|
|
||||||
)
|
|
||||||
.expect("Always valid")
|
|
||||||
.as_basic_value_enum();
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Translates the block tag label push.
|
|
||||||
pub fn push_tag<'ctx, D>(
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>,
|
|
||||||
value: String,
|
|
||||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
let result = context
|
|
||||||
.word_type()
|
|
||||||
.const_int_from_string(value.as_str(), inkwell::types::StringRadix::Decimal)
|
|
||||||
.expect("Always valid");
|
|
||||||
Ok(result.as_basic_value_enum())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Translates the stack memory duplicate.
|
|
||||||
pub fn dup<'ctx, D>(
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>,
|
|
||||||
offset: usize,
|
|
||||||
height: usize,
|
|
||||||
original: &mut Option<String>,
|
|
||||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
let element = &context.evmla().stack[height - offset - 1];
|
|
||||||
let value = context.build_load(
|
|
||||||
revive_llvm_context::PolkaVMPointer::new_stack_field(
|
|
||||||
context,
|
|
||||||
element.to_llvm().into_pointer_value(),
|
|
||||||
),
|
|
||||||
format!("dup{offset}").as_str(),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
element.original.clone_into(original);
|
|
||||||
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Translates the stack memory swap.
|
|
||||||
pub fn swap<D>(
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<D>,
|
|
||||||
offset: usize,
|
|
||||||
height: usize,
|
|
||||||
) -> anyhow::Result<()>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
let top_element = context.evmla().stack[height - 1].to_owned();
|
|
||||||
let top_pointer = revive_llvm_context::PolkaVMPointer::new_stack_field(
|
|
||||||
context,
|
|
||||||
top_element.to_llvm().into_pointer_value(),
|
|
||||||
);
|
|
||||||
let top_value = context.build_load(top_pointer, format!("swap{offset}_top_value").as_str())?;
|
|
||||||
|
|
||||||
let swap_element = context.evmla().stack[height - offset - 1].to_owned();
|
|
||||||
let swap_pointer = revive_llvm_context::PolkaVMPointer::new_stack_field(
|
|
||||||
context,
|
|
||||||
swap_element.to_llvm().into_pointer_value(),
|
|
||||||
);
|
|
||||||
let swap_value =
|
|
||||||
context.build_load(swap_pointer, format!("swap{offset}_swap_value").as_str())?;
|
|
||||||
|
|
||||||
swap_element
|
|
||||||
.original
|
|
||||||
.clone_into(&mut context.evmla_mut().stack[height - 1].original);
|
|
||||||
top_element
|
|
||||||
.original
|
|
||||||
.clone_into(&mut context.evmla_mut().stack[height - offset - 1].original);
|
|
||||||
|
|
||||||
context.build_store(top_pointer, swap_value)?;
|
|
||||||
context.build_store(swap_pointer, top_value)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Translates the stack memory pop.
|
|
||||||
pub fn pop<D>(_context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()>
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -1,295 +0,0 @@
|
|||||||
//! The `solc --asm-json` output.
|
|
||||||
|
|
||||||
pub mod data;
|
|
||||||
pub mod instruction;
|
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde::Serialize;
|
|
||||||
use sha3::Digest;
|
|
||||||
|
|
||||||
use crate::evmla::ethereal_ir::entry_link::EntryLink;
|
|
||||||
use crate::evmla::ethereal_ir::EtherealIR;
|
|
||||||
use crate::solc::standard_json::output::contract::evm::extra_metadata::ExtraMetadata;
|
|
||||||
|
|
||||||
use self::data::Data;
|
|
||||||
use self::instruction::name::Name as InstructionName;
|
|
||||||
use self::instruction::Instruction;
|
|
||||||
|
|
||||||
/// The JSON assembly.
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
||||||
pub struct Assembly {
|
|
||||||
/// The metadata string.
|
|
||||||
#[serde(rename = ".auxdata")]
|
|
||||||
pub auxdata: Option<String>,
|
|
||||||
/// The deploy code instructions.
|
|
||||||
#[serde(rename = ".code")]
|
|
||||||
pub code: Option<Vec<Instruction>>,
|
|
||||||
/// The runtime code.
|
|
||||||
#[serde(rename = ".data")]
|
|
||||||
pub data: Option<BTreeMap<String, Data>>,
|
|
||||||
|
|
||||||
/// The full contract path.
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
pub full_path: Option<String>,
|
|
||||||
/// The factory dependency paths.
|
|
||||||
#[serde(default = "HashSet::new")]
|
|
||||||
pub factory_dependencies: HashSet<String>,
|
|
||||||
/// The EVMLA extra metadata.
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
pub extra_metadata: Option<ExtraMetadata>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Assembly {
|
|
||||||
/// Gets the contract `keccak256` hash.
|
|
||||||
pub fn keccak256(&self) -> String {
|
|
||||||
let json = serde_json::to_vec(self).expect("Always valid");
|
|
||||||
hex::encode(sha3::Keccak256::digest(json.as_slice()))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the full contract path.
|
|
||||||
pub fn set_full_path(&mut self, full_path: String) {
|
|
||||||
self.full_path = Some(full_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the full contract path if it is set, or `<undefined>` otherwise.
|
|
||||||
/// # Panics
|
|
||||||
/// If the `full_path` has not been set.
|
|
||||||
pub fn full_path(&self) -> &str {
|
|
||||||
self.full_path
|
|
||||||
.as_deref()
|
|
||||||
.unwrap_or_else(|| panic!("The full path of some contracts is unset"))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the list of missing deployable libraries.
|
|
||||||
pub fn get_missing_libraries(&self) -> HashSet<String> {
|
|
||||||
let mut missing_libraries = HashSet::new();
|
|
||||||
if let Some(code) = self.code.as_ref() {
|
|
||||||
for instruction in code.iter() {
|
|
||||||
if let InstructionName::PUSHLIB = instruction.name {
|
|
||||||
let library_path = instruction.value.to_owned().expect("Always exists");
|
|
||||||
missing_libraries.insert(library_path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(data) = self.data.as_ref() {
|
|
||||||
for (_, data) in data.iter() {
|
|
||||||
missing_libraries.extend(data.get_missing_libraries());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
missing_libraries
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Replaces the deploy code dependencies with full contract path and returns the list.
|
|
||||||
pub fn deploy_dependencies_pass(
|
|
||||||
&mut self,
|
|
||||||
full_path: &str,
|
|
||||||
hash_data_mapping: &BTreeMap<String, String>,
|
|
||||||
) -> anyhow::Result<BTreeMap<String, String>> {
|
|
||||||
let mut index_path_mapping = BTreeMap::new();
|
|
||||||
let index = "0".repeat(revive_common::BYTE_LENGTH_WORD * 2);
|
|
||||||
index_path_mapping.insert(index, full_path.to_owned());
|
|
||||||
|
|
||||||
let dependencies = match self.data.as_mut() {
|
|
||||||
Some(dependencies) => dependencies,
|
|
||||||
None => return Ok(index_path_mapping),
|
|
||||||
};
|
|
||||||
for (index, data) in dependencies.iter_mut() {
|
|
||||||
if index == "0" {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut index_extended = "0".repeat(revive_common::BYTE_LENGTH_WORD * 2 - index.len());
|
|
||||||
index_extended.push_str(index.as_str());
|
|
||||||
|
|
||||||
*data = match data {
|
|
||||||
Data::Assembly(assembly) => {
|
|
||||||
let hash = assembly.keccak256();
|
|
||||||
let full_path =
|
|
||||||
hash_data_mapping
|
|
||||||
.get(hash.as_str())
|
|
||||||
.cloned()
|
|
||||||
.ok_or_else(|| {
|
|
||||||
anyhow::anyhow!("Contract path not found for hash `{}`", hash)
|
|
||||||
})?;
|
|
||||||
self.factory_dependencies.insert(full_path.to_owned());
|
|
||||||
|
|
||||||
index_path_mapping.insert(index_extended, full_path.clone());
|
|
||||||
Data::Path(full_path)
|
|
||||||
}
|
|
||||||
Data::Hash(hash) => {
|
|
||||||
index_path_mapping.insert(index_extended, hash.to_owned());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
_ => continue,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(index_path_mapping)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Replaces the runtime code dependencies with full contract path and returns the list.
|
|
||||||
pub fn runtime_dependencies_pass(
|
|
||||||
&mut self,
|
|
||||||
full_path: &str,
|
|
||||||
hash_data_mapping: &BTreeMap<String, String>,
|
|
||||||
) -> anyhow::Result<BTreeMap<String, String>> {
|
|
||||||
let mut index_path_mapping = BTreeMap::new();
|
|
||||||
let index = "0".repeat(revive_common::BYTE_LENGTH_WORD * 2);
|
|
||||||
index_path_mapping.insert(index, full_path.to_owned());
|
|
||||||
|
|
||||||
let dependencies = match self
|
|
||||||
.data
|
|
||||||
.as_mut()
|
|
||||||
.and_then(|data| data.get_mut("0"))
|
|
||||||
.and_then(|data| data.get_assembly_mut())
|
|
||||||
.and_then(|assembly| assembly.data.as_mut())
|
|
||||||
{
|
|
||||||
Some(dependencies) => dependencies,
|
|
||||||
None => return Ok(index_path_mapping),
|
|
||||||
};
|
|
||||||
for (index, data) in dependencies.iter_mut() {
|
|
||||||
let mut index_extended = "0".repeat(revive_common::BYTE_LENGTH_WORD * 2 - index.len());
|
|
||||||
index_extended.push_str(index.as_str());
|
|
||||||
|
|
||||||
*data = match data {
|
|
||||||
Data::Assembly(assembly) => {
|
|
||||||
let hash = assembly.keccak256();
|
|
||||||
let full_path =
|
|
||||||
hash_data_mapping
|
|
||||||
.get(hash.as_str())
|
|
||||||
.cloned()
|
|
||||||
.ok_or_else(|| {
|
|
||||||
anyhow::anyhow!("Contract path not found for hash `{}`", hash)
|
|
||||||
})?;
|
|
||||||
self.factory_dependencies.insert(full_path.to_owned());
|
|
||||||
|
|
||||||
index_path_mapping.insert(index_extended, full_path.clone());
|
|
||||||
Data::Path(full_path)
|
|
||||||
}
|
|
||||||
Data::Hash(hash) => {
|
|
||||||
index_path_mapping.insert(index_extended, hash.to_owned());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
_ => continue,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(index_path_mapping)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<D> revive_llvm_context::PolkaVMWriteLLVM<D> for Assembly
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
fn declare(
|
|
||||||
&mut self,
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<D>,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
let mut entry = revive_llvm_context::PolkaVMEntryFunction::default();
|
|
||||||
entry.declare(context)?;
|
|
||||||
|
|
||||||
revive_llvm_context::PolkaVMDeployCodeFunction::new(
|
|
||||||
revive_llvm_context::PolkaVMDummyLLVMWritable::default(),
|
|
||||||
)
|
|
||||||
.declare(context)?;
|
|
||||||
revive_llvm_context::PolkaVMRuntimeCodeFunction::new(
|
|
||||||
revive_llvm_context::PolkaVMDummyLLVMWritable::default(),
|
|
||||||
)
|
|
||||||
.declare(context)?;
|
|
||||||
revive_llvm_context::PolkaVMImmutableDataLoadFunction.declare(context)?;
|
|
||||||
|
|
||||||
entry.into_llvm(context)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn into_llvm(
|
|
||||||
mut self,
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<D>,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
let full_path = self.full_path().to_owned();
|
|
||||||
|
|
||||||
context
|
|
||||||
.debug_config()
|
|
||||||
.dump_evmla(full_path.as_str(), self.to_string().as_str())?;
|
|
||||||
|
|
||||||
let deploy_code_blocks = EtherealIR::get_blocks(
|
|
||||||
context.evmla().version.to_owned(),
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Deploy,
|
|
||||||
self.code
|
|
||||||
.as_deref()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Deploy code instructions not found"))?,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let data = self
|
|
||||||
.data
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Runtime code data not found"))?
|
|
||||||
.remove("0")
|
|
||||||
.expect("Always exists");
|
|
||||||
|
|
||||||
context
|
|
||||||
.debug_config()
|
|
||||||
.dump_evmla(full_path.as_str(), data.to_string().as_str())?;
|
|
||||||
|
|
||||||
let runtime_code_instructions = match data {
|
|
||||||
Data::Assembly(assembly) => assembly
|
|
||||||
.code
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Runtime code instructions not found"))?,
|
|
||||||
Data::Hash(hash) => {
|
|
||||||
anyhow::bail!("Expected runtime code instructions, found hash `{}`", hash)
|
|
||||||
}
|
|
||||||
Data::Path(path) => {
|
|
||||||
anyhow::bail!("Expected runtime code instructions, found path `{}`", path)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let runtime_code_blocks = EtherealIR::get_blocks(
|
|
||||||
context.evmla().version.to_owned(),
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Runtime,
|
|
||||||
runtime_code_instructions.as_slice(),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let extra_metadata = self.extra_metadata.take().unwrap_or_default();
|
|
||||||
let mut blocks = deploy_code_blocks;
|
|
||||||
blocks.extend(runtime_code_blocks);
|
|
||||||
let mut ethereal_ir =
|
|
||||||
EtherealIR::new(context.evmla().version.to_owned(), extra_metadata, blocks)?;
|
|
||||||
|
|
||||||
context
|
|
||||||
.debug_config()
|
|
||||||
.dump_ethir(full_path.as_str(), ethereal_ir.to_string().as_str())?;
|
|
||||||
|
|
||||||
ethereal_ir.declare(context)?;
|
|
||||||
ethereal_ir.into_llvm(context)?;
|
|
||||||
|
|
||||||
revive_llvm_context::PolkaVMDeployCodeFunction::new(EntryLink::new(
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Deploy,
|
|
||||||
))
|
|
||||||
.into_llvm(context)?;
|
|
||||||
revive_llvm_context::PolkaVMRuntimeCodeFunction::new(EntryLink::new(
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Runtime,
|
|
||||||
))
|
|
||||||
.into_llvm(context)?;
|
|
||||||
revive_llvm_context::PolkaVMImmutableDataLoadFunction.into_llvm(context)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Assembly {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
if let Some(instructions) = self.code.as_ref() {
|
|
||||||
for (index, instruction) in instructions.iter().enumerate() {
|
|
||||||
match instruction.name {
|
|
||||||
InstructionName::Tag => writeln!(f, "{index:03} {instruction}")?,
|
|
||||||
_ => writeln!(f, "{index:03} {instruction}")?,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
//! The Ethereal IR entry function link.
|
|
||||||
|
|
||||||
use inkwell::values::BasicValue;
|
|
||||||
|
|
||||||
use crate::evmla::ethereal_ir::EtherealIR;
|
|
||||||
|
|
||||||
/// The Ethereal IR entry function link.
|
|
||||||
/// The link represents branching between the deploy and runtime code.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct EntryLink {
|
|
||||||
/// The code part type.
|
|
||||||
pub code_type: revive_llvm_context::PolkaVMCodeType,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EntryLink {
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new(code_type: revive_llvm_context::PolkaVMCodeType) -> Self {
|
|
||||||
Self { code_type }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<D> revive_llvm_context::PolkaVMWriteLLVM<D> for EntryLink
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> {
|
|
||||||
let target = context
|
|
||||||
.get_function(EtherealIR::DEFAULT_ENTRY_FUNCTION_NAME)
|
|
||||||
.expect("Always exists")
|
|
||||||
.borrow()
|
|
||||||
.declaration();
|
|
||||||
let is_deploy_code = match self.code_type {
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Deploy => context
|
|
||||||
.integer_type(revive_common::BIT_LENGTH_BOOLEAN)
|
|
||||||
.const_int(1, false),
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Runtime => context
|
|
||||||
.integer_type(revive_common::BIT_LENGTH_BOOLEAN)
|
|
||||||
.const_int(0, false),
|
|
||||||
};
|
|
||||||
context.build_call(
|
|
||||||
target,
|
|
||||||
&[is_deploy_code.as_basic_value_enum()],
|
|
||||||
format!("call_link_{}", EtherealIR::DEFAULT_ENTRY_FUNCTION_NAME).as_str(),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,37 +0,0 @@
|
|||||||
//! The Ethereal IR block element stack element.
|
|
||||||
|
|
||||||
/// The Ethereal IR block element stack element.
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub enum Element {
|
|
||||||
/// The runtime value.
|
|
||||||
Value(String),
|
|
||||||
/// The compile-time value.
|
|
||||||
Constant(num::BigUint),
|
|
||||||
/// The compile-time destination tag.
|
|
||||||
Tag(num::BigUint),
|
|
||||||
/// The compile-time path.
|
|
||||||
Path(String),
|
|
||||||
/// The compile-time hexadecimal data chunk.
|
|
||||||
Data(String),
|
|
||||||
/// The recursive function return address.
|
|
||||||
ReturnAddress(usize),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Element {
|
|
||||||
pub fn value(identifier: String) -> Self {
|
|
||||||
Self::Value(identifier)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Element {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::Value(identifier) => write!(f, "V_{identifier}"),
|
|
||||||
Self::Constant(value) => write!(f, "{value:X}"),
|
|
||||||
Self::Tag(tag) => write!(f, "T_{tag}"),
|
|
||||||
Self::Path(path) => write!(f, "{path}"),
|
|
||||||
Self::Data(data) => write!(f, "{data}"),
|
|
||||||
Self::ReturnAddress(_) => write!(f, "RETURN_ADDRESS"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
//! The Ethereal IR block element stack.
|
|
||||||
|
|
||||||
pub mod element;
|
|
||||||
|
|
||||||
use self::element::Element;
|
|
||||||
|
|
||||||
/// The Ethereal IR block element stack.
|
|
||||||
#[derive(Debug, Default, Clone)]
|
|
||||||
pub struct Stack {
|
|
||||||
/// The stack elements.
|
|
||||||
pub elements: Vec<Element>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Stack {
|
|
||||||
/// The default stack size.
|
|
||||||
pub const DEFAULT_STACK_SIZE: usize = 16;
|
|
||||||
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
elements: Vec::with_capacity(Self::DEFAULT_STACK_SIZE),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn with_capacity(capacity: usize) -> Self {
|
|
||||||
Self {
|
|
||||||
elements: Vec::with_capacity(capacity),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new_with_elements(elements: Vec<Element>) -> Self {
|
|
||||||
Self { elements }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The stack state hash, which acts as a block identifier.
|
|
||||||
/// Each block clone has its own initial stack state, which uniquely identifies the block.
|
|
||||||
pub fn hash(&self) -> md5::Digest {
|
|
||||||
let mut hash_context = md5::Context::new();
|
|
||||||
for element in self.elements.iter() {
|
|
||||||
match element {
|
|
||||||
Element::Tag(tag) => hash_context.consume(tag.to_bytes_be()),
|
|
||||||
_ => hash_context.consume([0]),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hash_context.compute()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Pushes an element onto the stack.
|
|
||||||
pub fn push(&mut self, element: Element) {
|
|
||||||
self.elements.push(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Appends another stack on top of this one.
|
|
||||||
pub fn append(&mut self, other: &mut Self) {
|
|
||||||
self.elements.append(&mut other.elements);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Pops a stack element.
|
|
||||||
pub fn pop(&mut self) -> anyhow::Result<Element> {
|
|
||||||
self.elements
|
|
||||||
.pop()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Stack underflow"))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Pops the tag from the top.
|
|
||||||
pub fn pop_tag(&mut self) -> anyhow::Result<num::BigUint> {
|
|
||||||
match self.elements.pop() {
|
|
||||||
Some(Element::Tag(tag)) => Ok(tag),
|
|
||||||
Some(element) => anyhow::bail!("Expected tag, found {}", element),
|
|
||||||
None => anyhow::bail!("Stack underflow"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Swaps two stack elements.
|
|
||||||
pub fn swap(&mut self, index: usize) -> anyhow::Result<()> {
|
|
||||||
if self.elements.len() < index + 1 {
|
|
||||||
anyhow::bail!("Stack underflow");
|
|
||||||
}
|
|
||||||
|
|
||||||
let length = self.elements.len();
|
|
||||||
self.elements.swap(length - 1, length - 1 - index);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Duplicates a stack element.
|
|
||||||
pub fn dup(&mut self, index: usize) -> anyhow::Result<Element> {
|
|
||||||
if self.elements.len() < index {
|
|
||||||
anyhow::bail!("Stack underflow");
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(self.elements[self.elements.len() - index].to_owned())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the stack length.
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.elements.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an emptiness flag.
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
self.elements.len() == 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Stack {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"[ {} ]",
|
|
||||||
self.elements
|
|
||||||
.iter()
|
|
||||||
.map(Element::to_string)
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
.join(" | ")
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,156 +0,0 @@
|
|||||||
//! The Ethereal IR block.
|
|
||||||
|
|
||||||
pub mod element;
|
|
||||||
|
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use num::Zero;
|
|
||||||
|
|
||||||
use crate::evmla::assembly::instruction::name::Name as InstructionName;
|
|
||||||
use crate::evmla::assembly::instruction::Instruction;
|
|
||||||
|
|
||||||
use self::element::stack::Stack as ElementStack;
|
|
||||||
use self::element::Element;
|
|
||||||
|
|
||||||
/// The Ethereal IR block.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Block {
|
|
||||||
/// The Solidity compiler version.
|
|
||||||
pub solc_version: semver::Version,
|
|
||||||
/// The block key.
|
|
||||||
pub key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
/// The block instance.
|
|
||||||
pub instance: Option<usize>,
|
|
||||||
/// The block elements relevant to the stack consistency.
|
|
||||||
pub elements: Vec<Element>,
|
|
||||||
/// The block predecessors.
|
|
||||||
pub predecessors: HashSet<(revive_llvm_context::PolkaVMFunctionBlockKey, usize)>,
|
|
||||||
/// The initial stack state.
|
|
||||||
pub initial_stack: ElementStack,
|
|
||||||
/// The stack.
|
|
||||||
pub stack: ElementStack,
|
|
||||||
/// The extra block hashes for alternative routes.
|
|
||||||
pub extra_hashes: Vec<md5::Digest>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Block {
|
|
||||||
/// The elements vector initial capacity.
|
|
||||||
pub const ELEMENTS_VECTOR_DEFAULT_CAPACITY: usize = 64;
|
|
||||||
/// The predecessors hashset initial capacity.
|
|
||||||
pub const PREDECESSORS_HASHSET_DEFAULT_CAPACITY: usize = 4;
|
|
||||||
|
|
||||||
/// Assembles a block from the sequence of instructions.
|
|
||||||
pub fn try_from_instructions(
|
|
||||||
solc_version: semver::Version,
|
|
||||||
code_type: revive_llvm_context::PolkaVMCodeType,
|
|
||||||
slice: &[Instruction],
|
|
||||||
) -> anyhow::Result<(Self, usize)> {
|
|
||||||
let mut cursor = 0;
|
|
||||||
|
|
||||||
let tag: num::BigUint = match slice[cursor].name {
|
|
||||||
InstructionName::Tag => {
|
|
||||||
let tag = slice[cursor]
|
|
||||||
.value
|
|
||||||
.as_deref()
|
|
||||||
.expect("Always exists")
|
|
||||||
.parse()
|
|
||||||
.expect("Always valid");
|
|
||||||
cursor += 1;
|
|
||||||
tag
|
|
||||||
}
|
|
||||||
_ => num::BigUint::zero(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut block = Self {
|
|
||||||
solc_version: solc_version.clone(),
|
|
||||||
key: revive_llvm_context::PolkaVMFunctionBlockKey::new(code_type, tag),
|
|
||||||
instance: None,
|
|
||||||
elements: Vec::with_capacity(Self::ELEMENTS_VECTOR_DEFAULT_CAPACITY),
|
|
||||||
predecessors: HashSet::with_capacity(Self::PREDECESSORS_HASHSET_DEFAULT_CAPACITY),
|
|
||||||
initial_stack: ElementStack::new(),
|
|
||||||
stack: ElementStack::new(),
|
|
||||||
extra_hashes: vec![],
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut dead_code = false;
|
|
||||||
while cursor < slice.len() {
|
|
||||||
if !dead_code {
|
|
||||||
let element: Element = Element::new(solc_version.clone(), slice[cursor].to_owned());
|
|
||||||
block.elements.push(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
match slice[cursor].name {
|
|
||||||
InstructionName::RETURN
|
|
||||||
| InstructionName::REVERT
|
|
||||||
| InstructionName::STOP
|
|
||||||
| InstructionName::INVALID => {
|
|
||||||
cursor += 1;
|
|
||||||
dead_code = true;
|
|
||||||
}
|
|
||||||
InstructionName::JUMP => {
|
|
||||||
cursor += 1;
|
|
||||||
dead_code = true;
|
|
||||||
}
|
|
||||||
InstructionName::Tag => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
cursor += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((block, cursor))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Inserts a predecessor tag.
|
|
||||||
pub fn insert_predecessor(
|
|
||||||
&mut self,
|
|
||||||
key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
instance: usize,
|
|
||||||
) {
|
|
||||||
self.predecessors.insert((key, instance));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<D> revive_llvm_context::PolkaVMWriteLLVM<D> for Block
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> {
|
|
||||||
context.set_code_type(self.key.code_type);
|
|
||||||
|
|
||||||
for element in self.elements.into_iter() {
|
|
||||||
element.into_llvm(context)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Block {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
writeln!(
|
|
||||||
f,
|
|
||||||
"block_{}/{}: {}",
|
|
||||||
self.key,
|
|
||||||
self.instance.unwrap_or_default(),
|
|
||||||
if self.predecessors.is_empty() {
|
|
||||||
"".to_owned()
|
|
||||||
} else {
|
|
||||||
format!(
|
|
||||||
"(predecessors: {})",
|
|
||||||
self.predecessors
|
|
||||||
.iter()
|
|
||||||
.map(|(key, instance)| format!("{}/{}", key, instance))
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
.join(", ")
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
for element in self.elements.iter() {
|
|
||||||
writeln!(f, " {element}")?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,29 +0,0 @@
|
|||||||
//! The Ethereal IR block queue element.
|
|
||||||
|
|
||||||
use crate::evmla::ethereal_ir::function::block::element::stack::Stack;
|
|
||||||
|
|
||||||
/// The Ethereal IR block queue element.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct QueueElement {
|
|
||||||
/// The block key.
|
|
||||||
pub block_key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
/// The block predecessor.
|
|
||||||
pub predecessor: Option<(revive_llvm_context::PolkaVMFunctionBlockKey, usize)>,
|
|
||||||
/// The predecessor's last stack state.
|
|
||||||
pub stack: Stack,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl QueueElement {
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new(
|
|
||||||
block_key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
predecessor: Option<(revive_llvm_context::PolkaVMFunctionBlockKey, usize)>,
|
|
||||||
stack: Stack,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
block_key,
|
|
||||||
predecessor,
|
|
||||||
stack,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
//! The Ethereal IR function type.
|
|
||||||
|
|
||||||
/// The Ethereal IR function type.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum Type {
|
|
||||||
/// The initial function, combining deploy and runtime code.
|
|
||||||
Initial,
|
|
||||||
/// The recursive function with a specific block starting its recursive context.
|
|
||||||
Recursive {
|
|
||||||
/// The function name.
|
|
||||||
name: String,
|
|
||||||
/// The function initial block key.
|
|
||||||
block_key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
/// The size of stack input (in cells or 256-bit words).
|
|
||||||
input_size: usize,
|
|
||||||
/// The size of stack output (in cells or 256-bit words).
|
|
||||||
output_size: usize,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Type {
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new_initial() -> Self {
|
|
||||||
Self::Initial
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new_recursive(
|
|
||||||
name: String,
|
|
||||||
block_key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
input_size: usize,
|
|
||||||
output_size: usize,
|
|
||||||
) -> Self {
|
|
||||||
Self::Recursive {
|
|
||||||
name,
|
|
||||||
block_key,
|
|
||||||
input_size,
|
|
||||||
output_size,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
//! The Ethereal IR block visited element.
|
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
|
||||||
|
|
||||||
/// The Ethereal IR block visited element.
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
||||||
pub struct VisitedElement {
|
|
||||||
/// The block key.
|
|
||||||
pub block_key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
/// The initial stack state hash.
|
|
||||||
pub stack_hash: md5::Digest,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl VisitedElement {
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new(
|
|
||||||
block_key: revive_llvm_context::PolkaVMFunctionBlockKey,
|
|
||||||
stack_hash: md5::Digest,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
block_key,
|
|
||||||
stack_hash,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for VisitedElement {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for VisitedElement {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
match (self.block_key.code_type, other.block_key.code_type) {
|
|
||||||
(
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Deploy,
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Runtime,
|
|
||||||
) => Ordering::Less,
|
|
||||||
(
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Runtime,
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Deploy,
|
|
||||||
) => Ordering::Greater,
|
|
||||||
(
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Deploy,
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Deploy,
|
|
||||||
)
|
|
||||||
| (
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Runtime,
|
|
||||||
revive_llvm_context::PolkaVMCodeType::Runtime,
|
|
||||||
) => {
|
|
||||||
let tag_comparison = self.block_key.tag.cmp(&other.block_key.tag);
|
|
||||||
if tag_comparison == Ordering::Equal {
|
|
||||||
if self.stack_hash == other.stack_hash {
|
|
||||||
Ordering::Equal
|
|
||||||
} else {
|
|
||||||
Ordering::Less
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tag_comparison
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
//! The Ethereal IR of the EVM bytecode.
|
|
||||||
|
|
||||||
pub mod entry_link;
|
|
||||||
pub mod function;
|
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
use std::collections::BTreeSet;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use crate::evmla::assembly::instruction::Instruction;
|
|
||||||
use crate::solc::standard_json::output::contract::evm::extra_metadata::ExtraMetadata;
|
|
||||||
|
|
||||||
use self::function::block::Block;
|
|
||||||
use self::function::r#type::Type as FunctionType;
|
|
||||||
use self::function::Function;
|
|
||||||
|
|
||||||
/// The Ethereal IR of the EVM bytecode.
|
|
||||||
/// The Ethereal IR (EthIR) is a special IR between the EVM legacy assembly and LLVM IR. It is
|
|
||||||
/// created to facilitate the translation and provide an additional environment for applying some
|
|
||||||
/// transformations, duplicating parts of the call and control flow graphs, tracking the
|
|
||||||
/// data flow, and a few more algorithms of static analysis.
|
|
||||||
/// The most important feature of EthIR is flattening the block tags and duplicating blocks for
|
|
||||||
/// each of initial states of the stack. The LLVM IR supports only static control flow, so the
|
|
||||||
/// stack state must be known all the way throughout the program.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct EtherealIR {
|
|
||||||
/// The Solidity compiler version.
|
|
||||||
pub solc_version: semver::Version,
|
|
||||||
/// The EVMLA extra metadata.
|
|
||||||
pub extra_metadata: ExtraMetadata,
|
|
||||||
/// The all-inlined function.
|
|
||||||
pub entry_function: Function,
|
|
||||||
/// The recursive functions.
|
|
||||||
pub recursive_functions: BTreeMap<revive_llvm_context::PolkaVMFunctionBlockKey, Function>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EtherealIR {
|
|
||||||
/// The default entry function name.
|
|
||||||
pub const DEFAULT_ENTRY_FUNCTION_NAME: &'static str = "main";
|
|
||||||
|
|
||||||
/// The blocks hashmap initial capacity.
|
|
||||||
pub const BLOCKS_HASHMAP_DEFAULT_CAPACITY: usize = 64;
|
|
||||||
|
|
||||||
/// Assembles a sequence of functions from the sequence of instructions.
|
|
||||||
pub fn new(
|
|
||||||
solc_version: semver::Version,
|
|
||||||
extra_metadata: ExtraMetadata,
|
|
||||||
blocks: HashMap<revive_llvm_context::PolkaVMFunctionBlockKey, Block>,
|
|
||||||
) -> anyhow::Result<Self> {
|
|
||||||
let mut entry_function = Function::new(solc_version.clone(), FunctionType::new_initial());
|
|
||||||
let mut recursive_functions = BTreeMap::new();
|
|
||||||
let mut visited_functions = BTreeSet::new();
|
|
||||||
entry_function.traverse(
|
|
||||||
&blocks,
|
|
||||||
&mut recursive_functions,
|
|
||||||
&extra_metadata,
|
|
||||||
&mut visited_functions,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
solc_version,
|
|
||||||
extra_metadata,
|
|
||||||
entry_function,
|
|
||||||
recursive_functions,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets blocks for the specified type of the contract code.
|
|
||||||
pub fn get_blocks(
|
|
||||||
solc_version: semver::Version,
|
|
||||||
code_type: revive_llvm_context::PolkaVMCodeType,
|
|
||||||
instructions: &[Instruction],
|
|
||||||
) -> anyhow::Result<HashMap<revive_llvm_context::PolkaVMFunctionBlockKey, Block>> {
|
|
||||||
let mut blocks = HashMap::with_capacity(Self::BLOCKS_HASHMAP_DEFAULT_CAPACITY);
|
|
||||||
let mut offset = 0;
|
|
||||||
|
|
||||||
while offset < instructions.len() {
|
|
||||||
let (block, size) = Block::try_from_instructions(
|
|
||||||
solc_version.clone(),
|
|
||||||
code_type,
|
|
||||||
&instructions[offset..],
|
|
||||||
)?;
|
|
||||||
blocks.insert(
|
|
||||||
revive_llvm_context::PolkaVMFunctionBlockKey::new(code_type, block.key.tag.clone()),
|
|
||||||
block,
|
|
||||||
);
|
|
||||||
offset += size;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(blocks)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<D> revive_llvm_context::PolkaVMWriteLLVM<D> for EtherealIR
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
fn declare(
|
|
||||||
&mut self,
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<D>,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
self.entry_function.declare(context)?;
|
|
||||||
|
|
||||||
for (_key, function) in self.recursive_functions.iter_mut() {
|
|
||||||
function.declare(context)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> {
|
|
||||||
context.evmla_mut().stack = vec![];
|
|
||||||
|
|
||||||
self.entry_function.into_llvm(context)?;
|
|
||||||
|
|
||||||
for (_key, function) in self.recursive_functions.into_iter() {
|
|
||||||
function.into_llvm(context)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for EtherealIR {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
writeln!(f, "{}", self.entry_function)?;
|
|
||||||
|
|
||||||
for (_key, function) in self.recursive_functions.iter() {
|
|
||||||
writeln!(f, "{}", function)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
//! The EVM legacy assembly compiling tools.
|
|
||||||
|
|
||||||
pub mod assembly;
|
|
||||||
pub mod ethereal_ir;
|
|
||||||
+13
-43
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
pub(crate) mod build;
|
pub(crate) mod build;
|
||||||
pub(crate) mod r#const;
|
pub(crate) mod r#const;
|
||||||
pub(crate) mod evmla;
|
|
||||||
pub(crate) mod missing_libraries;
|
pub(crate) mod missing_libraries;
|
||||||
pub(crate) mod process;
|
pub(crate) mod process;
|
||||||
pub(crate) mod project;
|
pub(crate) mod project;
|
||||||
@@ -26,7 +25,6 @@ pub use self::project::Project;
|
|||||||
pub use self::r#const::*;
|
pub use self::r#const::*;
|
||||||
pub use self::solc::combined_json::contract::Contract as SolcCombinedJsonContract;
|
pub use self::solc::combined_json::contract::Contract as SolcCombinedJsonContract;
|
||||||
pub use self::solc::combined_json::CombinedJson as SolcCombinedJson;
|
pub use self::solc::combined_json::CombinedJson as SolcCombinedJson;
|
||||||
pub use self::solc::pipeline::Pipeline as SolcPipeline;
|
|
||||||
#[cfg(not(target_os = "emscripten"))]
|
#[cfg(not(target_os = "emscripten"))]
|
||||||
pub use self::solc::solc_compiler::SolcCompiler;
|
pub use self::solc::solc_compiler::SolcCompiler;
|
||||||
#[cfg(target_os = "emscripten")]
|
#[cfg(target_os = "emscripten")]
|
||||||
@@ -53,6 +51,7 @@ pub mod test_utils;
|
|||||||
pub mod tests;
|
pub mod tests;
|
||||||
|
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
|
use std::io::Write;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
/// Runs the Yul mode.
|
/// Runs the Yul mode.
|
||||||
@@ -119,7 +118,6 @@ pub fn standard_output<T: Compiler>(
|
|||||||
evm_version: Option<revive_common::EVMVersion>,
|
evm_version: Option<revive_common::EVMVersion>,
|
||||||
solc_optimizer_enabled: bool,
|
solc_optimizer_enabled: bool,
|
||||||
optimizer_settings: revive_llvm_context::OptimizerSettings,
|
optimizer_settings: revive_llvm_context::OptimizerSettings,
|
||||||
force_evmla: bool,
|
|
||||||
include_metadata_hash: bool,
|
include_metadata_hash: bool,
|
||||||
base_path: Option<String>,
|
base_path: Option<String>,
|
||||||
include_paths: Vec<String>,
|
include_paths: Vec<String>,
|
||||||
@@ -129,7 +127,6 @@ pub fn standard_output<T: Compiler>(
|
|||||||
debug_config: revive_llvm_context::DebugConfig,
|
debug_config: revive_llvm_context::DebugConfig,
|
||||||
) -> anyhow::Result<Build> {
|
) -> anyhow::Result<Build> {
|
||||||
let solc_version = solc.version()?;
|
let solc_version = solc.version()?;
|
||||||
let solc_pipeline = SolcPipeline::new(&solc_version, force_evmla);
|
|
||||||
|
|
||||||
let solc_input = SolcStandardJsonInput::try_from_paths(
|
let solc_input = SolcStandardJsonInput::try_from_paths(
|
||||||
SolcStandardJsonInputLanguage::Solidity,
|
SolcStandardJsonInputLanguage::Solidity,
|
||||||
@@ -137,7 +134,7 @@ pub fn standard_output<T: Compiler>(
|
|||||||
input_files,
|
input_files,
|
||||||
libraries,
|
libraries,
|
||||||
remappings,
|
remappings,
|
||||||
SolcStandardJsonInputSettingsSelection::new_required(solc_pipeline),
|
SolcStandardJsonInputSettingsSelection::new_required(),
|
||||||
SolcStandardJsonInputSettingsOptimizer::new(
|
SolcStandardJsonInputSettingsOptimizer::new(
|
||||||
solc_optimizer_enabled,
|
solc_optimizer_enabled,
|
||||||
None,
|
None,
|
||||||
@@ -145,7 +142,6 @@ pub fn standard_output<T: Compiler>(
|
|||||||
optimizer_settings.is_fallback_to_size_enabled(),
|
optimizer_settings.is_fallback_to_size_enabled(),
|
||||||
),
|
),
|
||||||
None,
|
None,
|
||||||
solc_pipeline == SolcPipeline::Yul,
|
|
||||||
suppressed_warnings,
|
suppressed_warnings,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
@@ -156,13 +152,7 @@ pub fn standard_output<T: Compiler>(
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let libraries = solc_input.settings.libraries.clone().unwrap_or_default();
|
let libraries = solc_input.settings.libraries.clone().unwrap_or_default();
|
||||||
let mut solc_output = solc.standard_json(
|
let mut solc_output = solc.standard_json(solc_input, base_path, include_paths, allow_paths)?;
|
||||||
solc_input,
|
|
||||||
solc_pipeline,
|
|
||||||
base_path,
|
|
||||||
include_paths,
|
|
||||||
allow_paths,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
if let Some(errors) = solc_output.errors.as_deref() {
|
if let Some(errors) = solc_output.errors.as_deref() {
|
||||||
let mut has_errors = false;
|
let mut has_errors = false;
|
||||||
@@ -172,7 +162,7 @@ pub fn standard_output<T: Compiler>(
|
|||||||
has_errors = true;
|
has_errors = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("{error}");
|
writeln!(std::io::stderr(), "{error}")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if has_errors {
|
if has_errors {
|
||||||
@@ -180,13 +170,8 @@ pub fn standard_output<T: Compiler>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let project = solc_output.try_to_project(
|
let project =
|
||||||
source_code_files,
|
solc_output.try_to_project(source_code_files, libraries, &solc_version, &debug_config)?;
|
||||||
libraries,
|
|
||||||
solc_pipeline,
|
|
||||||
&solc_version,
|
|
||||||
&debug_config,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let build = project.compile(optimizer_settings, include_metadata_hash, debug_config)?;
|
let build = project.compile(optimizer_settings, include_metadata_hash, debug_config)?;
|
||||||
|
|
||||||
@@ -194,20 +179,17 @@ pub fn standard_output<T: Compiler>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Runs the standard JSON mode.
|
/// Runs the standard JSON mode.
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
pub fn standard_json<T: Compiler>(
|
pub fn standard_json<T: Compiler>(
|
||||||
solc: &mut T,
|
solc: &mut T,
|
||||||
detect_missing_libraries: bool,
|
detect_missing_libraries: bool,
|
||||||
force_evmla: bool,
|
|
||||||
base_path: Option<String>,
|
base_path: Option<String>,
|
||||||
include_paths: Vec<String>,
|
include_paths: Vec<String>,
|
||||||
allow_paths: Option<String>,
|
allow_paths: Option<String>,
|
||||||
debug_config: revive_llvm_context::DebugConfig,
|
debug_config: revive_llvm_context::DebugConfig,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let solc_version = solc.version()?;
|
let solc_version = solc.version()?;
|
||||||
let solc_pipeline = SolcPipeline::new(&solc_version, force_evmla);
|
|
||||||
|
|
||||||
let solc_input = SolcStandardJsonInput::try_from_stdin(solc_pipeline)?;
|
let solc_input = SolcStandardJsonInput::try_from_stdin()?;
|
||||||
let source_code_files = solc_input
|
let source_code_files = solc_input
|
||||||
.sources
|
.sources
|
||||||
.iter()
|
.iter()
|
||||||
@@ -225,13 +207,7 @@ pub fn standard_json<T: Compiler>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let libraries = solc_input.settings.libraries.clone().unwrap_or_default();
|
let libraries = solc_input.settings.libraries.clone().unwrap_or_default();
|
||||||
let mut solc_output = solc.standard_json(
|
let mut solc_output = solc.standard_json(solc_input, base_path, include_paths, allow_paths)?;
|
||||||
solc_input,
|
|
||||||
solc_pipeline,
|
|
||||||
base_path,
|
|
||||||
include_paths,
|
|
||||||
allow_paths,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
if let Some(errors) = solc_output.errors.as_deref() {
|
if let Some(errors) = solc_output.errors.as_deref() {
|
||||||
for error in errors.iter() {
|
for error in errors.iter() {
|
||||||
@@ -242,13 +218,8 @@ pub fn standard_json<T: Compiler>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let project = solc_output.try_to_project(
|
let project =
|
||||||
source_code_files,
|
solc_output.try_to_project(source_code_files, libraries, &solc_version, &debug_config)?;
|
||||||
libraries,
|
|
||||||
solc_pipeline,
|
|
||||||
&solc_version,
|
|
||||||
&debug_config,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
if detect_missing_libraries {
|
if detect_missing_libraries {
|
||||||
let missing_libraries = project.get_missing_libraries();
|
let missing_libraries = project.get_missing_libraries();
|
||||||
@@ -271,7 +242,6 @@ pub fn combined_json<T: Compiler>(
|
|||||||
evm_version: Option<revive_common::EVMVersion>,
|
evm_version: Option<revive_common::EVMVersion>,
|
||||||
solc_optimizer_enabled: bool,
|
solc_optimizer_enabled: bool,
|
||||||
optimizer_settings: revive_llvm_context::OptimizerSettings,
|
optimizer_settings: revive_llvm_context::OptimizerSettings,
|
||||||
force_evmla: bool,
|
|
||||||
include_metadata_hash: bool,
|
include_metadata_hash: bool,
|
||||||
base_path: Option<String>,
|
base_path: Option<String>,
|
||||||
include_paths: Vec<String>,
|
include_paths: Vec<String>,
|
||||||
@@ -289,7 +259,6 @@ pub fn combined_json<T: Compiler>(
|
|||||||
evm_version,
|
evm_version,
|
||||||
solc_optimizer_enabled,
|
solc_optimizer_enabled,
|
||||||
optimizer_settings,
|
optimizer_settings,
|
||||||
force_evmla,
|
|
||||||
include_metadata_hash,
|
include_metadata_hash,
|
||||||
base_path,
|
base_path,
|
||||||
include_paths,
|
include_paths,
|
||||||
@@ -309,10 +278,11 @@ pub fn combined_json<T: Compiler>(
|
|||||||
combined_json.write_to_directory(output_directory.as_path(), overwrite)?;
|
combined_json.write_to_directory(output_directory.as_path(), overwrite)?;
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
println!(
|
writeln!(
|
||||||
|
std::io::stdout(),
|
||||||
"{}",
|
"{}",
|
||||||
serde_json::to_string(&combined_json).expect("Always valid")
|
serde_json::to_string(&combined_json).expect("Always valid")
|
||||||
);
|
)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
//! The contract EVM legacy assembly source code.
|
|
||||||
|
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
use crate::evmla::assembly::Assembly;
|
|
||||||
use crate::solc::standard_json::output::contract::evm::extra_metadata::ExtraMetadata;
|
|
||||||
|
|
||||||
/// The contract EVM legacy assembly source code.
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
#[allow(clippy::upper_case_acronyms)]
|
|
||||||
pub struct EVMLA {
|
|
||||||
/// The EVM legacy assembly source code.
|
|
||||||
pub assembly: Assembly,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EVMLA {
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new(mut assembly: Assembly, extra_metadata: ExtraMetadata) -> Self {
|
|
||||||
assembly.extra_metadata = Some(extra_metadata);
|
|
||||||
Self { assembly }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the list of missing deployable libraries.
|
|
||||||
pub fn get_missing_libraries(&self) -> HashSet<String> {
|
|
||||||
self.assembly.get_missing_libraries()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<D> revive_llvm_context::PolkaVMWriteLLVM<D> for EVMLA
|
|
||||||
where
|
|
||||||
D: revive_llvm_context::PolkaVMDependency + Clone,
|
|
||||||
{
|
|
||||||
fn declare(
|
|
||||||
&mut self,
|
|
||||||
context: &mut revive_llvm_context::PolkaVMContext<D>,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
self.assembly.declare(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> {
|
|
||||||
self.assembly.into_llvm(context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
//! The contract source code.
|
//! The contract source code.
|
||||||
|
|
||||||
pub mod evmla;
|
|
||||||
pub mod llvm_ir;
|
pub mod llvm_ir;
|
||||||
pub mod yul;
|
pub mod yul;
|
||||||
|
|
||||||
@@ -9,24 +8,17 @@ use std::collections::HashSet;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::evmla::assembly::Assembly;
|
|
||||||
use crate::solc::standard_json::output::contract::evm::extra_metadata::ExtraMetadata;
|
|
||||||
use crate::yul::parser::statement::object::Object;
|
use crate::yul::parser::statement::object::Object;
|
||||||
|
|
||||||
use self::evmla::EVMLA;
|
|
||||||
use self::llvm_ir::LLVMIR;
|
use self::llvm_ir::LLVMIR;
|
||||||
use self::yul::Yul;
|
use self::yul::Yul;
|
||||||
|
|
||||||
/// The contract source code.
|
/// The contract source code.
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
#[allow(clippy::upper_case_acronyms)]
|
#[allow(clippy::upper_case_acronyms)]
|
||||||
#[allow(clippy::enum_variant_names)]
|
|
||||||
pub enum IR {
|
pub enum IR {
|
||||||
/// The Yul source code.
|
/// The Yul source code.
|
||||||
Yul(Yul),
|
Yul(Yul),
|
||||||
/// The EVM legacy assembly source code.
|
|
||||||
EVMLA(EVMLA),
|
|
||||||
/// The LLVM IR source code.
|
/// The LLVM IR source code.
|
||||||
LLVMIR(LLVMIR),
|
LLVMIR(LLVMIR),
|
||||||
}
|
}
|
||||||
@@ -37,11 +29,6 @@ impl IR {
|
|||||||
Self::Yul(Yul::new(source_code, object))
|
Self::Yul(Yul::new(source_code, object))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new_evmla(assembly: Assembly, extra_metadata: ExtraMetadata) -> Self {
|
|
||||||
Self::EVMLA(EVMLA::new(assembly, extra_metadata))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A shortcut constructor.
|
/// A shortcut constructor.
|
||||||
pub fn new_llvm_ir(path: String, source: String) -> Self {
|
pub fn new_llvm_ir(path: String, source: String) -> Self {
|
||||||
Self::LLVMIR(LLVMIR::new(path, source))
|
Self::LLVMIR(LLVMIR::new(path, source))
|
||||||
@@ -51,7 +38,6 @@ impl IR {
|
|||||||
pub fn get_missing_libraries(&self) -> HashSet<String> {
|
pub fn get_missing_libraries(&self) -> HashSet<String> {
|
||||||
match self {
|
match self {
|
||||||
Self::Yul(inner) => inner.get_missing_libraries(),
|
Self::Yul(inner) => inner.get_missing_libraries(),
|
||||||
Self::EVMLA(inner) => inner.get_missing_libraries(),
|
|
||||||
Self::LLVMIR(_inner) => HashSet::new(),
|
Self::LLVMIR(_inner) => HashSet::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,7 +53,6 @@ where
|
|||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
match self {
|
match self {
|
||||||
Self::Yul(inner) => inner.declare(context),
|
Self::Yul(inner) => inner.declare(context),
|
||||||
Self::EVMLA(inner) => inner.declare(context),
|
|
||||||
Self::LLVMIR(_inner) => Ok(()),
|
Self::LLVMIR(_inner) => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,7 +60,6 @@ where
|
|||||||
fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> {
|
fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> {
|
||||||
match self {
|
match self {
|
||||||
Self::Yul(inner) => inner.into_llvm(context),
|
Self::Yul(inner) => inner.into_llvm(context),
|
||||||
Self::EVMLA(inner) => inner.into_llvm(context),
|
|
||||||
Self::LLVMIR(_inner) => Ok(()),
|
Self::LLVMIR(_inner) => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,12 +54,10 @@ impl Contract {
|
|||||||
|
|
||||||
/// Returns the contract identifier, which is:
|
/// Returns the contract identifier, which is:
|
||||||
/// - the Yul object identifier for Yul
|
/// - the Yul object identifier for Yul
|
||||||
/// - the full contract path for EVM legacy assembly
|
|
||||||
/// - the module name for LLVM IR
|
/// - the module name for LLVM IR
|
||||||
pub fn identifier(&self) -> &str {
|
pub fn identifier(&self) -> &str {
|
||||||
match self.ir {
|
match self.ir {
|
||||||
IR::Yul(ref yul) => yul.object.identifier.as_str(),
|
IR::Yul(ref yul) => yul.object.identifier.as_str(),
|
||||||
IR::EVMLA(ref evm) => evm.assembly.full_path(),
|
|
||||||
IR::LLVMIR(ref llvm_ir) => llvm_ir.path.as_str(),
|
IR::LLVMIR(ref llvm_ir) => llvm_ir.path.as_str(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,7 +66,6 @@ impl Contract {
|
|||||||
pub fn drain_factory_dependencies(&mut self) -> HashSet<String> {
|
pub fn drain_factory_dependencies(&mut self) -> HashSet<String> {
|
||||||
match self.ir {
|
match self.ir {
|
||||||
IR::Yul(ref mut yul) => yul.object.factory_dependencies.drain().collect(),
|
IR::Yul(ref mut yul) => yul.object.factory_dependencies.drain().collect(),
|
||||||
IR::EVMLA(ref mut evm) => evm.assembly.factory_dependencies.drain().collect(),
|
|
||||||
IR::LLVMIR(_) => HashSet::new(),
|
IR::LLVMIR(_) => HashSet::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,10 +126,6 @@ impl Contract {
|
|||||||
IR::Yul(_) => {
|
IR::Yul(_) => {
|
||||||
context.set_yul_data(Default::default());
|
context.set_yul_data(Default::default());
|
||||||
}
|
}
|
||||||
IR::EVMLA(_) => {
|
|
||||||
let evmla_data = revive_llvm_context::PolkaVMContextEVMLAData::new(version.default);
|
|
||||||
context.set_evmla_data(evmla_data);
|
|
||||||
}
|
|
||||||
IR::LLVMIR(_) => {}
|
IR::LLVMIR(_) => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,14 +13,14 @@ use path_slash::PathExt;
|
|||||||
/// output directory.
|
/// output directory.
|
||||||
/// Example: resolc ERC20.sol -O3 --bin --output-dir './build/'
|
/// Example: resolc ERC20.sol -O3 --bin --output-dir './build/'
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
#[structopt(name = "The PolkaVM Solidity compiler")]
|
#[command(name = "The PolkaVM Solidity compiler", arg_required_else_help = true)]
|
||||||
pub struct Arguments {
|
pub struct Arguments {
|
||||||
/// Print the version and exit.
|
/// Print the version and exit.
|
||||||
#[structopt(long = "version")]
|
#[arg(long = "version")]
|
||||||
pub version: bool,
|
pub version: bool,
|
||||||
|
|
||||||
/// Print the licence and exit.
|
/// Print the licence and exit.
|
||||||
#[structopt(long = "license")]
|
#[arg(long = "license")]
|
||||||
pub license: bool,
|
pub license: bool,
|
||||||
|
|
||||||
/// Specify the input paths and remappings.
|
/// Specify the input paths and remappings.
|
||||||
@@ -31,159 +31,141 @@ pub struct Arguments {
|
|||||||
|
|
||||||
/// Set the given path as the root of the source tree instead of the root of the filesystem.
|
/// Set the given path as the root of the source tree instead of the root of the filesystem.
|
||||||
/// Passed to `solc` without changes.
|
/// Passed to `solc` without changes.
|
||||||
#[structopt(long = "base-path")]
|
#[arg(long = "base-path")]
|
||||||
pub base_path: Option<String>,
|
pub base_path: Option<String>,
|
||||||
|
|
||||||
/// Make an additional source directory available to the default import callback.
|
/// Make an additional source directory available to the default import callback.
|
||||||
/// Can be used multiple times. Can only be used if the base path has a non-empty value.
|
/// Can be used multiple times. Can only be used if the base path has a non-empty value.
|
||||||
/// Passed to `solc` without changes.
|
/// Passed to `solc` without changes.
|
||||||
#[structopt(long = "include-path")]
|
#[arg(long = "include-path")]
|
||||||
pub include_paths: Vec<String>,
|
pub include_paths: Vec<String>,
|
||||||
|
|
||||||
/// Allow a given path for imports. A list of paths can be supplied by separating them with a comma.
|
/// Allow a given path for imports. A list of paths can be supplied by separating them with a comma.
|
||||||
/// Passed to `solc` without changes.
|
/// Passed to `solc` without changes.
|
||||||
#[structopt(long = "allow-paths")]
|
#[arg(long = "allow-paths")]
|
||||||
pub allow_paths: Option<String>,
|
pub allow_paths: Option<String>,
|
||||||
|
|
||||||
/// Create one file per component and contract/file at the specified directory, if given.
|
/// Create one file per component and contract/file at the specified directory, if given.
|
||||||
#[structopt(short = 'o', long = "output-dir")]
|
#[arg(short = 'o', long = "output-dir")]
|
||||||
pub output_directory: Option<PathBuf>,
|
pub output_directory: Option<PathBuf>,
|
||||||
|
|
||||||
/// Overwrite existing files (used together with -o).
|
/// Overwrite existing files (used together with -o).
|
||||||
#[structopt(long = "overwrite")]
|
#[arg(long = "overwrite")]
|
||||||
pub overwrite: bool,
|
pub overwrite: bool,
|
||||||
|
|
||||||
/// Set the optimization parameter -O[0 | 1 | 2 | 3 | s | z].
|
/// Set the optimization parameter -O[0 | 1 | 2 | 3 | s | z].
|
||||||
/// Use `3` for best performance and `z` for minimal size.
|
/// Use `3` for best performance and `z` for minimal size.
|
||||||
#[structopt(short = 'O', long = "optimization")]
|
#[arg(short = 'O', long = "optimization")]
|
||||||
pub optimization: Option<char>,
|
pub optimization: Option<char>,
|
||||||
|
|
||||||
/// Try to recompile with -Oz if the bytecode is too large.
|
/// Try to recompile with -Oz if the bytecode is too large.
|
||||||
#[structopt(long = "fallback-Oz")]
|
#[arg(long = "fallback-Oz")]
|
||||||
pub fallback_to_optimizing_for_size: bool,
|
pub fallback_to_optimizing_for_size: bool,
|
||||||
|
|
||||||
/// Disable the `solc` optimizer.
|
/// Disable the `solc` optimizer.
|
||||||
/// Use it if your project uses the `MSIZE` instruction, or in other cases.
|
/// Use it if your project uses the `MSIZE` instruction, or in other cases.
|
||||||
/// Beware that it will prevent libraries from being inlined.
|
/// Beware that it will prevent libraries from being inlined.
|
||||||
#[structopt(long = "disable-solc-optimizer")]
|
#[arg(long = "disable-solc-optimizer")]
|
||||||
pub disable_solc_optimizer: bool,
|
pub disable_solc_optimizer: bool,
|
||||||
|
|
||||||
/// Specify the path to the `solc` executable. By default, the one in `${PATH}` is used.
|
/// Specify the path to the `solc` executable. By default, the one in `${PATH}` is used.
|
||||||
/// Yul mode: `solc` is used for source code validation, as `resolc` itself assumes that the input Yul is valid.
|
/// Yul mode: `solc` is used for source code validation, as `resolc` itself assumes that the input Yul is valid.
|
||||||
/// LLVM IR mode: `solc` is unused.
|
/// LLVM IR mode: `solc` is unused.
|
||||||
#[structopt(long = "solc")]
|
#[arg(long = "solc")]
|
||||||
pub solc: Option<String>,
|
pub solc: Option<String>,
|
||||||
|
|
||||||
/// The EVM target version to generate IR for.
|
/// The EVM target version to generate IR for.
|
||||||
/// See https://github.com/paritytech/revive/blob/main/crates/common/src/evm_version.rs for reference.
|
/// See https://github.com/paritytech/revive/blob/main/crates/common/src/evm_version.rs for reference.
|
||||||
#[structopt(long = "evm-version")]
|
#[arg(long = "evm-version")]
|
||||||
pub evm_version: Option<String>,
|
pub evm_version: Option<String>,
|
||||||
|
|
||||||
/// Specify addresses of deployable libraries. Syntax: `<libraryName>=<address> [, or whitespace] ...`.
|
/// Specify addresses of deployable libraries. Syntax: `<libraryName>=<address> [, or whitespace] ...`.
|
||||||
/// Addresses are interpreted as hexadecimal strings prefixed with `0x`.
|
/// Addresses are interpreted as hexadecimal strings prefixed with `0x`.
|
||||||
#[structopt(short = 'l', long = "libraries")]
|
#[arg(short = 'l', long = "libraries")]
|
||||||
pub libraries: Vec<String>,
|
pub libraries: Vec<String>,
|
||||||
|
|
||||||
/// Output a single JSON document containing the specified information.
|
/// Output a single JSON document containing the specified information.
|
||||||
/// Available arguments: `abi`, `hashes`, `metadata`, `devdoc`, `userdoc`, `storage-layout`, `ast`, `asm`, `bin`, `bin-runtime`.
|
/// Available arguments: `abi`, `hashes`, `metadata`, `devdoc`, `userdoc`, `storage-layout`, `ast`, `asm`, `bin`, `bin-runtime`.
|
||||||
#[structopt(long = "combined-json")]
|
#[arg(long = "combined-json")]
|
||||||
pub combined_json: Option<String>,
|
pub combined_json: Option<String>,
|
||||||
|
|
||||||
/// Switch to standard JSON input/output mode. Read from stdin, write the result to stdout.
|
/// Switch to standard JSON input/output mode. Read from stdin, write the result to stdout.
|
||||||
/// This is the default used by the Hardhat plugin.
|
/// This is the default used by the Hardhat plugin.
|
||||||
#[structopt(long = "standard-json")]
|
#[arg(long = "standard-json")]
|
||||||
pub standard_json: bool,
|
pub standard_json: bool,
|
||||||
|
|
||||||
/// Switch to missing deployable libraries detection mode.
|
/// Switch to missing deployable libraries detection mode.
|
||||||
/// Only available for standard JSON input/output mode.
|
/// Only available for standard JSON input/output mode.
|
||||||
/// Contracts are not compiled in this mode, and all compilation artifacts are not included.
|
/// Contracts are not compiled in this mode, and all compilation artifacts are not included.
|
||||||
#[structopt(long = "detect-missing-libraries")]
|
#[arg(long = "detect-missing-libraries")]
|
||||||
pub detect_missing_libraries: bool,
|
pub detect_missing_libraries: bool,
|
||||||
|
|
||||||
/// Switch to Yul mode.
|
/// Switch to Yul mode.
|
||||||
/// Only one input Yul file is allowed.
|
/// Only one input Yul file is allowed.
|
||||||
/// Cannot be used with combined and standard JSON modes.
|
/// Cannot be used with combined and standard JSON modes.
|
||||||
#[structopt(long = "yul")]
|
#[arg(long = "yul")]
|
||||||
pub yul: bool,
|
pub yul: bool,
|
||||||
|
|
||||||
/// Switch to LLVM IR mode.
|
/// Switch to LLVM IR mode.
|
||||||
/// Only one input LLVM IR file is allowed.
|
/// Only one input LLVM IR file is allowed.
|
||||||
/// Cannot be used with combined and standard JSON modes.
|
/// Cannot be used with combined and standard JSON modes.
|
||||||
/// Use this mode at your own risk, as LLVM IR input validation is not implemented.
|
/// Use this mode at your own risk, as LLVM IR input validation is not implemented.
|
||||||
#[structopt(long = "llvm-ir")]
|
#[arg(long = "llvm-ir")]
|
||||||
pub llvm_ir: bool,
|
pub llvm_ir: bool,
|
||||||
|
|
||||||
/// Forcibly switch to EVM legacy assembly pipeline.
|
|
||||||
/// It is useful for older revisions of `solc` 0.8, where Yul was considered highly experimental
|
|
||||||
/// and contained more bugs than today.
|
|
||||||
#[structopt(long = "force-evmla")]
|
|
||||||
pub force_evmla: bool,
|
|
||||||
|
|
||||||
/// Set metadata hash mode.
|
/// Set metadata hash mode.
|
||||||
/// The only supported value is `none` that disables appending the metadata hash.
|
/// The only supported value is `none` that disables appending the metadata hash.
|
||||||
/// Is enabled by default.
|
/// Is enabled by default.
|
||||||
#[structopt(long = "metadata-hash")]
|
#[arg(long = "metadata-hash")]
|
||||||
pub metadata_hash: Option<String>,
|
pub metadata_hash: Option<String>,
|
||||||
|
|
||||||
/// Output PolkaVM assembly of the contracts.
|
/// Output PolkaVM assembly of the contracts.
|
||||||
#[structopt(long = "asm")]
|
#[arg(long = "asm")]
|
||||||
pub output_assembly: bool,
|
pub output_assembly: bool,
|
||||||
|
|
||||||
/// Output PolkaVM bytecode of the contracts.
|
/// Output PolkaVM bytecode of the contracts.
|
||||||
#[structopt(long = "bin")]
|
#[arg(long = "bin")]
|
||||||
pub output_binary: bool,
|
pub output_binary: bool,
|
||||||
|
|
||||||
/// Suppress specified warnings.
|
/// Suppress specified warnings.
|
||||||
/// Available arguments: `ecrecover`, `sendtransfer`, `extcodesize`, `txorigin`, `blocktimestamp`, `blocknumber`, `blockhash`.
|
/// Available arguments: `ecrecover`, `sendtransfer`, `extcodesize`, `txorigin`, `blocktimestamp`, `blocknumber`, `blockhash`.
|
||||||
#[structopt(long = "suppress-warnings")]
|
#[arg(long = "suppress-warnings")]
|
||||||
pub suppress_warnings: Option<Vec<String>>,
|
pub suppress_warnings: Option<Vec<String>>,
|
||||||
|
|
||||||
/// Generate source based debug information in the output code file. This only has an effect
|
/// Generate source based debug information in the output code file. This only has an effect
|
||||||
/// with the LLVM-IR code generator and is ignored otherwise.
|
/// with the LLVM-IR code generator and is ignored otherwise.
|
||||||
#[structopt(short = 'g')]
|
#[arg(short = 'g')]
|
||||||
pub emit_source_debug_info: bool,
|
pub emit_source_debug_info: bool,
|
||||||
|
|
||||||
/// Dump all IRs to files in the specified directory.
|
/// Dump all IRs to files in the specified directory.
|
||||||
/// Only for testing and debugging.
|
/// Only for testing and debugging.
|
||||||
#[structopt(long = "debug-output-dir")]
|
#[arg(long = "debug-output-dir")]
|
||||||
pub debug_output_directory: Option<PathBuf>,
|
pub debug_output_directory: Option<PathBuf>,
|
||||||
|
|
||||||
/// Set the verify-each option in LLVM.
|
/// Set the verify-each option in LLVM.
|
||||||
/// Only for testing and debugging.
|
/// Only for testing and debugging.
|
||||||
#[structopt(long = "llvm-verify-each")]
|
#[arg(long = "llvm-verify-each")]
|
||||||
pub llvm_verify_each: bool,
|
pub llvm_verify_each: bool,
|
||||||
|
|
||||||
/// Set the debug-logging option in LLVM.
|
/// Set the debug-logging option in LLVM.
|
||||||
/// Only for testing and debugging.
|
/// Only for testing and debugging.
|
||||||
#[structopt(long = "llvm-debug-logging")]
|
#[arg(long = "llvm-debug-logging")]
|
||||||
pub llvm_debug_logging: bool,
|
pub llvm_debug_logging: bool,
|
||||||
|
|
||||||
/// Run this process recursively and provide JSON input to compile a single contract.
|
/// Run this process recursively and provide JSON input to compile a single contract.
|
||||||
/// Only for usage from within the compiler.
|
/// Only for usage from within the compiler.
|
||||||
#[structopt(long = "recursive-process")]
|
#[arg(long = "recursive-process")]
|
||||||
pub recursive_process: bool,
|
pub recursive_process: bool,
|
||||||
|
|
||||||
/// Specify the input file to use instead of stdin when --recursive-process is given.
|
/// Specify the input file to use instead of stdin when --recursive-process is given.
|
||||||
/// This is only intended for use when developing the compiler.
|
/// This is only intended for use when developing the compiler.
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[structopt(long = "recursive-process-input")]
|
#[arg(long = "recursive-process-input")]
|
||||||
pub recursive_process_input: Option<String>,
|
pub recursive_process_input: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Arguments {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Arguments {
|
impl Arguments {
|
||||||
/// A shortcut constructor.
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self::parse()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Validates the arguments.
|
/// Validates the arguments.
|
||||||
#[allow(clippy::collapsible_if)]
|
|
||||||
pub fn validate(&self) -> anyhow::Result<()> {
|
pub fn validate(&self) -> anyhow::Result<()> {
|
||||||
if self.version && std::env::args().count() > 2 {
|
if self.version && std::env::args().count() > 2 {
|
||||||
anyhow::bail!("No other options are allowed while getting the compiler version.");
|
anyhow::bail!("No other options are allowed while getting the compiler version.");
|
||||||
@@ -248,27 +230,19 @@ impl Arguments {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.force_evmla {
|
|
||||||
anyhow::bail!("EVM legacy assembly mode is not supported in Yul, LLVM IR and PolkaVM assembly modes.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.disable_solc_optimizer {
|
if self.disable_solc_optimizer {
|
||||||
anyhow::bail!("Disabling the solc optimizer is not supported in Yul, LLVM IR and PolkaVM assembly modes.");
|
anyhow::bail!("Disabling the solc optimizer is not supported in Yul, LLVM IR and PolkaVM assembly modes.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.llvm_ir {
|
if self.llvm_ir && self.solc.is_some() {
|
||||||
if self.solc.is_some() {
|
anyhow::bail!("`solc` is not used in LLVM IR and PolkaVM assembly modes.");
|
||||||
anyhow::bail!("`solc` is not used in LLVM IR and PolkaVM assembly modes.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.combined_json.is_some() {
|
if self.combined_json.is_some() && (self.output_assembly || self.output_binary) {
|
||||||
if self.output_assembly || self.output_binary {
|
anyhow::bail!(
|
||||||
anyhow::bail!(
|
"Cannot output assembly or binary outside of JSON in combined JSON mode."
|
||||||
"Cannot output assembly or binary outside of JSON in combined JSON mode."
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.standard_json {
|
if self.standard_json {
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user