Transcript
Page 1: PHP, Under The Hood - DPC

PHPUnder The Hood

Page 2: PHP, Under The Hood - DPC

What Is PHP?● An Easy To Learn Language?

○ Sure!● A Simple Language?

○ Usually● A Well Designed Language?

○ HA!● An Effective Language!

○ Absolutely!

Page 3: PHP, Under The Hood - DPC
Page 4: PHP, Under The Hood - DPC

PHP Is...

Dynamically Typed

Page 5: PHP, Under The Hood - DPC

$a = 1;$b = "a";

Page 6: PHP, Under The Hood - DPC

PHP Is...

Weak Typed

Page 7: PHP, Under The Hood - DPC

1 + "1b" == 2

Page 8: PHP, Under The Hood - DPC

PHP Is...

Implementation Defined

Page 9: PHP, Under The Hood - DPC

ImplementationsMain:

C-PHP: www.php.netAlt:

HipHop - FacebookRoadsend - CompilerPhalanger - .NETPHP Compiler - CompilerPHPPHP - PHP

Page 10: PHP, Under The Hood - DPC
Page 11: PHP, Under The Hood - DPC

Interesting Note:C-PHP Requires PHP To Compile!

Page 12: PHP, Under The Hood - DPC

PHP Is...

Compiled

Page 13: PHP, Under The Hood - DPC
Page 14: PHP, Under The Hood - DPC

PHP Is...

DynamicallyCompiled

On-Demand

Page 15: PHP, Under The Hood - DPC
Page 16: PHP, Under The Hood - DPC

It All Starts With TheSAPI

Page 17: PHP, Under The Hood - DPC

SAPI● Server API● "Starts" PHP and issues request(s)● Common SAPIs:

○ CLI○ CGI (and FastCGI)○ mod_php○ FPM○ etc...

Page 18: PHP, Under The Hood - DPC

Execution Pipeline

Page 19: PHP, Under The Hood - DPC

Execution PipelineStart Up

Request

Shut Down

SAPI

Page 20: PHP, Under The Hood - DPC

Execution PipelineStart Up

Request

Shut Down

SAPIConfig Init

Engine Init

Module Init

Page 21: PHP, Under The Hood - DPC

Zend/zend.c

Page 22: PHP, Under The Hood - DPC

PHPPHP/PHP.php

Page 23: PHP, Under The Hood - DPC

Execution PipelineStart Up

Request

Shut Down

SAPI

Module Shutdown

EngineShutdown

Page 24: PHP, Under The Hood - DPC

Zend/zend.c

Page 25: PHP, Under The Hood - DPC

Execution PipelineStart Up

Request

Shut Down

SAPI

Request Init

Compile Code

Execute Code

Request Shutdown

Page 26: PHP, Under The Hood - DPC
Page 27: PHP, Under The Hood - DPC

Compiler PipelineLexer

Page 28: PHP, Under The Hood - DPC

Lexer Step● C-PHP

○ Tokenizes using RE2C○ Produces Array of Tokens

● PHPPHP○ Uses core tokenizer (or emulative)○ PHPParser Powered

Page 29: PHP, Under The Hood - DPC

Zend/zend_language_scanner.l

Page 30: PHP, Under The Hood - DPC

PHPParser/Lexer/Emulative.php

Page 31: PHP, Under The Hood - DPC

Compiler PipelineLexer

Parser

Page 32: PHP, Under The Hood - DPC

Parse Step● C-PHP

○ Directly Generates OPCode Array○ BISON Powered

● PHPPHP○ Generates AST Structure○ PHPParser Powered

Page 33: PHP, Under The Hood - DPC

Zend/zend_language_parser.y

Page 34: PHP, Under The Hood - DPC

Compiler PipelineLexer

Parser

Compiler

Page 35: PHP, Under The Hood - DPC

Compile Step● C-PHP

○ Compiled Directly During Parse○ Single Pass Compilation

● PHPPHP○ Recurses Over AST○ Single Pass (for now)○

Page 36: PHP, Under The Hood - DPC

PHPPHP/Engine/Compiler.php

Page 37: PHP, Under The Hood - DPC

Compiler PipelineLexer

Parser

Compiler

OpCode

Page 38: PHP, Under The Hood - DPC

OpCodes● A Series of Operations

○ Just like Assembler Opcodes○ Represent "units of functionality"

● Designed to run on Virtual Machine○ Zend Engine○ Or PHPPHP!

Page 39: PHP, Under The Hood - DPC

$a = 1;$b = 2;echo $a + $b;

Page 40: PHP, Under The Hood - DPC
Page 41: PHP, Under The Hood - DPC

Notice Anything?

Page 42: PHP, Under The Hood - DPC
Page 43: PHP, Under The Hood - DPC
Page 44: PHP, Under The Hood - DPC

What If We Cached The OpCodes?

Page 45: PHP, Under The Hood - DPC

We Can Cache!● Given the compiler is Idempodent

○ (has no side-effects)○ (hint: it's not)

● OpCodes are really Pointers○ Swizzling!!!

Page 46: PHP, Under The Hood - DPC

In Other Words

Page 47: PHP, Under The Hood - DPC

OpCode CachingIs Hard!

Page 48: PHP, Under The Hood - DPC
Page 49: PHP, Under The Hood - DPC

Time To Execute!

Page 50: PHP, Under The Hood - DPC

Zend/zend_vm_execute.h

Page 51: PHP, Under The Hood - DPC

PHPPHP/Engine/Executor.php

Page 52: PHP, Under The Hood - DPC

Executor Pipeline

OpCode

Is Return?No Yes

Return

Page 53: PHP, Under The Hood - DPC
Page 54: PHP, Under The Hood - DPC

But What Are We Executing?

Page 55: PHP, Under The Hood - DPC

Zend/zend_vm_execute.h

Page 56: PHP, Under The Hood - DPC

Interesting Note:vm_execute.h

Is Generated By PHP

Page 57: PHP, Under The Hood - DPC

PHPPHP/Engine/OpLines/Add.php

Page 58: PHP, Under The Hood - DPC

Variables!

Page 59: PHP, Under The Hood - DPC

Zend/zend.h

Page 60: PHP, Under The Hood - DPC

PHPPHP/Engine/Zval/Value.php

Page 61: PHP, Under The Hood - DPC
Page 62: PHP, Under The Hood - DPC

Ref-Counting● RefCount + References

○ Allows Copy-On-Write● Variable Is "Deleted" When

RefCount = 0

● Enables Primitive Garbage Collection○ Circular GC is also implemented

Page 63: PHP, Under The Hood - DPC

That's All There Is To It!

Page 64: PHP, Under The Hood - DPC
Page 65: PHP, Under The Hood - DPC

Let's Look At An Example

Page 66: PHP, Under The Hood - DPC

$a = 1;$b = 2;var_dump( $a + $b);

Page 67: PHP, Under The Hood - DPC

line # op return operands----------------------------------- 2 0 ASSIGN !0, 1 3 1 ASSIGN !1, 2 6 2 ADD ~2 !0, !1 3 SEND_VAL ~2 4 DO_FCALL 'var_dump' 5 RETURN 1

Page 68: PHP, Under The Hood - DPC

[0] => PHPPHP\Engine\OpLines\Assign[1] => PHPPHP\Engine\OpLines\Assign[2] => PHPPHP\Engine\OpLines\Add[3] => PHPPHP\Engine\OpLines\InitFCallByName[4] => PHPPHP\Engine\OpLines\Send[5] => PHPPHP\Engine\OpLines\FunctionCall[6] => PHPPHP\Engine\OpLines\ReturnOp

Page 69: PHP, Under The Hood - DPC

[0] => PHPPHP\Engine\OpLines\Assign Object (

[op1] => PHPPHP\Engine\Zval\Ptr Object (

[zval:protected] => PHPPHP\Engine\Zval\Variable Object (

[name:protected] => PHPPHP\Engine\Zval\Ptr Object (

[zval:protected] => PHPPHP\Engine\Zval\Value Object (

[value:protected] => a

[refcount:protected] => 1

[isRef:protected] =>

[dtorFunc:protected] =>

)

)

[class:protected] =>

[zval:protected] =>

[executor:protected] =>

[scope] => 1

)

)

[op2] => PHPPHP\Engine\Zval\Ptr Object (

[zval:protected] => PHPPHP\Engine\Zval\Value Object (

[value:protected] => 1

[refcount:protected] => 1

[isRef:protected] =>

[dtorFunc:protected] =>

)

)

[result] =>

[lineno] => 2

)

Page 70: PHP, Under The Hood - DPC

PHPPHP/Engine/OpLines/Assign.php

Page 71: PHP, Under The Hood - DPC

PHPPHP/Engine/OpLines/Add.php

Page 72: PHP, Under The Hood - DPC

PHPPHP/Engine/OpLines/InitFCallByName.php

Page 73: PHP, Under The Hood - DPC

PHPPHP/Engine/OpLines/Send.php

Page 74: PHP, Under The Hood - DPC

PHPPHP/Engine/OpLines/FunctionCall.php

Page 75: PHP, Under The Hood - DPC

There's A Ton More

Page 76: PHP, Under The Hood - DPC
Page 77: PHP, Under The Hood - DPC
Page 78: PHP, Under The Hood - DPC

Get Involved!

Page 79: PHP, Under The Hood - DPC
Page 80: PHP, Under The Hood - DPC

More Info● github.com/php/php-src● lxr.php.net● github.com/ircmaxell/PHPPHP

● Reference Series○ wiki.php.net○ blog.ircmaxell.com

■ PHP Internals Series

Page 81: PHP, Under The Hood - DPC

Anthony FerraraJoind.in/8443

@[email protected]/ircmaxell


Top Related