1--  Statements synthesis.
2--  Copyright (C) 2017 Tristan Gingold
3--
4--  This file is part of GHDL.
5--
6--  This program is free software; you can redistribute it and/or modify
7--  it under the terms of the GNU General Public License as published by
8--  the Free Software Foundation; either version 2 of the License, or
9--  (at your option) any later version.
10--
11--  This program is distributed in the hope that it will be useful,
12--  but WITHOUT ANY WARRANTY; without even the implied warranty of
13--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14--  GNU General Public License for more details.
15--
16--  You should have received a copy of the GNU General Public License
17--  along with this program; if not, write to the Free Software
18--  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19--  MA 02110-1301, USA.
20
21with Types; use Types;
22with Vhdl.Nodes; use Vhdl.Nodes;
23
24with Netlists; use Netlists;
25
26with Synth.Objtypes; use Synth.Objtypes;
27with Synth.Values; use Synth.Values;
28with Synth.Context; use Synth.Context;
29with Synth.Environment; use Synth.Environment;
30
31package Synth.Stmts is
32   procedure Synth_Subprogram_Association (Subprg_Inst : Synth_Instance_Acc;
33                                           Caller_Inst : Synth_Instance_Acc;
34                                           Inter_Chain : Node;
35                                           Assoc_Chain : Node);
36
37   --  Dynamic index for Synth_Assignment_Prefix.
38   --  As dynamic is about dynamic (!) index, the index is a net.
39   type Dyn_Name is record
40      --  Start and type of the indexed part, which can be a part of the
41      --  base name.
42      Pfx_Off : Value_Offsets;
43      Pfx_Typ : Type_Acc;
44
45      --  Variable offset.
46      Voff : Net;
47   end record;
48
49   No_Dyn_Name : constant Dyn_Name := (Pfx_Off => No_Value_Offsets,
50                                       Pfx_Typ => null,
51                                       Voff => No_Net);
52
53   --  Transform PFX into DEST_*.
54   --  DEST_BASE is the base object (with its own typ).  Can be the result,
55   --   a net or an object larger than the result.
56   --  DEST_TYP is the type of the result.
57   --  DEST_OFF is the offset, within DEST_DYN.
58   --  DEST_DYN is set (Voff field set) when there is a non-static index.
59   procedure Synth_Assignment_Prefix (Syn_Inst : Synth_Instance_Acc;
60                                      Pfx : Node;
61                                      Dest_Base : out Valtyp;
62                                      Dest_Typ : out Type_Acc;
63                                      Dest_Off : out Value_Offsets;
64                                      Dest_Dyn : out Dyn_Name);
65
66   procedure Synth_Assignment (Syn_Inst : Synth_Instance_Acc;
67                               Target : Node;
68                               Val : Valtyp;
69                               Loc : Node);
70
71   function Synth_Read_Memory (Syn_Inst : Synth_Instance_Acc;
72                               Obj : Valtyp;
73                               Res_Typ : Type_Acc;
74                               Off : Uns32;
75                               Dyn : Dyn_Name;
76                               Loc : Node) return Valtyp;
77
78   function Synth_User_Function_Call
79     (Syn_Inst : Synth_Instance_Acc; Expr : Node) return Valtyp;
80
81   --  Operation implemented by a user function.
82   function Synth_User_Operator (Syn_Inst : Synth_Instance_Acc;
83                                 Left_Expr : Node;
84                                 Right_Expr : Node;
85                                 Expr : Node) return Valtyp;
86
87   --  Generate netlists for concurrent statements STMTS.
88   procedure Synth_Concurrent_Statements
89     (Syn_Inst : Synth_Instance_Acc; Stmts : Node);
90
91   --  Apply attributes of UNIT.
92   procedure Synth_Attribute_Values
93     (Syn_Inst : Synth_Instance_Acc; Unit : Node);
94
95   procedure Synth_Verification_Unit
96     (Syn_Inst : Synth_Instance_Acc; Unit : Node);
97
98   --  For iterators.
99   procedure Update_Index (Rng : Discrete_Range_Type; V : in out Valtyp);
100
101private
102   --  There are 2 execution mode:
103   --  * static: it is like simulation, all the inputs are known, neither
104   --    gates nor signals are generated.  This mode is used during
105   --    elaboration and when all inputs of a subprogram are known.
106   --  * dynamic: inputs can be wires so gates are generated.  But many types
107   --    (like file or access) cannot be handled.
108   type Mode_Type is (Mode_Static, Mode_Dynamic);
109
110   type Loop_Context (Mode : Mode_Type);
111   type Loop_Context_Acc is access all Loop_Context;
112
113   type Loop_Context (Mode : Mode_Type) is record
114      Prev_Loop : Loop_Context_Acc;
115      Loop_Stmt : Node;
116
117      case Mode is
118         when Mode_Dynamic =>
119            --  Set when this loop has next/exit statements for itself.
120            --  Set to true so that inner loops have to declare W_Quit.
121            Need_Quit : Boolean;
122
123            --  Value of W_En at the entry of the loop.
124            Saved_En : Net;
125
126            --  Set to 0 in case of exit for the loop.
127            --  Set to 0 in case of exit/next for outer loop.
128            --  Initialized to 1.
129            W_Exit : Wire_Id;
130
131            --  Set to 0 if this loop has to be quited because of an
132            --  exit/next for an outer loop.  Initialized to 1.
133            W_Quit : Wire_Id;
134
135            --  Mark to release wires.
136            Wire_Mark : Wire_Id;
137         when Mode_Static =>
138            S_Exit : Boolean;
139            S_Quit : Boolean;
140      end case;
141   end record;
142
143   --  Context for sequential statements.
144   type Seq_Context (Mode : Mode_Type) is record
145      Inst : Synth_Instance_Acc;
146
147      Cur_Loop : Loop_Context_Acc;
148
149      Ret_Value : Valtyp;
150      Ret_Typ : Type_Acc;
151      Nbr_Ret : Int32;
152
153      case Mode is
154         when Mode_Dynamic =>
155            --  Enable execution.  For loop controls.
156            W_En : Wire_Id;
157
158            W_Ret : Wire_Id;
159
160            --  Return value.
161            W_Val : Wire_Id;
162
163            Ret_Init : Net;
164
165         when Mode_Static =>
166            S_En : Boolean;
167      end case;
168   end record;
169end Synth.Stmts;
170