From 900377463a8264871ee42e488112b076b7973b6a Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 27 Jan 2024 13:22:56 +0900 Subject: feat: partially implement execution --- src/Execution/Allocator.php | 162 ++++++++++ src/Execution/DataAddr.php | 13 + src/Execution/DataInst.php | 16 + src/Execution/ElemAddr.php | 13 + src/Execution/ElemInst.php | 19 ++ src/Execution/ExportInst.php | 17 ++ src/Execution/ExternAddr.php | 13 + src/Execution/ExternVal.php | 28 ++ src/Execution/ExternVals/Func.php | 16 + src/Execution/ExternVals/Global_.php | 16 + src/Execution/ExternVals/Mem.php | 16 + src/Execution/ExternVals/Table.php | 16 + src/Execution/FuncAddr.php | 13 + src/Execution/FuncInst.php | 21 ++ src/Execution/FuncInsts/Host.php | 20 ++ src/Execution/FuncInsts/Wasm.php | 20 ++ src/Execution/GlobalAddr.php | 13 + src/Execution/GlobalInst.php | 16 + src/Execution/MemAddr.php | 13 + src/Execution/MemInst.php | 19 ++ src/Execution/ModuleInst.php | 32 ++ src/Execution/Num.php | 40 +++ src/Execution/Nums/F32_.php | 18 ++ src/Execution/Nums/F64_.php | 18 ++ src/Execution/Nums/I32.php | 18 ++ src/Execution/Nums/I64.php | 18 ++ src/Execution/Ref.php | 25 ++ src/Execution/Refs/RefExtern.php | 16 + src/Execution/Refs/RefFunc.php | 16 + src/Execution/Refs/RefNull.php | 16 + src/Execution/Result.php | 21 ++ src/Execution/Results/Trap.php | 11 + src/Execution/Results/Values.php | 19 ++ src/Execution/Runtime.php | 561 +++++++++++++++++++++++++++++++++++ src/Execution/Stack.php | 42 +++ src/Execution/StackEntries/Frame.php | 23 ++ src/Execution/StackEntries/Label.php | 21 ++ src/Execution/StackEntries/Value.php | 16 + src/Execution/StackEntry.php | 38 +++ src/Execution/Store.php | 31 ++ src/Execution/TableAddr.php | 13 + src/Execution/TableInst.php | 19 ++ src/Execution/Val.php | 18 ++ src/Execution/Vals/Num.php | 16 + src/Execution/Vals/Ref.php | 16 + 45 files changed, 1562 insertions(+) create mode 100644 src/Execution/Allocator.php create mode 100644 src/Execution/DataAddr.php create mode 100644 src/Execution/DataInst.php create mode 100644 src/Execution/ElemAddr.php create mode 100644 src/Execution/ElemInst.php create mode 100644 src/Execution/ExportInst.php create mode 100644 src/Execution/ExternAddr.php create mode 100644 src/Execution/ExternVal.php create mode 100644 src/Execution/ExternVals/Func.php create mode 100644 src/Execution/ExternVals/Global_.php create mode 100644 src/Execution/ExternVals/Mem.php create mode 100644 src/Execution/ExternVals/Table.php create mode 100644 src/Execution/FuncAddr.php create mode 100644 src/Execution/FuncInst.php create mode 100644 src/Execution/FuncInsts/Host.php create mode 100644 src/Execution/FuncInsts/Wasm.php create mode 100644 src/Execution/GlobalAddr.php create mode 100644 src/Execution/GlobalInst.php create mode 100644 src/Execution/MemAddr.php create mode 100644 src/Execution/MemInst.php create mode 100644 src/Execution/ModuleInst.php create mode 100644 src/Execution/Num.php create mode 100644 src/Execution/Nums/F32_.php create mode 100644 src/Execution/Nums/F64_.php create mode 100644 src/Execution/Nums/I32.php create mode 100644 src/Execution/Nums/I64.php create mode 100644 src/Execution/Ref.php create mode 100644 src/Execution/Refs/RefExtern.php create mode 100644 src/Execution/Refs/RefFunc.php create mode 100644 src/Execution/Refs/RefNull.php create mode 100644 src/Execution/Result.php create mode 100644 src/Execution/Results/Trap.php create mode 100644 src/Execution/Results/Values.php create mode 100644 src/Execution/Runtime.php create mode 100644 src/Execution/Stack.php create mode 100644 src/Execution/StackEntries/Frame.php create mode 100644 src/Execution/StackEntries/Label.php create mode 100644 src/Execution/StackEntries/Value.php create mode 100644 src/Execution/StackEntry.php create mode 100644 src/Execution/Store.php create mode 100644 src/Execution/TableAddr.php create mode 100644 src/Execution/TableInst.php create mode 100644 src/Execution/Val.php create mode 100644 src/Execution/Vals/Num.php create mode 100644 src/Execution/Vals/Ref.php (limited to 'src/Execution') 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 @@ + $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 $externVals + * @param list $vals + * @param list> $refsList + * @param list $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 $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 $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 @@ + $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 @@ + $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 @@ + $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 @@ + $types + * @param list $funcAddrs + * @param list $tableAddrs + * @param list $memAddrs + * @param list $globalAddrs + * @param list $elemAddrs + * @param list $dataAddrs + * @param list $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 @@ + $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 @@ + $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 @@ + $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 $instrs + */ + private function execInstrs(array $instrs): void + { + foreach ($instrs as $i) { + $this->interpret($i); + } + } + + /** + * @param list $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 @@ + $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 @@ + $arity + * @param list $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 @@ + $arity + * @param list $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 @@ + $arity + * @param list $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 $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 @@ + $funcs + * @param list $tables + * @param list $mems + * @param list $globals + * @param list $elems + * @param list $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 @@ + $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 @@ +