From 8df7d16efb3d8471c69a60f85f8b8f72806bb779 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 31 Dec 2025 12:25:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cc | 10 +++++- src/Php/Translator.php | 78 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/main.cc b/main.cc index 734f55c5..121832cc 100644 --- a/main.cc +++ b/main.cc @@ -1,4 +1,7 @@ #include "sapi/embed/php_embed.h" +#if PPROF_ON +#include +#endif #include void php_main(); @@ -9,6 +12,9 @@ extern php::Var argv; int main(int cpp_argc, char **cpp_argv) { php_embed_init(cpp_argc, cpp_argv); int rc = 0; +#if PPROF_ON + ProfilerStart("myapp.prof"); +#endif zend_first_try { argc = php::global("argc"); argv = php::global("argv"); @@ -18,7 +24,9 @@ int main(int cpp_argc, char **cpp_argv) { rc = EG(exit_status); } zend_end_try(); - +#if PPROF_ON + ProfilerStop(); +#endif argc.unset(); argv.unset(); php_embed_shutdown(); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 68348aa4..7c35911e 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -76,8 +76,9 @@ class Translator extends \PhpAot\Core\Translator public function __construct(string $rootPath) { $this->rootPath = $rootPath; - $this->climate = new CLImate; - $this->climate->arguments->add([ + $climate = new CLImate; + $this->climate = $climate; + $climate->arguments->add([ 'optimize' => [ 'prefix' => 'O', 'longPrefix' => 'optimize', @@ -86,11 +87,62 @@ class Translator extends \PhpAot\Core\Translator 'castTo' => 'int', 'defaultValue' => 0, ], + 'output' => [ + 'prefix' => 'o', + 'longPrefix' => 'output', + 'description' => 'Output file', + ], + 'help' => [ + 'prefix' => 'h', + 'longPrefix' => 'help', + 'description' => 'Show help', + 'noValue' => true, + ], + 'profile' => [ + 'longPrefix' => 'profile', + 'description' => 'Enable performance profiling', + 'required' => false, + 'noValue' => true, + ], ]); $this->preprocessArgvAdvanced(); - $this->climate->arguments->parse(); - $this->optimizeLevel = $this->climate->arguments->get('optimize'); + $climate->arguments->parse(); + if ($climate->arguments->defined('help')) { + $this->showUsage(); + exit(0); + } + $this->optimizeLevel = $climate->arguments->get('optimize'); + } + + function showUsage(): void + { + $climate = $this->climate; + $climate->bold()->green('PHP AOT Compiler v1.0.0'); + $climate->br(); + + $climate->bold('USAGE:'); + $climate->tab()->out('./bin/compiler.php [options]'); + $climate->br(); + + $climate->bold('ARGUMENTS:'); + $climate->tab()->out(' Input PHP file/directory to compile'); + $climate->br(); + + $climate->bold('OPTIONS:'); + $climate->tab()->out('-O Optimization level (0-3, default: 0)'); + $climate->tab()->out('-p, --profile Enable performance profiling'); + $climate->tab()->out('-o, --output Output binary name (default: input basename)'); + $climate->tab()->out('-v, --verbose Verbose output'); + $climate->tab()->out('-h, --help Show this help message'); + $climate->br(); + + $climate->bold('EXAMPLES:'); + $climate->tab()->out('./bin/compiler.php examples/hello.php'); + $climate->tab()->out('./bin/compiler.php examples/bench.php -O2'); + $climate->tab()->out('./bin/compiler.php examples/bench.php -O2 -p'); + $climate->tab()->out('./bin/compiler.php examples/app.php -O3 -o myapp -v'); + $climate->br(); } function preprocessArgvAdvanced(): void @@ -869,18 +921,20 @@ class Translator extends \PhpAot\Core\Translator return $out; } - private function addOptimizeOption(string &$cmd): void + private function addCompilationOption(string &$cmd): void { $cmd .= ' -O' . $this->optimizeLevel; + $cmd .= ' -g'; + if ($this->climate->arguments->defined('profile')) { + $cmd .= ' -lprofiler'; + $cmd .= ' -DPPROF_ON=1'; + } } public function compileFile($file): void { $cmd = $this->cppCompiler . ' -c ' . $file . ' -o ' . $file . '.o ' . $this->parseIncludes(); - $this->addOptimizeOption($cmd); - if ($this->debugInfo) { - $cmd .= ' -g'; - } + $this->addCompilationOption($cmd); $this->climate->comment($cmd); shell_exec($cmd); } @@ -888,8 +942,11 @@ class Translator extends \PhpAot\Core\Translator public function compileBinary($targetFile, $objectFile): void { $this->genGlobalVars(); + if ($this->climate->arguments->defined('output')) { + $targetFile = $this->climate->arguments->get('output'); + } $cmd = $this->cppCompiler . ' main.cc global_vars.cc ' . $objectFile . ' -o ' . $targetFile . ' ' . $this->parseIncludes() . $this->parseLdflags() . $this->parseLibs(); - $this->addOptimizeOption($cmd); + $this->addCompilationOption($cmd); $this->climate->comment($cmd); shell_exec($cmd); } @@ -1415,7 +1472,6 @@ class Translator extends \PhpAot\Core\Translator foreach ($v->vars as $v) { if (!$this->hasGlobalVar($v->name)) { $this->addGlobalVar($v->name, self::TYPE_VAR); - var_dump($this->globalVars); } } return '';