]>
Commit | Line | Data |
---|---|---|
70482933 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- E X P _ P A K D -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
fbf5a39b | 9 | -- Copyright (C) 1992-2003 Free Software Foundation, Inc. -- |
70482933 RK |
10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
13 | -- ware Foundation; either version 2, or (at your option) any later ver- -- | |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- | |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
18 | -- Public License distributed with GNAT; see file COPYING. If not, write -- | |
19 | -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, -- | |
20 | -- MA 02111-1307, USA. -- | |
21 | -- -- | |
22 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 23 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
70482933 RK |
24 | -- -- |
25 | ------------------------------------------------------------------------------ | |
26 | ||
27 | with Atree; use Atree; | |
28 | with Checks; use Checks; | |
29 | with Einfo; use Einfo; | |
30 | with Exp_Dbug; use Exp_Dbug; | |
31 | with Exp_Util; use Exp_Util; | |
32 | with Nlists; use Nlists; | |
33 | with Nmake; use Nmake; | |
70482933 RK |
34 | with Rtsfind; use Rtsfind; |
35 | with Sem; use Sem; | |
fbf5a39b | 36 | with Sem_Ch3; use Sem_Ch3; |
70482933 RK |
37 | with Sem_Ch8; use Sem_Ch8; |
38 | with Sem_Ch13; use Sem_Ch13; | |
39 | with Sem_Eval; use Sem_Eval; | |
40 | with Sem_Res; use Sem_Res; | |
41 | with Sem_Util; use Sem_Util; | |
42 | with Sinfo; use Sinfo; | |
43 | with Snames; use Snames; | |
44 | with Stand; use Stand; | |
45 | with Targparm; use Targparm; | |
46 | with Tbuild; use Tbuild; | |
47 | with Ttypes; use Ttypes; | |
48 | with Uintp; use Uintp; | |
49 | ||
50 | package body Exp_Pakd is | |
51 | ||
52 | --------------------------- | |
53 | -- Endian Considerations -- | |
54 | --------------------------- | |
55 | ||
56 | -- As described in the specification, bit numbering in a packed array | |
57 | -- is consistent with bit numbering in a record representation clause, | |
58 | -- and hence dependent on the endianness of the machine: | |
59 | ||
60 | -- For little-endian machines, element zero is at the right hand end | |
61 | -- (low order end) of a bit field. | |
62 | ||
63 | -- For big-endian machines, element zero is at the left hand end | |
64 | -- (high order end) of a bit field. | |
65 | ||
66 | -- The shifts that are used to right justify a field therefore differ | |
67 | -- in the two cases. For the little-endian case, we can simply use the | |
68 | -- bit number (i.e. the element number * element size) as the count for | |
69 | -- a right shift. For the big-endian case, we have to subtract the shift | |
70 | -- count from an appropriate constant to use in the right shift. We use | |
71 | -- rotates instead of shifts (which is necessary in the store case to | |
72 | -- preserve other fields), and we expect that the backend will be able | |
73 | -- to change the right rotate into a left rotate, avoiding the subtract, | |
74 | -- if the architecture provides such an instruction. | |
75 | ||
76 | ---------------------------------------------- | |
77 | -- Entity Tables for Packed Access Routines -- | |
78 | ---------------------------------------------- | |
79 | ||
80 | -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call | |
81 | -- library routines. This table is used to obtain the entity for the | |
82 | -- proper routine. | |
83 | ||
84 | type E_Array is array (Int range 01 .. 63) of RE_Id; | |
85 | ||
86 | -- Array of Bits_nn entities. Note that we do not use library routines | |
87 | -- for the 8-bit and 16-bit cases, but we still fill in the table, using | |
88 | -- entries from System.Unsigned, because we also use this table for | |
89 | -- certain special unchecked conversions in the big-endian case. | |
90 | ||
91 | Bits_Id : constant E_Array := | |
92 | (01 => RE_Bits_1, | |
93 | 02 => RE_Bits_2, | |
94 | 03 => RE_Bits_03, | |
95 | 04 => RE_Bits_4, | |
96 | 05 => RE_Bits_05, | |
97 | 06 => RE_Bits_06, | |
98 | 07 => RE_Bits_07, | |
99 | 08 => RE_Unsigned_8, | |
100 | 09 => RE_Bits_09, | |
101 | 10 => RE_Bits_10, | |
102 | 11 => RE_Bits_11, | |
103 | 12 => RE_Bits_12, | |
104 | 13 => RE_Bits_13, | |
105 | 14 => RE_Bits_14, | |
106 | 15 => RE_Bits_15, | |
107 | 16 => RE_Unsigned_16, | |
108 | 17 => RE_Bits_17, | |
109 | 18 => RE_Bits_18, | |
110 | 19 => RE_Bits_19, | |
111 | 20 => RE_Bits_20, | |
112 | 21 => RE_Bits_21, | |
113 | 22 => RE_Bits_22, | |
114 | 23 => RE_Bits_23, | |
115 | 24 => RE_Bits_24, | |
116 | 25 => RE_Bits_25, | |
117 | 26 => RE_Bits_26, | |
118 | 27 => RE_Bits_27, | |
119 | 28 => RE_Bits_28, | |
120 | 29 => RE_Bits_29, | |
121 | 30 => RE_Bits_30, | |
122 | 31 => RE_Bits_31, | |
123 | 32 => RE_Unsigned_32, | |
124 | 33 => RE_Bits_33, | |
125 | 34 => RE_Bits_34, | |
126 | 35 => RE_Bits_35, | |
127 | 36 => RE_Bits_36, | |
128 | 37 => RE_Bits_37, | |
129 | 38 => RE_Bits_38, | |
130 | 39 => RE_Bits_39, | |
131 | 40 => RE_Bits_40, | |
132 | 41 => RE_Bits_41, | |
133 | 42 => RE_Bits_42, | |
134 | 43 => RE_Bits_43, | |
135 | 44 => RE_Bits_44, | |
136 | 45 => RE_Bits_45, | |
137 | 46 => RE_Bits_46, | |
138 | 47 => RE_Bits_47, | |
139 | 48 => RE_Bits_48, | |
140 | 49 => RE_Bits_49, | |
141 | 50 => RE_Bits_50, | |
142 | 51 => RE_Bits_51, | |
143 | 52 => RE_Bits_52, | |
144 | 53 => RE_Bits_53, | |
145 | 54 => RE_Bits_54, | |
146 | 55 => RE_Bits_55, | |
147 | 56 => RE_Bits_56, | |
148 | 57 => RE_Bits_57, | |
149 | 58 => RE_Bits_58, | |
150 | 59 => RE_Bits_59, | |
151 | 60 => RE_Bits_60, | |
152 | 61 => RE_Bits_61, | |
153 | 62 => RE_Bits_62, | |
154 | 63 => RE_Bits_63); | |
155 | ||
156 | -- Array of Get routine entities. These are used to obtain an element | |
157 | -- from a packed array. The N'th entry is used to obtain elements from | |
158 | -- a packed array whose component size is N. RE_Null is used as a null | |
159 | -- entry, for the cases where a library routine is not used. | |
160 | ||
161 | Get_Id : constant E_Array := | |
162 | (01 => RE_Null, | |
163 | 02 => RE_Null, | |
164 | 03 => RE_Get_03, | |
165 | 04 => RE_Null, | |
166 | 05 => RE_Get_05, | |
167 | 06 => RE_Get_06, | |
168 | 07 => RE_Get_07, | |
169 | 08 => RE_Null, | |
170 | 09 => RE_Get_09, | |
171 | 10 => RE_Get_10, | |
172 | 11 => RE_Get_11, | |
173 | 12 => RE_Get_12, | |
174 | 13 => RE_Get_13, | |
175 | 14 => RE_Get_14, | |
176 | 15 => RE_Get_15, | |
177 | 16 => RE_Null, | |
178 | 17 => RE_Get_17, | |
179 | 18 => RE_Get_18, | |
180 | 19 => RE_Get_19, | |
181 | 20 => RE_Get_20, | |
182 | 21 => RE_Get_21, | |
183 | 22 => RE_Get_22, | |
184 | 23 => RE_Get_23, | |
185 | 24 => RE_Get_24, | |
186 | 25 => RE_Get_25, | |
187 | 26 => RE_Get_26, | |
188 | 27 => RE_Get_27, | |
189 | 28 => RE_Get_28, | |
190 | 29 => RE_Get_29, | |
191 | 30 => RE_Get_30, | |
192 | 31 => RE_Get_31, | |
193 | 32 => RE_Null, | |
194 | 33 => RE_Get_33, | |
195 | 34 => RE_Get_34, | |
196 | 35 => RE_Get_35, | |
197 | 36 => RE_Get_36, | |
198 | 37 => RE_Get_37, | |
199 | 38 => RE_Get_38, | |
200 | 39 => RE_Get_39, | |
201 | 40 => RE_Get_40, | |
202 | 41 => RE_Get_41, | |
203 | 42 => RE_Get_42, | |
204 | 43 => RE_Get_43, | |
205 | 44 => RE_Get_44, | |
206 | 45 => RE_Get_45, | |
207 | 46 => RE_Get_46, | |
208 | 47 => RE_Get_47, | |
209 | 48 => RE_Get_48, | |
210 | 49 => RE_Get_49, | |
211 | 50 => RE_Get_50, | |
212 | 51 => RE_Get_51, | |
213 | 52 => RE_Get_52, | |
214 | 53 => RE_Get_53, | |
215 | 54 => RE_Get_54, | |
216 | 55 => RE_Get_55, | |
217 | 56 => RE_Get_56, | |
218 | 57 => RE_Get_57, | |
219 | 58 => RE_Get_58, | |
220 | 59 => RE_Get_59, | |
221 | 60 => RE_Get_60, | |
222 | 61 => RE_Get_61, | |
223 | 62 => RE_Get_62, | |
224 | 63 => RE_Get_63); | |
225 | ||
226 | -- Array of Get routine entities to be used in the case where the packed | |
227 | -- array is itself a component of a packed structure, and therefore may | |
228 | -- not be fully aligned. This only affects the even sizes, since for the | |
229 | -- odd sizes, we do not get any fixed alignment in any case. | |
230 | ||
231 | GetU_Id : constant E_Array := | |
232 | (01 => RE_Null, | |
233 | 02 => RE_Null, | |
234 | 03 => RE_Get_03, | |
235 | 04 => RE_Null, | |
236 | 05 => RE_Get_05, | |
237 | 06 => RE_GetU_06, | |
238 | 07 => RE_Get_07, | |
239 | 08 => RE_Null, | |
240 | 09 => RE_Get_09, | |
241 | 10 => RE_GetU_10, | |
242 | 11 => RE_Get_11, | |
243 | 12 => RE_GetU_12, | |
244 | 13 => RE_Get_13, | |
245 | 14 => RE_GetU_14, | |
246 | 15 => RE_Get_15, | |
247 | 16 => RE_Null, | |
248 | 17 => RE_Get_17, | |
249 | 18 => RE_GetU_18, | |
250 | 19 => RE_Get_19, | |
251 | 20 => RE_GetU_20, | |
252 | 21 => RE_Get_21, | |
253 | 22 => RE_GetU_22, | |
254 | 23 => RE_Get_23, | |
255 | 24 => RE_GetU_24, | |
256 | 25 => RE_Get_25, | |
257 | 26 => RE_GetU_26, | |
258 | 27 => RE_Get_27, | |
259 | 28 => RE_GetU_28, | |
260 | 29 => RE_Get_29, | |
261 | 30 => RE_GetU_30, | |
262 | 31 => RE_Get_31, | |
263 | 32 => RE_Null, | |
264 | 33 => RE_Get_33, | |
265 | 34 => RE_GetU_34, | |
266 | 35 => RE_Get_35, | |
267 | 36 => RE_GetU_36, | |
268 | 37 => RE_Get_37, | |
269 | 38 => RE_GetU_38, | |
270 | 39 => RE_Get_39, | |
271 | 40 => RE_GetU_40, | |
272 | 41 => RE_Get_41, | |
273 | 42 => RE_GetU_42, | |
274 | 43 => RE_Get_43, | |
275 | 44 => RE_GetU_44, | |
276 | 45 => RE_Get_45, | |
277 | 46 => RE_GetU_46, | |
278 | 47 => RE_Get_47, | |
279 | 48 => RE_GetU_48, | |
280 | 49 => RE_Get_49, | |
281 | 50 => RE_GetU_50, | |
282 | 51 => RE_Get_51, | |
283 | 52 => RE_GetU_52, | |
284 | 53 => RE_Get_53, | |
285 | 54 => RE_GetU_54, | |
286 | 55 => RE_Get_55, | |
287 | 56 => RE_GetU_56, | |
288 | 57 => RE_Get_57, | |
289 | 58 => RE_GetU_58, | |
290 | 59 => RE_Get_59, | |
291 | 60 => RE_GetU_60, | |
292 | 61 => RE_Get_61, | |
293 | 62 => RE_GetU_62, | |
294 | 63 => RE_Get_63); | |
295 | ||
296 | -- Array of Set routine entities. These are used to assign an element | |
297 | -- of a packed array. The N'th entry is used to assign elements for | |
298 | -- a packed array whose component size is N. RE_Null is used as a null | |
299 | -- entry, for the cases where a library routine is not used. | |
300 | ||
fbf5a39b | 301 | Set_Id : constant E_Array := |
70482933 RK |
302 | (01 => RE_Null, |
303 | 02 => RE_Null, | |
304 | 03 => RE_Set_03, | |
305 | 04 => RE_Null, | |
306 | 05 => RE_Set_05, | |
307 | 06 => RE_Set_06, | |
308 | 07 => RE_Set_07, | |
309 | 08 => RE_Null, | |
310 | 09 => RE_Set_09, | |
311 | 10 => RE_Set_10, | |
312 | 11 => RE_Set_11, | |
313 | 12 => RE_Set_12, | |
314 | 13 => RE_Set_13, | |
315 | 14 => RE_Set_14, | |
316 | 15 => RE_Set_15, | |
317 | 16 => RE_Null, | |
318 | 17 => RE_Set_17, | |
319 | 18 => RE_Set_18, | |
320 | 19 => RE_Set_19, | |
321 | 20 => RE_Set_20, | |
322 | 21 => RE_Set_21, | |
323 | 22 => RE_Set_22, | |
324 | 23 => RE_Set_23, | |
325 | 24 => RE_Set_24, | |
326 | 25 => RE_Set_25, | |
327 | 26 => RE_Set_26, | |
328 | 27 => RE_Set_27, | |
329 | 28 => RE_Set_28, | |
330 | 29 => RE_Set_29, | |
331 | 30 => RE_Set_30, | |
332 | 31 => RE_Set_31, | |
333 | 32 => RE_Null, | |
334 | 33 => RE_Set_33, | |
335 | 34 => RE_Set_34, | |
336 | 35 => RE_Set_35, | |
337 | 36 => RE_Set_36, | |
338 | 37 => RE_Set_37, | |
339 | 38 => RE_Set_38, | |
340 | 39 => RE_Set_39, | |
341 | 40 => RE_Set_40, | |
342 | 41 => RE_Set_41, | |
343 | 42 => RE_Set_42, | |
344 | 43 => RE_Set_43, | |
345 | 44 => RE_Set_44, | |
346 | 45 => RE_Set_45, | |
347 | 46 => RE_Set_46, | |
348 | 47 => RE_Set_47, | |
349 | 48 => RE_Set_48, | |
350 | 49 => RE_Set_49, | |
351 | 50 => RE_Set_50, | |
352 | 51 => RE_Set_51, | |
353 | 52 => RE_Set_52, | |
354 | 53 => RE_Set_53, | |
355 | 54 => RE_Set_54, | |
356 | 55 => RE_Set_55, | |
357 | 56 => RE_Set_56, | |
358 | 57 => RE_Set_57, | |
359 | 58 => RE_Set_58, | |
360 | 59 => RE_Set_59, | |
361 | 60 => RE_Set_60, | |
362 | 61 => RE_Set_61, | |
363 | 62 => RE_Set_62, | |
364 | 63 => RE_Set_63); | |
365 | ||
366 | -- Array of Set routine entities to be used in the case where the packed | |
367 | -- array is itself a component of a packed structure, and therefore may | |
368 | -- not be fully aligned. This only affects the even sizes, since for the | |
369 | -- odd sizes, we do not get any fixed alignment in any case. | |
370 | ||
fbf5a39b | 371 | SetU_Id : constant E_Array := |
70482933 RK |
372 | (01 => RE_Null, |
373 | 02 => RE_Null, | |
374 | 03 => RE_Set_03, | |
375 | 04 => RE_Null, | |
376 | 05 => RE_Set_05, | |
377 | 06 => RE_SetU_06, | |
378 | 07 => RE_Set_07, | |
379 | 08 => RE_Null, | |
380 | 09 => RE_Set_09, | |
381 | 10 => RE_SetU_10, | |
382 | 11 => RE_Set_11, | |
383 | 12 => RE_SetU_12, | |
384 | 13 => RE_Set_13, | |
385 | 14 => RE_SetU_14, | |
386 | 15 => RE_Set_15, | |
387 | 16 => RE_Null, | |
388 | 17 => RE_Set_17, | |
389 | 18 => RE_SetU_18, | |
390 | 19 => RE_Set_19, | |
391 | 20 => RE_SetU_20, | |
392 | 21 => RE_Set_21, | |
393 | 22 => RE_SetU_22, | |
394 | 23 => RE_Set_23, | |
395 | 24 => RE_SetU_24, | |
396 | 25 => RE_Set_25, | |
397 | 26 => RE_SetU_26, | |
398 | 27 => RE_Set_27, | |
399 | 28 => RE_SetU_28, | |
400 | 29 => RE_Set_29, | |
401 | 30 => RE_SetU_30, | |
402 | 31 => RE_Set_31, | |
403 | 32 => RE_Null, | |
404 | 33 => RE_Set_33, | |
405 | 34 => RE_SetU_34, | |
406 | 35 => RE_Set_35, | |
407 | 36 => RE_SetU_36, | |
408 | 37 => RE_Set_37, | |
409 | 38 => RE_SetU_38, | |
410 | 39 => RE_Set_39, | |
411 | 40 => RE_SetU_40, | |
412 | 41 => RE_Set_41, | |
413 | 42 => RE_SetU_42, | |
414 | 43 => RE_Set_43, | |
415 | 44 => RE_SetU_44, | |
416 | 45 => RE_Set_45, | |
417 | 46 => RE_SetU_46, | |
418 | 47 => RE_Set_47, | |
419 | 48 => RE_SetU_48, | |
420 | 49 => RE_Set_49, | |
421 | 50 => RE_SetU_50, | |
422 | 51 => RE_Set_51, | |
423 | 52 => RE_SetU_52, | |
424 | 53 => RE_Set_53, | |
425 | 54 => RE_SetU_54, | |
426 | 55 => RE_Set_55, | |
427 | 56 => RE_SetU_56, | |
428 | 57 => RE_Set_57, | |
429 | 58 => RE_SetU_58, | |
430 | 59 => RE_Set_59, | |
431 | 60 => RE_SetU_60, | |
432 | 61 => RE_Set_61, | |
433 | 62 => RE_SetU_62, | |
434 | 63 => RE_Set_63); | |
435 | ||
436 | ----------------------- | |
437 | -- Local Subprograms -- | |
438 | ----------------------- | |
439 | ||
440 | procedure Compute_Linear_Subscript | |
441 | (Atyp : Entity_Id; | |
442 | N : Node_Id; | |
443 | Subscr : out Node_Id); | |
444 | -- Given a constrained array type Atyp, and an indexed component node | |
445 | -- N referencing an array object of this type, build an expression of | |
446 | -- type Standard.Integer representing the zero-based linear subscript | |
447 | -- value. This expression includes any required range checks. | |
448 | ||
449 | procedure Convert_To_PAT_Type (Aexp : Node_Id); | |
450 | -- Given an expression of a packed array type, builds a corresponding | |
451 | -- expression whose type is the implementation type used to represent | |
452 | -- the packed array. Aexp is analyzed and resolved on entry and on exit. | |
453 | ||
980f237d GB |
454 | function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean; |
455 | -- There are two versions of the Set routines, the ones used when the | |
456 | -- object is known to be sufficiently well aligned given the number of | |
457 | -- bits, and the ones used when the object is not known to be aligned. | |
458 | -- This routine is used to determine which set to use. Obj is a reference | |
459 | -- to the object, and Csiz is the component size of the packed array. | |
460 | -- True is returned if the alignment of object is known to be sufficient, | |
461 | -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and | |
462 | -- 2 otherwise. | |
463 | ||
70482933 RK |
464 | function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id; |
465 | -- Build a left shift node, checking for the case of a shift count of zero | |
466 | ||
467 | function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id; | |
468 | -- Build a right shift node, checking for the case of a shift count of zero | |
469 | ||
470 | function RJ_Unchecked_Convert_To | |
471 | (Typ : Entity_Id; | |
472 | Expr : Node_Id) | |
473 | return Node_Id; | |
474 | -- The packed array code does unchecked conversions which in some cases | |
475 | -- may involve non-discrete types with differing sizes. The semantics of | |
476 | -- such conversions is potentially endian dependent, and the effect we | |
477 | -- want here for such a conversion is to do the conversion in size as | |
478 | -- though numeric items are involved, and we extend or truncate on the | |
479 | -- left side. This happens naturally in the little-endian case, but in | |
480 | -- the big endian case we can get left justification, when what we want | |
481 | -- is right justification. This routine does the unchecked conversion in | |
482 | -- a stepwise manner to ensure that it gives the expected result. Hence | |
483 | -- the name (RJ = Right justified). The parameters Typ and Expr are as | |
484 | -- for the case of a normal Unchecked_Convert_To call. | |
485 | ||
486 | procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id); | |
487 | -- This routine is called in the Get and Set case for arrays that are | |
488 | -- packed but not bit-packed, meaning that they have at least one | |
489 | -- subscript that is of an enumeration type with a non-standard | |
490 | -- representation. This routine modifies the given node to properly | |
491 | -- reference the corresponding packed array type. | |
492 | ||
493 | procedure Setup_Inline_Packed_Array_Reference | |
494 | (N : Node_Id; | |
495 | Atyp : Entity_Id; | |
496 | Obj : in out Node_Id; | |
497 | Cmask : out Uint; | |
498 | Shift : out Node_Id); | |
499 | -- This procedure performs common processing on the N_Indexed_Component | |
500 | -- parameter given as N, whose prefix is a reference to a packed array. | |
501 | -- This is used for the get and set when the component size is 1,2,4 | |
502 | -- or for other component sizes when the packed array type is a modular | |
503 | -- type (i.e. the cases that are handled with inline code). | |
504 | -- | |
505 | -- On entry: | |
506 | -- | |
507 | -- N is the N_Indexed_Component node for the packed array reference | |
508 | -- | |
509 | -- Atyp is the constrained array type (the actual subtype has been | |
510 | -- computed if necessary to obtain the constraints, but this is still | |
511 | -- the original array type, not the Packed_Array_Type value). | |
512 | -- | |
513 | -- Obj is the object which is to be indexed. It is always of type Atyp. | |
514 | -- | |
515 | -- On return: | |
516 | -- | |
517 | -- Obj is the object containing the desired bit field. It is of type | |
fbf5a39b AC |
518 | -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the |
519 | -- entire value, for the small static case, or the proper selected byte | |
520 | -- from the array in the large or dynamic case. This node is analyzed | |
521 | -- and resolved on return. | |
70482933 RK |
522 | -- |
523 | -- Shift is a node representing the shift count to be used in the | |
524 | -- rotate right instruction that positions the field for access. | |
525 | -- This node is analyzed and resolved on return. | |
526 | -- | |
527 | -- Cmask is a mask corresponding to the width of the component field. | |
528 | -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4). | |
529 | -- | |
530 | -- Note: in some cases the call to this routine may generate actions | |
531 | -- (for handling multi-use references and the generation of the packed | |
532 | -- array type on the fly). Such actions are inserted into the tree | |
533 | -- directly using Insert_Action. | |
534 | ||
535 | ------------------------------ | |
536 | -- Compute_Linear_Subcsript -- | |
537 | ------------------------------ | |
538 | ||
539 | procedure Compute_Linear_Subscript | |
540 | (Atyp : Entity_Id; | |
541 | N : Node_Id; | |
542 | Subscr : out Node_Id) | |
543 | is | |
544 | Loc : constant Source_Ptr := Sloc (N); | |
545 | Oldsub : Node_Id; | |
546 | Newsub : Node_Id; | |
547 | Indx : Node_Id; | |
548 | Styp : Entity_Id; | |
549 | ||
550 | begin | |
551 | Subscr := Empty; | |
552 | ||
553 | -- Loop through dimensions | |
554 | ||
555 | Indx := First_Index (Atyp); | |
556 | Oldsub := First (Expressions (N)); | |
557 | ||
558 | while Present (Indx) loop | |
559 | Styp := Etype (Indx); | |
560 | Newsub := Relocate_Node (Oldsub); | |
561 | ||
562 | -- Get expression for the subscript value. First, if Do_Range_Check | |
563 | -- is set on a subscript, then we must do a range check against the | |
564 | -- original bounds (not the bounds of the packed array type). We do | |
565 | -- this by introducing a subtype conversion. | |
566 | ||
567 | if Do_Range_Check (Newsub) | |
568 | and then Etype (Newsub) /= Styp | |
569 | then | |
570 | Newsub := Convert_To (Styp, Newsub); | |
571 | end if; | |
572 | ||
573 | -- Now evolve the expression for the subscript. First convert | |
574 | -- the subscript to be zero based and of an integer type. | |
575 | ||
576 | -- Case of integer type, where we just subtract to get lower bound | |
577 | ||
578 | if Is_Integer_Type (Styp) then | |
579 | ||
580 | -- If length of integer type is smaller than standard integer, | |
581 | -- then we convert to integer first, then do the subtract | |
582 | ||
583 | -- Integer (subscript) - Integer (Styp'First) | |
584 | ||
585 | if Esize (Styp) < Esize (Standard_Integer) then | |
586 | Newsub := | |
587 | Make_Op_Subtract (Loc, | |
588 | Left_Opnd => Convert_To (Standard_Integer, Newsub), | |
589 | Right_Opnd => | |
590 | Convert_To (Standard_Integer, | |
591 | Make_Attribute_Reference (Loc, | |
07fc65c4 | 592 | Prefix => New_Occurrence_Of (Styp, Loc), |
70482933 RK |
593 | Attribute_Name => Name_First))); |
594 | ||
595 | -- For larger integer types, subtract first, then convert to | |
596 | -- integer, this deals with strange long long integer bounds. | |
597 | ||
598 | -- Integer (subscript - Styp'First) | |
599 | ||
600 | else | |
601 | Newsub := | |
602 | Convert_To (Standard_Integer, | |
603 | Make_Op_Subtract (Loc, | |
604 | Left_Opnd => Newsub, | |
605 | Right_Opnd => | |
606 | Make_Attribute_Reference (Loc, | |
07fc65c4 | 607 | Prefix => New_Occurrence_Of (Styp, Loc), |
70482933 RK |
608 | Attribute_Name => Name_First))); |
609 | end if; | |
610 | ||
611 | -- For the enumeration case, we have to use 'Pos to get the value | |
612 | -- to work with before subtracting the lower bound. | |
613 | ||
614 | -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First)); | |
615 | ||
616 | -- This is not quite right for bizarre cases where the size of the | |
617 | -- enumeration type is > Integer'Size bits due to rep clause ??? | |
618 | ||
619 | else | |
620 | pragma Assert (Is_Enumeration_Type (Styp)); | |
621 | ||
622 | Newsub := | |
623 | Make_Op_Subtract (Loc, | |
624 | Left_Opnd => Convert_To (Standard_Integer, | |
625 | Make_Attribute_Reference (Loc, | |
07fc65c4 | 626 | Prefix => New_Occurrence_Of (Styp, Loc), |
70482933 | 627 | Attribute_Name => Name_Pos, |
07fc65c4 | 628 | Expressions => New_List (Newsub))), |
70482933 RK |
629 | |
630 | Right_Opnd => | |
631 | Convert_To (Standard_Integer, | |
632 | Make_Attribute_Reference (Loc, | |
07fc65c4 | 633 | Prefix => New_Occurrence_Of (Styp, Loc), |
70482933 | 634 | Attribute_Name => Name_Pos, |
07fc65c4 | 635 | Expressions => New_List ( |
70482933 | 636 | Make_Attribute_Reference (Loc, |
07fc65c4 | 637 | Prefix => New_Occurrence_Of (Styp, Loc), |
70482933 RK |
638 | Attribute_Name => Name_First))))); |
639 | end if; | |
640 | ||
641 | Set_Paren_Count (Newsub, 1); | |
642 | ||
643 | -- For the first subscript, we just copy that subscript value | |
644 | ||
645 | if No (Subscr) then | |
646 | Subscr := Newsub; | |
647 | ||
648 | -- Otherwise, we must multiply what we already have by the current | |
649 | -- stride and then add in the new value to the evolving subscript. | |
650 | ||
651 | else | |
652 | Subscr := | |
653 | Make_Op_Add (Loc, | |
654 | Left_Opnd => | |
655 | Make_Op_Multiply (Loc, | |
656 | Left_Opnd => Subscr, | |
657 | Right_Opnd => | |
658 | Make_Attribute_Reference (Loc, | |
659 | Attribute_Name => Name_Range_Length, | |
660 | Prefix => New_Occurrence_Of (Styp, Loc))), | |
661 | Right_Opnd => Newsub); | |
662 | end if; | |
663 | ||
664 | -- Move to next subscript | |
665 | ||
666 | Next_Index (Indx); | |
667 | Next (Oldsub); | |
668 | end loop; | |
669 | end Compute_Linear_Subscript; | |
670 | ||
671 | ------------------------- | |
672 | -- Convert_To_PAT_Type -- | |
673 | ------------------------- | |
674 | ||
675 | -- The PAT is always obtained from the actual subtype | |
676 | ||
677 | procedure Convert_To_PAT_Type (Aexp : Entity_Id) is | |
678 | Act_ST : Entity_Id; | |
679 | ||
680 | begin | |
681 | Convert_To_Actual_Subtype (Aexp); | |
682 | Act_ST := Underlying_Type (Etype (Aexp)); | |
683 | Create_Packed_Array_Type (Act_ST); | |
684 | ||
685 | -- Just replace the etype with the packed array type. This works | |
686 | -- because the expression will not be further analyzed, and Gigi | |
687 | -- considers the two types equivalent in any case. | |
688 | ||
689 | Set_Etype (Aexp, Packed_Array_Type (Act_ST)); | |
690 | end Convert_To_PAT_Type; | |
691 | ||
692 | ------------------------------ | |
693 | -- Create_Packed_Array_Type -- | |
694 | ------------------------------ | |
695 | ||
696 | procedure Create_Packed_Array_Type (Typ : Entity_Id) is | |
697 | Loc : constant Source_Ptr := Sloc (Typ); | |
698 | Ctyp : constant Entity_Id := Component_Type (Typ); | |
699 | Csize : constant Uint := Component_Size (Typ); | |
700 | ||
701 | Ancest : Entity_Id; | |
702 | PB_Type : Entity_Id; | |
703 | Esiz : Uint; | |
704 | Decl : Node_Id; | |
705 | PAT : Entity_Id; | |
706 | Len_Dim : Node_Id; | |
707 | Len_Expr : Node_Id; | |
708 | Len_Bits : Uint; | |
709 | Bits_U1 : Node_Id; | |
710 | PAT_High : Node_Id; | |
711 | Btyp : Entity_Id; | |
712 | Lit : Node_Id; | |
713 | ||
714 | procedure Install_PAT; | |
715 | -- This procedure is called with Decl set to the declaration for the | |
716 | -- packed array type. It creates the type and installs it as required. | |
717 | ||
718 | procedure Set_PB_Type; | |
719 | -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment | |
720 | -- requirements (see documentation in the spec of this package). | |
721 | ||
722 | ----------------- | |
723 | -- Install_PAT -- | |
724 | ----------------- | |
725 | ||
726 | procedure Install_PAT is | |
727 | Pushed_Scope : Boolean := False; | |
728 | ||
729 | begin | |
730 | -- We do not want to put the declaration we have created in the tree | |
731 | -- since it is often hard, and sometimes impossible to find a proper | |
732 | -- place for it (the impossible case arises for a packed array type | |
733 | -- with bounds depending on the discriminant, a declaration cannot | |
734 | -- be put inside the record, and the reference to the discriminant | |
735 | -- cannot be outside the record). | |
736 | ||
737 | -- The solution is to analyze the declaration while temporarily | |
738 | -- attached to the tree at an appropriate point, and then we install | |
739 | -- the resulting type as an Itype in the packed array type field of | |
740 | -- the original type, so that no explicit declaration is required. | |
741 | ||
742 | -- Note: the packed type is created in the scope of its parent | |
743 | -- type. There are at least some cases where the current scope | |
744 | -- is deeper, and so when this is the case, we temporarily reset | |
745 | -- the scope for the definition. This is clearly safe, since the | |
746 | -- first use of the packed array type will be the implicit | |
747 | -- reference from the corresponding unpacked type when it is | |
748 | -- elaborated. | |
749 | ||
750 | if Is_Itype (Typ) then | |
751 | Set_Parent (Decl, Associated_Node_For_Itype (Typ)); | |
752 | else | |
753 | Set_Parent (Decl, Declaration_Node (Typ)); | |
754 | end if; | |
755 | ||
756 | if Scope (Typ) /= Current_Scope then | |
757 | New_Scope (Scope (Typ)); | |
758 | Pushed_Scope := True; | |
759 | end if; | |
760 | ||
761 | Set_Is_Itype (PAT, True); | |
07fc65c4 | 762 | Set_Packed_Array_Type (Typ, PAT); |
70482933 RK |
763 | Analyze (Decl, Suppress => All_Checks); |
764 | ||
765 | if Pushed_Scope then | |
766 | Pop_Scope; | |
767 | end if; | |
768 | ||
769 | -- Set Esize and RM_Size to the actual size of the packed object | |
770 | -- Do not reset RM_Size if already set, as happens in the case | |
fbf5a39b | 771 | -- of a modular type. |
70482933 RK |
772 | |
773 | Set_Esize (PAT, Esiz); | |
774 | ||
775 | if Unknown_RM_Size (PAT) then | |
776 | Set_RM_Size (PAT, Esiz); | |
777 | end if; | |
778 | ||
779 | -- Set remaining fields of packed array type | |
780 | ||
07fc65c4 GB |
781 | Init_Alignment (PAT); |
782 | Set_Parent (PAT, Empty); | |
70482933 | 783 | Set_Associated_Node_For_Itype (PAT, Typ); |
07fc65c4 GB |
784 | Set_Is_Packed_Array_Type (PAT, True); |
785 | Set_Original_Array_Type (PAT, Typ); | |
70482933 RK |
786 | |
787 | -- We definitely do not want to delay freezing for packed array | |
788 | -- types. This is of particular importance for the itypes that | |
789 | -- are generated for record components depending on discriminants | |
790 | -- where there is no place to put the freeze node. | |
791 | ||
792 | Set_Has_Delayed_Freeze (PAT, False); | |
793 | Set_Has_Delayed_Freeze (Etype (PAT), False); | |
794 | end Install_PAT; | |
795 | ||
796 | ----------------- | |
797 | -- Set_PB_Type -- | |
798 | ----------------- | |
799 | ||
800 | procedure Set_PB_Type is | |
801 | begin | |
802 | -- If the user has specified an explicit alignment for the | |
07fc65c4 | 803 | -- type or component, take it into account. |
70482933 RK |
804 | |
805 | if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0 | |
07fc65c4 | 806 | or else Alignment (Typ) = 1 |
70482933 RK |
807 | or else Component_Alignment (Typ) = Calign_Storage_Unit |
808 | then | |
809 | PB_Type := RTE (RE_Packed_Bytes1); | |
810 | ||
07fc65c4 GB |
811 | elsif Csize mod 4 /= 0 |
812 | or else Alignment (Typ) = 2 | |
813 | then | |
70482933 RK |
814 | PB_Type := RTE (RE_Packed_Bytes2); |
815 | ||
816 | else | |
817 | PB_Type := RTE (RE_Packed_Bytes4); | |
818 | end if; | |
819 | end Set_PB_Type; | |
820 | ||
821 | -- Start of processing for Create_Packed_Array_Type | |
822 | ||
823 | begin | |
824 | -- If we already have a packed array type, nothing to do | |
825 | ||
826 | if Present (Packed_Array_Type (Typ)) then | |
827 | return; | |
828 | end if; | |
829 | ||
830 | -- If our immediate ancestor subtype is constrained, and it already | |
831 | -- has a packed array type, then just share the same type, since the | |
832 | -- bounds must be the same. | |
833 | ||
834 | if Ekind (Typ) = E_Array_Subtype then | |
835 | Ancest := Ancestor_Subtype (Typ); | |
836 | ||
837 | if Present (Ancest) | |
838 | and then Is_Constrained (Ancest) | |
839 | and then Present (Packed_Array_Type (Ancest)) | |
840 | then | |
841 | Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest)); | |
842 | return; | |
843 | end if; | |
844 | end if; | |
845 | ||
846 | -- We preset the result type size from the size of the original array | |
847 | -- type, since this size clearly belongs to the packed array type. The | |
848 | -- size of the conceptual unpacked type is always set to unknown. | |
849 | ||
850 | Esiz := Esize (Typ); | |
851 | ||
852 | -- Case of an array where at least one index is of an enumeration | |
853 | -- type with a non-standard representation, but the component size | |
854 | -- is not appropriate for bit packing. This is the case where we | |
855 | -- have Is_Packed set (we would never be in this unit otherwise), | |
856 | -- but Is_Bit_Packed_Array is false. | |
857 | ||
858 | -- Note that if the component size is appropriate for bit packing, | |
859 | -- then the circuit for the computation of the subscript properly | |
860 | -- deals with the non-standard enumeration type case by taking the | |
861 | -- Pos anyway. | |
862 | ||
863 | if not Is_Bit_Packed_Array (Typ) then | |
864 | ||
865 | -- Here we build a declaration: | |
866 | ||
867 | -- type tttP is array (index1, index2, ...) of component_type | |
868 | ||
869 | -- where index1, index2, are the index types. These are the same | |
870 | -- as the index types of the original array, except for the non- | |
871 | -- standard representation enumeration type case, where we have | |
872 | -- two subcases. | |
873 | ||
874 | -- For the unconstrained array case, we use | |
875 | ||
876 | -- Natural range <> | |
877 | ||
878 | -- For the constrained case, we use | |
879 | ||
880 | -- Natural range Enum_Type'Pos (Enum_Type'First) .. | |
881 | -- Enum_Type'Pos (Enum_Type'Last); | |
882 | ||
883 | PAT := | |
884 | Make_Defining_Identifier (Loc, | |
885 | Chars => New_External_Name (Chars (Typ), 'P')); | |
886 | ||
887 | Set_Packed_Array_Type (Typ, PAT); | |
888 | ||
889 | declare | |
fbf5a39b | 890 | Indexes : constant List_Id := New_List; |
70482933 RK |
891 | Indx : Node_Id; |
892 | Indx_Typ : Entity_Id; | |
893 | Enum_Case : Boolean; | |
894 | Typedef : Node_Id; | |
895 | ||
896 | begin | |
897 | Indx := First_Index (Typ); | |
898 | ||
899 | while Present (Indx) loop | |
900 | Indx_Typ := Etype (Indx); | |
901 | ||
902 | Enum_Case := Is_Enumeration_Type (Indx_Typ) | |
903 | and then Has_Non_Standard_Rep (Indx_Typ); | |
904 | ||
905 | -- Unconstrained case | |
906 | ||
907 | if not Is_Constrained (Typ) then | |
908 | if Enum_Case then | |
909 | Indx_Typ := Standard_Natural; | |
910 | end if; | |
911 | ||
912 | Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc)); | |
913 | ||
914 | -- Constrained case | |
915 | ||
916 | else | |
917 | if not Enum_Case then | |
918 | Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc)); | |
919 | ||
920 | else | |
921 | Append_To (Indexes, | |
922 | Make_Subtype_Indication (Loc, | |
923 | Subtype_Mark => | |
924 | New_Occurrence_Of (Standard_Natural, Loc), | |
925 | Constraint => | |
926 | Make_Range_Constraint (Loc, | |
927 | Range_Expression => | |
928 | Make_Range (Loc, | |
929 | Low_Bound => | |
930 | Make_Attribute_Reference (Loc, | |
931 | Prefix => | |
932 | New_Occurrence_Of (Indx_Typ, Loc), | |
933 | Attribute_Name => Name_Pos, | |
934 | Expressions => New_List ( | |
935 | Make_Attribute_Reference (Loc, | |
936 | Prefix => | |
937 | New_Occurrence_Of (Indx_Typ, Loc), | |
938 | Attribute_Name => Name_First))), | |
939 | ||
940 | High_Bound => | |
941 | Make_Attribute_Reference (Loc, | |
942 | Prefix => | |
943 | New_Occurrence_Of (Indx_Typ, Loc), | |
944 | Attribute_Name => Name_Pos, | |
945 | Expressions => New_List ( | |
946 | Make_Attribute_Reference (Loc, | |
947 | Prefix => | |
948 | New_Occurrence_Of (Indx_Typ, Loc), | |
949 | Attribute_Name => Name_Last))))))); | |
950 | ||
951 | end if; | |
952 | end if; | |
953 | ||
954 | Next_Index (Indx); | |
955 | end loop; | |
956 | ||
957 | if not Is_Constrained (Typ) then | |
958 | Typedef := | |
959 | Make_Unconstrained_Array_Definition (Loc, | |
960 | Subtype_Marks => Indexes, | |
961 | Subtype_Indication => | |
962 | New_Occurrence_Of (Ctyp, Loc)); | |
963 | ||
964 | else | |
965 | Typedef := | |
966 | Make_Constrained_Array_Definition (Loc, | |
967 | Discrete_Subtype_Definitions => Indexes, | |
968 | Subtype_Indication => | |
969 | New_Occurrence_Of (Ctyp, Loc)); | |
970 | end if; | |
971 | ||
972 | Decl := | |
973 | Make_Full_Type_Declaration (Loc, | |
974 | Defining_Identifier => PAT, | |
975 | Type_Definition => Typedef); | |
976 | end; | |
977 | ||
07fc65c4 GB |
978 | -- Set type as packed array type and install it |
979 | ||
980 | Set_Is_Packed_Array_Type (PAT); | |
70482933 RK |
981 | Install_PAT; |
982 | return; | |
983 | ||
07fc65c4 GB |
984 | -- Case of bit-packing required for unconstrained array. We create |
985 | -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed. | |
70482933 RK |
986 | |
987 | elsif not Is_Constrained (Typ) then | |
07fc65c4 GB |
988 | PAT := |
989 | Make_Defining_Identifier (Loc, | |
990 | Chars => Make_Packed_Array_Type_Name (Typ, Csize)); | |
991 | ||
992 | Set_Packed_Array_Type (Typ, PAT); | |
70482933 | 993 | Set_PB_Type; |
07fc65c4 GB |
994 | |
995 | Decl := | |
996 | Make_Subtype_Declaration (Loc, | |
997 | Defining_Identifier => PAT, | |
998 | Subtype_Indication => New_Occurrence_Of (PB_Type, Loc)); | |
999 | Install_PAT; | |
70482933 RK |
1000 | return; |
1001 | ||
1002 | -- Remaining code is for the case of bit-packing for constrained array | |
1003 | ||
1004 | -- The name of the packed array subtype is | |
1005 | ||
1006 | -- ttt___Xsss | |
1007 | ||
1008 | -- where sss is the component size in bits and ttt is the name of | |
1009 | -- the parent packed type. | |
1010 | ||
1011 | else | |
1012 | PAT := | |
1013 | Make_Defining_Identifier (Loc, | |
1014 | Chars => Make_Packed_Array_Type_Name (Typ, Csize)); | |
1015 | ||
1016 | Set_Packed_Array_Type (Typ, PAT); | |
1017 | ||
1018 | -- Build an expression for the length of the array in bits. | |
1019 | -- This is the product of the length of each of the dimensions | |
1020 | ||
1021 | declare | |
1022 | J : Nat := 1; | |
1023 | ||
1024 | begin | |
1025 | Len_Expr := Empty; -- suppress junk warning | |
1026 | ||
1027 | loop | |
1028 | Len_Dim := | |
1029 | Make_Attribute_Reference (Loc, | |
1030 | Attribute_Name => Name_Length, | |
1031 | Prefix => New_Occurrence_Of (Typ, Loc), | |
1032 | Expressions => New_List ( | |
1033 | Make_Integer_Literal (Loc, J))); | |
1034 | ||
1035 | if J = 1 then | |
1036 | Len_Expr := Len_Dim; | |
1037 | ||
1038 | else | |
1039 | Len_Expr := | |
1040 | Make_Op_Multiply (Loc, | |
1041 | Left_Opnd => Len_Expr, | |
1042 | Right_Opnd => Len_Dim); | |
1043 | end if; | |
1044 | ||
1045 | J := J + 1; | |
1046 | exit when J > Number_Dimensions (Typ); | |
1047 | end loop; | |
1048 | end; | |
1049 | ||
1050 | -- Temporarily attach the length expression to the tree and analyze | |
1051 | -- and resolve it, so that we can test its value. We assume that the | |
fbf5a39b AC |
1052 | -- total length fits in type Integer. This expression may involve |
1053 | -- discriminants, so we treat it as a default/per-object expression. | |
70482933 RK |
1054 | |
1055 | Set_Parent (Len_Expr, Typ); | |
fbf5a39b | 1056 | Analyze_Per_Use_Expression (Len_Expr, Standard_Integer); |
70482933 RK |
1057 | |
1058 | -- Use a modular type if possible. We can do this if we are we | |
1059 | -- have static bounds, and the length is small enough, and the | |
1060 | -- length is not zero. We exclude the zero length case because the | |
1061 | -- size of things is always at least one, and the zero length object | |
fbf5a39b | 1062 | -- would have an anomous size. |
70482933 RK |
1063 | |
1064 | if Compile_Time_Known_Value (Len_Expr) then | |
1065 | Len_Bits := Expr_Value (Len_Expr) * Csize; | |
1066 | ||
1067 | -- We normally consider small enough to mean no larger than the | |
fbf5a39b AC |
1068 | -- value of System_Max_Binary_Modulus_Power, checking that in the |
1069 | -- case of values longer than word size, we have long shifts. | |
70482933 RK |
1070 | |
1071 | if Len_Bits > 0 | |
1072 | and then | |
1073 | (Len_Bits <= System_Word_Size | |
1074 | or else (Len_Bits <= System_Max_Binary_Modulus_Power | |
fbf5a39b AC |
1075 | and then Support_Long_Shifts_On_Target)) |
1076 | ||
1077 | -- Also test for alignment given. If an alignment is given which | |
1078 | -- is smaller than the natural modular alignment, force the array | |
1079 | -- of bytes representation to accommodate the alignment. | |
1080 | ||
1081 | and then | |
1082 | (No (Alignment_Clause (Typ)) | |
1083 | or else | |
1084 | Alignment (Typ) >= ((Len_Bits + System_Storage_Unit) | |
1085 | / System_Storage_Unit)) | |
70482933 RK |
1086 | then |
1087 | -- We can use the modular type, it has the form: | |
1088 | ||
1089 | -- subtype tttPn is btyp | |
1090 | -- range 0 .. 2 ** (Esize (Typ) * Csize) - 1; | |
1091 | ||
fbf5a39b AC |
1092 | -- The bounds are statically known, and btyp is one |
1093 | -- of the unsigned types, depending on the length. If the | |
1094 | -- type is its first subtype, i.e. it is a user-defined | |
1095 | -- type, no object of the type will be larger, and it is | |
1096 | -- worthwhile to use a small unsigned type. | |
70482933 | 1097 | |
fbf5a39b AC |
1098 | if Len_Bits <= Standard_Short_Integer_Size |
1099 | and then First_Subtype (Typ) = Typ | |
1100 | then | |
1101 | Btyp := RTE (RE_Short_Unsigned); | |
1102 | ||
1103 | elsif Len_Bits <= Standard_Integer_Size then | |
70482933 | 1104 | Btyp := RTE (RE_Unsigned); |
fbf5a39b AC |
1105 | |
1106 | elsif Len_Bits <= Standard_Long_Integer_Size then | |
1107 | Btyp := RTE (RE_Long_Unsigned); | |
1108 | ||
70482933 RK |
1109 | else |
1110 | Btyp := RTE (RE_Long_Long_Unsigned); | |
1111 | end if; | |
1112 | ||
1113 | Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1); | |
1114 | Set_Print_In_Hex (Lit); | |
1115 | ||
1116 | Decl := | |
1117 | Make_Subtype_Declaration (Loc, | |
1118 | Defining_Identifier => PAT, | |
1119 | Subtype_Indication => | |
1120 | Make_Subtype_Indication (Loc, | |
1121 | Subtype_Mark => New_Occurrence_Of (Btyp, Loc), | |
1122 | ||
1123 | Constraint => | |
1124 | Make_Range_Constraint (Loc, | |
1125 | Range_Expression => | |
1126 | Make_Range (Loc, | |
1127 | Low_Bound => | |
1128 | Make_Integer_Literal (Loc, 0), | |
1129 | High_Bound => Lit)))); | |
1130 | ||
1131 | if Esiz = Uint_0 then | |
1132 | Esiz := Len_Bits; | |
1133 | end if; | |
1134 | ||
1135 | Install_PAT; | |
1136 | return; | |
1137 | end if; | |
1138 | end if; | |
1139 | ||
1140 | -- Could not use a modular type, for all other cases, we build | |
1141 | -- a packed array subtype: | |
1142 | ||
1143 | -- subtype tttPn is | |
1144 | -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1); | |
1145 | ||
1146 | -- Bits is the length of the array in bits. | |
1147 | ||
1148 | Set_PB_Type; | |
1149 | ||
1150 | Bits_U1 := | |
1151 | Make_Op_Add (Loc, | |
1152 | Left_Opnd => | |
1153 | Make_Op_Multiply (Loc, | |
1154 | Left_Opnd => | |
1155 | Make_Integer_Literal (Loc, Csize), | |
1156 | Right_Opnd => Len_Expr), | |
1157 | ||
1158 | Right_Opnd => | |
1159 | Make_Integer_Literal (Loc, 7)); | |
1160 | ||
1161 | Set_Paren_Count (Bits_U1, 1); | |
1162 | ||
1163 | PAT_High := | |
1164 | Make_Op_Subtract (Loc, | |
1165 | Left_Opnd => | |
1166 | Make_Op_Divide (Loc, | |
1167 | Left_Opnd => Bits_U1, | |
1168 | Right_Opnd => Make_Integer_Literal (Loc, 8)), | |
1169 | Right_Opnd => Make_Integer_Literal (Loc, 1)); | |
1170 | ||
1171 | Decl := | |
1172 | Make_Subtype_Declaration (Loc, | |
1173 | Defining_Identifier => PAT, | |
1174 | Subtype_Indication => | |
1175 | Make_Subtype_Indication (Loc, | |
1176 | Subtype_Mark => New_Occurrence_Of (PB_Type, Loc), | |
1177 | Constraint => | |
1178 | ||
1179 | Make_Index_Or_Discriminant_Constraint (Loc, | |
1180 | Constraints => New_List ( | |
1181 | Make_Range (Loc, | |
1182 | Low_Bound => | |
1183 | Make_Integer_Literal (Loc, 0), | |
1184 | High_Bound => PAT_High))))); | |
1185 | ||
1186 | Install_PAT; | |
1187 | end if; | |
1188 | end Create_Packed_Array_Type; | |
1189 | ||
1190 | ----------------------------------- | |
1191 | -- Expand_Bit_Packed_Element_Set -- | |
1192 | ----------------------------------- | |
1193 | ||
1194 | procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is | |
1195 | Loc : constant Source_Ptr := Sloc (N); | |
1196 | Lhs : constant Node_Id := Name (N); | |
1197 | ||
1198 | Ass_OK : constant Boolean := Assignment_OK (Lhs); | |
1199 | -- Used to preserve assignment OK status when assignment is rewritten | |
1200 | ||
1201 | Rhs : Node_Id := Expression (N); | |
1202 | -- Initially Rhs is the right hand side value, it will be replaced | |
1203 | -- later by an appropriate unchecked conversion for the assignment. | |
1204 | ||
1205 | Obj : Node_Id; | |
1206 | Atyp : Entity_Id; | |
1207 | PAT : Entity_Id; | |
1208 | Ctyp : Entity_Id; | |
1209 | Csiz : Int; | |
70482933 RK |
1210 | Cmask : Uint; |
1211 | ||
fbf5a39b AC |
1212 | Shift : Node_Id; |
1213 | -- The expression for the shift value that is required | |
1214 | ||
1215 | Shift_Used : Boolean := False; | |
1216 | -- Set True if Shift has been used in the generated code at least | |
1217 | -- once, so that it must be duplicated if used again | |
1218 | ||
70482933 RK |
1219 | New_Lhs : Node_Id; |
1220 | New_Rhs : Node_Id; | |
1221 | ||
1222 | Rhs_Val_Known : Boolean; | |
1223 | Rhs_Val : Uint; | |
1224 | -- If the value of the right hand side as an integer constant is | |
1225 | -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val | |
1226 | -- contains the value. Otherwise Rhs_Val_Known is set False, and | |
1227 | -- the Rhs_Val is undefined. | |
1228 | ||
fbf5a39b AC |
1229 | function Get_Shift return Node_Id; |
1230 | -- Function used to get the value of Shift, making sure that it | |
1231 | -- gets duplicated if the function is called more than once. | |
1232 | ||
1233 | --------------- | |
1234 | -- Get_Shift -- | |
1235 | --------------- | |
1236 | ||
1237 | function Get_Shift return Node_Id is | |
1238 | begin | |
1239 | -- If we used the shift value already, then duplicate it. We | |
1240 | -- set a temporary parent in case actions have to be inserted. | |
1241 | ||
1242 | if Shift_Used then | |
1243 | Set_Parent (Shift, N); | |
1244 | return Duplicate_Subexpr_No_Checks (Shift); | |
1245 | ||
1246 | -- If first time, use Shift unchanged, and set flag for first use | |
1247 | ||
1248 | else | |
1249 | Shift_Used := True; | |
1250 | return Shift; | |
1251 | end if; | |
1252 | end Get_Shift; | |
1253 | ||
1254 | -- Start of processing for Expand_Bit_Packed_Element_Set | |
1255 | ||
70482933 RK |
1256 | begin |
1257 | pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs)))); | |
1258 | ||
1259 | Obj := Relocate_Node (Prefix (Lhs)); | |
1260 | Convert_To_Actual_Subtype (Obj); | |
1261 | Atyp := Etype (Obj); | |
1262 | PAT := Packed_Array_Type (Atyp); | |
1263 | Ctyp := Component_Type (Atyp); | |
1264 | Csiz := UI_To_Int (Component_Size (Atyp)); | |
1265 | ||
1266 | -- We convert the right hand side to the proper subtype to ensure | |
1267 | -- that an appropriate range check is made (since the normal range | |
1268 | -- check from assignment will be lost in the transformations). This | |
1269 | -- conversion is analyzed immediately so that subsequent processing | |
1270 | -- can work with an analyzed Rhs (and e.g. look at its Etype) | |
1271 | ||
1272 | Rhs := Convert_To (Ctyp, Rhs); | |
1273 | Set_Parent (Rhs, N); | |
1274 | Analyze_And_Resolve (Rhs, Ctyp); | |
1275 | ||
1276 | -- Case of component size 1,2,4 or any component size for the modular | |
1277 | -- case. These are the cases for which we can inline the code. | |
1278 | ||
1279 | if Csiz = 1 or else Csiz = 2 or else Csiz = 4 | |
1280 | or else (Present (PAT) and then Is_Modular_Integer_Type (PAT)) | |
1281 | then | |
1282 | Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift); | |
1283 | ||
1284 | -- The statement to be generated is: | |
1285 | ||
1286 | -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift))) | |
1287 | ||
1288 | -- where mask1 is obtained by shifting Cmask left Shift bits | |
1289 | -- and then complementing the result. | |
1290 | ||
1291 | -- the "and Mask1" is omitted if rhs is constant and all 1 bits | |
1292 | ||
1293 | -- the "or ..." is omitted if rhs is constant and all 0 bits | |
1294 | ||
1295 | -- rhs is converted to the appropriate type. | |
1296 | ||
1297 | -- The result is converted back to the array type, since | |
1298 | -- otherwise we lose knowledge of the packed nature. | |
1299 | ||
1300 | -- Determine if right side is all 0 bits or all 1 bits | |
1301 | ||
1302 | if Compile_Time_Known_Value (Rhs) then | |
1303 | Rhs_Val := Expr_Rep_Value (Rhs); | |
1304 | Rhs_Val_Known := True; | |
1305 | ||
1306 | -- The following test catches the case of an unchecked conversion | |
1307 | -- of an integer literal. This results from optimizing aggregates | |
1308 | -- of packed types. | |
1309 | ||
1310 | elsif Nkind (Rhs) = N_Unchecked_Type_Conversion | |
1311 | and then Compile_Time_Known_Value (Expression (Rhs)) | |
1312 | then | |
1313 | Rhs_Val := Expr_Rep_Value (Expression (Rhs)); | |
1314 | Rhs_Val_Known := True; | |
1315 | ||
1316 | else | |
1317 | Rhs_Val := No_Uint; | |
1318 | Rhs_Val_Known := False; | |
1319 | end if; | |
1320 | ||
1321 | -- Some special checks for the case where the right hand value | |
1322 | -- is known at compile time. Basically we have to take care of | |
1323 | -- the implicit conversion to the subtype of the component object. | |
1324 | ||
1325 | if Rhs_Val_Known then | |
1326 | ||
1327 | -- If we have a biased component type then we must manually do | |
1328 | -- the biasing, since we are taking responsibility in this case | |
1329 | -- for constructing the exact bit pattern to be used. | |
1330 | ||
1331 | if Has_Biased_Representation (Ctyp) then | |
1332 | Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp)); | |
1333 | end if; | |
1334 | ||
1335 | -- For a negative value, we manually convert the twos complement | |
1336 | -- value to a corresponding unsigned value, so that the proper | |
1337 | -- field width is maintained. If we did not do this, we would | |
1338 | -- get too many leading sign bits later on. | |
1339 | ||
1340 | if Rhs_Val < 0 then | |
1341 | Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val; | |
1342 | end if; | |
1343 | end if; | |
1344 | ||
1345 | New_Lhs := Duplicate_Subexpr (Obj, True); | |
fbf5a39b | 1346 | New_Rhs := Duplicate_Subexpr_No_Checks (Obj); |
70482933 RK |
1347 | |
1348 | -- First we deal with the "and" | |
1349 | ||
1350 | if not Rhs_Val_Known or else Rhs_Val /= Cmask then | |
1351 | declare | |
1352 | Mask1 : Node_Id; | |
1353 | Lit : Node_Id; | |
1354 | ||
1355 | begin | |
1356 | if Compile_Time_Known_Value (Shift) then | |
1357 | Mask1 := | |
1358 | Make_Integer_Literal (Loc, | |
1359 | Modulus (Etype (Obj)) - 1 - | |
fbf5a39b | 1360 | (Cmask * (2 ** Expr_Value (Get_Shift)))); |
70482933 RK |
1361 | Set_Print_In_Hex (Mask1); |
1362 | ||
1363 | else | |
1364 | Lit := Make_Integer_Literal (Loc, Cmask); | |
1365 | Set_Print_In_Hex (Lit); | |
1366 | Mask1 := | |
1367 | Make_Op_Not (Loc, | |
fbf5a39b | 1368 | Right_Opnd => Make_Shift_Left (Lit, Get_Shift)); |
70482933 RK |
1369 | end if; |
1370 | ||
1371 | New_Rhs := | |
1372 | Make_Op_And (Loc, | |
1373 | Left_Opnd => New_Rhs, | |
1374 | Right_Opnd => Mask1); | |
1375 | end; | |
1376 | end if; | |
1377 | ||
1378 | -- Then deal with the "or" | |
1379 | ||
1380 | if not Rhs_Val_Known or else Rhs_Val /= 0 then | |
1381 | declare | |
1382 | Or_Rhs : Node_Id; | |
1383 | ||
1384 | procedure Fixup_Rhs; | |
1385 | -- Adjust Rhs by bias if biased representation for components | |
1386 | -- or remove extraneous high order sign bits if signed. | |
1387 | ||
1388 | procedure Fixup_Rhs is | |
1389 | Etyp : constant Entity_Id := Etype (Rhs); | |
1390 | ||
1391 | begin | |
1392 | -- For biased case, do the required biasing by simply | |
1393 | -- converting to the biased subtype (the conversion | |
1394 | -- will generate the required bias). | |
1395 | ||
1396 | if Has_Biased_Representation (Ctyp) then | |
1397 | Rhs := Convert_To (Ctyp, Rhs); | |
1398 | ||
1399 | -- For a signed integer type that is not biased, generate | |
1400 | -- a conversion to unsigned to strip high order sign bits. | |
1401 | ||
1402 | elsif Is_Signed_Integer_Type (Ctyp) then | |
1403 | Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs); | |
1404 | end if; | |
1405 | ||
1406 | -- Set Etype, since it can be referenced before the | |
1407 | -- node is completely analyzed. | |
1408 | ||
1409 | Set_Etype (Rhs, Etyp); | |
1410 | ||
1411 | -- We now need to do an unchecked conversion of the | |
1412 | -- result to the target type, but it is important that | |
1413 | -- this conversion be a right justified conversion and | |
1414 | -- not a left justified conversion. | |
1415 | ||
1416 | Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs); | |
1417 | ||
1418 | end Fixup_Rhs; | |
1419 | ||
1420 | begin | |
1421 | if Rhs_Val_Known | |
fbf5a39b | 1422 | and then Compile_Time_Known_Value (Get_Shift) |
70482933 RK |
1423 | then |
1424 | Or_Rhs := | |
1425 | Make_Integer_Literal (Loc, | |
fbf5a39b | 1426 | Rhs_Val * (2 ** Expr_Value (Get_Shift))); |
70482933 RK |
1427 | Set_Print_In_Hex (Or_Rhs); |
1428 | ||
1429 | else | |
1430 | -- We have to convert the right hand side to Etype (Obj). | |
1431 | -- A special case case arises if what we have now is a Val | |
1432 | -- attribute reference whose expression type is Etype (Obj). | |
1433 | -- This happens for assignments of fields from the same | |
1434 | -- array. In this case we get the required right hand side | |
1435 | -- by simply removing the inner attribute reference. | |
1436 | ||
1437 | if Nkind (Rhs) = N_Attribute_Reference | |
1438 | and then Attribute_Name (Rhs) = Name_Val | |
1439 | and then Etype (First (Expressions (Rhs))) = Etype (Obj) | |
1440 | then | |
1441 | Rhs := Relocate_Node (First (Expressions (Rhs))); | |
1442 | Fixup_Rhs; | |
1443 | ||
1444 | -- If the value of the right hand side is a known integer | |
1445 | -- value, then just replace it by an untyped constant, | |
1446 | -- which will be properly retyped when we analyze and | |
1447 | -- resolve the expression. | |
1448 | ||
1449 | elsif Rhs_Val_Known then | |
1450 | ||
1451 | -- Note that Rhs_Val has already been normalized to | |
1452 | -- be an unsigned value with the proper number of bits. | |
1453 | ||
1454 | Rhs := | |
1455 | Make_Integer_Literal (Loc, Rhs_Val); | |
1456 | ||
1457 | -- Otherwise we need an unchecked conversion | |
1458 | ||
1459 | else | |
1460 | Fixup_Rhs; | |
1461 | end if; | |
1462 | ||
fbf5a39b | 1463 | Or_Rhs := Make_Shift_Left (Rhs, Get_Shift); |
70482933 RK |
1464 | end if; |
1465 | ||
1466 | if Nkind (New_Rhs) = N_Op_And then | |
1467 | Set_Paren_Count (New_Rhs, 1); | |
1468 | end if; | |
1469 | ||
1470 | New_Rhs := | |
1471 | Make_Op_Or (Loc, | |
1472 | Left_Opnd => New_Rhs, | |
1473 | Right_Opnd => Or_Rhs); | |
1474 | end; | |
1475 | end if; | |
1476 | ||
1477 | -- Now do the rewrite | |
1478 | ||
1479 | Rewrite (N, | |
1480 | Make_Assignment_Statement (Loc, | |
1481 | Name => New_Lhs, | |
1482 | Expression => | |
1483 | Unchecked_Convert_To (Etype (New_Lhs), New_Rhs))); | |
1484 | Set_Assignment_OK (Name (N), Ass_OK); | |
1485 | ||
1486 | -- All other component sizes for non-modular case | |
1487 | ||
1488 | else | |
1489 | -- We generate | |
1490 | ||
1491 | -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs)) | |
1492 | ||
1493 | -- where Subscr is the computed linear subscript. | |
1494 | ||
1495 | declare | |
1496 | Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz)); | |
1497 | Set_nn : Entity_Id; | |
1498 | Subscr : Node_Id; | |
1499 | Atyp : Entity_Id; | |
1500 | ||
1501 | begin | |
fbf5a39b AC |
1502 | if No (Bits_nn) then |
1503 | ||
1504 | -- Error, most likely High_Integrity_Mode restriction. | |
1505 | ||
1506 | return; | |
1507 | end if; | |
1508 | ||
70482933 RK |
1509 | -- Acquire proper Set entity. We use the aligned or unaligned |
1510 | -- case as appropriate. | |
1511 | ||
980f237d | 1512 | if Known_Aligned_Enough (Obj, Csiz) then |
70482933 RK |
1513 | Set_nn := RTE (Set_Id (Csiz)); |
1514 | else | |
1515 | Set_nn := RTE (SetU_Id (Csiz)); | |
1516 | end if; | |
1517 | ||
1518 | -- Now generate the set reference | |
1519 | ||
1520 | Obj := Relocate_Node (Prefix (Lhs)); | |
1521 | Convert_To_Actual_Subtype (Obj); | |
1522 | Atyp := Etype (Obj); | |
1523 | Compute_Linear_Subscript (Atyp, Lhs, Subscr); | |
1524 | ||
fbf5a39b AC |
1525 | -- Below we must make the assumption that Obj is |
1526 | -- at least byte aligned, since otherwise its address | |
1527 | -- cannot be taken. The assumption holds since the | |
1528 | -- only arrays that can be misaligned are small packed | |
1529 | -- arrays which are implemented as a modular type, and | |
1530 | -- that is not the case here. | |
1531 | ||
70482933 RK |
1532 | Rewrite (N, |
1533 | Make_Procedure_Call_Statement (Loc, | |
1534 | Name => New_Occurrence_Of (Set_nn, Loc), | |
1535 | Parameter_Associations => New_List ( | |
fbf5a39b | 1536 | Make_Attribute_Reference (Loc, |
70482933 | 1537 | Attribute_Name => Name_Address, |
07fc65c4 | 1538 | Prefix => Obj), |
70482933 RK |
1539 | Subscr, |
1540 | Unchecked_Convert_To (Bits_nn, | |
1541 | Convert_To (Ctyp, Rhs))))); | |
1542 | ||
1543 | end; | |
1544 | end if; | |
1545 | ||
1546 | Analyze (N, Suppress => All_Checks); | |
1547 | end Expand_Bit_Packed_Element_Set; | |
1548 | ||
1549 | ------------------------------------- | |
1550 | -- Expand_Packed_Address_Reference -- | |
1551 | ------------------------------------- | |
1552 | ||
1553 | procedure Expand_Packed_Address_Reference (N : Node_Id) is | |
1554 | Loc : constant Source_Ptr := Sloc (N); | |
1555 | Ploc : Source_Ptr; | |
1556 | Pref : Node_Id; | |
1557 | Expr : Node_Id; | |
1558 | Term : Node_Id; | |
1559 | Atyp : Entity_Id; | |
1560 | Subscr : Node_Id; | |
1561 | ||
1562 | begin | |
1563 | Pref := Prefix (N); | |
1564 | Expr := Empty; | |
1565 | ||
1566 | -- We build up an expression serially that has the form | |
1567 | ||
1568 | -- outer_object'Address | |
1569 | -- + (linear-subscript * component_size for each array reference | |
1570 | -- + field'Bit_Position for each record field | |
1571 | -- + ... | |
1572 | -- + ...) / Storage_Unit; | |
1573 | ||
1574 | -- Some additional conversions are required to deal with the addition | |
1575 | -- operation, which is not normally visible to generated code. | |
1576 | ||
1577 | loop | |
1578 | Ploc := Sloc (Pref); | |
1579 | ||
1580 | if Nkind (Pref) = N_Indexed_Component then | |
1581 | Convert_To_Actual_Subtype (Prefix (Pref)); | |
1582 | Atyp := Etype (Prefix (Pref)); | |
1583 | Compute_Linear_Subscript (Atyp, Pref, Subscr); | |
1584 | ||
1585 | Term := | |
1586 | Make_Op_Multiply (Ploc, | |
1587 | Left_Opnd => Subscr, | |
1588 | Right_Opnd => | |
1589 | Make_Attribute_Reference (Ploc, | |
07fc65c4 | 1590 | Prefix => New_Occurrence_Of (Atyp, Ploc), |
70482933 RK |
1591 | Attribute_Name => Name_Component_Size)); |
1592 | ||
1593 | elsif Nkind (Pref) = N_Selected_Component then | |
1594 | Term := | |
1595 | Make_Attribute_Reference (Ploc, | |
07fc65c4 | 1596 | Prefix => Selector_Name (Pref), |
70482933 RK |
1597 | Attribute_Name => Name_Bit_Position); |
1598 | ||
1599 | else | |
1600 | exit; | |
1601 | end if; | |
1602 | ||
1603 | Term := Convert_To (RTE (RE_Integer_Address), Term); | |
1604 | ||
1605 | if No (Expr) then | |
1606 | Expr := Term; | |
1607 | ||
1608 | else | |
1609 | Expr := | |
1610 | Make_Op_Add (Ploc, | |
1611 | Left_Opnd => Expr, | |
1612 | Right_Opnd => Term); | |
1613 | end if; | |
1614 | ||
1615 | Pref := Prefix (Pref); | |
1616 | end loop; | |
1617 | ||
1618 | Rewrite (N, | |
1619 | Unchecked_Convert_To (RTE (RE_Address), | |
1620 | Make_Op_Add (Loc, | |
1621 | Left_Opnd => | |
1622 | Unchecked_Convert_To (RTE (RE_Integer_Address), | |
1623 | Make_Attribute_Reference (Loc, | |
07fc65c4 | 1624 | Prefix => Pref, |
70482933 RK |
1625 | Attribute_Name => Name_Address)), |
1626 | ||
1627 | Right_Opnd => | |
1628 | Make_Op_Divide (Loc, | |
1629 | Left_Opnd => Expr, | |
1630 | Right_Opnd => | |
1631 | Make_Integer_Literal (Loc, System_Storage_Unit))))); | |
1632 | ||
1633 | Analyze_And_Resolve (N, RTE (RE_Address)); | |
1634 | end Expand_Packed_Address_Reference; | |
1635 | ||
1636 | ------------------------------------ | |
1637 | -- Expand_Packed_Boolean_Operator -- | |
1638 | ------------------------------------ | |
1639 | ||
1640 | -- This routine expands "a op b" for the packed cases | |
1641 | ||
1642 | procedure Expand_Packed_Boolean_Operator (N : Node_Id) is | |
1643 | Loc : constant Source_Ptr := Sloc (N); | |
1644 | Typ : constant Entity_Id := Etype (N); | |
1645 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
1646 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
1647 | ||
1648 | Ltyp : Entity_Id; | |
1649 | Rtyp : Entity_Id; | |
1650 | PAT : Entity_Id; | |
1651 | ||
1652 | begin | |
1653 | Convert_To_Actual_Subtype (L); | |
1654 | Convert_To_Actual_Subtype (R); | |
1655 | ||
1656 | Ensure_Defined (Etype (L), N); | |
1657 | Ensure_Defined (Etype (R), N); | |
1658 | ||
1659 | Apply_Length_Check (R, Etype (L)); | |
1660 | ||
1661 | Ltyp := Etype (L); | |
1662 | Rtyp := Etype (R); | |
1663 | ||
1664 | -- First an odd and silly test. We explicitly check for the XOR | |
1665 | -- case where the component type is True .. True, since this will | |
1666 | -- raise constraint error. A special check is required since CE | |
1667 | -- will not be required other wise (cf Expand_Packed_Not). | |
1668 | ||
1669 | -- No such check is required for AND and OR, since for both these | |
1670 | -- cases False op False = False, and True op True = True. | |
1671 | ||
1672 | if Nkind (N) = N_Op_Xor then | |
1673 | declare | |
1674 | CT : constant Entity_Id := Component_Type (Rtyp); | |
1675 | BT : constant Entity_Id := Base_Type (CT); | |
1676 | ||
1677 | begin | |
1678 | Insert_Action (N, | |
1679 | Make_Raise_Constraint_Error (Loc, | |
1680 | Condition => | |
1681 | Make_Op_And (Loc, | |
1682 | Left_Opnd => | |
1683 | Make_Op_Eq (Loc, | |
1684 | Left_Opnd => | |
1685 | Make_Attribute_Reference (Loc, | |
1686 | Prefix => New_Occurrence_Of (CT, Loc), | |
1687 | Attribute_Name => Name_First), | |
1688 | ||
1689 | Right_Opnd => | |
1690 | Convert_To (BT, | |
1691 | New_Occurrence_Of (Standard_True, Loc))), | |
1692 | ||
1693 | Right_Opnd => | |
1694 | Make_Op_Eq (Loc, | |
1695 | Left_Opnd => | |
1696 | Make_Attribute_Reference (Loc, | |
1697 | Prefix => New_Occurrence_Of (CT, Loc), | |
1698 | Attribute_Name => Name_Last), | |
1699 | ||
1700 | Right_Opnd => | |
1701 | Convert_To (BT, | |
07fc65c4 GB |
1702 | New_Occurrence_Of (Standard_True, Loc)))), |
1703 | Reason => CE_Range_Check_Failed)); | |
70482933 RK |
1704 | end; |
1705 | end if; | |
1706 | ||
1707 | -- Now that that silliness is taken care of, get packed array type | |
1708 | ||
1709 | Convert_To_PAT_Type (L); | |
1710 | Convert_To_PAT_Type (R); | |
1711 | ||
1712 | PAT := Etype (L); | |
1713 | ||
1714 | -- For the modular case, we expand a op b into | |
1715 | ||
1716 | -- rtyp!(pat!(a) op pat!(b)) | |
1717 | ||
1718 | -- where rtyp is the Etype of the left operand. Note that we do not | |
1719 | -- convert to the base type, since this would be unconstrained, and | |
1720 | -- hence not have a corresponding packed array type set. | |
1721 | ||
fbf5a39b AC |
1722 | -- Note that both operands must be modular for this code to be used. |
1723 | ||
1724 | if Is_Modular_Integer_Type (PAT) | |
1725 | and then | |
1726 | Is_Modular_Integer_Type (Etype (R)) | |
1727 | then | |
70482933 RK |
1728 | declare |
1729 | P : Node_Id; | |
1730 | ||
1731 | begin | |
1732 | if Nkind (N) = N_Op_And then | |
1733 | P := Make_Op_And (Loc, L, R); | |
1734 | ||
1735 | elsif Nkind (N) = N_Op_Or then | |
1736 | P := Make_Op_Or (Loc, L, R); | |
1737 | ||
1738 | else -- Nkind (N) = N_Op_Xor | |
1739 | P := Make_Op_Xor (Loc, L, R); | |
1740 | end if; | |
1741 | ||
1742 | Rewrite (N, Unchecked_Convert_To (Rtyp, P)); | |
1743 | end; | |
1744 | ||
1745 | -- For the array case, we insert the actions | |
1746 | ||
1747 | -- Result : Ltype; | |
1748 | ||
1749 | -- System.Bitops.Bit_And/Or/Xor | |
1750 | -- (Left'Address, | |
1751 | -- Ltype'Length * Ltype'Component_Size; | |
1752 | -- Right'Address, | |
1753 | -- Rtype'Length * Rtype'Component_Size | |
1754 | -- Result'Address); | |
1755 | ||
1756 | -- where Left and Right are the Packed_Bytes{1,2,4} operands and | |
1757 | -- the second argument and fourth arguments are the lengths of the | |
1758 | -- operands in bits. Then we replace the expression by a reference | |
1759 | -- to Result. | |
1760 | ||
fbf5a39b AC |
1761 | -- Note that if we are mixing a modular and array operand, everything |
1762 | -- works fine, since we ensure that the modular representation has the | |
1763 | -- same physical layout as the array representation (that's what the | |
1764 | -- left justified modular stuff in the big-endian case is about). | |
1765 | ||
70482933 RK |
1766 | else |
1767 | declare | |
1768 | Result_Ent : constant Entity_Id := | |
1769 | Make_Defining_Identifier (Loc, | |
1770 | Chars => New_Internal_Name ('T')); | |
1771 | ||
1772 | E_Id : RE_Id; | |
1773 | ||
1774 | begin | |
1775 | if Nkind (N) = N_Op_And then | |
1776 | E_Id := RE_Bit_And; | |
1777 | ||
1778 | elsif Nkind (N) = N_Op_Or then | |
1779 | E_Id := RE_Bit_Or; | |
1780 | ||
1781 | else -- Nkind (N) = N_Op_Xor | |
1782 | E_Id := RE_Bit_Xor; | |
1783 | end if; | |
1784 | ||
1785 | Insert_Actions (N, New_List ( | |
1786 | ||
1787 | Make_Object_Declaration (Loc, | |
1788 | Defining_Identifier => Result_Ent, | |
1789 | Object_Definition => New_Occurrence_Of (Ltyp, Loc)), | |
1790 | ||
1791 | Make_Procedure_Call_Statement (Loc, | |
1792 | Name => New_Occurrence_Of (RTE (E_Id), Loc), | |
1793 | Parameter_Associations => New_List ( | |
1794 | ||
07fc65c4 | 1795 | Make_Byte_Aligned_Attribute_Reference (Loc, |
70482933 | 1796 | Attribute_Name => Name_Address, |
07fc65c4 | 1797 | Prefix => L), |
70482933 RK |
1798 | |
1799 | Make_Op_Multiply (Loc, | |
1800 | Left_Opnd => | |
1801 | Make_Attribute_Reference (Loc, | |
1802 | Prefix => | |
1803 | New_Occurrence_Of | |
1804 | (Etype (First_Index (Ltyp)), Loc), | |
1805 | Attribute_Name => Name_Range_Length), | |
1806 | Right_Opnd => | |
1807 | Make_Integer_Literal (Loc, Component_Size (Ltyp))), | |
1808 | ||
07fc65c4 | 1809 | Make_Byte_Aligned_Attribute_Reference (Loc, |
70482933 | 1810 | Attribute_Name => Name_Address, |
07fc65c4 | 1811 | Prefix => R), |
70482933 RK |
1812 | |
1813 | Make_Op_Multiply (Loc, | |
1814 | Left_Opnd => | |
1815 | Make_Attribute_Reference (Loc, | |
1816 | Prefix => | |
1817 | New_Occurrence_Of | |
1818 | (Etype (First_Index (Rtyp)), Loc), | |
1819 | Attribute_Name => Name_Range_Length), | |
1820 | Right_Opnd => | |
1821 | Make_Integer_Literal (Loc, Component_Size (Rtyp))), | |
1822 | ||
07fc65c4 | 1823 | Make_Byte_Aligned_Attribute_Reference (Loc, |
70482933 RK |
1824 | Attribute_Name => Name_Address, |
1825 | Prefix => New_Occurrence_Of (Result_Ent, Loc)))))); | |
1826 | ||
1827 | Rewrite (N, | |
1828 | New_Occurrence_Of (Result_Ent, Loc)); | |
1829 | end; | |
1830 | end if; | |
1831 | ||
1832 | Analyze_And_Resolve (N, Typ, Suppress => All_Checks); | |
1833 | end Expand_Packed_Boolean_Operator; | |
1834 | ||
1835 | ------------------------------------- | |
1836 | -- Expand_Packed_Element_Reference -- | |
1837 | ------------------------------------- | |
1838 | ||
1839 | procedure Expand_Packed_Element_Reference (N : Node_Id) is | |
1840 | Loc : constant Source_Ptr := Sloc (N); | |
1841 | Obj : Node_Id; | |
1842 | Atyp : Entity_Id; | |
1843 | PAT : Entity_Id; | |
1844 | Ctyp : Entity_Id; | |
1845 | Csiz : Int; | |
1846 | Shift : Node_Id; | |
1847 | Cmask : Uint; | |
1848 | Lit : Node_Id; | |
1849 | Arg : Node_Id; | |
1850 | ||
1851 | begin | |
1852 | -- If not bit packed, we have the enumeration case, which is easily | |
1853 | -- dealt with (just adjust the subscripts of the indexed component) | |
1854 | ||
1855 | -- Note: this leaves the result as an indexed component, which is | |
1856 | -- still a variable, so can be used in the assignment case, as is | |
1857 | -- required in the enumeration case. | |
1858 | ||
1859 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
1860 | Setup_Enumeration_Packed_Array_Reference (N); | |
1861 | return; | |
1862 | end if; | |
1863 | ||
1864 | -- Remaining processing is for the bit-packed case. | |
1865 | ||
1866 | Obj := Relocate_Node (Prefix (N)); | |
1867 | Convert_To_Actual_Subtype (Obj); | |
1868 | Atyp := Etype (Obj); | |
1869 | PAT := Packed_Array_Type (Atyp); | |
1870 | Ctyp := Component_Type (Atyp); | |
1871 | Csiz := UI_To_Int (Component_Size (Atyp)); | |
1872 | ||
1873 | -- Case of component size 1,2,4 or any component size for the modular | |
1874 | -- case. These are the cases for which we can inline the code. | |
1875 | ||
1876 | if Csiz = 1 or else Csiz = 2 or else Csiz = 4 | |
1877 | or else (Present (PAT) and then Is_Modular_Integer_Type (PAT)) | |
1878 | then | |
1879 | Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift); | |
1880 | Lit := Make_Integer_Literal (Loc, Cmask); | |
1881 | Set_Print_In_Hex (Lit); | |
1882 | ||
1883 | -- We generate a shift right to position the field, followed by a | |
1884 | -- masking operation to extract the bit field, and we finally do an | |
1885 | -- unchecked conversion to convert the result to the required target. | |
1886 | ||
1887 | -- Note that the unchecked conversion automatically deals with the | |
1888 | -- bias if we are dealing with a biased representation. What will | |
1889 | -- happen is that we temporarily generate the biased representation, | |
1890 | -- but almost immediately that will be converted to the original | |
1891 | -- unbiased component type, and the bias will disappear. | |
1892 | ||
1893 | Arg := | |
1894 | Make_Op_And (Loc, | |
1895 | Left_Opnd => Make_Shift_Right (Obj, Shift), | |
1896 | Right_Opnd => Lit); | |
1897 | ||
fbf5a39b AC |
1898 | -- We neded to analyze this before we do the unchecked convert |
1899 | -- below, but we need it temporarily attached to the tree for | |
1900 | -- this analysis (hence the temporary Set_Parent call). | |
1901 | ||
1902 | Set_Parent (Arg, Parent (N)); | |
70482933 RK |
1903 | Analyze_And_Resolve (Arg); |
1904 | ||
1905 | Rewrite (N, | |
1906 | RJ_Unchecked_Convert_To (Ctyp, Arg)); | |
1907 | ||
1908 | -- All other component sizes for non-modular case | |
1909 | ||
1910 | else | |
1911 | -- We generate | |
1912 | ||
1913 | -- Component_Type!(Get_nn (Arr'address, Subscr)) | |
1914 | ||
1915 | -- where Subscr is the computed linear subscript. | |
1916 | ||
1917 | declare | |
1918 | Get_nn : Entity_Id; | |
1919 | Subscr : Node_Id; | |
1920 | ||
1921 | begin | |
1922 | -- Acquire proper Get entity. We use the aligned or unaligned | |
1923 | -- case as appropriate. | |
1924 | ||
980f237d | 1925 | if Known_Aligned_Enough (Obj, Csiz) then |
70482933 RK |
1926 | Get_nn := RTE (Get_Id (Csiz)); |
1927 | else | |
1928 | Get_nn := RTE (GetU_Id (Csiz)); | |
1929 | end if; | |
1930 | ||
1931 | -- Now generate the get reference | |
1932 | ||
1933 | Compute_Linear_Subscript (Atyp, N, Subscr); | |
1934 | ||
fbf5a39b AC |
1935 | -- Below we make the assumption that Obj is at least byte |
1936 | -- aligned, since otherwise its address cannot be taken. | |
1937 | -- The assumption holds since the only arrays that can be | |
1938 | -- misaligned are small packed arrays which are implemented | |
1939 | -- as a modular type, and that is not the case here. | |
1940 | ||
70482933 RK |
1941 | Rewrite (N, |
1942 | Unchecked_Convert_To (Ctyp, | |
1943 | Make_Function_Call (Loc, | |
1944 | Name => New_Occurrence_Of (Get_nn, Loc), | |
1945 | Parameter_Associations => New_List ( | |
fbf5a39b | 1946 | Make_Attribute_Reference (Loc, |
70482933 | 1947 | Attribute_Name => Name_Address, |
07fc65c4 | 1948 | Prefix => Obj), |
70482933 RK |
1949 | Subscr)))); |
1950 | end; | |
1951 | end if; | |
1952 | ||
1953 | Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks); | |
1954 | ||
1955 | end Expand_Packed_Element_Reference; | |
1956 | ||
1957 | ---------------------- | |
1958 | -- Expand_Packed_Eq -- | |
1959 | ---------------------- | |
1960 | ||
1961 | -- Handles expansion of "=" on packed array types | |
1962 | ||
1963 | procedure Expand_Packed_Eq (N : Node_Id) is | |
1964 | Loc : constant Source_Ptr := Sloc (N); | |
1965 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
1966 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
1967 | ||
1968 | LLexpr : Node_Id; | |
1969 | RLexpr : Node_Id; | |
1970 | ||
1971 | Ltyp : Entity_Id; | |
1972 | Rtyp : Entity_Id; | |
1973 | PAT : Entity_Id; | |
1974 | ||
1975 | begin | |
1976 | Convert_To_Actual_Subtype (L); | |
1977 | Convert_To_Actual_Subtype (R); | |
1978 | Ltyp := Underlying_Type (Etype (L)); | |
1979 | Rtyp := Underlying_Type (Etype (R)); | |
1980 | ||
1981 | Convert_To_PAT_Type (L); | |
1982 | Convert_To_PAT_Type (R); | |
1983 | PAT := Etype (L); | |
1984 | ||
1985 | LLexpr := | |
1986 | Make_Op_Multiply (Loc, | |
1987 | Left_Opnd => | |
1988 | Make_Attribute_Reference (Loc, | |
1989 | Attribute_Name => Name_Length, | |
07fc65c4 | 1990 | Prefix => New_Occurrence_Of (Ltyp, Loc)), |
70482933 RK |
1991 | Right_Opnd => |
1992 | Make_Integer_Literal (Loc, Component_Size (Ltyp))); | |
1993 | ||
1994 | RLexpr := | |
1995 | Make_Op_Multiply (Loc, | |
1996 | Left_Opnd => | |
1997 | Make_Attribute_Reference (Loc, | |
1998 | Attribute_Name => Name_Length, | |
07fc65c4 | 1999 | Prefix => New_Occurrence_Of (Rtyp, Loc)), |
70482933 RK |
2000 | Right_Opnd => |
2001 | Make_Integer_Literal (Loc, Component_Size (Rtyp))); | |
2002 | ||
2003 | -- For the modular case, we transform the comparison to: | |
2004 | ||
2005 | -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R) | |
2006 | ||
2007 | -- where PAT is the packed array type. This works fine, since in the | |
2008 | -- modular case we guarantee that the unused bits are always zeroes. | |
2009 | -- We do have to compare the lengths because we could be comparing | |
2010 | -- two different subtypes of the same base type. | |
2011 | ||
2012 | if Is_Modular_Integer_Type (PAT) then | |
2013 | Rewrite (N, | |
2014 | Make_And_Then (Loc, | |
2015 | Left_Opnd => | |
2016 | Make_Op_Eq (Loc, | |
2017 | Left_Opnd => LLexpr, | |
2018 | Right_Opnd => RLexpr), | |
2019 | ||
2020 | Right_Opnd => | |
2021 | Make_Op_Eq (Loc, | |
2022 | Left_Opnd => L, | |
2023 | Right_Opnd => R))); | |
2024 | ||
2025 | -- For the non-modular case, we call a runtime routine | |
2026 | ||
2027 | -- System.Bit_Ops.Bit_Eq | |
2028 | -- (L'Address, L_Length, R'Address, R_Length) | |
2029 | ||
2030 | -- where PAT is the packed array type, and the lengths are the lengths | |
2031 | -- in bits of the original packed arrays. This routine takes care of | |
2032 | -- not comparing the unused bits in the last byte. | |
2033 | ||
2034 | else | |
2035 | Rewrite (N, | |
2036 | Make_Function_Call (Loc, | |
2037 | Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc), | |
2038 | Parameter_Associations => New_List ( | |
07fc65c4 | 2039 | Make_Byte_Aligned_Attribute_Reference (Loc, |
70482933 | 2040 | Attribute_Name => Name_Address, |
07fc65c4 | 2041 | Prefix => L), |
70482933 RK |
2042 | |
2043 | LLexpr, | |
2044 | ||
07fc65c4 | 2045 | Make_Byte_Aligned_Attribute_Reference (Loc, |
70482933 | 2046 | Attribute_Name => Name_Address, |
07fc65c4 | 2047 | Prefix => R), |
70482933 RK |
2048 | |
2049 | RLexpr))); | |
2050 | end if; | |
2051 | ||
2052 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
2053 | end Expand_Packed_Eq; | |
2054 | ||
2055 | ----------------------- | |
2056 | -- Expand_Packed_Not -- | |
2057 | ----------------------- | |
2058 | ||
2059 | -- Handles expansion of "not" on packed array types | |
2060 | ||
2061 | procedure Expand_Packed_Not (N : Node_Id) is | |
2062 | Loc : constant Source_Ptr := Sloc (N); | |
2063 | Typ : constant Entity_Id := Etype (N); | |
2064 | Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
2065 | ||
2066 | Rtyp : Entity_Id; | |
2067 | PAT : Entity_Id; | |
2068 | Lit : Node_Id; | |
2069 | ||
2070 | begin | |
2071 | Convert_To_Actual_Subtype (Opnd); | |
2072 | Rtyp := Etype (Opnd); | |
2073 | ||
2074 | -- First an odd and silly test. We explicitly check for the case | |
2075 | -- where the 'First of the component type is equal to the 'Last of | |
2076 | -- this component type, and if this is the case, we make sure that | |
2077 | -- constraint error is raised. The reason is that the NOT is bound | |
2078 | -- to cause CE in this case, and we will not otherwise catch it. | |
2079 | ||
2080 | -- Believe it or not, this was reported as a bug. Note that nearly | |
2081 | -- always, the test will evaluate statically to False, so the code | |
2082 | -- will be statically removed, and no extra overhead caused. | |
2083 | ||
2084 | declare | |
2085 | CT : constant Entity_Id := Component_Type (Rtyp); | |
2086 | ||
2087 | begin | |
2088 | Insert_Action (N, | |
2089 | Make_Raise_Constraint_Error (Loc, | |
2090 | Condition => | |
2091 | Make_Op_Eq (Loc, | |
2092 | Left_Opnd => | |
2093 | Make_Attribute_Reference (Loc, | |
2094 | Prefix => New_Occurrence_Of (CT, Loc), | |
2095 | Attribute_Name => Name_First), | |
2096 | ||
2097 | Right_Opnd => | |
2098 | Make_Attribute_Reference (Loc, | |
2099 | Prefix => New_Occurrence_Of (CT, Loc), | |
07fc65c4 GB |
2100 | Attribute_Name => Name_Last)), |
2101 | Reason => CE_Range_Check_Failed)); | |
70482933 RK |
2102 | end; |
2103 | ||
2104 | -- Now that that silliness is taken care of, get packed array type | |
2105 | ||
2106 | Convert_To_PAT_Type (Opnd); | |
2107 | PAT := Etype (Opnd); | |
2108 | ||
2109 | -- For the case where the packed array type is a modular type, | |
2110 | -- not A expands simply into: | |
2111 | ||
2112 | -- rtyp!(PAT!(A) xor mask) | |
2113 | ||
2114 | -- where PAT is the packed array type, and mask is a mask of all | |
2115 | -- one bits of length equal to the size of this packed type and | |
2116 | -- rtyp is the actual subtype of the operand | |
2117 | ||
2118 | Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1); | |
2119 | Set_Print_In_Hex (Lit); | |
2120 | ||
2121 | if not Is_Array_Type (PAT) then | |
2122 | Rewrite (N, | |
2123 | Unchecked_Convert_To (Rtyp, | |
2124 | Make_Op_Xor (Loc, | |
2125 | Left_Opnd => Opnd, | |
2126 | Right_Opnd => Lit))); | |
2127 | ||
2128 | -- For the array case, we insert the actions | |
2129 | ||
2130 | -- Result : Typ; | |
2131 | ||
2132 | -- System.Bitops.Bit_Not | |
2133 | -- (Opnd'Address, | |
2134 | -- Typ'Length * Typ'Component_Size; | |
2135 | -- Result'Address); | |
2136 | ||
2137 | -- where Opnd is the Packed_Bytes{1,2,4} operand and the second | |
2138 | -- argument is the length of the operand in bits. Then we replace | |
2139 | -- the expression by a reference to Result. | |
2140 | ||
2141 | else | |
2142 | declare | |
2143 | Result_Ent : constant Entity_Id := | |
2144 | Make_Defining_Identifier (Loc, | |
2145 | Chars => New_Internal_Name ('T')); | |
2146 | ||
2147 | begin | |
2148 | Insert_Actions (N, New_List ( | |
2149 | ||
2150 | Make_Object_Declaration (Loc, | |
2151 | Defining_Identifier => Result_Ent, | |
2152 | Object_Definition => New_Occurrence_Of (Rtyp, Loc)), | |
2153 | ||
2154 | Make_Procedure_Call_Statement (Loc, | |
2155 | Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc), | |
2156 | Parameter_Associations => New_List ( | |
2157 | ||
07fc65c4 | 2158 | Make_Byte_Aligned_Attribute_Reference (Loc, |
70482933 | 2159 | Attribute_Name => Name_Address, |
07fc65c4 | 2160 | Prefix => Opnd), |
70482933 RK |
2161 | |
2162 | Make_Op_Multiply (Loc, | |
2163 | Left_Opnd => | |
2164 | Make_Attribute_Reference (Loc, | |
2165 | Prefix => | |
2166 | New_Occurrence_Of | |
2167 | (Etype (First_Index (Rtyp)), Loc), | |
2168 | Attribute_Name => Name_Range_Length), | |
2169 | Right_Opnd => | |
2170 | Make_Integer_Literal (Loc, Component_Size (Rtyp))), | |
2171 | ||
07fc65c4 | 2172 | Make_Byte_Aligned_Attribute_Reference (Loc, |
70482933 RK |
2173 | Attribute_Name => Name_Address, |
2174 | Prefix => New_Occurrence_Of (Result_Ent, Loc)))))); | |
2175 | ||
2176 | Rewrite (N, | |
2177 | New_Occurrence_Of (Result_Ent, Loc)); | |
2178 | end; | |
2179 | end if; | |
2180 | ||
2181 | Analyze_And_Resolve (N, Typ, Suppress => All_Checks); | |
2182 | ||
2183 | end Expand_Packed_Not; | |
2184 | ||
2185 | ------------------------------------- | |
2186 | -- Involves_Packed_Array_Reference -- | |
2187 | ------------------------------------- | |
2188 | ||
2189 | function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is | |
2190 | begin | |
2191 | if Nkind (N) = N_Indexed_Component | |
2192 | and then Is_Bit_Packed_Array (Etype (Prefix (N))) | |
2193 | then | |
2194 | return True; | |
2195 | ||
2196 | elsif Nkind (N) = N_Selected_Component then | |
2197 | return Involves_Packed_Array_Reference (Prefix (N)); | |
2198 | ||
2199 | else | |
2200 | return False; | |
2201 | end if; | |
2202 | end Involves_Packed_Array_Reference; | |
2203 | ||
980f237d GB |
2204 | -------------------------- |
2205 | -- Known_Aligned_Enough -- | |
2206 | -------------------------- | |
2207 | ||
2208 | function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is | |
2209 | Typ : constant Entity_Id := Etype (Obj); | |
2210 | ||
2211 | function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean; | |
2212 | -- If the component is in a record that contains previous packed | |
2213 | -- components, consider it unaligned because the back-end might | |
2214 | -- choose to pack the rest of the record. Lead to less efficient code, | |
2215 | -- but safer vis-a-vis of back-end choices. | |
2216 | ||
2217 | -------------------------------- | |
2218 | -- In_Partially_Packed_Record -- | |
2219 | -------------------------------- | |
2220 | ||
2221 | function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is | |
2222 | Rec_Type : constant Entity_Id := Scope (Comp); | |
2223 | Prev_Comp : Entity_Id; | |
2224 | ||
2225 | begin | |
2226 | Prev_Comp := First_Entity (Rec_Type); | |
2227 | while Present (Prev_Comp) loop | |
2228 | if Is_Packed (Etype (Prev_Comp)) then | |
2229 | return True; | |
2230 | ||
2231 | elsif Prev_Comp = Comp then | |
2232 | return False; | |
2233 | end if; | |
2234 | ||
2235 | Next_Entity (Prev_Comp); | |
2236 | end loop; | |
2237 | ||
2238 | return False; | |
2239 | end In_Partially_Packed_Record; | |
2240 | ||
2241 | -- Start of processing for Known_Aligned_Enough | |
2242 | ||
2243 | begin | |
2244 | -- Odd bit sizes don't need alignment anyway | |
2245 | ||
2246 | if Csiz mod 2 = 1 then | |
2247 | return True; | |
2248 | ||
2249 | -- If we have a specified alignment, see if it is sufficient, if not | |
2250 | -- then we can't possibly be aligned enough in any case. | |
2251 | ||
07fc65c4 | 2252 | elsif Known_Alignment (Etype (Obj)) then |
980f237d GB |
2253 | -- Alignment required is 4 if size is a multiple of 4, and |
2254 | -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2) | |
2255 | ||
07fc65c4 | 2256 | if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then |
980f237d GB |
2257 | return False; |
2258 | end if; | |
2259 | end if; | |
2260 | ||
2261 | -- OK, alignment should be sufficient, if object is aligned | |
2262 | ||
2263 | -- If object is strictly aligned, then it is definitely aligned | |
2264 | ||
2265 | if Strict_Alignment (Typ) then | |
2266 | return True; | |
2267 | ||
2268 | -- Case of subscripted array reference | |
2269 | ||
2270 | elsif Nkind (Obj) = N_Indexed_Component then | |
2271 | ||
2272 | -- If we have a pointer to an array, then this is definitely | |
2273 | -- aligned, because pointers always point to aligned versions. | |
2274 | ||
2275 | if Is_Access_Type (Etype (Prefix (Obj))) then | |
2276 | return True; | |
2277 | ||
2278 | -- Otherwise, go look at the prefix | |
2279 | ||
2280 | else | |
2281 | return Known_Aligned_Enough (Prefix (Obj), Csiz); | |
2282 | end if; | |
2283 | ||
2284 | -- Case of record field | |
2285 | ||
2286 | elsif Nkind (Obj) = N_Selected_Component then | |
2287 | ||
2288 | -- What is significant here is whether the record type is packed | |
2289 | ||
2290 | if Is_Record_Type (Etype (Prefix (Obj))) | |
2291 | and then Is_Packed (Etype (Prefix (Obj))) | |
2292 | then | |
2293 | return False; | |
2294 | ||
2295 | -- Or the component has a component clause which might cause | |
2296 | -- the component to become unaligned (we can't tell if the | |
2297 | -- backend is doing alignment computations). | |
2298 | ||
2299 | elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then | |
2300 | return False; | |
2301 | ||
2302 | elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then | |
2303 | return False; | |
2304 | ||
2305 | -- In all other cases, go look at prefix | |
2306 | ||
2307 | else | |
2308 | return Known_Aligned_Enough (Prefix (Obj), Csiz); | |
2309 | end if; | |
2310 | ||
fbf5a39b AC |
2311 | elsif Nkind (Obj) = N_Type_Conversion then |
2312 | return Known_Aligned_Enough (Expression (Obj), Csiz); | |
980f237d | 2313 | |
fbf5a39b AC |
2314 | -- For a formal parameter, it is safer to assume that it is not |
2315 | -- aligned, because the formal may be unconstrained while the actual | |
2316 | -- is constrained. In this situation, a small constrained packed | |
2317 | -- array, represented in modular form, may be unaligned. | |
2318 | ||
2319 | elsif Is_Entity_Name (Obj) then | |
2320 | return not Is_Formal (Entity (Obj)); | |
980f237d | 2321 | else |
fbf5a39b AC |
2322 | |
2323 | -- If none of the above, must be aligned | |
980f237d GB |
2324 | return True; |
2325 | end if; | |
2326 | end Known_Aligned_Enough; | |
2327 | ||
70482933 RK |
2328 | --------------------- |
2329 | -- Make_Shift_Left -- | |
2330 | --------------------- | |
2331 | ||
2332 | function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is | |
2333 | Nod : Node_Id; | |
2334 | ||
2335 | begin | |
2336 | if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then | |
2337 | return N; | |
2338 | else | |
2339 | Nod := | |
2340 | Make_Op_Shift_Left (Sloc (N), | |
2341 | Left_Opnd => N, | |
2342 | Right_Opnd => S); | |
2343 | Set_Shift_Count_OK (Nod, True); | |
2344 | return Nod; | |
2345 | end if; | |
2346 | end Make_Shift_Left; | |
2347 | ||
2348 | ---------------------- | |
2349 | -- Make_Shift_Right -- | |
2350 | ---------------------- | |
2351 | ||
2352 | function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is | |
2353 | Nod : Node_Id; | |
2354 | ||
2355 | begin | |
2356 | if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then | |
2357 | return N; | |
2358 | else | |
2359 | Nod := | |
2360 | Make_Op_Shift_Right (Sloc (N), | |
2361 | Left_Opnd => N, | |
2362 | Right_Opnd => S); | |
2363 | Set_Shift_Count_OK (Nod, True); | |
2364 | return Nod; | |
2365 | end if; | |
2366 | end Make_Shift_Right; | |
2367 | ||
2368 | ----------------------------- | |
2369 | -- RJ_Unchecked_Convert_To -- | |
2370 | ----------------------------- | |
2371 | ||
2372 | function RJ_Unchecked_Convert_To | |
2373 | (Typ : Entity_Id; | |
2374 | Expr : Node_Id) | |
2375 | return Node_Id | |
2376 | is | |
2377 | Source_Typ : constant Entity_Id := Etype (Expr); | |
2378 | Target_Typ : constant Entity_Id := Typ; | |
2379 | ||
2380 | Src : Node_Id := Expr; | |
2381 | ||
2382 | Source_Siz : Nat; | |
2383 | Target_Siz : Nat; | |
2384 | ||
2385 | begin | |
2386 | Source_Siz := UI_To_Int (RM_Size (Source_Typ)); | |
2387 | Target_Siz := UI_To_Int (RM_Size (Target_Typ)); | |
2388 | ||
fbf5a39b AC |
2389 | -- First step, if the source type is not a discrete type, then we |
2390 | -- first convert to a modular type of the source length, since | |
2391 | -- otherwise, on a big-endian machine, we get left-justification. | |
2392 | -- We do it for little-endian machines as well, because there might | |
2393 | -- be junk bits that are not cleared if the type is not numeric. | |
2394 | ||
2395 | if Source_Siz /= Target_Siz | |
2396 | and then not Is_Discrete_Type (Source_Typ) | |
2397 | then | |
2398 | Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src); | |
2399 | end if; | |
2400 | ||
70482933 RK |
2401 | -- In the big endian case, if the lengths of the two types differ, |
2402 | -- then we must worry about possible left justification in the | |
2403 | -- conversion, and avoiding that is what this is all about. | |
2404 | ||
2405 | if Bytes_Big_Endian and then Source_Siz /= Target_Siz then | |
2406 | ||
70482933 RK |
2407 | -- Next step. If the target is not a discrete type, then we first |
2408 | -- convert to a modular type of the target length, since | |
2409 | -- otherwise, on a big-endian machine, we get left-justification. | |
2410 | ||
2411 | if not Is_Discrete_Type (Target_Typ) then | |
2412 | Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src); | |
2413 | end if; | |
2414 | end if; | |
2415 | ||
2416 | -- And now we can do the final conversion to the target type | |
2417 | ||
2418 | return Unchecked_Convert_To (Target_Typ, Src); | |
2419 | end RJ_Unchecked_Convert_To; | |
2420 | ||
2421 | ---------------------------------------------- | |
2422 | -- Setup_Enumeration_Packed_Array_Reference -- | |
2423 | ---------------------------------------------- | |
2424 | ||
2425 | -- All we have to do here is to find the subscripts that correspond | |
2426 | -- to the index positions that have non-standard enumeration types | |
2427 | -- and insert a Pos attribute to get the proper subscript value. | |
980f237d | 2428 | |
70482933 RK |
2429 | -- Finally the prefix must be uncheck converted to the corresponding |
2430 | -- packed array type. | |
2431 | ||
2432 | -- Note that the component type is unchanged, so we do not need to | |
2433 | -- fiddle with the types (Gigi always automatically takes the packed | |
2434 | -- array type if it is set, as it will be in this case). | |
2435 | ||
2436 | procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is | |
2437 | Pfx : constant Node_Id := Prefix (N); | |
2438 | Typ : constant Entity_Id := Etype (N); | |
2439 | Exprs : constant List_Id := Expressions (N); | |
2440 | Expr : Node_Id; | |
2441 | ||
2442 | begin | |
2443 | -- If the array is unconstrained, then we replace the array | |
2444 | -- reference with its actual subtype. This actual subtype will | |
2445 | -- have a packed array type with appropriate bounds. | |
2446 | ||
2447 | if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then | |
2448 | Convert_To_Actual_Subtype (Pfx); | |
2449 | end if; | |
2450 | ||
2451 | Expr := First (Exprs); | |
2452 | while Present (Expr) loop | |
2453 | declare | |
2454 | Loc : constant Source_Ptr := Sloc (Expr); | |
2455 | Expr_Typ : constant Entity_Id := Etype (Expr); | |
2456 | ||
2457 | begin | |
2458 | if Is_Enumeration_Type (Expr_Typ) | |
2459 | and then Has_Non_Standard_Rep (Expr_Typ) | |
2460 | then | |
2461 | Rewrite (Expr, | |
2462 | Make_Attribute_Reference (Loc, | |
07fc65c4 | 2463 | Prefix => New_Occurrence_Of (Expr_Typ, Loc), |
70482933 RK |
2464 | Attribute_Name => Name_Pos, |
2465 | Expressions => New_List (Relocate_Node (Expr)))); | |
2466 | Analyze_And_Resolve (Expr, Standard_Natural); | |
2467 | end if; | |
2468 | end; | |
2469 | ||
2470 | Next (Expr); | |
2471 | end loop; | |
2472 | ||
2473 | Rewrite (N, | |
2474 | Make_Indexed_Component (Sloc (N), | |
2475 | Prefix => | |
2476 | Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx), | |
2477 | Expressions => Exprs)); | |
2478 | ||
2479 | Analyze_And_Resolve (N, Typ); | |
2480 | ||
2481 | end Setup_Enumeration_Packed_Array_Reference; | |
2482 | ||
2483 | ----------------------------------------- | |
2484 | -- Setup_Inline_Packed_Array_Reference -- | |
2485 | ----------------------------------------- | |
2486 | ||
2487 | procedure Setup_Inline_Packed_Array_Reference | |
2488 | (N : Node_Id; | |
2489 | Atyp : Entity_Id; | |
2490 | Obj : in out Node_Id; | |
2491 | Cmask : out Uint; | |
2492 | Shift : out Node_Id) | |
2493 | is | |
2494 | Loc : constant Source_Ptr := Sloc (N); | |
70482933 RK |
2495 | PAT : Entity_Id; |
2496 | Otyp : Entity_Id; | |
2497 | Csiz : Uint; | |
2498 | Osiz : Uint; | |
2499 | ||
2500 | begin | |
70482933 RK |
2501 | Csiz := Component_Size (Atyp); |
2502 | ||
2503 | Convert_To_PAT_Type (Obj); | |
2504 | PAT := Etype (Obj); | |
2505 | ||
2506 | Cmask := 2 ** Csiz - 1; | |
2507 | ||
2508 | if Is_Array_Type (PAT) then | |
2509 | Otyp := Component_Type (PAT); | |
fbf5a39b | 2510 | Osiz := Component_Size (PAT); |
70482933 RK |
2511 | |
2512 | else | |
2513 | Otyp := PAT; | |
2514 | ||
2515 | -- In the case where the PAT is a modular type, we want the actual | |
2516 | -- size in bits of the modular value we use. This is neither the | |
2517 | -- Object_Size nor the Value_Size, either of which may have been | |
2518 | -- reset to strange values, but rather the minimum size. Note that | |
2519 | -- since this is a modular type with full range, the issue of | |
2520 | -- biased representation does not arise. | |
2521 | ||
2522 | Osiz := UI_From_Int (Minimum_Size (Otyp)); | |
2523 | end if; | |
2524 | ||
2525 | Compute_Linear_Subscript (Atyp, N, Shift); | |
2526 | ||
2527 | -- If the component size is not 1, then the subscript must be | |
2528 | -- multiplied by the component size to get the shift count. | |
2529 | ||
2530 | if Csiz /= 1 then | |
2531 | Shift := | |
2532 | Make_Op_Multiply (Loc, | |
2533 | Left_Opnd => Make_Integer_Literal (Loc, Csiz), | |
2534 | Right_Opnd => Shift); | |
2535 | end if; | |
2536 | ||
2537 | -- If we have the array case, then this shift count must be broken | |
2538 | -- down into a byte subscript, and a shift within the byte. | |
2539 | ||
2540 | if Is_Array_Type (PAT) then | |
2541 | ||
2542 | declare | |
2543 | New_Shift : Node_Id; | |
2544 | ||
2545 | begin | |
2546 | -- We must analyze shift, since we will duplicate it | |
2547 | ||
2548 | Set_Parent (Shift, N); | |
2549 | Analyze_And_Resolve | |
2550 | (Shift, Standard_Integer, Suppress => All_Checks); | |
2551 | ||
2552 | -- The shift count within the word is | |
2553 | -- shift mod Osiz | |
2554 | ||
2555 | New_Shift := | |
2556 | Make_Op_Mod (Loc, | |
2557 | Left_Opnd => Duplicate_Subexpr (Shift), | |
2558 | Right_Opnd => Make_Integer_Literal (Loc, Osiz)); | |
2559 | ||
2560 | -- The subscript to be used on the PAT array is | |
2561 | -- shift / Osiz | |
2562 | ||
2563 | Obj := | |
2564 | Make_Indexed_Component (Loc, | |
2565 | Prefix => Obj, | |
2566 | Expressions => New_List ( | |
2567 | Make_Op_Divide (Loc, | |
2568 | Left_Opnd => Duplicate_Subexpr (Shift), | |
2569 | Right_Opnd => Make_Integer_Literal (Loc, Osiz)))); | |
2570 | ||
2571 | Shift := New_Shift; | |
2572 | end; | |
2573 | ||
2574 | -- For the modular integer case, the object to be manipulated is | |
2575 | -- the entire array, so Obj is unchanged. Note that we will reset | |
2576 | -- its type to PAT before returning to the caller. | |
2577 | ||
2578 | else | |
2579 | null; | |
2580 | end if; | |
2581 | ||
2582 | -- The one remaining step is to modify the shift count for the | |
2583 | -- big-endian case. Consider the following example in a byte: | |
2584 | ||
2585 | -- xxxxxxxx bits of byte | |
2586 | -- vvvvvvvv bits of value | |
2587 | -- 33221100 little-endian numbering | |
2588 | -- 00112233 big-endian numbering | |
2589 | ||
2590 | -- Here we have the case of 2-bit fields | |
2591 | ||
2592 | -- For the little-endian case, we already have the proper shift | |
2593 | -- count set, e.g. for element 2, the shift count is 2*2 = 4. | |
2594 | ||
2595 | -- For the big endian case, we have to adjust the shift count, | |
2596 | -- computing it as (N - F) - shift, where N is the number of bits | |
2597 | -- in an element of the array used to implement the packed array, | |
2598 | -- F is the number of bits in a source level array element, and | |
2599 | -- shift is the count so far computed. | |
2600 | ||
2601 | if Bytes_Big_Endian then | |
2602 | Shift := | |
2603 | Make_Op_Subtract (Loc, | |
2604 | Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz), | |
2605 | Right_Opnd => Shift); | |
2606 | end if; | |
2607 | ||
2608 | Set_Parent (Shift, N); | |
2609 | Set_Parent (Obj, N); | |
2610 | Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks); | |
2611 | Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks); | |
2612 | ||
2613 | -- Make sure final type of object is the appropriate packed type | |
2614 | ||
2615 | Set_Etype (Obj, Otyp); | |
2616 | ||
2617 | end Setup_Inline_Packed_Array_Reference; | |
2618 | ||
2619 | end Exp_Pakd; |