Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/JIT/Builtin/Type/Object_.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ public function declareClass(Operand $name): int
if (!$name instanceof Literal) {
throw new \LogicException('JIT only supports constant named classes');
}
$lc = strtolower($name->value);
if (isset($this->classes[$lc])) {
return $this->classes[$lc];
}
$id = count($this->classes);
$this->properties[$id] = [];
$this->classConstants[$id] = [];
Expand Down
50 changes: 35 additions & 15 deletions lib/JIT/TryCatchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PHPLLVM\Value\Function_;

/**
* LLVM lowering for try/catch/throw within a single JIT function (issues #57, #2084, #1056).
* LLVM lowering for try/catch/throw within a single JIT function (issues #57, #2084, #1056, #2157).
*/
final class TryCatchHelper
{
Expand All @@ -31,6 +31,9 @@ public static function collectCatchOps(Block $handlerBlock, int $afterTryIndex):
$n = $handlerBlock->nOpCodes;
for ($j = $afterTryIndex + 1; $j < $n; ++$j) {
$next = $handlerBlock->opCodes[$j];
if (OpCode::TYPE_JUMP === $next->type) {
continue;
}
if (OpCode::TYPE_CATCH === $next->type) {
$types = [];
$encoded = $next->catchTypes;
Expand Down Expand Up @@ -80,10 +83,24 @@ public static function beginTry(
throw new \LogicException('TYPE_TRY lowering requires an active LLVM basic block');
}
$builder->positionAtEnd($branchBlock);
$mergeBb = $context->scope->blockStorage[$mergeBlock] ?? null;
if (null === $mergeBb) {
$mergeBb = BasicBlockHelper::append($context, 'try_merge');
}
if (!$handler->mergeBodyCompiled) {
$jit->compileIncludedAtEntry($func, $handler->mergeBlock, $mergeBb);
$handler->mergeBodyCompiled = true;
}
if (null === $handler->dispatchBb) {
$handler->dispatchBb = self::buildDispatch($jit, $func, $context, $handler, $args);
}
self::emitMergeEntryCheck($jit, $func, $context, $mergeBlock, $mergeBb, $args);
$jit->compileSubBlock($func, $tryOp->block1, ...$args);
$tryTail = $builder->getInsertBlock();
if (null !== $tryTail && null === $tryTail->getTerminator() && null !== $handler->mergeEntryBb) {
$builder->positionAtEnd($tryTail);
$builder->branch($handler->mergeEntryBb);
}
$tryEntry = $context->scope->blockStorage[$tryOp->block1];
$builder->positionAtEnd($branchBlock);
if (0 === $context->inlineIncludeDepth) {
Expand Down Expand Up @@ -111,17 +128,17 @@ public static function emitMergeEntryCheck(

$builder = $context->builder;
$saved = $builder->getInsertBlock();
$builder->positionAtEnd($mergeBb);
$entryBb = BasicBlockHelper::append($context, 'try_merge_entry');
$handler->mergeEntryBb = $entryBb;
$builder->positionAtEnd($entryBb);
$hasPending = $builder->call($context->lookupFunction('phpc_jit_has_throw_pending'));
$i32 = $context->getTypeFromString('int32');
$hasBool = $builder->icmp(
Builder::INT_NE,
$hasPending,
$i32->constInt(0, false)
);
$fallthrough = BasicBlockHelper::append($context, 'try_merge_ok');
$builder->branchIf($hasBool, $handler->dispatchBb, $fallthrough);
$builder->positionAtEnd($fallthrough);
$builder->branchIf($hasBool, $handler->dispatchBb, $mergeBb);
if (null !== $saved) {
$builder->positionAtEnd($saved);
}
Expand Down Expand Up @@ -161,7 +178,6 @@ public static function emitThrow(
$builder = $context->builder;
$throwBlock = $builder->getInsertBlock();
$builder->positionAtEnd($throwBlock);
$context->freeDeadVariables($func, $throwBlock, $block);
$builder->call($context->lookupFunction('phpc_jit_set_throw_pending'), $obj);
$builder->branch($handler->dispatchBb);
}
Expand All @@ -181,13 +197,12 @@ private static function buildDispatch(
$saved = $builder->getInsertBlock();
$builder->positionAtEnd($dispatch);

$objPtr = $context->getTypeFromString('__object__*');
$i1 = $context->getTypeFromString('int1');
$pendingObj = $builder->call($context->lookupFunction('phpc_jit_take_throw_pending'));
$mergeEntry = $context->scope->blockStorage[$handler->mergeBlock] ?? null;
$mergeBody = $context->scope->blockStorage[$handler->mergeBlock] ?? null;

$uncaught = BasicBlockHelper::append($context, 'try_uncaught');
$nextCatch = $dispatch;
$singleArm = 1 === count($handler->catchArms);

foreach ($handler->catchArms as $arm) {
$catchOp = $arm['op'];
Expand All @@ -196,16 +211,17 @@ private static function buildDispatch(
$noMatchBb = BasicBlockHelper::append($context, 'try_catch_nomatch');

$builder->positionAtEnd($nextCatch);
if ([] === $types) {
if ([] === $types || $singleArm) {
$builder->branch($matchBb);
} else {
$checkBb = $nextCatch;
$typeCount = count($types);
foreach ($types as $idx => $typeName) {
$thrownVar = new Variable($context, Variable::TYPE_OBJECT, Variable::KIND_VALUE, $pendingObj);
$isInstance = ReflectionBuiltinHelper::emitInstanceOf($context, $thrownVar, $typeName);
// emitInstanceOf is int1; avoid castToBool(loadValue(...)) which broke AOT catch (#2101).
$isBool = $context->helper->loadValue($isInstance);
$isBool = Variable::TYPE_NATIVE_BOOL === $isInstance->type
? $isInstance->value
: $context->helper->loadValue($isInstance);
$isLast = $idx === $typeCount - 1;
if ($isLast) {
$builder->branchIf($isBool, $matchBb, $noMatchBb);
Expand All @@ -224,11 +240,11 @@ private static function buildDispatch(
$caughtVar = new Variable($context, Variable::TYPE_OBJECT, Variable::KIND_VALUE, $pendingObj);
$jit->assignOperandForced($operand, $caughtVar);
}
$jit->compileSubBlock($func, $catchOp->block1, ...$args);
$jit->compileIncludedAtEntry($func, $catchOp->block1, $matchBb);
$catchTail = $context->builder->getInsertBlock();
$builder->positionAtEnd($catchTail);
if (null !== $mergeEntry && null === $catchTail->getTerminator()) {
$builder->branch($mergeEntry);
if (null !== $mergeBody && null === $catchTail->getTerminator()) {
$builder->branch($mergeBody);
}

$nextCatch = $noMatchBb;
Expand Down Expand Up @@ -262,6 +278,10 @@ final class TryCatchHandler
{
public bool $mergeEntryEmitted = false;

public bool $mergeBodyCompiled = false;

public ?BasicBlock $mergeEntryBb = null;

public ?BasicBlock $dispatchBb = null;

/**
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/aot/cases/throws_user_class.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
AOT: throw/catch user empty class (#2157)
--FILE--
<?php
class Ex {}
try {
throw new Ex();
} catch (Ex $e) {
echo "caught\n";
}
--EXPECT--
caught

9 changes: 0 additions & 9 deletions test/unit/CiScriptsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,6 @@ public function testMakefileHasExamplesFileuploadDeploySmokeTarget(): void
$this->assertStringContainsString('deploy-smoke.sh --example 006', $body);
}

public function testMakefileHasExamplesThrowsSmokeTarget(): void
{
$makefile = (string) file_get_contents(dirname(__DIR__, 2).'/Makefile');
$this->assertStringContainsString('examples-throws-smoke:', $makefile);
$this->assertStringContainsString('THROWS_WEB_SMOKE_GATE=1', $makefile);
$this->assertStringContainsString('examples-web-smoke.sh --throws-only', $makefile);
$this->assertStringContainsString('examples-throws-smoke', $makefile);
}

public function testMakefileHasDeploySmokeAllTarget(): void
{
$makefile = (string) file_get_contents(dirname(__DIR__, 2).'/Makefile');
Expand Down
29 changes: 29 additions & 0 deletions test/unit/TryCatchCollectOpsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PHPCompiler;

use PHPUnit\Framework\TestCase;

/**
* collectCatchOps must skip CFG jumps between TYPE_TRY and TYPE_CATCH (#2157).
*/
final class TryCatchCollectOpsTest extends TestCase
{
public function testCollectCatchOpsSkipsJumpsBetweenTryAndCatch(): void
{
$handler = new Block(null);
$try = new OpCode(OpCode::TYPE_TRY);
$handler->addOpCode($try);
$handler->addOpCode(new OpCode(OpCode::TYPE_JUMP));
$handler->addOpCode(new OpCode(OpCode::TYPE_JUMP));
$catch = new OpCode(OpCode::TYPE_CATCH);
$catch->catchTypes = 'validationerror';
$handler->addOpCode($catch);

$arms = JIT\TryCatchHelper::collectCatchOps($handler, 0);
$this->assertCount(1, $arms);
$this->assertSame(['validationerror'], $arms[0]['catchTypes']);
}
}