-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathBlock.php
More file actions
executable file
·130 lines (106 loc) · 3.54 KB
/
Block.php
File metadata and controls
executable file
·130 lines (106 loc) · 3.54 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
<?php
/*
* This file is part of PHP-Compiler, a PHP CFG Compiler for PHP code
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/
namespace PHPCompiler;
// used as a property type.
// @phan-suppress-next-line PhanUnreferencedUseNormal
use PHPCfg\Func;
use PHPCfg\Block as CfgBlock;
use PHPCfg\Operand;
use PHPCompiler\VM\Context;
use PHPCompiler\VM\Variable;
class Block {
/**
* @var OpCode[] $opCodes
*/
public array $opCodes = [];
public array $blocks = [];
public int $nOpCodes = 0;
public ?Func $func = null;
public ?CfgBlock $orig;
private \SplObjectStorage $scope;
/**
* @var Variable[] $constants
*/
public array $constants = [];
public \SplObjectStorage $args;
public ?Handler $handler = null;
public function __construct(?CfgBlock $block) {
$this->orig = $block;
$this->scope = new \SplObjectStorage;
$this->args = new \SplObjectStorage;
}
public function getOperand(int $offset): Operand {
foreach ($this->scope as $operand) {
if ($this->scope[$operand] === $offset) {
return $operand;
}
}
}
public function getVarSlot(Operand $operand, bool $isRead): int {
if (!$this->scope->contains($operand)) {
$this->scope[$operand] = $this->scope->count();
if ($isRead) {
$this->args[$operand] = $this->scope[$operand];
}
}
return $this->scope[$operand];
}
public function registerConstant(Operand $operand, Variable $const): int {
$slot = $this->getVarSlot($operand, false);
$this->constants[$slot] = $const;
return $slot;
}
public function addOpCode(OpCode ...$ops): void {
foreach ($ops as $op) {
$this->nOpCodes++;
$this->opCodes[] = $op;
}
}
public function findSlot(Operand $op, Frame $frame): ?Variable {
if (!$this->scope->contains($op)) {
// check PHI vars
if (!is_null($frame->parent)) {
return $frame->parent->block->findSlot($op, $frame->parent);
}
return null;
}
$idx = $this->scope[$op];
return $frame->scope[$idx];
}
public function getFrame(Context $context, ?Frame $frame = null): Frame {
// Todo: build scope
$scope = [];
$scopeSize = $this->scope->count();
foreach ($this->scope as $op) {
$pos = $this->scope[$op];
if (isset($this->constants[$pos])) {
$scope[$pos] = $this->constants[$pos];
} elseif ($this->args->contains($op)) {
if (is_null($frame)) {
throw new \LogicException("Argument var with no parent frame, illegal");
}
$found = false;
$parent = $frame->block->findSlot($op, $frame);
if (!is_null($parent)) {
$scope[$pos] = $parent;
$found = true;
}
if (!$found) {
throw new \LogicException("Could not resolve argument");
}
} else {
$scope[$pos] = new Variable(Variable::TYPE_NULL);
}
}
$return = new Frame(null, $this, $frame, ...$scope);
if (!is_null($frame) && !is_null($frame->returnVar)) {
$return->returnVar = $frame->returnVar;
}
return $return;
}
}