* tree-ssa-phiopt.c (conditional_replacement): Construct proper SSA
[official-gcc.git] / gcc / ada / s-finimp.adb
blobd746cd3676f7525a872ee293a9c49908fcf845f8
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . F I N A L I Z A T I O N _ I M P L E M E N T A T I O N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Ada.Exceptions;
35 with Ada.Tags;
37 with System.Storage_Elements;
38 with System.Soft_Links;
40 with Unchecked_Conversion;
41 with System.Restrictions;
43 package body System.Finalization_Implementation is
45 use Ada.Exceptions;
46 use System.Finalization_Root;
48 package SSL renames System.Soft_Links;
50 package SSE renames System.Storage_Elements;
51 use type SSE.Storage_Offset;
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 type RC_Ptr is access all Record_Controller;
59 function To_RC_Ptr is
60 new Unchecked_Conversion (Address, RC_Ptr);
62 procedure Raise_Exception_No_Defer
63 (E : in Exception_Id;
64 Message : in String := "");
65 pragma Import (Ada, Raise_Exception_No_Defer,
66 "ada__exceptions__raise_exception_no_defer");
67 pragma No_Return (Raise_Exception_No_Defer);
68 -- Raise an exception without deferring abort. Note that we have to
69 -- use this rather kludgy Ada Import interface, since this subprogram
70 -- is not available in the visible spec of Ada.Exceptions.
72 procedure Raise_From_Finalize
73 (L : Finalizable_Ptr;
74 From_Abort : Boolean;
75 E_Occ : Exception_Occurrence);
76 -- Deal with an exception raised during finalization of a list. L is a
77 -- pointer to the list of element not yet finalized. From_Abort is true
78 -- if the finalization actions come from an abort rather than a normal
79 -- exit. E_Occ represents the exception being raised.
81 function RC_Offset (T : Ada.Tags.Tag) return SSE.Storage_Offset;
82 pragma Import (Ada, RC_Offset, "ada__tags__get_rc_offset");
84 function Parent_Size (Obj : Address; T : Ada.Tags.Tag)
85 return SSE.Storage_Count;
86 pragma Import (Ada, Parent_Size, "ada__tags__parent_size");
88 function Get_Deep_Controller (Obj : System.Address) return RC_Ptr;
89 -- Given the address (obj) of a tagged object, return a
90 -- pointer to the record controller of this object.
92 ------------
93 -- Adjust --
94 ------------
96 procedure Adjust (Object : in out Record_Controller) is
98 First_Comp : Finalizable_Ptr;
99 My_Offset : constant SSE.Storage_Offset :=
100 Object.My_Address - Object'Address;
102 procedure Ptr_Adjust (Ptr : in out Finalizable_Ptr);
103 -- Subtract the offset to the pointer
105 procedure Reverse_Adjust (P : Finalizable_Ptr);
106 -- Ajust the components in the reverse order in which they are stored
107 -- on the finalization list. (Adjust and Finalization are not done in
108 -- the same order)
110 ----------------
111 -- Ptr_Adjust --
112 ----------------
114 procedure Ptr_Adjust (Ptr : in out Finalizable_Ptr) is
115 begin
116 if Ptr /= null then
117 Ptr := To_Finalizable_Ptr (To_Addr (Ptr) - My_Offset);
118 end if;
119 end Ptr_Adjust;
121 --------------------
122 -- Reverse_Adjust --
123 --------------------
125 procedure Reverse_Adjust (P : Finalizable_Ptr) is
126 begin
127 if P /= null then
128 Ptr_Adjust (P.Next);
129 Reverse_Adjust (P.Next);
130 Adjust (P.all);
131 Object.F := P; -- Successfully adjusted, so place in list.
132 end if;
133 end Reverse_Adjust;
135 -- Start of processing for Adjust
137 begin
138 -- Adjust the components and their finalization pointers next. We must
139 -- protect against an exception in some call to Adjust, so we keep
140 -- pointing to the list of successfully adjusted components, which can
141 -- be finalized if an exception is raised.
143 First_Comp := Object.F;
144 Object.F := null; -- nothing adjusted yet.
145 Ptr_Adjust (First_Comp); -- set addresss of first component.
146 Reverse_Adjust (First_Comp);
148 -- Then Adjust the controller itself
150 Object.My_Address := Object'Address;
152 exception
153 when others =>
154 -- Finalize those components that were successfully adjusted, and
155 -- propagate exception. The object itself is not yet attached to
156 -- global finalization list, so we cannot rely on the outer call to
157 -- Clean to take care of these components.
159 Finalize (Object);
160 raise;
161 end Adjust;
163 --------------------------
164 -- Attach_To_Final_List --
165 --------------------------
167 procedure Attach_To_Final_List
168 (L : in out Finalizable_Ptr;
169 Obj : in out Finalizable;
170 Nb_Link : Short_Short_Integer)
172 begin
173 -- Simple case: attachement to a one way list
175 if Nb_Link = 1 then
176 Obj.Next := L;
177 L := Obj'Unchecked_Access;
179 -- Dynamically allocated objects: they are attached to a doubly linked
180 -- list, so that an element can be finalized at any moment by means of
181 -- an unchecked deallocation. Attachement is protected against
182 -- multi-threaded access.
184 elsif Nb_Link = 2 then
186 Locked_Processing : begin
187 SSL.Lock_Task.all;
188 Obj.Next := L.Next;
189 Obj.Prev := L.Next.Prev;
190 L.Next.Prev := Obj'Unchecked_Access;
191 L.Next := Obj'Unchecked_Access;
192 SSL.Unlock_Task.all;
194 exception
195 when others =>
196 SSL.Unlock_Task.all;
197 raise;
198 end Locked_Processing;
200 -- Attachement of arrays to the final list (used only for objects
201 -- returned by function). Obj, in this case is the last element,
202 -- but all other elements are already threaded after it. We just
203 -- attach the rest of the final list at the end of the array list.
205 elsif Nb_Link = 3 then
206 declare
207 P : Finalizable_Ptr := Obj'Unchecked_Access;
209 begin
210 while P.Next /= null loop
211 P := P.Next;
212 end loop;
214 P.Next := L;
215 L := Obj'Unchecked_Access;
216 end;
217 end if;
218 end Attach_To_Final_List;
220 ---------------------
221 -- Deep_Tag_Adjust --
222 ---------------------
224 procedure Deep_Tag_Adjust
225 (L : in out SFR.Finalizable_Ptr;
226 A : System.Address;
227 B : Short_Short_Integer)
229 V : constant SFR.Finalizable_Ptr := To_Finalizable_Ptr (A);
230 Controller : constant RC_Ptr := Get_Deep_Controller (A);
232 begin
233 if Controller /= null then
234 Adjust (Controller.all);
235 Attach_To_Final_List (L, Controller.all, B);
236 end if;
238 -- Is controlled
240 if V.all in Finalizable then
241 Adjust (V.all);
242 Attach_To_Final_List (L, Finalizable (V.all), 1);
243 end if;
244 end Deep_Tag_Adjust;
246 ---------------------
247 -- Deep_Tag_Attach --
248 ----------------------
250 procedure Deep_Tag_Attach
251 (L : in out SFR.Finalizable_Ptr;
252 A : System.Address;
253 B : Short_Short_Integer)
255 V : constant SFR.Finalizable_Ptr := To_Finalizable_Ptr (A);
256 Controller : constant RC_Ptr := Get_Deep_Controller (A);
258 begin
259 if Controller /= null then
260 Attach_To_Final_List (L, Controller.all, B);
261 end if;
263 -- Is controlled
265 if V.all in Finalizable then
266 Attach_To_Final_List (L, V.all, B);
267 end if;
268 end Deep_Tag_Attach;
270 -----------------------
271 -- Deep_Tag_Finalize --
272 -----------------------
274 procedure Deep_Tag_Finalize
275 (L : in out SFR.Finalizable_Ptr;
276 A : System.Address;
277 B : Boolean)
279 pragma Warnings (Off, L);
281 V : constant SFR.Finalizable_Ptr := To_Finalizable_Ptr (A);
282 Controller : constant RC_Ptr := Get_Deep_Controller (A);
284 begin
285 if Controller /= null then
286 if B then
287 Finalize_One (Controller.all);
288 else
289 Finalize (Controller.all);
290 end if;
291 end if;
293 -- Is controlled
295 if V.all in Finalizable then
296 if B then
297 Finalize_One (V.all);
298 else
299 Finalize (V.all);
300 end if;
301 end if;
302 end Deep_Tag_Finalize;
304 -------------------------
305 -- Deep_Tag_Initialize --
306 -------------------------
308 procedure Deep_Tag_Initialize
309 (L : in out SFR.Finalizable_Ptr;
310 A : System.Address;
311 B : Short_Short_Integer)
313 V : constant SFR.Finalizable_Ptr := To_Finalizable_Ptr (A);
314 Controller : constant RC_Ptr := Get_Deep_Controller (A);
316 begin
317 -- This procedure should not be called if the object has no
318 -- controlled components
320 if Controller = null then
321 raise Program_Error;
323 -- Has controlled components
325 else
326 Initialize (Controller.all);
327 Attach_To_Final_List (L, Controller.all, B);
328 end if;
330 -- Is controlled
332 if V.all in Finalizable then
333 Initialize (V.all);
334 Attach_To_Final_List (Controller.F, Finalizable (Controller.all), 1);
335 end if;
336 end Deep_Tag_Initialize;
338 -----------------------------
339 -- Detach_From_Final_List --
340 -----------------------------
342 -- We know that the detach object is neither at the beginning nor at the
343 -- end of the list, thank's to the dummy First and Last Elements but the
344 -- object may not be attached at all if it is Finalize_Storage_Only
346 procedure Detach_From_Final_List (Obj : in out Finalizable) is
347 begin
349 -- When objects are not properly attached to a doubly linked list do
350 -- not try to detach them. The only case where it can happen is when
351 -- dealing with Finalize_Storage_Only objects which are not always
352 -- attached to the finalization list.
354 if Obj.Next /= null and then Obj.Prev /= null then
355 SSL.Lock_Task.all;
356 Obj.Next.Prev := Obj.Prev;
357 Obj.Prev.Next := Obj.Next;
358 SSL.Unlock_Task.all;
359 end if;
361 exception
362 when others =>
363 SSL.Unlock_Task.all;
364 raise;
365 end Detach_From_Final_List;
367 --------------
368 -- Finalize --
369 --------------
371 procedure Finalize (Object : in out Limited_Record_Controller) is
372 begin
373 Finalize_List (Object.F);
374 end Finalize;
376 --------------------------
377 -- Finalize_Global_List --
378 --------------------------
380 procedure Finalize_Global_List is
381 begin
382 -- There are three case here:
384 -- a. the application uses tasks, in which case Finalize_Global_Tasks
385 -- will defer abort.
387 -- b. the application doesn't use tasks but uses other tasking
388 -- constructs, such as ATCs and protected objects. In this case,
389 -- the binder will call Finalize_Global_List instead of
390 -- Finalize_Global_Tasks, letting abort undeferred, and leading
391 -- to assertion failures in the GNULL
393 -- c. the application doesn't use any tasking construct in which case
394 -- deferring abort isn't necessary.
396 -- Until another solution is found to deal with case b, we need to
397 -- call abort_defer here to pass the checks, but we do not need to
398 -- undefer abort, since Finalize_Global_List is the last procedure
399 -- called before exiting the partition.
401 SSL.Abort_Defer.all;
402 Finalize_List (Global_Final_List);
403 end Finalize_Global_List;
405 -------------------
406 -- Finalize_List --
407 -------------------
409 procedure Finalize_List (L : Finalizable_Ptr) is
410 P : Finalizable_Ptr := L;
411 Q : Finalizable_Ptr;
413 type Fake_Exception_Occurence is record
414 Id : Exception_Id;
415 end record;
416 type Ptr is access all Fake_Exception_Occurence;
418 function To_Ptr is new
419 Unchecked_Conversion (Exception_Occurrence_Access, Ptr);
421 X : Exception_Id := Null_Id;
423 begin
424 -- If abort is allowed, we get the current exception before starting
425 -- to finalize in order to check if we are in the abort case if an
426 -- exception is raised. When abort is not allowed, avoid accessing the
427 -- current exception since this can be a pretty costly operation in
428 -- programs using controlled types heavily.
430 if System.Restrictions.Abort_Allowed then
431 X := To_Ptr (System.Soft_Links.Get_Current_Excep.all).Id;
432 end if;
434 while P /= null loop
435 Q := P.Next;
436 Finalize (P.all);
437 P := Q;
438 end loop;
440 exception
441 when E_Occ : others =>
442 Raise_From_Finalize (
444 X = Standard'Abort_Signal'Identity,
445 E_Occ);
446 end Finalize_List;
448 ------------------
449 -- Finalize_One --
450 ------------------
452 procedure Finalize_One (Obj : in out Finalizable) is
453 begin
454 Detach_From_Final_List (Obj);
455 Finalize (Obj);
456 exception
457 when E_Occ : others => Raise_From_Finalize (null, False, E_Occ);
458 end Finalize_One;
460 -------------------------
461 -- Get_Deep_Controller --
462 -------------------------
464 function Get_Deep_Controller (Obj : System.Address) return RC_Ptr is
465 The_Tag : Ada.Tags.Tag := To_Finalizable_Ptr (Obj)'Tag;
466 Offset : SSE.Storage_Offset := RC_Offset (The_Tag);
468 begin
469 -- Fetch the controller from the Parent or above if necessary
470 -- when there are no controller at this level
472 while Offset = -2 loop
473 The_Tag := Ada.Tags.Parent_Tag (The_Tag);
474 Offset := RC_Offset (The_Tag);
475 end loop;
477 -- No Controlled component case
479 if Offset = 0 then
480 return null;
482 -- The _controller Offset is known statically
484 elsif Offset > 0 then
485 return To_RC_Ptr (Obj + Offset);
487 -- At this stage, we know that the controller is part of the
488 -- ancestor corresponding to the tag "The_Tag" and that its parent
489 -- is variable sized. We assume that the _controller is the first
490 -- compoment right after the parent.
492 -- ??? note that it may not be true if there are new discriminants
494 else -- Offset = -1
496 declare
497 -- define a faked record controller to avoid generating
498 -- unnecessary expanded code for controlled types
500 type Faked_Record_Controller is record
501 Tag, Prec, Next : Address;
502 end record;
504 -- Reconstruction of a type with characteristics
505 -- comparable to the original type
507 D : constant := SSE.Storage_Offset (Storage_Unit - 1);
509 type Parent_Type is new SSE.Storage_Array
510 (1 .. (Parent_Size (Obj, The_Tag) + D) /
511 SSE.Storage_Offset (Storage_Unit));
512 for Parent_Type'Alignment use Address'Alignment;
514 type Faked_Type_Of_Obj is record
515 Parent : Parent_Type;
516 Controller : Faked_Record_Controller;
517 end record;
519 type Obj_Ptr is access all Faked_Type_Of_Obj;
520 function To_Obj_Ptr is
521 new Unchecked_Conversion (Address, Obj_Ptr);
523 begin
524 return To_RC_Ptr (To_Obj_Ptr (Obj).Controller'Address);
525 end;
526 end if;
527 end Get_Deep_Controller;
529 ----------------
530 -- Initialize --
531 ----------------
533 procedure Initialize (Object : in out Limited_Record_Controller) is
534 pragma Warnings (Off, Object);
535 begin
536 null;
537 end Initialize;
539 procedure Initialize (Object : in out Record_Controller) is
540 begin
541 Object.My_Address := Object'Address;
542 end Initialize;
544 -------------------------
545 -- Raise_From_Finalize --
546 -------------------------
548 procedure Raise_From_Finalize
549 (L : Finalizable_Ptr;
550 From_Abort : Boolean;
551 E_Occ : Exception_Occurrence)
553 Msg : constant String := Exception_Message (E_Occ);
554 P : Finalizable_Ptr := L;
555 Q : Finalizable_Ptr;
557 begin
558 -- We already got an exception. We now finalize the remainder of
559 -- the list, ignoring all further exceptions.
561 while P /= null loop
562 Q := P.Next;
564 begin
565 Finalize (P.all);
566 exception
567 when others => null;
568 end;
570 P := Q;
571 end loop;
573 -- If finalization from an Abort, then nothing to do
575 if From_Abort then
576 null;
578 -- If no message, then add our own message saying what happened
580 elsif Msg = "" then
581 Raise_Exception_No_Defer
582 (E => Program_Error'Identity,
583 Message => "exception " &
584 Exception_Name (E_Occ) &
585 " raised during finalization");
587 -- If there was a message, pass it on
589 else
590 Raise_Exception_No_Defer (Program_Error'Identity, Msg);
591 end if;
592 end Raise_From_Finalize;
594 -- Initialization of package, set Adafinal soft link
596 begin
597 SSL.Adafinal := Finalize_Global_List'Access;
599 end System.Finalization_Implementation;