feat(compiler): 添加调试信息生成功能

- 在编译器选项配置中新增 debug-info 参数支持
- 在编译器基类中实现 genDebugInfo 方法用于生成调试信息
- 修改 parseAssignPropertyArrayDim 和 parseParentMethodCall 方法为受保护访问级别
- 在翻译器中集成 debug-info 命令行参数处理逻辑
- 更新属性赋值方式从 getPropertyIndirect 改为 attr 方法
- 在 C++ 主程序中添加异常处理时的未清理关闭标记
pull/1/head
韩天峰 7 months ago
parent a39a2e389a
commit fe34c72c87
  1. 22
      src/Php/CompilerBase.php
  2. 4
      src/Php/Translator.php
  3. 5
      src/config/compiler_options.php
  4. 1
      src/cpp/main.cc

@ -708,6 +708,7 @@ class CompilerBase extends \PhpAot\Core\Translator
}
$code .= "\n";
$this->indentLevel--;
$code .= $this->genDebugInfo();
$code .= $stmts;
$code .= "}\n";
@ -841,7 +842,7 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->afterStmtLines = [];
$result = '';
$this->writeLog('Line ' . $this->getLine($v) . ': ' . $class);
$lines[] = $this->genDebugInfo($v);
$lines[] = $this->getComment($v, $class);
switch ($class) {
case 'Stmt_Expression':
@ -3285,7 +3286,7 @@ class CompilerBase extends \PhpAot\Core\Translator
return self::PREFIX . $nativeFunc . '(' . $object . ', ' . $this->parseNativeCallArgs($args, $nativeFunc) . ')';
}
private function parseAssignPropertyArrayDim(Node $left, Node $right): string
protected function parseAssignPropertyArrayDim(Node $left, Node $right): string
{
$obj = $this->parseIdentifier($left->var->var);
$propName = $this->identifierToStr($left->var->name);
@ -3299,7 +3300,7 @@ class CompilerBase extends \PhpAot\Core\Translator
return $code . "{$obj}.updateArrayProperty({$propName}, {$dim}, {$value})";
}
private function parseParentMethodCall(Node\Expr\StaticCall $expr): string
protected function parseParentMethodCall(Node\Expr\StaticCall $expr): string
{
$method = $this->identifierToStr($expr->name);
if (empty($expr->args)) {
@ -3307,4 +3308,19 @@ class CompilerBase extends \PhpAot\Core\Translator
}
return 'this_.callParentMethod(' . $method . ', {' . $this->parseCallArgs($expr->args) . '})';
}
protected function genDebugInfo(?NodeAbstract $stmt = null): string
{
$code = '';
if ($this->debugInfo) {
if ($stmt) {
$code .= 'php::debug_info.php_line = ' . $stmt->getLine() . ';' . PHP_EOL;
$code .= 'php::debug_info.cpp_line = __LINE__;' . PHP_EOL;
} else {
$code .= 'php::debug_info.enable = true;' . PHP_EOL;
$code .= 'php::debug_info.php_file = "' . $this->escapeString($this->file) . '";' . PHP_EOL;
}
}
return $code;
}
}

@ -20,6 +20,7 @@ use PhpAot\Php\Exception\Redo;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\NodeAbstract;
use PhpParser\NodeTraverser;
use Symfony\Component\Yaml\Yaml;
@ -50,6 +51,7 @@ class Translator extends Preprocessor
$this->optimizeLevel = $this->climate->arguments->get('optimize');
$this->buildMode = $this->climate->arguments->get('mode');
$this->debugLine = intval($this->climate->arguments->get('debug-line'));
$this->debugInfo = $this->climate->arguments->defined('debug-info');
$this->noLiteralStrings = $this->climate->arguments->get('noLiteralStrings');
$this->enableProfiler = $this->climate->arguments->defined('profile');
$this->internalFunctions = array_flip(get_defined_functions()['internal']);
@ -751,7 +753,7 @@ class Translator extends Preprocessor
foreach ($classDef->properties as $property) {
if ($property->type === self::TYPE_ARRAY and $property->default and $property->default !== self::TYPE_ARRAY . '{}') {
$propOffset = self::PREFIX . $this->getPropertyOffset($property->name, $classDef->name, $classDef->namespace);
$cppCode .= $this->getIndent() . 'this_.getPropertyIndirect(' . $propOffset . ') = ' . $property->default . ';' . PHP_EOL;
$cppCode .= $this->getIndent() . 'this_.attr(' . $propOffset . ') = ' . $property->default . ';' . PHP_EOL;
}
}

@ -51,4 +51,9 @@ return [
'required' => false,
'defaultValue' => 0,
],
'debug-info' => [
'longPrefix' => 'debug-info',
'description' => 'Enable debug info',
'required' => false,
],
];

@ -45,6 +45,7 @@ int main(int cpp_argc, char **cpp_argv) {
php::eval("main();");
} catch (zend_object *e) {
rc = EG(exit_status);
CG(unclean_shutdown) = 1;
zend_exception_error(e, E_ERROR);
}
#if PPROF_ON

Loading…
Cancel
Save