forked from ircmaxell/php-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VM.php
executable file
·318 lines (304 loc) · 13.8 KB
/
VM.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?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;
use PHPCompiler\VM\Context;
use PHPCompiler\VM\ClassEntry;
use PHPCompiler\VM\ObjectEntry;
use PHPCompiler\VM\Variable;
class VM {
const SUCCESS = 1;
const FAILURE = 2;
public Context $context;
public function __construct(Context $context) {
$this->context = $context;
}
public function run(Block $block): int {
if (!is_null($block->handler)) {
$block->handler->execute($block->getFrame($this->context));
return self::SUCCESS;
}
$this->context->push($block->getFrame($this->context));
nextframe:
$frame = $this->context->pop();
if (is_null($frame)) {
return self::SUCCESS;
}
restart:
while ($frame->pos < $frame->block->nOpCodes) {
$op = $frame->block->opCodes[$frame->pos++];
switch ($op->type) {
case OpCode::TYPE_TYPE_ASSERT:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
$arg1->copyFrom($arg2);
break;
case OpCode::TYPE_ASSIGN:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
$arg3 = $frame->scope[$op->arg3];
$arg2->copyFrom($arg3);
$arg1->copyFrom($arg3);
break;
case OpCode::TYPE_ARRAY_DIM_FETCH:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
if (is_null($op->arg3)) {
if ($arg2->type !== Variable::TYPE_ARRAY) {
throw new \LogicException('[] is only supported for arrays');
}
$arg1->indirect($arg2->toArray()->append(new Variable));
break;
}
$arg3 = $frame->scope[$op->arg3];
if ($arg2->type === Variable::TYPE_STRING) {
$arg1->string($arg2->toString()[$arg3->toInt()]);
} elseif ($arg2->type === Variable::TYPE_ARRAY) {
$arg1->indirect($arg2->toArray()->findVariable($arg3, false));
} else {
throw new \LogicException('Illegal offset');
}
break;
case OpCode::TYPE_CAST_BOOL:
$frame->scope[$op->arg1]->castFrom(Variable::TYPE_BOOLEAN, $frame->scope[$op->arg2]);
break;
case OpCode::TYPE_IDENTICAL:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
$arg3 = $frame->scope[$op->arg3];
$arg1->bool($arg2->identicalTo($arg3));
break;
case OpCode::TYPE_EQUAL:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
$arg3 = $frame->scope[$op->arg3];
$arg1->bool($arg2->equals($arg3));
break;
case OpCode::TYPE_SMALLER:
case OpCode::TYPE_GREATER:
case OpCode::TYPE_SMALLER_OR_EQUAL:
case OpCode::TYPE_GREATER_OR_EQUAL:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
$arg3 = $frame->scope[$op->arg3];
$arg1->compareOp($op->type, $arg2, $arg3);
break;
case OpCode::TYPE_PLUS:
case OpCode::TYPE_MINUS:
case OpCode::TYPE_MUL:
case OpCode::TYPE_DIV:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
$arg3 = $frame->scope[$op->arg3];
$arg1->numericOp($op->type, $arg2, $arg3);
break;
case OpCode::TYPE_BITWISE_AND:
case OpCode::TYPE_BITWISE_OR:
case OpCode::TYPE_BITWISE_XOR:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
$arg3 = $frame->scope[$op->arg3];
$arg1->bitwiseOp($op->type, $arg2, $arg3);
break;
case OpCode::TYPE_UNARY_MINUS:
case OpCode::TYPE_UNARY_PLUS:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
$arg1->unaryOp($op->type, $arg2);
break;
case OpCode::TYPE_CONCAT:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2]->toString();
$arg3 = $frame->scope[$op->arg3]->toString();
$arg1->string($arg2 . $arg3);
break;
case OpCode::TYPE_ECHO:
echo $frame->scope[$op->arg1]->toString();
break;
case OpCode::TYPE_PRINT:
echo $frame->scope[$op->arg2]->toString();
$frame->scope[$op->arg1]->int(1);
break;
case OpCode::TYPE_JUMP:
$frame = $op->block1->getFrame(
$this->context,
$frame
);
goto restart;
case OpCode::TYPE_JUMPIF:
$arg1 = $frame->scope[$op->arg1]->toBool();
if ($arg1) {
$frame = $op->block1->getFrame($this->context, $frame);
} else {
$frame = $op->block2->getFrame($this->context, $frame);
}
goto restart;
case OpCode::TYPE_CASE:
$arg1 = $frame->scope[$op->arg1];
$arg2 = $frame->scope[$op->arg2];
if ($arg1->equals($arg2)) {
$frame = $op->block1->getFrame($this->context, $frame);
goto restart;
}
break;
case OpCode::TYPE_CONST_FETCH:
$value = null;
if (!is_null($op->arg3)) {
// try NS constant fetch
$value = $this->context->constantFetch($frame->scope[$op->arg3]->toString());
}
if (is_null($value)) {
$value = $this->context->constantFetch($frame->scope[$op->arg2]->toString());
}
if (is_null($value)) {
return $this->raise('Unknown constant fetch', $frame);
}
$frame->scope[$op->arg1]->copyFrom($value);
break;
case OpCode::TYPE_RETURN_VOID:
// TODO
goto nextframe;
case OpCode::TYPE_RETURN:
if (is_null($frame->returnVar)) {
var_dump($frame);
}
$frame->returnVar->copyFrom($frame->scope[$op->arg1]);
goto nextframe;
case OpCode::TYPE_FUNCDEF:
$name = $frame->scope[$op->arg1]->toString();
$lcname = strtolower($name);
if (isset($this->context->functions[$lcname])) {
throw new \LogicException("Duplicate function definition for $lcname()");
}
$this->context->declareFunction(new Func\PHP($name, $op->block1));
break;
case OpCode::TYPE_FUNCCALL_INIT:
$name = $frame->scope[$op->arg1]->toString();
$lcname = strtolower($name);
if (!isset($this->context->functions[$lcname])) {
throw new \LogicException("Call to undefined function $lcname()");
}
$frame->call = $this->context->functions[$lcname];
$frame->callArgs = [];
break;
case OpCode::TYPE_ARG_SEND:
$frame->callArgs[] = $frame->scope[$op->arg1];
break;
case OpCode::TYPE_FUNCCALL_EXEC_RETURN:
case OpCode::TYPE_FUNCCALL_EXEC_NORETURN:
if (is_null($frame->call)) {
// Used for null constructors, etc
break;
}
$new = $frame->call->getFrame($this->context);
if ($op->type === OpCode::TYPE_FUNCCALL_EXEC_RETURN) {
$new->returnVar = $frame->scope[$op->arg1];
}
$new->calledArgs = $frame->callArgs;
if ($new->hasHandler()) {
$new->handler->execute($new);
break;
}
$this->context->push($frame);
$frame = $new;
goto restart;
case OpCode::TYPE_ARG_RECV:
// Todo: do type checks and transformations
$arg1 = $frame->scope[$op->arg1];
$arg1->copyFrom($frame->calledArgs[$op->arg2]);
break;
case OpCode::TYPE_DECLARE_CLASS:
$name = $frame->scope[$op->arg1]->toString();
$lcname = strtolower($name);
if (isset($this->context->classes[$lcname])) {
throw new \LogicException("Duplicate class definition for $name");
}
$classEntry = new ClassEntry($name);
self::defineClass($classEntry, $op->block1);
$this->context->classes[$lcname] = $classEntry;
break;
case OpCode::TYPE_NEW:
$result = $frame->scope[$op->arg1];
$name = $frame->scope[$op->arg2]->toString();
$lcname = strtolower($name);
if (!isset($this->context->classes[$lcname])) {
throw new \LogicException("Attempting to instantiate non-existing class $name");
}
$class = $this->context->classes[$lcname];
$result->object(new ObjectEntry($class));
$frame->call = $result->toObject()->constructor;
$frame->callArgs = [$result];
break;
case OpCode::TYPE_PROPERTY_FETCH:
$result = $frame->scope[$op->arg1];
$var = $frame->scope[$op->arg2]->resolveIndirect();
$name = $frame->scope[$op->arg3]->toString();
if ($var->type !== Variable::TYPE_OBJECT) {
throw new \LogicException("Unsupported property fetch on non-object");
}
$result->indirect($var->toObject()->getProperty($name));
break;
case OpCode::TYPE_INIT_ARRAY:
$result = $frame->scope[$op->arg1];
$result->newArray();
if (is_null($op->arg2)) {
break;
}
// Fall through intentional
case OpCode::TYPE_ADD_ARRAY_ELEMENT:
$result = $frame->scope[$op->arg1];
$ht = $result->toArray();
if (is_null($op->arg3)) {
$ht->append($frame->scope[$op->arg2]);
break;
}
$key = $frame->scope[$op->arg3]->resolveIndirect();
if ($key->is(Variable::TYPE_INTEGER)) {
$ht->addIndex($key->toInt(), $frame->scope[$op->arg2]);
} else {
$ht->add($key->toString(), $frame->scope[$op->arg2]);
}
break;
case OpCode::TYPE_BOOLEAN_NOT:
$value = !($frame->scope[$op->arg2]->toBool());
$dst = $frame->scope[$op->arg1];
$dst->bool($value);
break;
case OpCode::TYPE_INCLUDE:
$file = $frame->scope[$op->arg1]->toString();
$parsed = $this->context->runtime->parseAndCompileFile($file);
$new = $parsed->getFrame($this->context);
$this->context->push($frame);
$frame = $new;
goto restart;
default:
throw new \LogicException("VM OpCode Not Implemented: " . $op->getType());
}
}
return self::SUCCESS;
}
protected function defineClass(ClassEntry $entry, Block $block): void {
$frame = $block->getFrame($this->context);
// TODO
foreach ($block->opCodes as $op) {
switch ($op->type) {
case OpCode::TYPE_DECLARE_PROPERTY:
$name = $frame->scope[$op->arg1];
assert(is_null($op->arg2)); // no defaults for now
$entry->properties[] = new VM\ClassProperty(
$name->toString(),
null,
$frame->scope[$op->arg3]
);
break;
default:
var_dump($op);
throw new \LogicException('Other class body types are not jittable for now');
}
}
}
}