protected function compileExpr(Op\Expr $op, $indent)
{
$phi = '';
$result = '';
switch ($op->getType()) {
case 'Expr_ArrayDimFetch':
$result = $this->getVarName($op->var) . "[" . $this->getVarName($op->dim) . "]";
break;
case 'Expr_Assign':
$result = $this->getVarName($op->var) . ' = ' . $this->getVarName($op->expr);
foreach ($op->var->usages as $usage) {
if ($usage instanceof Op\Phi) {
$phi .= $indent . $this->getVarName($usage->result) . " = " . $this->getVarName($op->var) . ";\n";
}
}
break;
case 'Expr_BinaryOp_BitwiseAnd':
$result = $this->getVarName($op->left) . " & " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_BitwiseOr':
$result = $this->getVarName($op->left) . " | " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_BitwiseXor':
$result = $this->getVarName($op->left) . " ^ " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Coalesce':
$result = $this->getVarName($op->left) . " ?? " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Concat':
$result = $this->getVarName($op->left) . " . " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Div':
$result = $this->getVarName($op->left) . " / " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Equal':
$result = $this->getVarName($op->left) . " == " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Greater':
$result = $this->getVarName($op->left) . " > " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_GreaterOrEqual':
$result = $this->getVarName($op->left) . " >= " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Identical':
$result = $this->getVarName($op->left) . " === " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_LogicalXor':
$result = $this->getVarName($op->left) . " XOR " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Minus':
$result = $this->getVarName($op->left) . " - " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Mod':
$result = $this->getVarName($op->left) . " % " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Mul':
$result = $this->getVarName($op->left) . " * " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_NotEqual':
$result = $this->getVarName($op->left) . " != " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_NotIdentical':
$result = $this->getVarName($op->left) . " !== " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Plus':
$result = $this->getVarName($op->left) . " + " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Pow':
$result = $this->getVarName($op->left) . " ** " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_ShiftLeft':
$result = $this->getVarName($op->left) . " << " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_ShiftRight':
$result = $this->getVarName($op->left) . " >> " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Smaller':
$result = $this->getVarName($op->left) . " < " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_SmallerOrEqual':
$result = $this->getVarName($op->left) . " <= " . $this->getVarName($op->right);
break;
case 'Expr_BinaryOp_Spaceship':
$result = $this->getVarName($op->left) . " <=> " . $this->getVarName($op->right);
break;
case 'Expr_Cast_Array':
$result = "(array) " . $this->getVarName($op->expr);
break;
case 'Expr_Cast_Bool':
$result = "(bool) " . $this->getVarName($op->expr);
break;
case 'Expr_Cast_Double':
$result = "(float) " . $this->getVarName($op->expr);
break;
case 'Expr_Cast_Int':
$result = "(int) " . $this->getVarName($op->expr);
break;
case 'Expr_Cast_Object':
$result = "(object) " . $this->getVarName($op->expr);
break;
case 'Expr_Cast_String':
$result = "(string) " . $this->getVarName($op->expr);
break;
case 'Expr_Cast_Unset':
$result = "(unset) " . $this->getVarName($op->expr);
break;
default:
throw new \RuntimeException("Unknown expression found: " . $op->getType());
}
foreach ($op->result->usages as $usage) {
if ($usage instanceof Op\Phi) {
$phi .= $indent . $this->getVarName($usage->result) . " = " . $this->getVarName($op->result) . ";\n";
}
}
if (count($op->result->usages) === 0) {
return $indent . $result . ";\n" . $phi;
}
return $indent . $this->getVarName($op->result) . " = " . $result . ";\n" . $phi;
}