OSDN Git Service

* reload1.c (reload_cse_simplify): Fix typo in rtx code check.
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_smem.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             E X P _ S M E M                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1998-2000 Free Software Foundation, Inc.          --
11 --                                                                          --
12 -- GNAT 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.  GNAT 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 GNAT;  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 -- GNAT was originally developed  by the GNAT team at  New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 --                                                                          --
26 ------------------------------------------------------------------------------
27
28 with Atree;    use Atree;
29 with Einfo;    use Einfo;
30 with Exp_Util; use Exp_Util;
31 with Nmake;    use Nmake;
32 with Namet;    use Namet;
33 with Nlists;   use Nlists;
34 with Rtsfind;  use Rtsfind;
35 with Sem;      use Sem;
36 with Sem_Util; use Sem_Util;
37 with Sinfo;    use Sinfo;
38 with Snames;   use Snames;
39 with Stand;    use Stand;
40 with Stringt;  use Stringt;
41 with Tbuild;   use Tbuild;
42
43 package body Exp_Smem is
44
45    Insert_Node : Node_Id;
46    --  Node after which a write call is to be inserted
47
48    -----------------------
49    -- Local Subprograms --
50    -----------------------
51
52    procedure Add_Read_Before (N : Node_Id);
53    --  Insert a Shared_Var_ROpen call for variable before node N
54
55    procedure Add_Write_After (N : Node_Id);
56    --  Insert a Shared_Var_WOpen call for variable after the node
57    --  Insert_Node, as recorded by On_Lhs_Of_Assigment (where it points
58    --  to the assignment statement) or Is_Out_Actual (where it points to
59    --  the procedure call statement).
60
61    procedure Build_Full_Name
62      (E : in  Entity_Id;
63       N : out String_Id);
64    --  Build the fully qualified string name of a shared variable.
65
66    function On_Lhs_Of_Assignment (N : Node_Id) return Boolean;
67    --  Determines if N is on the left hand of the assignment. This means
68    --  that either it is a simple variable, or it is a record or array
69    --  variable with a corresponding selected or indexed component on
70    --  the left side of an assignment. If the result is True, then
71    --  Insert_Node is set to point to the assignment
72
73    function Is_Out_Actual (N : Node_Id) return Boolean;
74    --  In a similar manner, this function determines if N appears as an
75    --  OUT or IN OUT parameter to a procedure call. If the result is
76    --  True, then Insert_Node is set to point to the assignment.
77
78    ---------------------
79    -- Add_Read_Before --
80    ---------------------
81
82    procedure Add_Read_Before (N : Node_Id) is
83       Loc : constant Source_Ptr := Sloc (N);
84       Ent : constant Node_Id    := Entity (N);
85
86    begin
87       if Present (Shared_Var_Read_Proc (Ent)) then
88          Insert_Action (N,
89            Make_Procedure_Call_Statement (Loc,
90              Name =>
91                New_Occurrence_Of (Shared_Var_Read_Proc (Ent), Loc),
92              Parameter_Associations => Empty_List));
93       end if;
94    end Add_Read_Before;
95
96    -------------------------------
97    -- Add_Shared_Var_Lock_Procs --
98    -------------------------------
99
100    procedure Add_Shared_Var_Lock_Procs (N : Node_Id) is
101       Loc   : constant Source_Ptr := Sloc (N);
102       Obj   : constant Entity_Id  := Entity (Expression (First_Actual (N)));
103       Inode : Node_Id;
104       Vnm   : String_Id;
105
106    begin
107       --  We have to add Shared_Var_Lock and Shared_Var_Unlock calls around
108       --  the procedure or function call node. First we locate the right
109       --  place to do the insertion, which is the call itself in the
110       --  procedure call case, or else the nearest non subexpression
111       --  node that contains the function call.
112
113       Inode := N;
114       while Nkind (Inode) /= N_Procedure_Call_Statement
115         and then Nkind (Inode) in N_Subexpr
116       loop
117          Inode := Parent (Inode);
118       end loop;
119
120       --  Now insert the Lock and Unlock calls and the read/write calls
121
122       --  Two concerns here. First we are not dealing with the exception
123       --  case, really we need some kind of cleanup routine to do the
124       --  Unlock. Second, these lock calls should be inside the protected
125       --  object processing, not outside, otherwise they can be done at
126       --  the wrong priority, resulting in dead lock situations ???
127
128       Build_Full_Name (Obj, Vnm);
129
130       --  First insert the Lock call before
131
132       Insert_Before_And_Analyze (Inode,
133         Make_Procedure_Call_Statement (Loc,
134           Name => New_Occurrence_Of (RTE (RE_Shared_Var_Lock), Loc),
135           Parameter_Associations => New_List (
136             Make_String_Literal (Loc, Vnm))));
137
138       --  Now, right after the Lock, insert a call to read the object
139
140       Insert_Before_And_Analyze (Inode,
141         Make_Procedure_Call_Statement (Loc,
142           Name => New_Occurrence_Of (Shared_Var_Read_Proc (Obj), Loc)));
143
144       --  Now insert the Unlock call after
145
146       Insert_After_And_Analyze (Inode,
147         Make_Procedure_Call_Statement (Loc,
148           Name => New_Occurrence_Of (RTE (RE_Shared_Var_Unlock), Loc),
149           Parameter_Associations => New_List (
150             Make_String_Literal (Loc, Vnm))));
151
152       --  Now for a procedure call, but not a function call, insert the
153       --  call to write the object just before the unlock.
154
155       if Nkind (N) = N_Procedure_Call_Statement then
156          Insert_After_And_Analyze (Inode,
157            Make_Procedure_Call_Statement (Loc,
158              Name => New_Occurrence_Of (Shared_Var_Assign_Proc (Obj), Loc)));
159       end if;
160
161    end Add_Shared_Var_Lock_Procs;
162
163    ---------------------
164    -- Add_Write_After --
165    ---------------------
166
167    procedure Add_Write_After (N : Node_Id) is
168       Loc : constant Source_Ptr := Sloc (N);
169       Ent : constant Node_Id    := Entity (N);
170
171    begin
172       if Present (Shared_Var_Assign_Proc (Ent)) then
173          Insert_After_And_Analyze (Insert_Node,
174            Make_Procedure_Call_Statement (Loc,
175              Name =>
176                New_Occurrence_Of (Shared_Var_Assign_Proc (Ent), Loc),
177              Parameter_Associations => Empty_List));
178       end if;
179    end Add_Write_After;
180
181    ---------------------
182    -- Build_Full_Name --
183    ---------------------
184
185    procedure Build_Full_Name
186      (E : in  Entity_Id;
187       N : out String_Id)
188    is
189
190       procedure Build_Name (E : Entity_Id);
191       --  This is a recursive routine used to construct the fully
192       --  qualified string name of the package corresponding to the
193       --  shared variable.
194
195       procedure Build_Name (E : Entity_Id) is
196       begin
197          if Scope (E) /= Standard_Standard then
198             Build_Name (Scope (E));
199             Store_String_Char ('.');
200          end if;
201
202          Get_Decoded_Name_String (Chars (E));
203          Store_String_Chars (Name_Buffer (1 .. Name_Len));
204       end Build_Name;
205
206    begin
207       Start_String;
208       Build_Name (E);
209       N := End_String;
210    end Build_Full_Name;
211
212    ------------------------------------
213    -- Expand_Shared_Passive_Variable --
214    ------------------------------------
215
216    procedure Expand_Shared_Passive_Variable (N : Node_Id) is
217       Typ : constant Entity_Id := Etype (N);
218
219    begin
220       --  Nothing to do for protected or limited objects
221
222       if Is_Limited_Type (Typ) or else Is_Concurrent_Type (Typ) then
223          return;
224
225       --  If we are on the left hand side of an assignment, then we add
226       --  the write call after the assignment.
227
228       elsif On_Lhs_Of_Assignment (N) then
229          Add_Write_After (N);
230
231       --  If we are a parameter for an out or in out formal, then put
232       --  the read before and the write after.
233
234       elsif Is_Out_Actual (N) then
235          Add_Read_Before (N);
236          Add_Write_After (N);
237
238       --  All other cases are simple reads
239
240       else
241          Add_Read_Before (N);
242       end if;
243    end Expand_Shared_Passive_Variable;
244
245    -------------------
246    -- Is_Out_Actual --
247    -------------------
248
249    function Is_Out_Actual (N : Node_Id) return Boolean is
250       Parnt  : constant Node_Id := Parent (N);
251       Formal : Entity_Id;
252       Call   : Node_Id;
253       Actual : Node_Id;
254
255    begin
256       if (Nkind (Parnt) = N_Indexed_Component
257             or else
258           Nkind (Parnt) = N_Selected_Component)
259         and then N = Prefix (Parnt)
260       then
261          return Is_Out_Actual (Parnt);
262
263       elsif Nkind (Parnt) = N_Parameter_Association
264         and then N = Explicit_Actual_Parameter (Parnt)
265       then
266          Call := Parent (Parnt);
267
268       elsif Nkind (Parnt) = N_Procedure_Call_Statement then
269          Call := Parnt;
270
271       else
272          return False;
273       end if;
274
275       --  Fall here if we are definitely a parameter
276
277       Actual := First_Actual (Call);
278       Formal := First_Formal (Entity (Name (Call)));
279
280       loop
281          if Actual = N then
282             if Ekind (Formal) /= E_In_Parameter then
283                Insert_Node := Call;
284                return True;
285             else
286                return False;
287             end if;
288
289          else
290             Actual := Next_Actual (Actual);
291             Formal := Next_Formal (Formal);
292          end if;
293       end loop;
294    end Is_Out_Actual;
295
296    ---------------------------
297    -- Make_Shared_Var_Procs --
298    ---------------------------
299
300    procedure Make_Shared_Var_Procs (N : Node_Id) is
301       Loc : constant Source_Ptr := Sloc (N);
302       Ent : constant Entity_Id  := Defining_Identifier (N);
303       Typ : constant Entity_Id  := Etype (Ent);
304       Vnm : String_Id;
305       Atr : Node_Id;
306
307       Assign_Proc : constant Entity_Id :=
308                       Make_Defining_Identifier (Loc,
309                         Chars => New_External_Name (Chars (Ent), 'A'));
310
311       Read_Proc : constant Entity_Id :=
312                     Make_Defining_Identifier (Loc,
313                       Chars => New_External_Name (Chars (Ent), 'R'));
314
315       S : Entity_Id;
316
317    --  Start of processing for Make_Shared_Var_Procs
318
319    begin
320       Build_Full_Name (Ent, Vnm);
321
322       --  We turn off Shared_Passive during construction and analysis of
323       --  the assign and read routines, to avoid improper attempts to
324       --  process the variable references within these procedures.
325
326       Set_Is_Shared_Passive (Ent, False);
327
328       --  Construct assignment routine
329
330       --    procedure VarA is
331       --       S : Ada.Streams.Stream_IO.Stream_Access;
332       --    begin
333       --       S := Shared_Var_WOpen ("pkg.var");
334       --       typ'Write (S, var);
335       --       Shared_Var_Close (S);
336       --    end VarA;
337
338       S   := Make_Defining_Identifier (Loc, Name_uS);
339
340       Atr :=
341         Make_Attribute_Reference (Loc,
342           Prefix => New_Occurrence_Of (Typ, Loc),
343           Attribute_Name => Name_Write,
344           Expressions => New_List (
345             New_Reference_To (S, Loc),
346             New_Occurrence_Of (Ent, Loc)));
347
348       Set_OK_For_Stream (Atr, True);
349
350       Insert_After_And_Analyze (N,
351         Make_Subprogram_Body (Loc,
352           Specification =>
353             Make_Procedure_Specification (Loc,
354               Defining_Unit_Name => Assign_Proc),
355
356          --  S : Ada.Streams.Stream_IO.Stream_Access;
357
358           Declarations => New_List (
359             Make_Object_Declaration (Loc,
360               Defining_Identifier => S,
361               Object_Definition =>
362                 New_Occurrence_Of (RTE (RE_Stream_Access), Loc))),
363
364           Handled_Statement_Sequence =>
365             Make_Handled_Sequence_Of_Statements (Loc,
366               Statements => New_List (
367
368                --  S := Shared_Var_WOpen ("pkg.var");
369
370                 Make_Assignment_Statement (Loc,
371                   Name => New_Reference_To (S, Loc),
372                   Expression =>
373                     Make_Function_Call (Loc,
374                       Name =>
375                         New_Occurrence_Of
376                           (RTE (RE_Shared_Var_WOpen), Loc),
377                       Parameter_Associations => New_List (
378                         Make_String_Literal (Loc, Vnm)))),
379
380                 Atr,
381
382                --  Shared_Var_Close (S);
383
384                 Make_Procedure_Call_Statement (Loc,
385                   Name =>
386                     New_Occurrence_Of (RTE (RE_Shared_Var_Close), Loc),
387                   Parameter_Associations =>
388                     New_List (New_Reference_To (S, Loc)))))));
389
390       --  Construct read routine
391
392       --    procedure varR is
393       --       S : Ada.Streams.Stream_IO.Stream_Access;
394       --    begin
395       --       S := Shared_Var_ROpen ("pkg.var");
396       --       if S /= null then
397       --          typ'Read (S, Var);
398       --          Shared_Var_Close (S);
399       --       end if;
400       --    end varR;
401
402       S   := Make_Defining_Identifier (Loc, Name_uS);
403
404       Atr :=
405         Make_Attribute_Reference (Loc,
406           Prefix => New_Occurrence_Of (Typ, Loc),
407           Attribute_Name => Name_Read,
408           Expressions => New_List (
409             New_Reference_To (S, Loc),
410             New_Occurrence_Of (Ent, Loc)));
411
412       Set_OK_For_Stream (Atr, True);
413
414       Insert_After_And_Analyze (N,
415         Make_Subprogram_Body (Loc,
416           Specification =>
417             Make_Procedure_Specification (Loc,
418               Defining_Unit_Name => Read_Proc),
419
420          --  S : Ada.Streams.Stream_IO.Stream_Access;
421
422           Declarations => New_List (
423             Make_Object_Declaration (Loc,
424               Defining_Identifier => S,
425               Object_Definition =>
426                 New_Occurrence_Of (RTE (RE_Stream_Access), Loc))),
427
428           Handled_Statement_Sequence =>
429             Make_Handled_Sequence_Of_Statements (Loc,
430               Statements => New_List (
431
432                --  S := Shared_Var_ROpen ("pkg.var");
433
434                 Make_Assignment_Statement (Loc,
435                   Name => New_Reference_To (S, Loc),
436                   Expression =>
437                     Make_Function_Call (Loc,
438                       Name =>
439                         New_Occurrence_Of
440                           (RTE (RE_Shared_Var_ROpen), Loc),
441                       Parameter_Associations => New_List (
442                         Make_String_Literal (Loc, Vnm)))),
443
444                --  if S /= null then
445
446                 Make_Implicit_If_Statement (N,
447                   Condition =>
448                     Make_Op_Ne (Loc,
449                       Left_Opnd  => New_Reference_To (S, Loc),
450                       Right_Opnd => Make_Null (Loc)),
451
452                    Then_Statements => New_List (
453
454                      --  typ'Read (S, Var);
455
456                      Atr,
457
458                      --  Shared_Var_Close (S);
459
460                      Make_Procedure_Call_Statement (Loc,
461                        Name =>
462                          New_Occurrence_Of
463                            (RTE (RE_Shared_Var_Close), Loc),
464                        Parameter_Associations =>
465                          New_List (New_Reference_To (S, Loc)))))))));
466
467       Set_Is_Shared_Passive      (Ent, True);
468       Set_Shared_Var_Assign_Proc (Ent, Assign_Proc);
469       Set_Shared_Var_Read_Proc   (Ent, Read_Proc);
470    end Make_Shared_Var_Procs;
471
472    --------------------------
473    -- On_Lhs_Of_Assignment --
474    --------------------------
475
476    function On_Lhs_Of_Assignment (N : Node_Id) return Boolean is
477       P : constant Node_Id := Parent (N);
478
479    begin
480       if Nkind (P) = N_Assignment_Statement then
481          if N = Name (P) then
482             Insert_Node := P;
483             return True;
484          else
485             return False;
486          end if;
487
488       elsif (Nkind (P) = N_Indexed_Component
489                or else
490              Nkind (P) = N_Selected_Component)
491         and then N = Prefix (P)
492       then
493          return On_Lhs_Of_Assignment (P);
494
495       else
496          return False;
497       end if;
498    end On_Lhs_Of_Assignment;
499
500
501 end Exp_Smem;