Skip to content

Commit

Permalink
Add wasm-core and update std to use u32 values
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaAmora committed May 9, 2022
1 parent 7e33c47 commit 41cf815
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 41 deletions.
1 change: 0 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ jobs:
name: 4orth
path: |
4orth
porth/std/core.porth
std/
- name: Test html Bundle
Expand Down
9 changes: 1 addition & 8 deletions .github/workflows/Release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Check out from Git
uses: actions/checkout@v3
- uses: dawidd6/action-download-artifact@v2
with:
workflow: CI.yml
Expand All @@ -35,12 +33,7 @@ jobs:
env:
TAG: ${{ steps.get_version.outputs.tag }}
run: |
mv 4orth porth/
mv std/wasm4.porth porth/std
mv std/w4std.porth porth/std
mv std/wasi.porth porth/std
cd porth
zip -r ../"4oth-${TAG}-linux".zip *
zip -r "4oth-${TAG}-linux".zip .
- name: Publish release if tag is new
if: ${{ steps.get_version.outputs.tag != steps.latest_version.outputs.latest_tag }}
Expand Down
12 changes: 5 additions & 7 deletions std/wasi.porth
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
include "core.porth"
include "wasm-core.porth"

export main "_start"

import module "wasi_unstable"
import proc fd_write ptr int ptr int -- ptr in end

const usize sizeof(u32) end

const ciovec.buff usize offset end
const ciovec.buf_len usize offset end
const ciovec.buff sizeof(u32) offset end
const ciovec.buf_len sizeof(u32) offset end
const sizeof(ciovec) reset end

proc puts int ptr in
memory ciovec sizeof(ciovec) end
memory written usize end
memory written sizeof(u32) end
ciovec !ptr
ciovec usize ptr+ !int
ciovec sizeof(u32) ptr+ !int
written 1 ciovec 1 fd_write
drop
end
113 changes: 113 additions & 0 deletions std/wasm-core.porth
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Core fundamental definitions of Porth, but lowered to u32 size.

const NULL 0 cast(ptr) end

const true 1 cast(bool) end
const false 0 cast(bool) end

const sizeof(u32) 4 end
const sizeof(u16) 2 end
const sizeof(u8) 1 end

const sizeof(ptr) sizeof(u32) end
const sizeof(bool) sizeof(u32) end
const sizeof(int) sizeof(u32) end
const sizeof(addr) sizeof(u32) end

inline proc @ptr ptr -- ptr in @32 cast(ptr) end
inline proc @@ptr ptr -- ptr in @ptr @ptr end
inline proc @bool ptr -- bool in @32 cast(bool) end
inline proc @int ptr -- int in @32 end
inline proc @addr ptr -- addr in @int cast(addr) end

inline proc !bool bool ptr in !32 end
inline proc !ptr ptr ptr in !32 end
inline proc !int int ptr in !32 end
inline proc !addr addr ptr in !32 end

inline proc ptr+ ptr int -- ptr in
swap cast(int)
swap cast(int)
+
cast(ptr)
end

inline proc ptr- ptr int -- ptr in
swap cast(int)
swap cast(int)
-
cast(ptr)
end

inline proc ptr!= ptr ptr -- bool in
swap cast(int)
swap cast(int)
!=
end

inline proc ptr= ptr ptr -- bool in
swap cast(int)
swap cast(int)
=
end

inline proc ptr< ptr ptr -- bool in
swap cast(int)
swap cast(int)
<
end

inline proc +ptr int ptr -- ptr in
cast(int) + cast(ptr)
end

inline proc ptr-diff ptr ptr -- int in
swap cast(int)
swap cast(int)
-
end

inline proc / int int -- int in divmod drop end
inline proc % int int -- int in divmod swap drop end
inline proc mod int int -- int in % end
inline proc div int int -- int in / end
inline proc imod int int -- int in idivmod swap drop end
inline proc idiv int int -- int in idivmod drop end
inline proc emod int int -- int in
let a b in
a
b imod
b +
b imod
end
end

inline proc lnot bool -- bool in
cast(int) 1 swap - cast(bool)
end

inline proc land bool bool -- bool in
swap cast(int)
swap cast(int)
and
cast(bool)
end

inline proc lor bool bool -- bool in
swap cast(int)
swap cast(int)
or
cast(bool)
end

inline proc inc32-by ptr int in over @32 + swap !32 end
inline proc inc32 ptr in dup @32 1 + swap !32 end
inline proc dec32 ptr in dup @32 1 - swap !32 end
inline proc inc8 ptr in dup @8 1 + swap !8 end
inline proc dec8 ptr in dup @8 1 - swap !8 end

inline proc neg int -- int in not 1 + end

inline proc ?null ptr -- bool in NULL ptr= end

inline proc toggle ptr in dup @bool lnot swap !32 end
44 changes: 21 additions & 23 deletions std/w4std.porth → std/wasm-std.porth
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include "wasm4.porth"
include "wasm-core.porth"

proc cstrlen ptr -- int in
dup
Expand Down Expand Up @@ -29,16 +29,16 @@ end

inline proc cstr-to-str ptr -- int ptr in dup cstrlen swap end

const offsetof(Str.count) sizeof(u64) offset end
const offsetof(Str.count) sizeof(u32) offset end
const offsetof(Str.data) sizeof(ptr) offset end
const sizeof(Str) reset end

inline proc Str.count ptr -- ptr in offsetof(Str.count) ptr+ end
inline proc Str.data ptr -- ptr in offsetof(Str.data) ptr+ end
inline proc @Str.count ptr -- int in Str.count @64 end
inline proc @Str.data ptr -- ptr in Str.data @64 cast(ptr) end
inline proc !Str.count int ptr in Str.count !64 end
inline proc !Str.data ptr ptr in Str.data !64 end
inline proc @Str.count ptr -- int in Str.count @32 end
inline proc @Str.data ptr -- ptr in Str.data @32 cast(ptr) end
inline proc !Str.count int ptr in Str.count !32 end
inline proc !Str.data ptr ptr in Str.data !32 end

inline proc @Str ptr -- int ptr in
let s in
Expand All @@ -57,12 +57,12 @@ end
inline proc str-null -- int ptr in 0 NULL end

inline proc str-chop-one-left ptr in
dup Str.count dec64
Str.data inc64
dup Str.count dec32
Str.data inc32
end

inline proc str-chop-one-right ptr in
Str.count dec64
Str.count dec32
end

proc ?space int -- bool in
Expand All @@ -88,7 +88,7 @@ in
else false end
do
dup str-chop-one-left
swap dup Str.count inc64 swap
swap dup Str.count inc32 swap
end
dup @Str.count 0 > if
dup str-chop-one-left
Expand Down Expand Up @@ -137,12 +137,10 @@ end

inline proc ?str-empty ptr -- bool in
offsetof(Str.count) ptr+
@64
@32
0 =
end

inline proc puts int ptr in traceUtf8 end

const PUTU_BUFFER_CAP 32 end
proc putu
int // number
Expand Down Expand Up @@ -188,14 +186,14 @@ in
memory src sizeof(ptr) end
memory dst sizeof(ptr) end
memory result sizeof(ptr) end
dst !64
src !64
dst !32
src !32
dst @ptr swap // result
while dup 0 > do
src @ptr @8
dst @ptr !8
src inc64
dst inc64
src inc32
dst inc32
1 -
end drop
end
Expand All @@ -208,13 +206,13 @@ proc memset
ptr // data
in
memory data sizeof(ptr) end
data !64
memory byte sizeof(u64) end
byte !64
data !32
memory byte sizeof(u32) end
byte !32
data @ptr swap
while dup 0 > do
byte @64 data @ptr !8
data inc64
byte @32 data @ptr !8
data inc32
1 -
end
drop
Expand Down Expand Up @@ -244,7 +242,7 @@ in
memcpy drop

count @ptr @int
count @ptr inc64
count @ptr inc32

true
else
Expand Down
7 changes: 5 additions & 2 deletions std/wasm4.porth
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// WASM-4: https://wasm4.org/docs

include "core.porth"
include "wasm-core.porth"

export main "start"
export update "update"
Expand Down Expand Up @@ -117,4 +117,7 @@ const $TONE_MODE3 8 end
const $TONE_MODE4 12 end

// Baked in random procedure. Returns a value between 1 and the given input.
proc rnd int -- int in end
proc rnd int -- int in end

// Alias for traceUtf8
inline proc puts int ptr in traceUtf8 end

0 comments on commit 41cf815

Please sign in to comment.