You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2800 lines
71 KiB
PHTML
2800 lines
71 KiB
PHTML
![]()
17 years ago
|
<?php
|
||
|
|
||
|
define('INDENT', "\t");
|
||
|
ini_set('error_reporting', E_ALL);
|
||
|
|
||
|
function color($str, $color = 33)
|
||
|
{
|
||
|
return "\x1B[{$color}m$str\x1B[0m";
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
function str($code, $indent = '') // {{{
|
||
|
{
|
||
![]()
12 years ago
|
if (is_array($code)) {
|
||
|
$array = array();
|
||
|
foreach ($code as $key => $value) {
|
||
|
$array[$key] = str($value, $indent);
|
||
|
}
|
||
|
return $array;
|
||
|
}
|
||
![]()
12 years ago
|
if (is_object($code)) {
|
||
![]()
12 years ago
|
$code = foldToCode($code, $indent);
|
||
|
return $code->toCode($indent);
|
||
![]()
12 years ago
|
}
|
||
|
|
||
|
return (string) $code;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function foldToCode($src, $indent = '') // {{{ wrap or rewrap anything to Decompiler_Code
|
||
![]()
17 years ago
|
{
|
||
|
if (is_array($indent)) {
|
||
|
$indent = $indent['indent'];
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
if (!is_object($src)) {
|
||
|
return new Decompiler_Code($src);
|
||
|
}
|
||
|
|
||
|
if (!method_exists($src, 'toCode')) {
|
||
|
var_dump($src);
|
||
|
exit('no toCode');
|
||
|
}
|
||
|
if (get_class($src) != 'Decompiler_Code') {
|
||
|
// rewrap it
|
||
|
$src = new Decompiler_Code($src->toCode($indent));
|
||
![]()
17 years ago
|
}
|
||
|
|
||
![]()
12 years ago
|
return $src;
|
||
![]()
17 years ago
|
}
|
||
|
// }}}
|
||
|
function value($value) // {{{
|
||
|
{
|
||
|
$spec = xcache_get_special_value($value);
|
||
|
if (isset($spec)) {
|
||
|
$value = $spec;
|
||
|
if (!is_array($value)) {
|
||
|
// constant
|
||
|
return $value;
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
if (is_a($value, 'Decompiler_Object')) {
|
||
![]()
12 years ago
|
// use as is
|
||
|
}
|
||
|
else if (is_array($value)) {
|
||
![]()
12 years ago
|
$value = new Decompiler_ConstArray($value);
|
||
![]()
17 years ago
|
}
|
||
|
else {
|
||
![]()
12 years ago
|
$value = new Decompiler_Value($value);
|
||
![]()
17 years ago
|
}
|
||
|
return $value;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function unquoteName_($str, $asVariableName, $indent = '') // {{{
|
||
![]()
12 years ago
|
{
|
||
|
$str = str($str, $indent);
|
||
![]()
12 years ago
|
if (preg_match("!^'[\\w_][\\w\\d_\\\\]*'\$!", $str)) {
|
||
|
return str_replace('\\\\', '\\', substr($str, 1, -1));
|
||
![]()
12 years ago
|
}
|
||
![]()
12 years ago
|
else if ($asVariableName) {
|
||
![]()
12 years ago
|
return "{" . $str . "}";
|
||
|
}
|
||
![]()
12 years ago
|
else {
|
||
|
return $str;
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function unquoteVariableName($str, $indent = '') // {{{
|
||
![]()
12 years ago
|
{
|
||
|
return unquoteName_($str, true, $indent);
|
||
![]()
12 years ago
|
}
|
||
|
// }}}
|
||
|
function unquoteName($str, $indent = '') // {{{
|
||
|
{
|
||
![]()
12 years ago
|
return unquoteName_($str, false, $indent);
|
||
![]()
12 years ago
|
}
|
||
|
// }}}
|
||
![]()
17 years ago
|
class Decompiler_Object // {{{
|
||
|
{
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_Value extends Decompiler_Object // {{{
|
||
|
{
|
||
|
var $value;
|
||
|
|
||
|
function Decompiler_Value($value = null)
|
||
|
{
|
||
|
$this->value = $value;
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
function toCode($indent)
|
||
![]()
17 years ago
|
{
|
||
![]()
12 years ago
|
$code = var_export($this->value, true);
|
||
|
if (gettype($this->value) == 'string') {
|
||
|
switch ($this->value) {
|
||
|
case "\r":
|
||
|
return '"\\r"';
|
||
|
case "\n":
|
||
|
return '"\\n"';
|
||
|
case "\r\n":
|
||
|
return '"\\r\\n"';
|
||
|
}
|
||
|
$code = str_replace("\r\n", '\' . "\\r\\n" . \'', $code);
|
||
|
$code = str_replace("\r", '\' . "\\r" . \'', $code);
|
||
|
$code = str_replace("\n", '\' . "\\n" . \'', $code);
|
||
|
}
|
||
|
return $code;
|
||
![]()
17 years ago
|
}
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_Code extends Decompiler_Object // {{{
|
||
|
{
|
||
|
var $src;
|
||
|
|
||
|
function Decompiler_Code($src)
|
||
|
{
|
||
![]()
12 years ago
|
assert('isset($src)');
|
||
![]()
17 years ago
|
$this->src = $src;
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
function toCode($indent)
|
||
![]()
12 years ago
|
{
|
||
![]()
12 years ago
|
return $this->src;
|
||
![]()
12 years ago
|
}
|
||
![]()
17 years ago
|
}
|
||
|
// }}}
|
||
|
class Decompiler_Binop extends Decompiler_Code // {{{
|
||
|
{
|
||
|
var $opc;
|
||
|
var $op1;
|
||
|
var $op2;
|
||
|
var $parent;
|
||
|
|
||
|
function Decompiler_Binop($parent, $op1, $opc, $op2)
|
||
|
{
|
||
|
$this->parent = &$parent;
|
||
|
$this->opc = $opc;
|
||
|
$this->op1 = $op1;
|
||
|
$this->op2 = $op2;
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
function toCode($indent)
|
||
![]()
17 years ago
|
{
|
||
![]()
12 years ago
|
$opstr = $this->parent->binops[$this->opc];
|
||
|
|
||
![]()
12 years ago
|
if (is_a($this->op1, 'Decompiler_TriOp') || is_a($this->op1, 'Decompiler_Binop') && $this->op1->opc != $this->opc) {
|
||
|
$op1 = "(" . str($this->op1, $indent) . ")";
|
||
![]()
17 years ago
|
}
|
||
![]()
12 years ago
|
else {
|
||
|
$op1 = $this->op1;
|
||
|
}
|
||
|
|
||
|
if (is_a($this->op2, 'Decompiler_TriOp') || is_a($this->op2, 'Decompiler_Binop') && $this->op2->opc != $this->opc && substr($opstr, -1) != '=') {
|
||
|
$op2 = "(" . str($this->op2, $indent) . ")";
|
||
|
}
|
||
|
else {
|
||
|
$op2 = $this->op2;
|
||
![]()
17 years ago
|
}
|
||
![]()
12 years ago
|
|
||
|
if (str($op1) == '0' && ($this->opc == XC_ADD || $this->opc == XC_SUB)) {
|
||
|
return $opstr . str($op2, $indent);
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
return str($op1, $indent) . ' ' . $opstr . ($this->opc == XC_ASSIGN_REF ? '' : ' ') . str($op2, $indent);
|
||
![]()
12 years ago
|
}
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_TriOp extends Decompiler_Code // {{{
|
||
|
{
|
||
|
var $condition;
|
||
|
var $trueValue;
|
||
|
var $falseValue;
|
||
|
|
||
|
function Decompiler_TriOp($condition, $trueValue, $falseValue)
|
||
|
{
|
||
|
$this->condition = $condition;
|
||
|
$this->trueValue = $trueValue;
|
||
|
$this->falseValue = $falseValue;
|
||
|
}
|
||
|
|
||
|
function toCode($indent)
|
||
|
{
|
||
|
$trueValue = $this->trueValue;
|
||
|
if (is_a($this->trueValue, 'Decompiler_TriOp')) {
|
||
|
$trueValue = "(" . str($trueValue, $indent) . ")";
|
||
|
}
|
||
|
$falseValue = $this->falseValue;
|
||
|
if (is_a($this->falseValue, 'Decompiler_TriOp')) {
|
||
|
$falseValue = "(" . str($falseValue, $indent) . ")";
|
||
|
}
|
||
|
|
||
|
return str($this->condition) . ' ? ' . str($trueValue) . ' : ' . str($falseValue);
|
||
![]()
17 years ago
|
}
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_Fetch extends Decompiler_Code // {{{
|
||
|
{
|
||
|
var $src;
|
||
|
var $fetchType;
|
||
|
|
||
|
function Decompiler_Fetch($src, $type, $globalsrc)
|
||
|
{
|
||
|
$this->src = $src;
|
||
|
$this->fetchType = $type;
|
||
|
$this->globalsrc = $globalsrc;
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
function toCode($indent)
|
||
![]()
17 years ago
|
{
|
||
|
switch ($this->fetchType) {
|
||
|
case ZEND_FETCH_LOCAL:
|
||
|
return '$' . substr($this->src, 1, -1);
|
||
|
case ZEND_FETCH_STATIC:
|
||
![]()
12 years ago
|
if (ZEND_ENGINE_2_3) {
|
||
|
// closure local variable?
|
||
|
return str($this->src);
|
||
|
}
|
||
![]()
17 years ago
|
die('static fetch cant to string');
|
||
|
case ZEND_FETCH_GLOBAL:
|
||
|
case ZEND_FETCH_GLOBAL_LOCK:
|
||
|
return $this->globalsrc;
|
||
|
default:
|
||
|
var_dump($this->fetchType);
|
||
|
assert(0);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_Box // {{{
|
||
|
{
|
||
|
var $obj;
|
||
|
|
||
|
function Decompiler_Box(&$obj)
|
||
|
{
|
||
|
$this->obj = &$obj;
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
function toCode($indent)
|
||
![]()
17 years ago
|
{
|
||
![]()
12 years ago
|
return $this->obj->toCode($indent);
|
||
![]()
17 years ago
|
}
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_Dim extends Decompiler_Value // {{{
|
||
|
{
|
||
|
var $offsets = array();
|
||
|
var $isLast = false;
|
||
![]()
12 years ago
|
var $isObject = false;
|
||
![]()
17 years ago
|
var $assign = null;
|
||
|
|
||
![]()
12 years ago
|
function toCode($indent)
|
||
![]()
17 years ago
|
{
|
||
|
if (is_a($this->value, 'Decompiler_ListBox')) {
|
||
![]()
12 years ago
|
$exp = str($this->value->obj->src, $indent);
|
||
![]()
17 years ago
|
}
|
||
|
else {
|
||
![]()
12 years ago
|
$exp = str($this->value, $indent);
|
||
![]()
17 years ago
|
}
|
||
![]()
12 years ago
|
$last = count($this->offsets) - 1;
|
||
|
foreach ($this->offsets as $i => $dim) {
|
||
|
if ($this->isObject && $i == $last) {
|
||
![]()
12 years ago
|
$exp .= '->' . unquoteVariableName($dim, $indent);
|
||
![]()
12 years ago
|
}
|
||
|
else {
|
||
|
$exp .= '[' . str($dim, $indent) . ']';
|
||
|
}
|
||
![]()
17 years ago
|
}
|
||
|
return $exp;
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_DimBox extends Decompiler_Box // {{{
|
||
|
{
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_List extends Decompiler_Code // {{{
|
||
|
{
|
||
|
var $src;
|
||
|
var $dims = array();
|
||
|
var $everLocked = false;
|
||
|
|
||
![]()
12 years ago
|
function toCode($indent)
|
||
![]()
17 years ago
|
{
|
||
|
if (count($this->dims) == 1 && !$this->everLocked) {
|
||
|
$dim = $this->dims[0];
|
||
|
unset($dim->value);
|
||
|
$dim->value = $this->src;
|
||
|
if (!isset($dim->assign)) {
|
||
![]()
12 years ago
|
return str($dim, $indent);
|
||
![]()
17 years ago
|
}
|
||
![]()
12 years ago
|
return str($this->dims[0]->assign, $indent) . ' = ' . str($dim, $indent);
|
||
![]()
17 years ago
|
}
|
||
|
/* flatten dims */
|
||
|
$assigns = array();
|
||
|
foreach ($this->dims as $dim) {
|
||
|
$assign = &$assigns;
|
||
|
foreach ($dim->offsets as $offset) {
|
||
|
$assign = &$assign[$offset];
|
||
|
}
|
||
![]()
12 years ago
|
$assign = foldToCode($dim->assign, $indent);
|
||
![]()
17 years ago
|
}
|
||
![]()
12 years ago
|
return str($this->toList($assigns)) . ' = ' . str($this->src, $indent);
|
||
![]()
17 years ago
|
}
|
||
|
|
||
|
function toList($assigns)
|
||
|
{
|
||
|
$keys = array_keys($assigns);
|
||
|
if (count($keys) < 2) {
|
||
|
$keys[] = 0;
|
||
|
}
|
||
|
$max = call_user_func_array('max', $keys);
|
||
|
$list = 'list(';
|
||
|
for ($i = 0; $i <= $max; $i ++) {
|
||
|
if ($i) {
|
||
|
$list .= ', ';
|
||
|
}
|
||
|
if (!isset($assigns[$i])) {
|
||
|
continue;
|
||
|
}
|
||
|
if (is_array($assigns[$i])) {
|
||
|
$list .= $this->toList($assigns[$i]);
|
||
|
}
|
||
|
else {
|
||
|
$list .= $assigns[$i];
|
||
|
}
|
||
|
}
|
||
|
return $list . ')';
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_ListBox extends Decompiler_Box // {{{
|
||
|
{
|
||
|
}
|
||
|
// }}}
|
||
|
class Decompiler_Array extends Decompiler_Value // {{{
|
||
|
{
|
||
![]()
12 years ago
|
// emenets
|
||
|
function Decompiler_Array()
|
||
![]()
17 years ago
|
{
|
||
![]()
12 years ago
|
$this->value = array();
|
||
![]()
17 years ago
|
}
|
||
|
|
||
![]()
12 years ago
|
function toCode($indent)
|
||
![]()
17 years ago
|
{
|
||
![]()
12 years ago
|
$subindent = $indent . INDENT;
|
||
|
|
||
|
$elementsCode = array();
|
||
|
$index = 0;
|
||
|
foreach ($this->value as $element) {
|
||
|
list($key, $value) = $element;
|
||
|
if (!isset($key)) {
|
||
|
$key = $index++;
|
||
|
}
|
||
|
$elementsCode[] = array(str($key, $subindent), str($value, $subindent), $key, $value);
|
||
|
}
|
||
|
|
||
![]()
17 years ago
|
$exp = "array(";
|
||
![]()
12 years ago
|
$indent = $indent . INDENT;
|
||
![]()
12 years ago
|
$assocWidth = 0;
|
||
![]()
17 years ago
|
$multiline = 0;
|
||
|
$i = 0;
|
||
![]()
12 years ago
|
foreach ($elementsCode as $element) {
|
||
|
list($keyCode, $valueCode) = $element;
|
||
|
if ((string) $i !== $keyCode) {
|
||
![]()
12 years ago
|
$assocWidth = 1;
|
||
![]()
12 years ago
|
break;
|
||
![]()
12 years ago
|
}
|
||
|
++$i;
|
||
|
}
|
||
![]()
12 years ago
|
foreach ($elementsCode as $element) {
|
||
|
list($keyCode, $valueCode, $key, $value) = $element;
|
||
![]()
12 years ago
|
if ($assocWidth) {
|
||
![]()
12 years ago
|
$len = strlen($keyCode);
|
||
![]()
12 years ago
|
if ($assocWidth < $len) {
|
||
|
$assocWidth = $len;
|
||
![]()
17 years ago
|
}
|
||
|
}
|
||
![]()
12 years ago
|
if (is_array($value) || is_a($value, 'Decompiler_Array')) {
|
||
![]()
17 years ago
|
$multiline ++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$i = 0;
|
||
![]()
12 years ago
|
foreach ($elementsCode as $element) {
|
||
|
list($keyCode, $value) = $element;
|
||
![]()
17 years ago
|
if ($multiline) {
|
||
|
if ($i) {
|
||
|
$exp .= ",";
|
||
|
}
|
||
|
$exp .= "\n";
|
||
|
$exp .= $indent;
|
||
|
}
|
||
|
else {
|
||
|
if ($i) {
|
||
|
$exp .= ", ";
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
if ($assocWidth) {
|
||
|
if ($multiline) {
|
||
![]()
12 years ago
|
$exp .= sprintf("%-{$assocWidth}s => ", $keyCode);
|
||
![]()
12 years ago
|
}
|
||
|
else {
|
||
![]()
12 years ago
|
$exp .= $keyCode . ' => ';
|
||
![]()
12 years ago
|
}
|
||
![]()
17 years ago
|
}
|
||
|
|
||
![]()
12 years ago
|
$exp .= $value;
|
||
![]()
17 years ago
|
|
||
|
$i ++;
|
||
|
}
|
||
|
if ($multiline) {
|
||
![]()
12 years ago
|
$exp .= "\n$indent)";
|
||
![]()
17 years ago
|
}
|
||
|
else {
|
||
|
$exp .= ")";
|
||
|
}
|
||
|
return $exp;
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
class Decompiler_ConstArray extends Decompiler_Array // {{{
|
||
|
{
|
||
|
function Decompiler_ConstArray($array)
|
||
|
{
|
||
|
$elements = array();
|
||
|
foreach ($array as $key => $value) {
|
||
|
$elements[] = array(value($key), value($value));
|
||
|
}
|
||
|
$this->value = $elements;
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
![]()
17 years ago
|
class Decompiler_ForeachBox extends Decompiler_Box // {{{
|
||
|
{
|
||
|
var $iskey;
|
||
|
|
||
![]()
12 years ago
|
function toCode($indent)
|
||
![]()
17 years ago
|
{
|
||
|
return 'foreach (' . '';
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
|
|
||
|
class Decompiler
|
||
|
{
|
||
![]()
12 years ago
|
var $namespace;
|
||
|
var $namespaceDecided;
|
||
![]()
17 years ago
|
|
||
|
function Decompiler()
|
||
|
{
|
||
![]()
12 years ago
|
// {{{ testing
|
||
|
// XC_UNDEF XC_OP_DATA
|
||
|
$this->test = !empty($_ENV['XCACHE_DECOMPILER_TEST']);
|
||
|
$this->usedOps = array();
|
||
|
|
||
|
if ($this->test) {
|
||
|
$content = file_get_contents(__FILE__);
|
||
|
for ($i = 0; $opname = xcache_get_opcode($i); $i ++) {
|
||
|
if (!preg_match("/\\bXC_" . $opname . "\\b(?!')/", $content)) {
|
||
|
echo "not recognized opcode ", $opname, "\n";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
![]()
17 years ago
|
// {{{ opinfo
|
||
|
$this->unaryops = array(
|
||
|
XC_BW_NOT => '~',
|
||
|
XC_BOOL_NOT => '!',
|
||
|
);
|
||
|
$this->binops = array(
|
||
|
XC_ADD => "+",
|
||
|
XC_ASSIGN_ADD => "+=",
|
||
|
XC_SUB => "-",
|
||
|
XC_ASSIGN_SUB => "-=",
|
||
|
XC_MUL => "*",
|
||
|
XC_ASSIGN_MUL => "*=",
|
||
|
XC_DIV => "/",
|
||
|
XC_ASSIGN_DIV => "/=",
|
||
|
XC_MOD => "%",
|
||
|
XC_ASSIGN_MOD => "%=",
|
||
|
XC_SL => "<<",
|
||
|
XC_ASSIGN_SL => "<<=",
|
||
|
XC_SR => ">>",
|
||
|
XC_ASSIGN_SR => ">>=",
|
||
|
XC_CONCAT => ".",
|
||
|
XC_ASSIGN_CONCAT => ".=",
|
||
|
XC_IS_IDENTICAL => "===",
|
||
|
XC_IS_NOT_IDENTICAL => "!==",
|
||
|
XC_IS_EQUAL => "==",
|
||
|
XC_IS_NOT_EQUAL => "!=",
|
||
|
XC_IS_SMALLER => "<",
|
||
|
XC_IS_SMALLER_OR_EQUAL => "<=",
|
||
|
XC_BW_OR => "|",
|
||
|
XC_ASSIGN_BW_OR => "|=",
|
||
|
XC_BW_AND => "&",
|
||
|
XC_ASSIGN_BW_AND => "&=",
|
||
|
XC_BW_XOR => "^",
|
||
|
XC_ASSIGN_BW_XOR => "^=",
|
||
|
XC_BOOL_XOR => "xor",
|
||
![]()
12 years ago
|
XC_ASSIGN => "=",
|
||
|
XC_ASSIGN_REF => "= &",
|
||
![]()
12 years ago
|
XC_JMP_SET => "?:",
|
||
![]()
12 years ago
|
XC_JMPZ_EX => "&&",
|
||
|
XC_JMPNZ_EX => "||",
|
||
![]()
17 years ago
|
);
|
||
|
// }}}
|
||
|
$this->includeTypes = array( // {{{
|
||
|
ZEND_EVAL => 'eval',
|
||
|
ZEND_INCLUDE => 'include',
|
||
|
ZEND_INCLUDE_ONCE => 'include_once',
|
||
|
ZEND_REQUIRE => 'require',
|
||
|
ZEND_REQUIRE_ONCE => 'require_once',
|
||
|
);
|
||
|
// }}}
|
||
|
}
|
||
![]()
12 years ago
|
function detectNamespace($name) // {{{
|
||
|
{
|
||
|
if ($this->namespaceDecided) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (strpos($name, '\\') !== false) {
|
||
|
$this->namespace = strtok($name, '\\');
|
||
|
echo 'namespace ', $this->namespace, ";\n\n";
|
||
|
}
|
||
|
|
||
|
$this->namespaceDecided = true;
|
||
|
}
|
||
|
// }}}
|
||
|
function stripNamespace($name) // {{{
|
||
|
{
|
||
|
$len = strlen($this->namespace) + 1;
|
||
|
if (substr($name, 0, $len) == $this->namespace . '\\') {
|
||
|
return substr($name, $len);
|
||
|
}
|
||
|
else {
|
||
|
return $name;
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function outputPhp(&$EX, $range) // {{{
|
||
![]()
17 years ago
|
{
|
||
![]()
12 years ago
|
$needBlankline = isset($EX['lastBlock']);
|
||
![]()
12 years ago
|
$indent = $EX['indent'];
|
||
![]()
17 years ago
|
$curticks = 0;
|
||
![]()
12 years ago
|
for ($i = $range[0]; $i <= $range[1]; $i ++) {
|
||
![]()
12 years ago
|
$op = $EX['opcodes'][$i];
|
||
|
if (isset($op['gofrom'])) {
|
||
![]()
12 years ago
|
if ($needBlankline) {
|
||
|
$needBlankline = false;
|
||
|
echo PHP_EOL;
|
||
|
}
|
||
![]()
12 years ago
|
echo 'label' . $i, ":\n";
|
||
|
}
|
||
![]()
17 years ago
|
if (isset($op['php'])) {
|
||
![]()
12 years ago
|
$toticks = isset($op['ticks']) ? (int) str($op['ticks']) : 0;
|
||
![]()
17 years ago
|
if ($curticks != $toticks) {
|
||
![]()
12 years ago
|
$oldticks = $curticks;
|
||
|
$curticks = $toticks;
|
||
|
if (!$curticks) {
|
||
![]()
12 years ago
|
echo $EX['indent'], "}\n\n";
|
||
|
$indent = $EX['indent'];
|
||
![]()
17 years ago
|
}
|
||
|
else {
|
||
![]()
12 years ago
|
if ($oldticks) {
|
||
![]()
12 years ago
|
echo $EX['indent'], "}\n\n";
|
||
![]()
17 years ago
|
}
|
||
![]()
12 years ago
|
else if (!$oldticks) {
|
||
![]()
17 years ago
|
$indent .= INDENT;
|
||
|
}
|
||
![]()
12 years ago
|
if ($needBlankline) {
|
||
|
$needBlankline = false;
|
||
|
echo PHP_EOL;
|
||
|
}
|
||
![]()
12 years ago
|
echo $EX['indent'], "declare (ticks=$curticks) {\n";
|
||
![]()
17 years ago
|
}
|
||
|
}
|
||
![]()
12 years ago
|
if ($needBlankline) {
|
||
|
$needBlankline = false;
|
||
|
echo PHP_EOL;
|
||
|
}
|
||
![]()
12 years ago
|
echo $indent, str($op['php'], $indent), ";\n";
|
||
![]()
12 years ago
|
$EX['lastBlock'] = 'basic';
|
||
![]()
17 years ago
|
}
|
||
|
}
|
||
|
if ($curticks) {
|
||
![]()
12 years ago
|
echo $EX['indent'], "}\n";
|
||
![]()
17 years ago
|
}
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function getOpVal($op, &$EX, $free = false) // {{{
|
||
![]()
17 years ago
|
{
|
||
|
switch ($op['op_type']) {
|
||
|
case XC_IS_CONST:
|
||
![]()
12 years ago
|
return value($op['constant']);
|
||
![]()
17 years ago
|
|
||
|
case XC_IS_VAR:
|
||
|
case XC_IS_TMP_VAR:
|
||
|
$T = &$EX['Ts'];
|
||
![]()
12 years ago
|
$ret = $T[$op['var']];
|
||
![]()
12 years ago
|
if ($free && empty($this->keepTs)) {
|
||
![]()
12 years ago
|
unset($T[$op['var']]);
|
||
![]()
17 years ago
|
}
|
||
|
return $ret;
|
||
|
|
||
|
case XC_IS_CV:
|
||
![]()
12 years ago
|
$var = $op['var'];
|
||
![]()
17 years ago
|
$var = $EX['op_array']['vars'][$var];
|
||
|
return '$' . $var['name'];
|
||
|
|
||
|
case XC_IS_UNUSED:
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function removeKeyPrefix($array, $prefix) // {{{
|
||
|
{
|
||
|
$prefixLen = strlen($prefix);
|
||
|
$ret = array();
|
||
|
foreach ($array as $key => $value) {
|
||
|
if (substr($key, 0, $prefixLen) == $prefix) {
|
||
|
$key = substr($key, $prefixLen);
|
||
|
}
|
||
|
$ret[$key] = $value;
|
||
|
}
|
||
|
return $ret;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function &fixOpcode($opcodes, $removeTailing = false, $defaultReturnValue = null) // {{{
|
||
![]()
17 years ago
|
{
|
||
![]()
12 years ago
|
$last = count($opcodes) - 1;
|
||
|
for ($i = 0; $i <= $last; $i ++) {
|
||
![]()
12 years ago
|
if (function_exists('xcache_get_fixed_opcode')) {
|
||
|
$opcodes[$i]['opcode'] = xcache_get_fixed_opcode($opcodes[$i]['opcode'], $i);
|
||
|
}
|
||
|
if (isset($opcodes[$i]['op1'])) {
|
||
|
$opcodes[$i]['op1'] = $this->removeKeyPrefix($opcodes[$i]['op1'], 'u.');
|
||
|
$opcodes[$i]['op2'] = $this->removeKeyPrefix($opcodes[$i]['op2'], 'u.');
|
||
|
$opcodes[$i]['result'] = $this->removeKeyPrefix($opcodes[$i]['result'], 'u.');
|
||
|
}
|
||
|
else {
|
||
|
$op = array(
|
||
|
'op1' => array(),
|
||
|
'op2' => array(),
|
||
|
'op3' => array(),
|
||
|
);
|
||
|
foreach ($opcodes[$i] as $name => $value) {
|
||
|
if (preg_match('!^(op1|op2|result)\\.(.*)!', $name, $m)) {
|
||
|
list(, $which, $field) = $m;
|
||
|
$op[$which][$field] = $value;
|
||
|
}
|
||
|
else if (preg_match('!^(op1|op2|result)_type$!', $name, $m)) {
|
||
|
list(, $which) = $m;
|
||
|
$op[$which]['op_type'] = $value;
|
||
|
}
|
||
|
else {
|
||
|
$op[$name] = $value;
|
||
|
}
|
||
|
}
|
||
|
$opcodes[$i] = $op;
|
||
|
}
|
||
|
}
|
||
![]()
12 years ago
|
|
||
|
if ($removeTailing) {
|
||
|
$last = count($opcodes) - 1;
|
||
|
if ($opcodes[$last]['opcode'] == XC_HANDLE_EXCEPTION) {
|
||
![]()
12 years ago
|
$this->usedOps[XC_HANDLE_EXCEPTION] = true;
|
||
![]()
12 years ago
|
$opcodes[$last]['opcode'] = XC_NOP;
|
||
![]()
12 years ago
|
--$last;
|
||
|
}
|
||
|
if ($opcodes[$last]['opcode'] == XC_RETURN) {
|
||
|
$op1 = $opcodes[$last]['op1'];
|
||
|
if ($op1['op_type'] == XC_IS_CONST && array_key_exists('constant', $op1) && $op1['constant'] === $defaultReturnValue) {
|
||
![]()
12 years ago
|
$opcodes[$last]['opcode'] = XC_NOP;
|
||
![]()
12 years ago
|
--$last;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
![]()
12 years ago
|
return $opcodes;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function decompileBasicBlock(&$EX, $range, $unhandled = false) // {{{
|
||
![]()
12 years ago
|
{
|
||
![]()
12 years ago
|
$this->dasmBasicBlock($EX, $range);
|
||
![]()
12 years ago
|
if ($unhandled) {
|
||
![]()
12 years ago
|
$this->dumpRange($EX, $range);
|
||
![]()
12 years ago
|
}
|
||
![]()
12 years ago
|
$this->outputPhp($EX, $range);
|
||
![]()
12 years ago
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function isIfCondition(&$EX, $range) // {{{
|
||
|
{
|
||
|
$opcodes = &$EX['opcodes'];
|
||
|
$firstOp = &$opcodes[$range[0]];
|
||
|
return $firstOp['opcode'] == XC_JMPZ && !empty($firstOp['jmpouts']) && $opcodes[$firstOp['jmpouts'][0] - 1]['opcode'] == XC_JMP
|
||
|
&& !empty($opcodes[$firstOp['jmpouts'][0] - 1]['jmpouts'])
|
||
|
&& $opcodes[$firstOp['jmpouts'][0] - 1]['jmpouts'][0] == $range[1] + 1;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function removeJmpInfo(&$EX, $line) // {{{
|
||
|
{
|
||
|
$opcodes = &$EX['opcodes'];
|
||
|
foreach ($opcodes[$line]['jmpouts'] as $jmpTo) {
|
||
|
$jmpins = &$opcodes[$jmpTo]['jmpins'];
|
||
|
$jmpins = array_flip($jmpins);
|
||
|
unset($jmpins[$line]);
|
||
|
$jmpins = array_keys($jmpins);
|
||
|
}
|
||
![]()
12 years ago
|
// $opcodes[$line]['opcode'] = XC_NOP;
|
||
![]()
12 years ago
|
unset($opcodes[$line]['jmpouts']);
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function beginScope(&$EX, $doIndent = true) // {{{
|
||
|
{
|
||
|
array_push($EX['scopeStack'], array($EX['lastBlock'], $EX['indent']));
|
||
|
if ($doIndent) {
|
||
|
$EX['indent'] .= INDENT;
|
||
|
}
|
||
|
$EX['lastBlock'] = null;
|
||
|
}
|
||
|
// }}}
|
||
|
function endScope(&$EX) // {{{
|
||
|
{
|
||
|
list($EX['lastBlock'], $EX['indent']) = array_pop($EX['scopeStack']);
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function beginComplexBlock(&$EX) // {{{
|
||
|
{
|
||
|
if (isset($EX['lastBlock'])) {
|
||
|
echo PHP_EOL;
|
||
|
$EX['lastBlock'] = null;
|
||
|
}
|
||
|
}
|
||
|
// }}}
|
||
|
function endComplexBlock(&$EX) // {{{
|
||
|
{
|
||
|
$EX['lastBlock'] = 'complex';
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
function decompileComplexBlock(&$EX, $range) // {{{
|
||
![]()
12 years ago
|
{
|
||
![]()
12 years ago
|
$T = &$EX['Ts'];
|
||
![]()
12 years ago
|
$opcodes = &$EX['opcodes'];
|
||
![]()
12 years ago
|
$indent = $EX['indent'];
|
||
![]()
12 years ago
|
|
||
![]()
12 years ago
|
$firstOp = &$opcodes[$range[0]];
|
||
|
$lastOp = &$opcodes[$range[1]];
|
||
![]()
12 years ago
|
|
||
![]()
12 years ago
|
// {{{ && || and or
|
||
|
if (($firstOp['opcode'] == XC_JMPZ_EX || $firstOp['opcode'] == XC_JMPNZ_EX) && !empty($firstOp['jmpouts'])
|
||
|
&& $firstOp['jmpouts'][0] == $range[1] + 1
|
||
|
&& $lastOp['opcode'] == XC_BOOL
|
||
|
&& $firstOp['opcode']['result']['var'] == $lastOp['opcode']['result']['var']
|
||
|
) {
|
||
|
$this->removeJmpInfo($EX, $range[0]);
|
||
|
|
||
![]()
12 years ago
|
$this->recognizeAndDecompileClosedBlocks($EX, array($range[0], $range[0]));
|
||
![]()
12 years ago
|
$op1 = $this->getOpVal($firstOp['result'], $EX, true);
|
||
![]()
12 years ago
|
|
||
![]()
12 years ago
|
$this->recognizeAndDecompileClosedBlocks($EX, array($range[0] + 1, $range[1]));
|
||
![]()
12 years ago
|
$op2 = $this->getOpVal($lastOp['result'], $EX, true);
|
||
![]()
12 years ago
|
|
||
|
$T[$firstOp['result']['var']] = new Decompiler_Binop($this, $op1, $firstOp['opcode'], $op2);
|
||
|
return false;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
// {{{ ?: excluding JMP_SET
|
||
![]()
12 years ago
|
if ($firstOp['opcode'] == XC_JMPZ && !empty($firstOp['jmpouts'])
|
||
![]()
12 years ago
|
&& $range[1] >= $range[0] + 3
|
||
![]()
12 years ago
|
&& $opcodes[$firstOp['jmpouts'][0] - 2]['opcode'] == XC_QM_ASSIGN
|
||
![]()
12 years ago
|
&& $opcodes[$firstOp['jmpouts'][0] - 1]['opcode'] == XC_JMP && $opcodes[$firstOp['jmpouts'][0] - 1]['jmpouts'][0] == $range[1] + 1
|
||
![]()
12 years ago
|
&& $lastOp['opcode'] == XC_QM_ASSIGN
|
||
|
) {
|
||
![]()
12 years ago
|
$trueRange = array($range[0] + 1, $firstOp['jmpouts'][0] - 2);
|
||
|
$falseRange = array($firstOp['jmpouts'][0], $range[1]);
|
||
|
$this->removeJmpInfo($EX, $range[0]);
|
||
![]()
12 years ago
|
|
||
|
$condition = $this->getOpVal($firstOp['op1'], $EX);
|
||
![]()
12 years ago
|
$this->recognizeAndDecompileClosedBlocks($EX, $trueRange);
|
||
![]()
12 years ago
|
$trueValue = $this->getOpVal($opcodes[$trueRange[1]]['result'], $EX, true);
|
||
![]()
12 years ago
|
$this->recognizeAndDecompileClosedBlocks($EX, $falseRange);
|
||
![]()
12 years ago
|
$falseValue = $this->getOpVal($opcodes[$falseRange[1]]['result'], $EX, true);
|
||
![]()
12 years ago
|
$T[$opcodes[$trueRange[1]]['result']['var']] = new Decompiler_TriOp($condition, $trueValue, $falseValue);
|
||
![]()
12 years ago
|
return false;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
// {{{ goto
|
||
|
if ($firstOp['opcode'] == XC_JMP && !empty($firstOp['jmpouts']) && $firstOp['jmpouts'][0] == $range[1] + 1) {
|
||
|
$this->removeJmpInfo($EX, $range[0]);
|
||
|
$firstOp['opcode'] = XC_GOTO;
|
||
|
$target = $firstOp['op1']['var'];
|
||
|
$firstOp['goto'] = $target;
|
||
|
$opcodes[$target]['gofrom'][] = $range[0];
|
||
|
|
||
![]()
12 years ago
|
$this->recognizeAndDecompileClosedBlocks($EX, $range);
|
||
![]()
12 years ago
|
return false;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
// {{{ for
|
||
![]()
12 years ago
|
if (!empty($firstOp['jmpins']) && $opcodes[$firstOp['jmpins'][0]]['opcode'] == XC_JMP
|
||
|
&& $lastOp['opcode'] == XC_JMP && !empty($lastOp['jmpouts']) && $lastOp['jmpouts'][0] <= $firstOp['jmpins'][0]
|
||
|
&& !empty($opcodes[$range[1] + 1]['jmpins']) && $opcodes[$opcodes[$range[1] + 1]['jmpins'][0]]['opcode'] == XC_JMPZNZ
|
||
|
) {
|
||
|
$nextRange = array($lastOp['jmpouts'][0], $firstOp['jmpins'][0]);
|
||
|
$conditionRange = array($range[0], $nextRange[0] - 1);
|
||
|
$this->removeJmpInfo($EX, $conditionRange[1]);
|
||
|
$bodyRange = array($nextRange[1], $range[1]);
|
||
|
$this->removeJmpInfo($EX, $bodyRange[1]);
|
||
|
|
||
|
$initial = '';
|
||
![]()
12 years ago
|
$this->beginScope($EX);
|
||
|
$this->dasmBasicBlock($EX, $conditionRange);
|
||
![]()
12 years ago
|
$conditionCodes = array();
|
||
|
for ($i = $conditionRange[0]; $i <= $conditionRange[1]; ++$i) {
|
||
|
if (isset($opcodes[$i]['php'])) {
|
||
|
$conditionCodes[] = str($opcodes[$i]['php'], $EX);
|
||
|
}
|
||
|
}
|
||
|
$conditionCodes[] = str($this->getOpVal($opcodes[$conditionRange[1]]['op1'], $EX), $EX);
|
||
|
if (implode(',', $conditionCodes) == 'true') {
|
||
|
$conditionCodes = array();
|
||
|
}
|
||
![]()
12 years ago
|
$this->endScope($EX);
|
||
![]()
12 years ago
|
|
||
![]()
12 years ago
|
$this->beginScope($EX);
|
||
|
$this->dasmBasicBlock($EX, $nextRange);
|
||
![]()
12 years ago
|
$nextCodes = array();
|
||
|
for ($i = $nextRange[0]; $i <= $nextRange[1]; ++$i) {
|
||
|
if (isset($opcodes[$i]['php'])) {
|
||
|
$nextCodes[] = str($opcodes[$i]['php'], $EX);
|
||
|
}
|
||
|
}
|
||
![]()
12 years ago
|
$this->endScope($EX);
|
||
|
|
||
![]()
12 years ago
|
$this->beginComplexBlock($EX);
|
||
|
echo $indent, 'for (', str($initial, $EX), '; ', implode(', ', $conditionCodes), '; ', implode(', ', $nextCodes), ') ', '{', PHP_EOL;
|
||
![]()
12 years ago
|
$this->beginScope($EX);
|
||
|
$this->recognizeAndDecompileClosedBlocks($EX, $bodyRange);
|
||
|
$this->endScope($EX);
|
||
![]()
12 years ago
|
echo $indent, '}', PHP_EOL;
|
||
|
$this->endComplexBlock($EX);
|
||
|
return;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
// {{{ if/elseif/else
|
||
|
if ($this->isIfCondition($EX, $range)) {
|
||
|
$this->beginComplexBlock($EX);
|
||
|
$isElseIf = false;
|
||
|
do {
|
||
|
$ifRange = array($range[0], $opcodes[$range[0]]['jmpouts'][0] - 1);
|
||
|
$this->removeJmpInfo($EX, $ifRange[0]);
|
||
|
$this->removeJmpInfo($EX, $ifRange[1]);
|
||
|
$condition = $this->getOpVal($opcodes[$ifRange[0]]['op1'], $EX);
|
||
|
|
||
|
echo $indent, $isElseIf ? 'else if' : 'if', ' (', str($condition, $EX), ') ', '{', PHP_EOL;
|
||
![]()
12 years ago
|
$this->beginScope($EX);
|
||
|
$this->recognizeAndDecompileClosedBlocks($EX, $ifRange);
|
||
|
$this->endScope($EX);
|
||
![]()
12 years ago
|
$EX['lastBlock'] = null;
|
||
|
echo $indent, '}', PHP_EOL;
|
||
|
|
||
|
$isElseIf = true;
|
||
|
// search for else if
|
||
|
$range[0] = $ifRange[1] + 1;
|
||
|
for ($i = $ifRange[1] + 1; $i <= $range[1]; ++$i) {
|
||
|
// find first jmpout
|
||
|
if (!empty($opcodes[$i]['jmpouts'])) {
|
||
|
if ($this->isIfCondition($EX, array($i, $range[1]))) {
|
||
|
$this->dasmBasicBlock($EX, array($range[0], $i));
|
||
|
$range[0] = $i;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
} while ($this->isIfCondition($EX, $range));
|
||
|
if ($ifRange[1] < $range[1]) {
|
||
|
$elseRange = array($ifRange[1], $range[1]);
|
||
|
echo $indent, 'else ', '{', PHP_EOL;
|
||
![]()
12 years ago
|
$this->beginScope($EX);
|
||
|
$this->recognizeAndDecompileClosedBlocks($EX, $elseRange);
|
||
|
$this->endScope($EX);
|
||
![]()
12 years ago
|
$EX['lastBlock'] = null;
|
||
|
echo $indent, '}', PHP_EOL;
|
||
|
}
|
||
|
$this->endComplexBlock($EX);
|
||
|
return;
|
||
|
}
|
||
![]()
12 years ago
|
if ($firstOp['opcode'] == XC_JMPZ && !empty($firstOp['jmpouts'])
|
||
|
&& $firstOp['jmpouts'][0] - 1 == $range[1] && $opcodes[$firstOp['jmpouts'][0] - 1]['opcode'] == XC_RETURN) {
|
||
|
$this->beginComplexBlock($EX);
|
||
|
$this->removeJmpInfo($EX, $range[0]);
|
||
|
$condition = $this->getOpVal($opcodes[$range[0]]['op1'], $EX);
|
||
|
|
||
|
echo $indent, 'if (', str($condition, $EX), ') ', '{', PHP_EOL;
|
||
|
$this->beginScope($EX);
|
||
|
$this->recognizeAndDecompileClosedBlocks($EX, $range);
|
||
|
$this->endScope($EX);
|
||
|
echo $indent, '}', PHP_EOL;
|
||
|
$this->endComplexBlock($EX);
|
||
|
return;
|
||
|
}
|
||
![]()
12 years ago
|
// }}}
|
||
![]()
12 years ago
|
// {{{ try/catch
|
||
![]()
12 years ago
|
if (!empty($firstOp['jmpins']) && !empty($opcodes[$firstOp['jmpins'][0]]['isCatchBegin'])) {
|
||
|
$catchBlocks = array();
|
||
|
$catchFirst = $firstOp['jmpins'][0];
|
||
|
|
||
![]()
12 years ago
|
$tryRange = array($range[0], $catchFirst - 1);
|
||
![]()
12 years ago
|
|
||
|
// search for XC_CATCH
|
||
|
$this->removeJmpInfo($EX, $catchFirst);
|
||
![]()
12 years ago
|
for ($i = $catchFirst; $i <= $range[1]; ) {
|
||
![]()
12 years ago
|
if ($opcodes[$i]['opcode'] == XC_CATCH) {
|
||
|
$catchOpLine = $i;
|
||
|
$this->removeJmpInfo($EX, $catchOpLine);
|
||
|
|
||
|
$catchNext = $opcodes[$catchOpLine]['extended_value'];
|
||
|
$catchBodyLast = $catchNext - 1;
|
||
|
if ($opcodes[$catchBodyLast]['opcode'] == XC_JMP) {
|
||
|
--$catchBodyLast;
|
||
|
}
|
||
|
|
||
|
$catchBlocks[$catchFirst] = array($catchOpLine, $catchBodyLast);
|
||
|
|
||
|
$i = $catchFirst = $catchNext;
|
||
|
}
|
||
|
else {
|
||
|
++$i;
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
if ($opcodes[$tryRange[1]]['opcode'] == XC_JMP) {
|
||
|
--$tryRange[1];
|
||
![]()
12 years ago
|
}
|
||
|
|
||
|
$this->beginComplexBlock($EX);
|
||
![]()
12 years ago
|
echo $indent, "try {", PHP_EOL;
|
||
![]()
12 years ago
|
$this->beginScope($EX);
|
||
|
$this->recognizeAndDecompileClosedBlocks($EX, $tryRange);
|
||
|
$this->endScope($EX);
|
||
![]()
12 years ago
|
echo $indent, '}', PHP_EOL;
|
||
|
foreach ($catchBlocks as $catchFirst => $catchInfo) {
|
||
|
list($catchOpLine, $catchBodyLast) = $catchInfo;
|
||
|
$catchBodyFirst = $catchOpLine + 1;
|
||
![]()
12 years ago
|
$this->dasmBasicBlock($EX, array($catchFirst, $catchOpLine));
|
||
![]()
12 years ago
|
$catchOp = &$opcodes[$catchOpLine];
|
||
![]()
12 years ago
|
echo $indent, 'catch (', str($this->getOpVal($catchOp['op1'], $EX)), ' ', str($this->getOpVal($catchOp['op2'], $EX)), ") {", PHP_EOL;
|
||
![]()
12 years ago
|
unset($catchOp);
|
||
|
|
||
|
$EX['lastBlock'] = null;
|
||
![]()
12 years ago
|
$this->beginScope($EX);
|
||
|
$this->recognizeAndDecompileClosedBlocks($EX, array($catchBodyFirst, $catchBodyLast));
|
||
|
$this->endScope($EX);
|
||
![]()
12 years ago
|
echo $indent, '}', PHP_EOL;
|
||
|
}
|
||
|
$this->endComplexBlock($EX);
|
||
|
return;
|
||
|
}
|
||
|
// }}}
|
||
![]()
12 years ago
|
// {{{ switch/case
|
||
![]()
12 years ago
|
if ($firstOp['opcode'] == XC_SWITCH_FREE && isset($T[$firstOp['op1']['var']])) {
|
||
|
// TODO: merge this code to CASE code. use SWITCH_FREE to detect begin of switch by using $Ts if possible
|
||
|
$this->beginComplexBlock($EX);
|
||
![]()
12 years ago
|
echo $indent, 'switch (', str($this->getOpVal($firstOp['op1'], $EX)), ") {", PHP_EOL;
|
||
![]()
12 years ago
|
echo $indent, '}', PHP_EOL;
|
||
|
$this->endComplexBlock($EX);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (
|
||
|
($firstOp['opcode'] == XC_CASE
|
||
|
|| $firstOp['opcode'] == XC_JMP && !empty($firstOp['jmpouts']) && $opcodes[$firstOp['jmpouts'][0]]['opcode'] == XC_CASE
|
||
|
)
|
||
|
&& !empty($lastOp['jmpouts'])
|
||
|
) {
|
||
|
$cases = array();
|
||
|
$caseDefault = null;
|
||
|
$caseOp = null;
|
||
![]()
12 years ago
|
for ($i = $range[0]; $i <= $range[1]; ) {
|
||
![]()
12 years ago
|
$op = $opcodes[$i];
|
||
|
if ($op['opcode'] == XC_CASE) {
|
||
|
if (!isset($caseOp)) {
|
||
|
$caseOp = $op;
|
||
|
}
|
||
|
$jmpz = $opcodes[$i + 1];
|
||
|
assert('$jmpz["opcode"] == XC_JMPZ');
|
||
|
$caseNext = $jmpz['jmpouts'][0];
|
||
![]()
12 years ago
|
$cases[$i] = $caseNext - 1;
|
||
|
$i = $caseNext;
|
||
![]()
12 years ago
|
}
|
||
![]()
12 years ago
|
else if ($op['opcode'] == XC_JMP && $op['jmpouts'][0] >= $i) {
|
||
![]()
12 years ago
|
// default
|
||
![]()
12 years ago
|
$caseNext = $op['jmpouts'][0];
|
||
|
$caseDefault = $i;
|
||
|
$cases[$i] = $caseNext - 1;
|
||
|
$i = $caseNext;
|
||
|
}
|
||
|
else {
|
||
|
++$i;
|
||
![]()
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
$this->beginComplexBlock($EX);
|
||
|
|
||
![]()
12 years ago
|
echo $indent, 'switch (', str($this->getOpVal($caseOp['op1'], $EX), $EX), ") {", PHP_EOL;
|
||
![]()
12 years ago
|
$caseIsOut = false;
|
||
|
foreach ($cases as $caseFirst => $caseLast) {
|
||
![]()
12 years ago
|
if ($caseIsOut && empty($lastCaseFall)) {
|
||
![]()
12 years ago
|
echo PHP_EOL;
|
||
|
}
|
||
|
|
||
|
$caseOp = $opcodes[$caseFirst];
|
||
|
|
||
|
echo $indent;
|
||
|
if ($caseOp['opcode'] == XC_CASE) {
|
||
|
echo 'case ';
|
||
|
echo str($this->getOpVal($caseOp['op2'], $EX), $EX);
|
||
|
echo ':', PHP_EOL;
|
||
|
|
||
|
$this->removeJmpInfo($EX, $caseFirst);
|
||
|
++$caseFirst;
|
||
|
|
||
|
assert('$opcodes[$caseFirst]["opcode"] == XC_JMPZ');
|
||
|
$this->removeJmpInfo($EX, $caseFirst);
|
||
|
++$caseFirst;
|
||
|
}
|
||
|
else {
|
||
|
echo 'default';
|
||
|
echo ':', PHP_EOL;
|
||
|
|
||
|
assert('$opcodes[$caseFirst]["opcode"] == XC_JMP');
|
||
|
$this->removeJmpInfo($EX, $caseFirst);
|
||
|
++$caseFirst;
|
||
|
}
|
||
|
|
||
|
assert('$opcodes[$caseLast]["opcode"] == XC_JMP');
|
||
|
$this->removeJmpInfo($EX, $caseLast);
|
||
|
--$caseLast;
|
||
|
switch ($opcodes[$caseLast]['opcode']) {
|
||
|
case XC_BRK:
|
||
|
case XC_CONT:
|
||
|
case XC_GOTO:
|
||
|
$lastCaseFall = false;
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
$lastCaseFall = true;
|
||
|
}
|
||
|
|
||
![]()
12 years ago
|
$this->beginScope($EX);
|
||
|
$this->recognizeAndDecompileClosedBlocks($EX, array($caseFirst, $caseLast));
|
||
|
$this->endScope($EX);
|
||
![]()
12 years ago
|
$caseIsOut = true;
|
||
|
}
|
||
|
echo $indent, '}', PHP_EOL;
|
||
|
|
||
|
$this->endComplexBlock($EX);
|
||
|
return;
|
||
|
}
|
||
![]()
12 years ago
|
// }}}
|
||
|
// {{{ do/while
|
||
![]()
12 years ago
|
if ($lastOp['opcode'] == XC_JMPNZ && !empty($lastOp['jmpouts'])
|
||
![]()
12 years ago
|
&& $lastOp['jmpouts'][0] == $range[0]) {
|
||
|
$this->removeJmpInfo($EX, $range[1]);
|
||
![]()
12 years ago
|
$this->beginComplexBlock($EX);
|
||
![]()
12 years ago
|
|
||
![]()
12 years ago
|
echo $indent, "do {", PHP_EOL;
|
||
![]()
12 years ago
|
$this->beginScope($EX);
|
||
|
$this->recognizeAndDecompileClosedBlocks($EX, $range);
|
||
|
$this->endScope($EX);
|
||
![]()
12 years ago
|
echo $indent, "} while (", str($this->getOpVal($lastOp['op1'], $EX)), ');', PHP_EOL;
|
||
![]()
12 years ago
|
|
||
![]()
12 years ago
|
$this->endComplexBlock($EX);
|
||
![]()
12 years ago
|
return;
|
||
|
}
|
||
![]()
12 years ago
|
// }}}
|
||
![]()
12 years ago
|
|
||
![]()
12 years ago
|
// {{{ search firstJmpOp
|
||
![]()
12 years ago
|
$firstJmp = null;
|
||
|
$firstJmpOp = null;
|
||
![]()
12 years ago
|
for ($i = $range[0]; $i <= $range[1]; ++$i) {
|
||
![]()
12 years ago
|
if (!empty($opcodes[$i]['jmpouts'])) {
|
||
|
$firstJmp = $i;
|
||
|
$firstJmpOp = &$opcodes[$firstJmp];
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
![]()
12 years ago
|
// }}}
|
||
![]()
12 years ago
|
|
||
![]()
12 years ago
|
// {{{ while
|
||
![]()
12 years ago
|
if (isset($firstJmpOp)
|
||
|
&& $firstJmpOp['opcode'] == XC_JMPZ
|
||
![]()
12 years ago
|
&& $firstJmpOp['jmpouts'][0] > $range[1]
|
||
![]()
12 years ago
|
&& $lastOp['opcode'] == XC_JMP && !empty($lastOp['jmpouts'])
|
||
![]()
12 years ago
|
&& $lastOp['jmpouts'][0] == $range[0]) {
|
||
![]()
12 years ago
|
$this->removeJmpInfo($EX, $firstJmp);
|
||
![]()
12 years ago
|