-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathAccumulator.php
More file actions
155 lines (131 loc) Β· 3.72 KB
/
Accumulator.php
File metadata and controls
155 lines (131 loc) Β· 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace PHPJava\Compiler\Emulator;
use PHPJava\Compiler\Builder\Attributes\Architects\Frames\Frame;
use PHPJava\Compiler\Builder\Generator\Operation\Operation;
use PHPJava\Exceptions\EmulatorException;
use PHPJava\Utilities\DebugTool;
class Accumulator
{
protected $backtraces = [];
protected $stacks = [];
protected $locals = [];
protected $frames = [];
protected $enableStack = true;
protected $effectiveProgramCounter = 0;
/**
* @var DebugTool
*/
protected $debugTool;
public static function factory()
{
return new static();
}
public function __construct()
{
$this->debugTool = new DebugTool(__CLASS__);
}
public function popFromOperandStack()
{
$this->debugTool->getLogger()->debug(
'Popped an item, remaining stacks: ' . (count($this->stacks) - 1)
);
if ((count($this->stacks) - 1) < 0) {
// Build backtrace
$backtrace = '';
foreach ($this->backtraces as [$programCounter, $operation]) {
/**
* @var Operation $operation
*/
$backtrace .= sprintf(
"%004s\t0x%02X\t%d\t%s\n",
$programCounter,
$operation->getOpCode(),
count($operation->getOperands()),
ltrim($operation->getMnemonic(), '_')
);
}
throw new EmulatorException(
'Cannot pop an item because stack is underflow.' . "\n" .
'Backtraces: ' . "\n" .
$backtrace
);
}
if (!$this->enableStack) {
// Dup a stack
$stack = array_pop($this->stacks);
$this->stacks[] = $stack;
return $stack;
}
return array_pop($this->stacks);
}
public function pushToOperandStack($item): self
{
$this->debugTool->getLogger()->debug(
'Stack an item, remaining stacks: ' . (count($this->stacks) + 1)
);
if (!$this->enableStack) {
return $this;
}
$this->stacks[] = $item;
return $this;
}
public function getFromLocal(int $index)
{
if (!array_key_exists($index, $this->locals)) {
throw new EmulatorException(
'Cannot get an item because item is not available.'
);
}
return $this->locals[$index];
}
public function setToLocal(int $index, $value): self
{
$this->locals[$index] = $value;
return $this;
}
public function getStacks(): array
{
return $this->stacks;
}
public function getLocals(): array
{
return $this->locals;
}
public function countStacks(): int
{
return count($this->stacks);
}
public function countLocals(): int
{
return count($this->locals);
}
public function enableStack(bool $enable): self
{
$this->enableStack = $enable;
return $this;
}
public function setEffectiveProgramCounter(int $programCounter): self
{
$this->effectiveProgramCounter = $programCounter;
return $this;
}
public function getEffectiveProgramCounter(): int
{
return $this->effectiveProgramCounter;
}
public function addFrame(Frame $frame): self
{
$this->frames[] = $frame;
return $this;
}
public function getFrames(): array
{
return $this->frames;
}
public function addBacktrace(int $programCounter, Operation $operation): self
{
$this->backtraces[] = [$programCounter, $operation];
return $this;
}
}