[Ada] Refactor scan_backend_switch to share logic across backends
authorGhjuvan Lacambre <lacambre@adacore.com>
Mon, 21 Jun 2021 09:47:59 +0000 (11:47 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Mon, 20 Sep 2021 12:31:29 +0000 (12:31 +0000)
gcc/ada/

* adabkend.adb (Scan_Back_End_Switches): Replace switch-scanning
logic with call to Backend_Utils.Scan_Common_Back_End_Switches.
* back_end.adb (Scan_Back_End_Switches): Replace switch-scanning
logic with call to Backend_Utils.Scan_Common_Back_End_Switches.
* backend_utils.adb: New file.
* backend_utils.ads: New file.
* gcc-interface/Make-lang.in: Add ada/backend_utils.o.

gcc/ada/adabkend.adb
gcc/ada/back_end.adb
gcc/ada/backend_utils.adb [new file with mode: 0644]
gcc/ada/backend_utils.ads [new file with mode: 0644]
gcc/ada/gcc-interface/Make-lang.in

index 2ad58ef..118ca95 100644 (file)
 
 --  This is the version of the Back_End package for back ends written in Ada
 
-with Atree;    use Atree;
+with Atree;         use Atree;
+with Backend_Utils; use Backend_Utils;
 with Debug;
 with Lib;
-with Opt;      use Opt;
-with Output;   use Output;
-with Osint;    use Osint;
-with Osint.C;  use Osint.C;
-with Switch.C; use Switch.C;
-with Types;    use Types;
+with Opt;           use Opt;
+with Output;        use Output;
+with Osint;         use Osint;
+with Osint.C;       use Osint.C;
+with Switch.C;      use Switch.C;
+with Types;         use Types;
 
 with System.OS_Lib; use System.OS_Lib;
 
@@ -182,48 +183,11 @@ package body Adabkend is
 
             return;
 
-         --  Special check, the back-end switch -fno-inline also sets the
-         --  front end flags to entirely inhibit all inlining. So we store it
-         --  and set the appropriate flags.
-
-         elsif Switch_Chars (First .. Last) = "fno-inline" then
-            Lib.Store_Compilation_Switch (Switch_Chars);
-            Opt.Disable_FE_Inline := True;
-            return;
-
-         --  Similar processing for -fpreserve-control-flow
-
-         elsif Switch_Chars (First .. Last) = "fpreserve-control-flow" then
-            Lib.Store_Compilation_Switch (Switch_Chars);
-            Opt.Suppress_Control_Flow_Optimizations := True;
-            return;
-
-         --  Recognize -gxxx switches
-
-         elsif Switch_Chars (First) = 'g' then
-            Debugger_Level := 2;
-
-            if First < Last then
-               case Switch_Chars (First + 1) is
-                  when '0' =>
-                     Debugger_Level := 0;
-                  when '1' =>
-                     Debugger_Level := 1;
-                  when '2' =>
-                     Debugger_Level := 2;
-                  when '3' =>
-                     Debugger_Level := 3;
-                  when others =>
-                     null;
-               end case;
-            end if;
-
-         elsif Switch_Chars (First .. Last) = "S" then
-            Generate_Asm := True;
-
          --  Ignore all other back-end switches
 
-         elsif Is_Back_End_Switch (Switch_Chars) then
+         elsif Scan_Common_Back_End_Switch (Switch_Chars)
+            or else Is_Back_End_Switch (Switch_Chars)
+         then
             null;
 
          --  Give error for junk switch
index 42d837d..abbd5ed 100644 (file)
 
 --  This is the version of the Back_End package for GCC back ends
 
-with Atree;    use Atree;
-with Debug;    use Debug;
-with Elists;   use Elists;
-with Errout;   use Errout;
-with Lib;      use Lib;
-with Osint;    use Osint;
-with Opt;      use Opt;
-with Osint.C;  use Osint.C;
-with Namet;    use Namet;
-with Nlists;   use Nlists;
-with Stand;    use Stand;
-with Sinput;   use Sinput;
-with Stringt;  use Stringt;
-with Switch;   use Switch;
-with Switch.C; use Switch.C;
-with System;   use System;
-with Types;    use Types;
+with Atree;         use Atree;
+with Backend_Utils; use Backend_Utils;
+with Debug;         use Debug;
+with Elists;        use Elists;
+with Errout;        use Errout;
+with Lib;           use Lib;
+with Osint;         use Osint;
+with Opt;           use Opt;
+with Osint.C;       use Osint.C;
+with Namet;         use Namet;
+with Nlists;        use Nlists;
+with Stand;         use Stand;
+with Sinput;        use Sinput;
+with Stringt;       use Stringt;
+with Switch;        use Switch;
+with Switch.C;      use Switch.C;
+with System;        use System;
+with Types;         use Types;
 
 with System.OS_Lib; use System.OS_Lib;
 
@@ -266,52 +267,20 @@ package body Back_End is
          --  specific switches that the Ada front-end knows about.
 
          else
-            Store_Compilation_Switch (Switch_Chars);
-
-            --  For gcc back ends, -fno-inline disables Inline pragmas only,
-            --  not Inline_Always to remain consistent with the always_inline
-            --  attribute behavior.
-
-            if Switch_Chars (First .. Last) = "fno-inline" then
-               Opt.Disable_FE_Inline := True;
-
-            --  Back end switch -fpreserve-control-flow also sets the front end
-            --  flag that inhibits improper control flow transformations.
-
-            elsif Switch_Chars (First .. Last) = "fpreserve-control-flow" then
-               Opt.Suppress_Control_Flow_Optimizations := True;
-
-            --  Back end switch -fdiagnostics-format=json tells the frontend to
-            --  output its error and warning messages in the same format GCC
-            --  uses when passed -fdiagnostics-format=json.
-
-            elsif Switch_Chars (First .. Last) = "fdiagnostics-format=json"
-            then
-               Opt.JSON_Output := True;
-
-            --  Back end switch -fdump-scos, which exists primarily for C, is
-            --  also accepted for Ada as a synonym of -gnateS.
-
-            elsif Switch_Chars (First .. Last) = "fdump-scos" then
-               Opt.Generate_SCO := True;
-               Opt.Generate_SCO_Instance_Table := True;
-
-            elsif Switch_Chars (First) = 'g' then
-               Debugger_Level := 2;
-
-               if First < Last then
-                  case Switch_Chars (First + 1) is
-                     when '0' =>
-                        Debugger_Level := 0;
-                     when '1' =>
-                        Debugger_Level := 1;
-                     when '2' =>
-                        Debugger_Level := 2;
-                     when '3' =>
-                        Debugger_Level := 3;
-                     when others =>
-                        null;
-                  end case;
+
+            if not Scan_Common_Back_End_Switch (Switch_Chars) then
+
+               --  Store compilation switch, as Scan_Common_Back_End_Switch
+               --  only stores switches it recognizes.
+
+               Store_Compilation_Switch (Switch_Chars);
+
+               --  Back end switch -fdump-scos, which exists primarily for C,
+               --  is also accepted for Ada as a synonym of -gnateS.
+
+               if Switch_Chars (First .. Last) = "fdump-scos" then
+                  Opt.Generate_SCO := True;
+                  Opt.Generate_SCO_Instance_Table := True;
                end if;
             end if;
          end if;
diff --git a/gcc/ada/backend_utils.adb b/gcc/ada/backend_utils.adb
new file mode 100644 (file)
index 0000000..6f492fd
--- /dev/null
@@ -0,0 +1,96 @@
+------------------------------------------------------------------------------
+--                                                                          --
+--                         GNAT COMPILER COMPONENTS                         --
+--                                                                          --
+--                         B A C K E N D _ U T I L S                        --
+--                                                                          --
+--                                 B o d y                                  --
+--                                                                          --
+--          Copyright (C) 2021-2021, Free Software Foundation, Inc.         --
+--                                                                          --
+-- GNAT is free software;  you can  redistribute it  and/or modify it under --
+-- terms of the  GNU General Public License as published  by the Free Soft- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
+-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
+-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
+-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
+-- for  more details.  You should have  received  a copy of the GNU General --
+-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
+-- http://www.gnu.org/licenses for a complete copy of the license.          --
+--                                                                          --
+-- GNAT was originally developed  by the GNAT team at  New York University. --
+-- Extensive contributions were provided by Ada Core Technologies Inc.      --
+--                                                                          --
+------------------------------------------------------------------------------
+
+with Lib;
+with Opt;    use Opt;
+with Switch; use Switch;
+
+package body Backend_Utils is
+
+   ---------------------------------
+   -- Scan_Common_Back_End_Switch --
+   ---------------------------------
+
+   function Scan_Common_Back_End_Switch (Switch_Chars : String) return Boolean
+   is
+      First : constant Positive := Switch_Chars'First + 1;
+      Last  : constant Natural  := Switch_Last (Switch_Chars);
+   begin
+
+      --  Recognize -gxxx switches
+
+      if Switch_Chars (First) = 'g' then
+         Debugger_Level := 2;
+
+         if First < Last then
+            case Switch_Chars (First + 1) is
+               when '0' =>
+                  Debugger_Level := 0;
+               when '1' =>
+                  Debugger_Level := 1;
+               when '2' =>
+                  Debugger_Level := 2;
+               when '3' =>
+                  Debugger_Level := 3;
+               when others =>
+                  null;
+            end case;
+         end if;
+
+      --  Back end switch -fdiagnostics-format=json tells the frontend to
+      --  output its error and warning messages in the same format GCC
+      --  uses when passed -fdiagnostics-format=json.
+
+      elsif Switch_Chars (First .. Last) = "fdiagnostics-format=json" then
+         Opt.JSON_Output := True;
+
+      --  Back-end switch -fno-inline also sets the front end flags to entirely
+      --  inhibit all inlining. So we store it and set the appropriate
+      --  flags.
+      --  For gcc back ends, -fno-inline disables Inline pragmas only,
+      --  not Inline_Always to remain consistent with the always_inline
+      --  attribute behavior.
+
+      elsif Switch_Chars (First .. Last) = "fno-inline" then
+         Opt.Disable_FE_Inline := True;
+
+      --  Back end switch -fpreserve-control-flow also sets the front end
+      --  flag that inhibits improper control flow transformations.
+
+      elsif Switch_Chars (First .. Last) = "fpreserve-control-flow" then
+         Opt.Suppress_Control_Flow_Optimizations := True;
+
+      elsif Switch_Chars (First .. Last) = "S" then
+         Generate_Asm := True;
+
+      else
+         return False;
+      end if;
+
+      Lib.Store_Compilation_Switch (Switch_Chars);
+      return True;
+   end Scan_Common_Back_End_Switch;
+
+end Backend_Utils;
diff --git a/gcc/ada/backend_utils.ads b/gcc/ada/backend_utils.ads
new file mode 100644 (file)
index 0000000..71321ef
--- /dev/null
@@ -0,0 +1,36 @@
+------------------------------------------------------------------------------
+--                                                                          --
+--                         GNAT COMPILER COMPONENTS                         --
+--                                                                          --
+--                         B A C K E N D _ U T I L S                        --
+--                                                                          --
+--                                 S p e c                                  --
+--                                                                          --
+--          Copyright (C) 2021-2021, Free Software Foundation, Inc.         --
+--                                                                          --
+-- GNAT is free software;  you can  redistribute it  and/or modify it under --
+-- terms of the  GNU General Public License as published  by the Free Soft- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
+-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
+-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
+-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
+-- for  more details.  You should have  received  a copy of the GNU General --
+-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
+-- http://www.gnu.org/licenses for a complete copy of the license.          --
+--                                                                          --
+-- GNAT was originally developed  by the GNAT team at  New York University. --
+-- Extensive contributions were provided by Ada Core Technologies Inc.      --
+--                                                                          --
+------------------------------------------------------------------------------
+
+--  Procedures and functions used by both the Adabkend and Back_End packages.
+
+package Backend_Utils is
+
+   function Scan_Common_Back_End_Switch (Switch_Chars : String) return Boolean;
+   --  Scan back-end switches which are common to all back-ends and have an
+   --  effect in the front-end. Call Store_Compilation_Switch and return True
+   --  if Switch_Chars is recognized as a common back end switch. Return False
+   --  otherwise.
+
+end Backend_Utils;
index 765654f..c341e2d 100644 (file)
@@ -298,6 +298,7 @@ GNAT_ADA_OBJS =     \
  ada/alloc.o   \
  ada/aspects.o  \
  ada/atree.o   \
+ ada/backend_utils.o   \
  ada/butil.o   \
  ada/casing.o  \
  ada/checks.o  \