Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122257 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 1811 invoked from network); 25 Jan 2024 17:37:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Jan 2024 17:37:42 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1706204307; bh=qRw8teux2oNUFkctbccQA7hRNKzmLRwJgzKwKY8/Fb8=; h=In-Reply-To:References:Date:From:To:Subject:From; b=b7KbQ568w4as4Mv3xHr9pwGnrm6bi1+0XquPxMAt3kUPTIgJNtNyCaVaLh+8LMvu5 sHxYHMgi/GdiN9N+LqnviplsCCh6J9pJXP1N3uu7z3YN9fXUi97RbB3eXrfooJ8ZTV opiQu9e3c9PNwGyEb1nDtyGDu+m1K/OCVqts+S8Tn59POyePE3MflM29Zfk4QSRkyO pg5FzhDfrT20W7zoevnvkHaC05DGInMTcHeVzi6kvxNilnkwhRY89NjHOgAGyAwAeV yJ/7tieQ725Dl9o11uL9Q8IRSfEvIJEN7ii5pdNf+C4RJQI+e+A669bl15QlaixwtW 8m8FKp7E63hXQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0C12E180048 for ; Thu, 25 Jan 2024 09:38:26 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 25 Jan 2024 09:38:25 -0800 (PST) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 3C2535C010B for ; Thu, 25 Jan 2024 12:37:39 -0500 (EST) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Thu, 25 Jan 2024 12:37:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm3; t=1706204259; x= 1706290659; bh=LFgvj+lmiCzhooOxxArgYs+fCivf/3GAeoYcnXbyc9k=; b=m hwUCqqSCazd6VzTEiSmq4HYzlIL2tjvaB9obxHOEDtk62mdfNHzy5r+zMNgNXaoY r08OBcLJ00V66rgLKg3WdpNJlIl3mBSCg2023i37/blnzfP8Wb8/GlhE2EuseCHG FLKcSUhQB918j+bGb05/3woHVsotICBDMRjB3a5mmCZnF7oGKybDLwUykGSKGeUw zzv9t7A1H54kNYMvCwMT/1WiJG2I/+6YzUIX+lfBjf7ezpW5h8hdiPBiHspxql5F Wzx4N8OMGYJcej6kr1Jo1SO+ve8yKQTdf9FMJLQ3OqXbx/Si8Ph1uLSRLK8OH6PY SAn2A9P9DIPFdinQY2SRA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1706204259; x=1706290659; bh=LFgvj+lmiCzhooOxxArgYs+fCivf /3GAeoYcnXbyc9k=; b=tum94cbC/dmSitFS1s9oW5dEeW7BBekArgEp2nk14J0p rxhY+Ske7BdKYO4FS1q5oFVTk6Ox66kpHrSFY7/HYqOz2MPGuj+7/vMU/Er43/fZ C8/K/Z298HkHImqcV4cT18ILa/vIpcVBDulHH5+G6IhkY9rSVaqEMje0BgEaqvlS U7xZYvn9PKvsMm4Hyy3y/qUwJG8Qy3h8EOcn/wJrrmXJsT50+R7HDmqCS2APtOqN 1963mF5NeXopGuJjHR8NbdkN/ocpmIXHSWNFkZ6ef9sEw8LsFcaK715hMqsPCyTx 3E00kBo1RancbTdLDS66W4RZCEkYBeijuV13IAm67A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdelhedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeefteevffetteeuueekgeejffduueehtdfhveefheei hfehgeehffehtdehgfeljeenucffohhmrghinhepphhhphdrnhgvthdpghhithhhuhgsrd gtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhep lhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id CC6AD1700093; Thu, 25 Jan 2024 12:37:38 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-119-ga8b98d1bd8-fm-20240108.001-ga8b98d1b MIME-Version: 1.0 Message-ID: <1a292ec3-3033-4579-bc36-5aba35600c8f@app.fastmail.com> In-Reply-To: References: Date: Thu, 25 Jan 2024 17:37:10 +0000 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Discussion: making continue and break into an expression From: larry@garfieldtech.com ("Larry Garfield") On Thu, Jan 25, 2024, at 11:28 AM, Ilija Tovilo wrote: > This leads to very similar issues as break/continue inside blocks. See: > https://wiki.php.net/rfc/match_blocks#technical_implications_of_control_statements > > I'll try to explain. > > The VM works with temporary variables. For the expression foo() + > bar() two temporary variables for the result of foo() and bar() will > be created, which are then used for the + operation. Normally, + will > consume both operands, i.e. use and then free them. However, with > break/continue etc. being expressions, it would become possible to > skip over the consuming instructions. > > do { > echo foo() + break; > } while (true); > echo 'Done'; > > Pseudo opcodes: > > 0000 V1 = CALL foo > 0001 JMP 0005 > 0002 V2 = ADD V1 false ; false is here represents a bottom value > that will never actually be used > 0003 ECHO V2 > 0004 JMP 0000 > 0005 ECHO 'Done' > > Since JMP will skip over the ADD instruction, V1 remains unused. A > similar problem already exists for break/continue in foreach itself. > > foreach ($foos as $foo) { > foreach ($bars as $bar) { > break 2; > } > } > > foreach holds a copy of $bars (in case it gets modified) that normally > gets cleaned up when the loop ends. With break over multiple > loop-boundaries, we can completely skip over this freeing mechanism. > PHP solves this by inserting an explicit FE_FREE instruction before > the break 2, which itself is essentially just a JMP to the end of the > outer loop. > > Hopefully it's now more evident why this is a problem: > > while (true) { > foo() && break; > } > > foo() returns a value that would normally be consumed by the && > operation. However, with break, we may skip over the && operation > entirely. As such, the break itself becomes responsible for freeing > these values. This requires significant changes in the compiler to > track variables that are currently "live" (i.e. haven't been consumed > yet), and emitting FREE opcodes for them as needed. I've implemented > this for match blocks here: > > https://github.com/php/php-src/compare/master...iluuu1994:php-src:match-blocks-var-tracking > > However, note that due to complexity, I've decided to disallow using > break/continue and the likes in such contexts to avoid this issue > completely, which isn't possible for what you are suggesting. > > There's another related issue. > > foo(bar(), break); > > Function calls in PHP consist of multiple instructions, namely an > INIT_CALL, 0-n SEND and a DO_CALL opcode. INIT_CALL creates a stack > frame, SEND pushes arguments onto the stack frame, and DO_CALL starts > the execution of the function and frees both arguments and stack frame > when the function ends. If prior to a SEND opcode we break, we skip > over the DO_CALL, so the stack frame needs to be freed manually. > > The patch linked above solves this by inserting CLEAN_UNFINISHED_CALLS > opcodes that do as the name suggests. This mechanism is already used > for exceptions. This should work for you, but was insufficient for > match blocks, for reasons I won't get into here. > > All this to say: Don't expect the implementation here to be trivial. > > Regards, > Ilija I'm curious, how did `throw` expressions manage to avoid these issues? Or was it just "Ilija did the hard work of tracking down the weirdness?" --Larry Garfield