Back to index

php5  5.3.10
zend_vm_gen.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003    +----------------------------------------------------------------------+
00004    | Zend Engine                                                          |
00005    +----------------------------------------------------------------------+
00006    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00007    +----------------------------------------------------------------------+
00008    | This source file is subject to version 2.00 of the Zend license,     |
00009    | that is bundled with this package in the file LICENSE, and is        |
00010    | available through the world-wide-web at the following url:           |
00011    | http://www.zend.com/license/2_00.txt.                                |
00012    | If you did not receive a copy of the Zend license and are unable to  |
00013    | obtain it through the world-wide-web, please send a note to          |
00014    | license@zend.com so we can mail you a copy immediately.              |
00015    +----------------------------------------------------------------------+
00016    | Authors: Dmitry Stogov <dmitry@zend.com>                             |
00017    +----------------------------------------------------------------------+
00018 
00019         $Id: zend_vm_gen.php 321634 2012-01-01 13:15:04Z felipe $
00020 */
00021 
00022 $header_text = <<< DATA
00023 /*
00024    +----------------------------------------------------------------------+
00025    | Zend Engine                                                          |
00026    +----------------------------------------------------------------------+
00027    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00028    +----------------------------------------------------------------------+
00029    | This source file is subject to version 2.00 of the Zend license,     |
00030    | that is bundled with this package in the file LICENSE, and is        |
00031    | available through the world-wide-web at the following url:           |
00032    | http://www.zend.com/license/2_00.txt.                                |
00033    | If you did not receive a copy of the Zend license and are unable to  |
00034    | obtain it through the world-wide-web, please send a note to          |
00035    | license@zend.com so we can mail you a copy immediately.              |
00036    +----------------------------------------------------------------------+
00037    | Authors: Andi Gutmans <andi@zend.com>                                |
00038    |          Zeev Suraski <zeev@zend.com>                                |
00039    |          Dmitry Stogov <dmitry@zend.com>                             |
00040    +----------------------------------------------------------------------+
00041 */
00042 
00043 
00044 DATA;
00045 
00046 /*
00047        This script creates zend_vm_execute.h and zend_vm_opcodes.h
00048        from existing zend_vm_def.h and zend_vm_execute.skl
00049 */
00050 
00051 error_reporting(E_ALL);
00052 
00053 define("ZEND_VM_KIND_CALL",   1);
00054 define("ZEND_VM_KIND_SWITCH", 2);
00055 define("ZEND_VM_KIND_GOTO",   3);
00056 
00057 $op_types = array(
00058        "ANY",
00059        "CONST",
00060        "TMP",
00061        "VAR",
00062        "UNUSED",
00063        "CV"
00064 );
00065 
00066 $prefix = array(
00067        "ANY"    => "",
00068        "TMP"    => "_TMP",
00069        "VAR"    => "_VAR",
00070        "CONST"  => "_CONST",
00071        "UNUSED" => "_UNUSED",
00072        "CV"     => "_CV",
00073 );
00074 
00075 $typecode = array(
00076        "ANY"    => 0,
00077        "TMP"    => 1,
00078        "VAR"    => 2,
00079        "CONST"  => 0,
00080        "UNUSED" => 3,
00081        "CV"     => 4,
00082 );
00083 
00084 $op1_type = array(
00085        "ANY"    => "opline->op1.op_type",
00086        "TMP"    => "IS_TMP_VAR",
00087        "VAR"    => "IS_VAR",
00088        "CONST"  => "IS_CONST",
00089        "UNUSED" => "IS_UNUSED",
00090        "CV"     => "IS_CV",
00091 );
00092 
00093 $op2_type = array(
00094        "ANY"    => "opline->op2.op_type",
00095        "TMP"    => "IS_TMP_VAR",
00096        "VAR"    => "IS_VAR",
00097        "CONST"  => "IS_CONST",
00098        "UNUSED" => "IS_UNUSED",
00099        "CV"     => "IS_CV",
00100 );
00101 
00102 $op1_free = array(
00103        "ANY"    => "(free_op1.var != NULL)",
00104        "TMP"    => "1",
00105        "VAR"    => "(free_op1.var != NULL)",
00106        "CONST"  => "0",
00107        "UNUSED" => "0",
00108        "CV"     => "0",
00109 );
00110 
00111 $op2_free = array(
00112        "ANY"    => "(free_op2.var != NULL)",
00113        "TMP"    => "1",
00114        "VAR"    => "(free_op2.var != NULL)",
00115        "CONST"  => "0",
00116        "UNUSED" => "0",
00117        "CV"     => "0",
00118 );
00119 
00120 $op1_get_zval_ptr = array(
00121        "ANY"    => "get_zval_ptr(&opline->op1, EX(Ts), &free_op1, \\1)",
00122        "TMP"    => "_get_zval_ptr_tmp(&opline->op1, EX(Ts), &free_op1 TSRMLS_CC)",
00123        "VAR"    => "_get_zval_ptr_var(&opline->op1, EX(Ts), &free_op1 TSRMLS_CC)",
00124        "CONST"  => "&opline->op1.u.constant",
00125        "UNUSED" => "NULL",
00126        "CV"     => "_get_zval_ptr_cv(&opline->op1, EX(Ts), \\1 TSRMLS_CC)",
00127 );
00128 
00129 $op2_get_zval_ptr = array(
00130        "ANY"    => "get_zval_ptr(&opline->op2, EX(Ts), &free_op2, \\1)",
00131        "TMP"    => "_get_zval_ptr_tmp(&opline->op2, EX(Ts), &free_op2 TSRMLS_CC)",
00132        "VAR"    => "_get_zval_ptr_var(&opline->op2, EX(Ts), &free_op2 TSRMLS_CC)",
00133        "CONST"  => "&opline->op2.u.constant",
00134        "UNUSED" => "NULL",
00135        "CV"     => "_get_zval_ptr_cv(&opline->op2, EX(Ts), \\1 TSRMLS_CC)",
00136 );
00137 
00138 $op1_get_zval_ptr_ptr = array(
00139        "ANY"    => "get_zval_ptr_ptr(&opline->op1, EX(Ts), &free_op1, \\1)",
00140        "TMP"    => "NULL",
00141        "VAR"    => "_get_zval_ptr_ptr_var(&opline->op1, EX(Ts), &free_op1 TSRMLS_CC)",
00142        "CONST"  => "NULL",
00143        "UNUSED" => "NULL",
00144        "CV"     => "_get_zval_ptr_ptr_cv(&opline->op1, EX(Ts), \\1 TSRMLS_CC)",
00145 );
00146 
00147 $op2_get_zval_ptr_ptr = array(
00148        "ANY"    => "get_zval_ptr_ptr(&opline->op2, EX(Ts), &free_op2, \\1)",
00149        "TMP"    => "NULL",
00150        "VAR"    => "_get_zval_ptr_ptr_var(&opline->op2, EX(Ts), &free_op2 TSRMLS_CC)",
00151        "CONST"  => "NULL",
00152        "UNUSED" => "NULL",
00153        "CV"     => "_get_zval_ptr_ptr_cv(&opline->op2, EX(Ts), \\1 TSRMLS_CC)",
00154 );
00155 
00156 $op1_get_obj_zval_ptr = array(
00157        "ANY"    => "get_obj_zval_ptr(&opline->op1, EX(Ts), &free_op1, \\1)",
00158        "TMP"    => "_get_zval_ptr_tmp(&opline->op1, EX(Ts), &free_op1 TSRMLS_CC)",
00159        "VAR"    => "_get_zval_ptr_var(&opline->op1, EX(Ts), &free_op1 TSRMLS_CC)",
00160        "CONST"  => "&opline->op1.u.constant",
00161        "UNUSED" => "_get_obj_zval_ptr_unused(TSRMLS_C)",
00162        "CV"     => "_get_zval_ptr_cv(&opline->op1, EX(Ts), \\1 TSRMLS_CC)",
00163 );
00164 
00165 $op2_get_obj_zval_ptr = array(
00166        "ANY"    => "get_obj_zval_ptr(&opline->op2, EX(Ts), &free_op2, \\1)",
00167        "TMP"    => "_get_zval_ptr_tmp(&opline->op2, EX(Ts), &free_op2 TSRMLS_CC)",
00168        "VAR"    => "_get_zval_ptr_var(&opline->op2, EX(Ts), &free_op2 TSRMLS_CC)",
00169        "CONST"  => "&opline->op2.u.constant",
00170        "UNUSED" => "_get_obj_zval_ptr_unused(TSRMLS_C)",
00171        "CV"     => "_get_zval_ptr_cv(&opline->op2, EX(Ts), \\1 TSRMLS_CC)",
00172 );
00173 
00174 $op1_get_obj_zval_ptr_ptr = array(
00175        "ANY"    => "get_obj_zval_ptr_ptr(&opline->op1, EX(Ts), &free_op1, \\1)",
00176        "TMP"    => "NULL",
00177        "VAR"    => "_get_zval_ptr_ptr_var(&opline->op1, EX(Ts), &free_op1 TSRMLS_CC)",
00178        "CONST"  => "NULL",
00179        "UNUSED" => "_get_obj_zval_ptr_ptr_unused(TSRMLS_C)",
00180        "CV"     => "_get_zval_ptr_ptr_cv(&opline->op1, EX(Ts), \\1 TSRMLS_CC)",
00181 );
00182 
00183 $op2_get_obj_zval_ptr_ptr = array(
00184        "ANY"    => "get_obj_zval_ptr_ptr(&opline->op2, EX(Ts), &free_op2, \\1)",
00185        "TMP"    => "NULL",
00186        "VAR"    => "_get_zval_ptr_ptr_var(&opline->op2, EX(Ts), &free_op2 TSRMLS_CC)",
00187        "CONST"  => "NULL",
00188        "UNUSED" => "_get_obj_zval_ptr_ptr_unused(TSRMLS_C)",
00189        "CV"     => "_get_zval_ptr_ptr_cv(&opline->op2, EX(Ts), \\1 TSRMLS_CC)",
00190 );
00191 
00192 $op1_is_tmp_free = array(
00193        "ANY"    => "IS_TMP_FREE(free_op1)",
00194        "TMP"    => "1",
00195        "VAR"    => "0",
00196        "CONST"  => "0",
00197        "UNUSED" => "0",
00198        "CV"     => "0",
00199 );
00200 
00201 $op2_is_tmp_free = array(
00202        "ANY"    => "IS_TMP_FREE(free_op2)",
00203        "TMP"    => "1",
00204        "VAR"    => "0",
00205        "CONST"  => "0",
00206        "UNUSED" => "0",
00207        "CV"     => "0",
00208 );
00209 
00210 $op1_free_op = array(
00211        "ANY"    => "FREE_OP(free_op1)",
00212        "TMP"    => "zval_dtor(free_op1.var)",
00213        "VAR"    => "if (free_op1.var) {zval_ptr_dtor(&free_op1.var);}",
00214        "CONST"  => "",
00215        "UNUSED" => "",
00216        "CV"     => "",
00217 );
00218 
00219 $op2_free_op = array(
00220        "ANY"    => "FREE_OP(free_op2)",
00221        "TMP"    => "zval_dtor(free_op2.var)",
00222        "VAR"    => "if (free_op2.var) {zval_ptr_dtor(&free_op2.var);}",
00223        "CONST"  => "",
00224        "UNUSED" => "",
00225        "CV"     => "",
00226 );
00227 
00228 $op1_free_op_if_var = array(
00229        "ANY"    => "FREE_OP_IF_VAR(free_op1)",
00230        "TMP"    => "",
00231        "VAR"    => "if (free_op1.var) {zval_ptr_dtor(&free_op1.var);}",
00232        "CONST"  => "",
00233        "UNUSED" => "",
00234        "CV"     => "",
00235 );
00236 
00237 $op2_free_op_if_var = array(
00238        "ANY"    => "FREE_OP_IF_VAR(free_op2)",
00239        "TMP"    => "",
00240        "VAR"    => "if (free_op2.var) {zval_ptr_dtor(&free_op2.var);}",
00241        "CONST"  => "",
00242        "UNUSED" => "",
00243        "CV"     => "",
00244 );
00245 
00246 $op1_free_op_var_ptr = array(
00247        "ANY"    => "if (free_op1.var) {zval_ptr_dtor(&free_op1.var);}",
00248        "TMP"    => "",
00249        "VAR"    => "if (free_op1.var) {zval_ptr_dtor(&free_op1.var);}",
00250        "CONST"  => "",
00251        "UNUSED" => "",
00252        "CV"     => "",
00253 );
00254 
00255 $op2_free_op_var_ptr = array(
00256        "ANY"    => "if (free_op2.var) {zval_ptr_dtor(&free_op2.var);}",
00257        "TMP"    => "",
00258        "VAR"    => "if (free_op2.var) {zval_ptr_dtor(&free_op2.var);}",
00259        "CONST"  => "",
00260        "UNUSED" => "",
00261        "CV"     => "",
00262 );
00263 
00264 $list    = array(); // list of opcode handlers and helpers in original order
00265 $opcodes = array(); // opcode handlers by code
00266 $helpers = array(); // opcode helpers by name
00267 $params  = array(); // parameters of helpers
00268 $opnames = array(); // opcode name to code mapping
00269 $line_no = 1;
00270 
00271 // Writes $s into resulting executor
00272 function out($f, $s) {
00273        global $line_no;
00274 
00275        fputs($f,$s);
00276        $line_no += substr_count($s, "\n");
00277 }
00278 
00279 // Resets #line directives in resulting executor
00280 function out_line($f) {
00281        global $line_no, $executor_file;
00282 
00283        fputs($f,"#line ".($line_no+1)." \"".$executor_file."\"\n");
00284        ++$line_no;
00285 }
00286 
00287 // Returns name of specialized helper
00288 function helper_name($name, $spec, $op1, $op2) {
00289        global $prefix, $helpers;
00290 
00291        if (isset($helpers[$name])) {
00292               // If we haven't helper with specified spicialized operands then
00293               // using unspecialized helper
00294               if (!isset($helpers[$name]["op1"][$op1]) &&
00295                   isset($helpers[$name]["op1"]["ANY"])) {
00296                      $op1 = "ANY";
00297               }
00298               if (!isset($helpers[$name]["op2"][$op2]) &&
00299                   isset($helpers[$name]["op2"]["ANY"])) {
00300                      $op2 = "ANY";
00301               }
00302        }
00303        return $name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2];
00304 }
00305 
00306 // Generates code for opcode handler or helper
00307 function gen_code($f, $spec, $kind, $export, $code, $op1, $op2, $name) {
00308        global $op1_type, $op2_type, $op1_get_zval_ptr, $op2_get_zval_ptr,
00309               $op1_get_zval_ptr_ptr, $op2_get_zval_ptr_ptr,
00310               $op1_get_obj_zval_ptr, $op2_get_obj_zval_ptr,
00311               $op1_get_obj_zval_ptr_ptr, $op2_get_obj_zval_ptr_ptr,
00312               $op1_is_tmp_free, $op2_is_tmp_free, $op1_free, $op2_free,
00313               $op1_free_op, $op2_free_op, $op1_free_op_if_var, $op2_free_op_if_var,
00314               $op1_free_op_var_ptr, $op2_free_op_var_ptr, $prefix;
00315 
00316        // Specializing
00317        $code = preg_replace(
00318               array(
00319                      "/OP1_TYPE/",
00320                      "/OP2_TYPE/",
00321                      "/OP1_FREE/",
00322                      "/OP2_FREE/",
00323                      "/GET_OP1_ZVAL_PTR\(([^)]*)\)/",
00324                      "/GET_OP2_ZVAL_PTR\(([^)]*)\)/",
00325                      "/GET_OP1_ZVAL_PTR_PTR\(([^)]*)\)/",
00326                      "/GET_OP2_ZVAL_PTR_PTR\(([^)]*)\)/",
00327                      "/GET_OP1_OBJ_ZVAL_PTR\(([^)]*)\)/",
00328                      "/GET_OP2_OBJ_ZVAL_PTR\(([^)]*)\)/",
00329                      "/GET_OP1_OBJ_ZVAL_PTR_PTR\(([^)]*)\)/",
00330                      "/GET_OP2_OBJ_ZVAL_PTR_PTR\(([^)]*)\)/",
00331                      "/IS_OP1_TMP_FREE\(\)/",
00332                      "/IS_OP2_TMP_FREE\(\)/",
00333                      "/FREE_OP1\(\)/",
00334                      "/FREE_OP2\(\)/",
00335                      "/FREE_OP1_IF_VAR\(\)/",
00336                      "/FREE_OP2_IF_VAR\(\)/",
00337                      "/FREE_OP1_VAR_PTR\(\)/",
00338                      "/FREE_OP2_VAR_PTR\(\)/",
00339                      "/^#ifdef\s+ZEND_VM_SPEC\s*\n/m",
00340                      "/^#ifndef\s+ZEND_VM_SPEC\s*\n/m",
00341                      "/\!defined\(ZEND_VM_SPEC\)/m",
00342                      "/defined\(ZEND_VM_SPEC\)/m",
00343                      "/ZEND_VM_C_LABEL\(\s*([A-Za-z_]*)\s*\)/m",
00344                      "/ZEND_VM_C_GOTO\(\s*([A-Za-z_]*)\s*\)/m",
00345                      "/^#if\s+1\s*\\|\\|.*[^\\\\]$/m",
00346                      "/^#if\s+0\s*&&.*[^\\\\]$/m",
00347                      "/^#ifdef\s+ZEND_VM_EXPORT\s*\n/m",
00348                      "/^#ifndef\s+ZEND_VM_EXPORT\s*\n/m"
00349               ),
00350               array(
00351                      $op1_type[$op1],
00352                      $op2_type[$op2],
00353                      $op1_free[$op1],
00354                      $op2_free[$op2],
00355                      $op1_get_zval_ptr[$op1],
00356                      $op2_get_zval_ptr[$op2],
00357                      $op1_get_zval_ptr_ptr[$op1],
00358                      $op2_get_zval_ptr_ptr[$op2],
00359                      $op1_get_obj_zval_ptr[$op1],
00360                      $op2_get_obj_zval_ptr[$op2],
00361                      $op1_get_obj_zval_ptr_ptr[$op1],
00362                      $op2_get_obj_zval_ptr_ptr[$op2],
00363                      $op1_is_tmp_free[$op1],
00364                      $op2_is_tmp_free[$op2],
00365                      $op1_free_op[$op1],
00366                      $op2_free_op[$op2],
00367                      $op1_free_op_if_var[$op1],
00368                      $op2_free_op_if_var[$op2],
00369                      $op1_free_op_var_ptr[$op1],
00370                      $op2_free_op_var_ptr[$op2],
00371                      ($op1!="ANY"||$op2!="ANY")?"#if 1\n":"#if 0\n",
00372                      ($op1!="ANY"||$op2!="ANY")?"#if 0\n":"#if 1\n",
00373                      ($op1!="ANY"||$op2!="ANY")?"0":"1",
00374                      ($op1!="ANY"||$op2!="ANY")?"1":"0",
00375                      "\\1".(($spec && $kind != ZEND_VM_KIND_CALL)?("_SPEC".$prefix[$op1].$prefix[$op2]):""),
00376                      "goto \\1".(($spec && $kind != ZEND_VM_KIND_CALL)?("_SPEC".$prefix[$op1].$prefix[$op2]):""),
00377                      "#if 1",
00378                      "#if 0",
00379                      $export?"#if 1\n":"#if 0\n",
00380                      $export?"#if 0\n":"#if 1\n"
00381               ),
00382               $code);
00383 
00384        if (0 && strpos($code, '{') === 0) {
00385               $code = "{\n\tfprintf(stderr, \"$name\\n\");\n" . substr($code, 1);
00386        }
00387        // Updating code according to selected threading model
00388        switch($kind) {
00389               case ZEND_VM_KIND_CALL:
00390                      $code = preg_replace(
00391                             array(
00392                                    "/EXECUTE_DATA/m",
00393                                    "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
00394                                    "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*\)/me",
00395                                    "/ZEND_VM_DISPATCH_TO_HELPER_EX\(\s*([A-Za-z_]*)\s*,\s*[A-Za-z_]*\s*,\s*(.*)\s*\);/me",
00396                             ),
00397                             array(
00398                                    "execute_data",
00399                                    "return \\1".($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)",
00400                                    "'return '.helper_name('\\1',$spec,'$op1','$op2').'(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)'",
00401                                    "'return '.helper_name('\\1',$spec,'$op1','$op2').'(\\2, ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);'",
00402                             ),
00403                             $code);
00404                      break;
00405               case ZEND_VM_KIND_SWITCH:
00406                      $code = preg_replace(
00407                             array(
00408                                    "/EXECUTE_DATA/m",
00409                                    "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
00410                                    "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*\)/me",
00411                                    "/ZEND_VM_DISPATCH_TO_HELPER_EX\(\s*([A-Za-z_]*)\s*,\s*([A-Za-z_]*)\s*,\s*(.*)\s*\);/me",
00412                             ),
00413                             array(
00414                                    "execute_data",
00415                                    "goto \\1".($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_LABEL",
00416                                    "'goto '.helper_name('\\1',$spec,'$op1','$op2')",
00417                                    "'\\2 = \\3; goto '.helper_name('\\1',$spec,'$op1','$op2').';'",
00418                             ),
00419                             $code);
00420                      break;
00421               case ZEND_VM_KIND_GOTO:
00422                      $code = preg_replace(
00423                             array(
00424                                    "/EXECUTE_DATA/m",
00425                                    "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
00426                                    "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*\)/me",
00427                                    "/ZEND_VM_DISPATCH_TO_HELPER_EX\(\s*([A-Za-z_]*)\s*,\s*([A-Za-z_]*)\s*,\s*(.*)\s*\);/me",
00428                             ),
00429                             array(
00430                                    "execute_data",
00431                                    "goto \\1".($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_HANDLER",
00432                                    "'goto '.helper_name('\\1',$spec,'$op1','$op2')",
00433                                    "'\\2 = \\3; goto '.helper_name('\\1',$spec,'$op1','$op2').';'",
00434                             ),
00435                             $code);
00436                      break;
00437        }
00438 
00439        /* Remove unused free_op1 and free_op2 declarations */
00440        if ($spec && preg_match_all('/^\s*zend_free_op\s+[^;]+;\s*$/me', $code, $matches, PREG_SET_ORDER)) {
00441               $n = 0;
00442               foreach ($matches as $match) {
00443                 $code = preg_replace('/'.preg_quote($match[0],'/').'/', "\$D$n", $code);
00444                 ++$n;
00445               }
00446               $del_free_op1 = (strpos($code, "free_op1") === false);
00447               $del_free_op2 = (strpos($code, "free_op2") === false);
00448               $n = 0;
00449               foreach ($matches as $match) {
00450                      $dcl = $match[0];
00451                      $changed = 0;
00452                      if ($del_free_op1 && strpos($dcl, "free_op1") !== false) {
00453                             $dcl = preg_replace("/free_op1\s*,\s*/", "", $dcl);
00454                             $dcl = preg_replace("/free_op1\s*;/", ";", $dcl);
00455                             $changed = 1;
00456                      }
00457                      if ($del_free_op2 && strpos($dcl, "free_op2") !== false) {
00458                             $dcl = preg_replace("/free_op2\s*,\s*/", "", $dcl);
00459                             $dcl = preg_replace("/free_op2\s*;/", ";", $dcl);
00460                             $changed = 1;
00461                      }
00462                      if ($changed) {
00463                             $dcl = preg_replace("/,\s*;/", ";", $dcl);
00464                             $dcl = preg_replace("/zend_free_op\s*;/", "", $dcl);
00465                      }
00466                 $code = preg_replace("/\\\$D$n/", $dcl, $code);
00467                 ++$n;
00468               }
00469        }
00470 
00471        /* Remove unnecessary ';' */
00472        $code = preg_replace('/^\s*;\s*$/m', '', $code);
00473 
00474        /* Remove WS */
00475        $code = preg_replace('/[ \t]+\n/m', "\n", $code);
00476 
00477        out($f, $code);
00478 }
00479 
00480 // Generates opcode handler
00481 function gen_handler($f, $spec, $kind, $name, $op1, $op2, $use, $code, $lineno) {
00482        global $definition_file, $prefix, $typecode, $opnames;
00483 
00484        if (ZEND_VM_LINES) {
00485               out($f, "#line $lineno \"$definition_file\"\n");
00486        }
00487 
00488        // Generate opcode handler's entry point according to selected threading model
00489        switch($kind) {
00490               case ZEND_VM_KIND_CALL:
00491                      out($f,"static int ZEND_FASTCALL  ".$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
00492                      break;
00493               case ZEND_VM_KIND_SWITCH:
00494                      if ($spec) {
00495                             out($f,"case ".((string)($opnames[$name]*25+($typecode[$op1]*5)+$typecode[$op2])).": /*".$name."_SPEC".$prefix[$op1].$prefix[$op2]."_HANDLER*/");
00496                      } else {
00497                             out($f,"case ".$name.":");
00498                      }
00499                      if ($use) {
00500                             // This handler is used by other handlers. We will add label to call it.
00501                             out($f," ".$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_LABEL:\n");
00502                      } else {
00503                             out($f,"\n");
00504                      }
00505                      break;
00506               case ZEND_VM_KIND_GOTO:
00507                      out($f,$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_HANDLER:\n");
00508                      break;
00509        }
00510 
00511        // Generate opcode handler's code
00512        gen_code($f, $spec, $kind, 0, $code, $op1, $op2, $name);
00513 }
00514 
00515 // Generates helper
00516 function gen_helper($f, $spec, $kind, $name, $op1, $op2, $param, $code, $lineno) {
00517        global $definition_file, $prefix;
00518 
00519        if (ZEND_VM_LINES) {
00520               out($f, "#line $lineno \"$definition_file\"\n");
00521        }
00522 
00523        // Generate helper's entry point according to selected threading model
00524        switch($kind) {
00525               case ZEND_VM_KIND_CALL:
00526                      if ($param == null) {
00527                        // Helper without parameters
00528                             out($f, "static int ZEND_FASTCALL ".$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."(ZEND_OPCODE_HANDLER_ARGS)\n");
00529                      } else {
00530                        // Helper with parameter
00531                             out($f, "static int ZEND_FASTCALL ".$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."(".$param.", ZEND_OPCODE_HANDLER_ARGS)\n");
00532                      }
00533                      break;
00534               case ZEND_VM_KIND_SWITCH:
00535                      out($f, $name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2].":\n");
00536                      break;
00537               case ZEND_VM_KIND_GOTO:
00538                      out($f, $name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2].":\n");
00539                      break;
00540        }
00541 
00542        // Generate helper's code
00543        gen_code($f, $spec, $kind, 0, $code, $op1, $op2, $name);
00544 }
00545 
00546 // Generates array of opcode handlers (specialized or unspecialized)
00547 function gen_labels($f, $spec, $kind, $prolog) {
00548        global $opcodes, $op_types, $prefix, $typecode;
00549 
00550        $next = 0;
00551        if ($spec) {
00552          // Emit labels for specialized executor
00553 
00554          // For each opcode in opcode number order
00555               foreach($opcodes as $num => $dsc) {
00556                      while ($next != $num) {
00557                        // If some opcode numbers are not used then fill hole with pointers
00558                        // to handler of undefined opcode
00559                             $op1t = $op_types;
00560                             // For each op1.op_type except ANY
00561                             foreach($op1t as $op1) {
00562                                    if ($op1 != "ANY") {
00563                                           $op2t = $op_types;
00564                                           // For each op2.op_type except ANY
00565                                           foreach($op2t as $op2) {
00566                                                  if ($op2 != "ANY") {
00567                                                    // Emit pointer to handler of undefined opcode
00568                                                         switch ($kind) {
00569                                                                case ZEND_VM_KIND_CALL:
00570                                                                       out($f,$prolog."ZEND_NULL_HANDLER,\n");
00571                                                                       break;
00572                                                                case ZEND_VM_KIND_SWITCH:
00573                                                                       out($f,$prolog."(opcode_handler_t)-1,\n");
00574                                                                       break;
00575                                                                case ZEND_VM_KIND_GOTO:
00576                                                                       out($f,$prolog."(opcode_handler_t)&&ZEND_NULL_HANDLER,\n");
00577                                                                       break;
00578                                                         }
00579                                                  }
00580                                           }
00581                                    }
00582                             }
00583                             $next++;
00584                      }
00585                      $next = $num + 1;
00586                      $op1t = $op_types;
00587                      // For each op1.op_type except ANY
00588                      foreach($op1t as $op1) {
00589                             if ($op1 != "ANY") {
00590                                    if (!isset($dsc["op1"][$op1])) {
00591                                           // Try to use unspecialized handler
00592                                           $op1 = "ANY";
00593                                    }
00594                                    $op2t = $op_types;
00595                                    // For each op2.op_type except ANY
00596                                    foreach($op2t as $op2) {
00597                                           if ($op2 != "ANY") {
00598                                                  if (!isset($dsc["op2"][$op2])) {
00599                                                         // Try to use unspecialized handler
00600                                                         $op2 = "ANY";
00601                                                  }
00602                                                  // Check if specialized handler is defined
00603                                                  if (isset($dsc["op1"][$op1]) &&
00604                                                      isset($dsc["op2"][$op2])) {
00605                                                    // Emit pointer to specialized handler
00606                                                         switch ($kind) {
00607                                                                case ZEND_VM_KIND_CALL:
00608                                                                       out($f,$prolog.$dsc["op"]."_SPEC".$prefix[$op1].$prefix[$op2]."_HANDLER,\n");
00609                                                                       break;
00610                                                                case ZEND_VM_KIND_SWITCH:
00611                                                                       out($f,$prolog."(opcode_handler_t)".((string)($num*25+$typecode[$op1]*5+$typecode[$op2])).",\n");
00612                                                                       break;
00613                                                                case ZEND_VM_KIND_GOTO:
00614                                                                       out($f,$prolog."(opcode_handler_t)&&".$dsc["op"]."_SPEC".$prefix[$op1].$prefix[$op2]."_HANDLER,\n");
00615                                                                       break;
00616                                                         }
00617                                                  } else {
00618                                                    // Emit pinter to handler of undefined opcode
00619                                                         switch ($kind) {
00620                                                                case ZEND_VM_KIND_CALL:
00621                                                                       out($f,$prolog."ZEND_NULL_HANDLER,\n");
00622                                                                       break;
00623                                                                case ZEND_VM_KIND_SWITCH:
00624                                                                       out($f,$prolog."(opcode_handler_t)-1,\n");
00625                                                                       break;
00626                                                                case ZEND_VM_KIND_GOTO:
00627                                                                       out($f,$prolog."(opcode_handler_t)&&ZEND_NULL_HANDLER,\n");
00628                                                                       break;
00629                                                         }
00630                                                  }
00631                                           }
00632                                    }
00633                             }
00634                      }
00635               }
00636        } else {
00637          // Emit labels for unspecialized executor
00638 
00639          // For each opcode in opcode number order
00640               foreach($opcodes as $num => $dsc) {
00641                      while ($next != $num) {
00642                        // If some opcode numbers are not used then fill hole with pointers
00643                        // to handler of undefined opcode
00644                             switch ($kind) {
00645                                    case ZEND_VM_KIND_CALL:
00646                                           out($f,$prolog."ZEND_NULL_HANDLER,\n");
00647                                           break;
00648                                    case ZEND_VM_KIND_SWITCH:
00649                                           out($f,$prolog."(opcode_handler_t)-1,\n");
00650                                           break;
00651                                    case ZEND_VM_KIND_GOTO:
00652                                           out($f,$prolog."(opcode_handler_t)&&ZEND_NULL_HANDLER,\n");
00653                                           break;
00654                             }
00655                             $next++;
00656                      }
00657                      $next = $num+1;
00658                 // Emit pointer to unspecialized handler
00659                      switch ($kind) {
00660                             case ZEND_VM_KIND_CALL:
00661                                    out($f,$prolog.$dsc["op"]."_HANDLER,\n");
00662                                    break;
00663                             case ZEND_VM_KIND_SWITCH:
00664                                    out($f,$prolog."(opcode_handler_t)".((string)$num).",\n");
00665                                    break;
00666                             case ZEND_VM_KIND_GOTO:
00667                                    out($f,$prolog."(opcode_handler_t)&&".$dsc["op"]."_HANDLER,\n");
00668                                    break;
00669                      }
00670               }
00671        }
00672 
00673        // Emit last handler's label (undefined opcode)
00674        switch ($kind) {
00675               case ZEND_VM_KIND_CALL:
00676                      out($f,$prolog."ZEND_NULL_HANDLER\n");
00677                      break;
00678               case ZEND_VM_KIND_SWITCH:
00679                      out($f,$prolog."(opcode_handler_t)-1\n");
00680                      break;
00681               case ZEND_VM_KIND_GOTO:
00682                      out($f,$prolog."(opcode_handler_t)&&ZEND_NULL_HANDLER\n");
00683                      break;
00684        }
00685 }
00686 
00687 // Generates handler for undefined opcodes (CALL threading model)
00688 function gen_null_handler($f) {
00689        static $done = 0;
00690 
00691        // New and all executors with CALL threading model can use the same handler
00692        // for undefined opcodes, do we emit code for it only once
00693        if (!$done) {
00694               $done = 1;
00695               out($f,"static int ZEND_FASTCALL ZEND_NULL_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
00696               out($f,"{\n");
00697               out($f,"\tzend_error_noreturn(E_ERROR, \"Invalid opcode %d/%d/%d.\", EX(opline)->opcode, EX(opline)->op1.op_type, EX(opline)->op2.op_type);\n");
00698               out($f,"}\n\n");
00699        }
00700 }
00701 
00702 // Generates all opcode handlers and helpers (specialized or unspecilaized)
00703 function gen_executor_code($f, $spec, $kind, $prolog) {
00704        global $list, $opcodes, $helpers, $op_types;
00705 
00706        if ($spec) {
00707               // Produce specialized executor
00708               $op1t = $op_types;
00709               // for each op1.op_type
00710               foreach($op1t as $op1) {
00711                      $op2t = $op_types;
00712                      // for each op2.op_type
00713                      foreach($op2t as $op2) {
00714                             // for each handlers in helpers in original order
00715                             foreach ($list as $lineno => $dsc) {
00716                                    if (isset($dsc["handler"])) {
00717                                           $num = $dsc["handler"];
00718                                           // Check if handler accepts such types of operands (op1 and op2)
00719                                           if (isset($opcodes[$num]["op1"][$op1]) &&
00720                                               isset($opcodes[$num]["op2"][$op2])) {
00721                                             // Generate handler code
00722                                                  gen_handler($f, 1, $kind, $opcodes[$num]["op"], $op1, $op2, isset($opcodes[$num]["use"]), $opcodes[$num]["code"], $lineno);
00723                                           }
00724                                    } else if (isset($dsc["helper"])) {
00725                                           $num = $dsc["helper"];
00726                                           // Check if handler accepts such types of operands (op1 and op2)
00727                                           if (isset($helpers[$num]["op1"][$op1]) &&
00728                                               isset($helpers[$num]["op2"][$op2])) {
00729                                             // Generate helper code
00730                                                  gen_helper($f, 1, $kind, $num, $op1, $op2, $helpers[$num]["param"], $helpers[$num]["code"], $lineno);
00731                                           }
00732                                    } else {
00733                                           var_dump($dsc);
00734                                           die("??? $kind:$num\n");
00735                                    }
00736                             }
00737                      }
00738               }
00739        } else {
00740               // Produce unspecialized executor
00741 
00742               // for each handlers in helpers in original order
00743               foreach ($list as $lineno => $dsc) {
00744                      if (isset($dsc["handler"])) {
00745                             $num = $dsc["handler"];
00746                        // Generate handler code
00747                             gen_handler($f, 0, $kind, $opcodes[$num]["op"], "ANY", "ANY", isset($opcodes[$num]["use"]), $opcodes[$num]["code"], $lineno);
00748                      } else if (isset($dsc["helper"])) {
00749                             $num = $dsc["helper"];
00750                        // Generate helper code
00751                             gen_helper($f, 0, $kind, $num, "ANY", "ANY", $helpers[$num]["param"], $helpers[$num]["code"], $lineno);
00752                      } else {
00753                             var_dump($dsc);
00754                             die("??? $kind:$num\n");
00755                      }
00756               }
00757        }
00758 
00759        if (ZEND_VM_LINES) {
00760               // Reset #line directives
00761               out_line($f);
00762        }
00763 
00764        // Generate handler for undefined opcodes
00765        switch ($kind) {
00766               case ZEND_VM_KIND_CALL:
00767                      gen_null_handler($f);
00768                      break;
00769               case ZEND_VM_KIND_SWITCH:
00770                      out($f,"default:\n");
00771                      out($f,"\tzend_error_noreturn(E_ERROR, \"Invalid opcode %d/%d/%d.\", EX(opline)->opcode, EX(opline)->op1.op_type, EX(opline)->op2.op_type);\n");
00772                      break;
00773               case ZEND_VM_KIND_GOTO:
00774                      out($f,"ZEND_NULL_HANDLER:\n");
00775                      out($f,"\tzend_error_noreturn(E_ERROR, \"Invalid opcode %d/%d/%d.\", EX(opline)->opcode, EX(opline)->op1.op_type, EX(opline)->op2.op_type);\n");
00776                      break;
00777        }
00778 }
00779 
00780 function skip_blanks($f, $prolog, $epilog) {
00781        if (trim($prolog) != "" || trim($epilog) != "") {
00782               out($f, $prolog.$epilog);
00783        }
00784 }
00785 
00786 // Generates executor from skeleton file and definition (specialized or unspecialized)
00787 function gen_executor($f, $skl, $spec, $kind, $executor_name, $initializer_name, $old) {
00788        global $params, $skeleton_file, $line_no;
00789 
00790        $lineno      = 0;
00791        foreach ($skl as $line) {
00792          // Skeleton file contains special markers in form %NAME% those are
00793          // substituted by custom code
00794               if (preg_match("/(.*)[{][%]([A-Z_]*)[%][}](.*)/", $line, $m)) {
00795                      switch ($m[2]) {
00796                             case "DEFINES":
00797                                    if (ZEND_VM_OLD_EXECUTOR && $spec) {
00798                                           out($f,"static int zend_vm_old_executor = 0;\n\n");
00799                                    }
00800                                    out($f,"static opcode_handler_t zend_vm_get_opcode_handler(zend_uchar opcode, zend_op* op);\n\n");
00801                                    switch ($kind) {
00802                                           case ZEND_VM_KIND_CALL:
00803                                                  out($f,"\n");
00804                                                  out($f,"#define ZEND_VM_CONTINUE()   return 0\n");
00805                                                  out($f,"#define ZEND_VM_RETURN()     return 1\n");
00806                                                  out($f,"#define ZEND_VM_ENTER()      return 2\n");
00807                                                  out($f,"#define ZEND_VM_LEAVE()      return 3\n");
00808                                                  out($f,"#define ZEND_VM_DISPATCH(opcode, opline) return zend_vm_get_opcode_handler(opcode, opline)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n\n");
00809                                                  out($f,"#define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL execute_data TSRMLS_CC\n");
00810                                                  out($f,"#undef EX\n");
00811                                                  out($f,"#define EX(element) execute_data->element\n\n");
00812                                                  break;
00813                                           case ZEND_VM_KIND_SWITCH:
00814                                                  out($f,"\n");
00815                                                  out($f,"#define ZEND_VM_CONTINUE() goto zend_vm_continue\n");
00816                                                  out($f,"#define ZEND_VM_RETURN()   EG(in_execution) = original_in_execution; return\n");
00817                                                  out($f,"#define ZEND_VM_ENTER()    op_array = EG(active_op_array); goto zend_vm_enter\n");
00818                                                  out($f,"#define ZEND_VM_LEAVE()    ZEND_VM_CONTINUE()\n");
00819                                                  out($f,"#define ZEND_VM_DISPATCH(opcode, opline) dispatch_handler = zend_vm_get_opcode_handler(opcode, opline); goto zend_vm_dispatch;\n\n");
00820                                                  out($f,"#define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL execute_data TSRMLS_CC\n");
00821                                                  out($f,"#undef EX\n");
00822                                                  out($f,"#define EX(element) execute_data->element\n\n");
00823                                                  break;
00824                                           case ZEND_VM_KIND_GOTO:
00825                                                  out($f,"\n");
00826                                                  out($f,"#define ZEND_VM_CONTINUE() goto *(void**)(EX(opline)->handler)\n");
00827                                                  out($f,"#define ZEND_VM_RETURN()   EG(in_execution) = original_in_execution; return\n");
00828                                                  out($f,"#define ZEND_VM_ENTER()    op_array = EG(active_op_array); goto zend_vm_enter\n");
00829                                                  out($f,"#define ZEND_VM_LEAVE()    ZEND_VM_CONTINUE()\n");
00830                                                  out($f,"#define ZEND_VM_DISPATCH(opcode, opline) goto *(void**)(zend_vm_get_opcode_handler(opcode, opline));\n\n");
00831                                                  out($f,"#define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL execute_data TSRMLS_CC\n");
00832                                                  out($f,"#undef EX\n");
00833                                                  out($f,"#define EX(element) execute_data->element\n\n");
00834                                                  break;
00835                                    }
00836                                    break;
00837                             case "EXECUTOR_NAME":
00838                                    out($f, $m[1].$executor_name.$m[3]."\n");
00839                                    break;
00840                             case "HELPER_VARS":
00841                                    if ($kind != ZEND_VM_KIND_CALL) {
00842                                           if ($kind == ZEND_VM_KIND_SWITCH) {
00843                                                  out($f,$m[1]."opcode_handler_t dispatch_handler;\n");
00844                                           }
00845                                      // Emit local variables those are used for helpers' parameters
00846                                           foreach ($params as $param => $x) {
00847                                                  out($f,$m[1].$param.";\n");
00848                                           }
00849                                    } else {
00850                                           skip_blanks($f, $m[1], $m[3]."\n");
00851                                    }
00852                                    break;
00853                             case "INTERNAL_LABELS":
00854                                    if ($kind == ZEND_VM_KIND_GOTO) {
00855                                      // Emit array of labels of opcode handlers and code for
00856                                      // zend_opcode_handlers initialization
00857                                           $prolog = $m[1];
00858                                           out($f,$prolog."if (op_array == NULL) {\n");
00859                                           out($f,$prolog."\tstatic const opcode_handler_t labels[] = {\n");
00860                                           gen_labels($f, $spec, $kind, $prolog."\t\t");
00861                                           out($f,$prolog."\t};\n");
00862                                           out($f,$prolog."\tzend_opcode_handlers = (opcode_handler_t*)labels;\n");
00863                                           out($f,$prolog."\treturn;\n");
00864                                           out($f,$prolog."}\n");
00865                                    } else {
00866                                           skip_blanks($f, $m[1], $m[3]);
00867                                    }
00868                                    break;
00869                             case "ZEND_VM_CONTINUE_LABEL":
00870                                    if ($kind == ZEND_VM_KIND_CALL) {
00871                                      // Only SWITCH dispatch method use it
00872                                           out($f,$m[1]."\tint ret;".$m[3]."\n");
00873                                    } else if ($kind == ZEND_VM_KIND_SWITCH) {
00874                                      // Only SWITCH dispatch method use it
00875                                           out($f,"zend_vm_continue:".$m[3]."\n");
00876                                    } else {
00877                                           skip_blanks($f, $m[1], $m[3]);
00878                                    }
00879                                    break;
00880                             case "ZEND_VM_DISPATCH":
00881                               // Emit code that dispatches to opcode handler
00882                                    switch ($kind) {
00883                                           case ZEND_VM_KIND_CALL:
00884                                                  out($f, $m[1]."if ((ret = EX(opline)->handler(execute_data TSRMLS_CC)) > 0)".$m[3]."\n");
00885                                                  break;
00886                                           case ZEND_VM_KIND_SWITCH:
00887                                                  out($f, $m[1]."dispatch_handler = EX(opline)->handler;\nzend_vm_dispatch:\n".$m[1]."switch ((int)dispatch_handler)".$m[3]."\n");
00888                                                  break;
00889                                           case ZEND_VM_KIND_GOTO:
00890                                                  out($f, $m[1]."goto *(void**)(EX(opline)->handler);".$m[3]."\n");
00891                                                  break;
00892                                    }
00893                                    break;
00894                             case "INTERNAL_EXECUTOR":
00895                                    if ($kind == ZEND_VM_KIND_CALL) {
00896                                      // Executor is defined as a set of functions
00897                                           out($f, $m[1]."switch (ret) {\n" . 
00898                                                   $m[1]."\tcase 1:\n" . 
00899                                                   $m[1]."\t\tEG(in_execution) = original_in_execution;\n".
00900                                                   $m[1]."\t\treturn;\n".
00901                                                   $m[1]."\tcase 2:\n" . 
00902                                                   $m[1]."\t\top_array = EG(active_op_array);\n".
00903                                                   $m[1]."\t\tgoto zend_vm_enter;\n".
00904                                                   $m[1]."\tcase 3:\n" . 
00905                                                   $m[1]."\t\texecute_data = EG(current_execute_data);\n".
00906                                                   $m[1]."\tdefault:\n".
00907                                                   $m[1]."\t\tbreak;\n".
00908                                                   $m[1]."}".$m[3]."\n");
00909                                    } else {
00910                                      // Emit executor code
00911                                           gen_executor_code($f, $spec, $kind, $m[1]);
00912                                    }
00913                                    break;
00914                             case "EXTERNAL_EXECUTOR":
00915                                    if ($kind == ZEND_VM_KIND_CALL) {
00916                                      // Unspecialized executor with CALL threading is the same as the
00917                                      // old one, so we don't need to produce code twitch
00918                                           if (!$old || ZEND_VM_SPEC || (ZEND_VM_KIND != ZEND_VM_KIND_CALL)) {
00919                                                  // Emit executor code
00920                                                  gen_executor_code($f, $spec, $kind, $m[1]);
00921                                           }
00922                                    }
00923                                    break;
00924                             case "INITIALIZER_NAME":
00925                                    out($f, $m[1].$initializer_name.$m[3]."\n");
00926                                    break;
00927                             case "EXTERNAL_LABELS":
00928                               // Emit code that initializes zend_opcode_handlers array
00929                                    $prolog = $m[1];
00930                                    if ($kind == ZEND_VM_KIND_GOTO) {
00931                                      // Labels are defined in the executor itself, so we call it
00932                                      // with op_array NULL and it sets zend_opcode_handlers array
00933                                           out($f,$prolog."TSRMLS_FETCH();\n");
00934                                           out($f,$prolog."zend_execute(NULL TSRMLS_CC);\n");
00935                                    } else {
00936                                           if ($old) {
00937                                             // Reserving space for user-defined opcodes
00938                                                  out($f,$prolog."static opcode_handler_t labels[512] = {\n");
00939                                           } else {
00940                                                  out($f,$prolog."static const opcode_handler_t labels[] = {\n");
00941                                           }
00942                                           gen_labels($f, $spec, $kind, $prolog."\t");
00943                                           out($f,$prolog."};\n");
00944                                           out($f,$prolog."zend_opcode_handlers = (opcode_handler_t*)labels;\n");
00945                                           if ($old) {
00946                                             // Setup old executor
00947                                                  out($f,$prolog."zend_vm_old_executor = 1;\n");
00948                                                  out($f,$prolog."zend_execute = old_execute;\n");
00949                                           }
00950                                    }
00951                                    break;
00952                             default:
00953                                    die("ERROR: Unknown keyword ".$m[2]." in skeleton file.\n");
00954                      }
00955               } else {
00956                 // Copy the line as is
00957                      out($f, $line);
00958               }
00959        }
00960 }
00961 
00962 function gen_vm($def, $skel) {
00963        global $definition_file, $skeleton_file, $executor_file,
00964               $op_types, $list, $opcodes, $helpers, $params, $opnames;
00965 
00966        // Load definition file
00967        $in = @file($def);
00968        if (!$in) {
00969               die("ERROR: Can not open definition file '$def'\n");
00970        }
00971        // We need absolute path to definition file to use it in #line directives
00972        $definition_file = realpath($def);
00973 
00974        // Load skeleton file
00975        $skl = @file($skel);
00976        if (!$skl) {
00977               die("ERROR: Can not open skeleton file '$skel'\n");
00978        }
00979        // We need absolute path to skeleton file to use it in #line directives
00980        $skeleton_file = realpath($skel);
00981 
00982        // Parse definition file into tree
00983        $lineno         = 0;
00984        $handler        = null;
00985        $helper         = null;
00986        $max_opcode_len = 0;
00987        $max_opcode     = 0;
00988        $export         = array();
00989        foreach ($in as $line) {
00990               ++$lineno;
00991               if (strpos($line,"ZEND_VM_HANDLER(") === 0) {
00992                 // Parsing opcode handler's definition
00993                      if (preg_match(
00994                                    "/^ZEND_VM_HANDLER\(\s*([0-9]+)\s*,\s*([A-Z_]+)\s*,\s*([A-Z|]+)\s*,\s*([A-Z|]+)\s*\)/",
00995                                    $line,
00996                                    $m) == 0) {
00997                             die("ERROR ($def:$lineno): Invalid ZEND_VM_HANDLER definition.\n");
00998                      }
00999                      $code = (int)$m[1];
01000                      $op   = $m[2];
01001                      $len  = strlen($op);
01002                      $op1  = array_flip(explode("|",$m[3]));
01003                      $op2  = array_flip(explode("|",$m[4]));
01004 
01005                      if ($len > $max_opcode_len) {
01006                             $max_opcode_len = $len;
01007                      }
01008                      if ($code > $max_opcode) {
01009                             $max_opcode = $code;
01010                      }
01011                      if (isset($opcodes[$code])) {
01012                             die("ERROR ($def:$lineno): Opcode with code '$code' is already defined.\n");
01013                      }
01014                      if (isset($opnames[$op])) {
01015                             die("ERROR ($def:$lineno): Opcode with name '$op' is already defined.\n");
01016                      }
01017                      $opcodes[$code] = array("op"=>$op,"op1"=>$op1,"op2"=>$op2,"code"=>"");
01018                      $opnames[$op] = $code;
01019                      $handler = $code;
01020                      $helper = null;
01021                      $list[$lineno] = array("handler"=>$handler);
01022               } else if (strpos($line,"ZEND_VM_HELPER(") === 0) {
01023                 // Parsing helper's definition
01024                      if (preg_match(
01025                                    "/^ZEND_VM_HELPER\(\s*([A-Za-z_]+)\s*,\s*([A-Z|]+)\s*,\s*([A-Z|]+)\s*\)/",
01026                                    $line,
01027                                    $m) == 0) {
01028                             die("ERROR ($def:$lineno): Invalid ZEND_VM_HELPER definition.\n");
01029                      }
01030                      $helper = $m[1];
01031                      $op1    = array_flip(explode("|",$m[2]));
01032                      $op2    = array_flip(explode("|",$m[3]));
01033                      if (isset($helpers[$helper])) {
01034                             die("ERROR ($def:$lineno): Helper with name '$helper' is already defined.\n");
01035                      }
01036                      $helpers[$helper] = array("op1"=>$op1,"op2"=>$op2,"param"=>null,"code"=>"");
01037                      $handler = null;
01038                      $list[$lineno] = array("helper"=>$helper);
01039               } else if (strpos($line,"ZEND_VM_HELPER_EX(") === 0) {
01040                 // Parsing helper with parameter definition
01041                      if (preg_match(
01042                                    "/^ZEND_VM_HELPER_EX\(\s*([A-Za-z_]+)\s*,\s*([A-Z|]+)\s*,\s*([A-Z|]+)\s*,\s*(.*)\s*\)/",
01043                                    $line,
01044                                    $m) == 0) {
01045                             die("ERROR ($def:$lineno): Invalid ZEND_VM_HELPER definition.\n");
01046                      }
01047                      $helper = $m[1];
01048                      $op1    = array_flip(explode("|",$m[2]));
01049                      $op2    = array_flip(explode("|",$m[3]));
01050                      $param  = $m[4];
01051                      if (isset($helpers[$helper])) {
01052                             die("ERROR ($def:$lineno): Helper with name '$helper' is already defined.\n");
01053                      }
01054 
01055                      // Store parameter
01056                      $params[$param] = 1;
01057 
01058                      $helpers[$helper] = array("op1"=>$op1,"op2"=>$op2,"param"=>$param,"code"=>"");
01059                      $handler = null;
01060                      $list[$lineno] = array("helper"=>$helper);
01061               } else if (strpos($line,"ZEND_VM_EXPORT_HANDLER(") === 0) {
01062                      if (preg_match(
01063                                    "/^ZEND_VM_EXPORT_HANDLER\(\s*([A-Za-z_]+)\s*,\s*([A-Z_]+)\s*\)/",
01064                                    $line,
01065                                    $m) == 0) {
01066                             die("ERROR ($def:$lineno): Invalid ZEND_VM_EXPORT_HANDLER definition.\n");
01067                      }
01068                      if (!isset($opnames[$m[2]])) {
01069                             die("ERROR ($def:$lineno): opcode '{$m[2]}' is not defined.\n");
01070                      }
01071                      $export[] = array("handler",$m[1],$m[2]);
01072               } else if (strpos($line,"ZEND_VM_EXPORT_HELPER(") === 0) {
01073                      if (preg_match(
01074                                    "/^ZEND_VM_EXPORT_HELPER\(\s*([A-Za-z_]+)\s*,\s*([A-Za-z_]+)\s*\)/",
01075                                    $line,
01076                                    $m) == 0) {
01077                             die("ERROR ($def:$lineno): Invalid ZEND_VM_EXPORT_HELPER definition.\n");
01078                      }
01079                      if (!isset($helpers[$m[2]])) {
01080                             die("ERROR ($def:$lineno): helper '{$m[2]}' is not defined.\n");
01081                      }
01082                      $export[] = array("helper",$m[1],$m[2]);
01083               } else if ($handler !== null) {
01084                 // Add line of code to current opcode handler
01085                      $opcodes[$handler]["code"] .= $line;
01086               } else if ($helper !== null) {
01087                 // Add line of code to current helper
01088                      $helpers[$helper]["code"] .= $line;
01089               }
01090        }
01091 
01092        ksort($opcodes);
01093 
01094        // Search for opcode handlers those are used by other opcode handlers
01095        foreach ($opcodes as $dsc) {
01096               if (preg_match("/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m", $dsc["code"], $m)) {
01097                      $op = $m[1];
01098                      if (!isset($opnames[$op])) {
01099                             die("ERROR ($def:$lineno): Opcode with name '$op' is not defined.\n");
01100                      }
01101                      $code = $opnames[$op];
01102                      $opcodes[$code]['use'] = 1;
01103               }
01104        }
01105 
01106        // Generate opcode #defines (zend_vm_opcodes.h)
01107        $code_len = strlen((string)$max_opcode);
01108        $f = fopen("zend_vm_opcodes.h", "w+") or die("ERROR: Cannot create zend_vm_opcodes.h\n");
01109 
01110        // Insert header
01111        out($f, $GLOBALS['header_text']);
01112 
01113        foreach ($opcodes as $code => $dsc) {
01114               $code = str_pad((string)$code,$code_len," ",STR_PAD_LEFT);
01115               $op = str_pad($dsc["op"],$max_opcode_len);
01116               fputs($f,"#define $op $code\n");
01117        }
01118        fclose($f);
01119        echo "zend_vm_opcodes.h generated successfully.\n";
01120 
01121        // Generate zend_vm_execute.h
01122        $f = fopen("zend_vm_execute.h", "w+") or die("ERROR: Cannot create zend_vm_execute.h\n");
01123        $executor_file = realpath("zend_vm_execute.h");
01124 
01125        // Insert header
01126        out($f, $GLOBALS['header_text']);
01127        
01128        // Suppress free_op1 warnings on Windows
01129        out($f, "#ifdef ZEND_WIN32\n# pragma warning(once : 4101)\n#endif\n");
01130 
01131        // Support for ZEND_USER_OPCODE
01132        out($f, "static user_opcode_handler_t zend_user_opcode_handlers[256] = {");
01133        for ($i = 0; $i < 255; ++$i) {
01134               out($f, "(user_opcode_handler_t)NULL,");
01135        }
01136        out($f, "(user_opcode_handler_t)NULL};\n\n");
01137 
01138        out($f, "static zend_uchar zend_user_opcodes[256] = {");
01139        for ($i = 0; $i < 255; ++$i) {
01140               out($f, "$i,");
01141        }
01142        out($f, "255};\n\n");
01143 
01144        // Generate specialized executor
01145        gen_executor($f, $skl, ZEND_VM_SPEC, ZEND_VM_KIND, "execute", "zend_init_opcodes_handlers", 0);
01146 
01147        // Generate un-specialized executor
01148        if (ZEND_VM_OLD_EXECUTOR) {
01149               out($f,"\n/* Old executor */\n\n");
01150               out($f,"#undef EX\n");
01151               out($f,"#define EX(element) execute_data.element\n\n");
01152               out($f,"#undef ZEND_VM_CONTINUE\n\n");
01153               out($f,"#undef ZEND_VM_RETURN\n\n");
01154               out($f,"#undef ZEND_VM_ENTER\n\n");
01155               out($f,"#undef ZEND_VM_LEAVE\n\n");
01156               out($f,"#undef ZEND_VM_DISPATCH\n\n");
01157               out($f,"#undef ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL\n\n");
01158               gen_executor($f, $skl, 0, ZEND_VM_KIND_CALL, "old_execute", "zend_vm_use_old_executor", 1);
01159        }
01160 
01161        // Generate zend_vm_get_opcode_handler() function
01162        out($f, "static opcode_handler_t zend_vm_get_opcode_handler(zend_uchar opcode, zend_op* op)\n");
01163        out($f, "{\n");
01164        if (!ZEND_VM_SPEC) {
01165               out($f, "\treturn zend_opcode_handlers[opcode];\n");
01166        } else {
01167               if (ZEND_VM_OLD_EXECUTOR) {
01168                      out($f, "\tif (zend_vm_old_executor) {\n");
01169                      out($f, "\t\treturn zend_opcode_handlers[opcode];\n");
01170                      out($f, "\t} else {\n");
01171               }
01172               out($f, "\t\tstatic const int zend_vm_decode[] = {\n");
01173               out($f, "\t\t\t_UNUSED_CODE, /* 0              */\n");
01174               out($f, "\t\t\t_CONST_CODE,  /* 1 = IS_CONST   */\n");
01175               out($f, "\t\t\t_TMP_CODE,    /* 2 = IS_TMP_VAR */\n");
01176               out($f, "\t\t\t_UNUSED_CODE, /* 3              */\n");
01177               out($f, "\t\t\t_VAR_CODE,    /* 4 = IS_VAR     */\n");
01178               out($f, "\t\t\t_UNUSED_CODE, /* 5              */\n");
01179               out($f, "\t\t\t_UNUSED_CODE, /* 6              */\n");
01180               out($f, "\t\t\t_UNUSED_CODE, /* 7              */\n");
01181               out($f, "\t\t\t_UNUSED_CODE, /* 8 = IS_UNUSED  */\n");
01182               out($f, "\t\t\t_UNUSED_CODE, /* 9              */\n");
01183               out($f, "\t\t\t_UNUSED_CODE, /* 10             */\n");
01184               out($f, "\t\t\t_UNUSED_CODE, /* 11             */\n");
01185               out($f, "\t\t\t_UNUSED_CODE, /* 12             */\n");
01186               out($f, "\t\t\t_UNUSED_CODE, /* 13             */\n");
01187               out($f, "\t\t\t_UNUSED_CODE, /* 14             */\n");
01188               out($f, "\t\t\t_UNUSED_CODE, /* 15             */\n");
01189               out($f, "\t\t\t_CV_CODE      /* 16 = IS_CV     */\n");
01190               out($f, "\t\t};\n");
01191               out($f, "\t\treturn zend_opcode_handlers[opcode * 25 + zend_vm_decode[op->op1.op_type] * 5 + zend_vm_decode[op->op2.op_type]];\n");
01192               if (ZEND_VM_OLD_EXECUTOR) {
01193                      out($f, "\t}\n");
01194               }
01195        }
01196        out($f, "}\n\n");
01197 
01198        // Generate zend_vm_get_opcode_handler() function
01199        out($f, "ZEND_API void zend_vm_set_opcode_handler(zend_op* op)\n");
01200        out($f, "{\n");
01201        out($f, "\top->handler = zend_vm_get_opcode_handler(zend_user_opcodes[op->opcode], op);\n");
01202        out($f, "}\n\n");
01203 
01204        // Export handlers and helpers
01205        if (count($export) > 0 &&
01206            !ZEND_VM_OLD_EXECUTOR &&
01207            ZEND_VM_KIND != ZEND_VM_KIND_CALL) {
01208               out($f,"#undef EX\n");
01209               out($f,"#define EX(element) execute_data->element\n\n");
01210               out($f,"#undef ZEND_VM_CONTINUE\n");
01211               out($f,"#undef ZEND_VM_RETURN\n");
01212               out($f,"#undef ZEND_VM_ENTER\n");
01213               out($f,"#undef ZEND_VM_LEAVE\n");
01214               out($f,"#undef ZEND_VM_DISPATCH\n");
01215               out($f,"#undef ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL\n\n");
01216               out($f,"#define ZEND_VM_CONTINUE()   return 0\n");
01217               out($f,"#define ZEND_VM_RETURN()     return 1\n");
01218               out($f,"#define ZEND_VM_ENTER()      return 2\n");
01219               out($f,"#define ZEND_VM_LEAVE()      return 3\n");
01220               out($f,"#define ZEND_VM_DISPATCH(opcode, opline) return zend_vm_get_opcode_handler(opcode, opline)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n\n");
01221               out($f,"#define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL execute_data TSRMLS_CC\n\n");
01222        }
01223        foreach ($export as $dsk) {
01224               list($kind, $func, $name) = $dsk;
01225               out($f, "ZEND_API int $func(");
01226               if ($kind == "handler") {
01227                      out($f, "ZEND_OPCODE_HANDLER_ARGS)\n");
01228                      $code = $opcodes[$opnames[$name]]['code'];
01229               } else {
01230                      $h = $helpers[$name];
01231                      if ($h['param'] == null) {
01232                             out($f, "ZEND_OPCODE_HANDLER_ARGS)\n");
01233                      } else {
01234                             out($f, $h['param']. ", ZEND_OPCODE_HANDLER_ARGS)\n");
01235                      }
01236                      $code = $h['code'];
01237               }
01238               $done = 0;
01239               if (ZEND_VM_OLD_EXECUTOR) {
01240                      if ($kind == "handler") {
01241                             out($f, "{\n\treturn ".$name."_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n}\n\n");
01242                             $done = 1;
01243                      } else if ($helpers[$name]["param"] == null) {
01244                             out($f, "{\n\treturn ".$name."(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n}\n\n");
01245                             $done = 1;
01246                      }
01247               } else if (ZEND_VM_KIND == ZEND_VM_KIND_CALL) {
01248                      if ($kind == "handler") {
01249                             $op = $opcodes[$opnames[$name]];
01250                             if (isset($op['op1']["ANY"]) && isset($op['op2']["ANY"])) {
01251                                    out($f, "{\n\treturn ".$name.(ZEND_VM_SPEC?"_SPEC":"")."_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n}\n\n");
01252                                    $done = 1;
01253                             }
01254                      } else if ($helpers[$name]["param"] == null) {
01255                             $h = $helpers[$name];
01256                             if (isset($h['op1']["ANY"]) && isset($h['op2']["ANY"])) {
01257                                    out($f, "{\n\treturn ".$name.(ZEND_VM_SPEC?"_SPEC":"")."(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n}\n\n");
01258                                    $done = 1;
01259                             }
01260                      }
01261               }
01262               if (!$done) {
01263                      gen_code($f, 0, ZEND_VM_KIND_CALL, 1, $code, 'ANY', 'ANY', $name);
01264               }
01265        }
01266 
01267        fclose($f);
01268        echo "zend_vm_execute.h generated successfully.\n";
01269 }
01270 
01271 function usage() {
01272        echo("\nUsage: php zend_vm_gen.php [options]\n".
01273             "\nOptions:".
01274             "\n  --with-vm-kind=CALL|SWITCH|GOTO - select threading model (default is CALL)".
01275             "\n  --without-specializer           - disable executor specialization".
01276             "\n  --with-old-executor             - enable old executor".
01277             "\n  --with-lines                    - enable #line directives".
01278             "\n\n");
01279 }
01280 
01281 // Parse arguments
01282 for ($i = 1;  $i < $argc; $i++) {
01283        if (strpos($argv[$i],"--with-vm-kind=") === 0) {
01284               $kind = substr($argv[$i], strlen("--with-vm-kind="));
01285               switch ($kind) {
01286                      case "CALL":
01287                             define("ZEND_VM_KIND", ZEND_VM_KIND_CALL);
01288                             break;
01289                      case "SWITCH":
01290                             define("ZEND_VM_KIND", ZEND_VM_KIND_SWITCH);
01291                             break;
01292                      case "GOTO":
01293                             define("ZEND_VM_KIND", ZEND_VM_KIND_GOTO);
01294                             break;
01295                      default:
01296                             echo("ERROR: Invalid vm kind '$kind'\n");
01297                             usage();
01298                             die();
01299               }
01300        } else if ($argv[$i] == "--without-specializer") {
01301          // Disabling specialization
01302               define("ZEND_VM_SPEC", 0);
01303        } else if ($argv[$i] == "--with-old-executor") {
01304          // Disabling code for old-style executor
01305               define("ZEND_VM_OLD_EXECUTOR", 1);
01306        } else if ($argv[$i] == "--with-lines") {
01307               // Enabling debuging using original zend_vm_def.h
01308               define("ZEND_VM_LINES", 1);
01309        } else if ($argv[$i] == "--help") {
01310               usage();
01311               exit();
01312        } else {
01313               echo("ERROR: Invalid option '".$argv[$i]."'\n");
01314               usage();
01315               die();
01316        }
01317 }
01318 
01319 // Using defaults
01320 if (!defined("ZEND_VM_KIND")) {
01321   // Using CALL threading by default
01322        define("ZEND_VM_KIND", ZEND_VM_KIND_CALL);
01323 }
01324 if (!defined("ZEND_VM_SPEC")) {
01325   // Using specialized executor by default
01326        define("ZEND_VM_SPEC", 1);
01327 }
01328 if (!defined("ZEND_VM_OLD_EXECUTOR")) {
01329   // Include old-style executor by default
01330        define("ZEND_VM_OLD_EXECUTOR", 0);
01331 }
01332 if (!defined("ZEND_VM_LINES")) {
01333   // Disabling #line directives
01334        define("ZEND_VM_LINES", 0);
01335 }
01336 
01337 gen_vm("zend_vm_def.h", "zend_vm_execute.skl");
01338 
01339 ?>