diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-01-27 13:22:56 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-02-26 11:07:06 +0900 |
| commit | 900377463a8264871ee42e488112b076b7973b6a (patch) | |
| tree | bf727d3484ca7c8d35f18ef29ea8d9305d4b57da /src/Execution | |
| parent | e2495878b872b341e6e04eed31dd255b1a6e256f (diff) | |
| download | php-waddiwasi-900377463a8264871ee42e488112b076b7973b6a.tar.gz php-waddiwasi-900377463a8264871ee42e488112b076b7973b6a.tar.zst php-waddiwasi-900377463a8264871ee42e488112b076b7973b6a.zip | |
feat: partially implement execution
Diffstat (limited to 'src/Execution')
45 files changed, 1562 insertions, 0 deletions
diff --git a/src/Execution/Allocator.php b/src/Execution/Allocator.php new file mode 100644 index 0000000..ad62fc7 --- /dev/null +++ b/src/Execution/Allocator.php @@ -0,0 +1,162 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Modules\ExportDescs; +use Nsfisis\Waddiwasi\Structure\Modules\Func; +use Nsfisis\Waddiwasi\Structure\Modules\Module; +use Nsfisis\Waddiwasi\Structure\Types\GlobalType; +use Nsfisis\Waddiwasi\Structure\Types\MemType; +use Nsfisis\Waddiwasi\Structure\Types\RefType; +use Nsfisis\Waddiwasi\Structure\Types\TableType; + +final readonly class Allocator +{ + public function __construct( + private readonly Store $store, + ) { + } + + /** + * @param list<ExternVal> $externVals + */ + public function allocPreInitModule( + Module $module, + array $externVals, + ): ModuleInst { + $m = new ModuleInst($module->types, [], [], [], [], [], [], []); + + foreach ($externVals as $externVal) { + if ($externVal instanceof ExternVals\Global_) { + $m->globalAddrs[] = $externVal->addr; + } + } + foreach ($module->funcs as $func) { + $m->funcAddrs[] = $this->allocFunc($func, $m); + } + + return $m; + } + + /** + * @param list<ExternVal> $externVals + * @param list<Val> $vals + * @param list<list<Ref>> $refsList + * @param list<FuncAddr> $preAllocatedFuncs + */ + public function allocModule( + Module $module, + array $externVals, + array $vals, + array $refsList, + array $preAllocatedFuncs, + ): ModuleInst { + $m = new ModuleInst($module->types, [], [], [], [], [], [], []); + + foreach ($externVals as $externVal) { + match ($externVal::class) { + ExternVals\Func::class => $m->funcAddrs[] = $externVal->addr, + ExternVals\Table::class => $m->tableAddrs[] = $externVal->addr, + ExternVals\Mem::class => $m->memAddrs[] = $externVal->addr, + ExternVals\Global_::class => $m->globalAddrs[] = $externVal->addr, + default => throw new \RuntimeException("unreachable"), + }; + } + + foreach ($preAllocatedFuncs as $funcAddr) { + $m->funcAddrs[] = $funcAddr; + $funcInst = $this->store->funcs[$funcAddr->value]; + assert($funcInst instanceof FuncInsts\Wasm); + // @phpstan-ignore-next-line + $this->store->funcs[$funcAddr->value] = FuncInst::Wasm( + $funcInst->type, + $m, + $funcInst->code, + ); + } + foreach ($module->tables as $table) { + $m->tableAddrs[] = $this->allocTable($table->type, Ref::RefNull($table->type->refType)); + } + foreach ($module->mems as $mem) { + $m->memAddrs[] = $this->allocMem($mem->type); + } + foreach ($module->datas as $data) { + $m->dataAddrs[] = $this->allocData($data->init); + } + + foreach ($module->globals as $i => $global) { + $m->globalAddrs[] = $this->allocGlobal($global->type, $vals[$i]); + } + foreach ($module->elems as $i => $elem) { + $m->elemAddrs[] = $this->allocElem($elem->type, $refsList[$i]); + } + + foreach ($module->exports as $export) { + $value = match ($export->desc::class) { + ExportDescs\Func::class => ExternVal::Func($m->funcAddrs[$export->desc->func->value]), + ExportDescs\Table::class => ExternVal::Table($m->tableAddrs[$export->desc->table->value]), + ExportDescs\Mem::class => ExternVal::Mem($m->memAddrs[$export->desc->mem->value]), + ExportDescs\Global_::class => ExternVal::Global_($m->globalAddrs[$export->desc->global->value]), + default => throw new \RuntimeException("unreachable"), + }; + $m->exports[] = new ExportInst($export->name, $value); + } + + return $m; + } + + private function allocFunc(Func $func, ModuleInst $moduleInst): FuncAddr + { + $funcType = $moduleInst->types[$func->type->value]; + $funcInst = FuncInst::Wasm($funcType, $moduleInst, $func); + $this->store->funcs[] = $funcInst; + return new FuncAddr(count($this->store->funcs) - 1); + } + + private function allocTable(TableType $tableType, Ref $ref): TableAddr + { + $minSize = $tableType->limits->min; + $elem = array_fill(0, $minSize, $ref); + $tableInst = new TableInst($tableType, $elem); + $this->store->tables[] = $tableInst; + return new TableAddr(count($this->store->tables) - 1); + } + + private function allocMem(MemType $memType): MemAddr + { + $minSize = $memType->limits->min; + $data = array_fill(0, $minSize * 64 * 1024, 0); + $memInst = new MemInst($memType, $data); + $this->store->mems[] = $memInst; + return new MemAddr(count($this->store->mems) - 1); + } + + private function allocGlobal(GlobalType $globalType, Val $val): GlobalAddr + { + $globalInst = new GlobalInst($globalType, $val); + $this->store->globals[] = $globalInst; + return new GlobalAddr(count($this->store->globals) - 1); + } + + /** + * @param list<Ref> $elem + */ + private function allocElem(RefType $refType, array $elem): ElemAddr + { + $elemInst = new ElemInst($refType, $elem); + $this->store->elems[] = $elemInst; + return new ElemAddr(count($this->store->elems) - 1); + } + + /** + * @param list<Byte> $data + */ + private function allocData(array $data): DataAddr + { + $dataInst = new DataInst($data); + $this->store->datas[] = $dataInst; + return new DataAddr(count($this->store->datas) - 1); + } +} diff --git a/src/Execution/DataAddr.php b/src/Execution/DataAddr.php new file mode 100644 index 0000000..b3c4516 --- /dev/null +++ b/src/Execution/DataAddr.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class DataAddr +{ + public function __construct( + public int $value, + ) { + } +} diff --git a/src/Execution/DataInst.php b/src/Execution/DataInst.php new file mode 100644 index 0000000..0db1da5 --- /dev/null +++ b/src/Execution/DataInst.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class DataInst +{ + /** + * @param list<Byte> $data + */ + public function __construct( + public array $data, + ) { + } +} diff --git a/src/Execution/ElemAddr.php b/src/Execution/ElemAddr.php new file mode 100644 index 0000000..e46b54f --- /dev/null +++ b/src/Execution/ElemAddr.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class ElemAddr +{ + public function __construct( + public int $value, + ) { + } +} diff --git a/src/Execution/ElemInst.php b/src/Execution/ElemInst.php new file mode 100644 index 0000000..24e655c --- /dev/null +++ b/src/Execution/ElemInst.php @@ -0,0 +1,19 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Types\RefType; + +final readonly class ElemInst +{ + /** + * @param list<Ref> $elem + */ + public function __construct( + public RefType $type, + public array $elem, + ) { + } +} diff --git a/src/Execution/ExportInst.php b/src/Execution/ExportInst.php new file mode 100644 index 0000000..e4521b4 --- /dev/null +++ b/src/Execution/ExportInst.php @@ -0,0 +1,17 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class ExportInst +{ + /** + * @param Name $name + */ + public function __construct( + public string $name, + public ExternVal $value, + ) { + } +} diff --git a/src/Execution/ExternAddr.php b/src/Execution/ExternAddr.php new file mode 100644 index 0000000..954127c --- /dev/null +++ b/src/Execution/ExternAddr.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class ExternAddr +{ + public function __construct( + public int $value, + ) { + } +} diff --git a/src/Execution/ExternVal.php b/src/Execution/ExternVal.php new file mode 100644 index 0000000..f095225 --- /dev/null +++ b/src/Execution/ExternVal.php @@ -0,0 +1,28 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +abstract readonly class ExternVal +{ + final public static function Func(FuncAddr $addr): ExternVals\Func + { + return new ExternVals\Func($addr); + } + + final public static function Table(TableAddr $addr): ExternVals\Table + { + return new ExternVals\Table($addr); + } + + final public static function Mem(MemAddr $addr): ExternVals\Mem + { + return new ExternVals\Mem($addr); + } + + final public static function Global_(GlobalAddr $addr): ExternVals\Global_ + { + return new ExternVals\Global_($addr); + } +} diff --git a/src/Execution/ExternVals/Func.php b/src/Execution/ExternVals/Func.php new file mode 100644 index 0000000..314d44d --- /dev/null +++ b/src/Execution/ExternVals/Func.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\ExternVals; + +use Nsfisis\Waddiwasi\Execution\ExternVal; +use Nsfisis\Waddiwasi\Execution\FuncAddr; + +final readonly class Func extends ExternVal +{ + protected function __construct( + public FuncAddr $addr, + ) { + } +} diff --git a/src/Execution/ExternVals/Global_.php b/src/Execution/ExternVals/Global_.php new file mode 100644 index 0000000..ae38c78 --- /dev/null +++ b/src/Execution/ExternVals/Global_.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\ExternVals; + +use Nsfisis\Waddiwasi\Execution\ExternVal; +use Nsfisis\Waddiwasi\Execution\GlobalAddr; + +final readonly class Global_ extends ExternVal +{ + protected function __construct( + public GlobalAddr $addr, + ) { + } +} diff --git a/src/Execution/ExternVals/Mem.php b/src/Execution/ExternVals/Mem.php new file mode 100644 index 0000000..7bded5a --- /dev/null +++ b/src/Execution/ExternVals/Mem.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\ExternVals; + +use Nsfisis\Waddiwasi\Execution\ExternVal; +use Nsfisis\Waddiwasi\Execution\MemAddr; + +final readonly class Mem extends ExternVal +{ + protected function __construct( + public MemAddr $addr, + ) { + } +} diff --git a/src/Execution/ExternVals/Table.php b/src/Execution/ExternVals/Table.php new file mode 100644 index 0000000..cd36bb2 --- /dev/null +++ b/src/Execution/ExternVals/Table.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\ExternVals; + +use Nsfisis\Waddiwasi\Execution\ExternVal; +use Nsfisis\Waddiwasi\Execution\TableAddr; + +final readonly class Table extends ExternVal +{ + protected function __construct( + public TableAddr $addr, + ) { + } +} diff --git a/src/Execution/FuncAddr.php b/src/Execution/FuncAddr.php new file mode 100644 index 0000000..101c7c9 --- /dev/null +++ b/src/Execution/FuncAddr.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class FuncAddr +{ + public function __construct( + public int $value, + ) { + } +} diff --git a/src/Execution/FuncInst.php b/src/Execution/FuncInst.php new file mode 100644 index 0000000..c69eb80 --- /dev/null +++ b/src/Execution/FuncInst.php @@ -0,0 +1,21 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Modules\Func; +use Nsfisis\Waddiwasi\Structure\Types\FuncType; + +abstract readonly class FuncInst +{ + final public static function Wasm(FuncType $type, ModuleInst $module, Func $code): FuncInsts\Wasm + { + return new FuncInsts\Wasm($type, $module, $code); + } + + final public static function Host(FuncType $type, callable $code): FuncInsts\Host + { + return new FuncInsts\Host($type, $code); + } +} diff --git a/src/Execution/FuncInsts/Host.php b/src/Execution/FuncInsts/Host.php new file mode 100644 index 0000000..e3cba4a --- /dev/null +++ b/src/Execution/FuncInsts/Host.php @@ -0,0 +1,20 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\FuncInsts; + +use Nsfisis\Waddiwasi\Execution\FuncInst; +use Nsfisis\Waddiwasi\Structure\Types\FuncType; + +final readonly class Host extends FuncInst +{ + /** + * @param callable $code + */ + public function __construct( + public FuncType $type, + public mixed $code, + ) { + } +} diff --git a/src/Execution/FuncInsts/Wasm.php b/src/Execution/FuncInsts/Wasm.php new file mode 100644 index 0000000..88eea61 --- /dev/null +++ b/src/Execution/FuncInsts/Wasm.php @@ -0,0 +1,20 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\FuncInsts; + +use Nsfisis\Waddiwasi\Execution\FuncInst; +use Nsfisis\Waddiwasi\Execution\ModuleInst; +use Nsfisis\Waddiwasi\Structure\Modules\Func; +use Nsfisis\Waddiwasi\Structure\Types\FuncType; + +final readonly class Wasm extends FuncInst +{ + public function __construct( + public FuncType $type, + public ModuleInst $module, + public Func $code, + ) { + } +} diff --git a/src/Execution/GlobalAddr.php b/src/Execution/GlobalAddr.php new file mode 100644 index 0000000..f9a08ff --- /dev/null +++ b/src/Execution/GlobalAddr.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class GlobalAddr +{ + public function __construct( + public int $value, + ) { + } +} diff --git a/src/Execution/GlobalInst.php b/src/Execution/GlobalInst.php new file mode 100644 index 0000000..47bd9a0 --- /dev/null +++ b/src/Execution/GlobalInst.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Types\GlobalType; + +final readonly class GlobalInst +{ + public function __construct( + public GlobalType $type, + public Val $value, + ) { + } +} diff --git a/src/Execution/MemAddr.php b/src/Execution/MemAddr.php new file mode 100644 index 0000000..5c141cc --- /dev/null +++ b/src/Execution/MemAddr.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class MemAddr +{ + public function __construct( + public int $value, + ) { + } +} diff --git a/src/Execution/MemInst.php b/src/Execution/MemInst.php new file mode 100644 index 0000000..3999052 --- /dev/null +++ b/src/Execution/MemInst.php @@ -0,0 +1,19 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Types\MemType; + +final readonly class MemInst +{ + /** + * @param list<Byte> $data + */ + public function __construct( + public MemType $type, + public array $data, + ) { + } +} diff --git a/src/Execution/ModuleInst.php b/src/Execution/ModuleInst.php new file mode 100644 index 0000000..2cd7f08 --- /dev/null +++ b/src/Execution/ModuleInst.php @@ -0,0 +1,32 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Types\FuncType; + +final class ModuleInst +{ + /** + * @param list<FuncType> $types + * @param list<FuncAddr> $funcAddrs + * @param list<TableAddr> $tableAddrs + * @param list<MemAddr> $memAddrs + * @param list<GlobalAddr> $globalAddrs + * @param list<ElemAddr> $elemAddrs + * @param list<DataAddr> $dataAddrs + * @param list<ExportInst> $exports + */ + public function __construct( + public array $types, + public array $funcAddrs, + public array $tableAddrs, + public array $memAddrs, + public array $globalAddrs, + public array $elemAddrs, + public array $dataAddrs, + public array $exports, + ) { + } +} diff --git a/src/Execution/Num.php b/src/Execution/Num.php new file mode 100644 index 0000000..03fec4f --- /dev/null +++ b/src/Execution/Num.php @@ -0,0 +1,40 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +abstract readonly class Num +{ + /** + * @param S32 $value + */ + final public static function I32(int $value): Nums\I32 + { + return new Nums\I32($value); + } + + /** + * @param S64 $value + */ + final public static function I64(int $value): Nums\I64 + { + return new Nums\I64($value); + } + + /** + * @param F32 $value + */ + final public static function F32(float $value): Nums\F32_ + { + return new Nums\F32_($value); + } + + /** + * @param F64 $value + */ + final public static function F64(float $value): Nums\F64_ + { + return new Nums\F64_($value); + } +} diff --git a/src/Execution/Nums/F32_.php b/src/Execution/Nums/F32_.php new file mode 100644 index 0000000..5c7b749 --- /dev/null +++ b/src/Execution/Nums/F32_.php @@ -0,0 +1,18 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Nums; + +use Nsfisis\Waddiwasi\Execution\Num; + +final readonly class F32_ extends Num +{ + /** + * @param F32 $value + */ + public function __construct( + public float $value, + ) { + } +} diff --git a/src/Execution/Nums/F64_.php b/src/Execution/Nums/F64_.php new file mode 100644 index 0000000..c24709e --- /dev/null +++ b/src/Execution/Nums/F64_.php @@ -0,0 +1,18 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Nums; + +use Nsfisis\Waddiwasi\Execution\Num; + +final readonly class F64_ extends Num +{ + /** + * @param F64 $value + */ + public function __construct( + public float $value, + ) { + } +} diff --git a/src/Execution/Nums/I32.php b/src/Execution/Nums/I32.php new file mode 100644 index 0000000..fbc6fab --- /dev/null +++ b/src/Execution/Nums/I32.php @@ -0,0 +1,18 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Nums; + +use Nsfisis\Waddiwasi\Execution\Num; + +final readonly class I32 extends Num +{ + /** + * @param S32 $value + */ + public function __construct( + public int $value, + ) { + } +} diff --git a/src/Execution/Nums/I64.php b/src/Execution/Nums/I64.php new file mode 100644 index 0000000..889b863 --- /dev/null +++ b/src/Execution/Nums/I64.php @@ -0,0 +1,18 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Nums; + +use Nsfisis\Waddiwasi\Execution\Num; + +final readonly class I64 extends Num +{ + /** + * @param S64 $value + */ + public function __construct( + public int $value, + ) { + } +} diff --git a/src/Execution/Ref.php b/src/Execution/Ref.php new file mode 100644 index 0000000..4f0c3c1 --- /dev/null +++ b/src/Execution/Ref.php @@ -0,0 +1,25 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Types\RefType; + +abstract readonly class Ref +{ + final public static function RefNull(RefType $type): Refs\RefNull + { + return new Refs\RefNull($type); + } + + final public static function RefFunc(FuncAddr $addr): Refs\RefFunc + { + return new Refs\RefFunc($addr); + } + + final public static function RefExtern(ExternAddr $addr): Refs\RefExtern + { + return new Refs\RefExtern($addr); + } +} diff --git a/src/Execution/Refs/RefExtern.php b/src/Execution/Refs/RefExtern.php new file mode 100644 index 0000000..4beccb4 --- /dev/null +++ b/src/Execution/Refs/RefExtern.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Refs; + +use Nsfisis\Waddiwasi\Execution\ExternAddr; +use Nsfisis\Waddiwasi\Execution\Ref; + +final readonly class RefExtern extends Ref +{ + public function __construct( + public ExternAddr $addr, + ) { + } +} diff --git a/src/Execution/Refs/RefFunc.php b/src/Execution/Refs/RefFunc.php new file mode 100644 index 0000000..449b780 --- /dev/null +++ b/src/Execution/Refs/RefFunc.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Refs; + +use Nsfisis\Waddiwasi\Execution\FuncAddr; +use Nsfisis\Waddiwasi\Execution\Ref; + +final readonly class RefFunc extends Ref +{ + public function __construct( + public FuncAddr $addr, + ) { + } +} diff --git a/src/Execution/Refs/RefNull.php b/src/Execution/Refs/RefNull.php new file mode 100644 index 0000000..b4323d7 --- /dev/null +++ b/src/Execution/Refs/RefNull.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Refs; + +use Nsfisis\Waddiwasi\Execution\Ref; +use Nsfisis\Waddiwasi\Structure\Types\RefType; + +final readonly class RefNull extends Ref +{ + public function __construct( + public RefType $type, + ) { + } +} diff --git a/src/Execution/Result.php b/src/Execution/Result.php new file mode 100644 index 0000000..e07752c --- /dev/null +++ b/src/Execution/Result.php @@ -0,0 +1,21 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +abstract readonly class Result +{ + /** + * @param list<Val> $values + */ + final public static function Values(array $values): Results\Values + { + return new Results\Values($values); + } + + final public static function Trap(): Results\Trap + { + return new Results\Trap(); + } +} diff --git a/src/Execution/Results/Trap.php b/src/Execution/Results/Trap.php new file mode 100644 index 0000000..eb676b2 --- /dev/null +++ b/src/Execution/Results/Trap.php @@ -0,0 +1,11 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Results; + +use Nsfisis\Waddiwasi\Execution\Result; + +final readonly class Trap extends Result +{ +} diff --git a/src/Execution/Results/Values.php b/src/Execution/Results/Values.php new file mode 100644 index 0000000..c3a9e36 --- /dev/null +++ b/src/Execution/Results/Values.php @@ -0,0 +1,19 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Results; + +use Nsfisis\Waddiwasi\Execution\Result; +use Nsfisis\Waddiwasi\Execution\Val; + +final readonly class Values extends Result +{ + /** + * @param list<Val> $values + */ + protected function __construct( + public array $values, + ) { + } +} diff --git a/src/Execution/Runtime.php b/src/Execution/Runtime.php new file mode 100644 index 0000000..030c46e --- /dev/null +++ b/src/Execution/Runtime.php @@ -0,0 +1,561 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Instructions\Instr; +use Nsfisis\Waddiwasi\Structure\Instructions\Instrs; +use Nsfisis\Waddiwasi\Structure\Modules\DataModes; +use Nsfisis\Waddiwasi\Structure\Modules\ElemModes; +use Nsfisis\Waddiwasi\Structure\Modules\Module; +use Nsfisis\Waddiwasi\Structure\Types\DataIdx; +use Nsfisis\Waddiwasi\Structure\Types\ElemIdx; + +// @todo split this class into multiple classes +final readonly class Runtime +{ + private function __construct( + private readonly Store $store, + private readonly Stack $stack, + private readonly ModuleInst $module, + ) { + } + + /** + * @param list<ExternVal> $externVals + */ + public static function instantiate( + Store $store, + Module $module, + array $externVals, + ): self { + $allocator = new Allocator($store); + + $moduleInstInit = $allocator->allocPreInitModule($module, $externVals); + + $stack = new Stack([]); + $frameInit = StackEntry::Frame(0, [], $moduleInstInit); + $stack->push($frameInit); + + $runtimeInit = new self($store, $stack, $moduleInstInit); + + $vals = []; + foreach ($module->globals as $global) { + $instrs = $global->init->instrs; + array_pop($instrs); // drop "end" + $vals[] = $runtimeInit->evalInstrs($instrs); + assert($stack->top() === $frameInit); + } + + $refsList = []; + foreach ($module->elems as $elem) { + $refs = []; + foreach ($elem->init as $expr) { + $instrs = $expr->instrs; + array_pop($instrs); // drop "end" + $result = $runtimeInit->evalInstrs($instrs); + assert($result instanceof Vals\Ref); + $refs[] = $result->inner; + } + $refsList[] = $refs; + } + + assert($stack->top() === $frameInit); + $stack->pop(); + + $moduleInst = $allocator->allocModule( + $module, + $externVals, + $vals, + $refsList, + $moduleInstInit->funcAddrs, + ); + + $runtime = new self($store, $stack, $moduleInst); + + $frame = StackEntry::Frame(0, [], $moduleInst); + $stack->push($frame); + + foreach ($module->elems as $i => $elem) { + if ($elem->mode instanceof ElemModes\Active) { + $n = count($elem->init); + $instrs = $elem->mode->offset->instrs; + array_pop($instrs); // drop "end" + $instrs[] = Instr::I32Const(0); + $instrs[] = Instr::I32Const($n); + $instrs[] = Instr::TableInit($elem->mode->table, new ElemIdx($i)); + $instrs[] = Instr::ElemDrop(new ElemIdx($i)); + $runtime->execInstrs($instrs); + } elseif ($elem->mode instanceof ElemModes\Declarative) { + $runtime->execInstrs([Instr::ElemDrop(new ElemIdx($i))]); + } + } + foreach ($module->datas as $i => $data) { + if ($data->mode instanceof DataModes\Active) { + assert($data->mode->memory->value === 0); + $n = count($data->init); + $instrs = $data->mode->offset->instrs; + array_pop($instrs); // drop "end" + $instrs[] = Instr::I32Const(0); + $instrs[] = Instr::I32Const($n); + $instrs[] = Instr::MemoryInit(new DataIdx($i)); + $instrs[] = Instr::DataDrop(new DataIdx($i)); + $runtime->execInstrs($instrs); + } + } + + if ($module->start !== null) { + $runtime->execInstrs([Instr::Call($module->start->func)]); + } + + assert($stack->top() === $frame); + $stack->pop(); + + return new self($store, $stack, $moduleInst); + } + + public function invoke(): void + { + } + + /** + * @param list<Instr> $instrs + */ + private function execInstrs(array $instrs): void + { + foreach ($instrs as $i) { + $this->interpret($i); + } + } + + /** + * @param list<Instr> $instrs + */ + private function evalInstrs(array $instrs): Val + { + $this->execInstrs($instrs); + $result = $this->stack->pop(); + assert($result instanceof StackEntries\Value); + return $result->inner; + } + + private function interpret(Instr $instr): void + { + if ($instr instanceof Instrs\Numeric\F32Abs) { + $v = $this->stack->pop(); + assert($v instanceof StackEntries\Value); + assert($v->inner instanceof Vals\Num); + assert($v->inner->inner instanceof Nums\F32_); + $this->stack->push(StackEntry::Value(Val::Num(Num::F32($v->inner->inner)))); + } elseif ($instr instanceof Instrs\Numeric\F32Add) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Ceil) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Const) { + $this->stack->push(StackEntry::Value(Val::Num(Num::F32($instr->value)))); + } elseif ($instr instanceof Instrs\Numeric\F32ConvertI32S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32ConvertI32U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32ConvertI64S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32ConvertI64U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32CopySign) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32DemoteF64) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Div) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Eq) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Floor) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Ge) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Gt) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Le) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Lt) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Max) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Min) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Mul) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Ne) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Nearest) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Neg) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32ReinterpretI32) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32ReinterpretI64) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Sqrt) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Sub) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F32Trunc) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Abs) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Add) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Ceil) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Const) { + $this->stack->push(StackEntry::Value(Val::Num(Num::F64($instr->value)))); + } elseif ($instr instanceof Instrs\Numeric\F64ConvertI32S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64ConvertI32U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64ConvertI64S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64ConvertI64U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64CopySign) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Div) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Eq) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Floor) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Ge) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Gt) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Le) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Lt) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Max) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Min) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Mul) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Ne) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Nearest) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Neg) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64PromoteF32) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64ReinterpretI32) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64ReinterpretI64) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Sqrt) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Sub) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\F64Trunc) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Add) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32And) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Clz) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Const) { + $this->stack->push(StackEntry::Value(Val::Num(Num::I32($instr->value)))); + } elseif ($instr instanceof Instrs\Numeric\I32Ctz) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32DivS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32DivU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Eq) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Eqz) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Extend16S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Extend8S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32GeS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32GeU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32GtS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32GtU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32LeS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32LeU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32LtS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32LtU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Mul) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Ne) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Or) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Popcnt) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32ReinterpretF32) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32ReinterpretF64) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32RemS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32RemU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32RotL) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32RotR) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Shl) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32ShrS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32ShrU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Sub) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32TruncF32S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32TruncF32U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32TruncF64S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32TruncF64U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32TruncSatF32S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32TruncSatF32U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32TruncSatF64S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32TruncSatF64U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32WrapI64) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I32Xor) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Add) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64And) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Clz) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Const) { + $this->stack->push(StackEntry::Value(Val::Num(Num::I64($instr->value)))); + } elseif ($instr instanceof Instrs\Numeric\I64Ctz) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64DivS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64DivU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Eq) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Eqz) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Extend16S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Extend32S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Extend8S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64ExtendI32S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64ExtendI32U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64GeS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64GeU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64GtS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64GtU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64LeS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64LeU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64LtS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64LtU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Mul) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Ne) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Or) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Popcnt) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64ReinterpretF32) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64ReinterpretF64) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64RemS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64RemU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64RotL) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64RotR) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Shl) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64ShrS) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64ShrU) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Sub) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64TruncF32S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64TruncF32U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64TruncF64S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64TruncF64U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64TruncSatF32S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64TruncSatF32U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64TruncSatF64S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64TruncSatF64U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Numeric\I64Xor) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Reference\RefFunc) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Reference\RefIsNull) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Reference\RefNull) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Parametric\Drop) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Parametric\Select) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Variable\GlobalGet) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Variable\GlobalSet) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Variable\LocalGet) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Variable\LocalSet) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Variable\LocalTee) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Table\ElemDrop) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Table\TableCopy) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Table\TableFill) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Table\TableGet) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Table\TableGrow) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Table\TableInit) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Table\TableSet) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Table\TableSize) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\DataDrop) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\F32Load) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\F32Store) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\F64Load) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\F64Store) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I32Load) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I32Load16S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I32Load16U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I32Load8S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I32Load8U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I32Store) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I32Store16) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I32Store8) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Load) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Load16S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Load16U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Load32S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Load32U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Load8S) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Load8U) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Store) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Store16) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Store32) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\I64Store8) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\MemoryCopy) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\MemoryFill) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\MemoryGrow) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\MemoryInit) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Memory\MemorySize) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\Block) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\Br) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\BrIf) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\BrTable) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\Call) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\CallIndirect) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\Else_) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\End) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\If_) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\Loop) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\Nop) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\Return_) { + assert(false, "not implemented " . $instr::class); + } elseif ($instr instanceof Instrs\Control\Unreachable) { + assert(false, "not implemented " . $instr::class); + } else { + assert(false); + } + } +} diff --git a/src/Execution/Stack.php b/src/Execution/Stack.php new file mode 100644 index 0000000..1fa2dae --- /dev/null +++ b/src/Execution/Stack.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final class Stack +{ + /** + * @param list<StackEntry> $entries + */ + public function __construct( + private array $entries, + ) { + } + + public function push(StackEntry $entry): void + { + $this->entries[] = $entry; + } + + public function pop(): ?StackEntry + { + return array_pop($this->entries); + } + + public function top(): ?StackEntry + { + $n = array_key_last($this->entries); + return $n === null ? null : $this->entries[$n]; + } + + public function count(): int + { + return count($this->entries); + } + + public function isEmpty(): bool + { + return $this->count() === 0; + } +} diff --git a/src/Execution/StackEntries/Frame.php b/src/Execution/StackEntries/Frame.php new file mode 100644 index 0000000..f8bd68e --- /dev/null +++ b/src/Execution/StackEntries/Frame.php @@ -0,0 +1,23 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\StackEntries; + +use Nsfisis\Waddiwasi\Execution\ModuleInst; +use Nsfisis\Waddiwasi\Execution\StackEntry; +use Nsfisis\Waddiwasi\Execution\Val; + +final readonly class Frame extends StackEntry +{ + /** + * @param int<0, max> $arity + * @param list<Val> $locals + */ + public function __construct( + public int $arity, + public array $locals, + public ModuleInst $module, + ) { + } +} diff --git a/src/Execution/StackEntries/Label.php b/src/Execution/StackEntries/Label.php new file mode 100644 index 0000000..b83fb0f --- /dev/null +++ b/src/Execution/StackEntries/Label.php @@ -0,0 +1,21 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\StackEntries; + +use Nsfisis\Waddiwasi\Execution\StackEntry; +use Nsfisis\Waddiwasi\Structure\Instructions\Instr; + +final readonly class Label extends StackEntry +{ + /** + * @param int<0, max> $arity + * @param list<Instr> $target + */ + public function __construct( + public int $arity, + public array $target, + ) { + } +} diff --git a/src/Execution/StackEntries/Value.php b/src/Execution/StackEntries/Value.php new file mode 100644 index 0000000..1cd1146 --- /dev/null +++ b/src/Execution/StackEntries/Value.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\StackEntries; + +use Nsfisis\Waddiwasi\Execution\StackEntry; +use Nsfisis\Waddiwasi\Execution\Val; + +final readonly class Value extends StackEntry +{ + public function __construct( + public Val $inner, + ) { + } +} diff --git a/src/Execution/StackEntry.php b/src/Execution/StackEntry.php new file mode 100644 index 0000000..8a9d7dd --- /dev/null +++ b/src/Execution/StackEntry.php @@ -0,0 +1,38 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Instructions\Instr; + +abstract readonly class StackEntry +{ + final public static function Value(Val $inner): StackEntries\Value + { + return new StackEntries\Value($inner); + } + + /** + * @param int<0, max> $arity + * @param list<Instr> $target + */ + final public static function Label( + int $arity, + array $target, + ): StackEntries\Label { + return new StackEntries\Label($arity, $target); + } + + /** + * @param int<0, max> $arity + * @param list<Val> $locals + */ + final public static function Frame( + int $arity, + array $locals, + ModuleInst $module, + ): StackEntries\Frame { + return new StackEntries\Frame($arity, $locals, $module); + } +} diff --git a/src/Execution/Store.php b/src/Execution/Store.php new file mode 100644 index 0000000..b7b0dfc --- /dev/null +++ b/src/Execution/Store.php @@ -0,0 +1,31 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final class Store +{ + /** + * @param list<FuncInst> $funcs + * @param list<TableInst> $tables + * @param list<MemInst> $mems + * @param list<GlobalInst> $globals + * @param list<ElemInst> $elems + * @param list<DataInst> $datas + */ + public function __construct( + public array $funcs, + public array $tables, + public array $mems, + public array $globals, + public array $elems, + public array $datas, + ) { + } + + public static function empty(): self + { + return new self([], [], [], [], [], []); + } +} diff --git a/src/Execution/TableAddr.php b/src/Execution/TableAddr.php new file mode 100644 index 0000000..e41eef1 --- /dev/null +++ b/src/Execution/TableAddr.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class TableAddr +{ + public function __construct( + public int $value, + ) { + } +} diff --git a/src/Execution/TableInst.php b/src/Execution/TableInst.php new file mode 100644 index 0000000..5a6766d --- /dev/null +++ b/src/Execution/TableInst.php @@ -0,0 +1,19 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +use Nsfisis\Waddiwasi\Structure\Types\TableType; + +final readonly class TableInst +{ + /** + * @param list<Ref> $elem + */ + public function __construct( + public TableType $type, + public array $elem, + ) { + } +} diff --git a/src/Execution/Val.php b/src/Execution/Val.php new file mode 100644 index 0000000..287f441 --- /dev/null +++ b/src/Execution/Val.php @@ -0,0 +1,18 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +abstract readonly class Val +{ + final public static function Num(Num $inner): Vals\Num + { + return new Vals\Num($inner); + } + + final public static function Ref(Ref $inner): Vals\Ref + { + return new Vals\Ref($inner); + } +} diff --git a/src/Execution/Vals/Num.php b/src/Execution/Vals/Num.php new file mode 100644 index 0000000..15b4433 --- /dev/null +++ b/src/Execution/Vals/Num.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Vals; + +use Nsfisis\Waddiwasi\Execution\Num as OrigNum; +use Nsfisis\Waddiwasi\Execution\Val; + +final readonly class Num extends Val +{ + public function __construct( + public OrigNum $inner, + ) { + } +} diff --git a/src/Execution/Vals/Ref.php b/src/Execution/Vals/Ref.php new file mode 100644 index 0000000..8ea8a94 --- /dev/null +++ b/src/Execution/Vals/Ref.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution\Vals; + +use Nsfisis\Waddiwasi\Execution\Ref as OrigRef; +use Nsfisis\Waddiwasi\Execution\Val; + +final readonly class Ref extends Val +{ + public function __construct( + public OrigRef $inner, + ) { + } +} |
