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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
|
-- Iir to ortho translator.
-- Copyright (C) 2002 - 2014 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 GCC; see the file COPYING. If not, write to the Free
-- Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.
with Files_Map;
with Errorout; use Errorout;
with Iirs_Utils; use Iirs_Utils;
with Evaluation; use Evaluation;
with Canon;
with Trans.Chap3;
with Trans.Chap7;
with Trans.Chap8;
with Trans.Chap14;
with Trans.Helpers2; use Trans.Helpers2;
with Trans_Decls; use Trans_Decls;
package body Trans.Chap6 is
use Trans.Helpers;
function Get_Array_Bound_Length (Arr : Mnode;
Arr_Type : Iir;
Dim : Natural)
return O_Enode
is
Index_Type : constant Iir := Get_Index_Type (Arr_Type, Dim - 1);
Tinfo : constant Type_Info_Acc := Get_Info (Arr_Type);
Constraint : Iir;
begin
if Tinfo.Type_Locally_Constrained then
Constraint := Get_Range_Constraint (Index_Type);
return New_Lit (Chap7.Translate_Static_Range_Length (Constraint));
else
return M2E
(Chap3.Range_To_Length
(Chap3.Get_Array_Range (Arr, Arr_Type, Dim)));
end if;
end Get_Array_Bound_Length;
procedure Gen_Bound_Error (Loc : Iir)
is
Constr : O_Assoc_List;
Name : Name_Id;
Line, Col : Natural;
begin
Files_Map.Location_To_Position (Get_Location (Loc), Name, Line, Col);
Start_Association (Constr, Ghdl_Bound_Check_Failed_L1);
Assoc_Filename_Line (Constr, Line);
New_Procedure_Call (Constr);
end Gen_Bound_Error;
procedure Gen_Program_Error (Loc : Iir; Code : Natural)
is
Assoc : O_Assoc_List;
begin
Start_Association (Assoc, Ghdl_Program_Error);
if Current_Filename_Node = O_Dnode_Null then
New_Association (Assoc, New_Lit (New_Null_Access (Char_Ptr_Type)));
New_Association (Assoc,
New_Lit (New_Signed_Literal (Ghdl_I32_Type, 0)));
else
Assoc_Filename_Line (Assoc, Get_Line_Number (Loc));
end if;
New_Association
(Assoc, New_Lit (New_Unsigned_Literal (Ghdl_Index_Type,
Unsigned_64 (Code))));
New_Procedure_Call (Assoc);
end Gen_Program_Error;
-- Generate code to emit a failure if COND is TRUE, indicating an
-- index violation for dimension DIM of an array. LOC is usually
-- the expression which has computed the index and is used only for
-- its location.
procedure Check_Bound_Error (Cond : O_Enode; Loc : Iir; Dim : Natural)
is
pragma Unreferenced (Dim);
If_Blk : O_If_Block;
begin
Start_If_Stmt (If_Blk, Cond);
Gen_Bound_Error (Loc);
Finish_If_Stmt (If_Blk);
end Check_Bound_Error;
-- Return TRUE if an array whose index type is RNG_TYPE indexed by
-- an expression of type EXPR_TYPE needs a bound check.
function Need_Index_Check (Expr_Type : Iir; Rng_Type : Iir)
return Boolean
is
Rng : Iir;
begin
-- Do checks if type of the expression is not a subtype.
-- FIXME: EXPR_TYPE shound not be NULL_IIR (generate stmt)
if Expr_Type = Null_Iir then
return True;
end if;
case Get_Kind (Expr_Type) is
when Iir_Kind_Integer_Subtype_Definition
| Iir_Kind_Enumeration_Subtype_Definition
| Iir_Kind_Enumeration_Type_Definition =>
null;
when others =>
return True;
end case;
-- No check if the expression has the type of the index.
if Expr_Type = Rng_Type then
return False;
end if;
-- No check for 'Range or 'Reverse_Range.
Rng := Get_Range_Constraint (Expr_Type);
if (Get_Kind (Rng) = Iir_Kind_Range_Array_Attribute
or Get_Kind (Rng) = Iir_Kind_Reverse_Range_Array_Attribute)
and then Get_Type (Rng) = Rng_Type
then
return False;
end if;
return True;
end Need_Index_Check;
procedure Get_Deep_Range_Expression
(Atype : Iir; Rng : out Iir; Is_Reverse : out Boolean)
is
T : Iir;
R : Iir;
begin
Is_Reverse := False;
-- T is an integer/enumeration subtype.
T := Atype;
loop
case Get_Kind (T) is
when Iir_Kind_Integer_Subtype_Definition
| Iir_Kind_Enumeration_Subtype_Definition
| Iir_Kind_Enumeration_Type_Definition =>
-- These types have a range.
null;
when others =>
Error_Kind ("get_deep_range_expression(1)", T);
end case;
R := Get_Range_Constraint (T);
case Get_Kind (R) is
when Iir_Kind_Range_Expression =>
Rng := R;
return;
when Iir_Kind_Range_Array_Attribute =>
null;
when Iir_Kind_Reverse_Range_Array_Attribute =>
Is_Reverse := not Is_Reverse;
when others =>
Error_Kind ("get_deep_range_expression(2)", R);
end case;
T := Get_Index_Subtype (R);
if T = Null_Iir then
Rng := Null_Iir;
return;
end if;
end loop;
end Get_Deep_Range_Expression;
function Translate_Index_To_Offset (Rng : Mnode;
Index : O_Enode;
Index_Expr : Iir;
Range_Type : Iir;
Loc : Iir)
return O_Enode
is
Need_Check : Boolean;
Dir : O_Enode;
If_Blk : O_If_Block;
Res : O_Dnode;
Off : O_Dnode;
Bound : O_Enode;
Cond1, Cond2 : O_Enode;
Index_Node : O_Dnode;
Bound_Node : O_Dnode;
Index_Info : Type_Info_Acc;
Deep_Rng : Iir;
Deep_Reverse : Boolean;
begin
Index_Info := Get_Info (Get_Base_Type (Range_Type));
if Index_Expr = Null_Iir then
Need_Check := True;
Deep_Rng := Null_Iir;
Deep_Reverse := False;
else
Need_Check := Need_Index_Check (Get_Type (Index_Expr), Range_Type);
Get_Deep_Range_Expression (Range_Type, Deep_Rng, Deep_Reverse);
end if;
Res := Create_Temp (Ghdl_Index_Type);
Open_Temp;
Off := Create_Temp (Index_Info.Ortho_Type (Mode_Value));
Bound := M2E (Chap3.Range_To_Left (Rng));
if Deep_Rng /= Null_Iir then
if Get_Direction (Deep_Rng) = Iir_To xor Deep_Reverse then
-- Direction TO: INDEX - LEFT.
New_Assign_Stmt (New_Obj (Off),
New_Dyadic_Op (ON_Sub_Ov,
Index, Bound));
else
-- Direction DOWNTO: LEFT - INDEX.
New_Assign_Stmt (New_Obj (Off),
New_Dyadic_Op (ON_Sub_Ov,
Bound, Index));
end if;
else
Index_Node := Create_Temp_Init
(Index_Info.Ortho_Type (Mode_Value), Index);
Bound_Node := Create_Temp_Init
(Index_Info.Ortho_Type (Mode_Value), Bound);
Dir := M2E (Chap3.Range_To_Dir (Rng));
-- Non-static direction.
Start_If_Stmt (If_Blk,
New_Compare_Op (ON_Eq, Dir,
New_Lit (Ghdl_Dir_To_Node),
Ghdl_Bool_Type));
-- Direction TO: INDEX - LEFT.
New_Assign_Stmt (New_Obj (Off),
New_Dyadic_Op (ON_Sub_Ov,
New_Obj_Value (Index_Node),
New_Obj_Value (Bound_Node)));
New_Else_Stmt (If_Blk);
-- Direction DOWNTO: LEFT - INDEX.
New_Assign_Stmt (New_Obj (Off),
New_Dyadic_Op (ON_Sub_Ov,
New_Obj_Value (Bound_Node),
New_Obj_Value (Index_Node)));
Finish_If_Stmt (If_Blk);
end if;
-- Get the offset.
New_Assign_Stmt
(New_Obj (Res), New_Convert_Ov (New_Obj_Value (Off),
Ghdl_Index_Type));
-- Check bounds.
if Need_Check then
Cond1 := New_Compare_Op
(ON_Lt,
New_Obj_Value (Off),
New_Lit (New_Signed_Literal (Index_Info.Ortho_Type (Mode_Value),
0)),
Ghdl_Bool_Type);
Cond2 := New_Compare_Op
(ON_Ge,
New_Obj_Value (Res),
M2E (Chap3.Range_To_Length (Rng)),
Ghdl_Bool_Type);
Check_Bound_Error (New_Dyadic_Op (ON_Or, Cond1, Cond2), Loc, 0);
end if;
Close_Temp;
return New_Obj_Value (Res);
end Translate_Index_To_Offset;
-- Translate index EXPR in dimension DIM of thin array into an
-- offset.
-- This checks bounds.
function Translate_Thin_Index_Offset (Index_Type : Iir;
Dim : Natural;
Expr : Iir)
return O_Enode
is
Index_Range : constant Iir := Get_Range_Constraint (Index_Type);
Obound : O_Cnode;
Res : O_Dnode;
Cond2 : O_Enode;
Index : O_Enode;
Index_Base_Type : Iir;
V : Iir_Int64;
B : Iir_Int64;
begin
B := Eval_Pos (Get_Left_Limit (Index_Range));
if Get_Expr_Staticness (Expr) = Locally then
V := Eval_Pos (Eval_Static_Expr (Expr));
if Get_Direction (Index_Range) = Iir_To then
B := V - B;
else
B := B - V;
end if;
return New_Lit
(New_Unsigned_Literal (Ghdl_Index_Type, Unsigned_64 (B)));
else
Index_Base_Type := Get_Base_Type (Index_Type);
Index := Chap7.Translate_Expression (Expr, Index_Base_Type);
if Get_Direction (Index_Range) = Iir_To then
-- Direction TO: INDEX - LEFT.
if B /= 0 then
Obound := Chap7.Translate_Static_Range_Left
(Index_Range, Index_Base_Type);
Index := New_Dyadic_Op (ON_Sub_Ov, Index, New_Lit (Obound));
end if;
else
-- Direction DOWNTO: LEFT - INDEX.
Obound := Chap7.Translate_Static_Range_Left
(Index_Range, Index_Base_Type);
Index := New_Dyadic_Op (ON_Sub_Ov, New_Lit (Obound), Index);
end if;
-- Get the offset.
Index := New_Convert_Ov (Index, Ghdl_Index_Type);
-- Since the value is unsigned, both left and right bounds are
-- checked in the same time.
if Get_Type (Expr) /= Index_Type then
Res := Create_Temp_Init (Ghdl_Index_Type, Index);
Cond2 := New_Compare_Op
(ON_Ge, New_Obj_Value (Res),
New_Lit (Chap7.Translate_Static_Range_Length (Index_Range)),
Ghdl_Bool_Type);
Check_Bound_Error (Cond2, Expr, Dim);
Index := New_Obj_Value (Res);
end if;
return Index;
end if;
end Translate_Thin_Index_Offset;
-- Translate an indexed name.
type Indexed_Name_Data is record
Offset : O_Dnode;
Res : Mnode;
end record;
function Translate_Indexed_Name_Init (Prefix_Orig : Mnode; Expr : Iir)
return Indexed_Name_Data
is
Prefix_Type : constant Iir := Get_Type (Get_Prefix (Expr));
Prefix_Info : constant Type_Info_Acc := Get_Info (Prefix_Type);
Index_List : constant Iir_List := Get_Index_List (Expr);
Type_List : constant Iir_List := Get_Index_Subtype_List (Prefix_Type);
Nbr_Dim : constant Natural := Get_Nbr_Elements (Index_List);
Prefix : Mnode;
Index : Iir;
Offset : O_Dnode;
R : O_Enode;
Length : O_Enode;
Itype : Iir;
Ibasetype : Iir;
Range_Ptr : Mnode;
begin
case Prefix_Info.Type_Mode is
when Type_Mode_Fat_Array =>
Prefix := Stabilize (Prefix_Orig);
when Type_Mode_Array =>
Prefix := Prefix_Orig;
when others =>
raise Internal_Error;
end case;
Offset := Create_Temp (Ghdl_Index_Type);
for Dim in 1 .. Nbr_Dim loop
Index := Get_Nth_Element (Index_List, Dim - 1);
Itype := Get_Index_Type (Type_List, Dim - 1);
Ibasetype := Get_Base_Type (Itype);
Open_Temp;
-- Compute index for the current dimension.
case Prefix_Info.Type_Mode is
when Type_Mode_Fat_Array =>
Range_Ptr := Stabilize
(Chap3.Get_Array_Range (Prefix, Prefix_Type, Dim));
R := Translate_Index_To_Offset
(Range_Ptr,
Chap7.Translate_Expression (Index, Ibasetype),
Null_Iir, Itype, Index);
when Type_Mode_Array =>
if Prefix_Info.Type_Locally_Constrained then
R := Translate_Thin_Index_Offset (Itype, Dim, Index);
else
-- Manually extract range since there is no infos for
-- index subtype.
Range_Ptr := Chap3.Bounds_To_Range
(Chap3.Get_Array_Type_Bounds (Prefix_Type),
Prefix_Type, Dim);
Stabilize (Range_Ptr);
R := Translate_Index_To_Offset
(Range_Ptr,
Chap7.Translate_Expression (Index, Ibasetype),
Index, Itype, Index);
end if;
when others =>
raise Internal_Error;
end case;
if Dim = 1 then
-- First dimension.
New_Assign_Stmt (New_Obj (Offset), R);
else
-- If there are more dimension(s) to follow, then multiply
-- the current offset by the length of the current dimension.
if Prefix_Info.Type_Locally_Constrained then
Length := New_Lit (Chap7.Translate_Static_Range_Length
(Get_Range_Constraint (Itype)));
else
Length := M2E (Chap3.Range_To_Length (Range_Ptr));
end if;
New_Assign_Stmt
(New_Obj (Offset),
New_Dyadic_Op (ON_Add_Ov,
New_Dyadic_Op (ON_Mul_Ov,
New_Obj_Value (Offset),
Length),
R));
end if;
Close_Temp;
end loop;
return (Offset => Offset,
Res => Chap3.Index_Base
(Chap3.Get_Array_Base (Prefix), Prefix_Type,
New_Obj_Value (Offset)));
end Translate_Indexed_Name_Init;
function Translate_Indexed_Name_Finish
(Prefix : Mnode; Expr : Iir; Data : Indexed_Name_Data)
return Mnode
is
begin
return Chap3.Index_Base (Chap3.Get_Array_Base (Prefix),
Get_Type (Get_Prefix (Expr)),
New_Obj_Value (Data.Offset));
end Translate_Indexed_Name_Finish;
function Translate_Indexed_Name (Prefix : Mnode; Expr : Iir)
return Mnode
is
begin
return Translate_Indexed_Name_Init (Prefix, Expr).Res;
end Translate_Indexed_Name;
type Slice_Name_Data is record
Off : Unsigned_64;
Is_Off : Boolean;
Unsigned_Diff : O_Dnode;
-- Variable pointing to the prefix.
Prefix_Var : Mnode;
-- Variable pointing to slice.
Slice_Range : Mnode;
end record;
procedure Translate_Slice_Name_Init
(Prefix : Mnode; Expr : Iir_Slice_Name; Data : out Slice_Name_Data)
is
-- Type of the prefix.
Prefix_Type : constant Iir := Get_Type (Get_Prefix (Expr));
-- Type info of the prefix.
Prefix_Info : Type_Info_Acc;
-- Type of the first (and only) index of the prefix array type.
Index_Type : constant Iir := Get_Index_Type (Prefix_Type, 0);
-- Type of the slice.
Slice_Type : constant Iir := Get_Type (Expr);
Slice_Info : Type_Info_Acc;
-- True iff the direction of the slice is known at compile time.
Static_Range : Boolean;
-- Suffix of the slice (discrete range).
Expr_Range : constant Iir := Get_Suffix (Expr);
-- Variable pointing to the prefix.
Prefix_Var : Mnode;
-- Type info of the range base type.
Index_Info : Type_Info_Acc;
-- Variables pointing to slice and prefix ranges.
Slice_Range : Mnode;
Prefix_Range : Mnode;
Diff : O_Dnode;
Unsigned_Diff : O_Dnode;
If_Blk, If_Blk1 : O_If_Block;
begin
-- Evaluate slice bounds.
Chap3.Create_Array_Subtype (Slice_Type, True);
-- The info may have just been created.
Prefix_Info := Get_Info (Prefix_Type);
Slice_Info := Get_Info (Slice_Type);
if Slice_Info.Type_Mode = Type_Mode_Array
and then Slice_Info.Type_Locally_Constrained
and then Prefix_Info.Type_Mode = Type_Mode_Array
and then Prefix_Info.Type_Locally_Constrained
then
Data.Is_Off := True;
Data.Prefix_Var := Prefix;
-- Both prefix and result are constrained array.
declare
Prefix_Left, Slice_Left : Iir_Int64;
Off : Iir_Int64;
Slice_Index_Type : Iir;
Slice_Range : Iir;
Slice_Length : Iir_Int64;
Index_Range : Iir;
begin
Index_Range := Get_Range_Constraint (Index_Type);
Prefix_Left := Eval_Pos (Get_Left_Limit (Index_Range));
Slice_Index_Type := Get_Index_Type (Slice_Type, 0);
Slice_Range := Get_Range_Constraint (Slice_Index_Type);
Slice_Left := Eval_Pos (Get_Left_Limit (Slice_Range));
Slice_Length := Eval_Discrete_Range_Length (Slice_Range);
if Slice_Length = 0 then
-- Null slice.
Data.Off := 0;
return;
end if;
if Get_Direction (Index_Range) /= Get_Direction (Slice_Range)
then
-- This is allowed with vhdl87
Off := 0;
Slice_Length := 0;
else
-- Both prefix and slice are thin array.
case Get_Direction (Index_Range) is
when Iir_To =>
Off := Slice_Left - Prefix_Left;
when Iir_Downto =>
Off := Prefix_Left - Slice_Left;
end case;
if Off < 0 then
-- Must have been caught by sem.
raise Internal_Error;
end if;
if Off + Slice_Length
> Eval_Discrete_Range_Length (Index_Range)
then
-- Must have been caught by sem.
raise Internal_Error;
end if;
end if;
Data.Off := Unsigned_64 (Off);
return;
end;
end if;
Data.Is_Off := False;
-- Save prefix.
Prefix_Var := Stabilize (Prefix);
Index_Info := Get_Info (Get_Base_Type (Index_Type));
-- Save prefix bounds.
Prefix_Range := Stabilize
(Chap3.Get_Array_Range (Prefix_Var, Prefix_Type, 1));
-- Save slice bounds.
Slice_Range := Stabilize
(Chap3.Bounds_To_Range (Chap3.Get_Array_Type_Bounds (Slice_Type),
Slice_Type, 1));
-- TRUE if the direction of the slice is known.
Static_Range := Get_Kind (Expr_Range) = Iir_Kind_Range_Expression;
-- Check direction against same direction, error if different.
-- FIXME: what about v87 -> if different then null slice
if not Static_Range
or else Get_Kind (Prefix_Type) /= Iir_Kind_Array_Subtype_Definition
then
-- Check same direction.
Check_Bound_Error
(New_Compare_Op (ON_Neq,
M2E (Chap3.Range_To_Dir (Prefix_Range)),
M2E (Chap3.Range_To_Dir (Slice_Range)),
Ghdl_Bool_Type),
Expr, 1);
end if;
Unsigned_Diff := Create_Temp (Ghdl_Index_Type);
-- Check if not a null slice.
-- The bounds of a null slice may be out of range. So DIFF cannot
-- be computed by substraction.
Start_If_Stmt
(If_Blk,
New_Compare_Op
(ON_Eq,
M2E (Chap3.Range_To_Length (Slice_Range)),
New_Lit (Ghdl_Index_0),
Ghdl_Bool_Type));
New_Assign_Stmt (New_Obj (Unsigned_Diff), New_Lit (Ghdl_Index_0));
New_Else_Stmt (If_Blk);
Diff := Create_Temp (Index_Info.Ortho_Type (Mode_Value));
-- Compute the offset in the prefix.
if not Static_Range then
Start_If_Stmt
(If_Blk1, New_Compare_Op (ON_Eq,
M2E (Chap3.Range_To_Dir (Slice_Range)),
New_Lit (Ghdl_Dir_To_Node),
Ghdl_Bool_Type));
end if;
if not Static_Range or else Get_Direction (Expr_Range) = Iir_To then
-- Diff = slice - bounds.
New_Assign_Stmt
(New_Obj (Diff),
New_Dyadic_Op (ON_Sub_Ov,
M2E (Chap3.Range_To_Left (Slice_Range)),
M2E (Chap3.Range_To_Left (Prefix_Range))));
end if;
if not Static_Range then
New_Else_Stmt (If_Blk1);
end if;
if not Static_Range or else Get_Direction (Expr_Range) = Iir_Downto
then
-- Diff = bounds - slice.
New_Assign_Stmt
(New_Obj (Diff),
New_Dyadic_Op (ON_Sub_Ov,
M2E (Chap3.Range_To_Left (Prefix_Range)),
M2E (Chap3.Range_To_Left (Slice_Range))));
end if;
if not Static_Range then
Finish_If_Stmt (If_Blk1);
end if;
-- Note: this also check for overflow.
New_Assign_Stmt
(New_Obj (Unsigned_Diff),
New_Convert_Ov (New_Obj_Value (Diff), Ghdl_Index_Type));
-- Check bounds.
declare
Err_1 : O_Enode;
Err_2 : O_Enode;
begin
-- Bounds error if left of slice is before left of prefix.
Err_1 := New_Compare_Op
(ON_Lt,
New_Obj_Value (Diff),
New_Lit (New_Signed_Literal (Index_Info.Ortho_Type (Mode_Value),
0)),
Ghdl_Bool_Type);
-- Bounds error if right of slice is after right of prefix.
Err_2 := New_Compare_Op
(ON_Gt,
New_Dyadic_Op (ON_Add_Ov,
New_Obj_Value (Unsigned_Diff),
M2E (Chap3.Range_To_Length (Slice_Range))),
M2E (Chap3.Range_To_Length (Prefix_Range)),
Ghdl_Bool_Type);
Check_Bound_Error (New_Dyadic_Op (ON_Or, Err_1, Err_2), Expr, 1);
end;
Finish_If_Stmt (If_Blk);
Data.Slice_Range := Slice_Range;
Data.Prefix_Var := Prefix_Var;
Data.Unsigned_Diff := Unsigned_Diff;
Data.Is_Off := False;
end Translate_Slice_Name_Init;
function Translate_Slice_Name_Finish
(Prefix : Mnode; Expr : Iir_Slice_Name; Data : Slice_Name_Data)
return Mnode
is
-- Type of the slice.
Slice_Type : constant Iir := Get_Type (Expr);
Slice_Info : constant Type_Info_Acc := Get_Info (Slice_Type);
-- Object kind of the prefix.
Kind : constant Object_Kind_Type := Get_Object_Kind (Prefix);
Res_D : O_Dnode;
begin
if Data.Is_Off then
return Chap3.Slice_Base
(Prefix, Slice_Type, New_Lit (New_Unsigned_Literal
(Ghdl_Index_Type, Data.Off)));
else
-- Create the result (fat array) and assign the bounds field.
case Slice_Info.Type_Mode is
when Type_Mode_Fat_Array =>
Res_D := Create_Temp (Slice_Info.Ortho_Type (Kind));
New_Assign_Stmt
(New_Selected_Element (New_Obj (Res_D),
Slice_Info.T.Bounds_Field (Kind)),
New_Value (M2Lp (Data.Slice_Range)));
New_Assign_Stmt
(New_Selected_Element (New_Obj (Res_D),
Slice_Info.T.Base_Field (Kind)),
M2E (Chap3.Slice_Base
(Chap3.Get_Array_Base (Prefix),
Slice_Type,
New_Obj_Value (Data.Unsigned_Diff))));
return Dv2M (Res_D, Slice_Info, Kind);
when Type_Mode_Array =>
return Chap3.Slice_Base
(Chap3.Get_Array_Base (Prefix),
Slice_Type,
New_Obj_Value (Data.Unsigned_Diff));
when others =>
raise Internal_Error;
end case;
end if;
end Translate_Slice_Name_Finish;
function Translate_Slice_Name (Prefix : Mnode; Expr : Iir_Slice_Name)
return Mnode
is
Data : Slice_Name_Data;
begin
Translate_Slice_Name_Init (Prefix, Expr, Data);
return Translate_Slice_Name_Finish (Data.Prefix_Var, Expr, Data);
end Translate_Slice_Name;
function Translate_Interface_Name
(Inter : Iir; Info : Ortho_Info_Acc; Kind : Object_Kind_Type)
return Mnode
is
Type_Info : constant Type_Info_Acc := Get_Info (Get_Type (Inter));
begin
case Info.Kind is
when Kind_Object =>
-- For a generic or a port.
return Get_Var (Info.Object_Var, Type_Info, Kind);
when Kind_Interface =>
-- For a parameter.
if Info.Interface_Field = O_Fnode_Null then
-- Normal case: the parameter was translated as an ortho
-- interface.
case Type_Info.Type_Mode is
when Type_Mode_Unknown =>
raise Internal_Error;
when Type_Mode_By_Value =>
return Dv2M (Info.Interface_Node, Type_Info, Kind);
when Type_Mode_By_Copy
| Type_Mode_By_Ref =>
-- Parameter is passed by reference.
return Dp2M (Info.Interface_Node, Type_Info, Kind);
end case;
else
-- The parameter was put somewhere else.
declare
Subprg : constant Iir := Get_Parent (Inter);
Subprg_Info : constant Subprg_Info_Acc :=
Get_Info (Subprg);
Linter : O_Lnode;
begin
if Info.Interface_Node = O_Dnode_Null then
-- The parameter is passed via a field of the PARAMS
-- record parameter.
if Subprg_Info.Subprg_Params_Var = Null_Var then
-- Direct access to the parameter.
Linter := New_Obj (Subprg_Info.Res_Interface);
else
-- Unnesting case: upscope access.
Linter := Get_Var (Subprg_Info.Subprg_Params_Var);
end if;
Linter := New_Selected_Element
(New_Acc_Value (Linter), Info.Interface_Field);
else
-- Unnesting case: the parameter was copied in the
-- subprogram frame so that nested subprograms can
-- reference it. Use field in FRAME.
Linter := New_Selected_Element
(Get_Instance_Ref (Subprg_Info.Subprg_Frame_Scope),
Info.Interface_Field);
end if;
case Type_Info.Type_Mode is
when Type_Mode_Unknown =>
raise Internal_Error;
when Type_Mode_By_Value =>
return Lv2M (Linter, Type_Info, Kind);
when Type_Mode_By_Copy
| Type_Mode_By_Ref =>
-- Parameter is passed by reference.
return Lp2M (Linter, Type_Info, Kind);
end case;
end;
end if;
when others =>
raise Internal_Error;
end case;
end Translate_Interface_Name;
function Translate_Selected_Element (Prefix : Mnode;
El : Iir_Element_Declaration)
return Mnode
is
El_Info : constant Field_Info_Acc := Get_Info (El);
El_Type : constant Iir := Get_Type (El);
El_Tinfo : constant Type_Info_Acc := Get_Info (El_Type);
Kind : constant Object_Kind_Type := Get_Object_Kind (Prefix);
Stable_Prefix : Mnode;
begin
if Is_Complex_Type (El_Tinfo) then
-- The element is in fact an offset.
Stable_Prefix := Stabilize (Prefix);
return E2M
(New_Unchecked_Address
(New_Slice
(New_Access_Element
(New_Unchecked_Address
(M2Lv (Stable_Prefix), Char_Ptr_Type)),
Chararray_Type,
New_Value
(New_Selected_Element (M2Lv (Stable_Prefix),
El_Info.Field_Node (Kind)))),
El_Tinfo.Ortho_Ptr_Type (Kind)),
El_Tinfo, Kind);
else
return Lv2M (New_Selected_Element (M2Lv (Prefix),
El_Info.Field_Node (Kind)),
El_Tinfo, Kind);
end if;
end Translate_Selected_Element;
-- function Translate_Formal_Interface_Name (Scope_Type : O_Tnode;
-- Scope_Param : O_Lnode;
-- Name : Iir;
-- Kind : Object_Kind_Type)
-- return Mnode
-- is
-- Type_Info : Type_Info_Acc;
-- Info : Ortho_Info_Acc;
-- Res : Mnode;
-- begin
-- Type_Info := Get_Info (Get_Type (Name));
-- Info := Get_Info (Name);
-- Push_Scope_Soft (Scope_Type, Scope_Param);
-- Res := Get_Var (Info.Object_Var, Type_Info, Kind);
-- Clear_Scope_Soft (Scope_Type);
-- return Res;
-- end Translate_Formal_Interface_Name;
-- function Translate_Formal_Name (Scope_Type : O_Tnode;
-- Scope_Param : O_Lnode;
-- Name : Iir)
-- return Mnode
-- is
-- Prefix : Iir;
-- Prefix_Name : Mnode;
-- begin
-- case Get_Kind (Name) is
-- when Iir_Kind_Interface_Constant_Declaration =>
-- return Translate_Formal_Interface_Name
-- (Scope_Type, Scope_Param, Name, Mode_Value);
-- when Iir_Kind_Interface_Signal_Declaration =>
-- return Translate_Formal_Interface_Name
-- (Scope_Type, Scope_Param, Name, Mode_Signal);
-- when Iir_Kind_Indexed_Name =>
-- Prefix := Get_Prefix (Name);
-- Prefix_Name := Translate_Formal_Name
-- (Scope_Type, Scope_Param, Prefix);
-- return Translate_Indexed_Name (Prefix_Name, Name);
-- when Iir_Kind_Slice_Name =>
-- Prefix := Get_Prefix (Name);
-- Prefix_Name := Translate_Formal_Name
-- (Scope_Type, Scope_Param, Prefix);
-- return Translate_Slice_Name (Prefix_Name, Name);
-- when Iir_Kind_Selected_Element =>
-- Prefix := Get_Prefix (Name);
-- Prefix_Name := Translate_Formal_Name
-- (Scope_Type, Scope_Param, Prefix);
-- return Translate_Selected_Element
-- (Prefix_Name, Get_Selected_Element (Name));
-- when others =>
-- Error_Kind ("translate_generic_name", Name);
-- end case;
-- end Translate_Formal_Name;
function Translate_Name (Name : Iir) return Mnode
is
Name_Type : constant Iir := Get_Type (Name);
Name_Info : constant Ortho_Info_Acc := Get_Info (Name);
Type_Info : constant Type_Info_Acc := Get_Info (Name_Type);
begin
case Get_Kind (Name) is
when Iir_Kind_Constant_Declaration
| Iir_Kind_Variable_Declaration
| Iir_Kind_File_Declaration =>
return Get_Var (Name_Info.Object_Var, Type_Info, Mode_Value);
when Iir_Kind_Attribute_Name =>
return Translate_Name (Get_Named_Entity (Name));
when Iir_Kind_Attribute_Value =>
return Get_Var
(Get_Info (Get_Attribute_Specification (Name)).Object_Var,
Type_Info, Mode_Value);
when Iir_Kind_Object_Alias_Declaration =>
-- Alias_Var is not like an object variable, since it is
-- always a pointer to the aliased object.
declare
R : O_Lnode;
begin
R := Get_Var (Name_Info.Alias_Var);
case Type_Info.Type_Mode is
when Type_Mode_Fat_Array =>
return Get_Var (Name_Info.Alias_Var, Type_Info,
Name_Info.Alias_Kind);
when Type_Mode_Array
| Type_Mode_Record
| Type_Mode_Acc
| Type_Mode_Fat_Acc =>
R := Get_Var (Name_Info.Alias_Var);
return Lp2M (R, Type_Info, Name_Info.Alias_Kind);
when Type_Mode_Scalar =>
R := Get_Var (Name_Info.Alias_Var);
if Name_Info.Alias_Kind = Mode_Signal then
return Lv2M (R, Type_Info, Name_Info.Alias_Kind);
else
return Lp2M (R, Type_Info, Name_Info.Alias_Kind);
end if;
when others =>
raise Internal_Error;
end case;
end;
when Iir_Kind_Signal_Declaration
| Iir_Kind_Stable_Attribute
| Iir_Kind_Quiet_Attribute
| Iir_Kind_Delayed_Attribute
| Iir_Kind_Transaction_Attribute
| Iir_Kind_Guard_Signal_Declaration =>
return Get_Var (Name_Info.Object_Var, Type_Info, Mode_Signal);
when Iir_Kind_Interface_Constant_Declaration =>
return Translate_Interface_Name (Name, Name_Info, Mode_Value);
when Iir_Kind_Interface_File_Declaration =>
return Translate_Interface_Name (Name, Name_Info, Mode_Value);
when Iir_Kind_Interface_Variable_Declaration =>
return Translate_Interface_Name (Name, Name_Info, Mode_Value);
when Iir_Kind_Interface_Signal_Declaration =>
return Translate_Interface_Name (Name, Name_Info, Mode_Signal);
when Iir_Kind_Indexed_Name =>
return Translate_Indexed_Name
(Translate_Name (Get_Prefix (Name)), Name);
when Iir_Kind_Slice_Name =>
return Translate_Slice_Name
(Translate_Name (Get_Prefix (Name)), Name);
when Iir_Kind_Dereference
| Iir_Kind_Implicit_Dereference =>
declare
Pfx : O_Enode;
begin
Pfx := Chap7.Translate_Expression (Get_Prefix (Name));
-- FIXME: what about fat pointer ??
return Lv2M (New_Access_Element (Pfx),
Type_Info, Mode_Value);
end;
when Iir_Kind_Selected_Element =>
return Translate_Selected_Element
(Translate_Name (Get_Prefix (Name)),
Get_Selected_Element (Name));
when Iir_Kind_Function_Call =>
-- This can appear as a prefix of a name, therefore, the
-- result is always a composite type or an access type.
declare
Imp : constant Iir := Get_Implementation (Name);
Obj : Iir;
Assoc_Chain : Iir;
begin
if Is_Implicit_Subprogram (Imp) then
-- FIXME : to be done
raise Internal_Error;
else
Canon.Canon_Subprogram_Call (Name);
Assoc_Chain := Get_Parameter_Association_Chain (Name);
Obj := Get_Method_Object (Name);
return E2M
(Chap8.Translate_Subprogram_Call (Imp, Assoc_Chain, Obj),
Type_Info, Mode_Value);
end if;
end;
when Iir_Kind_Image_Attribute =>
-- Can appear as a prefix.
return E2M (Chap14.Translate_Image_Attribute (Name),
Type_Info, Mode_Value);
when Iir_Kind_Simple_Name
| Iir_Kind_Selected_Name =>
return Translate_Name (Get_Named_Entity (Name));
when others =>
Error_Kind ("translate_name", Name);
end case;
end Translate_Name;
procedure Translate_Direct_Driver
(Name : Iir; Sig : out Mnode; Drv : out Mnode)
is
Name_Type : constant Iir := Get_Type (Name);
Name_Info : constant Ortho_Info_Acc := Get_Info (Name);
Type_Info : constant Type_Info_Acc := Get_Info (Name_Type);
begin
case Get_Kind (Name) is
when Iir_Kind_Simple_Name
| Iir_Kind_Selected_Name =>
Translate_Direct_Driver (Get_Named_Entity (Name), Sig, Drv);
when Iir_Kind_Object_Alias_Declaration =>
Translate_Direct_Driver (Get_Name (Name), Sig, Drv);
when Iir_Kind_Signal_Declaration
| Iir_Kind_Interface_Signal_Declaration =>
Sig := Get_Var (Name_Info.Object_Var, Type_Info, Mode_Signal);
Drv := Get_Var (Name_Info.Object_Driver, Type_Info, Mode_Value);
when Iir_Kind_Slice_Name =>
declare
Data : Slice_Name_Data;
Pfx_Sig : Mnode;
Pfx_Drv : Mnode;
begin
Translate_Direct_Driver
(Get_Prefix (Name), Pfx_Sig, Pfx_Drv);
Translate_Slice_Name_Init (Pfx_Sig, Name, Data);
Sig := Translate_Slice_Name_Finish
(Data.Prefix_Var, Name, Data);
Drv := Translate_Slice_Name_Finish (Pfx_Drv, Name, Data);
end;
when Iir_Kind_Indexed_Name =>
declare
Data : Indexed_Name_Data;
Pfx_Sig : Mnode;
Pfx_Drv : Mnode;
begin
Translate_Direct_Driver
(Get_Prefix (Name), Pfx_Sig, Pfx_Drv);
Data := Translate_Indexed_Name_Init (Pfx_Sig, Name);
Sig := Data.Res;
Drv := Translate_Indexed_Name_Finish (Pfx_Drv, Name, Data);
end;
when Iir_Kind_Selected_Element =>
declare
El : Iir;
Pfx_Sig : Mnode;
Pfx_Drv : Mnode;
begin
Translate_Direct_Driver
(Get_Prefix (Name), Pfx_Sig, Pfx_Drv);
El := Get_Selected_Element (Name);
Sig := Translate_Selected_Element (Pfx_Sig, El);
Drv := Translate_Selected_Element (Pfx_Drv, El);
end;
when others =>
Error_Kind ("translate_direct_driver", Name);
end case;
end Translate_Direct_Driver;
end Trans.Chap6;
|