public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Secondary stack and a-tags
@ 2022-05-18  8:43 Pierre-Marie de Rodat
  0 siblings, 0 replies; only message in thread
From: Pierre-Marie de Rodat @ 2022-05-18  8:43 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 815 bytes --]

The simple use of Ada.Tags triggers a dependency on the secondary stack
mechanism, which is unwanted on small embedded targets. To avoid this
dependency, we special case a-tags.ali in ALI.Scan_ALI to not set
Sec_Stack_Used. If some other code calls one of the functions returning
a string, this code will also be marked as requiring the secondary
stack. We also remove the need to import and set __gnat_binder_ss_count
in this case by ensuring this variable defaults to 0.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* ali.adb (Scan_ALI): Special case a-tags.ali when setting
	Sec_Stack_Used.
	* bindgen.adb (Gen_Adainit): Simplify handling of secondary
	stack related code, and only import __gnat_binder_ss_count when
	needed.
	* libgnat/s-secsta.adb (Binder_SS_Count): Default initialize to
	0.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 7657 bytes --]

diff --git a/gcc/ada/ali.adb b/gcc/ada/ali.adb
--- a/gcc/ada/ali.adb
+++ b/gcc/ada/ali.adb
@@ -2079,7 +2079,15 @@ package body ALI is
                --  Processing for SS
 
                elsif C = 'S' then
-                  Opt.Sec_Stack_Used := True;
+                  --  Special case: a-tags.ali by itself should not set
+                  --  Sec_Stack_Used, only if other code uses the secondary
+                  --  stack should we set this flag. This ensures that we do
+                  --  not bring the secondary stack unnecessarily when using
+                  --  Ada.Tags and not actually using the secondary stack.
+
+                  if Get_Name_String (F) /= "a-tags.ali" then
+                     Opt.Sec_Stack_Used := True;
+                  end if;
 
                --  Invalid switch starting with S
 


diff --git a/gcc/ada/bindgen.adb b/gcc/ada/bindgen.adb
--- a/gcc/ada/bindgen.adb
+++ b/gcc/ada/bindgen.adb
@@ -80,12 +80,6 @@ package body Bindgen is
    --  domains just before calling the main procedure from the environment
    --  task.
 
-   System_Secondary_Stack_Package_In_Closure : Boolean := False;
-   --  Flag indicating whether the unit System.Secondary_Stack is in the
-   --  closure of the partition. This is set by Resolve_Binder_Options, and
-   --  is used to initialize the package in cases where the run-time brings
-   --  in package but the secondary stack is not used.
-
    System_Tasking_Restricted_Stages_Used : Boolean := False;
    --  Flag indicating whether the unit System.Tasking.Restricted.Stages is in
    --  the closure of the partition. This is set by Resolve_Binder_Options,
@@ -612,33 +606,27 @@ package body Bindgen is
                  """__gnat_initialize_stack_limit"");");
          end if;
 
-         if System_Secondary_Stack_Package_In_Closure then
-            --  System.Secondary_Stack is in the closure of the program
-            --  because the program uses the secondary stack or the restricted
-            --  run-time is unconditionally calling SS_Init. In both cases,
-            --  SS_Init needs to know the number of secondary stacks created by
-            --  the binder.
-
+         if Num_Sec_Stacks > 0 then
             WBI ("      Binder_Sec_Stacks_Count : Natural;");
             WBI ("      pragma Import (Ada, Binder_Sec_Stacks_Count, " &
                  """__gnat_binder_ss_count"");");
             WBI ("");
+         end if;
 
-            --  Import secondary stack pool variables if the secondary stack
-            --  used. They are not referenced otherwise.
+         --  Import secondary stack pool variables if the secondary stack is
+         --  used. They are not referenced otherwise.
 
-            if Sec_Stack_Used then
-               WBI ("      Default_Secondary_Stack_Size : " &
-                    "System.Parameters.Size_Type;");
-               WBI ("      pragma Import (C, Default_Secondary_Stack_Size, " &
-                    """__gnat_default_ss_size"");");
+         if Sec_Stack_Used then
+            WBI ("      Default_Secondary_Stack_Size : " &
+                 "System.Parameters.Size_Type;");
+            WBI ("      pragma Import (C, Default_Secondary_Stack_Size, " &
+                 """__gnat_default_ss_size"");");
 
-               WBI ("      Default_Sized_SS_Pool : System.Address;");
-               WBI ("      pragma Import (Ada, Default_Sized_SS_Pool, " &
-                    """__gnat_default_ss_pool"");");
+            WBI ("      Default_Sized_SS_Pool : System.Address;");
+            WBI ("      pragma Import (Ada, Default_Sized_SS_Pool, " &
+                 """__gnat_default_ss_pool"");");
 
-               WBI ("");
-            end if;
+            WBI ("");
          end if;
 
          WBI ("   begin");
@@ -686,46 +674,36 @@ package body Bindgen is
          --  Generate the default-sized secondary stack pool if the secondary
          --  stack is used by the program.
 
-         if System_Secondary_Stack_Package_In_Closure then
-            if Sec_Stack_Used then
-               --  Elaborate the body of the binder to initialize the default-
-               --  sized secondary stack pool.
-
-               WBI ("");
-               WBI ("      " & Get_Ada_Main_Name & "'Elab_Body;");
-
-               --  Generate the default-sized secondary stack pool and set the
-               --  related secondary stack globals.
+         if Sec_Stack_Used then
+            --  Elaborate the body of the binder to initialize the default-
+            --  sized secondary stack pool.
 
-               Set_String ("      Default_Secondary_Stack_Size := ");
+            WBI ("");
+            WBI ("      " & Get_Ada_Main_Name & "'Elab_Body;");
 
-               if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
-                  Set_Int (Opt.Default_Sec_Stack_Size);
-               else
-                  Set_String
-                    ("System.Parameters.Runtime_Default_Sec_Stack_Size");
-               end if;
+            --  Generate the default-sized secondary stack pool and set the
+            --  related secondary stack globals.
 
-               Set_Char (';');
-               Write_Statement_Buffer;
+            Set_String ("      Default_Secondary_Stack_Size := ");
 
-               Set_String ("      Binder_Sec_Stacks_Count := ");
-               Set_Int (Num_Sec_Stacks);
-               Set_Char (';');
-               Write_Statement_Buffer;
+            if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
+               Set_Int (Opt.Default_Sec_Stack_Size);
+            else
+               Set_String
+                 ("System.Parameters.Runtime_Default_Sec_Stack_Size");
+            end if;
 
-               WBI ("      Default_Sized_SS_Pool := " &
-                      "Sec_Default_Sized_Stacks'Address;");
-               WBI ("");
+            Set_Char (';');
+            Write_Statement_Buffer;
 
-            else
-               --  The presence of System.Secondary_Stack in the closure of the
-               --  program implies the restricted run-time is unconditionally
-               --  calling SS_Init. Let SS_Init know that no stacks were
-               --  created.
+            Set_String ("      Binder_Sec_Stacks_Count := ");
+            Set_Int (Num_Sec_Stacks);
+            Set_Char (';');
+            Write_Statement_Buffer;
 
-               WBI ("      Binder_Sec_Stacks_Count := 0;");
-            end if;
+            WBI ("      Default_Sized_SS_Pool := " &
+                   "Sec_Default_Sized_Stacks'Address;");
+            WBI ("");
          end if;
 
       --  Normal case (standard library not suppressed). Set all global values
@@ -3276,12 +3254,6 @@ package body Bindgen is
 
          Check_Package (System_Restrictions_Used, "system.restrictions%s");
 
-         --  Ditto for the use of System.Secondary_Stack
-
-         Check_Package
-           (System_Secondary_Stack_Package_In_Closure,
-            "system.secondary_stack%s");
-
          --  Ditto for use of an SMP bareboard runtime
 
          Check_Package (System_BB_CPU_Primitives_Multiprocessors_Used,


diff --git a/gcc/ada/libgnat/s-secsta.adb b/gcc/ada/libgnat/s-secsta.adb
--- a/gcc/ada/libgnat/s-secsta.adb
+++ b/gcc/ada/libgnat/s-secsta.adb
@@ -53,7 +53,7 @@ package body System.Secondary_Stack is
    --  in order to avoid depending on the binder. Their values are set by the
    --  binder.
 
-   Binder_SS_Count : Natural;
+   Binder_SS_Count : Natural := 0;
    pragma Export (Ada, Binder_SS_Count, "__gnat_binder_ss_count");
    --  The number of secondary stacks in the pool created by the binder
 



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-18  8:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-18  8:43 [Ada] Secondary stack and a-tags Pierre-Marie de Rodat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).