From c0e224e4db927670d393b78f2e3e3269f28dc5f3 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 27 Mar 2026 13:05:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E6=9B=B4=E6=96=B0=E5=B9=82?= =?UTF-8?q?=E8=BF=90=E7=AE=97=E5=87=BD=E6=95=B0=E8=B0=83=E7=94=A8=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 php::call(php::pow) 替换为 php::math::pow 直接调用 - 修改赋值操作符中的幂运算表达式生成逻辑 - 优化临时变量创建方式,使用 addTmpVar 方法替代手动创建 - 在函数调用优化器中添加对 pow 函数的直接映射 - 添加 pow 函数相关的测试用例验证功能正确性 --- src/Php/CompilerBase.php | 11 +++++------ src/Php/FuncCallOptimizer.php | 3 +++ tests/aot/pow-assign-op.phpt | 3 +++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 35c5bacf..4aeb0691 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -2045,6 +2045,7 @@ class CompilerBase extends \PhpAot\Core\Translator $var = $this->parseIdentifier($node->var); $expr = $this->parseIdentifier($node->expr); $leftExprType = $node->var->getType(); + if ($this->isVarExpr($node->var)) { if (!$this->hasVar($var)) { $this->fatalError($node->var, 'Cannot assign to undefined variable'); @@ -2055,16 +2056,15 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->isArrayVar($node->var)) { $this->fatalError($node->var, 'Cannot concat string to array'); } - return $var . '.append(' . $rightExprStr . ')'; } if ($this->isAssignOpPow($op)) { - $powExpr = 'php::call(php::pow, {' . $var . ', ' . $rightExprStr . '})'; - + $powExpr = 'php::math::pow(' . $var . ', ' . $rightExprStr . ')'; return $var . ' = ' . $this->convertVarType($var, $powExpr); } return $var . ' ' . $op . ' ' . $rightExprStr; } + if ($leftExprType === self::EXPR_ARRAY_DIM_FETCH) { /** * $count[$r] -= 1; @@ -2313,8 +2313,7 @@ class CompilerBase extends \PhpAot\Core\Translator $fn = $this->getFuncPtr($name); $this->context->beforeStmtLines[] = '// Func Call: ' . $name . '()'; } else { - $tmpVar = $this->genTmpVarName(); - $this->addLocalVar($tmpVar, self::TYPE_VAR); + $tmpVar = $this->addTmpVar(self::TYPE_VAR); $this->context->beforeStmtLines[] = $tmpVar . ' = ' . $this->parseExpr($expr->name) . ';'; $placeHolder = $fn = $tmpVar; $name = ''; @@ -2633,7 +2632,7 @@ class CompilerBase extends \PhpAot\Core\Translator $left = $this->parseIdentifier($expr->left); $right = $this->parseIdentifier($expr->right); - return 'php::pow(' . $left . ', ' . $right . ')'; + return 'php::math::pow(' . $left . ', ' . $right . ')'; } protected function parsePreDec(Node\Expr\PreDec $expr): string diff --git a/src/Php/FuncCallOptimizer.php b/src/Php/FuncCallOptimizer.php index 44e9c9b5..be16620b 100644 --- a/src/Php/FuncCallOptimizer.php +++ b/src/Php/FuncCallOptimizer.php @@ -44,6 +44,9 @@ trait FuncCallOptimizer if ($name === 'abs') { return 'php::math::abs(' . $getArg(0) . ')'; } + if ($name === 'pow') { + return 'php::math::pow(' . $getArg(0) . ', ' . $getArg(1) . ')'; + } if ($name === 'ord') { return 'php::fn::ord(' . $getArg(0) . ')'; } diff --git a/tests/aot/pow-assign-op.phpt b/tests/aot/pow-assign-op.phpt index ce6a0b13..5efab255 100644 --- a/tests/aot/pow-assign-op.phpt +++ b/tests/aot/pow-assign-op.phpt @@ -5,6 +5,9 @@ strlen $a = 2; $a **= 10; var_dump($a); + +$d = pow(3, 4); +assert($d == 81); ?> --EXPECT-- int(1024)