Compare commits
8 Commits
master
...
speed_buil
| Author | SHA1 | Date |
|---|---|---|
|
|
eb523afbf0 | 3 weeks ago |
|
|
e0581f8ccc | 3 weeks ago |
|
|
fbaac125ae | 3 weeks ago |
|
|
e79ec2cf73 | 3 weeks ago |
|
|
7fa91622e2 | 3 weeks ago |
|
|
473bd284ab | 3 weeks ago |
|
|
ac7813d7b2 | 3 weeks ago |
|
|
449afc1516 | 3 weeks ago |
886 changed files with 11828 additions and 58479 deletions
@ -0,0 +1,179 @@ |
|||||||
|
{ |
||||||
|
"language": "zh", |
||||||
|
"sections": [ |
||||||
|
{ |
||||||
|
"name": "项目概览", |
||||||
|
"topics": [ |
||||||
|
{ |
||||||
|
"title": "项目简介", |
||||||
|
"level": 1, |
||||||
|
"slug": "overview", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "编译流水线", |
||||||
|
"level": 1, |
||||||
|
"slug": "pipeline", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "编译模式", |
||||||
|
"level": 1, |
||||||
|
"slug": "compilation-modes", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "快速开始", |
||||||
|
"level": 1, |
||||||
|
"slug": "quickstart", |
||||||
|
"group": null |
||||||
|
} |
||||||
|
], |
||||||
|
"groups": [] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"name": "前端:解析与符号", |
||||||
|
"topics": [ |
||||||
|
{ |
||||||
|
"title": "解析器与 AST", |
||||||
|
"level": 1, |
||||||
|
"slug": "parser", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "预处理与符号表", |
||||||
|
"level": 1, |
||||||
|
"slug": "preprocessor", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "AST 转换(Lowering)", |
||||||
|
"level": 1, |
||||||
|
"slug": "transform", |
||||||
|
"group": null |
||||||
|
} |
||||||
|
], |
||||||
|
"groups": [] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"name": "类型系统", |
||||||
|
"topics": [ |
||||||
|
{ |
||||||
|
"title": "原生类型映射", |
||||||
|
"level": 1, |
||||||
|
"slug": "native-types", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "类型检查与兼容", |
||||||
|
"level": 1, |
||||||
|
"slug": "type-check", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "高精度数值类型", |
||||||
|
"level": 1, |
||||||
|
"slug": "high-precision-types", |
||||||
|
"group": null |
||||||
|
} |
||||||
|
], |
||||||
|
"groups": [] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"name": "代码生成", |
||||||
|
"topics": [ |
||||||
|
{ |
||||||
|
"title": "生成器总览", |
||||||
|
"level": 1, |
||||||
|
"slug": "generator", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "函数与类生成", |
||||||
|
"level": 1, |
||||||
|
"slug": "gen-functions-classes", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "扩展模块生成", |
||||||
|
"level": 1, |
||||||
|
"slug": "gen-extension", |
||||||
|
"group": null |
||||||
|
} |
||||||
|
], |
||||||
|
"groups": [] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"name": "后端与构建", |
||||||
|
"topics": [ |
||||||
|
{ |
||||||
|
"title": "编译器后端", |
||||||
|
"level": 1, |
||||||
|
"slug": "backend", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "构建流水线", |
||||||
|
"level": 1, |
||||||
|
"slug": "build-pipeline", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "增量编译缓存", |
||||||
|
"level": 1, |
||||||
|
"slug": "incremental-cache", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "跨平台支持", |
||||||
|
"level": 1, |
||||||
|
"slug": "platform", |
||||||
|
"group": null |
||||||
|
} |
||||||
|
], |
||||||
|
"groups": [] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"name": "优化与性能", |
||||||
|
"topics": [ |
||||||
|
{ |
||||||
|
"title": "优化器", |
||||||
|
"level": 1, |
||||||
|
"slug": "optimizer", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "编译期属性", |
||||||
|
"level": 1, |
||||||
|
"slug": "compile-time-attributes", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "构建速度与性能", |
||||||
|
"level": 1, |
||||||
|
"slug": "build-speed", |
||||||
|
"group": null |
||||||
|
} |
||||||
|
], |
||||||
|
"groups": [] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"name": "运行时与边界", |
||||||
|
"topics": [ |
||||||
|
{ |
||||||
|
"title": "PHPX 运行时集成", |
||||||
|
"level": 1, |
||||||
|
"slug": "runtime", |
||||||
|
"group": null |
||||||
|
}, |
||||||
|
{ |
||||||
|
"title": "不支持的 PHP 特性", |
||||||
|
"level": 1, |
||||||
|
"slug": "incompatible-features", |
||||||
|
"group": null |
||||||
|
} |
||||||
|
], |
||||||
|
"groups": [] |
||||||
|
} |
||||||
|
] |
||||||
|
} |
||||||
@ -0,0 +1,43 @@ |
|||||||
|
<section> |
||||||
|
项目概览 |
||||||
|
<topic level="1" slug="overview">项目简介</topic> |
||||||
|
<topic level="1" slug="pipeline">编译流水线</topic> |
||||||
|
<topic level="1" slug="compilation-modes">编译模式</topic> |
||||||
|
<topic level="1" slug="quickstart">快速开始</topic> |
||||||
|
</section> |
||||||
|
<section> |
||||||
|
前端:解析与符号 |
||||||
|
<topic level="1" slug="parser">解析器与 AST</topic> |
||||||
|
<topic level="1" slug="preprocessor">预处理与符号表</topic> |
||||||
|
<topic level="1" slug="transform">AST 转换(Lowering)</topic> |
||||||
|
</section> |
||||||
|
<section> |
||||||
|
类型系统 |
||||||
|
<topic level="1" slug="native-types">原生类型映射</topic> |
||||||
|
<topic level="1" slug="type-check">类型检查与兼容</topic> |
||||||
|
<topic level="1" slug="high-precision-types">高精度数值类型</topic> |
||||||
|
</section> |
||||||
|
<section> |
||||||
|
代码生成 |
||||||
|
<topic level="1" slug="generator">生成器总览</topic> |
||||||
|
<topic level="1" slug="gen-functions-classes">函数与类生成</topic> |
||||||
|
<topic level="1" slug="gen-extension">扩展模块生成</topic> |
||||||
|
</section> |
||||||
|
<section> |
||||||
|
后端与构建 |
||||||
|
<topic level="1" slug="backend">编译器后端</topic> |
||||||
|
<topic level="1" slug="build-pipeline">构建流水线</topic> |
||||||
|
<topic level="1" slug="incremental-cache">增量编译缓存</topic> |
||||||
|
<topic level="1" slug="platform">跨平台支持</topic> |
||||||
|
</section> |
||||||
|
<section> |
||||||
|
优化与性能 |
||||||
|
<topic level="1" slug="optimizer">优化器</topic> |
||||||
|
<topic level="1" slug="compile-time-attributes">编译期属性</topic> |
||||||
|
<topic level="1" slug="build-speed">构建速度与性能</topic> |
||||||
|
</section> |
||||||
|
<section> |
||||||
|
运行时与边界 |
||||||
|
<topic level="1" slug="runtime">PHPX 运行时集成</topic> |
||||||
|
<topic level="1" slug="incompatible-features">不支持的 PHP 特性</topic> |
||||||
|
</section> |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<title>Project Wiki</title><style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg)} |
||||||
|
#app{display:flex;height:100vh} |
||||||
|
#side{width:300px;flex:0 0 300px;background:var(--card);border-right:1px solid var(--border);overflow:auto;padding:16px} |
||||||
|
#side h2{font-size:15px;color:#fff;margin:18px 0 8px;padding-bottom:6px;border-bottom:1px solid var(--border)} |
||||||
|
#side a{display:block;padding:5px 8px;border-radius:6px;color:var(--fg);text-decoration:none;font-size:13px} |
||||||
|
#side a:hover{background:#222633} |
||||||
|
#side a.lvl2{margin-left:12px;font-size:12px;color:var(--muted)} |
||||||
|
#main{flex:1;background:var(--bg)} |
||||||
|
#main iframe{width:100%;height:100%;border:none} |
||||||
|
.meta{color:var(--muted);font-size:11px;margin-top:20px;border-top:1px solid var(--border);padding-top:10px} |
||||||
|
</style></head> |
||||||
|
<body><div id="app"> |
||||||
|
<nav id="side"><h2>项目概览</h2><a class="lvl1" href="pages/overview.html" target="view">项目简介</a><a class="lvl1" href="pages/pipeline.html" target="view">编译流水线</a><a class="lvl1" href="pages/compilation-modes.html" target="view">编译模式</a><a class="lvl1" href="pages/quickstart.html" target="view">快速开始</a><h2>前端:解析与符号</h2><a class="lvl1" href="pages/parser.html" target="view">解析器与 AST</a><a class="lvl1" href="pages/preprocessor.html" target="view">预处理与符号表</a><a class="lvl1" href="pages/transform.html" target="view">AST 转换(Lowering)</a><h2>类型系统</h2><a class="lvl1" href="pages/native-types.html" target="view">原生类型映射</a><a class="lvl1" href="pages/type-check.html" target="view">类型检查与兼容</a><a class="lvl1" href="pages/high-precision-types.html" target="view">高精度数值类型</a><h2>代码生成</h2><a class="lvl1" href="pages/generator.html" target="view">生成器总览</a><a class="lvl1" href="pages/gen-functions-classes.html" target="view">函数与类生成</a><a class="lvl1" href="pages/gen-extension.html" target="view">扩展模块生成</a><h2>后端与构建</h2><a class="lvl1" href="pages/backend.html" target="view">编译器后端</a><a class="lvl1" href="pages/build-pipeline.html" target="view">构建流水线</a><a class="lvl1" href="pages/incremental-cache.html" target="view">增量编译缓存</a><a class="lvl1" href="pages/platform.html" target="view">跨平台支持</a><h2>优化与性能</h2><a class="lvl1" href="pages/optimizer.html" target="view">优化器</a><a class="lvl1" href="pages/compile-time-attributes.html" target="view">编译期属性</a><a class="lvl1" href="pages/build-speed.html" target="view">构建速度与性能</a><h2>运行时与边界</h2><a class="lvl1" href="pages/runtime.html" target="view">PHPX 运行时集成</a><a class="lvl1" href="pages/incompatible-features.html" target="view">不支持的 PHP 特性</a><div class="meta">rendered by code-wiki · static HTML</div></nav> |
||||||
|
<main id="main"><iframe name="view" src="pages/overview.html"></iframe></main> |
||||||
|
</div></body></html> |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>Project Wiki</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>Project Wiki</h1> |
||||||
|
<p>This wiki was generated by <strong>code-wiki</strong> from the repository at <code>D:\git\php\aot-compiler</code>.</p> |
||||||
|
<p>Select a topic from the left navigation to read its documentation.</p></div></body></html> |
||||||
@ -0,0 +1,39 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>编译器后端</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>编译器后端</h1> |
||||||
|
<p>TypePHP 不直接调 <code>g++</code>/<code>cl</code>,而是抽象出 <strong>平台(Platform)+ 编译器后端(Backend)</strong> 两层,由工厂在启动时自动探测。这样同一份生成代码能在 GCC / Clang / MSVC 下编译。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L164-L190">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Backend">src/Backend/</a></p> |
||||||
|
<h2>抽象结构</h2> |
||||||
|
<table><thead><tr><th>类</th><th>角色</th></tr></thead><tbody><tr><td><code>PlatformFactory</code></td><td>按 OS 创建 <code>Windows</code> / <code>Linux</code> / <code>macOS</code> 平台对象</td></tr><tr><td><code>CompilerFactory</code></td><td><code>detectCompilerName()</code> / <code>createByName()</code> / <code>autoDetect()</code> 选择后端</td></tr><tr><td><code>CompilerBackend</code>(接口)</td><td>定义编译/链接命令抽象</td></tr><tr><td><code>GccLikeBackend</code></td><td>GCC / Clang 共用逻辑</td></tr><tr><td><code>Gcc</code> / <code>Clang</code></td><td>具体后端</td></tr><tr><td><code>Msvc</code></td><td>MSVC(Windows)后端</td></tr></tbody></table> |
||||||
|
<p><code>detectPlatform()</code> 先建平台,再探测编译器名,最后 <code>CompilerFactory::createByName()</code> 生成后端;若失败则告警并回退到旧逻辑(<code>initializeNewArchitecture()</code>)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L164-L190">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L538-L560">src/Translator.php</a></p> |
||||||
|
<h2>命令生成</h2> |
||||||
|
<p>后端只负责「给定源/目标/选项 → 返回命令字符串」。真实的进程执行在 <a href="pages/build-pipeline.html">构建流水线</a> 的 <code>NativeBuilder</code> 中。例如:</p> |
||||||
|
<ul><li><code>compileCommand()</code> / <code>linkCommand()</code> 产出可复现的命令行;</li><li>命令行本身被写进<strong>缓存键</strong>,确保编译参数变化能触发重编译。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1408-L1417">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1644-L1647">src/Translator.php</a></p> |
||||||
|
<h2>预编译头</h2> |
||||||
|
<p>后端是否支持 PCH 由 <code>supportsPrecompiledHeaders()</code> 决定;支持的会在构建期预编译 <code>phpx.h</code> 等全局头,显著加速后续编译单元。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1449-L1487">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Build\PrecompiledHeaderManager.php">src/Build/PrecompiledHeaderManager.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>命令如何被执行与排队 → <a href="pages/build-pipeline.html">构建流水线</a></li><li>缓存如何依赖编译命令 → <a href="pages/incremental-cache.html">增量编译缓存</a></li><li>平台差异(Windows SDK / PHP lib) → <a href="pages/platform.html">跨平台支持</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 编译器后端 |
||||||
|
|
||||||
|
TypePHP 不直接调 `g++`/`cl`,而是抽象出 **平台(Platform)+ 编译器后端(Backend)** 两层,由工厂在启动时自动探测。这样同一份生成代码能在 GCC / Clang / MSVC 下编译。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L164-L190) · [src/Backend/](src/Backend/) |
||||||
|
|
||||||
|
## 抽象结构 |
||||||
|
|
||||||
|
| 类 | 角色 | |
||||||
|
|---|---| |
||||||
|
| `PlatformFactory` | 按 OS 创建 `Windows` / `Linux` / `macOS` 平台对象 | |
||||||
|
| `CompilerFactory` | `detectCompilerName()` / `createByName()` / `autoDetect()` 选择后端 | |
||||||
|
| `CompilerBackend`(接口) | 定义编译/链接命令抽象 | |
||||||
|
| `GccLikeBackend` | GCC / Clang 共用逻辑 | |
||||||
|
| `Gcc` / `Clang` | 具体后端 | |
||||||
|
| `Msvc` | MSVC(Windows)后端 | |
||||||
|
|
||||||
|
`detectPlatform()` 先建平台,再探测编译器名,最后 `CompilerFactory::createByName()` 生成后端;若失败则告警并回退到旧逻辑(`initializeNewArchitecture()`)。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L164-L190) · [src/Translator.php](src/Translator.php#L538-L560) |
||||||
|
|
||||||
|
## 命令生成 |
||||||
|
|
||||||
|
后端只负责「给定源/目标/选项 → 返回命令字符串」。真实的进程执行在 [构建流水线](build-pipeline) 的 `NativeBuilder` 中。例如: |
||||||
|
|
||||||
|
- `compileCommand()` / `linkCommand()` 产出可复现的命令行; |
||||||
|
- 命令行本身被写进**缓存键**,确保编译参数变化能触发重编译。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1408-L1417) · [src/Translator.php](src/Translator.php#L1644-L1647) |
||||||
|
|
||||||
|
## 预编译头 |
||||||
|
|
||||||
|
后端是否支持 PCH 由 `supportsPrecompiledHeaders()` 决定;支持的会在构建期预编译 `phpx.h` 等全局头,显著加速后续编译单元。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1449-L1487) · [src/Build/PrecompiledHeaderManager.php](src/Build/PrecompiledHeaderManager.php) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 命令如何被执行与排队 → [构建流水线](build-pipeline) |
||||||
|
- 缓存如何依赖编译命令 → [增量编译缓存](incremental-cache) |
||||||
|
- 平台差异(Windows SDK / PHP lib) → [跨平台支持](platform) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>构建流水线</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>构建流水线</h1> |
||||||
|
<p><code>Translator::compile()</code> 把生成的 C++ 与 phpx 运行时源文件编译为对象文件,<code>build()</code> 再链接成最终产物。两者都委托给 <code>src/Build/NativeBuilder</code>,它负责真正派发编译/链接进程。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1419-L1447">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1649-L1680">src/Translator.php</a></p> |
||||||
|
<h2>compile() 步骤</h2> |
||||||
|
<pre class="mermaid">flowchart TD |
||||||
|
A["compile(sourceFiles)"] --> B["追加 phpx misc 源\nfiber_generator / helper / main / cli_title"] |
||||||
|
B --> C["preparePhpXPrecompiledHeader()"] |
||||||
|
C --> D["compileResourceFile() (Windows)"] |
||||||
|
D --> E{"支持 pcntl 且 -j>1?"} |
||||||
|
E -- "否" --> F["compileSourceFile() 串行"] |
||||||
|
E -- "是" --> G["compileWithPcntl() 并行"]</pre> |
||||||
|
<p>phpx misc 源(<code>typephp_fiber_generator.cc</code>、<code>typephp_helper.cc</code>、<code>typephp_main.cc</code>、<code>php_cli_process_title.c</code>、<code>ps_title.c</code>)按模式追加:embed 加 <code>typephp_main.cc</code>,bin 加 cli 标题源。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1423-L1434">src/Translator.php</a></p> |
||||||
|
<h2>并行编译</h2> |
||||||
|
<p><code>compileWithPcntl()</code> 在 Unix/Linux/macOS 用 <code>pcntl_fork</code> 多进程编译,进度条实时刷新;无 <code>pcntl</code> 扩展时自动降级为串行。每个 <code>compileFile()</code> 调用前都会先查 <a href="pages/incremental-cache.html">增量缓存</a>,命中则跳过。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1584-L1637">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1349-L1392">src/Translator.php</a></p> |
||||||
|
<h2>NativeBuilder 与命令选项</h2> |
||||||
|
<table><thead><tr><th>组件</th><th>职责</th></tr></thead><tbody><tr><td><code>NativeBuilder</code></td><td>真正执行 <code>compile</code> / <code>link</code>,返回 <code>command</code>/<code>status</code>/<code>output</code></td></tr><tr><td><code>NativeCommandOptionsTrait</code></td><td>汇总编译/链接选项</td></tr><tr><td><code>CompileOptions</code> / <code>LinkOptions</code> / <code>CommandOptions</code></td><td>选项数据对象</td></tr><tr><td><code>FileScanner</code></td><td>识别原生源文件(C/C++/汇编/ObjC)</td></tr><tr><td><code>SourceCompileQueue</code> / <code>SourcePipelineTrait</code></td><td>编译队列与流水线调度</td></tr><tr><td><code>ResourceCompilationTrait</code></td><td>Windows <code>.rc</code> → <code>.res</code></td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Build\NativeBuilder.php">src/Build/NativeBuilder.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Build">src/Build/</a></p> |
||||||
|
<h2>build() 链接</h2> |
||||||
|
<p><code>build()</code> 调 <code>NativeBuilder::link()</code>,Windows 模式下把 <code>.res</code> 资源文件并入目标;链接失败或产物未生成则 <code>error()</code> 退出。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1649-L1680">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1653-L1659">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>后端如何产出命令 → <a href="pages/backend.html">编译器后端</a></li><li>缓存如何避免重复编译 → <a href="pages/incremental-cache.html">增量编译缓存</a></li><li>平台相关的链接差异 → <a href="pages/platform.html">跨平台支持</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,56 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 构建流水线 |
||||||
|
|
||||||
|
`Translator::compile()` 把生成的 C++ 与 phpx 运行时源文件编译为对象文件,`build()` 再链接成最终产物。两者都委托给 `src/Build/NativeBuilder`,它负责真正派发编译/链接进程。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1419-L1447) · [src/Translator.php](src/Translator.php#L1649-L1680) |
||||||
|
|
||||||
|
## compile() 步骤 |
||||||
|
|
||||||
|
```mermaid |
||||||
|
flowchart TD |
||||||
|
A["compile(sourceFiles)"] --> B["追加 phpx misc 源\nfiber_generator / helper / main / cli_title"] |
||||||
|
B --> C["preparePhpXPrecompiledHeader()"] |
||||||
|
C --> D["compileResourceFile() (Windows)"] |
||||||
|
D --> E{"支持 pcntl 且 -j>1?"} |
||||||
|
E -- "否" --> F["compileSourceFile() 串行"] |
||||||
|
E -- "是" --> G["compileWithPcntl() 并行"] |
||||||
|
``` |
||||||
|
|
||||||
|
phpx misc 源(`typephp_fiber_generator.cc`、`typephp_helper.cc`、`typephp_main.cc`、`php_cli_process_title.c`、`ps_title.c`)按模式追加:embed 加 `typephp_main.cc`,bin 加 cli 标题源。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1423-L1434) |
||||||
|
|
||||||
|
## 并行编译 |
||||||
|
|
||||||
|
`compileWithPcntl()` 在 Unix/Linux/macOS 用 `pcntl_fork` 多进程编译,进度条实时刷新;无 `pcntl` 扩展时自动降级为串行。每个 `compileFile()` 调用前都会先查 [增量缓存](incremental-cache),命中则跳过。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1584-L1637) · [src/Translator.php](src/Translator.php#L1349-L1392) |
||||||
|
|
||||||
|
## NativeBuilder 与命令选项 |
||||||
|
|
||||||
|
| 组件 | 职责 | |
||||||
|
|---|---| |
||||||
|
| `NativeBuilder` | 真正执行 `compile` / `link`,返回 `command`/`status`/`output` | |
||||||
|
| `NativeCommandOptionsTrait` | 汇总编译/链接选项 | |
||||||
|
| `CompileOptions` / `LinkOptions` / `CommandOptions` | 选项数据对象 | |
||||||
|
| `FileScanner` | 识别原生源文件(C/C++/汇编/ObjC) | |
||||||
|
| `SourceCompileQueue` / `SourcePipelineTrait` | 编译队列与流水线调度 | |
||||||
|
| `ResourceCompilationTrait` | Windows `.rc` → `.res` | |
||||||
|
|
||||||
|
Sources: [src/Build/NativeBuilder.php](src/Build/NativeBuilder.php) · [src/Build/](src/Build/) |
||||||
|
|
||||||
|
## build() 链接 |
||||||
|
|
||||||
|
`build()` 调 `NativeBuilder::link()`,Windows 模式下把 `.res` 资源文件并入目标;链接失败或产物未生成则 `error()` 退出。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1649-L1680) · [src/Translator.php](src/Translator.php#L1653-L1659) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 后端如何产出命令 → [编译器后端](backend) |
||||||
|
- 缓存如何避免重复编译 → [增量编译缓存](incremental-cache) |
||||||
|
- 平台相关的链接差异 → [跨平台支持](platform) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>构建速度与性能</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>构建速度与性能</h1> |
||||||
|
<p>TypePHP 的「编译速度」包含两个维度:<strong>编译期(PHP→C++→二进制)自身耗时</strong> 与 <strong>生成产物的运行性能</strong>。本章聚焦前者——哪些环节最慢,以及已落地的优化。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\AOT_BUILD_SPEED_RESEARCH.md">docs/AOT_BUILD_SPEED_RESEARCH.md</a> · <a href="file:///D:\git\php\aot-compiler\docs\aot-optimization-priority.md">docs/aot-optimization-priority.md</a></p> |
||||||
|
<h2>时间都花在哪</h2> |
||||||
|
<p>编译期主要成本:</p> |
||||||
|
<ol><li><strong>PHP 解析</strong>:每个文件被多次解析(预处理一次、转换一次)——这是主要冗余;</li><li><strong>C++ 翻译</strong>:<code>doConvert()</code> 遍历 AST 并发射 C++;</li><li><strong>C++ 编译</strong>:大量 <code>.cc</code> 单元编译,受 PCH 与并行度影响;</li><li><strong>链接</strong>:最终链接往往是大项目瓶颈之一。</li></ol> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\AOT_BUILD_SPEED_RESEARCH.md">docs/AOT_BUILD_SPEED_RESEARCH.md</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2455-L2459">src/Translator.php</a></p> |
||||||
|
<h2>已落地的优化</h2> |
||||||
|
<table><thead><tr><th>优化</th><th>说明</th><th>位置</th></tr></thead><tbody><tr><td><strong>S1 合并解析</strong></td><td>同一文件 AST 在进程内缓存复用(<code>parseCachedAst</code>),消除重复解析</td><td><code>src/Translator.php</code> <code>parseCachedAst()</code></td></tr><tr><td><strong>S2 增量对象缓存</strong></td><td>生成单元按「编译命令 + PHP ABI + 生成头内容指纹」缓存 <code>.o</code>,改动一处不必全量重编</td><td><a href="pages/incremental-cache.html">增量编译缓存</a></td></tr><tr><td><strong>PCH 预编译头</strong></td><td>预编译 <code>phpx.h</code> 等全局头,加速每个编译单元</td><td><code>src/Build/PrecompiledHeaderManager.php</code></td></tr><tr><td><strong>并行编译</strong></td><td><code>-j</code> 用 <code>pcntl</code> 多进程编译</td><td><code>src/Translator.php</code> <code>compileWithPcntl()</code></td></tr><tr><td><strong>misc 缓存</strong></td><td>phpx 运行时源按 ABI 指纹缓存</td><td><code>hasMiscObjectFileCache()</code></td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1116-L1160">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1449-L1487">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1584-L1637">src/Translator.php</a></p> |
||||||
|
<h2>调试与剖析</h2> |
||||||
|
<ul><li><code>--dry</code>:只生成 C++,跳过编译/链接,用于检查翻译与定位;</li><li><code>--profile</code>:启用 gperftools 剖析(仅 Linux),并在 <code>./tpc app.prof</code> 模式调 <code>pprof --web</code>;</li><li><code>--no-progress</code> / <code>-j</code>:控制进度展示与并行度。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\compiler.php#L48-L73">src/compiler.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L30-L36">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L251-L255">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>缓存机制细节 → <a href="pages/incremental-cache.html">增量编译缓存</a></li><li>优化器如何提升产物性能 → <a href="pages/optimizer.html">优化器</a></li><li>优先级规划 → <code>docs/aot-optimization-priority.md</code></li></ul></div></body></html> |
||||||
@ -0,0 +1,46 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 构建速度与性能 |
||||||
|
|
||||||
|
TypePHP 的「编译速度」包含两个维度:**编译期(PHP→C++→二进制)自身耗时** 与 **生成产物的运行性能**。本章聚焦前者——哪些环节最慢,以及已落地的优化。 |
||||||
|
|
||||||
|
Sources: [docs/AOT_BUILD_SPEED_RESEARCH.md](docs/AOT_BUILD_SPEED_RESEARCH.md) · [docs/aot-optimization-priority.md](docs/aot-optimization-priority.md) |
||||||
|
|
||||||
|
## 时间都花在哪 |
||||||
|
|
||||||
|
编译期主要成本: |
||||||
|
|
||||||
|
1. **PHP 解析**:每个文件被多次解析(预处理一次、转换一次)——这是主要冗余; |
||||||
|
2. **C++ 翻译**:`doConvert()` 遍历 AST 并发射 C++; |
||||||
|
3. **C++ 编译**:大量 `.cc` 单元编译,受 PCH 与并行度影响; |
||||||
|
4. **链接**:最终链接往往是大项目瓶颈之一。 |
||||||
|
|
||||||
|
Sources: [docs/AOT_BUILD_SPEED_RESEARCH.md](docs/AOT_BUILD_SPEED_RESEARCH.md) · [src/Translator.php](src/Translator.php#L2455-L2459) |
||||||
|
|
||||||
|
## 已落地的优化 |
||||||
|
|
||||||
|
| 优化 | 说明 | 位置 | |
||||||
|
|---|---|---| |
||||||
|
| **S1 合并解析** | 同一文件 AST 在进程内缓存复用(`parseCachedAst`),消除重复解析 | `src/Translator.php` `parseCachedAst()` | |
||||||
|
| **S2 增量对象缓存** | 生成单元按「编译命令 + PHP ABI + 生成头内容指纹」缓存 `.o`,改动一处不必全量重编 | [增量编译缓存](incremental-cache) | |
||||||
|
| **PCH 预编译头** | 预编译 `phpx.h` 等全局头,加速每个编译单元 | `src/Build/PrecompiledHeaderManager.php` | |
||||||
|
| **并行编译** | `-j` 用 `pcntl` 多进程编译 | `src/Translator.php` `compileWithPcntl()` | |
||||||
|
| **misc 缓存** | phpx 运行时源按 ABI 指纹缓存 | `hasMiscObjectFileCache()` | |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1116-L1160) · [src/Translator.php](src/Translator.php#L1449-L1487) · [src/Translator.php](src/Translator.php#L1584-L1637) |
||||||
|
|
||||||
|
## 调试与剖析 |
||||||
|
|
||||||
|
- `--dry`:只生成 C++,跳过编译/链接,用于检查翻译与定位; |
||||||
|
- `--profile`:启用 gperftools 剖析(仅 Linux),并在 `./tpc app.prof` 模式调 `pprof --web`; |
||||||
|
- `--no-progress` / `-j`:控制进度展示与并行度。 |
||||||
|
|
||||||
|
Sources: [src/compiler.php](src/compiler.php#L48-L73) · [src/Translator.php](src/Translator.php#L30-L36) · [src/Translator.php](src/Translator.php#L251-L255) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 缓存机制细节 → [增量编译缓存](incremental-cache) |
||||||
|
- 优化器如何提升产物性能 → [优化器](optimizer) |
||||||
|
- 优先级规划 → `docs/aot-optimization-priority.md` |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,46 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>编译模式</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>编译模式</h1> |
||||||
|
<p>TypePHP 通过 <code>-m / --mode</code> 支持三种产物形态,由 <code>Translator::setBuildMode()</code> 归一化。三种模式共享同一套 PHP→C++ 翻译,区别只在于<strong>入口约定与链接目标</strong>。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L574-L589">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\docs\COMPILATION_MODES.md">docs/COMPILATION_MODES.md</a></p> |
||||||
|
<h2>三种模式对比</h2> |
||||||
|
<table><thead><tr><th>模式</th><th>参数</th><th>产物</th><th>入口要求</th><th>典型用途</th></tr></thead><tbody><tr><td>二进制</td><td><code>bin</code>(默认)</td><td>独立可执行文件</td><td>必须定义 <code>main()</code></td><td>CLI 工具、独立服务</td></tr><tr><td>共享库</td><td><code>lib</code></td><td><code>.so</code> / <code>.dll</code> / <code>.dylib</code></td><td>不需要 <code>main()</code></td><td>供其它程序调用的逻辑库</td></tr><tr><td>扩展</td><td><code>ext</code></td><td>PHP 扩展 <code>.so</code></td><td>不需要 <code>main()</code></td><td>嵌入现有 PHP 应用(php-fpm 等)</td></tr></tbody></table> |
||||||
|
<p><code>bin</code> 是 <code>binary/cli</code> 的别名;<code>lib</code> 涵盖 <code>library/shared/dll/dylib/so</code>;<code>ext</code> 等价于 <code>extension</code>。未知值会报错退出。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L574-L589">src/Translator.php</a></p> |
||||||
|
<h2>二进制模式(embed)</h2> |
||||||
|
<p><code>bin</code> 模式在 <code>genExtension()</code> 中嵌入 <code>cli_set_process_title</code> / <code>cli_get_process_title</code> 内置函数,并在 <code>RINIT</code> 阶段用 <code>php::eval</code> 直接执行 <code>main()</code>(带 <code>$argc/$argv</code> 或空参数两种签名)。</p> |
||||||
|
<pre class="mermaid">flowchart LR |
||||||
|
A["main()"] --> B["RINIT: php_app_init()"] |
||||||
|
B --> C["php::eval('main($argc,$argv)')"] |
||||||
|
C --> D["RSHUTDOWN: php_app_clean()"]</pre> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1043-L1066">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1055-L1059">src/Translator.php</a></p> |
||||||
|
<h2>共享库模式</h2> |
||||||
|
<p>库模式下,编译器会:</p> |
||||||
|
<ul><li>对 <code>exported</code> 函数加上 <code>TYPEPHP_<NAME>_API</code> 导出宏;</li><li>生成 <code><target>.stub.php</code> 导入桩,供其它 TypePHP 工程按库调用;</li><li>在 Linux 上自动给目标名加 <code>lib</code> 前缀。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L652-L666">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L633-L650">src/Translator.php#L1736-L1738) · [src/Translator.php</a></p> |
||||||
|
<h2>扩展模式</h2> |
||||||
|
<p><code>ext</code> 模式会跳过 <code>main()</code>,使用 <code>ZEND_GET_MODULE()</code> 注册 Zend 模块,并在 <code>RSHUTDOWN</code> 清空函数/类/属性表以支持重复加载。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1033-L1037">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1090-L1091">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>命令行全量参数见 <a href="pages/compile-time-attributes.html">编译期属性</a> 与 <code>docs/COMPILER_CLI.md</code>。</li><li>产物如何被链接 → <a href="pages/build-pipeline.html">构建流水线</a>。</li></ul></div></body></html> |
||||||
@ -0,0 +1,54 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 编译模式 |
||||||
|
|
||||||
|
TypePHP 通过 `-m / --mode` 支持三种产物形态,由 `Translator::setBuildMode()` 归一化。三种模式共享同一套 PHP→C++ 翻译,区别只在于**入口约定与链接目标**。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L574-L589) · [docs/COMPILATION_MODES.md](docs/COMPILATION_MODES.md) |
||||||
|
|
||||||
|
## 三种模式对比 |
||||||
|
|
||||||
|
| 模式 | 参数 | 产物 | 入口要求 | 典型用途 | |
||||||
|
|---|---|---|---|---| |
||||||
|
| 二进制 | `bin`(默认) | 独立可执行文件 | 必须定义 `main()` | CLI 工具、独立服务 | |
||||||
|
| 共享库 | `lib` | `.so` / `.dll` / `.dylib` | 不需要 `main()` | 供其它程序调用的逻辑库 | |
||||||
|
| 扩展 | `ext` | PHP 扩展 `.so` | 不需要 `main()` | 嵌入现有 PHP 应用(php-fpm 等) | |
||||||
|
|
||||||
|
`bin` 是 `binary/cli` 的别名;`lib` 涵盖 `library/shared/dll/dylib/so`;`ext` 等价于 `extension`。未知值会报错退出。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L574-L589) |
||||||
|
|
||||||
|
## 二进制模式(embed) |
||||||
|
|
||||||
|
`bin` 模式在 `genExtension()` 中嵌入 `cli_set_process_title` / `cli_get_process_title` 内置函数,并在 `RINIT` 阶段用 `php::eval` 直接执行 `main()`(带 `$argc/$argv` 或空参数两种签名)。 |
||||||
|
|
||||||
|
```mermaid |
||||||
|
flowchart LR |
||||||
|
A["main()"] --> B["RINIT: php_app_init()"] |
||||||
|
B --> C["php::eval('main($argc,$argv)')"] |
||||||
|
C --> D["RSHUTDOWN: php_app_clean()"] |
||||||
|
``` |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1043-L1066) · [src/Translator.php](src/Translator.php#L1055-L1059) |
||||||
|
|
||||||
|
## 共享库模式 |
||||||
|
|
||||||
|
库模式下,编译器会: |
||||||
|
- 对 `exported` 函数加上 `TYPEPHP_<NAME>_API` 导出宏; |
||||||
|
- 生成 `<target>.stub.php` 导入桩,供其它 TypePHP 工程按库调用; |
||||||
|
- 在 Linux 上自动给目标名加 `lib` 前缀。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L652-L666) · [src/Translator.php#L1736-L1738) · [src/Translator.php](src/Translator.php#L633-L650) |
||||||
|
|
||||||
|
## 扩展模式 |
||||||
|
|
||||||
|
`ext` 模式会跳过 `main()`,使用 `ZEND_GET_MODULE()` 注册 Zend 模块,并在 `RSHUTDOWN` 清空函数/类/属性表以支持重复加载。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1033-L1037) · [src/Translator.php](src/Translator.php#L1090-L1091) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 命令行全量参数见 [编译期属性](compile-time-attributes) 与 `docs/COMPILER_CLI.md`。 |
||||||
|
- 产物如何被链接 → [构建流水线](build-pipeline)。 |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>编译期属性</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>编译期属性</h1> |
||||||
|
<p>除了 PHP 8 的运行时 attribute,TypePHP 引入一组<strong>编译期函数与属性</strong>,用于在翻译阶段向编译器传递类型/语义提示。它们被 <code>Transform</code> 层的 Lowering 算子在遍历 AST 时识别与校验。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Transform\CompileTimeAttribute.php">src/Transform/CompileTimeAttribute.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Transform\CompileTimeAttributeRegistry.php">src/Transform/CompileTimeAttributeRegistry.php</a> · <a href="file:///D:\git\php\aot-compiler\docs\COMPILE_TIME_FUNCTIONS.md">docs/COMPILE_TIME_FUNCTIONS.md</a></p> |
||||||
|
<h2>核心编译期函数</h2> |
||||||
|
<table><thead><tr><th>函数</th><th>用途</th></tr></thead><tbody><tr><td><code>any($v)</code></td><td>显式标记变量为「任意 PHP 值」(通用 <code>php::Var</code>),绕过原生类型推断</td></tr><tr><td><code>refval($v, Type)</code></td><td>以指定类型解释变量(类型恢复/断言)</td></tr><tr><td><code>objval($v, 'ClassName')</code></td><td>从数组/返回值恢复对象的类信息(见 <a href="pages/native-types.html">native-types</a>)</td></tr><tr><td><code>expected()</code> / <code>unexpected()</code></td><td>编译期断言/分支提示</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\COMPILE_TIME_FUNCTIONS.md">docs/COMPILE_TIME_FUNCTIONS.md</a></p> |
||||||
|
<h2>编译期属性</h2> |
||||||
|
<p><code>CompileTimeAttribute::validateNode()</code> 在 <code>Visitor::enterNode</code> 中对每个节点校验编译期属性;<code>CompileTimeAttributeRegistry</code> 维护已知属性注册表。典型如 <code>MustUse</code>(要求函数/方法有返回值且调用方使用)、<code>Any</code>(类型放宽)等——<code>Preprocessor</code> 在 <code>parseFunctionDecl()</code> 中也会对 <code>MustUse</code> 强行校验返回类型。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Transform\Visitor.php#L30">src/Transform/Visitor.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L537">src/Preprocessor.php</a></p> |
||||||
|
<h2>与 Lowering 的配合</h2> |
||||||
|
<p><code>FunctionAttributeLowering</code> / <code>ConstructorLowering</code> / <code>GetterLowering</code> / <code>PropertyMethodLowering</code> 把带编译期属性的节点降级为可翻译结构;<code>RuntimeAttributeFactoryLowering</code> 作为独立访客挂在遍历链上,处理运行时工厂属性。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Transform\Visitor.php#L30-L35">src/Transform/Visitor.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2467">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>遍历中如何调用这些算子 → <a href="pages/transform.html">AST 转换</a></li><li>类型如何因此被放宽/恢复 → <a href="pages/type-check.html">类型检查与兼容</a></li><li>SSA 优化如何消费类型信息 → <a href="pages/optimizer.html">优化器</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 编译期属性 |
||||||
|
|
||||||
|
除了 PHP 8 的运行时 attribute,TypePHP 引入一组**编译期函数与属性**,用于在翻译阶段向编译器传递类型/语义提示。它们被 `Transform` 层的 Lowering 算子在遍历 AST 时识别与校验。 |
||||||
|
|
||||||
|
Sources: [src/Transform/CompileTimeAttribute.php](src/Transform/CompileTimeAttribute.php) · [src/Transform/CompileTimeAttributeRegistry.php](src/Transform/CompileTimeAttributeRegistry.php) · [docs/COMPILE_TIME_FUNCTIONS.md](docs/COMPILE_TIME_FUNCTIONS.md) |
||||||
|
|
||||||
|
## 核心编译期函数 |
||||||
|
|
||||||
|
| 函数 | 用途 | |
||||||
|
|---|---| |
||||||
|
| `any($v)` | 显式标记变量为「任意 PHP 值」(通用 `php::Var`),绕过原生类型推断 | |
||||||
|
| `refval($v, Type)` | 以指定类型解释变量(类型恢复/断言) | |
||||||
|
| `objval($v, 'ClassName')` | 从数组/返回值恢复对象的类信息(见 [native-types](native-types)) | |
||||||
|
| `expected()` / `unexpected()` | 编译期断言/分支提示 | |
||||||
|
|
||||||
|
Sources: [docs/COMPILE_TIME_FUNCTIONS.md](docs/COMPILE_TIME_FUNCTIONS.md) |
||||||
|
|
||||||
|
## 编译期属性 |
||||||
|
|
||||||
|
`CompileTimeAttribute::validateNode()` 在 `Visitor::enterNode` 中对每个节点校验编译期属性;`CompileTimeAttributeRegistry` 维护已知属性注册表。典型如 `MustUse`(要求函数/方法有返回值且调用方使用)、`Any`(类型放宽)等——`Preprocessor` 在 `parseFunctionDecl()` 中也会对 `MustUse` 强行校验返回类型。 |
||||||
|
|
||||||
|
Sources: [src/Transform/Visitor.php](src/Transform/Visitor.php#L30) · [src/Preprocessor.php](src/Preprocessor.php#L537) |
||||||
|
|
||||||
|
## 与 Lowering 的配合 |
||||||
|
|
||||||
|
`FunctionAttributeLowering` / `ConstructorLowering` / `GetterLowering` / `PropertyMethodLowering` 把带编译期属性的节点降级为可翻译结构;`RuntimeAttributeFactoryLowering` 作为独立访客挂在遍历链上,处理运行时工厂属性。 |
||||||
|
|
||||||
|
Sources: [src/Transform/Visitor.php](src/Transform/Visitor.php#L30-L35) · [src/Translator.php](src/Translator.php#L2467) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 遍历中如何调用这些算子 → [AST 转换](transform) |
||||||
|
- 类型如何因此被放宽/恢复 → [类型检查与兼容](type-check) |
||||||
|
- SSA 优化如何消费类型信息 → [优化器](optimizer) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,44 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>扩展模块生成</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>扩展模块生成</h1> |
||||||
|
<p><code>genExtension()</code> 是流水线里最大的一块生成逻辑,产出 <code>extension-<target>.cc</code>——一个标准 Zend 扩展模块。它把前面收集的所有类、函数、常量、全局变量缝合进 Zend 的生命周期钩子(MINIT / MINIT / RINIT / RSHUTDOWN)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L739-L1105">src/Translator.php</a></p> |
||||||
|
<h2>生成结构</h2> |
||||||
|
<pre class="mermaid">flowchart TD |
||||||
|
A["extension-<target>.cc"] --> B["全局变量 / 类表 / 函数表 / 属性表 声明"] |
||||||
|
B --> C["php_get_class / php_get_func / php_get_method / php_get_prop"] |
||||||
|
C --> D["ext_functions[] (ZEND_FE 列表)"] |
||||||
|
D --> E["PHP_MINIT: 注册类/反射处理器/符号"] |
||||||
|
E --> F["PHP_RINIT: php_app_init + eval(main)"] |
||||||
|
F --> G["PHP_RSHUTDOWN: php_app_clean + request_shutdown"] |
||||||
|
G --> H["zend_module_entry"]</pre> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L769-L1087">src/Translator.php</a></p> |
||||||
|
<h2>关键片段</h2> |
||||||
|
<ul><li><strong>符号解析函数</strong>:<code>php_get_class</code> / <code>php_get_func</code> / <code>php_get_method</code> / <code>php_get_prop</code> 用缓存的 <code>class_map</code> / <code>func_map</code> / <code>prop_map</code> 把字符串名映射到运行时 <code>zend_class_entry<em></code> / <code>zend_function</em></code>,带 <code>UNEXPECTED</code> 快路径。</li><li><strong>MINIT</strong>:安装 Fiber/Generator 类、反射属性处理器,逐个注册类并设置属性处理器;最后调用各 <code>registerSymbols</code> 函数。</li><li><strong>RINIT</strong>:<code>php_app_init()</code> 注册常量、全局变量、静态属性、类数组常量;<code>bin</code> 模式下 <code>php::eval</code> 执行 <code>main()</code>(按 <code>$argc/$argv</code> 或空参两种签名)。</li><li><strong>RSHUTDOWN</strong>:<code>php_app_clean()</code> 清理全局变量、VAR 常量、类数组常量(含继承来的数组常量),<code>ext</code> 模式额外清空三张映射表以支持模块重载。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L791-L837">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L884-L1066">src/Translator.php</a></p> |
||||||
|
<h2>模式差异</h2> |
||||||
|
<ul><li><code>bin</code>:嵌入 <code>cli_set_process_title</code>,<code>RINIT</code> 直接跑 <code>main()</code>;</li><li><code>lib</code>:Linux 下导出 <code>save_ps_args</code> 占位符;<code>getModuleName()</code> 仍生成模块入口;</li><li><code>ext</code>:用 <code>ZEND_GET_MODULE()</code>,跳过 <code>main()</code>,RSHUTDOWN 清空映射表。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L754-L767">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1090-L1096">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>函数/类声明如何生成 → <a href="pages/gen-functions-classes.html">函数与类生成</a></li><li>生成的 <code>.cc</code> 如何被编译/缓存 → <a href="pages/incremental-cache.html">增量编译缓存</a></li><li>三种产物形态 → <a href="pages/compilation-modes.html">编译模式</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 扩展模块生成 |
||||||
|
|
||||||
|
`genExtension()` 是流水线里最大的一块生成逻辑,产出 `extension-<target>.cc`——一个标准 Zend 扩展模块。它把前面收集的所有类、函数、常量、全局变量缝合进 Zend 的生命周期钩子(MINIT / MINIT / RINIT / RSHUTDOWN)。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L739-L1105) |
||||||
|
|
||||||
|
## 生成结构 |
||||||
|
|
||||||
|
```mermaid |
||||||
|
flowchart TD |
||||||
|
A["extension-<target>.cc"] --> B["全局变量 / 类表 / 函数表 / 属性表 声明"] |
||||||
|
B --> C["php_get_class / php_get_func / php_get_method / php_get_prop"] |
||||||
|
C --> D["ext_functions[] (ZEND_FE 列表)"] |
||||||
|
D --> E["PHP_MINIT: 注册类/反射处理器/符号"] |
||||||
|
E --> F["PHP_RINIT: php_app_init + eval(main)"] |
||||||
|
F --> G["PHP_RSHUTDOWN: php_app_clean + request_shutdown"] |
||||||
|
G --> H["zend_module_entry"] |
||||||
|
``` |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L769-L1087) |
||||||
|
|
||||||
|
## 关键片段 |
||||||
|
|
||||||
|
- **符号解析函数**:`php_get_class` / `php_get_func` / `php_get_method` / `php_get_prop` 用缓存的 `class_map` / `func_map` / `prop_map` 把字符串名映射到运行时 `zend_class_entry*` / `zend_function*`,带 `UNEXPECTED` 快路径。 |
||||||
|
- **MINIT**:安装 Fiber/Generator 类、反射属性处理器,逐个注册类并设置属性处理器;最后调用各 `registerSymbols` 函数。 |
||||||
|
- **RINIT**:`php_app_init()` 注册常量、全局变量、静态属性、类数组常量;`bin` 模式下 `php::eval` 执行 `main()`(按 `$argc/$argv` 或空参两种签名)。 |
||||||
|
- **RSHUTDOWN**:`php_app_clean()` 清理全局变量、VAR 常量、类数组常量(含继承来的数组常量),`ext` 模式额外清空三张映射表以支持模块重载。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L791-L837) · [src/Translator.php](src/Translator.php#L884-L1066) |
||||||
|
|
||||||
|
## 模式差异 |
||||||
|
|
||||||
|
- `bin`:嵌入 `cli_set_process_title`,`RINIT` 直接跑 `main()`; |
||||||
|
- `lib`:Linux 下导出 `save_ps_args` 占位符;`getModuleName()` 仍生成模块入口; |
||||||
|
- `ext`:用 `ZEND_GET_MODULE()`,跳过 `main()`,RSHUTDOWN 清空映射表。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L754-L767) · [src/Translator.php](src/Translator.php#L1090-L1096) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 函数/类声明如何生成 → [函数与类生成](gen-functions-classes) |
||||||
|
- 生成的 `.cc` 如何被编译/缓存 → [增量编译缓存](incremental-cache) |
||||||
|
- 三种产物形态 → [编译模式](compilation-modes) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,41 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>函数与类生成</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>函数与类生成</h1> |
||||||
|
<p>本章聚焦两个最频繁的发射动作:<code>genFunctionWrapper()</code>(函数/方法 → C++ 函数)与 <code>genClassWrapper()</code>(类/接口/枚举 → C++ 类与 zend_class_entry 注册)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2515-L2528">src/Translator.php</a></p> |
||||||
|
<h2>函数生成</h2> |
||||||
|
<p><code>genFunctionDeclarations()</code> 为每个 <code>FunctionDef</code> 生成前向声明,规则包括:</p> |
||||||
|
<ul><li><strong>方法</strong>:首参加 <code>php::Object &this_</code>;trait 父类场景追加 <code>zend_class_entry *trait_parent_ce</code>;</li><li><strong>变参</strong>:声明为 <code>php::Array</code> 并附默认参数表达式;</li><li><strong>多返回</strong>:在 <code>typephp_multi_return</code> 命名空间再生成一份返回元组版本(<code>hasMultiReturn()</code>);</li><li><strong>热/冷路径</strong>:<code>hot</code>/<code>cold</code> 加 <code>TYPEPHP_HOT_ATTRIBUTE</code> / <code>TYPEPHP_COLD_ATTRIBUTE</code>;</li><li><strong>导入/导出</strong>:跨库函数用 <code>TYPEPHP_<LIB>_IMPORT</code> 宏,库导出函数用 <code>TYPEPHP_<NAME>_API</code>。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1740-L1830">src/Translator.php</a></p> |
||||||
|
<h2>类生成</h2> |
||||||
|
<p><code>genClassWrapper()</code> 为每个 <code>ClassDef</code> / <code>InterfaceDef</code> 生成:</p> |
||||||
|
<ol><li>C++ 类结构与属性布局;</li><li>静态属性默认值(<code>php::setStaticProperty</code>);</li><li><code>create_object</code> 钩子与属性处理器安装(<code>genClassPropertyInit()</code>);</li><li>数组常量(<code>genClassArrayConstants()</code>)。</li></ol> |
||||||
|
<p>属性初始化分两种情况(<a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1909-L1963">src/Translator.php</a>):</p> |
||||||
|
<ul><li><strong>标量/常量默认值</strong>:包成 <code>php::Var</code> 写入属性表;</li><li><strong>数组默认值</strong>:用 <code>ArrayInitPlan</code> 生成构造表达式,避免重复求值。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1894-L1966">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L974-L1002">src/Preprocessor.php</a></p> |
||||||
|
<h2>类注册排序</h2> |
||||||
|
<p><code>genClassCeList()</code> 用 <code>marcj/topsort</code> 按继承依赖拓扑排序生成 <code>zend_class_entry</code> 注册顺序,确保父类先于子类注册;内置类/接口(不在符号表中)直接加入排序而不参与依赖解析。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2539-L2568">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>所有声明头如何汇总 → <a href="pages/generator.html">生成器总览</a></li><li>模块入口如何引用这些符号 → <a href="pages/gen-extension.html">扩展模块生成</a></li><li>属性类型约束 → <a href="pages/native-types.html">原生类型映射</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,48 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 函数与类生成 |
||||||
|
|
||||||
|
本章聚焦两个最频繁的发射动作:`genFunctionWrapper()`(函数/方法 → C++ 函数)与 `genClassWrapper()`(类/接口/枚举 → C++ 类与 zend_class_entry 注册)。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L2515-L2528) |
||||||
|
|
||||||
|
## 函数生成 |
||||||
|
|
||||||
|
`genFunctionDeclarations()` 为每个 `FunctionDef` 生成前向声明,规则包括: |
||||||
|
|
||||||
|
- **方法**:首参加 `php::Object &this_`;trait 父类场景追加 `zend_class_entry *trait_parent_ce`; |
||||||
|
- **变参**:声明为 `php::Array` 并附默认参数表达式; |
||||||
|
- **多返回**:在 `typephp_multi_return` 命名空间再生成一份返回元组版本(`hasMultiReturn()`); |
||||||
|
- **热/冷路径**:`hot`/`cold` 加 `TYPEPHP_HOT_ATTRIBUTE` / `TYPEPHP_COLD_ATTRIBUTE`; |
||||||
|
- **导入/导出**:跨库函数用 `TYPEPHP_<LIB>_IMPORT` 宏,库导出函数用 `TYPEPHP_<NAME>_API`。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1740-L1830) |
||||||
|
|
||||||
|
## 类生成 |
||||||
|
|
||||||
|
`genClassWrapper()` 为每个 `ClassDef` / `InterfaceDef` 生成: |
||||||
|
|
||||||
|
1. C++ 类结构与属性布局; |
||||||
|
2. 静态属性默认值(`php::setStaticProperty`); |
||||||
|
3. `create_object` 钩子与属性处理器安装(`genClassPropertyInit()`); |
||||||
|
4. 数组常量(`genClassArrayConstants()`)。 |
||||||
|
|
||||||
|
属性初始化分两种情况([src/Translator.php](src/Translator.php#L1909-L1963)): |
||||||
|
- **标量/常量默认值**:包成 `php::Var` 写入属性表; |
||||||
|
- **数组默认值**:用 `ArrayInitPlan` 生成构造表达式,避免重复求值。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1894-L1966) · [src/Preprocessor.php](src/Preprocessor.php#L974-L1002) |
||||||
|
|
||||||
|
## 类注册排序 |
||||||
|
|
||||||
|
`genClassCeList()` 用 `marcj/topsort` 按继承依赖拓扑排序生成 `zend_class_entry` 注册顺序,确保父类先于子类注册;内置类/接口(不在符号表中)直接加入排序而不参与依赖解析。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L2539-L2568) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 所有声明头如何汇总 → [生成器总览](generator) |
||||||
|
- 模块入口如何引用这些符号 → [扩展模块生成](gen-extension) |
||||||
|
- 属性类型约束 → [原生类型映射](native-types) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>生成器总览</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>生成器总览</h1> |
||||||
|
<p>当 <code>doConvert()</code> 收集完一个文件的语句后,调用各 <code>genXxx</code> 方法把 PHP 构造发射为 C++。所有发射逻辑分两类:<strong>声明头文件生成</strong>(<code><em>.h</code>)与 <strong>实现单元生成</strong>(<code></em>.cc</code>)。<code>src/Generator/</code> 下是一组辅助生成器,负责闭包、匿名类、参数、资源文件等专项结构。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2477-L2536">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Generator">src/Generator/</a></p> |
||||||
|
<h2>主发射方法(在 Translator / CompilerBase)</h2> |
||||||
|
<table><thead><tr><th>方法</th><th>产物</th></tr></thead><tbody><tr><td><code>genFunctionDeclarations()</code></td><td><code>php_<target>_func_decl.h</code>:所有函数前向声明</td></tr><tr><td><code>genDataDeclarations()</code></td><td><code>php_<target>_data_decl.h</code>:全局变量 / 常量 / 类映射表声明</td></tr><tr><td><code>genExtension()</code></td><td><code>extension-<target>.cc</code>:模块入口(MINIT/RINIT/RSHUTDOWN)</td></tr><tr><td><code>genClassWrapper()</code></td><td>单个类/接口的 C++ 实现</td></tr><tr><td><code>genFunctionWrapper()</code></td><td>单个函数的 C++ 实现</td></tr><tr><td><code>genClassPropertyInit()</code></td><td>属性默认值与 create_object 初始化块</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1728-L1786">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L692-L737">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L739-L1105">src/Translator.php</a></p> |
||||||
|
<h2>专项生成器(src/Generator/)</h2> |
||||||
|
<table><thead><tr><th>生成器</th><th>负责</th></tr></thead><tbody><tr><td><code>ClosureGenerator</code></td><td>匿名函数 / 闭包 → C++ lambda 包装</td></tr><tr><td><code>AnonClassGenerator</code></td><td>匿名类</td></tr><tr><td><code>CallArgumentGenerator</code></td><td>调用实参的默认值/展开处理</td></tr><tr><td><code>DefaultArgumentGenerator</code></td><td>默认参数 helper(<code>genDefaultArgumentHelper*</code>)</td></tr><tr><td><code>FiberGenerator</code></td><td>协程 / Fiber 生成</td></tr><tr><td><code>PropertyPromotion</code></td><td>构造函数属性提升</td></tr><tr><td><code>ResourceFileGenerator</code></td><td>Windows <code>.rc</code> 资源文件</td></tr><tr><td><code>LibraryImportStubGenerator</code></td><td>跨库导入桩 <code><target>.stub.php</code></td></tr><tr><td><code>TypeCheckGenerator</code></td><td>运行时类型检查代码</td></tr><tr><td><code>Symbol</code></td><td>符号名转义工具</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Generator">src/Generator/</a></p> |
||||||
|
<h2>头文件依赖与增量缓存</h2> |
||||||
|
<p>每个生成的 <code>.cc</code> 都 <code>#include</code> 项目级 <code>func_decl.h</code> / <code>data_decl.h</code> 与各自的 <code>_arginfo.h</code>。这些共享头文件的内容指纹正是 <a href="pages/incremental-cache.html">增量编译缓存</a> 判定是否重编译的依据——任一头变化都会让依赖它的 <code>.cc</code> 失效。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1253-L1270">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1880-L1892">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>函数与类如何逐个生成 → <a href="pages/gen-functions-classes.html">函数与类生成</a></li><li>模块入口如何拼装 → <a href="pages/gen-extension.html">扩展模块生成</a></li><li>翻译前的 AST 改写 → <a href="pages/transform.html">AST 转换</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,51 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 生成器总览 |
||||||
|
|
||||||
|
当 `doConvert()` 收集完一个文件的语句后,调用各 `genXxx` 方法把 PHP 构造发射为 C++。所有发射逻辑分两类:**声明头文件生成**(`*.h`)与 **实现单元生成**(`*.cc`)。`src/Generator/` 下是一组辅助生成器,负责闭包、匿名类、参数、资源文件等专项结构。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L2477-L2536) · [src/Generator/](src/Generator/) |
||||||
|
|
||||||
|
## 主发射方法(在 Translator / CompilerBase) |
||||||
|
|
||||||
|
| 方法 | 产物 | |
||||||
|
|---|---| |
||||||
|
| `genFunctionDeclarations()` | `php_<target>_func_decl.h`:所有函数前向声明 | |
||||||
|
| `genDataDeclarations()` | `php_<target>_data_decl.h`:全局变量 / 常量 / 类映射表声明 | |
||||||
|
| `genExtension()` | `extension-<target>.cc`:模块入口(MINIT/RINIT/RSHUTDOWN) | |
||||||
|
| `genClassWrapper()` | 单个类/接口的 C++ 实现 | |
||||||
|
| `genFunctionWrapper()` | 单个函数的 C++ 实现 | |
||||||
|
| `genClassPropertyInit()` | 属性默认值与 create_object 初始化块 | |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1728-L1786) · [src/Translator.php](src/Translator.php#L692-L737) · [src/Translator.php](src/Translator.php#L739-L1105) |
||||||
|
|
||||||
|
## 专项生成器(src/Generator/) |
||||||
|
|
||||||
|
| 生成器 | 负责 | |
||||||
|
|---|---| |
||||||
|
| `ClosureGenerator` | 匿名函数 / 闭包 → C++ lambda 包装 | |
||||||
|
| `AnonClassGenerator` | 匿名类 | |
||||||
|
| `CallArgumentGenerator` | 调用实参的默认值/展开处理 | |
||||||
|
| `DefaultArgumentGenerator` | 默认参数 helper(`genDefaultArgumentHelper*`) | |
||||||
|
| `FiberGenerator` | 协程 / Fiber 生成 | |
||||||
|
| `PropertyPromotion` | 构造函数属性提升 | |
||||||
|
| `ResourceFileGenerator` | Windows `.rc` 资源文件 | |
||||||
|
| `LibraryImportStubGenerator` | 跨库导入桩 `<target>.stub.php` | |
||||||
|
| `TypeCheckGenerator` | 运行时类型检查代码 | |
||||||
|
| `Symbol` | 符号名转义工具 | |
||||||
|
|
||||||
|
Sources: [src/Generator/](src/Generator/) |
||||||
|
|
||||||
|
## 头文件依赖与增量缓存 |
||||||
|
|
||||||
|
每个生成的 `.cc` 都 `#include` 项目级 `func_decl.h` / `data_decl.h` 与各自的 `_arginfo.h`。这些共享头文件的内容指纹正是 [增量编译缓存](incremental-cache) 判定是否重编译的依据——任一头变化都会让依赖它的 `.cc` 失效。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1253-L1270) · [src/Translator.php](src/Translator.php#L1880-L1892) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 函数与类如何逐个生成 → [函数与类生成](gen-functions-classes) |
||||||
|
- 模块入口如何拼装 → [扩展模块生成](gen-extension) |
||||||
|
- 翻译前的 AST 改写 → [AST 转换](transform) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>高精度数值类型</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>高精度数值类型</h1> |
||||||
|
<p>PHP 原生 <code>int</code>(64 位)与 <code>float</code>(IEEE754 double)在金融、科学、密码学场景精度不足。TypePHP 提供三种高精度类型,底层直接调用成熟 C/C++ 数学库,<strong>零方法查找/解释器分派开销</strong>(运算本身仍有数学库计算与装箱成本)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\HIGH_PRECISION_TYPES.md#L1-L44">docs/HIGH_PRECISION_TYPES.md</a></p> |
||||||
|
<h2>三种类型概览</h2> |
||||||
|
<table><thead><tr><th>类型</th><th>底层库</th><th>特点</th></tr></thead><tbody><tr><td><code>bigInt</code> (<code>php::BigInt</code>)</td><td>GMP (<code>libgmp</code>)</td><td>任意精度整数,永不溢出</td></tr><tr><td><code>decimal</code> (<code>php::Decimal</code>)</td><td>libmpdec</td><td>十进制小数,约 50 位有效数字,无二进制浮点误差</td></tr><tr><td><code>bigFloat</code> (<code>php::BigFloat</code>)</td><td>MPFR (<code>libmpfr</code>)</td><td>默认 256 bit,字符串输出 64 位有效数字</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\HIGH_PRECISION_TYPES.md#L39-L43">docs/HIGH_PRECISION_TYPES.md</a> · <a href="file:///D:\git\php\aot-compiler\src\Type.php#L15-L17">src/Type.php</a></p> |
||||||
|
<h2>使用前置</h2> |
||||||
|
<pre><code><?php |
||||||
|
declare(strict_types=1); |
||||||
|
use native_types; |
||||||
|
function main(): void { |
||||||
|
$x = bigInt(123456789012345678901234567890); // 30 位整数不丢精度 |
||||||
|
}</code></pre> |
||||||
|
<p>系统需安装:<code>libgmp-dev</code>、<code>libmpdec-dev</code>、<code>libmpfr-dev</code>(见 README 依赖章节)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\HIGH_PRECISION_TYPES.md#L51-L59">docs/HIGH_PRECISION_TYPES.md</a> · <a href="file:///D:\git\php\aot-compiler\README.md#L29-L40">README.md</a></p> |
||||||
|
<h2>能力</h2> |
||||||
|
<ul><li><strong>算术/比较/复合赋值</strong>:<code>+ - * / %</code>、<code>< > ==</code> 以及 <code>+=</code> 等均可用于高精度类型;</li><li><strong>通用方法调用</strong>:通过 <a href="file:///D:\git\php\aot-compiler\universal-methods">通用方法</a>(见 <code>docs/UNIVERSAL_METHODS.md</code>)统一接口;</li><li><strong>类型转换</strong>:高精度类型之间及与原生类型可互转;</li><li><strong>超长字面量自动识别</strong>:超出原生范围的整数字面量自动识别为 <code>bigInt</code>;</li><li><strong>混合运算类型提升</strong>:不同精度参与运算时按规则提升。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\HIGH_PRECISION_TYPES.md#L7-L17">docs/HIGH_PRECISION_TYPES.md</a></p> |
||||||
|
<h2>设计取舍与优化</h2> |
||||||
|
<p>相关设计文档(属于调研/规划,可能与当前实现有出入):</p> |
||||||
|
<ul><li><code>docs/BIG_NUMBER_INPLACE_OPTIMIZATION_PLAN.md</code> —— 高精度类型原地运算优化方案;</li><li><code>docs/GMP_GAP.md</code> —— GMP 能力差异分析。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\BIG_NUMBER_INPLACE_OPTIMIZATION_PLAN.md">docs/BIG_NUMBER_INPLACE_OPTIMIZATION_PLAN.md</a> · <a href="file:///D:\git\php\aot-compiler\docs\GMP_GAP.md">docs/GMP_GAP.md</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>类型如何映射到 C++ → <a href="pages/native-types.html">原生类型映射</a></li><li>类型判定逻辑 → <a href="pages/type-check.html">类型检查与兼容</a></li><li>数组/容器配合 → <code>docs/STD_CONTAINERS.md</code></li></ul></div></body></html> |
||||||
@ -0,0 +1,58 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 高精度数值类型 |
||||||
|
|
||||||
|
PHP 原生 `int`(64 位)与 `float`(IEEE754 double)在金融、科学、密码学场景精度不足。TypePHP 提供三种高精度类型,底层直接调用成熟 C/C++ 数学库,**零方法查找/解释器分派开销**(运算本身仍有数学库计算与装箱成本)。 |
||||||
|
|
||||||
|
Sources: [docs/HIGH_PRECISION_TYPES.md](docs/HIGH_PRECISION_TYPES.md#L1-L44) |
||||||
|
|
||||||
|
## 三种类型概览 |
||||||
|
|
||||||
|
| 类型 | 底层库 | 特点 | |
||||||
|
|---|---|---| |
||||||
|
| `bigInt` (`php::BigInt`) | GMP (`libgmp`) | 任意精度整数,永不溢出 | |
||||||
|
| `decimal` (`php::Decimal`) | libmpdec | 十进制小数,约 50 位有效数字,无二进制浮点误差 | |
||||||
|
| `bigFloat` (`php::BigFloat`) | MPFR (`libmpfr`) | 默认 256 bit,字符串输出 64 位有效数字 | |
||||||
|
|
||||||
|
Sources: [docs/HIGH_PRECISION_TYPES.md](docs/HIGH_PRECISION_TYPES.md#L39-L43) · [src/Type.php](src/Type.php#L15-L17) |
||||||
|
|
||||||
|
## 使用前置 |
||||||
|
|
||||||
|
```php |
||||||
|
<?php |
||||||
|
declare(strict_types=1); |
||||||
|
use native_types; |
||||||
|
function main(): void { |
||||||
|
$x = bigInt(123456789012345678901234567890); // 30 位整数不丢精度 |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
系统需安装:`libgmp-dev`、`libmpdec-dev`、`libmpfr-dev`(见 README 依赖章节)。 |
||||||
|
|
||||||
|
Sources: [docs/HIGH_PRECISION_TYPES.md](docs/HIGH_PRECISION_TYPES.md#L51-L59) · [README.md](README.md#L29-L40) |
||||||
|
|
||||||
|
## 能力 |
||||||
|
|
||||||
|
- **算术/比较/复合赋值**:`+ - * / %`、`< > ==` 以及 `+=` 等均可用于高精度类型; |
||||||
|
- **通用方法调用**:通过 [通用方法](universal-methods)(见 `docs/UNIVERSAL_METHODS.md`)统一接口; |
||||||
|
- **类型转换**:高精度类型之间及与原生类型可互转; |
||||||
|
- **超长字面量自动识别**:超出原生范围的整数字面量自动识别为 `bigInt`; |
||||||
|
- **混合运算类型提升**:不同精度参与运算时按规则提升。 |
||||||
|
|
||||||
|
Sources: [docs/HIGH_PRECISION_TYPES.md](docs/HIGH_PRECISION_TYPES.md#L7-L17) |
||||||
|
|
||||||
|
## 设计取舍与优化 |
||||||
|
|
||||||
|
相关设计文档(属于调研/规划,可能与当前实现有出入): |
||||||
|
- `docs/BIG_NUMBER_INPLACE_OPTIMIZATION_PLAN.md` —— 高精度类型原地运算优化方案; |
||||||
|
- `docs/GMP_GAP.md` —— GMP 能力差异分析。 |
||||||
|
|
||||||
|
Sources: [docs/BIG_NUMBER_INPLACE_OPTIMIZATION_PLAN.md](docs/BIG_NUMBER_INPLACE_OPTIMIZATION_PLAN.md) · [docs/GMP_GAP.md](docs/GMP_GAP.md) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 类型如何映射到 C++ → [原生类型映射](native-types) |
||||||
|
- 类型判定逻辑 → [类型检查与兼容](type-check) |
||||||
|
- 数组/容器配合 → `docs/STD_CONTAINERS.md` |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>不支持的 PHP 特性</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>不支持的 PHP 特性</h1> |
||||||
|
<p>AOT 编译对 PHP 运行时做了静态化假设,因此<strong>并非所有 PHP 特性都可编译</strong>。遇到编译报错(如 <code>Not implemented</code>)时,应据此清单判断是设计限制、有意规则,还是尚未实现。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\INCOMPATIBLE_PHP_FEATURES.md">docs/INCOMPATIBLE_PHP_FEATURES.md</a> · <a href="file:///D:\git\php\aot-compiler\docs\PHP_INCOMPATIBILITY_CLASSIFICATION.md">docs/PHP_INCOMPATIBILITY_CLASSIFICATION.md</a></p> |
||||||
|
<h2>不兼容分类</h2> |
||||||
|
<p><code>PHP_INCOMPATIBILITY_CLASSIFICATION.md</code> 把限制分成四类,避免维护多份清单:</p> |
||||||
|
<table><thead><tr><th>类别</th><th>含义</th></tr></thead><tbody><tr><td><strong>Hard Limit</strong></td><td>架构上无法支持(如依赖运行时动态加载的特性)</td></tr><tr><td><strong>Intentional Rule</strong></td><td>有意为之的设计规则(如可执行代码必须放进 <code>main()</code>)</td></tr><tr><td><strong>Pending</strong></td><td>计划支持但尚未实现</td></tr><tr><td><strong>Partial</strong></td><td>部分支持,有约束条件</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\PHP_INCOMPATIBILITY_CLASSIFICATION.md">docs/PHP_INCOMPATIBILITY_CLASSIFICATION.md</a></p> |
||||||
|
<h2>常见设计规则</h2> |
||||||
|
<ul><li><strong>游离可执行代码</strong>:文件顶层的可执行语句(循环、函数调用)不被允许,必须放在 <code>main()</code> 或函数体内;</li><li><strong>固定值类型属性不可 <code>unset</code>/赋 <code>null</code></strong>:见 <a href="pages/native-types.html">原生类型映射</a>;</li><li><strong>显式返回类型</strong>:函数/方法必须声明返回类型(<code>parseFunctionDecl()</code> 会 <code>fatalError</code>)。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\QUICKSTART.md#L191-L203">docs/QUICKSTART.md</a> · <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L497-L536">src/Preprocessor.php</a></p> |
||||||
|
<h2>维护约定</h2> |
||||||
|
<ol><li>兼容性变化须<strong>同时更新</strong> <code>INCOMPATIBLE_PHP_FEATURES.md</code> 与分类文档;</li><li>所有语法/语义限制统一链接到当前兼容性清单,避免重复;</li><li>功能是否支持以 <strong>PHPT / PHPUnit 回归测试</strong> 为准;</li><li>历史研究文档(hhvm/kphp/peachpie 对比等)保留原始结论,不悄然改写为当前状态。</li></ol> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\README.md#L30-L35">docs/README.md</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>运行期能力边界 → <a href="pages/runtime.html">PHPX 运行时集成</a></li><li>类型约束 → <a href="pages/native-types.html">原生类型映射</a></li><li>属性/类约束 → <a href="pages/gen-functions-classes.html">函数与类生成</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 不支持的 PHP 特性 |
||||||
|
|
||||||
|
AOT 编译对 PHP 运行时做了静态化假设,因此**并非所有 PHP 特性都可编译**。遇到编译报错(如 `Not implemented`)时,应据此清单判断是设计限制、有意规则,还是尚未实现。 |
||||||
|
|
||||||
|
Sources: [docs/INCOMPATIBLE_PHP_FEATURES.md](docs/INCOMPATIBLE_PHP_FEATURES.md) · [docs/PHP_INCOMPATIBILITY_CLASSIFICATION.md](docs/PHP_INCOMPATIBILITY_CLASSIFICATION.md) |
||||||
|
|
||||||
|
## 不兼容分类 |
||||||
|
|
||||||
|
`PHP_INCOMPATIBILITY_CLASSIFICATION.md` 把限制分成四类,避免维护多份清单: |
||||||
|
|
||||||
|
| 类别 | 含义 | |
||||||
|
|---|---| |
||||||
|
| **Hard Limit** | 架构上无法支持(如依赖运行时动态加载的特性) | |
||||||
|
| **Intentional Rule** | 有意为之的设计规则(如可执行代码必须放进 `main()`) | |
||||||
|
| **Pending** | 计划支持但尚未实现 | |
||||||
|
| **Partial** | 部分支持,有约束条件 | |
||||||
|
|
||||||
|
Sources: [docs/PHP_INCOMPATIBILITY_CLASSIFICATION.md](docs/PHP_INCOMPATIBILITY_CLASSIFICATION.md) |
||||||
|
|
||||||
|
## 常见设计规则 |
||||||
|
|
||||||
|
- **游离可执行代码**:文件顶层的可执行语句(循环、函数调用)不被允许,必须放在 `main()` 或函数体内; |
||||||
|
- **固定值类型属性不可 `unset`/赋 `null`**:见 [原生类型映射](native-types); |
||||||
|
- **显式返回类型**:函数/方法必须声明返回类型(`parseFunctionDecl()` 会 `fatalError`)。 |
||||||
|
|
||||||
|
Sources: [docs/QUICKSTART.md](docs/QUICKSTART.md#L191-L203) · [src/Preprocessor.php](src/Preprocessor.php#L497-L536) |
||||||
|
|
||||||
|
## 维护约定 |
||||||
|
|
||||||
|
1. 兼容性变化须**同时更新** `INCOMPATIBLE_PHP_FEATURES.md` 与分类文档; |
||||||
|
2. 所有语法/语义限制统一链接到当前兼容性清单,避免重复; |
||||||
|
3. 功能是否支持以 **PHPT / PHPUnit 回归测试** 为准; |
||||||
|
4. 历史研究文档(hhvm/kphp/peachpie 对比等)保留原始结论,不悄然改写为当前状态。 |
||||||
|
|
||||||
|
Sources: [docs/README.md](docs/README.md#L30-L35) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 运行期能力边界 → [PHPX 运行时集成](runtime) |
||||||
|
- 类型约束 → [原生类型映射](native-types) |
||||||
|
- 属性/类约束 → [函数与类生成](gen-functions-classes) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>增量编译缓存</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>增量编译缓存</h1> |
||||||
|
<p>TypePHP 的二次编译速度,很大程度上取决于<strong>对象文件缓存是否命中</strong>。编译器对两类源文件做内容指纹缓存:phpx 的 <code>misc</code> 源,以及编译器自己生成的 <code>.cc</code> 单元。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1116-L1292">src/Translator.php</a></p> |
||||||
|
<h2>两类缓存</h2> |
||||||
|
<table><thead><tr><th>方法</th><th>适用对象</th><th>缓存键组成</th></tr></thead><tbody><tr><td><code>hasMiscObjectFileCache()</code></td><td><code>phpx/src/misc/*.cc</code></td><td>编译命令 + PHP ABI(<code>PHP_VERSION_ID</code> / <code>ZEND_MODULE_API_NO</code> / <code>PHP_ZTS</code> / <code>PHP_INT_SIZE</code> 等)+ phpx 头文件 mtime</td></tr><tr><td><code>hasGeneratedObjectFileCache()</code></td><td>构建目录下的生成 <code>.cc</code></td><td>编译命令 + PHP ABI + <strong>所有生成头文件内容指纹</strong>(<code>func_decl.h</code> / <code>data_decl.h</code> / 各 <code>_arginfo.h</code>)</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1116-L1160">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1207-L1240">src/Translator.php</a></p> |
||||||
|
<h2>缓存键构造</h2> |
||||||
|
<pre><code>hash('sha256', |
||||||
|
buildCompileFileCommand(source, object) // 可复现的编译命令行 |
||||||
|
. "\0" . serialize($abi) // PHP ABI 快照 |
||||||
|
[. "\0" . header . "\0" . hash_file(header)] // 仅生成单元:逐头文件内容 |
||||||
|
)</code></pre> |
||||||
|
<p>键写入 <code><object>.typephp-cache</code> 元数据文件(<code>getMiscObjectCacheMetadataFile()</code>)。每次 <code>compileFile()</code> 先比较元数据文件中的键,再比较 <code>.o</code> 与源/头的 mtime——<strong>任一不满足即失效并重编译</strong>。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1162-L1187">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1272-L1292">src/Translator.php</a></p> |
||||||
|
<h2>设计要点</h2> |
||||||
|
<ul><li><strong><code>extension-<target>.cc</code> 故意不进共享头依赖</strong>:它的内容随任意类变化而改变,若纳入指纹会让每个 <code>.cc</code> 都失效,破坏增量构建。它自身作为生成单元拥有独立缓存项。</li><li><strong><code>--force</code> / <code>--profile</code> 强制失效</strong>:这两类参数会跳过全部缓存,确保 misc 文件也被重编(profiler 需 <code>PPROF_ON</code> 宏生效)。</li><li><strong>头文件依赖集排序后参与哈希</strong>,避免路径顺序差异导致误失效。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1242-L1271">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1218-L1226">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>缓存何时被查询 → <a href="pages/build-pipeline.html">构建流水线</a></li><li>编译命令从何而来 → <a href="pages/backend.html">编译器后端</a></li><li>整体性能分析 → <a href="pages/build-speed.html">构建速度与性能</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,46 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 增量编译缓存 |
||||||
|
|
||||||
|
TypePHP 的二次编译速度,很大程度上取决于**对象文件缓存是否命中**。编译器对两类源文件做内容指纹缓存:phpx 的 `misc` 源,以及编译器自己生成的 `.cc` 单元。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1116-L1292) |
||||||
|
|
||||||
|
## 两类缓存 |
||||||
|
|
||||||
|
| 方法 | 适用对象 | 缓存键组成 | |
||||||
|
|---|---|---| |
||||||
|
| `hasMiscObjectFileCache()` | `phpx/src/misc/*.cc` | 编译命令 + PHP ABI(`PHP_VERSION_ID` / `ZEND_MODULE_API_NO` / `PHP_ZTS` / `PHP_INT_SIZE` 等)+ phpx 头文件 mtime | |
||||||
|
| `hasGeneratedObjectFileCache()` | 构建目录下的生成 `.cc` | 编译命令 + PHP ABI + **所有生成头文件内容指纹**(`func_decl.h` / `data_decl.h` / 各 `_arginfo.h`) | |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1116-L1160) · [src/Translator.php](src/Translator.php#L1207-L1240) |
||||||
|
|
||||||
|
## 缓存键构造 |
||||||
|
|
||||||
|
```php |
||||||
|
hash('sha256', |
||||||
|
buildCompileFileCommand(source, object) // 可复现的编译命令行 |
||||||
|
. "\0" . serialize($abi) // PHP ABI 快照 |
||||||
|
[. "\0" . header . "\0" . hash_file(header)] // 仅生成单元:逐头文件内容 |
||||||
|
) |
||||||
|
``` |
||||||
|
|
||||||
|
键写入 `<object>.typephp-cache` 元数据文件(`getMiscObjectCacheMetadataFile()`)。每次 `compileFile()` 先比较元数据文件中的键,再比较 `.o` 与源/头的 mtime——**任一不满足即失效并重编译**。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1162-L1187) · [src/Translator.php](src/Translator.php#L1272-L1292) |
||||||
|
|
||||||
|
## 设计要点 |
||||||
|
|
||||||
|
- **`extension-<target>.cc` 故意不进共享头依赖**:它的内容随任意类变化而改变,若纳入指纹会让每个 `.cc` 都失效,破坏增量构建。它自身作为生成单元拥有独立缓存项。 |
||||||
|
- **`--force` / `--profile` 强制失效**:这两类参数会跳过全部缓存,确保 misc 文件也被重编(profiler 需 `PPROF_ON` 宏生效)。 |
||||||
|
- **头文件依赖集排序后参与哈希**,避免路径顺序差异导致误失效。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1242-L1271) · [src/Translator.php](src/Translator.php#L1218-L1226) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 缓存何时被查询 → [构建流水线](build-pipeline) |
||||||
|
- 编译命令从何而来 → [编译器后端](backend) |
||||||
|
- 整体性能分析 → [构建速度与性能](build-speed) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,43 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>原生类型映射</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>原生类型映射</h1> |
||||||
|
<p>TypePHP 的核心性能来源之一:<strong>PHP 标量直接映射 C++ 原生类型</strong>,而不是一律装箱成 <code>zval</code>。所有可发射的 C++ 类型集中在 <code>Type</code> 类(<code>src/Type.php</code>),以字符串常量形式供生成器引用。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Type.php#L5-L27">src/Type.php</a> · <a href="file:///D:\git\php\aot-compiler\docs\NATIVE_TYPES.md">docs/NATIVE_TYPES.md</a></p> |
||||||
|
<h2>类型常量表</h2> |
||||||
|
<table><thead><tr><th>TypePHP 类型</th><th>C++ 映射</th><th>说明</th></tr></thead><tbody><tr><td><code>int</code></td><td><code>php::Int</code></td><td>zend_long,8 字节</td></tr><tr><td><code>float</code></td><td><code>php::Float</code></td><td>double,8 字节</td></tr><tr><td><code>bool</code></td><td><code>php::Bool</code></td><td>原生 bool</td></tr><tr><td><code>string</code></td><td><code>php::Str</code></td><td>字符串</td></tr><tr><td><code>array</code></td><td><code>php::Array</code></td><td>PHP 数组</td></tr><tr><td><code>object</code></td><td><code>php::Object</code></td><td>对象</td></tr><tr><td><code>mixed</code> / 动态</td><td><code>php::Var</code></td><td>通用 zval 盒子</td></tr><tr><td><code>resource</code> / <code>stream</code></td><td><code>php::Resource</code> / <code>php::Stream</code></td><td>资源</td></tr><tr><td><code>bigInt</code> / <code>decimal</code> / <code>bigFloat</code></td><td><code>php::BigInt</code> / <code>Decimal</code> / <code>php::BigFloat</code></td><td>高精度(见 <a href="pages/high-precision-types.html">high-precision-types</a>)</td></tr><tr><td><code>std</code> 容器</td><td><code>php::StdArray/StdVector/StdMap/StdOrderedMap</code></td><td>Std 容器</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Type.php#L7-L24">src/Type.php</a></p> |
||||||
|
<h2>固定值类型属性约束</h2> |
||||||
|
<p>AOT 要求<strong>对象属性在生命周期内保持声明类型</strong>。对 <code>int/float/bool/string/array</code> 这类固定值类型属性:</p> |
||||||
|
<ul><li><strong>不允许</strong> <code>unset()</code> 或赋值 <code>null</code>(等价于把类型改成 null);</li><li>需要可空时显式声明 <code>?int $id = null;</code>。</li></ul> |
||||||
|
<p>对象属性遵循静态类型规则:非空赋值需满足 <code>is-a</code> 继承关系,可用子类对象赋基类属性,反之不行。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\NATIVE_TYPES.md#L19-L51">docs/NATIVE_TYPES.md</a></p> |
||||||
|
<h2>使用前置</h2> |
||||||
|
<p>高精度/原生类型需在文件头声明 <code>use native_types</code>(并建议 <code>declare(strict_types=1)</code>),例如:</p> |
||||||
|
<pre><code><?php |
||||||
|
declare(strict_types=1); |
||||||
|
use native_types; |
||||||
|
function main(): void { /* ... */ }</code></pre> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\HIGH_PRECISION_TYPES.md#L51-L59">docs/HIGH_PRECISION_TYPES.md</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>类型如何被检查与兼容判定 → <a href="pages/type-check.html">类型检查与兼容</a></li><li>高精度类型的运算语义 → <a href="pages/high-precision-types.html">高精度数值类型</a></li><li>类型如何进入 C++ 生成 → <a href="pages/generator.html">生成器总览</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,55 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 原生类型映射 |
||||||
|
|
||||||
|
TypePHP 的核心性能来源之一:**PHP 标量直接映射 C++ 原生类型**,而不是一律装箱成 `zval`。所有可发射的 C++ 类型集中在 `Type` 类(`src/Type.php`),以字符串常量形式供生成器引用。 |
||||||
|
|
||||||
|
Sources: [src/Type.php](src/Type.php#L5-L27) · [docs/NATIVE_TYPES.md](docs/NATIVE_TYPES.md) |
||||||
|
|
||||||
|
## 类型常量表 |
||||||
|
|
||||||
|
| TypePHP 类型 | C++ 映射 | 说明 | |
||||||
|
|---|---|---| |
||||||
|
| `int` | `php::Int` | zend_long,8 字节 | |
||||||
|
| `float` | `php::Float` | double,8 字节 | |
||||||
|
| `bool` | `php::Bool` | 原生 bool | |
||||||
|
| `string` | `php::Str` | 字符串 | |
||||||
|
| `array` | `php::Array` | PHP 数组 | |
||||||
|
| `object` | `php::Object` | 对象 | |
||||||
|
| `mixed` / 动态 | `php::Var` | 通用 zval 盒子 | |
||||||
|
| `resource` / `stream` | `php::Resource` / `php::Stream` | 资源 | |
||||||
|
| `bigInt` / `decimal` / `bigFloat` | `php::BigInt` / `Decimal` / `php::BigFloat` | 高精度(见 [high-precision-types](high-precision-types)) | |
||||||
|
| `std` 容器 | `php::StdArray/StdVector/StdMap/StdOrderedMap` | Std 容器 | |
||||||
|
|
||||||
|
Sources: [src/Type.php](src/Type.php#L7-L24) |
||||||
|
|
||||||
|
## 固定值类型属性约束 |
||||||
|
|
||||||
|
AOT 要求**对象属性在生命周期内保持声明类型**。对 `int/float/bool/string/array` 这类固定值类型属性: |
||||||
|
- **不允许** `unset()` 或赋值 `null`(等价于把类型改成 null); |
||||||
|
- 需要可空时显式声明 `?int $id = null;`。 |
||||||
|
|
||||||
|
对象属性遵循静态类型规则:非空赋值需满足 `is-a` 继承关系,可用子类对象赋基类属性,反之不行。 |
||||||
|
|
||||||
|
Sources: [docs/NATIVE_TYPES.md](docs/NATIVE_TYPES.md#L19-L51) |
||||||
|
|
||||||
|
## 使用前置 |
||||||
|
|
||||||
|
高精度/原生类型需在文件头声明 `use native_types`(并建议 `declare(strict_types=1)`),例如: |
||||||
|
|
||||||
|
```php |
||||||
|
<?php |
||||||
|
declare(strict_types=1); |
||||||
|
use native_types; |
||||||
|
function main(): void { /* ... */ } |
||||||
|
``` |
||||||
|
|
||||||
|
Sources: [docs/HIGH_PRECISION_TYPES.md](docs/HIGH_PRECISION_TYPES.md#L51-L59) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 类型如何被检查与兼容判定 → [类型检查与兼容](type-check) |
||||||
|
- 高精度类型的运算语义 → [高精度数值类型](high-precision-types) |
||||||
|
- 类型如何进入 C++ 生成 → [生成器总览](generator) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,36 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>优化器</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>优化器</h1> |
||||||
|
<p>TypePHP 在语义分析阶段运行一组 <strong>SSA(静态单赋值)驱动</strong>的优化器,目标是减少运行时开销与生成的 C++ 体积。优化入口与 SSA 构建分别在 <code>src/Optimizer/</code> 与 <code>src/Analysis/SsaBuilder.php</code>。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Optimizer">src/Optimizer/</a> · <a href="file:///D:\git\php\aot-compiler\src\Analysis\SsaBuilder.php">src/Analysis/SsaBuilder.php</a></p> |
||||||
|
<h2>优化器清单</h2> |
||||||
|
<table><thead><tr><th>类</th><th>作用</th></tr></thead><tbody><tr><td><code>SsaBuilder</code></td><td>构建 SSA 形式,为下游优化提供定义-使用链</td></tr><tr><td><code>SsaTypeOptimizer</code></td><td>基于 SSA 的类型推导,传播确定类型以去除运行时 <code>php::Var</code> 装箱</td></tr><tr><td><code>SsaPropOptimizer</code></td><td>属性类型的 SSA 推导与去虚化</td></tr><tr><td><code>FuncCallOptimizer</code></td><td>函数调用优化(内联候选、已知函数特化等)</td></tr><tr><td><code>LoopVarOptimizer</code></td><td>循环变量类型/作用域优化</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Optimizer\SsaTypeOptimizer.php">src/Optimizer/SsaTypeOptimizer.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Optimizer\SsaPropOptimizer.php">src/Optimizer/SsaPropOptimizer.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Optimizer\FuncCallOptimizer.php">src/Optimizer/FuncCallOptimizer.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Optimizer\LoopVarOptimizer.php">src/Optimizer/LoopVarOptimizer.php</a></p> |
||||||
|
<h2>与优化级别的关系</h2> |
||||||
|
<p>命令行 <code>-O <level></code>(0–3)控制优化强度:级别越高,优化器介入越深、生成代码越紧凑,但编译期耗时也越大;<code>-d/--debug</code> 会关闭优化并加调试符号。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L234-L236">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L272-L274">src/Translator.php</a></p> |
||||||
|
<h2>与类型系统的协同</h2> |
||||||
|
<p>SSA 类型优化直接消费 <a href="pages/type-check.html">类型检查与兼容</a> 的判定结果——一旦推导确认某变量是固定原生类型,生成器即可发射 <code>php::Int</code> 而非通用 <code>php::Var</code>,省去 zval 装箱。这是「原生类型优先」原则在运行期的落地。</p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>类型如何判定 → <a href="pages/type-check.html">类型检查与兼容</a></li><li>编译期属性如何辅助优化 → <a href="pages/compile-time-attributes.html">编译期属性</a></li><li>整体速度瓶颈与改进 → <a href="pages/build-speed.html">构建速度与性能</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 优化器 |
||||||
|
|
||||||
|
TypePHP 在语义分析阶段运行一组 **SSA(静态单赋值)驱动**的优化器,目标是减少运行时开销与生成的 C++ 体积。优化入口与 SSA 构建分别在 `src/Optimizer/` 与 `src/Analysis/SsaBuilder.php`。 |
||||||
|
|
||||||
|
Sources: [src/Optimizer/](src/Optimizer/) · [src/Analysis/SsaBuilder.php](src/Analysis/SsaBuilder.php) |
||||||
|
|
||||||
|
## 优化器清单 |
||||||
|
|
||||||
|
| 类 | 作用 | |
||||||
|
|---|---| |
||||||
|
| `SsaBuilder` | 构建 SSA 形式,为下游优化提供定义-使用链 | |
||||||
|
| `SsaTypeOptimizer` | 基于 SSA 的类型推导,传播确定类型以去除运行时 `php::Var` 装箱 | |
||||||
|
| `SsaPropOptimizer` | 属性类型的 SSA 推导与去虚化 | |
||||||
|
| `FuncCallOptimizer` | 函数调用优化(内联候选、已知函数特化等) | |
||||||
|
| `LoopVarOptimizer` | 循环变量类型/作用域优化 | |
||||||
|
|
||||||
|
Sources: [src/Optimizer/SsaTypeOptimizer.php](src/Optimizer/SsaTypeOptimizer.php) · [src/Optimizer/SsaPropOptimizer.php](src/Optimizer/SsaPropOptimizer.php) · [src/Optimizer/FuncCallOptimizer.php](src/Optimizer/FuncCallOptimizer.php) · [src/Optimizer/LoopVarOptimizer.php](src/Optimizer/LoopVarOptimizer.php) |
||||||
|
|
||||||
|
## 与优化级别的关系 |
||||||
|
|
||||||
|
命令行 `-O <level>`(0–3)控制优化强度:级别越高,优化器介入越深、生成代码越紧凑,但编译期耗时也越大;`-d/--debug` 会关闭优化并加调试符号。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L234-L236) · [src/Translator.php](src/Translator.php#L272-L274) |
||||||
|
|
||||||
|
## 与类型系统的协同 |
||||||
|
|
||||||
|
SSA 类型优化直接消费 [类型检查与兼容](type-check) 的判定结果——一旦推导确认某变量是固定原生类型,生成器即可发射 `php::Int` 而非通用 `php::Var`,省去 zval 装箱。这是「原生类型优先」原则在运行期的落地。 |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 类型如何判定 → [类型检查与兼容](type-check) |
||||||
|
- 编译期属性如何辅助优化 → [编译期属性](compile-time-attributes) |
||||||
|
- 整体速度瓶颈与改进 → [构建速度与性能](build-speed) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>项目简介</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>项目简介</h1> |
||||||
|
<p><strong>TypePHP</strong> 是 Swoole 团队开发的 <strong>PHP 原生 AOT 编译器</strong>(仓库名 <code>aot-compiler</code>,Composer 包名 <code>swoole/typephp</code>)。它把 PHP 源码在编译期直接翻译成 <strong>C++</strong>,再借助 PHPX 运行时与 Zend 引擎嵌入(<code>embed</code>)能力,链接为 <strong>Windows / Linux / macOS</strong> 平台原生的可执行文件、共享库或 PHP 扩展。</p> |
||||||
|
<p>当前版本 <code>v0.4.3</code>(见 <code>src/Translator.php</code> 常量 <code>VERSION</code>)。编译器自身需要 <strong>PHP 8.4+</strong>;生成的产物可面向 <strong>PHP 8.2 ~ 8.5</strong> 的运行时语义。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\composer.json#L2-L4">composer.json</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L61-L62">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\README.md#L1-L6">README.md</a></p> |
||||||
|
<h2>为什么不是解释执行</h2> |
||||||
|
<p>传统 PHP 每次运行都要经过「词法 → 语法 → 字节码 → Zend 解释」四步。TypePHP 把前两步(以及大量语义分析)在编译期一次性完成,生成的 C++ 再被优化编译器(GCC/Clang/MSVC)进一步内联、逃逸分析与向量化,最终产出的二进制<strong>不再依赖 <code>php</code> 解释器即可运行</strong>——只依赖编译进来的 Zend 引擎(<code>libphp.so</code> / <code>php8ts.lib</code>)。</p> |
||||||
|
<blockquote>注意:预览版目前<strong>仅支持 Linux</strong>(建议 Ubuntu 22.04),Windows 编译需在具备 Windows SDK 与 PHP embed 库的环境下进行。</blockquote> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\README.md#L42-L43">README.md</a> · <a href="file:///D:\git\php\aot-compiler\composer.json#L8-L16">composer.json</a></p> |
||||||
|
<h2>设计原则</h2> |
||||||
|
<ul><li><strong>原生类型优先</strong>:标量(int/float/bool/string)直接映射 C++ 原生类型,避免 zval 装箱开销。</li><li><strong>PHP 语义对齐</strong>:类、接口、trait、枚举、反射、属性(attribute)、生成器(Generator)等尽量保持运行时行为。</li><li><strong>可增量构建</strong>:phpx misc 文件与编译器生成单元都带内容指纹缓存,改动一处不必全量重编译。</li></ul> |
||||||
|
<h2>阅读路线</h2> |
||||||
|
<ul><li>想了解「PHP 如何一步步变成二进制」→ <a href="pages/pipeline.html">编译流水线</a></li><li>想区分三种产物形态 → <a href="pages/compilation-modes.html">编译模式</a></li><li>想立刻跑通一个例子 → <a href="pages/quickstart.html">快速开始</a></li><li>想深入每个子系统 → 依次阅读 <a href="pages/parser.html">解析器与 AST</a> → <a href="pages/preprocessor.html">预处理与符号表</a> → <a href="pages/transform.html">AST 转换</a> → <a href="pages/generator.html">代码生成</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 项目简介 |
||||||
|
|
||||||
|
**TypePHP** 是 Swoole 团队开发的 **PHP 原生 AOT 编译器**(仓库名 `aot-compiler`,Composer 包名 `swoole/typephp`)。它把 PHP 源码在编译期直接翻译成 **C++**,再借助 PHPX 运行时与 Zend 引擎嵌入(`embed`)能力,链接为 **Windows / Linux / macOS** 平台原生的可执行文件、共享库或 PHP 扩展。 |
||||||
|
|
||||||
|
当前版本 `v0.4.3`(见 `src/Translator.php` 常量 `VERSION`)。编译器自身需要 **PHP 8.4+**;生成的产物可面向 **PHP 8.2 ~ 8.5** 的运行时语义。 |
||||||
|
|
||||||
|
Sources: [composer.json](composer.json#L2-L4) · [src/Translator.php](src/Translator.php#L61-L62) · [README.md](README.md#L1-L6) |
||||||
|
|
||||||
|
## 为什么不是解释执行 |
||||||
|
|
||||||
|
传统 PHP 每次运行都要经过「词法 → 语法 → 字节码 → Zend 解释」四步。TypePHP 把前两步(以及大量语义分析)在编译期一次性完成,生成的 C++ 再被优化编译器(GCC/Clang/MSVC)进一步内联、逃逸分析与向量化,最终产出的二进制**不再依赖 `php` 解释器即可运行**——只依赖编译进来的 Zend 引擎(`libphp.so` / `php8ts.lib`)。 |
||||||
|
|
||||||
|
> 注意:预览版目前**仅支持 Linux**(建议 Ubuntu 22.04),Windows 编译需在具备 Windows SDK 与 PHP embed 库的环境下进行。 |
||||||
|
|
||||||
|
Sources: [README.md](README.md#L42-L43) · [composer.json](composer.json#L8-L16) |
||||||
|
|
||||||
|
## 设计原则 |
||||||
|
|
||||||
|
- **原生类型优先**:标量(int/float/bool/string)直接映射 C++ 原生类型,避免 zval 装箱开销。 |
||||||
|
- **PHP 语义对齐**:类、接口、trait、枚举、反射、属性(attribute)、生成器(Generator)等尽量保持运行时行为。 |
||||||
|
- **可增量构建**:phpx misc 文件与编译器生成单元都带内容指纹缓存,改动一处不必全量重编译。 |
||||||
|
|
||||||
|
## 阅读路线 |
||||||
|
|
||||||
|
- 想了解「PHP 如何一步步变成二进制」→ [编译流水线](pipeline) |
||||||
|
- 想区分三种产物形态 → [编译模式](compilation-modes) |
||||||
|
- 想立刻跑通一个例子 → [快速开始](quickstart) |
||||||
|
- 想深入每个子系统 → 依次阅读 [解析器与 AST](parser) → [预处理与符号表](preprocessor) → [AST 转换](transform) → [代码生成](generator) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>解析器与 AST</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>解析器与 AST</h1> |
||||||
|
<p>TypePHP 复用 <strong>nikic/php-parser</strong> 作为前端词法/语法分析器(依赖固定版本 <code>5.6.1</code>)。解析得到的 PHP AST 是后续所有翻译与符号收集的输入。本章说明 AST 从何而来、以何种形态进入编译器。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\composer.json#L10">composer.json</a> · <a href="file:///D:\git\php\aot-compiler\project.yml#L34-L35">project.yml</a></p> |
||||||
|
<h2>AST 来源</h2> |
||||||
|
<p>解析发生在两个时机:</p> |
||||||
|
<ol><li><strong>预处理阶段</strong>:<code>Preprocessor::prepareFile()</code> 解析每个 PHP 文件,收集类/函数/常量/属性等符号,构建全局符号表(详见 <a href="pages/preprocessor.html">预处理与符号表</a>)。</li><li><strong>转换阶段</strong>:<code>doConvert()</code> 通过 <code>parseCachedAst()</code> 解析同一文件,得到供代码生成的 AST。</li></ol> |
||||||
|
<blockquote><code>parseCachedAst()</code> 做了两层缓存——同一文件的 AST 在进程内复用,避免重复解析;这也是编译性能优化(S1)的关键点之一。</blockquote> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L116">src/Preprocessor.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2459">src/Translator.php</a></p> |
||||||
|
<h2>表达式翻译:Parser Trait 集合</h2> |
||||||
|
<p>编译器主体 <code>CompilerBase</code> 并未把「PHP 表达式 → C++」的逻辑塞进一个巨型方法,而是拆成 <code>src/Parser/</code> 下的 <strong>21 个 trait</strong>,每个 trait 负责一类语法结构:</p> |
||||||
|
<table><thead><tr><th>Trait</th><th>负责的 PHP 结构</th></tr></thead><tbody><tr><td><code>BinaryOpTrait</code></td><td><code>+ - * /</code> 等二元运算</td></tr><tr><td><code>AssignOpTrait</code></td><td><code>+=</code>、<code>??=</code> 等复合赋值</td></tr><tr><td><code>UnaryExpressionTrait</code></td><td><code>!</code>、<code>-</code>、<code>++</code> 等一元运算</td></tr><tr><td><code>FunctionCallTrait</code></td><td>普通函数调用</td></tr><tr><td><code>MethodCallTrait</code> / <code>UniversalMethodCall</code></td><td>对象/静态方法调用</td></tr><tr><td><code>PropertyAccessTrait</code></td><td>属性读写</td></tr><tr><td><code>ArrayExpressionTrait</code></td><td>数组字面量</td></tr><tr><td><code>ForeachTrait</code> / <code>LoopControlTrait</code></td><td><code>foreach</code> / <code>for</code> / <code>while</code></td></tr><tr><td><code>SelectionExpressionTrait</code> / <code>SwitchTrait</code></td><td><code>if</code> / <code>switch</code></td></tr><tr><td><code>ConditionalControlTrait</code></td><td><code>?:</code> / <code>match</code></td></tr><tr><td><code>NullsafeAccessTrait</code></td><td><code>?-></code></td></tr><tr><td><code>ClassConstantFetchTrait</code></td><td>类常量读取</td></tr><tr><td><code>TypeConversionTrait</code> / <code>TypeDetectionTrait</code></td><td>类型转换与 <code>instanceof</code></td></tr><tr><td><code>ConstantExpressionTrait</code></td><td>编译期常量折叠</td></tr><tr><td><code>ExceptionControlFlowTrait</code></td><td><code>try/catch/throw</code></td></tr><tr><td><code>StdContainerTrait</code></td><td><code>std</code> 容器操作</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\CompilerBase.php#L43-L63">src/CompilerBase.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Parser">src/Parser/</a></p> |
||||||
|
<h2>访客链</h2> |
||||||
|
<p><code>doConvert()</code> 在遍历 AST 前先挂载四个 <code>NodeVisitor</code>:</p> |
||||||
|
<pre><code>$traverser->addVisitor(new NameResolver(...)); // 全限定名解析 |
||||||
|
$traverser->addVisitor(new Visitor(...)); // Transform lowering(见 transform) |
||||||
|
$traverser->addVisitor(new ConstantExpressionValidationVisitor(...)); |
||||||
|
$traverser->addVisitor(new RuntimeAttributeFactoryLowering(...));</code></pre> |
||||||
|
<p><code>NameResolver</code> 把短名解析为全限定名(但不替换节点),让后续生成能拿到准确的命名空间路径。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2460-L2467">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>符号如何被收集 → <a href="pages/preprocessor.html">预处理与符号表</a></li><li>遍历中做了哪些改写 → <a href="pages/transform.html">AST 转换</a></li><li>解析结果的消费方 → <a href="pages/generator.html">代码生成</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,66 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 解析器与 AST |
||||||
|
|
||||||
|
TypePHP 复用 **nikic/php-parser** 作为前端词法/语法分析器(依赖固定版本 `5.6.1`)。解析得到的 PHP AST 是后续所有翻译与符号收集的输入。本章说明 AST 从何而来、以何种形态进入编译器。 |
||||||
|
|
||||||
|
Sources: [composer.json](composer.json#L10) · [project.yml](project.yml#L34-L35) |
||||||
|
|
||||||
|
## AST 来源 |
||||||
|
|
||||||
|
解析发生在两个时机: |
||||||
|
|
||||||
|
1. **预处理阶段**:`Preprocessor::prepareFile()` 解析每个 PHP 文件,收集类/函数/常量/属性等符号,构建全局符号表(详见 [预处理与符号表](preprocessor))。 |
||||||
|
2. **转换阶段**:`doConvert()` 通过 `parseCachedAst()` 解析同一文件,得到供代码生成的 AST。 |
||||||
|
|
||||||
|
> `parseCachedAst()` 做了两层缓存——同一文件的 AST 在进程内复用,避免重复解析;这也是编译性能优化(S1)的关键点之一。 |
||||||
|
|
||||||
|
Sources: [src/Preprocessor.php](src/Preprocessor.php#L116) · [src/Translator.php](src/Translator.php#L2459) |
||||||
|
|
||||||
|
## 表达式翻译:Parser Trait 集合 |
||||||
|
|
||||||
|
编译器主体 `CompilerBase` 并未把「PHP 表达式 → C++」的逻辑塞进一个巨型方法,而是拆成 `src/Parser/` 下的 **21 个 trait**,每个 trait 负责一类语法结构: |
||||||
|
|
||||||
|
| Trait | 负责的 PHP 结构 | |
||||||
|
|---|---| |
||||||
|
| `BinaryOpTrait` | `+ - * /` 等二元运算 | |
||||||
|
| `AssignOpTrait` | `+=`、`??=` 等复合赋值 | |
||||||
|
| `UnaryExpressionTrait` | `!`、`-`、`++` 等一元运算 | |
||||||
|
| `FunctionCallTrait` | 普通函数调用 | |
||||||
|
| `MethodCallTrait` / `UniversalMethodCall` | 对象/静态方法调用 | |
||||||
|
| `PropertyAccessTrait` | 属性读写 | |
||||||
|
| `ArrayExpressionTrait` | 数组字面量 | |
||||||
|
| `ForeachTrait` / `LoopControlTrait` | `foreach` / `for` / `while` | |
||||||
|
| `SelectionExpressionTrait` / `SwitchTrait` | `if` / `switch` | |
||||||
|
| `ConditionalControlTrait` | `?:` / `match` | |
||||||
|
| `NullsafeAccessTrait` | `?->` | |
||||||
|
| `ClassConstantFetchTrait` | 类常量读取 | |
||||||
|
| `TypeConversionTrait` / `TypeDetectionTrait` | 类型转换与 `instanceof` | |
||||||
|
| `ConstantExpressionTrait` | 编译期常量折叠 | |
||||||
|
| `ExceptionControlFlowTrait` | `try/catch/throw` | |
||||||
|
| `StdContainerTrait` | `std` 容器操作 | |
||||||
|
|
||||||
|
Sources: [src/CompilerBase.php](src/CompilerBase.php#L43-L63) · [src/Parser/](src/Parser/) |
||||||
|
|
||||||
|
## 访客链 |
||||||
|
|
||||||
|
`doConvert()` 在遍历 AST 前先挂载四个 `NodeVisitor`: |
||||||
|
|
||||||
|
```php |
||||||
|
$traverser->addVisitor(new NameResolver(...)); // 全限定名解析 |
||||||
|
$traverser->addVisitor(new Visitor(...)); // Transform lowering(见 transform) |
||||||
|
$traverser->addVisitor(new ConstantExpressionValidationVisitor(...)); |
||||||
|
$traverser->addVisitor(new RuntimeAttributeFactoryLowering(...)); |
||||||
|
``` |
||||||
|
|
||||||
|
`NameResolver` 把短名解析为全限定名(但不替换节点),让后续生成能拿到准确的命名空间路径。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L2460-L2467) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 符号如何被收集 → [预处理与符号表](preprocessor) |
||||||
|
- 遍历中做了哪些改写 → [AST 转换](transform) |
||||||
|
- 解析结果的消费方 → [代码生成](generator) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,48 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>编译流水线</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>编译流水线</h1> |
||||||
|
<p>TypePHP 的核心入口是 <code>bin/tpc.php</code>,它通过 <code>main()</code> 串起整条流水线:<code>prepare → convert → compile → build → run</code>。本章用一张图和一个阶段表把全链路讲清楚。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\bin\tpc.php#L1-L9">bin/tpc.php</a> · <a href="file:///D:\git\php\aot-compiler\src\compiler.php#L4-L46">src/compiler.php</a></p> |
||||||
|
<h2>总体流程</h2> |
||||||
|
<pre class="mermaid">flowchart TD |
||||||
|
A["main(argc, argv)"] --> B["prepare(argv)\n扫描 PHP 文件 + 预处理"] |
||||||
|
B --> C["convert(files)\nPHP AST → C++ 源文件"] |
||||||
|
C --> D{"--dry?"} |
||||||
|
D -- "是" --> Z["仅输出 C++,结束"] |
||||||
|
D -- "否" --> E["compile(sourceFiles)\nC++ → .o / .obj"] |
||||||
|
E --> F["build(objectFiles)\n链接为二进制 / 库 / 扩展"] |
||||||
|
F --> G{"--run?"} |
||||||
|
G -- "是" --> H["run(binary)\n立即执行"] |
||||||
|
G -- "否" --> Z</pre> |
||||||
|
<p><code>.prof</code> 后缀会被切换为 <code>profileAnalyze</code> 模式,调用 <code>pprof --web</code> 做性能剖析。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\compiler.php#L14-L18">src/compiler.php</a> · <a href="file:///D:\git\php\aot-compiler\src\compiler.php#L26-L45">src/compiler.php</a></p> |
||||||
|
<h2>各阶段职责</h2> |
||||||
|
<table><thead><tr><th>阶段</th><th>方法</th><th>关键工作</th></tr></thead><tbody><tr><td>准备</td><td><code>Translator::prepare()</code></td><td>扫描所有 PHP 文件、解析命令行、构建符号表(类/函数/常量/属性)</td></tr><tr><td>转换</td><td><code>Translator::convert()</code> → <code>convertFile()</code> → <code>doConvert()</code></td><td>对每个 PHP 文件解析 AST,跑 Transform 访客,生成 <code>.cc</code> 与 <code>_arginfo.h</code></td></tr><tr><td>编译</td><td><code>Translator::compile()</code></td><td>追加 phpx misc 源文件、准备 PCH、单进程或 <code>pcntl</code> 并行编译</td></tr><tr><td>构建</td><td><code>Translator::build()</code></td><td>调用原生链接器把目标文件 + 资源文件链接成最终产物</td></tr><tr><td>运行</td><td><code>Translator::run()</code></td><td>仅在 <code>bin</code> 模式且带 <code>--run</code> 时执行产物</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L504-L527">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1419-L1447">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1649-L1680">src/Translator.php</a></p> |
||||||
|
<h2>转换阶段的两趟扫描</h2> |
||||||
|
<p><code>doConvert()</code> 内部做了两件事(<a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2455-L2537">src/Translator.php</a>):</p> |
||||||
|
<ol><li><strong>AST 遍历</strong>:用 <code>NodeTraverser</code> 串联 <code>NameResolver</code>、<code>Transform\Visitor</code>、<code>ConstantExpressionValidationVisitor</code>、<code>RuntimeAttributeFactoryLowering</code> 四个访客,做名称解析、编译期属性 lowering、常量表达式校验。</li><li><strong>语句分发</strong>:遍历顶层语句,按 <code>Stmt_Class</code> / <code>Stmt_Function</code> / <code>Stmt_Const</code> 等类型调用对应的 <code>parseXxx()</code>,最后为每个类、接口、函数生成 C++ 包装代码(<code>genClassWrapper</code> / <code>genFunctionWrapper</code>)。</li></ol> |
||||||
|
<blockquote>前端的解析/符号表逻辑在 <a href="pages/preprocessor.html">preprocessor</a> 与 <a href="pages/transform.html">transform</a> 章节展开;C++ 生成细节在 <a href="pages/generator.html">generator</a>。</blockquote> |
||||||
|
<h2>关键文件</h2> |
||||||
|
<ul><li><code>src/compiler.php</code> — 顶层 <code>main()</code> 与 <code>.prof</code> 剖析模式。</li><li><code>src/Translator.php</code> — 流水线编排 + 构建/链接/缓存。</li><li><code>src/Preprocessor.php</code> / <code>src/CompilerBase.php</code> — 解析、符号收集、C++ 代码发射(占 7000+ 行,是编译器主体)。</li></ul></div></body></html> |
||||||
@ -0,0 +1,55 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 编译流水线 |
||||||
|
|
||||||
|
TypePHP 的核心入口是 `bin/tpc.php`,它通过 `main()` 串起整条流水线:`prepare → convert → compile → build → run`。本章用一张图和一个阶段表把全链路讲清楚。 |
||||||
|
|
||||||
|
Sources: [bin/tpc.php](bin/tpc.php#L1-L9) · [src/compiler.php](src/compiler.php#L4-L46) |
||||||
|
|
||||||
|
## 总体流程 |
||||||
|
|
||||||
|
```mermaid |
||||||
|
flowchart TD |
||||||
|
A["main(argc, argv)"] --> B["prepare(argv)\n扫描 PHP 文件 + 预处理"] |
||||||
|
B --> C["convert(files)\nPHP AST → C++ 源文件"] |
||||||
|
C --> D{"--dry?"} |
||||||
|
D -- "是" --> Z["仅输出 C++,结束"] |
||||||
|
D -- "否" --> E["compile(sourceFiles)\nC++ → .o / .obj"] |
||||||
|
E --> F["build(objectFiles)\n链接为二进制 / 库 / 扩展"] |
||||||
|
F --> G{"--run?"} |
||||||
|
G -- "是" --> H["run(binary)\n立即执行"] |
||||||
|
G -- "否" --> Z |
||||||
|
``` |
||||||
|
|
||||||
|
`.prof` 后缀会被切换为 `profileAnalyze` 模式,调用 `pprof --web` 做性能剖析。 |
||||||
|
|
||||||
|
Sources: [src/compiler.php](src/compiler.php#L14-L18) · [src/compiler.php](src/compiler.php#L26-L45) |
||||||
|
|
||||||
|
## 各阶段职责 |
||||||
|
|
||||||
|
| 阶段 | 方法 | 关键工作 | |
||||||
|
|---|---|---| |
||||||
|
| 准备 | `Translator::prepare()` | 扫描所有 PHP 文件、解析命令行、构建符号表(类/函数/常量/属性) | |
||||||
|
| 转换 | `Translator::convert()` → `convertFile()` → `doConvert()` | 对每个 PHP 文件解析 AST,跑 Transform 访客,生成 `.cc` 与 `_arginfo.h` | |
||||||
|
| 编译 | `Translator::compile()` | 追加 phpx misc 源文件、准备 PCH、单进程或 `pcntl` 并行编译 | |
||||||
|
| 构建 | `Translator::build()` | 调用原生链接器把目标文件 + 资源文件链接成最终产物 | |
||||||
|
| 运行 | `Translator::run()` | 仅在 `bin` 模式且带 `--run` 时执行产物 | |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L504-L527) · [src/Translator.php](src/Translator.php#L1419-L1447) · [src/Translator.php](src/Translator.php#L1649-L1680) |
||||||
|
|
||||||
|
## 转换阶段的两趟扫描 |
||||||
|
|
||||||
|
`doConvert()` 内部做了两件事([src/Translator.php](src/Translator.php#L2455-L2537)): |
||||||
|
|
||||||
|
1. **AST 遍历**:用 `NodeTraverser` 串联 `NameResolver`、`Transform\Visitor`、`ConstantExpressionValidationVisitor`、`RuntimeAttributeFactoryLowering` 四个访客,做名称解析、编译期属性 lowering、常量表达式校验。 |
||||||
|
2. **语句分发**:遍历顶层语句,按 `Stmt_Class` / `Stmt_Function` / `Stmt_Const` 等类型调用对应的 `parseXxx()`,最后为每个类、接口、函数生成 C++ 包装代码(`genClassWrapper` / `genFunctionWrapper`)。 |
||||||
|
|
||||||
|
> 前端的解析/符号表逻辑在 [preprocessor](preprocessor) 与 [transform](transform) 章节展开;C++ 生成细节在 [generator](generator)。 |
||||||
|
|
||||||
|
## 关键文件 |
||||||
|
|
||||||
|
- `src/compiler.php` — 顶层 `main()` 与 `.prof` 剖析模式。 |
||||||
|
- `src/Translator.php` — 流水线编排 + 构建/链接/缓存。 |
||||||
|
- `src/Preprocessor.php` / `src/CompilerBase.php` — 解析、符号收集、C++ 代码发射(占 7000+ 行,是编译器主体)。 |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,41 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>跨平台支持</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>跨平台支持</h1> |
||||||
|
<p>TypePHP 目标 <strong>Windows / Linux / macOS</strong> 三大桌面平台,通过 <code>Platform</code> 抽象隔离系统差异(编译器名、目标扩展名、PHP embed 库查找等)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L164-L190">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Platform">src/Platform/</a></p> |
||||||
|
<h2>平台抽象</h2> |
||||||
|
<p><code>PlatformFactory::create()</code> 按 OS 返回具体 <code>Platform</code> 对象(Windows / Linux / macOS)。<code>detectPlatform()</code> 在 <code>Translator</code> 构造时调用,得到平台名 + 后端名(如 <code>Windows + MSVC</code>)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L164-L190">src/Translator.php</a></p> |
||||||
|
<h2>各平台要点</h2> |
||||||
|
<table><thead><tr><th>平台</th><th>默认后端</th><th>链接产物</th><th>特殊点</th></tr></thead><tbody><tr><td>Linux</td><td>GCC/Clang</td><td>ELF 可执行 / <code>.so</code></td><td>推荐 Ubuntu 22.04,需 <code>libphp.so</code></td></tr><tr><td>macOS</td><td>Clang</td><td>Mach-O / <code>.dylib</code></td><td><code>strip</code> 可选</td></tr><tr><td>Windows</td><td>MSVC</td><td>PE <code>.exe</code> / <code>.dll</code></td><td>需 Windows SDK + <code>php8ts.lib</code>;资源文件编译</td></tr></tbody></table> |
||||||
|
<p>Windows 分支会额外 <code>detectPhpLibs()</code> 探测 embed 与 core 的 <code>.lib</code>,并把 <code>isZts</code>、SDK 路径注入 <code>Windows</code> 对象。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L170-L181">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\README.md#L42-L43">README.md</a></p> |
||||||
|
<h2>Windows 资源文件</h2> |
||||||
|
<p><code>bin</code> 模式在 Windows 下编译 <code>.rc</code>(图标、版本信息、UAC 等,由 <code>project.yml</code> 的 <code>resource</code> 配置),生成的 <code>.res</code> 在 <code>build()</code> 链接时并入。资源文件生成见 <code>Generator/ResourceFileGenerator</code> 与 <code>Build/ResourceCompilationTrait</code>。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\project.yml#L10-L24">project.yml</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1653-L1659">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Build\ResourceCompilationTrait.php">src/Build/ResourceCompilationTrait.php</a></p> |
||||||
|
<h2>libphp.so 自动安装</h2> |
||||||
|
<p>Linux 缺 <code>libphp.so</code> 时,<code>tpc.php</code> 可交互式下载 PHP 源码并自动构建,详见 <code>docs/LIBPHP_INSTALLER.md</code>;安装器逻辑在 <code>src/Installer/</code>。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\README.md#L27">README.md</a> · <a href="file:///D:\git\php\aot-compiler\src\Installer">src/Installer/</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>后端命令如何生成 → <a href="pages/backend.html">编译器后端</a></li><li>链接与资源并入 → <a href="pages/build-pipeline.html">构建流水线</a></li><li>二进制打包(UPX/strip) → README 的 Release packaging 段</li></ul></div></body></html> |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 跨平台支持 |
||||||
|
|
||||||
|
TypePHP 目标 **Windows / Linux / macOS** 三大桌面平台,通过 `Platform` 抽象隔离系统差异(编译器名、目标扩展名、PHP embed 库查找等)。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L164-L190) · [src/Platform/](src/Platform/) |
||||||
|
|
||||||
|
## 平台抽象 |
||||||
|
|
||||||
|
`PlatformFactory::create()` 按 OS 返回具体 `Platform` 对象(Windows / Linux / macOS)。`detectPlatform()` 在 `Translator` 构造时调用,得到平台名 + 后端名(如 `Windows + MSVC`)。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L164-L190) |
||||||
|
|
||||||
|
## 各平台要点 |
||||||
|
|
||||||
|
| 平台 | 默认后端 | 链接产物 | 特殊点 | |
||||||
|
|---|---|---|---| |
||||||
|
| Linux | GCC/Clang | ELF 可执行 / `.so` | 推荐 Ubuntu 22.04,需 `libphp.so` | |
||||||
|
| macOS | Clang | Mach-O / `.dylib` | `strip` 可选 | |
||||||
|
| Windows | MSVC | PE `.exe` / `.dll` | 需 Windows SDK + `php8ts.lib`;资源文件编译 | |
||||||
|
|
||||||
|
Windows 分支会额外 `detectPhpLibs()` 探测 embed 与 core 的 `.lib`,并把 `isZts`、SDK 路径注入 `Windows` 对象。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L170-L181) · [README.md](README.md#L42-L43) |
||||||
|
|
||||||
|
## Windows 资源文件 |
||||||
|
|
||||||
|
`bin` 模式在 Windows 下编译 `.rc`(图标、版本信息、UAC 等,由 `project.yml` 的 `resource` 配置),生成的 `.res` 在 `build()` 链接时并入。资源文件生成见 `Generator/ResourceFileGenerator` 与 `Build/ResourceCompilationTrait`。 |
||||||
|
|
||||||
|
Sources: [project.yml](project.yml#L10-L24) · [src/Translator.php](src/Translator.php#L1653-L1659) · [src/Build/ResourceCompilationTrait.php](src/Build/ResourceCompilationTrait.php) |
||||||
|
|
||||||
|
## libphp.so 自动安装 |
||||||
|
|
||||||
|
Linux 缺 `libphp.so` 时,`tpc.php` 可交互式下载 PHP 源码并自动构建,详见 `docs/LIBPHP_INSTALLER.md`;安装器逻辑在 `src/Installer/`。 |
||||||
|
|
||||||
|
Sources: [README.md](README.md#L27) · [src/Installer/](src/Installer/) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 后端命令如何生成 → [编译器后端](backend) |
||||||
|
- 链接与资源并入 → [构建流水线](build-pipeline) |
||||||
|
- 二进制打包(UPX/strip) → README 的 Release packaging 段 |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>预处理与符号表</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>预处理与符号表</h1> |
||||||
|
<p>在生成任何 C++ 之前,编译器必须先<strong>通读所有 PHP 文件、收集符号、解决依赖顺序</strong>。这一步由 <code>Preprocessor</code>(父类)与 <code>CompilerBase</code>(主体)承担,产物是贯穿全程的全局符号表。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php">src/Preprocessor.php</a> · <a href="file:///D:\git\php\aot-compiler\src\CompilerBase.php#L97">src/CompilerBase.php</a></p> |
||||||
|
<h2>文件排序</h2> |
||||||
|
<p><code>prepare()</code> 扫描出 PHP 文件后,用 <code>getSortedFiles()</code> 做拓扑排序,保证被依赖的文件先被处理。<code>marcj/topsort</code> 负责依赖排序(Composer 依赖之一)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L43-L75">src/Preprocessor.php</a> · <a href="file:///D:\git\php\aot-compiler\composer.json#L13">composer.json</a></p> |
||||||
|
<h2>符号收集</h2> |
||||||
|
<p>每个文件经 <code>prepareFile()</code> 解析后,编译器为每个语言构造建立 <code>Entity</code> 定义对象:</p> |
||||||
|
<table><thead><tr><th>实体</th><th>定义类</th><th>收集方法</th></tr></thead><tbody><tr><td>函数</td><td><code>FunctionDef</code></td><td><code>prepareFunction()</code> / <code>parseFunctionDecl()</code></td></tr><tr><td>类 / trait / 枚举</td><td><code>ClassDef</code></td><td><code>prepareClass()</code></td></tr><tr><td>接口</td><td><code>InterfaceDef</code></td><td><code>prepareClass()</code></td></tr><tr><td>属性</td><td><code>PropertyDef</code></td><td><code>addClassProperty()</code></td></tr><tr><td>常量</td><td><code>ConstantDef</code></td><td><code>parseClassLikeConstant()</code> / <code>parseConstDef()</code></td></tr><tr><td>方法</td><td><code>MethodDef</code></td><td><code>parseFunctionDecl()</code></td></tr></tbody></table> |
||||||
|
<p><code>main()</code> 有专门校验:参数必须形如 <code>(int $argc, array $argv)</code> 且返回 <code>void</code>(仅 <code>bin</code> 模式强制要求)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L489-L589">src/Preprocessor.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L685">src/Preprocessor.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L1124">src/Preprocessor.php</a></p> |
||||||
|
<h2>符号仓库</h2> |
||||||
|
<p>收集到的符号统一存放在 <code>Symbol\SymbolRepository</code>(<code>$this->symbols</code>),按 functions / classes / interfaces 分别索引。<code>genExtension()</code>、各类 <code>genXxx</code> 方法都从仓库读取,保证「声明一次、多处生成」。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Symbol\SymbolRepository.php">src/Symbol/SymbolRepository.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L863-L880">src/Translator.php</a></p> |
||||||
|
<h2>属性默认值计划</h2> |
||||||
|
<p>标量/数组属性默认值会被预处理成 <code>ArrayInitPlan</code>,避免 C++ 初始化时重复求值;数组常量也会被标记 special 处理(见 <a href="pages/generator.html">generator</a> 的属性初始化)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L974-L1002">src/Preprocessor.php</a></p> |
||||||
|
<h2>数值与常量</h2> |
||||||
|
<p><code>loadInternalConstants()</code> 把 PHP 内置常量(排除用户态、<code>TypePHP</code> 扩展组)载入编译期常量表,供常量折叠使用。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L139-L159">src/Translator.php</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>解析器如何产出 AST → <a href="pages/parser.html">解析器与 AST</a></li><li>遍历中如何改写节点 → <a href="pages/transform.html">AST 转换</a></li><li>符号如何变成 C++ → <a href="pages/generator.html">代码生成</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,56 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 预处理与符号表 |
||||||
|
|
||||||
|
在生成任何 C++ 之前,编译器必须先**通读所有 PHP 文件、收集符号、解决依赖顺序**。这一步由 `Preprocessor`(父类)与 `CompilerBase`(主体)承担,产物是贯穿全程的全局符号表。 |
||||||
|
|
||||||
|
Sources: [src/Preprocessor.php](src/Preprocessor.php) · [src/CompilerBase.php](src/CompilerBase.php#L97) |
||||||
|
|
||||||
|
## 文件排序 |
||||||
|
|
||||||
|
`prepare()` 扫描出 PHP 文件后,用 `getSortedFiles()` 做拓扑排序,保证被依赖的文件先被处理。`marcj/topsort` 负责依赖排序(Composer 依赖之一)。 |
||||||
|
|
||||||
|
Sources: [src/Preprocessor.php](src/Preprocessor.php#L43-L75) · [composer.json](composer.json#L13) |
||||||
|
|
||||||
|
## 符号收集 |
||||||
|
|
||||||
|
每个文件经 `prepareFile()` 解析后,编译器为每个语言构造建立 `Entity` 定义对象: |
||||||
|
|
||||||
|
| 实体 | 定义类 | 收集方法 | |
||||||
|
|---|---|---| |
||||||
|
| 函数 | `FunctionDef` | `prepareFunction()` / `parseFunctionDecl()` | |
||||||
|
| 类 / trait / 枚举 | `ClassDef` | `prepareClass()` | |
||||||
|
| 接口 | `InterfaceDef` | `prepareClass()` | |
||||||
|
| 属性 | `PropertyDef` | `addClassProperty()` | |
||||||
|
| 常量 | `ConstantDef` | `parseClassLikeConstant()` / `parseConstDef()` | |
||||||
|
| 方法 | `MethodDef` | `parseFunctionDecl()` | |
||||||
|
|
||||||
|
`main()` 有专门校验:参数必须形如 `(int $argc, array $argv)` 且返回 `void`(仅 `bin` 模式强制要求)。 |
||||||
|
|
||||||
|
Sources: [src/Preprocessor.php](src/Preprocessor.php#L489-L589) · [src/Preprocessor.php](src/Preprocessor.php#L685) · [src/Preprocessor.php](src/Preprocessor.php#L1124) |
||||||
|
|
||||||
|
## 符号仓库 |
||||||
|
|
||||||
|
收集到的符号统一存放在 `Symbol\SymbolRepository`(`$this->symbols`),按 functions / classes / interfaces 分别索引。`genExtension()`、各类 `genXxx` 方法都从仓库读取,保证「声明一次、多处生成」。 |
||||||
|
|
||||||
|
Sources: [src/Symbol/SymbolRepository.php](src/Symbol/SymbolRepository.php) · [src/Translator.php](src/Translator.php#L863-L880) |
||||||
|
|
||||||
|
## 属性默认值计划 |
||||||
|
|
||||||
|
标量/数组属性默认值会被预处理成 `ArrayInitPlan`,避免 C++ 初始化时重复求值;数组常量也会被标记 special 处理(见 [generator](generator) 的属性初始化)。 |
||||||
|
|
||||||
|
Sources: [src/Preprocessor.php](src/Preprocessor.php#L974-L1002) |
||||||
|
|
||||||
|
## 数值与常量 |
||||||
|
|
||||||
|
`loadInternalConstants()` 把 PHP 内置常量(排除用户态、`TypePHP` 扩展组)载入编译期常量表,供常量折叠使用。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L139-L159) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 解析器如何产出 AST → [解析器与 AST](parser) |
||||||
|
- 遍历中如何改写节点 → [AST 转换](transform) |
||||||
|
- 符号如何变成 C++ → [代码生成](generator) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,53 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>快速开始</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>快速开始</h1> |
||||||
|
<p>本章给出一个最小可编译示例,以及常用命令。完整的用户手册在独立的 <code>aot/docs</code> 仓库,这里聚焦「能在 5 分钟内跑起来」。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\QUICKSTART.md">docs/QUICKSTART.md</a> · <a href="file:///D:\git\php\aot-compiler\README.md#L7-L26">README.md</a></p> |
||||||
|
<h2>安装依赖</h2> |
||||||
|
<pre><code>composer install # 安装 nikic/php-parser, phpx 等 |
||||||
|
php bin/tpc.php --help # 验证安装</code></pre> |
||||||
|
<p>Linux 还需高精度数学库:<code>sudo apt install libgmp-dev libmpfr-dev libmpdec-dev</code>。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\README.md#L29-L40">README.md</a></p> |
||||||
|
<h2>写一个最小程序</h2> |
||||||
|
<p>可执行代码必须放在 <code>main()</code> 里,类/函数/常量可在全局定义:</p> |
||||||
|
<pre><code><?php |
||||||
|
class Calculator { |
||||||
|
public function add(int $a, int $b): int { return $a + $b; } |
||||||
|
} |
||||||
|
function main(): void { |
||||||
|
$c = new Calculator(); |
||||||
|
echo $c->add(5, 3) . PHP_EOL; |
||||||
|
}</code></pre> |
||||||
|
<p>编译并运行:</p> |
||||||
|
<pre><code>php bin/tpc.php main.php -o myapp |
||||||
|
./myapp # 输出 8</code></pre> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\QUICKSTART.md#L166-L189">docs/QUICKSTART.md</a> · <a href="file:///D:\git\php\aot-compiler\docs\QUICKSTART.md#L99-L127">docs/QUICKSTART.md</a></p> |
||||||
|
<h2>常用命令</h2> |
||||||
|
<table><thead><tr><th>命令</th><th>作用</th></tr></thead><tbody><tr><td><code>php bin/tpc.php hello.php -O2</code></td><td>指定优化级别编译</td></tr><tr><td><code>php bin/tpc.php src/ -o app -j4</code></td><td>目录编译 + 4 路并行</td></tr><tr><td><code>php bin/tpc.php src/ --dry --build-dir /tmp/b</code></td><td>只生成 C++ 不编译(调试用)</td></tr><tr><td><code>php bin/tpc.php app.php -r -O2 -- --flag1 v1</code></td><td>编译后直接运行,并传参</td></tr><tr><td><code>php bin/tpc.php project.yml</code></td><td>用 <code>project.yml</code> 配置编译</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L225-L258">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\docs\QUICKSTART.md#L271-L280">docs/QUICKSTART.md</a></p> |
||||||
|
<h2>调试中间产物</h2> |
||||||
|
<p><code>--dry</code> 会把生成的 <code>.cc</code> / <code>_arginfo.h</code> 写入 <code>--build-dir</code> 指定的目录,便于检查翻译结果;<code>--no-progress</code> 则逐文件打印进度。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L30-L36">src/Translator.php</a></p> |
||||||
|
<h2>下一步</h2> |
||||||
|
<ul><li>了解产物形态差异 → <a href="pages/compilation-modes.html">编译模式</a></li><li>理解 <code>project.yml</code> 字段 → 见仓库根 <code>project.yml</code> 与 <code>docs/COMPILER_CLI.md</code></li><li>排查「Not implemented」→ <a href="pages/incompatible-features.html">不支持的 PHP 特性</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,68 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 快速开始 |
||||||
|
|
||||||
|
本章给出一个最小可编译示例,以及常用命令。完整的用户手册在独立的 `aot/docs` 仓库,这里聚焦「能在 5 分钟内跑起来」。 |
||||||
|
|
||||||
|
Sources: [docs/QUICKSTART.md](docs/QUICKSTART.md) · [README.md](README.md#L7-L26) |
||||||
|
|
||||||
|
## 安装依赖 |
||||||
|
|
||||||
|
```bash |
||||||
|
composer install # 安装 nikic/php-parser, phpx 等 |
||||||
|
php bin/tpc.php --help # 验证安装 |
||||||
|
``` |
||||||
|
|
||||||
|
Linux 还需高精度数学库:`sudo apt install libgmp-dev libmpfr-dev libmpdec-dev`。 |
||||||
|
|
||||||
|
Sources: [README.md](README.md#L29-L40) |
||||||
|
|
||||||
|
## 写一个最小程序 |
||||||
|
|
||||||
|
可执行代码必须放在 `main()` 里,类/函数/常量可在全局定义: |
||||||
|
|
||||||
|
```php |
||||||
|
<?php |
||||||
|
class Calculator { |
||||||
|
public function add(int $a, int $b): int { return $a + $b; } |
||||||
|
} |
||||||
|
function main(): void { |
||||||
|
$c = new Calculator(); |
||||||
|
echo $c->add(5, 3) . PHP_EOL; |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
编译并运行: |
||||||
|
|
||||||
|
```bash |
||||||
|
php bin/tpc.php main.php -o myapp |
||||||
|
./myapp # 输出 8 |
||||||
|
``` |
||||||
|
|
||||||
|
Sources: [docs/QUICKSTART.md](docs/QUICKSTART.md#L166-L189) · [docs/QUICKSTART.md](docs/QUICKSTART.md#L99-L127) |
||||||
|
|
||||||
|
## 常用命令 |
||||||
|
|
||||||
|
| 命令 | 作用 | |
||||||
|
|---|---| |
||||||
|
| `php bin/tpc.php hello.php -O2` | 指定优化级别编译 | |
||||||
|
| `php bin/tpc.php src/ -o app -j4` | 目录编译 + 4 路并行 | |
||||||
|
| `php bin/tpc.php src/ --dry --build-dir /tmp/b` | 只生成 C++ 不编译(调试用) | |
||||||
|
| `php bin/tpc.php app.php -r -O2 -- --flag1 v1` | 编译后直接运行,并传参 | |
||||||
|
| `php bin/tpc.php project.yml` | 用 `project.yml` 配置编译 | |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L225-L258) · [docs/QUICKSTART.md](docs/QUICKSTART.md#L271-L280) |
||||||
|
|
||||||
|
## 调试中间产物 |
||||||
|
|
||||||
|
`--dry` 会把生成的 `.cc` / `_arginfo.h` 写入 `--build-dir` 指定的目录,便于检查翻译结果;`--no-progress` 则逐文件打印进度。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L30-L36) |
||||||
|
|
||||||
|
## 下一步 |
||||||
|
|
||||||
|
- 了解产物形态差异 → [编译模式](compilation-modes) |
||||||
|
- 理解 `project.yml` 字段 → 见仓库根 `project.yml` 与 `docs/COMPILER_CLI.md` |
||||||
|
- 排查「Not implemented」→ [不支持的 PHP 特性](incompatible-features) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,44 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>PHPX 运行时集成</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>PHPX 运行时集成</h1> |
||||||
|
<p>TypePHP 不是从零实现 PHP 语义,而是站在 <strong>PHPX</strong>(Swoole 的 C++ PHP 绑定库)与 <strong>Zend 引擎 embed</strong> 之上。生成的 C++ 大量调用 <code>phpx.h</code> 提供的 <code>php::</code> 命名空间 API,最终把 Zend 引擎链接进产物。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L79-L89">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\README.md#L44-L61">README.md</a></p> |
||||||
|
<h2>全局头文件</h2> |
||||||
|
<p><code>Translator::$globalHeaders</code> 列出每个编译单元默认包含的头:</p> |
||||||
|
<pre><code>['cstring', 'phpx.h', 'phpx_helper.h', 'phpx_big_int.h', |
||||||
|
'phpx_big_float.h', 'phpx_decimal.h', 'typephp_helper.h', |
||||||
|
'typephp_fiber_generator.h', 'phpx_std.h']</code></pre> |
||||||
|
<p>高精度类型(<code>BigInt</code>/<code>BigFloat</code>/<code>Decimal</code>)的头正是此处引入;<code>typephp_*</code> 是 TypePHP 自带的扩展头(Fiber/Generator、helper)。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L79-L89">src/Translator.php</a></p> |
||||||
|
<h2>运行时依赖源</h2> |
||||||
|
<p><code>compile()</code> 会追加 phpx 的 <code>src/misc/</code> 源文件(C++ 运行时实现),以及 embed 模式下的 <code>typephp_main.cc</code>、bin 模式下的 cli 标题源。这些 misc 源受 <a href="pages/incremental-cache.html">增量编译缓存</a> 的 ABI 指纹保护。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L1423-L1434">src/Translator.php</a></p> |
||||||
|
<h2>关键运行时能力</h2> |
||||||
|
<ul><li><strong>Fiber / Generator</strong>:<code>typephp_fiber_generator.cc/.h</code> 提供协程与生成器支持,详见 <code>docs/YIELD_GENERATOR.md</code>;</li><li><strong>混合 C++/PHP</strong>:可在工程中直接写 C++ 并与 PHP 互调,见 <code>docs/MIXED_CPP_PHP.md</code>;</li><li><strong>反射</strong>:<code>typephp_install_reflection_attribute_handlers()</code> 在 MINIT 安装反射/属性处理器;</li><li><strong>类继承</strong>:PHP 类到 C++ 的映射与 <code>extends</code> 处理见 <code>docs/CLASS_INHERITANCE.md</code>。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L887-L890">src/Translator.php</a> · <a href="file:///D:\git\php\aot-compiler\docs\YIELD_GENERATOR.md">docs/YIELD_GENERATOR.md</a> · <a href="file:///D:\git\php\aot-compiler\docs\MIXED_CPP_PHP.md">docs/MIXED_CPP_PHP.md</a> · <a href="file:///D:\git\php\aot-compiler\docs\CLASS_INHERITANCE.md">docs/CLASS_INHERITANCE.md</a></p> |
||||||
|
<h2>动态链接库</h2> |
||||||
|
<p>运行时要求 <code>libphp.so</code>(Linux)/ <code>php8ts.lib</code>(Windows)与 <code>libphpx.so</code> 就位;缺库时可用 <code>docs/LIBPHP_INSTALLER.md</code> 自动构建。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\README.md#L57-L70">README.md</a> · <a href="file:///D:\git\php\aot-compiler\docs\LIBPHP_INSTALLER.md">docs/LIBPHP_INSTALLER.md</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>产物如何被链接 → <a href="pages/build-pipeline.html">构建流水线</a></li><li>跨平台库差异 → <a href="pages/platform.html">跨平台支持</a></li><li>哪些 PHP 特性尚不支持 → <a href="pages/incompatible-features.html">不支持的 PHP 特性</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,50 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# PHPX 运行时集成 |
||||||
|
|
||||||
|
TypePHP 不是从零实现 PHP 语义,而是站在 **PHPX**(Swoole 的 C++ PHP 绑定库)与 **Zend 引擎 embed** 之上。生成的 C++ 大量调用 `phpx.h` 提供的 `php::` 命名空间 API,最终把 Zend 引擎链接进产物。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L79-L89) · [README.md](README.md#L44-L61) |
||||||
|
|
||||||
|
## 全局头文件 |
||||||
|
|
||||||
|
`Translator::$globalHeaders` 列出每个编译单元默认包含的头: |
||||||
|
|
||||||
|
```php |
||||||
|
['cstring', 'phpx.h', 'phpx_helper.h', 'phpx_big_int.h', |
||||||
|
'phpx_big_float.h', 'phpx_decimal.h', 'typephp_helper.h', |
||||||
|
'typephp_fiber_generator.h', 'phpx_std.h'] |
||||||
|
``` |
||||||
|
|
||||||
|
高精度类型(`BigInt`/`BigFloat`/`Decimal`)的头正是此处引入;`typephp_*` 是 TypePHP 自带的扩展头(Fiber/Generator、helper)。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L79-L89) |
||||||
|
|
||||||
|
## 运行时依赖源 |
||||||
|
|
||||||
|
`compile()` 会追加 phpx 的 `src/misc/` 源文件(C++ 运行时实现),以及 embed 模式下的 `typephp_main.cc`、bin 模式下的 cli 标题源。这些 misc 源受 [增量编译缓存](incremental-cache) 的 ABI 指纹保护。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L1423-L1434) |
||||||
|
|
||||||
|
## 关键运行时能力 |
||||||
|
|
||||||
|
- **Fiber / Generator**:`typephp_fiber_generator.cc/.h` 提供协程与生成器支持,详见 `docs/YIELD_GENERATOR.md`; |
||||||
|
- **混合 C++/PHP**:可在工程中直接写 C++ 并与 PHP 互调,见 `docs/MIXED_CPP_PHP.md`; |
||||||
|
- **反射**:`typephp_install_reflection_attribute_handlers()` 在 MINIT 安装反射/属性处理器; |
||||||
|
- **类继承**:PHP 类到 C++ 的映射与 `extends` 处理见 `docs/CLASS_INHERITANCE.md`。 |
||||||
|
|
||||||
|
Sources: [src/Translator.php](src/Translator.php#L887-L890) · [docs/YIELD_GENERATOR.md](docs/YIELD_GENERATOR.md) · [docs/MIXED_CPP_PHP.md](docs/MIXED_CPP_PHP.md) · [docs/CLASS_INHERITANCE.md](docs/CLASS_INHERITANCE.md) |
||||||
|
|
||||||
|
## 动态链接库 |
||||||
|
|
||||||
|
运行时要求 `libphp.so`(Linux)/ `php8ts.lib`(Windows)与 `libphpx.so` 就位;缺库时可用 `docs/LIBPHP_INSTALLER.md` 自动构建。 |
||||||
|
|
||||||
|
Sources: [README.md](README.md#L57-L70) · [docs/LIBPHP_INSTALLER.md](docs/LIBPHP_INSTALLER.md) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 产物如何被链接 → [构建流水线](build-pipeline) |
||||||
|
- 跨平台库差异 → [跨平台支持](platform) |
||||||
|
- 哪些 PHP 特性尚不支持 → [不支持的 PHP 特性](incompatible-features) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>AST 转换(Lowering)</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>AST 转换(Lowering)</h1> |
||||||
|
<p>在 <code>doConvert()</code> 遍历 AST 时,<code>src/Transform/Visitor.php</code> 作为 <code>NodeVisitor</code> 挂载在遍历链上,对节点做<strong>编译期改写(lowering)</strong>——把 PHP 8.x 的高层语法(属性钩子、构造函数提升、getter、编译期属性等)降级为编译器能直接翻译的普通结构。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Transform\Visitor.php#L19-L36">src/Transform/Visitor.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2462">src/Translator.php</a></p> |
||||||
|
<h2>两个遍历钩子</h2> |
||||||
|
<p><code>Visitor</code> 实现 <code>enterNode()</code> 与 <code>leaveNode()</code> 两阶段处理:</p> |
||||||
|
<ul><li><strong>enterNode</strong>(进入节点前):校验 + 即时 lowering。依次调用 <code>CompileTimeAttribute::validateNode</code>、<code>FunctionAttributeLowering::lower</code>、<code>GetterLowering::validateTarget</code>、<code>PropertyMethodLowering::validateTarget</code>、<code>ConstructorLowering::validateTarget</code>。</li><li><strong>leaveNode</strong>(离开节点后):对函数/闭包做 <code>ParameterValidationLowering</code>;对类/属性做 <code>PropertyHookLowering</code>、<code>GetterLowering</code>、<code>PropertyMethodLowering</code>,把钩子属性、getter、提升属性改写成方法或标记。</li></ul> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Transform\Visitor.php#L28-L80">src/Transform/Visitor.php</a></p> |
||||||
|
<h2>Lowering 算子清单</h2> |
||||||
|
<table><thead><tr><th>类</th><th>作用</th></tr></thead><tbody><tr><td><code>CompileTimeAttribute</code> / <code>CompileTimeAttributeRegistry</code></td><td>识别并校验编译期属性(如 <code>Any</code>、<code>MustUse</code>)</td></tr><tr><td><code>FunctionAttributeLowering</code></td><td>处理函数上的编译期属性</td></tr><tr><td><code>ConstructorLowering</code></td><td>处理构造函数提升参数</td></tr><tr><td><code>GetterLowering</code></td><td><code>get</code> 钩子属性 → getter 方法</td></tr><tr><td><code>PropertyHookLowering</code></td><td><code>get</code>/<code>set</code> 钩子属性 → 访问器方法</td></tr><tr><td><code>PropertyMethodLowering</code></td><td>属性 ↔ 方法互转</td></tr><tr><td><code>ParameterValidationLowering</code></td><td>参数类型校验,拒绝箭头函数误用</td></tr><tr><td><code>PrinterLowering</code> / <code>ValidateLowering</code></td><td>打印/校验相关降级</td></tr><tr><td><code>ArrayableLowering</code> / <code>NotEmptyLowering</code> / <code>NotNullLowering</code></td><td>特定表达式降级</td></tr><tr><td><code>RuntimeAttributeFactoryLowering</code></td><td>运行时工厂属性(遍历链上独立挂载)</td></tr></tbody></table> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Transform">src/Transform/</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2467">src/Translator.php</a></p> |
||||||
|
<h2>顺序意义</h2> |
||||||
|
<p>Lowering 必须早于 C++ 生成:例如属性钩子若不先降级成方法,后续 <code>genClassWrapper</code> 就无法生成对应的 C++ 访问逻辑。它处在 <a href="pages/parser.html">解析器与 AST</a> 之后、<a href="pages/generator.html">代码生成</a> 之前。</p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>编译期属性语义 → <a href="pages/compile-time-attributes.html">编译期属性</a></li><li>降级后如何发射 C++ → <a href="pages/generator.html">生成器总览</a></li><li>常量表达式校验 → <a href="pages/type-check.html">类型检查与兼容</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# AST 转换(Lowering) |
||||||
|
|
||||||
|
在 `doConvert()` 遍历 AST 时,`src/Transform/Visitor.php` 作为 `NodeVisitor` 挂载在遍历链上,对节点做**编译期改写(lowering)**——把 PHP 8.x 的高层语法(属性钩子、构造函数提升、getter、编译期属性等)降级为编译器能直接翻译的普通结构。 |
||||||
|
|
||||||
|
Sources: [src/Transform/Visitor.php](src/Transform/Visitor.php#L19-L36) · [src/Translator.php](src/Translator.php#L2462) |
||||||
|
|
||||||
|
## 两个遍历钩子 |
||||||
|
|
||||||
|
`Visitor` 实现 `enterNode()` 与 `leaveNode()` 两阶段处理: |
||||||
|
|
||||||
|
- **enterNode**(进入节点前):校验 + 即时 lowering。依次调用 `CompileTimeAttribute::validateNode`、`FunctionAttributeLowering::lower`、`GetterLowering::validateTarget`、`PropertyMethodLowering::validateTarget`、`ConstructorLowering::validateTarget`。 |
||||||
|
- **leaveNode**(离开节点后):对函数/闭包做 `ParameterValidationLowering`;对类/属性做 `PropertyHookLowering`、`GetterLowering`、`PropertyMethodLowering`,把钩子属性、getter、提升属性改写成方法或标记。 |
||||||
|
|
||||||
|
Sources: [src/Transform/Visitor.php](src/Transform/Visitor.php#L28-L80) |
||||||
|
|
||||||
|
## Lowering 算子清单 |
||||||
|
|
||||||
|
| 类 | 作用 | |
||||||
|
|---|---| |
||||||
|
| `CompileTimeAttribute` / `CompileTimeAttributeRegistry` | 识别并校验编译期属性(如 `Any`、`MustUse`) | |
||||||
|
| `FunctionAttributeLowering` | 处理函数上的编译期属性 | |
||||||
|
| `ConstructorLowering` | 处理构造函数提升参数 | |
||||||
|
| `GetterLowering` | `get` 钩子属性 → getter 方法 | |
||||||
|
| `PropertyHookLowering` | `get`/`set` 钩子属性 → 访问器方法 | |
||||||
|
| `PropertyMethodLowering` | 属性 ↔ 方法互转 | |
||||||
|
| `ParameterValidationLowering` | 参数类型校验,拒绝箭头函数误用 | |
||||||
|
| `PrinterLowering` / `ValidateLowering` | 打印/校验相关降级 | |
||||||
|
| `ArrayableLowering` / `NotEmptyLowering` / `NotNullLowering` | 特定表达式降级 | |
||||||
|
| `RuntimeAttributeFactoryLowering` | 运行时工厂属性(遍历链上独立挂载) | |
||||||
|
|
||||||
|
Sources: [src/Transform/](src/Transform/) · [src/Translator.php](src/Translator.php#L2467) |
||||||
|
|
||||||
|
## 顺序意义 |
||||||
|
|
||||||
|
Lowering 必须早于 C++ 生成:例如属性钩子若不先降级成方法,后续 `genClassWrapper` 就无法生成对应的 C++ 访问逻辑。它处在 [解析器与 AST](parser) 之后、[代码生成](generator) 之前。 |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 编译期属性语义 → [编译期属性](compile-time-attributes) |
||||||
|
- 降级后如何发射 C++ → [生成器总览](generator) |
||||||
|
- 常量表达式校验 → [类型检查与兼容](type-check) |
||||||
|
|
||||||
|
</blog> |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
<!doctype html><html lang="zh"><head><meta charset="utf-8"> |
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1"> |
||||||
|
<title>类型检查与兼容</title> |
||||||
|
<script src="../script/mermaid.min.js"></script> |
||||||
|
<script>document.addEventListener('DOMContentLoaded',function(){if(window.mermaid)mermaid.initialize({startOnLoad:true});});</script> |
||||||
|
<style> |
||||||
|
:root{--bg:#0f1117;--fg:#e6e6e6;--muted:#9aa0aa;--accent:#6ea8fe;--card:#171a21;--border:#262b36;} |
||||||
|
*{box-sizing:border-box} |
||||||
|
body{margin:0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:var(--bg);color:var(--fg);line-height:1.65} |
||||||
|
.wrap{max-width:900px;margin:0 auto;padding:32px 24px 80px} |
||||||
|
h1,h2,h3{color:#fff;line-height:1.3;margin-top:1.6em} |
||||||
|
h1{border-bottom:1px solid var(--border);padding-bottom:.3em} |
||||||
|
a{color:var(--accent);text-decoration:none} |
||||||
|
a:hover{text-decoration:underline} |
||||||
|
code{background:#0b0d12;padding:.15em .4em;border-radius:4px;font-size:.9em} |
||||||
|
pre{background:#0b0d12;border:1px solid var(--border);border-radius:8px;padding:14px;overflow:auto} |
||||||
|
pre code{background:none;padding:0} |
||||||
|
table{border-collapse:collapse;width:100%;margin:1em 0} |
||||||
|
th,td{border:1px solid var(--border);padding:8px 10px;text-align:left} |
||||||
|
th{background:var(--card)} |
||||||
|
blockquote{border-left:3px solid var(--accent);margin:1em 0;padding:.2em 1em;color:var(--muted);background:var(--card)} |
||||||
|
hr{border:none;border-top:1px solid var(--border);margin:2em 0} |
||||||
|
</style></head> |
||||||
|
<body><div class="wrap"><h1>类型检查与兼容</h1> |
||||||
|
<p>类型检查负责回答两个问题:<strong>「这个值在此处是什么类型?」</strong> 与 <strong>「把 A 赋给期望 B 的槽位是否合法?」</strong>。TypePHP 的类型逻辑集中在 <code>src/TypeSystem/</code> 两个 trait 中,并在遍历/生成阶段被大量调用。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\TypeSystem">src/TypeSystem/</a></p> |
||||||
|
<h2>两个核心 Trait</h2> |
||||||
|
<table><thead><tr><th>Trait</th><th>职责</th></tr></thead><tbody><tr><td><code>NativeTypeCompatibilityTrait</code></td><td>判断原生类型之间的赋值/参数兼容(含子类→基类、可空、协变等规则)</td></tr><tr><td><code>CompositeTypeCheckerTrait</code></td><td>处理联合类型、<code>mixed</code>、交集等复合类型的成员判定</td></tr></tbody></table> |
||||||
|
<p>它们被 <code>CompilerBase</code> 通过 <code>use</code> 引入(与 Parser 各 trait 并列),为 <code>parseParams</code>、<code>genArgumentDeclaration</code> 等生成逻辑提供判定依据。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\CompilerBase.php#L79-L80">src/CompilerBase.php</a></p> |
||||||
|
<h2>检查发生的时机</h2> |
||||||
|
<ol><li><strong>参数解析</strong>:<code>parseParameterType()</code> 决定形参的 C++ 类型;不兼容的默认/传入会在此报错。</li><li><strong>返回值</strong>:函数/方法返回类型在 <code>parseFunctionDecl()</code> 校验,未声明返回类型会 <code>fatalError</code>(要求显式声明)。</li><li><strong>属性赋值</strong>:固定值类型属性赋值受 <a href="pages/native-types.html">native-types</a> 的静态类型规则约束。</li><li><strong>常量表达式</strong>:<code>ConstantExpressionValidationVisitor</code> 在遍历链上校验编译期常量表达式的合法性。</li></ol> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L328-L368">src/Preprocessor.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Preprocessor.php#L497-L536">src/Preprocessor.php</a> · <a href="file:///D:\git\php\aot-compiler\src\Translator.php#L2463-L2466">src/Translator.php</a></p> |
||||||
|
<h2>类型丢失与恢复</h2> |
||||||
|
<p>从数组/函数返回值取出对象时会丢失类型上下文,需用编译期函数 <code>objval($var, 'ClassName')</code> 显式恢复类信息;<code>refval()</code> / <code>any()</code> 等见 <a href="pages/compile-time-attributes.html">编译期属性</a>。</p> |
||||||
|
<p>Sources: <a href="file:///D:\git\php\aot-compiler\docs\NATIVE_TYPES.md#L55-L70">docs/NATIVE_TYPES.md</a></p> |
||||||
|
<h2>相关阅读</h2> |
||||||
|
<ul><li>类型如何映射到 C++ → <a href="pages/native-types.html">原生类型映射</a></li><li>编译期属性如何参与类型推断 → <a href="pages/compile-time-attributes.html">编译期属性</a></li><li>生成时如何消费类型 → <a href="pages/generator.html">生成器总览</a></li></ul></div></body></html> |
||||||
@ -0,0 +1,41 @@ |
|||||||
|
<blog> |
||||||
|
|
||||||
|
# 类型检查与兼容 |
||||||
|
|
||||||
|
类型检查负责回答两个问题:**「这个值在此处是什么类型?」** 与 **「把 A 赋给期望 B 的槽位是否合法?」**。TypePHP 的类型逻辑集中在 `src/TypeSystem/` 两个 trait 中,并在遍历/生成阶段被大量调用。 |
||||||
|
|
||||||
|
Sources: [src/TypeSystem/](src/TypeSystem/) |
||||||
|
|
||||||
|
## 两个核心 Trait |
||||||
|
|
||||||
|
| Trait | 职责 | |
||||||
|
|---|---| |
||||||
|
| `NativeTypeCompatibilityTrait` | 判断原生类型之间的赋值/参数兼容(含子类→基类、可空、协变等规则) | |
||||||
|
| `CompositeTypeCheckerTrait` | 处理联合类型、`mixed`、交集等复合类型的成员判定 | |
||||||
|
|
||||||
|
它们被 `CompilerBase` 通过 `use` 引入(与 Parser 各 trait 并列),为 `parseParams`、`genArgumentDeclaration` 等生成逻辑提供判定依据。 |
||||||
|
|
||||||
|
Sources: [src/CompilerBase.php](src/CompilerBase.php#L79-L80) |
||||||
|
|
||||||
|
## 检查发生的时机 |
||||||
|
|
||||||
|
1. **参数解析**:`parseParameterType()` 决定形参的 C++ 类型;不兼容的默认/传入会在此报错。 |
||||||
|
2. **返回值**:函数/方法返回类型在 `parseFunctionDecl()` 校验,未声明返回类型会 `fatalError`(要求显式声明)。 |
||||||
|
3. **属性赋值**:固定值类型属性赋值受 [native-types](native-types) 的静态类型规则约束。 |
||||||
|
4. **常量表达式**:`ConstantExpressionValidationVisitor` 在遍历链上校验编译期常量表达式的合法性。 |
||||||
|
|
||||||
|
Sources: [src/Preprocessor.php](src/Preprocessor.php#L328-L368) · [src/Preprocessor.php](src/Preprocessor.php#L497-L536) · [src/Translator.php](src/Translator.php#L2463-L2466) |
||||||
|
|
||||||
|
## 类型丢失与恢复 |
||||||
|
|
||||||
|
从数组/函数返回值取出对象时会丢失类型上下文,需用编译期函数 `objval($var, 'ClassName')` 显式恢复类信息;`refval()` / `any()` 等见 [编译期属性](compile-time-attributes)。 |
||||||
|
|
||||||
|
Sources: [docs/NATIVE_TYPES.md](docs/NATIVE_TYPES.md#L55-L70) |
||||||
|
|
||||||
|
## 相关阅读 |
||||||
|
|
||||||
|
- 类型如何映射到 C++ → [原生类型映射](native-types) |
||||||
|
- 编译期属性如何参与类型推断 → [编译期属性](compile-time-attributes) |
||||||
|
- 生成时如何消费类型 → [生成器总览](generator) |
||||||
|
|
||||||
|
</blog> |
||||||
File diff suppressed because one or more lines are too long
@ -0,0 +1,274 @@ |
|||||||
|
# Code Reuse Improvement Plan |
||||||
|
|
||||||
|
## Analysis Summary |
||||||
|
|
||||||
|
| Metric | Value | |
||||||
|
|--------|-------| |
||||||
|
| Total source lines | ~11,000 (PHP only) | |
||||||
|
| CompilerBase | 5,917 lines, 269 methods, 20 traits | |
||||||
|
| Gcc↔Clang duplication | ~70-80% of methods | |
||||||
|
| Linux↔Macos duplication | ~80% of methods | |
||||||
|
| `fatalError()` call sites | 174+ across codebase | |
||||||
|
| Test setUp/tearDown dup | 4+ test classes | |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Phase 1: High-Impact Backend/Platform Deduplication (P0) |
||||||
|
|
||||||
|
### 1.1 Extract `UnixPlatform` base class |
||||||
|
|
||||||
|
**Files**: `Platform/Linux.php` (249 lines), `Platform/Macos.php` (278 lines) |
||||||
|
|
||||||
|
These 13 methods are 100% identical between Linux and Macos: |
||||||
|
- `getIncludeFlags()`, `getLibraryPathFlags()`, `getObjectExtension()`, `getExecutableExtension()`, `getPathSeparator()`, `getPhpDir()`, `getRpathOptions()`, `getPicFlag()`, `buildPhpIncludePaths()`, `findPhpConfig()`, `buildPhpLibPaths()` |
||||||
|
|
||||||
|
Near-identical with minor parameterization: |
||||||
|
- `getLibraryFlags()` — only the regex differs (`.a|.so` vs `.a|.dylib`) |
||||||
|
- `detectPhpLibs()` — only the lib name differs (`libphp.so` vs `libphp.dylib`) |
||||||
|
|
||||||
|
**Plan**: Create `UnixPlatform extends PlatformBase` between `PlatformBase` and `Linux`/`Macos`. Move all identical methods up. Add abstract `getSharedLibraryExtension()` (already exists) and a protected `getSharedLibName()` for the single differing method. |
||||||
|
|
||||||
|
**Expected savings**: ~180 lines removed, ~150 lines added = net ~30 lines but massive maintainability gain. |
||||||
|
|
||||||
|
### 1.2 Extract `GccLikeBackend` base class |
||||||
|
|
||||||
|
**Files**: `Backend/Gcc.php` (379 lines), `Backend/Clang.php` (515 lines) |
||||||
|
|
||||||
|
These methods are structurally identical with only Windows-specific branching: |
||||||
|
- `compileFile()`, `linkObjects()`, `buildCompileCommand()`, `buildCCompileCommand()`, `buildNativeCompileCommand()`, `buildLinkCommand()`, `buildCompileOptions()`, `buildLinkOptions()`, `buildFullCompileOptions()`, `buildFullLinkOptions()` |
||||||
|
|
||||||
|
**Plan**: Create `GccLikeBackend extends CompilerBackend` with all shared logic. Define template-method hooks for the differences: |
||||||
|
- `getCompilerSpecificFlags()` — empty for Gcc, MSVC compat flags for Clang/Windows |
||||||
|
- `getOutputFlag($isWindows)` — `-o` vs `/OUT:` |
||||||
|
- `getSanitizerFlag($type)` — handle the `address`/`addr` aliasing difference |
||||||
|
- `getPICHandling($config)` — Gcc always adds `-fPIC`, Clang skips on Windows |
||||||
|
|
||||||
|
**Expected savings**: ~250+ lines removed from Gcc.php and Clang.php. Msvc.php is sufficiently different (different flag syntax) to remain standalone. |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Phase 2: CompilerBase Internal Deduplication (P1) |
||||||
|
|
||||||
|
### 2.1 Consolidate Big* type dispatch in BinaryOpTrait |
||||||
|
|
||||||
|
**File**: `Parser/BinaryOpTrait.php` (lines 31-107) |
||||||
|
|
||||||
|
The three blocks for BigFloat (lines 31-54), Decimal (lines 56-76), and BigInt (lines 78-107) in `parseBinaryOp()` share identical structure: |
||||||
|
1. Check if either operand is the big type |
||||||
|
2. Guard against incompatible mixing |
||||||
|
3. Convert the non-matching operand |
||||||
|
4. Dispatch to arithmetic or comparison operator |
||||||
|
|
||||||
|
**Plan**: Extract `parseBigNumBinaryOp(string $type, string $left, string $right, ...)` parameterized by type name, conversion function, and operator maps. Same refactoring applies to `genBigNumericCmp()` (lines 315-355). |
||||||
|
|
||||||
|
**Expected savings**: ~40 lines. |
||||||
|
|
||||||
|
### 2.2 Data-driven operator dispatch tables |
||||||
|
|
||||||
|
**Files**: `Parser/BinaryOpTrait.php` (16 wrapper methods), `Parser/AssignOpTrait.php` (14 wrapper methods) |
||||||
|
|
||||||
|
30+ thin methods that are just `parseBinaryOp($left, $right, '+')` / `parseAssignOp($node, '+=')`. |
||||||
|
|
||||||
|
**Plan**: Replace with a static map in `parseExpr()`: |
||||||
|
```php |
||||||
|
private const BINARY_OP_MAP = [ |
||||||
|
'Expr_BinaryOp_Plus' => '+', |
||||||
|
'Expr_BinaryOp_Minus' => '-', |
||||||
|
// ... |
||||||
|
]; |
||||||
|
private const ASSIGN_OP_MAP = [ |
||||||
|
'Expr_AssignOp_Plus' => '+=', |
||||||
|
// ... |
||||||
|
]; |
||||||
|
``` |
||||||
|
|
||||||
|
**Expected savings**: ~200 lines removed (boilerplate method bodies). |
||||||
|
|
||||||
|
### 2.3 Deduplicate call dispatch patterns |
||||||
|
|
||||||
|
**Files**: `CompilerBase.php` (`parseFuncCall`, `parseMethodCall`, `parseStaticCall` — ~300 lines combined), `UniversalMethodCall.php` (`tryOptimizePhpFn` vs `dispatchFuncCall`) |
||||||
|
|
||||||
|
These share the same overall flow: resolve callable → try native/optimized path → on `PlaceHolder` fall back to placeholder → parse args → wrap in `php::call()`. Additionally, `tryOptimizePhpFn()` (UniversalMethodCall lines 720-772) duplicates the argument type conversion logic already present in `dispatchFuncCall()` (FuncCallOptimizer lines 234-269). |
||||||
|
|
||||||
|
**Plan**: Extract a shared `resolveCall(CallLike $expr, ...)` method. Unify arg conversion so `tryOptimizePhpFn` delegates to `dispatchFuncCall` instead of reimplementing it. |
||||||
|
|
||||||
|
**Expected savings**: ~40 lines, fixes double-calculation of arg conversions. |
||||||
|
|
||||||
|
### 2.4 Deduplicate UNIVERSAL_METHODS math entries |
||||||
|
|
||||||
|
**File**: `UniversalMethodCall.php` (lines 12-69) |
||||||
|
|
||||||
|
The INT block (lines 12-41) and FLOAT block (lines 42-69) contain 20 identical math method entries (`abs`, `ceil`, `floor`, `sqrt`, `sin`, `cos`, etc.) differing only in `return_type`. Also the `calc_op` entries (add/sub/mul/div) are duplicated. |
||||||
|
|
||||||
|
**Plan**: Define math method names once in a shared array, generate both INT and FLOAT entries in the constructor with the appropriate `return_type`. |
||||||
|
|
||||||
|
**Expected savings**: ~25 lines of config data. |
||||||
|
|
||||||
|
### 2.5 Deduplicate constant folding methods |
||||||
|
|
||||||
|
**File**: `Optimizer/FuncCallOptimizer.php` (lines 515-593) |
||||||
|
|
||||||
|
8 methods (`doFoldStringLen`, `doFoldStringCase`, `doFoldCmp2`, `doFoldCmp3`, `doFoldCountLiteral`, `doFoldKnownClass`, `doFoldKnownConstant`, `doFoldSsaType`) follow the identical pattern: extract args → check types → compute → return literal or false. |
||||||
|
|
||||||
|
**Plan**: Create a generic `tryFold(callable $check, callable $compute)` that handles the arg extraction and short-circuit boilerplate. Each folder becomes a one-liner. |
||||||
|
|
||||||
|
**Expected savings**: ~50 lines. |
||||||
|
|
||||||
|
### 2.6 Remove MSVC compat flag duplication in Clang |
||||||
|
|
||||||
|
**File**: `Backend/Clang.php` |
||||||
|
|
||||||
|
The 4-line MSVC compatibility block (`-fms-compatibility`, `-fms-compatibility-version=19.40`, `-fdelayed-template-parsing`, `-fms-extensions`) appears 7 times (compileFile, buildCompileCommand, buildCCompileCommand, buildNativeCompileCommand, buildFullCompileOptions, buildCompileOptions, buildLinkOptions). |
||||||
|
|
||||||
|
**Plan**: Extract `private function getMsvcCompatFlags(): string` method. Called once per method that needs it instead of repeated inline. |
||||||
|
|
||||||
|
**Expected savings**: ~24 lines, single point of change if MSVC compat flags need updating. |
||||||
|
|
||||||
|
### 2.7 Merge return-check blocks |
||||||
|
|
||||||
|
**File**: `CompilerBase.php`, `parseReturn()` (line 1621) and `genReturnCode()` (line 5741) |
||||||
|
|
||||||
|
Identical 7-line union type check blocks. |
||||||
|
|
||||||
|
**Plan**: Extract `genUnionReturnWrapper(string $exprVar)` method. |
||||||
|
|
||||||
|
**Expected savings**: ~10 lines, eliminates drift risk. |
||||||
|
|
||||||
|
### 2.8 Fix `buildCCompileCommand()` inconsistency between Gcc and Clang |
||||||
|
|
||||||
|
**Files**: `Backend/Gcc.php` (lines 131-137), `Backend/Clang.php` (lines 198-206) |
||||||
|
|
||||||
|
Gcc unconditionally appends `-O$level` then conditionally appends `-g`. Clang treats debug and optimization as mutually exclusive (`if debug: -O0 -g` else `-O$level`). This is a behavioral inconsistency between backends implementing the same abstract method. |
||||||
|
|
||||||
|
**Plan**: Standardize on one behavior (the Clang pattern of `-O0 -g` for debug is the correct one — debug builds should not optimize). This will be automatically resolved by Phase 1.2 (GccLikeBackend). |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Phase 3: Structural Improvements (P2) |
||||||
|
|
||||||
|
### 3.1 Entity flag-check consistency |
||||||
|
|
||||||
|
**File**: `Entity/PropertyDef.php` has `isPrivate()`, `isProtected()`, `isPublic()`, `isStatic()`. `Entity/MethodDef.php` has none — flag checks are done inline in CompilerBase. |
||||||
|
|
||||||
|
**Plan**: Add a `HasFlags` trait used by both `PropertyDef` and `MethodDef`: |
||||||
|
```php |
||||||
|
trait HasFlags { |
||||||
|
public function isPrivate(): bool { return $this->flags & Modifiers::PRIVATE; } |
||||||
|
public function isProtected(): bool { return $this->flags & Modifiers::PROTECTED; } |
||||||
|
public function isPublic(): bool { return !$this->isPrivate() && !$this->isProtected(); } |
||||||
|
public function isStatic(): bool { return $this->flags & Modifiers::STATIC; } |
||||||
|
public function isAbstract(): bool { return $this->flags & Modifiers::ABSTRACT; } |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
**Expected savings**: Removes inline flag checks from CompilerBase, adds clarity. |
||||||
|
|
||||||
|
### 3.2 Test infrastructure base class |
||||||
|
|
||||||
|
**Files**: `phpunit/src/AstNodeTypeTest.php`, `CompilerBaseAdapterTest.php`, `TraitsTest.php`, `PreprocessorTest.php` |
||||||
|
|
||||||
|
All 4 duplicate the same setUp/tearDown pattern: create temp dir, `CompilerTest::create()`, recursive cleanup. |
||||||
|
|
||||||
|
**Plan**: Add `CompilerTestCase extends \PHPUnit\Framework\TestCase` to `phpunit/bootstrap.php`: |
||||||
|
```php |
||||||
|
abstract class CompilerTestCase extends TestCase { |
||||||
|
protected string $tmpDir; |
||||||
|
protected CompilerTest $compiler; |
||||||
|
|
||||||
|
protected function setUp(): void { |
||||||
|
parent::setUp(); |
||||||
|
$this->tmpDir = sys_get_temp_dir() . '/compiler_test_' . uniqid(); |
||||||
|
mkdir($this->tmpDir, 0777, true); |
||||||
|
$this->compiler = CompilerTest::create($this->tmpDir); |
||||||
|
} |
||||||
|
|
||||||
|
protected function tearDown(): void { |
||||||
|
parent::tearDown(); |
||||||
|
// recursive cleanup |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
### 3.3 Eliminate `buildFull*Options` / `build*Options` duality |
||||||
|
|
||||||
|
**Files**: `Backend/Gcc.php`, `Backend/Clang.php`, `Backend/Msvc.php` |
||||||
|
|
||||||
|
All three backends implement both `buildFullCompileOptions()` / `buildCompileOptions()` and `buildFullLinkOptions()` / `buildLinkOptions()`. The "full" variants are subsets of the "standard" variants working from differently-keyed option arrays. They have drifted independently (e.g., RPATH handling differs between the two in Gcc/Clang). |
||||||
|
|
||||||
|
**Plan**: Make the "full" variants delegate to the "standard" variants by normalizing their option keys once at the call site. Keep only one code path for each (compile/link). |
||||||
|
|
||||||
|
**Expected savings**: ~100+ lines, eliminates drift between the two variants. |
||||||
|
|
||||||
|
### 3.4 Deduplicate Preprocessor AST switch |
||||||
|
|
||||||
|
**File**: `Preprocessor.php` |
||||||
|
|
||||||
|
`prepareFile()` (lines 115-151) and `prepareNamespace()` (lines 196-223) both switch over the same set of AST `Stmt_*` types with nearly identical case bodies. |
||||||
|
|
||||||
|
**Plan**: Extract `processStmt(Node $v)` method that both callers share. |
||||||
|
|
||||||
|
**Expected savings**: ~25 lines. |
||||||
|
|
||||||
|
### 3.5 Remove dead code: `ScopeContext` |
||||||
|
|
||||||
|
**File**: `Context/ScopeContext.php` (7 lines) |
||||||
|
|
||||||
|
An empty class with no properties or methods. Used only as a type annotation in `FunctionContext`. Either populate it with scope-relevant state, or remove it and use plain `\stdClass` / array / null. |
||||||
|
|
||||||
|
### 3.6 Reduce StdContainerTrait coupling |
||||||
|
|
||||||
|
**File**: `Parser/StdContainerTrait.php` (823 lines, 48 methods) |
||||||
|
|
||||||
|
This is effectively a standalone subsystem for std container handling. As a trait, it has unrestricted access to CompilerBase's internals. |
||||||
|
|
||||||
|
**Plan**: Extract core logic into `StdContainerHandler` service class. The trait becomes a thin facade that delegates to the handler. |
||||||
|
|
||||||
|
**Expected savings**: Better testability, clearer boundaries, easier to understand. |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Phase 4: Longer-Term Architectural (P3) |
||||||
|
|
||||||
|
### 4.1 Break CompilerBase into domain-specific classes |
||||||
|
|
||||||
|
Currently CompilerBase is a 5,917-line god class using 20 traits as a workaround for PHP's single inheritance. Consider: |
||||||
|
|
||||||
|
- `ExpressionCompiler` — all parseExpr sub-dispatch (~500 lines) |
||||||
|
- `StatementCompiler` — parseStmts, parseIf, parseWhile, parseFor, parseSwitch, etc. |
||||||
|
- `TypeResolver` — parseTypeDecl, detectClassOfExpr, type checking |
||||||
|
- `CallResolver` — parseFuncCall, parseMethodCall, parseStaticCall, parseNew |
||||||
|
|
||||||
|
These would be injected services rather than traits, making CompilerBase a coordinator. |
||||||
|
|
||||||
|
### 4.2 Shared AST walker pattern with Python Translator |
||||||
|
|
||||||
|
Both PHP and Python translators implement the same "walk-collect-indent-emit" pipeline independently. `Core\Translator` could define a standard `walkAst($nodes, callable $visitor)` that handles indentation and line collection. |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Implementation Order & Impact Matrix |
||||||
|
|
||||||
|
| # | Item | Savings | Risk | Effort | |
||||||
|
|---|------|---------|------|--------| |
||||||
|
| 1.1 | UnixPlatform base class | ~180 dup lines | Low | 2-3h | |
||||||
|
| 1.2 | GccLikeBackend base class | ~250 dup lines | Medium | 3-4h | |
||||||
|
| 2.1 | BigNum dispatch consolidation | ~40 lines | Low | 1h | |
||||||
|
| 2.2 | Data-driven op dispatch | ~200 lines | Low | 1-2h | |
||||||
|
| 2.3 | Unify call dispatch patterns | ~40 lines | Low | 1-2h | |
||||||
|
| 2.4 | UNIVERSAL_METHODS math dedup | ~25 lines | Low | 30m | |
||||||
|
| 2.5 | Fold method template | ~50 lines | Low | 1h | |
||||||
|
| 2.6 | MSVC compat flags in Clang | ~24 lines | Low | 30m | |
||||||
|
| 2.7 | Merge return-check blocks | ~10 lines | Low | 30m | |
||||||
|
| 2.8 | Fix buildCCompileCommand drift | bug fix | Low | 30m | |
||||||
|
| 3.1 | HasFlags trait | clarity | Low | 1h | |
||||||
|
| 3.2 | CompilerTestCase base class | boilerplate | Low | 1h | |
||||||
|
| 3.3 | Eliminate Full*Options duality | ~100 lines | Medium | 2h | |
||||||
|
| 3.4 | Preprocessor AST switch dedup | ~25 lines | Low | 1h | |
||||||
|
| 3.5 | Remove dead ScopeContext | 7 lines | Low | 15m | |
||||||
|
| 3.6 | StdContainer service class | boundary | Medium | 3-4h | |
||||||
|
| 4.1 | Domain classes | architecture | High | 1-2 weeks | |
||||||
|
| 4.2 | AST walker pattern | architecture | Medium | 3-5h | |
||||||
|
|
||||||
|
**Total estimated savings**: ~950+ lines of duplicated / dead code. |
||||||
|
|
||||||
|
**Recommended execution**: Phase 1 → Phase 2 → Phase 3. Items within each phase are independent and can be parallelized. |
||||||
@ -0,0 +1,325 @@ |
|||||||
|
# Encapsulation Review |
||||||
|
|
||||||
|
## Summary |
||||||
|
|
||||||
|
| Metric | Value | |
||||||
|
|--------|-------| |
||||||
|
| Entity classes with all-public fields | 9/9 (100%) | |
||||||
|
| FunctionContext public properties | 26 (all mutable) | |
||||||
|
| CompilerBase private methods | 5/269 (1.9%) | |
||||||
|
| Traits with direct `$this->context->` access | 6 traits, 60+ sites | |
||||||
|
| ScopeContext (dead code) | 7 lines, empty class | |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Issue 1: Entity classes — all-public mutable fields |
||||||
|
|
||||||
|
**Severity**: High. Every entity class exposes all internal state as public writable properties. External code in Preprocessor/CompilerBase directly mutates them. |
||||||
|
|
||||||
|
### 1.1 ClassDef (18 public properties) |
||||||
|
|
||||||
|
`src/Php/Entity/ClassDef.php` |
||||||
|
|
||||||
|
```php |
||||||
|
public array $methods = []; // externally populated: $classDef->properties[$name] = ... |
||||||
|
public array $properties = []; // externally populated |
||||||
|
public array $constants = []; // externally populated |
||||||
|
public array $implements = []; // externally populated |
||||||
|
public string $extends = ''; // externally set: $this->classDef->extends = ... |
||||||
|
public bool $requireCtor = false; |
||||||
|
public bool $enum = false; |
||||||
|
public ?string $enumBackingType = null; |
||||||
|
public array $enumCases = []; |
||||||
|
public array $abstractMethods = []; |
||||||
|
public ?Trait_ $trait = null; |
||||||
|
public array $traitAliases = []; |
||||||
|
public array $traitIgnored = []; |
||||||
|
public int $flags; // no visibility checks, raw bitmask |
||||||
|
public bool $inheritedFromInternalClass = false; |
||||||
|
public string $ctorInit = ''; // mutated during code generation |
||||||
|
public string $ctorClean = ''; // mutated during code generation |
||||||
|
public FunctionContext $propertyContext; // set after construction |
||||||
|
``` |
||||||
|
|
||||||
|
**Issues**: |
||||||
|
- `$properties`, `$methods`, `$constants` — exposed as raw arrays. External code does `$classDef->properties[$name] = $propDef`. No validation that the key matches `$propDef->name`, no type enforcement. |
||||||
|
- `$flags` — raw int, no guarantee it's a valid Modifiers bitmask. |
||||||
|
- `$ctorInit` / `$ctorClean` — mutated by CompilerBase during code generation, not initialization. |
||||||
|
- Property additions use `addMethod()`, `addAbstractMethod()` but array properties are also set directly via `[] =`. |
||||||
|
- `$extends` — set directly as raw string, bypasses `parent::__construct()` which also sets it on ClassLikeDef. |
||||||
|
|
||||||
|
**Recommendation**: |
||||||
|
- Make `$methods`, `$properties`, `$constants` private, expose via `addMethod()`/`getMethod()` (already exists) |
||||||
|
- Make `$flags` private, expose `isAbstract()` (already exists), add `isFinal()`, `isReadonly()` |
||||||
|
- Make `$extends` write-once via `setExtends(string)` with validation |
||||||
|
- Add `appendCtorInit(string)` and `appendCtorClean(string)` methods instead of direct string mutation |
||||||
|
|
||||||
|
### 1.2 FunctionDef (12 public properties) |
||||||
|
|
||||||
|
`src/Php/Entity/FunctionDef.php` |
||||||
|
|
||||||
|
```php |
||||||
|
public string $name; |
||||||
|
public string $returnType; |
||||||
|
public array $argInfoList = []; // externally populated: $functionDef->argInfoList[] = $argInfo |
||||||
|
public int $argCountRequired = 0; |
||||||
|
public string $params = ''; // generated C++ param string, mutated during compilation |
||||||
|
public string $namespace; |
||||||
|
public bool $method = false; |
||||||
|
public bool $stub = false; |
||||||
|
public bool $returnTypeUndeclared = false; |
||||||
|
public string $returnClass = ''; |
||||||
|
public ?array $returnTypeCheck = null; |
||||||
|
public string $returnTypeStr = ''; |
||||||
|
public ?NodeAbstract $returnTypeNode = null; |
||||||
|
``` |
||||||
|
|
||||||
|
**Issues**: |
||||||
|
- `$name` and `$namespace` are set in constructor but still publicly writable — should be readonly |
||||||
|
- `$argInfoList[]` is directly appended to by Preprocessor (line 329) |
||||||
|
- `$params` is a codegen artifact stored on the entity — belongs in a separate compilation context |
||||||
|
|
||||||
|
**Recommendation**: |
||||||
|
- Make constructor-set properties readonly (`$name`, `$namespace`, `$returnType`) |
||||||
|
- Add `addArg(ArgInfo $arg)` method instead of direct array mutation |
||||||
|
- Extract `$params` to a compilation context separate from the definition entity |
||||||
|
|
||||||
|
### 1.3 PropertyDef (7 public properties) |
||||||
|
|
||||||
|
`src/Php/Entity/PropertyDef.php` |
||||||
|
|
||||||
|
```php |
||||||
|
public string $name; |
||||||
|
public string $type; |
||||||
|
public int $flags; |
||||||
|
public ?string $default = null; |
||||||
|
public ?ArrayInitPlan $arrayInitPlan = null; |
||||||
|
public bool $nullable = false; |
||||||
|
public string $class = ''; // set after construction |
||||||
|
``` |
||||||
|
|
||||||
|
**Issues**: |
||||||
|
- `$class` is set after construction externally (`$propDef->class = $fullClassName`) |
||||||
|
- `$flags` is raw int — already has `isPrivate()`/`isProtected()`/`isPublic()`/`isStatic()` methods, good |
||||||
|
- Constructor already sets all core fields — `$class` should be added to the constructor |
||||||
|
|
||||||
|
**Recommendation**: |
||||||
|
- Add `$class` to the constructor (it's always known at construction time) |
||||||
|
- Make constructor-set fields readonly or private |
||||||
|
|
||||||
|
### 1.4 MethodDef (4 public properties) |
||||||
|
|
||||||
|
`src/Php/Entity/MethodDef.php` |
||||||
|
|
||||||
|
```php |
||||||
|
public int $flags; |
||||||
|
public string $name; |
||||||
|
public ?FunctionDef $functionDef = null; // set after construction |
||||||
|
public bool $hasDynamicCall = false; |
||||||
|
``` |
||||||
|
|
||||||
|
**Issues**: |
||||||
|
- No flag-check methods — inline checks in CompilerBase should use `$methodDef->isPrivate()` instead |
||||||
|
- `$functionDef` is set externally: `$this->methodDef->functionDef = $functionDef` (Preprocessor line 411) |
||||||
|
|
||||||
|
**Recommendation**: |
||||||
|
- Add `HasFlags` trait (from Phase 3.1 of reuse plan) |
||||||
|
- Add `setFunctionDef(FunctionDef $fd)` method with validation |
||||||
|
|
||||||
|
### 1.5 ConstantDef (8 public properties) |
||||||
|
|
||||||
|
```php |
||||||
|
public string $name; |
||||||
|
public string $type; |
||||||
|
public int $flags; |
||||||
|
public string $value; |
||||||
|
public string $arrayExpr = ''; |
||||||
|
public string $class = ''; |
||||||
|
public ?NodeAbstract $valueExpr = null; |
||||||
|
``` |
||||||
|
|
||||||
|
**Issues**: Same pattern — constructor sets core fields, but `$class` is set externally afterward. |
||||||
|
|
||||||
|
**Recommendation**: Add `$class` to constructor. |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Issue 2: FunctionContext — public mutable grab-bag |
||||||
|
|
||||||
|
**Severity**: High. 26 public properties, all writable by any code with access to the context object. |
||||||
|
|
||||||
|
`src/Php/Context/FunctionContext.php` |
||||||
|
|
||||||
|
```php |
||||||
|
public ?SsaBuilder $ssaBuilder = null; // transient analysis state |
||||||
|
public array $stableObjects = []; // SSA optimizer state |
||||||
|
public array $hoistedProps = []; // SSA optimizer state |
||||||
|
public array $unsafeObjectProps = []; // SSA optimizer state |
||||||
|
public array $objects = []; // object variable tracking |
||||||
|
public array $stdArrays = []; // std container tracking |
||||||
|
public array $stdContainers = []; // std container tracking |
||||||
|
public array $localVars = []; // local variable table |
||||||
|
public array $staticVars = []; // static variable table |
||||||
|
public array $globalVars = []; // global variable table |
||||||
|
public array $ceWrappers = []; // class entry wrappers |
||||||
|
public int $tmpVarIndex = 0; // auto-increment counter |
||||||
|
public array $arguments = []; // function arguments |
||||||
|
public bool $inLoop = false; // control-flow state |
||||||
|
public bool $inClosure = false; // control-flow state |
||||||
|
public bool $hasMultiLevelBreak = false; |
||||||
|
public bool $hasMultiLevelContinue = false; |
||||||
|
public bool $inAssignExpr = false; // expression context |
||||||
|
public array $beforeStmtLines = []; // deferred code (flushed before stmts) |
||||||
|
public array $afterStmtLines = []; // deferred code (flushed after stmts) |
||||||
|
public array $objectProps; // (uninitialized!) |
||||||
|
public array $staticPropRefs = []; // static property references |
||||||
|
public int $scopeLevel = 0; // lexical scope depth |
||||||
|
/** @var array<int, ScopeContext> */ |
||||||
|
public array $scopeLayouts = []; // per-scope data |
||||||
|
``` |
||||||
|
|
||||||
|
**Issues**: |
||||||
|
- Traits directly mutate deeply nested state: `$this->context->stdArrays[$var] = ...`, `$this->context->localVars[$name] = ...` |
||||||
|
- No semantic grouping — analysis state, variable tracking, control-flow flags all mixed |
||||||
|
- `$objectProps` is declared but never initialized (could be null at runtime) |
||||||
|
- `$scopeLayouts` is managed through `enterScope()`/`leaveScope()` — but can be bypassed |
||||||
|
- `$tmpVarIndex` auto-increment — should use a method instead of direct `++` |
||||||
|
|
||||||
|
**Recommendation**: |
||||||
|
- Group related properties into sub-objects: `VariableTable`, `ControlFlowState`, `ScopeManager` |
||||||
|
- Make properties that should only be read by the compiler layer private/protected with getters |
||||||
|
- Add `incrementTmpVar(): int`, `addLocalVar()`, `addBeforeStmt()` methods |
||||||
|
- Initialize `$objectProps = []` |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Issue 3: CompilerBase — only 1.9% private methods |
||||||
|
|
||||||
|
**Severity**: Medium. Virtually everything is public or protected. |
||||||
|
|
||||||
|
`src/Php/CompilerBase.php` — 269 methods total: |
||||||
|
- ~25 public methods (many should be protected or internal) |
||||||
|
- ~239 protected methods (most should be private — internal helpers) |
||||||
|
- **5 private methods** (1.9%) |
||||||
|
|
||||||
|
### 3.1 Methods that should be private |
||||||
|
|
||||||
|
The following methods are internal helpers only called from within CompilerBase (not from Preprocessor, Translator, or traits). They are unnecessarily `protected`: |
||||||
|
|
||||||
|
| Method | Line | Called from | |
||||||
|
|--------|------|-------------| |
||||||
|
| `resetFunction()` | 833 | Internal only | |
||||||
|
| `resetMethod()` | 840 | Internal only | |
||||||
|
| `resetClass()` | 846 | Internal only | |
||||||
|
| `resolveObjectClassDef()` | 816 | Already private ✓ | |
||||||
|
| `getBigIntLiteralString()` | 1183 | Already private ✓ | |
||||||
|
| `getDecimalLiteralString()` | 1188 | Already private ✓ | |
||||||
|
| `parseBeforeStmtLines()` | 1321 | Internal, but accessed by traits | |
||||||
|
| `parseAfterStmtLines()` | 1331 | Internal, but accessed by traits | |
||||||
|
| `genTmpVarName()` | 734 | Public — should at least be protected | |
||||||
|
|
||||||
|
### 3.2 Public methods that are internal concern |
||||||
|
|
||||||
|
| Method | Current visibility | Issue | |
||||||
|
|--------|-------------------|-------| |
||||||
|
| `genTmpVarName()` | public | Only used internally for variable name generation | |
||||||
|
| `writeFile()` | public | File I/O — should be a separate service | |
||||||
|
| `stop()` | public | Error helper — could be internal | |
||||||
|
| `isScalarInt()` | public | AST helper, only used internally | |
||||||
|
| `getType()` | public | AST helper, only used internally | |
||||||
|
| `getObjectType()` | public | Type mapping, used internally | |
||||||
|
| `getTypeFromZendType()` | public | Type mapping, used internally | |
||||||
|
| `getIncludeDir()` | public | Config getter — should be on a Config object | |
||||||
|
| `getBuildDir()` | public | Config getter — should be on a Config object | |
||||||
|
|
||||||
|
### 3.3 Public constants leaked as API |
||||||
|
|
||||||
|
30 public constants for internal type names, literal values, etc. These are needed by traits but expose internal naming conventions. |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Issue 4: Trait → Context coupling |
||||||
|
|
||||||
|
**Severity**: Medium. Traits bypass any encapsulation boundary and directly mutate `$this->context`. |
||||||
|
|
||||||
|
| Trait | `$this->context->` accesses | |
||||||
|
|-------|---------------------------| |
||||||
|
| `StdContainerTrait` | 40+ accesses to `stdArrays`, `stdContainers`, `objects`, `localVars` | |
||||||
|
| `LoopVarOptimizer` | accesses to `localVars`, `arguments`, `scopeLevel`, `inLoop` | |
||||||
|
| `SsaPropOptimizer` | accesses to `stableObjects`, `hoistedProps`, `unsafeObjectProps`, `objects` | |
||||||
|
| `FuncCallOptimizer` | accesses to `beforeStmtLines`, `arguments`, `localVars` | |
||||||
|
| `SsaTypeOptimizer` | accesses to `localVars` | |
||||||
|
| `BinaryOpTrait` | accesses to `objects`, `localVars` | |
||||||
|
|
||||||
|
**Issues**: |
||||||
|
- Traits have no declared contract — they assume `$this->context` exists and has specific properties |
||||||
|
- If a property name changes in FunctionContext, all 6 traits break silently |
||||||
|
- No type safety — arrays are indexed by string but accessed with arbitrary keys |
||||||
|
|
||||||
|
**Recommendation**: |
||||||
|
- Define a `ContextAccess` interface that traits must use instead of direct property access |
||||||
|
- Or: inject context into trait methods as a parameter instead of reading from `$this` |
||||||
|
- Short-term: add `@property-read` annotations to document the contract |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Issue 5: Preprocessor directly mutates entity state |
||||||
|
|
||||||
|
**Severity**: Medium. Preprocessor bypasses entity boundaries. |
||||||
|
|
||||||
|
`src/Php/Preprocessor.php`: |
||||||
|
```php |
||||||
|
line 279: $argInfo->name = $name; // direct property set |
||||||
|
line 329: $functionDef->argInfoList[] = $argInfo; // direct array append |
||||||
|
line 411: $this->methodDef->functionDef = $functionDef; // direct property set |
||||||
|
line 447: $this->classDef->extends = $this->parentClass; // direct property set |
||||||
|
line 585: $this->classDef->constants[$constInfo->name] = ...; // direct array set |
||||||
|
line 618: $this->classDef->properties[$name] = $propDef; // direct array set |
||||||
|
``` |
||||||
|
|
||||||
|
**Recommendation**: Use entity methods: `$functionDef->addArg($argInfo)`, `$this->methodDef->setFunctionDef($functionDef)`, `$classDef->addProperty($propDef)`, etc. |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Issue 6: CompilerBase protected state leaked to inheritance chain |
||||||
|
|
||||||
|
**Severity**: Low-Medium. The chain CompilerBase → Preprocessor → Translator means any protected property in CompilerBase is accessible from Translator. |
||||||
|
|
||||||
|
CompilerBase has ~50 protected properties. Translator is 3301 lines and accesses many of them. There's no way to know which properties are "safe to use" vs "internal to CompilerBase." |
||||||
|
|
||||||
|
**Recommendation**: Migrate internal-only properties to `private` over time, with explicit getter methods where needed. |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Issue 7: Platform/Backend — well encapsulated |
||||||
|
|
||||||
|
**Severity**: None. The Platform and Backend layers are well-encapsulated: |
||||||
|
- All state is private (e.g., `$compilerCommand`, `$linkerCommand` in GccLikeBackend) |
||||||
|
- Only methods are public |
||||||
|
- Abstract contracts are clear |
||||||
|
- Factory pattern is used consistently |
||||||
|
|
||||||
|
**No changes needed in this layer.** |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Issue 8: ScopeContext is dead code |
||||||
|
|
||||||
|
**Severity**: Low. `src/Php/Context/ScopeContext.php` — 7 lines, empty class body. Used as a placeholder type in FunctionContext's `$scopeLayouts` array. Either populate it or remove it. |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Implementation Priority |
||||||
|
|
||||||
|
| # | Issue | Impact | Effort | Risk | |
||||||
|
|---|-------|--------|--------|------| |
||||||
|
| 1.1 | Entity: readonly for constructor fields | Data integrity | 2h | Low | |
||||||
|
| 1.2 | Entity: add mutation methods (addArg, addProperty, etc.) | Safe mutation | 3h | Medium | |
||||||
|
| 2.1 | FunctionContext: group properties into sub-objects | Clarity | 4h | Medium | |
||||||
|
| 2.2 | FunctionContext: add accessor methods | Controlled mutation | 3h | Medium | |
||||||
|
| 3 | CompilerBase: demote public→protected, protected→private | Boundary clarity | 4h | Medium | |
||||||
|
| 4 | Define trait context contract | Safe coupling | 3h | Medium | |
||||||
|
| 5 | Preprocessor: use entity methods | Consistent mutation | 2h | Low | |
||||||
|
| 8 | Remove ScopeContext dead code | Cleanup | 15m | None | |
||||||
|
|
||||||
|
**Recommended order**: Start with 8 (quick win), then 1.1 + 1.2 (entity cleanup), then 2.1 + 2.2 (context cleanup), then 3 + 5 + 4 (CompilerBase boundary). |
||||||
@ -1,21 +0,0 @@ |
|||||||
name: Patch PHP headers for C++ |
|
||||||
description: Apply temporary upstream PHP header fixes required by generated C++. |
|
||||||
|
|
||||||
runs: |
|
||||||
using: composite |
|
||||||
steps: |
|
||||||
# Temporary workaround for php/php-src#22935. Some supported PHP packages |
|
||||||
# contain a php_hash.h revision that is valid C but invalid C++. Remove |
|
||||||
# this action when all PHP 8.4/8.5 packages include php/php-src#22940. |
|
||||||
- name: Patch php_hash.h |
|
||||||
shell: bash |
|
||||||
run: | |
|
||||||
php_include_dir="$(php-config --include-dir)" |
|
||||||
php_hash_header="${php_include_dir}/ext/hash/php_hash.h" |
|
||||||
|
|
||||||
if grep -Fq 'char *base = ecalloc(' "${php_hash_header}"; then |
|
||||||
sudo patch --directory="${php_include_dir}" --strip=1 \ |
|
||||||
< "${GITHUB_ACTION_PATH}/../../patches/php-hash-cxx.patch" |
|
||||||
else |
|
||||||
echo "php_hash.h already contains the upstream C++ fix" |
|
||||||
fi |
|
||||||
@ -0,0 +1,90 @@ |
|||||||
|
# Copilot instructions for this repository |
||||||
|
|
||||||
|
## Project overview |
||||||
|
|
||||||
|
TypePHP is a PHP native compilation project. Its `tpc` command is TypePHP Compiler (AOT), which translates PHP source into C++, then compiles and links it into a native binary or a PHP extension. The primary entrypoint boots `src/compiler.php`; that drives `TypePhp\Translator` through a fixed pipeline: |
||||||
|
|
||||||
|
1. `prepare()` scans files, parses ASTs, collects symbols, and topologically sorts PHP files by cross-file symbol usage. |
||||||
|
2. `convert()` turns PHP ASTs into generated `.cc` files while passing through native source files (`.cpp`, `.c`, `.s`, `.m`, `.mm`). |
||||||
|
3. `compile()` chooses the platform/compiler backend, generates support sources and headers, and compiles sources, using `pcntl` parallelism when available. |
||||||
|
4. `build()` links object files into the final executable or extension. |
||||||
|
|
||||||
|
`src/CompilerBase.php` contains most PHP-to-C++ translation logic and mixes in many traits for syntax handling and optimizations. `src/Preprocessor.php` owns dependency discovery and file ordering. Platform-specific behavior lives under `src/Platform/`, compiler backends under `src/Backend/`, and metadata/state objects under `src/Entity/` and `src/Context/`. |
||||||
|
|
||||||
|
## Setup and build commands |
||||||
|
|
||||||
|
The repo expects PHP 8.4+, GCC 9+ with C++17, CMake 3.24+, and a compiled `swoole/phpx` dependency. Install PHP dependencies with: |
||||||
|
|
||||||
|
```bash |
||||||
|
composer install |
||||||
|
``` |
||||||
|
|
||||||
|
Build `phpx` before relying on compiler runs: |
||||||
|
|
||||||
|
```bash |
||||||
|
cd vendor/swoole/phpx |
||||||
|
cmake . |
||||||
|
make -j32 |
||||||
|
``` |
||||||
|
|
||||||
|
Compile a project, directory, single file, or `project.yml`: |
||||||
|
|
||||||
|
```bash |
||||||
|
./tpc <path-to-project-or-file> |
||||||
|
./tpc <path> -O2 |
||||||
|
./tpc <path> --mode=ext -o <output_name> |
||||||
|
``` |
||||||
|
|
||||||
|
## Test commands |
||||||
|
|
||||||
|
Run the PHPUnit suite: |
||||||
|
|
||||||
|
```bash |
||||||
|
./vendor/bin/phpunit |
||||||
|
``` |
||||||
|
|
||||||
|
Run a single PHPUnit file or a single test method: |
||||||
|
|
||||||
|
```bash |
||||||
|
./vendor/bin/phpunit phpunit/src/Platform/PlatformTest.php |
||||||
|
./vendor/bin/phpunit --filter testWindowsBasic phpunit/src/Platform/PlatformTest.php |
||||||
|
``` |
||||||
|
|
||||||
|
Run PHPT integration tests: |
||||||
|
|
||||||
|
```bash |
||||||
|
php run-tests.php tests/compiler/ |
||||||
|
php run-tests.php tests/compiler/arrays.phpt |
||||||
|
``` |
||||||
|
|
||||||
|
For parser/runtime comparison without AOT compilation, there are docs using: |
||||||
|
|
||||||
|
```bash |
||||||
|
php run-tests.php --no-aot tests/compiler/arrow-functions.phpt |
||||||
|
``` |
||||||
|
|
||||||
|
## Formatting |
||||||
|
|
||||||
|
The repo ships a PHP CS Fixer config in `.php-cs-fixer.dist.php`: |
||||||
|
|
||||||
|
```bash |
||||||
|
php vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php <path> |
||||||
|
``` |
||||||
|
|
||||||
|
Generated C++ is auto-formatted by the compiler itself when `clang-format` is available. |
||||||
|
|
||||||
|
## Configuration and repository conventions |
||||||
|
|
||||||
|
- `project.yml` is the project-level build config. Important keys include `name`, `build-mode`, `cxx-std`, `cxx-flags`, `ld-flags`, `sources`, `ignore`, and `resource`. |
||||||
|
- Command-line options intentionally override YAML values. `Translator` parses YAML first, then applies CLI arguments last. |
||||||
|
- YAML parsing accepts both hyphenated and underscored variants for several keys, but existing examples use hyphenated names such as `build-mode` and `cxx-std`. |
||||||
|
- In `bin` mode, compiled programs must define `main()`. In `ext` mode they do not. |
||||||
|
- File discovery is mixed-language by design: PHP is translated, while native sources are compiled directly if they appear in configured sources. |
||||||
|
- Generated files are written under `build/`, with generated C++ paths mirroring the source tree and generated headers under `build/include/`. |
||||||
|
- Platform/compiler selection is centralized: `PlatformFactory` detects the OS, and `CompilerFactory` picks the backend (`Gcc`, `Clang`, `Msvc`) with environment/config overrides. |
||||||
|
|
||||||
|
## Test-specific conventions |
||||||
|
|
||||||
|
- PHPUnit tests for compiler internals should use `CompilerTest::create(ROOT_PATH)`, which enables test mode instead of normal fatal exits. |
||||||
|
- `phpunit/bootstrap.php` exposes a `BaseTest::exec()` helper that expects compilation failures to surface as `TypePhp\Exception\TestError`. |
||||||
|
- PHPT end-to-end tests live in `tests/compiler/`; existing guidance and examples generally put executable test logic inside a `main()` function. |
||||||
@ -1,11 +0,0 @@ |
|||||||
--- a/ext/hash/php_hash.h
|
|
||||||
+++ b/ext/hash/php_hash.h
|
|
||||||
@@ -158,7 +158,7 @@ static inline void *php_hash_alloc_context(const php_hash_ops *ops) {
|
|
||||||
/* Zero out context memory so serialization doesn't expose internals */
|
|
||||||
if (ops->context_align > 0) {
|
|
||||||
size_t align = ops->context_align;
|
|
||||||
- char *base = ecalloc(1, ops->context_size + align);
|
|
||||||
+ char *base = (char *) ecalloc(1, ops->context_size + align);
|
|
||||||
size_t offset = align - ((uintptr_t)base & (align - 1));
|
|
||||||
char *ptr = base + offset;
|
|
||||||
ptr[-1] = (char)offset;
|
|
||||||
@ -1,285 +0,0 @@ |
|||||||
name: tests |
|
||||||
|
|
||||||
on: |
|
||||||
push: |
|
||||||
pull_request: |
|
||||||
workflow_dispatch: |
|
||||||
|
|
||||||
permissions: |
|
||||||
contents: read |
|
||||||
|
|
||||||
concurrency: |
|
||||||
group: tests-${{ github.workflow }}-${{ github.ref }} |
|
||||||
cancel-in-progress: true |
|
||||||
|
|
||||||
env: |
|
||||||
COMPOSER_NO_INTERACTION: 1 |
|
||||||
COMPOSER_PROCESS_TIMEOUT: 0 |
|
||||||
|
|
||||||
jobs: |
|
||||||
build-phpx: |
|
||||||
name: Build PHPX (PHP ${{ matrix.php }}) |
|
||||||
runs-on: ubuntu-latest |
|
||||||
timeout-minutes: 30 |
|
||||||
strategy: |
|
||||||
fail-fast: false |
|
||||||
matrix: |
|
||||||
php: ["8.4", "8.5"] |
|
||||||
env: |
|
||||||
PHPX_HOME: ${{ github.workspace }}/vendor/swoole/phpx |
|
||||||
|
|
||||||
steps: |
|
||||||
- name: Checkout TypePHP |
|
||||||
uses: actions/checkout@v4 |
|
||||||
|
|
||||||
- name: Checkout phpy |
|
||||||
uses: actions/checkout@v4 |
|
||||||
with: |
|
||||||
repository: swoole/phpy |
|
||||||
path: third_party/phpy |
|
||||||
|
|
||||||
- name: Setup PHP |
|
||||||
uses: shivammathur/setup-php@v2 |
|
||||||
with: |
|
||||||
php-version: ${{ matrix.php }} |
|
||||||
coverage: none |
|
||||||
ini-values: precision=17, memory_limit=4G, error_reporting=E_ERROR|E_WARNING, display_errors=1, display_startup_errors=1, log_errors=0 |
|
||||||
tools: composer:v2 |
|
||||||
|
|
||||||
- name: Patch php_hash.h C++ compatibility |
|
||||||
uses: ./.github/actions/patch-php-headers |
|
||||||
|
|
||||||
- name: Install native build dependencies |
|
||||||
run: | |
|
||||||
sudo apt-get update |
|
||||||
sudo apt-get install --yes build-essential cmake libgmp-dev libmpfr-dev pkg-config python3-dev |
|
||||||
|
|
||||||
- name: Install Composer dependencies |
|
||||||
run: composer install --prefer-dist --no-progress |
|
||||||
|
|
||||||
- name: Build PHPX |
|
||||||
shell: bash |
|
||||||
run: | |
|
||||||
cmake -S "${PHPX_HOME}" -B "${PHPX_HOME}/build" \ |
|
||||||
-D CMAKE_BUILD_TYPE=Release \ |
|
||||||
-D BUILD_TESTS=OFF \ |
|
||||||
-D BUILD_EXT=OFF \ |
|
||||||
-D GITHUB_ACTION=ON |
|
||||||
cmake --build "${PHPX_HOME}/build" --target phpx --parallel 2 |
|
||||||
test -f "${PHPX_HOME}/lib/libphpx.so" |
|
||||||
|
|
||||||
- name: Build phpy |
|
||||||
working-directory: third_party/phpy |
|
||||||
run: | |
|
||||||
phpize |
|
||||||
./configure |
|
||||||
make -j2 |
|
||||||
test -f modules/phpy.so |
|
||||||
|
|
||||||
- name: Upload PHPX library |
|
||||||
uses: actions/upload-artifact@v4 |
|
||||||
with: |
|
||||||
name: libphpx-php-${{ matrix.php }} |
|
||||||
if-no-files-found: error |
|
||||||
retention-days: 1 |
|
||||||
path: vendor/swoole/phpx/lib/libphpx.so |
|
||||||
|
|
||||||
- name: Upload phpy extension |
|
||||||
uses: actions/upload-artifact@v4 |
|
||||||
with: |
|
||||||
name: phpy-php-${{ matrix.php }} |
|
||||||
if-no-files-found: error |
|
||||||
retention-days: 1 |
|
||||||
path: third_party/phpy/modules/phpy.so |
|
||||||
|
|
||||||
phpunit: |
|
||||||
name: PHPUnit (PHP ${{ matrix.php }}) |
|
||||||
runs-on: ubuntu-latest |
|
||||||
timeout-minutes: 30 |
|
||||||
needs: build-phpx |
|
||||||
strategy: |
|
||||||
fail-fast: false |
|
||||||
matrix: |
|
||||||
php: ["8.4", "8.5"] |
|
||||||
env: |
|
||||||
PHPX_HOME: ${{ github.workspace }}/vendor/swoole/phpx |
|
||||||
|
|
||||||
steps: |
|
||||||
- name: Checkout TypePHP |
|
||||||
uses: actions/checkout@v4 |
|
||||||
|
|
||||||
- name: Setup PHP |
|
||||||
uses: shivammathur/setup-php@v2 |
|
||||||
with: |
|
||||||
php-version: ${{ matrix.php }} |
|
||||||
coverage: none |
|
||||||
extensions: curl, redis, mbstring, ffi |
|
||||||
ini-values: ffi.enable=1, phpy.enable_operator_overloading=0, precision=17, memory_limit=4G, error_reporting=E_ERROR|E_WARNING, display_errors=1, display_startup_errors=1, log_errors=0 |
|
||||||
tools: composer:v2 |
|
||||||
|
|
||||||
- name: Patch php_hash.h C++ compatibility |
|
||||||
uses: ./.github/actions/patch-php-headers |
|
||||||
|
|
||||||
- name: Install native build dependencies |
|
||||||
run: | |
|
||||||
sudo apt-get update |
|
||||||
sudo apt-get install --yes build-essential cmake libgmp-dev libmpfr-dev pkg-config python3-dev |
|
||||||
|
|
||||||
- name: Install Composer dependencies |
|
||||||
run: composer install --prefer-dist --no-progress |
|
||||||
|
|
||||||
- name: Download PHPX library |
|
||||||
uses: actions/download-artifact@v4 |
|
||||||
with: |
|
||||||
name: libphpx-php-${{ matrix.php }} |
|
||||||
path: vendor/swoole/phpx/lib |
|
||||||
|
|
||||||
- name: Download phpy extension |
|
||||||
uses: actions/download-artifact@v4 |
|
||||||
with: |
|
||||||
name: phpy-php-${{ matrix.php }} |
|
||||||
path: third_party/phpy/modules |
|
||||||
|
|
||||||
- name: Enable phpy extension |
|
||||||
shell: bash |
|
||||||
run: | |
|
||||||
php_ini_dir="$(php-config --ini-dir)" |
|
||||||
echo "extension=${GITHUB_WORKSPACE}/third_party/phpy/modules/phpy.so" \ |
|
||||||
| sudo tee "${php_ini_dir}/90-phpy.ini" |
|
||||||
echo "PHP_INI_SCAN_DIR=${php_ini_dir}" >> "${GITHUB_ENV}" |
|
||||||
php --ri phpy |
|
||||||
|
|
||||||
- name: Configure native library path |
|
||||||
shell: bash |
|
||||||
run: echo "LD_LIBRARY_PATH=${PHPX_HOME}/lib:$(php-config --prefix)/lib" >> "${GITHUB_ENV}" |
|
||||||
|
|
||||||
- name: Run PHPUnit |
|
||||||
run: vendor/bin/phpunit |
|
||||||
|
|
||||||
phpt: |
|
||||||
name: PHPT (PHP ${{ matrix.php }}) |
|
||||||
runs-on: ubuntu-latest |
|
||||||
timeout-minutes: 180 |
|
||||||
needs: build-phpx |
|
||||||
strategy: |
|
||||||
fail-fast: false |
|
||||||
matrix: |
|
||||||
php: ["8.4", "8.5"] |
|
||||||
env: |
|
||||||
PHPX_HOME: ${{ github.workspace }}/vendor/swoole/phpx |
|
||||||
NO_INTERACTION: 1 |
|
||||||
REPORT_EXIT_STATUS: 1 |
|
||||||
TYPEPHP_PHPT_GENERATED_ARTIFACT_DIR: ${{ github.workspace }}/build/phpt-generated |
|
||||||
|
|
||||||
steps: |
|
||||||
- name: Checkout TypePHP |
|
||||||
uses: actions/checkout@v4 |
|
||||||
|
|
||||||
- name: Setup PHP |
|
||||||
uses: shivammathur/setup-php@v2 |
|
||||||
with: |
|
||||||
php-version: ${{ matrix.php }} |
|
||||||
coverage: none |
|
||||||
extensions: embed, curl, redis, mbstring, ffi |
|
||||||
ini-values: ffi.enable=1, phpy.enable_operator_overloading=0, opcache.jit=0, precision=17, memory_limit=4G, error_reporting=E_ERROR|E_WARNING, display_errors=1, display_startup_errors=1, log_errors=0 |
|
||||||
tools: composer:v2 |
|
||||||
|
|
||||||
- name: Patch php_hash.h C++ compatibility |
|
||||||
uses: ./.github/actions/patch-php-headers |
|
||||||
|
|
||||||
- name: Install native build dependencies |
|
||||||
run: | |
|
||||||
sudo apt-get update |
|
||||||
sudo apt-get install --yes build-essential cmake libgmp-dev libmpfr-dev pkg-config python3-dev |
|
||||||
|
|
||||||
- name: Configure version-matched PHP embed library |
|
||||||
shell: bash |
|
||||||
run: | |
|
||||||
embed_package="libphp${{ matrix.php }}-embed" |
|
||||||
php_version="$(php-config --version)" |
|
||||||
embed_version="$(dpkg-query --show --showformat='${Version}' "${embed_package}")" |
|
||||||
case "${embed_version}" in |
|
||||||
"${php_version}"*) ;; |
|
||||||
*) echo "PHP embed package ${embed_version} does not match PHP ${php_version}" >&2; exit 1 ;; |
|
||||||
esac |
|
||||||
embed_library="$(dpkg-query --listfiles "${embed_package}" \ |
|
||||||
| sed -n '/\/libphp[0-9][^/]*\.so$/ { p; q; }')" |
|
||||||
test -n "${embed_library}" |
|
||||||
test -f "${embed_library}" |
|
||||||
|
|
||||||
php_home="${RUNNER_TEMP}/typephp-php-${{ matrix.php }}" |
|
||||||
mkdir -p "${php_home}/bin" "${php_home}/include" "${php_home}/lib" |
|
||||||
ln -s "$(command -v php-config)" "${php_home}/bin/php-config" |
|
||||||
ln -s "$(php-config --include-dir)" "${php_home}/include/php" |
|
||||||
ln -s "${embed_library}" "${php_home}/lib/libphp.so" |
|
||||||
|
|
||||||
echo "PHP_HOME=${php_home}" >> "${GITHUB_ENV}" |
|
||||||
echo "Using ${embed_library} from ${embed_package} ${embed_version} for PHP ${php_version}" |
|
||||||
|
|
||||||
- name: Install Composer dependencies |
|
||||||
run: composer install --prefer-dist --no-progress |
|
||||||
|
|
||||||
- name: Download PHPX library |
|
||||||
uses: actions/download-artifact@v4 |
|
||||||
with: |
|
||||||
name: libphpx-php-${{ matrix.php }} |
|
||||||
path: vendor/swoole/phpx/lib |
|
||||||
|
|
||||||
- name: Download phpy extension |
|
||||||
uses: actions/download-artifact@v4 |
|
||||||
with: |
|
||||||
name: phpy-php-${{ matrix.php }} |
|
||||||
path: third_party/phpy/modules |
|
||||||
|
|
||||||
- name: Enable phpy extension |
|
||||||
shell: bash |
|
||||||
run: | |
|
||||||
php_ini_dir="$(php-config --ini-dir)" |
|
||||||
echo "extension=${GITHUB_WORKSPACE}/third_party/phpy/modules/phpy.so" \ |
|
||||||
| sudo tee "${php_ini_dir}/90-phpy.ini" |
|
||||||
echo "PHP_INI_SCAN_DIR=${php_ini_dir}" >> "${GITHUB_ENV}" |
|
||||||
php --ri phpy |
|
||||||
|
|
||||||
- name: Configure native library path |
|
||||||
shell: bash |
|
||||||
run: | |
|
||||||
test -f "${PHPX_HOME}/lib/libphpx.so" |
|
||||||
test -f "${PHP_HOME}/lib/libphp.so" |
|
||||||
echo "LD_LIBRARY_PATH=${PHPX_HOME}/lib:${PHP_HOME}/lib" >> "${GITHUB_ENV}" |
|
||||||
|
|
||||||
- name: Build bootstrap TypePHP compiler |
|
||||||
run: | |
|
||||||
php bin/tpc.php project.yml --job 2 --no-progress |
|
||||||
test -x ./tpc |
|
||||||
./tpc --version |
|
||||||
|
|
||||||
- name: Show build environment |
|
||||||
run: | |
|
||||||
php -v |
|
||||||
php --ini |
|
||||||
ldd ./tpc | grep -E 'libphp(x)?[0-9.]*\.so' |
|
||||||
cmake --version |
|
||||||
c++ --version |
|
||||||
|
|
||||||
- name: Run compiler PHPT suite with bootstrap compiler |
|
||||||
run: | |
|
||||||
mkdir -p build |
|
||||||
php run-tests.php -q -j8 --compiler ./tpc \ |
|
||||||
-w build/failed-tests.txt -W build/test-results.txt tests/compiler |
|
||||||
|
|
||||||
- name: Upload PHPT failure artifacts |
|
||||||
if: failure() |
|
||||||
uses: actions/upload-artifact@v4 |
|
||||||
with: |
|
||||||
name: phpt-failures-php-${{ matrix.php }} |
|
||||||
if-no-files-found: ignore |
|
||||||
retention-days: 7 |
|
||||||
path: | |
|
||||||
build/failed-tests.txt |
|
||||||
build/test-results.txt |
|
||||||
build/**/*.cc |
|
||||||
build/**/*.h |
|
||||||
php_test_results_*.txt |
|
||||||
tests/compiler/**/*.diff |
|
||||||
tests/compiler/**/*.log |
|
||||||
tests/compiler/**/*.out |
|
||||||
@ -0,0 +1,76 @@ |
|||||||
|
# 2026-08-03 工作日志 |
||||||
|
|
||||||
|
## TypePHP 编译性能分析(docs/COMPILATION_PERFORMANCE_ANALYSIS.md) |
||||||
|
|
||||||
|
**任务**:分析编译实现、提升编译速度、评估 mago 替代 AST 解析。 |
||||||
|
|
||||||
|
**编译管线**(4 阶段): |
||||||
|
1. prepare:扫描+parse(#1)+符号表+拓扑排序(SourcePipelineTrait::prepare → Preprocessor::prepareFile) |
||||||
|
2. convert:parse(#2,重复)+NodeTraverser+生成 C++(Translator::doConvert) |
||||||
|
3. compile:PCH + pcntl 并行(Windows 串行) |
||||||
|
4. build:链接 |
||||||
|
|
||||||
|
**实测瓶颈**(src/ 139 文件,php-parser 5.6.1): |
||||||
|
- 解析吞吐仅 3.18 MB/s,单遍 parse+resolve ≈ 599ms |
||||||
|
- **每个 PHP 文件解析两次**(prepare + convert)→ 纯 PHP 解析 ~1.2s 起步 |
||||||
|
- 生成 .cc 无增量缓存(只有 phpx misc 有),writeFile 无内容比对 → mtime 必变触发全量重编 |
||||||
|
- prepare/convert 串行;Windows 无 pcntl 串行编译 |
||||||
|
- AST serialize 体积膨胀 28.8 倍(47.6MB/1.65MB),unserialize 260ms vs parse+resolve 599ms(快 2.3 倍) |
||||||
|
|
||||||
|
**提速方案**(按 ROI): |
||||||
|
- S1(P0) prepare/convert 合并单次解析(AST 内存/磁盘缓存) |
||||||
|
- S2(P0) 生成 .cc 增量对象缓存 + writeFile 内容比对(key 须含公共声明头) |
||||||
|
- S3(P0) PCH/misc 指纹轻量化(mtime 先行,内容 hash 兜底) |
||||||
|
- S4(P1) 解析阶段 pcntl 并行 |
||||||
|
- S5(P1) Windows 并行(Msvc /MP 或 proc_open) |
||||||
|
- S7(P2) 合并多次 NodeFinder 全树扫描 |
||||||
|
|
||||||
|
**mago 评估结论:不建议替换**。mago 是 Rust 的 PHP 工具链(lint/format/analyze),AST 是 Rust 结构 + CST 模型,与 TypePHP 深度耦合的 php-parser API(296 处引用/75 文件,依赖 NodeTraverser/NameResolver/ConstExprEvaluator/PrettyPrinter/attribute 机制)不兼容。JSON 桥接或 FFI 的成本会吃掉解析速度优势。合理定位:前置语法预检或未来原生前端路线参考。 |
||||||
|
|
||||||
|
**产物**:docs/COMPILATION_PERFORMANCE_ANALYSIS.md(完整报告)、.workbuddy/bench_ast.php(可复现基准脚本) |
||||||
|
|
||||||
|
## 实施 S1+S2(下午追加) |
||||||
|
|
||||||
|
**S1 合并两次解析**: |
||||||
|
- `CompilerBase` 新增 `parseCachedAst()`(按 `$this->file` 缓存原始 AST)+ `cloneAst()`(CloningVisitor 深拷贝) |
||||||
|
- `prepareFile()` 与 `doConvert()` 都改用它 → 每个文件只 parse 一次 |
||||||
|
- 关键约束:`RuntimeAttributeFactoryLowering`/`Visitor` 是有状态 visitor(会向 AST 追加节点),**同一棵 AST 不能二次遍历**,必须返回深拷贝 |
||||||
|
|
||||||
|
**S2 增量缓存**: |
||||||
|
- `writeFile()` 内容比对:相同不覆盖,返回 bool(mtime 稳定,防头文件抖动) |
||||||
|
- 新增 `hasGeneratedObjectFileCache()`:key = sha256(编译命令 + PHP ABI + 生成的 func_decl.h/data_decl.h/_arginfo.h 内容 hash) |
||||||
|
- `isGeneratedSourceFile()`:buildDir 前缀 + .cc 扩展判断 |
||||||
|
- `compileFile()` 对生成的 .cc 走缓存;`save()`/`genExtension()` 仅实际写入时 format |
||||||
|
- 注意:extension-<target>.cc **不应**列入 header 依赖(内容随任何类变化,会让所有 .cc 全量失效) |
||||||
|
|
||||||
|
**实测**(302 文件合成项目,dry,3 次均值): |
||||||
|
- 全量:基线 6011ms → 改动后 5629ms(-6.4%) |
||||||
|
- 增量(不清理 build,第 3 次):5023ms(-16.4% 相对基线冷构建) |
||||||
|
- dry 不含 C++ 编译,S2 完整收益需在 build 场景验证 |
||||||
|
|
||||||
|
**环境/验证要点**: |
||||||
|
- 本机完整编译需要 `PHP_HOME=D:/git/php/tpc_v1095_windows_x86_64`(含 php8embed.lib、SDK/lib/php8ts.lib、phpx/)+ 复制 `tpc_v1095/phpx.dll` 到 `phpx/build/phpx.dll` |
||||||
|
- `tpc_v1095/tpc.exe` 是编译自 `D:\workspace\compiler` 的二进制,本机无法运行(内嵌路径不符) |
||||||
|
- typephp-think(554 文件)补丁后 prepare 通过,convert 阶段 `Cannot re-assign` 是仓库源码类型系统比 v1095 正式版严格(`$str` array→str),非本次改动问题 |
||||||
|
- **git 事故**:git stash 失败损坏 refs(HEAD→speed_build 指向 a92fc0df 对象缺失)。已恢复:分支指向 18decac(可用 commit),onepiece-doudizhu 示例内容在暂存区完好。备份在 `.git-backup-20260803/`,确认后删除 |
||||||
|
|
||||||
|
## git 恢复与提交(下午追加 2) |
||||||
|
|
||||||
|
- **git stash 事故根因确认**:stash 失败不仅丢 refs,还导致 onepiece-doudizhu-win32 全部 19 个文件的 blob 对象缺失(index 引用了不存在的对象)。工作区文件完好。 |
||||||
|
- **恢复方法**:`git reset -- <dir>` 取消暂存 → `git add <dir>`(从工作区重建 blob)→ commit。已恢复为 `449afc1 add onepiece-ddz game example`(4226 行,与原提交同名)。 |
||||||
|
- **S1/S2 已提交**:`ac7813d perf: cache AST across prepare/convert and add generated object cache`(3 文件 +206/-14)。 |
||||||
|
- **最终 git fsck = 0 missing,仓库健康**。 |
||||||
|
- 遗留:`.git-backup-20260803/`(18MB,git 事故备份,确认后删)、`.workbuddy/onepiece-backup/`(文件级备份,可删)。 |
||||||
|
|
||||||
|
## code-wiki 文档生成(2026-08-04) |
||||||
|
|
||||||
|
- 为 aot-compiler 生成了中文 AI Wiki:22 页,7 个章节(概览 / 前端解析 / 类型系统 / 代码生成 / 后端构建 / 优化性能 / 运行时边界)。 |
||||||
|
- **重要修正**:code-wiki skill 的文档写 `<workspace>/.agents/wiki/`,但 `scripts/render.py` 实际读取 `<workspace>/.workbuddy/wiki/`(`wiki_dir = os.path.join(workspace, ".workbuddy", "wiki")`)。写 catalog.xml / pages 必须放 `.workbuddy/wiki/`,否则 render 报 "catalog.xml not found"。 |
||||||
|
- 产物:`D:\git\php\aot-compiler\.workbuddy\wiki/index.html`(左侧目录导航 + 右侧 iframe,离线 Mermaid)。render 命令:`python <skill>/scripts/render.py render <workspace> --lang zh`。 |
||||||
|
|
||||||
|
## 环境结论(重要) |
||||||
|
|
||||||
|
- **本机 MSVC 工具链不完整**:cl.exe + C++ 标准库头存在(`C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.44.35207/include/cstring`),但 **Windows SDK(Windows Kits 目录)完全缺失** → 无法完成真实 C++ 编译链接,只能验证到 dry 模式(prepare+convert+生成 .cc)。 |
||||||
|
- `tpc_v1095_windows_x86_64` 是完整 PHP 构建环境:php8embed.lib、SDK/lib/php8ts.lib、phpx/(含 lib/phpx.lib);phpx.dll 需复制到 `phpx/build/phpx.dll` 才能通过校验。 |
||||||
|
- 完整编译需在装有 Windows SDK 的机器执行(用户原编译机)。 |
||||||
|
|
||||||
@ -0,0 +1,154 @@ |
|||||||
|
# 2026-08-04 |
||||||
|
|
||||||
|
## code-wiki 渲染路径修正 |
||||||
|
|
||||||
|
- 用户将 code-wiki skill 的 render.py 渲染目录从 `.workbuddy/wiki` 改为 `.agents/wiki`。 |
||||||
|
- 实测发现:render.py 的**文档注释**改了,但 `render()`(第243行)与 `browse()`(第328行)函数体仍硬编码 `.workbuddy`。已把这两处改为 `.agents` 并重新渲染成功。 |
||||||
|
- 结论:code-wiki 产物目录现统一为 **`.agents/wiki/`**(catalog.xml 在 `.agents/wiki/data/`,页面 md 在 `.agents/wiki/pages/`)。下次 render 直接 `python render.py render <ws> --lang zh`,无需再复制。 |
||||||
|
- 重新渲染结果:`[done] rendered 22 pages -> .agents/wiki/index.html`。 |
||||||
|
|
||||||
|
## 修复 Windows 资源编译 RC2135 |
||||||
|
|
||||||
|
- 报错:`app_resource.rc(1) : error RC2135 : file not found: by`(资源编译阶段)。 |
||||||
|
- 根因:`src/Build/ResourceCompilationTrait.php` 写 rc 文件时强制加了 UTF-8 BOM(`"\xEF\xBB\xBF" . $rcContent`),而 `src/Generator/ResourceFileGenerator.php` 生成的注释用 `//`。BOM 顶在文件头导致 rc.exe 第1行识别不出 `//` 注释,把 `Generated by TypePHP` 当语句、`by` 当文件名 → RC2135(老版/部分 Windows SDK 的 rc.exe 有此坑)。 |
||||||
|
- 修复:① 去掉 BOM(直接写 `$rcContent`);② 生成内容里的 `//` 注释全改为 `;`(rc 全版本支持的注释符)。中文靠 `#pragma code_page(65001)` 正常处理,无需 BOM。`.h` 头文件里的 `//` 不动(rc 不处理)。 |
||||||
|
- 重要:此修复需**重新编译 tpc.exe** 才生效——当前运行的预编译 `tpc.exe` 每次构建都会按自身逻辑重新生成 rc 文件,改 build/app_resource.rc 不持久。 |
||||||
|
- 临时绕过(不重编也能跑通):把 project.yml 的 `resource:` 整段(第10~24行 icon + version-info)注释掉,`hasResource()` 返回 false,资源编译被跳过,可先验证后续链接;编好带修复的 tpc.exe 后再恢复该段即可重新带图标/版本。 |
||||||
|
- 已应用临时绕过:project.yml 的 `resource:` 整段已改为全注释(带"待带修复的 tpc.exe 重新编译后恢复"标记)。用户可立即重跑 tpc.exe 验证链接链路。 |
||||||
|
|
||||||
|
## LNK1104 链接失败(输出 tpc.exe 被锁) |
||||||
|
|
||||||
|
- 报错:`LINK : fatal error LNK1104: 无法打开文件"tpc.exe"`(乱码是 GBK 被当 UTF-8 解码的"无法打开文件")。 |
||||||
|
- 根因:project.yml `name: tpc` → 输出 `tpc.exe`;`name` 无路径 → `outputDir` 为空,TypePHP 把最终 exe 输出到 cwd 相对路径(`getTargetFileName()` 返回 `'tpc.exe'`,`/OUT:"tpc.exe"`)。用户从编译器自身目录 `D:\git\php\tpc_v1095_windows_x86_64` 启动 tpc.exe(cwd=该目录),于是 `/OUT:"tpc.exe"` 落到正在运行的 `D:\git\php\tpc_v1095_windows_x86_64\tpc.exe` 上,系统拒绝改写正在执行的 exe → LNK1104。 |
||||||
|
- 佐证:无残留 tpc 进程;`build\tpc.exe` 不存在;`build\` 可写;中间产物 rc 在 `tpc_v1095/build/` → cwd 即编译器目录。 |
||||||
|
- 修复:从项目目录 `D:\git\php\aot-compiler` 启动编译器(cd 过去再用绝对路径调 tpc.exe),输出落到 `aot-compiler/tpc.exe`,避开与运行中编译器同名冲突。备选:给 project.yml 换输出名/输出目录(不建议改 `name`,会影响生成符号)。 |
||||||
|
|
||||||
|
## tpc.exe 编译 hello.php 时 OOM(VirtualAlloc 失败) |
||||||
|
|
||||||
|
- 现象:`.\tpc.exe examples/hello.php` 跑到打印第3条 cl 命令(typephp_fiber_generator.cc)后,`VirtualAlloc() failed [0x8 内存资源不足]` x2,随后 `Fatal error: Out of memory (allocated 8388608) (tried to allocate 140736835400432 ≈128TB) in src/compiler.php on line 4`。 |
||||||
|
- 诊断结论:**不是机器内存不够**。实测本机 96GB 物理内存(空闲 74GB)、页面文件空闲 81GB、20 核。`VirtualAlloc` 拒绝是因为请求了一个被算坏的 ~128TB 尺寸(符号扩展/未初始化),OOM 报告把这个坏尺寸打印出来——属症状非根因。 |
||||||
|
- Windows 上 `Platform/Windows::supportsPcntlParallelCompile()` 返回 false(`Windows.php:226`),`Translator.php:1441` 走 `compileSourceFile()` **顺序编译**(`passthru` 逐个跑 cl),**不并行**,`-j` 在此无效。 |
||||||
|
- 隔离测试:`--dry`(仅 prepare+convert 生成 C++)能干净退出(exit 0,不 OOM)→ bug 只在 **compile/build(链接)阶段**。源码 `compiler.php:8` 已 `ini_set('memory_limit','-1')`,且报错走的是 Zend MM "out of system memory" 路径(OS 拒 VirtualAlloc),非撞 memory_limit。 |
||||||
|
- 诡异点:该 tpc.exe 能编译整个 tpc 工程(project.yml)却编不了 151B 的 hello.php → 疑似"单文件 .php→exe"模式的专属 bug(parseArgv 对单文件默认也是 BUILD_MODE_BIN,与 project.yml 同模式,故非 buildMode 差异)。 |
||||||
|
- 待办/建议给用户:① `.\tpc.exe project.yml -O2 -j 8`(从 aot-compiler 目录)确认工程模式仍正常,隔离是否单文件模式专属;② 用 v1095 预编译 `tpc.exe` 跑 `examples/hello.php` 对比,判断是"当前源码回归"还是"v1095 误编译";③ 若需进一步定位,查单文件 build/link 路径里计算分配尺寸的代码(读取文件/字符串长度处)。 |
||||||
|
|
||||||
|
## 确认:崩的是"S1+S2 优化版",v1095 基础版不崩 |
||||||
|
|
||||||
|
- 用户确认:`tpc_v1095`(基础版,未做编译速度优化)编译 `examples/hello.php` **不崩**;刚编出来的 tpc.exe(带 S1+S2:ac7813d)崩 128TB。→ 回归在 S1+S2,或 v1095 误编译了 S1+S2 源码。 |
||||||
|
- S1+S2(ac7813d)只改 3 文件:`CompilerBase.php`/`Preprocessor.php`/`Translator.php`。compile 阶段新增代码**只有** S2 的 generated 缓存:`hasGeneratedObjectFileCache`/`getGeneratedObjectCacheKey`/`getGeneratedHeaderDependencies`/`isGeneratedSourceFile`/`compileFile` 的 `$isGenerated` 分支。 |
||||||
|
- 已逐行审 S2 缓存代码:均读小 metadata、算 sha256、拼字符串,**无直接大块分配**;且前两个 generated 文件(hello.cc/extension-hello.cc)已成功跑过缓存检查+编译(日志里 cl 命令已打印),说明新函数本身能跑。 |
||||||
|
- **S2 设计缺陷(确定要修)**:`getGeneratedHeaderDependencies()` 用 `FilesystemIterator` 扫描整个 `build/include` 收集所有 `*_arginfo.h`。但 `build/include` 被**上一次 tpc 工程编译的残留污染**(含 `php_src_CompilerBase_arginfo.h` 423KB、`php_tpc_func_decl.h`、`php_src_Translator_arginfo.h` 等几十个 tpc 工程头)。后果:① 编译 hello.php 时缓存 key 竟依赖无关 tpc 工程头文件(增量缓存正确性错误);② 每个 generated 单元算 key 都要 `hash_update_file` 这些大残留头,浪费且可能触发异常路径。 |
||||||
|
- **本机 Bash 无法复现**:缺 vcvars/cl 时该 AOT 二进制静默 exit 0 无输出,必须在用户带 VS 环境的命令行里测。 |
||||||
|
- 给用户的两个隔离测试(在 VS 命令行执行): |
||||||
|
- 测试1:`.\tpc.exe examples/hello.php --force`(关掉两套缓存检查)。若通过→缓存路径是触发点。 |
||||||
|
- 测试2(若测试1通过):`rmdir /s /q build` 后 `.\tpc.exe examples/hello.php` 跑两次。若第1次(缓存miss)过、第2次(缓存hit,完整算key并 hash_update_file 污染头)崩→bug 在缓存命中路径的 key 计算;若第1次就崩→在 compile/link 本身(更可能是 v1095 误编译)。 |
||||||
|
- **用户实测测试2:清空 build 后首次(缓存 miss)就崩** → 直接排除"缓存命中路径"(缓存命中路径只在第2次才触发 `hash_update_file` 扫描污染头;首次 `$objectFile` 不存在,`hasGenerated/hasMiscObjectFileCache` 在 1215/1123 行 early-return false,根本走不到 key 计算)。 |
||||||
|
- 进一步逐行审查首次 compile 路径全部可能大分配点: |
||||||
|
- `writeFile` 改返回 bool:7 处调用(gen_stub/ResourceCompilationTrait/Translator 498/663/736/1100/1785)均安全,无副作用。 |
||||||
|
- `parseCachedAst`/`cloneAst`:仅 prepare/convert 用,`--dry` 已验证正常。 |
||||||
|
- `compileFile` 缓存 `if` 块(1354-1360):首次恒 false,benign。 |
||||||
|
- `NativeBuilder::compile()`(31-41):Windows 顺序模式走 `passthru($command,$status)`,**不把 cl 输出累积进数组**,无"读输出撑爆"路径。 |
||||||
|
- `compileSourceFile` 循环(1489-1532):每文件 `compileFile(false)` → miss → `compile()` → passthru 跑 cl → 打印命令。前3个文件成功(cl 命令已打印=status 0),崩在第4个文件进入时。 |
||||||
|
- **最终结论(高置信)**:S1+S2 在首次 compile 路径上没有任何大分配来源,128TB 在 PHP 源码层无出处;而 v1095 基础版编 hello.php 正常、我们的版本崩 → **判定为 v1095 编译器对我们当前源码(含 S1+S2)的误编译(miscompilation)**:v1095 在 AOT 编译改动后的 `Translator.php` 时,把 `compileFile`/编译循环邻近某处 size 符号扩展/算坏(典型 ~2⁴⁷)。`--dry` 不进 compile 循环故不触发。 |
||||||
|
- **决定性验证 = Bootstrap 自举**:用当前 tpc.exe(它编工程成功,编 hello.php 崩)重编 tpc 工程得自举版 tpc.exe-B,再用它编 hello.php。 |
||||||
|
- 通过 → 确认 v1095 误编译,自举换了编译上下文/偏移绕开,后续用自举版做 typephp-think 速度对比即可。 |
||||||
|
- 仍崩 → 源码确有隐藏 bug,需在 `compileFile` 入口加 STDERR 文件名日志 + `NativeBuilder::compile` 前后标记,再编一次精确定位第4个文件及崩因。 |
||||||
|
- 待修(不影响崩溃):`getGeneratedHeaderDependencies()` 通配 `*_arginfo.h` 会纳入 build/include 残留 tpc 工程头(`php_src_*`),应限定为当前 `$this->targetName` 前缀(`str_starts_with($name,'php_'.$targetName.'_') && str_ends_with($name,'_arginfo.h')`)。等自举成功后再单独修并验证,避免重编混入变量。 |
||||||
|
|
||||||
|
## Bootstrap 自举也崩 → 推翻"纯误编译"早期判断 |
||||||
|
|
||||||
|
- 用户实测:`.\tpc.exe project.yml -O2 -j 8`(用我们的 tpc.exe 重编工程,即自举)**同样崩在 typephp_fiber_generator.cc**,与基础版崩溃签名完全一致(2⁴⁷)。 |
||||||
|
- 关键修正:自举崩**不能区分**"v1095 误编译"与"源码真 bug"——因为自举二进制由我们的 tpc.exe(本身可能已被 v1095 误编译)再编一次,缺陷会继承传递。所以自举崩对两种假设都"符合",不是决定性证据。 |
||||||
|
- 进一步精读定位: |
||||||
|
- `getLanguageFromExtension('.cc')` 返回 **null** → misc 文件走 `getCompileCommandOptions()` → 合法 `cl /c` 命令(解释了日志里 misc 文件没抛 RuntimeException,而是正常打印 cl 命令)。 |
||||||
|
- `getMiscObjectCacheKey()` 只做 `hash('sha256', buildCompileFileCommand(...) . "\0" . serialize($abi))`;而 `buildCompileFileCommand`(generated 文件也用)在 generated 路径已验证正常 → **misc 专属 metadata 写入路径不是 2⁴⁷ 来源**。 |
||||||
|
- 仓库真实状态:HEAD=ac7813d(S1+S2);工作树仅我的 RC 修复 + project.yml 注释,无其它碰编译链路的改动。 |
||||||
|
- S1 是前端 AST 缓存(misc 文件不是 PHP,根本不进前端)→ 已排除。S2 是唯一落在 compile 阶段的改动。 |
||||||
|
|
||||||
|
## 二分开关:已禁用 S2 缓存逻辑(compileFile) |
||||||
|
|
||||||
|
- 在 `src/Translator.php` 的 `compileFile()` 里,把 S2 增量对象缓存逻辑**整段去掉**(缓存命中跳过 + `invalidateMisc/GeneratedObjectCache` + `writeMisc/GeneratedObjectCacheMetadata`),退回 S2 之前的纯编译行为。加了 `// [bisect]` 注释标记,定位清楚后删除。 |
||||||
|
- 目的:若用 v1095 重新编译(带此开关的源码)后 `.\tpc.exe examples/hello.php` **不再崩** → 坐实"S2 那段代码被 v1095 AOT 误编译",下一步改写 S2 避开该结构(如去掉 SPL `RecursiveIteratorIterator`、改用更简单 size 计算)。 |
||||||
|
- 若仍崩 → 不是 S2,需扩大范围(revert 全部 S1+S2、或排查 S1+S2 之外的更早提交/phpx 依赖更新 2129145)。 |
||||||
|
- 用户重编命令(必须用 v1095,因为我们的 tpc.exe 也会崩,不能用来重编): |
||||||
|
- 从 `D:\git\php\aot-compiler` 目录:`D:\git\php\tpc_v1095_windows_x86_64\tpc.exe project.yml -O2 -j 8`(RC 修复已在源码、resource 已注释,且无输出冲突 → 可编译+链接成功)。 |
||||||
|
- 再测:`.\tpc.exe examples/hello.php`。 |
||||||
|
|
||||||
|
## 坐实:S2 触发 v1095 误编译 + 已保守化重写 |
||||||
|
|
||||||
|
- 用户实测:用 v1095 重编"二分开关版"(S2 在 compileFile 的调用整段去掉)源码,产出的 `tpc2.exe` 编 `examples/hello.php` **不再崩,成功产出 hello.exe**(7 文件编译+链接全过)。→ **坐实 S2 那段增量对象缓存代码被 v1095 AOT 误编译**(去掉调用即不再触发 2⁴⁷)。S1 已确认安全(--dry 正常)。 |
||||||
|
- 首次编译实际会执行的 S2 高危路径:`writeGeneratedObjectCacheMetadata` → `getGeneratedObjectCacheKey`。该函数原版用了 `hash_init`/`hash_update`/`hash_update_file`/`hash_final` 哈希**资源流**,并调用 `getGeneratedHeaderDependencies()`(含 `FilesystemIterator` SPL 迭代器)。这两类结构在 v1095 AOT 编译下最易被误编译成坏 size(2⁴⁷ 符号扩展)。`hasMiscObjectFileCache` 原版也含 `RecursiveIteratorIterator`/`RecursiveDirectoryIterator`。 |
||||||
|
- **已重写 S2 为保守版**(src/Translator.php),消除全部高危结构: |
||||||
|
1. `getGeneratedObjectCacheKey`:去掉 `hash_init/update/update_file/final` 流,改用 `hash('sha256', implode("\0", $parts))` 单次 + 每个依赖头 `hash_file('sha256', $header)` 单次。 |
||||||
|
2. `getGeneratedHeaderDependencies`:去掉 `FilesystemIterator`,改用 `scandir` 一层遍历;并把 `*_arginfo.h` 通配**限定为当前 target 前缀**(`str_starts_with($name,'php_'.$targetName.'_')`),顺手修了"扫描整个 build/include 含 tpc 工程残留头"的设计缺陷。 |
||||||
|
3. `hasMiscObjectFileCache`:去掉 `RecursiveIteratorIterator`,新增 `collectHeaderFiles()`(scandir 递归,无 SPL 迭代器)替代。 |
||||||
|
4. `getMiscObjectCacheKey` 保持原 `hash('sha256', ...)` 单次调用(已验证安全)。 |
||||||
|
5. 恢复 `compileFile` 里的 S2 调用(缓存命中跳过 + invalidate + writeMetadata),删除二分 `[bisect]` 标记。 |
||||||
|
- 验证:`php -l src/Translator.php` 无语法错误;Grep 确认 Translator.php 内已无 RecursiveIteratorIterator/RecursiveDirectoryIterator/FilesystemIterator/hash_init/hash_update/hash_update_file/hash_final 残留。 |
||||||
|
- **未动的同类结构**:`FileScanner.php`、`Build/PrecompiledHeaderManager.php`、`gen_stub.php` 仍有 SPL 迭代器 + hash 资源流,但**不是本次崩溃元凶**(v1095 编整个工程时这些文件都被正常 AOT 编译且没崩),保持最小改动原则暂不碰。 |
||||||
|
- **下一步验证(用户执行,必须用 v1095 重编,因为我们的 tpc.exe 自己也崩)**: |
||||||
|
``` |
||||||
|
cd /d D:\git\php\aot-compiler |
||||||
|
D:\git\php\tpc_v1095_windows_x86_64\tpc.exe project.yml -O2 -j 8 # 产新 tpc.exe:含保守版S2 + 源码内修复版ResourceFileGenerator(自身无图标,因resource注释) |
||||||
|
rmdir /s /q build |
||||||
|
.\tpc.exe examples\hello.php # 第一次:应不崩出 hello.exe(验证S2修复) |
||||||
|
.\tpc.exe examples\hello.php # 第二次:应命中缓存([cache] skip),更快(验证S2加速) |
||||||
|
``` |
||||||
|
- 若第一次仍崩 → 误编译源不在已改这几处,需更细二分(如逐个恢复 hasMisc/hasGenerated 与 write*Metadata)。 |
||||||
|
- 若两次都过 → S2 修复生效。之后可恢复 project.yml 的 resource 段,用新 tpc.exe 编 project.yml 验证 RC2135 也修好且带图标。 |
||||||
|
|
||||||
|
## 关键反转:保守化重写的 S2 仍崩 128TB → 判定为 v1095 跨函数误编译 |
||||||
|
|
||||||
|
- 用户实测:用 v1095 重编"保守版 S2"(去 SPL 迭代器 + 去 hash 资源流,compileFile 已恢复 S2 调用)源码,产出的 `tpc2.exe` 编 `examples/hello.php` **仍然崩 128TB**,崩溃签名与原始版完全一致(`tried to allocate 140736835400432 ≈2⁴⁷`)。 |
||||||
|
- 这**推翻了"是 SPL 迭代器 / hash 资源流被误编译"的假设**。进一步定位:崩溃发生在**首次编译** `hello.cc`(generated 文件)打印 cl 命令后、进入 `getNativeBuilder()->compile()` 时。首次编译对 `hello.cc` 实际执行的 S2 代码只有 `isPhpxMiscFile()`、`isGeneratedSourceFile()`、`hasGeneratedObjectFileCache()`(obj 不存在→early-return false)、`invalidateGeneratedObjectCache()`;而我新写的保守 `hash()`/`hash_file()` 此时**还没轮到**(它在编译后才写的 metadata 里)。→ 崩点根本到不了哈希,说明它不是某个 helper 的内部写法问题。 |
||||||
|
- **最终判定:v1095 在 AOT 编译 `compileFile` 里那段 S2 包裹代码时,连带把后续 `getNativeBuilder()->compile()` 编坏了**(典型跨函数误编译:A 函数误编→B 函数运行时崩)。窄重写 helper 救不了,因为触发点是"这段 S2 代码被编译/被调用"这件事本身,而不是具体哪行语句。 |
||||||
|
- **这是 v1095 编译器(AOT)的 bug,不是我们源码的逻辑 bug**。在源码层面做局部改写无法可靠规避。 |
||||||
|
- **决策:禁用 S2**(从 `compileFile` 移除全部 S2 调用,退回纯编译态,等同于已证明稳定的 bisect 版),保留 S1(合并两次解析,主要提速点,已确认安全)。S2 的 helper 方法(hasMisc/hasGenerated/getXxxCacheKey/writeXxxMetadata/collectHeaderFiles)作为"死代码"**保留**(已证明:定义但不被 compileFile 调用就安全),待编译器修复后复用。 |
||||||
|
- 当前 `compileFile`(src/Translator.php ~1378)已加注释明确 S2 禁用原因;`php -l` 通过。 |
||||||
|
- **后续可选项(暂不阻塞)**: |
||||||
|
1. 用 v1095 重编当前源码(S1 开、S2 关)→ 应稳定,可正常编 hello.php 与 project.yml。 |
||||||
|
2. 想保留 S2 加速的可试验:用**更低优化级别**(如 `project.yml -O1` 而非 `-O2`)重编 tpc.exe。若 v1095 的误编译是优化级别相关,低级别可能绕开 → 届时再恢复 compileFile 的 S2 调用测试。但 -O1 会让 tpc.exe 自身变慢,需权衡。 |
||||||
|
3. 向 v1095 上游报告该 AOT 误编译(含最小复现:带 S2 包裹的 compileFile → 2⁴⁷ VirtualAlloc)。 |
||||||
|
- 当前 HEAD 仍为 ac7813d 之上的未提交改动(RC2135 修复 + S2 禁用 + resource 段已注释)。待用户验证稳定后,建议提交 S1 部分(已稳定)并将 S2 作为"被编译器 bug 阻塞"的待办保留。 |
||||||
|
|
||||||
|
## typephp-think 编译报 Duplicate class(重复类 fatal) |
||||||
|
|
||||||
|
- 现象:`.\tpc.exe`(S1 开/S2 关的稳定版)编译 `D:\git\php\typephp-think` 时,`Preprocessor->prepareFile` 报 `Duplicate class think\exception\ClassNotFoundException`(首次撞在 `think-container/src/exception/ClassNotFoundException.php`)。 |
||||||
|
- **不是 S1/S2 引入**:`Preprocessor.php:697` 的重复检测 `isset($this->symbolDeclInFile[...])` 是 prepare 阶段的**全局符号表**(第733行赋值、无 resetClass 重置、ac7813d 未动过它)。我们的编译器对跨文件重名类是全局 fatal,比 v1095(别的电脑能编过的预编译版)更严格 → 这正是"别的电脑能编过"的原因(v1095 容忍重复类)。 |
||||||
|
- **根因 = 工程的重名类 + 补丁机制不会解决它们**:`typephp-think` 有 `patches/` 目录镜像 `vendor/`,由 `patch.php`(`copyPatchesSafely()`)在 `composer post-autoload-dump` 时把 `patches/*` **覆盖拷贝**到 `vendor/*` 并写 `.patches_applied`。但 `patches/topthink/` 只含 `framework/think-dumper/think-filesystem/think-orm/think-trace`,**不含 think-container**;覆盖拷贝不会删文件。所以重名类依然存在。用户"今天只更新代码没打补丁"使 vendor 回到未打补丁态(需重跑 `php patch.php`)。 |
||||||
|
- **vendor/ 内真实重名类只有 4 个**(编译器实际会撞的;patches/vendor 成对的不算,因编译器不扫 patches/): |
||||||
|
1. `think\exception\ClassNotFoundException`:framework vs **think-container**(两份字节一致,删 think-container 那份安全)。 |
||||||
|
2. `think\Exception`:framework vs **think-orm/stubs**/Exception.php(stub,删 stub 安全)。 |
||||||
|
3. `think\Facade`:think-container vs **think-orm/stubs**/Facade.php(stub,删 stub 安全)。 |
||||||
|
4. `think\route\Dispatch`:framework 自身两份(`route/Dispatch.php` 规范基类 vs `route/dispatch/Dispatch.php` 命名空间写错成 `think\route`、全工程无引用 `think\route\dispatch\Dispatch` → 死代码,删后者安全)。 |
||||||
|
- **决策:放宽编译器重复检测,而非删 vendor**(为公平对比:v1095 容忍重复类,我们的 tpc.exe 也应如此,避免改输入导致 our-tpc vs v1095 不对等)。改 `Preprocessor.php`: |
||||||
|
- `prepareClass`(~697):重复时 `$this->warning(...)` + `resetClass()` + `return ''`(不再 fatal,保留首个声明)。 |
||||||
|
- `parseInterface`(~1507):重复时 `$this->warning(...)` + `return;`(不再 fatal)。 |
||||||
|
- warning 方法签名 `warning(Node $node, string $msg): void`(CompilerDiagnosticTrait)。`php -l` 通过。 |
||||||
|
- 验证:重编 tpc.exe(v1095)→ 编 typephp-think,应越过该 Duplicate class 继续。若再撞别的重名,说明还有未覆盖的 vendor 内部重复(再扫描确认)。 |
||||||
|
- 备选(不推荐):直接删上述 4 个冗余 vendor 文件。缺点:composer 会还原 + 让我们与 v1095 的输入不一致,破坏速度对比公平性。 |
||||||
|
|
||||||
|
## typephp-think 编译报 Cannot re-assign $str(string→array 类型冲突 fatal) |
||||||
|
|
||||||
|
- 现象:`.\tpc.exe` 编 `typephp-think` 走到 `symfony/var-dumper/Dumper/CliDumper.php:207` 时,`Preprocessor/Translator` 在 convert 阶段 fatal:`Cannot re-assign $str from php::Array to php::Str`。该行是 `dumpString(Cursor $cursor, string $str, bool $bin, int $cut)` 内 `$str = $bin && str_contains($str,"\0") ? [$str] : explode("\n",$str);` —— 形参 `string $str` 先当字符串用,后在 207 行被赋值为数组。 |
||||||
|
- **根因 = 我们的编译器对局部变量/形参做了"单静态类型"假设**:`AssignOpTrait::parseAssignFinally`(~337)对已存在变量调用 `checkVarAssignExpr($left, 现有类型, 新值类型)`;`CompilerBase::checkVarAssignExpr` 对"不兼容"的两次赋值直接 fatal。但 PHP 是动态类型,变量中途换类型是合法的——v1095(别的电脑能编过的预编译版)对此类变量用运行时 variant(`php::Var`)承载,所以不崩。我们之前对 vendor 的容忍(Duplicate class→warning)没覆盖到这种"标量→数组"类型变更。 |
||||||
|
- **关键正确性约束**:不能简单把 fatal 降级为 warning——否则 C++ 里 `php::Str str;` 后面接 `str = <数组>;` 会是类型错误甚至**静默误编译**。正确做法是:被重新赋以不兼容类型的变量,从一开始(声明处)就声明为运行时 variant `php::Var`。 |
||||||
|
- **修复(src 三处)**: |
||||||
|
1. `CompilerBase.php`:新增 `protected array $variantVars = [];`(resetFunction 里清空);新增 `areTypesAssignable($existing,$new): bool`,与 `checkVarAssignExpr` 的兼容规则逐字一致(VAR/REF/同类型/双 native/big 类型 都兼容,其余冲突),仅返回 bool 不 fatal,供预扫描判定。 |
||||||
|
2. `Translator.php`:新增 `computeVariantVars()` + `collectAssignTypes()/collectAssignTypesNode()`(递归走函数体,跳过嵌套 function/closure/arrow,收集每个简单变量赋值 RHS 的 detectTypeOfExpr 类型,并以形参 argInfo->type 作种子);`parseFunction()` 在 SSA 之后调用它,若某变量类型集合存在不兼容对 → 记入 `variantVars`,并把这些形参的 `argInfo->type` 改为 `php::Var`、重拼 `functionDef->params`(用 `genArgumentDeclaration`)。 |
||||||
|
3. `Parser/AssignOpTrait.php`:`parseAssignFinally` 首次声明局部变量时,若 `$var` 在 `variantVars` 中 → `finalVarType = Type::VAR`(声明即 variant),后续赋值走 variant 路径、`checkVarAssignExpr` 因 VAR 提前返回,不再 fatal。 |
||||||
|
- **兼容性**:决策逻辑与旧 `checkVarAssignExpr` 等价——原来能通过的不兼容赋值(int↔float、同类型重复、含 VAR/big)仍不触发;原来会 fatal 的(str↔arr、str↔int 等)现在正确升级为 variant,而非静默误编译。已用独立脚本验证决策矩阵。 |
||||||
|
- **限制**:预扫描只覆盖 `Function_`/`ClassMethod`(走 parseFunction);`Closure`/`ArrowFunction` 走 `parseStmts` 不经过 computeVariantVars,其形参若中途换类型仍可能 fatal(vendor 里少见,遇到再补)。`php -l` 三个文件均通过。 |
||||||
|
- **验证状态**:本机无法端到端重编验证——缺 PHP/Zend 头文件(php.h 不存在),且当前 `PHP_HOME=D:\git\php\tpc_v1092_windows_x86_64`(陈旧残留,非 PHP 安装)导致 `detectPhpLibs` 找不到 php8ts.lib。用户的真实构建环境(含 PHP dev 头 + 正确的 PHP_HOME)在另一台机器,已多次成功重编。须由用户执行重编+重跑验证(见下条命令)。 |
||||||
|
- **用户重编+重跑序列(必须用 v1095,因我们的 tpc.exe 自身也需它编译;S2 已禁用故稳定)**: |
||||||
|
``` |
||||||
|
cd /d D:\git\php\typephp-think && php patch.php |
||||||
|
cd /d D:\git\php\aot-compiler |
||||||
|
set PHP_HOME=<用户正确的 PHP TS dev 安装目录> # 例 D:\Program Files\PhpWebStudy-Data\app\php-8.3.24-ts(且其 dev\php8ts.lib 需在 $phpDir/lib 或 $phpDir 下,必要时拷贝) |
||||||
|
D:\git\php\tpc_v1095_windows_x86_64\tpc.exe project.yml -O2 -j 8 |
||||||
|
rmdir /s /q build |
||||||
|
.\tpc.exe examples\hello.php # 确认自举稳定 |
||||||
|
cd /d D:\git\php\typephp-think |
||||||
|
D:\git\php\aot-compiler\tpc.exe project.yml -O2 -j 8 # 应越过 Duplicate class 与 Cannot re-assign,继续编译 |
||||||
|
``` |
||||||
|
- **注意**:本机实测 `php-8.3.24-ts` 根目录有 `php8embed.lib`、但 `php8ts.lib` 只在 `dev/` 子目录;`detectPhpLibs` 只查 `$phpDir/$phpDir\SDK\lib/$phpDir\lib`,故需把 `dev\php8ts.lib` 拷到 `php-8.3.24-ts\lib\`(或根)才能让 v1095 的 tpc.exe 找到核心库——这是用户环境的常规布局操作。 |
||||||
@ -0,0 +1,103 @@ |
|||||||
|
# CLAUDE.md |
||||||
|
|
||||||
|
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
||||||
|
|
||||||
|
## Project Overview |
||||||
|
|
||||||
|
TypePHP is a PHP native compilation project. Its `tpc` command is TypePHP Compiler (AOT), which translates PHP source code to C++, then compiles it with GCC/Clang/MSVC into native binaries. It supports Linux (primary), macOS, and Windows. |
||||||
|
|
||||||
|
**Prerequisites**: PHP 8.2+, GCC 9+ (C++17), CMake 3.24+. The `swoole/phpx` extension must be compiled (see README.md). |
||||||
|
|
||||||
|
## TypePHP Language Design Principles |
||||||
|
|
||||||
|
TypePHP should not blindly mirror every PHP language behavior. Most PHP syntax and semantics should remain compatible with ZendPHP, but some legal PHP constructs are historical baggage or language-design mistakes that conflict with static compilation, clear semantics, or robust generated C++ code. |
||||||
|
|
||||||
|
When reviewing or changing compiler behavior: |
||||||
|
|
||||||
|
- Prefer PHP compatibility for common, well-defined syntax that does not weaken the TypePHP static model. |
||||||
|
- Reject PHP historical baggage when the syntax is ambiguous, surprising, or only preserved for legacy compatibility. |
||||||
|
- Diagnose such cases as early as possible during preprocessing/static compilation, instead of deferring to runtime TypeCheck or ZendVM errors. |
||||||
|
- Provide precise errors that include the relevant function/method name, parameter/property name, and type information where applicable. |
||||||
|
- Compare with other statically compiled languages such as C/C++, Java, C#, Go, Rust, Kotlin, and TypeScript before deciding whether TypePHP should preserve or reject a PHP behavior. |
||||||
|
|
||||||
|
Example: `function test($a = 1, $b, $c) {}` is legal in PHP, but the default value for `$a` is effectively ignored and all parameters become required. This is a PHP historical compatibility artifact. TypePHP should reject it during preprocessing instead of preserving the behavior. |
||||||
|
|
||||||
|
Example: PHP permits `return $value;` inside `__construct()` and lets callers consume `parent::__construct()` as a value, even though constructors cannot declare a return type. TypePHP treats constructors consistently with C++/Java-style semantics: constructors initialize objects and must not return values. `return;` is allowed, but `return $value;` or using a constructor call as a value must be rejected during static compilation. |
||||||
|
|
||||||
|
## Build & Test Commands |
||||||
|
|
||||||
|
```bash |
||||||
|
# Install PHP dependencies |
||||||
|
composer install |
||||||
|
|
||||||
|
# Compile a PHP project to a native binary |
||||||
|
./tpc <path-to-project-or-file> |
||||||
|
|
||||||
|
# Run all PHPUnit tests |
||||||
|
./vendor/bin/phpunit |
||||||
|
|
||||||
|
# Run a single PHPUnit test class |
||||||
|
./vendor/bin/phpunit phpunit/src/AstNodeTypeTest.php |
||||||
|
|
||||||
|
# Run PHPT integration tests (all) |
||||||
|
php run-tests.php tests/compiler/ |
||||||
|
|
||||||
|
# Run a single PHPT test |
||||||
|
php run-tests.php tests/compiler/arrays.phpt |
||||||
|
``` |
||||||
|
|
||||||
|
## Architecture |
||||||
|
|
||||||
|
### Translation Pipeline |
||||||
|
|
||||||
|
The compiler follows a 4-stage pipeline, orchestrated by `src/Translator.php` (the main entry point): |
||||||
|
|
||||||
|
1. **prepare()** — Scan PHP files, collect symbol declarations and dependencies, topological-sort for compilation order |
||||||
|
2. **convert()** — Parse PHP AST via `nikic/php-parser`, translate each node to C++ source code |
||||||
|
3. **compile()** — Invoke the platform C++ compiler (GCC/Clang/MSVC) on generated `.cc` files |
||||||
|
4. **build()** — Link object files into a native binary executable |
||||||
|
|
||||||
|
### Class Hierarchy |
||||||
|
|
||||||
|
``` |
||||||
|
src/CompilerBase.php (core PHP→C++ translation logic, indent/output/mode helpers) |
||||||
|
├─ uses traits: AstNodeType, FuncCallOptimizer, ClosureGenerator, |
||||||
|
│ PlaceHolderGenerator, PropertyPromotion, MagicMethodDetector |
||||||
|
└─ src/Preprocessor.php (scanning, symbol tables, dependency sort, YAML config) |
||||||
|
└─ src/Translator.php (full pipeline: prepare→convert→compile→build) |
||||||
|
└─ src/CompilerTest.php (test-only subclass, used by PHPUnit tests) |
||||||
|
``` |
||||||
|
|
||||||
|
### Key Components |
||||||
|
|
||||||
|
| Directory | Purpose | |
||||||
|
|-----------|---------| |
||||||
|
| `src/Entity/` | Data classes: `ClassDef`, `FunctionDef`, `MethodDef`, `PropertyDef`, `ConstantDef`, `InterfaceDef` | |
||||||
|
| `src/Generator/` | Codegen helpers: `ClosureGenerator`, `PlaceHolderGenerator`, `PropertyPromotion`, `Utils` | |
||||||
|
| `src/Backend/` | Compiler abstraction: `CompilerBackend` (abstract) → `Gcc`, `Clang`, `Msvc`. Factory pattern via `CompilerFactory` | |
||||||
|
| `src/Platform/` | OS abstraction: `PlatformBase` → `Linux`, `Macos`, `Windows`. Factory via `PlatformFactory` | |
||||||
|
| `src/Context/` | `ScopeContext` and `FunctionContext` for variable scoping and type tracking | |
||||||
|
| `src/Exception/` | `SyntaxError`, `Unsupported`, `DynamicCall`, `PlaceHolder`, `Skip`, `Redo`, `TestError` | |
||||||
|
| `src/Parser/` | Special-purpose parsers like `StdContainerParser` (C++ std container foreach support) | |
||||||
|
| `src/Resolver/Reflection.php` | Static helpers wrapping PHP reflection (internal class/function detection) | |
||||||
|
| `src/Generator/Symbol.php` | Maps PHP operations to `phpx` C++ API symbol names | |
||||||
|
| `src/Build/FileScanner.php` | Recursive file discovery with extension filtering (supports `.php`, `.cpp`, `.c`, `.s`, `.m`, `.mm`) | |
||||||
|
| `src/Entity/ArgInfo.php` | Generates C function argument info structures for internal function registration | |
||||||
|
| `src/Extractor.php` | Extracts interfaces from PHP classes | |
||||||
|
| `src/Transform/Visitor.php` | Base `NodeVisitorAbstract` extension (skeleton for custom AST visitors) | |
||||||
|
|
||||||
|
### Configuration |
||||||
|
|
||||||
|
- `project.yml` — per-project build config (name, build-mode, C++ standard, compiler flags, sources, resources/icon) |
||||||
|
- Command-line arguments and YAML config are merged in `Preprocessor`, with CLI taking highest priority |
||||||
|
|
||||||
|
### Generated Output |
||||||
|
|
||||||
|
Generated `.cc` and `.o` files land in `build/` directory. The compiled binary is named from `project.yml`'s `name` field (default: `app`). |
||||||
|
|
||||||
|
### Test Infrastructure |
||||||
|
|
||||||
|
- **PHPUnit tests** (`phpunit/src/`) — unit/integration tests for compiler internals. Bootstrap at `phpunit/bootstrap.php` defines a `BaseTest` class with an `exec()` helper that runs the compiler and expects a `TestError` exception containing a given string |
||||||
|
- **PHPT tests** (`tests/compiler/`) — end-to-end tests using the standard PHPT format (`run-tests.php`). Each `.phpt` contains PHP source and expected output sections |
||||||
|
|
||||||
|
When writing new compiler tests, use `CompilerTest::create(ROOT_PATH)` (in `src/CompilerTest.php`) which sets `forTest = true` to enable test-specific behavior without writing files to disk. |
||||||
@ -1,674 +0,0 @@ |
|||||||
GNU GENERAL PUBLIC LICENSE |
|
||||||
Version 3, 29 June 2007 |
|
||||||
|
|
||||||
Copyright (C) 2026 上海识沃网络科技有限公司. <https://www.swoole.com/> |
|
||||||
Everyone is permitted to copy and distribute verbatim copies |
|
||||||
of this license document, but changing it is not allowed. |
|
||||||
|
|
||||||
Preamble |
|
||||||
|
|
||||||
The GNU General Public License is a free, copyleft license for |
|
||||||
software and other kinds of works. |
|
||||||
|
|
||||||
The licenses for most software and other practical works are designed |
|
||||||
to take away your freedom to share and change the works. By contrast, |
|
||||||
the GNU General Public License is intended to guarantee your freedom to |
|
||||||
share and change all versions of a program--to make sure it remains free |
|
||||||
software for all its users. We, the Free Software Foundation, use the |
|
||||||
GNU General Public License for most of our software; it applies also to |
|
||||||
any other work released this way by its authors. You can apply it to |
|
||||||
your programs, too. |
|
||||||
|
|
||||||
When we speak of free software, we are referring to freedom, not |
|
||||||
price. Our General Public Licenses are designed to make sure that you |
|
||||||
have the freedom to distribute copies of free software (and charge for |
|
||||||
them if you wish), that you receive source code or can get it if you |
|
||||||
want it, that you can change the software or use pieces of it in new |
|
||||||
free programs, and that you know you can do these things. |
|
||||||
|
|
||||||
To protect your rights, we need to prevent others from denying you |
|
||||||
these rights or asking you to surrender the rights. Therefore, you have |
|
||||||
certain responsibilities if you distribute copies of the software, or if |
|
||||||
you modify it: responsibilities to respect the freedom of others. |
|
||||||
|
|
||||||
For example, if you distribute copies of such a program, whether |
|
||||||
gratis or for a fee, you must pass on to the recipients the same |
|
||||||
freedoms that you received. You must make sure that they, too, receive |
|
||||||
or can get the source code. And you must show them these terms so they |
|
||||||
know their rights. |
|
||||||
|
|
||||||
Developers that use the GNU GPL protect your rights with two steps: |
|
||||||
(1) assert copyright on the software, and (2) offer you this License |
|
||||||
giving you legal permission to copy, distribute and/or modify it. |
|
||||||
|
|
||||||
For the developers' and authors' protection, the GPL clearly explains |
|
||||||
that there is no warranty for this free software. For both users' and |
|
||||||
authors' sake, the GPL requires that modified versions be marked as |
|
||||||
changed, so that their problems will not be attributed erroneously to |
|
||||||
authors of previous versions. |
|
||||||
|
|
||||||
Some devices are designed to deny users access to install or run |
|
||||||
modified versions of the software inside them, although the manufacturer |
|
||||||
can do so. This is fundamentally incompatible with the aim of |
|
||||||
protecting users' freedom to change the software. The systematic |
|
||||||
pattern of such abuse occurs in the area of products for individuals to |
|
||||||
use, which is precisely where it is most unacceptable. Therefore, we |
|
||||||
have designed this version of the GPL to prohibit the practice for those |
|
||||||
products. If such problems arise substantially in other domains, we |
|
||||||
stand ready to extend this provision to those domains in future versions |
|
||||||
of the GPL, as needed to protect the freedom of users. |
|
||||||
|
|
||||||
Finally, every program is threatened constantly by software patents. |
|
||||||
States should not allow patents to restrict development and use of |
|
||||||
software on general-purpose computers, but in those that do, we wish to |
|
||||||
avoid the special danger that patents applied to a free program could |
|
||||||
make it effectively proprietary. To prevent this, the GPL assures that |
|
||||||
patents cannot be used to render the program non-free. |
|
||||||
|
|
||||||
The precise terms and conditions for copying, distribution and |
|
||||||
modification follow. |
|
||||||
|
|
||||||
TERMS AND CONDITIONS |
|
||||||
|
|
||||||
0. Definitions. |
|
||||||
|
|
||||||
"This License" refers to version 3 of the GNU General Public License. |
|
||||||
|
|
||||||
"Copyright" also means copyright-like laws that apply to other kinds of |
|
||||||
works, such as semiconductor masks. |
|
||||||
|
|
||||||
"The Program" refers to any copyrightable work licensed under this |
|
||||||
License. Each licensee is addressed as "you". "Licensees" and |
|
||||||
"recipients" may be individuals or organizations. |
|
||||||
|
|
||||||
To "modify" a work means to copy from or adapt all or part of the work |
|
||||||
in a fashion requiring copyright permission, other than the making of an |
|
||||||
exact copy. The resulting work is called a "modified version" of the |
|
||||||
earlier work or a work "based on" the earlier work. |
|
||||||
|
|
||||||
A "covered work" means either the unmodified Program or a work based |
|
||||||
on the Program. |
|
||||||
|
|
||||||
To "propagate" a work means to do anything with it that, without |
|
||||||
permission, would make you directly or secondarily liable for |
|
||||||
infringement under applicable copyright law, except executing it on a |
|
||||||
computer or modifying a private copy. Propagation includes copying, |
|
||||||
distribution (with or without modification), making available to the |
|
||||||
public, and in some countries other activities as well. |
|
||||||
|
|
||||||
To "convey" a work means any kind of propagation that enables other |
|
||||||
parties to make or receive copies. Mere interaction with a user through |
|
||||||
a computer network, with no transfer of a copy, is not conveying. |
|
||||||
|
|
||||||
An interactive user interface displays "Appropriate Legal Notices" |
|
||||||
to the extent that it includes a convenient and prominently visible |
|
||||||
feature that (1) displays an appropriate copyright notice, and (2) |
|
||||||
tells the user that there is no warranty for the work (except to the |
|
||||||
extent that warranties are provided), that licensees may convey the |
|
||||||
work under this License, and how to view a copy of this License. If |
|
||||||
the interface presents a list of user commands or options, such as a |
|
||||||
menu, a prominent item in the list meets this criterion. |
|
||||||
|
|
||||||
1. Source Code. |
|
||||||
|
|
||||||
The "source code" for a work means the preferred form of the work |
|
||||||
for making modifications to it. "Object code" means any non-source |
|
||||||
form of a work. |
|
||||||
|
|
||||||
A "Standard Interface" means an interface that either is an official |
|
||||||
standard defined by a recognized standards body, or, in the case of |
|
||||||
interfaces specified for a particular programming language, one that |
|
||||||
is widely used among developers working in that language. |
|
||||||
|
|
||||||
The "System Libraries" of an executable work include anything, other |
|
||||||
than the work as a whole, that (a) is included in the normal form of |
|
||||||
packaging a Major Component, but which is not part of that Major |
|
||||||
Component, and (b) serves only to enable use of the work with that |
|
||||||
Major Component, or to implement a Standard Interface for which an |
|
||||||
implementation is available to the public in source code form. A |
|
||||||
"Major Component", in this context, means a major essential component |
|
||||||
(kernel, window system, and so on) of the specific operating system |
|
||||||
(if any) on which the executable work runs, or a compiler used to |
|
||||||
produce the work, or an object code interpreter used to run it. |
|
||||||
|
|
||||||
The "Corresponding Source" for a work in object code form means all |
|
||||||
the source code needed to generate, install, and (for an executable |
|
||||||
work) run the object code and to modify the work, including scripts to |
|
||||||
control those activities. However, it does not include the work's |
|
||||||
System Libraries, or general-purpose tools or generally available free |
|
||||||
programs which are used unmodified in performing those activities but |
|
||||||
which are not part of the work. For example, Corresponding Source |
|
||||||
includes interface definition files associated with source files for |
|
||||||
the work, and the source code for shared libraries and dynamically |
|
||||||
linked subprograms that the work is specifically designed to require, |
|
||||||
such as by intimate data communication or control flow between those |
|
||||||
subprograms and other parts of the work. |
|
||||||
|
|
||||||
The Corresponding Source need not include anything that users |
|
||||||
can regenerate automatically from other parts of the Corresponding |
|
||||||
Source. |
|
||||||
|
|
||||||
The Corresponding Source for a work in source code form is that |
|
||||||
same work. |
|
||||||
|
|
||||||
2. Basic Permissions. |
|
||||||
|
|
||||||
All rights granted under this License are granted for the term of |
|
||||||
copyright on the Program, and are irrevocable provided the stated |
|
||||||
conditions are met. This License explicitly affirms your unlimited |
|
||||||
permission to run the unmodified Program. The output from running a |
|
||||||
covered work is covered by this License only if the output, given its |
|
||||||
content, constitutes a covered work. This License acknowledges your |
|
||||||
rights of fair use or other equivalent, as provided by copyright law. |
|
||||||
|
|
||||||
You may make, run and propagate covered works that you do not |
|
||||||
convey, without conditions so long as your license otherwise remains |
|
||||||
in force. You may convey covered works to others for the sole purpose |
|
||||||
of having them make modifications exclusively for you, or provide you |
|
||||||
with facilities for running those works, provided that you comply with |
|
||||||
the terms of this License in conveying all material for which you do |
|
||||||
not control copyright. Those thus making or running the covered works |
|
||||||
for you must do so exclusively on your behalf, under your direction |
|
||||||
and control, on terms that prohibit them from making any copies of |
|
||||||
your copyrighted material outside their relationship with you. |
|
||||||
|
|
||||||
Conveying under any other circumstances is permitted solely under |
|
||||||
the conditions stated below. Sublicensing is not allowed; section 10 |
|
||||||
makes it unnecessary. |
|
||||||
|
|
||||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law. |
|
||||||
|
|
||||||
No covered work shall be deemed part of an effective technological |
|
||||||
measure under any applicable law fulfilling obligations under article |
|
||||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or |
|
||||||
similar laws prohibiting or restricting circumvention of such |
|
||||||
measures. |
|
||||||
|
|
||||||
When you convey a covered work, you waive any legal power to forbid |
|
||||||
circumvention of technological measures to the extent such circumvention |
|
||||||
is effected by exercising rights under this License with respect to |
|
||||||
the covered work, and you disclaim any intention to limit operation or |
|
||||||
modification of the work as a means of enforcing, against the work's |
|
||||||
users, your or third parties' legal rights to forbid circumvention of |
|
||||||
technological measures. |
|
||||||
|
|
||||||
4. Conveying Verbatim Copies. |
|
||||||
|
|
||||||
You may convey verbatim copies of the Program's source code as you |
|
||||||
receive it, in any medium, provided that you conspicuously and |
|
||||||
appropriately publish on each copy an appropriate copyright notice; |
|
||||||
keep intact all notices stating that this License and any |
|
||||||
non-permissive terms added in accord with section 7 apply to the code; |
|
||||||
keep intact all notices of the absence of any warranty; and give all |
|
||||||
recipients a copy of this License along with the Program. |
|
||||||
|
|
||||||
You may charge any price or no price for each copy that you convey, |
|
||||||
and you may offer support or warranty protection for a fee. |
|
||||||
|
|
||||||
5. Conveying Modified Source Versions. |
|
||||||
|
|
||||||
You may convey a work based on the Program, or the modifications to |
|
||||||
produce it from the Program, in the form of source code under the |
|
||||||
terms of section 4, provided that you also meet all of these conditions: |
|
||||||
|
|
||||||
a) The work must carry prominent notices stating that you modified |
|
||||||
it, and giving a relevant date. |
|
||||||
|
|
||||||
b) The work must carry prominent notices stating that it is |
|
||||||
released under this License and any conditions added under section |
|
||||||
7. This requirement modifies the requirement in section 4 to |
|
||||||
"keep intact all notices". |
|
||||||
|
|
||||||
c) You must license the entire work, as a whole, under this |
|
||||||
License to anyone who comes into possession of a copy. This |
|
||||||
License will therefore apply, along with any applicable section 7 |
|
||||||
additional terms, to the whole of the work, and all its parts, |
|
||||||
regardless of how they are packaged. This License gives no |
|
||||||
permission to license the work in any other way, but it does not |
|
||||||
invalidate such permission if you have separately received it. |
|
||||||
|
|
||||||
d) If the work has interactive user interfaces, each must display |
|
||||||
Appropriate Legal Notices; however, if the Program has interactive |
|
||||||
interfaces that do not display Appropriate Legal Notices, your |
|
||||||
work need not make them do so. |
|
||||||
|
|
||||||
A compilation of a covered work with other separate and independent |
|
||||||
works, which are not by their nature extensions of the covered work, |
|
||||||
and which are not combined with it such as to form a larger program, |
|
||||||
in or on a volume of a storage or distribution medium, is called an |
|
||||||
"aggregate" if the compilation and its resulting copyright are not |
|
||||||
used to limit the access or legal rights of the compilation's users |
|
||||||
beyond what the individual works permit. Inclusion of a covered work |
|
||||||
in an aggregate does not cause this License to apply to the other |
|
||||||
parts of the aggregate. |
|
||||||
|
|
||||||
6. Conveying Non-Source Forms. |
|
||||||
|
|
||||||
You may convey a covered work in object code form under the terms |
|
||||||
of sections 4 and 5, provided that you also convey the |
|
||||||
machine-readable Corresponding Source under the terms of this License, |
|
||||||
in one of these ways: |
|
||||||
|
|
||||||
a) Convey the object code in, or embodied in, a physical product |
|
||||||
(including a physical distribution medium), accompanied by the |
|
||||||
Corresponding Source fixed on a durable physical medium |
|
||||||
customarily used for software interchange. |
|
||||||
|
|
||||||
b) Convey the object code in, or embodied in, a physical product |
|
||||||
(including a physical distribution medium), accompanied by a |
|
||||||
written offer, valid for at least three years and valid for as |
|
||||||
long as you offer spare parts or customer support for that product |
|
||||||
model, to give anyone who possesses the object code either (1) a |
|
||||||
copy of the Corresponding Source for all the software in the |
|
||||||
product that is covered by this License, on a durable physical |
|
||||||
medium customarily used for software interchange, for a price no |
|
||||||
more than your reasonable cost of physically performing this |
|
||||||
conveying of source, or (2) access to copy the |
|
||||||
Corresponding Source from a network server at no charge. |
|
||||||
|
|
||||||
c) Convey individual copies of the object code with a copy of the |
|
||||||
written offer to provide the Corresponding Source. This |
|
||||||
alternative is allowed only occasionally and noncommercially, and |
|
||||||
only if you received the object code with such an offer, in accord |
|
||||||
with subsection 6b. |
|
||||||
|
|
||||||
d) Convey the object code by offering access from a designated |
|
||||||
place (gratis or for a charge), and offer equivalent access to the |
|
||||||
Corresponding Source in the same way through the same place at no |
|
||||||
further charge. You need not require recipients to copy the |
|
||||||
Corresponding Source along with the object code. If the place to |
|
||||||
copy the object code is a network server, the Corresponding Source |
|
||||||
may be on a different server (operated by you or a third party) |
|
||||||
that supports equivalent copying facilities, provided you maintain |
|
||||||
clear directions next to the object code saying where to find the |
|
||||||
Corresponding Source. Regardless of what server hosts the |
|
||||||
Corresponding Source, you remain obligated to ensure that it is |
|
||||||
available for as long as needed to satisfy these requirements. |
|
||||||
|
|
||||||
e) Convey the object code using peer-to-peer transmission, provided |
|
||||||
you inform other peers where the object code and Corresponding |
|
||||||
Source of the work are being offered to the general public at no |
|
||||||
charge under subsection 6d. |
|
||||||
|
|
||||||
A separable portion of the object code, whose source code is excluded |
|
||||||
from the Corresponding Source as a System Library, need not be |
|
||||||
included in conveying the object code work. |
|
||||||
|
|
||||||
A "User Product" is either (1) a "consumer product", which means any |
|
||||||
tangible personal property which is normally used for personal, family, |
|
||||||
or household purposes, or (2) anything designed or sold for incorporation |
|
||||||
into a dwelling. In determining whether a product is a consumer product, |
|
||||||
doubtful cases shall be resolved in favor of coverage. For a particular |
|
||||||
product received by a particular user, "normally used" refers to a |
|
||||||
typical or common use of that class of product, regardless of the status |
|
||||||
of the particular user or of the way in which the particular user |
|
||||||
actually uses, or expects or is expected to use, the product. A product |
|
||||||
is a consumer product regardless of whether the product has substantial |
|
||||||
commercial, industrial or non-consumer uses, unless such uses represent |
|
||||||
the only significant mode of use of the product. |
|
||||||
|
|
||||||
"Installation Information" for a User Product means any methods, |
|
||||||
procedures, authorization keys, or other information required to install |
|
||||||
and execute modified versions of a covered work in that User Product from |
|
||||||
a modified version of its Corresponding Source. The information must |
|
||||||
suffice to ensure that the continued functioning of the modified object |
|
||||||
code is in no case prevented or interfered with solely because |
|
||||||
modification has been made. |
|
||||||
|
|
||||||
If you convey an object code work under this section in, or with, or |
|
||||||
specifically for use in, a User Product, and the conveying occurs as |
|
||||||
part of a transaction in which the right of possession and use of the |
|
||||||
User Product is transferred to the recipient in perpetuity or for a |
|
||||||
fixed term (regardless of how the transaction is characterized), the |
|
||||||
Corresponding Source conveyed under this section must be accompanied |
|
||||||
by the Installation Information. But this requirement does not apply |
|
||||||
if neither you nor any third party retains the ability to install |
|
||||||
modified object code on the User Product (for example, the work has |
|
||||||
been installed in ROM). |
|
||||||
|
|
||||||
The requirement to provide Installation Information does not include a |
|
||||||
requirement to continue to provide support service, warranty, or updates |
|
||||||
for a work that has been modified or installed by the recipient, or for |
|
||||||
the User Product in which it has been modified or installed. Access to a |
|
||||||
network may be denied when the modification itself materially and |
|
||||||
adversely affects the operation of the network or violates the rules and |
|
||||||
protocols for communication across the network. |
|
||||||
|
|
||||||
Corresponding Source conveyed, and Installation Information provided, |
|
||||||
in accord with this section must be in a format that is publicly |
|
||||||
documented (and with an implementation available to the public in |
|
||||||
source code form), and must require no special password or key for |
|
||||||
unpacking, reading or copying. |
|
||||||
|
|
||||||
7. Additional Terms. |
|
||||||
|
|
||||||
"Additional permissions" are terms that supplement the terms of this |
|
||||||
License by making exceptions from one or more of its conditions. |
|
||||||
Additional permissions that are applicable to the entire Program shall |
|
||||||
be treated as though they were included in this License, to the extent |
|
||||||
that they are valid under applicable law. If additional permissions |
|
||||||
apply only to part of the Program, that part may be used separately |
|
||||||
under those permissions, but the entire Program remains governed by |
|
||||||
this License without regard to the additional permissions. |
|
||||||
|
|
||||||
When you convey a copy of a covered work, you may at your option |
|
||||||
remove any additional permissions from that copy, or from any part of |
|
||||||
it. (Additional permissions may be written to require their own |
|
||||||
removal in certain cases when you modify the work.) You may place |
|
||||||
additional permissions on material, added by you to a covered work, |
|
||||||
for which you have or can give appropriate copyright permission. |
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, for material you |
|
||||||
add to a covered work, you may (if authorized by the copyright holders of |
|
||||||
that material) supplement the terms of this License with terms: |
|
||||||
|
|
||||||
a) Disclaiming warranty or limiting liability differently from the |
|
||||||
terms of sections 15 and 16 of this License; or |
|
||||||
|
|
||||||
b) Requiring preservation of specified reasonable legal notices or |
|
||||||
author attributions in that material or in the Appropriate Legal |
|
||||||
Notices displayed by works containing it; or |
|
||||||
|
|
||||||
c) Prohibiting misrepresentation of the origin of that material, or |
|
||||||
requiring that modified versions of such material be marked in |
|
||||||
reasonable ways as different from the original version; or |
|
||||||
|
|
||||||
d) Limiting the use for publicity purposes of names of licensors or |
|
||||||
authors of the material; or |
|
||||||
|
|
||||||
e) Declining to grant rights under trademark law for use of some |
|
||||||
trade names, trademarks, or service marks; or |
|
||||||
|
|
||||||
f) Requiring indemnification of licensors and authors of that |
|
||||||
material by anyone who conveys the material (or modified versions of |
|
||||||
it) with contractual assumptions of liability to the recipient, for |
|
||||||
any liability that these contractual assumptions directly impose on |
|
||||||
those licensors and authors. |
|
||||||
|
|
||||||
All other non-permissive additional terms are considered "further |
|
||||||
restrictions" within the meaning of section 10. If the Program as you |
|
||||||
received it, or any part of it, contains a notice stating that it is |
|
||||||
governed by this License along with a term that is a further |
|
||||||
restriction, you may remove that term. If a license document contains |
|
||||||
a further restriction but permits relicensing or conveying under this |
|
||||||
License, you may add to a covered work material governed by the terms |
|
||||||
of that license document, provided that the further restriction does |
|
||||||
not survive such relicensing or conveying. |
|
||||||
|
|
||||||
If you add terms to a covered work in accord with this section, you |
|
||||||
must place, in the relevant source files, a statement of the |
|
||||||
additional terms that apply to those files, or a notice indicating |
|
||||||
where to find the applicable terms. |
|
||||||
|
|
||||||
Additional terms, permissive or non-permissive, may be stated in the |
|
||||||
form of a separately written license, or stated as exceptions; |
|
||||||
the above requirements apply either way. |
|
||||||
|
|
||||||
8. Termination. |
|
||||||
|
|
||||||
You may not propagate or modify a covered work except as expressly |
|
||||||
provided under this License. Any attempt otherwise to propagate or |
|
||||||
modify it is void, and will automatically terminate your rights under |
|
||||||
this License (including any patent licenses granted under the third |
|
||||||
paragraph of section 11). |
|
||||||
|
|
||||||
However, if you cease all violation of this License, then your |
|
||||||
license from a particular copyright holder is reinstated (a) |
|
||||||
provisionally, unless and until the copyright holder explicitly and |
|
||||||
finally terminates your license, and (b) permanently, if the copyright |
|
||||||
holder fails to notify you of the violation by some reasonable means |
|
||||||
prior to 60 days after the cessation. |
|
||||||
|
|
||||||
Moreover, your license from a particular copyright holder is |
|
||||||
reinstated permanently if the copyright holder notifies you of the |
|
||||||
violation by some reasonable means, this is the first time you have |
|
||||||
received notice of violation of this License (for any work) from that |
|
||||||
copyright holder, and you cure the violation prior to 30 days after |
|
||||||
your receipt of the notice. |
|
||||||
|
|
||||||
Termination of your rights under this section does not terminate the |
|
||||||
licenses of parties who have received copies or rights from you under |
|
||||||
this License. If your rights have been terminated and not permanently |
|
||||||
reinstated, you do not qualify to receive new licenses for the same |
|
||||||
material under section 10. |
|
||||||
|
|
||||||
9. Acceptance Not Required for Having Copies. |
|
||||||
|
|
||||||
You are not required to accept this License in order to receive or |
|
||||||
run a copy of the Program. Ancillary propagation of a covered work |
|
||||||
occurring solely as a consequence of using peer-to-peer transmission |
|
||||||
to receive a copy likewise does not require acceptance. However, |
|
||||||
nothing other than this License grants you permission to propagate or |
|
||||||
modify any covered work. These actions infringe copyright if you do |
|
||||||
not accept this License. Therefore, by modifying or propagating a |
|
||||||
covered work, you indicate your acceptance of this License to do so. |
|
||||||
|
|
||||||
10. Automatic Licensing of Downstream Recipients. |
|
||||||
|
|
||||||
Each time you convey a covered work, the recipient automatically |
|
||||||
receives a license from the original licensors, to run, modify and |
|
||||||
propagate that work, subject to this License. You are not responsible |
|
||||||
for enforcing compliance by third parties with this License. |
|
||||||
|
|
||||||
An "entity transaction" is a transaction transferring control of an |
|
||||||
organization, or substantially all assets of one, or subdividing an |
|
||||||
organization, or merging organizations. If propagation of a covered |
|
||||||
work results from an entity transaction, each party to that |
|
||||||
transaction who receives a copy of the work also receives whatever |
|
||||||
licenses to the work the party's predecessor in interest had or could |
|
||||||
give under the previous paragraph, plus a right to possession of the |
|
||||||
Corresponding Source of the work from the predecessor in interest, if |
|
||||||
the predecessor has it or can get it with reasonable efforts. |
|
||||||
|
|
||||||
You may not impose any further restrictions on the exercise of the |
|
||||||
rights granted or affirmed under this License. For example, you may |
|
||||||
not impose a license fee, royalty, or other charge for exercise of |
|
||||||
rights granted under this License, and you may not initiate litigation |
|
||||||
(including a cross-claim or counterclaim in a lawsuit) alleging that |
|
||||||
any patent claim is infringed by making, using, selling, offering for |
|
||||||
sale, or importing the Program or any portion of it. |
|
||||||
|
|
||||||
11. Patents. |
|
||||||
|
|
||||||
A "contributor" is a copyright holder who authorizes use under this |
|
||||||
License of the Program or a work on which the Program is based. The |
|
||||||
work thus licensed is called the contributor's "contributor version". |
|
||||||
|
|
||||||
A contributor's "essential patent claims" are all patent claims |
|
||||||
owned or controlled by the contributor, whether already acquired or |
|
||||||
hereafter acquired, that would be infringed by some manner, permitted |
|
||||||
by this License, of making, using, or selling its contributor version, |
|
||||||
but do not include claims that would be infringed only as a |
|
||||||
consequence of further modification of the contributor version. For |
|
||||||
purposes of this definition, "control" includes the right to grant |
|
||||||
patent sublicenses in a manner consistent with the requirements of |
|
||||||
this License. |
|
||||||
|
|
||||||
Each contributor grants you a non-exclusive, worldwide, royalty-free |
|
||||||
patent license under the contributor's essential patent claims, to |
|
||||||
make, use, sell, offer for sale, import and otherwise run, modify and |
|
||||||
propagate the contents of its contributor version. |
|
||||||
|
|
||||||
In the following three paragraphs, a "patent license" is any express |
|
||||||
agreement or commitment, however denominated, not to enforce a patent |
|
||||||
(such as an express permission to practice a patent or covenant not to |
|
||||||
sue for patent infringement). To "grant" such a patent license to a |
|
||||||
party means to make such an agreement or commitment not to enforce a |
|
||||||
patent against the party. |
|
||||||
|
|
||||||
If you convey a covered work, knowingly relying on a patent license, |
|
||||||
and the Corresponding Source of the work is not available for anyone |
|
||||||
to copy, free of charge and under the terms of this License, through a |
|
||||||
publicly available network server or other readily accessible means, |
|
||||||
then you must either (1) cause the Corresponding Source to be so |
|
||||||
available, or (2) arrange to deprive yourself of the benefit of the |
|
||||||
patent license for this particular work, or (3) arrange, in a manner |
|
||||||
consistent with the requirements of this License, to extend the patent |
|
||||||
license to downstream recipients. "Knowingly relying" means you have |
|
||||||
actual knowledge that, but for the patent license, your conveying the |
|
||||||
covered work in a country, or your recipient's use of the covered work |
|
||||||
in a country, would infringe one or more identifiable patents in that |
|
||||||
country that you have reason to believe are valid. |
|
||||||
|
|
||||||
If, pursuant to or in connection with a single transaction or |
|
||||||
arrangement, you convey, or propagate by procuring conveyance of, a |
|
||||||
covered work, and grant a patent license to some of the parties |
|
||||||
receiving the covered work authorizing them to use, propagate, modify |
|
||||||
or convey a specific copy of the covered work, then the patent license |
|
||||||
you grant is automatically extended to all recipients of the covered |
|
||||||
work and works based on it. |
|
||||||
|
|
||||||
A patent license is "discriminatory" if it does not include within |
|
||||||
the scope of its coverage, prohibits the exercise of, or is |
|
||||||
conditioned on the non-exercise of one or more of the rights that are |
|
||||||
specifically granted under this License. You may not convey a covered |
|
||||||
work if you are a party to an arrangement with a third party that is |
|
||||||
in the business of distributing software, under which you make payment |
|
||||||
to the third party based on the extent of your activity of conveying |
|
||||||
the work, and under which the third party grants, to any of the |
|
||||||
parties who would receive the covered work from you, a discriminatory |
|
||||||
patent license (a) in connection with copies of the covered work |
|
||||||
conveyed by you (or copies made from those copies), or (b) primarily |
|
||||||
for and in connection with specific products or compilations that |
|
||||||
contain the covered work, unless you entered into that arrangement, |
|
||||||
or that patent license was granted, prior to 28 March 2007. |
|
||||||
|
|
||||||
Nothing in this License shall be construed as excluding or limiting |
|
||||||
any implied license or other defenses to infringement that may |
|
||||||
otherwise be available to you under applicable patent law. |
|
||||||
|
|
||||||
12. No Surrender of Others' Freedom. |
|
||||||
|
|
||||||
If conditions are imposed on you (whether by court order, agreement or |
|
||||||
otherwise) that contradict the conditions of this License, they do not |
|
||||||
excuse you from the conditions of this License. If you cannot convey a |
|
||||||
covered work so as to satisfy simultaneously your obligations under this |
|
||||||
License and any other pertinent obligations, then as a consequence you may |
|
||||||
not convey it at all. For example, if you agree to terms that obligate you |
|
||||||
to collect a royalty for further conveying from those to whom you convey |
|
||||||
the Program, the only way you could satisfy both those terms and this |
|
||||||
License would be to refrain entirely from conveying the Program. |
|
||||||
|
|
||||||
13. Use with the GNU Affero General Public License. |
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, you have |
|
||||||
permission to link or combine any covered work with a work licensed |
|
||||||
under version 3 of the GNU Affero General Public License into a single |
|
||||||
combined work, and to convey the resulting work. The terms of this |
|
||||||
License will continue to apply to the part which is the covered work, |
|
||||||
but the special requirements of the GNU Affero General Public License, |
|
||||||
section 13, concerning interaction through a network will apply to the |
|
||||||
combination as such. |
|
||||||
|
|
||||||
14. Revised Versions of this License. |
|
||||||
|
|
||||||
The Free Software Foundation may publish revised and/or new versions of |
|
||||||
the GNU General Public License from time to time. Such new versions will |
|
||||||
be similar in spirit to the present version, but may differ in detail to |
|
||||||
address new problems or concerns. |
|
||||||
|
|
||||||
Each version is given a distinguishing version number. If the |
|
||||||
Program specifies that a certain numbered version of the GNU General |
|
||||||
Public License "or any later version" applies to it, you have the |
|
||||||
option of following the terms and conditions either of that numbered |
|
||||||
version or of any later version published by the Free Software |
|
||||||
Foundation. If the Program does not specify a version number of the |
|
||||||
GNU General Public License, you may choose any version ever published |
|
||||||
by the Free Software Foundation. |
|
||||||
|
|
||||||
If the Program specifies that a proxy can decide which future |
|
||||||
versions of the GNU General Public License can be used, that proxy's |
|
||||||
public statement of acceptance of a version permanently authorizes you |
|
||||||
to choose that version for the Program. |
|
||||||
|
|
||||||
Later license versions may give you additional or different |
|
||||||
permissions. However, no additional obligations are imposed on any |
|
||||||
author or copyright holder as a result of your choosing to follow a |
|
||||||
later version. |
|
||||||
|
|
||||||
15. Disclaimer of Warranty. |
|
||||||
|
|
||||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY |
|
||||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT |
|
||||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY |
|
||||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, |
|
||||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
||||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM |
|
||||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF |
|
||||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION. |
|
||||||
|
|
||||||
16. Limitation of Liability. |
|
||||||
|
|
||||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
|
||||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS |
|
||||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY |
|
||||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE |
|
||||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF |
|
||||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD |
|
||||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), |
|
||||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
|
||||||
SUCH DAMAGES. |
|
||||||
|
|
||||||
17. Interpretation of Sections 15 and 16. |
|
||||||
|
|
||||||
If the disclaimer of warranty and limitation of liability provided |
|
||||||
above cannot be given local legal effect according to their terms, |
|
||||||
reviewing courts shall apply local law that most closely approximates |
|
||||||
an absolute waiver of all civil liability in connection with the |
|
||||||
Program, unless a warranty or assumption of liability accompanies a |
|
||||||
copy of the Program in return for a fee. |
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS |
|
||||||
|
|
||||||
How to Apply These Terms to Your New Programs |
|
||||||
|
|
||||||
If you develop a new program, and you want it to be of the greatest |
|
||||||
possible use to the public, the best way to achieve this is to make it |
|
||||||
free software which everyone can redistribute and change under these terms. |
|
||||||
|
|
||||||
To do so, attach the following notices to the program. It is safest |
|
||||||
to attach them to the start of each source file to most effectively |
|
||||||
state the exclusion of warranty; and each file should have at least |
|
||||||
the "copyright" line and a pointer to where the full notice is found. |
|
||||||
|
|
||||||
<one line to give the program's name and a brief idea of what it does.> |
|
||||||
Copyright (C) <year> <name of author> |
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify |
|
||||||
it under the terms of the GNU General Public License as published by |
|
||||||
the Free Software Foundation, either version 3 of the License, or |
|
||||||
(at your option) any later version. |
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, |
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||||
GNU General Public License for more details. |
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
||||||
|
|
||||||
Also add information on how to contact you by electronic and paper mail. |
|
||||||
|
|
||||||
If the program does terminal interaction, make it output a short |
|
||||||
notice like this when it starts in an interactive mode: |
|
||||||
|
|
||||||
<program> Copyright (C) <year> <name of author> |
|
||||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. |
|
||||||
This is free software, and you are welcome to redistribute it |
|
||||||
under certain conditions; type `show c' for details. |
|
||||||
|
|
||||||
The hypothetical commands `show w' and `show c' should show the appropriate |
|
||||||
parts of the General Public License. Of course, your program's commands |
|
||||||
might be different; for a GUI interface, you would use an "about box". |
|
||||||
|
|
||||||
You should also get your employer (if you work as a programmer) or school, |
|
||||||
if any, to sign a "copyright disclaimer" for the program, if necessary. |
|
||||||
For more information on this, and how to apply and follow the GNU GPL, see |
|
||||||
<https://www.gnu.org/licenses/>. |
|
||||||
|
|
||||||
The GNU General Public License does not permit incorporating your program |
|
||||||
into proprietary programs. If your program is a subroutine library, you |
|
||||||
may consider it more useful to permit linking proprietary applications with |
|
||||||
the library. If this is what you want to do, use the GNU Lesser General |
|
||||||
Public License instead of this License. But first, please read |
|
||||||
<https://www.gnu.org/licenses/why-not-lgpl.html>. |
|
||||||
@ -1,430 +0,0 @@ |
|||||||
[简体中文](README-CN.md) | [English](README.md) |
|
||||||
|
|
||||||
<div align="center"> |
|
||||||
|
|
||||||
# TypePHP |
|
||||||
|
|
||||||
**PHP 原生 AOT 编译器** |
|
||||||
|
|
||||||
将 PHP 源码提前(AOT)编译为原生机器码,生成独立的可执行文件、PHP 扩展和静态库, |
|
||||||
同时保留你熟悉的 PHP 语法。 |
|
||||||
|
|
||||||
</div> |
|
||||||
|
|
||||||
--- |
|
||||||
|
|
||||||
## 什么是 TypePHP? |
|
||||||
|
|
||||||
TypePHP 是一个 AOT(Ahead-Of-Time,提前编译)编译器,它把 PHP 源码翻译为 C++, |
|
||||||
再编译为原生机器码。与字节码缓存或虚拟机不同,它不会在运行时解释 opcode, |
|
||||||
而是直接生成在 CPU 上运行的原生二进制。 |
|
||||||
|
|
||||||
它保留熟悉的 PHP 语法,同时引入编译期类型信息,让编译器为你的性能热点生成快速、 |
|
||||||
静态类型的 C++ 代码,而其余代码仍运行在久经考验的 Zend 引擎上。 |
|
||||||
|
|
||||||
## 特性 |
|
||||||
|
|
||||||
- **真正的 AOT 编译** —— PHP 先降级为 C++17,再编译为原生机器码。无解释器、 |
|
||||||
无 opcode 缓存、无 JIT 预热。 |
|
||||||
- **三种构建模式** —— 同一份代码可编译为独立 `bin` 可执行文件、可加载的 PHP |
|
||||||
`ext` 扩展,或 `lib` 静态库。 |
|
||||||
- **原生类型系统** —— `int`、`float`、`bool` 直接映射为 C++ 标量类型 |
|
||||||
(`int64_t`、`double`、`bool`),数值代码可获得数量级的性能提升。 |
|
||||||
- **高精度数值** —— `bigInt`(GMP)、`decimal`(libmpdec)、`bigFloat`(MPFR), |
|
||||||
零开销算术运算。 |
|
||||||
- **强类型容器** —— `std::array`、`std::vector`、`std::map`、`std::ordered_map`, |
|
||||||
元素类型在编译期确定;最高比 PHP 数组快 **10 倍**,性能与 C++ `std::vector` 相当。 |
|
||||||
- **通用方法(Universal Methods)** —— 在原生类型上直接调用方法 |
|
||||||
(`$s->upper()`、`$arr->contains()`、`$big->mul(2)`),零运行时派发开销。 |
|
||||||
- **混合 C++ / PHP 编程** —— 在性能关键内核中直接调用 C++ 函数(反之亦然)。 |
|
||||||
- **编译期函数与关键词** —— `any()`、`refval()`、`objval()`、`expected()`、 |
|
||||||
`unexpected()`,以及 `toInt()`、`toString()`、`toArray()` 等。 |
|
||||||
- **编译期安全检查** —— `#[Immutable]` 只读契约和 `#[ArrayDef]` 数组结构元数据, |
|
||||||
在编译期检查,零运行时开销。 |
|
||||||
- **现代 PHP 支持** —— PHP 8.4 property hooks、非对称可见性、PHP 8.5 |
|
||||||
`clone()`-with 以及 `(void)` 丢弃表达式。 |
|
||||||
- **跨平台与 WASM** —— 面向 x86-64 和 ARM64 的 Linux、Windows、macOS 目标, |
|
||||||
以及 WASI 0.2 和浏览器(Jco)输出。 |
|
||||||
- **Python 桥接** —— 为 Python 模块生成 IDE helper,并将 Python 脚本转换为 TypePHP。 |
|
||||||
|
|
||||||
## 为什么选择 TypePHP? |
|
||||||
|
|
||||||
| | TypePHP AOT | 字节码缓存(OPcache) | JIT(PHP 8+) | |
|
||||||
|---|---|---|---| |
|
||||||
| 编译目标 | 原生机器码 | 字节码 | 机器码(trace) | |
|
||||||
| 启动 / 预热 | 无(已编译完成) | 每进程预热 | JIT 预热 | |
|
||||||
| 类型驱动优化 | 编译期、全程序 | 无 | 有限,基于 trace | |
|
||||||
| 独立可执行文件 | 支持 | 不支持 | 不支持 | |
|
||||||
| 源码保护 | 编译为机器码 | 字节码(可还原) | 字节码(可还原) | |
|
||||||
| 性能确定性 | 是 | 否 | 否 | |
|
||||||
|
|
||||||
**相较原生 PHP 的优势:** |
|
||||||
|
|
||||||
- **接近原生的性能。** 数值密集和容器密集的热点路径会编译为与 C++ 程序相同的机器码。 |
|
||||||
见下方[基准测试](#基准测试)。 |
|
||||||
- **源码保护。** 源码被编译掉——交付物是原生二进制,而不是可读的 PHP 文件。 |
|
||||||
- **零依赖部署。** 二进制模式生成单个自包含可执行文件,无需 PHP 运行时即可运行。 |
|
||||||
- **渐进式类型,真正带来收益。** 只在性能关键处添加 `use native_types`、`std::` |
|
||||||
容器和类型声明,其余保持普通 PHP。 |
|
||||||
- **完整 PHP 生态互通。** 扩展模式以标准 PHP 扩展形式加载到 `php-fpm`, |
|
||||||
现有框架和工具链可继续使用。 |
|
||||||
|
|
||||||
## 前置要求 |
|
||||||
|
|
||||||
- **PHP 8.4 – 8.5**,需包含 `embed` 模块(`libphp.so`) |
|
||||||
- **GCC 9+**(或 Clang),支持 **C++17** |
|
||||||
- **CMake 3.24+** |
|
||||||
- 高精度数学库:**GMP**、**MPFR**、**libmpdec** |
|
||||||
|
|
||||||
```shell |
|
||||||
# Ubuntu/Debian |
|
||||||
sudo apt install libgmp-dev libmpfr-dev libmpdec-dev |
|
||||||
|
|
||||||
# RHEL/CentOS/Fedora |
|
||||||
sudo dnf install gmp-devel mpfr-devel libmpdec-devel |
|
||||||
|
|
||||||
# Arch Linux |
|
||||||
sudo pacman -S gmp mpfr mpdecimal |
|
||||||
``` |
|
||||||
|
|
||||||
> GMP 用于 `bigInt`,MPFR 用于 `bigFloat`,libmpdec 用于 `decimal`。 |
|
||||||
|
|
||||||
预览版目前以 **Linux** 为主要开发平台(推荐 Ubuntu 22.04)。Windows 和 macOS |
|
||||||
打包通过同一入口点支持。 |
|
||||||
|
|
||||||
## 安装 |
|
||||||
|
|
||||||
### 通过 Composer |
|
||||||
|
|
||||||
```bash |
|
||||||
composer require --dev swoole/typephp |
|
||||||
``` |
|
||||||
|
|
||||||
然后编译你的项目: |
|
||||||
|
|
||||||
```bash |
|
||||||
vendor/bin/tpc.php project.yml |
|
||||||
``` |
|
||||||
|
|
||||||
在 TypePHP 源码仓库中开发时,改用本地入口: |
|
||||||
|
|
||||||
```bash |
|
||||||
bin/tpc.php project.yml |
|
||||||
``` |
|
||||||
|
|
||||||
### 构建 `libphp.so` |
|
||||||
|
|
||||||
`tpc` 需要以 `embed` SAPI 构建的 PHP。如果 Linux 上缺少 `libphp.so`, |
|
||||||
`tpc.php` 可以交互式下载 PHP 源码并自动构建。详见 |
|
||||||
[自动构建 libphp.so](docs/LIBPHP_INSTALLER.md)。 |
|
||||||
|
|
||||||
## 快速开始 |
|
||||||
|
|
||||||
创建 `hello.php`: |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
echo "Hello World!\n"; |
|
||||||
var_dump(PHP_VERSION); |
|
||||||
var_dump(php_uname()); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
编译并运行: |
|
||||||
|
|
||||||
```bash |
|
||||||
bin/tpc.php hello.php |
|
||||||
./hello |
|
||||||
``` |
|
||||||
|
|
||||||
输出: |
|
||||||
|
|
||||||
``` |
|
||||||
Hello World! |
|
||||||
string(5) "8.4.x" |
|
||||||
string(16) "Linux ..." |
|
||||||
``` |
|
||||||
|
|
||||||
> 二进制模式需要全局 `main()` 函数。它可以声明为无参数,或 |
|
||||||
> `main(int $argc, array $argv)` 以接收命令行参数,且必须返回 `void`。 |
|
||||||
|
|
||||||
## 编译模式 |
|
||||||
|
|
||||||
TypePHP 支持三种构建模式,通过 `-m` / `--mode` 选择: |
|
||||||
|
|
||||||
| 模式 | 参数 | 输出 | 需要 `main()` | 典型用途 | |
|
||||||
|---|---|---|---|---| |
|
||||||
| 二进制 | `-m bin`(默认) | 可执行文件 | 是 | CLI 工具、常驻服务、独立应用 | |
|
||||||
| 扩展 | `-m ext` | `.so` / `.dll` | 否 | `php-fpm` 上的 Web 应用、即插即用 PHP 扩展 | |
|
||||||
| 库 | `-m lib` | 静态库 | 否 | 将编译后的代码嵌入其他项目 | |
|
||||||
|
|
||||||
```bash |
|
||||||
# 二进制(默认) |
|
||||||
bin/tpc.php app.php -o myapp |
|
||||||
|
|
||||||
# PHP 扩展 |
|
||||||
bin/tpc.php extension/ -m ext -o my_extension |
|
||||||
|
|
||||||
# 静态库 |
|
||||||
bin/tpc.php lib/ -m lib -o mylib |
|
||||||
``` |
|
||||||
|
|
||||||
详见[编译模式](docs/COMPILATION_MODES.md)。 |
|
||||||
|
|
||||||
## 使用示例 |
|
||||||
|
|
||||||
### 1. 原生类型 —— 编译期数值加速 |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
use native_types; |
|
||||||
|
|
||||||
function fib(int $n): int |
|
||||||
{ |
|
||||||
if ($n == 1 || $n == 2) { |
|
||||||
return 1; |
|
||||||
} |
|
||||||
return fib($n - 1) + fib($n - 2); |
|
||||||
} |
|
||||||
|
|
||||||
function main(int $argc, array $argv): void |
|
||||||
{ |
|
||||||
$n = (int)$argv[1]; |
|
||||||
$begin = microtime(true); |
|
||||||
echo fib($n) . "\n"; |
|
||||||
echo "Time: " . (microtime(true) - $begin) . "\n"; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
```bash |
|
||||||
bin/tpc.php fib.php -O3 -o fib |
|
||||||
./fib 30 |
|
||||||
``` |
|
||||||
|
|
||||||
使用 `use native_types` 后,`int` 变量变为 C++ `int64_t`,算术运算直接编译为 |
|
||||||
CPU 指令,而不是 ZendVM 调用。 |
|
||||||
|
|
||||||
### 2. 高精度数值 |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
declare(strict_types=1); |
|
||||||
use native_types; |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
// 54 位整数 —— 自动识别并存储为 bigInt |
|
||||||
$a = std::bigInt("123456789012345678901234567890123456789012345678901234"); |
|
||||||
$b = std::bigInt("987654321098765432109876543210987654321098765432109876"); |
|
||||||
|
|
||||||
echo $a->add($b)->toString() . "\n"; // 精确计算,不会溢出 |
|
||||||
|
|
||||||
// 精确的十进制运算 —— 无二进制浮点误差 |
|
||||||
$c = std::decimal("0.1")->add(std::decimal("0.2")); |
|
||||||
echo $c->toString() . "\n"; // "0.3" |
|
||||||
|
|
||||||
// 256 位浮点数 |
|
||||||
$pi = std::bigFloat("3.14159265358979323846264338327950288419716939937510"); |
|
||||||
echo $pi->mul(2)->toString() . "\n"; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
详见[高精度类型](docs/HIGH_PRECISION_TYPES.md)和[原生类型](docs/NATIVE_TYPES.md)。 |
|
||||||
|
|
||||||
### 3. 强类型容器 |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
use native_types; |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
$vector = std::vector(Type::Int); |
|
||||||
|
|
||||||
$vector[] = 1; |
|
||||||
$vector[] = 2; |
|
||||||
$vector[] = 3; |
|
||||||
|
|
||||||
$sum = 0; |
|
||||||
foreach ($vector as $value) { |
|
||||||
$sum += $value; |
|
||||||
} |
|
||||||
|
|
||||||
echo $sum . "\n"; // 6 |
|
||||||
echo $vector[1] . "\n"; // 2 |
|
||||||
|
|
||||||
// 固定 key/value 类型的映射 |
|
||||||
$map = std::ordered_map(Type::String, Type::Int); |
|
||||||
$map["a"] = 1; |
|
||||||
$map["b"] = 2; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
详见 [Std 容器](docs/STD_CONTAINERS.md)。 |
|
||||||
|
|
||||||
### 4. 通用方法 |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
$s = "hello world"; |
|
||||||
echo $s->length() . "\n"; // strlen() |
|
||||||
echo $s->upper() . "\n"; // strtoupper() |
|
||||||
echo $s->substr(0, 5) . "\n"; // substr() |
|
||||||
|
|
||||||
$arr = [1, 3, 5, 7, 9]; |
|
||||||
echo $arr->count() . "\n"; // count() |
|
||||||
var_dump($arr->contains(3)); // in_array() |
|
||||||
|
|
||||||
$big = std::bigInt("12345678901234567890"); |
|
||||||
echo $big->mul(2)->toString() . "\n"; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
原生类型上的方法调用在编译期被解析为直接的 C/C++ 函数调用——没有虚表查找、 |
|
||||||
没有反射、没有运行时派发。详见[通用方法](docs/UNIVERSAL_METHODS.md)。 |
|
||||||
|
|
||||||
### 5. 混合 C++ / PHP |
|
||||||
|
|
||||||
用 C++ 编写性能关键内核,并在 PHP 中调用: |
|
||||||
|
|
||||||
```cpp |
|
||||||
// math.cpp |
|
||||||
#include <phpx.h> |
|
||||||
|
|
||||||
using namespace php; |
|
||||||
|
|
||||||
var php_fast_sum(Int a, Int b) { |
|
||||||
return a + b; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
// math.stub.php —— 声明 C++ 函数签名 |
|
||||||
function fast_sum(int $a, int $b): int; |
|
||||||
``` |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
echo fast_sum(3, 4) . "\n"; // 7 |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
详见[混合 C++/PHP](docs/MIXED_CPP_PHP.md)。 |
|
||||||
|
|
||||||
## 基准测试 |
|
||||||
|
|
||||||
一个 10000×100000 的元素累加循环,对比 PHP 数组、TypePHP `std::array` |
|
||||||
与原生 C++: |
|
||||||
|
|
||||||
| 实现 | 耗时 | |
|
||||||
|---|---| |
|
||||||
| PHP 数组(JIT) | 67.6 秒 | |
|
||||||
| `std::array`(TypePHP AOT) | **6.4 秒** | |
|
||||||
| C++ `std::vector` | 6.2 秒 | |
|
||||||
|
|
||||||
`std::array` 比 PHP 数组快约 **10 倍**,性能与手写 C++ 完全一致。 |
|
||||||
完整基准测试见 [Std 容器](docs/STD_CONTAINERS.md)。 |
|
||||||
|
|
||||||
## 命令行 |
|
||||||
|
|
||||||
```bash |
|
||||||
bin/tpc.php <file|dir|project.yml> [options] [-- program-args...] |
|
||||||
``` |
|
||||||
|
|
||||||
常用示例: |
|
||||||
|
|
||||||
```bash |
|
||||||
# 编译单个文件 |
|
||||||
bin/tpc.php app.php |
|
||||||
|
|
||||||
# 优化并运行,`--` 后的参数传给生成的程序 |
|
||||||
bin/tpc.php app.php -O3 -r -- --flag value |
|
||||||
|
|
||||||
# 编译 project.yml 定义的项目 |
|
||||||
bin/tpc.php project.yml -O2 -j 8 |
|
||||||
|
|
||||||
# 生成 PHP 扩展 |
|
||||||
bin/tpc.php extension/ -m ext -o my_extension |
|
||||||
|
|
||||||
# 只生成 C++(跳过编译与链接) |
|
||||||
bin/tpc.php app.php --dry --build-dir /tmp/typephp-build |
|
||||||
|
|
||||||
# 编译为 WASI 0.2 |
|
||||||
bin/tpc.php --wasm app.php |
|
||||||
|
|
||||||
# 编译为浏览器目标(需要 jco) |
|
||||||
bin/tpc.php --wasm=browser app.php |
|
||||||
``` |
|
||||||
|
|
||||||
主要选项: |
|
||||||
|
|
||||||
| 选项 | 说明 | |
|
||||||
|---|---| |
|
||||||
| `-O <0-3>` | 优化级别(默认 `0`) | |
|
||||||
| `-d`, `--debug` | 调试构建,带符号和源码跟踪 | |
|
||||||
| `-o`, `--output <file>` | 输出文件名 | |
|
||||||
| `-m`, `--mode <bin\|lib\|ext>` | 构建模式(默认 `bin`) | |
|
||||||
| `-r`, `--run` | 构建成功后运行 | |
|
||||||
| `-j`, `--job <num>` | 并行编译任务数(默认 `4`) | |
|
||||||
| `--build-dir <dir>` | 生成 C++ 与中间产物的目录 | |
|
||||||
| `--dry` | 只生成 C++,跳过编译与链接 | |
|
||||||
| `--php-version <8.4\|8.5>` | 接受的 PHP 语法版本 | |
|
||||||
| `--cxx-std <ver>` | C++ 标准(如 `c++17`、`c++20`) | |
|
||||||
| `--march <arch>` | 目标指令集(如 `native`) | |
|
||||||
| `--lto` | 启用链接时优化 | |
|
||||||
| `--sanitize <type>` | 启用 sanitizer(如 `address`) | |
|
||||||
|
|
||||||
运行 `bin/tpc.php --help` 查看权威的最新参数列表。详见 |
|
||||||
[编译器命令行](docs/COMPILER_CLI.md),包括 Bash 补全: |
|
||||||
|
|
||||||
```bash |
|
||||||
source <(./tpc --generate-completion=bash) |
|
||||||
``` |
|
||||||
|
|
||||||
## Python 桥接 |
|
||||||
|
|
||||||
TypePHP 内置一个 Python 工具子模块,复用 `tpc` 入口: |
|
||||||
|
|
||||||
```shell |
|
||||||
# 为 Python 模块生成 IDE helper |
|
||||||
./tpc --gen-python-helper math |
|
||||||
./tpc --gen-python-helper numpy --output-dir .ide-helper |
|
||||||
|
|
||||||
# 将 Python 脚本转换为 TypePHP |
|
||||||
./tpc --convert-python-to-php script.py > script.php |
|
||||||
``` |
|
||||||
|
|
||||||
详见 [Python 工具子模块](docs/python/tools.md)。 |
|
||||||
|
|
||||||
## 文档 |
|
||||||
|
|
||||||
- [快速入门](docs/QUICKSTART.md) —— 最小编译流程 |
|
||||||
- [编译模式](docs/COMPILATION_MODES.md) —— `bin`、`ext`、`lib` |
|
||||||
- [编译器命令行](docs/COMPILER_CLI.md) —— CLI 参数与项目配置 |
|
||||||
- [不兼容 PHP 特性清单](docs/INCOMPATIBLE_PHP_FEATURES.md) —— 当前限制 |
|
||||||
- [原生类型](docs/NATIVE_TYPES.md) —— 原生标量类型 |
|
||||||
- [高精度类型](docs/HIGH_PRECISION_TYPES.md) —— BigInt / Decimal / BigFloat |
|
||||||
- [Std 容器](docs/STD_CONTAINERS.md) —— 强类型容器 |
|
||||||
- [通用方法](docs/UNIVERSAL_METHODS.md) —— 零开销方法 |
|
||||||
- [编译期函数](docs/COMPILE_TIME_FUNCTIONS.md) —— `any()`、`refval()`、`objval()` 等 |
|
||||||
- [混合 C++/PHP](docs/MIXED_CPP_PHP.md) —— C++/PHP 互操作 |
|
||||||
- [`#[Immutable]`](docs/IMMUTABLE.md) —— 编译期只读契约 |
|
||||||
- [WASI 构建](docs/WASI_BUILD.md) —— WASI 目标 |
|
||||||
|
|
||||||
## 授权协议 |
|
||||||
|
|
||||||
TypePHP 采用 [GNU General Public License v3.0](LICENSE) 授权。 |
|
||||||
|
|
||||||
## 社区 |
|
||||||
|
|
||||||
- 代码仓库:<https://github.com/swoole/typephp> |
|
||||||
- 版权所有 © 2026 上海识沃网络科技有限公司(Swoole) |
|
||||||
@ -1,449 +1,84 @@ |
|||||||
[English](README.md) | [简体中文](README-CN.md) |
# 依赖 |
||||||
|
- 编译器需要 PHP 8.4 以上版本;生成的扩展仍可面向 PHP 8.2~8.5 |
||||||
|
- 需要 GCC-9 以上版本,支持 C++17 标准 |
||||||
|
- 需要 CMake-3.24 以上版本 |
||||||
|
- 需要高精度数学库:`GMP`、`MPFR`、`libmpdec` |
||||||
|
|
||||||
<div align="center"> |
# Composer 安装 |
||||||
|
|
||||||
# TypePHP |
在项目中安装 TypePHP: |
||||||
|
|
||||||
**A native AOT compiler for PHP** |
|
||||||
|
|
||||||
Compile PHP source code into native machine code ahead of time — producing |
|
||||||
standalone executables, PHP extensions, and static libraries — while keeping |
|
||||||
the PHP syntax you already know. |
|
||||||
|
|
||||||
</div> |
|
||||||
|
|
||||||
--- |
|
||||||
|
|
||||||
## What is TypePHP? |
|
||||||
|
|
||||||
TypePHP is an Ahead-Of-Time (AOT) compiler that translates PHP source code into |
|
||||||
C++ and then into native machine code. Unlike a bytecode cache or a VM, it does |
|
||||||
not interpret opcodes at runtime: it generates optimized native binaries that |
|
||||||
run directly on the CPU. |
|
||||||
|
|
||||||
It keeps familiar PHP syntax and adds compile-time type information, so the |
|
||||||
compiler can emit fast, statically-typed C++ for your hot paths — while the |
|
||||||
rest of your code continues to run on the battle-tested Zend engine. |
|
||||||
|
|
||||||
## Features |
|
||||||
|
|
||||||
- **True AOT compilation** — PHP is lowered to C++17, then to native machine |
|
||||||
code. No interpreter, no opcode cache, no JIT warm-up. |
|
||||||
- **Three build modes** — build a standalone `bin` executable, a loadable PHP |
|
||||||
`ext` extension, or a `lib` static library from the same codebase. |
|
||||||
- **Native type system** — `int`, `float`, and `bool` map directly to C++ |
|
||||||
scalar types (`int64_t`, `double`, `bool`) for orders-of-magnitude speedups |
|
||||||
on numeric code. |
|
||||||
- **High-precision numerics** — `bigInt` (GMP), `decimal` (libmpdec), and |
|
||||||
`bigFloat` (MPFR) with zero-overhead arithmetic. |
|
||||||
- **Strongly-typed containers** — `std::array`, `std::vector`, `std::map`, and |
|
||||||
`std::ordered_map` with compile-time element types; up to **10×** faster than |
|
||||||
PHP arrays and on par with C++ `std::vector`. |
|
||||||
- **Universal methods** — call methods directly on primitives |
|
||||||
(`$s->upper()`, `$arr->contains()`, `$big->mul(2)`) with zero runtime |
|
||||||
dispatch overhead. |
|
||||||
- **Mixed C++ / PHP** — call C++ functions from PHP (and vice versa) for |
|
||||||
performance-critical kernels. |
|
||||||
- **Compile-time functions & keywords** — `any()`, `refval()`, `objval()`, |
|
||||||
`expected()`, `unexpected()`, plus `toInt()`, `toString()`, `toArray()` and |
|
||||||
friends. |
|
||||||
- **Compile-time safety** — `#[Immutable]` read-only contracts and `#[ArrayDef]` |
|
||||||
array-shape metadata, checked at compile time with zero runtime cost. |
|
||||||
- **Modern PHP support** — PHP 8.4 property hooks, asymmetric visibility, |
|
||||||
PHP 8.5 `clone()`-with, and `(void)` discard expressions. |
|
||||||
- **Cross-platform & WASM** — Linux, Windows, and macOS targets for x86-64 and |
|
||||||
ARM64, plus WASI 0.2 and browser (Jco) output. |
|
||||||
- **Python bridge** — generate IDE helpers for Python modules and convert |
|
||||||
Python scripts to TypePHP. |
|
||||||
|
|
||||||
## Why TypePHP? |
|
||||||
|
|
||||||
| | TypePHP AOT | Opcode cache (OPcache) | JIT (PHP 8+) | |
|
||||||
|---|---|---|---| |
|
||||||
| Compilation target | Native machine code | Bytecode | Machine code (trace) | |
|
||||||
| Startup / warm-up | None (already compiled) | Per-process warm-up | JIT warm-up | |
|
||||||
| Type-driven optimization | Compile-time, full-program | None | Limited, trace-based | |
|
||||||
| Standalone executable | Yes | No | No | |
|
||||||
| Source code protection | Compiled to machine code | Bytecode (reversible) | Bytecode (reversible) | |
|
||||||
| Deterministic performance | Yes | No | No | |
|
||||||
|
|
||||||
**Strengths over plain PHP:** |
|
||||||
|
|
||||||
- **Near-native performance.** Numeric and container-heavy hot paths compile |
|
||||||
down to the same machine code a C++ program would produce. See the |
|
||||||
[benchmark](#benchmark) below. |
|
||||||
- **Source protection.** Your source is compiled away — shipped artifacts are |
|
||||||
native binaries, not readable PHP files. |
|
||||||
- **Zero-dependency deployment.** Binary mode produces a single self-contained |
|
||||||
executable that runs without a PHP runtime. |
|
||||||
- **Gradual typing that actually pays off.** Add `use native_types`, `std::` |
|
||||||
containers, and type declarations only where performance matters; the rest |
|
||||||
stays ordinary PHP. |
|
||||||
- **Full PHP ecosystem interop.** Extension mode loads as a standard PHP |
|
||||||
extension into `php-fpm`, so existing frameworks and tooling keep working. |
|
||||||
|
|
||||||
## Requirements |
|
||||||
|
|
||||||
- **PHP 8.4 – 8.5** with the `embed` module (`libphp.so`) |
|
||||||
- **GCC 9+** (or Clang) with **C++17** |
|
||||||
- **CMake 3.24+** |
|
||||||
- High-precision math libraries: **GMP**, **MPFR**, **libmpdec** |
|
||||||
|
|
||||||
```shell |
|
||||||
# Ubuntu/Debian |
|
||||||
sudo apt install libgmp-dev libmpfr-dev libmpdec-dev |
|
||||||
|
|
||||||
# RHEL/CentOS/Fedora |
|
||||||
sudo dnf install gmp-devel mpfr-devel libmpdec-devel |
|
||||||
|
|
||||||
# Arch Linux |
|
||||||
sudo pacman -S gmp mpfr mpdecimal |
|
||||||
``` |
|
||||||
|
|
||||||
> GMP powers `bigInt`, MPFR powers `bigFloat`, and libmpdec powers `decimal`. |
|
||||||
|
|
||||||
The preview currently targets **Linux** as the primary development platform |
|
||||||
(Ubuntu 22.04 recommended). Windows and macOS packaging is supported through |
|
||||||
the same entry point. |
|
||||||
|
|
||||||
## Installation |
|
||||||
|
|
||||||
### Via Composer |
|
||||||
|
|
||||||
```bash |
```bash |
||||||
composer require --dev swoole/typephp |
composer require --dev swoole/typephp |
||||||
``` |
``` |
||||||
|
|
||||||
Then compile your project: |
安装后可直接编译项目: |
||||||
|
|
||||||
```bash |
```bash |
||||||
vendor/bin/tpc.php project.yml |
vendor/bin/tpc.php project.yml |
||||||
``` |
``` |
||||||
|
|
||||||
When working inside the TypePHP source repository, use the local entry point |
在 TypePHP 源码仓库中则使用: |
||||||
instead: |
|
||||||
|
|
||||||
```bash |
```bash |
||||||
bin/tpc.php project.yml |
bin/tpc.php project.yml |
||||||
``` |
``` |
||||||
|
|
||||||
### Building `libphp.so` |
Linux 环境缺少 `libphp.so` 时,`tpc.php` 可以交互式下载 PHP 源码并自动构建,详见 [自动构建 libphp.so](docs/LIBPHP_INSTALLER.md)。 |
||||||
|
|
||||||
`tpc` requires a PHP built with the `embed` SAPI. If `libphp.so` is missing on |
|
||||||
Linux, `tpc.php` can interactively download the PHP source and build it for |
|
||||||
you. See [Automatic libphp.so build](docs/LIBPHP_INSTALLER.md). |
|
||||||
|
|
||||||
## Quick Start |
|
||||||
|
|
||||||
Create `hello.php`: |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
echo "Hello World!\n"; |
|
||||||
var_dump(PHP_VERSION); |
|
||||||
var_dump(php_uname()); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
Compile and run it: |
|
||||||
|
|
||||||
```bash |
|
||||||
bin/tpc.php hello.php |
|
||||||
./hello |
|
||||||
``` |
|
||||||
|
|
||||||
Output: |
|
||||||
|
|
||||||
``` |
|
||||||
Hello World! |
|
||||||
string(5) "8.4.x" |
|
||||||
string(16) "Linux ..." |
|
||||||
``` |
|
||||||
|
|
||||||
> Binary mode requires a global `main()` function. It may be declared with no |
|
||||||
> parameters, or as `main(int $argc, array $argv)` to receive command-line |
|
||||||
> arguments, and must return `void`. |
|
||||||
|
|
||||||
## Compilation Modes |
|
||||||
|
|
||||||
TypePHP supports three build modes, selected with `-m` / `--mode`: |
|
||||||
|
|
||||||
| Mode | Flag | Output | Needs `main()` | Typical use | |
```shell |
||||||
|---|---|---|---|---| |
# Ubuntu/Debian |
||||||
| Binary | `-m bin` (default) | Executable | Yes | CLI tools, long-running services, standalone apps | |
sudo apt install libgmp-dev libmpfr-dev libmpdec-dev |
||||||
| Extension | `-m ext` | `.so` / `.dll` | No | Web apps on `php-fpm`, drop-in PHP extension | |
|
||||||
| Library | `-m lib` | Static library | No | Embedding compiled code into other projects | |
|
||||||
|
|
||||||
```bash |
|
||||||
# Binary (default) |
|
||||||
bin/tpc.php app.php -o myapp |
|
||||||
|
|
||||||
# PHP extension |
|
||||||
bin/tpc.php extension/ -m ext -o my_extension |
|
||||||
|
|
||||||
# Static library |
|
||||||
bin/tpc.php lib/ -m lib -o mylib |
|
||||||
``` |
|
||||||
|
|
||||||
See [Compilation modes](docs/COMPILATION_MODES.md) for details. |
|
||||||
|
|
||||||
## Examples |
|
||||||
|
|
||||||
### 1. Native types — compile-time numeric speedup |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
use native_types; |
|
||||||
|
|
||||||
function fib(int $n): int |
|
||||||
{ |
|
||||||
if ($n == 1 || $n == 2) { |
|
||||||
return 1; |
|
||||||
} |
|
||||||
return fib($n - 1) + fib($n - 2); |
|
||||||
} |
|
||||||
|
|
||||||
function main(int $argc, array $argv): void |
|
||||||
{ |
|
||||||
$n = (int)$argv[1]; |
|
||||||
$begin = microtime(true); |
|
||||||
echo fib($n) . "\n"; |
|
||||||
echo "Time: " . (microtime(true) - $begin) . "\n"; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
```bash |
|
||||||
bin/tpc.php fib.php -O3 -o fib |
|
||||||
./fib 30 |
|
||||||
``` |
|
||||||
|
|
||||||
With `use native_types`, `int` variables become C++ `int64_t` and arithmetic |
|
||||||
compiles to plain CPU instructions instead of ZendVM calls. |
|
||||||
|
|
||||||
### 2. High-precision numerics |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
declare(strict_types=1); |
|
||||||
use native_types; |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
// 54-digit integer — automatically detected and stored as bigInt |
|
||||||
$a = std::bigInt("123456789012345678901234567890123456789012345678901234"); |
|
||||||
$b = std::bigInt("987654321098765432109876543210987654321098765432109876"); |
|
||||||
|
|
||||||
echo $a->add($b)->toString() . "\n"; // exact, no overflow |
|
||||||
|
|
||||||
// Exact decimal arithmetic — no binary floating-point error |
|
||||||
$c = std::decimal("0.1")->add(std::decimal("0.2")); |
|
||||||
echo $c->toString() . "\n"; // "0.3" |
|
||||||
|
|
||||||
// 256-bit floating point |
|
||||||
$pi = std::bigFloat("3.14159265358979323846264338327950288419716939937510"); |
|
||||||
echo $pi->mul(2)->toString() . "\n"; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
See [High-precision types](docs/HIGH_PRECISION_TYPES.md) and |
|
||||||
[Native types](docs/NATIVE_TYPES.md). |
|
||||||
|
|
||||||
### 3. Strongly-typed containers |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
use native_types; |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
$vector = std::vector(Type::Int); |
|
||||||
|
|
||||||
$vector[] = 1; |
|
||||||
$vector[] = 2; |
|
||||||
$vector[] = 3; |
|
||||||
|
|
||||||
$sum = 0; |
|
||||||
foreach ($vector as $value) { |
|
||||||
$sum += $value; |
|
||||||
} |
|
||||||
|
|
||||||
echo $sum . "\n"; // 6 |
|
||||||
echo $vector[1] . "\n"; // 2 |
|
||||||
|
|
||||||
// key-value map with fixed key/value types |
|
||||||
$map = std::ordered_map(Type::String, Type::Int); |
|
||||||
$map["a"] = 1; |
|
||||||
$map["b"] = 2; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
See [Std containers](docs/STD_CONTAINERS.md). |
|
||||||
|
|
||||||
### 4. Universal methods |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
$s = "hello world"; |
|
||||||
echo $s->length() . "\n"; // strlen() |
|
||||||
echo $s->upper() . "\n"; // strtoupper() |
|
||||||
echo $s->substr(0, 5) . "\n"; // substr() |
|
||||||
|
|
||||||
$arr = [1, 3, 5, 7, 9]; |
# RHEL/CentOS/Fedora |
||||||
echo $arr->count() . "\n"; // count() |
sudo dnf install gmp-devel mpfr-devel libmpdec-devel |
||||||
var_dump($arr->contains(3)); // in_array() |
|
||||||
|
|
||||||
$big = std::bigInt("12345678901234567890"); |
# Arch Linux |
||||||
echo $big->mul(2)->toString() . "\n"; |
sudo pacman -S gmp mpfr mpdecimal |
||||||
} |
|
||||||
``` |
``` |
||||||
|
|
||||||
Method calls on primitives are resolved at compile time into direct C/C++ |
> GMP 用于 `BigInt` 任意精度整数,MPFR 用于 `BigFloat` 高精度浮点数,libmpdec 用于 `Decimal` 十进制高精度小数。 |
||||||
function calls — no vtable lookup, no reflection, no runtime dispatch. See |
|
||||||
[Universal methods](docs/UNIVERSAL_METHODS.md). |
|
||||||
|
|
||||||
### 5. Mixed C++ / PHP |
> 预览版目前仅支持 `Linux` 系统,建议使用 `Ubuntu 22.04` |
||||||
|
|
||||||
Write performance-critical kernels in C++ and call them from PHP: |
## PHP |
||||||
|
必须包含 embed 模块 |
||||||
|
|
||||||
```cpp |
## PHPX |
||||||
// math.cpp |
可使用 `composer install` 安装依赖。 |
||||||
#include <phpx.h> |
进入 `vendor/swoole/phpx` 目录,编译 `phpx` |
||||||
|
|
||||||
using namespace php; |
|
||||||
|
|
||||||
var php_fast_sum(Int a, Int b) { |
|
||||||
return a + b; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
```php |
```shell |
||||||
<?php |
cd vendor/swoole/phpx |
||||||
// math.stub.php — declares the C++ function signature |
cmake . |
||||||
function fast_sum(int $a, int $b): int; |
make -j32 |
||||||
``` |
``` |
||||||
|
|
||||||
```php |
## 动态链接库 |
||||||
<?php |
```shell |
||||||
function main(): void |
sudo ldconfig -p | grep php |
||||||
{ |
|
||||||
echo fast_sum(3, 4) . "\n"; // 7 |
|
||||||
} |
|
||||||
``` |
``` |
||||||
|
必须包含 `libphp.so` 和 `libphpx.so` |
||||||
|
|
||||||
See [Mixed C++/PHP](docs/MIXED_CPP_PHP.md). |
若编译完成,但找不到动态链接库,需要修改 |
||||||
|
```shell |
||||||
## Benchmark |
vim /etc/ld.so.conf.d/swoole.conf |
||||||
|
|
||||||
A 10000×100000 element update loop, comparing PHP arrays against TypePHP's |
|
||||||
`std::array` and native C++: |
|
||||||
|
|
||||||
| Implementation | Time | |
|
||||||
|---|---| |
|
||||||
| PHP array (JIT) | 67.6 s | |
|
||||||
| `std::array` (TypePHP AOT) | **6.4 s** | |
|
||||||
| C++ `std::vector` | 6.2 s | |
|
||||||
|
|
||||||
`std::array` is roughly **10× faster** than PHP arrays and performs |
|
||||||
identically to hand-written C++. See the full benchmark in |
|
||||||
[Std containers](docs/STD_CONTAINERS.md). |
|
||||||
|
|
||||||
## Command Line |
|
||||||
|
|
||||||
```bash |
|
||||||
bin/tpc.php <file|dir|project.yml> [options] [-- program-args...] |
|
||||||
``` |
``` |
||||||
|
|
||||||
Common usage: |
添加路径 |
||||||
|
|
||||||
```bash |
|
||||||
# Compile a single file |
|
||||||
bin/tpc.php app.php |
|
||||||
|
|
||||||
# Optimize and run, passing args to the program after `--` |
|
||||||
bin/tpc.php app.php -O3 -r -- --flag value |
|
||||||
|
|
||||||
# Compile a project defined in project.yml |
|
||||||
bin/tpc.php project.yml -O2 -j 8 |
|
||||||
|
|
||||||
# Build a PHP extension |
|
||||||
bin/tpc.php extension/ -m ext -o my_extension |
|
||||||
|
|
||||||
# Only generate C++ (skip compile & link) |
|
||||||
bin/tpc.php app.php --dry --build-dir /tmp/typephp-build |
|
||||||
|
|
||||||
# Compile to WASI 0.2 |
|
||||||
bin/tpc.php --wasm app.php |
|
||||||
|
|
||||||
# Compile for the browser (requires jco) |
|
||||||
bin/tpc.php --wasm=browser app.php |
|
||||||
``` |
``` |
||||||
|
/home/swoole/workspace/projects/phpx/lib |
||||||
Key options: |
/opt/php-8.4/lib/ |
||||||
|
|
||||||
| Option | Description | |
|
||||||
|---|---| |
|
||||||
| `-O <0-3>` | Optimization level (default `0`) | |
|
||||||
| `-d`, `--debug` | Debug build with symbols and source tracking | |
|
||||||
| `-o`, `--output <file>` | Output file name | |
|
||||||
| `-m`, `--mode <bin\|lib\|ext>` | Build mode (default `bin`) | |
|
||||||
| `-r`, `--run` | Run after a successful build | |
|
||||||
| `-j`, `--job <num>` | Parallel compile jobs (default `4`) | |
|
||||||
| `--build-dir <dir>` | Directory for generated C++ and intermediates | |
|
||||||
| `--dry` | Generate C++ only, skip compile and link | |
|
||||||
| `--php-version <8.4\|8.5>` | PHP syntax version to accept | |
|
||||||
| `--cxx-std <ver>` | C++ standard (e.g. `c++17`, `c++20`) | |
|
||||||
| `--march <arch>` | Target instruction set (e.g. `native`) | |
|
||||||
| `--lto` | Enable link-time optimization | |
|
||||||
| `--sanitize <type>` | Enable a sanitizer (e.g. `address`) | |
|
||||||
|
|
||||||
Run `bin/tpc.php --help` for the authoritative, up-to-date list. See |
|
||||||
[Compiler CLI](docs/COMPILER_CLI.md) for details, including Bash completion: |
|
||||||
|
|
||||||
```bash |
|
||||||
source <(./tpc --generate-completion=bash) |
|
||||||
``` |
``` |
||||||
|
|
||||||
## Python bridge |
## Release packaging |
||||||
|
|
||||||
TypePHP ships a Python tool submodule that shares the `tpc` entry point: |
Use the same PHP entry point on Windows, Linux, and macOS: |
||||||
|
|
||||||
```shell |
```shell |
||||||
# Generate IDE helpers for Python modules |
php package.php |
||||||
./tpc --gen-python-helper math |
|
||||||
./tpc --gen-python-helper numpy --output-dir .ide-helper |
|
||||||
|
|
||||||
# Convert a Python script to TypePHP |
|
||||||
./tpc --convert-python-to-php script.py > script.php |
|
||||||
``` |
``` |
||||||
|
|
||||||
See [Python tool submodule](docs/python/tools.md). |
Windows packaging requires `PHP_HOME` and `PHPX_HOME`; Linux packaging requires |
||||||
|
UPX; macOS uses `strip` when available. TypePHP rejects 32-bit targets and |
||||||
## Documentation |
supports common 64-bit CPU architectures, including x86-64 and ARM64. |
||||||
|
|
||||||
- [Quick Start](docs/QUICKSTART.md) — minimal compilation flow |
|
||||||
- [Compilation modes](docs/COMPILATION_MODES.md) — `bin`, `ext`, `lib` |
|
||||||
- [Compiler CLI](docs/COMPILER_CLI.md) — CLI arguments and project config |
|
||||||
- [Incompatible PHP features](docs/INCOMPATIBLE_PHP_FEATURES.md) — current limits |
|
||||||
- [Native types](docs/NATIVE_TYPES.md) — native scalar types |
|
||||||
- [High-precision types](docs/HIGH_PRECISION_TYPES.md) — BigInt / Decimal / BigFloat |
|
||||||
- [Std containers](docs/STD_CONTAINERS.md) — strongly-typed containers |
|
||||||
- [Universal methods](docs/UNIVERSAL_METHODS.md) — zero-overhead methods |
|
||||||
- [Compile-time functions](docs/COMPILE_TIME_FUNCTIONS.md) — `any()`, `refval()`, `objval()`, … |
|
||||||
- [Mixed C++/PHP](docs/MIXED_CPP_PHP.md) — C++/PHP interop |
|
||||||
- [`#[Immutable]`](docs/IMMUTABLE.md) — compile-time read-only contracts |
|
||||||
- [WASI build](docs/WASI_BUILD.md) — WASI targets |
|
||||||
|
|
||||||
## License |
|
||||||
|
|
||||||
TypePHP is licensed under the [GNU General Public License v3.0](LICENSE). |
|
||||||
|
|
||||||
## Community |
|
||||||
|
|
||||||
- Repository: <https://github.com/swoole/typephp> |
|
||||||
- Copyright © 2026 上海识沃网络科技有限公司 (Swoole) |
|
||||||
|
|||||||
@ -0,0 +1,52 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||||||
|
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> |
||||||
|
|
||||||
|
<!-- 程序集标识 --> |
||||||
|
<assemblyIdentity |
||||||
|
type="win32" |
||||||
|
name="AotCompiler.App" |
||||||
|
version="1.0.0.0" /> |
||||||
|
|
||||||
|
<!-- 信任信息 / UAC 权限级别 --> |
||||||
|
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> |
||||||
|
<security> |
||||||
|
<requestedPrivileges> |
||||||
|
<!-- |
||||||
|
asInvoker: 以当前用户权限运行(推荐默认值) |
||||||
|
highestAvailable: 请求当前用户能获得的最高权限 |
||||||
|
requireAdministrator: 要求管理员权限 |
||||||
|
--> |
||||||
|
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> |
||||||
|
</requestedPrivileges> |
||||||
|
</security> |
||||||
|
</trustInfo> |
||||||
|
|
||||||
|
<!-- 应用程序兼容性 --> |
||||||
|
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> |
||||||
|
<application> |
||||||
|
<!-- Windows 10 / 11 --> |
||||||
|
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" /> |
||||||
|
<!-- Windows 8.1 --> |
||||||
|
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" /> |
||||||
|
<!-- Windows 8 --> |
||||||
|
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" /> |
||||||
|
<!-- Windows 7 --> |
||||||
|
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" /> |
||||||
|
</application> |
||||||
|
</compatibility> |
||||||
|
|
||||||
|
<!-- DPI 感知(Windows 10 版本 1607+) --> |
||||||
|
<asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"> |
||||||
|
<asmv3:windowsSettings> |
||||||
|
<!-- |
||||||
|
PerMonitorV2: 逐显示器 DPI 感知 v2(推荐,支持混合模式 DPI 缩放) |
||||||
|
PerMonitor: 逐显示器 DPI 感知 v1 |
||||||
|
System: 系统 DPI 感知 |
||||||
|
None / 不设置: DPI 不感知(系统会自动缩放) |
||||||
|
--> |
||||||
|
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware> |
||||||
|
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness> |
||||||
|
</asmv3:windowsSettings> |
||||||
|
</asmv3:application> |
||||||
|
|
||||||
|
</assembly> |
||||||
@ -0,0 +1,495 @@ |
|||||||
|
#!/usr/bin/env python3 |
||||||
|
""" |
||||||
|
使用 libclang 解析 C++ 代码并添加头文件路径 |
||||||
|
""" |
||||||
|
|
||||||
|
import clang.cindex |
||||||
|
from clang.cindex import Index, CursorKind, TypeKind, StorageClass |
||||||
|
import json |
||||||
|
import sys |
||||||
|
import os |
||||||
|
from pathlib import Path |
||||||
|
import subprocess |
||||||
|
import argparse |
||||||
|
|
||||||
|
|
||||||
|
class PHPConfigHelper: |
||||||
|
"""PHP 配置辅助类""" |
||||||
|
|
||||||
|
def __init__(self, php_config_path='php-config'): |
||||||
|
self.php_config = php_config_path |
||||||
|
self._check_availability() |
||||||
|
|
||||||
|
def _check_availability(self): |
||||||
|
"""检查 php-config 是否可用""" |
||||||
|
try: |
||||||
|
result = subprocess.run( |
||||||
|
[self.php_config, '--version'], |
||||||
|
capture_output=True, |
||||||
|
text=True, |
||||||
|
check=True |
||||||
|
) |
||||||
|
print(f"✓ 找到 PHP {result.stdout.strip()}") |
||||||
|
except (FileNotFoundError, subprocess.CalledProcessError) as e: |
||||||
|
print(f"警告: php-config 不可用: {e}") |
||||||
|
# 尝试使用常见路径 |
||||||
|
common_paths = [ |
||||||
|
'/usr/bin/php-config', |
||||||
|
'/usr/local/bin/php-config', |
||||||
|
'/opt/php/bin/php-config' |
||||||
|
] |
||||||
|
for path in common_paths: |
||||||
|
if os.path.exists(path): |
||||||
|
self.php_config = path |
||||||
|
try: |
||||||
|
result = subprocess.run( |
||||||
|
[self.php_config, '--version'], |
||||||
|
capture_output=True, |
||||||
|
text=True, |
||||||
|
check=True |
||||||
|
) |
||||||
|
print(f"✓ 找到 PHP {result.stdout.strip()} 在 {path}") |
||||||
|
return |
||||||
|
except (FileNotFoundError, subprocess.CalledProcessError): |
||||||
|
continue |
||||||
|
print("警告: php-config 在任何常见路径都不可用,使用默认路径") |
||||||
|
|
||||||
|
def get_includes(self): |
||||||
|
""" |
||||||
|
获取 include 路径列表 |
||||||
|
|
||||||
|
Returns: |
||||||
|
list: 头文件路径列表(不带 -I 前缀) |
||||||
|
""" |
||||||
|
try: |
||||||
|
result = subprocess.run( |
||||||
|
[self.php_config, '--includes'], |
||||||
|
capture_output=True, |
||||||
|
text=True, |
||||||
|
check=True |
||||||
|
) |
||||||
|
|
||||||
|
# 解析输出: "-I/path1 -I/path2" -> ['/path1', '/path2'] |
||||||
|
includes = [] |
||||||
|
for flag in result.stdout.strip().split(): |
||||||
|
if flag.startswith('-I'): |
||||||
|
includes.append(flag[2:]) |
||||||
|
|
||||||
|
return includes |
||||||
|
except (subprocess.CalledProcessError, FileNotFoundError): |
||||||
|
print("警告: 无法获取 PHP includes,使用默认路径") |
||||||
|
return ['/usr/include/php', '/usr/include/php/20210902'] # 默认路径 |
||||||
|
|
||||||
|
def get_include_dir(self): |
||||||
|
"""获取主 include 目录""" |
||||||
|
try: |
||||||
|
result = subprocess.run( |
||||||
|
[self.php_config, '--include-dir'], |
||||||
|
capture_output=True, |
||||||
|
text=True, |
||||||
|
check=True |
||||||
|
) |
||||||
|
return result.stdout.strip() |
||||||
|
except (subprocess.CalledProcessError, FileNotFoundError): |
||||||
|
return '/usr/include/php' |
||||||
|
|
||||||
|
def get_extension_dir(self): |
||||||
|
"""获取扩展目录""" |
||||||
|
try: |
||||||
|
result = subprocess.run( |
||||||
|
[self.php_config, '--extension-dir'], |
||||||
|
capture_output=True, |
||||||
|
text=True, |
||||||
|
check=True |
||||||
|
) |
||||||
|
return result.stdout.strip() |
||||||
|
except (subprocess.CalledProcessError, FileNotFoundError): |
||||||
|
return '/usr/lib/php' |
||||||
|
|
||||||
|
def get_version(self): |
||||||
|
"""获取 PHP 版本""" |
||||||
|
try: |
||||||
|
result = subprocess.run( |
||||||
|
[self.php_config, '--version'], |
||||||
|
capture_output=True, |
||||||
|
text=True, |
||||||
|
check=True |
||||||
|
) |
||||||
|
return result.stdout.strip() |
||||||
|
except (subprocess.CalledProcessError, FileNotFoundError): |
||||||
|
return 'unknown' |
||||||
|
|
||||||
|
def get_php_binary(self): |
||||||
|
"""获取 PHP 二进制路径""" |
||||||
|
try: |
||||||
|
result = subprocess.run( |
||||||
|
[self.php_config, '--php-binary'], |
||||||
|
capture_output=True, |
||||||
|
text=True, |
||||||
|
check=True |
||||||
|
) |
||||||
|
return result.stdout.strip() |
||||||
|
except (subprocess.CalledProcessError, FileNotFoundError): |
||||||
|
return 'php' |
||||||
|
|
||||||
|
def get_configure_options(self): |
||||||
|
"""获取配置选项""" |
||||||
|
try: |
||||||
|
result = subprocess.run( |
||||||
|
[self.php_config, '--configure-options'], |
||||||
|
capture_output=True, |
||||||
|
text=True, |
||||||
|
check=True |
||||||
|
) |
||||||
|
return result.stdout.strip() |
||||||
|
except (subprocess.CalledProcessError, FileNotFoundError): |
||||||
|
return '' |
||||||
|
|
||||||
|
def get_all_info(self): |
||||||
|
"""获取所有配置信息""" |
||||||
|
return { |
||||||
|
'version': self.get_version(), |
||||||
|
'includes': self.get_includes(), |
||||||
|
'include_dir': self.get_include_dir(), |
||||||
|
'extension_dir': self.get_extension_dir(), |
||||||
|
'php_binary': self.get_php_binary(), |
||||||
|
'configure_options': self.get_configure_options(), |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
class ClangParser: |
||||||
|
def __init__(self, libclang_path=None): |
||||||
|
""" |
||||||
|
初始化 Clang 解析器 |
||||||
|
|
||||||
|
Args: |
||||||
|
libclang_path: libclang 库的路径(可选) |
||||||
|
""" |
||||||
|
if libclang_path: |
||||||
|
try: |
||||||
|
clang.cindex.Config.set_library_file(libclang_path) |
||||||
|
except Exception as e: |
||||||
|
print(f"警告: 无法设置 libclang 路径 {libclang_path}: {e}") |
||||||
|
print("尝试使用默认路径...") |
||||||
|
|
||||||
|
try: |
||||||
|
self.index = Index.create() |
||||||
|
except Exception as e: |
||||||
|
print(f"错误: 无法创建 Clang 索引: {e}") |
||||||
|
print("请确保已安装 python3-clang 和 clang 库") |
||||||
|
raise |
||||||
|
|
||||||
|
def parse_file(self, filename, include_paths=None, defines=None, |
||||||
|
compiler_args=None, language='c++'): |
||||||
|
""" |
||||||
|
解析 C++ 文件 |
||||||
|
|
||||||
|
Args: |
||||||
|
filename: 要解析的文件路径 |
||||||
|
include_paths: 头文件搜索路径列表 |
||||||
|
defines: 宏定义列表 ['MACRO=value', 'DEBUG'] |
||||||
|
compiler_args: 额外的编译器参数 |
||||||
|
language: 语言类型 ('c', 'c++', 'objective-c') |
||||||
|
|
||||||
|
Returns: |
||||||
|
TranslationUnit 对象 |
||||||
|
""" |
||||||
|
if not os.path.exists(filename): |
||||||
|
raise FileNotFoundError(f"文件不存在: {filename}") |
||||||
|
|
||||||
|
args = [] |
||||||
|
|
||||||
|
# 1. 设置语言标准 |
||||||
|
if language == 'c++': |
||||||
|
args.extend([ |
||||||
|
'-x', 'c++', |
||||||
|
'-std=c++14', # 更标准的 C++ 版本 |
||||||
|
]) |
||||||
|
elif language == 'c': |
||||||
|
args.extend(['-x', 'c', '-std=c11']) |
||||||
|
|
||||||
|
# 2. 添加头文件搜索路径 |
||||||
|
if include_paths: |
||||||
|
for path in include_paths: |
||||||
|
if os.path.exists(path): # 检查路径是否存在 |
||||||
|
args.append(f'-I{path}') |
||||||
|
else: |
||||||
|
print(f"警告: 包含路径不存在: {path}") |
||||||
|
|
||||||
|
# 3. 添加宏定义 |
||||||
|
if defines: |
||||||
|
for define in defines: |
||||||
|
args.append(f'-D{define}') |
||||||
|
|
||||||
|
# 4. 添加额外的编译器参数 |
||||||
|
if compiler_args: |
||||||
|
args.extend(compiler_args) |
||||||
|
|
||||||
|
# 5. 常用的编译选项 |
||||||
|
args.extend([ |
||||||
|
'-Wno-pragma-once-outside-header', # 忽略警告 |
||||||
|
'-ferror-limit=0', # 不限制错误数量 |
||||||
|
'-fno-delayed-template-parsing', # 避免某些 C++ 模板解析问题 |
||||||
|
'-w', # 禁用所有警告以减少输出 |
||||||
|
]) |
||||||
|
|
||||||
|
print(f"编译参数: {' '.join(args)}") |
||||||
|
|
||||||
|
# 解析文件 |
||||||
|
try: |
||||||
|
tu = self.index.parse( |
||||||
|
filename, |
||||||
|
args=args, |
||||||
|
options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD |
||||||
|
) |
||||||
|
except Exception as e: |
||||||
|
print(f"解析文件时出错: {e}") |
||||||
|
print("尝试使用最小参数集...") |
||||||
|
# 尝试使用最小参数集 |
||||||
|
minimal_args = ['-x', 'c++', '-std=c++14', '-w'] |
||||||
|
if include_paths: |
||||||
|
for path in include_paths: |
||||||
|
if os.path.exists(path): |
||||||
|
minimal_args.append(f'-I{path}') |
||||||
|
try: |
||||||
|
tu = self.index.parse( |
||||||
|
filename, |
||||||
|
args=minimal_args, |
||||||
|
options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD |
||||||
|
) |
||||||
|
print("使用最小参数集成功解析") |
||||||
|
except Exception as e2: |
||||||
|
print(f"使用最小参数集也失败: {e2}") |
||||||
|
print("尝试解析不包含头文件的简化版本...") |
||||||
|
# 创建一个临时文件,移除头文件包含行 |
||||||
|
temp_filename = filename + ".tmp" |
||||||
|
with open(filename, 'r') as original: |
||||||
|
lines = original.readlines() |
||||||
|
|
||||||
|
# 移除 #include 行 |
||||||
|
filtered_lines = [line for line in lines if not line.strip().startswith('#include')] |
||||||
|
|
||||||
|
with open(temp_filename, 'w') as temp: |
||||||
|
temp.writelines(filtered_lines) |
||||||
|
|
||||||
|
try: |
||||||
|
tu = self.index.parse( |
||||||
|
temp_filename, |
||||||
|
args=minimal_args, |
||||||
|
options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD |
||||||
|
) |
||||||
|
print("解析简化版本成功") |
||||||
|
# 清理临时文件 |
||||||
|
os.remove(temp_filename) |
||||||
|
except Exception as e3: |
||||||
|
print(f"简化版本也失败: {e3}") |
||||||
|
# 清理临时文件 |
||||||
|
if os.path.exists(temp_filename): |
||||||
|
os.remove(temp_filename) |
||||||
|
raise |
||||||
|
|
||||||
|
# 检查诊断信息 |
||||||
|
if tu.diagnostics: |
||||||
|
print(f"\n诊断信息 ({len(tu.diagnostics)} 个):") |
||||||
|
error_count = 0 |
||||||
|
warning_count = 0 |
||||||
|
|
||||||
|
for diag in tu.diagnostics: |
||||||
|
if diag.severity >= 3: # 错误级别 |
||||||
|
error_count += 1 |
||||||
|
else: # 警告级别 |
||||||
|
warning_count += 1 |
||||||
|
|
||||||
|
print(f"错误: {error_count}, 警告: {warning_count}") |
||||||
|
|
||||||
|
# 只显示前几个诊断信息,避免输出过多 |
||||||
|
for i, diag in enumerate(tu.diagnostics): |
||||||
|
if i >= 5: # 只显示前5个 |
||||||
|
print("... 还有更多诊断信息") |
||||||
|
break |
||||||
|
print(f" [{diag.severity}] {diag.spelling}") |
||||||
|
if diag.location.file: |
||||||
|
print(f" at {diag.location.file.name}:{diag.location.line}") |
||||||
|
|
||||||
|
return tu |
||||||
|
|
||||||
|
def extract_functions(self, tu, name_prefixes=None): |
||||||
|
""" |
||||||
|
提取函数定义 |
||||||
|
|
||||||
|
Args: |
||||||
|
tu: TranslationUnit 对象 |
||||||
|
name_prefixes: 函数名前缀过滤列表 |
||||||
|
|
||||||
|
Returns: |
||||||
|
函数信息列表 |
||||||
|
""" |
||||||
|
functions = [] |
||||||
|
|
||||||
|
def visit_node(node, depth=0): |
||||||
|
# 只处理函数声明/定义 |
||||||
|
if node.kind == CursorKind.FUNCTION_DECL: |
||||||
|
try: |
||||||
|
func_info = self.parse_function(node) |
||||||
|
|
||||||
|
# 过滤函数名 |
||||||
|
if name_prefixes: |
||||||
|
if any(func_info['name'].startswith(prefix) |
||||||
|
for prefix in name_prefixes): |
||||||
|
functions.append(func_info) |
||||||
|
else: |
||||||
|
functions.append(func_info) |
||||||
|
except Exception as e: |
||||||
|
print(f"解析函数时出错: {e}") |
||||||
|
|
||||||
|
# 递归访问子节点 |
||||||
|
for child in node.get_children(): |
||||||
|
visit_node(child, depth + 1) |
||||||
|
|
||||||
|
visit_node(tu.cursor) |
||||||
|
return functions |
||||||
|
|
||||||
|
def parse_function(self, cursor): |
||||||
|
""" |
||||||
|
解析函数详细信息 |
||||||
|
""" |
||||||
|
# 检查方法是否存在 |
||||||
|
def safe_call(method, default_value=None): |
||||||
|
try: |
||||||
|
return method() |
||||||
|
except AttributeError: |
||||||
|
return default_value |
||||||
|
|
||||||
|
# 基本信息 |
||||||
|
func_info = { |
||||||
|
'name': cursor.spelling, |
||||||
|
'displayName': cursor.displayname, |
||||||
|
'mangledName': cursor.mangled_name, |
||||||
|
'returnType': cursor.result_type.spelling, |
||||||
|
'isStatic': cursor.storage_class == StorageClass.STATIC, |
||||||
|
'isInline': safe_call(lambda: cursor.is_inline_function(), False), |
||||||
|
'isVirtual': safe_call(lambda: cursor.is_virtual_method(), False), |
||||||
|
'isConst': safe_call(lambda: cursor.is_const_method(), False), |
||||||
|
'location': { |
||||||
|
'file': str(cursor.location.file) if cursor.location.file else None, |
||||||
|
'line': cursor.location.line, |
||||||
|
'column': cursor.location.column, |
||||||
|
}, |
||||||
|
'parameters': [], |
||||||
|
'namespaces': self.get_namespaces(cursor), |
||||||
|
} |
||||||
|
|
||||||
|
# 解析参数 |
||||||
|
for arg in cursor.get_arguments(): |
||||||
|
param_info = { |
||||||
|
'name': arg.spelling or f'arg{len(func_info["parameters"])}', |
||||||
|
'type': arg.type.spelling, |
||||||
|
'canonicalType': arg.type.get_canonical().spelling, |
||||||
|
} |
||||||
|
|
||||||
|
# 检查是否有默认值 |
||||||
|
try: |
||||||
|
for token in arg.get_tokens(): |
||||||
|
if token.spelling == '=': |
||||||
|
# 有默认值 |
||||||
|
param_info['hasDefault'] = True |
||||||
|
break |
||||||
|
except: |
||||||
|
# 如果无法获取 tokens,跳过默认值检查 |
||||||
|
pass |
||||||
|
|
||||||
|
func_info['parameters'].append(param_info) |
||||||
|
|
||||||
|
return func_info |
||||||
|
|
||||||
|
def get_namespaces(self, cursor): |
||||||
|
""" |
||||||
|
获取函数所在的命名空间 |
||||||
|
""" |
||||||
|
namespaces = [] |
||||||
|
parent = cursor.semantic_parent |
||||||
|
|
||||||
|
while parent and parent.kind != CursorKind.TRANSLATION_UNIT: |
||||||
|
if parent.kind == CursorKind.NAMESPACE: |
||||||
|
namespaces.insert(0, parent.spelling) |
||||||
|
parent = parent.semantic_parent |
||||||
|
|
||||||
|
return namespaces |
||||||
|
|
||||||
|
|
||||||
|
def main(): |
||||||
|
parser = argparse.ArgumentParser(description='使用 libclang 解析 C++ 代码并提取函数信息') |
||||||
|
parser.add_argument('filename', help='要解析的 C++ 文件路径') |
||||||
|
parser.add_argument('--libclang-path', help='libclang 库路径') |
||||||
|
parser.add_argument('--include-paths', nargs='*', help='额外的包含路径') |
||||||
|
parser.add_argument('--function-prefixes', nargs='*', help='函数名前缀过滤器') |
||||||
|
|
||||||
|
args = parser.parse_args() |
||||||
|
|
||||||
|
if not os.path.exists(args.filename): |
||||||
|
print(f"错误: 文件不存在: {args.filename}") |
||||||
|
sys.exit(1) |
||||||
|
|
||||||
|
try: |
||||||
|
# 创建解析器 |
||||||
|
parser_obj = ClangParser(libclang_path=args.libclang_path) |
||||||
|
|
||||||
|
# 配置头文件路径 |
||||||
|
include_paths = args.include_paths or [ |
||||||
|
"/usr/include/linux", |
||||||
|
"/home/swoole/workspace/projects/phpx/include" |
||||||
|
] |
||||||
|
|
||||||
|
# 尝试获取 PHP 配置的头文件路径 |
||||||
|
try: |
||||||
|
php_config = PHPConfigHelper() |
||||||
|
php_includes = php_config.get_includes() |
||||||
|
include_paths.extend(php_includes) |
||||||
|
except Exception as e: |
||||||
|
print(f"警告: 无法获取 PHP 配置: {e}") |
||||||
|
print("继续使用默认路径...") |
||||||
|
|
||||||
|
# 配置宏定义 |
||||||
|
defines = [ |
||||||
|
'HAVE_CONFIG_H', |
||||||
|
'ZEND_ENABLE_STATIC_TSRMLS_CACHE=1', |
||||||
|
] |
||||||
|
|
||||||
|
# 额外的编译器参数 |
||||||
|
compiler_args = [ |
||||||
|
'-fparse-all-comments', # 解析所有注释 |
||||||
|
'-Wno-unknown-pragmas', |
||||||
|
] |
||||||
|
|
||||||
|
# 解析文件 |
||||||
|
tu = parser_obj.parse_file( |
||||||
|
args.filename, |
||||||
|
include_paths=include_paths, |
||||||
|
defines=defines, |
||||||
|
compiler_args=compiler_args, |
||||||
|
language='c++' |
||||||
|
) |
||||||
|
|
||||||
|
# 提取函数 |
||||||
|
name_prefixes = args.function_prefixes or None |
||||||
|
functions = parser_obj.extract_functions(tu, name_prefixes=name_prefixes) |
||||||
|
|
||||||
|
# 输出结果 |
||||||
|
output = { |
||||||
|
'file': args.filename, |
||||||
|
'functions': functions, |
||||||
|
'total': len(functions), |
||||||
|
} |
||||||
|
|
||||||
|
print(json.dumps(output, indent=2, ensure_ascii=False)) |
||||||
|
|
||||||
|
except clang.cindex.TranslationUnitLoadError as e: |
||||||
|
print(f"翻译单元加载错误: {e}") |
||||||
|
print("这通常意味着 C++ 代码包含语法错误或缺少必要的头文件") |
||||||
|
sys.exit(1) |
||||||
|
except Exception as e: |
||||||
|
print(f"错误: {e}") |
||||||
|
sys.exit(1) |
||||||
|
|
||||||
|
if __name__ == '__main__': |
||||||
|
main() |
||||||
@ -1,146 +0,0 @@ |
|||||||
#!/usr/bin/env php |
|
||||||
<?php |
|
||||||
/** |
|
||||||
* This file is part of Swoole-Compiler(AOT). |
|
||||||
* |
|
||||||
* @link https://www.swoole.com/ |
|
||||||
* @contact service@swoole.com |
|
||||||
*/ |
|
||||||
|
|
||||||
declare(strict_types=1); |
|
||||||
|
|
||||||
use TypePhp\Testing\TestCoverageAnalyzer; |
|
||||||
|
|
||||||
require __DIR__ . '/bootstrap.php'; |
|
||||||
|
|
||||||
$format = 'summary'; |
|
||||||
$output = null; |
|
||||||
$includePhpUnit = true; |
|
||||||
$strict = false; |
|
||||||
$phpVersions = ['8.4', '8.5']; |
|
||||||
$paths = []; |
|
||||||
|
|
||||||
foreach (array_slice($argv, 1) as $argument) { |
|
||||||
if ($argument === '--help' || $argument === '-h') { |
|
||||||
printUsage($argv[0]); |
|
||||||
exit(0); |
|
||||||
} |
|
||||||
if ($argument === '--no-phpunit') { |
|
||||||
$includePhpUnit = false; |
|
||||||
continue; |
|
||||||
} |
|
||||||
if ($argument === '--strict') { |
|
||||||
$strict = true; |
|
||||||
continue; |
|
||||||
} |
|
||||||
if (str_starts_with($argument, '--format=')) { |
|
||||||
$format = substr($argument, strlen('--format=')); |
|
||||||
continue; |
|
||||||
} |
|
||||||
if (str_starts_with($argument, '--output=')) { |
|
||||||
$output = substr($argument, strlen('--output=')); |
|
||||||
continue; |
|
||||||
} |
|
||||||
if (str_starts_with($argument, '--php-versions=')) { |
|
||||||
$phpVersions = array_values(array_filter(array_map('trim', explode(',', substr($argument, strlen('--php-versions=')))))); |
|
||||||
continue; |
|
||||||
} |
|
||||||
if (str_starts_with($argument, '-')) { |
|
||||||
fwrite(STDERR, 'Unknown option: ' . $argument . PHP_EOL); |
|
||||||
exit(2); |
|
||||||
} |
|
||||||
$paths[] = $argument; |
|
||||||
} |
|
||||||
|
|
||||||
if (!in_array($format, ['summary', 'json', 'markdown'], true)) { |
|
||||||
fwrite(STDERR, 'Invalid format. Expected summary, json or markdown.' . PHP_EOL); |
|
||||||
exit(2); |
|
||||||
} |
|
||||||
if ($phpVersions === []) { |
|
||||||
fwrite(STDERR, 'At least one target PHP version is required.' . PHP_EOL); |
|
||||||
exit(2); |
|
||||||
} |
|
||||||
foreach ($phpVersions as $version) { |
|
||||||
if (!preg_match('/^\d+\.\d+$/', $version)) { |
|
||||||
fwrite(STDERR, 'Invalid PHP version: ' . $version . PHP_EOL); |
|
||||||
exit(2); |
|
||||||
} |
|
||||||
} |
|
||||||
if ($paths === []) { |
|
||||||
$paths = ['tests/compiler']; |
|
||||||
} |
|
||||||
|
|
||||||
try { |
|
||||||
$analyzer = new TestCoverageAnalyzer(ROOT_PATH, $phpVersions); |
|
||||||
$report = $analyzer->analyze( |
|
||||||
$paths, |
|
||||||
$includePhpUnit ? ROOT_PATH . '/phpunit/src' : null, |
|
||||||
$includePhpUnit ? ROOT_PATH . '/phpunit/code' : null, |
|
||||||
); |
|
||||||
} catch (Throwable $error) { |
|
||||||
fwrite(STDERR, 'Coverage analysis failed: ' . $error->getMessage() . PHP_EOL); |
|
||||||
exit(1); |
|
||||||
} |
|
||||||
|
|
||||||
$rendered = match ($format) { |
|
||||||
'summary' => $analyzer->renderSummary($report), |
|
||||||
'markdown' => $analyzer->renderMarkdown($report), |
|
||||||
'json' => json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR) . PHP_EOL, |
|
||||||
}; |
|
||||||
|
|
||||||
if ($output === null) { |
|
||||||
echo $rendered; |
|
||||||
} else { |
|
||||||
$outputPath = isAbsolutePath($output) ? $output : ROOT_PATH . DIRECTORY_SEPARATOR . $output; |
|
||||||
$directory = dirname($outputPath); |
|
||||||
if (!is_dir($directory) && !mkdir($directory, 0777, true) && !is_dir($directory)) { |
|
||||||
fwrite(STDERR, 'Unable to create output directory: ' . $directory . PHP_EOL); |
|
||||||
exit(1); |
|
||||||
} |
|
||||||
if (file_put_contents($outputPath, $rendered) === false) { |
|
||||||
fwrite(STDERR, 'Unable to write report: ' . $outputPath . PHP_EOL); |
|
||||||
exit(1); |
|
||||||
} |
|
||||||
echo 'Wrote ', $format, ' coverage report: ', relativePath(ROOT_PATH, $outputPath), PHP_EOL; |
|
||||||
} |
|
||||||
|
|
||||||
if ($strict && ($report['parse_errors'] !== [] || $report['unresolved_phpunit_fixtures'] !== [])) { |
|
||||||
exit(1); |
|
||||||
} |
|
||||||
|
|
||||||
function printUsage(string $script): void |
|
||||||
{ |
|
||||||
echo <<<USAGE |
|
||||||
Usage: |
|
||||||
php {$script} [options] [PHPT path ...] |
|
||||||
|
|
||||||
Options: |
|
||||||
--format=summary|json|markdown Output format (default: summary) |
|
||||||
--output=<file> Write the report to a file |
|
||||||
--php-versions=8.4,8.5 Target PHP version columns |
|
||||||
--no-phpunit Do not scan PHPUnit compiler fixtures |
|
||||||
--strict Fail on parse issues or unresolved fixture links |
|
||||||
-h, --help Show this help |
|
||||||
|
|
||||||
Examples: |
|
||||||
php {$script} |
|
||||||
php {$script} --format=markdown --output=build/test-coverage.md |
|
||||||
php {$script} --format=json tests/compiler/type_decl tests/compiler/basic |
|
||||||
|
|
||||||
The tool reports separate, explicitly denominated AST-node, positive compile, |
|
||||||
runtime semantic and negative diagnostic coverage. It never emits a combined |
|
||||||
overall percentage. |
|
||||||
|
|
||||||
USAGE; |
|
||||||
} |
|
||||||
|
|
||||||
function isAbsolutePath(string $path): bool |
|
||||||
{ |
|
||||||
return $path !== '' && ($path[0] === '/' || preg_match('/^[A-Za-z]:[\\\\\/]/', $path) === 1); |
|
||||||
} |
|
||||||
|
|
||||||
function relativePath(string $root, string $path): string |
|
||||||
{ |
|
||||||
$prefix = rtrim($root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
||||||
return str_starts_with($path, $prefix) ? substr($path, strlen($prefix)) : $path; |
|
||||||
} |
|
||||||
@ -1,178 +0,0 @@ |
|||||||
#!/usr/bin/env bash |
|
||||||
|
|
||||||
set -euo pipefail |
|
||||||
|
|
||||||
readonly MIN_AGE_MINUTES=60 |
|
||||||
|
|
||||||
dry_run=false |
|
||||||
tmp_root=/tmp |
|
||||||
|
|
||||||
usage() { |
|
||||||
cat <<'EOF' |
|
||||||
Usage: ./cleanup-typephp-tmp.sh [options] |
|
||||||
|
|
||||||
Remove inactive TypePHP temporary files and directories from /tmp. |
|
||||||
An entry is skipped when it or any of its descendants was modified or |
|
||||||
metadata-changed during the last 60 minutes. |
|
||||||
Recognized prefixes: typephp-, typephp_, utils_test_, and phpx-windows. |
|
||||||
|
|
||||||
Options: |
|
||||||
-n, --dry-run Show what would be removed without deleting anything |
|
||||||
--tmp-dir DIR Use another temporary directory (primarily for testing) |
|
||||||
-h, --help Show this help |
|
||||||
EOF |
|
||||||
} |
|
||||||
|
|
||||||
while (($# > 0)); do |
|
||||||
case "$1" in |
|
||||||
-n | --dry-run) |
|
||||||
dry_run=true |
|
||||||
;; |
|
||||||
--tmp-dir) |
|
||||||
if (($# < 2)); then |
|
||||||
echo "Error: --tmp-dir requires a directory." >&2 |
|
||||||
exit 2 |
|
||||||
fi |
|
||||||
tmp_root=$2 |
|
||||||
shift |
|
||||||
;; |
|
||||||
-h | --help) |
|
||||||
usage |
|
||||||
exit 0 |
|
||||||
;; |
|
||||||
*) |
|
||||||
echo "Error: unknown option: $1" >&2 |
|
||||||
usage >&2 |
|
||||||
exit 2 |
|
||||||
;; |
|
||||||
esac |
|
||||||
shift |
|
||||||
done |
|
||||||
|
|
||||||
if [[ ! -d "$tmp_root" ]]; then |
|
||||||
echo "Error: temporary directory does not exist: $tmp_root" >&2 |
|
||||||
exit 1 |
|
||||||
fi |
|
||||||
|
|
||||||
tmp_root=$(realpath -e -- "$tmp_root") |
|
||||||
if [[ -z "$tmp_root" || "$tmp_root" == / ]]; then |
|
||||||
echo "Error: refusing to use an unsafe temporary directory." >&2 |
|
||||||
exit 1 |
|
||||||
fi |
|
||||||
|
|
||||||
readonly tmp_root |
|
||||||
readonly owner_uid=${SUDO_UID:-$(id -u)} |
|
||||||
|
|
||||||
format_size() { |
|
||||||
local kib=$1 |
|
||||||
awk -v kib="$kib" 'BEGIN { |
|
||||||
if (kib >= 1048576) { |
|
||||||
printf "%.2f GiB", kib / 1048576 |
|
||||||
} else if (kib >= 1024) { |
|
||||||
printf "%.2f MiB", kib / 1024 |
|
||||||
} else { |
|
||||||
printf "%d KiB", kib |
|
||||||
} |
|
||||||
}' |
|
||||||
} |
|
||||||
|
|
||||||
entry_size_kib() { |
|
||||||
local output |
|
||||||
output=$(du -sk -- "$1" 2>/dev/null) || { |
|
||||||
printf '0' |
|
||||||
return |
|
||||||
} |
|
||||||
printf '%s' "${output%%$'\t'*}" |
|
||||||
} |
|
||||||
|
|
||||||
has_recent_entry() { |
|
||||||
local candidate=$1 |
|
||||||
local recent |
|
||||||
|
|
||||||
# Check the complete tree. Looking only at the top-level directory mtime |
|
||||||
# would miss writes to an existing file in a nested build directory. |
|
||||||
if ! recent=$(find -P "$candidate" -xdev \ |
|
||||||
\( -mmin "-${MIN_AGE_MINUTES}" -o -cmin "-${MIN_AGE_MINUTES}" \) \ |
|
||||||
-printf '1' -quit 2>/dev/null); then |
|
||||||
return 0 |
|
||||||
fi |
|
||||||
|
|
||||||
[[ -n "$recent" ]] |
|
||||||
} |
|
||||||
|
|
||||||
matched_count=0 |
|
||||||
removed_count=0 |
|
||||||
skipped_recent_count=0 |
|
||||||
skipped_error_count=0 |
|
||||||
total_kib=0 |
|
||||||
|
|
||||||
while IFS= read -r -d '' candidate; do |
|
||||||
((matched_count += 1)) |
|
||||||
|
|
||||||
# Keep the target constrained to one direct child of the selected root. |
|
||||||
if [[ "$candidate" != "$tmp_root"/* || "${candidate%/*}" != "$tmp_root" ]]; then |
|
||||||
echo "[skip unsafe] $candidate" >&2 |
|
||||||
((skipped_error_count += 1)) |
|
||||||
continue |
|
||||||
fi |
|
||||||
|
|
||||||
if has_recent_entry "$candidate"; then |
|
||||||
echo "[skip recent] $candidate" |
|
||||||
((skipped_recent_count += 1)) |
|
||||||
continue |
|
||||||
fi |
|
||||||
|
|
||||||
size_kib=$(entry_size_kib "$candidate") |
|
||||||
size=$(format_size "$size_kib") |
|
||||||
|
|
||||||
# The size scan can take noticeable time for a large build tree. Recheck |
|
||||||
# freshness immediately before acting in case a compiler started using it. |
|
||||||
if has_recent_entry "$candidate"; then |
|
||||||
echo "[skip recent] $candidate" |
|
||||||
((skipped_recent_count += 1)) |
|
||||||
continue |
|
||||||
fi |
|
||||||
|
|
||||||
if $dry_run; then |
|
||||||
echo "[would remove] $size $candidate" |
|
||||||
((removed_count += 1)) |
|
||||||
((total_kib += size_kib)) |
|
||||||
continue |
|
||||||
fi |
|
||||||
|
|
||||||
if [[ -d "$candidate" && ! -L "$candidate" ]]; then |
|
||||||
if rm -rf --one-file-system -- "$candidate"; then |
|
||||||
echo "[removed] $size $candidate" |
|
||||||
((removed_count += 1)) |
|
||||||
((total_kib += size_kib)) |
|
||||||
else |
|
||||||
echo "[skip error] failed to remove: $candidate" >&2 |
|
||||||
((skipped_error_count += 1)) |
|
||||||
fi |
|
||||||
elif rm -f -- "$candidate"; then |
|
||||||
echo "[removed] $size $candidate" |
|
||||||
((removed_count += 1)) |
|
||||||
((total_kib += size_kib)) |
|
||||||
else |
|
||||||
echo "[skip error] failed to remove: $candidate" >&2 |
|
||||||
((skipped_error_count += 1)) |
|
||||||
fi |
|
||||||
done < <( |
|
||||||
find -P "$tmp_root" -mindepth 1 -maxdepth 1 -uid "$owner_uid" \ |
|
||||||
\( -name 'typephp-*' -o -name 'typephp_*' \ |
|
||||||
-o -name 'utils_test_*' -o -name 'phpx-windows*' \) -print0 |
|
||||||
) |
|
||||||
|
|
||||||
if $dry_run; then |
|
||||||
action='would remove' |
|
||||||
else |
|
||||||
action='removed' |
|
||||||
fi |
|
||||||
|
|
||||||
printf 'Summary: matched %d, %s %d (%s), skipped recent %d, errors %d.\n' \ |
|
||||||
"$matched_count" "$action" "$removed_count" "$(format_size "$total_kib")" \ |
|
||||||
"$skipped_recent_count" "$skipped_error_count" |
|
||||||
|
|
||||||
if ((skipped_error_count > 0)); then |
|
||||||
exit 1 |
|
||||||
fi |
|
||||||
@ -1,151 +0,0 @@ |
|||||||
# Bash completion for the TypePHP compiler. |
|
||||||
# Generated by: tpc --generate-completion=bash |
|
||||||
|
|
||||||
_typephp_tpc_complete_files() |
|
||||||
{ |
|
||||||
local candidate |
|
||||||
COMPREPLY=() |
|
||||||
while IFS= read -r candidate; do |
|
||||||
if [[ -d "$candidate" || "$candidate" == *.php || "$candidate" == *.yml || "$candidate" == *.yaml || "$candidate" == *.prof ]]; then |
|
||||||
COMPREPLY+=("$candidate") |
|
||||||
fi |
|
||||||
done < <(compgen -f -- "$1") |
|
||||||
} |
|
||||||
|
|
||||||
_typephp_tpc_complete_python_files() |
|
||||||
{ |
|
||||||
local candidate |
|
||||||
COMPREPLY=() |
|
||||||
while IFS= read -r candidate; do |
|
||||||
if [[ -d "$candidate" || "$candidate" == *.py ]]; then |
|
||||||
COMPREPLY+=("$candidate") |
|
||||||
fi |
|
||||||
done < <(compgen -f -- "$1") |
|
||||||
} |
|
||||||
|
|
||||||
_typephp_tpc_complete_paths() |
|
||||||
{ |
|
||||||
local mode="$1" candidate |
|
||||||
COMPREPLY=() |
|
||||||
shift |
|
||||||
while IFS= read -r candidate; do |
|
||||||
COMPREPLY+=("$candidate") |
|
||||||
done < <(compgen "$mode" -- "$1") |
|
||||||
} |
|
||||||
|
|
||||||
_typephp_tpc() |
|
||||||
{ |
|
||||||
local current previous value candidate |
|
||||||
current="${COMP_WORDS[COMP_CWORD]}" |
|
||||||
previous="" |
|
||||||
if (( COMP_CWORD > 0 )); then |
|
||||||
previous="${COMP_WORDS[COMP_CWORD - 1]}" |
|
||||||
fi |
|
||||||
|
|
||||||
local index |
|
||||||
for ((index = 1; index < COMP_CWORD; index++)); do |
|
||||||
if [[ "${COMP_WORDS[index]}" == -- ]]; then |
|
||||||
compopt -o default |
|
||||||
return |
|
||||||
fi |
|
||||||
done |
|
||||||
|
|
||||||
case "$previous" in |
|
||||||
-O) |
|
||||||
COMPREPLY=( $(compgen -W '0 1 2 3' -- "$current") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
--optimize) |
|
||||||
COMPREPLY=( $(compgen -W '0 1 2 3' -- "$current") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
-m) |
|
||||||
COMPREPLY=( $(compgen -W 'bin lib ext' -- "$current") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
--mode) |
|
||||||
COMPREPLY=( $(compgen -W 'bin lib ext' -- "$current") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
--php-version) |
|
||||||
COMPREPLY=( $(compgen -W '8.4 8.5' -- "$current") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
--cxx-std) |
|
||||||
COMPREPLY=( $(compgen -W 'c++17 c++20 c++23' -- "$current") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
--sanitize) |
|
||||||
COMPREPLY=( $(compgen -W 'address undefined' -- "$current") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
--build-dir|--output-dir|-I|--include-path|-L|--link-path) |
|
||||||
compopt -o filenames |
|
||||||
_typephp_tpc_complete_paths -d "$current" |
|
||||||
return |
|
||||||
;; |
|
||||||
--convert-python-to-php) |
|
||||||
compopt -o filenames |
|
||||||
_typephp_tpc_complete_python_files "$current" |
|
||||||
return |
|
||||||
;; |
|
||||||
-o|--output) |
|
||||||
compopt -o filenames |
|
||||||
_typephp_tpc_complete_paths -f "$current" |
|
||||||
return |
|
||||||
;; |
|
||||||
esac |
|
||||||
|
|
||||||
case "$current" in |
|
||||||
-O[0-3]) |
|
||||||
COMPREPLY=("$current") |
|
||||||
return |
|
||||||
;; |
|
||||||
-O*) |
|
||||||
value="${current#-O}" |
|
||||||
COMPREPLY=( $(compgen -W '0 1 2 3' -P '-O' -- "$value") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
--wasm=*) |
|
||||||
value="${current#--wasm=}" |
|
||||||
COMPREPLY=( $(compgen -W 'component browser' -P '--wasm=' -- "$value") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
--generate-completion=*) |
|
||||||
value="${current#--generate-completion=}" |
|
||||||
COMPREPLY=( $(compgen -W 'bash' -P '--generate-completion=' -- "$value") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
--build-dir=*) |
|
||||||
value="${current#--build-dir=}" |
|
||||||
compopt -o filenames |
|
||||||
COMPREPLY=() |
|
||||||
while IFS= read -r candidate; do |
|
||||||
COMPREPLY+=("--build-dir=${candidate}") |
|
||||||
done < <(compgen -d -- "$value") |
|
||||||
return |
|
||||||
;; |
|
||||||
--output-dir=*) |
|
||||||
value="${current#--output-dir=}" |
|
||||||
compopt -o filenames |
|
||||||
COMPREPLY=() |
|
||||||
while IFS= read -r candidate; do |
|
||||||
COMPREPLY+=("--output-dir=${candidate}") |
|
||||||
done < <(compgen -d -- "$value") |
|
||||||
return |
|
||||||
;; |
|
||||||
-* ) |
|
||||||
COMPREPLY=( $(compgen -W '-O --optimize -o --output -h --help -v --version --profile --no-literal-strings --php-version -f --force -m --mode -r --run --debug -j --job --no-console --sanitize --cxx-std --march --target-platform --no-color --build-dir --dry -I --include-path -D --define --no-progress --lto --format -l --link-lib -L --link-path --wasm --wasm= --gen-python-helper --convert-python-to-php --output-dir --output-dir= --build-dir= --generate-completion=' -- "$current") ) |
|
||||||
return |
|
||||||
;; |
|
||||||
esac |
|
||||||
|
|
||||||
compopt -o filenames |
|
||||||
_typephp_tpc_complete_files "$current" |
|
||||||
} |
|
||||||
|
|
||||||
complete -F _typephp_tpc tpc |
|
||||||
complete -F _typephp_tpc ./tpc |
|
||||||
complete -F _typephp_tpc tpc.php |
|
||||||
complete -F _typephp_tpc bin/tpc.php |
|
||||||
complete -F _typephp_tpc ./bin/tpc.php |
|
||||||
@ -1,42 +0,0 @@ |
|||||||
# `#[ArrayDef]` compile-time array contracts |
|
||||||
|
|
||||||
`#[ArrayDef]` attaches key/value type information to a property declared |
|
||||||
exactly as `array`. It supports both Zend classes and `#[Native]` classes and |
|
||||||
has no runtime metadata or per-read overhead. |
|
||||||
|
|
||||||
```php |
|
||||||
class Index |
|
||||||
{ |
|
||||||
#[ArrayDef(Type::String)] |
|
||||||
public array $names = []; // list<string> |
|
||||||
|
|
||||||
#[ArrayDef(Type::Int, Type::String)] |
|
||||||
public array $labels = []; // map<int, string> |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
One argument defines a list value type. Two arguments define a map key type |
|
||||||
and value type. Map keys are restricted to `Type::Int` or `Type::String`. |
|
||||||
`ClassName::class` is therefore valid only as a list element type or as the |
|
||||||
second (value) argument of a map. |
|
||||||
|
|
||||||
For direct writes whose expression types are known, the compiler either emits |
|
||||||
the normal write unchanged or reports a fatal type error. An `any` key/value is |
|
||||||
checked with PHPX exact-type helpers at runtime. No coercive `intval()` or |
|
||||||
string conversion is performed. |
|
||||||
|
|
||||||
List writes support `[]` and non-negative integer indexes up to PHP's current |
|
||||||
append position. Indexed writes uniformly emit `php::safeArrayIndex(index, |
|
||||||
array)`. The helper uses `zend_hash_next_free_element()` and follows the |
|
||||||
initial-index rule of `zend_hash_next_index_insert()`, which remains correct |
|
||||||
when `unset()` has created holes or removed the highest numeric key. An index |
|
||||||
equal to that value behaves like the next `$array[]` append; earlier indexes |
|
||||||
may update or refill an element. Negative indexes and indexes beyond the append |
|
||||||
position fail at runtime. There is no AST special case for |
|
||||||
`property[count(property)]`. Maps do not support `[]` append writes. |
|
||||||
|
|
||||||
The contract intentionally applies only to direct element assignment lowered |
|
||||||
by TypePHP. Reads and in-place operators are unchanged. Values passed through |
|
||||||
dynamic functions, callbacks, Reflection, `eval()`, or other ZendVM escape |
|
||||||
paths are outside the contract and have undefined behavior from ArrayDef's |
|
||||||
perspective. |
|
||||||
@ -0,0 +1,224 @@ |
|||||||
|
# TypePHP 编译实现分析、提速方案与 mago 替代评估 |
||||||
|
|
||||||
|
> 生成日期:2026-08-03 |
||||||
|
> 适用范围:`swoole/typephp` v0.4.3(本仓库 `D:\git\php\aot-compiler`) |
||||||
|
> 说明:所有耗时数字均来自本仓库实际基准测试(`src/` 下 139 个 PHP 文件、约 1.65 MB 源码,PHP 8.4.16 CLI;端到端对比用 302 文件合成项目,Windows + MSVC) |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## 0. 实施状态(2026-08-03 更新) |
||||||
|
|
||||||
|
**S1(合并两次解析)与 S2(writeFile 内容比对 + 生成 .cc 增量缓存)已实施并验证。** |
||||||
|
|
||||||
|
### 已落地的改动 |
||||||
|
|
||||||
|
| 文件 | 改动 | |
||||||
|
|---|---| |
||||||
|
| `src/CompilerBase.php` | 新增 `parseCachedAst()` / `cloneAst()`(AST 内存缓存 + CloningVisitor 深拷贝);`writeFile()` 增加内容比对(内容相同不覆盖,返回 bool 保持 mtime 稳定) | |
||||||
|
| `src/Preprocessor.php` | `prepareFile()` 改用 `parseCachedAst()`,消除与 convert 的重复 parse | |
||||||
|
| `src/Translator.php` | `doConvert()` 改用 `parseCachedAst()`;新增 `hasGeneratedObjectFileCache()` / `getGeneratedObjectCacheKey()` / `getGeneratedHeaderDependencies()` / `isGeneratedSourceFile()`;`compileFile()` 对生成的 .cc 走对象缓存;`save()`/`genExtension()` 仅在实际写入时 format | |
||||||
|
|
||||||
|
### 实测收益(302 文件合成项目,dry 模式,3 次取均值) |
||||||
|
|
||||||
|
| 场景 | 基线 | 改动后 | 提升 | |
||||||
|
|---|---|---|---| |
||||||
|
| 全量构建(冷) | 6011 ms | 5629 ms | **-6.4%** | |
||||||
|
| 增量构建(第 3 次,不清理 build) | — | 5023 ms | **-16.4%**(相对基线冷构建) | |
||||||
|
|
||||||
|
说明:dry 模式不包含 C++ 编译,S2 对象缓存的更大收益(跳过 g++/cl 编译)需在完整 build 场景体现;dry 耗时中固定开销(CLI 启动、YAML、符号表)占比高,稀释了 S1 的解析节省。 |
||||||
|
|
||||||
|
### 验证记录 |
||||||
|
|
||||||
|
- `parseCachedAst`:同一文件 prepare+convert 各调用一次,第二次命中缓存且返回深拷贝(不共享节点、无 resolvedName 残留污染)✓ |
||||||
|
- `writeFile` 内容比对:相同内容跳过写入,mtime 保持不变 ✓ |
||||||
|
- `isGeneratedSourceFile`:buildDir 前缀 + .cc 扩展名判断,与 `getCppFile` 路径一致 ✓ |
||||||
|
- PHPUnit:Entity 目录 85 测试、FileScanner 等 110 测试通过(1 个 Windows 路径分隔符既有失败与本次无关) |
||||||
|
- `typephp-think`(554 文件):补丁后 prepare 通过,convert 阶段因「仓库源码类型系统比 v1095 正式版严格」(`Cannot re-assign`)受阻,属源码版本差异,非本次改动问题 |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## 1. 编译是如何实现的(管线剖析) |
||||||
|
|
||||||
|
### 1.1 总体流程 |
||||||
|
|
||||||
|
入口为 `bin/tpc.php` → `src/compiler.php` 的 `main()` → `src/Translator.php`(继承 `Preprocessor` → `CompilerBase`)。编译是一条四阶段流水线: |
||||||
|
|
||||||
|
``` |
||||||
|
┌────────────────────────────────────────────────────────────────────────┐ |
||||||
|
│ ① prepare() 扫描+解析+建符号表+依赖拓扑排序 │ |
||||||
|
│ src/Build/SourcePipelineTrait.php::prepare() │ |
||||||
|
│ → 每文件: Preprocessor::prepareFile() │ |
||||||
|
│ parser->parse() ←── AST 解析 #1 │ |
||||||
|
│ NodeTraverser(NameResolver + Visitor + 常量校验 + AttributeLower) │ |
||||||
|
│ 收集 symbolDeclInFile / symbolCallInFile / classMap / funcMap │ |
||||||
|
│ → getSortedFiles(): marcj/topsort 按文件间符号依赖拓扑排序 │ |
||||||
|
├────────────────────────────────────────────────────────────────────────┤ |
||||||
|
│ ② convert() 每文件: AST → C++ 源码 │ |
||||||
|
│ src/Build/SourcePipelineTrait.php::convert() │ |
||||||
|
│ → Translator::convertFile() → doConvert() │ |
||||||
|
│ loadFile() 读盘(第二次) │ |
||||||
|
│ parser->parse() ←── AST 解析 #2(同一文件解析两次!) │ |
||||||
|
│ NodeTraverser 再次遍历(与 prepare 相同的 4 个 visitor) │ |
||||||
|
│ 逐节点翻译生成 C++(CompilerBase 4000 行核心逻辑) │ |
||||||
|
│ → 生成 func_decl.h / data_decl.h / extension-<target>.cc │ |
||||||
|
├────────────────────────────────────────────────────────────────────────┤ |
||||||
|
│ ③ compile() C++ → .o(外部编译器) │ |
||||||
|
│ Translator::compile() │ |
||||||
|
│ → phpx misc 文件 + 生成的 .cc │ |
||||||
|
│ → PCH 预编译头(PrecompiledHeaderManager,内容指纹缓存) │ |
||||||
|
│ → Linux/macOS: pcntl fork 并行(默认 4 jobs,大文件优先调度) │ |
||||||
|
│ → Windows: 串行(supportsPcntlParallelCompile() = false) │ |
||||||
|
│ → phpx misc 有 .o 缓存(hasMiscObjectFileCache,命令+ABI 指纹) │ |
||||||
|
├────────────────────────────────────────────────────────────────────────┤ |
||||||
|
│ ④ build() 链接为可执行文件 / 扩展 / 库 │ |
||||||
|
│ Translator::build() → NativeBuilder::link() │ |
||||||
|
└────────────────────────────────────────────────────────────────────────┘ |
||||||
|
``` |
||||||
|
|
||||||
|
### 1.2 关键实现位置 |
||||||
|
|
||||||
|
| 职责 | 文件 | |
||||||
|
|---|---| |
||||||
|
| 四阶段编排 | `src/Build/SourcePipelineTrait.php`(prepare/convert)、`src/Translator.php`(compile/build) | |
||||||
|
| PHP→C++ 逐节点翻译 | `src/CompilerBase.php`(4133 行,核心) | |
||||||
|
| 扫描/符号表/依赖排序 | `src/Preprocessor.php` | |
||||||
|
| 编译器抽象 | `src/Backend/`(CompilerBackend → GccLikeBackend → Gcc/Clang、Msvc) | |
||||||
|
| 平台抽象 | `src/Platform/`(Linux/Macos/Windows) | |
||||||
|
| 并行编译调度 | `src/Build/NativeBuilder.php::dispatchParallel()` + `SourceCompileQueue` | |
||||||
|
| PCH 管理 | `src/Build/PrecompiledHeaderManager.php` | |
||||||
|
| 生成物落盘 | `CompilerBase::writeFile()`(无内容比对,直接覆盖) | |
||||||
|
|
||||||
|
### 1.3 当前已具备的优化 |
||||||
|
|
||||||
|
1. **PCH 预编译头**:`phpx.h` 等 8 个公共头打成 `.gch`,带 24 位 sha256 内容指纹缓存(`PrecompiledHeaderManager`)。 |
||||||
|
2. **phpx misc 对象缓存**:`phpx/src/misc/*.cc` 的 `.o` 用「编译命令 + PHP ABI」指纹缓存,避免每次全量重编 PHPX 运行时。 |
||||||
|
3. **编译并行**:Linux/macOS 用 pcntl fork 并行(`-j`,默认 4),且按「大文件优先占满槽位 + 小文件快速通道」调度。 |
||||||
|
4. **编译期常量展开、字面量字符串表**、`--dry` 只生成 C++ 等。 |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## 2. 性能瓶颈实测 |
||||||
|
|
||||||
|
### 2.1 基准方法 |
||||||
|
|
||||||
|
用 `nikic/php-parser 5.6.1`(与仓库 `composer.json` 一致)对 `src/` 全部 139 个 PHP 文件(约 1.65 MB)做多轮测量: |
||||||
|
|
||||||
|
``` |
||||||
|
parse (nikic/php-parser) ×3 1455.5 ms → 单遍 ≈ 485 ms(3.18 MB/s) |
||||||
|
parse + NameResolver ×3 1797.0 ms → 单遍 ≈ 599 ms |
||||||
|
parse+resolve+serialize (1x) 701.1 ms |
||||||
|
unserialize ×3 779.8 ms → 单遍 ≈ 260 ms |
||||||
|
parse+resolve+prettyPrint ×3 2196.0 ms |
||||||
|
serialized AST 体积 47.6 MB(源码的 28.8 倍) |
||||||
|
``` |
||||||
|
|
||||||
|
### 2.2 结论性瓶颈清单(按影响排序) |
||||||
|
|
||||||
|
| # | 瓶颈 | 证据 | 影响 | |
||||||
|
|---|---|---|---| |
||||||
|
| B1 | **每个 PHP 文件被解析两次** | `prepareFile()`(Preprocessor.php:130)与 `doConvert()`(Translator.php:2325)各自 `parser->parse()` 一次,且各自跑一遍完整 NodeTraverser | PHP 侧最大浪费:139 文件规模下纯解析+resolve ≈ 2×599ms ≈ **1.2s 起步**,大项目线性放大 | |
||||||
|
| B2 | **生成的 .cc 无增量缓存** | `compileFile()` 只对 phpx misc 走 `hasMiscObjectFileCache`;用户代码生成的 `.cc` 每次全量重编 | 改 1 个 PHP 文件也要重编全部 .cc(C++ 编译是耗时的绝对大头) | |
||||||
|
| B3 | **生成文件无内容比对** | `CompilerBase::writeFile()` 直接 `file_put_contents` 覆盖,mtime 必然变化 | 即使内容相同也触发下游重新依赖判断/重编,破坏增量可能性 | |
||||||
|
| B4 | **prepare/convert 阶段完全串行** | 两阶段都是单进程 `foreach` | 多核只用于 compile;PHP 侧解析/翻译占用的核数为 1 | |
||||||
|
| B5 | **Windows 无并行编译** | `Windows::supportsPcntlParallelCompile() = false`,直接走 `compileSourceFile()` 串行 | Windows 大项目编译时间 = 串行全量 | |
||||||
|
| B6 | **PCH/缓存指纹每次全量计算** | `PrecompiledHeaderManager::buildFingerprint()` 每次构建都递归遍历 phpx 头文件并逐个 hash(实测 131 个头文件 ≈ 42ms,PHP 侧还有 misc 缓存同样的遍历) | 每次构建的固定开销,虽不大但在累积 | |
||||||
|
| B7 | 转换阶段反复 `getType()` 字符串比较、NodeFinder 多次全树扫描(如 `findSymbolUsing`、`canOptimizeMultiReturn`) | CompilerBase/Preprocessor 中多次独立 `findInstanceOf` | 每文件多次 O(N) 全树扫描,可合并为一次遍历 | |
||||||
|
|
||||||
|
### 2.3 一个反直觉的事实 |
||||||
|
|
||||||
|
**AST 序列化缓存是划算的**:unserialize 单遍 260ms vs parse+resolve 599ms(快 2.3 倍),即使加上 serialize 成本(首轮 ~700ms/139 文件),二次构建纯 PHP 解析阶段也能从 ~1.2s 降到 ~260ms。但注意序列化体积膨胀 28.8 倍(47.6MB/1.65MB),对磁盘 I/O 有压力——更优做法是**合并两阶段解析**(见方案 S1)。 |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## 3. 提速方案(按性价比排序) |
||||||
|
|
||||||
|
### S1(P0)prepare/convert 合并为单次解析 —— 省一半 PHP 解析时间 |
||||||
|
|
||||||
|
- **问题**:同一文件 `prepare` 和 `convert` 各 parse+traverse 一次,逻辑完全重复(visitor 列表一致)。 |
||||||
|
- **做法**:让 `prepareFile()` 把 parse 好的 `$stmts`(NameResolver 等已跑完)挂到文件缓存(内存 `array<string, array<Node>>`,或按 `filemtime+内容hash` 落盘 `build/cache/ast/`),`doConvert()` 优先取缓存;取不到(如 `--force`、跨进程)才重新 parse。 |
||||||
|
- **收益**:139 文件规模 PHP 解析从 ~1.2s → ~0.6s;大项目直接减半。 |
||||||
|
- **风险**:低。`convert` 依赖的 `resolvedName` 等 attribute 由 NameResolver 在 traverse 时写入,缓存的是 traverse 之后的 AST,语义不变。需注意 `ConstantExpressionValidationVisitor` 的报错回调在 prepare 是 warning、convert 是 fatal,缓存后错误在 prepare 阶段就能暴露,行为只会更早更一致。 |
||||||
|
|
||||||
|
### S2(P0)生成 .cc 的增量对象缓存 —— 编译期最大收益 |
||||||
|
|
||||||
|
- **做法**:把 `hasMiscObjectFileCache()` 的机制推广到所有生成的 `.cc`:缓存 key = 编译命令 + PHP ABI + **生成物内容 hash(或源 PHP hash + 编译器版本)**。内容未变 → 跳过 g++/clang。 |
||||||
|
- **配套**:`writeFile()` 加「内容比对,相同则不落盘」逻辑(解决 B3),保证 mtime 稳定。 |
||||||
|
- **收益**:改 1 个文件后二次构建,C++ 编译从全量变为仅差异文件;这是肉眼可感知的最大提升。 |
||||||
|
- **风险**:中。生成 .cc 的内容由 `genFunctionDeclarations`/`genDataDeclarations` 等公共头影响——若公共头变了,所有 .cc 的 `#include` 语义可能变,但 .cc 内容 hash 不会变。**必须把公共声明头也纳入 key**(hash func_decl.h/data_decl.h/extension 文件内容),或采用「.cc 内容未变 且 公共头未变 且 .o 存在 且 命令指纹未变」四条件跳过。 |
||||||
|
|
||||||
|
### S3(P0)修复 PCH/缓存指纹的计算时机 |
||||||
|
|
||||||
|
- `buildFingerprint()` 每次构建全量扫描+hash 头文件(~42ms + misc 缓存同款遍历),应改为「mtime+size 列表指纹」先行判断,内容 hash 仅在前者变化时执行。 |
||||||
|
|
||||||
|
### S4(P1)prepare/convert 并行化 |
||||||
|
|
||||||
|
- parse+resolve 是纯函数,可用 pcntl fork 分片解析(子进程只做 parse+serialize,父进程归并反序列化),类似已有 `dispatchParallel` 的模式。转换阶段因共享状态(literalStrings、classMap 等)风险高,可先只并行「解析」,翻译仍串行。 |
||||||
|
|
||||||
|
### S5(P1)Windows 并行编译 |
||||||
|
|
||||||
|
- 两条路:(a) 给 `Msvc` 后端加 `/MP`(MSVC 原生多进程编译,单命令即可);(b) Windows 上用 `proc_open` 实现多进程调度替代 pcntl。收益对 Windows 用户显著。 |
||||||
|
|
||||||
|
### S6(P2)工具链层 |
||||||
|
|
||||||
|
- `ccache`/`sccache` 兜底(对 C++ 编译做内容缓存,配合 S2 效果更稳);链接器换 `mold`/`lld`;`-pipe`、`-fno-asynchronous-unwind-tables` 等编译 flag 微调。 |
||||||
|
|
||||||
|
### S7(P2)单次遍历替代多次 NodeFinder |
||||||
|
|
||||||
|
- `findSymbolUsing()` 等每文件多次 `findInstanceOf` 全树扫描,可合并为一次 visit 收集函数调用/类引用/常量引用三类符号,减少 O(N) 重复。 |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## 4. AST 解析能否用 mago 替代?—— 结论:不建议(现阶段不可行) |
||||||
|
|
||||||
|
### 4.1 mago 是什么 |
||||||
|
|
||||||
|
[mago](https://github.com/carthage-software/mago)(carthage-software/mago)是一个 **Rust 编写的 PHP 工具链**:lint、format、静态分析(对标 Clippy/OXC),v1.45.0(2026-07),MIT/Apache-2.0 双许可,开发活跃。其核心是自研高性能 PHP 解析器(crate:`mago-syntax` 词法/语法/遍历、`mago-names` 名称解析),官方基准:7M 行 WordPress 静态分析 1.46s(PHPStan 55.9s)、lint 0.88s、format 0.43s。**已支持 PHP 8.4 property hooks、asymmetric visibility(private(set) 等)。** |
||||||
|
|
||||||
|
### 4.2 为什么不能直接替代 |
||||||
|
|
||||||
|
| 维度 | nikic/php-parser(现状) | mago | 兼容性判断 | |
||||||
|
|---|---|---|---| |
||||||
|
| 语言/进程 | PHP 类库,进程内对象 | Rust 二进制/crate,AST 是 Rust 结构 | 无法在 PHP 进程中直接消费 | |
||||||
|
| AST 模型 | AST(抽象语法树),含 attribute 机制(`resolvedName`/`namespacedName` 等存于节点属性) | CST(具体语法树,含 trivia),`mago ast --json` 是调试输出 | 结构不同,需要完整映射层 | |
||||||
|
| 下游依赖 | TypePHP 全库 296 处 `PhpParser\*` 引用、75 个文件;依赖 `NodeTraverser`/`NodeVisitor`/`NameResolver`/`NodeFinder`/`ConstExprEvaluator`/`PrettyPrinter`/`Modifiers` 等 | 无 PHP API | 替换 = 重写编译器前端 | |
||||||
|
| 名称解析 | `NameResolver` 写入节点 attribute,后续代码生成大量读取 | `mago-names` 输出到 Rust 侧 | 需要桥接层搬运 | |
||||||
|
| 代码生成 | `PrettyPrinter` 用于 stub 生成(`LibraryImportStubGenerator`、`gen_stub.php`) | 无等价物 | 缺失 | |
||||||
|
| 错误语义 | `PhpParser\Error` + 行列号,被 `SyntaxError` 包装 | 错误模型不同 | 需要适配 | |
||||||
|
|
||||||
|
**核心矛盾**:TypePHP 不是「解析完就丢」的 linter,而是「AST 必须留在 PHP 进程内、被 4000 行 CompilerBase 逐节点翻译」的编译器。走 mago 只有两条路,都不可行: |
||||||
|
|
||||||
|
1. **mago CLI → JSON AST → PHP 重建对象**:JSON 序列化体积远大于源码(参考第 2 节:php-parser AST 序列化已膨胀 28.8 倍,mago CST 只多不少),反序列化 + 对象重建的 CPU/内存开销会**吃掉**解析省下的时间,且每文件一次进程启动(exec)成本极高;同时丢失 attribute 机制、注释/行列语义需重新映射。 |
||||||
|
2. **PHP FFI 绑定 mago-syntax crate**:需要为整个 Rust AST 写 FFI 边界 + 转 PHP 对象层,工程量接近重写前端,且 mago 的 CST 与 TypePHP 需要的「带 resolvedName 的 AST」不是一回事,仍要在 PHP 侧自建名称解析与代码生成数据模型。 |
||||||
|
|
||||||
|
### 4.3 mago 在本项目中的合理定位(可选) |
||||||
|
|
||||||
|
- **前置快速语法校验**:编译前用 `mago`(或 `mago lint --quick`)做廉价的全项目语法/明显错误预检,给出比 php-parser 更快的反馈(适合 IDE/CI 场景,不进入编译流水线核心)。 |
||||||
|
- **性能参照物**:mago 证明了「Rust 原生解析」的量级(~百 MB/s vs php-parser 3 MB/s),可作为未来「把前端重写为原生编译器(如 Rust 侧做 parse+name-resolution,产物是自研序列化 AST)」的路线参考——但那是一个全新项目,而非对现有编译器的增量改造。 |
||||||
|
- **若仍想引入原生解析**:更现实的是保留 php-parser 语义、仅加速解析环节(如 `--php-parser` 的 `PhpParser\ParserFactory` 换用 tree-sitter 绑定并在 PHP 侧重建 php-parser 节点),但收益-成本比仍不如 S1/S2。 |
||||||
|
|
||||||
|
### 4.4 结论 |
||||||
|
|
||||||
|
> **不建议用 mago 替换 nikic/php-parser**。mago 是「解析即弃」的工具链,TypePHP 是「解析后深度消费 AST」的编译器,二者 AST 模型与进程模型不兼容,桥接成本会抹平解析速度优势。 |
||||||
|
> 真正的提速路径在 **S1(两阶段合并单次解析)+ S2(生成 .cc 增量缓存 + writeFile 内容比对)+ S4/S5(并行化)**,全部基于现有架构增量改造,风险可控、收益立竿见影。 |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## 5. 落地顺序建议 |
||||||
|
|
||||||
|
``` |
||||||
|
第一阶段(P0,改动小收益大,1-2 天) |
||||||
|
1. writeFile 内容比对(S2 前置) |
||||||
|
2. prepare/convert 共享 AST(S1) |
||||||
|
3. 生成 .cc 增量对象缓存(S2,key 含公共头内容 hash) |
||||||
|
第二阶段(P1,结构性收益) |
||||||
|
4. PCH/misc 指纹轻量化(S3) |
||||||
|
5. 解析阶段 pcntl 并行(S4 解析部分) |
||||||
|
6. Windows 并行(/MP 或 proc_open)(S5) |
||||||
|
第三阶段(P2,长期) |
||||||
|
7. 单次遍历合并 NodeFinder(S7) |
||||||
|
8. ccache / mold / lld(S6) |
||||||
|
``` |
||||||
|
|
||||||
|
## 6. 附:基准脚本 |
||||||
|
|
||||||
|
分析所用基准脚本:`.workbuddy/bench_ast.php`(可重复运行:`php .workbuddy/bench_ast.php`),复现本节所有数字。 |
||||||
@ -1,356 +0,0 @@ |
|||||||
# C++ 命名空间、前缀与符号 ABI 规则 |
|
||||||
|
|
||||||
本文是 TypePHP、PHPX 以及 TypePHP 生成代码的内部 C++ 命名规范。它解决以下问题: |
|
||||||
|
|
||||||
- 区分 TypePHP 运行时逻辑、PHPX ZendAPI 封装、项目私有实现和用户 PHP 符号; |
|
||||||
- 防止框架 helper 与用户定义的 PHP 函数或类方法生成相同的 C++ 符号; |
|
||||||
- 明确哪些名称属于稳定 ABI,哪些名称仅限单个生成项目内部使用; |
|
||||||
- 为新增 helper、缓存、入口函数和生成符号提供统一的命名决策。 |
|
||||||
|
|
||||||
## 1. 总体规则 |
|
||||||
|
|
||||||
| 命名域 | 含义 | 典型形式 | 可见范围 | ABI 属性 | |
|
||||||
| --- | --- | --- | --- | --- | |
|
||||||
| `typephp_` | TypePHP 独有的运行时或编译产物支持逻辑 | `typephp_call_parent_constructor()` | TypePHP/PHPX 运行时 | 内部或显式导出 ABI | |
|
||||||
| `php::` | 对 ZendAPI、zval、HashTable、call frame 等 PHP 运行时能力的 C++ 封装 | `php::deindirect()` | PHPX C++ API | PHPX API | |
|
||||||
| `typephp_<project>` | 单个编译项目的私有 C++ 命名空间 | `namespace typephp_tpc` | 当前生成项目 | 非公共 ABI | |
|
||||||
| `php_` | 用户 PHP 函数和类方法映射后的 C++ callable 符号 | `php_app__user__save()` | 链接器可见 | TypePHP/stub callable ABI | |
|
||||||
|
|
||||||
核心约束: |
|
||||||
|
|
||||||
1. 不得新增全局的框架 `php_*` helper。 |
|
||||||
2. 与 TypePHP 无关、只是包装 ZendAPI 的能力必须放入 `namespace php`。 |
|
||||||
3. TypePHP 独有且需要跨生成文件调用的逻辑使用 `typephp_` 前缀。 |
|
||||||
4. 只服务于一个编译项目的数据和函数放入 `typephp_<project>` 命名空间。 |
|
||||||
5. 全局 `php_*` callable 名称保留给用户 PHP 声明的编译 ABI。 |
|
||||||
|
|
||||||
## 2. `typephp_`:TypePHP 独有逻辑 |
|
||||||
|
|
||||||
`typephp_` 表示该 API 的语义由 TypePHP 定义,不是 ZendAPI 的一般性 C++ 包装。常见场景包括: |
|
||||||
|
|
||||||
- TypePHP 属性读写规则; |
|
||||||
- TypePHP 构造、克隆和父方法调用链; |
|
||||||
- TypePHP 编译期 Attribute 的运行时支持; |
|
||||||
- TypePHP Native Class、Property Hook 等专属运行时逻辑; |
|
||||||
- TypePHP embed runtime 的初始化和关闭入口。 |
|
||||||
|
|
||||||
示例: |
|
||||||
|
|
||||||
```cpp |
|
||||||
typephp_call_parent_constructor(object, constructor, args); |
|
||||||
typephp_call_parent_clone(object, clone_method); |
|
||||||
typephp_install_property_handlers(class_entry, handlers); |
|
||||||
typephp_write_property_scoped(object, member, value, scope); |
|
||||||
TYPEPHP_RUNTIME_INIT(project)(argc, argv); |
|
||||||
``` |
|
||||||
|
|
||||||
### 2.1 使用边界 |
|
||||||
|
|
||||||
- 该前缀是 TypePHP 内部 C/C++ 名称空间,不代表 PHP 用户函数。 |
|
||||||
- 新增 API 时应使用完整、可识别的 snake_case 名称,不能使用含义过宽的名称,例如 `typephp_call()`。 |
|
||||||
- 仅在一个 `.cc` 文件中使用的函数还应增加 `static` 或放入匿名命名空间。 |
|
||||||
- 需要跨动态库边界时,使用对应的导出宏;不需要导出的 helper 不应扩大符号可见性。 |
|
||||||
- 不要仅因为代码位于 `typephp_helper.h` 就使用 `typephp_`;判断依据是语义是否为 TypePHP 独有。 |
|
||||||
|
|
||||||
### 2.2 正反例 |
|
||||||
|
|
||||||
```cpp |
|
||||||
// 正确:TypePHP 独有的构造链语义。 |
|
||||||
typephp_call_parent_constructor(object, constructor, args); |
|
||||||
|
|
||||||
// 错误:只是将 INDIRECT zval 物化为普通值,并非 TypePHP 独有。 |
|
||||||
typephp_deindirect(value); |
|
||||||
|
|
||||||
// 正确:通用 Zend 值包装属于 PHPX。 |
|
||||||
php::deindirect(value); |
|
||||||
``` |
|
||||||
|
|
||||||
## 3. `php::`:ZendAPI 的 C++ 封装 |
|
||||||
|
|
||||||
`namespace php` 由 PHPX 提供,用于把 Zend 的 C API、宏、裸指针和手工资源管理封装为类型安全、RAII 友好的 C++ API。 |
|
||||||
|
|
||||||
这一命名域包含两类能力: |
|
||||||
|
|
||||||
1. PHP 值和运行时对象,如 `php::Var`、`php::Str`、`php::Array`、`php::Object`; |
|
||||||
2. ZendAPI 的安全包装,如符号查询、作用域管理、值转换、对象创建和调用。 |
|
||||||
|
|
||||||
示例: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php::Var value; |
|
||||||
php::Array arguments; |
|
||||||
|
|
||||||
auto plain = php::deindirect(value); |
|
||||||
auto called_ce = php::getCalledCe(this_); |
|
||||||
auto scope = php::getCallableScope(function, this_); |
|
||||||
auto create_object = php::getCreateObjectFn(class_entry); |
|
||||||
auto globals = php::globalsArray(); |
|
||||||
``` |
|
||||||
|
|
||||||
### 3.1 何时使用 `php::` |
|
||||||
|
|
||||||
满足以下条件时应放入 `namespace php`: |
|
||||||
|
|
||||||
- API 对任何 PHPX C++ 调用者都有意义; |
|
||||||
- API 的行为可以完全用 Zend/PHP 运行时语义解释; |
|
||||||
- API 不依赖 TypePHP AST、编译期 Attribute 或 TypePHP 特有语言规则; |
|
||||||
- API 的主要作用是隐藏 Zend 宏、裸 `zval *`、引用计数或异常检查。 |
|
||||||
|
|
||||||
### 3.2 禁止全局 `php_*` helper |
|
||||||
|
|
||||||
以下旧式写法是禁止的: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php::Var php_deindirect(const php::Var &value); |
|
||||||
php::Str php_get_called_class(php::Object &this_); |
|
||||||
zend_class_entry *php_get_called_ce(php::Object &this_); |
|
||||||
auto php_get_create_object_fn(zend_class_entry *ce); |
|
||||||
``` |
|
||||||
|
|
||||||
它们必须写成: |
|
||||||
|
|
||||||
```cpp |
|
||||||
namespace php { |
|
||||||
|
|
||||||
Var deindirect(const Var &value); |
|
||||||
Str getCalledClass(Object &this_); |
|
||||||
zend_class_entry *getCalledCe(Object &this_); |
|
||||||
auto getCreateObjectFn(zend_class_entry *ce); |
|
||||||
|
|
||||||
} // namespace php |
|
||||||
``` |
|
||||||
|
|
||||||
原因是用户可以合法声明: |
|
||||||
|
|
||||||
```php |
|
||||||
function deindirect(mixed $value): mixed {} |
|
||||||
function get_called_ce(): string {} |
|
||||||
function get_create_object_fn(): string {} |
|
||||||
``` |
|
||||||
|
|
||||||
这些 PHP 函数会生成 `php_deindirect`、`php_get_called_ce` 和 |
|
||||||
`php_get_create_object_fn`。如果 PHPX 也在全局定义同名 helper,可能在声明、重载解析或链接阶段发生冲突。 |
|
||||||
|
|
||||||
### 3.3 命名风格 |
|
||||||
|
|
||||||
PHPX C++ API 使用现有的 camelCase 风格: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php::getCalledClass(); |
|
||||||
php::getClassEntrySafe(); |
|
||||||
php::getPersistentCache(); |
|
||||||
php::stdCreateObject(); |
|
||||||
``` |
|
||||||
|
|
||||||
不要把 Zend 的 snake_case 名称机械地保留为全局 C++ 名称。底层调用可以继续使用 Zend 原始 API,例如 `zend_objects_new()`,但对生成代码暴露的包装层应使用 `php::`。 |
|
||||||
|
|
||||||
## 4. `typephp_<project>`:项目私有命名空间 |
|
||||||
|
|
||||||
每个 TypePHP 编译项目拥有独立的 C++ 命名空间: |
|
||||||
|
|
||||||
```text |
|
||||||
typephp_<target-name> |
|
||||||
``` |
|
||||||
|
|
||||||
例如项目名为 `tpc`: |
|
||||||
|
|
||||||
```cpp |
|
||||||
namespace typephp_tpc { |
|
||||||
// Project-private generated state and helpers. |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
项目名中的 `-` 和 `*` 会转换为 `_`,其余字符必须满足编译器的 target identifier 校验。由于固定带有 `typephp_` 前缀,即使项目名以数字开头,最终 C++ namespace 仍是合法标识符。 |
|
||||||
|
|
||||||
### 4.1 应放入该命名空间的内容 |
|
||||||
|
|
||||||
- literal string 表和 `get_str()`; |
|
||||||
- class/function/property cache 表及其访问函数; |
|
||||||
- 当前项目的全局变量存储; |
|
||||||
- class entry、object handler 和默认属性模板; |
|
||||||
- module entry、MINIT/RINIT/RSHUTDOWN 辅助状态; |
|
||||||
- `module_init()`、`module_clean()` 等仅在生成 extension 文件内部调用的函数; |
|
||||||
- Python module cache 等项目级生成状态。 |
|
||||||
|
|
||||||
示意: |
|
||||||
|
|
||||||
```cpp |
|
||||||
namespace typephp_demo { |
|
||||||
|
|
||||||
static php::Str literal_strings[] = { |
|
||||||
php::Str{"hello"}, |
|
||||||
}; |
|
||||||
|
|
||||||
php::Str &get_str(uint32_t index) { |
|
||||||
return literal_strings[index]; |
|
||||||
} |
|
||||||
|
|
||||||
static THREAD_LOCAL zend_class_entry *class_map[8]; |
|
||||||
|
|
||||||
zend_class_entry *get_class(int id, const php::Str &name) { |
|
||||||
// Resolve and cache a symbol owned by this project. |
|
||||||
} |
|
||||||
|
|
||||||
static void module_init() { |
|
||||||
// Initialize this project's generated state. |
|
||||||
} |
|
||||||
|
|
||||||
} // namespace typephp_demo |
|
||||||
``` |
|
||||||
|
|
||||||
### 4.2 可见性与 ABI |
|
||||||
|
|
||||||
- `typephp_<project>` 内的名称是实现细节,不是 library stub ABI。 |
|
||||||
- 可限制为 `static` 的对象和函数应继续标记为 `static`。 |
|
||||||
- 生成头文件可以声明必须跨 translation unit 使用的项目内部 accessor,但不应暴露底层数组或缓存表。 |
|
||||||
- 外部手写 C++ 代码不得依赖 literal index、cache index 或项目内部 storage 名称。 |
|
||||||
- 不同 TypePHP 项目可以链接到同一进程,因为相同的内部短名称位于不同的项目 namespace 中。 |
|
||||||
|
|
||||||
### 4.3 作用域优先于名称拼写 |
|
||||||
|
|
||||||
项目 namespace 中仍可能出现历史生成名称,例如: |
|
||||||
|
|
||||||
```cpp |
|
||||||
typephp_demo::php_class_entry_App_User |
|
||||||
``` |
|
||||||
|
|
||||||
虽然成员名以 `php_` 开头,但完整符号位于 `typephp_demo` 中,因此它属于项目私有实现,而不是第 5 节所述的全局用户 callable ABI。新增项目内部 helper 应优先使用不带 `php_` 的短名称,例如 `get_class()`、`get_func()` 和 `get_str()`。 |
|
||||||
|
|
||||||
## 5. `php_`:用户 PHP callable 的 C++ ABI |
|
||||||
|
|
||||||
全局 `php_` 前缀用于 TypePHP 将用户声明的 PHP 函数和类方法映射为 C++ callable 符号。这套命名同时被生成代码、library stub 和外部 C++ 实现使用,因此不能随意改变。 |
|
||||||
|
|
||||||
示例: |
|
||||||
|
|
||||||
```php |
|
||||||
namespace App; |
|
||||||
|
|
||||||
function greet(string $name): string {} |
|
||||||
|
|
||||||
class User |
|
||||||
{ |
|
||||||
public function save(): bool {} |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
概念上的 C++ 符号为: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php::Str php_app__greet(php::Str name); |
|
||||||
php::Bool php_app__user__save(php::Object &this_); |
|
||||||
``` |
|
||||||
|
|
||||||
规则包括: |
|
||||||
|
|
||||||
- 使用 `php_` 标识“由 PHP 声明映射而来”; |
|
||||||
- PHP namespace、class 和 method/function 名经过规范化后组合; |
|
||||||
- `__` 是现有 ABI 的组合分隔符; |
|
||||||
- 实例方法的第一个参数是对象 `this_`; |
|
||||||
- stub、library 和消费方必须使用完全相同的映射规则。 |
|
||||||
|
|
||||||
### 5.1 为什么内部 helper 不能使用 `php_` |
|
||||||
|
|
||||||
`php_` 映射不是独立的保留关键字空间,而是用户 PHP 名称的机械 ABI。以下用户声明: |
|
||||||
|
|
||||||
```php |
|
||||||
function deindirect(mixed $value): mixed {} |
|
||||||
``` |
|
||||||
|
|
||||||
会自然生成: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php::Var php_deindirect(php::Var value); |
|
||||||
``` |
|
||||||
|
|
||||||
因此框架若定义全局 `php_deindirect()`,就侵占了用户符号空间。正确做法是 `php::deindirect()`。 |
|
||||||
|
|
||||||
### 5.2 组合冲突 |
|
||||||
|
|
||||||
由于当前 ABI 使用 `__` 组合 PHP namespace、class 和 callable 名,下列两个 PHP 声明可能映射到同一个 C++ 符号: |
|
||||||
|
|
||||||
```php |
|
||||||
function App\user__test(): void {} |
|
||||||
|
|
||||||
namespace App; |
|
||||||
class User |
|
||||||
{ |
|
||||||
public function test(): void {} |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
编译器必须在预处理阶段检测这种情况并抛出 FatalError,不能通过覆盖、链接顺序或增加运行时分派来处理。修改映射分隔规则会破坏既有 stub/ABI,因此冲突必须由用户重命名解决。 |
|
||||||
|
|
||||||
### 5.3 入口符号例外 |
|
||||||
|
|
||||||
少量 C ABI/嵌入入口由生成器固定定义,不属于普通用户 callable。例如: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php_<project>_embed_get_module(); |
|
||||||
typephp_<project>_runtime_init(argc, argv); |
|
||||||
typephp_<project>_runtime_shutdown(); |
|
||||||
``` |
|
||||||
|
|
||||||
这些是 binary/library embed runtime 与当前项目 module entry 的连接点。定义和引用统一通过 |
|
||||||
`TYPEPHP_EMBED_GET_MODULE_FUNCTION()`、`TYPEPHP_RUNTIME_INIT_FUNCTION()`、 |
|
||||||
`TYPEPHP_RUNTIME_SHUTDOWN_FUNCTION()` 及对应的符号宏生成,风格与 Zend 的 |
|
||||||
`PHP_MINIT_FUNCTION()`/`PHP_MINIT()` 一致。最终符号包含项目名,不得作为通用 helper 命名模板。 |
|
||||||
|
|
||||||
### 5.4 多扩展进程中的公共运行时 |
|
||||||
|
|
||||||
TypePHP 扩展不得分别编译或静态链接包含进程级 Zend 状态的 PHPX 实现。Reflection handler、 |
|
||||||
`FiberGenerator` class entry、作用域和 Property Hook 运行时均由共享的 `libphpx` 唯一提供: |
|
||||||
|
|
||||||
- host 模式的 extension/library 必须链接 `libphpx.so`、`libphpx.dylib` 或 `phpx.dll`,不能回退到 `libphpx.a`; |
|
||||||
- Unix PHP extension 不链接 Embed `libphp.so`,Zend/PHP 符号由加载它的 SAPI 提供; |
|
||||||
- macOS extension 使用 `-undefined dynamic_lookup` 解析宿主符号; |
|
||||||
- binary 和独立 WASI 程序仍可以静态链接,因为每个进程或 Wasm 实例只有一份运行时。 |
|
||||||
|
|
||||||
`src/core/typephp_*.cc` 只承载 TypePHP 专属的 `typephp_*` 运行时;`php::` ZendAPI 包装应放在不带 |
|
||||||
`typephp_` 前缀的 core 源文件中,例如 `src/core/scope.cc`。 |
|
||||||
|
|
||||||
## 6. 名称选择流程 |
|
||||||
|
|
||||||
新增 C++ API 时按以下顺序判断: |
|
||||||
|
|
||||||
1. **它是否是用户 PHP 函数或类方法的编译本体?** |
|
||||||
- 是:使用既有 `php_` callable ABI 生成器,禁止手写另一套映射。 |
|
||||||
2. **它是否只服务于当前一个 TypePHP 项目?** |
|
||||||
- 是:放入 `typephp_<project>`,并尽可能使用 `static` 或私有 accessor。 |
|
||||||
3. **它是否实现 TypePHP 独有语义?** |
|
||||||
- 是:使用 `typephp_` 前缀。 |
|
||||||
4. **它是否只是对 Zend/PHP 运行时能力的 C++ 封装?** |
|
||||||
- 是:放入 `namespace php`,使用 PHPX camelCase 风格。 |
|
||||||
5. **以上都不是?** |
|
||||||
- 不应随意加入 `typephp_helper.h`;应重新确认所属模块和公共 API 边界。 |
|
||||||
|
|
||||||
## 7. 代码审查清单 |
|
||||||
|
|
||||||
新增或修改生成 helper 时必须检查: |
|
||||||
|
|
||||||
- [ ] `typephp_helper.h` 中没有新增全局 `php_*` helper; |
|
||||||
- [ ] ZendAPI 包装位于 `namespace php`; |
|
||||||
- [ ] TypePHP 独有逻辑使用 `typephp_`; |
|
||||||
- [ ] 项目缓存和 storage 位于 `typephp_<project>`; |
|
||||||
- [ ] 项目私有表没有通过生成头文件直接 `extern` 暴露; |
|
||||||
- [ ] 用户 callable 仍使用统一的 `php_` ABI 生成器; |
|
||||||
- [ ] 新名称不会与用户可声明的 PHP 函数或方法发生冲突; |
|
||||||
- [ ] bin、lib、ext 和 WASM 构建使用相同的项目名推导规则; |
|
||||||
- [ ] 修改公开 callable 映射时同步评估 stub 和既有 ABI; |
|
||||||
- [ ] 至少增加一个用户同名函数的编译回归测试。 |
|
||||||
|
|
||||||
当前相关回归测试为: |
|
||||||
|
|
||||||
```text |
|
||||||
tests/compiler/basic/helper-symbol-collision.phpt |
|
||||||
``` |
|
||||||
|
|
||||||
## 8. 主要实现位置 |
|
||||||
|
|
||||||
| 责任 | 文件 | |
|
||||||
| --- | --- | |
|
||||||
| `php_` callable 前缀与组合分隔符 | `src/CompilerBase.php` | |
|
||||||
| callable 组合冲突检测 | `src/Preprocessor.php` | |
|
||||||
| `typephp_<project>` 生成及项目私有表 | `src/Translator.php` | |
|
||||||
| TypePHP extension 前缀常量 | `src/Metadata/Constants.php` | |
|
||||||
| PHPX/TypePHP helper 分类 | `vendor/swoole/phpx/include/typephp_helper.h` | |
|
||||||
| embed module accessor 拼接 | `vendor/swoole/phpx/src/misc/typephp_main.cc` | |
|
||||||
@ -1,114 +0,0 @@ |
|||||||
# `#[Immutable]` compile-time effect checking |
|
||||||
|
|
||||||
## Purpose |
|
||||||
|
|
||||||
`#[Immutable]` is a TypePHP compile-time annotation modelled after C++ `const`. |
|
||||||
It prevents accidental mutation in statically compiled code without adding a |
|
||||||
wrapper object, Zend metadata, runtime branch, or ABI change. |
|
||||||
|
|
||||||
It is intentionally a best-effort static tool rather than a security boundary. |
|
||||||
Calls whose target is deliberately made dynamic are an escape hatch and do not |
|
||||||
receive a runtime guard. |
|
||||||
|
|
||||||
## Supported targets |
|
||||||
|
|
||||||
```php |
|
||||||
#[Immutable] |
|
||||||
public function name(): string |
|
||||||
{ |
|
||||||
return $this->name; |
|
||||||
} |
|
||||||
|
|
||||||
function inspect(#[Immutable] User $user): string |
|
||||||
{ |
|
||||||
return $user->name(); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
The attribute is valid on methods and on function, method, and closure |
|
||||||
parameters. On an instance method it makes `$this` immutable. On a parameter it |
|
||||||
makes the binding immutable and, when it can contain an object, treats the |
|
||||||
referenced object as immutable as well. |
|
||||||
|
|
||||||
## Rejected operations |
|
||||||
|
|
||||||
For an immutable root such as `$this` or `$user`, the compiler rejects: |
|
||||||
|
|
||||||
- assignment, destructuring, and array-element or object-property writes; |
|
||||||
- compound assignment, `++`, `--`, `unset()`, taking a reference, and |
|
||||||
`foreach (... as &$value)`; |
|
||||||
- a statically named method call unless the resolved method is also marked |
|
||||||
`#[Immutable]`; |
|
||||||
- a mutating value extension such as `$array->sort()`; read-only array/string |
|
||||||
methods remain available; |
|
||||||
- passing an object to a statically resolved parameter that is not itself |
|
||||||
`#[Immutable]`; |
|
||||||
- passing any immutable value to a mutable by-reference parameter, including |
|
||||||
extension functions such as `sort()`; |
|
||||||
- storing an immutable object identity in an object property, array, |
|
||||||
global/static variable, or returning/yielding it as a mutable value. |
|
||||||
|
|
||||||
An immutable by-reference parameter is supported. It acts like a C++ `const &`: |
|
||||||
the reference is accepted because the callee is checked against mutation. |
|
||||||
|
|
||||||
`#[MethodsFor]` follows the same contract. An object extension is callable on |
|
||||||
an immutable receiver only when its receiver parameter is marked |
|
||||||
`#[Immutable]`. |
|
||||||
|
|
||||||
## Aliases, closures, generators, and inheritance |
|
||||||
|
|
||||||
Local aliases of immutable objects remain immutable: |
|
||||||
|
|
||||||
```php |
|
||||||
$alias = $user; |
|
||||||
$alias->rename('new'); // compile-time error |
|
||||||
``` |
|
||||||
|
|
||||||
`clone` creates a distinct mutable object and therefore intentionally drops the |
|
||||||
annotation. Captured variables, arrow functions, closure `$this`, and Fiber |
|
||||||
generator bodies carry immutable metadata into their generated function |
|
||||||
contexts. |
|
||||||
|
|
||||||
An overriding class or interface method may strengthen an ordinary contract by |
|
||||||
adding `#[Immutable]`, but it cannot remove `#[Immutable]` from an inherited |
|
||||||
method or parameter. |
|
||||||
|
|
||||||
## Value versus object semantics |
|
||||||
|
|
||||||
Scalar values and PHP copy-on-write values can be read and copied normally. For |
|
||||||
example, `count($values)` and `$copy = $values` do not modify an immutable array. |
|
||||||
The compiler propagates immutability through an expression only when object |
|
||||||
identity is possible. |
|
||||||
|
|
||||||
## Explicit escape hatches |
|
||||||
|
|
||||||
The following intentionally bypass static method-effect checking: |
|
||||||
|
|
||||||
```php |
|
||||||
$method = 'rename'; |
|
||||||
$user->$method('new'); |
|
||||||
|
|
||||||
$callable = getRuntimeCallable(); |
|
||||||
$callable($user); |
|
||||||
``` |
|
||||||
|
|
||||||
The same applies to other runtime-only mechanisms that hide the target from the |
|
||||||
compiler, including reflection and dynamic ZendVM code. TypePHP neither inserts |
|
||||||
a runtime read-only proxy nor attempts to recover the escaped value later. |
|
||||||
|
|
||||||
This boundary is deliberate: `#[Immutable]` should cost nothing in generated |
|
||||||
code and should not complicate PHPX/ZendVM object semantics. |
|
||||||
|
|
||||||
## Property hooks and magic access |
|
||||||
|
|
||||||
Property-hook reads are lowered to generated method calls. Consequently, a hook |
|
||||||
used through an immutable receiver must itself carry an `#[Immutable]` method |
|
||||||
contract; otherwise the generated call is rejected. Fully dynamic magic access |
|
||||||
is covered by the same escape-hatch rule as other runtime-only behavior. |
|
||||||
|
|
||||||
## Implementation boundaries |
|
||||||
|
|
||||||
The implementation is isolated in `src/Immutable/ImmutableSupportTrait.php`. |
|
||||||
`FunctionDef` and `ArgInfo` retain only compile-time effect bits, while each |
|
||||||
`FunctionContext` stores the immutable roots and object aliases relevant to that |
|
||||||
body. Checks run during AST lowering and emit no C++ code when successful. |
|
||||||
@ -1,116 +0,0 @@ |
|||||||
# Interface Property Hooks 实现方案 |
|
||||||
|
|
||||||
本文记录 TP-AOT-010 的设计与实施计划。目标是支持 PHP 8.4 的 Interface Property Hook 契约,同时保持 TypePHP Native 调用的零成本抽象,并让 PHP 8.4 ZendVM 的 Reflection、动态类链接和继承检查获得完整元数据。 |
|
||||||
|
|
||||||
## 当前状态(2026-08-14) |
|
||||||
|
|
||||||
第一阶段已经落地:Interface 契约模型、AOT 实现检查、get/set 方向方差、PHPX 抽象 Hook 元数据、Reflection、动态 PHP 实现类及回归测试均已接通。显式 setter 参数类型仍按下文约定在编译期拒绝;完成独立写入类型模型后再开放。 |
|
||||||
|
|
||||||
## 1. 设计结论 |
|
||||||
|
|
||||||
Interface 中的 Hooked Property 只表示属性契约: |
|
||||||
|
|
||||||
```php |
|
||||||
interface Named |
|
||||||
{ |
|
||||||
public string $name { get; set; } |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
- Interface 不持有属性槽,不生成 getter/setter 实现,也不产生访问时的契约检查。 |
|
||||||
- TypePHP 在编译期验证已知 AOT 类是否满足属性的可见性、类型和 `get`/`set` 能力。 |
|
||||||
- PHP 8.4 目标在 MINIT 注册原生 Zend Hook 元数据,使 Reflection 和动态 PHP 类获得相同契约。 |
|
||||||
- TypePHP、PHPX 和最终目标运行时的最低版本均为 PHP 8.4,不提供旧版本降级路径。 |
|
||||||
|
|
||||||
## 2. 语法与诊断 |
|
||||||
|
|
||||||
支持三类契约: |
|
||||||
|
|
||||||
```php |
|
||||||
public string $readable { get; } |
|
||||||
public string $writable { set; } |
|
||||||
public string $readWrite { get; set; } |
|
||||||
``` |
|
||||||
|
|
||||||
Interface Property Hook 必须是 `public`、非 `static`、无默认值且 Hook 不得包含函数体。普通 Interface Property、`private`/`protected`、`readonly`、重复或未知 Hook,以及带实现体的 Hook 均在 TypePHP 编译期抛出 FatalError。错误信息应尽可能与 PHP 8.4 一致。 |
|
||||||
|
|
||||||
第一阶段只接收隐式 setter 参数: |
|
||||||
|
|
||||||
```php |
|
||||||
public string $name { set; } |
|
||||||
``` |
|
||||||
|
|
||||||
PHP 8.4 还允许 `set(string|Stringable $value)` 这类显式、可逆变的 setter 参数。该语法需要让编译期契约模型与 Zend Hook `arg_info` 同时保存独立于属性读取类型的写入类型;在这部分完成前,TypePHP 会给出明确的编译期错误,不生成可能错误的运行时元数据。 |
|
||||||
|
|
||||||
## 3. 编译器模型 |
|
||||||
|
|
||||||
Interface Property Hook 不应伪装成普通属性或 lowering 后的普通方法。为其建立独立契约模型,至少保存: |
|
||||||
|
|
||||||
- 属性名和声明节点; |
|
||||||
- 解析后的 TypePHP 类型与类类型; |
|
||||||
- 是否要求 `get`; |
|
||||||
- 是否要求 `set`; |
|
||||||
- 可见性及其他用于诊断的标志。 |
|
||||||
|
|
||||||
契约存放在 `InterfaceDef` 中。AST/预处理阶段只收集和验证声明,不为 Interface 分配属性槽,不运行具体类使用的 `PropertyHookLowering`,也不生成隐藏方法。 |
|
||||||
|
|
||||||
所有类型完成预处理后再执行契约链接:展开父 Interface 契约,然后检查实现类自身或父类提供的属性。普通 public backed property 同时满足读写契约;Hooked Property 根据实际 Hook 能力判断。get-only 类型按读取方向协变,set-only 类型按写入方向逆变,同时包含 get/set 时保持不变。 |
|
||||||
|
|
||||||
## 4. PHPX 与 Zend 元数据 |
|
||||||
|
|
||||||
现有 `typephp_register_property_hooks()` 用于具有真实 AOT getter/setter 的具体类,不能复用于抽象 Interface Hook。 |
|
||||||
|
|
||||||
PHPX 增加独立 helper: |
|
||||||
|
|
||||||
```cpp |
|
||||||
typephp_register_abstract_property_hooks( |
|
||||||
zend_class_entry *interface_ce, |
|
||||||
zend_property_info *property_info, |
|
||||||
bool readable, |
|
||||||
bool writable |
|
||||||
); |
|
||||||
``` |
|
||||||
|
|
||||||
TypePHP/PHPX 已统一要求 PHP 8.4+,因此该 helper 直接访问 PHP 8.4 ABI,并负责: |
|
||||||
|
|
||||||
- 持久化分配 `zend_property_info::hooks`; |
|
||||||
- 创建没有 handler 的 abstract `get`/`set` `zend_internal_function` 元数据; |
|
||||||
- 设置 `ZEND_ACC_PUBLIC | ZEND_ACC_ABSTRACT`、正确的参数/返回类型及 `common.prop_info`; |
|
||||||
- 更新 `num_hooked_props`,使 Zend inheritance 和 Reflection 识别该契约; |
|
||||||
- 保证所有字符串、Hook 表和函数描述具有 MINIT 级持久生命周期。 |
|
||||||
|
|
||||||
生成代码先注册 Interface,再以 `IS_UNDEF`、`ZEND_ACC_PUBLIC | ZEND_ACC_ABSTRACT | ZEND_ACC_VIRTUAL` 声明属性并挂载抽象 Hook,最后才注册和链接实现类。 |
|
||||||
|
|
||||||
## 5. PHP 版本边界 |
|
||||||
|
|
||||||
TypePHP 区分源码语言版本与链接运行时: |
|
||||||
|
|
||||||
- `--php-version` 只允许 `8.4` 或 `8.5`,用于解析语法和处理项目条件; |
|
||||||
- PHPX headers、`libphp` 与最终运行时必须为 PHP 8.4 或更高版本; |
|
||||||
- `--php-version` 与 `libphp.so` 的小版本不要求完全一致,例如使用 8.5 语法模式并链接 PHP 8.4 时,最终能否构建仍由实际使用的 Zend API 决定; |
|
||||||
- PHP 8.4 以下环境在 TypePHP/PHPX 构建入口直接拒绝。 |
|
||||||
|
|
||||||
## 6. TDD 覆盖 |
|
||||||
|
|
||||||
实现前先加入失败测试,覆盖: |
|
||||||
|
|
||||||
1. get-only、set-only、get/set Interface 契约; |
|
||||||
2. 普通 backed property、Hooked Property 和继承属性满足契约; |
|
||||||
3. 缺失属性、缺少 get/set、非 public 和类型不兼容的编译错误; |
|
||||||
4. Interface 继承、多个契约的合并与冲突; |
|
||||||
5. Reflection 的 abstract、virtual、hasHook/getHook 元数据; |
|
||||||
6. PHP 8.4 动态 PHP 类的成功与失败链接; |
|
||||||
7. O0/O3 结果一致,Interface 不生成属性槽或 Native Hook 实现; |
|
||||||
8. PHPX helper 在 NTS/ZTS 和 PHP 8.4/8.5 下的生命周期与 ABI 回归。 |
|
||||||
|
|
||||||
## 7. 实施顺序 |
|
||||||
|
|
||||||
1. 添加 TP-AOT-010 正常场景及语法错误 PHPT,确认当前失败。 |
|
||||||
2. 增加 Interface Property Contract 模型和预处理收集逻辑。 |
|
||||||
3. 实现 Interface 继承与实现类的编译期契约检查。 |
|
||||||
4. 在 PHPX 增加抽象 Hook 元数据 helper。 |
|
||||||
5. 修改 stub 生成和类注册顺序,接入 PHP 8.4 Zend 元数据。 |
|
||||||
6. 添加 Reflection、动态类链接、目标版本和生成代码测试。 |
|
||||||
7. 执行 Interface、Property Hook、Reflection 及全量编译器回归。 |
|
||||||
|
|
||||||
完成后的运行时属性访问仍直接进入实现类的普通属性或 Native Hook;Interface 契约本身只存在于编译期模型和 MINIT 元数据中,不进入请求热路径。 |
|
||||||
@ -1,137 +0,0 @@ |
|||||||
# Native Class 实现验收矩阵 |
|
||||||
|
|
||||||
> 审计日期:2026-08-17 |
|
||||||
> 本文记录 `#[Native]` 对象模型的需求、实现入口和直接验证证据。它是 |
|
||||||
> [NATIVE_CLASS_OBJECT.md](NATIVE_CLASS_OBJECT.md) 的实现验收附件,不替代语义设计文档。 |
|
||||||
|
|
||||||
## 1. 验收原则 |
|
||||||
|
|
||||||
每一项能力必须同时具有: |
|
||||||
|
|
||||||
1. 明确的语言边界; |
|
||||||
2. 可定位的编译器或 PHPX 实现; |
|
||||||
3. 正向 PHPT、负向 PHPUnit 或 PHPX C++ 单测中的直接证据。 |
|
||||||
|
|
||||||
仅有代码、仅有文档或“当前没有发现失败”均不视为完成。Native Object 没有 Zend |
|
||||||
表示,因此任何不能静态证明安全的跨边界行为都必须在生成 C++ 前拒绝。 |
|
||||||
|
|
||||||
## 2. 对象模型与代码生成 |
|
||||||
|
|
||||||
| 要求 | 实现证据 | 测试证据 | 结论 | |
|
||||||
|---|---|---|---| |
|
||||||
| `#[Native]` 只用于具名 class | `NativeClassAttributeLowering`、`NativeClassSupportTrait` | `testRejectsNativeAttributeOnInterface/Trait/Enum/AnonymousClass` | 已验证 | |
|
||||||
| 不注册 Zend class/object handlers | Native struct、descriptor 和自由函数生成路径 | `clone-and-zend-invisible.phpt`、Reflection 负向测试 | 已验证 | |
|
||||||
| 方法保持 `php_*` 自由函数 ABI | Native method/virtual thunk 生成路径 | `basic.phpt`、`chained-call.phpt` | 已验证 | |
|
||||||
| 静态可解析的 `new NativeClass()` 使用 Native Heap | `CompilerBase::parseNew()`、`php::nativeConstruct()` | `basic.phpt`、`construction-gc-roots.phpt` | 已验证 | |
|
||||||
| `new (表达式)()` 保持普通 PHP 动态实例化 | `parseNew()` 只对 `Node\\Name` 进入 Native 分支 | `testLeavesDynamicClassExpressionsToTheOrdinaryPhpPath` | 已验证 | |
|
||||||
| Native 对象本身不能充当动态 class target | `assertNotNativeObjectDynamicClassTarget()` | dynamic new/static call/class constant 负向测试 | 已验证 | |
|
||||||
| 所有不支持的用法在编译期终止 | Native 边界检查、类型兼容检查 | 131 项 `NativeClassValidationTest` | 已验证 | |
|
||||||
|
|
||||||
## 3. 属性与固定布局 |
|
||||||
|
|
||||||
| 要求 | 实现证据 | 测试证据 | 结论 | |
|
||||||
|---|---|---|---| |
|
||||||
| 所有属性必须声明类型 | Native field validation | `testRejectsUntypedProperty` | 已验证 | |
|
||||||
| bool/int/float 使用固定值字段 | Native field C++ type mapping | `basic.phpt`、`numeric-properties.phpt` | 已验证 | |
|
||||||
| string/array/object/typed object/Stream/mixed 可作为字段 | Native PHPX field mapping、写入检查 | `phpx-properties.phpt`、`stream-property.phpt`、`composite-property-types.phpt` | 已验证 | |
|
||||||
| BigInt/BigFloat/Decimal 可作为字段 | 高精度字段映射与 trace/destroy | `high-precision-properties.phpt` | 已验证 | |
|
||||||
| Native 类型字段保存裸指针,可形成循环类型 | struct 前置声明、descriptor trace | `mutual-reference-types.phpt`、`gc-cycle.phpt` | 已验证 | |
|
||||||
| 未显式初始化字段使用确定零值 | Native field initializer | `zero-values.phpt` | 已验证 | |
|
||||||
| 属性写入保持声明类型 | Native property assignment validation | composite、stream 及多项负向 PHPUnit | 已验证 | |
|
||||||
| 仅 `any` 属性允许取 PHP 引用 | Native property reference lowering | `any-property-reference.phpt` 及 mixed/fixed property 负向测试 | 已验证 | |
|
||||||
| Native 属性不支持 `unset()` | property unset validator | `testRejectsUnsetOnNativeObjectProperties` | 已验证 | |
|
||||||
| readonly 属性不支持 | Native declaration validator | `testRejectsReadonlyPropertyUntilNativeWriteStateIsImplemented` | 已验证 | |
|
||||||
| Box/Std Container 不能嵌入字段 | Native field validator | Box/Std Container property 负向测试 | 已验证 | |
|
||||||
|
|
||||||
## 4. 身份、空值与调用 ABI |
|
||||||
|
|
||||||
| 要求 | 实现证据 | 测试证据 | 结论 | |
|
||||||
|---|---|---|---| |
|
||||||
| `$a = $b` 只复制指针并共享对象身份 | Native pointer local representation | `parameter-semantics.phpt` | 已验证 | |
|
||||||
| Native 参数和返回必须显式声明具体类 | call argument/return boundary validation | untyped/mixed/interface 参数与返回负向测试 | 已验证 | |
|
||||||
| 普通 Native 参数非空,`?Class` 才可为空 | function entry/return checks | `non-null-parameter.phpt`、`nullable-signatures.phpt`、`return-nullability.phpt` | 已验证 | |
|
||||||
| Native 参数、返回和变量禁止 `&` | reference boundary validation | reference parameter/return/assignment/function/method 负向测试 | 已验证 | |
|
||||||
| Native variadic、union/intersection signature 不支持 | signature validation | variadic/union/null-union 负向测试 | 已验证 | |
|
||||||
| `unset($object)`/`$object = null` 只清当前 pointer slot | Native root slot lowering | `unset-alias.phpt` | 已验证 | |
|
||||||
| `===`/`!==` 与 `match` 使用指针身份 | Native identity lowering | `strict-identity.phpt`、`match-identity.phpt` | 已验证 | |
|
||||||
| ternary/match/coalesce 为兄弟子类选择最近公共 Native 基类 | `getCommonNativeObjectClass()`、selection pointer cast | `value-selection.phpt`、跨文件 global discovery 测试 | 已验证 | |
|
||||||
| 条件表达式检查非空指针,不调用 `toBool()` | Native condition lowering | `conditions.phpt` | 已验证 | |
|
||||||
| 松散比较、算术、位运算、增减、复合写入和 switch 禁止 | operator validators | 对应 PHPUnit 负向测试 | 已验证 | |
|
||||||
| `isset`/`empty`/`is_null`/nullsafe 保持 typed pointer | Native selection/nullsafe lowering | `isset-empty.phpt`、`is-null.phpt`、`nullsafe.phpt` | 已验证 | |
|
||||||
| 调用参数严格从左到右求值并在 safe point 精确 rooting | Native call argument materialization | `call-argument-roots.phpt`、`constructor-argument-roots.phpt` | 已验证 | |
|
||||||
|
|
||||||
## 5. 类语言能力 |
|
||||||
|
|
||||||
| 要求 | 实现证据 | 测试证据 | 结论 | |
|
||||||
|---|---|---|---| |
|
||||||
| 单继承、abstract 与有限虚分派 | Native C++ inheritance/virtual slot adapters | `abstract-method.phpt`、`polymorphic-clone.phpt`、`virtual-signature-variance.phpt` | 已验证 | |
|
||||||
| public/private/protected 在编译期检查 | Native member resolution | `method-visibility.phpt` 及不可访问方法/常量负向测试 | 已验证 | |
|
||||||
| Trait 在注入后按普通 Native member 编译 | 现有 Trait AST 注入 + Native member generation | `trait-inheritance-interface.phpt` | 已验证 | |
|
||||||
| Interface 仅作编译期契约,不能成为值表示 | interface contract validator | `internal-interface.phpt`、`interface-property-hooks.phpt` 及 interface escape 负向测试 | 已验证 | |
|
||||||
| 编译期可解析的 `instanceof` 折叠 | Native instanceof lowering | `instanceof.phpt`、dynamic instanceof 负向测试 | 已验证 | |
|
||||||
| Getter/Setter 注解生成直接调用 | annotation lowering + Native method path | `generators.phpt` | 已验证 | |
|
||||||
| Property Hook 只支持直接 get/set | Native hook lowering | `property-hooks.phpt`、`property-hook-native-object.phpt` 及间接操作负向测试 | 已验证 | |
|
||||||
| `clone` 保持动态子类、PHPX COW 和浅对象语义 | Native clone descriptor/thunk、`php::nativeClone()` | clone 系列 PHPT、`clone-phpx-fields.phpt` | 已验证 | |
|
||||||
| `__construct` 仅由 `new` 调用 | Native construction path、显式调用检查 | construction 系列 PHPT、explicit constructor 负向测试 | 已验证 | |
|
||||||
| `__destruct` 由 GC 至多执行一次,继承链 derived-to-base | Native finalizer chain | destructor/finalizer/lifecycle 系列 PHPT | 已验证 | |
|
||||||
| `__invoke` 和 `__toString` 使用确定 Native Call | Native magic method allow-list | `magic-methods.phpt` | 已验证 | |
|
||||||
| 动态魔术方法、变量属性/方法名不支持 | Native magic/dynamic access deny-list | dynamic magic、variable method/property 负向测试 | 已验证 | |
|
||||||
| `toArray/toString/toInt/toFloat/toBool/toObject` 要求实体方法、零参数和精确返回类型 | Native keyword method resolution | `keyword-conversions.phpt`、`testNativeObjectToObjectKeywordUsesDeclaredNativeMethod` 及签名负向测试 | 已验证 | |
|
||||||
| `count($obj)` 仅在实现 Countable 时特化 | Native count optimizer | `keyword-conversions.phpt`、count-without-countable 负向测试 | 已验证 | |
|
||||||
| `ArrayAccess` 直接语法映射到 Native `offset*()` 方法 | Native array access lowering | `array-access.phpt` | 已验证 | |
|
||||||
| Native `ArrayAccess` 禁止间接修改和引用 | writable-chain/reference validators | ArrayAccess compound/increment/nested/property/reference/coalesce 负向测试 | 已验证 | |
|
||||||
| Native `Iterator` foreach 映射到协议方法,保持 PHP 调用顺序 | Native foreach lowering | `iterator.phpt` | 已验证 | |
|
||||||
| `IteratorAggregate` 分流 Native Iterator 与 PHP Traversable | aggregate return-type lowering | `iterator.phpt` | 已验证 | |
|
||||||
| Native foreach 不枚举属性且禁止引用遍历 | interface/reference validators | foreach 负向 PHPUnit | 已验证 | |
|
||||||
|
|
||||||
## 6. GC 与生命周期 |
|
||||||
|
|
||||||
| 要求 | 实现证据 | 测试证据 | 结论 | |
|
||||||
|---|---|---|---| |
|
||||||
| Wren 风格精确、非移动、STW mark-sweep | `phpx/thirdparty/wren-gc`、`native_gc.cc` | PHPX `wren_gc.*` | 已验证 | |
|
||||||
| 裸指针写入无 RC、无 write barrier | Native pointer field/local codegen | 生成 C++ 审查、Native PHPT | 已验证 | |
|
||||||
| 16 MiB 初始阈值、1 MiB 下限、50% headroom | Wren GC 配置 | `wren_gc.uses_stable_native_heap_defaults` | 已验证 | |
|
||||||
| 精确 root frame 保持对象图存活 | `NativeRootFrame`、generated root slots | PHPX root tests、`gc-cycle.phpt` | 已验证 | |
|
||||||
| Fiber 非 LIFO 生命周期安全 | root frame registry | `fiber-lifetime.phpt`、`fiber-shutdown.phpt`、PHPX Fiber root tests | 已验证 | |
|
||||||
| global/static request roots 在 ZTS 下为 thread-local | generated globals/root registration | ZTS 环境下 `global-and-static.phpt`、PHPX request root tests | 已验证 | |
|
||||||
| RSHUTDOWN 清空 root 并销毁 heap | `nativeGcRequestShutdown()` | PHPX shutdown tests | 已验证 | |
|
||||||
| finalizer 可复活一次,之后不重复执行 | Wren/Native finalization state | `gc-cycle.phpt`、PHPX resurrection tests | 已验证 | |
|
||||||
| finalizer 中分配、异常和 Zend 状态安全 | finalizer queue/exception cleanup | finalizer/lifecycle PHPT、PHPX finalizer tests | 已验证 | |
|
||||||
| 构造或克隆失败不产生悬空对象,已逃逸对象保持有效 | `nativeConstruct()`、`nativeClone()` failure paths | `failed-lifecycle-escape.phpt`、`failed-clone-finalizer.phpt` | 已验证 | |
|
||||||
|
|
||||||
## 7. ZendVM 边界与容器 |
|
||||||
|
|
||||||
| 要求 | 实现证据 | 测试证据 | 结论 | |
|
||||||
|---|---|---|---| |
|
||||||
| Native Object 不能进入 PHP array/object property/mixed | escape and boundary validators | 对应 PHPUnit 负向测试 | 已验证 | |
|
||||||
| 不能传给 PHP/ZendVM 动态函数、Closure 或 constructor | call boundary validator | dynamic call、Closure、Zend constructor 负向测试 | 已验证 | |
|
||||||
| Reflection/WeakReference/serialize/json_encode 不支持 | facility-specific diagnostics | 对应 PHPUnit 负向测试 | 已验证 | |
|
||||||
| Generator 不能保存、接收或产出 Native pointer | generator boundary validator | generator 系列负向测试 | 已验证 | |
|
||||||
| 普通函数跨 Fiber suspend 的 Native local 有精确 root | root frame lifecycle | Fiber PHPT | 已验证 | |
|
||||||
| 局部 Std Container 可保存具体 Native pointer | Std Container Native value mapping/root frame | `std-containers.phpt` | 已验证 | |
|
||||||
| Native Std Container 不能逃逸为 Zend 值、static/global 或 closure capture | container escape validation | Std Container 系列负向 PHPUnit | 已验证 | |
|
||||||
| `include`/`eval` 不暴露 Native local 到 Zend symbol table | include scope filtering | `include-native-scope.phpt` | 已验证 | |
|
||||||
|
|
||||||
## 8. 项目级分析 |
|
||||||
|
|
||||||
| 要求 | 实现证据 | 测试证据 | 结论 | |
|
||||||
|---|---|---|---| |
|
||||||
| Native class 前向声明不依赖文件顺序 | declaration discovery pre-pass | `testDiscoversNativeTypesBeforeCrossFileSignaturePreprocessing` | 已验证 | |
|
||||||
| global Native slot ABI 在任一 C++ 文件生成前确定 | `NativeGlobalDiscovery`、`NativeGlobalTypeResolver` | `testDiscoversNativeGlobalSlotBeforeEarlierReaderIsConverted`,实际双文件构建 | 已验证 | |
|
||||||
| `global $slot` 与静态可解析的 `$GLOBALS[...]` 使用同一 Native root slot | literal/constant global slot lowering、request root registration | `global-and-static.phpt`、跨文件 Closure/常量 `$GLOBALS` fixture | 已验证 | |
|
||||||
| 动态 `$GLOBALS[$key]` 不得承载 Native Object | dynamic Zend boundary validation | `testRejectsNativeObjectStoredThroughDynamicGlobalsKey` | 已验证 | |
|
||||||
| global slot 固定首个 Native 类型,只允许子类或 null | global registration/type validation | `global-and-static.phpt`、global type change 负向测试 | 已验证 | |
|
||||||
| 未使用 Native Class 的项目跳过 Native global pre-pass | `discoverNativeGlobalObjects()` fast return | 源码检查、全量 PHPUnit | 已验证 | |
|
||||||
|
|
||||||
## 9. 当前验证命令 |
|
||||||
|
|
||||||
```bash |
|
||||||
./run-tests.php -j4 --compiler ./tpc tests/compiler/native-class/ |
|
||||||
vendor/bin/phpunit phpunit/src/NativeClass/NativeClassValidationTest.php |
|
||||||
/home/swoole/workspace/aot/phpx/build/bin/phpx-tests \ |
|
||||||
--gtest_filter='wren_gc.*:native_gc.*' |
|
||||||
``` |
|
||||||
|
|
||||||
本次 Iterator 专项结果为:`iterator.phpt` 1/1、Native Class PHPUnit 136/136, |
|
||||||
普通 foreach 回归 14/14。Native Class PHPT 目录现有 71 项;按当前任务约定暂未重复执行 |
|
||||||
该目录及编译器 PHPT 全量测试,留待下一轮统一回归。 |
|
||||||
File diff suppressed because it is too large
Load Diff
@ -1,212 +0,0 @@ |
|||||||
# Zend Object 创建与属性默认值初始化 |
|
||||||
|
|
||||||
本文记录 TypePHP 生成的 Zend Class 在 MINIT 和对象创建阶段的初始化职责,重点说明何时需要自定义 `create_object`、其中允许执行哪些行为,以及对象创建热路径上的性能边界。 |
|
||||||
|
|
||||||
本文只讨论注册到 ZendVM 的普通 TypePHP Class。`#[Native]` Class 使用 Native Heap 与 GC,不走本文流程。 |
|
||||||
|
|
||||||
## 1. 两个初始化阶段必须分开 |
|
||||||
|
|
||||||
TypePHP Class 的属性初始化分为两个阶段: |
|
||||||
|
|
||||||
1. `gen_stub.php` 在 MINIT 生成 `register_class_*()`,建立 `zend_class_entry`、属性元数据和默认属性表; |
|
||||||
2. 只有默认属性表无法准确表达的值,才在每次创建对象时由自定义 `create_object` 补充。 |
|
||||||
|
|
||||||
这两个阶段不能重复执行相同的属性赋值。`register_class_*()` 已写入的值会由 Zend 的 `object_properties_init()` 复制到新对象;再次调用 `zend_update_property()` 不仅没有语义价值,还会进入属性名查找、类型检查、handler 分派和引用计数路径。 |
|
||||||
|
|
||||||
## 2. gen_stub.php 负责的默认值 |
|
||||||
|
|
||||||
以下值可以准确写入 Zend Class 的默认属性表: |
|
||||||
|
|
||||||
| 源代码默认值 | 注册阶段表示 | 是否需要在 `create_object` 中再次写入 | |
|
||||||
|---|---|---| |
|
||||||
| `null` | `ZVAL_NULL` | 否 | |
|
||||||
| `bool` | `ZVAL_TRUE/FALSE` | 否 | |
|
||||||
| `int` | `ZVAL_LONG` | 否 | |
|
||||||
| `float` | `ZVAL_DOUBLE` | 否 | |
|
||||||
| `string` | 持久化 `zend_string` | 否 | |
|
||||||
| 标量常量表达式 | 编译期求值后的标量 zval | 否 | |
|
||||||
| `[]` | `ZVAL_EMPTY_ARRAY` | 否 | |
|
||||||
| 没有显式默认值的 TypePHP typed property | TypePHP 规定的零值、空字符串、空数组、`null` 或 `UNDEF` | 否 | |
|
||||||
|
|
||||||
例如: |
|
||||||
|
|
||||||
```php |
|
||||||
class Value |
|
||||||
{ |
|
||||||
private const BASE = 20; |
|
||||||
|
|
||||||
public int $id = self::BASE + 3; |
|
||||||
public string $name = 'type' . 'php'; |
|
||||||
public array $items = []; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
只要表达式能够在编译期安全求值,以上三个属性都应完全依赖 Zend Class 默认属性表。创建 `Value` 时不得再次调用 `zend_update_property()`。 |
|
||||||
|
|
||||||
## 3. 默认值何时需要运行时补充 |
|
||||||
|
|
||||||
当前 `gen_stub.php` 不能在默认属性表中准确表示以下值。 |
|
||||||
|
|
||||||
### 3.1 非空数组 |
|
||||||
|
|
||||||
非空数组默认值当前在注册函数中使用 `ZVAL_EMPTY_ARRAY` 作为占位值。每个对象必须构造独立、语义正确的数组值: |
|
||||||
|
|
||||||
```php |
|
||||||
class Request |
|
||||||
{ |
|
||||||
public array $options = ['timeout' => 10]; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
因此 `Request::$options` 需要在 `create_object` 中补充。多个对象仍遵守 PHP 数组的 copy-on-write 语义;修改一个对象的数组不得影响其他对象。 |
|
||||||
|
|
||||||
数组常量也遵守相同规则。若编译器只能确定它是数组、不能证明它为空,则保守地保留运行时初始化。 |
|
||||||
|
|
||||||
### 3.2 Enum case |
|
||||||
|
|
||||||
Enum case 是对象,不是标量常量: |
|
||||||
|
|
||||||
```php |
|
||||||
enum State |
|
||||||
{ |
|
||||||
case Ready; |
|
||||||
} |
|
||||||
|
|
||||||
class Task |
|
||||||
{ |
|
||||||
public State $state = State::Ready; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
类注册代码当前只能先生成占位值,`create_object` 再取得真正的 enum case 对象并写入属性。因此“只有非空数组才需要自定义 `create_object`”并不成立,enum case 是明确的第二类反例。 |
|
||||||
|
|
||||||
### 3.3 无法安全解析的常量表达式 |
|
||||||
|
|
||||||
若预处理阶段无法证明默认值可由 Zend 默认属性表准确表达,编译器必须保守地保留运行时初始化。优化只能删除已证明冗余的工作,不能根据表达式外形猜测其运行时类型。 |
|
||||||
|
|
||||||
## 4. handlers 与父类 allocator |
|
||||||
|
|
||||||
### 4.1 Property Hook 与非对称 set 可见性不单独触发 |
|
||||||
|
|
||||||
PHP 8.4 Property Hook、`private(set)` 和 `protected(set)` 会安装 TypePHP 自定义 object handlers,但这本身不要求覆盖 `create_object`。Zend 8.4 的 `object_properties_init()` 直接复制 class default table,不调用 read/write handler;普通 `php::stdCreateObject()` 已能正确设置最终 handlers。 |
|
||||||
|
|
||||||
只有该类同时含有非空数组、enum case 等运行时默认值时,才需要自定义创建流程。补充初始化必须绕过 setter;即使使用 `zend_std_write_property()`,PHP 8.4 也会根据 Hook 元数据调用 setter。当前生成代码因此使用编译期已知的 property offset,经 PHPX `Object::attr(offset)` 直接更新 backing slot。 |
|
||||||
|
|
||||||
### 4.2 父类自定义对象分配器 |
|
||||||
|
|
||||||
若父类来自 PHP 内置扩展,或祖先类拥有自定义对象存储布局,子类不能绕过父类的 allocator。当前类因运行时默认值确实需要自定义创建流程时,必须先调用保存的父类 `create_object`,再补充当前类的值。 |
|
||||||
|
|
||||||
TypePHP 父类已经安装自定义 allocator 时,普通子类通常直接继承它。只有子类自身也需要补充初始化时,才生成新的委派层。 |
|
||||||
|
|
||||||
## 5. 自定义 create_object 的执行流程 |
|
||||||
|
|
||||||
生成代码通过 `typephp_create_object_with_defaults()` 完成以下步骤: |
|
||||||
|
|
||||||
1. 保存类最终的 `default_object_handlers`; |
|
||||||
2. 若必须尊重父类对象布局,调用保存的父类 allocator;否则执行 `zend_objects_new()` 与 `object_properties_init()`; |
|
||||||
3. 临时把新对象切换到 Zend 标准 object handlers,确保异常路径和其他对象操作处于可控状态; |
|
||||||
4. 只执行标记为 `requiresRuntimeDefaultInit` 的属性初始化,并通过缓存的 declared-property offset 直接写 backing slot; |
|
||||||
5. 每次写入后检查 Zend 异常; |
|
||||||
6. 无论正常返回还是发生 C++ 异常,都恢复最终 handlers; |
|
||||||
7. 返回已完整初始化的 `zend_object *`。 |
|
||||||
|
|
||||||
初始化器是模板参数和编译期 lambda,不使用 `std::function`,也不会为 lambda 动态分配内存。`delegate_to_base` 是调用点确定的布尔值,优化构建中通常可被 C++ 编译器折叠。 |
|
||||||
|
|
||||||
以下行为不属于 `create_object`: |
|
||||||
|
|
||||||
- PHP `__construct()` 的函数体; |
|
||||||
- static property 默认值初始化;它在 `module_init()` 中完成; |
|
||||||
- 已由默认属性表表达的标量、`null` 和空数组赋值; |
|
||||||
- clone 后重新应用默认值;clone 应复制源对象当前状态,而不是重新创建默认状态。 |
|
||||||
|
|
||||||
## 6. 已修复的主要性能问题 |
|
||||||
|
|
||||||
旧生成逻辑只要类中存在任意显式非 static 默认值,就安装自定义 `create_object`,并在每次创建对象时重新 update 所有默认属性。这会产生两层重复成本: |
|
||||||
|
|
||||||
1. 只含 `public int $value = 0` 的普通类也绕过标准快速创建路径; |
|
||||||
2. 一个类只要含有一个非空数组,其他标量属性也会被逐个重复 update。 |
|
||||||
|
|
||||||
当前规则已经调整为: |
|
||||||
|
|
||||||
- 只有确实需要运行时补充的属性才使 `requireCtor` 生效; |
|
||||||
- 已由 `gen_stub.php` 准确注册的属性不会出现在运行时初始化 block 中; |
|
||||||
- 只有 Hook/非对称可见性而没有运行时默认值的类不再生成空的自定义 allocator; |
|
||||||
- Hook 与运行时默认值同时存在时,使用固定 property offset 更新 backing slot,不调用 setter。 |
|
||||||
|
|
||||||
在 micro benchmark 中,仅包含标量属性的 `new Foo()` 已从约 `1.8s` 降至约 `0.78s`,与同环境 ZendPHP 扣除空循环后的约 `0.83s` 接近。该数字只用于记录优化量级,不是跨机器性能承诺。 |
|
||||||
|
|
||||||
## 7. 已实现优化、剩余成本与后续方向 |
|
||||||
|
|
||||||
### 7.1 非空数组使用请求级模板与 copy-on-write |
|
||||||
|
|
||||||
不能把非空数组放进 internal class 的默认属性表,但这不等于必须为每个对象重新构建数组。当前生成器已经使用请求级默认值模板: |
|
||||||
|
|
||||||
1. 每个包含运行时数组默认值的类拥有一组 `THREAD_LOCAL php::Var` 模板和一个初始化状态,NTS 构建不引入锁; |
|
||||||
2. 第一次创建该类对象时,通过 `UNEXPECTED(!initialized)` 惰性构建该类的全部模板; |
|
||||||
3. 模板全部在局部临时值中成功构建后才提交并设置初始化标记,构造异常不会发布半初始化状态; |
|
||||||
4. 模板初始化发生在对象分配之前,失败时不会遗留一个尚未返回的对象; |
|
||||||
5. 后续创建对象时只把模板 zval 复制到目标 backing slot,即增加一次数组引用计数; |
|
||||||
6. 某个对象第一次修改该属性时,由 Zend/PHPX 的 `SEPARATE_ARRAY` 执行 copy-on-write; |
|
||||||
7. 在 `module_clean()` 中释放模板并重置初始化状态,request allocator 分配的 HashTable 不会跨越 RSHUTDOWN。 |
|
||||||
|
|
||||||
以如下默认值为例: |
|
||||||
|
|
||||||
```php |
|
||||||
class Request |
|
||||||
{ |
|
||||||
public array $options = [ |
|
||||||
'timeout' => 10, |
|
||||||
'headers' => ['Accept' => 'application/json'], |
|
||||||
]; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
若创建一万个对象但不修改 `$options`,数组及嵌套数组只构建一次;每个对象只持有共享 zval。若其中一个对象执行 `$request->options['timeout'] = 30`,只有该对象在写入时分离,其他对象和模板保持不变。嵌套数组也继续使用 Zend 原有的逐层 copy-on-write 规则。 |
|
||||||
|
|
||||||
PHP 属性默认数组不能包含引用,允许出现在常量表达式中的对象主要是不可变的 enum case,因此共享模板符合默认属性语义。PHPT 已覆盖顶层写入、嵌套写入、`unset`、引用写入和动态对象写入,确认这些路径都会正确分离。 |
|
||||||
|
|
||||||
不能简单地在 MINIT 构造持久化数组并传给 `zend_declare_typed_property()`。TypePHP 注册的是 `ZEND_INTERNAL_CLASS`,Zend 8.4 明确禁止 internal property 使用 refcounted default zval;`_object_properties_init()` 的 internal-class 快速路径也不会增加默认值引用计数。非空 array 与 enum object 都属于 refcounted value。 |
|
||||||
|
|
||||||
因此,在不改变“TypePHP Class 注册为 internal class”这一基础设计、也不修改 Zend ABI 的前提下,非空数组仍不进入 class default table;表外请求级模板把数组构造成本从“每个对象一次”降为“每个请求、每个默认值一次”。未修改默认数组的对象只承担 zval 复制和引用计数成本,实际修改的对象才承担数组分离成本。 |
|
||||||
|
|
||||||
模板按类惰性初始化,而不是在 RINIT 无条件构建全部模板:大型项目中很多类在一次请求内不会实例化。每个对象只增加一个高度可预测的初始化状态分支;第一次之后该分支稳定为 false。 |
|
||||||
|
|
||||||
暂不生成模块生命周期的 persistent immutable template。该方案需要完整验证 persistent HashTable、interned string、嵌套数组、MSHUTDOWN 和 ZTS,并且包含运行时常量或 enum case 的数组仍要走请求级路径。在 ZendVM 对这些组合的约束得到充分验证前,请求级模板是安全边界。 |
|
||||||
|
|
||||||
### 7.2 已改为固定属性槽写入 |
|
||||||
|
|
||||||
运行时补充的属性在编译期已经知道 class、属性名、offset 和类型。当前实现复用 persistent property-offset cache,并通过 `php::Object::attr(offset)` 更新槽位,已经省去每个对象上的属性名 hash 查询、通用 write handler 和 Property Hook setter。 |
|
||||||
|
|
||||||
这里仍会为 initializer 建立一个短生命周期 `php::Object` carrier,并读取 offset cache。后续若 profiling 证明它是热点,可以在 MINIT 后直接保存最终 offset,或在 PHPX 增加不取得对象所有权的初始化 helper。任何进一步优化都必须继续处理旧值析构、引用计数、父类 private slot、Hook backing slot 和异常安全,不能退回裸指针的无保护赋值。 |
|
||||||
|
|
||||||
### 7.3 Enum case 可提前绑定 |
|
||||||
|
|
||||||
Enum case 同样是 refcounted object,不能直接作为 internal class 默认 zval。可考虑在 MINIT 缓存稳定的 enum case 指针或 zval,再在每次创建对象时执行正确的引用计数复制,从而省去重复 class/case 查找;仍不能省略对象属性写入本身。 |
|
||||||
|
|
||||||
### 7.4 继承链上的多层 allocator |
|
||||||
|
|
||||||
父类和子类都拥有运行时默认值时,创建流程会逐层委派并执行各自初始化,成本随相关继承层数增长。未来可以对完全由 TypePHP 控制、且没有特殊对象布局的继承链合并初始化计划;内置扩展父类仍必须调用其 allocator。 |
|
||||||
|
|
||||||
### 7.5 保守常量可能产生不必要的 allocator |
|
||||||
|
|
||||||
无法在预处理阶段解析的常量会保守进入运行时路径。可以在符号准备完成后增加一次统一的常量默认值分类,减少“实际是标量,但早期无法证明”的自定义 allocator。该优化必须保留 enum case 和数组常量的区别。 |
|
||||||
|
|
||||||
### 7.6 自定义 handlers 的动态访问成本 |
|
||||||
|
|
||||||
TypePHP 当前为普通 Zend Class 安装属性 handlers,以支持 typed property 的 unset 语义、Property Hook 和非对称写可见性。安装发生在 MINIT,不等同于安装自定义 `create_object`;但动态属性读写仍可能进入 handler。已被编译器解析为固定槽位的 Native 属性访问不应因此退化。 |
|
||||||
|
|
||||||
## 8. 回归测试要求 |
|
||||||
|
|
||||||
修改该流程至少应覆盖: |
|
||||||
|
|
||||||
- 标量、标量常量表达式和空数组不生成自定义 allocator; |
|
||||||
- 非空数组生成 allocator,且两个对象的数组修改互不影响; |
|
||||||
- enum case 默认值在对象创建后是真正的 enum object; |
|
||||||
- 仅含 Property Hook 或非对称 set 可见性的类不生成空 allocator,且 Reflection、动态读写行为不退化; |
|
||||||
- Property Hook/非对称属性与运行时默认值组合时不触发 setter; |
|
||||||
- 父子类分别声明运行时默认值时,父类和子类属性都正确; |
|
||||||
- 继承内置扩展类时不破坏其对象布局; |
|
||||||
- 异常路径恢复 object handlers; |
|
||||||
- 自举编译和完整 PHPUnit/PHPT 回归通过。 |
|
||||||
|
|
||||||
当前针对代码生成的核心断言位于 `NewObjectCodegenTest`,运行语义由 `default-initialization-paths.phpt`、`default-expressions-inheritance.phpt` 和 Property Hook 测试组覆盖。 |
|
||||||
@ -1,302 +0,0 @@ |
|||||||
# TypePHP 三套对象存储与传递模型 |
|
||||||
|
|
||||||
> 状态:当前架构约束。本文解释 TypePHP 为什么同时保留 Zend Object、PHPX Box 和 |
|
||||||
> Native Class Object 三套对象式值模型,以及它们各自的所有权、传递方式和边界。 |
|
||||||
|
|
||||||
## 1. 结论 |
|
||||||
|
|
||||||
TypePHP 当前存在三套对象存储与传递机制: |
|
||||||
|
|
||||||
1. 普通 PHP/Zend Object; |
|
||||||
2. PHPX Box,包括 Std Container 和高精度类型; |
|
||||||
3. `#[Native]` Native Class Object。 |
|
||||||
|
|
||||||
三者并非同一设计的历史残留,而是分别解决三类互相冲突的问题: |
|
||||||
|
|
||||||
- Zend Object 保留 PHP 的动态对象语义和 ZendVM 生态兼容性; |
|
||||||
- Box 为无法完整写进 PHP 类型声明的 C++ 类型提供不透明 Zend value 载体; |
|
||||||
- Native Class Object 为静态可知的业务对象提供接近 C/C++ 的固定布局、裸指针调用和 |
|
||||||
tracing GC。 |
|
||||||
|
|
||||||
任何一种机制都不能在不损失另一种机制核心能力的前提下替代其余两种。当前设计明确接受 |
|
||||||
三套模型长期共存,不以“统一对象表示”为目标。 |
|
||||||
|
|
||||||
## 2. 总览 |
|
||||||
|
|
||||||
| 维度 | Zend Object | PHPX Box | Native Class Object | |
|
||||||
| --- | --- | --- | --- | |
|
||||||
| 典型值 | 普通 PHP class 实例 | Std Container、BigInt、BigFloat、Decimal | `#[Native] class` 实例 | |
|
||||||
| 主要表示 | `zend_object` / zval | `zend_resource` + `php::Box *` | Native Heap 中的 C++ struct + 裸指针 | |
|
||||||
| 类型身份 | `zend_class_entry *` | Box C++ 动态类型、`type_info`/类型 ID | 编译期 Native class,descriptor 保存动态类型 | |
|
||||||
| 生命周期 | Zend 引用计数 + Zend 循环 GC | Zend resource 引用计数调用 Box destructor | Wren 风格精确、非移动 mark-sweep GC | |
|
||||||
| 参数传递 | `php::Object` / `php::Var`,复制句柄并调整 RC | `php::Var` 携带 resource;热路径提取具体 C++ 引用 | 具体 `NativeClass *` 按值传递,不调整 RC | |
|
||||||
| 属性/方法访问 | Zend handlers、动态查找或已缓存 Native Call | 编译器根据具体 Box 类型生成操作 | 固定偏移字段访问和确定的 `php_*` Native Call | |
|
||||||
| 动态 PHP 互操作 | 完整 | 作为不透明 resource 有限互操作 | 不可进入 ZendVM value 边界 | |
|
||||||
| 循环图处理 | Zend GC 可扫描 Zend object graph | Zend GC 不扫描 Box 内部 C++ 对象图 | Native descriptor 精确 trace Native pointer graph | |
|
||||||
| 核心目标 | PHP 兼容性 | 携带 C++ 泛型/扩展值 | 极致静态性能 | |
|
||||||
|
|
||||||
## 3. 普通 PHP/Zend Object |
|
||||||
|
|
||||||
### 3.1 存储 |
|
||||||
|
|
||||||
普通 class 注册到 ZendVM,实例由 `zend_object` 表示。TypePHP 通过 `php::Object`、 |
|
||||||
`php::Variant`/`php::Var` 等 PHPX RAII 类型持有对应 zval。 |
|
||||||
|
|
||||||
对象具有 Zend 的 class entry、属性表、对象 handlers 和方法元数据。根据编译期信息, |
|
||||||
TypePHP 可以把部分访问优化为确定的 Native Call,但对象身份和生命周期仍属于 ZendVM。 |
|
||||||
|
|
||||||
### 3.2 传递和生命周期 |
|
||||||
|
|
||||||
PHP 对象赋值和参数传递复制对象句柄,不复制对象实体,并遵循 Zend 引用计数。对象图中的 |
|
||||||
循环引用由 Zend GC 处理。对象可以自然进入: |
|
||||||
|
|
||||||
- PHP array 和普通对象属性; |
|
||||||
- `mixed`/`object` 变量; |
|
||||||
- Closure、Generator、Fiber 和动态调用; |
|
||||||
- Reflection、序列化和扩展函数; |
|
||||||
- ZendVM 执行的 PHP 代码。 |
|
||||||
|
|
||||||
### 3.3 必须保留的原因 |
|
||||||
|
|
||||||
只有 Zend Object 能完整承载 PHP 的运行时对象语义。用 Box 替代会丢失 class entry、对象 |
|
||||||
handlers、可见性、Reflection 和动态分派;用 Native Object 替代则会失去 ZendVM 可见性, |
|
||||||
并迫使所有动态行为退化为编译期限制。 |
|
||||||
|
|
||||||
普通 PHP class 因此始终使用 Zend Object。编译器可以优化调用,但不能改变其对象模型。 |
|
||||||
|
|
||||||
## 4. PHPX Box |
|
||||||
|
|
||||||
### 4.1 存储 |
|
||||||
|
|
||||||
`php::Box` 是由 PHPX 管理的 C++ 多态基类。Box 指针注册为 Zend resource,并由 |
|
||||||
`php::Var` 携带: |
|
||||||
|
|
||||||
```text |
|
||||||
zval(IS_RESOURCE) |
|
||||||
-> zend_resource |
|
||||||
-> php::Box* |
|
||||||
-> concrete C++ value |
|
||||||
``` |
|
||||||
|
|
||||||
Zend resource 的析构回调最终调用 `Box::destroy()`。Box 因而可以经过普通 zval/Variant |
|
||||||
调用边界,同时隐藏 Zend 无法表达的具体 C++ 类型。 |
|
||||||
|
|
||||||
当前主要使用者包括: |
|
||||||
|
|
||||||
- `StdContainerBox<std::vector<T>>`; |
|
||||||
- `StdContainerBox<std::array<T, N>>`; |
|
||||||
- `StdContainerBox<map-like type>`; |
|
||||||
- BigInt、BigFloat、Decimal 等高精度值。 |
|
||||||
|
|
||||||
### 4.2 Std Container 的热路径 |
|
||||||
|
|
||||||
Std Container 局部变量具有两层表示: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php::Var values = php::Var(new php::StdContainerBox<Container>(type_id)); |
|
||||||
auto &values_ref = values.toBox<php::StdContainerBox<Container>>()->container; |
|
||||||
``` |
|
||||||
|
|
||||||
`php::Var` 负责生命周期和必要的边界传递,具体容器引用用于后续元素访问,避免每次操作都 |
|
||||||
重复提取 Box。容器的 key/value/长度等泛型信息由编译器和具体 C++ 模板类型共同保存。 |
|
||||||
|
|
||||||
Std Container 跨 TypePHP 函数传递时,PHP 函数签名无法表达以下 C++ 类型信息: |
|
||||||
|
|
||||||
```text |
|
||||||
std::vector<int> |
|
||||||
std::vector<string> |
|
||||||
std::map<string, App\User> |
|
||||||
``` |
|
||||||
|
|
||||||
PHP 参数最多只能声明一个非泛型类名或伪类型,不能同时携带容器种类、key 类型、value |
|
||||||
类型、数组维度和长度。当前使用 `UnsafePtr`/`std::unsafe_cast()` 加编译器类型 ID 校验, |
|
||||||
而不是把所有组合生成为 PHP class。 |
|
||||||
|
|
||||||
理论上可以增加参数和返回值注解描述泛型,但这要求每个声明、调用、返回、属性和传播点 |
|
||||||
都维护额外元数据,PHP Reflection 仍无法完整表达它。当前不引入这套独立泛型 ABI。 |
|
||||||
|
|
||||||
### 4.3 Box 的边界 |
|
||||||
|
|
||||||
Box 是不透明值载体,不是通用对象系统: |
|
||||||
|
|
||||||
- Zend GC 只看见 resource,不会扫描 Box 内部保存的 C++ 引用; |
|
||||||
- Box 不提供 PHP class 的方法表、属性表、继承和 Reflection; |
|
||||||
- 通过 `dynamic_cast`、类型 ID 或专用 helper 恢复具体类型; |
|
||||||
- 不应使用 Box 构建需要跨 Zend/Box 双向追踪的任意循环对象图; |
|
||||||
- Std Container 的可用位置和逃逸路径继续受编译器限制。 |
|
||||||
|
|
||||||
Box 适合数值、容器和其他边界明确的扩展值。它不适合代替具有任意字段引用关系的 Native |
|
||||||
业务对象。 |
|
||||||
|
|
||||||
### 4.4 必须保留的原因 |
|
||||||
|
|
||||||
Std Container 的泛型类型无法由 PHP 函数参数完整表达;高精度值又需要作为 `php::Var` |
|
||||||
参与现有运算和调用。Box 同时提供: |
|
||||||
|
|
||||||
- 可放进 zval 的稳定载体; |
|
||||||
- C++ 具体类型的运行时恢复; |
|
||||||
- Zend request 生命周期内的自动析构; |
|
||||||
- 不为每一种模板实例注册一套 PHP class 的轻量实现。 |
|
||||||
|
|
||||||
Zend Object 无法直接表达 C++ 模板实例;Native 裸指针则无法安全穿过 `php::Var` 和动态 |
|
||||||
ZendVM 边界。因此 Box 仍有独立存在的必要。 |
|
||||||
|
|
||||||
## 5. Native Class Object |
|
||||||
|
|
||||||
### 5.1 存储 |
|
||||||
|
|
||||||
`#[Native]` class 不注册 Zend class,不生成 Zend object handlers,也没有 zval 表示。每个 |
|
||||||
对象是 Native Heap 中的固定布局 C++ struct,TypePHP 局部变量、参数、返回值和字段保存 |
|
||||||
具体 Native 指针: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php_app__point *point; |
|
||||||
``` |
|
||||||
|
|
||||||
方法继续使用 TypePHP 的自由函数 ABI: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php::Float php_app__point__length(php_app__point &this_); |
|
||||||
``` |
|
||||||
|
|
||||||
普通调用只传递一个指针值。不会创建 zval、注册 resource、执行引用计数或通过 |
|
||||||
`zend_call_function()`。 |
|
||||||
|
|
||||||
### 5.2 生命周期 |
|
||||||
|
|
||||||
Native Object 使用 PHPX 中独立的 Wren 风格精确、非移动、stop-the-world mark-sweep GC: |
|
||||||
|
|
||||||
- Native 局部变量、参数、返回临时值和 global/static slot 进入精确 root frame; |
|
||||||
- Native 对象 descriptor 负责 trace Native pointer 字段; |
|
||||||
- Std Container 保存 Native pointer 时注册专用 container root frame; |
|
||||||
- 循环引用由 tracing GC 回收,不依赖引用计数降为零; |
|
||||||
- 16-byte GC header 保存收集器所需的最小状态; |
|
||||||
- `__destruct()` 由 Native finalization 执行,而不是由 Zend object destructor 执行。 |
|
||||||
|
|
||||||
Native 指针赋值不增加引用计数,也不需要 write barrier。固定字段直接按 C++ 偏移访问。 |
|
||||||
|
|
||||||
### 5.3 传递边界 |
|
||||||
|
|
||||||
Native Object 参数和返回值必须显式声明具体 Native class,或受支持的 nullable 具体类型: |
|
||||||
|
|
||||||
```php |
|
||||||
function distance(Point $left, Point $right): float; |
|
||||||
function findPoint(): ?Point; |
|
||||||
``` |
|
||||||
|
|
||||||
这使编译器可以把签名直接生成为 `Point *`。Native Object 不支持: |
|
||||||
|
|
||||||
- 传给 PHP/ZendVM 函数、Closure 或动态 callable; |
|
||||||
- 保存到 PHP array、普通 Zend Object 属性或 `mixed`; |
|
||||||
- 自动转换为 `php::Object`、`php::Var` 或 Interface value; |
|
||||||
- 依靠运行时 class name 恢复类型; |
|
||||||
- 使用通用 PHPX `toObject()` helper 完成装箱或拆箱。Native Class 可以声明自己的 |
|
||||||
`toObject(): object` 方法;关键词调用会直接解析为该 Native Call,并不提供通用 bridge。 |
|
||||||
|
|
||||||
需要进入 PHP API 时,用户必须显式转换数据,例如先调用 Native `toArray(): array`,再把 |
|
||||||
结果传给 `json_encode()`。该转换产生的是数据副本,不保留 Native 对象身份。 |
|
||||||
|
|
||||||
### 5.4 必须保留的原因 |
|
||||||
|
|
||||||
Native Class 的目标是接近 C/C++ 的热路径性能: |
|
||||||
|
|
||||||
- 一个机器字的对象句柄; |
|
||||||
- 固定字段布局; |
|
||||||
- 不进行 Zend RC 增减; |
|
||||||
- 不分配 `zend_object` 或 `zend_resource` carrier; |
|
||||||
- 确定符号 Native Call; |
|
||||||
- 可由 C++ 编译器内联和去虚化。 |
|
||||||
|
|
||||||
若改用 Box,每个 Native Object 都需要 resource/zval 封装、RC 管理和具体类型恢复,而且 |
|
||||||
Zend GC 无法扫描 Box 内部 Native 指针图;这既降低性能,也不能正确替代 Native tracing |
|
||||||
GC。若改用自定义 `zend_object`,虽然能够接入 Zend GC 和动态边界,但对象 header、RC、 |
|
||||||
handlers 和访问路径都会改变 Native Class 的性能定位。 |
|
||||||
|
|
||||||
因此 Native Class 继续使用独立 Native Heap 和裸指针 ABI。 |
|
||||||
|
|
||||||
## 6. 为什么不能统一 |
|
||||||
|
|
||||||
### 6.1 不能全部改为 Zend Object |
|
||||||
|
|
||||||
这样可以统一动态语义,却会让 Std Container 泛型实例和 Native Class 都承担 Zend object |
|
||||||
header、RC、handlers、class registration 与动态访问成本。Native Class 将不再接近 C/C++, |
|
||||||
Std Container 也需要为大量模板组合设计运行时 class 体系。 |
|
||||||
|
|
||||||
### 6.2 不能全部改为 Box |
|
||||||
|
|
||||||
Box 能通过 zval 携带 C++ 值,但 Zend GC 不理解 Box 内部对象图。它不能替代普通 PHP |
|
||||||
Object 的动态元数据,也不能在保持 Native 循环回收能力的同时提供裸指针热路径。 |
|
||||||
|
|
||||||
### 6.3 不能全部改为 Native pointer |
|
||||||
|
|
||||||
Native pointer 要求完整静态类型。普通 PHP 对象需要 Reflection、动态属性、动态 callable |
|
||||||
和 Zend 扩展互操作;Std Container 的完整泛型类型又无法写入 PHP 参数签名。把这些值都 |
|
||||||
改为裸指针会产生无法静态证明安全的类型擦除,并可能导致错误指针转换和崩溃。 |
|
||||||
|
|
||||||
### 6.4 不增加自动桥接 |
|
||||||
|
|
||||||
三套模型之间不进行隐式对象身份转换。自动装箱/拆箱会隐藏分配、复制、RC 和 GC root |
|
||||||
变化,也会使编译器边界不再可靠。 |
|
||||||
|
|
||||||
允许的转换必须具有明确语义: |
|
||||||
|
|
||||||
- Std Container 转 PHP array:复制容器数据; |
|
||||||
- Native Object 的 `toArray()` 等实体方法:由用户定义并显式复制数据; |
|
||||||
- 高精度类型的显式标量转换:产生新的 PHP 标量值; |
|
||||||
- 普通 Zend Object 不会自动变成 Native Object。 |
|
||||||
|
|
||||||
## 7. 编译器实现约束 |
|
||||||
|
|
||||||
后续修改必须保持以下不变量: |
|
||||||
|
|
||||||
1. 先根据静态类型确定对象模型,再选择代码生成路径;不得在运行时猜测三者之一。 |
|
||||||
2. Native Object 不得因通用 fallback 被包装成 `php::Var` 或传入 ZendVM。 |
|
||||||
3. Box 的具体类型恢复必须校验 resource 类型和 concrete C++ 类型/类型 ID。 |
|
||||||
4. Zend Object 优化不得改变 Zend 对象身份、生命周期或动态可见性。 |
|
||||||
5. 三种模型的参数 ABI 不得混用:`php::Object`、Box-bearing `php::Var`、`NativeClass *` |
|
||||||
分别代表不同所有权和类型约束。 |
|
||||||
6. 跨模型转换必须显式,并在文档和生成代码中体现分配或复制成本。 |
|
||||||
7. 若一个新特性需要牺牲所有 Native Class 热路径来获得少量动态兼容,应优先在编译期禁止。 |
|
||||||
8. 若一种新的 C++ 泛型类型需要穿过 Zend value 边界,应优先评估 Box,而不是扩大 Native |
|
||||||
Object 的动态边界。 |
|
||||||
9. 若一个值需要完整 PHP 对象语义,应使用 Zend Object,不能把 Box 当作简化的 PHP class。 |
|
||||||
|
|
||||||
## 8. 代码位置 |
|
||||||
|
|
||||||
主要实现入口: |
|
||||||
|
|
||||||
```text |
|
||||||
普通 Zend Object |
|
||||||
compiler/src/Parser/* |
|
||||||
phpx/include/phpx.h Object / Variant / Zend API wrappers |
|
||||||
|
|
||||||
PHPX Box 与 Std Container |
|
||||||
phpx/include/phpx.h Box / StdContainerBox<T> |
|
||||||
phpx/src/core/base.cc Box resource registration and destructor |
|
||||||
compiler/src/Parser/StdContainerTrait.php |
|
||||||
|
|
||||||
Native Class Object |
|
||||||
compiler/src/NativeClass/ |
|
||||||
compiler/src/Transform/NativeClassAttributeLowering.php |
|
||||||
phpx/include/phpx_native_gc.h |
|
||||||
phpx/src/core/native_gc.cc |
|
||||||
phpx/thirdparty/wren-gc/ |
|
||||||
``` |
|
||||||
|
|
||||||
详细规则分别见 [STD_CONTAINERS.md](STD_CONTAINERS.md)、 |
|
||||||
[NATIVE_CLASS_OBJECT.md](NATIVE_CLASS_OBJECT.md) 和 |
|
||||||
[NATIVE_CLASS_IMPLEMENTATION_AUDIT.md](NATIVE_CLASS_IMPLEMENTATION_AUDIT.md)。 |
|
||||||
|
|
||||||
## 9. 当前决策 |
|
||||||
|
|
||||||
当前阶段不实施以下重构: |
|
||||||
|
|
||||||
- 不移除 Wren GC; |
|
||||||
- 不把 Native Object 改为 Box 或自定义 Zend Object; |
|
||||||
- 不给 Native Object 增加通用 `toObject()` 动态恢复机制;Native Class 自定义的 |
|
||||||
`toObject(): object` 仍是普通的确定 Native Call; |
|
||||||
- 不把 Std Container 改为无法跨签名表达类型的裸指针 ABI; |
|
||||||
- 不尝试用单一统一 wrapper 覆盖三种对象模型。 |
|
||||||
|
|
||||||
未来只有在 PHP 语言层能够稳定表达泛型参数、或者有经过 benchmark 和完整 GC 正确性验证 |
|
||||||
的新 ABI 时,才重新评估这些边界。在此之前,三套机制的共存是有意的架构选择。 |
|
||||||
@ -1,355 +0,0 @@ |
|||||||
# 重建 PHPX WASM 静态库 |
|
||||||
|
|
||||||
本文面向 TypePHP/PHPX 开发者,说明如何为 `wasm32-wasip2` 重新编译并安装 |
|
||||||
PHPX 静态库。普通 TypePHP 用户不需要执行这些步骤;发行包应直接提供完整的 |
|
||||||
WASI SDK。 |
|
||||||
|
|
||||||
## 目录约定 |
|
||||||
|
|
||||||
本文假设源码布局如下: |
|
||||||
|
|
||||||
```text |
|
||||||
/home/swoole/workspace/aot/ |
|
||||||
├── compiler/ |
|
||||||
└── phpx/ |
|
||||||
``` |
|
||||||
|
|
||||||
建议先设置 PHPX 根目录: |
|
||||||
|
|
||||||
```shell |
|
||||||
export PHPX_HOME=/home/swoole/workspace/aot/phpx |
|
||||||
``` |
|
||||||
|
|
||||||
安装前缀固定为: |
|
||||||
|
|
||||||
```text |
|
||||||
$PHPX_HOME/wasm/wasm32-wasip2 |
|
||||||
``` |
|
||||||
|
|
||||||
该目录既是已有 PHP/WASI SDK 的输入,也是 PHPX 构建结果的安装位置: |
|
||||||
|
|
||||||
```text |
|
||||||
wasm/wasm32-wasip2/ |
|
||||||
├── include/php/ PHP/WASI 头文件 |
|
||||||
├── include/phpx/ PHPX/TypePHP 运行时头文件 |
|
||||||
├── lib/libphp.a |
|
||||||
├── lib/libphpx.a |
|
||||||
├── lib/libgmp.a |
|
||||||
├── lib/libgmpxx.a |
|
||||||
├── lib/libmpfr.a |
|
||||||
├── lib/libmpdec.a |
|
||||||
├── lib/libmpdec++.a |
|
||||||
└── .typephp-wasi-sdk-abi |
|
||||||
``` |
|
||||||
|
|
||||||
不要把 host 平台的 `libphpx.so`、`phpx.dll` 或 `.a` 文件复制到这里。 |
|
||||||
WASM 静态库包含目标 ABI,不能跨 WASI、Linux、macOS 或 Windows 使用。 |
|
||||||
|
|
||||||
## 工具链准备 |
|
||||||
|
|
||||||
PHPX WASM 当前只支持 WASI 0.2 Preview 2。将 WASI SDK 加入 `PATH`: |
|
||||||
|
|
||||||
```shell |
|
||||||
export PATH=/opt/wasi-sdk-33.0/bin:$PATH |
|
||||||
``` |
|
||||||
|
|
||||||
`PATH` 只负责让 shell 和构建工具找到 WASI SDK 程序,并不会让 CMake 自动选择 |
|
||||||
WASI target。第一次配置构建目录时仍然必须传入 |
|
||||||
`-DCMAKE_TOOLCHAIN_FILE=.../wasi-sdk-p2.cmake`。如果省略它,CMake 会选择 host |
|
||||||
平台的 `/usr/bin/cc` 和 `/usr/bin/c++`,PHPX 的目标检查会立即拒绝该配置。 |
|
||||||
|
|
||||||
确认必要工具: |
|
||||||
|
|
||||||
```shell |
|
||||||
command -v wasm32-wasip2-clang |
|
||||||
command -v wasm32-wasip2-clang++ |
|
||||||
command -v llvm-ar |
|
||||||
command -v llvm-ranlib |
|
||||||
command -v llvm-nm |
|
||||||
command -v cmake |
|
||||||
command -v ninja |
|
||||||
``` |
|
||||||
|
|
||||||
确认编译目标: |
|
||||||
|
|
||||||
```shell |
|
||||||
wasm32-wasip2-clang++ --print-target-triple |
|
||||||
``` |
|
||||||
|
|
||||||
必须输出: |
|
||||||
|
|
||||||
```text |
|
||||||
wasm32-unknown-wasip2 |
|
||||||
``` |
|
||||||
|
|
||||||
安装前缀必须已经包含与当前 PHPX 匹配的 PHP/WASI 头文件和 `libphp.a`: |
|
||||||
|
|
||||||
```shell |
|
||||||
test -f "$PHPX_HOME/wasm/wasm32-wasip2/include/php/main/php.h" |
|
||||||
test -f "$PHPX_HOME/wasm/wasm32-wasip2/lib/libphp.a" |
|
||||||
``` |
|
||||||
|
|
||||||
## 日常开发:直接使用 CMake 重建 PHPX |
|
||||||
|
|
||||||
PHPX 的 `.cc` 或头文件发生变化时,直接使用 `phpx/wasm/CMakeLists.txt` 增量重建。 |
|
||||||
这是日常开发的推荐流程,不会重新下载或编译 PHP、GMP 和 MPFR,也不会重新生成 |
|
||||||
`libphp.a`。 |
|
||||||
|
|
||||||
首先从当前 WASI 编译器定位 CMake toolchain,避免依赖硬编码的 SDK 版本路径: |
|
||||||
|
|
||||||
```shell |
|
||||||
WASI_RESOURCE_DIR="$(wasm32-wasip2-clang++ --print-resource-dir)" |
|
||||||
WASI_SDK_ROOT="$(cd "$WASI_RESOURCE_DIR/../../.." && pwd)" |
|
||||||
WASI_CMAKE_TOOLCHAIN="$WASI_SDK_ROOT/share/cmake/wasi-sdk-p2.cmake" |
|
||||||
test -f "$WASI_CMAKE_TOOLCHAIN" |
|
||||||
``` |
|
||||||
|
|
||||||
### 使用 Ninja(推荐) |
|
||||||
|
|
||||||
首次配置持久化构建目录: |
|
||||||
|
|
||||||
```shell |
|
||||||
cmake \ |
|
||||||
-S "$PHPX_HOME/wasm" \ |
|
||||||
-B "$PHPX_HOME/build/wasm32-wasip2" \ |
|
||||||
-G Ninja \ |
|
||||||
-DCMAKE_TOOLCHAIN_FILE="$WASI_CMAKE_TOOLCHAIN" \ |
|
||||||
-DCMAKE_BUILD_TYPE=Release \ |
|
||||||
-DPHPX_WASI_SDK_DIR="$PHPX_HOME/wasm/wasm32-wasip2" \ |
|
||||||
-DCMAKE_INSTALL_PREFIX="$PHPX_HOME/wasm/wasm32-wasip2" |
|
||||||
``` |
|
||||||
|
|
||||||
toolchain 在 CMake 执行 `project()` 时生效,所以只能在构建目录的第一次配置时设置。 |
|
||||||
如果该目录此前未传 toolchain、已经缓存了 host 编译器,不要直接在原缓存上补参数; |
|
||||||
改用一个新的构建目录,例如: |
|
||||||
|
|
||||||
```shell |
|
||||||
cmake \ |
|
||||||
-S "$PHPX_HOME/wasm" \ |
|
||||||
-B "$PHPX_HOME/build/wasm32-wasip2-wasi" \ |
|
||||||
-G Ninja \ |
|
||||||
-DCMAKE_TOOLCHAIN_FILE="$WASI_CMAKE_TOOLCHAIN" \ |
|
||||||
-DCMAKE_BUILD_TYPE=Release \ |
|
||||||
-DPHPX_WASI_SDK_DIR="$PHPX_HOME/wasm/wasm32-wasip2" \ |
|
||||||
-DCMAKE_INSTALL_PREFIX="$PHPX_HOME/wasm/wasm32-wasip2" |
|
||||||
``` |
|
||||||
|
|
||||||
后续的 build/install 命令也应使用这个新目录。 |
|
||||||
|
|
||||||
编译并安装: |
|
||||||
|
|
||||||
```shell |
|
||||||
cmake --build "$PHPX_HOME/build/wasm32-wasip2" --parallel 16 |
|
||||||
cmake --install "$PHPX_HOME/build/wasm32-wasip2" |
|
||||||
``` |
|
||||||
|
|
||||||
以后 PHPX 源码再次变化时,只需要执行: |
|
||||||
|
|
||||||
```shell |
|
||||||
cmake --build "$PHPX_HOME/build/wasm32-wasip2" --parallel 16 |
|
||||||
cmake --install "$PHPX_HOME/build/wasm32-wasip2" |
|
||||||
``` |
|
||||||
|
|
||||||
CMake/Ninja 只会重新编译发生变化的源文件,然后更新安装目录中的 `libphpx.a`。 |
|
||||||
|
|
||||||
### 使用 Make |
|
||||||
|
|
||||||
可以使用 `make`,但首次配置时必须选择 `Unix Makefiles` 生成器,并使用另一个构建 |
|
||||||
目录,不能在已经由 Ninja 配置的目录中切换生成器: |
|
||||||
|
|
||||||
```shell |
|
||||||
cmake \ |
|
||||||
-S "$PHPX_HOME/wasm" \ |
|
||||||
-B "$PHPX_HOME/build/wasm32-wasip2-make" \ |
|
||||||
-G "Unix Makefiles" \ |
|
||||||
-DCMAKE_TOOLCHAIN_FILE="$WASI_CMAKE_TOOLCHAIN" \ |
|
||||||
-DCMAKE_BUILD_TYPE=Release \ |
|
||||||
-DPHPX_WASI_SDK_DIR="$PHPX_HOME/wasm/wasm32-wasip2" \ |
|
||||||
-DCMAKE_INSTALL_PREFIX="$PHPX_HOME/wasm/wasm32-wasip2" |
|
||||||
|
|
||||||
make -C "$PHPX_HOME/build/wasm32-wasip2-make" -j16 |
|
||||||
make -C "$PHPX_HOME/build/wasm32-wasip2-make" install |
|
||||||
``` |
|
||||||
|
|
||||||
后续修改 PHPX 代码后,只需重复两条 `make` 命令。也可以使用生成器无关的形式: |
|
||||||
|
|
||||||
```shell |
|
||||||
cmake --build "$PHPX_HOME/build/wasm32-wasip2-make" --parallel 16 |
|
||||||
cmake --install "$PHPX_HOME/build/wasm32-wasip2-make" |
|
||||||
``` |
|
||||||
|
|
||||||
Ninja 与 Make 的产物相同;Ninja 通常依赖扫描和增量构建更快,因此内部开发默认 |
|
||||||
使用 Ninja。 |
|
||||||
|
|
||||||
此流程会更新: |
|
||||||
|
|
||||||
- `lib/libphpx.a` |
|
||||||
- `lib/libmpdec.a` 和 `lib/libmpdec++.a`(仅相关源码变化时重编) |
|
||||||
- `include/phpx/` 下的 PHPX 公共头文件 |
|
||||||
- `.typephp-wasi-runtime-abi` |
|
||||||
|
|
||||||
它不会更新 `libphp.a`、GMP、MPFR,也不会重写完整 SDK 的 |
|
||||||
`.typephp-wasi-sdk-abi`。因此该流程应在一个已经完整安装的 SDK 上执行。 |
|
||||||
|
|
||||||
### 强制重新编译 PHPX |
|
||||||
|
|
||||||
怀疑旧对象或 CMake 缓存不再可信时,优先使用一个新的、明确的构建目录: |
|
||||||
|
|
||||||
```shell |
|
||||||
cmake \ |
|
||||||
-S "$PHPX_HOME/wasm" \ |
|
||||||
-B "$PHPX_HOME/build/wasm32-wasip2-clean" \ |
|
||||||
-G Ninja \ |
|
||||||
-DCMAKE_TOOLCHAIN_FILE="$WASI_CMAKE_TOOLCHAIN" \ |
|
||||||
-DCMAKE_BUILD_TYPE=Release \ |
|
||||||
-DPHPX_WASI_SDK_DIR="$PHPX_HOME/wasm/wasm32-wasip2" \ |
|
||||||
-DCMAKE_INSTALL_PREFIX="$PHPX_HOME/wasm/wasm32-wasip2" |
|
||||||
|
|
||||||
cmake --build "$PHPX_HOME/build/wasm32-wasip2-clean" --parallel 16 |
|
||||||
cmake --install "$PHPX_HOME/build/wasm32-wasip2-clean" |
|
||||||
``` |
|
||||||
|
|
||||||
这样不会删除安装目录中的 `libphp.a` 和依赖库,也不会混用旧的 CMake 配置。 |
|
||||||
|
|
||||||
## 首次构建或重建 PHPX 数值依赖 |
|
||||||
|
|
||||||
以下情况使用 PHPX 的统一构建入口: |
|
||||||
|
|
||||||
- 首次建立 PHPX WASI 安装目录; |
|
||||||
- GMP 或 MPFR 版本、补丁、编译参数发生变化; |
|
||||||
- PHPX vendored mpdecimal 或其 WASI 配置发生变化; |
|
||||||
- 需要同时检查并安装 PHPX 所有 WASI 头文件和静态库。 |
|
||||||
|
|
||||||
```shell |
|
||||||
cd "$PHPX_HOME" |
|
||||||
|
|
||||||
./wasm/build.sh \ |
|
||||||
--prefix "$PHPX_HOME/wasm/wasm32-wasip2" \ |
|
||||||
--build-dir "$PHPX_HOME/build/wasm32-wasip2-sdk" \ |
|
||||||
--jobs 16 |
|
||||||
``` |
|
||||||
|
|
||||||
显式使用 `$PHPX_HOME/build/`,避免默认 `/tmp` 构建目录在重启后丢失。下载的 GMP、 |
|
||||||
MPFR 源码与构建缓存会保留,可以在后续构建中复用。 |
|
||||||
|
|
||||||
该入口会构建或安装: |
|
||||||
|
|
||||||
- `libphpx.a` |
|
||||||
- `libgmp.a`、`libgmpxx.a` |
|
||||||
- `libmpfr.a` |
|
||||||
- `libmpdec.a`、`libmpdec++.a` |
|
||||||
- 对应头文件和 PHPX runtime ABI marker |
|
||||||
|
|
||||||
它要求安装前缀中已经存在 PHP/WASI 头文件;它不会构建 `libphp.a`。 |
|
||||||
|
|
||||||
## PHP ABI 变化:重建完整 SDK |
|
||||||
|
|
||||||
如果 PHP 源码、扩展集合、PHP 配置、Zend ABI 或 PHP 安装头文件发生变化,必须从 |
|
||||||
TypePHP 编译器仓库重建完整 SDK,不能只替换 `libphpx.a`: |
|
||||||
|
|
||||||
```shell |
|
||||||
cd /home/swoole/workspace/aot/compiler |
|
||||||
|
|
||||||
./wasm/build-sdk.sh \ |
|
||||||
--prefix "$PHPX_HOME/wasm/wasm32-wasip2" \ |
|
||||||
--php-source "$PWD/projects/php-8.5.9" \ |
|
||||||
--phpx-source "$PHPX_HOME" \ |
|
||||||
--build-dir "$PWD/build/wasm-sdk" \ |
|
||||||
--jobs 16 |
|
||||||
``` |
|
||||||
|
|
||||||
完整构建依次安装 PHP 与 PHPX 部分,并在全部产物验证成功后写入: |
|
||||||
|
|
||||||
```text |
|
||||||
.typephp-wasi-sdk-abi |
|
||||||
``` |
|
||||||
|
|
||||||
不要手工伪造该 marker。marker 存在只表示构建流程声明 ABI 匹配,不能修复实际混用 |
|
||||||
的旧头文件或静态库。 |
|
||||||
|
|
||||||
## 产物检查 |
|
||||||
|
|
||||||
完成安装后检查关键文件: |
|
||||||
|
|
||||||
```shell |
|
||||||
WASI_PREFIX="$PHPX_HOME/wasm/wasm32-wasip2" |
|
||||||
|
|
||||||
test -s "$WASI_PREFIX/lib/libphpx.a" |
|
||||||
test -s "$WASI_PREFIX/lib/libphp.a" |
|
||||||
test -f "$WASI_PREFIX/include/phpx/phpx.h" |
|
||||||
test -f "$WASI_PREFIX/include/phpx/phpx_helper.h" |
|
||||||
test -f "$WASI_PREFIX/include/phpx/typephp_helper.h" |
|
||||||
|
|
||||||
llvm-ar t "$WASI_PREFIX/lib/libphpx.a" | head |
|
||||||
cat "$WASI_PREFIX/.typephp-wasi-runtime-abi" |
|
||||||
cat "$WASI_PREFIX/.typephp-wasi-sdk-abi" |
|
||||||
``` |
|
||||||
|
|
||||||
当前 marker 应分别为: |
|
||||||
|
|
||||||
```text |
|
||||||
typephp-wasip2-phpx-abi-v1 |
|
||||||
typephp-wasip2-sdk-abi-v4 |
|
||||||
``` |
|
||||||
|
|
||||||
marker 版本将随 ABI 设计升级;如果代码中的预期值已经变化,应以当前构建脚本为准, |
|
||||||
不能为了通过检测而回写旧值。 |
|
||||||
|
|
||||||
## TypePHP 回归验证 |
|
||||||
|
|
||||||
先验证 Wasmtime component: |
|
||||||
|
|
||||||
```shell |
|
||||||
cd /home/swoole/workspace/aot/compiler |
|
||||||
|
|
||||||
PHPX_HOME="$PHPX_HOME" \ |
|
||||||
./run-tests.php --wasm --compiler ./bin/tpc.php tests/wasm/ |
|
||||||
``` |
|
||||||
|
|
||||||
再验证 Wasmtime 与 Chrome 输出一致,并覆盖并行 build/output 目录隔离: |
|
||||||
|
|
||||||
```shell |
|
||||||
PHPX_HOME="$PHPX_HOME" \ |
|
||||||
./run-tests.php -j 4 --target wasm-all --compiler ./bin/tpc.php tests/wasm/ |
|
||||||
``` |
|
||||||
|
|
||||||
browser 测试还要求 `jco`、Node.js 和 Chrome 位于 `PATH`。`wasm-all` 会分别在 |
|
||||||
Wasmtime 和 Chrome 中执行每个用例,并比较两端输出。 |
|
||||||
|
|
||||||
最后构建浏览器示例: |
|
||||||
|
|
||||||
```shell |
|
||||||
cd examples/wasm-hello |
|
||||||
PHPX_HOME="$PHPX_HOME" ../../bin/tpc.php project.yml |
|
||||||
npm run build |
|
||||||
``` |
|
||||||
|
|
||||||
## 常见错误 |
|
||||||
|
|
||||||
### `PersistentCacheSlot` 或 PHPX helper 未定义 |
|
||||||
|
|
||||||
生成代码使用了新 PHPX 头文件/API,但安装前缀中的 `include/phpx/` 或 |
|
||||||
`lib/libphpx.a` 仍是旧版本。执行“日常开发:仅重建 PHPX”流程,并确保配置和安装 |
|
||||||
使用同一个 `PHPX_WASI_SDK_DIR`/`CMAKE_INSTALL_PREFIX`。 |
|
||||||
|
|
||||||
### `TypePHP WASI SDK is missing or ABI-incompatible` |
|
||||||
|
|
||||||
检查 `PHPX_HOME` 是否指向实际 PHPX 根目录,以及完整 SDK marker、PHP/PHPX 头文件 |
|
||||||
和静态库是否来自同一次兼容构建。PHP ABI 已变化时执行完整 SDK 重建。 |
|
||||||
|
|
||||||
### CMake 检测到 host 编译器 |
|
||||||
|
|
||||||
必须传入 WASI SDK 的 `wasi-sdk-p2.cmake`。不要直接用 PHPX 根目录的 host |
|
||||||
`CMakeLists.txt` 构建 WASM。将 WASI SDK 加入 `PATH` 本身并不等价于加载 CMake |
|
||||||
toolchain。如果 `CMakeCache.txt` 已记录 `/usr/bin/cc` 或 `/usr/bin/c++`,使用一个 |
|
||||||
新的构建目录重新配置。 |
|
||||||
|
|
||||||
### 修改 PHPX 后 TypePHP 仍链接旧实现 |
|
||||||
|
|
||||||
确认 `PHPX_HOME` 的优先级高于 Composer 目录,并检查实际产物时间: |
|
||||||
|
|
||||||
```shell |
|
||||||
stat "$PHPX_HOME/wasm/wasm32-wasip2/lib/libphpx.a" |
|
||||||
``` |
|
||||||
|
|
||||||
TypePHP 应从同一个 `$PHPX_HOME/wasm/wasm32-wasip2` 同时读取头文件与静态库。 |
|
||||||
@ -1,223 +0,0 @@ |
|||||||
# PHP 8.4 Property Hook 集成设计 |
|
||||||
|
|
||||||
本文记录 TypePHP 编译器与 PHPX 对 PHP 8.4 Property Hook 的实现方式,重点说明 Zend 元数据注册、对象内省、内存生命周期和版本兼容边界。本文是内部维护文档;用户侧语法说明应放在外部文档仓库。 |
|
||||||
|
|
||||||
Interface 中不带实现体的 Property Hook 属于抽象属性契约,不走本文描述的具体类 lowering 流程;其模型、方差检查和 Zend 元数据注册见 [Interface Property Hook 实现方案](INTERFACE_PROPERTY_HOOKS.md)。 |
|
||||||
|
|
||||||
## 1. 背景 |
|
||||||
|
|
||||||
TypePHP 会把 Property Hook 的函数体编译成隐藏的 AOT getter/setter。仅完成这一步,可以满足编译器明确识别出的属性读写,但 ZendVM 并不知道这些隐藏方法代表 Property Hook,因此以下动态能力会与 PHP 8.4 不一致: |
|
||||||
|
|
||||||
- `ReflectionProperty::hasHooks()`、`getHooks()` 和 `isVirtual()`; |
|
||||||
- `get_object_vars()`、`json_encode()` 和 `var_export()`; |
|
||||||
- 对象的 `foreach` 遍历; |
|
||||||
- backed property 与 virtual property 的存储差异; |
|
||||||
- ZendVM 发起的动态属性读写。 |
|
||||||
|
|
||||||
TypePHP 不单独模拟这些 PHP 行为。编译器在 lowering 后保留 Hook 元数据,类在 MINIT 阶段注册时由 PHPX 将 AOT 方法接入 PHP 8.4 原生 Property Hook 结构。此后 Reflection 和对象内省复用 ZendVM 的标准实现。 |
|
||||||
|
|
||||||
## 2. 编译流程 |
|
||||||
|
|
||||||
### 2.1 AST lowering |
|
||||||
|
|
||||||
`PropertyHookLowering` 将每个 Hook 转换为隐藏类方法,并在属性 AST 上记录: |
|
||||||
|
|
||||||
- getter/setter 对应的隐藏方法名; |
|
||||||
- Hook 是否访问自身 backing storage; |
|
||||||
- 属性是否为 virtual property。 |
|
||||||
|
|
||||||
例如: |
|
||||||
|
|
||||||
```php |
|
||||||
public string $name { |
|
||||||
get => strtoupper($this->name); |
|
||||||
set => $this->name = trim($value); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
在内部会产生等价的隐藏 getter/setter。Hook 中的 `$this->name` 会被标记为 backing access,避免再次调用 Hook 而递归。 |
|
||||||
|
|
||||||
如果 Hook 没有访问 backing storage,则该属性标记为 virtual。这个结论必须在 lowering 阶段获得,因为生成 Zend 属性声明时需要据此决定是否分配属性槽位。 |
|
||||||
|
|
||||||
### 2.2 类注册代码 |
|
||||||
|
|
||||||
`gen_stub.php` 声明属性并取得 `zend_property_info *` 后生成: |
|
||||||
|
|
||||||
```cpp |
|
||||||
typephp_register_property_hooks( |
|
||||||
class_entry, |
|
||||||
property_info, |
|
||||||
getter_method_name, |
|
||||||
setter_method_name |
|
||||||
); |
|
||||||
``` |
|
||||||
|
|
||||||
调用发生在类的持久化注册阶段,不在请求热路径中。 |
|
||||||
|
|
||||||
## 3. PHPX 注册流程 |
|
||||||
|
|
||||||
PHPX 的 `typephp_register_property_hooks()` 只在 PHP 8.4 及以上版本实现,并位于 TypePHP 专用 helper 中。 |
|
||||||
|
|
||||||
### 3.1 查找 AOT 实现方法 |
|
||||||
|
|
||||||
PHPX 从类方法表找到 lowering 生成的隐藏方法: |
|
||||||
|
|
||||||
```cpp |
|
||||||
zend_hash_str_find_ptr(&ce->function_table, method_name.data(), method_name.size()); |
|
||||||
``` |
|
||||||
|
|
||||||
该方法是已注册的 `zend_internal_function`,其 handler 最终进入 TypePHP 生成的 C++ getter/setter。查找只执行一次;属性读写时不会重复查询函数表。 |
|
||||||
|
|
||||||
### 3.2 创建 Hook 函数描述 |
|
||||||
|
|
||||||
不能直接修改或复用类方法表中的隐藏函数对象。Zend Property Hook 需要独立的函数身份和属性关联: |
|
||||||
|
|
||||||
```cpp |
|
||||||
hook->function_name = "$name::get"; // 或 "$name::set" |
|
||||||
hook->prop_info = property_info; |
|
||||||
``` |
|
||||||
|
|
||||||
PHPX 因此复制一份 `zend_internal_function` 描述,并替换 Hook 专属字段。复制不会生成另一份 C++ 实现;handler、参数信息和其他持久化数据仍来自原 AOT 方法。 |
|
||||||
|
|
||||||
独立函数描述可以避免修改隐藏方法后破坏类方法表的 key、反射名称或所有权关系,并让 Reflection 正确报告 `$name::get` 和 `$name::set`。 |
|
||||||
|
|
||||||
### 3.3 挂载属性 Hook |
|
||||||
|
|
||||||
PHP 8.4 在 `zend_property_info` 中新增了 Hook 表: |
|
||||||
|
|
||||||
```cpp |
|
||||||
property_info->hooks[ZEND_PROPERTY_HOOK_GET] = getter; |
|
||||||
property_info->hooks[ZEND_PROPERTY_HOOK_SET] = setter; |
|
||||||
``` |
|
||||||
|
|
||||||
同时必须更新: |
|
||||||
|
|
||||||
```cpp |
|
||||||
ce->num_hooked_props++; |
|
||||||
``` |
|
||||||
|
|
||||||
Zend 的 Reflection、对象属性构建和继承检查都会读取这些元数据。只注册隐藏方法而不填写 `property_info->hooks`,不会被 Zend 识别为真正的 Property Hook。 |
|
||||||
|
|
||||||
### 3.4 安装 Hook 对象遍历器 |
|
||||||
|
|
||||||
PHPX 在类没有自定义 iterator 时设置: |
|
||||||
|
|
||||||
```cpp |
|
||||||
ce->get_iterator = zend_hooked_object_get_iterator; |
|
||||||
``` |
|
||||||
|
|
||||||
`zend_hooked_object_get_iterator()` 是 PHP 8.4 在 `zend_property_hooks.h` 中导出的 `ZEND_API`。PHP 自身编译包含 Property Hook 的类时也会安装这个 iterator。 |
|
||||||
|
|
||||||
普通对象 iterator 主要遍历物理属性槽,而 Hook iterator 还负责: |
|
||||||
|
|
||||||
- 对 backed property 和 virtual property 调用 getter; |
|
||||||
- 跳过没有 getter 的 virtual property; |
|
||||||
- 执行属性可见性规则; |
|
||||||
- 拒绝不支持的引用遍历; |
|
||||||
- 合并动态属性。 |
|
||||||
|
|
||||||
因此不应在 PHPX 中复制一套遍历实现。直接复用 Zend 的导出实现可以保持 `foreach` 行为一致,并降低后续维护成本。 |
|
||||||
|
|
||||||
## 4. Virtual property |
|
||||||
|
|
||||||
PHP 8.4 使用特殊 offset 表示 virtual property: |
|
||||||
|
|
||||||
```cpp |
|
||||||
#define ZEND_VIRTUAL_PROPERTY_OFFSET ((uint32_t) -1) |
|
||||||
``` |
|
||||||
|
|
||||||
Zend 声明属性时,需要以 `IS_UNDEF` 作为声明值,才会为带 `ZEND_ACC_VIRTUAL` 的属性建立 virtual offset。因此生成代码使用: |
|
||||||
|
|
||||||
```cpp |
|
||||||
zval default_value; |
|
||||||
ZVAL_UNDEF(&default_value); |
|
||||||
``` |
|
||||||
|
|
||||||
不能用 `null` 或普通默认值代替,否则 Zend 可能分配 backing slot,`ReflectionProperty::isVirtual()` 也会得到错误结果。 |
|
||||||
|
|
||||||
## 5. 对象内省与序列化 |
|
||||||
|
|
||||||
当 `ce->num_hooked_props` 非零时,Zend 的 `zend_std_get_properties_for()` 会在 JSON、`get_object_vars()` 和 `var_export()` 等场景调用 `zend_hooked_object_build_properties()`。该函数读取 Hook 后的公开属性值。 |
|
||||||
|
|
||||||
序列化采用不同语义: |
|
||||||
|
|
||||||
- virtual property 没有持久状态,不进入序列化结果; |
|
||||||
- backed property 序列化 backing value,而不是 getter 计算后的值; |
|
||||||
- 私有存储属性仍按 PHP 的属性名修饰规则序列化。 |
|
||||||
|
|
||||||
这一区别是 PHP 8.4 的既有行为,不应为了让 JSON 和序列化输出相同而覆盖。 |
|
||||||
|
|
||||||
## 6. 生命周期与线程安全 |
|
||||||
|
|
||||||
TypePHP AOT 类以 persistent internal class 注册。Hook 表、Hook 函数描述和函数名必须具有相同的进程级生命周期,因此 PHPX 使用: |
|
||||||
|
|
||||||
```cpp |
|
||||||
pemalloc(size, true); |
|
||||||
zend_string_init(data, length, true); |
|
||||||
``` |
|
||||||
|
|
||||||
不能使用 request 内存;否则 RSHUTDOWN 后 class entry 会保留悬空指针,下一请求访问属性或 Reflection 时可能崩溃。 |
|
||||||
|
|
||||||
注册只发生在 MINIT: |
|
||||||
|
|
||||||
- 请求执行期间只读 Hook 元数据; |
|
||||||
- 不需要在每次请求重新构建; |
|
||||||
- 不需要在每次属性访问查找隐藏方法; |
|
||||||
- NTS 没有锁开销; |
|
||||||
- ZTS 下在工作线程处理请求前已完成注册,不会并发修改 class entry。 |
|
||||||
|
|
||||||
## 7. PHP 版本边界 |
|
||||||
|
|
||||||
TypePHP 与 PHPX 的最低版本均为 PHP 8.4,因此 Property Hook 实现直接使用以下 PHP 8.4 ABI: |
|
||||||
|
|
||||||
- `zend_property_info::hooks`; |
|
||||||
- `zend_class_entry::num_hooked_props`; |
|
||||||
- `ZEND_PROPERTY_HOOK_*`; |
|
||||||
- `ZEND_PROPERTY_HOOK_STRUCT_SIZE`; |
|
||||||
- `ZEND_VIRTUAL_PROPERTY_OFFSET`; |
|
||||||
- `zend_hooked_object_get_iterator()`。 |
|
||||||
|
|
||||||
PHPX 头文件和 CMake 配置会拒绝 PHP 8.4 以下的 headers/`php-config`。PHP 8.4 与 8.5 仍分别构建对应 PHPX 二进制;`--php-version` 只控制源码语法,不要求与 `libphp.so` 的小版本完全相同,但两者都必须不低于 8.4。 |
|
||||||
|
|
||||||
## 8. ABI 风险和升级检查 |
|
||||||
|
|
||||||
`zend_hooked_object_get_iterator()` 是导出的 Zend API,但 Property Hook 整体仍属于版本相关的底层 Zend ABI。PHP 8.4 没有提供一个完整的高层 `zend_declare_property_hook()` 扩展 API,因此当前实现需要填写 Zend 元数据。 |
|
||||||
|
|
||||||
采用该方案的依据是: |
|
||||||
|
|
||||||
1. TypePHP 与 PHPX 版本绑定,并针对具体 PHP 版本重新编译; |
|
||||||
2. 注册流程与 Zend 编译器处理原生 Property Hook 的步骤一致; |
|
||||||
3. 只复用 Zend 导出的 iterator,不复制其复杂实现; |
|
||||||
4. PHP 8.4 以下版本在构建入口统一拒绝; |
|
||||||
5. 所有注册均在 MINIT 完成,不增加请求热路径上的名称查找。 |
|
||||||
|
|
||||||
升级 PHP 版本时必须检查: |
|
||||||
|
|
||||||
1. `zend_property_info` 的 Hook 字段和所有权是否变化; |
|
||||||
2. `ZEND_PROPERTY_HOOK_COUNT` 和 Hook kind 是否增加; |
|
||||||
3. virtual property 的声明条件和 offset 是否变化; |
|
||||||
4. `zend_hooked_object_get_iterator()` 是否仍为导出 API; |
|
||||||
5. class linking、继承、variance 和 Reflection 是否增加新的必填元数据; |
|
||||||
6. persistent internal function 的销毁和继承复制规则是否变化。 |
|
||||||
|
|
||||||
如果 Zend 将来提供正式的扩展注册 API,应优先迁移到该 API,减少对内部结构布局的直接依赖。 |
|
||||||
|
|
||||||
## 9. 测试要求 |
|
||||||
|
|
||||||
Property Hook 改动至少需要覆盖: |
|
||||||
|
|
||||||
- 直接 getter/setter 和 backing access; |
|
||||||
- virtual property 与 backed property 的 Reflection 差异; |
|
||||||
- `hasHooks()`、`getHooks()`、Hook 名称和 final 状态; |
|
||||||
- `get_object_vars()`、JSON 和对象 `foreach`; |
|
||||||
- 序列化只包含真实存储状态; |
|
||||||
- 动态 Zend 属性读写; |
|
||||||
- 继承和属性可见性; |
|
||||||
- PHP 8.4 与 PHP 8.5 构建。 |
|
||||||
|
|
||||||
当前核心回归测试位于: |
|
||||||
|
|
||||||
- `tests/compiler/object_property/property-hooks.phpt`; |
|
||||||
- `tests/compiler/object_property/property-hooks-operations.phpt`; |
|
||||||
- `tests/compiler/object_property/property-hooks-reflection.phpt`; |
|
||||||
- `tests/compiler/object_property/property-hooks-introspection.phpt`。 |
|
||||||
@ -1,151 +0,0 @@ |
|||||||
# py2php:Python → TypePHP 源码转换工具 |
|
||||||
|
|
||||||
## 用法 |
|
||||||
|
|
||||||
```bash |
|
||||||
./bin/tpc.php --convert-python-to-php examples/python/version.py > examples/python/version.php |
|
||||||
``` |
|
||||||
|
|
||||||
生成的 PHP 源码输出到 stdout,错误输出到 stderr,退出码 0 成功 / 1 失败。 |
|
||||||
|
|
||||||
## 架构 |
|
||||||
|
|
||||||
``` |
|
||||||
.py 源码 |
|
||||||
└─ PythonAstLoader python3 子进程(ast 模块)→ JSON AST |
|
||||||
└─ PythonToTypePhpConverter AST → TypePHP 源码字符串 |
|
||||||
└─ Command::execute CLI 分发(--convert-python-to-php) |
|
||||||
``` |
|
||||||
|
|
||||||
- 源码:`src/PythonTools/Command.php`、`src/PythonTools/Converter/` |
|
||||||
- 不支持的语法抛出 `RuntimeException("{file}:{line}: unsupported Python syntax {节点类型}[: 详情]")`,CLI 层转为 stderr + 退出码 1。 |
|
||||||
- 测试:`phpunit/src/PythonTools/`(`PythonToTypePhpConverterTest`、`PythonAstLoaderTest`、`PythonToolsCommandTest`),与本文档逐项对应。 |
|
||||||
|
|
||||||
## 语句支持矩阵 |
|
||||||
|
|
||||||
| Python 语法 | 状态 | 转换规则 / 报错 | |
|
||||||
|---|---|---| |
|
||||||
| `x = expr` | ✅ | `$x = expr;`,模块级变量自动注入 `global` | |
|
||||||
| `x = y = 1`(链式赋值) | ✅ | `$x = $y = 1;`(仅限名称目标;含属性/下标目标时报错) | |
|
||||||
| `x += expr` 等增强赋值 | ✅ | 支持 `+ - * / % ** << >> \| ^ &` 系列;`//=` `@=` 展开为 `python\operator\floordiv/matmul($x, ...)` 调用 | |
|
||||||
| `x: int = expr` | ✅ | 忽略注解,转换为普通赋值 | |
|
||||||
| `x: int`(纯注解) | ✅ | 转为注释 `// annotation-only declaration: x`,不登记为模块全局 | |
|
||||||
| `a, b = x`(解构) | ✅ | `[$a, $b] = $x->toArray();`(PyObject 转 PHP 数组后解构;元素允许名称/属性/下标。嵌套解构、星号解构 `a, *b = x`、链式解构不支持。元素个数不匹配时按 PHP 语义补 null,不报 Python 的 ValueError) | |
|
||||||
| `def f(...)` | ✅ | 见「函数签名」;名为 `main` 的函数重命名为 `main_`(避免与 TypePHP 入口冲突),调用点同步改写 | |
|
||||||
| 嵌套 `def` | ❌ | `FunctionDef: nested functions require Python closure scope analysis` | |
|
||||||
| `@decorator` | ✅ | 见「函数装饰器」 | |
|
||||||
| `return [expr]` | ✅ | `return [expr];` | |
|
||||||
| `if / elif / else` | ✅ | 同构转换 | |
|
||||||
| `while` | ✅ | 同构转换;`while/else` 不支持 | |
|
||||||
| `for i in iter` | ✅ | `foreach (iter as $i)`;`for/else`、元组目标不支持 | |
|
||||||
| `break` / `continue` / `pass` | ✅ | `pass` → `// pass` 注释 | |
|
||||||
| `global x` | ✅ | `global $x;`(与自动注入的 global 并存时会重复出现,冗余但合法,属已知行为) | |
|
||||||
| `del x` / `del o.a` / `del d[k]` | ✅ | `unset(...)`;`del (a, b)` 元组/列表目标逐项展开;非法 del 目标(如 `del f()`)由 Python 解析器先行拒绝 | |
|
||||||
| 模块级字符串字面量(docstring) | ✅ | 转为 `/** ... */` 注释(`*/` 转义为 `* /`) | |
|
||||||
| `import a.b` | ✅ | `use python\a;`(仅首段作为别名,见「已知行为」) | |
|
||||||
| `import a.b as x` | ✅ | `use python\a\b as x;`(别名等于末段时省略 `as`) | |
|
||||||
| `from m import f [as g]` | ✅ | 调用点映射为 `python\m\f(...)` | |
|
||||||
| `from . import m` | ❌ | `ImportFrom: relative imports are not supported yet` | |
|
||||||
| `from m import *` | ❌ | `ImportFrom: star imports are not supported` | |
|
||||||
| `class` | ❌ | `ClassDef` | |
|
||||||
| `with` | ❌ | `With` | |
|
||||||
| `raise` / `try` / `assert` | ❌ | `Raise` / `Try` / `Assert` | |
|
||||||
| `async def` / `await` | ❌ | `AsyncFunctionDef`(`await` 不可达,外层先报错) | |
|
||||||
| `match` | ❌ | `Match` | |
|
||||||
| `nonlocal` | ❌ | `Nonlocal` | |
|
||||||
|
|
||||||
## 函数签名 |
|
||||||
|
|
||||||
| Python 形态 | 状态 | TypePHP 输出 | |
|
||||||
|---|---|---| |
|
||||||
| `def f(x, y=4)` | ✅ | `function f($x, $y = 4)` | |
|
||||||
| `def f(a, *, b)` | ✅ | `function f($a, $b = null)`(无默认值的仅关键字参数补 `null`) | |
|
||||||
| `def f(*args)` / `def f(**kw)` | ✅ | `function f(...$args)` | |
|
||||||
| `def f(*a, **kw)` | ❌ | `FunctionDef: simultaneous *args and **kwargs cannot be represented by one PHP signature` | |
|
||||||
| `lambda a, b=2: a + b` | ✅ | `fn ($a, $b = 2) => $a + $b` | |
|
||||||
|
|
||||||
## 表达式支持矩阵 |
|
||||||
|
|
||||||
| Python 语法 | 状态 | 转换规则 / 报错 | |
|
||||||
|---|---|---| |
|
||||||
| 字面量 `int / float / str / True / False / None` | ✅ | `var_export`;`None` → `null` | |
|
||||||
| `b'...'` bytes | ❌ | `{file}: Python bytes literals are not supported yet`(无行号) | |
|
||||||
| `1j` complex | ❌ | `{file}: Python complex literals are not supported yet`(无行号) | |
|
||||||
| 变量名 | ✅ | `$name`;`this` 转义为 `$this_` | |
|
||||||
| 模块别名作为值 | ❌ | `a Python module cannot be used as a first-class value in TypePHP namespace syntax` | |
|
||||||
| 属性链 `o.a.b` | ✅ | `$o->a->b`;模块别名链仅首段为模块成员:`sys.version_info.major` → `sys\version_info->major` | |
|
||||||
| 模块属性赋值/删除 | ❌ | `Attribute: Python module attributes cannot be assigned or deleted` | |
|
||||||
| 函数调用 | ✅ | 已定义函数直连 `f(...)`;内置函数映射 `python\len(...)`;`from m import f` 映射 `python\m\f(...)`;其他名字按变量可调用 `$f(...)` | |
|
||||||
| 关键字参数 / `*args` / `**kwargs` 调用 | ✅ | `f(x: 1, ...$args)` | |
|
||||||
| 容器字面量 `[] () {} {:}` | ✅ | `python\list/tuple/set/dict([...])`,支持 `...` 解包 | |
|
||||||
| 二元运算 `+ - * / % ** << >> \| ^ &` | ✅ | 同构转换 | |
|
||||||
| `//` 整除 / `@` 矩阵乘 | ✅ | `python\operator\floordiv(a, b)` / `python\operator\matmul(a, b)` | |
|
||||||
| 一元运算 `- + not ~` | ✅ | `- + ! ~` | |
|
||||||
| 比较 `== != < <= > >=` | ✅ | 同构转换 | |
|
||||||
| `is` / `is not` | ✅ | `===` / `!==` | |
|
||||||
| `in` / `not in` | ✅ | `python\operator\contains(b, a)`(参数交换)/ 取反 | |
|
||||||
| 链式比较 `a < b < c` | ❌ | `Compare: chained comparisons require explicit temporary variables` | |
|
||||||
| `a and b` / `a or b` | ❌ | `BoolOp` | |
|
||||||
| `x if c else y` | ✅ | `(c ? x : y)` | |
|
||||||
| 下标 `a[i]` / 切片 `a[l:u:s]` | ✅ | `$a[$i]` / `$a[python\slice(l, u, s)]`(缺省为 `null`) | |
|
||||||
| f-string | ✅ | 拼接 + `->toString()`;运算符等优先级敏感表达式整体加括号 | |
|
||||||
| f-string 的 `!r` 转换 / `:03d` 格式说明 | ❌ | `FormattedValue: formatted f-string conversions are not supported yet` | |
|
||||||
| 海象 `:=` | ✅ | 表达式内赋值 `($n = 10)` | |
|
||||||
| 推导式 / 生成器表达式 | ❌ | `ListComp` / `SetComp` / `DictComp` / `GeneratorExp` | |
|
||||||
| `yield` / `yield from` | ❌ | `Yield` / `YieldFrom` | |
|
||||||
|
|
||||||
## 函数装饰器 |
|
||||||
|
|
||||||
装饰器在 `main()` 起始处(其他顶层语句之前)按 Python 语义**自底向上**重绑定到同名模块变量: |
|
||||||
|
|
||||||
```python |
|
||||||
@a |
|
||||||
@b |
|
||||||
def greet(): ... |
|
||||||
``` |
|
||||||
|
|
||||||
```php |
|
||||||
function greet() { ... } |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
global $greet; |
|
||||||
$greet = b('greet'); |
|
||||||
$greet = a('greet'); |
|
||||||
... |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
- 装饰器可以是已定义函数、`from m import f` 导入符号、模块属性或装饰器工厂(`@dec('x')` → `$greet = dec('x')('greet');`) |
|
||||||
- 被装饰函数名登记为模块全局,所有调用点(包括其他函数体内)经 `global` + 变量间接调用装饰结果:`$greet()` |
|
||||||
- 被装饰函数体内的递归调用同样解析到装饰后的变量,与 Python 语义一致 |
|
||||||
|
|
||||||
## print / sys.exit 降级规则 |
|
||||||
|
|
||||||
仅当 PHP 行为与 Python 完全一致时才降级为原生语句: |
|
||||||
|
|
||||||
| 形态 | 输出 | |
|
||||||
|---|---| |
|
||||||
| `print()` | `echo "\n";` | |
|
||||||
| `print("a", "b")`(字符串/整数常量、模块属性、容器、f-string) | `echo 'a', ' ', 'b', "\n";` | |
|
||||||
| `print(1.5)`、`print(True)`、`print(x, sep=...)` | 不降级:`python\print(...)` | |
|
||||||
| 用户定义/导入/赋值遮蔽 `print` 后 | 不降级 | |
|
||||||
| `sys.exit()` / `sys.exit(2)`(含 `from sys import exit` 形式) | `exit;` / `exit(2);` | |
|
||||||
| `sys.exit("fail")` | 不降级:`sys\exit('fail');` | |
|
||||||
|
|
||||||
## 已知行为(非错误,但需留意) |
|
||||||
|
|
||||||
1. `import os.path`(无别名)只引入首段 `use python\os;`。 |
|
||||||
2. 函数内显式 `global x` 与按模块全局自动注入的 `global x` 会重复出现(合法 PHP)。 |
|
||||||
3. `print = str` 这类把内置名赋给变量的写法,右侧按变量处理(`$print = $str;`),不做内置名解析。 |
|
||||||
4. bytes/complex 字面量的报错没有行号(常量在 AST 加载阶段编码,位置信息未传递)。 |
|
||||||
5. 装饰器重绑定统一在 `main()` 起始处执行,与 Python "def 处即装饰" 的精确位置略有差异;装饰器表达式若依赖顶层语句后段的赋值,求值时机可能不同。 |
|
||||||
6. 被装饰函数名会登记为模块全局,导致所有函数的自动 `global` 注入清单中出现该名字(冗余但合法)。 |
|
||||||
|
|
||||||
## 运行测试 |
|
||||||
|
|
||||||
```bash |
|
||||||
vendor/bin/phpunit --filter 'PythonToTypePhpConverterTest|PythonAstLoaderTest|PythonToolsCommandTest' |
|
||||||
``` |
|
||||||
|
|
||||||
转换器测试依赖真实 `python3` 解析 AST,环境缺失时自动跳过。 |
|
||||||
@ -1,559 +0,0 @@ |
|||||||
<!doctype html> |
|
||||||
<html lang="zh-CN"> |
|
||||||
<head> |
|
||||||
<meta charset="utf-8"> |
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1"> |
|
||||||
<title>TypePHP 运行时初始化与关闭流程</title> |
|
||||||
<style> |
|
||||||
:root { |
|
||||||
color-scheme: dark; |
|
||||||
--bg: #09111f; |
|
||||||
--panel: #101b2d; |
|
||||||
--panel-2: #142238; |
|
||||||
--line: #2b3d59; |
|
||||||
--text: #e7edf7; |
|
||||||
--muted: #9baac0; |
|
||||||
--host: #94a3b8; |
|
||||||
--php: #9d8cff; |
|
||||||
--phpx: #29c7c9; |
|
||||||
--typephp: #ffae57; |
|
||||||
--project: #57d68d; |
|
||||||
--danger: #ff6b7a; |
|
||||||
--warn: #ffd166; |
|
||||||
--ok: #57d68d; |
|
||||||
--shadow: 0 18px 50px rgba(0, 0, 0, .24); |
|
||||||
} |
|
||||||
|
|
||||||
* { box-sizing: border-box; } |
|
||||||
|
|
||||||
html { scroll-behavior: smooth; } |
|
||||||
|
|
||||||
body { |
|
||||||
margin: 0; |
|
||||||
background: |
|
||||||
radial-gradient(circle at 10% 0%, rgba(157, 140, 255, .14), transparent 30rem), |
|
||||||
radial-gradient(circle at 90% 15%, rgba(41, 199, 201, .10), transparent 34rem), |
|
||||||
var(--bg); |
|
||||||
color: var(--text); |
|
||||||
font: 15px/1.7 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, |
|
||||||
"Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif; |
|
||||||
} |
|
||||||
|
|
||||||
main { width: min(1240px, calc(100% - 32px)); margin: 0 auto 80px; } |
|
||||||
|
|
||||||
header { |
|
||||||
padding: 64px 0 32px; |
|
||||||
border-bottom: 1px solid var(--line); |
|
||||||
} |
|
||||||
|
|
||||||
h1, h2, h3 { line-height: 1.25; letter-spacing: -.02em; } |
|
||||||
h1 { margin: 0 0 12px; font-size: clamp(32px, 5vw, 56px); } |
|
||||||
h2 { margin: 64px 0 20px; font-size: 30px; } |
|
||||||
h3 { margin: 28px 0 12px; font-size: 20px; } |
|
||||||
p { margin: 10px 0; } |
|
||||||
a { color: #8fcbff; } |
|
||||||
|
|
||||||
code { |
|
||||||
padding: .12em .38em; |
|
||||||
border: 1px solid rgba(255, 255, 255, .08); |
|
||||||
border-radius: 5px; |
|
||||||
background: rgba(0, 0, 0, .24); |
|
||||||
color: #f2f6fc; |
|
||||||
font: .92em/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; |
|
||||||
} |
|
||||||
|
|
||||||
pre { |
|
||||||
overflow: auto; |
|
||||||
padding: 18px; |
|
||||||
border: 1px solid var(--line); |
|
||||||
border-radius: 12px; |
|
||||||
background: #07101d; |
|
||||||
box-shadow: inset 0 1px rgba(255, 255, 255, .03); |
|
||||||
} |
|
||||||
|
|
||||||
pre code { padding: 0; border: 0; background: none; } |
|
||||||
|
|
||||||
.subtitle { max-width: 920px; color: var(--muted); font-size: 18px; } |
|
||||||
.meta { margin-top: 20px; color: var(--muted); font-size: 13px; } |
|
||||||
|
|
||||||
nav { |
|
||||||
position: sticky; |
|
||||||
z-index: 20; |
|
||||||
top: 0; |
|
||||||
display: flex; |
|
||||||
gap: 8px; |
|
||||||
overflow-x: auto; |
|
||||||
padding: 12px 0; |
|
||||||
background: rgba(9, 17, 31, .88); |
|
||||||
border-bottom: 1px solid var(--line); |
|
||||||
backdrop-filter: blur(14px); |
|
||||||
} |
|
||||||
|
|
||||||
nav a { |
|
||||||
flex: none; |
|
||||||
padding: 7px 11px; |
|
||||||
border: 1px solid var(--line); |
|
||||||
border-radius: 999px; |
|
||||||
color: var(--muted); |
|
||||||
text-decoration: none; |
|
||||||
} |
|
||||||
|
|
||||||
nav a:hover { color: var(--text); border-color: #567093; } |
|
||||||
|
|
||||||
.grid { display: grid; gap: 14px; } |
|
||||||
.grid.four { grid-template-columns: repeat(4, minmax(0, 1fr)); } |
|
||||||
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); } |
|
||||||
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); } |
|
||||||
|
|
||||||
.card { |
|
||||||
padding: 18px; |
|
||||||
border: 1px solid var(--line); |
|
||||||
border-radius: 14px; |
|
||||||
background: linear-gradient(145deg, rgba(20, 34, 56, .96), rgba(13, 24, 41, .96)); |
|
||||||
box-shadow: var(--shadow); |
|
||||||
} |
|
||||||
|
|
||||||
.card h3 { margin-top: 0; } |
|
||||||
.card p:last-child { margin-bottom: 0; } |
|
||||||
.layer-host { --accent: var(--host); } |
|
||||||
.layer-php { --accent: var(--php); } |
|
||||||
.layer-phpx { --accent: var(--phpx); } |
|
||||||
.layer-typephp { --accent: var(--typephp); } |
|
||||||
.layer-project { --accent: var(--project); } |
|
||||||
.layer-card { border-top: 3px solid var(--accent); } |
|
||||||
.layer-card .tag { color: var(--accent); } |
|
||||||
|
|
||||||
.tag { |
|
||||||
display: inline-block; |
|
||||||
margin-bottom: 9px; |
|
||||||
font-weight: 750; |
|
||||||
font-size: 12px; |
|
||||||
letter-spacing: .08em; |
|
||||||
text-transform: uppercase; |
|
||||||
} |
|
||||||
|
|
||||||
.callout { |
|
||||||
margin: 20px 0; |
|
||||||
padding: 16px 18px; |
|
||||||
border-left: 4px solid var(--warn); |
|
||||||
border-radius: 0 10px 10px 0; |
|
||||||
background: rgba(255, 209, 102, .09); |
|
||||||
} |
|
||||||
|
|
||||||
.callout.danger { border-color: var(--danger); background: rgba(255, 107, 122, .09); } |
|
||||||
.callout.ok { border-color: var(--ok); background: rgba(87, 214, 141, .09); } |
|
||||||
|
|
||||||
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 14px; } |
|
||||||
table { width: 100%; border-collapse: collapse; min-width: 780px; background: rgba(16, 27, 45, .78); } |
|
||||||
th, td { padding: 13px 15px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; } |
|
||||||
th { background: #17263d; color: #dce8f8; } |
|
||||||
tr:last-child td { border-bottom: 0; } |
|
||||||
td:first-child, th:first-child { white-space: nowrap; } |
|
||||||
|
|
||||||
.legend { display: flex; flex-wrap: wrap; gap: 10px 18px; margin: 18px 0; } |
|
||||||
.legend span { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); } |
|
||||||
.legend i { width: 11px; height: 11px; border-radius: 3px; background: var(--accent); } |
|
||||||
|
|
||||||
.flowchart { |
|
||||||
overflow-x: auto; |
|
||||||
margin: 22px 0; |
|
||||||
padding: 20px; |
|
||||||
border: 1px solid var(--line); |
|
||||||
border-radius: 16px; |
|
||||||
background: rgba(7, 16, 29, .72); |
|
||||||
box-shadow: var(--shadow); |
|
||||||
} |
|
||||||
|
|
||||||
.flow-title { margin: 0 0 16px; font-size: 17px; } |
|
||||||
.flow { display: flex; align-items: stretch; gap: 8px; min-width: 900px; } |
|
||||||
.flow.vertical { min-width: 0; flex-direction: column; max-width: 920px; margin: 0 auto; } |
|
||||||
.flow-step { |
|
||||||
position: relative; |
|
||||||
flex: 1; |
|
||||||
min-width: 145px; |
|
||||||
padding: 13px 12px; |
|
||||||
border: 1px solid color-mix(in srgb, var(--accent), transparent 45%); |
|
||||||
border-radius: 10px; |
|
||||||
background: color-mix(in srgb, var(--accent) 11%, #101b2d); |
|
||||||
} |
|
||||||
|
|
||||||
.flow-step strong { display: block; color: var(--accent); } |
|
||||||
.flow-step small { display: block; margin-top: 5px; color: var(--muted); line-height: 1.45; } |
|
||||||
.arrow { align-self: center; flex: none; color: #69809f; font-size: 23px; font-weight: 800; } |
|
||||||
.flow.vertical .arrow { transform: rotate(90deg); } |
|
||||||
|
|
||||||
.phase { |
|
||||||
display: grid; |
|
||||||
grid-template-columns: 118px minmax(0, 1fr); |
|
||||||
gap: 14px; |
|
||||||
align-items: start; |
|
||||||
padding: 14px 0; |
|
||||||
border-bottom: 1px dashed var(--line); |
|
||||||
} |
|
||||||
|
|
||||||
.phase:last-child { border-bottom: 0; } |
|
||||||
.phase-name { color: var(--muted); font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; } |
|
||||||
.phase-steps { display: flex; flex-wrap: wrap; align-items: center; gap: 7px; } |
|
||||||
.phase-steps .flow-step { flex: 0 1 245px; } |
|
||||||
.phase-steps .arrow { font-size: 18px; } |
|
||||||
|
|
||||||
.module-stack { display: grid; gap: 10px; } |
|
||||||
.module-row { |
|
||||||
display: grid; |
|
||||||
grid-template-columns: 150px minmax(0, 1fr) 160px; |
|
||||||
gap: 10px; |
|
||||||
align-items: center; |
|
||||||
padding: 12px; |
|
||||||
border: 1px solid var(--line); |
|
||||||
border-radius: 11px; |
|
||||||
background: rgba(20, 34, 56, .68); |
|
||||||
} |
|
||||||
|
|
||||||
.module-row .owner { color: var(--project); font-weight: 700; } |
|
||||||
.module-row .shared { color: var(--phpx); } |
|
||||||
.module-row .private { color: var(--typephp); text-align: right; } |
|
||||||
|
|
||||||
.status { font-weight: 750; } |
|
||||||
.yes { color: var(--ok); } |
|
||||||
.no { color: var(--danger); } |
|
||||||
.conditional { color: var(--warn); } |
|
||||||
|
|
||||||
ul, ol { padding-left: 22px; } |
|
||||||
li + li { margin-top: 6px; } |
|
||||||
|
|
||||||
.source-list code { word-break: break-all; } |
|
||||||
footer { margin-top: 64px; padding-top: 22px; border-top: 1px solid var(--line); color: var(--muted); } |
|
||||||
|
|
||||||
@media (max-width: 900px) { |
|
||||||
.grid.four, .grid.three, .grid.two { grid-template-columns: 1fr; } |
|
||||||
.module-row { grid-template-columns: 1fr; } |
|
||||||
.module-row .private { text-align: left; } |
|
||||||
.phase { grid-template-columns: 1fr; } |
|
||||||
} |
|
||||||
|
|
||||||
@media print { |
|
||||||
:root { color-scheme: light; --bg: #fff; --panel: #fff; --panel-2: #f7f9fc; --line: #ccd5e1; --text: #172033; --muted: #526176; } |
|
||||||
body { background: #fff; } |
|
||||||
nav { display: none; } |
|
||||||
.card, .flowchart { box-shadow: none; } |
|
||||||
} |
|
||||||
</style> |
|
||||||
</head> |
|
||||||
<body> |
|
||||||
<main> |
|
||||||
<header> |
|
||||||
<h1>TypePHP 运行时生命周期</h1> |
|
||||||
<p class="subtitle">PHP、PHPX、TypePHP 生成模块与具体项目的 init/shutdown 调用关系;覆盖原生 bin、ext、lib,多 TypePHP 模块,以及 WASI command / component。</p> |
|
||||||
<p class="meta">依据当前实现整理 · 2026-08-22 · TypePHP <code>4e00799</code> · PHPX <code>f0a67ba</code> · PHP/WASI <code>4dafa96b</code></p> |
|
||||||
</header> |
|
||||||
|
|
||||||
<nav aria-label="章节导航"> |
|
||||||
<a href="#layers">四层职责</a> |
|
||||||
<a href="#matrix">模式对比</a> |
|
||||||
<a href="#ext">EXT 多模块</a> |
|
||||||
<a href="#bin">BIN + EXT</a> |
|
||||||
<a href="#lib">LIB 多模块</a> |
|
||||||
<a href="#wasm">WASM</a> |
|
||||||
<a href="#shutdown">关闭顺序</a> |
|
||||||
<a href="#names">名称辨析</a> |
|
||||||
<a href="#rules">维护规则</a> |
|
||||||
</nav> |
|
||||||
|
|
||||||
<section id="layers"> |
|
||||||
<h2>一、先分清四个层次</h2> |
|
||||||
<div class="grid four"> |
|
||||||
<article class="card layer-card layer-php"> |
|
||||||
<span class="tag">PHP / ZendVM</span> |
|
||||||
<h3>运行时所有者</h3> |
|
||||||
<p><code>php_embed_init()</code>、<code>php_request_startup()</code>、<code>php_embed_shutdown()</code>,以及 MINIT/RINIT/RSHUTDOWN/MSHUTDOWN 的最终调度者。</p> |
|
||||||
</article> |
|
||||||
<article class="card layer-card layer-phpx"> |
|
||||||
<span class="tag">PHPX</span> |
|
||||||
<h3>C++ 安全封装与共享状态</h3> |
|
||||||
<p><code>php::request_init()</code> 初始化 Decimal、Native GC、Box 资源;<code>php::request_shutdown()</code> 执行 Native finalizer 并清理请求缓存。</p> |
|
||||||
</article> |
|
||||||
<article class="card layer-card layer-typephp"> |
|
||||||
<span class="tag">TypePHP</span> |
|
||||||
<h3>生成的 Zend 模块</h3> |
|
||||||
<p>生成 <code>zend_module_entry</code>、项目专属 MINIT/MSHUTDOWN/RINIT/RSHUTDOWN,以及 <code>typephp_<project>_runtime_*</code> ABI。</p> |
|
||||||
</article> |
|
||||||
<article class="card layer-card layer-project"> |
|
||||||
<span class="tag">Project</span> |
|
||||||
<h3>项目私有状态</h3> |
|
||||||
<p>全局变量、静态属性、数组常量、请求模板、Python module cache、项目符号 cache,以及 bin 模式的 <code>main()</code>。</p> |
|
||||||
</article> |
|
||||||
</div> |
|
||||||
|
|
||||||
<div class="callout"> |
|
||||||
<strong>同名但不同层:</strong>PHPX 的文件内 <code>module_init(zend_module_entry *)</code> 表示“向 Zend 注册并启动一个模块”;生成在项目命名空间里的 <code>static module_init()</code> 表示“初始化项目请求级数据”。后者不是 MINIT。 |
|
||||||
</div> |
|
||||||
|
|
||||||
<h3>生命周期嵌套关系</h3> |
|
||||||
<figure class="flowchart" aria-label="四层生命周期嵌套流程图"> |
|
||||||
<div class="flow-title">外层寿命包含内层寿命;内层必须先结束</div> |
|
||||||
<div class="flow"> |
|
||||||
<div class="flow-step layer-php"><strong>PHP / SAPI lifetime</strong><small>SINIT → module startup → module shutdown → SSHUTDOWN</small></div> |
|
||||||
<div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>TypePHP module lifetime</strong><small>MINIT → MSHUTDOWN;每个项目模块各一份</small></div> |
|
||||||
<div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-php"><strong>PHP request lifetime</strong><small>request startup → shutdown callbacks/destructors → request shutdown</small></div> |
|
||||||
<div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-project"><strong>Project request state</strong><small>RINIT/module_init → 执行 → RSHUTDOWN/module_clean</small></div> |
|
||||||
</div> |
|
||||||
</figure> |
|
||||||
</section> |
|
||||||
|
|
||||||
<section id="matrix"> |
|
||||||
<h2>二、模式总览</h2> |
|
||||||
<div class="table-wrap"> |
|
||||||
<table> |
|
||||||
<thead><tr><th>模式</th><th>PHP runtime 所有者</th><th>项目模块何时注册</th><th>项目 RINIT/RSHUTDOWN</th><th>入口</th><th>多模块结论</th></tr></thead> |
|
||||||
<tbody> |
|
||||||
<tr><td><code>ext</code></td><td>外部 PHP SAPI(CLI/FPM/Embed)</td><td>PHP module startup 期间</td><td>PHP 自动调度</td><td>外部 PHP 脚本调用编译符号</td><td><span class="status yes">支持多个</span>,但 PHP 符号不得冲突</td></tr> |
|
||||||
<tr><td><code>bin</code></td><td>生成的 C++ <code>main()</code></td><td><code>php_embed_init()</code> 完成后才注册</td><td>自身项目由 PHPX 手动补调</td><td>RINIT 中 <code>php::eval(... main())</code></td><td>可再加载多个常规 ext</td></tr> |
|
||||||
<tr><td><code>lib</code></td><td>宿主显式调用项目 runtime ABI</td><td>与 bin 相同,属于晚注册模块</td><td>自身项目由 PHPX 手动补调</td><td>宿主调用导出函数,无生成 main</td><td>可加载多个库;同进程只能有一个活动 Zend runtime</td></tr> |
|
||||||
<tr><td>WASI command</td><td>WASM <code>_start/main</code></td><td>与原生 bin 同构</td><td>手动补调自身项目</td><td>执行 TypePHP <code>main()</code></td><td>单实例、静态链接;无动态 ext</td></tr> |
|
||||||
<tr><td>WASI component library</td><td>WIT runtime resource</td><td><code>create-runtime</code> 时</td><td>resource 创建/析构负责</td><td><code>#[WasmExport]</code> 方法</td><td>每个 Component 实例仅一个活动 resource</td></tr> |
|
||||||
</tbody> |
|
||||||
</table> |
|
||||||
</div> |
|
||||||
</section> |
|
||||||
|
|
||||||
<section id="ext"> |
|
||||||
<h2>三、EXT:PHP 自动管理多个 TypePHP 模块</h2> |
|
||||||
<p>假设 PHP 同时加载 <code>typephp_app_a</code>、<code>typephp_app_b</code>。两个模块都在 PHP 收集 request handlers 之前注册,因此走标准 Zend 模块生命周期。</p> |
|
||||||
|
|
||||||
<figure class="flowchart" aria-label="多个 TypePHP 扩展启动流程图"> |
|
||||||
<div class="flow-title">进程启动与一次 request</div> |
|
||||||
<div class="phase"> |
|
||||||
<div class="phase-name">Module startup</div> |
|
||||||
<div class="phase-steps"> |
|
||||||
<div class="flow-step layer-php"><strong>PHP module startup</strong><small>读取 extension 配置,注册模块</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>A::MINIT</strong><small>注册 A 的类、函数、属性元数据</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>B::MINIT</strong><small>注册 B;共享 hook 采用计数</small></div> |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
<div class="phase"> |
|
||||||
<div class="phase-name">Request startup</div> |
|
||||||
<div class="phase-steps"> |
|
||||||
<div class="flow-step layer-php"><strong>php_request_startup</strong><small>zend_activate_modules()</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>A::RINIT</strong><small><code>php::request_init()</code> 首次真正初始化;A <code>module_init()</code></small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>B::RINIT</strong><small>PHPX init 幂等返回;B <code>module_init()</code></small></div> |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
<div class="phase"> |
|
||||||
<div class="phase-name">Request shutdown</div> |
|
||||||
<div class="phase-steps"> |
|
||||||
<div class="flow-step layer-php"><strong>shutdown callbacks</strong><small>先执行注册的 shutdown function 与对象析构</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>B::RSHUTDOWN</strong><small>首次 <code>php::request_shutdown()</code> 清共享 PHPX 状态;再清 B</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>A::RSHUTDOWN</strong><small>PHPX shutdown 幂等返回;再清 A</small></div> |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
<div class="phase"> |
|
||||||
<div class="phase-name">Module shutdown</div> |
|
||||||
<div class="phase-steps"> |
|
||||||
<div class="flow-step layer-typephp"><strong>B::MSHUTDOWN</strong><small>清 B persistent cache,共享 hook 计数减一</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>A::MSHUTDOWN</strong><small>最后一个模块恢复 Reflection handler、清 FiberGenerator 借用指针</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-php"><strong>PHP module shutdown</strong><small>Zend/SAPI 继续退出</small></div> |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
</figure> |
|
||||||
|
|
||||||
<h3>多 ext 时哪些状态共享,哪些隔离</h3> |
|
||||||
<div class="module-stack"> |
|
||||||
<div class="module-row"><span class="owner">App A</span><span class="shared">共享:同一 PHPX request、Native GC、Box 类型、Reflection/Fiber hook</span><span class="private">私有:A globals/cache</span></div> |
|
||||||
<div class="module-row"><span class="owner">App B</span><span class="shared">共享:同一 PHPX request、Native GC、Box 类型、Reflection/Fiber hook</span><span class="private">私有:B globals/cache</span></div> |
|
||||||
<div class="module-row"><span class="owner">App C</span><span class="shared">共享:同一 PHPX request、Native GC、Box 类型、Reflection/Fiber hook</span><span class="private">私有:C globals/cache</span></div> |
|
||||||
</div> |
|
||||||
<div class="callout ok"><strong>隔离保证:</strong>生成的 C++ 数据表位于 <code>typephp_<project></code> 命名空间,runtime/get-module 符号也带项目名。多个 ext 不再因为 PHPX misc 或通用表名发生原生符号冲突。</div> |
|
||||||
<div class="callout danger"><strong>仍然禁止:</strong>两个项目向 ZendVM 注册相同的 PHP namespace/class/function 组合。C++ 符号隔离不能消除 PHP function table/class table 的语义冲突。</div> |
|
||||||
</section> |
|
||||||
|
|
||||||
<section id="bin"> |
|
||||||
<h2>四、BIN:自身项目晚注册,额外 EXT 正常注册</h2> |
|
||||||
<p><code>php_embed_init()</code> 内部已经完成 SAPI startup、PHP module startup 和 PHP request startup。此后 bin 才取得自身的 <code>zend_module_entry</code> 并注册,所以自身项目不在 PHP 预先收集的 RINIT/RSHUTDOWN handler 列表中。</p> |
|
||||||
|
|
||||||
<figure class="flowchart" aria-label="Bin 加载额外 TypePHP 扩展的启动关闭流程图"> |
|
||||||
<div class="flow-title">bin owner + ext A + ext B</div> |
|
||||||
<div class="flow vertical"> |
|
||||||
<div class="flow-step layer-host"><strong>C++ main()</strong><small>调用 <code>typephp_<bin>_runtime_init(argc, argv)</code></small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-php"><strong>php_embed_init()</strong><small>SAPI/PHP 启动;ext A/B 自动完成 MINIT,然后在 request startup 自动完成 RINIT</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>注册 bin 自身项目模块</strong><small><code>zend_register_module_ex()</code> + <code>zend_startup_module_ex()</code> → bin::MINIT</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>手动 bin::RINIT</strong><small><code>php::request_init()</code>(通常已被 ext 初始化)→ 项目 <code>module_init()</code></small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-project"><strong>php::eval(... main())</strong><small>开始执行 TypePHP 项目;所有模块共享这一 Zend request</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-php"><strong>shutdown function + __destruct</strong><small>项目请求状态仍然存活,先让用户清理代码执行完</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>手动 bin::RSHUTDOWN</strong><small>PHPX request shutdown → bin 项目 module_clean</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>从 module_registry 删除 bin</strong><small>触发 bin::MSHUTDOWN;避免 Embed 退出时 persistent string 重复释放</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-php"><strong>php_embed_shutdown()</strong><small>PHP request shutdown → ext B/A RSHUTDOWN → ext B/A MSHUTDOWN → SAPI shutdown</small></div> |
|
||||||
</div> |
|
||||||
</figure> |
|
||||||
|
|
||||||
<div class="callout"><strong>为什么不能让 PHP 自动调用 bin 自身的 RSHUTDOWN?</strong>PHP 在 <code>php_embed_init()</code> 期间已经收集完 module handler 数组;bin 模块注册得更晚,不在数组里。不手动补调会遗漏项目全局变量、Native roots、请求数组模板和缓存清理。</div> |
|
||||||
</section> |
|
||||||
|
|
||||||
<section id="lib"> |
|
||||||
<h2>五、LIB:生命周期由宿主显式包围</h2> |
|
||||||
<p>lib 与 bin 使用同一份 PHPX Embed 实现,但定义 <code>TYPEPHP_NO_MAIN</code>,不会生成 C++ <code>main()</code>。宿主必须调用项目名隔离的 ABI。</p> |
|
||||||
<pre><code>typephp_demo_runtime_init(argc, argv); |
|
||||||
// 调用 demo 导出的 TypePHP 函数 |
|
||||||
typephp_demo_runtime_shutdown();</code></pre> |
|
||||||
|
|
||||||
<figure class="flowchart" aria-label="Library 生命周期流程图"> |
|
||||||
<div class="flow-title">单个活动 lib runtime</div> |
|
||||||
<div class="flow"> |
|
||||||
<div class="flow-step layer-host"><strong>Host</strong><small>dlopen/LoadLibrary 只装载代码</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>runtime_init</strong><small>真正启动 PHP Embed + 项目 MINIT/RINIT</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-project"><strong>Exports</strong><small>所有调用共享一个 PHP request</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>runtime_shutdown</strong><small>项目 RSHUTDOWN/MSHUTDOWN + PHP Embed shutdown</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-host"><strong>Unload</strong><small>运行时结束后才可卸载动态库</small></div> |
|
||||||
</div> |
|
||||||
</figure> |
|
||||||
|
|
||||||
<h3>同一宿主加载多个 TypePHP lib</h3> |
|
||||||
<div class="table-wrap"> |
|
||||||
<table> |
|
||||||
<thead><tr><th>操作</th><th>结论</th><th>原因</th></tr></thead> |
|
||||||
<tbody> |
|
||||||
<tr><td>只加载 A、B 的动态库,不调用 init</td><td><span class="status yes">可以</span></td><td>导出 runtime/get-module 符号带项目名;装载代码不等于启动 ZendVM。</td></tr> |
|
||||||
<tr><td><code>init(A) → use(A) → shutdown(A) → init(B)</code></td><td><span class="status conditional">可串行</span></td><td>前一 runtime 必须完整退出后,下一库才可重新启动进程级 PHP Embed 状态。</td></tr> |
|
||||||
<tr><td><code>init(A) → init(B)</code>,两个同时活动</td><td><span class="status no">不支持</span></td><td>ZendVM/Embed 是进程级状态;每个 lib 的局部 <code>runtime_started</code> 无法阻止另一 lib 再次调用 <code>php_embed_init()</code>。</td></tr> |
|
||||||
<tr><td>多个项目需要同时工作</td><td><span class="status conditional">一个 owner</span></td><td>使用一个 bin/lib 拥有 runtime,其余能力作为启动前加载的 ext,或合并为同一个 TypePHP 项目。</td></tr> |
|
||||||
</tbody> |
|
||||||
</table> |
|
||||||
</div> |
|
||||||
<div class="callout danger"><strong>关键约束:</strong>项目名隔离解决的是 C++ ABI 符号冲突,不会把一个进程切成多个 ZendVM。多个 native lib 的活动区间不得重叠;NTS 下也不得并发或重入调用同一 runtime。</div> |
|
||||||
</section> |
|
||||||
|
|
||||||
<section id="wasm"> |
|
||||||
<h2>六、WASM:相同内核,两种 Host 入口</h2> |
|
||||||
<p>WASI 使用静态链接的 PHP、PHPX 和 TypePHP 生成代码。浏览器与 Wasmtime 的 Host API 不同,但 PHP/ZendVM 生命周期相同。当前不支持在运行时动态加载 <code>.so/.dll</code> 形式的 TypePHP ext。</p> |
|
||||||
<div class="callout"><strong>两个维度不要混淆:</strong><code>mode: command/library</code> 决定生命周期入口(生成 main,或由 runtime resource 管理);<code>wasm: component/browser</code> 决定产物与 Host 适配方式。它们是正交配置,浏览器构建并不自动等于 library 模式。</div> |
|
||||||
|
|
||||||
<div class="grid two"> |
|
||||||
<article class="card"> |
|
||||||
<span class="tag" style="color: var(--typephp)">WASI command</span> |
|
||||||
<h3>由 <code>_start/main</code> 自动包围</h3> |
|
||||||
<div class="flow vertical"> |
|
||||||
<div class="flow-step layer-host"><strong>Wasmtime / WASI Host</strong><small>实例化 command 并调用 <code>_start</code></small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>runtime_init</strong><small>WASI Embed + 项目 MINIT/RINIT</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-project"><strong>main()</strong><small>与原生 bin 相同的入口语义</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>runtime_shutdown</strong><small>完整执行请求、模块与 SAPI 关闭</small></div> |
|
||||||
</div> |
|
||||||
</article> |
|
||||||
<article class="card"> |
|
||||||
<span class="tag" style="color: var(--phpx)">WASI component library</span> |
|
||||||
<h3>由 WIT resource 包围</h3> |
|
||||||
<div class="flow vertical"> |
|
||||||
<div class="flow-step layer-host"><strong>Instantiate component</strong><small>只建立 WASM 实例,尚未启动 ZendVM</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>create-runtime</strong><small>调用项目 <code>runtime_init</code>,返回 resource</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-project"><strong>#[WasmExport]</strong><small>多次调用共享同一 request;异常映射为 WIT result</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>drop/dispose resource</strong><small>调用项目 <code>runtime_shutdown</code></small></div> |
|
||||||
</div> |
|
||||||
</article> |
|
||||||
</div> |
|
||||||
|
|
||||||
<h3>WASM 多实例与多模块</h3> |
|
||||||
<ul> |
|
||||||
<li><strong>同一个 Component 实例:</strong>生成的 adapter 只允许一个活动 runtime resource,并拒绝同一 resource 上的并发或重入调用。</li> |
|
||||||
<li><strong>多个 Component 实例:</strong>各自拥有独立 linear memory 和 C/C++ 全局区,因此 ZendVM 状态互相隔离,可以由 Host 分别管理。</li> |
|
||||||
<li><strong>同一个 WASM 内的扩展:</strong>PHP 扩展必须在 SDK/最终链接阶段静态纳入,不能像原生 PHP 一样在运行时加载多个 TypePHP 动态扩展。</li> |
|
||||||
<li><strong>浏览器:</strong>应在 <code>finally</code> 中显式调用 <code>runtime[Symbol.dispose]()</code>;直接终止 Worker 只会回收整个实例,不保证 shutdown callback 被执行。</li> |
|
||||||
<li><strong>Wasmtime:</strong>command 返回时自动关闭;component library 应显式 drop resource。两者与浏览器使用相同的 TypePHP runtime ABI。</li> |
|
||||||
</ul> |
|
||||||
</section> |
|
||||||
|
|
||||||
<section id="shutdown"> |
|
||||||
<h2>七、为什么关闭顺序不能随意调整</h2> |
|
||||||
<figure class="flowchart" aria-label="TypePHP 关闭顺序与资源依赖流程图"> |
|
||||||
<div class="flow-title">从仍可执行用户代码,到最终释放 PHP 内存池</div> |
|
||||||
<div class="flow"> |
|
||||||
<div class="flow-step layer-php"><strong>shutdown functions</strong><small>允许访问项目 globals</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-php"><strong>__destruct()</strong><small>允许回调 TypePHP 方法</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-phpx"><strong>PHPX request shutdown</strong><small>Native GC finalizer 仍可访问项目状态</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-project"><strong>project module_clean</strong><small>释放 globals、数组模板和请求 cache</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-typephp"><strong>MSHUTDOWN</strong><small>清 persistent pointer cache / shared hook</small></div><div class="arrow">→</div> |
|
||||||
<div class="flow-step layer-php"><strong>PHP memory manager</strong><small>最后销毁 request arena 与 interned strings</small></div> |
|
||||||
</div> |
|
||||||
</figure> |
|
||||||
|
|
||||||
<ol> |
|
||||||
<li><strong>用户清理代码在前:</strong>shutdown callback 和对象析构可能继续访问 TypePHP 全局变量、调用动态函数,不能在它们之前销毁项目状态。</li> |
|
||||||
<li><strong>PHPX 在项目 clean 之前:</strong>Native finalizer 属于用户代码,可能访问项目 globals;因此生成的 RSHUTDOWN 当前是 <code>php::request_shutdown()</code> 后接 <code>module_clean()</code>。</li> |
|
||||||
<li><strong>请求对象在 PHP 内存池之前释放:</strong>若 PHP request arena 已销毁,栈上或全局的 PHPX wrapper 再析构会变成悬空指针访问。</li> |
|
||||||
<li><strong>Embed 晚注册模块要提前移出 registry:</strong>删除操作会通过 Zend 的 module destructor 执行项目 MSHUTDOWN,同时规避 Embed 对 persistent strings 的重复释放问题。</li> |
|
||||||
<li><strong>多 ext 共享 PHPX:</strong>第一个进入的 TypePHP RSHUTDOWN 完成共享 PHPX cleanup,后续调用幂等返回;每个项目自己的 <code>module_clean()</code> 仍各执行一次。</li> |
|
||||||
</ol> |
|
||||||
</section> |
|
||||||
|
|
||||||
<section id="names"> |
|
||||||
<h2>八、函数名称辨析</h2> |
|
||||||
<div class="table-wrap"> |
|
||||||
<table> |
|
||||||
<thead><tr><th>符号</th><th>定义层</th><th>寿命</th><th>职责</th></tr></thead> |
|
||||||
<tbody> |
|
||||||
<tr><td><code>php_embed_init/shutdown</code></td><td>PHP Embed SAPI</td><td>整个 runtime</td><td>启动/关闭 SAPI、PHP modules 和一个 PHP request。</td></tr> |
|
||||||
<tr><td><code>PHP_MINIT_FUNCTION(typephp_x)</code></td><td>TypePHP 生成模块</td><td>module</td><td>注册项目类/函数/属性元数据;安装共享 TypePHP handler。</td></tr> |
|
||||||
<tr><td><code>PHP_RINIT_FUNCTION(typephp_x)</code></td><td>TypePHP 生成模块</td><td>request</td><td>调用 PHPX request init,再初始化项目请求状态;bin 额外执行 main。</td></tr> |
|
||||||
<tr><td><code>php::request_init/shutdown</code></td><td>PHPX</td><td>每线程/每 request 共享</td><td>管理 Decimal、Native GC、Box 和动态调用缓存;对多模块调用幂等。</td></tr> |
|
||||||
<tr><td><code>static module_init()</code></td><td>生成的项目命名空间</td><td>request</td><td>初始化项目 globals、静态属性、数组常量、请求模板和 roots。</td></tr> |
|
||||||
<tr><td><code>static module_clean()</code></td><td>生成的项目命名空间</td><td>request</td><td>清项目 globals/cache,并将 user-code symbol pointer 表归零。</td></tr> |
|
||||||
<tr><td><code>module_init(zend_module_entry *)</code></td><td>PHPX Embed glue</td><td>module</td><td>调用 Zend API 注册晚到的 bin/lib 项目模块并触发其 MINIT。</td></tr> |
|
||||||
<tr><td><code>typephp_<project>_runtime_init/shutdown</code></td><td>TypePHP 稳定 ABI,PHPX 实现</td><td>runtime</td><td>bin main、native lib host、WIT adapter 共同使用的外层入口。</td></tr> |
|
||||||
<tr><td><code>php_<project>_embed_get_module</code></td><td>TypePHP 生成模块</td><td>module</td><td>返回该项目唯一的 <code>zend_module_entry</code>;项目名避免多模块 C++ 符号冲突。</td></tr> |
|
||||||
</tbody> |
|
||||||
</table> |
|
||||||
</div> |
|
||||||
</section> |
|
||||||
|
|
||||||
<section id="rules"> |
|
||||||
<h2>九、实现与维护规则</h2> |
|
||||||
<div class="grid two"> |
|
||||||
<article class="card"> |
|
||||||
<h3>必须保持</h3> |
|
||||||
<ul> |
|
||||||
<li>每个项目的 MINIT/MSHUTDOWN/RINIT/RSHUTDOWN 恰好各执行一次。</li> |
|
||||||
<li>所有可执行用户代码发生在项目请求状态和 PHP 内存池仍有效时。</li> |
|
||||||
<li>请求级指针在 RSHUTDOWN 清空;module persistent cache 在 MSHUTDOWN 清空。</li> |
|
||||||
<li>共享 hook 使用安装计数;项目私有表使用项目 namespace/static storage。</li> |
|
||||||
<li>WASM resource 必须显式释放,不依赖 Host GC 的最终时间。</li> |
|
||||||
</ul> |
|
||||||
</article> |
|
||||||
<article class="card"> |
|
||||||
<h3>禁止出现</h3> |
|
||||||
<ul> |
|
||||||
<li>在同一进程同时启动两个 native lib 所拥有的 PHP Embed runtime。</li> |
|
||||||
<li>在 <code>php_embed_shutdown()</code> 后析构持有 request zval 的 C++ 对象。</li> |
|
||||||
<li>让晚注册的 bin/lib 项目依赖 PHP 自动 RINIT/RSHUTDOWN。</li> |
|
||||||
<li>在多 ext 中使用未加项目名的全局 C++ 数据表或 runtime ABI。</li> |
|
||||||
<li>误把项目 <code>module_init()</code> 当作 Zend MINIT,或在 MINIT 中创建请求 zval。</li> |
|
||||||
</ul> |
|
||||||
</article> |
|
||||||
</div> |
|
||||||
|
|
||||||
<h3>排查顺序</h3> |
|
||||||
<ol> |
|
||||||
<li>先确认当前是 <code>bin</code>、<code>ext</code>、<code>lib</code>、WASI command 还是 component library。</li> |
|
||||||
<li>确认谁拥有 PHP runtime:外部 SAPI、生成 main、native host,还是 WIT resource。</li> |
|
||||||
<li>确认目标模块是启动前注册还是 <code>php_embed_init()</code> 后晚注册。</li> |
|
||||||
<li>为每个项目分别记录 MINIT/RINIT/RSHUTDOWN/MSHUTDOWN 次数,不要只看共享 PHPX 标志。</li> |
|
||||||
<li>崩溃若发生在 PHP memory manager shutdown,检查是否仍有项目 global、Closure、Object 或 PHPX wrapper 晚析构。</li> |
|
||||||
</ol> |
|
||||||
</section> |
|
||||||
|
|
||||||
<section id="sources"> |
|
||||||
<h2>十、代码依据</h2> |
|
||||||
<ul class="source-list"> |
|
||||||
<li><code>compiler/src/Translator.php::genExtension()</code>:生成项目 module entry、MINIT/MSHUTDOWN/RINIT/RSHUTDOWN、<code>module_init/module_clean</code>。</li> |
|
||||||
<li><code>phpx/src/misc/typephp_main.cc</code>:原生 bin/lib 与 WASI 共用的 Embed runtime owner、晚注册模块和 shutdown 顺序。</li> |
|
||||||
<li><code>phpx/src/core/base.cc::php::request_init/request_shutdown</code>:PHPX 请求级共享状态。</li> |
|
||||||
<li><code>phpx/include/typephp_runtime.h</code>:项目名隔离的 runtime/get-module ABI 宏。</li> |
|
||||||
<li><code>compiler/src/Build/WasmInterfaceGenerator.php</code>:WIT runtime resource、单实例约束、异常边界和 resource destructor。</li> |
|
||||||
<li><code>projects/php-8.5.9/sapi/embed/php_embed.c</code>:<code>php_embed_init/shutdown</code> 内部的 SAPI/module/request 调度。</li> |
|
||||||
<li><code>projects/php-8.5.9/main/main.c::php_request_shutdown()</code>:shutdown function、析构、RSHUTDOWN、内存池销毁的标准顺序。</li> |
|
||||||
</ul> |
|
||||||
</section> |
|
||||||
|
|
||||||
<footer>此页面是内部实现文档。流程变化时,应同时更新对应代码注释、生命周期测试和本图。</footer> |
|
||||||
</main> |
|
||||||
</body> |
|
||||||
</html> |
|
||||||
@ -1,401 +0,0 @@ |
|||||||
# TypePHP 作用域管理设计 |
|
||||||
|
|
||||||
本文是 TypePHP 与 PHPX 的内部实现文档,说明当前三种作用域管理器的职责、实现方式、生命周期、性能特征和适用场景。这里的“作用域”并不是同一个 Zend 概念:callable 解析、执行帧类作用域和 `EG(fake_scope)` 分别服务于不同子系统,不能相互替代。 |
|
||||||
|
|
||||||
## 1. 设计目标 |
|
||||||
|
|
||||||
TypePHP 生成的 C++ 方法并不是普通 Zend user function。动态调用回到 ZendVM 时,Zend 仍然需要以下信息才能复现 PHP 的可见性规则: |
|
||||||
|
|
||||||
- 声明方法的词法作用域,用于判断 private/protected 成员是否可访问; |
|
||||||
- 当前 late static binding 的 called scope; |
|
||||||
- 当前实例 `$this`,用于解析非静态方法 callable; |
|
||||||
- 某些 Zend 属性、对象和异常 API 所读取的 `EG(fake_scope)`。 |
|
||||||
|
|
||||||
Scope 设计遵循以下原则: |
|
||||||
|
|
||||||
1. 优先显式传递作用域,不修改 Zend 的全局或真实执行帧状态。 |
|
||||||
2. 一个 AOT 方法调用期间只创建一次可复用的 callable context,循环中的多次调用共享它。 |
|
||||||
3. 只有编译器无法确定 callback 位置时,才临时修改最近的 user-code frame。 |
|
||||||
4. 修改 Zend executor 状态时必须使用 RAII,并保证异常路径恢复。 |
|
||||||
5. 不为纯 Native Call 或 public、绝对定位的 callback 支付额外包装成本。 |
|
||||||
|
|
||||||
## 2. 总览 |
|
||||||
|
|
||||||
| 管理器 | 管理的状态 | 主要用途 | 是否修改 Zend 当前状态 | |
|
||||||
| --- | --- | --- | --- | |
|
||||||
| `php::CallableScope` | synthetic `zend_execute_data`,包含 lexical scope、called scope 和 `$this` | 动态方法调用、first-class callable、内置函数 callback | 否 | |
|
||||||
| `php::UserCodeScopeGuard` | 最近 user-code frame 的 `zend_function::common.scope` | `call_user_func*` 及 callback 隐藏在参数展开中的动态调用路径 | 是,析构时恢复 | |
|
||||||
| `php::FakeScopeGuard` | `EG(fake_scope)` | Zend 属性、对象、异常等读取 fake scope 的 API | 是,析构或显式 `restore()` 时恢复 | |
|
||||||
|
|
||||||
选择规则可以简化为: |
|
||||||
|
|
||||||
- 能拿到明确 callable 值:使用 `CallableScope`。 |
|
||||||
- 调用 `call_user_func*`,或其他内置函数的 callback 藏在 `...$args` 中:使用 `UserCodeScopeGuard`。 |
|
||||||
- 调用的 Zend API 明确读取 `EG(fake_scope)`:使用 `FakeScopeGuard`。 |
|
||||||
- 纯 native 调用或不依赖调用者可见性的操作:不创建任何 Scope 管理器。 |
|
||||||
|
|
||||||
## 3. `php::CallableScope` |
|
||||||
|
|
||||||
### 3.1 职责 |
|
||||||
|
|
||||||
`CallableScope` 是当前普通 callable 解析的主路径。它将调用者上下文显式交给 `zend_is_callable_at_frame()`,用于: |
|
||||||
|
|
||||||
- 解析 private/protected 方法; |
|
||||||
- 解析 `self`、`parent`、`static` callback; |
|
||||||
- 保留 late static binding 的 called scope; |
|
||||||
- 为非静态方法提供真实 `$this`; |
|
||||||
- 在不修改 `EG(current_execute_data)` 和真实执行帧的前提下调用动态方法。 |
|
||||||
|
|
||||||
它不负责属性访问,也不会设置 `EG(fake_scope)`。 |
|
||||||
|
|
||||||
### 3.2 内部结构 |
|
||||||
|
|
||||||
类定义在 PHPX 的 `include/phpx.h` 中,持有: |
|
||||||
|
|
||||||
```cpp |
|
||||||
zend_function *caller_function_; |
|
||||||
zend_class_entry *called_scope_; |
|
||||||
zend_object *this_object_; |
|
||||||
mutable zend_execute_data frame_{}; |
|
||||||
``` |
|
||||||
|
|
||||||
构造时通过 `zend_vm_init_call_frame()` 初始化一个 synthetic frame: |
|
||||||
|
|
||||||
- `caller_function_->common.scope` 是 lexical scope,即声明当前方法的类; |
|
||||||
- `called_scope_` 是运行时 called scope; |
|
||||||
- 实例调用设置 `ZEND_CALL_HAS_THIS` 并携带真实 `zend_object *`; |
|
||||||
- 静态调用不携带对象,只传 called scope; |
|
||||||
- 若 called scope 为空,则回退到 lexical scope。 |
|
||||||
|
|
||||||
解析时调用: |
|
||||||
|
|
||||||
```cpp |
|
||||||
zend_is_callable_at_frame(callable, object, &frame_, 0, cache, error); |
|
||||||
``` |
|
||||||
|
|
||||||
synthetic frame 不会安装到 `EG(current_execute_data)`,因此不会污染当前 Zend 调用栈,也不需要在退出时恢复全局状态。 |
|
||||||
|
|
||||||
### 3.3 生命周期与所有权 |
|
||||||
|
|
||||||
`CallableScope` 不拥有 `zend_function`、`zend_class_entry` 或 `zend_object`,只在当前 AOT 方法栈帧内借用这些指针: |
|
||||||
|
|
||||||
- TypePHP 编译方法使用 persistent `zend_function`,其生命周期覆盖请求调用; |
|
||||||
- Closure 的 `zend_function *` 在 Closure 对象存活期间有效; |
|
||||||
- `$this` 在当前方法执行期间有效; |
|
||||||
- `CallableScope` 不可复制、不可移动,防止 synthetic frame 被意外转移或跨生命周期保存。 |
|
||||||
|
|
||||||
不得把 `CallableScope` 缓存到请求之外,也不得让它比所属方法或 Closure 活得更久。 |
|
||||||
|
|
||||||
### 3.4 编译器生成方式 |
|
||||||
|
|
||||||
编译器通过 `FunctionContext::$callableScopeVar` 延迟申请 Scope 变量。第一次需要显式 callable scope 时,`getCallableScopeExpr()` 分配临时变量;随后 `genScopeVarDecl()` 将初始化代码提升到函数入口: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php::CallableScope tmp_var_1 = php::getCallableScope( |
|
||||||
get_persistent_method(...), |
|
||||||
this_ |
|
||||||
); |
|
||||||
``` |
|
||||||
|
|
||||||
`php::getCallableScope()` 根据 `this_` 同时构建 called scope 和真实实例信息。一个方法内所有 scoped call 都引用同一个 `tmp_var_1`,因此循环中的重复调用不会重复创建 synthetic frame。 |
|
||||||
|
|
||||||
如果方法从未使用 scoped dynamic call、first-class callable 或 scoped callback,编译器不会生成该变量。 |
|
||||||
|
|
||||||
### 3.5 使用入口 |
|
||||||
|
|
||||||
#### `php::callScoped()` |
|
||||||
|
|
||||||
用于动态函数或对象方法调用。内部 `call_function_impl()` 使用 `CallableScope::resolve()` 获取 `zend_fcall_info_cache`,然后执行 `zend_call_function()`。 |
|
||||||
|
|
||||||
典型场景是编译器无法将对象方法解析为 Native Call,但仍需保留当前类的 private/protected 访问权。 |
|
||||||
|
|
||||||
#### `php::makeScopedCallable()` |
|
||||||
|
|
||||||
用于 first-class callable 语法。此语法的结果必须是一个真正的 `Closure`,所以即使目标方法是 public,也不能只返回原始 callback 数组或字符串。 |
|
||||||
|
|
||||||
```php |
|
||||||
$callback = self::privateMethod(...); |
|
||||||
$callback = $this->publicMethod(...); |
|
||||||
``` |
|
||||||
|
|
||||||
普通方法通过 `zend_create_fake_closure()` 创建 Closure。若 Zend 返回 `ZEND_ACC_CALL_VIA_TRAMPOLINE`,则使用转发 Closure 保留 magic `__call()` / `__callStatic()` 的动态语义。 |
|
||||||
|
|
||||||
#### `php::prepareScopedCallback()` |
|
||||||
|
|
||||||
用于向 `array_map()`、`usort()` 等 PHP 内置函数传递 callback。这里的目标只是让内置函数正确调用 callback,不要求参数本身变成 Closure。 |
|
||||||
|
|
||||||
因此它会优先复用以下 callback 的原始值: |
|
||||||
|
|
||||||
- public 方法; |
|
||||||
- 使用绝对类名定位; |
|
||||||
- 不依赖 trampoline。 |
|
||||||
|
|
||||||
只有 private/protected 方法、`self` / `parent` / `static` 相对 callback 或 trampoline 才创建 Closure。这避免了循环中每次调用内置函数都无条件分配 fake Closure。 |
|
||||||
|
|
||||||
### 3.6 为什么仍要运行时识别 `self` / `parent` / `static` |
|
||||||
|
|
||||||
直接语法中的 `self::class` 可以在编译期展开为具体类名,但 PHP callback 也允许动态值: |
|
||||||
|
|
||||||
```php |
|
||||||
$class = 'self'; |
|
||||||
$callback = [$class, 'method']; |
|
||||||
``` |
|
||||||
|
|
||||||
此时只有运行时才能知道数组中的类名是否为相对类名。因此 `isRelativeCallableClass()` 不能完全移到编译期。对于已知的绝对 public callback,该检查会很快返回 false,并复用原值。 |
|
||||||
|
|
||||||
## 4. `php::UserCodeScopeGuard` |
|
||||||
|
|
||||||
### 4.1 职责与适用范围 |
|
||||||
|
|
||||||
`UserCodeScopeGuard` 服务于完全动态的 `call_user_func()` / `call_user_func_array()`、callback map,以及编译器无法静态改写 callback 的参数展开场景。 |
|
||||||
|
|
||||||
```php |
|
||||||
$args = [[$this, 'privateMethod'], 1]; |
|
||||||
call_user_func(...$args); |
|
||||||
``` |
|
||||||
|
|
||||||
内置函数 callback 可能位于固定位置、倒数位置、命名参数中,甚至一个函数有多个 callback。执行 `...$args` 展开前,编译器并不知道最终的 positional/named 参数布局,无法只对对应值调用 `prepareScopedCallback()`。 |
|
||||||
|
|
||||||
`call_user_func*` 本身就是 ZendVM 的完全动态调用边界,无论 callback 是否显式出现,都不创建 fake Closure。如果 callable 数组使用 `self`、`parent` 或 `static`,则先由 `normalizeCallableClass()` 将 class 部分转换为真实类名: |
|
||||||
|
|
||||||
- `self` 转为 `CallableScope::lexicalScope()`; |
|
||||||
- `parent` 转为 lexical scope 的父类; |
|
||||||
- `static` 转为 `CallableScope::calledScope()`。 |
|
||||||
|
|
||||||
规范化只复制需要修改的 callback 数组。绝对类名、对象 callback、Closure 和普通函数名保持原值。 |
|
||||||
|
|
||||||
`preg_replace_callback_array()` 是 callback map 的特例。Zend 会在函数内部逐项解析 map 中的 callback;若提前包装 map,则每次调用都要执行 O(N) 扫描,并可能触发数组 COW 和多个 Closure 分配。因此编译器保留原始 map,在方法入口创建一次 `UserCodeScopeGuard`,让 Zend 直接按正确作用域解析。 |
|
||||||
|
|
||||||
除完全动态调用、callback map 和参数展开外,普通 callback 参数不得使用此 guard;只要单个 callback 的 AST 参数位置已知,就应使用 `CallableScope` 路径。 |
|
||||||
|
|
||||||
### 4.2 实现方式 |
|
||||||
|
|
||||||
构造函数从 `EG(current_execute_data)` 开始向上查找最近的 user-code frame,并跳过 internal frame: |
|
||||||
|
|
||||||
```cpp |
|
||||||
while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) { |
|
||||||
frame = frame->prev_execute_data; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
找到后保存,并使用 `CallableScope::lexicalScope()` 设置可见性作用域: |
|
||||||
|
|
||||||
```cpp |
|
||||||
function_ = frame->func; |
|
||||||
previous_scope_ = function_->common.scope; |
|
||||||
function_->common.scope = callable_scope.lexicalScope(); |
|
||||||
``` |
|
||||||
|
|
||||||
析构函数恢复 `previous_scope_`。类不可复制、不可移动,保证一次构造对应一次恢复。如果没有可用的 user-code frame,会抛出: |
|
||||||
|
|
||||||
```text |
|
||||||
A user-code frame is required for scoped dynamic callback calls |
|
||||||
``` |
|
||||||
|
|
||||||
该 guard 操作的是从当前请求执行链找到的 user-code frame,不是 TypePHP 注册在 MINIT 的 persistent internal method。`EG(current_execute_data)` 本身属于当前 executor 上下文。其影响窗口被限制在当前 AOT 方法调用的 RAII 生命周期内。 |
|
||||||
|
|
||||||
### 4.3 编译器生成方式 |
|
||||||
|
|
||||||
编译器维护语义明确的标记: |
|
||||||
|
|
||||||
```php |
|
||||||
FunctionContext::$needsUserCodeCallableScope |
|
||||||
``` |
|
||||||
|
|
||||||
当编译器遇到 `call_user_func*` 的动态 callback,或一个已知会同步调用 callback 的内置函数存在无法匹配的参数展开时,`markUserCodeCallableScope()` 设置该标记。状态属于当前 `FunctionContext`,因此普通方法、嵌套 Closure 和 Fiber 各自独立,不会把 guard 错误泄漏到外层函数。每个函数体入口只生成一个: |
|
||||||
|
|
||||||
```cpp |
|
||||||
php::CallableScope tmp_var_1 = php::getCallableScope(..., this_); |
|
||||||
php::UserCodeScopeGuard tmp_var_2{tmp_var_1}; |
|
||||||
``` |
|
||||||
|
|
||||||
即使调用形态是 `call_user_func($closure)`,且 Closure 内部再次通过 |
|
||||||
`call_user_func(['self', 'method'])` 调用,每一层也只读取自己的 |
|
||||||
`FunctionContext`、lexical scope 和 `$this`,不能复用或污染外层 guard。 |
|
||||||
|
|
||||||
它不是按 call site 或循环迭代创建的。没有上述动态 callback 的方法不会产生此成本。 |
|
||||||
|
|
||||||
### 4.4 为什么当前保留该兜底 |
|
||||||
|
|
||||||
若完全移除它,编译器必须在参数展开完成后增加一套结构化参数绑定和改写流程,正确处理: |
|
||||||
|
|
||||||
- positional 与 named 参数合并; |
|
||||||
- callback 的正向和倒数位置; |
|
||||||
- 一个函数的多个 callback; |
|
||||||
- callback map; |
|
||||||
- unpack 中重复、缺失或覆盖参数时的 PHP 错误语义。 |
|
||||||
|
|
||||||
这不是一个局部替换,而是对 `parseCallArgs()` 和参数容器生成流程的中等规模重构。在完成统一的运行时参数后处理机制前,保留范围严格受控的 `UserCodeScopeGuard` 更简单可靠。 |
|
||||||
|
|
||||||
## 5. `php::FakeScopeGuard` |
|
||||||
|
|
||||||
### 5.1 职责 |
|
||||||
|
|
||||||
`FakeScopeGuard` 是 `EG(fake_scope)` 的 RAII 包装。部分 Zend API 不接受显式调用 frame,而是直接读取 `EG(fake_scope)` 来判断类成员可见性或执行类作用域相关操作。只有这些 API 才应使用它。 |
|
||||||
|
|
||||||
当前典型场景包括: |
|
||||||
|
|
||||||
- 动态属性读取、写入和属性 hook; |
|
||||||
- Zend object handler 调用; |
|
||||||
- 类作用域下的默认值或对象初始化; |
|
||||||
- 异常对象相关的 Zend 操作; |
|
||||||
- 其他明确读取 `EG(fake_scope)` 的 Zend 内部接口。 |
|
||||||
|
|
||||||
TypePHP 的属性访问生成器会通过 `FakeScopeGuard::current()` 将当前 fake scope 传给 PHPX 属性 helper。 |
|
||||||
|
|
||||||
### 5.2 实现方式 |
|
||||||
|
|
||||||
构造时保存旧值并设置新值,析构时恢复: |
|
||||||
|
|
||||||
```cpp |
|
||||||
explicit FakeScopeGuard(Scope scope) noexcept : previous_(current()) { |
|
||||||
EG(fake_scope) = scope; |
|
||||||
} |
|
||||||
|
|
||||||
~FakeScopeGuard() noexcept { |
|
||||||
restore(); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
`Scope` 通过 `decltype(EG(fake_scope))` 推导,以同时兼容 PHP 8.4 的可变指针和 PHP 8.5 的 pointer-to-const。`restore()` 是幂等操作,可以安全地提前调用一次。 |
|
||||||
|
|
||||||
### 5.3 Zend bailout 注意事项 |
|
||||||
|
|
||||||
C++ 异常展开会执行析构函数,但 Zend bailout 使用 `longjmp`,不会执行 C++ 析构函数。如果 guard 的生命周期跨越 bailout 边界,必须在对应的 `zend_catch` 路径中显式调用: |
|
||||||
|
|
||||||
```cpp |
|
||||||
fake_scope_guard.restore(); |
|
||||||
``` |
|
||||||
|
|
||||||
然后再继续 bailout 或转换异常。仅依赖析构函数处理 bailout 是错误的。 |
|
||||||
|
|
||||||
### 5.4 不适用场景 |
|
||||||
|
|
||||||
`FakeScopeGuard` 不能替代 `CallableScope`: |
|
||||||
|
|
||||||
- 它没有 synthetic frame; |
|
||||||
- 它不能携带 `$this`; |
|
||||||
- 它不能完整表达 lexical scope 与 called scope; |
|
||||||
- `zend_is_callable_at_frame()` 的解析语义不应通过全局 fake scope 间接模拟。 |
|
||||||
|
|
||||||
同样,不能为了“可能需要访问 private”而在整个 AOT 方法入口无条件设置 `EG(fake_scope)`。这会扩大全局状态的影响范围,并让无关的 native 密集调用承担成本。 |
|
||||||
|
|
||||||
## 6. 三种 Scope 的调用流程 |
|
||||||
|
|
||||||
### 6.1 已知动态方法调用 |
|
||||||
|
|
||||||
```text |
|
||||||
AOT method entry |
|
||||||
-> lazily generated CallableScope |
|
||||||
-> php::callScoped() |
|
||||||
-> CallableScope::resolve() |
|
||||||
-> zend_is_callable_at_frame(synthetic frame) |
|
||||||
-> zend_call_function() |
|
||||||
``` |
|
||||||
|
|
||||||
整个过程不修改真实 Zend frame。 |
|
||||||
|
|
||||||
### 6.2 已知内置函数 callback |
|
||||||
|
|
||||||
```text |
|
||||||
compiler marks callback argument |
|
||||||
-> prepareScopedCallback(value, CallableScope) |
|
||||||
-> public absolute callback: reuse value |
|
||||||
-> scoped/trampoline callback: create Closure |
|
||||||
-> call PHP internal function |
|
||||||
``` |
|
||||||
|
|
||||||
first-class callable 使用同一解析基础,但必须调用 `makeScopedCallable()` 并返回 Closure。 |
|
||||||
|
|
||||||
### 6.3 参数展开中的 callback |
|
||||||
|
|
||||||
```text |
|
||||||
AOT method entry |
|
||||||
-> UserCodeScopeGuard changes nearest user-code frame scope |
|
||||||
-> internal function receives expanded arguments |
|
||||||
-> Zend resolves hidden callback using that frame scope |
|
||||||
-> method exit / C++ exception unwind |
|
||||||
-> guard restores original scope |
|
||||||
``` |
|
||||||
|
|
||||||
### 6.4 属性或对象 handler |
|
||||||
|
|
||||||
```text |
|
||||||
save EG(fake_scope) |
|
||||||
-> install FakeScopeGuard |
|
||||||
-> call Zend property/object API |
|
||||||
-> restore on normal/C++ exception exit |
|
||||||
-> explicitly restore in zend_catch if bailout is possible |
|
||||||
``` |
|
||||||
|
|
||||||
## 7. 禁止混用与维护约束 |
|
||||||
|
|
||||||
1. 不要用 `FakeScopeGuard` 解析 callable。 |
|
||||||
2. 不要为普通已知 callback 修改真实 user-code frame;使用 `prepareScopedCallback()`。 |
|
||||||
3. 不要让 `UserCodeScopeGuard` 重新变成所有动态调用的通用入口。 |
|
||||||
4. 不要在循环中的 call site 重建 `CallableScope`;应由 `FunctionContext` 提升到方法入口并复用。 |
|
||||||
5. 不要缓存 `CallableScope` 借用的函数、对象或 synthetic frame 到请求之外。 |
|
||||||
6. 不要把 first-class callable 改为返回原始 callback;其 PHP 结果类型必须是 Closure。 |
|
||||||
7. 新增会同步调用 callback 的 PHP 内置函数时,需要更新 callback 参数描述表,注明位置、参数名以及是否为 callback map。 |
|
||||||
8. 保存 callback 但不立即调用的函数不能仅因接收 callable 就标记 scope fallback,例如 `spl_autoload_register()`。 |
|
||||||
9. 新增跨 Zend bailout 的 `FakeScopeGuard` 用法时,代码审查必须检查 `zend_catch` 是否显式恢复。 |
|
||||||
|
|
||||||
## 8. 性能模型 |
|
||||||
|
|
||||||
| 路径 | 主要成本 | 优化策略 | |
|
||||||
| --- | --- | --- | |
|
||||||
| `CallableScope` | 初始化一个 synthetic frame | 每个 AOT 方法最多一次,循环复用 | |
|
||||||
| `callScoped()` | `zend_is_callable_at_frame()` 动态解析 | 仅动态调用使用;可解析的 Native Call 不进入此路径 | |
|
||||||
| `prepareScopedCallback()` | 一次 callable 解析 | public 绝对 callback 不创建 Closure | |
|
||||||
| `makeScopedCallable()` | callable 解析及 Closure 分配 | 仅 first-class callable 使用 | |
|
||||||
| `UserCodeScopeGuard` | 方法入口一次指针查找、写入和退出恢复 | 只为 `call_user_func*`、callback map 或未解析的 unpack callback 生成 | |
|
||||||
| `FakeScopeGuard` | 两次 executor-global 指针赋值 | 仅包围确实读取 fake scope 的 Zend API | |
|
||||||
|
|
||||||
这套设计刻意让常见的纯 Native Call、无 callback 方法和 public callback 保持最短路径。不要为了统一表面形式而把低频 fallback 下沉到所有调用中。 |
|
||||||
|
|
||||||
## 9. 测试要求 |
|
||||||
|
|
||||||
Scope 修改至少应覆盖以下层次: |
|
||||||
|
|
||||||
- PHPX 单测:`FakeScopeGuard` 保存、嵌套、恢复和提前 `restore()`; |
|
||||||
- 编译器结构测试:一个方法只生成一个 `php::getCallableScope()`,多处调用复用同一变量; |
|
||||||
- PHPT:private/protected callback、非静态 `self::method(...)`、public callback; |
|
||||||
- PHPT:callback map 中 public 与 scoped callback 混合; |
|
||||||
- PHPT:`...$args` 中 private callback 可调用,异常退出后 scope 已恢复; |
|
||||||
- PHPT:Closure、Fiber、普通方法中的作用域生成路径; |
|
||||||
- 回归测试:纯 Native Call 不应生成额外 Scope guard。 |
|
||||||
|
|
||||||
当前相关测试包括: |
|
||||||
|
|
||||||
- `phpunit/src/ScopedCallContextTest.php` |
|
||||||
- `phpunit/code/scoped-call-context-reuse.php` |
|
||||||
- `tests/compiler/place-holder/non-static-self.phpt` |
|
||||||
- `tests/compiler/callable/scoped-internal-callbacks.phpt` |
|
||||||
- `tests/compiler/callable/unpacked-callback-scope-restored.phpt` |
|
||||||
- PHPX `tests/src/scope_guard.cpp` |
|
||||||
|
|
||||||
涉及动态调用抛出异常的 PHPT 可能触发已知 ZendVM 内存泄漏报告;只有确认泄漏来自 Zend 动态调用异常路径时,测试才可局部设置 `USE_ZEND_ALLOC=0`,不能全局关闭内存检查。 |
|
||||||
|
|
||||||
## 10. 代码位置索引 |
|
||||||
|
|
||||||
| 内容 | 位置 | |
|
||||||
| --- | --- | |
|
||||||
| `CallableScope` 及 public helper 声明 | `vendor/swoole/phpx/include/phpx.h` | |
|
||||||
| callable 解析与包装 | `vendor/swoole/phpx/src/core/base.cc`、`vendor/swoole/phpx/src/core/closure.cc` | |
|
||||||
| `FakeScopeGuard` | `vendor/swoole/phpx/include/phpx_fake_scope_guard.h` | |
|
||||||
| `UserCodeScopeGuard` | `vendor/swoole/phpx/include/typephp_helper.h`、`src/core/scope.cc` | |
|
||||||
| `php::getCallableScope()` | `vendor/swoole/phpx/include/typephp_helper.h` | |
|
||||||
| callback 标记和 Scope 变量生成 | `src/CompilerBase.php` | |
|
||||||
| callback 参数包装 | `src/Generator/CallArgumentGenerator.php` | |
|
||||||
| Closure/Fiber fallback guard | `src/Generator/ClosureGenerator.php`、`FiberGenerator.php` | |
|
||||||
| 方法 fallback guard | `src/Translator.php` | |
|
||||||
| Scope 状态 | `src/Context/FunctionContext.php` | |
|
||||||
| 属性访问中的 fake scope | `src/Parser/PropertyAccessTrait.php` | |
|
||||||
|
|
||||||
## 11. 后续演进原则 |
|
||||||
|
|
||||||
`UserCodeScopeGuard` 是复杂动态调用的长期保留机制,不以删除为目标。它修改的是当前线程、当前请求中的 user-code frame,并通过 RAII 恢复;ZTS 下不同线程拥有各自的执行上下文,因此不会共享被修改的 frame 状态。 |
|
||||||
|
|
||||||
`CallableScope` 用于编译器能够确定 callback 位置与调用边界的单一场景,以减少 frame 修改和 Closure 包装;它是一条更快、更明确的路径,而不是要求覆盖 unpack、多层动态 callback 等所有场景。遇到难以静态证明安全的组合时,应优先保留 `UserCodeScopeGuard`,不要为了形式上的统一强行改写为 `CallableScope`。 |
|
||||||
|
|
||||||
未来新增 Scope 抽象前,应先确认 Zend API 依赖的是 synthetic call frame、真实 user-code frame,还是 `EG(fake_scope)`。名称和类型应直接表达所管理的 Zend 状态,避免再次出现一个含义过宽的通用 `Scope` 类。 |
|
||||||
@ -1,48 +0,0 @@ |
|||||||
# 测试覆盖清单 |
|
||||||
|
|
||||||
`bin/analyze-test-coverage.php` 从 PHPT 和编译器 PHPUnit fixture 的源码生成覆盖清单。它是静态测试意图分析工具,不替代测试执行。 |
|
||||||
|
|
||||||
## 使用 |
|
||||||
|
|
||||||
```bash |
|
||||||
# 终端摘要 |
|
||||||
php bin/analyze-test-coverage.php |
|
||||||
|
|
||||||
# 可审阅的完整矩阵 |
|
||||||
php bin/analyze-test-coverage.php \ |
|
||||||
--format=markdown \ |
|
||||||
--output=build/test-coverage.md |
|
||||||
|
|
||||||
# 供 CI 或其他工具读取 |
|
||||||
php bin/analyze-test-coverage.php \ |
|
||||||
--format=json \ |
|
||||||
--output=build/test-coverage.json \ |
|
||||||
--strict |
|
||||||
``` |
|
||||||
|
|
||||||
默认扫描 `tests/compiler`、`phpunit/src` 和 `phpunit/code`。也可以在命令末尾传入一个或多个 PHPT 文件或目录;`--no-phpunit` 只分析 PHPT,`--php-versions=8.4,8.5` 设置矩阵的 PHP 版本列。 |
|
||||||
|
|
||||||
`--strict` 在存在非预期的源码解析失败或无法解析的 PHPUnit fixture 引用时返回非零状态。负向数据提供器中故意不能被 php-parser 接受的样本会单独记入 `expected_parser_diagnostics`,不会伪装成工具故障。 |
|
||||||
|
|
||||||
## 三类覆盖证据 |
|
||||||
|
|
||||||
每个适用的 `PHP 版本 × 特性` 行分别记录: |
|
||||||
|
|
||||||
- `positive_compile`:有效 PHPT,或正向 PHPUnit 编译 fixture; |
|
||||||
- `runtime_semantics`:含 `EXPECT`、`EXPECTF` 或 `EXPECTREGEX` 的有效 PHPT; |
|
||||||
- `negative_diagnostic`:期待诊断的 PHPT,或明确期待失败的 PHPUnit 测试/数据提供器。 |
|
||||||
|
|
||||||
`XFAIL` 和无条件 `SKIPIF` 不计入任何证据轴。PHP 版本范围从测试标题、`SKIPIF` 中的 `PHP_VERSION_ID` 条件以及 PHPUnit 数据行中的版本字符串推断。 |
|
||||||
|
|
||||||
## 分母 |
|
||||||
|
|
||||||
报告只给出带明确分母的比率: |
|
||||||
|
|
||||||
- AST 节点覆盖分母:当前安装的 `nikic/php-parser` 所提供的具体 AST 节点种类;用于错误恢复的 `Expr_Error` 不计入。 |
|
||||||
- 特性轴覆盖分母:特性目录中 `introduced <= 目标 PHP 版本` 的行数。每个正向编译、运行语义和负向诊断轴独立计算。 |
|
||||||
|
|
||||||
工具不会把不同含义的三个轴合成一个“项目总覆盖率”。完整 JSON 同时保留特性目录、逐项证据来源、矩阵、AST 节点出现次数、解析问题和排除原因,便于 CI 进一步检查。 |
|
||||||
|
|
||||||
## 分类边界 |
|
||||||
|
|
||||||
AST 节点由 parser 自动提取。无法只靠节点区分的语义特性(例如 DNF 出现位置、属性 hook 变体、`exit(message: ...)`)由分析器中的显式特性目录补充。新增语言特性时应同时登记其引入版本和检测规则,以维持版本矩阵的明确分母。 |
|
||||||
@ -1,462 +0,0 @@ |
|||||||
# TypePHP WASM 技术方案与实施计划 |
|
||||||
|
|
||||||
> 状态:WASI 0.2 Component 与 Chrome Worker 原型已实现 |
|
||||||
> 调研日期:2026-08-07 |
|
||||||
> 当前目标:WASI 0.2(Preview 2),NTS,单线程;不支持 WASI 0.1 |
|
||||||
|
|
||||||
## 1. 文档目的 |
|
||||||
|
|
||||||
本文记录 TypePHP 支持 WebAssembly 的技术决策、功能边界、运行时架构、主要风险、验证方法和分阶段实施计划。 |
|
||||||
|
|
||||||
2026-08-07 的实现验证已经证明:精简 PHP 8.5、PHPX 核心、TypePHP 生成代码、GMP、MPFR 和 mpdecimal 可以通过 WASI SDK 静态链接为单个模块,并在 Wasmtime 中运行。可复现构建方法见 [构建 TypePHP WASI 程序](WASI_BUILD.md)。本文余下内容同时保留浏览器阶段的设计目标。 |
|
||||||
|
|
||||||
## 2. 核心结论 |
|
||||||
|
|
||||||
首个 TypePHP WASM 版本采用以下路线: |
|
||||||
|
|
||||||
```text |
|
||||||
PHP 源码 |
|
||||||
-> TypePHP 编译器 |
|
||||||
-> TypePHP 生成的 C++ |
|
||||||
-> WASI SDK 编译和静态链接 |
|
||||||
+ PHP NTS |
|
||||||
+ PHPX |
|
||||||
+ TypePHP runtime |
|
||||||
+ GMP / MPFR / mpdecimal |
|
||||||
+ PHP embed/WASI 运行时 |
|
||||||
-> typephp.wasm(WASI 0.2 command component) |
|
||||||
``` |
|
||||||
|
|
||||||
具体决策如下: |
|
||||||
|
|
||||||
1. 第一版复用当前 C++/Zend 后端,不直接生成 WAT/WASM,也不重新实现 PHP 运行时。 |
|
||||||
2. 使用 WASI SDK 的 `wasm32-wasip2` sysroot 直接生成 Component;Chrome 由 Jco 转译为 ESM,不维护第二套 Emscripten ABI。 |
|
||||||
3. PHP、PHPX、TypePHP 生成代码和高精度库全部静态链接到一个 `.wasm` 模块。 |
|
||||||
4. Wasmtime 和 Chrome 共同提供 CLI、stdio、exit、clocks、random 和受控文件系统;Chrome host 固定运行在 Worker 中。 |
|
||||||
5. 仅支持 PHP NTS,不支持线程。 |
|
||||||
6. 禁用 Fiber 和 Generator。 |
|
||||||
7. 必须支持 C++ 异常以及 Zend bailout 所需的 `setjmp/longjmp`。 |
|
||||||
8. 保留 PHP stream 框架和本地 stream,禁用网络 transport 和依赖操作系统进程能力的功能。 |
|
||||||
9. WordPress Playground 和其他 PHP-WASM 项目只作为补丁与移植经验来源,不作为 TypePHP 的依赖或代码基础。 |
|
||||||
|
|
||||||
本文描述的是最短可落地路径。长期的后端中立方案参见 [BACKEND_NEUTRAL_IR.md](BACKEND_NEUTRAL_IR.md)。WASI 原型证明无需为了 WASM 重写 TypePHP 前端和语义层。 |
|
||||||
|
|
||||||
## 3. 为什么不采用 WordPress Playground |
|
||||||
|
|
||||||
WordPress Playground 是一个成熟的浏览器 WordPress 产品,但不是小型 PHP-WASM 移植层。其仓库和构建体系同时服务于: |
|
||||||
|
|
||||||
- 多个 PHP 版本和扩展组合; |
|
||||||
- WordPress 发行版及其资源; |
|
||||||
- 浏览器、Web Worker 和 Node.js 运行时; |
|
||||||
- 虚拟文件系统、挂载和持久化; |
|
||||||
- 网络代理和浏览器 HTTP 适配; |
|
||||||
- NPM 包、网站、开发工具及集成测试; |
|
||||||
- WordPress 特有的 API 和产品功能。 |
|
||||||
|
|
||||||
TypePHP 无法直接复用 Playground 发布的 PHP-WASM 二进制,因为 TypePHP 需要把 PHPX、编译生成的 C++ 和高精度库一起静态链接。若 fork Playground,TypePHP 还会被其 monorepo、Node/NPM 构建、版本矩阵和产品发布周期绑定。 |
|
||||||
|
|
||||||
因此采用以下原则: |
|
||||||
|
|
||||||
- 不 fork WordPress Playground; |
|
||||||
- 不把 `@php-wasm/*` 作为 TypePHP 的运行时依赖; |
|
||||||
- 不复制其 WordPress、网络代理、文件同步和 UI 层; |
|
||||||
- 仅研究 PHP configure 参数、php-src 补丁、Emscripten 兼容处理和最小 C API; |
|
||||||
- 所有借用补丁必须拆分、注明来源,并验证是否仍适用于 TypePHP 固定的 PHP/Emscripten 版本。 |
|
||||||
|
|
||||||
`seanmorris/php-wasm`、`soyuka/php-wasm` 等项目也遵循相同原则:可用作构建参考和问题索引,但不成为 TypePHP 的基础仓库。 |
|
||||||
|
|
||||||
## 4. 目标与非目标 |
|
||||||
|
|
||||||
### 4.1 当前 WASI 目标 |
|
||||||
|
|
||||||
- 在 Wasmtime 等 WASI runtime 中加载 TypePHP 编译产物。 |
|
||||||
- 执行静态编译的 TypePHP 应用入口。 |
|
||||||
- 保持 TypePHP 当前基于 Zend 和 PHPX 的主要语言语义。 |
|
||||||
- 正确处理 PHP request 生命周期、C++ 异常和 Zend bailout。 |
|
||||||
- 支持 GMP、MPFR 和 mpdecimal 高精度类型。 |
|
||||||
- 支持 WASI 文件系统和必要的本地 PHP stream。 |
|
||||||
- 对不支持的功能给出确定、可测试的错误,而不是链接失败或运行时崩溃。 |
|
||||||
- 构建过程可复现,php-src、WASI SDK 和数值库版本固定。 |
|
||||||
|
|
||||||
### 4.2 首期非目标 |
|
||||||
|
|
||||||
- 无宿主适配的浏览器直接运行。 |
|
||||||
- pthread、Web Worker 并行 PHP 或共享内存。 |
|
||||||
- Fiber 和 Generator。 |
|
||||||
- 动态扩展加载。 |
|
||||||
- PHP 源码的运行时编译或通用 `eval()`。 |
|
||||||
- TCP、UDP、Unix socket 和监听端口。 |
|
||||||
- MySQL、PostgreSQL、Redis 等网络客户端。 |
|
||||||
- `curl`、FTP、SMTP 等网络协议实现。 |
|
||||||
- `fork`、`exec`、`system`、`shell_exec`、`proc_open` 和信号处理。 |
|
||||||
- FFI、JIT、opcache 和调试器。 |
|
||||||
- 完整 WordPress 兼容性。 |
|
||||||
- 在第一阶段实现异步宿主调用。 |
|
||||||
|
|
||||||
## 5. 目标平台选择 |
|
||||||
|
|
||||||
### 5.1 当前使用 WASI SDK |
|
||||||
|
|
||||||
当前先建立命令行可验证基线。WASI SDK 已经验证可以同时提供: |
|
||||||
|
|
||||||
- C/C++ 到 WebAssembly 的完整工具链; |
|
||||||
- 标准 Wasm C++ exception handling; |
|
||||||
- Zend bailout 所需的 SJLJ; |
|
||||||
- capability-based 文件系统; |
|
||||||
- libc、时间和随机数接口。 |
|
||||||
|
|
||||||
PHP、PHPX 和所有 TypePHP C++ 翻译单元必须使用一致的 Wasm EH/SJLJ 参数。链接器必须将函数签名不一致视为致命错误。 |
|
||||||
|
|
||||||
### 5.2 Chrome Component host |
|
||||||
|
|
||||||
Chrome 当前不能原生实例化 Component。构建器使用 Jco 将同一份 WASI 0.2 Component 转译为 core Wasm 与 ESM,并由 `examples/wasm-hello/typephp-worker.mjs` 演示宿主入口。浏览器适配不包含 PHP、PHPX 或高精度类型语义。 |
|
||||||
|
|
||||||
## 6. 产物和运行模型 |
|
||||||
|
|
||||||
### 6.1 发布产物 |
|
||||||
|
|
||||||
建议最小发布物为: |
|
||||||
|
|
||||||
```text |
|
||||||
dist/ |
|
||||||
├── typephp.wasm |
|
||||||
└── typephp-wasm.mjs |
|
||||||
``` |
|
||||||
|
|
||||||
所有 C/C++ 代码进入 `typephp.wasm`。浏览器自身不会自动提供 WASI imports;`typephp-wasm.mjs` 作为 WASI host/adapter 的装载入口,只负责: |
|
||||||
|
|
||||||
- 获取和实例化 `.wasm`; |
|
||||||
- 提供 stdout/stderr; |
|
||||||
- 初始化内存文件系统; |
|
||||||
- 实现或接入 WASI clocks、随机数等宿主能力; |
|
||||||
- 调用导出的 TypePHP 生命周期接口; |
|
||||||
- 把状态码和错误信息转换为 JavaScript 结果。 |
|
||||||
|
|
||||||
不应把 PHP 语义、Zend 对象操作或 TypePHP 业务逻辑放入 JavaScript loader。 |
|
||||||
|
|
||||||
### 6.2 生命周期 |
|
||||||
|
|
||||||
建议采用“模块启动一次、请求可重复执行”的模型: |
|
||||||
|
|
||||||
```text |
|
||||||
instantiate wasm |
|
||||||
-> typephp_wasm_module_startup() |
|
||||||
-> typephp_wasm_request_startup() |
|
||||||
-> TypePHP AOT entry |
|
||||||
-> typephp_wasm_request_shutdown() |
|
||||||
-> 可再次执行 request |
|
||||||
-> typephp_wasm_module_shutdown() |
|
||||||
``` |
|
||||||
|
|
||||||
每次请求必须有独立的 PHP request 内存池。执行成功、PHP 异常、C++ 异常和 Zend bailout 都必须进入统一的清理路径。 |
|
||||||
|
|
||||||
模块导出 API 可从以下最小集合开始,名称以实际实现为准: |
|
||||||
|
|
||||||
```c |
|
||||||
int typephp_wasm_module_startup(void); |
|
||||||
int typephp_wasm_run(int argc, const char **argv); |
|
||||||
const char *typephp_wasm_last_error(void); |
|
||||||
void typephp_wasm_module_shutdown(void); |
|
||||||
``` |
|
||||||
|
|
||||||
`typephp_wasm_run()` 执行已经静态链接的 AOT 入口,不负责在运行时解析和编译任意 PHP 源码。 |
|
||||||
|
|
||||||
## 7. PHP 构建策略 |
|
||||||
|
|
||||||
### 7.1 基础配置 |
|
||||||
|
|
||||||
- 固定一个明确的 php-src commit,而不是只固定分支名。 |
|
||||||
- NTS 构建。 |
|
||||||
- 禁用 CLI、CGI、FPM、Apache 等现有 SAPI。 |
|
||||||
- 新增最小 `typephp_wasm` SAPI,或先用极小的 embed 原型验证生命周期,再收敛为专用 SAPI。 |
|
||||||
- 禁用 opcache/JIT。 |
|
||||||
- 所有扩展静态链接。 |
|
||||||
- 关闭不需要的扩展和自动探测,避免宿主机环境改变构建结果。 |
|
||||||
- 用 `config.site` 和独立 patch 目录记录交叉编译结论。 |
|
||||||
|
|
||||||
首期不要直接复制其他项目的完整 configure 参数。应从最小 PHP core 启动,根据 TypePHP PHPT 和运行时依赖逐项增加扩展。 |
|
||||||
|
|
||||||
### 7.2 扩展分层 |
|
||||||
|
|
||||||
建议把扩展分为三组: |
|
||||||
|
|
||||||
1. **必须启用**:TypePHP 和 Zend 基本运行所需的 core、standard、SPL、date、pcre、hash、json 等,最终以实际链接和测试结果为准。 |
|
||||||
2. **可选本地扩展**:ctype、filter、mbstring、tokenizer、fileinfo、zlib 等,无操作系统网络依赖,但会增加体积。 |
|
||||||
3. **首期禁用**:sockets、curl、mysqli、PDO 网络驱动、pcntl、posix、FFI、shm、sysv、readline、opcache/JIT 等。 |
|
||||||
|
|
||||||
GMP、MPFR 和 mpdecimal 首先作为 PHPX/TypePHP 高精度实现的静态依赖处理,不要求启用 PHP `ext/gmp`。 |
|
||||||
|
|
||||||
## 8. PHP stream 和操作系统能力 |
|
||||||
|
|
||||||
### 8.1 不关闭整个 stream 子系统 |
|
||||||
|
|
||||||
PHP 标准库大量依赖 stream。完全关闭 stream 会破坏文件读写、`php://`、include 路径处理以及部分标准扩展,收益小而兼容成本高。 |
|
||||||
|
|
||||||
首期保留: |
|
||||||
|
|
||||||
- 普通文件 stream,底层使用 Emscripten MEMFS; |
|
||||||
- `php://memory`; |
|
||||||
- `php://temp`; |
|
||||||
- `php://stdin`、`php://stdout`、`php://stderr` 的宿主映射; |
|
||||||
- `data://` 是否启用由体积和安全评估决定; |
|
||||||
- 纯内存 stream filter 可按需启用。 |
|
||||||
|
|
||||||
### 8.2 禁用网络 stream |
|
||||||
|
|
||||||
应在 PHP 构建和运行时注册阶段禁用或不注册: |
|
||||||
|
|
||||||
- TCP、UDP 和 Unix socket transport; |
|
||||||
- socket 扩展; |
|
||||||
- `http://`、`https://`、`ftp://` 等依赖网络的 wrapper; |
|
||||||
- `fsockopen()`、`pfsockopen()`、`stream_socket_*()`; |
|
||||||
- 网络数据库和网络客户端扩展。 |
|
||||||
|
|
||||||
首期不应通过同步 XHR 或隐式 JavaScript fetch 模拟 PHP socket。若未来需要 HTTP,应设计显式、可授权的异步宿主 API,而不是伪造 POSIX socket。 |
|
||||||
|
|
||||||
### 8.3 其他 OS 相关功能 |
|
||||||
|
|
||||||
以下能力需要禁用、降级或由宿主注入: |
|
||||||
|
|
||||||
| 能力 | 首期策略 | |
|
||||||
|---|---| |
|
||||||
| 文件系统 | MEMFS;可选只读预加载文件 | |
|
||||||
| 当前目录和路径 | 虚拟根目录,禁止泄漏宿主路径 | |
|
||||||
| 环境变量 | loader 注入白名单 | |
|
||||||
| 时间 | WASI clocks;浏览器宿主使用浏览器时钟实现该接口 | |
|
||||||
| 随机数 | WASI random;浏览器宿主使用安全随机源实现,不使用弱伪随机替代 | |
|
||||||
| DNS、socket | 不支持 | |
|
||||||
| 进程、shell | 不支持 | |
|
||||||
| 信号 | 不支持 | |
|
||||||
| 用户、组、权限 | 固定值或明确报错 | |
|
||||||
| 文件锁 | 首期不支持跨实例锁;单实例内按需降级 | |
|
||||||
| 持久化 | 默认关闭;Chrome 可显式启用 OPFS 文件系统快照 | |
|
||||||
|
|
||||||
编译器应逐步增加 WASM target capability 检查:静态可识别的不支持函数在编译期报错;动态调用无法静态判断时,由运行时返回确定错误。禁止让这些调用表现为链接期缺失符号、空函数或未定义行为。 |
|
||||||
|
|
||||||
## 9. 异常、bailout 和清理 |
|
||||||
|
|
||||||
这是项目的首要技术风险,必须早于完整 PHP 功能移植进行验证。 |
|
||||||
|
|
||||||
### 9.1 编译选项 |
|
||||||
|
|
||||||
使用原生 WebAssembly exceptions 时,C 和 C++ 对 `setjmp/longjmp` 的模式必须一致。原型建议验证以下组合: |
|
||||||
|
|
||||||
```text |
|
||||||
C 编译: |
|
||||||
-sSUPPORT_LONGJMP=wasm |
|
||||||
|
|
||||||
C++ 编译: |
|
||||||
-fwasm-exceptions |
|
||||||
-sSUPPORT_LONGJMP=wasm |
|
||||||
|
|
||||||
最终链接: |
|
||||||
-fwasm-exceptions |
|
||||||
-sSUPPORT_LONGJMP=wasm |
|
||||||
``` |
|
||||||
|
|
||||||
所有 PHP、PHPX、TypePHP 和第三方 C/C++ 对象必须使用同一套 ABI 和异常配置。不能只在最终链接阶段补开 C++ exception catching。 |
|
||||||
|
|
||||||
如果目标浏览器兼容性不允许原生 Wasm EH,可研究 Emscripten JavaScript exception 模式作为备选,但不得在同一发布物中混用两套模型。 |
|
||||||
|
|
||||||
### 9.2 边界规则 |
|
||||||
|
|
||||||
- C++ 异常不得未经处理地穿过导出函数进入 JavaScript。 |
|
||||||
- Zend bailout 必须被 request 顶层捕获,并进入 request shutdown。 |
|
||||||
- bailout 后不能继续析构依赖已销毁 request 内存池的悬空 PHPX 对象。 |
|
||||||
- 栈上的 PHPX `Variant`、`Object`、`Array` 和高精度对象必须在内存池仍有效时完成析构,或由专门的 bailout 安全边界接管。 |
|
||||||
- 一个请求失败后,下一次请求必须仍可执行;否则运行时只能定义为一次性实例,并在 API 中明确。 |
|
||||||
|
|
||||||
### 9.3 必测场景 |
|
||||||
|
|
||||||
- PHP 正常返回。 |
|
||||||
- PHP `throw` 被 TypePHP 代码捕获。 |
|
||||||
- PHP 未捕获异常到达请求顶层。 |
|
||||||
- `fatalError`/Zend bailout。 |
|
||||||
- C++ `throw` 和 `catch`。 |
|
||||||
- PHP 调用 C++、C++ 再调用 PHP 时抛出异常。 |
|
||||||
- bailout 发生时栈上存在 PHPX 对象和高精度对象。 |
|
||||||
- 连续执行成功、失败、成功三个请求。 |
|
||||||
- 内存增长后再次执行请求。 |
|
||||||
|
|
||||||
## 10. 内存和高精度库 |
|
||||||
|
|
||||||
### 10.1 WASM 内存 |
|
||||||
|
|
||||||
首期使用单一线性内存,并验证 `-sALLOW_MEMORY_GROWTH`。需要记录: |
|
||||||
|
|
||||||
- 初始内存; |
|
||||||
- 最大内存; |
|
||||||
- PHP memory_limit; |
|
||||||
- request 结束后的 Zend 内存回收; |
|
||||||
- Emscripten allocator 的实际峰值; |
|
||||||
- 多次 request 后是否持续增长。 |
|
||||||
|
|
||||||
不要在没有基准测试前选择 `emmalloc`。PHP、GMP、MPFR 和 mpdecimal 都是分配密集型组件,应在 `dlmalloc`、`emmalloc` 等候选之间测试体积与运行时间。 |
|
||||||
|
|
||||||
### 10.2 GMP、MPFR 和 mpdecimal |
|
||||||
|
|
||||||
- 全部使用 Emscripten 工具链静态编译。 |
|
||||||
- 禁用汇编和宿主 CPU 专用优化。 |
|
||||||
- 固定 limb、整数宽度和 ABI 检测结果。 |
|
||||||
- 不依赖运行时动态库搜索。 |
|
||||||
- 运行现有 BigInt、BigFloat、Decimal PHPT,并增加最大内存、除零、精度、舍入和异常路径测试。 |
|
||||||
- 验证库异常或分配失败不会绕过 PHP request 清理。 |
|
||||||
|
|
||||||
## 11. 建议的仓库结构 |
|
||||||
|
|
||||||
建议在实现阶段增加独立目录,不把 Emscripten 条件散落到现有构建代码中: |
|
||||||
|
|
||||||
```text |
|
||||||
wasm/ |
|
||||||
├── README.md |
|
||||||
├── build.sh |
|
||||||
├── versions.env |
|
||||||
├── config.site |
|
||||||
├── cmake/ |
|
||||||
│ └── TypePhpWasmToolchain.cmake |
|
||||||
├── patches/ |
|
||||||
│ ├── php-src/ |
|
||||||
│ ├── gmp/ |
|
||||||
│ ├── mpfr/ |
|
||||||
│ └── mpdecimal/ |
|
||||||
├── sapi/ |
|
||||||
│ └── typephp_wasm/ |
|
||||||
├── runtime/ |
|
||||||
│ └── typephp-wasm.mjs |
|
||||||
└── tests/ |
|
||||||
``` |
|
||||||
|
|
||||||
维护原则: |
|
||||||
|
|
||||||
- patch 应小而独立,一项兼容问题一个 patch; |
|
||||||
- 每个 patch 记录上游版本、来源、原因和可删除条件; |
|
||||||
- 下载缓存不提交到 Git; |
|
||||||
- php-src、Emscripten 和第三方库使用校验和锁定; |
|
||||||
- 构建产物不进入源码仓库; |
|
||||||
- CI 至少保留 debug 和 release 两种构建。 |
|
||||||
|
|
||||||
## 12. 分阶段实施计划 |
|
||||||
|
|
||||||
### 阶段 0:工具链风险验证 |
|
||||||
|
|
||||||
目标:不接入完整 TypePHP,先证明关键底层机制可行。 |
|
||||||
|
|
||||||
- 固定 Emscripten 版本。 |
|
||||||
- 编译最小 C/C++ 混合程序。 |
|
||||||
- 验证 C++ exception。 |
|
||||||
- 验证 `setjmp/longjmp`。 |
|
||||||
- 验证两者嵌套和重复调用。 |
|
||||||
- 验证主流浏览器支持情况。 |
|
||||||
|
|
||||||
退出条件:异常和 longjmp 行为稳定,没有不可接受的浏览器缺口。 |
|
||||||
|
|
||||||
### 阶段 1:最小 PHP NTS |
|
||||||
|
|
||||||
目标:PHP core 在浏览器中完成模块和请求生命周期。 |
|
||||||
|
|
||||||
- 交叉编译最小 php-src。 |
|
||||||
- 实现最小 WASM SAPI 或 embed 验证层。 |
|
||||||
- 支持 stdout/stderr 和 MEMFS。 |
|
||||||
- 执行固定入口。 |
|
||||||
- 验证 fatal error、异常和 request shutdown。 |
|
||||||
|
|
||||||
退出条件:连续执行“成功、失败、成功”请求无崩溃、无持续内存增长。 |
|
||||||
|
|
||||||
### 阶段 2:接入 PHPX 和 TypePHP |
|
||||||
|
|
||||||
目标:现有 TypePHP C++ 后端可以由 `em++` 编译并静态链接。 |
|
||||||
|
|
||||||
- 为编译器增加 WASM platform/backend 配置。 |
|
||||||
- 统一 PHPX、TypePHP 和第三方库编译 flags。 |
|
||||||
- 链接一个最小 TypePHP `main()`。 |
|
||||||
- 建立 WASM smoke PHPT 子集。 |
|
||||||
- 为不支持的系统 API 增加 capability diagnostics。 |
|
||||||
|
|
||||||
退出条件:基础类型、函数、类、异常、数组和对象测试通过。 |
|
||||||
|
|
||||||
### 阶段 3:高精度与本地 stream |
|
||||||
|
|
||||||
目标:支持 TypePHP 关键运行时能力。 |
|
||||||
|
|
||||||
- 静态链接 GMP、MPFR、mpdecimal。 |
|
||||||
- 运行高精度完整运算符和边界测试。 |
|
||||||
- 支持必要的 `file://` 和 `php://` stream。 |
|
||||||
- 增加预加载只读资源机制。 |
|
||||||
- 明确所有被禁用的 wrapper、transport 和扩展。 |
|
||||||
|
|
||||||
退出条件:高精度测试通过,本地文件行为确定,网络 API 全部可预测地失败。 |
|
||||||
|
|
||||||
### 阶段 4:体积、性能和发布 |
|
||||||
|
|
||||||
目标:形成可分发的 TypePHP WASM SDK。 |
|
||||||
|
|
||||||
- release 优化和 dead-code elimination。 |
|
||||||
- 检查导出符号白名单。 |
|
||||||
- 比较 allocator 和内存增长配置。 |
|
||||||
- 建立下载体积、启动时间和峰值内存基准。 |
|
||||||
- 生成 `typephp.wasm` 和薄 `.mjs` loader。 |
|
||||||
- 编写用户侧功能与限制文档。 |
|
||||||
|
|
||||||
退出条件:产物可复现,兼容性清单完整,性能达到预设基线。 |
|
||||||
|
|
||||||
### 阶段 5:可选宿主能力 |
|
||||||
|
|
||||||
后续按真实需求选择,不作为基础运行时默认能力: |
|
||||||
|
|
||||||
- IDBFS 或 OPFS 持久化; |
|
||||||
- 显式 HTTP host API; |
|
||||||
- Node.js 宿主; |
|
||||||
- WASI 原型; |
|
||||||
- 多实例隔离; |
|
||||||
- Web Worker 并行实例。 |
|
||||||
|
|
||||||
每项能力都必须通过显式 capability 开启,不能让 PHP 代码默认获得宿主全部权限。 |
|
||||||
|
|
||||||
## 13. 测试策略 |
|
||||||
|
|
||||||
### 13.1 测试层次 |
|
||||||
|
|
||||||
1. **工具链测试**:exception、longjmp、静态库、链接和导出符号。 |
|
||||||
2. **PHP 生命周期测试**:module/request startup、shutdown、bailout 和重复请求。 |
|
||||||
3. **PHPX 测试**:Variant、Object、Array、引用、异常和资源析构。 |
|
||||||
4. **TypePHP PHPT**:选择不依赖 OS 的现有测试,并维护 WASM 跳过原因。 |
|
||||||
5. **高精度测试**:完整运算符、边界、错误和内存压力。 |
|
||||||
6. **能力限制测试**:网络、进程、线程和动态扩展必须稳定拒绝。 |
|
||||||
7. **浏览器测试**:Chrome、Firefox、Safari 的最低支持版本。 |
|
||||||
|
|
||||||
### 13.2 关键指标 |
|
||||||
|
|
||||||
- `.wasm` 原始大小和压缩大小; |
|
||||||
- 首次实例化时间; |
|
||||||
- module startup 和 request startup 时间; |
|
||||||
- 简单 TypePHP 程序执行时间; |
|
||||||
- 初始、峰值和多请求后的线性内存; |
|
||||||
- 异常和 bailout 后的可恢复性; |
|
||||||
- JavaScript loader 大小; |
|
||||||
- 相同输入的可复现构建校验和。 |
|
||||||
|
|
||||||
## 14. Go/No-Go 条件 |
|
||||||
|
|
||||||
出现以下任一情况,应暂停完整移植并重新评估架构: |
|
||||||
|
|
||||||
- Zend bailout 与 C++ 栈析构无法建立安全边界; |
|
||||||
- 请求失败会稳定破坏后续请求,且不能接受一次性实例模型; |
|
||||||
- GMP、MPFR 或 mpdecimal 需要大规模侵入式 fork; |
|
||||||
- `.wasm` 体积或浏览器峰值内存明显超出目标场景可接受范围; |
|
||||||
- Safari、Firefox、Chrome 需要互不兼容的异常 ABI; |
|
||||||
- PHPX 中依赖原生线程、动态链接或 OS 资源的假设无法隔离。 |
|
||||||
|
|
||||||
如果最短路径不可行,再评估 [BACKEND_NEUTRAL_IR.md](BACKEND_NEUTRAL_IR.md) 所述的独立 WASM runtime/backend,不应在没有原型数据时提前启动该重写。 |
|
||||||
|
|
||||||
## 15. 外部参考 |
|
||||||
|
|
||||||
- [PHP 源码仓库](https://github.com/php/php-src) |
|
||||||
- [Emscripten:C setjmp/longjmp 支持](https://emscripten.org/docs/porting/setjmp-longjmp.html) |
|
||||||
- [Emscripten:C/C++ 可移植性说明](https://emscripten.org/docs/porting/guidelines/portability_guidelines.html) |
|
||||||
- [Emscripten:代码与内存优化](https://emscripten.org/docs/optimizing/Optimizing-Code.html) |
|
||||||
- [WordPress Playground:编译 PHP 到 WebAssembly](https://developer.wordpress.org/playground/developers/architecture/wasm-php-compiling/) |
|
||||||
- [WordPress Playground 架构](https://wordpress.github.io/wordpress-playground/developers/architecture/) |
|
||||||
- [seanmorris/php-wasm](https://github.com/seanmorris/php-wasm) |
|
||||||
- [soyuka/php-wasm](https://github.com/soyuka/php-wasm) |
|
||||||
|
|
||||||
这些链接用于追踪上游行为和已知移植问题。TypePHP 的最终实现和兼容性必须由自己的构建、测试及基准验证,不能直接继承其他项目的结论。 |
|
||||||
@ -1,228 +0,0 @@ |
|||||||
# 构建 TypePHP WASI 程序 |
|
||||||
|
|
||||||
TypePHP 使用稳定的 WASI 0.2(Preview 2)和 Component Model。TypePHP 生成的 C++、PHPX 核心、精简的 PHP 8.5 NTS、GMP、MPFR 和 mpdecimal 会静态链接为单个 `.wasm` command 或 library component。WASI 0.1(Preview 1)不受支持。 |
|
||||||
|
|
||||||
## 环境要求 |
|
||||||
|
|
||||||
- WASI SDK 33 或更高版本(LLVM/Clang/LLD 22 或更高) |
|
||||||
- PHP 8.4 或更高版本,用于运行 TypePHP 编译器 |
|
||||||
- Wasmtime 47 或更高版本,用于运行和测试产物 |
|
||||||
- Jco 1 或更高版本,用于 browser profile;component profile 不需要 Jco |
|
||||||
- wit-bindgen-cli 0.60.0,用于 library/WasmExport 模式;command 模式不需要 |
|
||||||
- 与当前 TypePHP 版本绑定的 `wasm32-wasip2` 集成 SDK |
|
||||||
|
|
||||||
WASI SDK 的 `bin` 目录和 Wasmtime 必须加入系统 `PATH`。编译器不会探测或使用 `/opt` 等约定安装目录,也不接受专用的工具目录配置。WASI 静态库和头文件统一安装到 PHPX 的 `wasm/wasm32-wasip2/`: |
|
||||||
|
|
||||||
```bash |
|
||||||
export PATH="<wasi-sdk-bin>:<wasmtime-bin>:$PATH" |
|
||||||
``` |
|
||||||
|
|
||||||
TypePHP 使用现有的 PHPX 定位规则:优先读取 `PHPX_HOME`,其次读取 Composer 的 `swoole/phpx` 安装位置,最后使用 `vendor/swoole/phpx`。不新增 WASI 专用环境变量。 |
|
||||||
|
|
||||||
WASI 构建会检查 `wasm32-wasip2-clang`、`wasm32-wasip2-clang++`、`llvm-ar`、`llvm-ranlib`、`llvm-nm`、`wasm-component-ld` 和 `wasmtime`,并确认目标是 `wasm32-unknown-wasip2`。browser profile 另外检查 `jco`,library 模式另外检查固定版本的 `wit-bindgen`。所有工具只从 `PATH` 查找;npm script 会自动将项目本地的 `node_modules/.bin` 加入 `PATH`。 |
|
||||||
|
|
||||||
## 一条命令构建 |
|
||||||
|
|
||||||
command 模式的源文件必须提供 `main(): void`: |
|
||||||
|
|
||||||
```php |
|
||||||
<?php |
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
echo "Hello from TypePHP/WASI\n"; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
执行: |
|
||||||
|
|
||||||
```bash |
|
||||||
php bin/tpc.php --wasm hello.php |
|
||||||
``` |
|
||||||
|
|
||||||
单文件输入默认只生成当前目录下可由 Wasmtime 执行的 `hello.wasm` Component,不要求安装 Jco。生成的 `.cc` 与 host 模式使用相同的 build 目录规则,默认位于 TypePHP 根目录的 `build/`;可以使用 `--build-dir <directory>` 覆盖。 |
|
||||||
|
|
||||||
项目可以直接使用 `project.yml`: |
|
||||||
|
|
||||||
```yaml |
|
||||||
name: wasm-hello |
|
||||||
mode: bin |
|
||||||
wasm: component |
|
||||||
build-dir: build |
|
||||||
output: component/wasm-hello.wasm |
|
||||||
sources: |
|
||||||
- src |
|
||||||
``` |
|
||||||
|
|
||||||
`wasm` 只接受 `component` 或 `browser`,不接受布尔值。配置后直接执行 `php bin/tpc.php project.yml` 即可进入 WASI 构建,无需重复传入 `--wasm`。WASM 项目未配置 `target-platform` 时默认使用 `wasm32-wasip2`;`build-dir`、`output` 和 `wasm-browser-dir` 都相对于项目文件解析。完整浏览器应用见 `examples/wasm-hello/`,它显式使用 `wasm: browser`。 |
|
||||||
|
|
||||||
需要生成浏览器模块时,配置 `wasm: browser` 和 `wasm-browser-dir`,并确保 Jco 位于 `PATH`。 |
|
||||||
|
|
||||||
命令行也可以显式选择产物: |
|
||||||
|
|
||||||
- `--wasm` 或 `--wasm=component`:仅生成可由 Wasmtime 运行的 Component,不检测 Jco。 |
|
||||||
- `--wasm=browser`:生成 Component 和 Jco 浏览器模块,需要 `jco` 位于 `PATH`。 |
|
||||||
|
|
||||||
路径、sources 等详细配置继续放在 `project.yml`,不通过 `--wasm=` 传递。 |
|
||||||
|
|
||||||
PHP、PHPX、TypePHP runtime、GMP、MPFR 和 mpdecimal 由 SDK 发布阶段预编译为 WASI 静态库。应用构建只编译 TypePHP 为当前程序生成的 C++,然后链接这些 `.a`。`tpc --wasm` 不会下载源码,也不会调用 PHP、PHPX 或高精度库的构建脚本。library 模式会调用 `PATH` 中的 `wit-bindgen-cli 0.60.0` 生成当前应用的 Canonical ABI 绑定。 |
|
||||||
|
|
||||||
PHP/WASI 当前静态内建 `date`、`pcre`、`hash`、`json`、`lexbor`、`random`、`Reflection`、`SPL`、`standard`、`uri`、`ctype`、`calendar`、`bcmath`、`filter`、`tokenizer`、`mbstring`、`zlib`、`fileinfo`、`sodium`、`openssl`、`libxml`、`dom`、`SimpleXML`、`xml`、`xmlreader`、`xmlwriter`、`PDO`、`pdo_sqlite`、`zip`、`bz2` 和 `exif` 扩展。OpenSSL 采用 crypto-only 构建,不包含 TLS stream transport;HTTP/HTTPS 仍由 WASI HTTP Component 提供。 |
|
||||||
|
|
||||||
每个 C/C++ 翻译单元统一使用标准 Wasm C++ exceptions 和 WASI SJLJ;链接阶段将 ABI 警告视为错误,旧的 32 位 `zend_long` 缓存也会自动失效。 |
|
||||||
|
|
||||||
运行: |
|
||||||
|
|
||||||
```bash |
|
||||||
wasmtime hello.wasm |
|
||||||
``` |
|
||||||
|
|
||||||
Chrome Demo: |
|
||||||
|
|
||||||
```bash |
|
||||||
cd examples/wasm-hello |
|
||||||
npm ci |
|
||||||
npm run wasm |
|
||||||
npm run dev |
|
||||||
``` |
|
||||||
|
|
||||||
浏览器端始终在专用 Worker 中执行 Component。默认使用内存文件系统;发送给 Worker 的启动消息设置 `persistent: true` 后,会在启动和退出时通过 OPFS 恢复、保存文件系统快照。程序执行期间仍使用同步内存文件系统,避免每次 PHP 文件访问跨越异步 JS 边界。 |
|
||||||
|
|
||||||
## Command 与 Library 的 ZendVM 生命周期 |
|
||||||
|
|
||||||
### Command 模式 |
|
||||||
|
|
||||||
command 模式具有生成的 C++ `main()` 入口。入口依次调用: |
|
||||||
|
|
||||||
```text |
|
||||||
typephp_<project>_runtime_init(argc, argv) |
|
||||||
→ php_embed_init() |
|
||||||
→ PHP/SAPI module startup 与 MINIT |
|
||||||
→ PHP request startup 与 RINIT |
|
||||||
→ 注册并启动当前 TypePHP 应用模块 |
|
||||||
→ 当前应用的 MINIT 与 RINIT |
|
||||||
|
|
||||||
执行 TypePHP main() |
|
||||||
|
|
||||||
typephp_<project>_runtime_shutdown() |
|
||||||
→ 当前应用的 RSHUTDOWN 与模块清理 |
|
||||||
→ php_embed_shutdown() |
|
||||||
→ PHP request/module/SAPI shutdown |
|
||||||
``` |
|
||||||
|
|
||||||
调用者不需要感知这些步骤,因为生成的原生 `main()` 会自动包围整个程序生命周期。 |
|
||||||
|
|
||||||
### Library 模式必须先创建 runtime resource |
|
||||||
|
|
||||||
library component 没有可自动执行的 `main()`,单纯实例化 `.wasm` 只完成 Component 和 C/C++ Runtime 的实例化,不代表 ZendVM request 已经可用。Host 必须先调用生成的 WIT 函数: |
|
||||||
|
|
||||||
```wit |
|
||||||
create-runtime: func() -> result<runtime, typephp-error>; |
|
||||||
``` |
|
||||||
|
|
||||||
浏览器中对应的调用为: |
|
||||||
|
|
||||||
```js |
|
||||||
const component = await instantiate(null, wasi.getImportObject()); |
|
||||||
const runtime = await component.api.createRuntime(); |
|
||||||
|
|
||||||
try { |
|
||||||
const result = await runtime.someExportedFunction(); |
|
||||||
} finally { |
|
||||||
runtime[Symbol.dispose](); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
`createRuntime()` 内部通过 `TYPEPHP_RUNTIME_INIT(<project>)(1, argv)` 调用项目级初始化符号。Host |
|
||||||
只需要调用这一层稳定接口,不应直接调用 `php_embed_init()`、MINIT、RINIT 或任何 Zend C API。 |
|
||||||
|
|
||||||
当前初始化顺序如下: |
|
||||||
|
|
||||||
1. `php_embed_init()` 初始化 Embed SAPI、PHP 核心和静态扩展,并启动 PHP request;PHP 核心与已经注册的静态扩展在这里完成 MINIT/RINIT。 |
|
||||||
2. 设置 PHPX 的异常桥接,使 PHP 异常可以安全返回到生成的 WIT `result`。 |
|
||||||
3. 取得当前 TypePHP 应用的 `zend_module_entry`,调用 `zend_register_module_ex()` 和 `zend_startup_module_ex()`,完成应用模块注册与 MINIT。 |
|
||||||
4. 注册标准流并设置请求路径等 SAPI 请求信息。 |
|
||||||
5. 因为 Embed request 和请求内存池此时已经启动,生成代码会显式调用当前应用模块的 `request_startup_func`,补做该模块的 RINIT;RINIT 再初始化 TypePHP 请求级全局变量和类静态数据,完成后才返回 `runtime` resource。 |
|
||||||
|
|
||||||
这里“手动”调用的是 Host 可见的 `create-runtime()`,而不是让用户手动拼装 ZendVM 生命周期。MINIT/RINIT 的具体调用及其先后顺序全部封装在 PHPX 和生成的 Component adapter 中。 |
|
||||||
|
|
||||||
### 导出调用共享同一个 request |
|
||||||
|
|
||||||
同一 `runtime` resource 上的所有 `#[WasmExport]` 调用共享一次 RINIT 建立的 Zend request: |
|
||||||
|
|
||||||
- 不会在每次函数调用前后重复执行 RINIT/RSHUTDOWN。 |
|
||||||
- PHP request 内存池、请求级全局变量和静态状态会持续到 resource 被释放。 |
|
||||||
- 当前仅支持 NTS;同一个 runtime 上的调用必须串行,生成的 adapter 会拒绝并发或重入调用。 |
|
||||||
- 普通 PHP 异常会被转换为 WIT `result` 错误,runtime 仍然可以继续使用。 |
|
||||||
- Zend bailout 表示请求状态可能已经损坏,adapter 会将 runtime 标记为 failed,后续调用会被拒绝,直到 resource 被释放。 |
|
||||||
|
|
||||||
### 释放 resource 才会执行 RSHUTDOWN |
|
||||||
|
|
||||||
释放 WIT `runtime` resource 会通过 `TYPEPHP_RUNTIME_SHUTDOWN(<project>)()` 调用项目级关闭符号: |
|
||||||
|
|
||||||
1. 调用当前 TypePHP 应用模块的 RSHUTDOWN,清理 TypePHP 请求级对象和全局数据。 |
|
||||||
2. 注销并关闭当前应用模块,执行相应模块清理。 |
|
||||||
3. 调用 `php_embed_shutdown()`,完成其余扩展的 request shutdown、module shutdown 和 SAPI shutdown。 |
|
||||||
4. 最后释放 request 内存池,避免 PHP/CPP 包装对象在内存池消失后继续析构。 |
|
||||||
|
|
||||||
不要只依赖 JavaScript GC 触发 resource finalizer。浏览器和 Node Host 应在 `finally` 中显式调用 `runtime[Symbol.dispose]()`;Wasmtime 或其他 Host binding 也应显式 drop resource。直接终止 Worker 或进程会回收整个 Wasm 实例,但不保证 PHP 的 RSHUTDOWN/MSHUTDOWN 回调得到执行,因此不能把必须持久化的数据只放在关闭回调中。 |
|
||||||
|
|
||||||
一个 Component 实例当前只允许同时存在一个活动的 runtime resource。释放完成后可以重新创建;初始化失败或发生 Zend bailout 时,应先释放旧 resource,而不是继续调用导出函数。 |
|
||||||
|
|
||||||
## 高精度类型 |
|
||||||
|
|
||||||
WASI 产物包含 TypePHP 的三种语言级高精度类型: |
|
||||||
|
|
||||||
- `BigInt`:GMP 6.3.0 |
|
||||||
- `BigFloat`:MPFR 4.2.2 |
|
||||||
- `Decimal`:mpdecimal 4.0.1 |
|
||||||
|
|
||||||
完整示例位于 [high-precision.php](../examples/high-precision.php)。构建并运行: |
|
||||||
|
|
||||||
```bash |
|
||||||
php bin/tpc.php --wasm examples/high-precision.php |
|
||||||
wasmtime -S http high-precision.wasm |
|
||||||
``` |
|
||||||
|
|
||||||
预期输出: |
|
||||||
|
|
||||||
```text |
|
||||||
1111111101111111110111111111010 |
|
||||||
1000000000000000000000000000001 |
|
||||||
12348.14159265358979324 |
|
||||||
``` |
|
||||||
|
|
||||||
wasm32 使用 32 位指针,但 PHP 的 `zend_long` 保持 64 位,以维持 TypePHP 与 64 位 PHP 的整数语义。GMP 和 mpdecimal 使用 32 位 limb;这不改变任意精度语义,但大数吞吐量低于具有汇编优化的原生 64 位构建。 |
|
||||||
|
|
||||||
## 当前平台边界 |
|
||||||
|
|
||||||
- 仅支持 NTS、单线程。 |
|
||||||
- Fiber 和 Generator 被禁用;编译器在发现 `yield` 时直接报致命错误。 |
|
||||||
- PHPX Facade API 在 `__wasi__` 下整体禁用。PHPX 核心类型和 `phpx_std` 仍可使用。 |
|
||||||
- 不支持动态扩展、网络 socket、进程、shell 和信号。静态可识别的调用会在编译期报致命错误。 |
|
||||||
- 保留 PHP stream 框架、本地文件能力以及由 WASI host 提供的时间和随机数能力。 |
|
||||||
- command component 可由 Wasmtime 直接运行;library component 需要 Host 按 WIT 接口调用 `create-runtime()` 和导出函数。Chrome 使用 Jco 生成的 ESM 和 `examples/wasm-hello/typephp-worker.mjs` 中的 Worker host。 |
|
||||||
|
|
||||||
PHPX Facade 只是为 PHP 可选扩展生成的便捷包装,并非 TypePHP ABI 的组成部分。WASI 下整体关闭它,可以避免把 curl、socket、Swoole 等不可用 API 暴露为“可编译但链接失败”的接口;PHP/WASI 静态内建扩展本身不受 Facade 开关影响。 |
|
||||||
|
|
||||||
## WASI SDK 目录 |
|
||||||
|
|
||||||
集成 SDK 使用唯一、完整的前缀,位于 PHPX 根目录的 `wasm/wasm32-wasip2/`: |
|
||||||
|
|
||||||
```text |
|
||||||
phpx/wasm/wasm32-wasip2/ |
|
||||||
├── include/php/ # PHP 安装头文件 |
|
||||||
├── include/phpx/ # PHPX 和 TypePHP runtime 头文件 |
|
||||||
├── include/gmp.h ... |
|
||||||
├── lib/libphp.a |
|
||||||
├── lib/libphpx.a |
|
||||||
├── lib/libgmp.a |
|
||||||
├── lib/libgmpxx.a |
|
||||||
├── lib/libmpfr.a |
|
||||||
├── lib/libmpdec.a |
|
||||||
├── lib/libmpdec++.a |
|
||||||
└── .typephp-wasi-sdk-abi |
|
||||||
``` |
|
||||||
|
|
||||||
普通用户通过 TypePHP/PHPX 集成安装包获得该目录。TypePHP 开发者需要自行 clone 与当前版本绑定的 `php-8.5.9-wasm` 和 PHPX 源码,并通过 `wasm/build-sdk.sh` 组装完整 SDK。PHP/WASI 只负责 PHP;PHPX 负责 GMP、MPFR、其专属的 mpdecimal 以及 PHPX runtime。所有产物安装到同一个 PHPX checkout。若 PHPX 不在 `vendor/swoole/phpx`,继续使用已有的 `PHPX_HOME` 指向该 checkout。 |
|
||||||
|
|
||||||
不提供单独覆盖 `libphp.a`、`libphpx.a` 或数值库的路径;所有库、头文件和 `.typephp-wasi-sdk-abi` 必须来自同一次兼容构建,避免混用不同的 `zend_long`、C++ exceptions、SJLJ 或 Component Model ABI。 |
|
||||||
@ -1,986 +0,0 @@ |
|||||||
# TypePHP 与 Python 语言级互调用设计 |
|
||||||
|
|
||||||
> 状态:核心设计已确认,按 `python/implementation-plan.md` 分阶段实施。 |
|
||||||
> |
|
||||||
> 本文是语法、类型语义、运行时边界和兼容性目标的设计规范;尚未确认的细节继续在文末维护。 |
|
||||||
|
|
||||||
## 1. 目标 |
|
||||||
|
|
||||||
TypePHP 应在语言层面提供从 TypePHP 调用 Python 包的能力: |
|
||||||
|
|
||||||
1. TypePHP 导入 Python 模块,访问模块成员,调用 Python 函数和类。 |
|
||||||
2. TypePHP 操作 Python 对象,包括属性、方法、下标、迭代、运算符和调用协议。 |
|
||||||
3. TypePHP 函数、闭包和对象可以作为 Python 调用的参数,并允许 Python 在该次动态调用关系中同步回调。 |
|
||||||
4. 两个 VM 在同一进程内直接互调用,不通过 JSON、RPC 或子进程。 |
|
||||||
5. 默认保留 Python 对象身份和类型信息,避免不必要的深拷贝。 |
|
||||||
6. 语法面向普通 TypePHP/PHP 开发者,常规调用不要求理解 CPython C API、GIL 或引用计数。 |
|
||||||
7. 本功能是可选的扩展级能力;不使用 Python 语法的项目不依赖 phpy。 |
|
||||||
|
|
||||||
其中最主要的语言变化是 Python 特殊根命名空间。全局命名空间中的 `python\module\member()`,或其他命名空间中的 `\python\module\member()`,可以直接访问模块成员;`use python\module` 完全按照 PHP 的普通 namespace alias 规则工作,编译器不对 `use` 语句进行 Python 特殊处理。两种形式都把 phpy 原本需要手写的 `PyCore::import('module')` 和返回变量提升为编译期可识别的 lazy module binding。Python 对象的属性、方法、下标、迭代、参数转换、返回包装和异常等能力原则上复用 phpy 已有实现,不在 TypePHP 中重新建立一套运行时。 |
|
||||||
|
|
||||||
非目标: |
|
||||||
|
|
||||||
- 不编译 Python 源码,也不试图替代 CPython。 |
|
||||||
- 不承诺将动态 Python API 静态类型化。 |
|
||||||
- 永久不支持 Python 线程、`asyncio` 或 CPython subinterpreter。 |
|
||||||
- 不生成 Python extension,不向 Python 注册 TypePHP 函数、类或模块。 |
|
||||||
- 不提供 `#[PythonExport]` 或其他 TypePHP 符号导出机制。 |
|
||||||
- 不追求兼容 Python 语法;目标是让 TypePHP 程序方便、可靠地调用 Python 包。 |
|
||||||
- 不将任意 Python 容器自动、递归地复制为 TypePHP 数组。 |
|
||||||
|
|
||||||
## 2. 参考设计 |
|
||||||
|
|
||||||
### 2.1 Mojo |
|
||||||
|
|
||||||
Mojo 使用未经修改的 CPython 运行时保证 Python 生态兼容性,并用统一的 `PythonObject` 包装动态 Python 值。TypePHP 只借鉴其嵌入和对象包装设计,不采用其导出机制。 |
|
||||||
|
|
||||||
可借鉴的部分: |
|
||||||
|
|
||||||
- Python 值默认保持为包装对象。 |
|
||||||
- TypePHP 基础值传入 Python 时可自动转换。 |
|
||||||
- Python 值转回 TypePHP 原生类型时显式转换。 |
|
||||||
- 动态 Python 值使用统一代理类型承载。 |
|
||||||
|
|
||||||
参考:[Mojo Python interoperability](https://docs.modular.com/stable/mojo/manual/python/)、[Mojo Python types](https://docs.modular.com/mojo/manual/python/types)。 |
|
||||||
|
|
||||||
### 2.2 pybind11 |
|
||||||
|
|
||||||
pybind11 明确区分对象所有权、返回值策略、解释器生命周期、GIL guard、位置参数和关键字参数。其经验说明:跨语言调用最危险的部分不是调用语法,而是对象生命周期和异常路径。 |
|
||||||
|
|
||||||
TypePHP 不应把 pybind11 的所有权策略暴露给普通用户,但运行时必须建立同等严格的内部契约。 |
|
||||||
|
|
||||||
参考:[pybind11 embedding](https://pybind11.readthedocs.io/en/stable/advanced/embedding.html)、[pybind11 functions](https://pybind11.readthedocs.io/en/stable/advanced/functions.html)。 |
|
||||||
|
|
||||||
### 2.3 PyO3 |
|
||||||
|
|
||||||
PyO3 使用 GIL token 和带生命周期的 Python 对象指针,从类型系统上区分持有对象、借用对象和 GIL 绑定对象。 |
|
||||||
|
|
||||||
TypePHP 无需向用户暴露生命周期参数,但 phpy 的 C++ 层应借鉴这一点:所有 CPython API 调用必须能证明当前持有 GIL,所有 `PyObject*` 必须明确是 owned、borrowed 还是 stolen reference。 |
|
||||||
|
|
||||||
参考:[PyO3 object model](https://pyo3.rs/main/doc/pyo3/)、[PyO3 Python object types](https://pyo3.rs/main/types)。 |
|
||||||
|
|
||||||
## 3. phpy 的定位 |
|
||||||
|
|
||||||
phpy 是本功能的运行时基础候选,而不是已经验证完成的稳定依赖。 |
|
||||||
|
|
||||||
可复用能力包括: |
|
||||||
|
|
||||||
- 在 ZendVM 进程内初始化 CPython。 |
|
||||||
- `zval` 与 `PyObject*` 的边界转换。 |
|
||||||
- Python 模块、对象、字符串、序列、字典、集合、迭代器和 callable 的代理对象。 |
|
||||||
- TypePHP/PHP 闭包传入 Python后的 callable 代理。 |
|
||||||
- Python 异常到 Zend 异常的基础映射。 |
|
||||||
- Python 同步调用由 ZendVM 主动传入的函数、对象和 callable 代理的基础设施。 |
|
||||||
- GIL RAII guard 的雏形。 |
|
||||||
|
|
||||||
但是不能直接假定现有实现完全正确。后续实施必须同时 review phpy、重构边界、增加测试、修复 BUG 和优化性能。 |
|
||||||
|
|
||||||
设计阶段已经识别出的重点审计项: |
|
||||||
|
|
||||||
- CPython 初始化、重复初始化、关闭顺序和仍存活对象的析构。 |
|
||||||
- 每个 CPython API 的 owned/borrowed/stolen reference 规则。 |
|
||||||
- 所有成功路径和异常路径的 `Py_INCREF/Py_DECREF` 对称性。 |
|
||||||
- GIL 获取、重入调用和 TypePHP 回调 Python 再回调 TypePHP 的行为。 |
|
||||||
- 转换过程已改为每次顶层转换创建独立的 C++ 转换器对象;转换策略、递归栈和深度限制均为对象内状态,并由 RAII 恢复,不再使用进程级或线程级临时函数指针。仍需继续审计跨 VM 回调和生命周期边界。 |
|
||||||
- Python 异常转 Zend 异常后,CPython error indicator 是否始终被正确清理。 |
|
||||||
- Zend 异常转 Python 异常时,原始异常类型、消息和 traceback 的保存。 |
|
||||||
- 运算符协议是否正确。例如 PHP `/` 不能映射为 Python floor division。 |
|
||||||
- Python 大整数、无效 UTF-8、包含 NUL 的 bytes、递归容器和循环引用。 |
|
||||||
- Python 代理持有 Zend 对象时,Zend GC 与 CPython GC 之间可能形成的跨 VM 引用环。 |
|
||||||
- Python 线程、`asyncio`、subinterpreter 必须被永久、显式拒绝,而不是产生未定义行为。 |
|
||||||
|
|
||||||
TypePHP 通过 ZendVM 动态调用 phpy 扩展公开的 `PyCore`、`PyObject`、`PyDict` 等 Facade,不直接链接 `libphpy.so`,也不生成任何 phpy C++ 符号引用。现有公开名称必须保留,TypePHP 不建立第二套用户可见命名体系。 |
|
||||||
|
|
||||||
职责边界: |
|
||||||
|
|
||||||
- phpy 负责所有运行时问题:CPython 初始化、GIL、引用计数、对象代理、类型转换、异常和双 VM 生命周期。 |
|
||||||
- phpy 负责提供稳定、可测试的 Zend internal class/function/object-handler API。 |
|
||||||
- TypePHP 只负责识别语言语法、静态类型和求值顺序,并生成基于 `zend_function*` 与 PHPX/Zend 通用对象 API 的动态调用。 |
|
||||||
- TypePHP 不直接操作裸 `PyObject*`,不复制 phpy 的 GIL、引用计数或异常实现。 |
|
||||||
- 修复运行时 BUG 时优先修复 phpy,不能只在 TypePHP 生成代码中增加补丁绕过。 |
|
||||||
|
|
||||||
最小适配原则: |
|
||||||
|
|
||||||
- TypePHP 的核心新增能力是 Python `use` 解析、模块别名符号和对应代码生成。 |
|
||||||
- `python\name()`、`module\name`、`module\name()` 和运算符 lowering 都应落到 phpy 的 Zend Facade;Python 运算符通过标准库 `operator` module 调用完整的 CPython 运算协议。 |
|
||||||
- phpy 已正确解决的行为只补测试并复用;只有 review 或测试证明存在 BUG、隐式转换不符合 TypePHP 规则,或者缺少 Zend 动态入口时,才修改 phpy。 |
|
||||||
- TypePHP 不实现 CPython 协议细节,不在生成代码中复制 `PyCore`、`PyObject` 或 `PyModule` 的逻辑。 |
|
||||||
|
|
||||||
## 4. 可选扩展与运行时检测 |
|
||||||
|
|
||||||
Python 互调用是扩展级特性,不是 TypePHP 核心程序的强制依赖。 |
|
||||||
|
|
||||||
- TypePHP 生成代码只依赖 ZendVM/PHPX,不 include phpy 头文件,也不链接 `libphpy.so`。 |
|
||||||
- 编译器识别 Python 语法并保留逻辑上的 `PyObject` 类型信息,但不检查 phpy SDK、动态库、ABI 或 Python module 是否存在。 |
|
||||||
- phpy 必须像普通 PHP 扩展一样由运行环境加载并注册 `PyCore`、`PyObject` 等 Zend internal classes。 |
|
||||||
- 首次实际使用 Python 符号时,TypePHP 通过 class map/func map 解析 `PyCore` 和对应的 `zend_function*`。 |
|
||||||
- phpy 未加载时,Zend class lookup 抛出可捕获的 PHP `Error`;若未捕获,则按普通 PHP 规则成为 fatal error。 |
|
||||||
- phpy 已加载但 Python module 不存在时,`PyCore::import()` 通过 phpy 抛出 `PyError`。 |
|
||||||
- 只有 `use python\sys` 而没有实际访问任何 Python 符号时,不发生运行时解析,因此即使没有安装 phpy 也不会报错。 |
|
||||||
|
|
||||||
这种模型使同一个 TypePHP 二进制可以在未安装 phpy 的环境中运行不涉及 Python 的路径,也避免 TypePHP 与 phpy 建立原生 C++ ABI 依赖。 |
|
||||||
|
|
||||||
### 4.1 TypePHP 代码隔离 |
|
||||||
|
|
||||||
TypePHP 中所有 Python 专用实现必须集中到独立子目录,暂定为: |
|
||||||
|
|
||||||
```text |
|
||||||
src/Python/ |
|
||||||
``` |
|
||||||
|
|
||||||
该目录负责: |
|
||||||
|
|
||||||
- `python` 特殊根命名空间识别。 |
|
||||||
- import/module symbol 表。 |
|
||||||
- Python Zend class/method 名称和逻辑返回类型映射。 |
|
||||||
- Python 语法糖和静态返回类型映射。 |
|
||||||
- Python 调用、属性、下标、迭代和运算符的 C++ lowering。 |
|
||||||
- Python 专用诊断。 |
|
||||||
|
|
||||||
通用 Parser、TypeSystem、Optimizer 和 Generator 只允许保留最小、稳定的扩展入口,不应散落 `if ($isPython...)` 特判。Python 功能未启用时,不加载 Python 专用分析器,也不改变现有代码生成路径。 |
|
||||||
|
|
||||||
测试同样独立组织,建议使用: |
|
||||||
|
|
||||||
```text |
|
||||||
phpunit/src/Python/ |
|
||||||
phpunit/code/python/ |
|
||||||
tests/compiler/python/ |
|
||||||
``` |
|
||||||
|
|
||||||
具体目录名在 coding 计划阶段确认,但“实现与测试隔离”是设计约束。 |
|
||||||
|
|
||||||
## 5. 总体运行时模型 |
|
||||||
|
|
||||||
采用以下模型: |
|
||||||
|
|
||||||
- 一个进程内同时存在一个 ZendVM 和一个 CPython 主解释器。 |
|
||||||
- CPython 完全通过 phpy 已有的扩展生命周期初始化和关闭;TypePHP 不建立第二套初始化路径。 |
|
||||||
- 所有 Python API 边界自动获取 GIL,普通用户不操作 GIL。 |
|
||||||
- `PyObject` 及其 `PyDict`、`PyList`、`PyStr` 等子类持有 CPython strong reference。 |
|
||||||
- Python 代理对象复制时增加引用计数,析构时在合法的解释器/GIL 上下文中减少引用计数。 |
|
||||||
- borrowed reference 只允许存在于 phpy 内部的短生命周期作用域,不暴露给 TypePHP。 |
|
||||||
- TypePHP 调用 Python、Python 同步回调由 TypePHP 作为参数传入的 callable、该 callable 再调用 Python,必须支持同步重入。 |
|
||||||
- Python 不能独立导入 TypePHP 应用,也不能通过全局注册表查找 TypePHP 函数或类型。 |
|
||||||
|
|
||||||
解释器关闭前必须先释放所有由 TypePHP 持有的 Python 对象。不能依赖 `Py_Finalize()` 自动修复错误的生命周期。 |
|
||||||
|
|
||||||
## 6. 模块名称与导入语法 |
|
||||||
|
|
||||||
`python` 是编译器识别的保留根命名空间: |
|
||||||
|
|
||||||
```php |
|
||||||
python\math\sqrt(16); |
|
||||||
python\os\path\join('/tmp', 'file.txt'); |
|
||||||
|
|
||||||
use python\sys; |
|
||||||
use Python\numpy as np; |
|
||||||
use python\numpy\linalg as linalg; |
|
||||||
``` |
|
||||||
|
|
||||||
完整名称不要求先写 `use`: |
|
||||||
|
|
||||||
```php |
|
||||||
$root = python\math\sqrt(16); |
|
||||||
$pi = Python\math\pi; |
|
||||||
``` |
|
||||||
|
|
||||||
最后一个 `\` 之前、Python 根之后的所有片段均构成 Python module path,最后一个片段是模块成员。PHP 的 `\` 在导入时转换为 Python 的 `.`。因此全局命名空间中的 `python\os\path\join()` 明确表示 module `os.path` 的 `join` callable。 |
|
||||||
|
|
||||||
Python module 名称仍严格服从 PHP 的 namespace 解析规则。位于普通 PHP namespace 内时,完整模块名必须使用前导 `\`: |
|
||||||
|
|
||||||
```php |
|
||||||
namespace App; |
|
||||||
|
|
||||||
\python\math\sqrt(16); // Python module math |
|
||||||
python\math\sqrt(16); // 普通 PHP 名称 App\python\math\sqrt,不是 Python module |
|
||||||
``` |
|
||||||
|
|
||||||
这是 PHP 语法的一部分,`python` 不作为例外绕过当前 namespace。`use python\math;` 与其他 PHP `use` 声明一样从根名称导入,因此在 namespace 内也可以使用 alias 简写。 |
|
||||||
|
|
||||||
`use` 仅用于缩短完整名称,不是访问 Python module 的前置条件: |
|
||||||
|
|
||||||
PHP 的 `use function` 和 `use const` 同样适用,并支持普通的 `as` alias: |
|
||||||
|
|
||||||
```php |
|
||||||
use function python\len; |
|
||||||
use function python\math\sqrt as py_sqrt; |
|
||||||
use const python\math\pi as py_pi; |
|
||||||
|
|
||||||
$length = len([1, 2, 3]); |
|
||||||
$root = py_sqrt(16); |
|
||||||
$pi = py_pi; |
|
||||||
``` |
|
||||||
|
|
||||||
这些声明仍完全由 PHP 名称解析处理。TypePHP 只在 `FuncCall` 或 `ConstFetch` 的最终完整名称位于根命名空间 `python\...` 时进入 Python lowering;`use` 声明本身不会导入 Python module。 |
|
||||||
|
|
||||||
分别等价于: |
|
||||||
|
|
||||||
```python |
|
||||||
import sys |
|
||||||
import numpy as np |
|
||||||
import numpy.linalg as linalg |
|
||||||
``` |
|
||||||
|
|
||||||
在现有 phpy PHP API 中,语义上对应: |
|
||||||
|
|
||||||
```php |
|
||||||
$sys = PyCore::import('sys'); |
|
||||||
$np = PyCore::import('numpy'); |
|
||||||
$linalg = PyCore::import('numpy.linalg'); |
|
||||||
``` |
|
||||||
|
|
||||||
`PyCore::import()` 返回一个 `PyModule`/`PyObject` 变量,后续属性和方法均通过该变量访问。`use python\module` 只是普通 PHP namespace alias,不立即执行导入,也不生成 ZendVM class、namespace 或用户可见变量。编译器仅在处理函数调用或常量读取时检查 PHP 已解析的完整名称。 |
|
||||||
|
|
||||||
当编译器在函数代码中发现 `module\attr` 或 `module\func()` 时,采用与现有 `funcMap` 相同的编译器结构:为实际使用的完整 module 名称分配整数 ID,生成统一的 `THREAD_LOCAL` zval array,并通过 lazy getter 动态调用 `PyCore::import()`。下列名称只是设计示意: |
|
||||||
|
|
||||||
```cpp |
|
||||||
THREAD_LOCAL zval php_python_module_map[module_count]; |
|
||||||
|
|
||||||
php::Object php_get_python_module(int module_id, const php::Str &module_name) |
|
||||||
{ |
|
||||||
zval *module = &php_python_module_map[module_id]; |
|
||||||
if (UNEXPECTED(Z_ISUNDEF_P(module))) { |
|
||||||
// Resolve PyCore::import through classMap/funcMap and invoke zend_function*. |
|
||||||
php::Variant value = php::call(/* cached zend_function* */, php::ArgList{module_name}); |
|
||||||
ZVAL_COPY(module, value.ptr()); |
|
||||||
} |
|
||||||
return php::Object(module); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
对应 lowering: |
|
||||||
|
|
||||||
```text |
|
||||||
use Python\numpy as np |
|
||||||
-> compile-time namespace marker: np => "numpy" |
|
||||||
-> module id allocated only when np is actually referenced |
|
||||||
|
|
||||||
np\version |
|
||||||
-> php::Object(php_get_python_module(module_id, "numpy")).attr("version") |
|
||||||
|
|
||||||
np\array($value) |
|
||||||
-> php::Object(php_get_python_module(module_id, "numpy")).call("array", converted($value)) |
|
||||||
|
|
||||||
python\numpy\array($value) |
|
||||||
-> the same module id and lowering as np\array($value) |
|
||||||
|
|
||||||
python\os\path\join($left, $right) |
|
||||||
-> php::Object(php_get_python_module(module_id, "os.path")).call("join", ...) |
|
||||||
``` |
|
||||||
|
|
||||||
同一完整 module 名称在整个 TypePHP 构建中只分配一个 ID;完整名称和任意 `use` 别名引用同一 module 时也共享该 ID。如果当前 `.php` 文件只有 `use python\sys`,但没有出现任何 `sys\attr`、`sys\func()` 或其他 `sys` 符号访问,则编译器不为它分配 module ID,运行时不调用 `import('sys')`,也不会因为 Python 环境缺少该 module 而报错。 |
|
||||||
|
|
||||||
未使用 module 不触发任何 phpy 运行时解析。`tpc` 只检查 `use python\sys` 本身的语法和别名冲突,不检查 phpy SDK/ABI,也不增加 phpy 链接依赖。 |
|
||||||
|
|
||||||
### 6.1 与 `funcMap` 的关系 |
|
||||||
|
|
||||||
`pythonModuleMap` 复用 `funcMap` 已验证的整体模式: |
|
||||||
|
|
||||||
- 编译期使用 `完整 module 名称 → integer ID` 的 map 去重。 |
|
||||||
- 数据声明集中生成,普通 `.cc` 只引用 extern array 和 getter。 |
|
||||||
- getter 首次访问时初始化,后续通过数组直接命中。 |
|
||||||
- 只为真正出现成员访问或调用的 module 分配 ID。 |
|
||||||
- 在应用/request clean 阶段集中清理。 |
|
||||||
|
|
||||||
但是两者不能机械地使用完全相同的清理代码: |
|
||||||
|
|
||||||
- `funcMap` 保存由 Zend function table 拥有的 non-owning `zend_function*`,清理时可以直接 `memset`。 |
|
||||||
- `pythonModuleMap` 保存 phpy 返回的 Zend `PyModule` object zval,不能直接 `memset` 覆盖有效对象。 |
|
||||||
- request clean 必须逐项执行 `zval_ptr_dtor()` 并恢复为 `UNDEF`,让 phpy 自己的 Zend object destructor 处理 Python reference 和 GIL。 |
|
||||||
- import 失败时 slot 保持 `UNDEF`,不能缓存异常值或半初始化对象。 |
|
||||||
|
|
||||||
清理由 TypePHP 使用普通 Zend zval API 完成,不调用 phpy C++ 符号: |
|
||||||
|
|
||||||
```cpp |
|
||||||
for (zval &module : php_python_module_map) { |
|
||||||
if (!Z_ISUNDEF(module)) { |
|
||||||
zval_ptr_dtor(&module); |
|
||||||
ZVAL_UNDEF(&module); |
|
||||||
} |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
TypePHP 只释放 Zend object;其内部 Python 引用计数、GIL 和 error state 仍由 phpy object handler 负责。 |
|
||||||
|
|
||||||
### 6.2 `sys.modules` 仍是全局事实来源 |
|
||||||
|
|
||||||
Python import 本身就是全局的。getter 首次调用底层 import 时,CPython 从 `sys.modules` 返回已加载 module 或执行首次加载。`pythonModuleMap` 不是第二套 import 系统,只相当于 Python 文件执行 `import numpy as np` 后保存在该文件 namespace 中的绑定: |
|
||||||
|
|
||||||
```text |
|
||||||
php_get_python_module(id, "numpy") |
|
||||||
-> TypePHP request 内的 PyModule zval binding |
|
||||||
-> CPython sys.modules(全局 module identity 与加载状态) |
|
||||||
``` |
|
||||||
|
|
||||||
它避免每次函数调用都重复进入 Python import API,同时不承担包查找、加载或 reload 逻辑。即使同一个 module 被多个 TypePHP 文件以不同别名引用,只要完整 module 名称相同,就使用同一个 ID 和 `PyModule` Zend object zval。 |
|
||||||
|
|
||||||
该绑定与 Python 普通 import 一致:Python 代码之后删除或替换 `sys.modules['numpy']`,不会自动改变已经完成的 `np` 绑定;显式执行 `PyCore::import('numpy')` 则按调用当时的 `sys.modules` 状态处理。 |
|
||||||
|
|
||||||
规则: |
|
||||||
|
|
||||||
- `python` 根命名空间的大小写不敏感,`python`、`Python`、`PYTHON` 均识别为同一个语言符号。 |
|
||||||
- 只有根命名空间不区分大小写。后续模块路径、成员、方法和关键字参数名称严格区分大小写。 |
|
||||||
- 全局 namespace 中的 `python\package\module\member`,以及其他 namespace 中的 `\python\package\module\member`,是完整 module 访问,不需要 `use`,并按首次实际执行进行 lazy import。 |
|
||||||
- namespace 内没有前导 `\` 的 `python\...` 是相对 PHP 名称,必须按 PHP 规则加上当前 namespace,不能识别为 Python module。 |
|
||||||
- `use python\...` 只能导入 Python 模块。 |
|
||||||
- 是否存在该模块只能在运行时由 CPython 判断。 |
|
||||||
- 不支持 `from package import *`。 |
|
||||||
- 初版不设计单独的 `from package import name` 语法,成员统一通过模块别名访问。 |
|
||||||
- 根命名空间 `\python` 保留给语言互调用;例如 `App\python` 仍是普通 PHP namespace。 |
|
||||||
- 模块别名不能与当前文件中的 TypePHP 类、命名空间导入或其他 Python 模块别名冲突。 |
|
||||||
- 用户仍可直接调用 `PyCore::import()` 并把返回的 `PyModule` 保存到普通变量;完整名称和经 PHP 普通 `use` 解析后的名称都使用 `pythonModuleMap` lazy binding。 |
|
||||||
|
|
||||||
示例: |
|
||||||
|
|
||||||
```php |
|
||||||
python\len($value); // 正确 |
|
||||||
Python\len($value); // 正确,根命名空间大小写不同 |
|
||||||
python\Len($value); // 错误,Python builtin 名称大小写错误 |
|
||||||
Python\Len($value); // 错误 |
|
||||||
``` |
|
||||||
|
|
||||||
解析结果位于根命名空间 `\python` 时,它由 TypePHP 编译器转换为 Python 语言符号;解析为 `App\python` 等其他名称时,仍进行普通 PHP 函数或类查找。 |
|
||||||
|
|
||||||
## 7. 模块成员 |
|
||||||
|
|
||||||
Python module 在 TypePHP 中表现为 namespace,而不是 class。模块中的名称仍由 Python VM 作为属性动态解析。 |
|
||||||
|
|
||||||
### 7.1 包变量 |
|
||||||
|
|
||||||
读取 Python 包变量使用 PHP namespace constant 的语法形式 `module\name`: |
|
||||||
|
|
||||||
```php |
|
||||||
use python\math; |
|
||||||
use python\os; |
|
||||||
use python\numpy as np; |
|
||||||
|
|
||||||
$pi = math\pi; |
|
||||||
$environ = os\environ; |
|
||||||
$arrayType = np\ndarray; |
|
||||||
$directPi = python\math\pi; |
|
||||||
$text = math\pi->__str__(); |
|
||||||
``` |
|
||||||
|
|
||||||
这里使用的是 PHP 合法的 namespace constant 表达式,但 TypePHP 不会把它注册为 Zend constant,也不会进行常量折叠。编译器将每次读取 lowering 为 Python module attribute lookup,结果保持为 `PyObject`,因此可以继续调用对象方法。 |
|
||||||
|
|
||||||
不允许使用 `math::pi` 或 `math::$pi` 读取包变量;两者都是 class member 语法,会错误地把 module 表达为 class。编译器发现这类旧语法时给出有针对性的 FatalError,并提示改用 `math\pi`。 |
|
||||||
|
|
||||||
### 7.2 包函数和类构造 |
|
||||||
|
|
||||||
调用 Python 包中的 callable 使用 PHP namespace function 语法 `module\name(...)`: |
|
||||||
|
|
||||||
```php |
|
||||||
$a = np\array([1, 2, 3]); |
|
||||||
$b = np\array([4, 5, 6]); |
|
||||||
$c = np\add($a, $b); |
|
||||||
$root = python\math\sqrt(16); |
|
||||||
$joined = python\os\path\join('/tmp', 'file.txt'); |
|
||||||
``` |
|
||||||
|
|
||||||
编译器读取 module 的 `name` 属性,并调用得到的 Python 对象。该对象可以是: |
|
||||||
|
|
||||||
- Python 函数。 |
|
||||||
- Python class,此时调用执行该类的构造过程并返回实例。 |
|
||||||
- 实现 `__call__` 的其他 Python 对象。 |
|
||||||
|
|
||||||
TypePHP 不需要也不能仅根据 `np\array()` 的语法判断它是函数还是类构造;可调用性由 Python 在运行时判断。成员不存在时产生 Python `AttributeError`,成员不可调用时产生 Python `TypeError`,并统一映射为 `PyError`。 |
|
||||||
|
|
||||||
初版只支持读取模块属性。PHP namespace constant 语法本身不能作为赋值目标;需要写入时应通过 Python 对象 API 显式完成: |
|
||||||
|
|
||||||
```php |
|
||||||
$os = PyCore::import('os'); |
|
||||||
python\setattr($os, 'name', $value); |
|
||||||
``` |
|
||||||
|
|
||||||
## 8. Python 内置函数与 phpy 语法糖 |
|
||||||
|
|
||||||
`python\name()` 表示调用 Python builtins: |
|
||||||
|
|
||||||
```php |
|
||||||
python\print('hello'); // 等价于 PyCore::print('hello') |
|
||||||
$length = python\len($value)->toValue()->toInt(); |
|
||||||
$range = python\range(0, 10); |
|
||||||
$type = python\type($value); |
|
||||||
``` |
|
||||||
|
|
||||||
它不是普通 TypePHP 命名空间函数。编译器使用 class/func map 解析 `PyCore` 对应的 `zend_function*` 并动态调用,运行时语义与直接编写对应 `PyCore` 调用一致。 |
|
||||||
|
|
||||||
名称严格区分大小写。对于编译器内建映射中已知的错误名称,可以在编译期报错;其他动态 builtin lookup 失败时产生 Python `AttributeError`。 |
|
||||||
|
|
||||||
一部分名称是现有 phpy 类型构造器的语法糖,而不是直接调用同名 Python builtin: |
|
||||||
|
|
||||||
| TypePHP 语法 | 等价 phpy API | |
|
||||||
|---|---| |
|
||||||
| `python\dict($array)` | `new PyDict($array)` | |
|
||||||
| `python\list($array)` | `new PyList($array)` | |
|
||||||
| `python\tuple($array)` | `new PyTuple($array)` | |
|
||||||
| `python\set($array)` | `new PySet($array)` | |
|
||||||
| `python\str($value)` | `new PyStr($value)` | |
|
||||||
| `python\object($value)` | `new PyObject($value)` | |
|
||||||
| `python\print(...)` | `PyCore::print(...)` | |
|
||||||
| `python\scalar($value)` | `PyCore::scalar($value)` | |
|
||||||
|
|
||||||
例如: |
|
||||||
|
|
||||||
```php |
|
||||||
$dict1 = new PyDict([1, 2, 3, 4]); |
|
||||||
$dict2 = python\dict([1, 2, 3, 4]); |
|
||||||
``` |
|
||||||
|
|
||||||
二者必须具有完全相同的运行时语义。这里不能简单转发 CPython `dict([1, 2, 3, 4])`,因为原生 Python builtin 会把参数解释为 key/value pair iterable,与 `PyDict` 的 PHP array 构造规则不同。 |
|
||||||
|
|
||||||
所有语法糖的映射必须形成封闭、经过测试的表,不能仅凭函数名猜测。 |
|
||||||
|
|
||||||
该映射同时决定编译期静态类型: |
|
||||||
|
|
||||||
```php |
|
||||||
$list1 = new PyList(); |
|
||||||
$list2 = python\list(); |
|
||||||
|
|
||||||
$dict1 = new PyDict(); |
|
||||||
$dict2 = python\dict(); |
|
||||||
``` |
|
||||||
|
|
||||||
- `$list1` 与 `$list2` 都是 `PyList` typed object。 |
|
||||||
- `$dict1` 与 `$dict2` 都是 `PyDict` typed object。 |
|
||||||
- 两种写法必须使用相同的类型检查、方法解析和 Native Call 优化。 |
|
||||||
- 语法糖不能退化成 `mixed`、`var` 或只有基础类型 `PyObject`。 |
|
||||||
- Python builtin 调用同样遵守对象保持规则,例如 `python\len()` 返回包装 Python int 的 `PyObject`;需要先以 `toValue()`(或函数入口 `python\scalar()`)离开 Python 对象规则,再使用普通 TypePHP 转换得到确定类型。`python\print()` 的 Python `None` 结果也保持为 `PyObject`,作为独立语句使用时可直接丢弃。 |
|
||||||
- `PyObject::toValue()` 和 `python\scalar()` 都不是普通 Python builtin 调用,而是明确要求退出 Python 类型规则的转换边界,因此返回 TypePHP `var`。 |
|
||||||
- 动态 Python module 成员调用统一返回 `PyObject`。 |
|
||||||
|
|
||||||
## 9. Python 对象类型 |
|
||||||
|
|
||||||
所有无法在编译期确定静态类型的 Python 值统一表示为: |
|
||||||
|
|
||||||
```php |
|
||||||
PyObject |
|
||||||
``` |
|
||||||
|
|
||||||
`PyObject` 是现有 phpy 的公开类型,也是 TypePHP 的正式运行时类型。不会再引入 `python\Object` 或 `python\Any`。 |
|
||||||
|
|
||||||
Python 内建类型继续使用 phpy 已有的具体代理类,例如 `PyDict`、`PyList`、`PyTuple`、`PySet`、`PyStr`、`PyType`、`PyFn` 和 `PyIter`。这样普通 PHP 与 TypePHP 用户看到的是同一套类型体系。 |
|
||||||
|
|
||||||
Python 的 `None` 也是一个合法 Python 对象。它与 TypePHP `null` 的自动转换规则需要单独定义,不能通过空指针表示 Python `None`。 |
|
||||||
|
|
||||||
## 10. 对象操作 |
|
||||||
|
|
||||||
### 10.1 属性和方法 |
|
||||||
|
|
||||||
```php |
|
||||||
$env = os\environ; |
|
||||||
$items = $env->items(); |
|
||||||
$name = $object->name; |
|
||||||
$object->name = 'new value'; |
|
||||||
unset($object->name); |
|
||||||
``` |
|
||||||
|
|
||||||
分别映射为 Python 的 `getattr`、call、`setattr` 和 `delattr` 协议。 |
|
||||||
|
|
||||||
`PyObject` 明确提供 `toValue()` 和 `toArray()` 两个 PHP Facade 方法。`toValue()` 等价于 `PyCore::scalar()` / `python\scalar()`,把 Python 值递归转换为 PHP 内置值。其返回值再使用普通 TypePHP 转换方法确定类型: |
|
||||||
|
|
||||||
```php |
|
||||||
$pyValue = np\int64(42); // PyObject |
|
||||||
$value = $pyValue->toValue()->toInt(); // TypePHP int |
|
||||||
``` |
|
||||||
|
|
||||||
这里的 `toInt()` 作用于 `toValue()` 已返回的 TypePHP 值,并非作用于 `PyObject`。 |
|
||||||
|
|
||||||
`toArray()` 仅转换 Python `list`、`tuple`、`set`、`dict` 以及 iterator。容器元素递归转换为 PHP 值;iterator 会被消费,后续再次转换只能得到其剩余元素。不支持转换的 Python 类型返回空数组。`toArray()` 同时是 TypePHP 关键词方法,但 PHPX 的对象转换路径会调用 `PyObject::toArray()`;`toString()` 则继续通过关键词方法调用 `PyObject::__toString()`,phpy 不重复声明 `toString()`。 |
|
||||||
|
|
||||||
### 10.2 下标 |
|
||||||
|
|
||||||
```php |
|
||||||
$value = $object[$key]; |
|
||||||
$object[$key] = $value; |
|
||||||
unset($object[$key]); |
|
||||||
isset($object[$key]); |
|
||||||
``` |
|
||||||
|
|
||||||
分别映射到 Python mapping/sequence protocol。 |
|
||||||
|
|
||||||
`isset()` 保持 PHP 的空值语义:键或索引不存在时返回 `false`,对应值为 Python `None` 时也返回 `false`。运行时只把 `KeyError` / `IndexError` 识别为“缺失”;Python protocol 抛出的其他异常必须继续映射为 `PyError`,不得被 `isset()` 吞掉。list 和 tuple 的整数下标遵循 Python 负索引规则。 |
|
||||||
|
|
||||||
### 10.3 调用对象 |
|
||||||
|
|
||||||
```php |
|
||||||
$result = $callable($arg1, $arg2); |
|
||||||
``` |
|
||||||
|
|
||||||
运行时使用 `PyObject_Call`。不可调用对象产生 Python `TypeError`,并映射为 TypePHP 可捕获的 Python 异常。 |
|
||||||
|
|
||||||
### 10.4 迭代 |
|
||||||
|
|
||||||
```php |
|
||||||
foreach ($pythonIterable as $value) { |
|
||||||
// Python __iter__ / __next__ |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
带 key 的形式: |
|
||||||
|
|
||||||
```php |
|
||||||
foreach ($pythonIterable as $index => $value) { |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
通用 Python iterator 使用从 `0` 开始的 TypePHP 迭代序号作为 `$index`,`$value` 是 `__next__()` 产出的对象。`PyDict` 是 phpy 的专用 mapping wrapper,带 key 的 `foreach` 使用 PHP mapping 习惯:`$index` 是 dict key,`$value` 是对应 dict value。`__iter__()` / `__next__()` 的 Python 异常必须传播为 `PyError`,不能当作正常迭代结束。 |
|
||||||
|
|
||||||
## 11. 参数与关键字参数 |
|
||||||
|
|
||||||
普通参数按从左到右顺序求值,然后构造 Python positional args: |
|
||||||
|
|
||||||
```php |
|
||||||
$model = AutoModel\from_pretrained( |
|
||||||
'model-name', |
|
||||||
trust_remote_code: true, |
|
||||||
device_map: 'auto', |
|
||||||
); |
|
||||||
``` |
|
||||||
|
|
||||||
TypePHP 命名参数映射为 Python keyword arguments。参数名严格区分大小写。 |
|
||||||
|
|
||||||
PHP/TypePHP 数组展开规则可用于构造位置参数和关键字参数,但必须满足: |
|
||||||
|
|
||||||
- 整数 key 生成 positional argument。 |
|
||||||
- 字符串 key 生成 keyword argument。 |
|
||||||
- positional argument 不能出现在 keyword argument 之后。 |
|
||||||
- 重复 keyword 产生 Python `TypeError`。 |
|
||||||
|
|
||||||
是否增加显式的 `python\args()` / `python\kwargs()` 类型,留待后续讨论;初版尽量复用现有调用和数组展开语法。 |
|
||||||
|
|
||||||
## 12. 显式转换原则 |
|
||||||
|
|
||||||
TypePHP 不继承 phpy 在 ZendVM Facade/opcode 层面的返回值隐式转换行为。语言层采用“参数进入 Python 边界时自动转换、Python 返回值保持对象、返回 TypePHP 时显式转换”的原则。 |
|
||||||
|
|
||||||
允许自动转换的场景必须由语法明确指出正在进入 Python: |
|
||||||
|
|
||||||
- `python\name(...)`。 |
|
||||||
- Python module 调用,例如 `np\array(...)`。 |
|
||||||
- `PyObject` 的方法或 callable 调用。 |
|
||||||
- 显式 Python 容器构造,例如 `new PyList(...)` 或 `python\list(...)`。 |
|
||||||
- 参数声明要求 `PyObject`、`PyDict` 等 phpy 类型。 |
|
||||||
- `PyObject` 与 TypePHP 值组成的混合运算表达式。 |
|
||||||
|
|
||||||
在这些调用边界内,所有参数表达式先严格按照 TypePHP 从左到右的顺序求值,再转换为 Python 能接受的对象。TypePHP 标量转换为对应 Python scalar;TypePHP 数组递归转换为 Python list/dict,这一过程会产生深拷贝。这不应扩散为不含 Python 对象的普通 TypePHP 表达式中的全局隐式转换。 |
|
||||||
|
|
||||||
“所有参数自动转换”只适用于转换表明确支持的 TypePHP 类型;resource 或其他没有 Python 表示形式的值必须抛出清晰的类型错误,不能静默转换或传递无效指针。 |
|
||||||
|
|
||||||
以下场景不允许隐式转换: |
|
||||||
|
|
||||||
- 将 `PyObject` 直接赋给 `int`、`float`、`bool`、`string` 或 `array`。 |
|
||||||
- 将 Python 容器隐式深拷贝成 TypePHP array。 |
|
||||||
- 因算术、比较或字符串上下文而擅自把 Python 对象变成 TypePHP 标量。 |
|
||||||
- 根据运行时 Python 类型改变 TypePHP 变量的静态类型。 |
|
||||||
|
|
||||||
`echo $pyObject` 可继续兼容现有 `PyObject::__toString()`,但这只属于输出协议,不能被编译器当作一般的字符串隐式转换。 |
|
||||||
|
|
||||||
## 13. TypePHP 到 Python 的转换 |
|
||||||
|
|
||||||
Python 调用边界允许以下自动转换: |
|
||||||
|
|
||||||
| TypePHP | Python | 语义 | |
|
||||||
|---|---|---| |
|
||||||
| `null` | `None` | 单例,不是空 `PyObject*` | |
|
||||||
| `bool` | `bool` | 值转换 | |
|
||||||
| `int` | `int` | Python 任意精度整数 | |
|
||||||
| `float` | `float` | double | |
|
||||||
| `string` | `str` | 要求合法 UTF-8 | |
|
||||||
| list array | `list` | 递归复制 | |
|
||||||
| map array | `dict` | 递归复制 | |
|
||||||
| `PyObject` 及其子类 | 原对象 | 零拷贝,只传递引用 | |
|
||||||
| TypePHP callable | Python callable proxy | Python 可同步回调 TypePHP | |
|
||||||
| TypePHP object | Zend object proxy | 不自动复制对象属性 | |
|
||||||
|
|
||||||
PHP array 使用 `zend_array_is_list()` 一类规则决定转换为 Python `list` 还是 `dict`。空数组默认转换为 Python `list`;如需空 dict,必须提供显式构造 API。 |
|
||||||
|
|
||||||
数组和普通 TypePHP 字符串每次进入 Python 边界都可能产生分配与复制。文档和性能诊断应建议高频调用、循环调用或大数据场景尽早构造并复用 `PyDict`、`PyList`、`PyStr` 等原生 Python 代理类型,避免重复深拷贝。`PyObject` 及其子类进入 Python 边界时只传递原对象引用,不做内容复制。 |
|
||||||
|
|
||||||
推荐写法: |
|
||||||
|
|
||||||
```php |
|
||||||
// 只转换一次,后续调用传递同一个 Python 对象。 |
|
||||||
use python\processor; |
|
||||||
|
|
||||||
$pyItems = python\list($items); |
|
||||||
for ($i = 0; $i < 1000; $i++) { |
|
||||||
processor\consume($pyItems); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
应避免在循环中反复把同一个 TypePHP 容器作为参数传入,因为每次跨越 Python 调用边界都会重新深拷贝: |
|
||||||
|
|
||||||
```php |
|
||||||
for ($i = 0; $i < 1000; $i++) { |
|
||||||
processor\consume($items); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
字符串与 bytes 必须区分。TypePHP `string` 默认映射到 Python `str`;二进制内容使用显式 `python\bytes()`。 |
|
||||||
|
|
||||||
递归数组、循环引用和超深嵌套必须检测并抛出异常,不能无限递归。 |
|
||||||
|
|
||||||
## 14. Python 到 TypePHP 的转换 |
|
||||||
|
|
||||||
### 14.1 默认规则 |
|
||||||
|
|
||||||
TypePHP 的 Python 专用调用路径必须关闭 phpy 的返回值隐式转换,所有 Python 函数、方法、构造调用和运算结果均保持为 phpy 对象。动态调用的静态返回类型统一为 `PyObject`,不能因为运行时结果恰好是 Python `bool`、`int`、`float`、`str`、`list` 或 `dict` 就隐式转换为 TypePHP 值。 |
|
||||||
|
|
||||||
当前实现由生成代码在首次实际执行 Python 表达式时,动态调用 `PyCore::setOptions(['return_as_object' => true])`。该初始化是请求级 lazy guard:只写 `use python\module` 而不访问 Python 符号不会触发 phpy;constructor-only 程序也会在构造前完成配置;request clean 会重置 TypePHP 自身的 guard。后续若 phpy 提供无全局模式的对象保持型独立入口,可在不改变语言语义的前提下替换这一运行时实现。 |
|
||||||
|
|
||||||
编译器已知的 phpy 构造语法糖仍保留精确子类,例如 `python\list()` 返回 `PyList`、`python\dict()` 返回 `PyDict`;这些类型本身都是 `PyObject` 子类,不构成返回值隐式转换。 |
|
||||||
|
|
||||||
phpy Zend Facade 应提供相互独立的“保持 Python 对象”和“显式转换为 TypePHP”入口。不能通过修改进程级全局函数指针或全局转换模式来临时切换,否则嵌套调用、同步重入和异常路径可能把错误策略泄漏给后续调用。TypePHP 生成的普通 Python 调用只动态调用对象保持入口;`PyObject::toValue()` 与 `python\scalar()` 最终都调用明确的标量转换入口。 |
|
||||||
|
|
||||||
phpy 内部已使用 `PythonToPhpConverter` 与 `PhpToPythonConverter` 实现这一约束。每次顶层转换拥有独立实例,递归子值复用同一实例;容器进入与退出由 RAII guard 管理,循环容器和超过深度限制的输入会抛出 `PyError`,不会污染后续转换或导致进程崩溃。 |
|
||||||
|
|
||||||
原因: |
|
||||||
|
|
||||||
- 保留 Python 对象身份和精确类型。 |
|
||||||
- 避免容器返回时立即深拷贝。 |
|
||||||
- Python `int` 可能超过 TypePHP `int` 范围。 |
|
||||||
- Python 类型的子类可能重载协议,不能按基础容器强制展开。 |
|
||||||
- 避免 phpy 当前“部分标量自动转换、部分对象保留包装”的行为进入 TypePHP 静态类型系统。 |
|
||||||
|
|
||||||
### 14.2 显式转换 |
|
||||||
|
|
||||||
Python 对象只有通过 `toValue()`、`python\scalar()`(或手写等价的 `PyCore::scalar()`)才能进入 TypePHP 类型规则: |
|
||||||
|
|
||||||
```php |
|
||||||
$nativeValue1 = PyCore::scalar($value); |
|
||||||
$nativeValue2 = python\scalar($value); // 完全等价的语法糖 |
|
||||||
$nativeValue3 = $value->toValue(); |
|
||||||
$integer = $value->toValue()->toInt(); |
|
||||||
$float = $value->toValue()->toFloat(); |
|
||||||
$boolean = $value->toValue()->toBool(); |
|
||||||
$string = $value->toValue()->toString(); |
|
||||||
$array = $value->toArray(); |
|
||||||
``` |
|
||||||
|
|
||||||
规则: |
|
||||||
|
|
||||||
- `toValue()` 是 `PyObject` 的普通公开方法,不注册为 TypePHP 关键词方法;它在 phpy 内部复用与 `PyCore::scalar()` 相同的转换器。 |
|
||||||
- `toArray()` 保留 TypePHP 全局关键词方法语义。PHPX 对对象执行数组转换时优先调用其公开的 `toArray()`,因此会进入 phpy 实现。 |
|
||||||
- 显式转换完成后,结果完全进入 TypePHP 的静态类型、运算符和参数传递规则,不再采用 Python protocol。 |
|
||||||
- 容器转换属于显式深转换,并检测递归引用。 |
|
||||||
- Python 大整数不能静默溢出;现有转换规则需要 review 后再确定与 TypePHP `BigInt` 的精确映射。 |
|
||||||
- Python `str` 与 `bytes` 必须区分,不能都无条件转换为 TypePHP string。 |
|
||||||
- phpy 负责 `PyObject::toValue()` / `PyCore::scalar()` 的通用值转换,以及受限的 `PyObject::toArray()` 容器转换。`toInt/toFloat/toBool` 属于转换后的 PHP 值;`toString()` 仍由 TypePHP 关键词方法调用 `PyObject::__toString()`。 |
|
||||||
|
|
||||||
现有 phpy 的 PHP 用户仍可保留兼容行为;TypePHP 调用 phpy 的对象保持型 Zend API。为此可以重构或新增 phpy internal class method,但不增加 TypePHP 到 phpy 的 C++ 链接依赖。 |
|
||||||
|
|
||||||
## 15. 通过 Python `operator` module 实现运算符 |
|
||||||
|
|
||||||
对于 `PyObject` 及其子类: |
|
||||||
|
|
||||||
- `+ - * / % ** << >> & | ^` 映射为 Python 标准库 `operator` module 的对应函数。 |
|
||||||
- `/` 映射 `operator.truediv()`,不能映射 `operator.floordiv()`。 |
|
||||||
- Python floor division 暂用 `python\floordiv($a, $b)`,因为 TypePHP 没有 `//` 运算符。 |
|
||||||
- `== != < <= > >=` 分别映射 `operator.eq/ne/lt/le/gt/ge()`。 |
|
||||||
- `===` / `!==` 分别映射 `operator.is_()` / `operator.is_not()`。 |
|
||||||
- `if ($object)`、`!$object` 使用 `operator.truth()`。 |
|
||||||
- compound assignment 映射 `operator.iadd/isub/...()`,并用返回对象更新左值。 |
|
||||||
|
|
||||||
基础映射: |
|
||||||
|
|
||||||
| TypePHP | 生成的动态调用 | |
|
||||||
|---|---| |
|
||||||
| `$a + $b` | `operator\add($a, $b)` | |
|
||||||
| `$a - $b` | `operator\sub($a, $b)` | |
|
||||||
| `$a * $b` | `operator\mul($a, $b)` | |
|
||||||
| `$a / $b` | `operator\truediv($a, $b)` | |
|
||||||
| `$a % $b` | `operator\mod($a, $b)` | |
|
||||||
| `$a ** $b` | `operator\pow($a, $b)` | |
|
||||||
| `$a << $b` | `operator\lshift($a, $b)` | |
|
||||||
| `$a >> $b` | `operator\rshift($a, $b)` | |
|
||||||
| `$a & $b` | `operator\and_($a, $b)` | |
|
||||||
| bitwise OR | `operator\or_($a, $b)` | |
|
||||||
| `$a ^ $b` | `operator\xor($a, $b)` | |
|
||||||
| `-$a` | `operator\neg($a)` | |
|
||||||
| `+$a` | `operator\pos($a)` | |
|
||||||
| `~$a` | `operator\invert($a)` | |
|
||||||
| `$a += $b` | `$a = operator\iadd($a, $b)` | |
|
||||||
|
|
||||||
所有操作数必须严格从左到右求值。 |
|
||||||
|
|
||||||
即使源码没有显式写出 `use python\operator`,出现 Python 运算符时,编译器也将其视为一个仅供内部 lowering 使用的隐式 module binding,并通过同一 `pythonModuleMap` 取得 `operator` module。它不向用户文件注入可见别名,因此不会与用户自己定义的 `operator` class 或 use alias 冲突。用户显式 `use python\operator` 时,内部 lowering 和用户访问复用同一个 module ID。 |
|
||||||
|
|
||||||
identity 比较调用 `operator\is_()` / `operator\is_not()`。即使两个对象的 `operator\eq()` 结果为真,只要不是同一个 Python object,`===` 仍为假。 |
|
||||||
|
|
||||||
允许 Python 对象与 TypePHP 值直接混合运算。只要当前运算节点的一侧静态类型为 `PyObject` 或其子类,另一侧的 TypePHP 表达式先完整地按 TypePHP 规则求值,再把所得值转换为 Python 对象,最后由 CPython 执行当前运算节点对应的 protocol。 |
|
||||||
|
|
||||||
例如: |
|
||||||
|
|
||||||
```php |
|
||||||
$result1 = $pyInt + 10; // 10 转为 Python int,由 Python 执行加法 |
|
||||||
$result2 = $pyList * getCount(); // 先求值 getCount(),再转为 Python int |
|
||||||
$native = $pyInt->toValue()->toInt() + 10; // 已显式转为 TypePHP int,使用 TypePHP 加法 |
|
||||||
``` |
|
||||||
|
|
||||||
`operator` 调用结果仍为 `PyObject`,以保留 Python 自定义运算符可能返回的任意对象。`===` / `!==` 和条件分支是例外:`operator.is_/is_not/truth()` 的 Python bool 结果随后通过显式 phpy 转换入口得到 TypePHP `bool`。两侧操作数必须严格从左到右各求值一次,转换过程不得导致表达式重复执行。 |
|
||||||
|
|
||||||
phpy 作为普通 PHP 扩展时,可以继续使用 Zend opcode handler 提供运算符重载兼容性;TypePHP 不依赖这些 handler。 |
|
||||||
|
|
||||||
动态 ZendVM 代码存在一个明确保留的限制:Zend 会把 `-$value` / `+$value` 编译为乘以 `-1` / `1`,phpy 的 opcode handler 无法再识别源码中的一元运算。因此动态代码保持 `$value * -1` / `$value * 1` 的协议行为,不通过全局 AST hook 改写普通 PHP 代码;自定义 Python 对象的 `__neg__()` / `__pos__()` 与 `__mul__()` 不一致时,结果可能不同。TypePHP AOT 仍按照上表生成 `operator.neg()` / `operator.pos()`。外部用户文档 `python.md` 已明确说明这一限制。 |
|
||||||
|
|
||||||
TypePHP 编译器在识别到静态类型为 `PyObject`、`PyDict` 等 phpy 对象时,把运算符改写为普通 Python module callable 调用: |
|
||||||
|
|
||||||
```text |
|
||||||
TypePHP operator |
|
||||||
-> compile-time lowering |
|
||||||
-> implicit python\operator module binding |
|
||||||
-> operator\add/sub/... dynamic call |
|
||||||
-> CPython complete operator protocol |
|
||||||
``` |
|
||||||
|
|
||||||
该抽象不直接链接 phpy,也不经过 phpy 的 user opcode handler,但它不是无调用成本的 C++ inline 操作: |
|
||||||
|
|
||||||
- `zend_function*` 和 class entry 使用现有 func/class map lazy cache。 |
|
||||||
- 参数仍需要构造为 Zend values,并由 phpy 转为 Python 对象。 |
|
||||||
- Python module member lookup、GIL、CPython call 和引用计数成本仍然存在。 |
|
||||||
- 优点是 TypePHP 二进制只依赖 ZendVM/PHPX,phpy 可以作为真正的可选运行时扩展。 |
|
||||||
|
|
||||||
使用标准库 `operator.add()` 而不是直接调用 `__add__()`,可以复用 CPython 对 `NotImplemented`、`__radd__()`、右操作数子类优先级等完整规则,TypePHP 不实现 reflected-operation fallback。 |
|
||||||
|
|
||||||
当前实现已经覆盖二元算术和位运算、比较、identity、一元运算、条件真假值、短路逻辑,以及 variable、属性和下标左值的复合赋值。Python module function/property、builtin、动态方法、属性、下标和 callable 的结果都会继续传播 `PyObject` 静态类型,因此可以直接链式访问或参与后续 Python 运算。 |
|
||||||
|
|
||||||
## 16. 异常 |
|
||||||
|
|
||||||
Python 调用失败时抛出统一的 TypePHP 异常类型,暂定: |
|
||||||
|
|
||||||
```php |
|
||||||
PyError |
|
||||||
``` |
|
||||||
|
|
||||||
异常至少保留: |
|
||||||
|
|
||||||
- Python exception type。 |
|
||||||
- message。 |
|
||||||
- Python traceback 对象。 |
|
||||||
- 格式化后的 traceback 字符串。 |
|
||||||
- 原始 Python exception instance。 |
|
||||||
|
|
||||||
示例: |
|
||||||
|
|
||||||
```php |
|
||||||
try { |
|
||||||
np\array('invalid')->reshape(2, 2); |
|
||||||
} catch (PyError $error) { |
|
||||||
echo $error->pythonType(); |
|
||||||
echo $error->pythonTraceback(); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
Python 同步调用 TypePHP callable 代理时,如果 TypePHP 抛出异常,应转换为普通 Python 异常,并保留原始 TypePHP 类名和消息。该异常只沿当前动态调用栈传播,不要求注册 `typephp` Python module 或专用的全局异常类型。 |
|
||||||
|
|
||||||
异常跨 VM 后必须清理源 VM 的 pending exception 状态。任何异常转换失败都不能导致 coredump、重复抛出或遗留错误状态。 |
|
||||||
|
|
||||||
## 17. TypePHP callable 传给 Python |
|
||||||
|
|
||||||
TypePHP 函数、闭包和可调用对象可以自动包装为 Python callable: |
|
||||||
|
|
||||||
```php |
|
||||||
$values = python\list([1, 2, 3]); |
|
||||||
$result = python\map(fn (int $value): int => $value * 2, $values); |
|
||||||
``` |
|
||||||
|
|
||||||
Python 调用代理时: |
|
||||||
|
|
||||||
1. Python 参数按边界规则转换或包装为 TypePHP 值。 |
|
||||||
2. 进入 ZendVM 调用 callable。 |
|
||||||
3. 返回值转换为 Python 值。 |
|
||||||
4. TypePHP 异常转换为 Python 异常。 |
|
||||||
|
|
||||||
闭包代理必须持有 Zend callable,防止 callable 在 Python 仍引用它时被释放。跨 VM 引用环必须由运行时显式检测或提供可预测的回收策略。 |
|
||||||
|
|
||||||
TypePHP callable 代理只是参数值,不是导出机制:只有 TypePHP 主动把代理传给 Python 后,Python 才能在该对象存活期间动态调用它。TypePHP 不生成可供 Python 独立导入的 module,也不注册全局函数或类。 |
|
||||||
|
|
||||||
## 18. phpy 生命周期与集成方式 |
|
||||||
|
|
||||||
TypePHP 复用 phpy 自己的 PHP 扩展入口和生命周期,不增加独立的 CPython bootstrap: |
|
||||||
|
|
||||||
1. phpy 的 `MINIT` 初始化共享运行时、CPython 以及 `PyObject`、`PyDict` 等 Zend 类。 |
|
||||||
2. phpy 的 `RINIT` 建立本次请求需要的状态。 |
|
||||||
3. TypePHP 程序在请求期间通过 phpy 注册到 ZendVM 的 internal classes、methods 和 object handlers 动态调用 Python。 |
|
||||||
4. phpy 的 `RSHUTDOWN` 释放请求级资源和代理。 |
|
||||||
5. phpy 的 `MSHUTDOWN` 在所有代理均已安全释放后关闭共享运行时和 CPython。 |
|
||||||
|
|
||||||
TypePHP 应通过与其他静态或动态链接 PHP 扩展相同的机制执行这些入口,不能重复初始化 CPython,也不能绕过 phpy 生命周期直接调用 `Py_Initialize()` 或 `Py_Finalize()`。 |
|
||||||
|
|
||||||
唯一产物是以 TypePHP 为入口的主程序或库。不会生成可被 CPython 导入的 `.so` / `.pyd`,不会向 Python 注册 TypePHP module、函数或类,也不存在 `#[PythonExport]`。 |
|
||||||
|
|
||||||
## 19. 性能原则 |
|
||||||
|
|
||||||
- `PyObject` 传参只增加必要的引用计数,不复制 Python 对象。 |
|
||||||
- `pythonModuleMap` 只缓存已经绑定的 `PyModule` Zend object zval,真实加载和全局 identity 直接复用 CPython `sys.modules`;builtin/member lookup 初版保持简单,只有基准测试证明必要时才单独设计缓存。 |
|
||||||
- 参数应直接构造 vectorcall 所需数组,优先使用 CPython vectorcall API。 |
|
||||||
- 避免先构造 PHP 数组,再由 phpy 二次转换为 Python tuple/dict。 |
|
||||||
- TypePHP 数组到 Python 容器属于显式 O(n) 转换,不宣称零成本。 |
|
||||||
- 对进入热点 Python 调用的 TypePHP 数组和字符串,应提升为可复用的 `PyList`、`PyDict`、`PyStr`;编译器不擅自缓存转换结果,因为原 TypePHP 值可能已经改变。 |
|
||||||
- GIL guard 应覆盖最小必要区域;单线程同步重入期间必须保持正确的解释器状态。 |
|
||||||
- 异常路径与正常路径必须同等测试引用计数和内存泄漏。 |
|
||||||
|
|
||||||
## 20. 永久边界与不支持能力 |
|
||||||
|
|
||||||
- Python 线程,包括 `threading` 创建线程以及任何从非主线程进入 phpy/TypePHP bridge 的调用。 |
|
||||||
- `asyncio`、Python coroutine、`async`/`await` 及跨语言事件循环调度。 |
|
||||||
- CPython subinterpreter 和 per-interpreter GIL 模式。 |
|
||||||
- Python 作为入口独立加载 TypePHP 程序。 |
|
||||||
- 生成 Python extension 或将 TypePHP 函数、类、对象注册为可导入的 Python module。 |
|
||||||
- 运行时反射生成 TypePHP 静态类型。 |
|
||||||
- 自动导入 `from module import *`。 |
|
||||||
- pickle/serialize Python 对象。 |
|
||||||
- 跨进程传递 `PyObject`。 |
|
||||||
- WASM target 中的 Python 互调用。 |
|
||||||
|
|
||||||
禁止能力必须有明确防线:编译器对能够静态识别的 `threading`、`_thread`、`asyncio` 和 subinterpreter API 给出 FatalError;phpy 记录创建运行时的 owner thread,并拒绝从其他线程进入 ZendVM bridge。动态导入、反射或第三方包不能被编译器完整识别,因此运行时检查不能省略。 |
|
||||||
|
|
||||||
第三方 native package 内部完全封闭、从不进入 CPython API 或 phpy/ZendVM bridge 的计算线程不属于这里的 Python 线程能力;它们对 TypePHP 不可见,也不得产生跨线程回调。 |
|
||||||
|
|
||||||
## 21. TDD 与测试门禁 |
|
||||||
|
|
||||||
本项目的实现和重构必须严格遵循 TDD,顺序不可颠倒: |
|
||||||
|
|
||||||
1. 根据已确认的设计语义编写测试。 |
|
||||||
2. 运行测试,确认它因为目标能力尚未实现或现有 BUG 而失败。 |
|
||||||
3. 编写使该测试通过的最小实现。 |
|
||||||
4. 运行相关测试和完整回归。 |
|
||||||
5. 在测试保护下重构、清理和优化。 |
|
||||||
6. 再次运行完整回归、内存检查和覆盖率检查。 |
|
||||||
|
|
||||||
禁止先完成实现,再补写只能验证当前实现细节的测试。每个 BUG 必须先添加能够稳定复现问题的回归测试。 |
|
||||||
|
|
||||||
### 21.1 三层强制测试 |
|
||||||
|
|
||||||
#### PHPUnit |
|
||||||
|
|
||||||
TypePHP 仓库的 PHPUnit 用于验证编译器自身: |
|
||||||
|
|
||||||
- Python import 和特殊名称解析。 |
|
||||||
- AST、符号表和类型推断。 |
|
||||||
- C++ 代码生成。 |
|
||||||
- 编译期错误和诊断位置。 |
|
||||||
- 永久禁用能力的编译期诊断,以及无 phpy 环境仍能成功生成代码。 |
|
||||||
- 不需要启动 CPython 的边界逻辑。 |
|
||||||
|
|
||||||
phpy 仓库现有 PHPUnit 用于验证 ZendVM/PHP Facade 与共享 Runtime: |
|
||||||
|
|
||||||
- `PyCore`、`PyObject`、`PyDict` 等公开 PHP API。 |
|
||||||
- PHP 值与 Python 对象转换。 |
|
||||||
- Python 异常映射为 `PyError`。 |
|
||||||
- opcode handler 与 TypePHP 使用的 Zend dynamic-call API 具有一致语义。 |
|
||||||
- TypePHP 需要的对象保持型调用路径。 |
|
||||||
- GIL、引用计数、析构和异常路径。 |
|
||||||
|
|
||||||
#### PHPT |
|
||||||
|
|
||||||
用于从 TypePHP 用户视角验证语言和运行时的端到端行为: |
|
||||||
|
|
||||||
- 导入、`module\name` 包变量读取、`module\name()` callable 调用和关键字参数。 |
|
||||||
- `use python\module as alias` 与手写 `$alias = PyCore::import('module')` 的结果、异常和对象 identity 等价。 |
|
||||||
- 多个别名、嵌套模块和跨 `.cc` 重复导入。 |
|
||||||
- 只有 `use python\module` 而未访问任何相关符号时,不生成 helper、不调用 import,也不检查该 Python module 是否存在。 |
|
||||||
- 同一完整 module 名称跨函数、跨 `.cc` 只分配一个 ID,并只在首次访问时调用 import API。 |
|
||||||
- import 失败保持 map slot 为 `UNDEF`;异常被捕获后,下一次访问可以重新尝试。 |
|
||||||
- request clean 对 module zval 逐项执行 `zval_ptr_dtor()` 并恢复为 `UNDEF`,不得直接 `memset` 有效 Zend object。 |
|
||||||
- 删除或替换 `sys.modules` 条目不会改变已经缓存的 TypePHP module binding。 |
|
||||||
- `module::name` / `module::$name` 旧 class member 语法的编译期 FatalError,以及不存在成员和不可调用成员的运行时异常。 |
|
||||||
- 属性、下标、迭代、运算符和 truthiness。 |
|
||||||
- TypePHP 参数到 Python 的转换,以及 Python 返回值的显式转换。 |
|
||||||
- 空 TypePHP 数组默认转换为 Python list,以及数组递归深拷贝、异常中止和重复转换行为。 |
|
||||||
- Python builtin、模块函数、方法和运算结果不会隐式变成 TypePHP 标量。 |
|
||||||
- `$obj->toValue()->toInt()`、`$obj->toArray()`、`python\scalar($obj)->toInt()` 等显式边界及其后的普通 TypePHP 转换恢复静态类型和运算规则。 |
|
||||||
- Python 异常到 TypePHP 异常。 |
|
||||||
- phpy 未加载时首次 Python 调用抛出 PHP `Error`,而仅声明未使用的 Python `use` 不报错。 |
|
||||||
- TypePHP callable 被 Python 回调。 |
|
||||||
- 引用计数、对象析构和重复调用。 |
|
||||||
- 编译后的真实程序输出,而不是只检查生成代码字符串。 |
|
||||||
|
|
||||||
#### pytest |
|
||||||
|
|
||||||
pytest 用于 phpy 自身已有 Python-facing bridge 的回归测试;它不表示 TypePHP 会生成 Python extension。需要验证: |
|
||||||
|
|
||||||
- Python 调用 PHP 函数、对象和 callable。 |
|
||||||
- 同步重入和 phpy module 生命周期。 |
|
||||||
- Python 对 Zend callable/object proxy 的持有、释放和异常映射。 |
|
||||||
- 永久禁止从 Python 线程进入 ZendVM 的防护。 |
|
||||||
|
|
||||||
三层测试不能互相替代。C++/GoogleTest 可以覆盖 phpy 内部的引用计数、RAII 和低层转换,但不能代替 PHPUnit、PHPT 或 pytest。 |
|
||||||
|
|
||||||
### 21.2 每项语义的测试矩阵 |
|
||||||
|
|
||||||
每个已支持能力至少考虑以下维度: |
|
||||||
|
|
||||||
- 正常路径。 |
|
||||||
- 错误类型和错误消息。 |
|
||||||
- 边界值及空值。 |
|
||||||
- Python 子类和动态协议。 |
|
||||||
- TypePHP → Python → TypePHP 重入。 |
|
||||||
- 仅由 TypePHP 发起、经 callable 代理发生的 Python → TypePHP → Python 同步重入。 |
|
||||||
- 正常析构和异常析构。 |
|
||||||
- 重复执行、`sys.modules` identity 和重复 import 不重新执行 module 代码。 |
|
||||||
- Debug、Release 以及支持的平台。 |
|
||||||
|
|
||||||
转换测试必须包含: |
|
||||||
|
|
||||||
- `PHP_INT_MIN/PHP_INT_MAX` 及超出范围的 Python int。 |
|
||||||
- `NaN`、`INF`、`-INF` 和负零。 |
|
||||||
- 空字符串、Unicode、无效 UTF-8、内含 NUL 的 bytes。 |
|
||||||
- 空 list/dict、混合 key、深层容器、递归容器和循环引用。 |
|
||||||
- 同一 Python 对象经多次包装后的 identity。 |
|
||||||
|
|
||||||
### 21.3 内存与稳定性测试 |
|
||||||
|
|
||||||
涉及 `PyObject*` 或 `zval` 所有权的修改,除功能测试外还必须执行: |
|
||||||
|
|
||||||
- PHP memory leak report。 |
|
||||||
- Python debug build/refcount 检查(环境可用时)。 |
|
||||||
- ASan/UBSan 构建。 |
|
||||||
- 异常注入测试,覆盖每一个可能提前返回的分支。 |
|
||||||
- 循环创建和销毁对象的压力测试。 |
|
||||||
- 进程退出时仍存在跨 VM 代理对象的测试。 |
|
||||||
|
|
||||||
不允许把 coredump、泄漏或未清理的 pending exception 标记为“预期行为”来绕过测试。 |
|
||||||
|
|
||||||
### 21.4 覆盖率要求 |
|
||||||
|
|
||||||
- 设计文档中每一条规范性行为都必须能够对应到至少一个测试。 |
|
||||||
- 新增和修改的桥接代码需要覆盖正常分支与错误分支。 |
|
||||||
- 项目整体覆盖率不得因本功能下降。 |
|
||||||
- 对 GIL、引用计数、异常和析构代码,不能只依赖行覆盖率,必须人工检查分支矩阵。 |
|
||||||
- 最终 coding 计划必须先列出测试清单,再列实现任务。 |
|
||||||
|
|
||||||
## 22. 已确认与待确认问题 |
|
||||||
|
|
||||||
已确认: |
|
||||||
|
|
||||||
1. Python 互调用是可选的扩展级特性;TypePHP 不链接或在编译期检查 `libphpy.so`,首次实际调用时若 phpy 未加载则由 Zend 抛出 PHP `Error`。 |
|
||||||
2. TypePHP 尽可能采用显式转换,不继承 phpy 的全部隐式转换行为。 |
|
||||||
3. TypePHP 运算符在编译期改写为 `operator\add($left, $right)` 一类 Python 标准库调用,不使用 phpy opcode handler,也不生成 phpy C++ 符号调用。 |
|
||||||
4. `python` 根命名空间大小写不敏感,其后的所有 Python 符号大小写敏感。 |
|
||||||
5. `python` 是编译器处理的特殊语言命名空间。 |
|
||||||
6. 运行时类继续使用 `PyObject`、`PyDict` 等 phpy 公开名称。 |
|
||||||
7. `python\dict()` 等构造语法是现有 phpy 类构造器的语法糖;`python\print()` 等是 `PyCore` API 的语法糖。 |
|
||||||
8. `new PyList()` 与 `python\list()` 具有相同的 `PyList` typed object 类型和优化能力。 |
|
||||||
9. phpy 解决运行时问题,TypePHP 只通过缓存的 `zend_function*` 和 PHPX/Zend 通用对象 API 动态调用 phpy Facade。 |
|
||||||
10. TypePHP 的 Python 专用实现与测试放入独立子目录,通过受控入口接入通用编译流程。 |
|
||||||
11. Python 线程、`asyncio` 和 subinterpreter 永久禁止,且不作为后续兼容目标。 |
|
||||||
12. `===` / `!==` 分别映射 Python identity 的 `is` / `is not`;`==` / `!=` 使用 Python 值比较。 |
|
||||||
13. 仅支持 TypePHP 主动调用 Python;不生成 Python extension,不提供 `#[PythonExport]`,不向 Python 注册 TypePHP 符号。 |
|
||||||
14. CPython 和 bridge 生命周期完全复用 phpy 的 `MINIT/RINIT/RSHUTDOWN/MSHUTDOWN` 入口。 |
|
||||||
15. Python 包变量使用 PHP namespace constant 语法 `math\pi` 读取,但在运行时执行动态 Python attribute lookup;`math::pi` 和 `math::$pi` 是错误的 class member 表达法。 |
|
||||||
16. `np\array()` 表示读取并调用 Python 包成员;该成员可以是函数、class 或其他 callable,具体类型由 Python 运行时决定。 |
|
||||||
17. `PyObject` 可以与 TypePHP 值混合运算;TypePHP 操作数转换为 Python 对象后,整个运算由 CPython protocol 执行,结果保持为 `PyObject`。 |
|
||||||
18. Python 函数、方法、class 构造和 builtin 调用的结果一律保持为 `PyObject` 或已知的 phpy 子类;禁用 phpy 返回值隐式转换。 |
|
||||||
19. `PyObject::toValue()` 是显式标量/容器转换方法,等价于 `python\scalar()`;`PyObject::toArray()` 只接受可转换容器和 iterator,不支持的类型返回空数组。转换后可继续使用普通 TypePHP 转换,例如 `$obj->toValue()->toInt()`。 |
|
||||||
20. TypePHP 调用 Python 时,所有参数自动转换为 Python 类型;TypePHP 数组递归深拷贝,空数组默认转换为 Python list。 |
|
||||||
21. 性能敏感代码应复用 `PyDict`、`PyList`、`PyStr` 等代理对象,避免同一 TypePHP 值反复转换和深拷贝。 |
|
||||||
22. TypePHP 的主要语言增量是 `use python\...` 和模块别名;使用别名时通过与 `funcMap` 同类的 lazy indexed map 调用 phpy import,其他运行时能力优先直接复用 phpy。 |
|
||||||
23. `use python\module` 完全交由 PHP namespace 解析处理;当前 `.php` 文件没有实际访问解析到该 module 的符号时,不生成 helper,也不执行运行时 import。 |
|
||||||
24. 发现 `module\attr` 或 `module\func()` 时,才为完整 module 名称分配 ID;未使用的 `use` 不占 map slot,也不执行 import。 |
|
||||||
25. `pythonModuleMap` 与 `funcMap` 一样集中声明、按 ID lazy lookup;区别是 module 保存为拥有引用的 Zend object zval,必须在 request clean 中逐项 `zval_ptr_dtor()` 并恢复为 `UNDEF`。 |
|
||||||
26. `sys.modules` 负责全局加载状态和 identity,`pythonModuleMap` 只表示 TypePHP 已经完成并缓存的 module binding。 |
|
||||||
27. TypePHP 生成代码只依赖 PHPX/ZendVM;`PyCore::import()`、builtin、对象方法和转换均解析为 `zend_function*` 动态调用。 |
|
||||||
28. Python 运算符隐式使用 `python\operator` module;完整运算协议由 CPython `operator` 函数处理,不直接调用 dunder,也不由 TypePHP 实现 reflected fallback。 |
|
||||||
|
|
||||||
模块 namespace attribute 初版只读。PHP namespace constant 表达式不能作为赋值目标;后续若增加写入能力,应采用显式 API,并在确定语义后先补测试。 |
|
||||||
@ -1,103 +0,0 @@ |
|||||||
# TypePHP Python 互调用分阶段实施计划 |
|
||||||
|
|
||||||
> 本计划以 `python/design.md` 为规范。每个阶段严格执行:先增加 PHPUnit/PHPT/pytest 测试并确认失败,再实现,再运行相关测试和完整回归。 |
|
||||||
|
|
||||||
## 阶段 1:Python module name、use 与 lazy binding |
|
||||||
|
|
||||||
目标是完成最小可运行闭环,不实现运算符和通用转换: |
|
||||||
|
|
||||||
1. 识别全局 namespace 中的 `python\module\member()` / `python\module\member`、其他 namespace 中的 `\python\module\member()` / `\python\module\member`、可选的 `use python\module` 简写、根名称大小写不敏感和 Python 后续名称大小写敏感。 |
|
||||||
2. 不建立 Python 专用 alias 表,也不特殊处理 `use`;使用 PHP 的普通 namespace、`use function`、`use const`、`as` alias、冲突检查和完整名称解析。在 namespace 内必须写成 `\python\module`,相对的 `python\module` 仍解析为当前 PHP namespace 下的名称。 |
|
||||||
3. 仅在出现 `python\module\attr`、`python\module\func()` 或其 alias 形式时分配 module ID。 |
|
||||||
4. 生成与 `funcMap` 同类的 `pythonModuleMap`、lazy getter 和 request-clean 代码。 |
|
||||||
5. 使用 Zend class/function map 动态调用 `PyCore::import()`;不 include、link 或检测 phpy。 |
|
||||||
6. 使用 Zend object API 读取 module 属性及调用 module callable。 |
|
||||||
7. phpy 未加载时在首次实际使用处抛出 PHP `Error`;未使用的 Python use 不触发错误。 |
|
||||||
|
|
||||||
测试顺序:PHPUnit 代码生成与诊断测试 → PHPT 运行时测试 → 现有 compiler 回归。 |
|
||||||
|
|
||||||
实现状态:已完成。完整名称与任意 alias 按 Python dotted module name 共享同一个 runtime slot;完整名称不要求 `use`,两种语法都在首次实际执行时 lazy import。识别前严格采用 PHP namespace resolution:例如在 `namespace App` 中,相对名称 `python\math\sqrt()` 是普通的 `App\python\math\sqrt()`,只有 `\python\math\sqrt()` 指向 Python 根命名空间。 |
|
||||||
|
|
||||||
## 阶段 2:builtins、构造语法糖与静态类型 |
|
||||||
|
|
||||||
1. `python\name()` 通过 phpy Zend Facade 动态调用:显式 `PyCore` 方法直接复用,其他名称经 Python `builtins` module lookup。 |
|
||||||
2. `python\list/dict/tuple/set/str/object()` 映射既有 phpy Zend 类或方法。 |
|
||||||
3. `new PyList()` 与 `python\list()` 等写法获得相同的逻辑静态类型。 |
|
||||||
4. Python 调用结果保持 `PyObject` 或已知 phpy 子类,关闭 TypePHP 路径的隐式 scalar conversion。 |
|
||||||
5. 缺少 phpy、builtin 不存在、参数错误和异常映射测试。 |
|
||||||
|
|
||||||
实现状态:已完成。TypePHP 在首次实际执行 Python 表达式时延迟启用 phpy 的 `return_as_object`,仅声明未使用的 Python 符号仍不触发运行时依赖。 |
|
||||||
|
|
||||||
## 阶段 3:参数转换与显式结果转换 |
|
||||||
|
|
||||||
1. TypePHP 参数从左到右求值后自动转换为 Python 值。 |
|
||||||
2. 标量、数组、空数组、嵌套容器及 TypePHP callable 转换。 |
|
||||||
3. 通过 `$py->toValue()` 或 `python\scalar($py)` 离开 Python 对象规则;需要确定原生类型时继续使用普通 TypePHP 转换,例如 `$py->toValue()->toInt()`。容器和 iterator 可直接使用 `$py->toArray()`。 |
|
||||||
4. 深拷贝、递归容器、溢出、Unicode/bytes 和异常路径测试。 |
|
||||||
5. review 并重构 phpy 转换策略,移除影响同步重入的全局临时转换状态。 |
|
||||||
|
|
||||||
实现状态:核心边界已完成。TypePHP 参数严格从左到右求值,支持标量、空数组、嵌套 list/dict 与 callable;`PyObject::toValue()` 与 `python\scalar()` 复用 phpy 的显式转换入口,`PyObject::toArray()` 转换受支持的容器和 iterator。phpy 已移除进程级转换函数指针,改为局部有状态转换器、RAII 递归保护和 128 层深度限制,并覆盖无效 UTF-8、PHP 自引用数组及 Python 循环容器错误路径。Python 大整数与 bytes 的最终语言映射仍保留在本阶段后续工作中。 |
|
||||||
|
|
||||||
## 阶段 4:运算符 |
|
||||||
|
|
||||||
1. 将运算符改写为 Python 标准库 `operator` module 的动态调用。 |
|
||||||
2. 混合操作数先转换为 `PyObject`。 |
|
||||||
3. 严格保证从左到右、各求值一次。 |
|
||||||
4. 使用 `operator.is_/is_not/truth` 实现 identity 和 truthiness,使用 `iadd/isub/...` 实现 compound assignment。 |
|
||||||
5. 验证 `operator` 自动处理 `NotImplemented`、reflected dunder 和子类优先级。 |
|
||||||
6. 对照 phpy opcode-handler 行为,修复 `/` 错误映射 floor division 等既有问题。 |
|
||||||
|
|
||||||
实现状态:已完成。二元算术、位运算、比较、`===`/`!==`、一元运算、条件真假值、短路逻辑和复合赋值均通过隐式 `operator` module binding 执行;`/` 使用 `truediv`。混合 TypePHP 操作数由 phpy 在调用边界转换,结果继续保持 `PyObject`,比较和真假值结果显式收敛为 TypePHP `bool`。属性和下标左值由阶段 5 的动态写入协议完成回写。 |
|
||||||
|
|
||||||
## 阶段 5:完整对象协议 |
|
||||||
|
|
||||||
1. Python 对象属性读写和删除。 |
|
||||||
2. 下标读写、删除、`isset()`。 |
|
||||||
3. iterator/foreach。 |
|
||||||
4. Python callable 和 TypePHP callable proxy 的同步重入。 |
|
||||||
5. keyword argument、argument unpacking 和错误语义。 |
|
||||||
|
|
||||||
实现状态:已完成。Python proxy 的动态属性、未知方法、下标、删除、`isset()`、`foreach` 和 callable 均复用 phpy 的 Zend object protocol,不生成 phpy C++ 符号。方法、属性、下标和 callable 结果会继续传播为 `PyObject`,支持链式访问和后续 Python 运算。named argument 与 unpacking 复用统一调用参数管线并保持从左到右求值;属性和下标复合赋值使用 `operator.i*()` 的返回对象回写原左值。 |
|
||||||
|
|
||||||
phpy 同步完成了对象协议加固:`__set()` 转换引用释放、`__unset()`、list/tuple 负索引、list 删除、缺失键与 Python `None` 的 `isset()` 语义、删除和 contains 状态检查,以及 iterator/count 异常传播。相关 BUG 均由 phpy PHPUnit 与 TypePHP PHPT 独立覆盖。 |
|
||||||
|
|
||||||
## 阶段 6:phpy 稳定性与性能收尾 |
|
||||||
|
|
||||||
1. CPython/ZendVM 生命周期、GIL、owned/borrowed/stolen reference 全量审计。 |
|
||||||
2. Python/Zend 异常状态和 traceback 审计。 |
|
||||||
3. 跨 VM 引用环、析构和异常注入测试。 |
|
||||||
4. ASan/UBSan、PHP leak report、Python debug build 和压力测试。 |
|
||||||
5. 基准测试动态 Zend call、module map、参数转换和 `operator` module 调用;只优化被数据证明的热点。 |
|
||||||
6. 完整 PHPUnit、pytest、PHPT 和现有 TypePHP compiler 回归。 |
|
||||||
|
|
||||||
实现状态:已完成。第一轮 CPython 失败路径审计已覆盖通用对象、list、dict、tuple、set 的构造和下标写入,以及 sequence/set 的 `contains()`。PHP 到 Python 的 key/value 转换失败现在会立即映射为 `PyError`,所有已取得的新引用均由作用域守卫释放;构造失败不再留下未处理的 CPython error indicator,`contains()` 的 `-1` 错误结果也不再被误判为 `true`。无效 UTF-8、unhashable set member、失败后容器仍可继续使用等路径已有 phpy PHPUnit 回归测试。 |
|
||||||
|
|
||||||
第二轮审计覆盖 module import、异常转换、callable 检查和显式 iterator API。`PyImport_ImportModule()`、`PyErr_Fetch()` 和 `PyIter_Next()` 转移给调用方的新引用现在都会在 Zend wrapper 取得独立引用后统一释放;重复 import、Python 异常或显式 iterator next 不再持续增加引用计数。调用非 callable 的 Python 属性或 `PyObject` 会稳定抛出 `PyError(TypeError)`,不再因为 `PyCallable_Check()` 未设置 error indicator 而静默返回 `null`。`PyCore::next()` 也会区分正常迭代结束和 iterator 异常。上述路径均先建立失败的 phpy PHPUnit 回归测试,其中对象调用行为另有 TypePHP PHPT 集成覆盖。 |
|
||||||
|
|
||||||
第三轮审计覆盖 `PyCore` Facade 的转换失败与函数缓存。`PyCore::eval()` 会在 globals 转换失败后立即抛出 `PyError`,`PyCore::bytes()` 对非字符串标量使用转换后的 `zend_string`,两者不再解引用空指针或错误的 zval union 字段而导致进程崩溃。`PyCore::next()` 同时释放参数转换产生的 iterator 引用。builtin/operator 函数缓存改用 `std::string` 内容键,不再把请求级 `char*` 地址作为长期 key,也避免同名动态调用不断重复缓存并增加 Python function 引用计数;调用存在但不可调用的 builtin 会释放临时引用并抛出 `PyError(TypeError)`。所有问题均由先失败的独立 PHPUnit 覆盖,其中两个崩溃用禁用 core dump 的隔离进程确认退出码 139 后再修复。 |
|
||||||
|
|
||||||
第四轮审计覆盖 Python 到 PHP 的同步回调边界。phpy 会将 Python keyword arguments 转换为 Zend named parameters,并在任一位置参数或命名参数转换失败后立即停止,不会执行只接收到部分参数的 PHP callable。PHPX 为 AOT 原生闭包生成并管理 Zend `arg_info` 参数名元数据,因此 Python kwargs 可以按名称绑定到 TypePHP 闭包,而不是依赖参数位置或降级为字符串 callable。phpy PHPUnit、PHPX 单元测试和 TypePHP PHPT 分别覆盖了转换失败、Zend 命名绑定以及完整的 Python→TypePHP 回调链路。 |
|
||||||
|
|
||||||
第五轮审计覆盖 Python 字符串跨 Zend 边界时的异常和所有权。Python 孤立代理字符无法编码为 UTF-8 时,`phpy.String`、动态 PHP 类名、字典键、`PyObject::__toString()` 和 Python 异常消息格式化都不会再使用空指针或未初始化长度;修复前相关隔离测试会退出 139 或尝试分配异常大的内存。`StrObject` 现在具有显式有效状态,所有调用方必须在访问指针前检查转换结果;异常消息的字符串化仅作为 best-effort 辅助信息,失败时保留原始 Python error/type/value,并清理临时 CPython error indicator。`new_string()` 同时补齐 Zend carrier 析构注册,定长字符串直接取得唯一的 `zend_string` 引用,消除了成功路径的泄漏和未初始化 zval。 |
|
||||||
|
|
||||||
第六轮引用审计修复了 `PySequence::slice()` 的 new-reference 泄漏。切片在包装为 Zend `PyObject` 后会释放 CPython API 返回的原始所有权,同时保留 wrapper 自己持有的引用;由 `sys.getrefcount()` 压力测试验证重复创建并销毁切片不会继续增加元素引用计数。切片创建失败也会在接触空指针前转换为 `PyError`。 |
|
||||||
|
|
||||||
第七轮审计覆盖动态 PHP 的 Python 运算符协议。phpy opcode handler 现在使用 CPython `PyNumber_*` / `PyObject_RichCompareBool()`,`/` 与 `/=` 使用 true division,复合赋值会用 in-place API 返回的对象更新 Zend 左值,并正确处理不可变 Python 对象、Zend 引用变量、表达式结果和失败后左值状态。`===` / `!==` 使用 Python object identity,bool cast、`!` 和条件分支使用 Python truth protocol;PHP 操作数转换、结果和异常路径的新引用统一由 RAII 守卫管理。TypePHP 的 `operators.phpt` 同时以 ZendPHP + opcode handler 和 AOT + `operator` module 运行,因此是两条实现的输出一致性门禁。 |
|
||||||
|
|
||||||
动态代码的一元正负运算已确定为兼容性边界:PHP 会把 `-$value` / `+$value` 编译成乘以 `-1` / `1`,opcode handler 无法区分它与源码中的显式乘法。动态 ZendVM 代码不再尝试改写 AST,而是明确保留 `$value * -1` / `$value * 1` 的行为。Python 内置数值和 NumPy 等常见对象的结果通常一致,但自定义对象的 `__neg__()` / `__pos__()` 可能与 `__mul__()` 不同。AOT TypePHP 保留原始 AST,仍分别 lowering 为 `operator.neg()` / `operator.pos()`,语义不受影响。外部用户文档 `python.md` 已将这一差异列入兼容性限制。 |
|
||||||
|
|
||||||
性能基准分别覆盖 module property、operator module call、已有 `PyObject` 参数和 PHP 标量参数转换。在当前未优化构建中,module property 约为 0.7–0.9 μs/op,operator call 约为 1.8–2.5 μs/op;参数是否预先包装为 `PyObject` 没有呈现稳定差异。尝试以 indirect zval wrapper 消除 module map 每次访问的引用计数后,A/B 中位数仍处于同一噪声区间,因此没有保留生命周期更敏感且收益未经证明的优化。 |
|
||||||
|
|
||||||
phpy 的 CMake Python-extension 目标同时完成了 out-of-tree 构建修复,并通过从 Zend 标准 cast handler 推导返回类型来兼容旧版 PHP 的 `int` ABI 与新版 PHP 的 `zend_result` ABI。PHP 8.1 和 PHP 8.4 均已完成全新构建验证。 |
|
||||||
|
|
||||||
内存门禁使用 Valgrind Memcheck 执行。测试关闭 Zend allocator 与 PCRE JIT,在最小独立进程中分别循环 100 次 PHP Closure kwargs 回调、可调用 PHP 对象 kwargs 回调、sequence slice 创建销毁、无效 Unicode 的对象字符串化、字典键转换和异常格式化。结果为 0 invalid-access、0 definite leak、0 indirect leak;进程退出时由 PHP/CPython 保留的 493,106 bytes 均为 still-reachable,不计为泄漏。ASan 扩展无法安全 `dlopen` 到当前启用了 `RTLD_DEEPBIND` 的非 ASan PHP,因此本轮采用不要求 PHP 同步重编译的 Valgrind 作为内存检查工具。 |
|
||||||
|
|
||||||
阶段 6 最终门禁结果:phpy PHPUnit 135 tests / 469 assertions 通过(1 个既有 warning、1 个环境相关 skip),pytest 26/26 通过,TypePHP Python PHPT 14/14 通过,TypePHP PHPUnit 1103 tests / 2729 assertions 通过;TypePHP compiler 全量 PHPT 共 934 项,其中 932 PASS、2 SKIP、0 FAIL、0 WARN。动态 operator 压力测试在 Valgrind 下为 0 invalid access、0 definite leak、0 indirect leak。 |
|
||||||
|
|
||||||
## 阶段门禁 |
|
||||||
|
|
||||||
- 当前阶段的失败测试未先建立,不开始实现。 |
|
||||||
- 当前阶段所有测试未通过,不进入下一阶段。 |
|
||||||
- phpy 的行为变更必须先在 phpy 仓库增加 PHPUnit/pytest 测试。 |
|
||||||
- 每个已修复 BUG 必须保留独立回归测试。 |
|
||||||
- 不以修改第三方测试期望来掩盖实现差异。 |
|
||||||
@ -1,98 +0,0 @@ |
|||||||
# Python 工具子模块 |
|
||||||
|
|
||||||
TypePHP 将 Python IDE helper 生成器和 Python 源码转换器集成到了 `tpc`。两者位于独立的 |
|
||||||
`src/PythonTools` 目录,只复用 `tpc` 命令入口,不进入正常的 PHP 预处理、C++ 生成和编译流水线。 |
|
||||||
|
|
||||||
## Python namespace IDE helper |
|
||||||
|
|
||||||
```shell |
|
||||||
./tpc --gen-python-helper math |
|
||||||
./tpc --gen-python-helper numpy.linalg |
|
||||||
./tpc --gen-python-helper numpy --output-dir .ide-helper |
|
||||||
``` |
|
||||||
|
|
||||||
命令通过 PHPy 导入指定 Python module,并使用 Python `inspect` API 采集函数、参数、类、方法和 |
|
||||||
module attribute。PHPy 扩展以及目标 Python module 必须安装在执行 `tpc` 的主机环境中。 |
|
||||||
|
|
||||||
默认生成文件位于当前目录的 `ide-helper` 中。`--output-dir` 可以替换这个输出根目录,既支持 |
|
||||||
相对当前目录的路径,也支持绝对路径: |
|
||||||
|
|
||||||
```text |
|
||||||
ide-helper/python/math.php |
|
||||||
ide-helper/python/numpy/linalg.php |
|
||||||
ide-helper/python.php |
|
||||||
ide-helper/PyObject.php |
|
||||||
``` |
|
||||||
|
|
||||||
每次生成 module helper 时,会同时扫描 Python `builtins` 并生成根命名空间文件 |
|
||||||
`python.php`,为 `python\tuple()`、`python\len()` 等内置符号提供 IDE 补全。该文件会 |
|
||||||
随当前 Python 环境重新生成。 |
|
||||||
|
|
||||||
首次生成 module helper 时,还会生成公共的 `PyObject.php`。它包含 `PyObject` 的动态访问、调用、 |
|
||||||
数组访问、迭代以及 `toArray()`、`toValue()` 等方法提示,供所有 Python module helper 共享。若该文件 |
|
||||||
已经存在,生成器会保留原文件,不进行覆盖。 |
|
||||||
|
|
||||||
生成内容使用 TypePHP 的 module-as-namespace 形式,例如 `python\math\sqrt()`,并兼容普通 |
|
||||||
`use`、`use function` 和 `use const` 的 IDE 名称解析。文件末尾包含 `die`,用于在误执行时明确 |
|
||||||
终止程序。helper 只能交给 IDE 索引,不能被 include,也不能加入 TypePHP 项目的 sources 或编译输入。 |
|
||||||
|
|
||||||
`PyObject::IDE_HELPER_ONLY` 是所有 helper 共用的提示常量。非 `void` stub 的方法体使用 |
|
||||||
`die(\PyObject::IDE_HELPER_ONLY)`,以满足 IDE 对返回类型控制流的检查,不会再产生“缺少 return |
|
||||||
语句”的诊断。module attribute 使用命名空间 `const` 声明,支持 IDE 的常量补全和 `use const`。 |
|
||||||
PHP 8.1 及以上允许在常量初始化表达式中使用 `new`。module attribute 因此直接使用仅供 IDE |
|
||||||
分析的 `PyObject` 实例作为占位值: |
|
||||||
|
|
||||||
```php |
|
||||||
const pi = new \PyObject(); |
|
||||||
``` |
|
||||||
|
|
||||||
这样 IDE 会将常量精确识别为 `PyObject`,而不是从 `null` 推断出错误类型。 |
|
||||||
|
|
||||||
公共 `PyObject` helper 还声明了 TypePHP 的虚拟关键词方法,包括 `toInt()`、`toFloat()`、 |
|
||||||
`toString()`、`toBool()`、`toStream()`、高精度类型转换、`toObject()`、`toAny()` 和 `toRef()`。 |
|
||||||
这些声明仅用于 IDE 补全;调用会在编译期展开,并不是 PHPy `PyObject` 运行时类的实体方法。 |
|
||||||
`toArray()` 和 `toValue()` 则仍是 PHPy 提供的真实方法。 |
|
||||||
|
|
||||||
Python class 的构造函数会显式调用 `parent::__construct()`。Python 对象若定义了 `count()`,helper |
|
||||||
不会重复声明它,因为 `PyObject::count(): int` 已用于 PHP `Countable`。需要调用 Python 自身的 |
|
||||||
`count()` 时,应显式写为 `$object->__call('count', $arguments)`。 |
|
||||||
|
|
||||||
PHP function/class 名称大小写不敏感,而 Python 名称大小写敏感;PHP 保留字也不能声明为普通 |
|
||||||
stub symbol。生成器会以注释报告无法用合法 PHP 声明表达的符号,不会擅自重命名 Python API。 |
|
||||||
`python\print()` 的调用语法合法,但 PHP 禁止声明名为 `print` 的函数,因此单纯的 |
|
||||||
PHP helper 文件无法为它提供无语法错误的符号声明。`list`、`int`、`float` 等 PHP |
|
||||||
保留字存在同样的限制。 |
|
||||||
|
|
||||||
## Python 转 TypePHP |
|
||||||
|
|
||||||
```shell |
|
||||||
./tpc --convert-python-to-php script.py > script.php |
|
||||||
``` |
|
||||||
|
|
||||||
转换器调用 PATH 中的 `python3` 解析 Python AST,然后输出使用 TypePHP Python namespace |
|
||||||
语法的 PHP 源码。普通 module import 会转换为 namespace import: |
|
||||||
|
|
||||||
```python |
|
||||||
import math |
|
||||||
print(math.sqrt(16)) |
|
||||||
``` |
|
||||||
|
|
||||||
```php |
|
||||||
use python\math; |
|
||||||
|
|
||||||
function main(): void |
|
||||||
{ |
|
||||||
python\print(math\sqrt(16)); |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
当前支持普通 import、函数、赋值、调用、容器字面量、基础运算、单项比较、if/while/for、 |
|
||||||
lambda 和基础 f-string。module 顶层变量会转换为 PHP global,以保持函数读取 module 变量的能力。 |
|
||||||
当语义可以严格保持时,转换器会直接使用 PHP 原生语法:无参数或可安全转换的 |
|
||||||
`print()` 生成带换行的 `echo`,`sys.exit()` 和整数字面量退出码生成 `exit`。具有 |
|
||||||
`sep`、`end`、`file`、`flush` 参数的 `print()`,以及字符串或对象形式的 `sys.exit()` |
|
||||||
与 PHP 行为不完全一致,仍保留为 Python 调用。 |
|
||||||
|
|
||||||
转换器遵循“不能可靠保持语义就拒绝”的原则。class、async、generator、try/with、decorator、 |
|
||||||
destructuring assignment、chained comparison、嵌套函数以及 loop-else 等尚未完成的语法会抛出带 |
|
||||||
源文件和行号的错误,不会生成看似可用但语义错误的 PHP 代码。 |
|
||||||
@ -1,22 +0,0 @@ |
|||||||
## 编译 |
|
||||||
|
|
||||||
```shell |
|
||||||
./tpc --wasm test.php |
|
||||||
``` |
|
||||||
|
|
||||||
编译成功后默认只生成可由 Wasmtime 执行的 WASI 0.2 Component `test.wasm`。WASI 0.1 不受支持。 |
|
||||||
生成的 C++ 源码默认写入 `build/`,也可以通过 `--build-dir <directory>` 指定。 |
|
||||||
|
|
||||||
## 执行 |
|
||||||
|
|
||||||
```shell |
|
||||||
wasmtime test.wasm |
|
||||||
``` |
|
||||||
|
|
||||||
## Chrome |
|
||||||
|
|
||||||
```shell |
|
||||||
./tpc --wasm=browser test.php |
|
||||||
``` |
|
||||||
|
|
||||||
浏览器模式额外生成 `test.browser/` Jco 模块并要求 `jco` 位于 `PATH`。完整浏览器 Demo 位于仓库 `examples/wasm-hello/`,并使用 `wasm: browser` 的 `project.yml` 构建。TypePHP 在专用 Worker 中执行;默认文件系统驻留内存,可显式启用 OPFS 快照持久化。网络 socket、进程、shell 和信号在 WASI 目标下明确不支持。 |
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue