ocarina-backends-pn-components.adb 135 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--       O C A R I N A . B A C K E N D S . P N . C O M P O N E N T S        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2014 ESA & ISAE.      --
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT 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 Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
jhugues's avatar
jhugues committed
29 30
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
31 32 33 34 35
--                                                                          --
------------------------------------------------------------------------------

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Instances.Nodes;
36
with Ocarina.Namet;
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
with Ocarina.Backends.PN.Nodes;
with Ocarina.Backends.PN.Nutils;

with Ocarina.ME_AADL.AADL_Instances.Entities;
with Ocarina.ME_AADL.AADL_Instances.Nutils;

with Ocarina.Backends.Properties;
with Ocarina.AADL_Values;
with Ocarina.Instances.Queries;
with Ocarina.Backends.PN.Iutils;
with Ocarina.Backends.PN.Utils;
with Ocarina.Backends.Messages;       use Ocarina.Backends.Messages;

with Ocarina.Backends.Utils;

with Ocarina.Backends.PN.Debug;

package body Ocarina.Backends.PN.Components is

   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package OPU renames Ocarina.Backends.PN.Nutils;
   package AINU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package OPN renames Ocarina.Backends.PN.Nodes;
   package OPND renames Ocarina.Backends.PN.Debug;

   package OAV renames Ocarina.AADL_Values;

jhugues's avatar
jhugues committed
64 65 66 67 68 69 70
   procedure PN_Process_Final_System
     (PN_Generated : Node_Id;
      PN_P_Init_Proc : PN_Init_Node;
      PN_P_New_Proc  : PN_New_Node;
      PN_A_Init_Proc : PN_Init_Arc;
      PN_A_New_Proc  : PN_New_Node;
      PN_Dup_Arc_Proc : PN_Dup_Arc);
71 72

   function Process_Component_Instance
73 74
     (Instance : Ocarina.Types.Node_Id;
      PN_Generated : Ocarina.Types.Node_Id)
75 76 77
     return Boolean;

   function Process_Thread_Instance
78 79
     (Aadl_Instance : Ocarina.Types.Node_Id;
      PN_Generated : Ocarina.Types.Node_Id)
80 81
     return Boolean;

jhugues's avatar
jhugues committed
82
   procedure PN_Process_Thread_Pattern
83 84 85
     (Aadl_Instance : Ocarina.Types.Node_Id;
      PN_Thread : Ocarina.Types.Node_Id;
      PN_Generated : Ocarina.Types.Node_Id;
jhugues's avatar
jhugues committed
86 87 88 89 90 91 92 93
      PN_P_Init_Proc : PN_Init_Node;
      PN_P_New_Proc : PN_New_Node;
      PN_T_Init_Proc : PN_Init_Node;
      PN_T_New_Proc : PN_New_Node;
      PN_A_Init_Proc : PN_Init_Arc;
      PN_A_New_Proc : PN_New_Node);

   procedure PN_Thread_Skeleton
94 95 96
     (Aadl_Instance : Ocarina.Types.Node_Id;
      PN_Thread : Ocarina.Types.Node_Id;
      PN_Generated : Ocarina.Types.Node_Id;
jhugues's avatar
jhugues committed
97 98 99 100 101 102 103 104
      PN_P_Init_Proc : PN_Init_Node;
      PN_P_New_Proc : PN_New_Node;
      PN_T_Init_Proc : PN_Init_Node;
      PN_T_New_Proc : PN_New_Node;
      PN_A_Init_Proc : PN_Init_Arc;
      PN_A_New_Proc : PN_New_Node);

   procedure PN_Build_Spg_Par
105 106 107 108
     (Aadl_Instance : Ocarina.Types.Node_Id;
      PN_Generated  : Ocarina.Types.Node_Id;
      Spg_Feat : Ocarina.Types.Node_Id;
      PN_Spg_Call : Ocarina.Types.Node_Id;
109
      In_Par : Boolean := True;
jhugues's avatar
jhugues committed
110 111 112 113 114 115 116
      PN_P_Init_Proc : PN_Init_Node;
      PN_P_New_Proc  : PN_New_Node;
      PN_T_Init_Proc : PN_Init_Node;
      PN_T_New_Proc  : PN_New_Node;
      PN_A_Init_Proc : PN_Init_Arc;
      PN_A_New_Proc  : PN_New_Node);

117 118 119 120
   procedure PN_Build_Port (Aadl_Instance : Ocarina.Types.Node_Id;
                            PN_Generated  : Ocarina.Types.Node_Id;
                            PN_Thread : Ocarina.Types.Node_Id;
                            F : Ocarina.Types.Value_Id);
121

jhugues's avatar
jhugues committed
122
   procedure PN_Build_Tpn_Port
123
     (Aadl_Instance : Node_Id;
124
      PN_Generated  : Ocarina.Types.Node_Id;
125
      Port_Instance : Node_Id;
jhugues's avatar
jhugues committed
126
      PN_Port : Node_Id;
127 128
      Is_Data : Boolean := True);

jhugues's avatar
jhugues committed
129
   procedure PN_Build_Cpn_Port
130
     (Aadl_Instance : Node_Id;
131
      PN_Generated  : Ocarina.Types.Node_Id;
132
      Port_Instance : Node_Id;
jhugues's avatar
jhugues committed
133
      PN_Port : Node_Id;
134 135 136 137 138 139 140
      Is_Data : Boolean := True);

   -----------------------------------
   -- Process_Architecture_Instance --
   -----------------------------------

   function Process_Architecture_Instance
141
     (Architecture_Instance : Ocarina.Types.Node_Id;
142
      F : Unsigned_Long_Long)
143
     return Ocarina.Types.Node_Id
144 145 146 147 148 149 150 151
   is
      use Ocarina.ME_AADL.AADL_Instances.Nodes;
      use Ocarina.Backends.PN.Iutils;
      use Ocarina.ME_AADL.AADL_Instances.Nutils;
      use Ocarina.Aadl_Values;
      use Ocarina.Backends.PN.Nodes;

      Instance : Node_Id;
jhugues's avatar
jhugues committed
152
      PN_Generated : Node_Id;
153 154 155 156 157 158 159 160 161 162 163 164
      Success : Boolean := False;

      --  0 for CPN, 1 for TPN
      Formalism : constant Value_Id := New_Integer_Value (F);
   begin
      if Architecture_Instance = No_Node then
         return No_Node;
      end if;
      --  We get the root system of the AADL architecture
      Instance := Root_System (Architecture_Instance);

      --  initialization of the Petri Net top box
jhugues's avatar
jhugues committed
165 166
      PN_Generated := PN_Get_New_PN_Generated;
      PN_Init_PN_Generated (PN_Generated,
167 168 169 170
                            Formalism);

      declare
         F : constant Value_Type := Get_Value_Type
jhugues's avatar
jhugues committed
171
           (OPN.Formalism (PN_Generated));
172 173 174 175
      begin
         case F.Ival is
            when 0 =>
               --  CPN Pattern
jhugues's avatar
jhugues committed
176 177 178
               Set_PN_Formalism_Specific_Informations
                 (PN_Generated,
                  PN_Get_New_CPN_Informations);
179

jhugues's avatar
jhugues committed
180
               if Instance /= No_Node and then PN_Generated /= No_Node then
181
                  Success := Process_Component_Instance (Instance,
jhugues's avatar
jhugues committed
182
                                                         PN_Generated);
183 184 185 186 187 188
                  if Success then
                     null;
                  end if;
               end if;

               --  assembly and final processing
jhugues's avatar
jhugues committed
189 190 191 192 193 194
               PN_Process_Final_System (PN_Generated,
                                        PN_Cpn_Init_Place'Access,
                                        PN_Cpn_Get_New_Place'Access,
                                        PN_Cpn_Init_Arc'Access,
                                        PN_Cpn_Get_New_Arc'Access,
                                        PN_Cpn_Duplicate_Arc'Access);
195 196 197
            when 1 =>
               --  TPN Pattern
               --  set specific informations
jhugues's avatar
jhugues committed
198 199 200
               Set_PN_Formalism_Specific_Informations
                 (PN_Generated,
                  PN_Get_New_TPN_Informations);
201

jhugues's avatar
jhugues committed
202
               if Instance /= No_Node and then PN_Generated /= No_Node then
203
                  Success := Process_Component_Instance (Instance,
jhugues's avatar
jhugues committed
204
                                                         PN_Generated);
205 206 207 208 209 210
                  if Success then
                     null;
                  end if;
               end if;

               --  assembly and final processing
jhugues's avatar
jhugues committed
211 212 213 214 215 216
               PN_Process_Final_System (PN_Generated,
                                        PN_Tpn_Init_Place'Access,
                                        PN_Tpn_Get_New_Place'Access,
                                        PN_Tpn_Init_Arc'Access,
                                        PN_Tpn_Get_New_Arc'Access,
                                        PN_Tpn_Duplicate_Arc'Access);
217 218 219 220 221
            when others =>
               null;
         end case;
      end;

jhugues's avatar
jhugues committed
222
      return PN_Generated;
223 224 225
   end Process_Architecture_Instance;

   -------------------------------
jhugues's avatar
jhugues committed
226
   --  PN_Process_Final_System  --
227 228
   -------------------------------

jhugues's avatar
jhugues committed
229 230 231 232 233 234 235
   procedure PN_Process_Final_System
     (PN_Generated : Node_Id;
      PN_P_Init_Proc : PN_Init_Node;
      PN_P_New_Proc  : PN_New_Node;
      PN_A_Init_Proc : PN_Init_Arc;
      PN_A_New_Proc  : PN_New_Node;
      PN_Dup_Arc_Proc : PN_Dup_Arc)
236 237 238 239

   is
      use OPN;
      use Ocarina.Backends.PN.Nutils;
240
      use Ocarina.Namet;
241 242 243 244 245 246 247 248 249 250 251 252
      use Ocarina.Backends.Properties;
      use Ocarina.Aadl_Values;
      use Ocarina.Backends.PN.Iutils;
      use Ocarina.Backends.Utils;

      use Ocarina.ME_AADL.AADL_Instances.Entities;
      use Ocarina.ME_AADL;

      Thread_Iter : Node_Id;
   begin

      Thread_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
253 254
        (PN_Subcomponents
         (PN_Box (PN_Generated)));
255 256 257 258 259 260 261

      --  run across subcomponents
      while Present (Thread_Iter) loop

         if OPN.Kind (Thread_Iter) = K_Thread_Pattern then
            --  run across threads

jhugues's avatar
jhugues committed
262
            if OPN.Kind (PN_Formalism_Specific_Informations (PN_Generated))
263 264 265 266
              = K_TPN_Specific_Informations then

               --  manage processor
               declare
jhugues's avatar
jhugues committed
267
                  PN_Proc : Node_Id;
268 269 270 271 272 273 274 275 276
                  Aadl_Proc : constant Node_Id
                    := Get_Bound_Processor (AIN.Corresponding_Instance
                                            (Get_Container_Process
                                             (Th_Instance
                                              (Thread_Iter))));
                  Proc_Prio_Node : Node_Id;
               begin
                  if Aadl_Proc /= No_Node then

jhugues's avatar
jhugues committed
277
                     PN_Proc := Get_Handling (Aadl_Proc,
278 279 280
                                              By_Node,
                                              H_PN_Proc_Creation);

jhugues's avatar
jhugues committed
281
                     if PN_Proc = No_Node then
282 283

                        --  create processor
jhugues's avatar
jhugues committed
284 285
                        PN_Proc := PN_Get_New_Processor_Pattern;
                        PN_Init_Processor_Pattern (PN_Proc,
286 287 288 289 290 291
                                                   Aadl_Proc);

                        --  add place in component
                        declare
                           P_Proc : Node_Id;
                        begin
jhugues's avatar
jhugues committed
292 293
                           P_Proc := PN_Tpn_Get_New_Place;
                           PN_Tpn_Init_Place (P_Proc,
294 295
                                              Aadl_Proc,
                                              Get_String_Name ("_Processor"),
jhugues's avatar
jhugues committed
296
                                              PN_Generated,
297 298 299 300 301 302
                                              0);

                           Set_Tokens_Number (P_Proc,
                                              New_Integer_Value (1));
                           --  add place into component
                           Append_Node_To_List (P_Proc,
jhugues's avatar
jhugues committed
303
                                                Internal_Places (PN_Proc));
304 305 306 307 308
                        end;

                        Set_Handling (Aadl_Proc,
                                      By_Node,
                                      H_PN_Proc_Creation,
jhugues's avatar
jhugues committed
309
                                      PN_Proc);
310 311

                        --  add processor into components list
jhugues's avatar
jhugues committed
312 313 314
                        Append_Node_To_List (PN_Proc,
                                             PN_Subcomponents
                                             (PN_Box (PN_Generated)));
315 316 317 318 319 320 321 322
                     end if;

                     --  here we have the processor component
                     --  run across spgs to update connections

                     --  first, find dedicated entry in tpn_info to update
                     --  priorities
                     if Is_Empty (Priorities
jhugues's avatar
jhugues committed
323 324
                                  (PN_Formalism_Specific_Informations
                                   (PN_Generated))) then
325
                        --  first entry in the list
jhugues's avatar
jhugues committed
326 327
                        Proc_Prio_Node := PN_Get_New_Processor_Priority;
                        PN_Init_Processor_Priority (Proc_Prio_Node,
328 329 330 331
                                                    Aadl_Proc);
                        Append_Node_To_List
                          (Proc_Prio_Node,
                           Priorities
jhugues's avatar
jhugues committed
332 333
                           (PN_Formalism_Specific_Informations
                            (PN_Generated)));
334 335 336 337
                     else
                        --  find it
                        Proc_Prio_Node := OPN.First_Node
                          (Priorities
jhugues's avatar
jhugues committed
338 339
                           (PN_Formalism_Specific_Informations
                            (PN_Generated)));
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
                        while Present (Proc_Prio_Node) loop
                           if P_Instance (Proc_Prio_Node) = Aadl_Proc then
                              --  leave loop
                              exit;
                           end if;
                           --  next
                           Proc_Prio_Node := OPN.Next_Node (Proc_Prio_Node);
                        end loop;
                     end if;
                     --  here we have proc struct
                     --  (needed to print priorities later)

                     if not Is_Empty (Call_Seq (Thread_Iter)) then
                        declare
                           Cs_Iter, Spg_Iter, Trans_Iter : Node_Id;
                           P_Proc : constant Node_Id :=
jhugues's avatar
jhugues committed
356
                             OPN.First_Node (Internal_Places (PN_Proc));
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
                        begin
                           Cs_Iter := OPN.First_Node (Call_Seq (Thread_Iter));
                           while Present (Cs_Iter) loop
                              if not Is_Empty (Spg_Call (Cs_Iter)) then
                                 Spg_Iter := OPN.First_Node
                                   (Spg_Call (Cs_Iter));
                                 while Present (Spg_Iter) loop
                                    --  take processor at begin
                                    --  leave it at preemp1
                                    --  take it again at preemp2
                                    --  leave it at end

                                    Trans_Iter := OPN.First_Node
                                      (Public_Interfaces (Spg_Iter));
                                    declare
                                       New_Arc : Node_Id;
                                    begin
jhugues's avatar
jhugues committed
374 375
                                       New_Arc := PN_A_New_Proc.all;
                                       PN_A_Init_Proc (New_Arc,
376 377 378 379 380 381 382 383
                                                       Th_Instance
                                                       (Thread_Iter),
                                                       P_Proc,
                                                       Trans_Iter,
                                                       0);

                                       Append_Node_To_List
                                         (New_Arc,
jhugues's avatar
jhugues committed
384
                                          PN_Arcs_In (Trans_Iter));
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
                                    end;
                                    --  since it is begin,
                                    --  its priority needs to be
                                    --  recorded
                                    Set_Priority (Trans_Iter,
                                                  New_Integer_Value
                                                  (Get_Thread_Priority
                                                   (Th_Instance
                                                    (Thread_Iter))));
                                    Append_Trans_To_Processor (Trans_Iter,
                                                               Proc_Prio_Node);
                                    -----
                                    Trans_Iter := OPN.Next_Node (Trans_Iter);
                                    declare
                                       New_Arc : Node_Id;
                                    begin
jhugues's avatar
jhugues committed
401 402
                                       New_Arc := PN_A_New_Proc.all;
                                       PN_A_Init_Proc (New_Arc,
403 404 405 406 407 408 409 410
                                                       Th_Instance
                                                       (Thread_Iter),
                                                       Trans_Iter,
                                                       P_Proc,
                                                       0);

                                       Append_Node_To_List
                                         (New_Arc,
jhugues's avatar
jhugues committed
411
                                          PN_Arcs_Out (Trans_Iter));
412 413 414 415 416 417 418 419 420
                                    end;

                                    --  handle context switch
                                    Trans_Iter := OPN.First_Node
                                      (Internal_Transitions
                                       (Spg_Iter));
                                    declare
                                       New_Arc : Node_Id;
                                    begin
jhugues's avatar
jhugues committed
421 422
                                       New_Arc := PN_A_New_Proc.all;
                                       PN_A_Init_Proc (New_Arc,
423 424 425 426 427 428 429 430
                                                       Th_Instance
                                                       (Thread_Iter),
                                                       Trans_Iter,
                                                       P_Proc,
                                                       0);

                                       Append_Node_To_List
                                         (New_Arc,
jhugues's avatar
jhugues committed
431
                                          PN_Arcs_Out (Trans_Iter));
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
                                    end;
                                    --  since it is preemp1, its priority needs
                                    --  to be recorded
                                    Set_Priority (Trans_Iter,
                                                  New_Integer_Value
                                                  (Get_Thread_Priority
                                                   (Th_Instance
                                                    (Thread_Iter))));
                                    Append_Trans_To_Processor (Trans_Iter,
                                                               Proc_Prio_Node);
                                    -----
                                    Trans_Iter := OPN.Next_Node (Trans_Iter);
                                    declare
                                       New_Arc : Node_Id;
                                    begin
jhugues's avatar
jhugues committed
447 448
                                       New_Arc := PN_A_New_Proc.all;
                                       PN_A_Init_Proc (New_Arc,
449 450 451 452 453 454 455 456
                                                       Th_Instance
                                                       (Thread_Iter),
                                                       P_Proc,
                                                       Trans_Iter,
                                                       0);

                                       Append_Node_To_List
                                         (New_Arc,
jhugues's avatar
jhugues committed
457
                                          PN_Arcs_In (Trans_Iter));
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
                                    end;

                                    --  next
                                    Spg_Iter := OPN.Next_Node (Spg_Iter);
                                 end loop;    --  spg
                              end if;         --  has spg

                              --  next
                              Cs_Iter := OPN.Next_Node (Cs_Iter);
                           end loop;          --  cs
                        end;
                     end if;                  --  has call seq
                  end if;                        --  have processor properties
               end;
            end if;                     --  tpn for processors

jhugues's avatar
jhugues committed
474
            if OPN.Kind (PN_Formalism_Specific_Informations (PN_Generated))
475 476 477 478 479
              = K_TPN_Specific_Informations then
               --  TPN
               --  set hyperperiod
               Set_Hyperperiod (Thread_Iter,
                                Hyperperiod
jhugues's avatar
jhugues committed
480 481
                                (PN_Formalism_Specific_Informations
                                 (PN_Generated)));
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

               --  set hyperperiod values to hyperperiod place
               declare
                  Node_Iter : Node_Id;
               begin
                  if not Is_Empty (Internal_Places (Thread_Iter)) then
                     Node_Iter := OPN.First_Node
                       (Internal_Places (Thread_Iter));
                     while Present (Node_Iter) loop

                        declare
                           S : constant String :=
                             Get_Name_String (Name (Identifier (Node_Iter)));
                           Token_Nb : Unsigned_Long_Long;
                           Hyper_Th : Value_Type;
                           Time_Th : Time_Type;
                        begin
                           if S (S'Last - 10 .. S'Last) = "Hyperperiod" then
                              if Get_Thread_Dispatch_Protocol
                                (Th_Instance (Thread_Iter)) = Thread_Periodic
                                or else
                                 Get_Thread_Dispatch_Protocol
                                (Th_Instance (Thread_Iter)) = Thread_Sporadic
                              then
                                 Time_Th := Get_Thread_Period
                                   (Th_Instance (Thread_Iter));
                                 Hyper_Th := Get_Value_Type
                                   (Hyperperiod (Thread_Iter));
                                 Token_Nb :=
                                   Hyper_Th.Ival / Time_Th.T;

                                 Set_Tokens_Number (Node_Iter,
                                                    New_Integer_Value
                                                    (Token_Nb));
                              end if;
                           end if;
                        end;

                        Node_Iter := OPN.Next_Node (Node_Iter);
                     end loop;
                  end if;               --  internal places (thread)
               end;
            end if;                     --  TPN treatments (hyperperiod)

            --  COMMON PART
            --  manage call sequences
            if Get_Thread_Implementation_Kind
              (Th_Instance (Thread_Iter))
              = Thread_With_Call_Sequence then

               --  manage port's pop / in param fusion
               declare
                  Beg_Node : Node_Id;   --  start node for CS
                  Pop_Node : Node_Id;   --  pop node for in port
                  Cs_Iter, Spg_Iter, Param_Iter : Node_Id;
               begin
                  Cs_Iter := OPN.First_Node (Call_Seq (Thread_Iter));
                  while Present (Cs_Iter) loop
                     --  run across CS
                     Spg_Iter := OPN.First_Node (Spg_Call (Cs_Iter));
                     while Present (Spg_Iter) loop
                        --  run across CS's Spg
                        Beg_Node := OPN.First_Node (Public_Interfaces
                                                    (Spg_Iter));

                        if not Is_Empty (Param_In (Spg_Iter)) then
                           Param_Iter := OPN.First_Node (Param_In (Spg_Iter));
                           while Present (Param_Iter) loop
                              --  run across CS's Spg's In param

                              --  find dedicated in port
                              declare
                                 Port_Iter : Node_Id;
                              begin
                                 if not Is_Empty (In_Ports (Thread_Iter)) then
                                    Port_Iter := OPN.First_Node
                                      (In_Ports (Thread_Iter));

                                    while Present (Port_Iter) loop
                                       --  run across in ports

                                       if not Is_Empty
                                         (Public_Interfaces (Port_Iter)) then
                                          Pop_Node := OPN.Next_Node
                                            (OPN.First_Node (Public_Interfaces
                                                             (Port_Iter)));

                                          if Port_Instance (Port_Iter) =
                                            Par_Instance (Param_Iter) then
                                             --  match
                                             --  update arcs to
                                             --  delete port's pop
                                             declare
                                                Arc_Iter : Node_Id;
                                             begin
                                                --  look at in arcs
                                                Arc_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
579
                                                  (PN_Arcs_In (Pop_Node));
580 581
                                                while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
582
                                                   PN_Dup_Arc_Proc.all
583 584 585 586 587 588 589 590 591 592 593 594 595
                                                     (Arc_Iter,
                                                      Th_Instance
                                                      (Thread_Iter),
                                                      Beg_Node,
                                                      False);

                                                   --  next
                                                   Arc_Iter := OPN.Next_Node
                                                     (Arc_Iter);
                                                end loop;

                                                --  look at out arcs
                                                Arc_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
596
                                                  (PN_Arcs_Out (Pop_Node));
597 598
                                                while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
599
                                                   PN_Dup_Arc_Proc.all
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
                                                     (Arc_Iter,
                                                      Th_Instance
                                                      (Thread_Iter),
                                                      Beg_Node,
                                                      True);

                                                   Arc_Iter := OPN.Next_Node
                                                     (Arc_Iter);
                                                end loop;
                                             end;
                                             --  record where is
                                             --  pop interface now
                                             --  by_node cause
                                             --  name could fail...
                                             Set_Handling (Port_Iter,
                                                           By_Node,
                                                           H_PN_Port_Creation,
                                                           Beg_Node);

                                             --  delete pop Node
                                             Delete_Node_From_List
                                               (Pop_Node,
                                                Public_Interfaces (Port_Iter));

                                          end if; --  match port / in param

                                       end if;
                                       --  next
                                       Port_Iter := OPN.Next_Node (Port_Iter);
                                    end loop;

                                 end if; --  have in ports

                              end;

                              --  next
                              Param_Iter := OPN.Next_Node (Param_Iter);
                           end loop;
                        end if;         --  have in param
                        --  next
                        Spg_Iter := OPN.Next_Node (Spg_Iter);
                     end loop;

                     --  next
                     Cs_Iter := OPN.Next_Node (Cs_Iter);
                  end loop;
               end;

               --  manage port's push / out param fusion
               declare
                  End_Node : Node_Id;   --  start node for CS
                  Push_Node : Node_Id;   --  push node for out port
                  Cs_Iter, Spg_Iter, Param_Iter : Node_Id;
               begin
                  Cs_Iter := OPN.First_Node (Call_Seq (Thread_Iter));
                  while Present (Cs_Iter) loop
                     --  run across CS
                     Spg_Iter := OPN.First_Node (Spg_Call (Cs_Iter));
                     while Present (Spg_Iter) loop
                        --  run across CS's Spg
                        End_Node := OPN.Next_Node
                          (OPN.First_Node (Public_Interfaces (Spg_Iter)));

                        if not Is_Empty (Param_Out (Spg_Iter)) then
                           Param_Iter := OPN.First_Node (Param_Out (Spg_Iter));
                           while Present (Param_Iter) loop
                              --  run across CS's Spg's Out param

                              --  find dedicated in port
                              declare
                                 Port_Iter : Node_Id;
                              begin
                                 if not Is_Empty (Out_Ports (Thread_Iter)) then
                                    Port_Iter := OPN.First_Node
                                      (Out_Ports (Thread_Iter));

                                    while Present (Port_Iter) loop
                                       --  run across in ports

                                       Push_Node := OPN.First_Node
                                         (Public_Interfaces (Port_Iter));
                                       if Push_Node /= No_Node then
                                          if Port_Instance (Port_Iter) =
                                            Par_Instance (Param_Iter) then
                                             --  match
                                             --  update arcs to
                                             --  delete port's pop
                                             declare
                                                Arc_Iter : Node_Id;
                                             begin
                                                --  look at in arcs
                                                Arc_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
692
                                                  (PN_Arcs_In (Push_Node));
693 694
                                                while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
695
                                                   PN_Dup_Arc_Proc.all
696 697 698 699 700 701 702 703 704 705 706 707 708
                                                     (Arc_Iter,
                                                      Th_Instance
                                                      (Thread_Iter),
                                                      End_Node,
                                                      False);

                                                   --  next
                                                   Arc_Iter := OPN.Next_Node
                                                     (Arc_Iter);
                                                end loop;

                                                --  look at out arcs
                                                Arc_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
709
                                                  (PN_Arcs_Out (Push_Node));
710 711
                                                while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
712
                                                   PN_Dup_Arc_Proc.all
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
                                                     (Arc_Iter,
                                                      Th_Instance
                                                      (Thread_Iter),
                                                      End_Node,
                                                      True);

                                                   Arc_Iter := OPN.Next_Node
                                                     (Arc_Iter);
                                                end loop;
                                             end;
                                             --  record where is
                                             --  push interface now
                                             --  by_node cause name
                                             --  could fail...
                                             if OPN.Next_Node (Spg_Iter)
                                               /= No_Node then
                                                --  push = next (begin node)
                                                --  since end will be deleted
                                                Set_Handling
                                                  (Port_Iter,
                                                   By_Node,
                                                   H_PN_Port_Creation,
                                                   OPN.First_Node
                                                   (Public_Interfaces
                                                    (OPN.Next_Node
                                                     (Spg_Iter))));
                                             else
                                                --  push = end node
                                                --  (last spg, no
                                                --  deletion)
                                                Set_Handling
                                                  (Port_Iter,
                                                   By_Node,
                                                   H_PN_Port_Creation,
                                                   End_Node);
                                             end if;

                                             --  delete push Node
                                             Delete_Node_From_List
                                               (Push_Node,
                                                Public_Interfaces (Port_Iter));

                                          end if; --  match port / out param
                                       end if;
                                       --  next
                                       Port_Iter := OPN.Next_Node (Port_Iter);
                                    end loop;

                                 end if; --  have out ports

                              end;

                              --  next
                              Param_Iter := OPN.Next_Node (Param_Iter);
                           end loop;
                        end if;         --  have out param
                        --  next
                        Spg_Iter := OPN.Next_Node (Spg_Iter);
                     end loop;

                     --  next
                     Cs_Iter := OPN.Next_Node (Cs_Iter);
                  end loop;
               end;

               --  manage CS steps fusion (end, begin (next) )
               declare
                  Cs_Iter, Spg_Iter, Spg_Next_Iter : Node_Id;
                  End_Node, Begin_Next_Node : Node_Id;
               begin
                  Cs_Iter := OPN.First_Node (Call_Seq (Thread_Iter));
                  while Present (Cs_Iter) loop
                     --  run across CS
                     Spg_Iter := OPN.First_Node (Spg_Call (Cs_Iter));
                     while Present (Spg_Iter)
                       and then Present (OPN.Next_Node (Spg_Iter)) loop
                        --  run across CS's Spg
                        Spg_Next_Iter := OPN.Next_Node (Spg_Iter);
                        End_Node := OPN.Next_Node
                          (OPN.First_Node (Public_Interfaces (Spg_Iter)));
                        Begin_Next_Node := OPN.First_Node (Public_Interfaces
                                                           (Spg_Next_Iter));

                        --  update arcs to delete spg's "end_node"
                        declare
                           Arc_Iter : Node_Id;
                        begin
                           --  look at in arcs
jhugues's avatar
jhugues committed
801
                           Arc_Iter := OPN.First_Node (PN_Arcs_In (End_Node));
802 803
                           while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
804
                              PN_Dup_Arc_Proc.all
805 806 807 808 809 810 811 812 813 814
                                (Arc_Iter,
                                 Th_Instance (Thread_Iter),
                                 Begin_Next_Node,
                                 False);

                              --  next
                              Arc_Iter := OPN.Next_Node (Arc_Iter);
                           end loop;

                           --  look at out arcs
jhugues's avatar
jhugues committed
815
                           Arc_Iter := OPN.First_Node (PN_Arcs_Out (End_Node));
816 817
                           while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
818
                              PN_Dup_Arc_Proc.all
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
                                (Arc_Iter,
                                 Th_Instance (Thread_Iter),
                                 Begin_Next_Node,
                                 True);

                              --  next
                              Arc_Iter := OPN.Next_Node (Arc_Iter);
                           end loop;
                        end;
                        --  delete end_node
                        Delete_Node_From_List (End_Node,
                                               Public_Interfaces (Spg_Iter));

                        --  next
                        Spg_Iter := OPN.Next_Node (Spg_Iter);
                     end loop;

                     --  next
                     Cs_Iter := OPN.Next_Node (Cs_Iter);
                  end loop;
               end;

            end if;                     --  thread with CS kind

            --  here, we have compact compute box
            --  manage dispatch

            if Get_Thread_Dispatch_Protocol
              (Th_Instance (Thread_Iter)) = Thread_Aperiodic
              or else
              Get_Thread_Dispatch_Protocol
              (Th_Instance (Thread_Iter)) = Thread_Sporadic then

               declare
                  Wait_Node : Node_Id;
                  Port_Iter : Node_Id;
                  Pop_Node : Node_Id;
               begin
                  --  connect wait_dispatch to each port's pop interface
                  --  since any port could trigger the dispatch
                  --  (in case of port_compute_entrypoint)
                  if not Is_Empty (In_Ports (Thread_Iter)) then
                     Port_Iter := OPN.First_Node (In_Ports (Thread_Iter));

                     Wait_Node := OPN.Next_Node (First_Node (Internal_Places
                                                             (Thread_Iter)));

                     while Present (Port_Iter) loop
                        --  run across in ports
                        Pop_Node := Get_Handling (Port_Iter,
                                                  By_Node,
                                                  H_PN_Port_Creation);
                        if Pop_Node = No_Node then
                           --  this port have not been merged into a param
                           Pop_Node := OPN.Next_Node (OPN.First_Node
                                                      (Public_Interfaces
                                                       (Port_Iter)));
                        end if;

                        --  here, we have the port's pop interface
                        declare
                           New_Arc : Node_Id;
                        begin
jhugues's avatar
jhugues committed
882 883
                           New_Arc := PN_A_New_Proc.all;
                           PN_A_Init_Proc (New_Arc,
884 885 886 887 888 889
                                           Th_Instance (Thread_Iter),
                                           Wait_Node,
                                           Pop_Node,
                                           1);

                           Append_Node_To_List (New_Arc,
jhugues's avatar
jhugues committed
890
                                                PN_Arcs_In
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
                                                (Pop_Node));
                        end;

                        --  next
                        Port_Iter := OPN.Next_Node (Port_Iter);
                     end loop;
                  end if;
               end;

            end if;                     --  aperiodic or sporadic thread

            --  manage clock for periodic or sporadic

            if Get_Thread_Dispatch_Protocol
              (Th_Instance (Thread_Iter)) = Thread_Periodic then
               --  manage periodic
               declare
jhugues's avatar
jhugues committed
908
                  Beg_Node : Node_Id := No_Node;
909 910 911 912 913 914 915 916 917 918 919 920
                  Wait_Node : Node_Id;
                  Clock_Node : Node_Id;
                  Node_Iter : Node_Id;
               begin
                  --  connect wait_dispatch to first call sequence
                  --  first, find wait node
                  Wait_Node := OPN.Next_Node (First_Node (Internal_Places
                                                          (Thread_Iter)));

                  --  find clock place
                  if not Is_Empty (Internal_Places (Thread_Iter)) then

jhugues's avatar
jhugues committed
921 922
                     if OPN.Kind (PN_Formalism_Specific_Informations
                                  (PN_Generated))
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
                       = K_TPN_Specific_Informations then

                        Node_Iter := OPN.First_Node
                          (Internal_Places (Thread_Iter));
                        while Present (Node_Iter) loop

                           declare
                              S : constant String :=
                                Get_Name_String (Name
                                                 (Identifier (Node_Iter)));
                           begin
                              if S (S'Last - 4 .. S'Last) = "Clock" then
                                 Clock_Node := Node_Iter;
                              end if;
                           end;

                           Node_Iter := OPN.Next_Node (Node_Iter);
                        end loop;

                     end if;

                     --  find begin_node
                     if not Is_Empty (Call_Seq (Thread_Iter)) then

                        Node_Iter := OPN.First_Node (Call_Seq
                                                     (Thread_Iter));
                        if not Is_Empty (Spg_Call (Node_Iter)) then

                           Node_Iter := OPN.First_Node (Spg_Call
                                                        (Node_Iter));
                           if not Is_Empty (Public_Interfaces
                                            (Node_Iter)) then

                              Beg_Node := OPN.First_Node (Public_Interfaces
                                                          (Node_Iter));
                           end if;
                        end if;

                     end if;

                     --  add new arc for clock
jhugues's avatar
jhugues committed
964 965 966 967 968
                     if Present (Beg_Node)
                       and then OPN.Kind (PN_Formalism_Specific_Informations
                                            (PN_Generated))
                       = K_TPN_Specific_Informations
                     then
969 970 971
                        declare
                           New_Arc : Node_Id;
                        begin
jhugues's avatar
jhugues committed
972 973
                           New_Arc := PN_Tpn_Get_New_Arc;
                           PN_Tpn_Init_Arc (New_Arc,
974 975 976 977 978
                                            Th_Instance (Thread_Iter),
                                            Clock_Node,
                                            Beg_Node,
                                            0);
                           Append_Node_To_List (New_Arc,
jhugues's avatar
jhugues committed
979
                                                PN_Arcs_In (Beg_Node));
980 981 982 983
                        end;
                     end if;

                     --  add new arc for wait node
jhugues's avatar
jhugues committed
984 985 986 987 988 989 990 991 992 993
                     if Present (Beg_Node) then
                        declare
                           New_Arc : Node_Id;
                        begin
                           New_Arc := PN_A_New_Proc.all;
                           PN_A_Init_Proc (New_Arc,
                                           Th_Instance (Thread_Iter),
                                           Wait_Node,
                                           Beg_Node,
                                           1);
994

jhugues's avatar
jhugues committed
995 996 997 998 999
                           Append_Node_To_List (New_Arc,
                                                PN_Arcs_In (Beg_Node));
                        end;
                     end if;               --  thread internal places for clock
                  end if;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
               end;
            end if;                     --  periodic thread clock

            --  sporadic thread clock
            if Get_Thread_Dispatch_Protocol
              (Th_Instance (Thread_Iter)) = Thread_Sporadic then
               --  manage sporadic
               declare
                  Clock_Node : Node_Id;
                  Node_Iter : Node_Id;
               begin
                  --  find clock place
                  if not Is_Empty (Internal_Places (Thread_Iter)) then
jhugues's avatar
jhugues committed
1013 1014
                     if OPN.Kind (PN_Formalism_Specific_Informations
                                  (PN_Generated))
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
                       = K_TPN_Specific_Informations then

                        Node_Iter := OPN.First_Node
                          (Internal_Places (Thread_Iter));
                        while Present (Node_Iter) loop

                           declare
                              S : constant String :=
                                Get_Name_String (Name (Identifier
                                                       (Node_Iter)));
                           begin
                              if S (S'Last - 4 .. S'Last) = "Clock" then
                                 Clock_Node := Node_Iter;
                              end if;
                           end;

                           Node_Iter := OPN.Next_Node (Node_Iter);
                        end loop;

                        declare
                           Port_Iter : Node_Id;
                           Pop_Node : Node_Id;
                        begin
                           --  connect clock to each port's pop interface
                           --  since any port could trigger the dispatch
                           if not Is_Empty (In_Ports (Thread_Iter)) then
                              Port_Iter := OPN.First_Node
                                (In_Ports (Thread_Iter));

                              while Present (Port_Iter) loop
                                 --  run across in ports
                                 Pop_Node := Get_Handling (Port_Iter,
                                                           By_Node,
                                                           H_PN_Port_Creation);
                                 if Pop_Node = No_Node then
                                    --  this port have not been merged
                                    --  into a param
                                    Pop_Node := OPN.Next_Node
                                      (OPN.First_Node
                                       (Public_Interfaces (Port_Iter)));
                                 end if;

                                 --  here, we have the port's pop interface
                                 declare
                                    New_Arc : Node_Id;
                                 begin
jhugues's avatar
jhugues committed
1061 1062
                                    New_Arc := PN_Tpn_Get_New_Arc;
                                    PN_Tpn_Init_Arc (New_Arc,
1063 1064 1065 1066 1067 1068 1069
                                                     Th_Instance (Thread_Iter),
                                                     Clock_Node,
                                                     Pop_Node,
                                                     0);

                                    Append_Node_To_List
                                      (New_Arc,
jhugues's avatar
jhugues committed
1070
                                       PN_Arcs_In (Pop_Node));
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
                                 end;

                                 --  next
                                 Port_Iter := OPN.Next_Node (Port_Iter);
                              end loop;
                           end if;
                        end;
                     end if;
                  end if;               --  find clock place
               end;
            end if;                     --  sporadic thread clock

            --  delete _compute trans from public interfaces of thread
            --  it is now useless

            Delete_Node_From_List (OPN.First_Node (Public_Interfaces
                                                   (Thread_Iter)),
                                   Public_Interfaces (Thread_Iter));

            --  now, _complete is the first node of public_interfaces

            --  manage thread interconnection adding place in pn_box
            --  here, for out ports only, and for evrey threads,
            --  we add an extra
            --  place in pn_interconnections, and connect it to pop's interface
            --  a second loop will be necessary (through in ports) to connect
            --  to in ports
            declare
               Port_Iter : Node_Id;
1100
               Interconnection_Node : Ocarina.Types.Node_Id;
1101 1102 1103 1104 1105 1106 1107
               Pop_Node : Node_Id;
            begin
               if not Is_Empty (Out_Ports (Thread_Iter)) then
                  Port_Iter := OPN.First_Node (Out_Ports (Thread_Iter));

                  while Present (Port_Iter) loop
                     --  create new node
jhugues's avatar
jhugues committed
1108 1109
                     Interconnection_Node := PN_P_New_Proc.all;
                     PN_P_Init_Proc (Interconnection_Node,
1110 1111
                                     Port_Instance (Port_Iter),
                                     Get_String_Name ("_Bus"),
jhugues's avatar
jhugues committed
1112
                                     PN_Generated,
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
                                     0);

                     --  set handling to the target port instance
                     --  for next loop
                     Set_Handling (Interconnection_Node,
                                   By_Node,
                                   H_PN_Port_Creation,
                                   Target_Instance (Port_Iter));

                     --  add place to pn_box
                     Append_Node_To_List (Interconnection_Node,
jhugues's avatar
jhugues committed
1124 1125
                                          PN_Interconnections
                                          (PN_Box (PN_Generated)));
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

                     --  find pop's interface
                     if Get_Handling (Port_Iter,
                                      By_Node,
                                      H_PN_Port_Creation) /= No_Node then
                        --  this port have been merged into a param
                        Pop_Node := OPN.First_Node
                          (Public_Interfaces (Port_Iter));

                        --  CPN specific
                        --  set handling to the source port push
                        --  for next loop
                        Set_Handling (Interconnection_Node,
                                      By_Node,
                                      H_PN_Interconnection,
                                      Get_Handling (Port_Iter,
                                                    By_Node,
                                                    H_PN_Port_Creation));
                        --  end CPN specific
                     else
                        --  still two interfaces
                        Pop_Node := OPN.Next_Node (OPN.First_Node
                                                   (Public_Interfaces
                                                    (Port_Iter)));
                        --  CPN specific
                        --  set handling to the source port push
                        --  for next loop
                        Set_Handling (Interconnection_Node,
                                      By_Node,
                                      H_PN_Interconnection,
                                      OPN.First_Node (Public_Interfaces
                                                      (Port_Iter)));
                        --  end CPN specific
                     end if;

                     --  create arc
jhugues's avatar
jhugues committed
1162 1163
                     if OPN.Kind (PN_Formalism_Specific_Informations
                                  (PN_Generated))
1164 1165 1166 1167
                       = K_TPN_Specific_Informations then
                        declare
                           New_Arc : Node_Id;
                        begin
jhugues's avatar
jhugues committed
1168 1169
                           New_Arc := PN_A_New_Proc.all;
                           PN_A_Init_Proc (New_Arc,
1170 1171 1172 1173 1174 1175
                                           Th_Instance (Thread_Iter),
                                           Pop_Node,
                                           Interconnection_Node,
                                           2);

                           Append_Node_To_List (New_Arc,
jhugues's avatar
jhugues committed
1176
                                                PN_Arcs_Out (Pop_Node));
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
                        end;
                     else
                        --  delete Pop
                        Delete_Node_From_List (Pop_Node,
                                               Public_Interfaces (Port_Iter));
                     end if;

                     --  next
                     Port_Iter := OPN.Next_Node (Port_Iter);
                  end loop;             --  out ports loop
               end if;                  --  have out ports
            end;                        --  run across out ports

            --  last step
            --  connect each CS "end_node" to wait_dispatch
            --  and then delete complete transition

            declare
               Complete_Node : Node_Id;
               Wait_Node : Node_Id;
               Cs_Iter, Spg_Iter : Node_Id;
               End_Node : Node_Id;
            begin
               --  find wait_node
               Wait_Node := OPN.Next_Node (OPN.First_Node (Internal_Places
                                                           (Thread_Iter)));

               --  find complete node
               --  since we already have deleted compute_trans
               --  complete_trans is first node of public_interface
               Complete_Node := OPN.First_Node (Public_Interfaces
                                                (Thread_Iter));

               if not Is_Empty (Call_Seq (Thread_Iter)) then
                  Cs_Iter := OPN.First_Node (Call_Seq (Thread_Iter));

                  while Present (Cs_Iter) loop

                     if not Is_Empty (Spg_Call (Cs_Iter)) then
                        --  last spg call
                        Spg_Iter := OPN.Last_Node (Spg_Call (Cs_Iter));
                        --  end node is last public interface of spg call
                        if not Is_Empty (Public_Interfaces (Spg_Iter)) then
                           End_Node := OPN.Last_Node (Public_Interfaces
                                                      (Spg_Iter));

                           --  make connection
                           declare
                              New_Arc : Node_Id;
                           begin
jhugues's avatar
jhugues committed
1227 1228
                              New_Arc := PN_A_New_Proc.all;
                              PN_A_Init_Proc (New_Arc,
1229 1230 1231 1232 1233 1234
                                              Th_Instance (Thread_Iter),
                                              End_Node,
                                              Wait_Node,
                                              1);

                              Append_Node_To_List (New_Arc,
jhugues's avatar
jhugues committed
1235
                                                   PN_Arcs_Out (End_Node));
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
                           end;

                        end if;         --  public interface
                     end if;            --  spg call

                     --  next
                     Cs_Iter := OPN.Next_Node (Cs_Iter);
                  end loop;          --  for each CS
               end if;                  --  have cs

               --  delete complete_node
               Delete_Node_From_List (Complete_Node,
                                      Public_Interfaces (Thread_Iter));
            end;

         end if;                        --  thread subcomponent

         --  next
         Thread_Iter := OPN.Next_Node (Thread_Iter);
      end loop;                         --  next subcomponent

      -----------
      --  next loop through in ports to connect to interconnection
      --  used to merge all init_dispatch too
      Thread_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
1261 1262
        (PN_Subcomponents
         (PN_Box (PN_Generated)));
1263

1264 1265 1266 1267 1268
      if No (Thread_Iter) then
         Display_Error ("Petri Net backend : "
                          & "incomplete AADL model", True);
      end if;

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
      declare
         Init_Node : constant Node_Id
           := OPN.First_Node (Internal_Transitions (Thread_Iter));
      begin
         --  run across subcomponents
         while Present (Thread_Iter) loop

            if OPN.Kind (Thread_Iter) = K_Thread_Pattern then

               --  merge init
               if OPN.First_Node (Internal_Transitions (Thread_Iter))
                 /= Init_Node then
                  --  copy all in and out arcs
                  declare
                     Arc_Iter : Node_Id;
                     Current_Init : constant Node_Id
                       := OPN.First_Node (Internal_Transitions (Thread_Iter));
                  begin
                     --  look at in arcs
                     Arc_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
1289
                       (PN_Arcs_In (Current_Init));
1290 1291
                     while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
1292
                        PN_Dup_Arc_Proc.all
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
                          (Arc_Iter,
                           Th_Instance (Thread_Iter),
                           Init_Node,
                           False);

                        --  next
                        Arc_Iter := OPN.Next_Node (Arc_Iter);
                     end loop;

                     --  look at out arcs
jhugues's avatar
jhugues committed
1303
                     Arc_Iter := OPN.First_Node (PN_Arcs_Out (Current_Init));
1304 1305
                     while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
1306
                        PN_Dup_Arc_Proc.all
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
                          (Arc_Iter,
                           Th_Instance (Thread_Iter),
                           Init_Node,
                           True);

                        --  next
                        Arc_Iter := OPN.Next_Node (Arc_Iter);
                     end loop;

                     --  delete current init transition
                     Delete_Node_From_List (Current_Init,
                                            Internal_Transitions
                                            (Thread_Iter));
                  end;
               end if;                     --  update init
            end if;                     --  thread kind

            if OPN.Kind (Thread_Iter) = K_Thread_Pattern then
               --  run across threads

               declare
                  Port_Iter, Inter_Iter : Node_Id;
                  Interconnection_Node : Node_Id := No_Node;
                  Push_Node : Node_Id;
                  Handling_Node : Node_Id;
               begin
                  if not Is_Empty (In_Ports (Thread_Iter)) then
                     Port_Iter := OPN.First_Node (In_Ports (Thread_Iter));

                     while Present (Port_Iter) loop
                        --  run through in ports

                        --  find interconnection place
jhugues's avatar
jhugues committed
1340 1341
                        if not Is_Empty (PN_Interconnections
                                         (PN_Box (PN_Generated))) then
1342
                           Inter_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
1343
                             (PN_Interconnections (PN_Box (PN_Generated)));
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368

                           while Present (Inter_Iter) loop

                              Handling_Node := Get_Handling
                                (Inter_Iter,
                                 By_Node,
                                 H_PN_Port_Creation);

                              if Handling_Node = Port_Instance (Port_Iter) then
                                 --  match : pin port = target_port (inter)
                                 Interconnection_Node := Inter_Iter;
                                 --  exit;
                              end if;

                              --  next
                              Inter_Iter := OPN.Next_Node (Inter_Iter);
                           end loop;
                        end if;

                        --  find push node
                        if not Is_Empty (Public_Interfaces (Port_Iter)) then
                           Push_Node := OPN.First_Node (Public_Interfaces
                                                        (Port_Iter));
                        end if;

jhugues's avatar
jhugues committed
1369 1370
                        if OPN.Kind (PN_Formalism_Specific_Informations
                                     (PN_Generated))
1371 1372 1373 1374 1375 1376
                          = K_TPN_Specific_Informations then

                           --  create arc
                           declare
                              New_Arc : Node_Id;
                           begin
jhugues's avatar
jhugues committed
1377 1378
                              New_Arc := PN_A_New_Proc.all;
                              PN_A_Init_Proc (New_Arc,
1379 1380 1381 1382 1383 1384
                                              Th_Instance (Thread_Iter),
                                              Interconnection_Node,
                                              Push_Node,
                                              3);

                              Append_Node_To_List (New_Arc,
jhugues's avatar
jhugues committed
1385
                                                   PN_Arcs_In (Push_Node));
1386 1387 1388 1389

                           end;
                        end if;

jhugues's avatar
jhugues committed
1390 1391
                        if OPN.Kind (PN_Formalism_Specific_Informations
                                     (PN_Generated))
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
                          = K_CPN_Specific_Informations then
                           --  find ovf node
                           --  and connect to interconnection_node
                           declare
                              Ovf_Node : constant Node_Id
                                := OPN.Next_Node (Push_Node);
                              --  only one next
                              --  because pop has
                              --  been deleted
                              Push_Source : Node_Id;
                           begin
                              --  get push from source port
                              --  merge it with both push and ovf
                              --  delete interconnection_node
                              Push_Source := Get_Handling
                                (Interconnection_Node,
                                 By_Node,
                                 H_PN_Interconnection);
                              if Push_Source /= No_Node then
                                 declare
                                    Arc_Iter : Node_Id;
                                 begin
                                    --  look at in arcs
                                    Arc_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
1416
                                      (PN_Arcs_In (Push_Source));
1417 1418
                                    while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
1419
                                       PN_Dup_Arc_Proc.all
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
                                         (Arc_Iter,
                                          Port_Instance (Port_Iter),
                                          Push_Node,
                                          False);

                                       --  next
                                       Arc_Iter := OPN.Next_Node (Arc_Iter);
                                    end loop;

                                    --  look at out arcs
                                    Arc_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
1431
                                      (PN_Arcs_Out (Push_Source));
1432 1433
                                    while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
1434
                                       PN_Dup_Arc_Proc.all
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
                                         (Arc_Iter,
                                          Port_Instance (Port_Iter),
                                          Push_Node,
                                          True);

                                       --  next
                                       Arc_Iter := OPN.Next_Node (Arc_Iter);
                                    end loop;
                                 end;
                                 --  same for ovf
                                 if Ovf_Node /= No_Node then
                                    declare
                                       Arc_Iter : Node_Id;
                                    begin
                                       --  look at in arcs
                                       Arc_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
1451
                                         (PN_Arcs_In (Push_Source));
1452 1453
                                       while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
1454
                                          PN_Dup_Arc_Proc.all
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
                                            (Arc_Iter,
                                             Port_Instance (Port_Iter),
                                             Ovf_Node,
                                             False);

                                          --  next
                                          Arc_Iter := OPN.Next_Node (Arc_Iter);
                                       end loop;

                                       --  look at out arcs
                                       Arc_Iter := OPN.First_Node
jhugues's avatar
jhugues committed
1466
                                         (PN_Arcs_Out (Push_Source));
1467 1468
                                       while Present (Arc_Iter) loop

jhugues's avatar
jhugues committed
1469
                                          PN_Dup_Arc_Proc.all
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
                                            (Arc_Iter,
                                             Port_Instance (Port_Iter),
                                             Ovf_Node,
                                             True);

                                          --  next
                                          Arc_Iter := OPN.Next_Node (Arc_Iter);
                                       end loop;
                                    end;
                                 end if;
                              end if;
                              --  delete interconnection_node
                              Delete_Node_From_List (Interconnection_Node,
jhugues's avatar
jhugues committed
1483 1484 1485
                                                     PN_Interconnections
                                                     (PN_Box
                                                      (PN_Generated)));
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
                              --  delete push source
                              --  too complicated
                              Set_Handling (Push_Source,
                                            By_Node,
                                            H_PN_To_Delete,
                                            Port_Iter);
                              --  impact on GMA can be negleted
                           end;
                        end if;

                        --  next
                        Port_Iter := OPN.Next_Node (Port_Iter);
                     end loop;
                  end if;                  --  have in ports
               end;

            end if;                        --  thread kind

            --  next
            Thread_Iter := OPN.Next_Node (Thread_Iter);
         end loop;                         --  next subcomponent
      end;