1--  Values in 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;
24with Areapools; use Areapools;
25
26with Grt.Files_Operations;
27
28with Netlists; use Netlists;
29
30with Synth.Objtypes; use Synth.Objtypes;
31with Synth.Environment; use Synth.Environment;
32with Synth.Source; use Synth.Source;
33
34package Synth.Values is
35   --  Values is how signals and variables are decomposed.  This is similar to
36   --  values in simulation, but simplified (no need to handle files,
37   --  accesses...)
38
39   type Value_Kind is
40     (
41      --  Value is for a vector or a bit, and is the output of a gate.
42      Value_Net,
43
44      --  Also a vector or a bit, but from an object.  Has to be transformed
45      --  into a net.
46      Value_Wire,
47
48      --  Any kind of constant value, raw stored in memory.
49      Value_Memory,
50
51      Value_File,
52
53      --  A constant.  This is a named value.  One purpose is to avoid to
54      --  create many times the same net for the same value.
55      Value_Const,
56
57      --  An alias.  This is a reference to another value with a different
58      --  (but compatible) type.
59      Value_Alias
60     );
61
62   type Value_Type (Kind : Value_Kind);
63
64   type Value_Acc is access Value_Type;
65
66   type Heap_Index is new Uns32;
67   Null_Heap_Index : constant Heap_Index := 0;
68
69   subtype File_Index is Grt.Files_Operations.Ghdl_File_Index;
70
71   type Value_Type (Kind : Value_Kind) is record
72      case Kind is
73         when Value_Net =>
74            N : Net;
75         when Value_Wire =>
76            W : Wire_Id;
77         when Value_Memory =>
78            Mem : Memory_Ptr;
79         when Value_File =>
80            File : File_Index;
81         when Value_Const =>
82            C_Val : Value_Acc;
83            C_Loc : Syn_Src;
84            C_Net : Net;
85         when Value_Alias =>
86            A_Obj : Value_Acc;
87            A_Typ : Type_Acc;  --  The type of A_Obj.
88            A_Off : Value_Offsets;
89      end case;
90   end record;
91
92   --  A tuple of type and value.
93   type Valtyp is record
94      Typ : Type_Acc;
95      Val : Value_Acc;
96   end record;
97
98   No_Valtyp : constant Valtyp := (null, null);
99
100   type Valtyp_Array is array (Nat32 range <>) of Valtyp;
101   type Valtyp_Array_Acc is access Valtyp_Array;
102
103   procedure Free_Valtyp_Array is new Ada.Unchecked_Deallocation
104     (Valtyp_Array, Valtyp_Array_Acc);
105
106   --  True if VAL is static, ie contains neither nets nor wires.
107   function Is_Static (Val : Value_Acc) return Boolean;
108
109   --  Can also return true for nets and wires.
110   --  Use Get_Static_Discrete to get the value.
111   function Is_Static_Val (Val : Value_Acc) return Boolean;
112
113   function Is_Equal (L, R : Valtyp) return Boolean;
114
115   function Create_Value_Memtyp (Mt : Memtyp) return Valtyp;
116
117   --  Create a Value_Net.
118   function Create_Value_Net (N : Net; Ntype : Type_Acc) return Valtyp;
119
120   --  Create a Value_Wire.  For a bit wire, RNG must be null.
121   function Create_Value_Wire (W : Wire_Id; Wtype : Type_Acc) return Valtyp;
122
123   function Create_Value_Memory (Vtype : Type_Acc) return Valtyp;
124   function Create_Value_Memory (Mt : Memtyp) return Valtyp;
125
126   function Create_Value_Uns (Val : Uns64; Vtype : Type_Acc) return Valtyp;
127   function Create_Value_Int (Val : Int64; Vtype : Type_Acc) return Valtyp;
128   function Create_Value_Discrete (Val : Int64; Vtype : Type_Acc)
129                                  return Valtyp;
130
131   function Create_Value_Access (Val : Heap_Index; Acc_Typ : Type_Acc)
132                                return Valtyp;
133
134   function Create_Value_Float (Val : Fp64; Vtype : Type_Acc) return Valtyp;
135
136   function Create_Value_File (Vtype : Type_Acc; File : File_Index)
137                              return Valtyp;
138
139   function Create_Value_Alias
140     (Obj : Valtyp; Off : Value_Offsets; Typ : Type_Acc) return Valtyp;
141
142   function Create_Value_Const (Val : Valtyp; Loc : Syn_Src)
143                               return Valtyp;
144
145   --  If VAL is a const, replace it by its value.
146   procedure Strip_Const (Vt : in out Valtyp);
147
148   --  If VAL is a const or an alias, replace it by its value.
149   --  Used to extract the real data of a static value.  Note that the type
150   --  is not correct anymore.
151   function Strip_Alias_Const (V : Valtyp) return Valtyp;
152
153   --  Return the memtyp of V; also strip const and aliases.
154   function Get_Memtyp (V : Valtyp) return Memtyp;
155
156   function Unshare (Src : Valtyp; Pool : Areapool_Acc) return Valtyp;
157
158   --  Create a default initial value for TYP.
159   function Create_Value_Default (Typ : Type_Acc) return Valtyp;
160   procedure Write_Value_Default (M : Memory_Ptr; Typ : Type_Acc);
161
162   --  Convert a value to a string.  The value must be a const_array of scalar,
163   --  which represent characters.
164   function Value_To_String (Val : Valtyp) return String;
165
166   --  Memory access.
167   procedure Write_Discrete (Vt : Valtyp; Val : Int64);
168   function Read_Discrete (Vt : Valtyp) return Int64;
169
170   procedure Write_Access (Mem : Memory_Ptr; Val : Heap_Index);
171   function Read_Access (Mt : Memtyp) return Heap_Index;
172   function Read_Access (Vt : Valtyp) return Heap_Index;
173
174   function Read_Fp64 (Vt : Valtyp) return Fp64;
175
176   procedure Write_Value (Dest : Memory_Ptr; Vt : Valtyp);
177end Synth.Values;
178