Add an UNSPEC_PROLOGUE_USE to prevent the link register from being considered dead.
[official-gcc.git] / gcc / ada / s-stache.adb
blobf5780be6636568921c5408fcc60d284e87d953f7
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . S T A C K _ C H E C K I N G --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1999-2001 Free Software Foundation, Inc. --
11 -- --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
29 -- --
30 -- GNARL was developed by the GNARL team at Florida State University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 -- --
33 ------------------------------------------------------------------------------
35 with Ada.Exceptions;
37 with System.Storage_Elements; use System.Storage_Elements;
38 with System.Parameters; use System.Parameters;
39 with System.Soft_Links;
41 package body System.Stack_Checking is
43 Kilobyte : constant Storage_Offset := 1024;
44 Default_Env_Stack_Size : constant Storage_Offset := 8000 * Kilobyte;
45 -- This size is assumed for the environment stack when no size has been
46 -- set by the runtime, and no GNAT_STACK_LIMIT environment variable was
47 -- present. The value is chosen to be just under 8 MB whic is the actual
48 -- default size on some systems including GNU/LinuxThreads, so we will get
49 -- correct storage errors on those systems without setting environment
50 -- variables.
52 function Set_Stack_Info (Stack : access Stack_Access) return Stack_Access;
54 -- The function Set_Stack_Info is the actual function that updates
55 -- the cache containing a pointer to the Stack_Info. It may also
56 -- be used for detecting asynchronous abort in combination with
57 -- Invalidate_Self_Cache.
59 -- Set_Stack_Info should do the following things in order:
60 -- 1) Get the Stack_Access value for the current task
61 -- 2) Set Stack.all to the value obtained in 1)
62 -- 3) Optionally Poll to check for asynchronous abort
64 -- This order is important because if at any time a write to
65 -- the stack cache is pending, that write should be followed
66 -- by a Poll to prevent loosing signals.
68 -- Note: This function must be compiled with Polling turned off
70 -- Note: on systems like VxWorks and OS/2 with real thread-local storage,
71 -- Set_Stack_Info should return an access value for such local
72 -- storage. In those cases the cache will always be up-to-date.
74 -- The following constants should be imported from some system-specific
75 -- constants package. The constants must be static for performance reasons.
77 ----------------------------
78 -- Invalidate_Stack_Cache --
79 ----------------------------
81 procedure Invalidate_Stack_Cache (Any_Stack : Stack_Access) is
82 pragma Warnings (Off, Any_Stack);
84 begin
85 Cache := Null_Stack;
86 end Invalidate_Stack_Cache;
88 --------------------
89 -- Set_Stack_Info --
90 --------------------
92 function Set_Stack_Info
93 (Stack : access Stack_Access)
94 return Stack_Access
96 type Frame_Mark is null record;
97 Frame_Location : Frame_Mark;
98 Frame_Address : Address := Frame_Location'Address;
100 My_Stack : Stack_Access;
101 Limit_Chars : System.Address;
102 Limit : Integer;
104 function getenv (S : String) return System.Address;
105 pragma Import (C, getenv, External_Name => "getenv");
107 function atoi (A : System.Address) return Integer;
108 pragma Import (C, atoi);
110 begin
111 -- The order of steps 1 .. 3 is important, see specification.
113 -- 1) Get the Stack_Access value for the current task
115 My_Stack := Soft_Links.Get_Stack_Info.all;
117 if My_Stack.Base = Null_Address then
119 -- First invocation, initialize based on the assumption that
120 -- there are Environment_Stack_Size bytes available beyond
121 -- the current frame address.
123 if My_Stack.Size = 0 then
125 My_Stack.Size := Default_Env_Stack_Size;
127 -- When the environment variable GNAT_STACK_LIMIT is set,
128 -- set Environment_Stack_Size to that number of kB.
130 Limit_Chars := getenv ("GNAT_STACK_LIMIT" & ASCII.NUL);
132 if Limit_Chars /= Null_Address then
133 Limit := atoi (Limit_Chars);
134 if Limit >= 0 then
135 My_Stack.Size := Storage_Offset (Limit) * Kilobyte;
136 end if;
137 end if;
138 end if;
140 My_Stack.Base := Frame_Address;
142 if Stack_Grows_Down then
144 -- Prevent wrap-around on too big stack sizes
146 My_Stack.Limit := My_Stack.Base - My_Stack.Size;
148 if My_Stack.Limit > My_Stack.Base then
149 My_Stack.Limit := Address'First;
150 end if;
152 else
153 My_Stack.Limit := My_Stack.Base + My_Stack.Size;
155 -- Prevent wrap-around on too big stack sizes
157 if My_Stack.Limit < My_Stack.Base then
158 My_Stack.Limit := Address'Last;
159 end if;
160 end if;
161 end if;
163 -- 2) Set Stack.all to the value obtained in 1)
165 Stack.all := My_Stack;
167 -- 3) Optionally Poll to check for asynchronous abort
169 if Soft_Links.Check_Abort_Status.all /= 0 then
170 raise Standard'Abort_Signal;
171 end if;
173 return My_Stack; -- Never trust the cached value, but return local copy!
174 end Set_Stack_Info;
176 --------------------
177 -- Set_Stack_Size --
178 --------------------
180 -- Specify the stack size for the current frame.
182 procedure Set_Stack_Size
183 (Stack_Size : System.Storage_Elements.Storage_Offset)
185 My_Stack : Stack_Access;
186 Frame_Address : constant System.Address := My_Stack'Address;
188 begin
189 My_Stack := Stack_Check (Frame_Address);
191 if Stack_Grows_Down then
192 My_Stack.Limit := My_Stack.Base - Stack_Size;
193 else
194 My_Stack.Limit := My_Stack.Base + Stack_Size;
195 end if;
196 end Set_Stack_Size;
198 -----------------
199 -- Stack_Check --
200 -----------------
202 function Stack_Check
203 (Stack_Address : System.Address)
204 return Stack_Access
206 type Frame_Marker is null record;
207 Marker : Frame_Marker;
208 Cached_Stack : constant Stack_Access := Cache;
209 Frame_Address : constant System.Address := Marker'Address;
211 begin
212 -- This function first does a "cheap" check which is correct
213 -- if it succeeds. In case of failure, the full check is done.
214 -- Ideally the cheap check should be done in an optimized manner,
215 -- or be inlined.
217 if (Stack_Grows_Down and then
218 (Frame_Address <= Cached_Stack.Base
220 Stack_Address > Cached_Stack.Limit))
221 or else
222 (not Stack_Grows_Down and then
223 (Frame_Address >= Cached_Stack.Base
225 Stack_Address < Cached_Stack.Limit))
226 then
227 -- Cached_Stack is valid as it passed the stack check
228 return Cached_Stack;
229 end if;
231 Full_Check :
232 declare
233 My_Stack : Stack_Access := Set_Stack_Info (Cache'Access);
234 -- At this point Stack.all might already be invalid, so
235 -- it is essential to use our local copy of Stack!
237 begin
239 if (Stack_Grows_Down and then
240 (not (Frame_Address <= My_Stack.Base)))
241 or else
242 (not Stack_Grows_Down and then
243 (not (Frame_Address >= My_Stack.Base)))
244 then
245 -- The returned Base is lower than the stored one,
246 -- so assume that the original one wasn't right and use the
247 -- current Frame_Address as new one. This allows initializing
248 -- Base with the Frame_Address as approximation.
249 -- During initialization the Frame_Address will be close to
250 -- the stack base anyway: the difference should be compensated
251 -- for in the stack reserve.
253 My_Stack.Base := Frame_Address;
254 end if;
256 if (Stack_Grows_Down and then
257 Stack_Address < My_Stack.Limit)
258 or else
259 (not Stack_Grows_Down and then
260 Stack_Address > My_Stack.Limit)
261 then
262 Ada.Exceptions.Raise_Exception
263 (E => Storage_Error'Identity,
264 Message => "stack overflow detected");
265 end if;
267 return My_Stack;
268 end Full_Check;
269 end Stack_Check;
271 ------------------------
272 -- Update_Stack_Cache --
273 ------------------------
275 procedure Update_Stack_Cache (Stack : Stack_Access) is
276 begin
277 if not Multi_Processor then
278 Cache := Stack;
279 end if;
280 end Update_Stack_Cache;
282 end System.Stack_Checking;