ocarina-backends-lnt-tree_generator_port.adb 10.5 KB
Newer Older
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
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                OCARINA.BACKENDS.LNT.TREE_GENERATOR_PORT                  --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2016 ESA & ISAE.                       --
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. 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.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------

with Ocarina.Namet; use Ocarina.Namet;

with Ocarina.Backends;
with Ocarina.Backends.LNT.Nutils;
with Ocarina.Backends.LNT.Nodes;
with Ocarina.Backends.LNT.Components;

use Ocarina.Backends.LNT.Components;

with Ada.Text_IO; use Ada.Text_IO;

package body Ocarina.Backends.LNT.Tree_Generator_Port is

   package BLN renames Ocarina.Backends.LNT.Nodes;
   package BLNu renames Ocarina.Backends.LNT.Nutils;
   use BLN;
   use BLNu;

   procedure Add_LNT_Data_Port (L_Ports : List_Id);
   procedure Add_LNT_Event_Port (L_Ports : List_Id; Is_Sporadic : boolean);

   function Make_Loop_Select (In_Select : List_Id)
     return Node_Id;

   function Make_Loop_Select (In_Select : List_Id)
     return Node_Id is
      N : Node_Id;
   begin
      N := Make_Loop_Statement
        (New_List (Make_Select_Statement (In_Select)));
      return N;
   end Make_Loop_Select;

   procedure Add_LNT_Data_Port (L_Ports : List_Id) is
      N : Node_Id;
      In_Select : List_Id;
   begin
      In_Select := New_List (
        Make_Select_Statement_Alternative
         (New_List (Make_Communication_Statement
                 (Make_Identifier ("Input"),
                  New_List (Make_Offer_Statement
                      (No_Node,
                       Make_Identifier ("AADLDATA"),
                       false)),
                  false,
                  No_Node))),
        Make_Select_Statement_Alternative
         (New_List (Make_Communication_Statement
                 (Make_Identifier ("Output"),
                  New_List (Make_Offer_Statement
                      (No_Node,
                       Make_Identifier ("AADLDATA"),
                       false)),
                  false,
                  No_Node)))
      );
      N :=  Make_Process_Definition
      (No_Node, Make_Identifier ("Data_Port"),
       No_List,
       New_List (
         Make_Gate_Declaration
hana's avatar
hana committed
94
           (Make_Identifier ("LNT_Channel_Port"),
95 96
            Make_Identifier ("Input")),
         Make_Gate_Declaration
hana's avatar
hana committed
97
           (Make_Identifier ("LNT_Channel_Port"),
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
            Make_Identifier ("Output"))),
       No_List,
       No_List,
       New_List (Make_Loop_Select (In_Select)));

      BLNu.Append_Node_To_List (N, L_Ports);
   end Add_LNT_Data_Port;

   procedure Add_LNT_Event_Port (L_Ports : List_Id;
      Is_Sporadic : boolean) is
      N : Node_Id;
      N_Port : Node_Id;
      Notify : Node_Id;
      L_Parameters : List_Id;
      L_Gates : List_Id;
      Var_Dec : List_Id;
      Out_Loop : List_Id;
      In_Select : List_Id;
      In_Select_1 : List_Id;

   begin
      L_Parameters := New_List;

      BLNu.Append_Node_To_List (
       Make_Parameter_Specification (
         Make_Identifier ("Queue_Size"),
         Make_Identifier ("Nat")),
         L_Parameters);

      Var_Dec := New_List (
         Make_Var_Declaration (
          Make_Identifier ("Nb_Input"),
          Make_Identifier ("Nat")));
      if (Is_Sporadic) then
         BLNu.Append_Node_To_List (
           Make_Var_Declaration (
             Make_Identifier ("Is_New"),
             Make_Identifier ("bool")),
           Var_Dec);
      end if;
      Out_Loop := New_List (
         Make_Assignment_Statement (
          Make_Identifier ("Nb_Input"),
          Make_Identifier ("0"))
      );
      if (Is_Sporadic) then
         BLNu.Append_Node_To_List (
           Make_Assignment_Statement (
             Make_Identifier ("Is_New"),
             Make_Identifier ("false")),
           Out_Loop);
      end if;

      In_Select_1 := New_List (
            Make_Communication_Statement (
              Make_Identifier ("Input"),
              New_List (Make_Identifier ("AADLDATA"))),

            Make_If_Statement (
              Make_Infix_Function_Call_Expression (
                      Make_Predefined_Function (
                         K_Less_Than, false),
                      Make_Identifier ("Nb_Input"),
                      Make_Identifier ("Queue_Size")),

              New_List (
                Make_Assignment_Statement (
                  Make_Identifier ("Nb_Input"),
                    Make_Infix_Function_Call_Expression (
                      Make_IDentifier ("+"),
                      Make_Identifier ("Nb_Input"),
                      Make_Nat (1)))),
              No_List,
              No_List));
      if (Is_Sporadic) then
         BLNu.Append_Node_To_List (
           Make_Assignment_Statement (
               Make_Identifier ("Is_New"),
               Make_Identifier ("true")),
           In_Select_1);
      end if;

      In_Select := New_List (
        Make_Select_Statement_Alternative (In_Select_1),

        Make_Select_Statement_Alternative (
          New_List (
            Make_If_Statement (
              Make_Infix_Function_Call_Expression (
                      Make_Predefined_Function (
                         K_Greater_Than, false),
                      Make_Identifier ("Nb_Input"),
                      Make_Identifier ("0")),
              New_List (
                 Make_Communication_Statement (
                  Make_Identifier ("Output"),
                  New_List (Make_Identifier ("AADLDATA"))),
                 Make_Assignment_Statement (
                  Make_Identifier ("Nb_Input"),
                    Make_Infix_Function_Call_Expression (
                      Make_IDentifier ("-"),
                      Make_Identifier ("Nb_Input"),
                      Make_Nat (1)))),
              No_List,
              No_List)))
      );
      if (Is_Sporadic) then
         Notify := Make_Select_Statement_Alternative (
          New_List (
            Make_If_Statement (
              Make_Identifier ("Is_New"),
              New_List (
                 Make_Communication_Statement (
                  Make_Identifier ("Notify"),
                  New_List (Make_Identifier ("Incoming_Event"))),
                 Make_Assignment_Statement (
                  Make_Identifier ("Is_New"),
                  Make_Identifier ("false"))),
              No_List,
              New_List (
                 Make_Communication_Statement (
                  Make_Identifier ("Notify"),
                  New_List (Make_Identifier ("No_Event"))))
              )));
         BLNu.Append_Node_To_List (Notify, In_Select);
      end if;
      L_Gates := New_List (
         Make_Gate_Declaration
           (Make_Identifier ("LNT_Channel_Data"),
            Make_Identifier ("Input")),
         Make_Gate_Declaration
           (Make_Identifier ("LNT_Channel_Data"),
            Make_Identifier ("Output")));
      if (Is_Sporadic) then
         BLNu.Append_Node_To_List (
           Make_Gate_Declaration
             (Make_Identifier ("LNT_Channel_Event"),
              Make_Identifier ("Notify")),
           L_Gates);
      end if;

      if (Is_Sporadic) then
         N_Port := Make_Identifier ("Event_Port_Sporadic");
      else
         N_Port := Make_Identifier ("Event_Port_Periodic");
      end if;

      N :=  Make_Process_Definition
      (No_Node, N_Port,
       L_Parameters,
       L_Gates,
       No_List,
       No_List,
       New_List (Make_Var_Loop_Select (Var_Dec, Out_Loop, In_Select)));

      BLNu.Append_Node_To_List (N, L_Ports);
   end Add_LNT_Event_Port;

   Module_Node : Node_Id := No_Node;
   Definitions_List : List_Id := No_List;
   Modules_List : List_Id := No_List;
   Predefined_Functions_List : List_Id := No_List;

   --  Module_Pragma : List_Id := No_List;

   function Generate_LNT_Port
     return Node_Id is
   begin
      Put_Line ("Begin Port");

      Module_Node := Make_Module_Definition
       (New_Identifier (Get_String_Name ("_Ports"),
                        Get_Name_String (System_Name)));
      Definitions_List := New_List;
      Add_LNT_Data_Port (Definitions_List);
      Add_LNT_Event_Port (Definitions_List, false);
hana's avatar
hana committed
274
      if (Not_Periodic_Thread_Number > 0) then
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
         Add_LNT_Event_Port (Definitions_List, true);
      end if;

      Modules_List := New_List (New_Identifier (
                        Get_String_Name ("_Types"),
                        Get_Name_String (System_Name)));
      Predefined_Functions_List := New_List;

      Set_Definitions (Module_Node, Definitions_List);
      Set_Modules (Module_Node, Modules_List);
      Set_Predefined_Functions (Module_Node, Predefined_Functions_List);
      return Module_Node;
   end Generate_LNT_Port;

end Ocarina.Backends.LNT.Tree_Generator_Port;