OSDN Git Service

2010-12-06 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-stausa.adb
index bf14beb..e85bc46 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---         Copyright (C) 2004-2009, Free Software Foundation, Inc.          --
+--         Copyright (C) 2004-2010, Free Software Foundation, Inc.          --
 --                                                                          --
 -- GNARL 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- --
@@ -173,7 +173,7 @@ package body System.Stack_Usage is
    Index_Str       : constant String  := "Index";
    Task_Name_Str   : constant String  := "Task Name";
    Stack_Size_Str  : constant String  := "Stack Size";
-   Actual_Size_Str : constant String  := "Stack usage [Value +/- Variation]";
+   Actual_Size_Str : constant String  := "Stack usage";
 
    function Get_Usage_Range (Result : Task_Result) return String;
    --  Return string representing the range of possible result of stack usage
@@ -203,10 +203,10 @@ package body System.Stack_Usage is
       Result_Array := new Result_Array_Type (1 .. Buffer_Size);
       Result_Array.all :=
         (others =>
-           (Task_Name   => (others => ASCII.NUL),
+           (Task_Name => (others => ASCII.NUL),
             Variation => 0,
-            Value => 0,
-            Max_Size    => 0));
+            Value     => 0,
+            Max_Size  => 0));
 
       --  Set the Is_Enabled flag to true, so that the task wrapper knows that
       --  it has to handle dynamic stack analysis
@@ -232,7 +232,8 @@ package body System.Stack_Usage is
                "ENVIRONMENT TASK",
                My_Stack_Size,
                My_Stack_Size,
-               System.Storage_Elements.To_Integer (Bottom_Of_Stack'Address));
+               System.Storage_Elements.To_Integer (Bottom_Of_Stack'Address),
+               0);
 
             Fill_Stack (Environment_Task_Analyzer);
 
@@ -259,56 +260,96 @@ package body System.Stack_Usage is
       Stack_Used_When_Filling : Integer;
       Current_Stack_Level     : aliased Integer;
 
+      Guard : constant Integer := 256;
+      --  Guard space between the Current_Stack_Level'Address and the last
+      --  allocated byte on the stack.
+
    begin
-      --  Readjust the pattern size. When we arrive in this function, there is
-      --  already a given amount of stack used, that we won't analyze.
+      --  Easiest and most accurate method: the top of the stack is known.
+
+      if Analyzer.Top_Pattern_Mark /= 0 then
+         Analyzer.Pattern_Size :=
+           Stack_Size (Analyzer.Top_Pattern_Mark,
+                       To_Stack_Address (Current_Stack_Level'Address))
+           - Guard;
+
+         if System.Parameters.Stack_Grows_Down then
+            Analyzer.Stack_Overlay_Address :=
+              To_Address (Analyzer.Top_Pattern_Mark);
+         else
+            Analyzer.Stack_Overlay_Address :=
+              To_Address (Analyzer.Top_Pattern_Mark
+                            - Stack_Address (Analyzer.Pattern_Size));
+         end if;
+
+         declare
+            Pattern : aliased Stack_Slots
+                        (1 .. Analyzer.Pattern_Size / Bytes_Per_Pattern);
+            for Pattern'Address use Analyzer.Stack_Overlay_Address;
+
+         begin
+            if System.Parameters.Stack_Grows_Down then
+               for J in reverse Pattern'Range loop
+                  Pattern (J) := Analyzer.Pattern;
+               end loop;
+
+               Analyzer.Bottom_Pattern_Mark :=
+                 To_Stack_Address (Pattern (Pattern'Last)'Address);
 
-      Stack_Used_When_Filling :=
-        Stack_Size
-         (Analyzer.Bottom_Of_Stack,
-          To_Stack_Address (Current_Stack_Level'Address))
-          + Natural (Current_Stack_Level'Size);
+            else
+               for J in Pattern'Range loop
+                  Pattern (J) := Analyzer.Pattern;
+               end loop;
 
-      if Stack_Used_When_Filling > Analyzer.Pattern_Size then
-         --  In this case, the known size of the stack is too small, we've
-         --  already taken more than expected, so there's no possible
-         --  computation
+               Analyzer.Bottom_Pattern_Mark :=
+                 To_Stack_Address (Pattern (Pattern'First)'Address);
+            end if;
+         end;
 
-         Analyzer.Pattern_Size := 0;
       else
-         Analyzer.Pattern_Size :=
-           Analyzer.Pattern_Size - Stack_Used_When_Filling;
-      end if;
+         --  Readjust the pattern size. When we arrive in this function, there
+         --  is already a given amount of stack used, that we won't analyze.
 
-      declare
-         Stack : aliased Stack_Slots
-                           (1 .. Analyzer.Pattern_Size / Bytes_Per_Pattern);
+         Stack_Used_When_Filling :=
+           Stack_Size (Analyzer.Bottom_Of_Stack,
+                       To_Stack_Address (Current_Stack_Level'Address));
 
-      begin
-         Stack := (others => Analyzer.Pattern);
+         if Stack_Used_When_Filling > Analyzer.Pattern_Size then
 
-         Analyzer.Stack_Overlay_Address := Stack'Address;
+            --  In this case, the known size of the stack is too small, we've
+            --  already taken more than expected, so there's no possible
+            --  computation
 
-         if Analyzer.Pattern_Size /= 0 then
-            Analyzer.Bottom_Pattern_Mark :=
-              To_Stack_Address (Stack (Bottom_Slot_Index_In (Stack))'Address);
-            Analyzer.Top_Pattern_Mark :=
-              To_Stack_Address (Stack (Top_Slot_Index_In (Stack))'Address);
+            Analyzer.Pattern_Size := 0;
          else
-            Analyzer.Bottom_Pattern_Mark := To_Stack_Address (Stack'Address);
-            Analyzer.Top_Pattern_Mark := To_Stack_Address (Stack'Address);
+            Analyzer.Pattern_Size :=
+              Analyzer.Pattern_Size - Stack_Used_When_Filling;
          end if;
 
-         --  If Arr has been packed, the following assertion must be true (we
-         --  add the size of the element whose address is:
-         --    Min (Analyzer.Inner_Pattern_Mark, Analyzer.Outer_Pattern_Mark)):
+         declare
+            Stack : aliased Stack_Slots
+              (1 .. Analyzer.Pattern_Size / Bytes_Per_Pattern);
 
-         pragma Assert
-           (Analyzer.Pattern_Size = 0 or else
-            Analyzer.Pattern_Size =
-              Stack_Size
-                (Analyzer.Top_Pattern_Mark, Analyzer.Bottom_Pattern_Mark));
-      end;
+         begin
+            Stack := (others => Analyzer.Pattern);
+
+            Analyzer.Stack_Overlay_Address := Stack'Address;
+
+            if Analyzer.Pattern_Size /= 0 then
+               Analyzer.Bottom_Pattern_Mark :=
+                 To_Stack_Address
+                   (Stack (Bottom_Slot_Index_In (Stack))'Address);
+               Analyzer.Top_Pattern_Mark :=
+                 To_Stack_Address
+                   (Stack (Top_Slot_Index_In (Stack))'Address);
+            else
+               Analyzer.Bottom_Pattern_Mark :=
+                 To_Stack_Address (Stack'Address);
+               Analyzer.Top_Pattern_Mark :=
+                 To_Stack_Address (Stack'Address);
+            end if;
+         end;
+      end if;
    end Fill_Stack;
 
    -------------------------
@@ -321,18 +362,19 @@ package body System.Stack_Usage is
       My_Stack_Size    : Natural;
       Max_Pattern_Size : Natural;
       Bottom           : Stack_Address;
+      Top              : Stack_Address;
       Pattern          : Unsigned_32 := 16#DEAD_BEEF#)
    is
    begin
       --  Initialize the analyzer fields
 
-      Analyzer.Bottom_Of_Stack := Bottom;
-      Analyzer.Stack_Size := My_Stack_Size;
-      Analyzer.Pattern_Size := Max_Pattern_Size;
-      Analyzer.Pattern := Pattern;
-      Analyzer.Result_Id := Next_Id;
-
-      Analyzer.Task_Name := (others => ' ');
+      Analyzer.Bottom_Of_Stack  := Bottom;
+      Analyzer.Stack_Size       := My_Stack_Size;
+      Analyzer.Pattern_Size     := Max_Pattern_Size;
+      Analyzer.Pattern          := Pattern;
+      Analyzer.Result_Id        := Next_Id;
+      Analyzer.Task_Name        := (others => ' ');
+      Analyzer.Top_Pattern_Mark := Top;
 
       --  Compute the task name, and truncate if bigger than Task_Name_Length
 
@@ -415,10 +457,11 @@ package body System.Stack_Usage is
 
    function Get_Usage_Range (Result : Task_Result) return String is
       Variation_Used_Str : constant String :=
-        Natural'Image (Result.Variation);
-      Value_Used_Str : constant String := Natural'Image (Result.Value);
+                             Natural'Image (Result.Variation);
+      Value_Used_Str     : constant String :=
+                             Natural'Image (Result.Value);
    begin
-      return "[" & Value_Used_Str & " +/- " & Variation_Used_Str & "]";
+      return Value_Used_Str & " +/- " & Variation_Used_Str;
    end Get_Usage_Range;
 
    ---------------------
@@ -488,8 +531,8 @@ package body System.Stack_Usage is
          for J in Result_Array'Range loop
             exit when J >= Next_Id;
 
-            if Result_Array (J).Value
-              > Result_Array (Max_Actual_Use_Result_Id).Value
+            if Result_Array (J).Value >
+               Result_Array (Max_Actual_Use_Result_Id).Value
             then
                Max_Actual_Use_Result_Id := J;
             end if;
@@ -569,15 +612,18 @@ package body System.Stack_Usage is
 
    begin
       if Analyzer.Pattern_Size = 0 then
+
          --  If we have that result, it means that we didn't do any computation
          --  at all. In other words, we used at least everything (and possibly
          --  more).
 
          Min := Analyzer.Stack_Size - Overflow_Guard;
          Max := Analyzer.Stack_Size;
+
       else
-         Min := Stack_Size
-           (Analyzer.Topmost_Touched_Mark, Analyzer.Bottom_Of_Stack);
+         Min :=
+           Stack_Size
+             (Analyzer.Topmost_Touched_Mark, Analyzer.Bottom_Of_Stack);
          Max := Min + Overflow_Guard;
       end if;
 
@@ -606,20 +652,18 @@ package body System.Stack_Usage is
             --  Take either the label size or the number image size for the
             --  size of the column "Stack Size".
 
-            if Size_Str_Len > Stack_Size_Str'Length then
-               Max_Stack_Size_Len := Size_Str_Len;
-            else
-               Max_Stack_Size_Len := Stack_Size_Str'Length;
-            end if;
+            Max_Stack_Size_Len :=
+              (if Size_Str_Len > Stack_Size_Str'Length
+               then Size_Str_Len
+               else Stack_Size_Str'Length);
 
             --  Take either the label size or the number image size for the
-            --  size of the column "Stack Usage"
+            --  size of the column "Stack Usage".
 
-            if Result_Str_Len > Actual_Size_Str'Length then
-               Max_Actual_Use_Len := Result_Str_Len;
-            else
-               Max_Actual_Use_Len := Actual_Size_Str'Length;
-            end if;
+            Max_Actual_Use_Len :=
+              (if Result_Str_Len > Actual_Size_Str'Length
+               then Result_Str_Len
+               else Actual_Size_Str'Length);
 
             Output_Result
               (Analyzer.Result_Id,