refactor(php): 重构PHP编译器基础功能并优化代码结构

- 移除不支持函数列表的硬编码定义
- 添加对不支持函数调用的运行时检查和错误报告
- 简化静态属性和类常量解析中的self关键字处理逻辑
- 修复类方法调用中self关键字的替换逻辑
- 将扩展模块入口声明从static改为全局作用域
- 重写gen_stub.php脚本的主函数结构和命令行参数处理
- 添加类信息和函数信息的独立生成选项
- 优化桩文件生成时的跳过提示输出
- 重构翻译器中的类解析方法实现
- 更新不支持函数列表包含更多动态函数
pull/1/head
韩天峰 7 months ago
parent ec106db274
commit 8b0c3d2b72
  1. 56
      bin/gen_stub.php
  2. 20
      src/Php/CompilerBase.php
  3. 24
      src/Php/Translator.php
  4. 10
      src/cpp/main.cc
  5. 2
      src/template/extension.cc.php

@ -16,14 +16,6 @@ use PhpParser\Node\Stmt\Trait_;
use PhpParser\PrettyPrinter\Standard;
use PhpParser\PrettyPrinterAbstract;
error_reporting(E_ALL);
ini_set("precision", "-1");
require __DIR__ . '/bootstrap.php';
$translator = new PhpAot\Php\Translator(ROOT_PATH);
$translator->setIndent("\t");
$translator->setIndentLevel(1);
const PHP_70_VERSION_ID = 70000;
const PHP_80_VERSION_ID = 80000;
const PHP_81_VERSION_ID = 80100;
@ -94,6 +86,7 @@ function processStubFile(string $stubFile, Context $context, bool $includeOnly =
$oldStubHash = extractStubHash($arginfoFile);
if ($stubHash === $oldStubHash && !$context->forceParse) {
/* Stub file did not change, do not regenerate. */
echo "Skipping $stubFile, stub hash unchanged\n";
return null;
}
}
@ -4238,11 +4231,16 @@ class FileInfo {
/**
* @return iterable<FuncInfo>
*/
public function getAllFuncInfos(): iterable {
yield from $this->funcInfos;
public function getAllFuncInfos(): iterable
{
global $genClass;
if ($genClass) {
foreach ($this->classInfos as $classInfo) {
yield from $classInfo->funcInfos;
}
} else {
yield from $this->funcInfos;
}
}
/** @return array<string, ConstInfo> */
@ -5171,6 +5169,8 @@ function generateArgInfoCode(
$generatedFuncInfos = [];
global $genClass;
$argInfoCode = generateCodeWithConditions(
$fileInfo->getAllFuncInfos(), "\n",
static function (FuncInfo $funcInfo) use (&$generatedFuncInfos, $fileInfo) {
@ -5221,14 +5221,16 @@ function generateArgInfoCode(
$code .= generateFunctionEntries(null, $fileInfo->funcInfos);
if ($genClass) {
foreach ($fileInfo->classInfos as $classInfo) {
$code .= generateFunctionEntries($classInfo->name, $classInfo->funcInfos, $classInfo->cond);
}
}
}
$php80MinimumCompatibility = $fileInfo->getMinimumPhpVersionIdCompatibility() === null || $fileInfo->getMinimumPhpVersionIdCompatibility() >= PHP_80_VERSION_ID;
if ($fileInfo->generateClassEntries) {
if ($genClass and $fileInfo->generateClassEntries) {
$declaredStrings = [];
$attributeInitializationCode = generateFunctionAttributeInitialization($fileInfo->funcInfos, $allConstInfos, $fileInfo->getMinimumPhpVersionIdCompatibility(), null, $declaredStrings);
$attributeInitializationCode .= generateGlobalConstantAttributeInitialization($fileInfo->constInfos, $allConstInfos, $fileInfo->getMinimumPhpVersionIdCompatibility(), null, $declaredStrings);
@ -6097,15 +6099,27 @@ function initPhpParser() {
$isInitialized = true;
}
$optind = null;
function main()
{
global $argv, $argc, $genClass, $translator;
error_reporting(E_ALL);
ini_set("precision", "-1");
require __DIR__ . '/bootstrap.php';
$translator = new PhpAot\Php\Translator(ROOT_PATH);
$translator->setIndent("\t");
$translator->setIndentLevel(1);
$opt_index = 0;
$options = getopt(
"fh",
[
"force-regeneration", "parameter-stats", "help", "verify", "verify-manual", "replace-predefined-constants",
"generate-classsynopses", "replace-classsynopses", "generate-methodsynopses", "replace-methodsynopses",
"generate-optimizer-info",
"generate-optimizer-info", "gen-class-info", "gen-func-info",
],
$optind
$opt_index
);
$context = new Context;
@ -6118,6 +6132,15 @@ $replaceClassSynopses = isset($options["replace-classsynopses"]);
$generateMethodSynopses = isset($options["generate-methodsynopses"]);
$replaceMethodSynopses = isset($options["replace-methodsynopses"]);
$generateOptimizerInfo = isset($options["generate-optimizer-info"]);
if (isset($options["gen-class-info"])) {
$genClass = true;
} elseif (isset($options["gen-func-info"])) {
$genClass = false;
} else {
die("Please specify whether to generate class or function info\n");
}
$context->forceRegeneration = isset($options["f"]) || isset($options["force-regeneration"]);
$context->forceParse = $context->forceRegeneration || $printParameterStats || $verify || $verifyManual || $replacePredefinedConstants || $generateClassSynopses || $generateOptimizerInfo || $replaceClassSynopses || $generateMethodSynopses || $replaceMethodSynopses;
@ -6125,7 +6148,7 @@ if (isset($options["h"]) || isset($options["help"])) {
die("\nUsage: gen_stub.php [ -f | --force-regeneration ] [ --replace-predefined-constants ] [ --generate-classsynopses ] [ --replace-classsynopses ] [ --generate-methodsynopses ] [ --replace-methodsynopses ] [ --parameter-stats ] [ --verify ] [ --verify-manual ] [ --generate-optimizer-info ] [ -h | --help ] [ name.stub.php | directory ] [ directory ]\n\n");
}
$locations = array_slice($argv, $optind);
$locations = array_slice($argv, $opt_index);
$locationCount = count($locations);
if ($replacePredefinedConstants && $locationCount < 2) {
die("At least one source stub path and a target manual directory has to be provided:\n./build/gen_stub.php --replace-predefined-constants ./ ../doc-en/\n");
@ -6400,3 +6423,6 @@ if ($verifyManual) {
}
}
}
}
main();

@ -53,13 +53,7 @@ class CompilerBase extends \PhpAot\Core\Translator
'float' => self::TYPE_FLOAT,
'bool' => self::TYPE_BOOL,
];
protected array $reservedNames;
protected array $unsupportedFunctions = [
'compact',
'extract'
];
protected array $globalHeaders = [
'phpx.h',
'phpx_helper.h',
@ -135,7 +129,6 @@ class CompilerBase extends \PhpAot\Core\Translator
$climate = new CLImate();
$this->climate = $climate;
// $this->noLiteralStrings = $climate->arguments->get('no-literal-strings');
$this->noLiteralStrings = true;
}
public function setPhpxDir($dir): void
@ -1295,6 +1288,9 @@ class CompilerBase extends \PhpAot\Core\Translator
$name = '';
} elseif ($expr->name->getType() === 'Name') {
$name = $this->parseIdentifier($expr->name);
if (in_array($name, $this->unsupportedFunctions)) {
$this->fatalError($expr, 'Unsupported function: `' . $name . '`');
}
$nativeFn = $this->findNativeFunction($name);
if ($nativeFn) {
return self::PREFIX . $nativeFn . '(' . $this->parseCallArgs($expr->args, $name) . ')';
@ -2106,10 +2102,7 @@ class CompilerBase extends \PhpAot\Core\Translator
} elseif ($var instanceof Node\Expr\ArrayDimFetch) {
return $this->parseIdentifier($var->var) . ".offsetExists(" . $this->parseIdentifier($var->dim) . ')';
} elseif ($var instanceof Node\Expr\StaticPropertyFetch) {
$class = $this->parseIdentifier($var->class);
$prop = $var->name;
$class = $class === 'self' ? $this->class : $class;
return 'php::hasStaticProperty("' . $class . '", ' . $this->identifierToStr($prop) . ')';
return 'php::hasStaticProperty(' . $this->identifierToStr($var->class) . ', ' . $this->identifierToStr($var->name) . ')';
} elseif ($var instanceof Node\Expr\PropertyFetch) {
$object = $var->var;
$prop = $var->name;
@ -2228,6 +2221,9 @@ class CompilerBase extends \PhpAot\Core\Translator
}
return $id;
} else {
if ($id === 'self') {
$id = $this->class;
}
return '"'. $id . '"';
}
}
@ -2245,6 +2241,7 @@ class CompilerBase extends \PhpAot\Core\Translator
$fn = 'php::concat({' . $this->identifierToStr($expr->class). ', "::", ' . $this->identifierToStr($expr->name) . '})';
} else {
$class = $this->parseIdentifier($expr->class);
$class = $class === 'self' ? $this->class : $class;
$method = $this->parseIdentifier($expr->name);
$fn = '"' . $class . '::' . $method . '"';
}
@ -2263,6 +2260,7 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function parseClassConstFetch(Node\Expr\ClassConstFetch $expr): string
{
$class = $this->parseIdentifier($expr->class);
$class = ($class === 'self' or $class === 'this_') ? $this->class : $class;
$const = $this->parseIdentifier($expr->name);
return 'php::constant("' . $class . '::' . $const . '")';
}

@ -9,6 +9,13 @@ use PhpParser\NodeTraverser;
class Translator extends Preprocessor
{
protected bool $verbose = false;
protected array $unsupportedFunctions = [
'compact',
'extract',
'func_num_args',
'func_get_arg',
'func_get_args',
];
public function __construct(string $rootPath)
{
@ -358,15 +365,13 @@ class Translator extends Preprocessor
return $code;
}
protected function parseClass(Node\Stmt\Class_ $class): string
protected function genClassStubFile(Node\Stmt\Class_ $class, string $file): void
{
$this->class = $this->parseIdentifier($class->name);
if (!$this->stubFileIncluded) {
$genStubCmd = PHP_BINARY. ' ' . $this->rootPath . '/bin/gen_stub.php -f ' . $this->file;
$genStubCmd = PHP_BINARY. ' ' . $this->rootPath . '/bin/gen_stub.php --gen-class-info -f ' . $file;
$output = shell_exec($genStubCmd);
$this->climate->info('generate stub file: ' . $this->file);
$this->climate->info('generate stub file: ' . $file);
$this->climate->comment($genStubCmd);
$stubFilenameWithoutExtension = str_replace([".stub.php", '.php'], "", $this->file);
$stubFilenameWithoutExtension = str_replace([".stub.php", '.php'], "", $file);
$headerFile = $this->getArgInfoHeaderFile($stubFilenameWithoutExtension, true);
if (!str_starts_with($output, "Saved")) {
$this->fatalError($class, "failed to generate arginfo header file: `$headerFile`, output: $output");
@ -375,6 +380,13 @@ class Translator extends Preprocessor
$this->stubFileIncluded = true;
}
protected function parseClass(Node\Stmt\Class_ $class): string
{
$this->class = $this->parseIdentifier($class->name);
if (!$this->stubFileIncluded) {
$this->genClassStubFile($class, $this->file);
}
$this->classDef = new ClassDef();
$this->classDef->name = $this->class;
if ($class->extends) {

@ -9,9 +9,7 @@ extern php::Var argv;
extern void php_app_init();
extern void php_app_clean();
BEGIN_EXTERN_C()
extern zend_module_entry *get_module();
END_EXTERN_C()
extern zend_module_entry app_module_entry;
static void throw_exception(zend_object *ex) {
zend_bailout();
@ -21,10 +19,14 @@ int main(int cpp_argc, char **cpp_argv) {
php_embed_init(cpp_argc, cpp_argv);
zend_throw_exception_hook = throw_exception;
if (zend_register_module_ex(get_module(), MODULE_TEMPORARY) == NULL) {
if (zend_register_module_ex(&app_module_entry, MODULE_TEMPORARY) == NULL) {
zend_error(E_ERROR, "Failed to register module");
}
if (zend_startup_module_ex(&app_module_entry) == FAILURE) {
zend_error(E_ERROR, "Failed to startup module");
}
int rc = 0;
#if PPROF_ON
ProfilerStart("myapp.prof");

@ -104,7 +104,7 @@ foreach ($this->nativeConstants as $name => $constant):
<?php endforeach; ?>
}
static zend_module_entry app_module_entry = {
zend_module_entry app_module_entry = {
STANDARD_MODULE_HEADER,
"app",
ext_functions,

Loading…
Cancel
Save