aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-03-15 19:02:22 +0900
committernsfisis <nsfisis@gmail.com>2024-03-15 19:02:22 +0900
commit9b45796547c8022b98a9254320323d72aecd81cb (patch)
tree5aaf49a070859ff4c226c4efef5de0174d51d36d /src
parentb2f2bbf72d7c5009f56f15fa972957ea399ad221 (diff)
downloadphp-waddiwasi-9b45796547c8022b98a9254320323d72aecd81cb.tar.gz
php-waddiwasi-9b45796547c8022b98a9254320323d72aecd81cb.tar.zst
php-waddiwasi-9b45796547c8022b98a9254320323d72aecd81cb.zip
refactor: remove Expr class
Diffstat (limited to 'src')
-rw-r--r--src/BinaryFormat/Code.php5
-rw-r--r--src/BinaryFormat/Decoder.php16
-rw-r--r--src/Execution/Runtime.php18
-rw-r--r--src/Structure/Instructions/Expr.php23
-rw-r--r--src/Structure/Modules/DataMode.php7
-rw-r--r--src/Structure/Modules/DataModes/Active.php7
-rw-r--r--src/Structure/Modules/Elem.php4
-rw-r--r--src/Structure/Modules/ElemMode.php7
-rw-r--r--src/Structure/Modules/ElemModes/Active.php7
-rw-r--r--src/Structure/Modules/Func.php5
-rw-r--r--src/Structure/Modules/Global_.php7
11 files changed, 47 insertions, 59 deletions
diff --git a/src/BinaryFormat/Code.php b/src/BinaryFormat/Code.php
index cb0dbe6..6acf1e7 100644
--- a/src/BinaryFormat/Code.php
+++ b/src/BinaryFormat/Code.php
@@ -4,16 +4,17 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\BinaryFormat;
-use Nsfisis\Waddiwasi\Structure\Instructions\Expr;
+use Nsfisis\Waddiwasi\Structure\Instructions\Instr;
final readonly class Code
{
/**
* @param list<Locals> $compressedLocals
+ * @param list<Instr> $body
*/
public function __construct(
public array $compressedLocals,
- public Expr $body,
+ public array $body,
) {
}
}
diff --git a/src/BinaryFormat/Decoder.php b/src/BinaryFormat/Decoder.php
index 24089d0..30c5b56 100644
--- a/src/BinaryFormat/Decoder.php
+++ b/src/BinaryFormat/Decoder.php
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\BinaryFormat;
-use Nsfisis\Waddiwasi\Structure\Instructions\Expr;
use Nsfisis\Waddiwasi\Structure\Instructions\Instr;
use Nsfisis\Waddiwasi\Structure\Instructions\Instrs;
use Nsfisis\Waddiwasi\Structure\Instructions\Instrs\Control\BlockType;
@@ -434,7 +433,7 @@ final class Decoder
return new Elem(
RefType::FuncRef,
array_map(
- fn ($funcRef) => new Expr([Instr::RefFunc($funcRef)]),
+ fn ($funcRef) => [Instr::RefFunc($funcRef)],
$initFuncRefs,
),
ElemMode::Active(0, $offset),
@@ -445,7 +444,7 @@ final class Decoder
return new Elem(
$refType,
array_map(
- fn ($funcRef) => new Expr([Instr::RefFunc($funcRef)]),
+ fn ($funcRef) => [Instr::RefFunc($funcRef)],
$initFuncRefs,
),
ElemMode::Passive(),
@@ -458,7 +457,7 @@ final class Decoder
return new Elem(
$refType,
array_map(
- fn ($funcRef) => new Expr([Instr::RefFunc($funcRef)]),
+ fn ($funcRef) => [Instr::RefFunc($funcRef)],
$initFuncRefs,
),
ElemMode::Active($table, $offset),
@@ -469,7 +468,7 @@ final class Decoder
return new Elem(
$refType,
array_map(
- fn ($funcRef) => new Expr([Instr::RefFunc($funcRef)]),
+ fn ($funcRef) => [Instr::RefFunc($funcRef)],
$initFuncRefs,
),
ElemMode::Declarative(),
@@ -650,9 +649,12 @@ final class Decoder
return $this->decodeU32();
}
- private function decodeExpr(): Expr
+ /**
+ * @return list<Instr>
+ */
+ private function decodeExpr(): array
{
- return new Expr($this->decodeInstrsUntil([Instrs\Control\End::class])[0]);
+ return $this->decodeInstrsUntil([Instrs\Control\End::class])[0];
}
private function decodeInstr(): Instr
diff --git a/src/Execution/Runtime.php b/src/Execution/Runtime.php
index 3a67ec9..38759ea 100644
--- a/src/Execution/Runtime.php
+++ b/src/Execution/Runtime.php
@@ -55,9 +55,7 @@ final class Runtime
$vals = [];
foreach ($module->globals as $global) {
- $instrs = $global->init->instrs;
- array_pop($instrs); // drop "end"
- $vals[] = $runtimeInit->evalInstrsForInit($instrs);
+ $vals[] = $runtimeInit->evalInstrsForInit($global->init);
assert($stack->top() === $frameInit);
}
@@ -65,9 +63,7 @@ final class Runtime
foreach ($module->elems as $elem) {
$refs = [];
foreach ($elem->init as $expr) {
- $instrs = $expr->instrs;
- array_pop($instrs); // drop "end"
- $result = $runtimeInit->evalInstrsForInit($instrs);
+ $result = $runtimeInit->evalInstrsForInit($expr);
assert($result instanceof Ref);
$refs[] = $result;
}
@@ -93,8 +89,7 @@ final class Runtime
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 = $elem->mode->offset;
$instrs[] = Instr::I32Const(0);
$instrs[] = Instr::I32Const($n);
$instrs[] = Instr::TableInit($elem->mode->table, $i);
@@ -108,8 +103,7 @@ final class Runtime
if ($data->mode instanceof DataModes\Active) {
assert($data->mode->memory === 0);
$n = count($data->init);
- $instrs = $data->mode->offset->instrs;
- array_pop($instrs); // drop "end"
+ $instrs = $data->mode->offset;
$instrs[] = Instr::I32Const(0);
$instrs[] = Instr::I32Const($n);
$instrs[] = Instr::MemoryInit($i);
@@ -220,8 +214,6 @@ final class Runtime
$resultTypes = $fn->type->results->types;
$m = count($resultTypes);
$ts = $fn->code->locals;
- $instrs = $fn->code->body->instrs;
- array_pop($instrs); // drop "end"
$vals = [];
for ($i = 0; $i < $n; $i++) {
$vals[] = $this->stack->popValue();
@@ -237,7 +229,7 @@ final class Runtime
);
$this->activateFrame($f);
$l = new Label($m);
- $this->execInstrs($instrs, $l);
+ $this->execInstrs($fn->code->body, $l);
$this->deactivateFrame($m);
}
diff --git a/src/Structure/Instructions/Expr.php b/src/Structure/Instructions/Expr.php
deleted file mode 100644
index ceefcc0..0000000
--- a/src/Structure/Instructions/Expr.php
+++ /dev/null
@@ -1,23 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace Nsfisis\Waddiwasi\Structure\Instructions;
-
-final readonly class Expr
-{
- /**
- * @var non-empty-list<Instr>
- */
- public array $instrs;
-
- /**
- * @param list<Instr> $instrs
- */
- public function __construct(
- array $instrs,
- ) {
- $instrs[] = Instr::End();
- $this->instrs = $instrs;
- }
-}
diff --git a/src/Structure/Modules/DataMode.php b/src/Structure/Modules/DataMode.php
index 96cd7dc..e9b5a72 100644
--- a/src/Structure/Modules/DataMode.php
+++ b/src/Structure/Modules/DataMode.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\Structure\Modules;
-use Nsfisis\Waddiwasi\Structure\Instructions\Expr;
+use Nsfisis\Waddiwasi\Structure\Instructions\Instr;
abstract readonly class DataMode
{
@@ -13,9 +13,12 @@ abstract readonly class DataMode
return new DataModes\Passive();
}
+ /**
+ * @param list<Instr> $offset
+ */
final public static function Active(
int $memory,
- Expr $offset,
+ array $offset,
): DataModes\Active {
return new DataModes\Active($memory, $offset);
}
diff --git a/src/Structure/Modules/DataModes/Active.php b/src/Structure/Modules/DataModes/Active.php
index 1a46f3e..de52861 100644
--- a/src/Structure/Modules/DataModes/Active.php
+++ b/src/Structure/Modules/DataModes/Active.php
@@ -4,14 +4,17 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\Structure\Modules\DataModes;
-use Nsfisis\Waddiwasi\Structure\Instructions\Expr;
+use Nsfisis\Waddiwasi\Structure\Instructions\Instr;
use Nsfisis\Waddiwasi\Structure\Modules\DataMode;
final readonly class Active extends DataMode
{
+ /**
+ * @param list<Instr> $offset
+ */
protected function __construct(
public int $memory,
- public Expr $offset,
+ public array $offset,
) {
}
}
diff --git a/src/Structure/Modules/Elem.php b/src/Structure/Modules/Elem.php
index e074590..5050fad 100644
--- a/src/Structure/Modules/Elem.php
+++ b/src/Structure/Modules/Elem.php
@@ -4,13 +4,13 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\Structure\Modules;
-use Nsfisis\Waddiwasi\Structure\Instructions\Expr;
use Nsfisis\Waddiwasi\Structure\Types\RefType;
+use Nsfisis\Waddiwasi\Structure\Instructions\Instr;
final readonly class Elem
{
/**
- * @param list<Expr> $init
+ * @param list<list<Instr>> $init
*/
public function __construct(
public RefType $type,
diff --git a/src/Structure/Modules/ElemMode.php b/src/Structure/Modules/ElemMode.php
index 051a7e1..e3e7693 100644
--- a/src/Structure/Modules/ElemMode.php
+++ b/src/Structure/Modules/ElemMode.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\Structure\Modules;
-use Nsfisis\Waddiwasi\Structure\Instructions\Expr;
+use Nsfisis\Waddiwasi\Structure\Instructions\Instr;
abstract readonly class ElemMode
{
@@ -13,9 +13,12 @@ abstract readonly class ElemMode
return new ElemModes\Passive();
}
+ /**
+ * @param list<Instr> $offset
+ */
final public static function Active(
int $table,
- Expr $offset,
+ array $offset,
): ElemModes\Active {
return new ElemModes\Active($table, $offset);
}
diff --git a/src/Structure/Modules/ElemModes/Active.php b/src/Structure/Modules/ElemModes/Active.php
index bd5f123..5487542 100644
--- a/src/Structure/Modules/ElemModes/Active.php
+++ b/src/Structure/Modules/ElemModes/Active.php
@@ -4,14 +4,17 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\Structure\Modules\ElemModes;
-use Nsfisis\Waddiwasi\Structure\Instructions\Expr;
+use Nsfisis\Waddiwasi\Structure\Instructions\Instr;
use Nsfisis\Waddiwasi\Structure\Modules\ElemMode;
final readonly class Active extends ElemMode
{
+ /**
+ * @param list<Instr> $offset
+ */
protected function __construct(
public int $table,
- public Expr $offset,
+ public array $offset,
) {
}
}
diff --git a/src/Structure/Modules/Func.php b/src/Structure/Modules/Func.php
index bab1889..7c76b5e 100644
--- a/src/Structure/Modules/Func.php
+++ b/src/Structure/Modules/Func.php
@@ -4,17 +4,18 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\Structure\Modules;
-use Nsfisis\Waddiwasi\Structure\Instructions\Expr;
+use Nsfisis\Waddiwasi\Structure\Instructions\Instr;
final readonly class Func
{
/**
* @param list<Local> $locals
+ * @param list<Instr> $body
*/
public function __construct(
public int $type,
public array $locals,
- public Expr $body,
+ public array $body,
) {
}
}
diff --git a/src/Structure/Modules/Global_.php b/src/Structure/Modules/Global_.php
index 9d6d371..aae1706 100644
--- a/src/Structure/Modules/Global_.php
+++ b/src/Structure/Modules/Global_.php
@@ -4,14 +4,17 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\Structure\Modules;
-use Nsfisis\Waddiwasi\Structure\Instructions\Expr;
+use Nsfisis\Waddiwasi\Structure\Instructions\Instr;
use Nsfisis\Waddiwasi\Structure\Types\GlobalType;
final readonly class Global_
{
+ /**
+ * @param list<Instr> $init
+ */
public function __construct(
public GlobalType $type,
- public Expr $init,
+ public array $init,
) {
}
}