aboutsummaryrefslogtreecommitdiffstats
path: root/src/vhdl/vhdl-ieee-std_logic_unsigned.adb
blob: b57c0aa7a74a7cdcce1eeafcb079d83c356610a4 (plain)
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
94
95
96
97
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
--  Nodes recognizer for ieee.std_logic_unsigned and ieee.std_logic_signed
--  Copyright (C) 2016 Tristan Gingold
--
--  GHDL is free software; you can redistribute it and/or modify it under
--  the terms of the GNU General Public License as published by the Free
--  Software Foundation; either version 2, or (at your option) any later
--  version.
--
--  GHDL 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.  See the GNU General Public License
--  for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with GHDL; see the file COPYING.  If not, write to the Free
--  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
--  02111-1307, USA.

with Vhdl.Std_Package;
with Std_Names; use Std_Names;
with Vhdl.Ieee.Std_Logic_1164;

package body Vhdl.Ieee.Std_Logic_Unsigned is
   type Arg_Kind is (Arg_Slv, Arg_Int, Arg_Log);
   type Args_Kind is (Arg_Slv_Slv, Arg_Slv_Int, Arg_Int_Slv,
                      Arg_Slv_Log, Arg_Log_Slv);

   type Binary_Pattern_Type is array (Args_Kind) of Iir_Predefined_Functions;

   type Unary_Pattern_Type is array (Sign_Kind) of Iir_Predefined_Functions;

   Eq_Uns_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Eq_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Unsigned_Eq_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Eq_Int_Slv,
      others => Iir_Predefined_None);

   Eq_Sgn_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Eq_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Signed_Eq_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Eq_Int_Slv,
      others => Iir_Predefined_None);

   Ne_Uns_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Ne_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Unsigned_Ne_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Ne_Int_Slv,
      others => Iir_Predefined_None);

   Ne_Sgn_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Ne_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Signed_Ne_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Ne_Int_Slv,
      others => Iir_Predefined_None);

   Lt_Uns_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Lt_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Unsigned_Lt_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Lt_Int_Slv,
      others => Iir_Predefined_None);

   Lt_Sgn_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Lt_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Signed_Lt_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Lt_Int_Slv,
      others => Iir_Predefined_None);

   Le_Uns_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Le_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Unsigned_Le_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Le_Int_Slv,
      others => Iir_Predefined_None);

   Le_Sgn_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Le_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Signed_Le_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Le_Int_Slv,
      others => Iir_Predefined_None);

   Gt_Uns_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Gt_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Unsigned_Gt_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Gt_Int_Slv,
      others => Iir_Predefined_None);

   Gt_Sgn_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Gt_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Signed_Gt_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Gt_Int_Slv,
      others => Iir_Predefined_None);

   Ge_Uns_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Ge_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Unsigned_Ge_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Ge_Int_Slv,
      others => Iir_Predefined_None);

   Ge_Sgn_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Ge_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Signed_Ge_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Ge_Int_Slv,
      others => Iir_Predefined_None);

   Add_Uns_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Add_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Unsigned_Add_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Add_Int_Slv,
      Arg_Slv_Log => Iir_Predefined_Ieee_Std_Logic_Unsigned_Add_Slv_Log,
      Arg_Log_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Add_Log_Slv);

   Sub_Uns_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Sub_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Unsigned_Sub_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Sub_Int_Slv,
      Arg_Slv_Log => Iir_Predefined_Ieee_Std_Logic_Unsigned_Sub_Slv_Log,
      Arg_Log_Slv => Iir_Predefined_Ieee_Std_Logic_Unsigned_Sub_Log_Slv);

   Add_Sgn_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Add_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Signed_Add_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Add_Int_Slv,
      Arg_Slv_Log => Iir_Predefined_Ieee_Std_Logic_Signed_Add_Slv_Log,
      Arg_Log_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Add_Log_Slv);

   Sub_Sgn_Patterns : constant Binary_Pattern_Type :=
     (Arg_Slv_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Sub_Slv_Slv,
      Arg_Slv_Int => Iir_Predefined_Ieee_Std_Logic_Signed_Sub_Slv_Int,
      Arg_Int_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Sub_Int_Slv,
      Arg_Slv_Log => Iir_Predefined_Ieee_Std_Logic_Signed_Sub_Slv_Log,
      Arg_Log_Slv => Iir_Predefined_Ieee_Std_Logic_Signed_Sub_Log_Slv);

   Id_Patterns : constant Unary_Pattern_Type :=
     (Pkg_Unsigned => Iir_Predefined_Ieee_Std_Logic_Unsigned_Id_Slv,
      Pkg_Signed   => Iir_Predefined_Ieee_Std_Logic_Signed_Id_Slv);

   Conv_Patterns : constant Unary_Pattern_Type :=
     (Pkg_Unsigned => Iir_Predefined_Ieee_Std_Logic_Unsigned_Conv_Integer,
      Pkg_Signed   => Iir_Predefined_Ieee_Std_Logic_Signed_Conv_Integer);

   Mul_Patterns : constant Unary_Pattern_Type :=
     (Pkg_Unsigned => Iir_Predefined_Ieee_Std_Logic_Unsigned_Mul_Slv_Slv,
      Pkg_Signed   => Iir_Predefined_Ieee_Std_Logic_Signed_Mul_Slv_Slv);

   Shl_Patterns : constant Unary_Pattern_Type :=
     (Pkg_Unsigned => Iir_Predefined_Ieee_Std_Logic_Unsigned_Shl,
      Pkg_Signed   => Iir_Predefined_Ieee_Std_Logic_Signed_Shl);

   Shr_Patterns : constant Unary_Pattern_Type :=
     (Pkg_Unsigned => Iir_Predefined_Ieee_Std_Logic_Unsigned_Shr,
      Pkg_Signed   => Iir_Predefined_Ieee_Std_Logic_Signed_Shr);

   Error : exception;

   procedure Classify_Arg (Arg : Iir; Kind : out Arg_Kind)
   is
      Arg_Type : constant Iir := Get_Type (Arg);
   begin
      if Arg_Type = Vhdl.Std_Package.Integer_Subtype_Definition then
         Kind := Arg_Int;
      elsif Arg_Type = Ieee.Std_Logic_1164.Std_Logic_Type then
         Kind := Arg_Log;
      elsif Arg_Type = Ieee.Std_Logic_1164.Std_Logic_Vector_Type then
         Kind := Arg_Slv;
      else
         raise Error;
      end if;
   end Classify_Arg;

   procedure Extract_Declaration (Decl : Iir; Sign : Sign_Kind)
   is
      Arg1, Arg2 : Iir;
      Arg1_Kind, Arg2_Kind : Arg_Kind;

      function Handle_Binary (Unsigned_Pats : Binary_Pattern_Type;
                              Signed_Pats : Binary_Pattern_Type)
                             return Iir_Predefined_Functions
      is
         Kind : Args_Kind;
      begin
         case Arg1_Kind is
            when Arg_Slv =>
               case Arg2_Kind is
                  when Arg_Slv => Kind := Arg_Slv_Slv;
                  when Arg_Log => Kind := Arg_Slv_Log;
                  when Arg_Int => Kind := Arg_Slv_Int;
               end case;
            when Arg_Int =>
               case Arg2_Kind is
                  when Arg_Slv => Kind := Arg_Int_Slv;
                  when Arg_Log
                    | Arg_Int => raise Error;
               end case;
            when Arg_Log =>
               case Arg2_Kind is
                  when Arg_Slv => Kind := Arg_Log_Slv;
                  when Arg_Log
                    | Arg_Int => raise Error;
               end case;
         end case;

         case Sign is
            when Pkg_Unsigned =>
               return Unsigned_Pats (Kind);
            when Pkg_Signed =>
               return Signed_Pats (Kind);
         end case;
      end Handle_Binary;

      Res : Iir_Predefined_Functions;
   begin
      Arg1 := Get_Interface_Declaration_Chain (Decl);
      if Is_Null (Arg1) then
         raise Error;
      end if;

      Res := Iir_Predefined_None;

      Classify_Arg (Arg1, Arg1_Kind);
      Arg2 := Get_Chain (Arg1);
      if Is_Valid (Arg2) then
         --  Dyadic function.
         Classify_Arg (Arg2, Arg2_Kind);

         case Get_Identifier (Decl) is
            when Name_Op_Equality =>
               Res := Handle_Binary (Eq_Uns_Patterns, Eq_Sgn_Patterns);
            when Name_Op_Inequality =>
               Res := Handle_Binary (Ne_Uns_Patterns, Ne_Sgn_Patterns);
            when Name_Op_Less =>
               Res := Handle_Binary (Lt_Uns_Patterns, Lt_Sgn_Patterns);
            when Name_Op_Less_Equal =>
               Res := Handle_Binary (Le_Uns_Patterns, Le_Sgn_Patterns);
            when Name_Op_Greater =>
               Res := Handle_Binary (Gt_Uns_Patterns, Gt_Sgn_Patterns);
            when Name_Op_Greater_Equal =>
               Res := Handle_Binary (Ge_Uns_Patterns, Ge_Sgn_Patterns);
            when Name_Op_Plus =>
               Res := Handle_Binary (Add_Uns_Patterns, Add_Sgn_Patterns);
            when Name_Op_Minus =>
               Res := Handle_Binary (Sub_Uns_Patterns, Sub_Sgn_Patterns);
            when Name_Op_Mul =>
               pragma Assert (Arg1_Kind = Arg_Slv);
               pragma Assert (Arg2_Kind = Arg_Slv);
               Res := Mul_Patterns (Sign);
            when Name_Shl =>
               pragma Assert (Arg1_Kind = Arg_Slv);
               pragma Assert (Arg2_Kind = Arg_Slv);
               Res := Shl_Patterns (Sign);
            when Name_Shr =>
               pragma Assert (Arg1_Kind = Arg_Slv);
               pragma Assert (Arg2_Kind = Arg_Slv);
               Res := Shr_Patterns (Sign);
            when others =>
               null;
         end case;
      else
         --  Monadic function.
         case Get_Identifier (Decl) is
            when Name_Conv_Integer =>
               Res := Conv_Patterns (Sign);
            when Name_Op_Plus =>
               pragma Assert (Arg1_Kind = Arg_Slv);
               Res := Id_Patterns (Sign);
            when Name_Op_Minus =>
               if Sign = Pkg_Signed and Arg1_Kind = Arg_Slv then
                  Res := Iir_Predefined_Ieee_Std_Logic_Signed_Neg_Slv;
               end if;
            when Name_Abs =>
               if Sign = Pkg_Signed and Arg1_Kind = Arg_Slv then
                  Res := Iir_Predefined_Ieee_Std_Logic_Signed_Abs_Slv;
               end if;
            when others =>
               null;
         end case;
      end if;
      Set_Implicit_Definition (Decl, Res);
   end Extract_Declaration;

   procedure Extract_Declarations
     (Pkg : Iir_Package_Declaration; Sign : Sign_Kind)
   is
      Decl : Iir;
   begin
      Decl := Get_Declaration_Chain (Pkg);

      --  Handle functions.
      while Is_Valid (Decl) loop
         if Get_Kind (Decl) /= Iir_Kind_Function_Declaration then
            raise Error;
         end if;

         Extract_Declaration (Decl, Sign);

         Decl := Get_Chain (Decl);
      end loop;
   end Extract_Declarations;
end Vhdl.Ieee.Std_Logic_Unsigned;