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
|
-- Cross references.
-- Copyright (C) 2002, 2003, 2004, 2005 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 GNAT.Table;
with GNAT.Heap_Sort_A;
with Flags;
with Std_Package;
with Errorout; use Errorout;
with Nodes;
package body Xrefs is
type Xref_Type is record
-- Where the cross-reference (or the name) appears.
Loc : Location_Type;
-- What the name refer to.
Ref : Iir;
-- Kind of reference (See package specification).
Kind : Xref_Kind;
end record;
package Xref_Table is new GNAT.Table
(Table_Index_Type => Natural,
Table_Component_Type => Xref_Type,
Table_Low_Bound => 0,
Table_Initial => 128,
Table_Increment => 100);
function Get_Xref_Location (N : Xref) return Location_Type is
begin
return Xref_Table.Table (N).Loc;
end Get_Xref_Location;
function Get_Xref_Kind (N : Xref) return Xref_Kind is
begin
return Xref_Table.Table (N).Kind;
end Get_Xref_Kind;
function Get_Xref_Node (N : Xref) return Iir is
begin
return Xref_Table.Table (N).Ref;
end Get_Xref_Node;
function Get_Last_Xref return Xref is
begin
return Xref_Table.Last;
end Get_Last_Xref;
procedure Init is
begin
Xref_Table.Set_Last (Bad_Xref);
end Init;
procedure Add_Xref (Loc : Location_Type; Ref : Iir; Kind : Xref_Kind) is
begin
Xref_Table.Append (Xref_Type'(Loc => Loc,
Ref => Ref,
Kind => Kind));
end Add_Xref;
procedure Xref_Decl (Decl : Iir) is
begin
if Flags.Flag_Xref then
Add_Xref (Get_Location (Decl), Decl, Xref_Decl);
end if;
end Xref_Decl;
procedure Xref_Ref (Name : Iir; Decl : Iir) is
begin
if Flags.Flag_Xref then
Add_Xref (Get_Location (Name), Decl, Xref_Ref);
end if;
end Xref_Ref;
procedure Xref_Body (Bod : Iir; Spec : Iir) is
begin
if Flags.Flag_Xref then
Add_Xref (Get_Location (Bod), Spec, Xref_Body);
end if;
end Xref_Body;
procedure Xref_End (Loc : Location_Type; Decl : Iir) is
begin
if Flags.Flag_Xref then
Add_Xref (Loc, Decl, Xref_End);
end if;
end Xref_End;
procedure Xref_Name_1 (Name : Iir)
is
Res : Iir;
begin
case Get_Kind (Name) is
when Iir_Kind_Simple_Name
| Iir_Kind_Selected_Name
| Iir_Kind_Operator_Symbol =>
Res := Get_Named_Entity (Name);
if Res = Std_Package.Error_Mark then
return;
end if;
Add_Xref (Get_Location (Name), Res, Xref_Ref);
when Iir_Kind_Parenthesis_Name
| Iir_Kind_Selected_By_All_Name
| Iir_Kind_Slice_Name =>
null;
when Iir_Kind_Attribute_Name =>
-- FIXME: user defined attributes.
null;
when others =>
Error_Kind ("xref_name_1", Name);
end case;
case Get_Kind (Name) is
when Iir_Kind_Simple_Name
| Iir_Kind_Operator_Symbol =>
null;
when Iir_Kind_Selected_Name
| Iir_Kind_Parenthesis_Name
| Iir_Kind_Attribute_Name
| Iir_Kind_Slice_Name
| Iir_Kind_Selected_By_All_Name =>
Xref_Name_1 (Get_Prefix (Name));
when others =>
Error_Kind ("xref_name_1", Name);
end case;
end Xref_Name_1;
procedure Xref_Name (Name : Iir) is
begin
if Flags.Flag_Xref and Name /= Null_Iir then
Xref_Name_1 (Name);
end if;
end Xref_Name;
procedure Move (From : Natural; To : Natural)
is
Tmp : Xref_Type;
begin
Tmp := Xref_Table.Table (To);
Xref_Table.Table (To) := Xref_Table.Table (From);
Xref_Table.Table (From) := Tmp;
end Move;
function Loc_Lt (Op1, Op2 : Natural) return Boolean is
begin
return Xref_Table.Table (Op1).Loc < Xref_Table.Table (Op2).Loc;
end Loc_Lt;
procedure Sort_By_Location is
begin
GNAT.Heap_Sort_A.Sort (Xref_Table.Last, Move'Access, Loc_Lt'Access);
end Sort_By_Location;
-- Sorting function by ref field.
-- If ref fields are the same, then compare by location.
function Node_Lt (Op1, Op2 : Natural) return Boolean
is
L1, L2 : Location_Type;
N1, N2 : Iir;
K1, K2 : Xref_Kind;
begin
L1 := Get_Location (Get_Xref_Node (Op1));
L2 := Get_Location (Get_Xref_Node (Op2));
if L1 /= L2 then
return L1 < L2;
end if;
-- L1 = L2.
-- Note: nodes of std_standard have the same location. FIXME ?
N1 := Get_Xref_Node (Op1);
N2 := Get_Xref_Node (Op2);
if Iirs."/=" (N1, N2) then
return Nodes."<" (N1, N2);
end if;
-- Try to get declaration first.
K1 := Get_Xref_Kind (Op1);
K2 := Get_Xref_Kind (Op2);
if K1 /= K2 then
return K1 < K2;
end if;
L1 := Get_Xref_Location (Op1);
L2 := Get_Xref_Location (Op2);
return L1 < L2;
end Node_Lt;
procedure Sort_By_Node_Location is
begin
GNAT.Heap_Sort_A.Sort (Xref_Table.Last, Move'Access, Node_Lt'Access);
end Sort_By_Node_Location;
function Find (Loc : Location_Type) return Xref
is
Low : Xref;
High : Xref;
Mid : Xref;
Mid_Loc : Location_Type;
begin
Low := First_Xref;
High := Xref_Table.Last;
loop
Mid := (Low + High + 1) / 2;
Mid_Loc := Xref_Table.Table (Mid).Loc;
if Loc = Mid_Loc then
return Mid;
end if;
if Mid = Low then
return Bad_Xref;
end if;
if Loc > Mid_Loc then
Low := Mid + 1;
else
High := Mid - 1;
end if;
end loop;
end Find;
procedure Fix_End_Xrefs
is
N : Iir;
begin
for I in First_Xref .. Get_Last_Xref loop
if Get_Xref_Kind (I) = Xref_End then
N := Get_Xref_Node (I);
case Get_Kind (N) is
when Iir_Kind_Function_Body
| Iir_Kind_Procedure_Body =>
Xref_Table.Table (I).Ref := Get_Subprogram_Specification (N);
when others =>
null;
end case;
end if;
end loop;
end Fix_End_Xrefs;
end Xrefs;
|