aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Execution/Stack.php
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-03-14 00:28:30 +0900
committernsfisis <nsfisis@gmail.com>2024-03-14 00:28:30 +0900
commitf490780439450a06cf71eb40b453fc0154264337 (patch)
tree85e636efabc1e6ae6ae26e1cd58991e1b4dba560 /src/Execution/Stack.php
parent44f5eb882bcaab2f5a19a0d7680929d6b0463f62 (diff)
downloadphp-waddiwasi-f490780439450a06cf71eb40b453fc0154264337.tar.gz
php-waddiwasi-f490780439450a06cf71eb40b453fc0154264337.tar.zst
php-waddiwasi-f490780439450a06cf71eb40b453fc0154264337.zip
perf: unwrap StackEntry
Diffstat (limited to 'src/Execution/Stack.php')
-rw-r--r--src/Execution/Stack.php39
1 files changed, 21 insertions, 18 deletions
diff --git a/src/Execution/Stack.php b/src/Execution/Stack.php
index ab118c5..396cb29 100644
--- a/src/Execution/Stack.php
+++ b/src/Execution/Stack.php
@@ -9,32 +9,32 @@ use Nsfisis\Waddiwasi\Structure\Types\RefType;
final class Stack
{
/**
- * @var list<StackEntries\Frame>
+ * @var list<Frame>
*/
private array $frames = [];
/**
- * @param list<StackEntry> $entries
+ * @param list<int|float|Ref|Frame|Label> $entries
*/
public function __construct(
private array $entries,
) {
}
- public function pushFrame(StackEntries\Frame $frame): void
+ public function pushFrame(Frame $frame): void
{
$this->push($frame);
$this->frames[] = $frame;
}
- public function pushLabel(StackEntries\Label $label): void
+ public function pushLabel(Label $label): void
{
$this->push($label);
}
public function pushValue(int|float|Ref $val): void
{
- $this->push(StackEntry::Value($val));
+ $this->push($val);
}
public function pushBool(bool $value): void
@@ -57,10 +57,10 @@ final class Stack
$this->pushValue(Ref::RefExtern($addr));
}
- public function popFrame(): StackEntries\Frame
+ public function popFrame(): Frame
{
$result = $this->pop();
- assert($result instanceof StackEntries\Frame);
+ assert($result instanceof Frame);
array_pop($this->frames);
return $result;
}
@@ -68,8 +68,11 @@ final class Stack
public function popValue(): int|float|Ref
{
$result = $this->pop();
- assert($result instanceof StackEntries\Value, 'Expected a value on the stack, but got ' . print_r($result, true));
- return $result->inner;
+ assert(
+ is_int($result) || is_float($result) || $result instanceof Ref,
+ 'Expected a value on the stack, but got ' . print_r($result, true),
+ );
+ return $result;
}
/**
@@ -116,11 +119,11 @@ final class Stack
$results = [];
while (!$this->isEmpty()) {
$top = $this->pop();
- if ($top instanceof StackEntries\Value) {
- $results[] = $top->inner;
- } else {
- assert($top instanceof StackEntries\Label);
+ if ($top instanceof Label) {
break;
+ } else {
+ assert(is_int($top) || is_float($top) || $top instanceof Ref);
+ $results[] = $top;
}
}
return $results;
@@ -129,14 +132,14 @@ final class Stack
public function popEntriesToCurrentFrame(): void
{
while (!$this->isEmpty()) {
- if ($this->pop() instanceof StackEntries\Frame) {
+ if ($this->pop() instanceof Frame) {
break;
}
}
array_pop($this->frames);
}
- public function top(): ?StackEntry
+ public function top(): int|float|Ref|Frame|Label|null
{
$n = array_key_last($this->entries);
return $n === null ? null : $this->entries[$n];
@@ -152,18 +155,18 @@ final class Stack
return $this->count() === 0;
}
- public function currentFrame(): StackEntries\Frame
+ public function currentFrame(): Frame
{
assert(count($this->frames) !== 0);
return $this->frames[count($this->frames) - 1];
}
- private function push(StackEntry $entry): void
+ private function push(int|float|Ref|Frame|Label $entry): void
{
$this->entries[] = $entry;
}
- private function pop(): ?StackEntry
+ private function pop(): int|float|Ref|Frame|Label|null
{
return array_pop($this->entries);
}