1--  Expressions 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 Ada.Unchecked_Deallocation;
22
23with Types; use Types;
24
25with PSL.Types;
26with Vhdl.Nodes; use Vhdl.Nodes;
27
28with Netlists; use Netlists;
29with Netlists.Builders; use Netlists.Builders;
30
31with Synth.Source;
32with Synth.Objtypes; use Synth.Objtypes;
33with Synth.Values; use Synth.Values;
34with Synth.Context; use Synth.Context;
35
36package Synth.Expr is
37   --  Perform a subtype conversion.  Check constraints.
38   function Synth_Subtype_Conversion (Ctxt : Context_Acc;
39                                      Vt : Valtyp;
40                                      Dtype : Type_Acc;
41                                      Bounds : Boolean;
42                                      Loc : Source.Syn_Src)
43                                     return Valtyp;
44
45   --  For a static value V, return the value.
46   function Get_Static_Discrete (V : Valtyp) return Int64;
47
48   --  Return the memory (as a memtyp) of static value V.
49   function Get_Value_Memtyp (V : Valtyp) return Memtyp;
50
51   --  Return True only if discrete value V is known to be positive or 0.
52   --  False means either not positive or unknown.
53   function Is_Positive (V : Valtyp) return Boolean;
54
55   --  Return the bounds of a one dimensional array/vector type and the
56   --  width of the element.
57   procedure Get_Onedimensional_Array_Bounds
58     (Typ : Type_Acc; Bnd : out Bound_Type; El_Typ : out Type_Acc);
59
60   --  Create an array subtype from bound BND.
61   function Create_Onedimensional_Array_Subtype
62     (Btyp : Type_Acc; Bnd : Bound_Type) return Type_Acc;
63
64   procedure From_Std_Logic (Enum : Int64; Val : out Uns32; Zx : out Uns32);
65   procedure From_Bit (Enum : Int64; Val : out Uns32);
66   procedure To_Logic
67     (Enum : Int64; Etype : Type_Acc; Val : out Uns32; Zx : out Uns32);
68
69   --  Try to match: clk'event and clk = X
70   --            or: clk = X and clk'event
71   --  where X is '0' or '1'.
72   function Synth_Clock_Edge
73     (Syn_Inst : Synth_Instance_Acc; Left, Right : Node) return Net;
74
75   procedure Concat_Array
76     (Ctxt : Context_Acc; Arr : in out Net_Array; N : out Net);
77
78   --  Synthesize EXPR.  The expression must be self-constrained.
79   --  If EN is not No_Net, the execution is controlled by EN.  This is used
80   --  for assertions and checks.
81   function Synth_Expression
82     (Syn_Inst : Synth_Instance_Acc; Expr : Node) return Valtyp;
83
84   --  Same as Synth_Expression, but the expression may be constrained by
85   --  EXPR_TYPE.
86   function Synth_Expression_With_Type (Syn_Inst : Synth_Instance_Acc;
87                                        Expr : Node;
88                                        Expr_Type : Type_Acc) return Valtyp;
89
90   --  Use base type of EXPR to synthesize EXPR.  Useful when the type of
91   --  EXPR is defined by itself or a range.
92   function Synth_Expression_With_Basetype (Syn_Inst : Synth_Instance_Acc;
93                                            Expr : Node) return Valtyp;
94
95   function Synth_PSL_Expression
96     (Syn_Inst : Synth_Instance_Acc; Expr : PSL.Types.PSL_Node) return Net;
97
98   function Synth_Bounds_From_Range (Syn_Inst : Synth_Instance_Acc;
99                                     Atype : Node) return Bound_Type;
100
101   function Synth_Array_Bounds (Syn_Inst : Synth_Instance_Acc;
102                                Atype : Node;
103                                Dim : Dim_Type) return Bound_Type;
104
105   function Synth_Discrete_Range_Expression
106     (L : Int64; R : Int64; Dir : Direction_Type) return Discrete_Range_Type;
107   function Synth_Discrete_Range_Expression
108     (Syn_Inst : Synth_Instance_Acc; Rng : Node) return Discrete_Range_Type;
109   function Synth_Float_Range_Expression
110     (Syn_Inst : Synth_Instance_Acc; Rng : Node) return Float_Range_Type;
111
112   procedure Synth_Discrete_Range (Syn_Inst : Synth_Instance_Acc;
113                                   Bound : Node;
114                                   Rng : out Discrete_Range_Type);
115
116   procedure Synth_Slice_Suffix (Syn_Inst : Synth_Instance_Acc;
117                                 Name : Node;
118                                 Pfx_Bnd : Bound_Type;
119                                 El_Typ : Type_Acc;
120                                 Res_Bnd : out Bound_Type;
121                                 Inp : out Net;
122                                 Off : out Value_Offsets);
123
124   --  If VOFF is No_Net then OFF is valid, if VOFF is not No_Net then
125   --  OFF is 0.
126   procedure Synth_Indexed_Name (Syn_Inst : Synth_Instance_Acc;
127                                 Name : Node;
128                                 Pfx_Type : Type_Acc;
129                                 Voff : out Net;
130                                 Off : out Value_Offsets);
131
132   --  Return the type of EXPR (an object) without evaluating it (except when
133   --  needed, like bounds of a slice).
134   function Synth_Type_Of_Object (Syn_Inst : Synth_Instance_Acc; Expr : Node)
135                                 return Type_Acc;
136
137   --  Conversion to logic vector.
138
139   type Digit_Index is new Natural;
140   type Logvec_Array is array (Digit_Index range <>) of Logic_32;
141   type Logvec_Array_Acc is access Logvec_Array;
142
143   procedure Free_Logvec_Array is new Ada.Unchecked_Deallocation
144     (Logvec_Array, Logvec_Array_Acc);
145
146   --  Convert W bits from OFF of VAL to a Logvec_Array.
147   --  OFF and W are offset and width in bit representation.
148   procedure Value2logvec (Val : Memtyp;
149                           Off : Uns32;
150                           W : Width;
151                           Vec : in out Logvec_Array;
152                           Vec_Off : in out Uns32;
153                           Has_Zx : in out Boolean);
154end Synth.Expr;
155