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