From patchwork Thu Oct 10 15:29:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1174655 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-510642-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="rGO41sTT"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46pw7f1749z9sCJ for ; Fri, 11 Oct 2019 02:32:29 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=n7pE0Kc0aDnGwj8AjtUjgTeqDCdo9iLudK6Ri2OJEGWndH78TV IqJW61PGCZPQpmjMV4xXb86DyjPIzNAaPLeRT6Y+XXDmawI95rChP1THAojTRZvX Kd0SWsmOoUfNr6xYZva5WulKDY4suL5sN3XMUfKN6sgazLwASlYSAF9DA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=pIsY6qH4SW3XQMOJjEYLrp1sj7k=; b=rGO41sTTzfrP6K1LBRXs 42MnmhkyefS1TVOsB5usU/OZhfJ/aLes2bxSgInQ28sSy50T3/5ewcqRbW6Gq/+q xunRPl921Ghs5Nnv+g+W/FOYrR4z0JdU976PmUQY0LE1fASVhjqPLyCYbEuOMndq K93dkZ/xvTS6p3t9QaP3Ub0= Received: (qmail 43646 invoked by alias); 10 Oct 2019 15:30:00 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 43196 invoked by uid 89); 10 Oct 2019 15:29:56 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_NEUTRAL autolearn=ham version=3.3.1 spammy=exp_ch7.adb, UD:exp_ch7.adb, 42306, exp_ch7adb X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (209.51.188.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 10 Oct 2019 15:29:54 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iIaNz-0005uR-8Q for gcc-patches@gcc.gnu.org; Thu, 10 Oct 2019 11:29:52 -0400 Received: from rock.gnat.com ([205.232.38.15]:35541) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iIaNy-0005tq-W0 for gcc-patches@gcc.gnu.org; Thu, 10 Oct 2019 11:29:51 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id BADC95609C; Thu, 10 Oct 2019 11:29:47 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id RrijrJ7VPUb6; Thu, 10 Oct 2019 11:29:47 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id 821EF560A1; Thu, 10 Oct 2019 11:29:47 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id 812E8646; Thu, 10 Oct 2019 11:29:47 -0400 (EDT) Date: Thu, 10 Oct 2019 11:29:47 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Gary Dismukes Subject: [Ada] Handling up-level references in loops within library-level declarations Message-ID: <20191010152947.GA87708@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 205.232.38.15 X-IsSubscribed: yes For GNAT-LLVM, we now wrap top-level loop statements in library package declaration lists within a procedure when there are nested subprograms within the loop that might make up-level references to entities of the loop (or to entities of loops and blocks nested within the outer loop for that matter). Tested on x86_64-pc-linux-gnu, committed on trunk 2019-10-10 Gary Dismukes gcc/ada/ * exp_ch7.adb (Check_Unnesting_In_Decls_Or_Stmts): When encountering a loop at the top level of a package declaration list (that is, within the declarations of a package spec or body) that has nested subprograms, call Unnest_Loop to create a new library-level procedure that will contain the loop, to allow for proper handling of up-level references from within nested subprograms, such as to loop parameters. (Unnest_Loop): New procedure that takes a loop statement and creates a new procedure body to enclose the loop statement, along with generating a call to the procedure. --- gcc/ada/exp_ch7.adb +++ gcc/ada/exp_ch7.adb @@ -398,6 +398,14 @@ package body Exp_Ch7 is -- actions or secondary-stack management, in which case the nested -- subprogram is a finalizer. + procedure Unnest_Loop (Loop_Stmt : Node_Id); + -- Top-level Loops that contain nested subprograms with up-level references + -- need to have activation records. We do this by rewriting the loop as a + -- procedure containing the loop, followed by a call to the procedure in + -- the same library-level declarative list, to replicate the semantics of + -- the original loop. Such loops can occur due to aggregate expansions and + -- other constructs. + procedure Check_Visibly_Controlled (Prim : Final_Primitives; Typ : Entity_Id; @@ -4230,6 +4238,23 @@ package body Exp_Ch7 is then Unnest_Block (Decl_Or_Stmt); + elsif Nkind (Decl_Or_Stmt) = N_Loop_Statement then + declare + Id : constant Entity_Id := + Entity (Identifier (Decl_Or_Stmt)); + + begin + -- When a top-level loop within declarations of a library + -- package spec or body contains nested subprograms, we wrap + -- it in a procedure to handle possible up-level references + -- to entities associated with the loop (such as loop + -- parameters). + + if Present (Id) and then Contains_Subprogram (Id) then + Unnest_Loop (Decl_Or_Stmt); + end if; + end; + elsif Nkind (Decl_Or_Stmt) = N_Package_Declaration and then not Modify_Tree_For_C then @@ -9256,6 +9281,67 @@ package body Exp_Ch7 is end loop; end Unnest_Block; + ----------------- + -- Unnest_Loop -- + ----------------- + + procedure Unnest_Loop (Loop_Stmt : Node_Id) is + Loc : constant Source_Ptr := Sloc (Loop_Stmt); + Ent : Entity_Id; + Local_Body : Node_Id; + Local_Call : Node_Id; + Local_Proc : Entity_Id; + Local_Scop : Entity_Id; + Loop_Copy : constant Node_Id := + Relocate_Node (Loop_Stmt); + begin + Local_Scop := Entity (Identifier (Loop_Stmt)); + Ent := First_Entity (Local_Scop); + + Local_Proc := + Make_Defining_Identifier (Loc, + Chars => New_Internal_Name ('P')); + + Local_Body := + Make_Subprogram_Body (Loc, + Specification => + Make_Procedure_Specification (Loc, + Defining_Unit_Name => Local_Proc), + Declarations => Empty_List, + Handled_Statement_Sequence => + Make_Handled_Sequence_Of_Statements (Loc, + Statements => New_List (Loop_Copy))); + + Set_First_Real_Statement + (Handled_Statement_Sequence (Local_Body), Loop_Copy); + + Rewrite (Loop_Stmt, Local_Body); + Analyze (Loop_Stmt); + + Set_Has_Nested_Subprogram (Local_Proc); + + Local_Call := + Make_Procedure_Call_Statement (Loc, + Name => New_Occurrence_Of (Local_Proc, Loc)); + + Insert_After (Loop_Stmt, Local_Call); + Analyze (Local_Call); + + -- New procedure has the same scope as the original loop, and the scope + -- of the loop is the new procedure. + + Set_Scope (Local_Proc, Scope (Local_Scop)); + Set_Scope (Local_Scop, Local_Proc); + + -- The entity list of the new procedure is that of the loop + + Set_First_Entity (Local_Proc, Ent); + + -- Note that the entities associated with the loop don't need to have + -- their Scope fields reset, since they're still associated with the + -- same loop entity that now belongs to the copied loop statement. + end Unnest_Loop; + -------------------------------- -- Wrap_Transient_Declaration -- --------------------------------