Skip to content

Commit 6fb975d

Browse files
atheiascjones
andauthored
Initial RISC-V support (#1718)
* Fix message dispatch for riscv * Fix error handlers for riscv * Fix allocator for riscv * Fix ext for riscv * Remove not needed manifest keys (they are already removed from the template) * Add no_main to all examples * Fix CI * Fix multisig doc test * Update linking to latest supported toolchain * fmt * fmt * fmt * Fix mess caused by rustfmt * Combine no_std and no_main * Update .gitlab-ci.yml Co-authored-by: Andrew Jones <[email protected]> * Remove not needed block --------- Co-authored-by: Andrew Jones <[email protected]>
1 parent b436933 commit 6fb975d

File tree

73 files changed

+1206
-825
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1206
-825
lines changed

.gitlab-ci.yml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ variables:
3939
# this fix not yet in stable: https://github.com/rust-lang/rust-clippy/issues/8895.
4040
# Remove the following line again as soon as `clippy` on stable succeeds again.
4141
CLIPPY_ALLOWED: "clippy::extra_unused_lifetimes"
42-
# This is a target that does not have a standard library in contrast to Wasm. We compile against
43-
# this target to make sure that we don't pull in `std` by accident (through dependencies).
44-
# See https://doc.rust-lang.org/nightly/rustc/platform-support.html#tier-2.
45-
NO_STD_TARGET: "aarch64-unknown-none"
42+
# We plan to fully support RISC-V as a bytecode for contracts soon.
43+
# RISC-V does not have a standard library in contrast to Wasm. Compiling against
44+
# this target also makes sure that we don't pull in `std` by accident (through dependencies).
45+
# RISC-V is a modular archtitecture. We might switch to a different flavor with more features
46+
# later. For example, `riscv32imc-unknown-none-elf`.
47+
RISCV_TARGET: "riscv32i-unknown-none-elf"
4648

4749
workflow:
4850
rules:
@@ -209,16 +211,15 @@ check-wasm:
209211
cargo check --verbose --no-default-features --target wasm32-unknown-unknown --manifest-path ./crates/${crate}/Cargo.toml;
210212
done
211213

212-
# We just check on a target which doesn't have `std` to make sure that we don't pull in `std` by accident
213-
check-no-std:
214+
check-riscv:
214215
stage: check
215216
<<: *docker-env
216217
<<: *test-refs
217218
variables:
218219
RUSTC_BOOTSTRAP: "1"
219220
script:
220221
- for crate in ${ALSO_WASM_CRATES}; do
221-
cargo check --verbose --no-default-features --target $NO_STD_TARGET -Zbuild-std="core,alloc" --manifest-path ./crates/${crate}/Cargo.toml;
222+
cargo check --verbose --no-default-features --target $RISCV_TARGET -Zbuild-std="core,alloc" --manifest-path ./crates/${crate}/Cargo.toml;
222223
done
223224

224225
dylint:
@@ -426,8 +427,8 @@ examples-contract-build:
426427
done
427428
- cargo +stable contract build --manifest-path ./integration-tests/set_code_hash/updated_incrementer/Cargo.toml
428429

429-
# We only check a few basic contracts here, no need to check them all.
430-
examples-no-std-check:
430+
# TODO: Use cargo contract as soon as it has RISC-V support
431+
examples-contract-build-riscv:
431432
stage: examples
432433
<<: *docker-env
433434
<<: *test-refs
@@ -442,15 +443,15 @@ examples-no-std-check:
442443
# - upgradeable-contracts
443444
# This uses dlmalloc which is only supported on select targets.
444445
# - custom_allocator
445-
# Pulls in sp-std which needlessly requires atomic pointers.
446+
# Pulls in sp-std which needlessly requires atomic pointers (TODO: Fix sp-std and enable this example)
446447
# - call-runtime
447448
- for example in integration-tests/*/; do
448449
if [ "$example" = "integration-tests/lang-err-integration-tests/" ]; then continue; fi;
449450
if [ "$example" = "integration-tests/upgradeable-contracts/" ]; then continue; fi;
450451
if [ "$example" = "integration-tests/custom_allocator/" ]; then continue; fi;
451452
if [ "$example" = "integration-tests/call-runtime/" ]; then continue; fi;
452453
pushd $example &&
453-
cargo +stable check --no-default-features --target $NO_STD_TARGET -Zbuild-std="core,alloc" &&
454+
cargo +stable build --no-default-features --target $RISCV_TARGET -Zbuild-std="core,alloc" &&
454455
popd;
455456
done
456457

crates/allocator/src/bump.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,21 @@ struct InnerAlloc {
6868
impl InnerAlloc {
6969
const fn new() -> Self {
7070
Self {
71-
next: 0,
72-
upper_limit: 0,
71+
next: Self::heap_start(),
72+
upper_limit: Self::heap_end(),
7373
}
7474
}
7575

7676
cfg_if::cfg_if! {
7777
if #[cfg(test)] {
78+
const fn heap_start() -> usize {
79+
0
80+
}
81+
82+
const fn heap_end() -> usize {
83+
0
84+
}
85+
7886
/// Request a `pages` number of page sized sections of Wasm memory. Each page is `64KiB` in size.
7987
///
8088
/// Returns `None` if a page is not available.
@@ -85,13 +93,29 @@ impl InnerAlloc {
8593
Some(self.upper_limit)
8694
}
8795
} else if #[cfg(feature = "std")] {
96+
const fn heap_start() -> usize {
97+
0
98+
}
99+
100+
const fn heap_end() -> usize {
101+
0
102+
}
103+
88104
fn request_pages(&mut self, _pages: usize) -> Option<usize> {
89105
unreachable!(
90106
"This branch is only used to keep the compiler happy when building tests, and
91107
should never actually be called outside of a test run."
92108
)
93109
}
94110
} else if #[cfg(target_arch = "wasm32")] {
111+
const fn heap_start() -> usize {
112+
0
113+
}
114+
115+
const fn heap_end() -> usize {
116+
0
117+
}
118+
95119
/// Request a `pages` number of pages of Wasm memory. Each page is `64KiB` in size.
96120
///
97121
/// Returns `None` if a page is not available.
@@ -103,11 +127,24 @@ impl InnerAlloc {
103127

104128
prev_page.checked_mul(PAGE_SIZE)
105129
}
106-
} else {
107-
/// On other architectures growing memory probably doesn't make sense.
130+
} else if #[cfg(target_arch = "riscv32")] {
131+
const fn heap_start() -> usize {
132+
// Placeholder value until we specified our riscv VM
133+
0x7000_0000
134+
}
135+
136+
const fn heap_end() -> usize {
137+
// Placeholder value until we specified our riscv VM
138+
// Let's just assume a cool megabyte of mem for now
139+
0x7000_0400
140+
}
141+
108142
fn request_pages(&mut self, _pages: usize) -> Option<usize> {
143+
// On riscv the memory can't be grown
109144
None
110145
}
146+
} else {
147+
core::compile_error!("ink! only supports wasm32 and riscv32");
111148
}
112149
}
113150

crates/allocator/src/handlers.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

crates/allocator/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@
2323
html_favicon_url = "https://use.ink/crate-docs/favicon.png"
2424
)]
2525
#![cfg_attr(not(feature = "std"), no_std)]
26-
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
2726

2827
#[cfg(not(any(feature = "std", feature = "no-allocator")))]
2928
#[global_allocator]
3029
static mut ALLOC: bump::BumpAllocator = bump::BumpAllocator {};
3130

3231
mod bump;
3332

34-
#[cfg(not(feature = "std"))]
35-
mod handlers;
36-
3733
#[cfg(all(test, feature = "std", feature = "ink-fuzz-tests",))]
3834
#[macro_use(quickcheck)]
3935
extern crate quickcheck_macros;

crates/env/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "ink_env"
33
version = "4.1.0"
44
authors = ["Parity Technologies <[email protected]>", "Robin Freyler <[email protected]>"]
55
edition = "2021"
6+
rust-version = "1.68"
67

78
license = "Apache-2.0"
89
readme = "README.md"

0 commit comments

Comments
 (0)