Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust runtime #2782

Merged
merged 22 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ jobs:
cd main
just ${{ env.JUST_ARGS }} build runtime

- name: Test Rust runtime
run: |
cd main/runtime/rust
cargo test --release

# We use the options:
# - -fhide-source-paths
# - -fwrite-ide-info -hiedir=.hie
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ TAGS
# other
.DS_Store

/runtime/include/
/runtime/c/include/
_build/
_build.*/
*.agdai
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ fast-build: submodules runtime

.PHONY: runtime
runtime:
cd runtime && make -j 4 -s
cd runtime && make

# -- Install

Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Compile/Native/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ nativeHelperOptions opts =
| otherwise = nativeReleaseRuntime
where
nativeReleaseRuntime :: BS.ByteString
nativeReleaseRuntime = $(FE.makeRelativeToProject "runtime/_build.native64/libjuvix.a" >>= FE.embedFile)
nativeReleaseRuntime = $(FE.makeRelativeToProject "runtime/c/_build.native64/libjuvix.a" >>= FE.embedFile)

nativeDebugRuntime :: BS.ByteString
nativeDebugRuntime = $(FE.makeRelativeToProject "runtime/_build.native64-debug/libjuvix.a" >>= FE.embedFile)
nativeDebugRuntime = $(FE.makeRelativeToProject "runtime/c/_build.native64-debug/libjuvix.a" >>= FE.embedFile)

nativeDefaultOutputFile :: Path Abs File -> Path Abs File -> Path Abs File
nativeDefaultOutputFile inputFile baseOutputFile =
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Compile/NativeWasiHelper/RuntimeWriter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ writeRuntime runtime = do
mapM_ (uncurry writeHeader) headersDir
where
headersDir :: [(Path Rel File, BS.ByteString)]
headersDir = map (first relFile) $(FE.makeRelativeToProject "runtime/include" >>= FE.embedDir)
headersDir = map (first relFile) $(FE.makeRelativeToProject "runtime/c/include" >>= FE.embedDir)
4 changes: 2 additions & 2 deletions app/Commands/Compile/Wasi/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ wasiHelperOptions opts =
| otherwise = wasiReleaseRuntime
where
wasiReleaseRuntime :: BS.ByteString
wasiReleaseRuntime = $(FE.makeRelativeToProject "runtime/_build.wasm32-wasi/libjuvix.a" >>= FE.embedFile)
wasiReleaseRuntime = $(FE.makeRelativeToProject "runtime/c/_build.wasm32-wasi/libjuvix.a" >>= FE.embedFile)

wasiDebugRuntime :: BS.ByteString
wasiDebugRuntime = $(FE.makeRelativeToProject "runtime/_build.wasm32-wasi-debug/libjuvix.a" >>= FE.embedFile)
wasiDebugRuntime = $(FE.makeRelativeToProject "runtime/c/_build.wasm32-wasi-debug/libjuvix.a" >>= FE.embedFile)

wasiDefaultOutputFile :: Path Abs File -> Path Abs File -> Path Abs File
wasiDefaultOutputFile inputFile baseOutputFile =
Expand Down
10 changes: 5 additions & 5 deletions app/Commands/Extra/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,24 @@ prepareRuntime buildDir o = do
AppTargetCairo -> return ()
where
wasiReleaseRuntime :: BS.ByteString
wasiReleaseRuntime = $(FE.makeRelativeToProject "runtime/_build.wasm32-wasi/libjuvix.a" >>= FE.embedFile)
wasiReleaseRuntime = $(FE.makeRelativeToProject "runtime/c/_build.wasm32-wasi/libjuvix.a" >>= FE.embedFile)

nativeReleaseRuntime :: BS.ByteString
nativeReleaseRuntime = $(FE.makeRelativeToProject "runtime/_build.native64/libjuvix.a" >>= FE.embedFile)
nativeReleaseRuntime = $(FE.makeRelativeToProject "runtime/c/_build.native64/libjuvix.a" >>= FE.embedFile)

wasiDebugRuntime :: BS.ByteString
wasiDebugRuntime = $(FE.makeRelativeToProject "runtime/_build.wasm32-wasi-debug/libjuvix.a" >>= FE.embedFile)
wasiDebugRuntime = $(FE.makeRelativeToProject "runtime/c/_build.wasm32-wasi-debug/libjuvix.a" >>= FE.embedFile)

nativeDebugRuntime :: BS.ByteString
nativeDebugRuntime = $(FE.makeRelativeToProject "runtime/_build.native64-debug/libjuvix.a" >>= FE.embedFile)
nativeDebugRuntime = $(FE.makeRelativeToProject "runtime/c/_build.native64-debug/libjuvix.a" >>= FE.embedFile)

writeRuntime :: BS.ByteString -> Sem r ()
writeRuntime =
liftIO
. BS.writeFile (toFilePath (buildDir <//> $(mkRelFile "libjuvix.a")))

headersDir :: [(Path Rel File, BS.ByteString)]
headersDir = map (first relFile) $(FE.makeRelativeToProject "runtime/include" >>= FE.embedDir)
headersDir = map (first relFile) $(FE.makeRelativeToProject "runtime/c/include" >>= FE.embedDir)

includeDir :: Path Abs Dir
includeDir = juvixIncludeDir buildDir
Expand Down
14 changes: 8 additions & 6 deletions cntlines.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ function count_ext () {
find $2 -name $1 -print | xargs sed '/^[[:space:]]*$/d' | wc -l | tr -d ' '
}

RUNTIME_C=$(count runtime/src/juvix)
RUNTIME_VAMPIR=$(count_ext '*.pir' runtime/src/vampir)
RUNTIME_JVT=$(count_ext '*.jvt' runtime/src/tree)
RUNTIME_CASM=$(count_ext '*.casm' runtime/src/casm)
RUNTIME_C=$(count runtime/c/src/juvix)
RUNTIME_RUST=$(count runtime/rust/src)
RUNTIME_VAMPIR=$(count_ext '*.pir' runtime/vampir)
RUNTIME_JVT=$(count_ext '*.jvt' runtime/tree)
RUNTIME_CASM=$(count_ext '*.casm' runtime/casm)

RUNTIME=$((RUNTIME_C+RUNTIME_VAMPIR+RUNTIME_JVT+RUNTIME_CASM))
RUNTIME=$((RUNTIME_C+RUNTIME_RUST+RUNTIME_VAMPIR+RUNTIME_JVT+RUNTIME_CASM))

BACKENDC=$(count src/Juvix/Compiler/Backend/C/)
CAIRO=$(count src/Juvix/Compiler/Backend/Cairo/)
Expand Down Expand Up @@ -62,6 +63,7 @@ echo " JuvixTree: $TREE LOC"
echo " JuvixCore: $CORE LOC"
echo "Runtime: $RUNTIME LOC"
echo " C runtime: $RUNTIME_C LOC"
echo " Rust runtime: $RUNTIME_RUST LOC"
echo " JuvixTree runtime: $RUNTIME_JVT LOC"
echo " Cairo assembly runtime: $RUNTIME_CASM LOC"
echo " VampIR runtime: $RUNTIME_VAMPIR LOC"
Expand All @@ -74,4 +76,4 @@ echo " Data: $DATA LOC"
echo " Prelude: $PRELUDE LOC"
echo "Tests: $TESTS LOC"
echo ""
echo "Total: $TOTAL Haskell LOC + $RUNTIME_C C LOC + $RUNTIME_JVT JuvixTree LOC + $RUNTIME_CASM CASM LOC + $RUNTIME_VAMPIR VampIR LOC"
echo "Total: $TOTAL Haskell LOC + $RUNTIME_C C LOC + $RUNTIME_RUST Rust LOC + $RUNTIME_JVT JuvixTree LOC + $RUNTIME_CASM CASM LOC + $RUNTIME_VAMPIR VampIR LOC"
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ run-profile +cmd:

# Build the juvix runtime
_buildRuntime:
cd runtime && make {{ runtimeArgs }} -j 4 -s
cd runtime && make {{ runtimeArgs }}

# Build the project. `build runtime` builds only the runtime.
[no-exit-message]
Expand Down
12 changes: 6 additions & 6 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ extra-source-files:
- juvix-stdlib/**/*.juvix
- include/package/**/*.juvix
- include/package-base/**/*.juvix
- runtime/include/**/*.h
- runtime/**/*.a
- runtime/src/tree/*.jvt
- runtime/src/vampir/*.pir
- runtime/src/casm/*.casm
- runtime/src/nockma/*.nockma
- runtime/c/include/**/*.h
- runtime/c/**/*.a
- runtime/tree/*.jvt
- runtime/vampir/*.pir
- runtime/casm/*.casm
- runtime/nockma/*.nockma

dependencies:
- aeson-better-errors == 0.9.*
Expand Down
62 changes: 14 additions & 48 deletions runtime/Makefile
Original file line number Diff line number Diff line change
@@ -1,55 +1,21 @@
all: release debug includes

release: wasm32-wasi native64
export

debug: wasm32-wasi-debug native64-debug
.PHONY: all
all: juvix_c juvix_rust

HEADERS := $(patsubst src/%,include/%,$(shell find src -name '*.h'))
.PHONY: juvix_c
juvix_c:
cd c && $(MAKE) -j 4 -s

includes: $(HEADERS)

$(HEADERS) : include/%.h : src/%.h
@mkdir -p `dirname $@`
@cp $< $@

wasm32:
$(MAKE) -f Makefile.generic CONFIG=WASM32+RELEASE

wasm32-wasi:
$(MAKE) -f Makefile.generic CONFIG=WASM32-WASI+RELEASE

native32:
$(MAKE) -f Makefile.generic CONFIG=NATIVE32+RELEASE

native64:
$(MAKE) -f Makefile.generic CONFIG=NATIVE64+RELEASE

x86_32:
$(MAKE) -f Makefile.generic CONFIG=X86_32+RELEASE

wasm32-debug:
$(MAKE) -f Makefile.generic CONFIG=WASM32+DEBUG

wasm32-wasi-debug:
$(MAKE) -f Makefile.generic CONFIG=WASM32-WASI+DEBUG

native32-debug:
$(MAKE) -f Makefile.generic CONFIG=NATIVE32+DEBUG

native64-debug:
$(MAKE) -f Makefile.generic CONFIG=NATIVE64+DEBUG

x86_32-debug:
$(MAKE) -f Makefile.generic CONFIG=X86_32+DEBUG

asm:
$(MAKE) -f Makefile.generic CONFIG=ASM+DEBUG

format:
@clang-format -i `find src -name '*.c' -or -name '*.h'`
.PHONY: juvix_rust
juvix_rust:
cd rust && cargo build --release

.PHONY: clean
clean:
@-rm -rf _build*
@-rm -rf include
cd c && $(MAKE) clean

.PHONY: release debug includes wasm32 wasm32-wasi native32 native64 x86_32 wasm32-debug wasm32-wasi-debug native32-debug native64-debug x86_32-debug asm format clean
.PHONY: format
format:
cd c && $(MAKE) format
56 changes: 56 additions & 0 deletions runtime/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
all: release debug includes

release: wasm32-wasi native64

debug: wasm32-wasi-debug native64-debug

HEADERS := $(patsubst src/%,include/%,$(shell find src -name '*.h'))
CFORMAT := clang-format

includes: $(HEADERS)

$(HEADERS) : include/%.h : src/%.h
@mkdir -p `dirname $@`
@cp $< $@

wasm32:
$(MAKE) -f Makefile.generic CONFIG=WASM32+RELEASE

wasm32-wasi:
$(MAKE) -f Makefile.generic CONFIG=WASM32-WASI+RELEASE

native32:
$(MAKE) -f Makefile.generic CONFIG=NATIVE32+RELEASE

native64:
$(MAKE) -f Makefile.generic CONFIG=NATIVE64+RELEASE

x86_32:
$(MAKE) -f Makefile.generic CONFIG=X86_32+RELEASE

wasm32-debug:
$(MAKE) -f Makefile.generic CONFIG=WASM32+DEBUG

wasm32-wasi-debug:
$(MAKE) -f Makefile.generic CONFIG=WASM32-WASI+DEBUG

native32-debug:
$(MAKE) -f Makefile.generic CONFIG=NATIVE32+DEBUG

native64-debug:
$(MAKE) -f Makefile.generic CONFIG=NATIVE64+DEBUG

x86_32-debug:
$(MAKE) -f Makefile.generic CONFIG=X86_32+DEBUG

asm:
$(MAKE) -f Makefile.generic CONFIG=ASM+DEBUG

format:
@$(CFORMAT) -i `find src -name '*.c' -or -name '*.h'`

clean:
@-rm -rf _build*
@-rm -rf include

.PHONY: release debug includes wasm32 wasm32-wasi native32 native64 x86_32 wasm32-debug wasm32-wasi-debug native32-debug native64-debug x86_32-debug asm format clean
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions runtime/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
Cargo.lock
*~
8 changes: 8 additions & 0 deletions runtime/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "juvix"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
70 changes: 70 additions & 0 deletions runtime/rust/src/apply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Closure application

use super::defs::*;
use super::memory::*;

pub enum AppResult {
// Call(fid)
Call(Word, Vec<Word>),
// Return(result_closure)
Return(Word),
// Continue(fid, args_left)
Continue(Word, Vec<Word>, Vec<Word>),
}

impl Memory {
pub fn apply(self: &mut Memory, cl: Word, cargs: &[Word]) -> AppResult {
let argsnum = self.get_closure_largs(cl);
if argsnum == cargs.len() {
let (fid, args) = self.call_closure(cl, cargs);
AppResult::Call(fid, args)
} else if argsnum > cargs.len() {
AppResult::Return(self.extend_closure(cl, cargs))
} else {
let (fid, args) = self.call_closure(cl, &cargs[0..argsnum]);
AppResult::Continue(fid, args, Vec::from(&cargs[argsnum..cargs.len()]))
}
}
}

#[macro_export]
macro_rules! tapply {
($lab:lifetime, $program:ident, $mem:ident, $fid:ident, $args:ident, $cl0:expr, $cargs0:expr) => {
let mut cl = $cl0;
let mut cargs = $cargs0;
loop {
match $mem.apply( cl, &cargs) {
apply::AppResult::Call(fid1, args1) => {
$fid = fid1;
$args = args1;
continue $lab;
}
apply::AppResult::Return(r) => break $lab r,
apply::AppResult::Continue(fid1, args1, cargs1) => {
cl = $program($mem, fid1, args1);
cargs = cargs1;
}
}
}
};
}

#[macro_export]
macro_rules! apply {
($program:ident, $mem:ident, $cl0:expr, $cargs0:expr) => {{
let mut cl = $cl0;
let mut cargs = $cargs0;
loop {
match $mem.apply(cl, &cargs) {
apply::AppResult::Call(fid, args) => {
break $program($mem, fid, args);
}
apply::AppResult::Return(r) => break r,
apply::AppResult::Continue(fid1, args1, cargs1) => {
cl = $program($mem, fid1, args1);
cargs = cargs1;
}
}
}
}};
}
Loading
Loading