source: trunk/phpdop.phpr @ 731

Last change on this file since 731 was 731, checked in by moo, 3 years ago

PHP_6: disassembler/decompiler: fix function static variable

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1#! /usr/bin/php
2<?php
3
4$srcdir = dirname(__FILE__);
5require_once("$srcdir/Decompiler.class.php");
6if (file_exists("$srcdir/phpdc.debug.php")) {
7    include("$srcdir/phpdc.debug.php");
8}
9
10function get_op($op)
11{
12    switch ($op['op_type']) {
13    case 1: // CONST
14        return var_export($op['constant'], true);
15
16    case 2: // IS_TMP_VAR
17        return 't@' . $op['var'];
18
19    case 4:
20        return 'v$' . $op['var'];
21
22    case 8: // UNUSED
23        if (isset($op['opline_num'])) {
24            return 'l#' . $op['opline_num'];
25        }
26        else {
27            return '-';
28        }
29
30    default:
31        return $op['op_type'] . $op['var'];
32    }
33}
34
35function dump_opcodes($opcodes, $indent = '')
36{
37    global $decompiler;
38
39    $types = array('result' => 5, 'op1' => 20, 'op2' => 20);
40    foreach ($decompiler->fixOpcode($opcodes) as $line => $op) {
41        echo $indent;
42        echo sprintf("%3d ", $op['lineno']);
43        echo sprintf("%3x ", $line);
44        $name = xcache_get_opcode($op['opcode']);
45
46        if (substr($name, 0, 5) == 'ZEND_') {
47            $name = substr($name, 5);
48        }
49        echo str_pad($name, 25);
50
51        foreach ($types as $t => $len) {
52            echo str_pad(isset($op[$t]) ? get_op($op[$t]) : "", $len);
53        }
54        printf("%5s", isset($op['extended_value']) ?  $op['extended_value'] : "");
55
56        echo "\n";
57    }
58}
59
60function dump_function($name, $func, $indent = '')
61{
62    if (isset($func['op_array'])) {
63        $op_array = $func['op_array'];
64        unset($func['op_array']);
65    }
66    else {
67        $op_array = null;
68    }
69    var_dump($func);
70    echo $indent, 'function ', $name, "\n";
71    if (isset($op_array)) {
72        dump_opcodes($op_array['opcodes'], "  " . $indent);
73    }
74}
75
76function dump_class($name, $class, $indent = '')
77{
78    if (isset($class['function_table'])) {
79        $funcs = $class['function_table'];
80        unset($class['function_table']);
81    }
82    else {
83        $funcs = null;
84    }
85    echo $indent, 'class ', $name, "\n";
86    if (isset($funcs)) {
87        foreach ($funcs as $name => $func) {
88            dump_function($name, $func, "  " . $indent);
89        }
90    }
91}
92
93if (!isset($argv[1])) {
94    die("Usage: $argv[0] <file>\n");
95}
96$decompiler = new Decompiler();
97$pk = xcache_dasm_file($argv[1]);
98$op_array = $funcs = $classes = null;
99if (isset($pk['op_array'])) {
100    $op_array = $pk['op_array'];
101    unset($pk['op_array']);
102}
103if (isset($pk['function_table'])) {
104    $funcs = $pk['function_table'];
105    unset($pk['function_table']);
106}
107if (isset($pk['class_table'])) {
108    $classes = $pk['class_table'];
109    unset($pk['class_table']);
110}
111var_dump($pk);
112if (isset($op_array)) {
113    dump_opcodes($op_array['opcodes']);
114}
115if (isset($funcs)) {
116    foreach ($funcs as $name => $func) {
117        dump_function($name, $func);
118    }
119}
120if (isset($classes)) {
121    foreach ($classes as $name => $class) {
122        dump_class($name, $class);
123    }
124}
125
Note: See TracBrowser for help on using the repository browser.