<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>Derick Rethans - tag: vld</title>
    <link>http://derickrethans.nl/feed-vld.xml</link>
    <description>This feed shows the latest 15 items with the tag vld</description>
    <language>en-us</language>
    <copyright>All rights reserved - Derick Rethans</copyright>
    <managingEditor>derick@derickrethans.nl (Derick Rethans)</managingEditor>
    <pubDate>Thu, 20 Jan 2011 23:18:37 +0000</pubDate>
    <lastBuildDate>Thu, 20 Jan 2011 23:18:37 +0000</lastBuildDate>
    <generator>eZ Components Feed dev (http://ezcomponents.org/docs/tutorials/Feed)</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>60</ttl>
    <item>
      <title>VLD released</title>
      <link>http://derickrethans.nl/vld-released.html</link>
      <description>&lt;div class="article"&gt;
  &lt;div class="body"&gt;
    &lt;div class="articleListItem"&gt;
      &lt;h1&gt;&lt;a name="vld_released"/&gt;VLD released&lt;/h1&gt;
      &lt;dl class="head"/&gt;
      &lt;div class="articleMetaData"&gt;
        &lt;div class="location"&gt; London, UK&lt;/div&gt;
        &lt;div class="date"&gt;Monday, April 12th 2010, 21:57 BST&lt;/div&gt;
      &lt;/div&gt;
      &lt;p&gt;I just released a new version of &lt;a href="http://derickrethans.nl/projects.html#vld"&gt;VLD&lt;/a&gt; through &lt;a href="http://pecl.php.net/package/vld"&gt;PECL&lt;/a&gt;. This release features the path analytics code that I've outlined in an &lt;a href="http://derickrethans.nl/more-source-analysis-with-vld.html"&gt;earlier article&lt;/a&gt;. There are also a few fixes for PHP's current trunk, as well as the possibility to format output slightly like a CSV file. That feature was used to make &lt;a href="http://www.zapt.info/opcodes.html"&gt;this&lt;/a&gt; documentation of opcodes.&lt;/p&gt;
      &lt;p&gt;The path analysis algorithm that &lt;a href="http://derickrethans.nl/projects.html#vld"&gt;VLD&lt;/a&gt; now has, can possibly also be used to get path/branch coverage working within &lt;a href="http://xdebug.org"&gt;Xdebug's&lt;/a&gt; code-coverage framework. This is however not a minor task.&lt;/p&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
</description>
      <guid>201004122157</guid>
      <pubDate>Mon, 12 Apr 2010 20:57:00 +0000</pubDate>
    </item>
    <item>
      <title>More source analysis with VLD</title>
      <link>http://derickrethans.nl/more-source-analysis-with-vld.html</link>
      <description>&lt;div class="article"&gt;
  &lt;div class="body"&gt;
    &lt;div class="articleListItem"&gt;
      &lt;h1&gt;&lt;a name="more_source_analysis_with_vld"/&gt;More source analysis with VLD&lt;/h1&gt;
      &lt;dl class="head"/&gt;
      &lt;div class="articleMetaData"&gt;
        &lt;div class="location"&gt; London, UK&lt;/div&gt;
        &lt;div class="date"&gt;Friday, February 19th 2010, 11:27 GMT&lt;/div&gt;
      &lt;/div&gt;
      &lt;p&gt;&lt;a href="http://derickrethans.nl/projects.html#vld"&gt;VLD&lt;/a&gt; is a tool that I started working on years ago to visualise the opcode arrays in PHP. Opcode arrays are what PHP's compiler generates from your source code and can be compared to assembler code that is generated by a C compiler. Instead of it being directly executed by the CPU, it is instead executed by PHP's interpreter.&lt;/p&gt;
      &lt;p&gt;Over the years I've been adding some functionality, also aided by &lt;a href="http://ilia.ws"&gt;Ilia&lt;/a&gt; and some others, to show more information. For example Ilia has added a more verbose dumping format for opcodes (through the &lt;code&gt;vld.verbosity&lt;/code&gt; setting) whereas I have added routines to find out which ops in oparrays can never be reached. A very simple example of the latter is shown here:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
function test()
{
        echo "Hello!\n";
        return true;

        echo "This will not be executed.\n";
}
?&gt;

&lt;/pre&gt;
      &lt;p&gt;If we run the above through VLD with &lt;code&gt;php -dvld.active=1 test.php&lt;/code&gt;, you'll see the following output (I removed the part about the script body itself):&lt;/p&gt;
      &lt;pre&gt;Function test:
filename:       /tmp/test1.php
function name:  test
number of ops:  9
compiled vars:  none
line     # *  op           fetch  ext  return  operands
---------------------------------------------------------
   2     0  &gt;   EXT_NOP
   4     1      EXT_STMT
         2      ECHO                           'Hello%21%0A'
   5     3      EXT_STMT
         4    &gt; RETURN                         true
   7     5*     EXT_STMT
         6*     ECHO                           'This+will+not+be+executed.%0A'
   8     7*     EXT_STMT
         8*   &gt; RETURN                         null

End of function test.

&lt;/pre&gt;
      &lt;p&gt;Every opcode that has a &lt;code&gt;*&lt;/code&gt; after the number (like in &lt;code&gt;5*&lt;/code&gt;) is code that can not be reached, and can possibly be eliminated from the oparrays in an optimiser.&lt;/p&gt;
      &lt;p&gt;The dead code analysis routines have also made their way into &lt;a href="http://xdebug.org"&gt;Xdebug&lt;/a&gt; which uses them for the &lt;a href="http://xdebug.org/docs/code_coverage"&gt;code coverage&lt;/a&gt; functionality to highlight dead code. This mostly makes sense if you are running your code coverage together with unit tests such as you can do with &lt;a href="http://www.phpunit.de"&gt;PHPUnit&lt;/a&gt;.&lt;/p&gt;
      &lt;p&gt;Recently I've been working on some new functionality to visualise all the code &lt;em&gt;paths&lt;/em&gt; that make up each function. These new routines sit on top of the routines that do dead code analysis. Every branch instruction (such as &lt;code&gt;if&lt;/code&gt;, but also &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;foreach&lt;/code&gt;) is analysed and a list of branches is created. Each branch contains information about the line on which the branch starts, the starting and ending opcode numbers that belong to the branch, as well as to which other branches this branch can jump to. There can be either no linked branches (when for example a &lt;code&gt;return&lt;/code&gt; or &lt;code&gt;throw&lt;/code&gt; statement is found), one linked branch (for an unconditional jump) or two linked branches (on a branch instruction).  However, you need to be aware that internally, PHP's opcode don't always reflect the &lt;em&gt;source code&lt;/em&gt; exactly.&lt;/p&gt;
      &lt;p&gt;Once all the branches and their links are found, another algorithm runs to figure out which paths can be created out of all the branches. It is best to illustrate this with an example. So let us look at the following script:&lt;/p&gt;
      &lt;pre&gt;&lt;?php
function test()
{
        for( $i = 0; $i &lt; 10; $i++ )
        {
                if ( $i &lt; 5 )
                {
                        echo "-";
                }
                else
                {
                        echo "+";
                }
        }
        echo "\n";
}
?&gt;

&lt;/pre&gt;
      &lt;p&gt;In this script we have a &lt;code&gt;for&lt;/code&gt;-loop with a nested &lt;code&gt;if&lt;/code&gt; construct. When we run this script through VLD (with &lt;code&gt;php -dvld.verbosity=0 -dvld.dump_paths=1
-dvld.active=1 test2.php&lt;/code&gt;) we get the following output (again, only the &lt;code&gt;test()&lt;/code&gt; function and with some white space modifications):&lt;/p&gt;
      &lt;pre&gt;Function test:
filename:       /tmp/test2.php
function name:  test
number of ops:  22
compiled vars:  !0 = $i
line     # *  op             fetch  ext  return  operands
-----------------------------------------------------------
   2     0  &gt;   EXT_NOP
   4     1      EXT_STMT
         2      ASSIGN                             !0, 0
         3  &gt;   IS_SMALLER                 ~1      !0, 10
         4      EXT_STMT
         5    &gt; JMPZNZ                  9          ~1, -&gt;18
         6  &gt;   POST_INC                   ~2      !0
         7      FREE                               ~2
         8    &gt; JMP                                -&gt;3
   6     9  &gt;   EXT_STMT
        10      IS_SMALLER                 ~3      !0, 5
   7    11    &gt; JMPZ                               ~3, -&gt;15
   8    12  &gt;   EXT_STMT
        13      ECHO                               '-'
   9    14    &gt; JMP                                -&gt;17
  12    15  &gt;   EXT_STMT
        16      ECHO                               '%2B'
  14    17  &gt; &gt; JMP                                -&gt;6
  15    18  &gt;   EXT_STMT
        19      ECHO                               '%0A'
  16    20      EXT_STMT
        21    &gt; RETURN                             null

branch: #  0; line:  2- 4; sop:  0; eop:  2; out1:   3
branch: #  3; line:  4- 4; sop:  3; eop:  5; out1:  18; out2:   9
branch: #  6; line:  4- 4; sop:  6; eop:  8; out1:   3
branch: #  9; line:  6- 7; sop:  9; eop: 11; out1:  12; out2:  15
branch: # 12; line:  8- 9; sop: 12; eop: 14; out1:  17
branch: # 15; line: 12-14; sop: 15; eop: 16; out1:  17
branch: # 17; line: 14-14; sop: 17; eop: 17; out1:   6
branch: # 18; line: 15-16; sop: 18; eop: 21
path #1: 0, 3, 18,
path #2: 0, 3, 9, 12, 17, 6, 3, 18,
path #3: 0, 3, 9, 15, 17, 6, 3, 18,
End of function test.

&lt;/pre&gt;
      &lt;p&gt;This dump consists of a few different parts. First of all we can see some basic information containing the name, the number of ops (22) and the compiled variables. The second part is a dump of all the opcodes that make up this function. The last part contains information about all the branches and the possible paths.  This information is a bit hard to visualize in its textual form, so I've also added some code that dumps this information to a file format that the &lt;a href="http://graphviz.org/"&gt;GraphViz&lt;/a&gt; tool "dot" can use to create a pretty graph. For this we re-run the previous PHP invocation as &lt;code&gt;php -dvld.dump_paths=1
-dvld.verbosity=0 -dvld.save_paths=1 -dvld.active=1 test2.php&lt;/code&gt;. This creates the file &lt;code&gt;/tmp/paths.dot&lt;/code&gt; that "dot" can use. If we run &lt;code&gt;dot -Tpng
/tmp/paths.dot &gt; /tmp/paths.png&lt;/code&gt; we end up with the following picture:&lt;/p&gt;
      &lt;img src="http://derickrethans.nl/images/vld-paths.png" alt="vld-paths.png"/&gt;
      &lt;p&gt;If we put this graph next to the code, we can explain how this works. Every branch is named by the number of the first opcode in that branch:&lt;/p&gt;
      &lt;ul&gt;
        &lt;li&gt;
          &lt;p&gt;&lt;code&gt;op #1&lt;/code&gt; is the assignment of &lt;code&gt;$i&lt;/code&gt; in line 4.&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;&lt;code&gt;op #3&lt;/code&gt; is the &lt;em&gt;loop test&lt;/em&gt; in line 4. If the condition doesn't match, we jump to &lt;code&gt;op #18&lt;/code&gt; on line 16 that echos the newline.&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;&lt;code&gt;op #9&lt;/code&gt; is the &lt;code&gt;if&lt;/code&gt; condition on line 6.&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;&lt;code&gt;op #12&lt;/code&gt; is when the &lt;code&gt;if&lt;/code&gt; condition returns true and&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;&lt;code&gt;op #15&lt;/code&gt; is when the &lt;code&gt;if&lt;/code&gt; condition returns false.&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;&lt;code&gt;op #17&lt;/code&gt; sits behind both &lt;code&gt;op #12&lt;/code&gt; and &lt;code&gt;op #15&lt;/code&gt; and makes sure there is a jump to the counting expression in &lt;code&gt;#op 6&lt;/code&gt;.&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;&lt;code&gt;op #6&lt;/code&gt; is the post increment operation on line 4 which will then again be followed by &lt;code&gt;op #3&lt;/code&gt; to check whether the end of the loop has been reached.&lt;/p&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
      &lt;p&gt;This is of course a very simple example, but it also works for (multiple) classes and functions in a file. You just need to make sure to tell VLD that you don't want the code &lt;em&gt;executed&lt;/em&gt; as the output could be very large. You can use the &lt;code&gt;vld.execute=0&lt;/code&gt; php.ini setting for that.&lt;/p&gt;
      &lt;p&gt;I hope this new functionality can spread some light on how loops etc. work in PHP. In order to play with the code, you need to check-out VLD from my SVN with &lt;code&gt;svn co svn://svn.xdebug.org/svn/php/vld/trunk vld&lt;/code&gt;. You can also view the code on-line at &lt;a href="http://svn.xdebug.org/cgi-bin/viewvc.cgi/vld/trunk/?root=php"&gt;http://svn.xdebug.org/cgi-bin/viewvc.cgi/vld/trunk/?root=php&lt;/a&gt;. Look out for a new release coming soon!&lt;/p&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
</description>
      <guid>201002191145</guid>
      <pubDate>Fri, 19 Feb 2010 11:27:00 +0000</pubDate>
    </item>
  </channel>
</rss>

