XCache is a fast, stable PHP opcode cacher that has been proven and is now running on production servers under high load. https://xcache.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3171 lines
82 KiB

<?php
define('INDENT', "\t");
ini_set('error_reporting', E_ALL);
assert_options(ASSERT_ACTIVE, 0);
function color($str, $color = 33)
{
return "\x1B[{$color}m$str\x1B[0m";
}
function printBacktrace() // {{{
{
$backtrace = debug_backtrace();
foreach ($backtrace as $stack) {
$args = array();
foreach ($stack['args'] as $arg) {
if (is_scalar($arg)) {
$args[] = var_export($arg, true);
}
else if (is_array($arg)) {
$array = array();
foreach ($arg as $key => $value) {
$array[] = var_export($key, true) . " => " . (is_scalar($value) ? var_export($value, true) : gettype($value));
if (count($array) >= 5) {
$array[] = '...';
break;
}
}
$args[] = "array(" . implode(', ', $array) . ')';
}
else {
$args[] = gettype($arg);
}
}
printf("%d: %s::%s(%s)" . PHP_EOL
, $stack['line']
, isset($stack['class']) ? $stack['class'] : ''
, $stack['function']
, implode(', ', $args)
);
}
}
// }}}
function str($code, $indent = '') // {{{
{
if (is_array($code)) {
$array = array();
foreach ($code as $key => $value) {
$array[$key] = str($value, $indent);
}
return $array;
}
if (is_object($code)) {
$code = foldToCode($code, $indent);
return $code->toCode($indent);
}
return (string) $code;
}
// }}}
function unsetArray(&$array, $name) // {{{
{
unset($array[$name]);
}
// }}}
function foldToCode($src, $indent = '') // {{{ wrap or rewrap anything to Decompiler_Code
{
if (is_array($indent)) {
$indent = $indent['indent'];
}
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));
}
return $src;
}
// }}}
function decompileAst($ast, $EX) // {{{
{
$kind = $ast['kind'];
$children = $ast['children'];
unset($ast['kind']);
unset($ast['children']);
switch ($kind) {
case ZEND_CONST:
return value($ast[0], $EX);
case XC_INIT_ARRAY:
$array = new Decompiler_Array();
for ($i = 0; $i < $children; $i += 2) {
if (isset($ast[$i + 1])) {
$key = decompileAst($ast[$i], $EX);
$value = decompileAst($ast[$i + 1], $EX);
$array->value[] = array($key, $value);
}
else {
$array->value[] = array(null, decompileAst($ast[$i], $EX));
}
}
return $array;
// ZEND_BOOL_AND: handled in binop
// ZEND_BOOL_OR: handled in binop
case ZEND_SELECT:
return new Decompiler_TriOp(
decompileAst($ast[0], $EX)
, decompileAst($ast[1], $EX)
, decompileAst($ast[2], $EX)
);
case ZEND_UNARY_PLUS:
return new Decompiler_Code('+' . str(decompileAst($ast[0], $EX)));
case ZEND_UNARY_MINUS:
return new Decompiler_Code('-' . str(decompileAst($ast[0], $EX)));
default:
$decompiler = $GLOBALS['__xcache_decompiler'];
if (isset($decompiler->binops[$kind])) {
return new Decompiler_Binop($decompiler
, decompileAst($ast[0], $EX)
, $kind
, decompileAst($ast[1], $EX)
);
}
return "un-handled kind $kind in zend_ast";
}
}
// }}}
function value($value, &$EX) // {{{
{
if (ZEND_ENGINE_2_6 && (xcache_get_type($value) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT_AST) {
return decompileAst(xcache_dasm_ast($value), $EX);
}
$originalValue = xcache_get_special_value($value);
if (isset($originalValue)) {
if ((xcache_get_type($value) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT) {
// constant
return $GLOBALS['__xcache_decompiler']->stripNamespace($originalValue);
}
$value = $originalValue;
}
if (is_a($value, 'Decompiler_Object')) {
// use as is
}
else if (is_array($value)) {
$value = new Decompiler_ConstArray($value, $EX);
}
else {
if (isset($EX['value2constant'][$value])) {
$value = new Decompiler_Code($EX['value2constant'][$value]);
}
else {
$value = new Decompiler_Value($value);
}
}
return $value;
}
// }}}
function unquoteName_($str, $asVariableName, $indent = '') // {{{
{
$str = str($str, $indent);
if (preg_match("!^'[\\w_][\\w\\d_\\\\]*'\$!", $str)) {
return str_replace('\\\\', '\\', substr($str, 1, -1));
}
else if ($asVariableName) {
return "{" . $str . "}";
}
else {
return $str;
}
}
// }}}
function unquoteVariableName($str, $indent = '') // {{{
{
return unquoteName_($str, true, $indent);
}
// }}}
function unquoteName($str, $indent = '') // {{{
{
return unquoteName_($str, false, $indent);
}
// }}}
class Decompiler_Object // {{{
{
}
// }}}
class Decompiler_Value extends Decompiler_Object // {{{
{
var $value;
function Decompiler_Value($value = null)
{
$this->value = $value;
}
function toCode($indent)
{
$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;
}
}
// }}}
class Decompiler_Code extends Decompiler_Object // {{{
{
var $src;
function Decompiler_Code($src)
{
if (!assert('isset($src)')) {
printBacktrace();
}
$this->src = $src;
}
function toCode($indent)
{
return $this->src;
}
}
// }}}
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;
}
function toCode($indent)
{
$opstr = $this->parent->binops[$this->opc];
if (is_a($this->op1, 'Decompiler_TriOp') || is_a($this->op1, 'Decompiler_Binop') && $this->op1->opc != $this->opc) {
$op1 = "(" . str($this->op1, $indent) . ")";
}
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;
}
if (str($op1) == '0' && ($this->opc == XC_ADD || $this->opc == XC_SUB)) {
return $opstr . str($op2, $indent);
}
return str($op1, $indent) . ' ' . $opstr . ($this->opc == XC_ASSIGN_REF ? '' : ' ') . str($op2, $indent);
}
}
// }}}
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);
}
}
// }}}
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;
}
function toCode($indent)
{
switch ($this->fetchType) {
case ZEND_FETCH_LOCAL:
return '$' . $this->src;
case ZEND_FETCH_STATIC:
if (ZEND_ENGINE_2_3) {
// closure local variable?
return 'STR' . str($this->src);
}
else {
$EX = array();
return str(value($this->src, $EX));
}
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;
}
function toCode($indent)
{
return $this->obj->toCode($indent);
}
}
// }}}
class Decompiler_Dim extends Decompiler_Value // {{{
{
var $offsets = array();
var $isLast = false;
var $isObject = false;
var $assign = null;
function toCode($indent)
{
if (is_a($this->value, 'Decompiler_ListBox')) {
$exp = str($this->value->obj->src, $indent);
}
else {
$exp = str($this->value, $indent);
}
$last = count($this->offsets) - 1;
foreach ($this->offsets as $i => $dim) {
if ($this->isObject && $i == $last) {
$exp .= '->' . unquoteVariableName($dim, $indent);
}
else {
$exp .= '[' . str($dim, $indent) . ']';
}
}
return $exp;
}
}
// }}}
class Decompiler_DimBox extends Decompiler_Box // {{{
{
}
// }}}
class Decompiler_List extends Decompiler_Code // {{{
{
var $src;
var $dims = array();
var $everLocked = false;
function toCode($indent)
{
if (count($this->dims) == 1 && !$this->everLocked) {
$dim = $this->dims[0];
unset($dim->value);
$dim->value = $this->src;
if (!isset($dim->assign)) {
return str($dim, $indent);
}
return str($this->dims[0]->assign, $indent) . ' = ' . str($dim, $indent);
}
/* flatten dims */
$assigns = array();
foreach ($this->dims as $dim) {
$assign = &$assigns;
foreach ($dim->offsets as $offset) {
$assign = &$assign[$offset];
}
$assign = foldToCode($dim->assign, $indent);
}
return str($this->toList($assigns)) . ' = ' . str($this->src, $indent);
}
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 // {{{
{
// emenets
function Decompiler_Array()
{
$this->value = array();
}
function toCode($indent)
{
$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);
}
$exp = "array(";
$indent = $indent . INDENT;
$assocWidth = 0;
$multiline = 0;
$i = 0;
foreach ($elementsCode as $element) {
list($keyCode, $valueCode) = $element;
if ((string) $i !== $keyCode) {
$assocWidth = 1;
break;
}
++$i;
}
foreach ($elementsCode as $element) {
list($keyCode, $valueCode, $key, $value) = $element;
if ($assocWidth) {
$len = strlen($keyCode);
if ($assocWidth < $len) {
$assocWidth = $len;
}
}
if (is_array($value) || is_a($value, 'Decompiler_Array')) {
$multiline++;
}
}
$i = 0;
foreach ($elementsCode as $element) {
list($keyCode, $value) = $element;
if ($multiline) {
if ($i) {
$exp .= ",";
}
$exp .= PHP_EOL;
$exp .= $indent;
}
else {
if ($i) {
$exp .= ", ";
}
}
if ($assocWidth) {
if ($multiline) {
$exp .= sprintf("%-{$assocWidth}s => ", $keyCode);
}
else {
$exp .= $keyCode . ' => ';
}
}
$exp .= $value;
$i++;
}
if ($multiline) {
$exp .= PHP_EOL . "$indent)";
}
else {
$exp .= ")";
}
return $exp;
}
}
// }}}
class Decompiler_ConstArray extends Decompiler_Array // {{{
{
function Decompiler_ConstArray($array, &$EX)
{
$elements = array();
foreach ($array as $key => $value) {
if ((xcache_get_type($value) & IS_CONSTANT_INDEX)) {
$keyCode = $GLOBALS['__xcache_decompiler']->stripNamespace(
ZEND_ENGINE_2_3
? substr($key, 0, -2)
: $key
);
}
else {
$keyCode = value($key, $EX);
}
$elements[] = array($keyCode, value($value, $EX));
}
$this->value = $elements;
}
}
// }}}
class Decompiler_ForeachBox extends Decompiler_Box // {{{
{
var $iskey;
function toCode($indent)
{
return '#foreachBox#';
}
}
// }}}
class Decompiler
{
var $namespace;
var $namespaceDecided;
var $activeFile;
var $activeDir;
var $activeClass;
var $activeMethod;
var $activeFunction;
var $outputPhp;
var $outputOpcode;
var $inComment = 0;
function Decompiler($outputTypes)
{
$this->outputPhp = in_array('php', $outputTypes);
$this->outputOpcode = in_array('opcode', $outputTypes);
$GLOBALS['__xcache_decompiler'] = $this;
// {{{ 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, PHP_EOL;
}
}
}
// }}}
// {{{ 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",
XC_ASSIGN => "=",
XC_ASSIGN_REF => "= &",
XC_JMP_SET => "?:",
XC_JMP_SET_VAR => "?:",
XC_JMPZ_EX => "&&",
XC_JMPNZ_EX => "||",
);
if (defined('IS_CONSTANT_AST')) {
$this->binops[ZEND_BOOL_AND] = '&&';
$this->binops[ZEND_BOOL_OR] = '||';
}
// }}}
$this->includeTypes = array( // {{{
ZEND_EVAL => 'eval',
ZEND_INCLUDE => 'include',
ZEND_INCLUDE_ONCE => 'include_once',
ZEND_REQUIRE => 'require',
ZEND_REQUIRE_ONCE => 'require_once',
);
// }}}
}
function detectNamespace($name) // {{{
{
if ($this->namespaceDecided) {
return;
}
if (strpos($name, '\\') !== false) {
$namespace = strtok($name, '\\');
if ($namespace == $this->namespace) {
return;
}
$this->namespace = $namespace;
echo 'namespace ', $this->namespace, ";", PHP_EOL, PHP_EOL;
}
$this->namespaceDecided = true;
}
// }}}
function stripNamespace($name) // {{{
{
if (!isset($name)) {
return $name;
}
$name = str($name);
$len = strlen($this->namespace) + 1;
if (substr($name, 0, $len) == $this->namespace . '\\') {
return substr($name, $len);
}
else {
return $name;
}
}
// }}}
function outputPhp($range) // {{{
{
$EX = &$range['EX'];
$needBlankline = isset($EX['lastBlock']);
$indent = $EX['indent'];
$curticks = 0;
for ($i = $range[0]; $i <= $range[1]; $i++) {
$op = $EX['opcodes'][$i];
if (isset($op['gofrom'])) {
if ($needBlankline) {
$needBlankline = false;
echo PHP_EOL;
}
echo 'label' . $i, ":", PHP_EOL;
}
if (isset($op['php'])) {
$toticks = isset($op['ticks']) ? (int) str($op['ticks']) : 0;
if ($curticks != $toticks) {
$oldticks = $curticks;
$curticks = $toticks;
if (!$curticks) {
echo $EX['indent'], "}", PHP_EOL, PHP_EOL;
$indent = $EX['indent'];
}
else {
if ($oldticks) {
echo $EX['indent'], "}", PHP_EOL, PHP_EOL;
}
else if (!$oldticks) {
$indent .= INDENT;
}
if ($needBlankline) {
$needBlankline = false;
echo PHP_EOL;
}
echo $EX['indent'], "declare (ticks=$curticks) {", PHP_EOL;
}
}
if ($needBlankline) {
$needBlankline = false;
echo PHP_EOL;
}
echo $indent, str($op['php'], $indent), ";", PHP_EOL;
$EX['lastBlock'] = 'basic';
}
}
if ($curticks) {
echo $EX['indent'], "}", PHP_EOL;
}
}
// }}}
function getOpVal($op, &$EX, $free = false) // {{{
{
switch ($op['op_type']) {
case XC_IS_CONST:
return value($op['constant'], $EX);
case XC_IS_VAR:
case XC_IS_TMP_VAR:
$T = &$EX['Ts'];
if (!isset($T[$op['var']])) {
if ($this->outputPhp && isset($free)) {
printBacktrace();
}
return null;
}
$ret = $T[$op['var']];
if ($free && empty($this->keepTs)) {
unset($T[$op['var']]);
}
return $ret;
case XC_IS_CV:
$var = $op['var'];
$var = $EX['op_array']['vars'][$var];
return '$' . $var['name'];
case XC_IS_UNUSED:
return null;
}
}
// }}}
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;
}
// }}}
function fixOpCode($opcodes, $removeTailing = false, $defaultReturnValue = null) // {{{
{
$last = count($opcodes) - 1;
for ($i = 0; $i <= $last; $i++) {
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(),
'result' => 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;
}
}
if ($removeTailing) {
$last = count($opcodes) - 1;
if ($opcodes[$last]['opcode'] == XC_HANDLE_EXCEPTION) {
$this->usedOps[XC_HANDLE_EXCEPTION] = true;
$opcodes[$last]['opcode'] = XC_NOP;
--$last;
}
if ($opcodes[$last]['opcode'] == XC_RETURN
|| $opcodes[$last]['opcode'] == XC_GENERATOR_RETURN) {
$op1 = $opcodes[$last]['op1'];
if ($op1['op_type'] == XC_IS_CONST && array_key_exists('constant', $op1) && $op1['constant'] === $defaultReturnValue) {
$opcodes[$last]['opcode'] = XC_NOP;
--$last;
}
}
}
return $opcodes;
}
// }}}
function decompileBasicBlock($range, $unhandled = false) // {{{
{
$this->dasmBasicBlock($range);
if ($unhandled) {
$this->dumpRange($range);
}
$this->outputPhp($range);
}
// }}}
function isIfCondition($range) // {{{
{
$opcodes = &$range['EX']['opcodes'];
$firstOp = &$opcodes[$range[0]];
return $firstOp['opcode'] == XC_JMPZ && !empty($firstOp['jmptos']) && $opcodes[$firstOp['jmptos'][0] - 1]['opcode'] == XC_JMP
&& !empty($opcodes[$firstOp['jmptos'][0] - 1]['jmptos'])
&& $opcodes[$firstOp['jmptos'][0] - 1]['jmptos'][0] == $range[1] + 1;
}
// }}}
function removeJmpInfo(&$EX, $line) // {{{
{
$opcodes = &$EX['opcodes'];
if (!isset($opcodes[$line]['jmptos'])) {
printBacktrace();
}
foreach ($opcodes[$line]['jmptos'] as $jmpTo) {
$jmpfroms = &$opcodes[$jmpTo]['jmpfroms'];
$jmpfroms = array_flip($jmpfroms);
unset($jmpfroms[$line]);
$jmpfroms = array_keys($jmpfroms);
}
// $opcodes[$line]['opcode'] = XC_NOP;
unset($opcodes[$line]['jmptos']);
}
// }}}
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']);
}
// }}}
function beginComplexBlock(&$EX) // {{{
{
if (isset($EX['lastBlock'])) {
echo PHP_EOL;
$EX['lastBlock'] = null;
}
}
// }}}
function endComplexBlock(&$EX) // {{{
{
$EX['lastBlock'] = 'complex';
}
// }}}
function op($range, $offset) // {{{
{
$opcodes = &$range['EX']['opcodes'];
if ($offset > 0) {
$last = $range['EX']['op_array']['last'];
for ($i = $offset; $i <= $range[1]; ++$i) {
if ($opcodes[$i]['opcode'] != XC_NOP) {
return $i;
}
}
}
else {
for ($i = -$offset; $i >= $range[0]; --$i) {
if ($opcodes[$i]['opcode'] != XC_NOP) {
return $i;
}
}
}
return -1;
}
// }}}
function decompileComplexBlock($range) // {{{
{
$EX = &$range['EX'];
$T = &$EX['Ts'];
$opcodes = &$EX['opcodes'];
$indent = $EX['indent'];
$firstOp = &$opcodes[$this->op($range, $range[0])];
$lastOp = &$opcodes[$this->op($range, -$range[1])];
// {{{ && || and or
if (($firstOp['opcode'] == XC_JMPZ_EX || $firstOp['opcode'] == XC_JMPNZ_EX) && !empty($firstOp['jmptos'])
&& $firstOp['jmptos'][0] == $range[1] + 1
&& $lastOp['opcode'] == XC_BOOL
&& $firstOp['opcode']['result']['var'] == $lastOp['opcode']['result']['var']
) {
$this->removeJmpInfo($EX, $range[0]);
$this->recognizeAndDecompileClosedBlocks(array($range[0], $range[0], 'EX' => &$EX));
$op1 = $this->getOpVal($firstOp['result'], $EX, true);
$this->recognizeAndDecompileClosedBlocks(array($range[0] + 1, $range[1], 'EX' => &$EX));
$op2 = $this->getOpVal($lastOp['result'], $EX, true);
$T[$firstOp['result']['var']] = new Decompiler_Binop($this, $op1, $firstOp['opcode'], $op2);
return false;
}
// }}}
// {{{ ?: excluding JMP_SET/JMP_SET_VAR
if ($firstOp['opcode'] == XC_JMPZ && !empty($firstOp['jmptos'])
&& $range[1] >= $range[0] + 3
&& ($opcodes[$firstOp['jmptos'][0] - 2]['opcode'] == XC_QM_ASSIGN || $opcodes[$firstOp['jmptos'][0] - 2]['opcode'] == XC_QM_ASSIGN_VAR)
&& $opcodes[$firstOp['jmptos'][0] - 1]['opcode'] == XC_JMP && $opcodes[$firstOp['jmptos'][0] - 1]['jmptos'][0] == $range[1] + 1
&& ($lastOp['opcode'] == XC_QM_ASSIGN || $lastOp['opcode'] == XC_QM_ASSIGN_VAR)
) {
$trueRange = array($range[0] + 1, $firstOp['jmptos'][0] - 2, 'EX' => &$EX);
$falseRange = array($firstOp['jmptos'][0], $range[1], 'EX' => &$EX);
$this->removeJmpInfo($EX, $range[0]);
$condition = $this->getOpVal($firstOp['op1'], $EX);
$this->recognizeAndDecompileClosedBlocks($trueRange);
$trueValue = $this->getOpVal($opcodes[$trueRange[1]]['result'], $EX, true);
$this->recognizeAndDecompileClosedBlocks($falseRange);
$falseValue = $this->getOpVal($opcodes[$falseRange[1]]['result'], $EX, true);
$T[$opcodes[$trueRange[1]]['result']['var']] = new Decompiler_TriOp($condition, $trueValue, $falseValue);
return false;
}
// }}}
// {{{ goto (TODO: recognize BRK which is translated to JMP by optimizer)
if ($firstOp['opcode'] == XC_JMP && !empty($firstOp['jmptos']) && $firstOp['jmptos'][0] == $range[1] + 1) {
$this->removeJmpInfo($EX, $range[0]);
assert(XC_GOTO != -1);
$firstOp['opcode'] = XC_GOTO;
$target = $firstOp['op1']['var'];
$firstOp['goto'] = $target;
$opcodes[$target]['gofrom'][] = $range[0];
$this->recognizeAndDecompileClosedBlocks($range);
return false;
}
// }}}
// {{{ for
if (!empty($firstOp['jmpfroms']) && $opcodes[$firstOp['jmpfroms'][0]]['opcode'] == XC_JMP
&& $lastOp['opcode'] == XC_JMP && !empty($lastOp['jmptos']) && $lastOp['jmptos'][0] <= $firstOp['jmpfroms'][0]
&& !empty($opcodes[$range[1] + 1]['jmpfroms']) && $opcodes[$opcodes[$range[1] + 1]['jmpfroms'][0]]['opcode'] == XC_JMPZNZ
) {