refactor(php): 更新幂运算函数调用实现

- 将 php::call(php::pow) 替换为 php::math::pow 直接调用
- 修改赋值操作符中的幂运算表达式生成逻辑
- 优化临时变量创建方式,使用 addTmpVar 方法替代手动创建
- 在函数调用优化器中添加对 pow 函数的直接映射
- 添加 pow 函数相关的测试用例验证功能正确性
pull/1/head
韩天峰 5 months ago
parent f179705296
commit c0e224e4db
  1. 11
      src/Php/CompilerBase.php
  2. 3
      src/Php/FuncCallOptimizer.php
  3. 3
      tests/aot/pow-assign-op.phpt

@ -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

@ -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) . ')';
}

@ -5,6 +5,9 @@ strlen
$a = 2;
$a **= 10;
var_dump($a);
$d = pow(3, 4);
assert($d == 81);
?>
--EXPECT--
int(1024)

Loading…
Cancel
Save