configure.ac: GCC_NO_EXECUTABLES was supposed to be commented in the patch from 3...
[official-gcc.git] / gcc / ada / exp_pakd.adb
blobe8c607adda5f9e7a7539552c3feae736ab17bffd
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-2002 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 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. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
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;
34 with Opt; use Opt;
35 with Rtsfind; use Rtsfind;
36 with Sem; use Sem;
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;
50 package body Exp_Pakd is
52 ---------------------------
53 -- Endian Considerations --
54 ---------------------------
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:
60 -- For little-endian machines, element zero is at the right hand end
61 -- (low order end) of a bit field.
63 -- For big-endian machines, element zero is at the left hand end
64 -- (high order end) of a bit field.
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.
76 ----------------------------------------------
77 -- Entity Tables for Packed Access Routines --
78 ----------------------------------------------
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.
84 type E_Array is array (Int range 01 .. 63) of RE_Id;
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.
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);
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.
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);
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.
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);
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.
301 Set_Id : E_Array :=
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);
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.
371 SetU_Id : E_Array :=
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);
436 -----------------------
437 -- Local Subprograms --
438 -----------------------
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.
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.
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.
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
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
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.
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.
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).
505 -- On entry:
507 -- N is the N_Indexed_Component node for the packed array reference
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).
513 -- Obj is the object which is to be indexed. It is always of type Atyp.
515 -- On return:
517 -- Obj is the object containing the desired bit field. It is of type
518 -- Unsigned or Long_Long_Unsigned, and is either the entire value,
519 -- for the small static case, or the proper selected byte from the
520 -- array in the large or dynamic case. This node is analyzed and
521 -- resolved on return.
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.
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).
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.
535 ------------------------------
536 -- Compute_Linear_Subcsript --
537 ------------------------------
539 procedure Compute_Linear_Subscript
540 (Atyp : Entity_Id;
541 N : Node_Id;
542 Subscr : out Node_Id)
544 Loc : constant Source_Ptr := Sloc (N);
545 Oldsub : Node_Id;
546 Newsub : Node_Id;
547 Indx : Node_Id;
548 Styp : Entity_Id;
550 begin
551 Subscr := Empty;
553 -- Loop through dimensions
555 Indx := First_Index (Atyp);
556 Oldsub := First (Expressions (N));
558 while Present (Indx) loop
559 Styp := Etype (Indx);
560 Newsub := Relocate_Node (Oldsub);
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.
567 if Do_Range_Check (Newsub)
568 and then Etype (Newsub) /= Styp
569 then
570 Newsub := Convert_To (Styp, Newsub);
571 end if;
573 -- Now evolve the expression for the subscript. First convert
574 -- the subscript to be zero based and of an integer type.
576 -- Case of integer type, where we just subtract to get lower bound
578 if Is_Integer_Type (Styp) then
580 -- If length of integer type is smaller than standard integer,
581 -- then we convert to integer first, then do the subtract
583 -- Integer (subscript) - Integer (Styp'First)
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,
592 Prefix => New_Occurrence_Of (Styp, Loc),
593 Attribute_Name => Name_First)));
595 -- For larger integer types, subtract first, then convert to
596 -- integer, this deals with strange long long integer bounds.
598 -- Integer (subscript - Styp'First)
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,
607 Prefix => New_Occurrence_Of (Styp, Loc),
608 Attribute_Name => Name_First)));
609 end if;
611 -- For the enumeration case, we have to use 'Pos to get the value
612 -- to work with before subtracting the lower bound.
614 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
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 ???
619 else
620 pragma Assert (Is_Enumeration_Type (Styp));
622 Newsub :=
623 Make_Op_Subtract (Loc,
624 Left_Opnd => Convert_To (Standard_Integer,
625 Make_Attribute_Reference (Loc,
626 Prefix => New_Occurrence_Of (Styp, Loc),
627 Attribute_Name => Name_Pos,
628 Expressions => New_List (Newsub))),
630 Right_Opnd =>
631 Convert_To (Standard_Integer,
632 Make_Attribute_Reference (Loc,
633 Prefix => New_Occurrence_Of (Styp, Loc),
634 Attribute_Name => Name_Pos,
635 Expressions => New_List (
636 Make_Attribute_Reference (Loc,
637 Prefix => New_Occurrence_Of (Styp, Loc),
638 Attribute_Name => Name_First)))));
639 end if;
641 Set_Paren_Count (Newsub, 1);
643 -- For the first subscript, we just copy that subscript value
645 if No (Subscr) then
646 Subscr := Newsub;
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.
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;
664 -- Move to next subscript
666 Next_Index (Indx);
667 Next (Oldsub);
668 end loop;
669 end Compute_Linear_Subscript;
671 -------------------------
672 -- Convert_To_PAT_Type --
673 -------------------------
675 -- The PAT is always obtained from the actual subtype
677 procedure Convert_To_PAT_Type (Aexp : Entity_Id) is
678 Act_ST : Entity_Id;
680 begin
681 Convert_To_Actual_Subtype (Aexp);
682 Act_ST := Underlying_Type (Etype (Aexp));
683 Create_Packed_Array_Type (Act_ST);
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.
689 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
690 end Convert_To_PAT_Type;
692 ------------------------------
693 -- Create_Packed_Array_Type --
694 ------------------------------
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);
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;
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.
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).
722 -----------------
723 -- Install_PAT --
724 -----------------
726 procedure Install_PAT is
727 Pushed_Scope : Boolean := False;
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).
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.
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.
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;
756 if Scope (Typ) /= Current_Scope then
757 New_Scope (Scope (Typ));
758 Pushed_Scope := True;
759 end if;
761 Set_Is_Itype (PAT, True);
762 Set_Packed_Array_Type (Typ, PAT);
763 Analyze (Decl, Suppress => All_Checks);
765 if Pushed_Scope then
766 Pop_Scope;
767 end if;
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
771 -- of a modular type
773 Set_Esize (PAT, Esiz);
775 if Unknown_RM_Size (PAT) then
776 Set_RM_Size (PAT, Esiz);
777 end if;
779 -- Set remaining fields of packed array type
781 Init_Alignment (PAT);
782 Set_Parent (PAT, Empty);
783 Set_Associated_Node_For_Itype (PAT, Typ);
784 Set_Is_Packed_Array_Type (PAT, True);
785 Set_Original_Array_Type (PAT, Typ);
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.
792 Set_Has_Delayed_Freeze (PAT, False);
793 Set_Has_Delayed_Freeze (Etype (PAT), False);
794 end Install_PAT;
796 -----------------
797 -- Set_PB_Type --
798 -----------------
800 procedure Set_PB_Type is
801 begin
802 -- If the user has specified an explicit alignment for the
803 -- type or component, take it into account.
805 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
806 or else Alignment (Typ) = 1
807 or else Component_Alignment (Typ) = Calign_Storage_Unit
808 then
809 PB_Type := RTE (RE_Packed_Bytes1);
811 elsif Csize mod 4 /= 0
812 or else Alignment (Typ) = 2
813 then
814 PB_Type := RTE (RE_Packed_Bytes2);
816 else
817 PB_Type := RTE (RE_Packed_Bytes4);
818 end if;
819 end Set_PB_Type;
821 -- Start of processing for Create_Packed_Array_Type
823 begin
824 -- If we already have a packed array type, nothing to do
826 if Present (Packed_Array_Type (Typ)) then
827 return;
828 end if;
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.
834 if Ekind (Typ) = E_Array_Subtype then
835 Ancest := Ancestor_Subtype (Typ);
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;
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.
850 Esiz := Esize (Typ);
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.
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.
863 if not Is_Bit_Packed_Array (Typ) then
865 -- Here we build a declaration:
867 -- type tttP is array (index1, index2, ...) of component_type
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.
874 -- For the unconstrained array case, we use
876 -- Natural range <>
878 -- For the constrained case, we use
880 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
881 -- Enum_Type'Pos (Enum_Type'Last);
883 PAT :=
884 Make_Defining_Identifier (Loc,
885 Chars => New_External_Name (Chars (Typ), 'P'));
887 Set_Packed_Array_Type (Typ, PAT);
889 declare
890 Indexes : List_Id := New_List;
891 Indx : Node_Id;
892 Indx_Typ : Entity_Id;
893 Enum_Case : Boolean;
894 Typedef : Node_Id;
896 begin
897 Indx := First_Index (Typ);
899 while Present (Indx) loop
900 Indx_Typ := Etype (Indx);
902 Enum_Case := Is_Enumeration_Type (Indx_Typ)
903 and then Has_Non_Standard_Rep (Indx_Typ);
905 -- Unconstrained case
907 if not Is_Constrained (Typ) then
908 if Enum_Case then
909 Indx_Typ := Standard_Natural;
910 end if;
912 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
914 -- Constrained case
916 else
917 if not Enum_Case then
918 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
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))),
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)))))));
951 end if;
952 end if;
954 Next_Index (Indx);
955 end loop;
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));
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;
972 Decl :=
973 Make_Full_Type_Declaration (Loc,
974 Defining_Identifier => PAT,
975 Type_Definition => Typedef);
976 end;
978 -- Set type as packed array type and install it
980 Set_Is_Packed_Array_Type (PAT);
981 Install_PAT;
982 return;
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.
987 elsif not Is_Constrained (Typ) then
988 PAT :=
989 Make_Defining_Identifier (Loc,
990 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
992 Set_Packed_Array_Type (Typ, PAT);
993 Set_PB_Type;
995 Decl :=
996 Make_Subtype_Declaration (Loc,
997 Defining_Identifier => PAT,
998 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
999 Install_PAT;
1000 return;
1002 -- Remaining code is for the case of bit-packing for constrained array
1004 -- The name of the packed array subtype is
1006 -- ttt___Xsss
1008 -- where sss is the component size in bits and ttt is the name of
1009 -- the parent packed type.
1011 else
1012 PAT :=
1013 Make_Defining_Identifier (Loc,
1014 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1016 Set_Packed_Array_Type (Typ, PAT);
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
1021 declare
1022 J : Nat := 1;
1024 begin
1025 Len_Expr := Empty; -- suppress junk warning
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)));
1035 if J = 1 then
1036 Len_Expr := Len_Dim;
1038 else
1039 Len_Expr :=
1040 Make_Op_Multiply (Loc,
1041 Left_Opnd => Len_Expr,
1042 Right_Opnd => Len_Dim);
1043 end if;
1045 J := J + 1;
1046 exit when J > Number_Dimensions (Typ);
1047 end loop;
1048 end;
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
1052 -- total length fits in type Integer.
1054 Set_Parent (Len_Expr, Typ);
1055 Analyze_And_Resolve (Len_Expr, Standard_Integer);
1057 -- Use a modular type if possible. We can do this if we are we
1058 -- have static bounds, and the length is small enough, and the
1059 -- length is not zero. We exclude the zero length case because the
1060 -- size of things is always at least one, and the zero length object
1061 -- would have an anomous size
1063 if Compile_Time_Known_Value (Len_Expr) then
1064 Len_Bits := Expr_Value (Len_Expr) * Csize;
1066 -- We normally consider small enough to mean no larger than the
1067 -- value of System_Max_Binary_Modulus_Power, except that in
1068 -- No_Run_Time mode, we use the Word Size on machines for
1069 -- which double length shifts are not generated in line.
1071 if Len_Bits > 0
1072 and then
1073 (Len_Bits <= System_Word_Size
1074 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1075 and then (not No_Run_Time
1076 or else
1077 Long_Shifts_Inlined_On_Target)))
1078 then
1079 -- We can use the modular type, it has the form:
1081 -- subtype tttPn is btyp
1082 -- range 0 .. 2 ** (Esize (Typ) * Csize) - 1;
1084 -- Here Siz is 1, 2 or 4, as computed above, and btyp is either
1085 -- Unsigned or Long_Long_Unsigned depending on the length.
1087 if Len_Bits <= Standard_Integer_Size then
1088 Btyp := RTE (RE_Unsigned);
1089 else
1090 Btyp := RTE (RE_Long_Long_Unsigned);
1091 end if;
1093 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1094 Set_Print_In_Hex (Lit);
1096 Decl :=
1097 Make_Subtype_Declaration (Loc,
1098 Defining_Identifier => PAT,
1099 Subtype_Indication =>
1100 Make_Subtype_Indication (Loc,
1101 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1103 Constraint =>
1104 Make_Range_Constraint (Loc,
1105 Range_Expression =>
1106 Make_Range (Loc,
1107 Low_Bound =>
1108 Make_Integer_Literal (Loc, 0),
1109 High_Bound => Lit))));
1111 if Esiz = Uint_0 then
1112 Esiz := Len_Bits;
1113 end if;
1115 Install_PAT;
1116 return;
1117 end if;
1118 end if;
1120 -- Could not use a modular type, for all other cases, we build
1121 -- a packed array subtype:
1123 -- subtype tttPn is
1124 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1126 -- Bits is the length of the array in bits.
1128 Set_PB_Type;
1130 Bits_U1 :=
1131 Make_Op_Add (Loc,
1132 Left_Opnd =>
1133 Make_Op_Multiply (Loc,
1134 Left_Opnd =>
1135 Make_Integer_Literal (Loc, Csize),
1136 Right_Opnd => Len_Expr),
1138 Right_Opnd =>
1139 Make_Integer_Literal (Loc, 7));
1141 Set_Paren_Count (Bits_U1, 1);
1143 PAT_High :=
1144 Make_Op_Subtract (Loc,
1145 Left_Opnd =>
1146 Make_Op_Divide (Loc,
1147 Left_Opnd => Bits_U1,
1148 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1149 Right_Opnd => Make_Integer_Literal (Loc, 1));
1151 Decl :=
1152 Make_Subtype_Declaration (Loc,
1153 Defining_Identifier => PAT,
1154 Subtype_Indication =>
1155 Make_Subtype_Indication (Loc,
1156 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1157 Constraint =>
1159 Make_Index_Or_Discriminant_Constraint (Loc,
1160 Constraints => New_List (
1161 Make_Range (Loc,
1162 Low_Bound =>
1163 Make_Integer_Literal (Loc, 0),
1164 High_Bound => PAT_High)))));
1166 Install_PAT;
1167 end if;
1168 end Create_Packed_Array_Type;
1170 -----------------------------------
1171 -- Expand_Bit_Packed_Element_Set --
1172 -----------------------------------
1174 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1175 Loc : constant Source_Ptr := Sloc (N);
1176 Lhs : constant Node_Id := Name (N);
1178 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1179 -- Used to preserve assignment OK status when assignment is rewritten
1181 Rhs : Node_Id := Expression (N);
1182 -- Initially Rhs is the right hand side value, it will be replaced
1183 -- later by an appropriate unchecked conversion for the assignment.
1185 Obj : Node_Id;
1186 Atyp : Entity_Id;
1187 PAT : Entity_Id;
1188 Ctyp : Entity_Id;
1189 Csiz : Int;
1190 Shift : Node_Id;
1191 Cmask : Uint;
1193 New_Lhs : Node_Id;
1194 New_Rhs : Node_Id;
1196 Rhs_Val_Known : Boolean;
1197 Rhs_Val : Uint;
1198 -- If the value of the right hand side as an integer constant is
1199 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1200 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1201 -- the Rhs_Val is undefined.
1203 begin
1204 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1206 Obj := Relocate_Node (Prefix (Lhs));
1207 Convert_To_Actual_Subtype (Obj);
1208 Atyp := Etype (Obj);
1209 PAT := Packed_Array_Type (Atyp);
1210 Ctyp := Component_Type (Atyp);
1211 Csiz := UI_To_Int (Component_Size (Atyp));
1213 -- We convert the right hand side to the proper subtype to ensure
1214 -- that an appropriate range check is made (since the normal range
1215 -- check from assignment will be lost in the transformations). This
1216 -- conversion is analyzed immediately so that subsequent processing
1217 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1219 Rhs := Convert_To (Ctyp, Rhs);
1220 Set_Parent (Rhs, N);
1221 Analyze_And_Resolve (Rhs, Ctyp);
1223 -- Case of component size 1,2,4 or any component size for the modular
1224 -- case. These are the cases for which we can inline the code.
1226 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1227 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1228 then
1229 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1231 -- The statement to be generated is:
1233 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1235 -- where mask1 is obtained by shifting Cmask left Shift bits
1236 -- and then complementing the result.
1238 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1240 -- the "or ..." is omitted if rhs is constant and all 0 bits
1242 -- rhs is converted to the appropriate type.
1244 -- The result is converted back to the array type, since
1245 -- otherwise we lose knowledge of the packed nature.
1247 -- Determine if right side is all 0 bits or all 1 bits
1249 if Compile_Time_Known_Value (Rhs) then
1250 Rhs_Val := Expr_Rep_Value (Rhs);
1251 Rhs_Val_Known := True;
1253 -- The following test catches the case of an unchecked conversion
1254 -- of an integer literal. This results from optimizing aggregates
1255 -- of packed types.
1257 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1258 and then Compile_Time_Known_Value (Expression (Rhs))
1259 then
1260 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1261 Rhs_Val_Known := True;
1263 else
1264 Rhs_Val := No_Uint;
1265 Rhs_Val_Known := False;
1266 end if;
1268 -- Some special checks for the case where the right hand value
1269 -- is known at compile time. Basically we have to take care of
1270 -- the implicit conversion to the subtype of the component object.
1272 if Rhs_Val_Known then
1274 -- If we have a biased component type then we must manually do
1275 -- the biasing, since we are taking responsibility in this case
1276 -- for constructing the exact bit pattern to be used.
1278 if Has_Biased_Representation (Ctyp) then
1279 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1280 end if;
1282 -- For a negative value, we manually convert the twos complement
1283 -- value to a corresponding unsigned value, so that the proper
1284 -- field width is maintained. If we did not do this, we would
1285 -- get too many leading sign bits later on.
1287 if Rhs_Val < 0 then
1288 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1289 end if;
1290 end if;
1292 New_Lhs := Duplicate_Subexpr (Obj, True);
1293 New_Rhs := Duplicate_Subexpr (Obj);
1295 -- First we deal with the "and"
1297 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1298 declare
1299 Mask1 : Node_Id;
1300 Lit : Node_Id;
1302 begin
1303 if Compile_Time_Known_Value (Shift) then
1304 Mask1 :=
1305 Make_Integer_Literal (Loc,
1306 Modulus (Etype (Obj)) - 1 -
1307 (Cmask * (2 ** Expr_Value (Shift))));
1308 Set_Print_In_Hex (Mask1);
1310 else
1311 Lit := Make_Integer_Literal (Loc, Cmask);
1312 Set_Print_In_Hex (Lit);
1313 Mask1 :=
1314 Make_Op_Not (Loc,
1315 Right_Opnd => Make_Shift_Left (Lit, Shift));
1316 end if;
1318 New_Rhs :=
1319 Make_Op_And (Loc,
1320 Left_Opnd => New_Rhs,
1321 Right_Opnd => Mask1);
1322 end;
1323 end if;
1325 -- Then deal with the "or"
1327 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1328 declare
1329 Or_Rhs : Node_Id;
1331 procedure Fixup_Rhs;
1332 -- Adjust Rhs by bias if biased representation for components
1333 -- or remove extraneous high order sign bits if signed.
1335 procedure Fixup_Rhs is
1336 Etyp : constant Entity_Id := Etype (Rhs);
1338 begin
1339 -- For biased case, do the required biasing by simply
1340 -- converting to the biased subtype (the conversion
1341 -- will generate the required bias).
1343 if Has_Biased_Representation (Ctyp) then
1344 Rhs := Convert_To (Ctyp, Rhs);
1346 -- For a signed integer type that is not biased, generate
1347 -- a conversion to unsigned to strip high order sign bits.
1349 elsif Is_Signed_Integer_Type (Ctyp) then
1350 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1351 end if;
1353 -- Set Etype, since it can be referenced before the
1354 -- node is completely analyzed.
1356 Set_Etype (Rhs, Etyp);
1358 -- We now need to do an unchecked conversion of the
1359 -- result to the target type, but it is important that
1360 -- this conversion be a right justified conversion and
1361 -- not a left justified conversion.
1363 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1365 end Fixup_Rhs;
1367 begin
1368 if Rhs_Val_Known
1369 and then Compile_Time_Known_Value (Shift)
1370 then
1371 Or_Rhs :=
1372 Make_Integer_Literal (Loc,
1373 Rhs_Val * (2 ** Expr_Value (Shift)));
1374 Set_Print_In_Hex (Or_Rhs);
1376 else
1377 -- We have to convert the right hand side to Etype (Obj).
1378 -- A special case case arises if what we have now is a Val
1379 -- attribute reference whose expression type is Etype (Obj).
1380 -- This happens for assignments of fields from the same
1381 -- array. In this case we get the required right hand side
1382 -- by simply removing the inner attribute reference.
1384 if Nkind (Rhs) = N_Attribute_Reference
1385 and then Attribute_Name (Rhs) = Name_Val
1386 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1387 then
1388 Rhs := Relocate_Node (First (Expressions (Rhs)));
1389 Fixup_Rhs;
1391 -- If the value of the right hand side is a known integer
1392 -- value, then just replace it by an untyped constant,
1393 -- which will be properly retyped when we analyze and
1394 -- resolve the expression.
1396 elsif Rhs_Val_Known then
1398 -- Note that Rhs_Val has already been normalized to
1399 -- be an unsigned value with the proper number of bits.
1401 Rhs :=
1402 Make_Integer_Literal (Loc, Rhs_Val);
1404 -- Otherwise we need an unchecked conversion
1406 else
1407 Fixup_Rhs;
1408 end if;
1410 Or_Rhs := Make_Shift_Left (Rhs, Shift);
1411 end if;
1413 if Nkind (New_Rhs) = N_Op_And then
1414 Set_Paren_Count (New_Rhs, 1);
1415 end if;
1417 New_Rhs :=
1418 Make_Op_Or (Loc,
1419 Left_Opnd => New_Rhs,
1420 Right_Opnd => Or_Rhs);
1421 end;
1422 end if;
1424 -- Now do the rewrite
1426 Rewrite (N,
1427 Make_Assignment_Statement (Loc,
1428 Name => New_Lhs,
1429 Expression =>
1430 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1431 Set_Assignment_OK (Name (N), Ass_OK);
1433 -- All other component sizes for non-modular case
1435 else
1436 -- We generate
1438 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1440 -- where Subscr is the computed linear subscript.
1442 declare
1443 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1444 Set_nn : Entity_Id;
1445 Subscr : Node_Id;
1446 Atyp : Entity_Id;
1448 begin
1449 -- Acquire proper Set entity. We use the aligned or unaligned
1450 -- case as appropriate.
1452 if Known_Aligned_Enough (Obj, Csiz) then
1453 Set_nn := RTE (Set_Id (Csiz));
1454 else
1455 Set_nn := RTE (SetU_Id (Csiz));
1456 end if;
1458 -- Now generate the set reference
1460 Obj := Relocate_Node (Prefix (Lhs));
1461 Convert_To_Actual_Subtype (Obj);
1462 Atyp := Etype (Obj);
1463 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1465 Rewrite (N,
1466 Make_Procedure_Call_Statement (Loc,
1467 Name => New_Occurrence_Of (Set_nn, Loc),
1468 Parameter_Associations => New_List (
1469 Make_Byte_Aligned_Attribute_Reference (Loc,
1470 Attribute_Name => Name_Address,
1471 Prefix => Obj),
1472 Subscr,
1473 Unchecked_Convert_To (Bits_nn,
1474 Convert_To (Ctyp, Rhs)))));
1476 end;
1477 end if;
1479 Analyze (N, Suppress => All_Checks);
1480 end Expand_Bit_Packed_Element_Set;
1482 -------------------------------------
1483 -- Expand_Packed_Address_Reference --
1484 -------------------------------------
1486 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1487 Loc : constant Source_Ptr := Sloc (N);
1488 Ploc : Source_Ptr;
1489 Pref : Node_Id;
1490 Expr : Node_Id;
1491 Term : Node_Id;
1492 Atyp : Entity_Id;
1493 Subscr : Node_Id;
1495 begin
1496 Pref := Prefix (N);
1497 Expr := Empty;
1499 -- We build up an expression serially that has the form
1501 -- outer_object'Address
1502 -- + (linear-subscript * component_size for each array reference
1503 -- + field'Bit_Position for each record field
1504 -- + ...
1505 -- + ...) / Storage_Unit;
1507 -- Some additional conversions are required to deal with the addition
1508 -- operation, which is not normally visible to generated code.
1510 loop
1511 Ploc := Sloc (Pref);
1513 if Nkind (Pref) = N_Indexed_Component then
1514 Convert_To_Actual_Subtype (Prefix (Pref));
1515 Atyp := Etype (Prefix (Pref));
1516 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1518 Term :=
1519 Make_Op_Multiply (Ploc,
1520 Left_Opnd => Subscr,
1521 Right_Opnd =>
1522 Make_Attribute_Reference (Ploc,
1523 Prefix => New_Occurrence_Of (Atyp, Ploc),
1524 Attribute_Name => Name_Component_Size));
1526 elsif Nkind (Pref) = N_Selected_Component then
1527 Term :=
1528 Make_Attribute_Reference (Ploc,
1529 Prefix => Selector_Name (Pref),
1530 Attribute_Name => Name_Bit_Position);
1532 else
1533 exit;
1534 end if;
1536 Term := Convert_To (RTE (RE_Integer_Address), Term);
1538 if No (Expr) then
1539 Expr := Term;
1541 else
1542 Expr :=
1543 Make_Op_Add (Ploc,
1544 Left_Opnd => Expr,
1545 Right_Opnd => Term);
1546 end if;
1548 Pref := Prefix (Pref);
1549 end loop;
1551 Rewrite (N,
1552 Unchecked_Convert_To (RTE (RE_Address),
1553 Make_Op_Add (Loc,
1554 Left_Opnd =>
1555 Unchecked_Convert_To (RTE (RE_Integer_Address),
1556 Make_Attribute_Reference (Loc,
1557 Prefix => Pref,
1558 Attribute_Name => Name_Address)),
1560 Right_Opnd =>
1561 Make_Op_Divide (Loc,
1562 Left_Opnd => Expr,
1563 Right_Opnd =>
1564 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1566 Analyze_And_Resolve (N, RTE (RE_Address));
1567 end Expand_Packed_Address_Reference;
1569 ------------------------------------
1570 -- Expand_Packed_Boolean_Operator --
1571 ------------------------------------
1573 -- This routine expands "a op b" for the packed cases
1575 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1576 Loc : constant Source_Ptr := Sloc (N);
1577 Typ : constant Entity_Id := Etype (N);
1578 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1579 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1581 Ltyp : Entity_Id;
1582 Rtyp : Entity_Id;
1583 PAT : Entity_Id;
1585 begin
1586 Convert_To_Actual_Subtype (L);
1587 Convert_To_Actual_Subtype (R);
1589 Ensure_Defined (Etype (L), N);
1590 Ensure_Defined (Etype (R), N);
1592 Apply_Length_Check (R, Etype (L));
1594 Ltyp := Etype (L);
1595 Rtyp := Etype (R);
1597 -- First an odd and silly test. We explicitly check for the XOR
1598 -- case where the component type is True .. True, since this will
1599 -- raise constraint error. A special check is required since CE
1600 -- will not be required other wise (cf Expand_Packed_Not).
1602 -- No such check is required for AND and OR, since for both these
1603 -- cases False op False = False, and True op True = True.
1605 if Nkind (N) = N_Op_Xor then
1606 declare
1607 CT : constant Entity_Id := Component_Type (Rtyp);
1608 BT : constant Entity_Id := Base_Type (CT);
1610 begin
1611 Insert_Action (N,
1612 Make_Raise_Constraint_Error (Loc,
1613 Condition =>
1614 Make_Op_And (Loc,
1615 Left_Opnd =>
1616 Make_Op_Eq (Loc,
1617 Left_Opnd =>
1618 Make_Attribute_Reference (Loc,
1619 Prefix => New_Occurrence_Of (CT, Loc),
1620 Attribute_Name => Name_First),
1622 Right_Opnd =>
1623 Convert_To (BT,
1624 New_Occurrence_Of (Standard_True, Loc))),
1626 Right_Opnd =>
1627 Make_Op_Eq (Loc,
1628 Left_Opnd =>
1629 Make_Attribute_Reference (Loc,
1630 Prefix => New_Occurrence_Of (CT, Loc),
1631 Attribute_Name => Name_Last),
1633 Right_Opnd =>
1634 Convert_To (BT,
1635 New_Occurrence_Of (Standard_True, Loc)))),
1636 Reason => CE_Range_Check_Failed));
1637 end;
1638 end if;
1640 -- Now that that silliness is taken care of, get packed array type
1642 Convert_To_PAT_Type (L);
1643 Convert_To_PAT_Type (R);
1645 PAT := Etype (L);
1647 -- For the modular case, we expand a op b into
1649 -- rtyp!(pat!(a) op pat!(b))
1651 -- where rtyp is the Etype of the left operand. Note that we do not
1652 -- convert to the base type, since this would be unconstrained, and
1653 -- hence not have a corresponding packed array type set.
1655 if Is_Modular_Integer_Type (PAT) then
1656 declare
1657 P : Node_Id;
1659 begin
1660 if Nkind (N) = N_Op_And then
1661 P := Make_Op_And (Loc, L, R);
1663 elsif Nkind (N) = N_Op_Or then
1664 P := Make_Op_Or (Loc, L, R);
1666 else -- Nkind (N) = N_Op_Xor
1667 P := Make_Op_Xor (Loc, L, R);
1668 end if;
1670 Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1671 end;
1673 -- For the array case, we insert the actions
1675 -- Result : Ltype;
1677 -- System.Bitops.Bit_And/Or/Xor
1678 -- (Left'Address,
1679 -- Ltype'Length * Ltype'Component_Size;
1680 -- Right'Address,
1681 -- Rtype'Length * Rtype'Component_Size
1682 -- Result'Address);
1684 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1685 -- the second argument and fourth arguments are the lengths of the
1686 -- operands in bits. Then we replace the expression by a reference
1687 -- to Result.
1689 else
1690 declare
1691 Result_Ent : constant Entity_Id :=
1692 Make_Defining_Identifier (Loc,
1693 Chars => New_Internal_Name ('T'));
1695 E_Id : RE_Id;
1697 begin
1698 if Nkind (N) = N_Op_And then
1699 E_Id := RE_Bit_And;
1701 elsif Nkind (N) = N_Op_Or then
1702 E_Id := RE_Bit_Or;
1704 else -- Nkind (N) = N_Op_Xor
1705 E_Id := RE_Bit_Xor;
1706 end if;
1708 Insert_Actions (N, New_List (
1710 Make_Object_Declaration (Loc,
1711 Defining_Identifier => Result_Ent,
1712 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1714 Make_Procedure_Call_Statement (Loc,
1715 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1716 Parameter_Associations => New_List (
1718 Make_Byte_Aligned_Attribute_Reference (Loc,
1719 Attribute_Name => Name_Address,
1720 Prefix => L),
1722 Make_Op_Multiply (Loc,
1723 Left_Opnd =>
1724 Make_Attribute_Reference (Loc,
1725 Prefix =>
1726 New_Occurrence_Of
1727 (Etype (First_Index (Ltyp)), Loc),
1728 Attribute_Name => Name_Range_Length),
1729 Right_Opnd =>
1730 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1732 Make_Byte_Aligned_Attribute_Reference (Loc,
1733 Attribute_Name => Name_Address,
1734 Prefix => R),
1736 Make_Op_Multiply (Loc,
1737 Left_Opnd =>
1738 Make_Attribute_Reference (Loc,
1739 Prefix =>
1740 New_Occurrence_Of
1741 (Etype (First_Index (Rtyp)), Loc),
1742 Attribute_Name => Name_Range_Length),
1743 Right_Opnd =>
1744 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1746 Make_Byte_Aligned_Attribute_Reference (Loc,
1747 Attribute_Name => Name_Address,
1748 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1750 Rewrite (N,
1751 New_Occurrence_Of (Result_Ent, Loc));
1752 end;
1753 end if;
1755 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1756 end Expand_Packed_Boolean_Operator;
1758 -------------------------------------
1759 -- Expand_Packed_Element_Reference --
1760 -------------------------------------
1762 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1763 Loc : constant Source_Ptr := Sloc (N);
1764 Obj : Node_Id;
1765 Atyp : Entity_Id;
1766 PAT : Entity_Id;
1767 Ctyp : Entity_Id;
1768 Csiz : Int;
1769 Shift : Node_Id;
1770 Cmask : Uint;
1771 Lit : Node_Id;
1772 Arg : Node_Id;
1774 begin
1775 -- If not bit packed, we have the enumeration case, which is easily
1776 -- dealt with (just adjust the subscripts of the indexed component)
1778 -- Note: this leaves the result as an indexed component, which is
1779 -- still a variable, so can be used in the assignment case, as is
1780 -- required in the enumeration case.
1782 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1783 Setup_Enumeration_Packed_Array_Reference (N);
1784 return;
1785 end if;
1787 -- Remaining processing is for the bit-packed case.
1789 Obj := Relocate_Node (Prefix (N));
1790 Convert_To_Actual_Subtype (Obj);
1791 Atyp := Etype (Obj);
1792 PAT := Packed_Array_Type (Atyp);
1793 Ctyp := Component_Type (Atyp);
1794 Csiz := UI_To_Int (Component_Size (Atyp));
1796 -- Case of component size 1,2,4 or any component size for the modular
1797 -- case. These are the cases for which we can inline the code.
1799 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1800 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1801 then
1802 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1803 Lit := Make_Integer_Literal (Loc, Cmask);
1804 Set_Print_In_Hex (Lit);
1806 -- We generate a shift right to position the field, followed by a
1807 -- masking operation to extract the bit field, and we finally do an
1808 -- unchecked conversion to convert the result to the required target.
1810 -- Note that the unchecked conversion automatically deals with the
1811 -- bias if we are dealing with a biased representation. What will
1812 -- happen is that we temporarily generate the biased representation,
1813 -- but almost immediately that will be converted to the original
1814 -- unbiased component type, and the bias will disappear.
1816 Arg :=
1817 Make_Op_And (Loc,
1818 Left_Opnd => Make_Shift_Right (Obj, Shift),
1819 Right_Opnd => Lit);
1821 Analyze_And_Resolve (Arg);
1823 Rewrite (N,
1824 RJ_Unchecked_Convert_To (Ctyp, Arg));
1826 -- All other component sizes for non-modular case
1828 else
1829 -- We generate
1831 -- Component_Type!(Get_nn (Arr'address, Subscr))
1833 -- where Subscr is the computed linear subscript.
1835 declare
1836 Get_nn : Entity_Id;
1837 Subscr : Node_Id;
1839 begin
1840 -- Acquire proper Get entity. We use the aligned or unaligned
1841 -- case as appropriate.
1843 if Known_Aligned_Enough (Obj, Csiz) then
1844 Get_nn := RTE (Get_Id (Csiz));
1845 else
1846 Get_nn := RTE (GetU_Id (Csiz));
1847 end if;
1849 -- Now generate the get reference
1851 Compute_Linear_Subscript (Atyp, N, Subscr);
1853 Rewrite (N,
1854 Unchecked_Convert_To (Ctyp,
1855 Make_Function_Call (Loc,
1856 Name => New_Occurrence_Of (Get_nn, Loc),
1857 Parameter_Associations => New_List (
1858 Make_Byte_Aligned_Attribute_Reference (Loc,
1859 Attribute_Name => Name_Address,
1860 Prefix => Obj),
1861 Subscr))));
1862 end;
1863 end if;
1865 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
1867 end Expand_Packed_Element_Reference;
1869 ----------------------
1870 -- Expand_Packed_Eq --
1871 ----------------------
1873 -- Handles expansion of "=" on packed array types
1875 procedure Expand_Packed_Eq (N : Node_Id) is
1876 Loc : constant Source_Ptr := Sloc (N);
1877 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1878 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1880 LLexpr : Node_Id;
1881 RLexpr : Node_Id;
1883 Ltyp : Entity_Id;
1884 Rtyp : Entity_Id;
1885 PAT : Entity_Id;
1887 begin
1888 Convert_To_Actual_Subtype (L);
1889 Convert_To_Actual_Subtype (R);
1890 Ltyp := Underlying_Type (Etype (L));
1891 Rtyp := Underlying_Type (Etype (R));
1893 Convert_To_PAT_Type (L);
1894 Convert_To_PAT_Type (R);
1895 PAT := Etype (L);
1897 LLexpr :=
1898 Make_Op_Multiply (Loc,
1899 Left_Opnd =>
1900 Make_Attribute_Reference (Loc,
1901 Attribute_Name => Name_Length,
1902 Prefix => New_Occurrence_Of (Ltyp, Loc)),
1903 Right_Opnd =>
1904 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
1906 RLexpr :=
1907 Make_Op_Multiply (Loc,
1908 Left_Opnd =>
1909 Make_Attribute_Reference (Loc,
1910 Attribute_Name => Name_Length,
1911 Prefix => New_Occurrence_Of (Rtyp, Loc)),
1912 Right_Opnd =>
1913 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
1915 -- For the modular case, we transform the comparison to:
1917 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
1919 -- where PAT is the packed array type. This works fine, since in the
1920 -- modular case we guarantee that the unused bits are always zeroes.
1921 -- We do have to compare the lengths because we could be comparing
1922 -- two different subtypes of the same base type.
1924 if Is_Modular_Integer_Type (PAT) then
1925 Rewrite (N,
1926 Make_And_Then (Loc,
1927 Left_Opnd =>
1928 Make_Op_Eq (Loc,
1929 Left_Opnd => LLexpr,
1930 Right_Opnd => RLexpr),
1932 Right_Opnd =>
1933 Make_Op_Eq (Loc,
1934 Left_Opnd => L,
1935 Right_Opnd => R)));
1937 -- For the non-modular case, we call a runtime routine
1939 -- System.Bit_Ops.Bit_Eq
1940 -- (L'Address, L_Length, R'Address, R_Length)
1942 -- where PAT is the packed array type, and the lengths are the lengths
1943 -- in bits of the original packed arrays. This routine takes care of
1944 -- not comparing the unused bits in the last byte.
1946 else
1947 Rewrite (N,
1948 Make_Function_Call (Loc,
1949 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
1950 Parameter_Associations => New_List (
1951 Make_Byte_Aligned_Attribute_Reference (Loc,
1952 Attribute_Name => Name_Address,
1953 Prefix => L),
1955 LLexpr,
1957 Make_Byte_Aligned_Attribute_Reference (Loc,
1958 Attribute_Name => Name_Address,
1959 Prefix => R),
1961 RLexpr)));
1962 end if;
1964 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
1965 end Expand_Packed_Eq;
1967 -----------------------
1968 -- Expand_Packed_Not --
1969 -----------------------
1971 -- Handles expansion of "not" on packed array types
1973 procedure Expand_Packed_Not (N : Node_Id) is
1974 Loc : constant Source_Ptr := Sloc (N);
1975 Typ : constant Entity_Id := Etype (N);
1976 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
1978 Rtyp : Entity_Id;
1979 PAT : Entity_Id;
1980 Lit : Node_Id;
1982 begin
1983 Convert_To_Actual_Subtype (Opnd);
1984 Rtyp := Etype (Opnd);
1986 -- First an odd and silly test. We explicitly check for the case
1987 -- where the 'First of the component type is equal to the 'Last of
1988 -- this component type, and if this is the case, we make sure that
1989 -- constraint error is raised. The reason is that the NOT is bound
1990 -- to cause CE in this case, and we will not otherwise catch it.
1992 -- Believe it or not, this was reported as a bug. Note that nearly
1993 -- always, the test will evaluate statically to False, so the code
1994 -- will be statically removed, and no extra overhead caused.
1996 declare
1997 CT : constant Entity_Id := Component_Type (Rtyp);
1999 begin
2000 Insert_Action (N,
2001 Make_Raise_Constraint_Error (Loc,
2002 Condition =>
2003 Make_Op_Eq (Loc,
2004 Left_Opnd =>
2005 Make_Attribute_Reference (Loc,
2006 Prefix => New_Occurrence_Of (CT, Loc),
2007 Attribute_Name => Name_First),
2009 Right_Opnd =>
2010 Make_Attribute_Reference (Loc,
2011 Prefix => New_Occurrence_Of (CT, Loc),
2012 Attribute_Name => Name_Last)),
2013 Reason => CE_Range_Check_Failed));
2014 end;
2016 -- Now that that silliness is taken care of, get packed array type
2018 Convert_To_PAT_Type (Opnd);
2019 PAT := Etype (Opnd);
2021 -- For the case where the packed array type is a modular type,
2022 -- not A expands simply into:
2024 -- rtyp!(PAT!(A) xor mask)
2026 -- where PAT is the packed array type, and mask is a mask of all
2027 -- one bits of length equal to the size of this packed type and
2028 -- rtyp is the actual subtype of the operand
2030 Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2031 Set_Print_In_Hex (Lit);
2033 if not Is_Array_Type (PAT) then
2034 Rewrite (N,
2035 Unchecked_Convert_To (Rtyp,
2036 Make_Op_Xor (Loc,
2037 Left_Opnd => Opnd,
2038 Right_Opnd => Lit)));
2040 -- For the array case, we insert the actions
2042 -- Result : Typ;
2044 -- System.Bitops.Bit_Not
2045 -- (Opnd'Address,
2046 -- Typ'Length * Typ'Component_Size;
2047 -- Result'Address);
2049 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2050 -- argument is the length of the operand in bits. Then we replace
2051 -- the expression by a reference to Result.
2053 else
2054 declare
2055 Result_Ent : constant Entity_Id :=
2056 Make_Defining_Identifier (Loc,
2057 Chars => New_Internal_Name ('T'));
2059 begin
2060 Insert_Actions (N, New_List (
2062 Make_Object_Declaration (Loc,
2063 Defining_Identifier => Result_Ent,
2064 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2066 Make_Procedure_Call_Statement (Loc,
2067 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2068 Parameter_Associations => New_List (
2070 Make_Byte_Aligned_Attribute_Reference (Loc,
2071 Attribute_Name => Name_Address,
2072 Prefix => Opnd),
2074 Make_Op_Multiply (Loc,
2075 Left_Opnd =>
2076 Make_Attribute_Reference (Loc,
2077 Prefix =>
2078 New_Occurrence_Of
2079 (Etype (First_Index (Rtyp)), Loc),
2080 Attribute_Name => Name_Range_Length),
2081 Right_Opnd =>
2082 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2084 Make_Byte_Aligned_Attribute_Reference (Loc,
2085 Attribute_Name => Name_Address,
2086 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2088 Rewrite (N,
2089 New_Occurrence_Of (Result_Ent, Loc));
2090 end;
2091 end if;
2093 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2095 end Expand_Packed_Not;
2097 -------------------------------------
2098 -- Involves_Packed_Array_Reference --
2099 -------------------------------------
2101 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2102 begin
2103 if Nkind (N) = N_Indexed_Component
2104 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2105 then
2106 return True;
2108 elsif Nkind (N) = N_Selected_Component then
2109 return Involves_Packed_Array_Reference (Prefix (N));
2111 else
2112 return False;
2113 end if;
2114 end Involves_Packed_Array_Reference;
2116 --------------------------
2117 -- Known_Aligned_Enough --
2118 --------------------------
2120 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2121 Typ : constant Entity_Id := Etype (Obj);
2123 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2124 -- If the component is in a record that contains previous packed
2125 -- components, consider it unaligned because the back-end might
2126 -- choose to pack the rest of the record. Lead to less efficient code,
2127 -- but safer vis-a-vis of back-end choices.
2129 --------------------------------
2130 -- In_Partially_Packed_Record --
2131 --------------------------------
2133 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2134 Rec_Type : constant Entity_Id := Scope (Comp);
2135 Prev_Comp : Entity_Id;
2137 begin
2138 Prev_Comp := First_Entity (Rec_Type);
2139 while Present (Prev_Comp) loop
2140 if Is_Packed (Etype (Prev_Comp)) then
2141 return True;
2143 elsif Prev_Comp = Comp then
2144 return False;
2145 end if;
2147 Next_Entity (Prev_Comp);
2148 end loop;
2150 return False;
2151 end In_Partially_Packed_Record;
2153 -- Start of processing for Known_Aligned_Enough
2155 begin
2156 -- Odd bit sizes don't need alignment anyway
2158 if Csiz mod 2 = 1 then
2159 return True;
2161 -- If we have a specified alignment, see if it is sufficient, if not
2162 -- then we can't possibly be aligned enough in any case.
2164 elsif Known_Alignment (Etype (Obj)) then
2165 -- Alignment required is 4 if size is a multiple of 4, and
2166 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2168 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2169 return False;
2170 end if;
2171 end if;
2173 -- OK, alignment should be sufficient, if object is aligned
2175 -- If object is strictly aligned, then it is definitely aligned
2177 if Strict_Alignment (Typ) then
2178 return True;
2180 -- Case of subscripted array reference
2182 elsif Nkind (Obj) = N_Indexed_Component then
2184 -- If we have a pointer to an array, then this is definitely
2185 -- aligned, because pointers always point to aligned versions.
2187 if Is_Access_Type (Etype (Prefix (Obj))) then
2188 return True;
2190 -- Otherwise, go look at the prefix
2192 else
2193 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2194 end if;
2196 -- Case of record field
2198 elsif Nkind (Obj) = N_Selected_Component then
2200 -- What is significant here is whether the record type is packed
2202 if Is_Record_Type (Etype (Prefix (Obj)))
2203 and then Is_Packed (Etype (Prefix (Obj)))
2204 then
2205 return False;
2207 -- Or the component has a component clause which might cause
2208 -- the component to become unaligned (we can't tell if the
2209 -- backend is doing alignment computations).
2211 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2212 return False;
2214 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2215 return False;
2217 -- In all other cases, go look at prefix
2219 else
2220 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2221 end if;
2223 -- If not selected or indexed component, must be aligned
2225 else
2226 return True;
2227 end if;
2228 end Known_Aligned_Enough;
2230 ---------------------
2231 -- Make_Shift_Left --
2232 ---------------------
2234 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2235 Nod : Node_Id;
2237 begin
2238 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2239 return N;
2240 else
2241 Nod :=
2242 Make_Op_Shift_Left (Sloc (N),
2243 Left_Opnd => N,
2244 Right_Opnd => S);
2245 Set_Shift_Count_OK (Nod, True);
2246 return Nod;
2247 end if;
2248 end Make_Shift_Left;
2250 ----------------------
2251 -- Make_Shift_Right --
2252 ----------------------
2254 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2255 Nod : Node_Id;
2257 begin
2258 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2259 return N;
2260 else
2261 Nod :=
2262 Make_Op_Shift_Right (Sloc (N),
2263 Left_Opnd => N,
2264 Right_Opnd => S);
2265 Set_Shift_Count_OK (Nod, True);
2266 return Nod;
2267 end if;
2268 end Make_Shift_Right;
2270 -----------------------------
2271 -- RJ_Unchecked_Convert_To --
2272 -----------------------------
2274 function RJ_Unchecked_Convert_To
2275 (Typ : Entity_Id;
2276 Expr : Node_Id)
2277 return Node_Id
2279 Source_Typ : constant Entity_Id := Etype (Expr);
2280 Target_Typ : constant Entity_Id := Typ;
2282 Src : Node_Id := Expr;
2284 Source_Siz : Nat;
2285 Target_Siz : Nat;
2287 begin
2288 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2289 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2291 -- In the big endian case, if the lengths of the two types differ,
2292 -- then we must worry about possible left justification in the
2293 -- conversion, and avoiding that is what this is all about.
2295 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2297 -- First step, if the source type is not a discrete type, then we
2298 -- first convert to a modular type of the source length, since
2299 -- otherwise, on a big-endian machine, we get left-justification.
2301 if not Is_Discrete_Type (Source_Typ) then
2302 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2303 end if;
2305 -- Next step. If the target is not a discrete type, then we first
2306 -- convert to a modular type of the target length, since
2307 -- otherwise, on a big-endian machine, we get left-justification.
2309 if not Is_Discrete_Type (Target_Typ) then
2310 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2311 end if;
2312 end if;
2314 -- And now we can do the final conversion to the target type
2316 return Unchecked_Convert_To (Target_Typ, Src);
2317 end RJ_Unchecked_Convert_To;
2319 ----------------------------------------------
2320 -- Setup_Enumeration_Packed_Array_Reference --
2321 ----------------------------------------------
2323 -- All we have to do here is to find the subscripts that correspond
2324 -- to the index positions that have non-standard enumeration types
2325 -- and insert a Pos attribute to get the proper subscript value.
2327 -- Finally the prefix must be uncheck converted to the corresponding
2328 -- packed array type.
2330 -- Note that the component type is unchanged, so we do not need to
2331 -- fiddle with the types (Gigi always automatically takes the packed
2332 -- array type if it is set, as it will be in this case).
2334 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2335 Pfx : constant Node_Id := Prefix (N);
2336 Typ : constant Entity_Id := Etype (N);
2337 Exprs : constant List_Id := Expressions (N);
2338 Expr : Node_Id;
2340 begin
2341 -- If the array is unconstrained, then we replace the array
2342 -- reference with its actual subtype. This actual subtype will
2343 -- have a packed array type with appropriate bounds.
2345 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2346 Convert_To_Actual_Subtype (Pfx);
2347 end if;
2349 Expr := First (Exprs);
2350 while Present (Expr) loop
2351 declare
2352 Loc : constant Source_Ptr := Sloc (Expr);
2353 Expr_Typ : constant Entity_Id := Etype (Expr);
2355 begin
2356 if Is_Enumeration_Type (Expr_Typ)
2357 and then Has_Non_Standard_Rep (Expr_Typ)
2358 then
2359 Rewrite (Expr,
2360 Make_Attribute_Reference (Loc,
2361 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2362 Attribute_Name => Name_Pos,
2363 Expressions => New_List (Relocate_Node (Expr))));
2364 Analyze_And_Resolve (Expr, Standard_Natural);
2365 end if;
2366 end;
2368 Next (Expr);
2369 end loop;
2371 Rewrite (N,
2372 Make_Indexed_Component (Sloc (N),
2373 Prefix =>
2374 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2375 Expressions => Exprs));
2377 Analyze_And_Resolve (N, Typ);
2379 end Setup_Enumeration_Packed_Array_Reference;
2381 -----------------------------------------
2382 -- Setup_Inline_Packed_Array_Reference --
2383 -----------------------------------------
2385 procedure Setup_Inline_Packed_Array_Reference
2386 (N : Node_Id;
2387 Atyp : Entity_Id;
2388 Obj : in out Node_Id;
2389 Cmask : out Uint;
2390 Shift : out Node_Id)
2392 Loc : constant Source_Ptr := Sloc (N);
2393 Ctyp : Entity_Id;
2394 PAT : Entity_Id;
2395 Otyp : Entity_Id;
2396 Csiz : Uint;
2397 Osiz : Uint;
2399 begin
2400 Ctyp := Component_Type (Atyp);
2401 Csiz := Component_Size (Atyp);
2403 Convert_To_PAT_Type (Obj);
2404 PAT := Etype (Obj);
2406 Cmask := 2 ** Csiz - 1;
2408 if Is_Array_Type (PAT) then
2409 Otyp := Component_Type (PAT);
2410 Osiz := Esize (Otyp);
2412 else
2413 Otyp := PAT;
2415 -- In the case where the PAT is a modular type, we want the actual
2416 -- size in bits of the modular value we use. This is neither the
2417 -- Object_Size nor the Value_Size, either of which may have been
2418 -- reset to strange values, but rather the minimum size. Note that
2419 -- since this is a modular type with full range, the issue of
2420 -- biased representation does not arise.
2422 Osiz := UI_From_Int (Minimum_Size (Otyp));
2423 end if;
2425 Compute_Linear_Subscript (Atyp, N, Shift);
2427 -- If the component size is not 1, then the subscript must be
2428 -- multiplied by the component size to get the shift count.
2430 if Csiz /= 1 then
2431 Shift :=
2432 Make_Op_Multiply (Loc,
2433 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2434 Right_Opnd => Shift);
2435 end if;
2437 -- If we have the array case, then this shift count must be broken
2438 -- down into a byte subscript, and a shift within the byte.
2440 if Is_Array_Type (PAT) then
2442 declare
2443 New_Shift : Node_Id;
2445 begin
2446 -- We must analyze shift, since we will duplicate it
2448 Set_Parent (Shift, N);
2449 Analyze_And_Resolve
2450 (Shift, Standard_Integer, Suppress => All_Checks);
2452 -- The shift count within the word is
2453 -- shift mod Osiz
2455 New_Shift :=
2456 Make_Op_Mod (Loc,
2457 Left_Opnd => Duplicate_Subexpr (Shift),
2458 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2460 -- The subscript to be used on the PAT array is
2461 -- shift / Osiz
2463 Obj :=
2464 Make_Indexed_Component (Loc,
2465 Prefix => Obj,
2466 Expressions => New_List (
2467 Make_Op_Divide (Loc,
2468 Left_Opnd => Duplicate_Subexpr (Shift),
2469 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2471 Shift := New_Shift;
2472 end;
2474 -- For the modular integer case, the object to be manipulated is
2475 -- the entire array, so Obj is unchanged. Note that we will reset
2476 -- its type to PAT before returning to the caller.
2478 else
2479 null;
2480 end if;
2482 -- The one remaining step is to modify the shift count for the
2483 -- big-endian case. Consider the following example in a byte:
2485 -- xxxxxxxx bits of byte
2486 -- vvvvvvvv bits of value
2487 -- 33221100 little-endian numbering
2488 -- 00112233 big-endian numbering
2490 -- Here we have the case of 2-bit fields
2492 -- For the little-endian case, we already have the proper shift
2493 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2495 -- For the big endian case, we have to adjust the shift count,
2496 -- computing it as (N - F) - shift, where N is the number of bits
2497 -- in an element of the array used to implement the packed array,
2498 -- F is the number of bits in a source level array element, and
2499 -- shift is the count so far computed.
2501 if Bytes_Big_Endian then
2502 Shift :=
2503 Make_Op_Subtract (Loc,
2504 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2505 Right_Opnd => Shift);
2506 end if;
2508 Set_Parent (Shift, N);
2509 Set_Parent (Obj, N);
2510 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2511 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2513 -- Make sure final type of object is the appropriate packed type
2515 Set_Etype (Obj, Otyp);
2517 end Setup_Inline_Packed_Array_Reference;
2519 end Exp_Pakd;