From b4b735644d88341b2e1a260cd1fcb4ca5b70a6a1 Mon Sep 17 00:00:00 2001 From: rango Date: Thu, 30 Apr 2026 17:36:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0PHP=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E5=9F=BA=E7=A1=80=E7=B1=BB=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现了PHP到C++的代码转换核心功能 - 添加了AST节点解析和类型系统映射 - 实现了变量、函数、类的声明和调用处理 - 集成了PHPParser进行语法树解析 - 添加了跨平台编译支持(Windows、Linux、macOS) - 实现了字面量字符串管理和内存优化 - 添加了全局变量和超全局变量处理 - 实现了魔术方法检测和闭包生成器 - 集成了CLImate库提供命令行界面支持 - 添加了错误处理和调试信息生成功能 - 实现了代码格式化和文件保存功能 --- examples/win32-hello/README.md | 27 +++++++++++++ src/Php/CompilerBase.php | 69 ++++++++++++++-------------------- src/Php/Translator.php | 2 +- 3 files changed, 57 insertions(+), 41 deletions(-) diff --git a/examples/win32-hello/README.md b/examples/win32-hello/README.md index 5aa6978f..e6ddaa8b 100644 --- a/examples/win32-hello/README.md +++ b/examples/win32-hello/README.md @@ -88,6 +88,31 @@ php ../../cli.php build --mode=bin --sanitize=address,undefined asan-test.php --- +### 5. debug-mode-test.php - 调试模式测试程序 + +**特点:** +- ✅ 演示 `--debug-info` 参数的使用 +- ✅ 禁用优化,便于调试 +- ✅ 生成完整的调试信息 +- ✅ 支持 GDB、LLDB、Visual Studio 调试 + +**编译命令:** +```bash +# 启用调试模式(自动禁用优化 + 生成调试信息) +php ../../cli.php build --mode=bin --debug-info --no-console debug-mode-test.php + +# 结合 AddressSanitizer +php ../../cli.php build --mode=bin --debug-info --sanitize=address --no-console debug-mode-test.php +``` + +**使用方法:** +1. 编译带调试信息的版本 +2. 使用调试器加载程序 +3. 设置断点、查看变量、单步执行 +4. 查看 `debug-mode-test.log` 了解运行流程 + +--- + ## 🔧 核心功能 ### C++ 函数导出 @@ -288,6 +313,7 @@ win32-hello/ ├── hello-console.php # 控制台版本示例 ├── hello-gui.php # GUI 版本示例 ├── debug-test.php # 调试测试程序(带日志) +├── debug-mode-test.php # 调试模式测试程序(--debug-info) ├── asan-test.php # AddressSanitizer 测试程序 ├── main.php # 另一个示例文件 ├── window.php # 窗口创建示例 @@ -298,6 +324,7 @@ win32-hello/ ├── CHINESE_SUPPORT_GUIDE.md # 中文支持指南 ├── CPP_FUNCTION_EXPORT_GUIDE.md # C++ 函数导出指南 ├── DEBUG_GUIDE.md # 调试指南(重要!) +├── DEBUG_MODE_GUIDE.md # 调试模式使用指南 ├── SANITIZER_GUIDE.md # AddressSanitizer 使用指南 └── README.md # 本文件 ``` diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 01b48f72..6a692090 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -2364,23 +2364,6 @@ class CompilerBase extends \PhpAot\Core\Translator } } } - - // 添加 CRT 和系统库(解决 LNK2019 错误) - $list[] = 'msvcrt.lib'; // C 运行时库(动态) - $list[] = 'oldnames.lib'; // 旧名称兼容库 - $list[] = 'kernel32.lib'; // Windows 核心 API - $list[] = 'user32.lib'; // 用户界面 - $list[] = 'gdi32.lib'; // GDI - $list[] = 'winspool.lib'; // 打印池 - $list[] = 'comdlg32.lib'; // 通用对话框 - $list[] = 'advapi32.lib'; // 高级 API - $list[] = 'shell32.lib'; // Shell API - $list[] = 'ole32.lib'; // OLE - $list[] = 'oleaut32.lib'; // OLE Automation - $list[] = 'uuid.lib'; // UUID - $list[] = 'odbc32.lib'; // ODBC - $list[] = 'odbccp32.lib'; // ODBC CP - $list[] = 'ws2_32.lib'; // Winsock } $out = ''; @@ -2431,23 +2414,26 @@ class CompilerBase extends \PhpAot\Core\Translator } } - // 优化级别 - switch ($this->optimizeLevel) { - case 0: - $cmd .= ' /Od'; // 禁用优化 - break; - case 1: - case 2: - $cmd .= ' /O2'; // 最大速度优化 - break; - case 3: - $cmd .= ' /Ox'; // 完全优化 - break; - } - - // 调试信息 + // 调试模式:当启用 --debug-info 时,自动禁用优化并添加调试信息 if ($this->debugInfo) { - $cmd .= ' /Zi'; // 生成完整调试信息 + $cmd .= ' /Od'; // 禁用优化(类似 gcc -O0) + $cmd .= ' /Zi'; // 生成完整调试信息(类似 gcc -g) + $cmd .= ' /DEBUG'; // 链接时生成 PDB 文件 + $this->climate->info('Debug mode enabled: optimizations disabled, debug info generated'); + } else { + // 非调试模式:使用用户指定的优化级别 + switch ($this->optimizeLevel) { + case 0: + $cmd .= ' /Od'; // 禁用优化 + break; + case 1: + case 2: + $cmd .= ' /O2'; // 最大速度优化 + break; + case 3: + $cmd .= ' /Ox'; // 完全优化 + break; + } } // 警告级别 @@ -2496,12 +2482,9 @@ class CompilerBase extends \PhpAot\Core\Translator // 所有输出需要通过 GUI 元素(如消息框)显示 } - // 解决 CRT 库冲突问题(LNK4098 警告和 Debug Assertion Failed) // 排除静态 CRT 库,只使用动态 CRT(/MD) $cmd .= ' /NODEFAULTLIB:LIBCMT'; - $cmd .= ' /NODEFAULTLIB:LIBCMTD'; - $cmd .= ' /NODEFAULTLIB:MSVCRTD'; - + $cmd .= ' ' . $this->parseWindowsLibs(); if ($this->ldflags) { $cmd .= ' ' . $this->ldflags; @@ -2520,11 +2503,17 @@ class CompilerBase extends \PhpAot\Core\Translator protected function addUnixCompilationOption(string &$cmd, bool $link): void { $cmd .= ' ' . $this->parseIncludes(); - $cmd .= ' -O' . $this->optimizeLevel; - + + // 调试模式:当启用 --debug-info 时,自动禁用优化并添加调试信息 if ($this->debugInfo) { - $cmd .= ' -g'; + $cmd .= ' -O0'; // 禁用优化(类似 MSVC /Od) + $cmd .= ' -g'; // 生成调试信息 + $this->climate->info('Debug mode enabled: optimizations disabled (-O0), debug info generated (-g)'); + } else { + // 非调试模式:使用用户指定的优化级别 + $cmd .= ' -O' . $this->optimizeLevel; } + $cmd .= ' -Wall'; // Sanitizer 支持(GCC/Clang 使用 -fsanitize) diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 7d76002f..e918d66e 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -97,7 +97,7 @@ class Translator extends Preprocessor $climate->bold('OPTIONS:'); $climate->tab()->out('-O Optimization level (0-3, default: 0)'); $climate->tab()->out('-p, --profile Enable performance profiling'); - $climate->tab()->out('-d, --debug-info Enable debug info'); + $climate->tab()->out('-d, --debug-info Enable debug info (auto-disable optimizations, add -g/-Zi)'); $climate->tab()->out('-o, --output Output binary name (default: input basename)'); $climate->tab()->out('-v, --version Show version'); $climate->tab()->out('-h, --help Show this help message');