Skip to content

Commit f534678

Browse files
committed
Update
1 parent 94c77d6 commit f534678

File tree

16 files changed

+174
-113
lines changed

16 files changed

+174
-113
lines changed

src/Compiler/Builder/Attributes/StackMapTable.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use PHPJava\Kernel\Types\_Long;
2929
use PHPJava\Kernel\Types\_Short;
3030
use PHPJava\Utilities\ArrayTool;
31+
use PHPJava\Utilities\Formatter;
3132

3233
class StackMapTable extends Attribute
3334
{
@@ -125,6 +126,19 @@ public function getValue(): string
125126
$emulatedAccumulator = new Accumulator();
126127
$currentOffset = 0;
127128

129+
// foreach ($this->localVariables as $variableName => $variable) {
130+
// [$index, $classType] = $variable;
131+
// $emulatedAccumulator
132+
// ->setDefaultLocal(
133+
// $index,
134+
// [
135+
// VerificationTypeTag::ITEM_Object,
136+
// $this->getEnhancedConstantPool()
137+
// ->findClass($classType),
138+
// ]
139+
// );
140+
// }
141+
128142
foreach ($this->operations as $operation) {
129143
$programCounter = $this->calculateProgramCounterByOperationCodes(
130144
$this->operations,
@@ -163,7 +177,7 @@ static function (FullFrame $a, FullFrame $b) {
163177
);
164178

165179
/**
166-
* Remove duplicated branch target.
180+
* Remove duplicated branch target and frame type was zero.
167181
*
168182
* @var FullFrame $previousFrame
169183
*/
@@ -186,7 +200,9 @@ static function (array $carry, FullFrame $frame) use (&$entries) {
186200
[]
187201
);
188202

189-
// Build frame
203+
/**
204+
* @var FullFrame[] $frames
205+
*/
190206
$frames = [];
191207

192208
$previousFrame = null;
@@ -206,15 +222,13 @@ static function (array $carry, FullFrame $frame) use (&$entries) {
206222
$offsetDelta = $frame->getBranchTarget() - $previousFrame->getBranchTarget() - 1;
207223
}
208224

209-
if ((
210-
$previousFrame !== null
211-
&& ArrayTool::compare($previousFrame->getLocals(), $frame->getLocals())
212-
&& ArrayTool::compare($previousFrame->getStacks(), $frame->getStacks())
213-
)
214-
|| ($stacks === 0 && ($locals === 0 || $samePreviousLocalsAndCurrentLocals))
225+
if ($stacks === 0
226+
&& ($locals === 0 || $samePreviousLocalsAndCurrentLocals)
215227
) {
216228
$entry = SameFrame::init();
217-
} elseif ($stacks > 0 && ($locals === 0 || $samePreviousLocalsAndCurrentLocals)) {
229+
} elseif ($stacks === 1
230+
&& ($locals === 0 || $samePreviousLocalsAndCurrentLocals)
231+
) {
218232
$entry = SameLocals1StackItemFrame::init();
219233
} elseif ($stacks === 0 && $locals > 0) {
220234
$entry = AppendFrame::init();
@@ -275,11 +289,26 @@ static function (array $carry, FullFrame $frame) use (&$entries) {
275289
* @var FullFrame $entry
276290
*/
277291
$writer->writeUnsignedShort($entry->getOffsetDelta());
292+
$locals = [];
293+
foreach ($this->localVariables as $variableName => $variable) {
294+
[$index, $classType, $deepArray] = $variable;
295+
$classType = Formatter::buildSignature($classType, $deepArray);
296+
$locals[$index] = [
297+
VerificationTypeTag::ITEM_Object,
298+
$this->getEnhancedConstantPool()
299+
->findClass($classType),
300+
];
301+
}
302+
303+
ArrayTool::concat(
304+
$locals,
305+
...$entry->getLocals()
306+
);
278307

279-
$writer->writeUnsignedShort(count($entry->getLocals()));
308+
$writer->writeUnsignedShort(count($locals));
280309
$this->writeStackMapTableVerificationSegment(
281310
$writer,
282-
$entry->getLocals()
311+
$locals
283312
);
284313

285314
$writer->writeUnsignedShort(count($entry->getStacks()));
@@ -319,7 +348,7 @@ protected function writeStackMapTableVerificationSegment(BinaryWriter $writer, a
319348
// Nothing to do.
320349
break;
321350
case VerificationTypeTag::ITEM_Object:
322-
[$classEntry] = $segment[1];
351+
$classEntry = $segment[1];
323352
/**
324353
* @var ConstantPoolFinderResult $classEntry
325354
*/
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22
namespace PHPJava\Compiler\Emulator\Mnemonics;
33

4-
use PHPJava\Exceptions\NotImplementedException;
5-
64
class _ifgt extends AbstractOperationCode implements OperationCodeInterface
75
{
86
use \PHPJava\Compiler\Emulator\Traits\GeneralProcessor;
97

108
public function execute(): void
119
{
12-
throw new NotImplementedException(__CLASS__);
10+
$this->accumulator
11+
->popFromOperandStack();
12+
13+
$this->addFrame();
1314
}
1415
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
22
namespace PHPJava\Compiler\Emulator\Mnemonics;
33

4-
use PHPJava\Exceptions\NotImplementedException;
4+
use PHPJava\Kernel\Maps\VerificationTypeTag;
55

66
class _irem extends AbstractOperationCode implements OperationCodeInterface
77
{
88
use \PHPJava\Compiler\Emulator\Traits\GeneralProcessor;
99

1010
public function execute(): void
1111
{
12-
throw new NotImplementedException(__CLASS__);
12+
$this->accumulator->popFromOperandStack();
13+
$this->accumulator->popFromOperandStack();
14+
$this->accumulator->pushToOperandStack(
15+
[VerificationTypeTag::ITEM_Integer]
16+
);
1317
}
1418
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
22
namespace PHPJava\Compiler\Emulator\Mnemonics;
33

4-
use PHPJava\Exceptions\NotImplementedException;
5-
64
class _nop extends AbstractOperationCode implements OperationCodeInterface
75
{
86
use \PHPJava\Compiler\Emulator\Traits\GeneralProcessor;
97

108
public function execute(): void
119
{
12-
throw new NotImplementedException(__CLASS__);
10+
// Nothing to do
1311
}
1412
}

src/Compiler/Lang/Assembler/MethodAssembler.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPJava\Kernel\Maps\OpCode;
1818
use PHPJava\Kernel\Types\_Void;
1919
use PHPJava\Utilities\ArrayTool;
20+
use PHPJava\Utilities\Formatter;
2021

2122
/**
2223
* @method ClassAssembler getParentAssembler()
@@ -97,16 +98,34 @@ public function assemble(): void
9798
continue;
9899
}
99100

101+
$type = (string) $documentParameter->getType();
102+
100103
// Update variable detail.
101-
$parameters[$documentParameter->getVariableName()] = ltrim(
102-
(string) $documentParameter->getType(),
103-
'\\'
104+
$parameters[$documentParameter->getVariableName()] = [
105+
'type' => str_replace(
106+
'[]',
107+
'',
108+
ltrim(
109+
$type,
110+
'\\'
111+
)
112+
),
113+
'deep_array' => substr_count($type, '[]'),
114+
];
115+
116+
$className = Formatter::buildSignature(
117+
$parameters[$documentParameter->getVariableName()]['type'],
118+
$parameters[$documentParameter->getVariableName()]['deep_array']
104119
);
105120

121+
$this->getEnhancedConstantPool()
122+
->addClass($className);
123+
106124
// Fill local storage number.
107125
$this->assembleAssignVariable(
108126
$documentParameter->getVariableName(),
109-
$parameters[$documentParameter->getVariableName()]
127+
$parameters[$documentParameter->getVariableName()]['type'],
128+
$parameters[$documentParameter->getVariableName()]['deep_array']
110129
);
111130
}
112131
}

src/Compiler/Lang/Assembler/Processors/ExpressionProcessor.php

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\MethodCallable;
1919
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\NumberLoadable;
2020
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\Outputable;
21+
use PHPJava\Compiler\Lang\Assembler\Traits\NodeConvertible;
2122
use PHPJava\Compiler\Lang\Assembler\Traits\OperationManageable;
2223
use PHPJava\Exceptions\AssembleStructureException;
2324
use PHPJava\Kernel\Maps\OpCode;
@@ -31,6 +32,7 @@ class ExpressionProcessor extends AbstractProcessor implements ProcessorInterfac
3132
{
3233
use OperationManageable;
3334
use OperationCalculatableFromNode;
35+
use NodeConvertible;
3436
use ConstLoadableFromNode;
3537
use MagicConstLoadableFromNode;
3638
use StringLoadableFromNode;
@@ -141,86 +143,33 @@ public function execute(array $nodes, ?callable $callback = null): array
141143
[
142144
// Left operator.
143145
$expression->left,
144-
],
145-
$callback
146-
),
147-
...$this->execute(
148-
[
149-
// Right operator.
150146
$expression->right,
151147
],
152148
$callback
153149
)
154150
);
155151
break;
156152
case \PhpParser\Node\Expr\BinaryOp\BooleanAnd::class:
157-
ArrayTool::concat(
158-
$operations,
159-
...$this->assembleCalculateOperationFromNode(
160-
$expression->left,
161-
$expression->right,
162-
OpCode::_iand,
163-
$callback
164-
)
165-
);
166-
break;
153+
case \PhpParser\Node\Expr\BinaryOp\BooleanOr::class:
167154
case \PhpParser\Node\Expr\BinaryOp\Mul::class:
168-
ArrayTool::concat(
169-
$operations,
170-
...$this->assembleCalculateOperationFromNode(
171-
$expression->left,
172-
$expression->right,
173-
OpCode::_imul,
174-
$callback
175-
)
176-
);
177-
break;
178155
case \PhpParser\Node\Expr\BinaryOp\Div::class:
179-
// TODO: Add float converter
180-
ArrayTool::concat(
181-
$operations,
182-
...$this->assembleCalculateOperationFromNode(
183-
$expression->left,
184-
$expression->right,
185-
OpCode::_idiv,
186-
$callback
187-
)
188-
);
189-
break;
190156
case \PhpParser\Node\Expr\BinaryOp\Minus::class:
191-
ArrayTool::concat(
192-
$operations,
193-
...$this->assembleCalculateOperationFromNode(
194-
$expression->left,
195-
$expression->right,
196-
OpCode::_isub,
197-
$callback
198-
)
199-
);
200-
break;
201157
case \PhpParser\Node\Expr\BinaryOp\Plus::class:
202-
ArrayTool::concat(
203-
$operations,
204-
...$this->assembleCalculateOperationFromNode(
205-
$expression->left,
206-
$expression->right,
207-
OpCode::_iadd,
208-
$callback
209-
)
210-
);
211-
break;
212158
case \PhpParser\Node\Expr\BinaryOp\Mod::class:
213159
ArrayTool::concat(
214160
$operations,
215161
...$this->assembleCalculateOperationFromNode(
216162
$expression->left,
217163
$expression->right,
218-
OpCode::_irem,
164+
$this->convertNodeToOpCode($expression),
219165
$callback
220166
)
221167
);
222168
break;
169+
case \PhpParser\Node\Expr\BinaryOp\Greater::class:
223170
case \PhpParser\Node\Expr\BinaryOp\Smaller::class:
171+
case \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual::class:
172+
case \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual::class:
224173
ArrayTool::concat(
225174
$operations,
226175
...$this->assembleCalculateOperationFromNode(
@@ -230,7 +179,7 @@ public function execute(array $nodes, ?callable $callback = null): array
230179
$callback
231180
),
232181
...$this->assembleConditions(
233-
OpCode::_iflt,
182+
$this->convertNodeToOpCode($expression),
234183
[
235184
\PHPJava\Compiler\Builder\Generator\Operation\Operation::create(
236185
OpCode::_iconst_1
@@ -244,6 +193,7 @@ public function execute(array $nodes, ?callable $callback = null): array
244193
)
245194
);
246195
break;
196+
case \PhpParser\Node\Expr\BinaryOp\NotIdentical::class:
247197
case \PhpParser\Node\Expr\BinaryOp\Identical::class:
248198
/**
249199
* @var \PhpParser\Node\Expr\BinaryOp\Identical $conditionNode
@@ -286,7 +236,7 @@ public function execute(array $nodes, ?callable $callback = null): array
286236
ArrayTool::concat(
287237
$operations,
288238
...$this->assembleConditions(
289-
OpCode::_ifeq,
239+
$this->convertNodeToOpCode($expression),
290240
[
291241
\PHPJava\Compiler\Builder\Generator\Operation\Operation::create(
292242
OpCode::_iconst_1

src/Compiler/Lang/Assembler/Processors/ProcessorInterface.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
use PHPJava\Compiler\Builder\Attributes\Architects\Operation;
55
use PHPJava\Compiler\Builder\Collection\ConstantPool;
66
use PHPJava\Compiler\Builder\Finder\ConstantPoolFinder;
7-
use PHPJava\Compiler\Lang\Assembler\AbstractAssembler;
8-
use PHPJava\Compiler\Lang\Assembler\AssemblerInterface;
97
use PHPJava\Compiler\Lang\Assembler\Enhancer\ConstantPoolEnhancer;
108
use PHPJava\Compiler\Lang\Assembler\Store\Store;
119
use PHPJava\Compiler\Lang\Stream\StreamReaderInterface;
@@ -22,8 +20,6 @@
2220
* @method Store getStore()
2321
* @method AbstractProcessor setNamespace(?array $namespace)
2422
* @method AbstractProcessor setOperation(Operation $operation)
25-
* @method AbstractProcessor setParentAssembler(AssemblerInterface $parentAssembler)
26-
* @method AbstractAssembler getParentAssembler()
2723
*/
2824
interface ProcessorInterface
2925
{

src/Compiler/Lang/Assembler/Processors/StatementProcessor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function execute(array $nodes, ?callable $callback = null): array
6060
'Unknown statement: ' . get_class($statement) . ' on ' . get_class($this)
6161
);
6262
}
63+
if ($assembler === null) {
64+
continue;
65+
}
6366
/**
6467
* @var AbstractAssembler $assembler
6568
*/

src/Compiler/Lang/Assembler/Processors/Traits/OperationCalculatableFromNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ private function assembleCalculateOperationFromNode(Node $left, Node $right, int
2121
$operations,
2222
...$this->execute(
2323
[
24-
// Left operator.
25-
$right,
26-
2724
// Left operator.
2825
$left,
26+
27+
// Right operator.
28+
$right,
2929
],
3030
$callback
3131
),

0 commit comments

Comments
 (0)