mirror of
https://github.com/remko/waforth
synced 2024-11-16 07:47:30 +01:00
Remove temporary find
This commit is contained in:
parent
a1ddce475f
commit
f4eed729ea
6 changed files with 192 additions and 67 deletions
9
Makefile
9
Makefile
|
@ -5,7 +5,7 @@ WAT2WASM_FLAGS=--debug-names
|
|||
endif
|
||||
PARCEL=./node_modules/.bin/parcel
|
||||
|
||||
WASM_FILES=dist/waforth.wasm
|
||||
WASM_FILES=dist/waforth.wasm dist/sieve-vanilla.wasm
|
||||
|
||||
all: $(WASM_FILES)
|
||||
$(PARCEL) build src/shell/index.html
|
||||
|
@ -17,12 +17,19 @@ dev-server: $(WASM_FILES)
|
|||
tests: $(WASM_FILES)
|
||||
$(PARCEL) --no-hmr -o dist/tests.html tests/index.html
|
||||
|
||||
.PHONY: sieve-vanilla
|
||||
sieve-vanilla: $(WASM_FILES)
|
||||
$(PARCEL) --no-hmr -o dist/sieve-vanilla.html benchmarks/sieve-vanilla/index.html
|
||||
|
||||
wasm: $(WASM_FILES) src/tools/quadruple.wasm.hex
|
||||
|
||||
dist/waforth.wasm: src/waforth.wat dist
|
||||
racket -f $< > src/waforth.wat.tmp
|
||||
$(WAT2WASM) $(WAT2WASM_FLAGS) -o $@ src/waforth.wat.tmp
|
||||
|
||||
dist/sieve-vanilla.wasm: benchmarks/sieve-vanilla/sieve-vanilla.wat
|
||||
$(WAT2WASM) $(WAT2WASM_FLAGS) -o $@ $<
|
||||
|
||||
dist:
|
||||
mkdir -p $@
|
||||
|
||||
|
|
10
benchmarks/sieve-vanilla/index.html
Normal file
10
benchmarks/sieve-vanilla/index.html
Normal file
|
@ -0,0 +1,10 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Sieve (Vanilla)</title>
|
||||
</head>
|
||||
<body>
|
||||
<script src="./index.js"></script>
|
||||
</body>
|
||||
</html>
|
12
benchmarks/sieve-vanilla/index.js
Normal file
12
benchmarks/sieve-vanilla/index.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
fetch("sieve-vanilla.wasm")
|
||||
.then(resp => resp.arrayBuffer())
|
||||
.then(module =>
|
||||
WebAssembly.instantiate(module, {
|
||||
js: {
|
||||
print: x => console.log(x)
|
||||
}
|
||||
})
|
||||
)
|
||||
.then(instance => {
|
||||
window.sieve = instance.instance.exports.sieve;
|
||||
});
|
45
benchmarks/sieve-vanilla/sieve-vanilla.wat
Normal file
45
benchmarks/sieve-vanilla/sieve-vanilla.wat
Normal file
|
@ -0,0 +1,45 @@
|
|||
(module
|
||||
(import "js" "print" (func $print (param i32)))
|
||||
(memory 4096)
|
||||
(func $sieve (export "sieve") (param $n i32) (result i32)
|
||||
(local $i i32)
|
||||
(local $j i32)
|
||||
(local $last i32)
|
||||
|
||||
(set_local $i (i32.const 0))
|
||||
(block $endLoop
|
||||
(loop $loop
|
||||
(br_if $endLoop (i32.ge_s (get_local $i) (get_local $n)))
|
||||
(i32.store8 (get_local $i) (i32.const 1))
|
||||
(set_local $i (i32.add (get_local $i) (i32.const 1)))
|
||||
(br $loop)))
|
||||
|
||||
(set_local $i (i32.const 2))
|
||||
(block $endLoop
|
||||
(loop $loop
|
||||
(br_if $endLoop (i32.ge_s (i32.mul (get_local $i) (get_local $i))
|
||||
(get_local $n)))
|
||||
(if (i32.eq (i32.load8_s (get_local $i)) (i32.const 1))
|
||||
(then
|
||||
(set_local $j (i32.mul (get_local $i) (get_local $i)))
|
||||
(block $endInnerLoop
|
||||
(loop $innerLoop
|
||||
(i32.store8 (get_local $j) (i32.const 0))
|
||||
(set_local $j (i32.add (get_local $j) (get_local $i)))
|
||||
(br_if $endInnerLoop (i32.ge_s (get_local $j) (get_local $n)))
|
||||
(br $innerLoop)))))
|
||||
(set_local $i (i32.add (get_local $i) (i32.const 1)))
|
||||
(br $loop)))
|
||||
|
||||
(set_local $i (i32.const 2))
|
||||
(block $endLoop
|
||||
(loop $loop
|
||||
(if (i32.eq (i32.load8_s (get_local $i)) (i32.const 1))
|
||||
(then
|
||||
;; (call $print (get_local $i))
|
||||
(set_local $last (get_local $i))))
|
||||
(set_local $i (i32.add (get_local $i) (i32.const 1)))
|
||||
(br_if $endLoop (i32.ge_s (get_local $i) (get_local $n)))
|
||||
(br $loop)))
|
||||
|
||||
(return (get_local $last))))
|
|
@ -75,74 +75,18 @@ class WAForth {
|
|||
table.grow(table.length); // Double size
|
||||
}
|
||||
var module = new WebAssembly.Module(data);
|
||||
// console.log("Load", tableBase, new Uint8Array(data), arrayToBase64(data));
|
||||
// console.log(
|
||||
// "Load",
|
||||
// tableBase,
|
||||
// new Uint8Array(data),
|
||||
// arrayToBase64(data)
|
||||
// );
|
||||
new WebAssembly.Instance(module, {
|
||||
env: { table, tableBase }
|
||||
});
|
||||
nextTableBase = nextTableBase + 1;
|
||||
return tableBase;
|
||||
}
|
||||
},
|
||||
tmp: {
|
||||
find: (latest, outOffset) => {
|
||||
const DICT_BASE = 0x20000;
|
||||
const wordAddr = new Int32Array(
|
||||
this.core.exports.memory.buffer,
|
||||
outOffset - 4,
|
||||
4
|
||||
)[0];
|
||||
const length = new Uint32Array(
|
||||
this.core.exports.memory.buffer,
|
||||
wordAddr,
|
||||
4
|
||||
)[0];
|
||||
const word = new Uint8Array(
|
||||
this.core.exports.memory.buffer,
|
||||
wordAddr + 4,
|
||||
length
|
||||
);
|
||||
const out = new Int32Array(
|
||||
this.core.exports.memory.buffer,
|
||||
outOffset - 4,
|
||||
8
|
||||
);
|
||||
// console.log("FIND", wordAddr, length, word);
|
||||
const u8 = new Uint8Array(
|
||||
this.core.exports.memory.buffer,
|
||||
DICT_BASE,
|
||||
0x10000
|
||||
);
|
||||
const s4 = new Int32Array(
|
||||
this.core.exports.memory.buffer,
|
||||
DICT_BASE,
|
||||
0x10000
|
||||
);
|
||||
let p = latest;
|
||||
while (p != 0) {
|
||||
// console.log("P", p);
|
||||
const wordLength = u8[p - DICT_BASE + 4] & 0x1f;
|
||||
const hidden = u8[p - DICT_BASE + 4] & 0x20;
|
||||
const immediate = (u8[p - DICT_BASE + 4] & 0x80) != 0;
|
||||
if (hidden == 0 && wordLength === length) {
|
||||
let ok = true;
|
||||
for (let i = 0; i < length; ++i) {
|
||||
if (word[i] !== u8[p - DICT_BASE + 5 + i]) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
// console.log("Found!");
|
||||
out[0] = p;
|
||||
out[1] = immediate ? 1 : -1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
p = s4[(p - DICT_BASE) / 4];
|
||||
}
|
||||
out[1] = 0;
|
||||
// console.log("Not found");
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
|
115
src/waforth.wat
115
src/waforth.wat
|
@ -134,8 +134,6 @@
|
|||
(import "shell" "load" (func $shell_load (param i32 i32) (result i32)))
|
||||
(import "shell" "debug" (func $shell_debug (param i32)))
|
||||
|
||||
(import "tmp" "find" (func $tmpFind (param i32 i32)))
|
||||
|
||||
(memory (export "memory") (!/ !memorySize 65536))
|
||||
|
||||
(type $void (func))
|
||||
|
@ -459,8 +457,53 @@
|
|||
|
||||
;; 6.1.1550
|
||||
(func $find (export "FIND")
|
||||
(call $tmpFind (get_global $latest) (get_global $tos))
|
||||
(set_global $tos (i32.add (get_global $tos) (i32.const 4))))
|
||||
(local $entryP i32)
|
||||
(local $entryNameP i32)
|
||||
(local $entryLF i32)
|
||||
(local $wordP i32)
|
||||
(local $wordStart i32)
|
||||
(local $wordLength i32)
|
||||
(local $wordEnd i32)
|
||||
|
||||
(set_local $wordLength
|
||||
(i32.load (tee_local $wordStart (i32.load (i32.sub (get_global $tos)
|
||||
(i32.const 4))))))
|
||||
(set_local $wordStart (i32.add (get_local $wordStart) (i32.const 4)))
|
||||
(set_local $wordEnd (i32.add (get_local $wordStart) (get_local $wordLength)))
|
||||
|
||||
(set_local $entryP (get_global $latest))
|
||||
(block $endLoop
|
||||
(loop $loop
|
||||
(set_local $entryLF (i32.load (i32.add (get_local $entryP) (i32.const 4))))
|
||||
(block $endCompare
|
||||
(if (i32.and
|
||||
(i32.eq (i32.and (get_local $entryLF) (i32.const !fHidden)) (i32.const 0))
|
||||
(i32.eq (i32.and (get_local $entryLF) (i32.const !lengthMask))
|
||||
(get_local $wordLength)))
|
||||
(then
|
||||
(set_local $wordP (get_local $wordStart))
|
||||
(set_local $entryNameP (i32.add (get_local $entryP) (i32.const 5)))
|
||||
(block $endCompareLoop
|
||||
(loop $compareLoop
|
||||
(br_if $endCompare (i32.ne (i32.load8_s (get_local $entryNameP))
|
||||
(i32.load8_s (get_local $wordP))))
|
||||
(set_local $entryNameP (i32.add (get_local $entryNameP) (i32.const 1)))
|
||||
(set_local $wordP (i32.add (get_local $wordP) (i32.const 1)))
|
||||
(br_if $endCompareLoop (i32.eq (get_local $wordP)
|
||||
(get_local $wordEnd)))
|
||||
(br $compareLoop)))
|
||||
(i32.store (i32.sub (get_global $tos) (i32.const 4))
|
||||
(get_local $entryP))
|
||||
(if (i32.eq (i32.and (get_local $entryLF) (i32.const !fImmediate)) (i32.const 0))
|
||||
(then
|
||||
(call $push (i32.const -1)))
|
||||
(else
|
||||
(call $push (i32.const 1))))
|
||||
(return))))
|
||||
(set_local $entryP (i32.load (get_local $entryP)))
|
||||
(br_if $endLoop (i32.eqz (get_local $entryP)))
|
||||
(br $loop)))
|
||||
(call $push (i32.const 0)))
|
||||
(!def_word "FIND" "$find")
|
||||
|
||||
;; 6.1.1650
|
||||
|
@ -1115,6 +1158,70 @@ EOF
|
|||
(if (i32.ne (call $interpret) (i32.const 0))
|
||||
(unreachable)))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; For benchmarking
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(func $sieve1_prime
|
||||
(call $here)
|
||||
(call $plus)
|
||||
(call $c-fetch)
|
||||
(call $zero-equals))
|
||||
|
||||
(func $sieve1_composite
|
||||
(call $here)
|
||||
(call $plus)
|
||||
(call $push (i32.const 1))
|
||||
(call $swap)
|
||||
(call $c-store))
|
||||
|
||||
(func $sieve1 (export "sieve1")
|
||||
(call $here)
|
||||
(call $over)
|
||||
(call $erase)
|
||||
(call $push (i32.const 2))
|
||||
(block $label$1
|
||||
(loop $label$2
|
||||
(call $two-dupe)
|
||||
(call $dupe)
|
||||
(call $star)
|
||||
(call $greater-than)
|
||||
(br_if $label$1 (i32.eqz (call $pop)))
|
||||
(call $dupe)
|
||||
(call $sieve1_prime)
|
||||
(if (i32.ne (call $pop) (i32.const 0))
|
||||
(block
|
||||
(call $two-dupe)
|
||||
(call $dupe)
|
||||
(call $star)
|
||||
(call $beginDo)
|
||||
(block $label$4
|
||||
(loop $label$5
|
||||
(call $i)
|
||||
(call $sieve1_composite)
|
||||
(call $dupe)
|
||||
(br_if $label$4 (call $endDo (call $pop)))
|
||||
(br $label$5)))))
|
||||
(call $one-plus)
|
||||
(br $label$2)))
|
||||
(call $drop)
|
||||
(call $push (i32.const 1))
|
||||
(call $swap)
|
||||
(call $push (i32.const 2))
|
||||
(call $beginDo)
|
||||
(block $label$6
|
||||
(loop $label$7
|
||||
(call $i)
|
||||
(call $sieve1_prime)
|
||||
(if (i32.ne (call $pop) (i32.const 0))
|
||||
(block
|
||||
(call $drop)
|
||||
(call $i)))
|
||||
(br_if $label$6 (call $endDo (i32.const 1)))
|
||||
(br $label$7))))
|
||||
(!def_word "sieve1" "$sieve1")
|
||||
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Data
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
|
Loading…
Reference in a new issue