summaryrefslogtreecommitdiffstats
path: root/gcc/ada/exp_smem.adb
blob: c2b129785e94f3370e2dad59f029e0f69adbc54e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                             E X P _ S M E M                              --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 1998-2005 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 2,  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 COPYING.  If not, write --
-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
-- MA 02111-1307, USA.                                                      --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

with Atree;    use Atree;
with Einfo;    use Einfo;
with Exp_Util; use Exp_Util;
with Nmake;    use Nmake;
with Namet;    use Namet;
with Nlists;   use Nlists;
with Rtsfind;  use Rtsfind;
with Sem;      use Sem;
with Sem_Util; use Sem_Util;
with Sinfo;    use Sinfo;
with Snames;   use Snames;
with Stand;    use Stand;
with Stringt;  use Stringt;
with Tbuild;   use Tbuild;

package body Exp_Smem is

   Insert_Node : Node_Id;
   --  Node after which a write call is to be inserted

   -----------------------
   -- Local Subprograms --
   -----------------------

   procedure Add_Read_Before (N : Node_Id);
   --  Insert a Shared_Var_ROpen call for variable before node N

   procedure Add_Write_After (N : Node_Id);
   --  Insert a Shared_Var_WOpen call for variable after the node
   --  Insert_Node, as recorded by On_Lhs_Of_Assigment (where it points
   --  to the assignment statement) or Is_Out_Actual (where it points to
   --  the procedure call statement).

   procedure Build_Full_Name
     (E : in  Entity_Id;
      N : out String_Id);
   --  Build the fully qualified string name of a shared variable.

   function On_Lhs_Of_Assignment (N : Node_Id) return Boolean;
   --  Determines if N is on the left hand of the assignment. This means
   --  that either it is a simple variable, or it is a record or array
   --  variable with a corresponding selected or indexed component on
   --  the left side of an assignment. If the result is True, then
   --  Insert_Node is set to point to the assignment

   function Is_Out_Actual (N : Node_Id) return Boolean;
   --  In a similar manner, this function determines if N appears as an
   --  OUT or IN OUT parameter to a procedure call. If the result is
   --  True, then Insert_Node is set to point to the assignment.

   ---------------------
   -- Add_Read_Before --
   ---------------------

   procedure Add_Read_Before (N : Node_Id) is
      Loc : constant Source_Ptr := Sloc (N);
      Ent : constant Node_Id    := Entity (N);

   begin
      if Present (Shared_Var_Read_Proc (Ent)) then
         Insert_Action (N,
           Make_Procedure_Call_Statement (Loc,
             Name =>
               New_Occurrence_Of (Shared_Var_Read_Proc (Ent), Loc),
             Parameter_Associations => Empty_List));
      end if;
   end Add_Read_Before;

   -------------------------------
   -- Add_Shared_Var_Lock_Procs --
   -------------------------------

   procedure Add_Shared_Var_Lock_Procs (N : Node_Id) is
      Loc   : constant Source_Ptr := Sloc (N);
      Obj   : constant Entity_Id  := Entity (Expression (First_Actual (N)));
      Inode : Node_Id;
      Vnm   : String_Id;

   begin
      --  We have to add Shared_Var_Lock and Shared_Var_Unlock calls around
      --  the procedure or function call node. First we locate the right
      --  place to do the insertion, which is the call itself in the
      --  procedure call case, or else the nearest non subexpression
      --  node that contains the function call.

      Inode := N;
      while Nkind (Inode) /= N_Procedure_Call_Statement
        and then Nkind (Inode) in N_Subexpr
      loop
         Inode := Parent (Inode);
      end loop;

      --  Now insert the Lock and Unlock calls and the read/write calls

      --  Two concerns here. First we are not dealing with the exception
      --  case, really we need some kind of cleanup routine to do the
      --  Unlock. Second, these lock calls should be inside the protected
      --  object processing, not outside, otherwise they can be done at
      --  the wrong priority, resulting in dead lock situations ???

      Build_Full_Name (Obj, Vnm);

      --  First insert the Lock call before

      Insert_Before_And_Analyze (Inode,
        Make_Procedure_Call_Statement (Loc,
          Name => New_Occurrence_Of (RTE (RE_Shared_Var_Lock), Loc),
          Parameter_Associations => New_List (
            Make_String_Literal (Loc, Vnm))));

      --  Now, right after the Lock, insert a call to read the object

      Insert_Before_And_Analyze (Inode,
        Make_Procedure_Call_Statement (Loc,
          Name => New_Occurrence_Of (Shared_Var_Read_Proc (Obj), Loc)));

      --  Now insert the Unlock call after

      Insert_After_And_Analyze (Inode,
        Make_Procedure_Call_Statement (Loc,
          Name => New_Occurrence_Of (RTE (RE_Shared_Var_Unlock), Loc),
          Parameter_Associations => New_List (
            Make_String_Literal (Loc, Vnm))));

      --  Now for a procedure call, but not a function call, insert the
      --  call to write the object just before the unlock.

      if Nkind (N) = N_Procedure_Call_Statement then
         Insert_After_And_Analyze (Inode,
           Make_Procedure_Call_Statement (Loc,
             Name => New_Occurrence_Of (Shared_Var_Assign_Proc (Obj), Loc)));
      end if;

   end Add_Shared_Var_Lock_Procs;

   ---------------------
   -- Add_Write_After --
   ---------------------

   procedure Add_Write_After (N : Node_Id) is
      Loc : constant Source_Ptr := Sloc (N);
      Ent : constant Node_Id    := Entity (N);

   begin
      if Present (Shared_Var_Assign_Proc (Ent)) then
         Insert_After_And_Analyze (Insert_Node,
           Make_Procedure_Call_Statement (Loc,
             Name =>
               New_Occurrence_Of (Shared_Var_Assign_Proc (Ent), Loc),
             Parameter_Associations => Empty_List));
      end if;
   end Add_Write_After;

   ---------------------
   -- Build_Full_Name --
   ---------------------

   procedure Build_Full_Name
     (E : in  Entity_Id;
      N : out String_Id)
   is

      procedure Build_Name (E : Entity_Id);
      --  This is a recursive routine used to construct the fully
      --  qualified string name of the package corresponding to the
      --  shared variable.

      procedure Build_Name (E : Entity_Id) is
      begin
         if Scope (E) /= Standard_Standard then
            Build_Name (Scope (E));
            Store_String_Char ('.');
         end if;

         Get_Decoded_Name_String (Chars (E));
         Store_String_Chars (Name_Buffer (1 .. Name_Len));
      end Build_Name;

   begin
      Start_String;
      Build_Name (E);
      N := End_String;
   end Build_Full_Name;

   ------------------------------------
   -- Expand_Shared_Passive_Variable --
   ------------------------------------

   procedure Expand_Shared_Passive_Variable (N : Node_Id) is
      Typ : constant Entity_Id := Etype (N);

   begin
      --  Nothing to do for protected or limited objects

      if Is_Limited_Type (Typ) or else Is_Concurrent_Type (Typ) then
         return;

      --  If we are on the left hand side of an assignment, then we add
      --  the write call after the assignment.

      elsif On_Lhs_Of_Assignment (N) then
         Add_Write_After (N);

      --  If we are a parameter for an out or in out formal, then put
      --  the read before and the write after.

      elsif Is_Out_Actual (N) then
         Add_Read_Before (N);
         Add_Write_After (N);

      --  All other cases are simple reads

      else
         Add_Read_Before (N);
      end if;
   end Expand_Shared_Passive_Variable;

   -------------------
   -- Is_Out_Actual --
   -------------------

   function Is_Out_Actual (N : Node_Id) return Boolean is
      Parnt  : constant Node_Id := Parent (N);
      Formal : Entity_Id;
      Call   : Node_Id;
      Actual : Node_Id;

   begin
      if (Nkind (Parnt) = N_Indexed_Component
            or else
          Nkind (Parnt) = N_Selected_Component)
        and then N = Prefix (Parnt)
      then
         return Is_Out_Actual (Parnt);

      elsif Nkind (Parnt) = N_Parameter_Association
        and then N = Explicit_Actual_Parameter (Parnt)
      then
         Call := Parent (Parnt);

      elsif Nkind (Parnt) = N_Procedure_Call_Statement then
         Call := Parnt;

      else
         return False;
      end if;

      --  Fall here if we are definitely a parameter

      Actual := First_Actual (Call);
      Formal := First_Formal (Entity (Name (Call)));

      loop
         if Actual = N then
            if Ekind (Formal) /= E_In_Parameter then
               Insert_Node := Call;
               return True;
            else
               return False;
            end if;

         else
            Actual := Next_Actual (Actual);
            Formal := Next_Formal (Formal);
         end if;
      end loop;
   end Is_Out_Actual;

   ---------------------------
   -- Make_Shared_Var_Procs --
   ---------------------------

   procedure Make_Shared_Var_Procs (N : Node_Id) is
      Loc : constant Source_Ptr := Sloc (N);
      Ent : constant Entity_Id  := Defining_Identifier (N);
      Typ : constant Entity_Id  := Etype (Ent);
      Vnm : String_Id;
      Atr : Node_Id;

      Assign_Proc : constant Entity_Id :=
                      Make_Defining_Identifier (Loc,
                        Chars => New_External_Name (Chars (Ent), 'A'));

      Read_Proc : constant Entity_Id :=
                    Make_Defining_Identifier (Loc,
                      Chars => New_External_Name (Chars (Ent), 'R'));

      S : Entity_Id;

   --  Start of processing for Make_Shared_Var_Procs

   begin
      Build_Full_Name (Ent, Vnm);

      --  We turn off Shared_Passive during construction and analysis of
      --  the assign and read routines, to avoid improper attempts to
      --  process the variable references within these procedures.

      Set_Is_Shared_Passive (Ent, False);

      --  Construct assignment routine

      --    procedure VarA is
      --       S : Ada.Streams.Stream_IO.Stream_Access;
      --    begin
      --       S := Shared_Var_WOpen ("pkg.var");
      --       typ'Write (S, var);
      --       Shared_Var_Close (S);
      --    end VarA;

      S   := Make_Defining_Identifier (Loc, Name_uS);

      Atr :=
        Make_Attribute_Reference (Loc,
          Prefix => New_Occurrence_Of (Typ, Loc),
          Attribute_Name => Name_Write,
          Expressions => New_List (
            New_Reference_To (S, Loc),
            New_Occurrence_Of (Ent, Loc)));

      Insert_After_And_Analyze (N,
        Make_Subprogram_Body (Loc,
          Specification =>
            Make_Procedure_Specification (Loc,
              Defining_Unit_Name => Assign_Proc),

         --  S : Ada.Streams.Stream_IO.Stream_Access;

          Declarations => New_List (
            Make_Object_Declaration (Loc,
              Defining_Identifier => S,
              Object_Definition =>
                New_Occurrence_Of (RTE (RE_Stream_Access), Loc))),

          Handled_Statement_Sequence =>
            Make_Handled_Sequence_Of_Statements (Loc,
              Statements => New_List (

               --  S := Shared_Var_WOpen ("pkg.var");

                Make_Assignment_Statement (Loc,
                  Name => New_Reference_To (S, Loc),
                  Expression =>
                    Make_Function_Call (Loc,
                      Name =>
                        New_Occurrence_Of
                          (RTE (RE_Shared_Var_WOpen), Loc),
                      Parameter_Associations => New_List (
                        Make_String_Literal (Loc, Vnm)))),

                Atr,

               --  Shared_Var_Close (S);

                Make_Procedure_Call_Statement (Loc,
                  Name =>
                    New_Occurrence_Of (RTE (RE_Shared_Var_Close), Loc),
                  Parameter_Associations =>
                    New_List (New_Reference_To (S, Loc)))))));

      --  Construct read routine

      --    procedure varR is
      --       S : Ada.Streams.Stream_IO.Stream_Access;
      --    begin
      --       S := Shared_Var_ROpen ("pkg.var");
      --       if S /= null then
      --          typ'Read (S, Var);
      --          Shared_Var_Close (S);
      --       end if;
      --    end varR;

      S   := Make_Defining_Identifier (Loc, Name_uS);

      Atr :=
        Make_Attribute_Reference (Loc,
          Prefix => New_Occurrence_Of (Typ, Loc),
          Attribute_Name => Name_Read,
          Expressions => New_List (
            New_Reference_To (S, Loc),
            New_Occurrence_Of (Ent, Loc)));

      Insert_After_And_Analyze (N,
        Make_Subprogram_Body (Loc,
          Specification =>
            Make_Procedure_Specification (Loc,
              Defining_Unit_Name => Read_Proc),

         --  S : Ada.Streams.Stream_IO.Stream_Access;

          Declarations => New_List (
            Make_Object_Declaration (Loc,
              Defining_Identifier => S,
              Object_Definition =>
                New_Occurrence_Of (RTE (RE_Stream_Access), Loc))),

          Handled_Statement_Sequence =>
            Make_Handled_Sequence_Of_Statements (Loc,
              Statements => New_List (

               --  S := Shared_Var_ROpen ("pkg.var");

                Make_Assignment_Statement (Loc,
                  Name => New_Reference_To (S, Loc),
                  Expression =>
                    Make_Function_Call (Loc,
                      Name =>
                        New_Occurrence_Of
                          (RTE (RE_Shared_Var_ROpen), Loc),
                      Parameter_Associations => New_List (
                        Make_String_Literal (Loc, Vnm)))),

               --  if S /= null then

                Make_Implicit_If_Statement (N,
                  Condition =>
                    Make_Op_Ne (Loc,
                      Left_Opnd  => New_Reference_To (S, Loc),
                      Right_Opnd => Make_Null (Loc)),

                   Then_Statements => New_List (

                     --  typ'Read (S, Var);

                     Atr,

                     --  Shared_Var_Close (S);

                     Make_Procedure_Call_Statement (Loc,
                       Name =>
                         New_Occurrence_Of
                           (RTE (RE_Shared_Var_Close), Loc),
                       Parameter_Associations =>
                         New_List (New_Reference_To (S, Loc)))))))));

      Set_Is_Shared_Passive      (Ent, True);
      Set_Shared_Var_Assign_Proc (Ent, Assign_Proc);
      Set_Shared_Var_Read_Proc   (Ent, Read_Proc);
   end Make_Shared_Var_Procs;

   --------------------------
   -- On_Lhs_Of_Assignment --
   --------------------------

   function On_Lhs_Of_Assignment (N : Node_Id) return Boolean is
      P : constant Node_Id := Parent (N);

   begin
      if Nkind (P) = N_Assignment_Statement then
         if N = Name (P) then
            Insert_Node := P;
            return True;
         else
            return False;
         end if;

      elsif (Nkind (P) = N_Indexed_Component
               or else
             Nkind (P) = N_Selected_Component)
        and then N = Prefix (P)
      then
         return On_Lhs_Of_Assignment (P);

      else
         return False;
      end if;
   end On_Lhs_Of_Assignment;

end Exp_Smem;
OpenPOWER on IntegriCloud