Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

TysonAndre
Copy link
Owner

@TysonAndre TysonAndre commented Jan 5, 2020

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:

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.

  • 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)

    php > eval('echo json_encode(__FILE__) . "\n";');
    "php shell code(1) : eval()'d code"
    
  • 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

carusogabriel and others added 3 commits January 5, 2020 18:30
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 TysonAndre force-pushed the static-const-opcode branch from af8d07f to d948924 Compare January 5, 2020 20:27
@TysonAndre
Copy link
Owner Author

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants