forked from ircmaxell/php-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjit.php
More file actions
executable file
·92 lines (83 loc) · 3.15 KB
/
jit.php
File metadata and controls
executable file
·92 lines (83 loc) · 3.15 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
<?php
declare(strict_types=1);
/**
* 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
*/
use PHPCompiler\Runtime;
use PHPCompiler\Block;
use PHPCompiler\JitMcjitEmbed;
use PHPCompiler\Web\Superglobals;
function php_compiler_jit_prepare_embed_code(string $filename, string $code): string
{
return JitMcjitEmbed::prepareClassless($code);
}
/**
* Run a script via MCJIT. CGI superglobals refresh each execution (issue #642, #2257):
* - `-q` / `-p` set QUERY_STRING / REQUEST_BODY (and POST method when body non-empty)
* - `REQUEST_METHOD`, `PATH_INFO`, `SCRIPT_NAME`, `HTTPS`, etc. come from the process
* environment (same keys as VM serve / AOT `__superglobals__refresh`)
*/
function run(string $filename, string $code, array $options): void
{
$code = php_compiler_jit_prepare_embed_code($filename, $code);
$runtime = new Runtime();
$queryString = $options['-q'] ?? null;
$postBody = $options['-p'] ?? null;
$scriptFilename = null;
if ('-' !== $filename && 'Command line code' !== $filename) {
$resolved = realpath($filename);
if (false !== $resolved) {
$scriptFilename = $resolved;
}
}
$queryArg = is_string($queryString) ? $queryString : null;
$postArg = is_string($postBody) ? $postBody : null;
Superglobals::populateFromEnvironment(
$runtime->vmContext,
$queryArg,
$postArg,
$scriptFilename
);
$debugFile = null;
if (isset($options['-y'])) {
if ($options['-y'] === true) {
$debugFile = str_replace('.php', '', $filename);
} else {
$debugFile = $options['-y'];
}
if (substr($debugFile, 0, 1) !== '/') {
$debugFile = getcwd().'/'.$debugFile;
}
$runtime->setDebug($debugFile);
}
$block = $runtime->parseAndCompile($code, $filename);
if (null !== $block && Block::requiresVmLowering($block)) {
// Generators, readonly, fibers, typed returns in script scope, etc. still VM-fallback (#2114).
// Try/catch/finally in functions uses MCJIT via TryCatchHelper (#4246).
} else {
$runtime->jit($block, $code, $filename);
}
if (! isset($options['-l'])) {
$runtime->syncJitSuperglobals($queryArg, $postArg, $scriptFilename);
$runtime->run($block);
}
}
// libffi RTLD_GLOBAL preload before MCJIT segfaults on php-compiler:22.04-dev (#98, #2055).
putenv('PHP_COMPILER_SKIP_LLVM_PRELOAD=1');
$_ENV['PHP_COMPILER_SKIP_LLVM_PRELOAD'] = '1';
$_SERVER['PHP_COMPILER_SKIP_LLVM_PRELOAD'] = '1';
if (
!(defined('PHP_COMPILER_LIB_SPINE_SMOKE') && PHP_COMPILER_LIB_SPINE_SMOKE)
&& !(\function_exists('php_compiler_cli_should_skip_entry_driver') && php_compiler_cli_should_skip_entry_driver())
) {
// Use literal require paths so self-host AOT/JIT can fold includes (#54, #1492).
require_once __DIR__.'/../src/cli.php';
php_compiler_cli_note_invocation_cwd();
chdir(__DIR__.'/..');
require_once 'src/cli.php';
require_once 'src/cli_driver.php';
php_compiler_cli_dispatch();
}