Skip to content

Commit 777ca99

Browse files
committed
Implemente Intermediate Code Compiler
1 parent b6eb6ff commit 777ca99

File tree

76 files changed

+3416
-48
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+3416
-48
lines changed

docs/compiler/README-ja.md

Lines changed: 723 additions & 0 deletions
Large diffs are not rendered by default.

docs/compiler/README.md

Lines changed: 722 additions & 0 deletions
Large diffs are not rendered by default.

src/Compiler/Builder/Attribute.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder;
3+
4+
use PHPJava\Compiler\Builder\Finder\Result\FinderResultInterface;
5+
use PHPJava\Compiler\Builder\Maps\EntryMap;
6+
7+
class Attribute implements BuilderInterface, EntryInterface
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $value;
13+
14+
/**
15+
* @var array
16+
*/
17+
protected $attributes = [];
18+
19+
/**
20+
* @var bool
21+
*/
22+
protected $hasAttribute = false;
23+
24+
/**
25+
* @var FinderResultInterface
26+
*/
27+
protected $constantPoolIndex;
28+
29+
public function __construct(FinderResultInterface $constantPoolIndex)
30+
{
31+
$this->constantPoolIndex = $constantPoolIndex;
32+
}
33+
34+
public function getConstantPoolIndex(): int
35+
{
36+
/**
37+
* @var EntryMap $result
38+
*/
39+
$result = $this->constantPoolIndex->getResult();
40+
return $result
41+
->getEntryIndex();
42+
}
43+
44+
public function getValue(): string
45+
{
46+
return $this->value;
47+
}
48+
49+
public function setValue(string $value): EntryInterface
50+
{
51+
$this->value = $value;
52+
return $this;
53+
}
54+
55+
public function hasAttribute(): bool
56+
{
57+
return $this->hasAttribute;
58+
}
59+
60+
public function getAttributes(): array
61+
{
62+
return $this->attributes;
63+
}
64+
65+
public function setAttributes(array $attributes): EntryInterface
66+
{
67+
$this->attributes = $attributes;
68+
return $this;
69+
}
70+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder\Attributes\Architects;
3+
4+
class Architect implements ArchitectInterface
5+
{
6+
public static function init()
7+
{
8+
return new static();
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder\Attributes\Architects;
3+
4+
interface ArchitectInterface
5+
{
6+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder\Attributes\Architects;
3+
4+
use PHPJava\Compiler\Builder\Finder\Result\ConstantPoolFinderResult;
5+
use PHPJava\Compiler\Builder\Types\Uint16;
6+
use PHPJava\Compiler\Builder\Types\Uint32;
7+
use PHPJava\Compiler\Builder\Types\Uint64;
8+
use PHPJava\Compiler\Builder\Types\Uint8;
9+
use PHPJava\Core\JVM\Stream\BinaryWriter;
10+
11+
class Operation extends Architect implements ArchitectInterface
12+
{
13+
/**
14+
* @var array
15+
*/
16+
protected $codes = [];
17+
18+
public function add(int $opcode, array $arguments = []): self
19+
{
20+
$this->codes[] = [$opcode, $arguments];
21+
return $this;
22+
}
23+
24+
public function getValue(): string
25+
{
26+
$writer = new BinaryWriter(fopen('php://memory', 'r+'));
27+
foreach ($this->codes as [$opcode, $arguments]) {
28+
$writer->writeUnsignedByte($opcode);
29+
foreach ($arguments as [$type, $value]) {
30+
if ($value instanceof ConstantPoolFinderResult) {
31+
$value = $value
32+
->getResult()
33+
->getEntryIndex();
34+
}
35+
switch ($type) {
36+
case Uint8::class:
37+
$writer->writeUnsignedByte($value);
38+
break;
39+
case Uint16::class:
40+
$writer->writeUnsignedShort($value);
41+
break;
42+
case Uint32::class:
43+
$writer->writeUnsignedInt($value);
44+
break;
45+
case Uint64::class:
46+
$writer->writeUnsignedLong($value);
47+
break;
48+
}
49+
}
50+
}
51+
return $writer->getStreamContents();
52+
}
53+
54+
public function __toString(): string
55+
{
56+
return $this->getValue();
57+
}
58+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder\Attributes;
3+
4+
use PHPJava\Compiler\Builder\Attribute;
5+
use PHPJava\Core\JVM\Stream\BinaryWriter;
6+
7+
class Code extends Attribute
8+
{
9+
protected $hasAttribute = true;
10+
11+
protected $maxStacks = 0;
12+
protected $maxLocals = 0;
13+
protected $code = '';
14+
protected $exceptionTables = [];
15+
16+
public function setMaxStacks(int $number): self
17+
{
18+
$this->maxStacks = $number;
19+
return $this;
20+
}
21+
22+
public function setMaxLocals(int $number): self
23+
{
24+
$this->maxLocals = $number;
25+
return $this;
26+
}
27+
28+
public function setCode(string $code): self
29+
{
30+
$this->code = $code;
31+
return $this;
32+
}
33+
34+
public function setExceptionTables(array $exceptionTables): self
35+
{
36+
$this->exceptionTables = $exceptionTables;
37+
return $this;
38+
}
39+
40+
public function getValue(): string
41+
{
42+
$writer = new BinaryWriter(
43+
fopen('php://memory', 'r+')
44+
);
45+
46+
// max stack
47+
$writer->writeUnsignedShort($this->maxStacks);
48+
49+
// max locals
50+
$writer->writeUnsignedShort($this->maxLocals);
51+
52+
// code length
53+
$writer->writeUnsignedInt(strlen($this->code));
54+
55+
// code
56+
$writer->write($this->code);
57+
58+
// exception_tables_length
59+
$writer->writeUnsignedShort(count($this->exceptionTables));
60+
61+
// exception_tables
62+
foreach ($this->exceptionTables as [$startPc, $endPc, $handlerPc, $catchType]) {
63+
$writer->writeUnsignedShort($startPc);
64+
$writer->writeUnsignedShort($endPc);
65+
$writer->writeUnsignedShort($handlerPc);
66+
$writer->writeUnsignedShort($catchType);
67+
}
68+
69+
// attributes length
70+
$writer->writeUnsignedShort(count($this->getAttributes()));
71+
72+
// attributes
73+
foreach ($this->getAttributes() as $attribute) {
74+
/**
75+
* @var Attribute $attribute
76+
*/
77+
$writer->write($attribute->getValue());
78+
}
79+
80+
return $writer->getStreamContents();
81+
}
82+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder;
3+
4+
interface BuilderInterface
5+
{
6+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder\Collection;
3+
4+
use PHPJava\Compiler\Builder\EntryInterface;
5+
use PHPJava\Compiler\Builder\Structures\EntryCollectionInterface;
6+
use PHPJava\Exceptions\NotAllowedDeleteException;
7+
8+
abstract class AbstractEntryCollection implements EntryCollectionInterface, \ArrayAccess, \IteratorAggregate
9+
{
10+
protected $entries = [];
11+
12+
public function add(EntryInterface $entry): EntryCollectionInterface
13+
{
14+
$entryNumber = count($this->entries);
15+
$this->entries[] = $entry;
16+
return $this;
17+
}
18+
19+
public function offsetGet($offset)
20+
{
21+
return $this->entries[$offset];
22+
}
23+
24+
public function offsetSet($offset, $value)
25+
{
26+
if (!($value instanceof EntryInterface)) {
27+
throw new NotAllowedDeleteException('The value is not allowed type.');
28+
}
29+
$this->entries[$offset] = $value;
30+
}
31+
32+
public function offsetUnset($offset)
33+
{
34+
throw new NotAllowedDeleteException('The entry cannot delete because the entry is an immutable.');
35+
}
36+
37+
public function offsetExists($offset)
38+
{
39+
return isset($this->entries[$offset]);
40+
}
41+
42+
public function toArray(): array
43+
{
44+
return $this->entries;
45+
}
46+
47+
public function getIterator()
48+
{
49+
return new \ArrayIterator($this->entries);
50+
}
51+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder\Collection;
3+
4+
use PHPJava\Compiler\Builder\Structures\EntryCollectionInterface;
5+
6+
class Attributes extends AbstractEntryCollection implements EntryCollectionInterface, \ArrayAccess
7+
{
8+
}

0 commit comments

Comments
 (0)