From beb01818f52362329556f663dcb176747f8cbb89 Mon Sep 17 00:00:00 2001 From: Tristan Gingold Date: Fri, 3 Apr 2020 08:46:23 +0200 Subject: synth: add value_memory and use it to store objects value. --- src/synth/synth-values.ads | 185 +++++++++++++++++++++++++-------------------- 1 file changed, 104 insertions(+), 81 deletions(-) (limited to 'src/synth/synth-values.ads') diff --git a/src/synth/synth-values.ads b/src/synth/synth-values.ads index 6e1b29e80..ffb554717 100644 --- a/src/synth/synth-values.ads +++ b/src/synth/synth-values.ads @@ -18,9 +18,12 @@ -- Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, -- MA 02110-1301, USA. +with Ada.Unchecked_Deallocation; + with Types; use Types; with Areapools; use Areapools; +with Grt.Types; use Grt.Types; with Grt.Files_Operations; with Netlists; use Netlists; @@ -60,8 +63,8 @@ package Synth.Values is type Bound_Array_Type is array (Dim_Type range <>) of Bound_Type; - type Bound_Array (Len : Dim_Type) is record - D : Bound_Array_Type (1 .. Len); + type Bound_Array (Ndim : Dim_Type) is record + D : Bound_Array_Type (1 .. Ndim); end record; type Bound_Array_Acc is access Bound_Array; @@ -92,7 +95,13 @@ package Synth.Values is type Type_Acc is access Type_Type; type Rec_El_Type is record - Off : Uns32; + -- Bit offset: offset of the element in a net. + Boff : Uns32; + + -- Memory offset: offset of the element in memory. + Moff : Size_Type; + + -- Type of the element. Typ : Type_Acc; end record; @@ -103,11 +112,20 @@ package Synth.Values is type Rec_El_Array_Acc is access Rec_El_Array; + -- Power of 2 alignment. + type Palign_Type is range 0 .. 3; + type Type_Type (Kind : Type_Kind) is record -- False if the type is not synthesisable: is or contains access/file. Is_Synth : Boolean; - -- Number of bits for this type. + -- Alignment (in bytes) for this type. + Al : Palign_Type; + + -- Number of bytes (when in memory) for this type. + Sz : Size_Type; + + -- Number of bits (when in a net) for this type. W : Width; case Kind is @@ -153,20 +171,9 @@ package Synth.Values is -- into a net. Value_Wire, - -- A discrete value (integer or enumeration). - Value_Discrete, + -- Any kind of constant value, raw stored in memory. + Value_Memory, - Value_Float, - - -- An array (const if all elements are constants). - Value_Array, - Value_Const_Array, - - -- A record (const if all elements are constants). - Value_Record, - Value_Const_Record, - - Value_Access, Value_File, -- A constant. This is a named value. One purpose is to avoid to @@ -184,9 +191,9 @@ package Synth.Values is type Value_Type_Array is array (Iir_Index32 range <>) of Value_Acc; - type Value_Array_Type (Len : Iir_Index32) is record + type Value_Array_Type (Ln : Iir_Index32) is record -- Values are from left to right. So V(1) is at index 'Left. - V : Value_Type_Array (1 .. Len); + V : Value_Type_Array (1 .. Ln); end record; type Value_Array_Acc is access Value_Array_Type; @@ -196,24 +203,33 @@ package Synth.Values is subtype File_Index is Grt.Files_Operations.Ghdl_File_Index; + type Memory_Element is mod 2**8; + type Memory_Array is array (Size_Type range <>) of Memory_Element; + + -- Flat pointer for a generic pointer. + type Memory_Ptr is access all Memory_Array (Size_Type); + + type Memtyp is record + Typ : Type_Acc; + Mem : Memory_Ptr; + end record; + + -- Offsets for a value. + type Value_Offsets is record + Net_Off : Uns32; + Mem_Off : Size_Type; + end record; + + function "+" (L, R : Value_Offsets) return Value_Offsets; + type Value_Type (Kind : Value_Kind) is record case Kind is when Value_Net => N : Net; when Value_Wire => W : Wire_Id; - when Value_Discrete => - Scal : Int64; - when Value_Float => - Fp : Fp64; - when Value_Array - | Value_Const_Array => - Arr : Value_Array_Acc; - when Value_Record - | Value_Const_Record => - Rec : Value_Array_Acc; - when Value_Access => - Acc : Heap_Index; + when Value_Memory => + Mem : Memory_Ptr; when Value_File => File : File_Index; when Value_Const => @@ -222,7 +238,7 @@ package Synth.Values is C_Net : Net; when Value_Alias => A_Obj : Value_Acc; - A_Off : Uns32; + A_Off : Value_Offsets; end case; end record; @@ -234,6 +250,12 @@ package Synth.Values is No_Valtyp : constant Valtyp := (null, null); + type Valtyp_Array is array (Nat32 range <>) of Valtyp; + type Valtyp_Array_Acc is access Valtyp_Array; + + procedure Free_Valtyp_Array is new Ada.Unchecked_Deallocation + (Valtyp_Array, Valtyp_Array_Acc); + Global_Pool : aliased Areapool; Expr_Pool : aliased Areapool; @@ -244,15 +266,19 @@ package Synth.Values is Instance_Pool : Areapool_Acc; -- Types. - function Create_Discrete_Type (Rng : Discrete_Range_Type; W : Width) + function Create_Discrete_Type (Rng : Discrete_Range_Type; + Sz : Size_Type; + W : Width) return Type_Acc; + function Create_Float_Type (Rng : Float_Range_Type) return Type_Acc; function Create_Vec_Type_By_Length (Len : Width; El : Type_Acc) return Type_Acc; function Create_Vector_Type (Bnd : Bound_Type; El_Type : Type_Acc) return Type_Acc; function Create_Unbounded_Vector (El_Type : Type_Acc) return Type_Acc; - function Create_Slice_Type (W : Width; El_Type : Type_Acc) return Type_Acc; + function Create_Slice_Type (Len : Uns32; El_Type : Type_Acc) + return Type_Acc; function Create_Bound_Array (Ndims : Dim_Type) return Bound_Array_Acc; function Create_Array_Type (Bnd : Bound_Array_Acc; El_Type : Type_Acc) return Type_Acc; @@ -260,8 +286,7 @@ package Synth.Values is return Type_Acc; function Create_Rec_El_Array (Nels : Iir_Index32) return Rec_El_Array_Acc; - function Create_Record_Type (Els : Rec_El_Array_Acc; W : Width) - return Type_Acc; + function Create_Record_Type (Els : Rec_El_Array_Acc) return Type_Acc; function Create_Access_Type (Acc_Type : Type_Acc) return Type_Acc; @@ -272,6 +297,9 @@ package Synth.Values is function Get_Array_Bound (Typ : Type_Acc; Dim : Dim_Type) return Bound_Type; + -- Return the length of RNG. + function Get_Range_Length (Rng : Discrete_Range_Type) return Uns32; + -- Return the element of a vector/array/unbounded_array. function Get_Array_Element (Arr_Type : Type_Acc) return Type_Acc; @@ -283,80 +311,55 @@ package Synth.Values is -- Can also return true for nets and wires. function Is_Static_Val (Val : Value_Acc) return Boolean; - function Is_Equal (L, R : Value_Acc) return Boolean; + function Is_Equal (L, R : Valtyp) return Boolean; function Are_Types_Equal (L, R : Type_Acc) return Boolean; -- Create a Value_Net. - function Create_Value_Net (N : Net) return Value_Acc; function Create_Value_Net (N : Net; Ntype : Type_Acc) return Valtyp; -- Create a Value_Wire. For a bit wire, RNG must be null. - function Create_Value_Wire (W : Wire_Id) return Value_Acc; function Create_Value_Wire (W : Wire_Id; Wtype : Type_Acc) return Valtyp; - function Create_Value_Discrete (Val : Int64) return Value_Acc; + function Create_Value_Memory (Vtype : Type_Acc) return Valtyp; + + function Create_Value_Uns (Val : Uns64; Vtype : Type_Acc) return Valtyp; + function Create_Value_Int (Val : Int64; Vtype : Type_Acc) return Valtyp; function Create_Value_Discrete (Val : Int64; Vtype : Type_Acc) return Valtyp; - function Create_Value_Float (Val : Fp64) return Value_Acc; - function Create_Value_Float (Val : Fp64; Vtype : Type_Acc) return Valtyp; - - function Create_Value_Access (Acc : Heap_Index) return Value_Acc; - function Create_Value_Access (Vtype : Type_Acc; Acc : Heap_Index) + function Create_Value_Access (Val : Heap_Index; Acc_Typ : Type_Acc) return Valtyp; - function Create_Value_File (File : File_Index) return Value_Acc; + function Create_Value_Float (Val : Fp64; Vtype : Type_Acc) return Valtyp; + function Create_Value_File (Vtype : Type_Acc; File : File_Index) return Valtyp; - function Create_Value_Array (Len : Iir_Index32) return Value_Array_Acc; + function Create_Value_Alias + (Obj : Value_Acc; Off : Value_Offsets; Typ : Type_Acc) return Valtyp; - -- Create a Value_Array. - function Create_Value_Array (Arr : Value_Array_Acc) return Value_Acc; - function Create_Value_Array (Bounds : Type_Acc; Arr : Value_Array_Acc) - return Valtyp; - function Create_Value_Const_Array (Arr : Value_Array_Acc) return Value_Acc; - function Create_Value_Const_Array (Bounds : Type_Acc; Arr : Value_Array_Acc) - return Valtyp; - - -- Like the previous one but automatically build the array. - function Create_Value_Array (Bounds : Type_Acc) return Value_Acc; - - -- Allocate the ARR component of the Value_Type ARR, using BOUNDS. - -- procedure Create_Array_Data (Arr : Value_Acc); - - function Create_Value_Record (Els : Value_Array_Acc) return Value_Acc; - function Create_Value_Record (Typ : Type_Acc; Els : Value_Array_Acc) - return Valtyp; - function Create_Value_Const_Record (Els : Value_Array_Acc) return Value_Acc; - function Create_Value_Const_Record (Typ : Type_Acc; Els : Value_Array_Acc) - return Valtyp; - - function Create_Value_Alias (Obj : Value_Acc; Off : Uns32) return Value_Acc; - function Create_Value_Alias (Obj : Value_Acc; Off : Uns32; Typ : Type_Acc) - return Valtyp; - function Create_Value_Const (Val : Value_Acc; Loc : Syn_Src) - return Value_Acc; function Create_Value_Const (Val : Valtyp; Loc : Syn_Src) return Valtyp; -- If VAL is a const, replace it by its value. - procedure Strip_Const (Val : in out Value_Acc); procedure Strip_Const (Vt : in out Valtyp); - function Strip_Const (Val : Value_Acc) return Value_Acc; -- If VAL is a const or an alias, replace it by its value. -- Used to extract the real data of a static value. Note that the type -- is not correct anymore. - function Strip_Alias_Const (V : Value_Acc) return Value_Acc; function Strip_Alias_Const (V : Valtyp) return Valtyp; - function Unshare (Src : Value_Acc; Pool : Areapool_Acc) - return Value_Acc; + -- Return the memtyp of V; also strip const and aliases. + function Get_Memtyp (V : Valtyp) return Memtyp; + + function Unshare (Src : Valtyp; Pool : Areapool_Acc) return Valtyp; + + -- Return the length of a vector type. + function Vec_Length (Typ : Type_Acc) return Iir_Index32; -- Get the number of indexes in array type TYP without counting -- sub-elements. - function Get_Array_Flat_Length (Typ : Type_Acc) return Width; + function Get_Array_Flat_Length (Typ : Type_Acc) return Iir_Index32; -- Return length of dimension DIM of type T. function Get_Bound_Length (T : Type_Acc; Dim : Dim_Type) return Width; @@ -366,12 +369,32 @@ package Synth.Values is function Get_Type_Width (Atype : Type_Acc) return Width; -- Create a default initial value for TYP. - function Create_Value_Default (Typ : Type_Acc) return Value_Acc; function Create_Value_Default (Typ : Type_Acc) return Valtyp; + procedure Write_Value_Default (M : Memory_Ptr; Typ : Type_Acc); -- Convert a value to a string. The value must be a const_array of scalar, -- which represent characters. - function Value_To_String (Val : Value_Acc) return String; + function Value_To_String (Val : Valtyp) return String; + + -- Memory access. + procedure Write_Discrete (Vt : Valtyp; Val : Int64); + function Read_Discrete (Mem : Memory_Ptr; Typ : Type_Acc) return Int64; + function Read_Discrete (Vt : Valtyp) return Int64; + + procedure Write_Access (Mem : Memory_Ptr; Val : Heap_Index); + function Read_Access (Vt : Valtyp) return Heap_Index; + + function Read_Fp64 (Mem : Memory_Ptr) return Fp64; + function Read_Fp64 (Vt : Valtyp) return Fp64; + + -- Low level subprograms. + function Read_U8 (Mem : Memory_Ptr) return Ghdl_U8; + procedure Write_U8 (Mem : Memory_Ptr; Val : Ghdl_U8); + + function "+" (Base : Memory_Ptr; Off : Size_Type) return Memory_Ptr; + + procedure Copy_Memory (Dest : Memory_Ptr; Src : Memory_Ptr; Sz : Size_Type); + procedure Write_Value (Dest : Memory_Ptr; Vt : Valtyp); procedure Init; -- cgit v1.2.3