forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Support 'static const CONST_NAME = expr;' #10
Open
TysonAndre
wants to merge
3
commits into
master
Choose a base branch
from
static-const-opcode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
These had originally used other exit codes as mail_basic5.phpt, but that was changed later with commit d1b12c9[1]. [1] <http://git.php.net/?p=php-src.git;a=commit;h=d1b12c9a3031841302722d6f6706e4598a639d7a>
Currently, this is limited to the global scope. This can be thought of as similar to C: the static const can only be seen within the namespace block scope. Differences from global constants: - A name can only be declared once per namespace block. - The name can only be used by the following statements in the namespace block. - This can support any expression define() would accept (function calls, $variable, properties, etc.). - This is eagerly evaluated when the expression might be dynamic. This is deliberate, and meant to make accidental infinite recursion (and unexpected side effects of fetching a constant for the first time) less likely. This may throw in the future. - The value from the first successful declaration is used and permanently stored. Similarities to global constants: - 'static const' rejects the same types (references, objects, and reference cycles.) - 'static const' cannot be modified once declared The current implementation allows code such as this: ```php static const LOCAL_X = "Local $x"; class Example { const X = LOCAL_X; } ``` Which is shorthand for: ``` if (!defined('UNIQUE_NAME_FOR_LOCAL_X')) { define('UNIQUE_NAME_FOR_LOCAL_X', "Local $x"); } class Example { const X = \LOCAL_NAME_FOR_LOCAL_X; } ``` Unfinished work: - Work out what to do if the expression throws an exception/error. (Fatal error? Leave as is?) - Decide what this should do if the `define()` call fails. (Fatal error? Throw a regular Error?) - (Optional) opcache optimizations such as evaluating functions at compile time.
TysonAndre
force-pushed
the
static-const-opcode
branch
from
January 5, 2020 20:27
af8d07f
to
d948924
Compare
Note to self: It might be worth looking into whether this improves performance, once I implement block scoping (e.g. for functions). Code such as the following wouldn't have to create a reference to a static variable and dereference it. <?php
function fib($n) : int {
return $n >= 1 ? fib($n - 1) + fib($n - 2) : $n;
}
function test1() : int {
static $x = null;
return $x ??= fib(5);
}
function test2() : int {
if (!defined('SOME_UNIQUE_CONST')) {
define('SOME_UNIQUE_CONST', fib(5));
}
return SOME_UNIQUE_CONST;
} |
TysonAndre
pushed a commit
that referenced
this pull request
May 31, 2021
The following opcodes would be generated: ... BB1: 0003 JMP BB3 BB2: 0004 INIT_FCALL 1 96 string("chr") 0005 #10.T3 [long] = SR #3.CV0($int) [long] #7.CV2($i) ... 0006 #11.T4 [long] RANGE[0..127] = BW_AND #10.T3 [long] ... 0007 #12.T3 [long] RANGE[128..255] = BW_OR #11.T4 [long] ... 0008 SEND_VAL #12.T3 [long] RANGE[128..255] 1 0009 #13.V3 [ref, rc1, rcn, any] = DO_ICALL 0010 ASSIGN_OP (CONCAT) #6.CV1($out) [rc1, rcn, string] 0011 ADD #7.CV2($i)... int(7) #7.CV2($i) ... -> #15.CV2($i) ... BB3: 0012 #8.T4 [long] = SR #3.CV0($int) #7.CV2($i) [long, double] 0013 #9.T3 [bool] RANGE[0..1] = IS_SMALLER int(128) #8.T4 0014 JMPNZ #9.T3 [bool] RANGE[0..1] BB2 ... Main changes are: 1. SR opcode covers new path in function zend_jit_long_math_helper(). 2. BW_AND and BW_OR opcodes are supported. See macro LONG_OP. 3. Function zend_jit_concat_helper() is added to support ASSIGN_OP opcode. Speficically, CONCAT and FAST_CONCAT is supported for statements "$out .= ...". 4. New path is covered in function zend_jit_cmp_long_long() by IS_SMALLER opcode. 5. New path is covered in macros ZVAL_PTR_DTOR and ZVAL_DTOR_FUNC when leaving.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, this is limited to the global scope.
This can be thought of as similar to C:
the static const can only be seen within the namespace block scope.
Differences from global constants:
A name can only be declared once per namespace block.
The name can only be used by the following statements in the namespace block.
This can support any expression define() would accept
(function calls, $variable, properties, etc.).
This is eagerly evaluated when the expression might be dynamic.
This is deliberate, and meant to make accidental infinite recursion
(and unexpected side effects of fetching a constant for the first time)
less likely.
This may throw in the future.
The value from the first successful declaration is used and permanently stored.
Similarities to global constants:
(references, objects, and reference cycles.)
The current implementation allows code such as this:
Which is shorthand for:
Unfinished work:
(Fatal error? Leave as is?)
define()
call fails.(Fatal error? Throw a regular Error?)
Fix eval - account for it being possible when generating unique constant names based on the current file.
Fix loading the same file path with different contents - if
require
is called twice, so the file name isn't unique (For files, a hash of the file contents would make more sense and work better with opcache)Benchmark static const vs static variable for repeated calls
Undo limitation to top-level statements (block scope is a TODO). Add a variant of DECLARE_CONST that ignores the constant already existing.
Look into how to allow opcache to optimize
static const DEBUG = false; if (DEBUG) {}
with dead code detection