refactor(compiler): 重构编译器代码结构并修复空指针检查问题

- 移除编译器主函数中过早的函数声明和全局变量声明生成逻辑
- 将扩展源文件生成逻辑移至翻译器类的编译流程中
- 修改genExtension方法返回生成的文件路径而不是接收文件参数
- 修复预处理器中对类标志的空指针检查,避免访问未设置的属性
- 更新并行编译时的进度显示颜色从蓝色调整为浅蓝色
- 重新组织源文件编译顺序,确保扩展模块文件正确加入编译列表
pull/1/head
韩天峰 5 months ago
parent 58b6d4fad4
commit 95e7b5f6e4
  1. 16
      bin/compiler.php
  2. 2
      src/Php/Preprocessor.php
  3. 29
      src/Php/Translator.php

@ -58,22 +58,6 @@ if (empty($sourceFiles)) {
$translator->stop("No valid source file found"); $translator->stop("No valid source file found");
} }
// 生成所有函数声明、全局变量声明
$translator->genFunctionDeclaration($translator->getIncludeDir() . '/php_func_decl.h');
$translator->genExternGlobalVars($translator->getIncludeDir() . '/php_global_var_decl.h');
// 生成所有全局变量源文件
$extensionSourceFile = $translator->getBuildDir() . '/extension.cc';
$translator->genExtension($extensionSourceFile);
$sourceFiles[] = $extensionSourceFile;
// 添加 main.cc 文件
if ($translator->getBuildMode() == 'bin') {
$sourceFiles[] = ROOT_PATH . '/src/cpp/main.cc';
$sourceFiles[] = ROOT_PATH . '/src/cpp/php_cli_process_title.c';
$sourceFiles[] = ROOT_PATH . '/src/cpp/ps_title.c';
}
// 编译所有 C++ 文件 // 编译所有 C++ 文件
$objectFiles = $translator->compile($sourceFiles); $objectFiles = $translator->compile($sourceFiles);

@ -572,7 +572,7 @@ class Preprocessor extends CompilerBase
$this->checkRequiredArgNum($name, $this->methodDef, $v); $this->checkRequiredArgNum($name, $this->methodDef, $v);
$this->classDef->addMethod($this->methodDef); $this->classDef->addMethod($this->methodDef);
} else { } else {
if (!($class->flags & Modifiers::ABSTRACT)) { if (isset($class->flags) and !($class->flags & Modifiers::ABSTRACT)) {
$this->fatalError($v, "Class {$this->class} cannot override non-abstract method {$v->name}"); $this->fatalError($v, "Class {$this->class} cannot override non-abstract method {$v->name}");
} }
if ($this->method === '__construct') { if ($this->method === '__construct') {

@ -241,7 +241,7 @@ class Translator extends Preprocessor
$this->writeFile($file, $code); $this->writeFile($file, $code);
} }
public function genExtension(string $file): void public function genExtension(): string
{ {
if ($this->buildMode == 'bin') { if ($this->buildMode == 'bin') {
if (!$this->hasFunction('main')) { if (!$this->hasFunction('main')) {
@ -249,12 +249,14 @@ class Translator extends Preprocessor
exit(1); exit(1);
} }
} }
$file = $this->getBuildDir() . '/extension-' . $this->getModuleName() . '.cc';
$this->localHeaders = $this->argInfoHeaderFiles; $this->localHeaders = $this->argInfoHeaderFiles;
$this->genClassCeList(); $this->genClassCeList();
$code = $this->render('extension.cc.php'); $code = $this->render('extension.cc.php');
$this->writeFile($file, $code); $this->writeFile($file, $code);
$this->formatCppCode($file); $this->formatCppCode($file);
$this->localHeaders = []; $this->localHeaders = [];
return $file;
} }
public function getModuleName(): string public function getModuleName(): string
@ -317,17 +319,18 @@ class Translator extends Preprocessor
$objectFiles = []; $objectFiles = [];
$job = $this->maxJob; $job = $this->maxJob;
// 如果只有一个文件或 job 为 1,则串行编译 // 生成所有函数声明、全局变量声明的头文件
if (count($sourceFiles) <= 1 || $job <= 1) { $this->genFunctionDeclaration($this->getIncludeDir() . '/php_func_decl.h');
foreach ($sourceFiles as $cppFile) { $this->genExternGlobalVars($this->getIncludeDir() . '/php_global_var_decl.h');
$objectFile = $this->getObjectFile($cppFile);
$this->compileFile($cppFile, $objectFile); // 生成扩展模块的源文件
if (!is_file($objectFile)) { $sourceFiles[] =$this->genExtension();
throw new \Exception('compile error');
} // embed 需要 main 函数,以及 cli 的内置函数定义
$objectFiles[] = $objectFile; if ($this->getBuildMode() == 'bin') {
} $sourceFiles[] = __DIR__ . '/../cpp/main.cc';
return $objectFiles; $sourceFiles[] = __DIR__ . '/../cpp/php_cli_process_title.c';
$sourceFiles[] = __DIR__ . '/../cpp/ps_title.c';
} }
// 并行编译 // 并行编译
@ -338,7 +341,7 @@ class Translator extends Preprocessor
$compiledCount = 0; $compiledCount = 0;
$failedFiles = []; $failedFiles = [];
$this->climate->blue("Starting parallel compilation with {$job} jobs for {$totalFiles} files"); $this->climate->lightBlue("Starting parallel compilation with {$job} jobs for {$totalFiles} files");
while ($compiledCount < $totalFiles) { while ($compiledCount < $totalFiles) {
// 启动新进程,直到达到最大并发数 // 启动新进程,直到达到最大并发数

Loading…
Cancel
Save