* c-cppbuiltin.c (c_cpp_builtins): Define __pic__ and __PIC__ when
[official-gcc.git] / gcc / ada / exp_pakd.adb
blob5b0a10a2bc6f62b87936e1ac8283fb18afb27ef7
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-2005, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Errout; use Errout;
31 with Exp_Dbug; use Exp_Dbug;
32 with Exp_Util; use Exp_Util;
33 with Nlists; use Nlists;
34 with Nmake; use Nmake;
35 with Rtsfind; use Rtsfind;
36 with Sem; use Sem;
37 with Sem_Ch3; use Sem_Ch3;
38 with Sem_Ch8; use Sem_Ch8;
39 with Sem_Ch13; use Sem_Ch13;
40 with Sem_Eval; use Sem_Eval;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Snames; use Snames;
45 with Stand; use Stand;
46 with Targparm; use Targparm;
47 with Tbuild; use Tbuild;
48 with Ttypes; use Ttypes;
49 with Uintp; use Uintp;
51 package body Exp_Pakd is
53 ---------------------------
54 -- Endian Considerations --
55 ---------------------------
57 -- As described in the specification, bit numbering in a packed array
58 -- is consistent with bit numbering in a record representation clause,
59 -- and hence dependent on the endianness of the machine:
61 -- For little-endian machines, element zero is at the right hand end
62 -- (low order end) of a bit field.
64 -- For big-endian machines, element zero is at the left hand end
65 -- (high order end) of a bit field.
67 -- The shifts that are used to right justify a field therefore differ
68 -- in the two cases. For the little-endian case, we can simply use the
69 -- bit number (i.e. the element number * element size) as the count for
70 -- a right shift. For the big-endian case, we have to subtract the shift
71 -- count from an appropriate constant to use in the right shift. We use
72 -- rotates instead of shifts (which is necessary in the store case to
73 -- preserve other fields), and we expect that the backend will be able
74 -- to change the right rotate into a left rotate, avoiding the subtract,
75 -- if the architecture provides such an instruction.
77 ----------------------------------------------
78 -- Entity Tables for Packed Access Routines --
79 ----------------------------------------------
81 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
82 -- library routines. This table is used to obtain the entity for the
83 -- proper routine.
85 type E_Array is array (Int range 01 .. 63) of RE_Id;
87 -- Array of Bits_nn entities. Note that we do not use library routines
88 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
89 -- entries from System.Unsigned, because we also use this table for
90 -- certain special unchecked conversions in the big-endian case.
92 Bits_Id : constant E_Array :=
93 (01 => RE_Bits_1,
94 02 => RE_Bits_2,
95 03 => RE_Bits_03,
96 04 => RE_Bits_4,
97 05 => RE_Bits_05,
98 06 => RE_Bits_06,
99 07 => RE_Bits_07,
100 08 => RE_Unsigned_8,
101 09 => RE_Bits_09,
102 10 => RE_Bits_10,
103 11 => RE_Bits_11,
104 12 => RE_Bits_12,
105 13 => RE_Bits_13,
106 14 => RE_Bits_14,
107 15 => RE_Bits_15,
108 16 => RE_Unsigned_16,
109 17 => RE_Bits_17,
110 18 => RE_Bits_18,
111 19 => RE_Bits_19,
112 20 => RE_Bits_20,
113 21 => RE_Bits_21,
114 22 => RE_Bits_22,
115 23 => RE_Bits_23,
116 24 => RE_Bits_24,
117 25 => RE_Bits_25,
118 26 => RE_Bits_26,
119 27 => RE_Bits_27,
120 28 => RE_Bits_28,
121 29 => RE_Bits_29,
122 30 => RE_Bits_30,
123 31 => RE_Bits_31,
124 32 => RE_Unsigned_32,
125 33 => RE_Bits_33,
126 34 => RE_Bits_34,
127 35 => RE_Bits_35,
128 36 => RE_Bits_36,
129 37 => RE_Bits_37,
130 38 => RE_Bits_38,
131 39 => RE_Bits_39,
132 40 => RE_Bits_40,
133 41 => RE_Bits_41,
134 42 => RE_Bits_42,
135 43 => RE_Bits_43,
136 44 => RE_Bits_44,
137 45 => RE_Bits_45,
138 46 => RE_Bits_46,
139 47 => RE_Bits_47,
140 48 => RE_Bits_48,
141 49 => RE_Bits_49,
142 50 => RE_Bits_50,
143 51 => RE_Bits_51,
144 52 => RE_Bits_52,
145 53 => RE_Bits_53,
146 54 => RE_Bits_54,
147 55 => RE_Bits_55,
148 56 => RE_Bits_56,
149 57 => RE_Bits_57,
150 58 => RE_Bits_58,
151 59 => RE_Bits_59,
152 60 => RE_Bits_60,
153 61 => RE_Bits_61,
154 62 => RE_Bits_62,
155 63 => RE_Bits_63);
157 -- Array of Get routine entities. These are used to obtain an element
158 -- from a packed array. The N'th entry is used to obtain elements from
159 -- a packed array whose component size is N. RE_Null is used as a null
160 -- entry, for the cases where a library routine is not used.
162 Get_Id : constant E_Array :=
163 (01 => RE_Null,
164 02 => RE_Null,
165 03 => RE_Get_03,
166 04 => RE_Null,
167 05 => RE_Get_05,
168 06 => RE_Get_06,
169 07 => RE_Get_07,
170 08 => RE_Null,
171 09 => RE_Get_09,
172 10 => RE_Get_10,
173 11 => RE_Get_11,
174 12 => RE_Get_12,
175 13 => RE_Get_13,
176 14 => RE_Get_14,
177 15 => RE_Get_15,
178 16 => RE_Null,
179 17 => RE_Get_17,
180 18 => RE_Get_18,
181 19 => RE_Get_19,
182 20 => RE_Get_20,
183 21 => RE_Get_21,
184 22 => RE_Get_22,
185 23 => RE_Get_23,
186 24 => RE_Get_24,
187 25 => RE_Get_25,
188 26 => RE_Get_26,
189 27 => RE_Get_27,
190 28 => RE_Get_28,
191 29 => RE_Get_29,
192 30 => RE_Get_30,
193 31 => RE_Get_31,
194 32 => RE_Null,
195 33 => RE_Get_33,
196 34 => RE_Get_34,
197 35 => RE_Get_35,
198 36 => RE_Get_36,
199 37 => RE_Get_37,
200 38 => RE_Get_38,
201 39 => RE_Get_39,
202 40 => RE_Get_40,
203 41 => RE_Get_41,
204 42 => RE_Get_42,
205 43 => RE_Get_43,
206 44 => RE_Get_44,
207 45 => RE_Get_45,
208 46 => RE_Get_46,
209 47 => RE_Get_47,
210 48 => RE_Get_48,
211 49 => RE_Get_49,
212 50 => RE_Get_50,
213 51 => RE_Get_51,
214 52 => RE_Get_52,
215 53 => RE_Get_53,
216 54 => RE_Get_54,
217 55 => RE_Get_55,
218 56 => RE_Get_56,
219 57 => RE_Get_57,
220 58 => RE_Get_58,
221 59 => RE_Get_59,
222 60 => RE_Get_60,
223 61 => RE_Get_61,
224 62 => RE_Get_62,
225 63 => RE_Get_63);
227 -- Array of Get routine entities to be used in the case where the packed
228 -- array is itself a component of a packed structure, and therefore may
229 -- not be fully aligned. This only affects the even sizes, since for the
230 -- odd sizes, we do not get any fixed alignment in any case.
232 GetU_Id : constant E_Array :=
233 (01 => RE_Null,
234 02 => RE_Null,
235 03 => RE_Get_03,
236 04 => RE_Null,
237 05 => RE_Get_05,
238 06 => RE_GetU_06,
239 07 => RE_Get_07,
240 08 => RE_Null,
241 09 => RE_Get_09,
242 10 => RE_GetU_10,
243 11 => RE_Get_11,
244 12 => RE_GetU_12,
245 13 => RE_Get_13,
246 14 => RE_GetU_14,
247 15 => RE_Get_15,
248 16 => RE_Null,
249 17 => RE_Get_17,
250 18 => RE_GetU_18,
251 19 => RE_Get_19,
252 20 => RE_GetU_20,
253 21 => RE_Get_21,
254 22 => RE_GetU_22,
255 23 => RE_Get_23,
256 24 => RE_GetU_24,
257 25 => RE_Get_25,
258 26 => RE_GetU_26,
259 27 => RE_Get_27,
260 28 => RE_GetU_28,
261 29 => RE_Get_29,
262 30 => RE_GetU_30,
263 31 => RE_Get_31,
264 32 => RE_Null,
265 33 => RE_Get_33,
266 34 => RE_GetU_34,
267 35 => RE_Get_35,
268 36 => RE_GetU_36,
269 37 => RE_Get_37,
270 38 => RE_GetU_38,
271 39 => RE_Get_39,
272 40 => RE_GetU_40,
273 41 => RE_Get_41,
274 42 => RE_GetU_42,
275 43 => RE_Get_43,
276 44 => RE_GetU_44,
277 45 => RE_Get_45,
278 46 => RE_GetU_46,
279 47 => RE_Get_47,
280 48 => RE_GetU_48,
281 49 => RE_Get_49,
282 50 => RE_GetU_50,
283 51 => RE_Get_51,
284 52 => RE_GetU_52,
285 53 => RE_Get_53,
286 54 => RE_GetU_54,
287 55 => RE_Get_55,
288 56 => RE_GetU_56,
289 57 => RE_Get_57,
290 58 => RE_GetU_58,
291 59 => RE_Get_59,
292 60 => RE_GetU_60,
293 61 => RE_Get_61,
294 62 => RE_GetU_62,
295 63 => RE_Get_63);
297 -- Array of Set routine entities. These are used to assign an element
298 -- of a packed array. The N'th entry is used to assign elements for
299 -- a packed array whose component size is N. RE_Null is used as a null
300 -- entry, for the cases where a library routine is not used.
302 Set_Id : constant E_Array :=
303 (01 => RE_Null,
304 02 => RE_Null,
305 03 => RE_Set_03,
306 04 => RE_Null,
307 05 => RE_Set_05,
308 06 => RE_Set_06,
309 07 => RE_Set_07,
310 08 => RE_Null,
311 09 => RE_Set_09,
312 10 => RE_Set_10,
313 11 => RE_Set_11,
314 12 => RE_Set_12,
315 13 => RE_Set_13,
316 14 => RE_Set_14,
317 15 => RE_Set_15,
318 16 => RE_Null,
319 17 => RE_Set_17,
320 18 => RE_Set_18,
321 19 => RE_Set_19,
322 20 => RE_Set_20,
323 21 => RE_Set_21,
324 22 => RE_Set_22,
325 23 => RE_Set_23,
326 24 => RE_Set_24,
327 25 => RE_Set_25,
328 26 => RE_Set_26,
329 27 => RE_Set_27,
330 28 => RE_Set_28,
331 29 => RE_Set_29,
332 30 => RE_Set_30,
333 31 => RE_Set_31,
334 32 => RE_Null,
335 33 => RE_Set_33,
336 34 => RE_Set_34,
337 35 => RE_Set_35,
338 36 => RE_Set_36,
339 37 => RE_Set_37,
340 38 => RE_Set_38,
341 39 => RE_Set_39,
342 40 => RE_Set_40,
343 41 => RE_Set_41,
344 42 => RE_Set_42,
345 43 => RE_Set_43,
346 44 => RE_Set_44,
347 45 => RE_Set_45,
348 46 => RE_Set_46,
349 47 => RE_Set_47,
350 48 => RE_Set_48,
351 49 => RE_Set_49,
352 50 => RE_Set_50,
353 51 => RE_Set_51,
354 52 => RE_Set_52,
355 53 => RE_Set_53,
356 54 => RE_Set_54,
357 55 => RE_Set_55,
358 56 => RE_Set_56,
359 57 => RE_Set_57,
360 58 => RE_Set_58,
361 59 => RE_Set_59,
362 60 => RE_Set_60,
363 61 => RE_Set_61,
364 62 => RE_Set_62,
365 63 => RE_Set_63);
367 -- Array of Set routine entities to be used in the case where the packed
368 -- array is itself a component of a packed structure, and therefore may
369 -- not be fully aligned. This only affects the even sizes, since for the
370 -- odd sizes, we do not get any fixed alignment in any case.
372 SetU_Id : constant E_Array :=
373 (01 => RE_Null,
374 02 => RE_Null,
375 03 => RE_Set_03,
376 04 => RE_Null,
377 05 => RE_Set_05,
378 06 => RE_SetU_06,
379 07 => RE_Set_07,
380 08 => RE_Null,
381 09 => RE_Set_09,
382 10 => RE_SetU_10,
383 11 => RE_Set_11,
384 12 => RE_SetU_12,
385 13 => RE_Set_13,
386 14 => RE_SetU_14,
387 15 => RE_Set_15,
388 16 => RE_Null,
389 17 => RE_Set_17,
390 18 => RE_SetU_18,
391 19 => RE_Set_19,
392 20 => RE_SetU_20,
393 21 => RE_Set_21,
394 22 => RE_SetU_22,
395 23 => RE_Set_23,
396 24 => RE_SetU_24,
397 25 => RE_Set_25,
398 26 => RE_SetU_26,
399 27 => RE_Set_27,
400 28 => RE_SetU_28,
401 29 => RE_Set_29,
402 30 => RE_SetU_30,
403 31 => RE_Set_31,
404 32 => RE_Null,
405 33 => RE_Set_33,
406 34 => RE_SetU_34,
407 35 => RE_Set_35,
408 36 => RE_SetU_36,
409 37 => RE_Set_37,
410 38 => RE_SetU_38,
411 39 => RE_Set_39,
412 40 => RE_SetU_40,
413 41 => RE_Set_41,
414 42 => RE_SetU_42,
415 43 => RE_Set_43,
416 44 => RE_SetU_44,
417 45 => RE_Set_45,
418 46 => RE_SetU_46,
419 47 => RE_Set_47,
420 48 => RE_SetU_48,
421 49 => RE_Set_49,
422 50 => RE_SetU_50,
423 51 => RE_Set_51,
424 52 => RE_SetU_52,
425 53 => RE_Set_53,
426 54 => RE_SetU_54,
427 55 => RE_Set_55,
428 56 => RE_SetU_56,
429 57 => RE_Set_57,
430 58 => RE_SetU_58,
431 59 => RE_Set_59,
432 60 => RE_SetU_60,
433 61 => RE_Set_61,
434 62 => RE_SetU_62,
435 63 => RE_Set_63);
437 -----------------------
438 -- Local Subprograms --
439 -----------------------
441 procedure Compute_Linear_Subscript
442 (Atyp : Entity_Id;
443 N : Node_Id;
444 Subscr : out Node_Id);
445 -- Given a constrained array type Atyp, and an indexed component node
446 -- N referencing an array object of this type, build an expression of
447 -- type Standard.Integer representing the zero-based linear subscript
448 -- value. This expression includes any required range checks.
450 procedure Convert_To_PAT_Type (Aexp : Node_Id);
451 -- Given an expression of a packed array type, builds a corresponding
452 -- expression whose type is the implementation type used to represent
453 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
455 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
456 -- There are two versions of the Set routines, the ones used when the
457 -- object is known to be sufficiently well aligned given the number of
458 -- bits, and the ones used when the object is not known to be aligned.
459 -- This routine is used to determine which set to use. Obj is a reference
460 -- to the object, and Csiz is the component size of the packed array.
461 -- True is returned if the alignment of object is known to be sufficient,
462 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
463 -- 2 otherwise.
465 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
466 -- Build a left shift node, checking for the case of a shift count of zero
468 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
469 -- Build a right shift node, checking for the case of a shift count of zero
471 function RJ_Unchecked_Convert_To
472 (Typ : Entity_Id;
473 Expr : Node_Id) 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, Long_Unsigned, or Long_Long_Unsigned, and is either the
519 -- entire value, for the small static case, or the proper selected byte
520 -- from the array in the large or dynamic case. This node is analyzed
521 -- and resolved on return.
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 -- This is not strictly the case ??? If the reference is an actual
690 -- in a call, the expansion of the prefix is delayed, and must be
691 -- reanalyzed, see Reset_Packed_Prefix. On the other hand, if the
692 -- prefix is a simple array reference, reanalysis can produce spurious
693 -- type errors when the PAT type is replaced again with the original
694 -- type of the array. The following is correct and minimal, but the
695 -- handling of more complex packed expressions in actuals is confused.
696 -- It is likely that the problem only remains for actuals in calls.
698 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
700 if Is_Entity_Name (Aexp)
701 or else
702 (Nkind (Aexp) = N_Indexed_Component
703 and then Is_Entity_Name (Prefix (Aexp)))
704 then
705 Set_Analyzed (Aexp);
706 end if;
707 end Convert_To_PAT_Type;
709 ------------------------------
710 -- Create_Packed_Array_Type --
711 ------------------------------
713 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
714 Loc : constant Source_Ptr := Sloc (Typ);
715 Ctyp : constant Entity_Id := Component_Type (Typ);
716 Csize : constant Uint := Component_Size (Typ);
718 Ancest : Entity_Id;
719 PB_Type : Entity_Id;
720 PASize : Uint;
721 Decl : Node_Id;
722 PAT : Entity_Id;
723 Len_Dim : Node_Id;
724 Len_Expr : Node_Id;
725 Len_Bits : Uint;
726 Bits_U1 : Node_Id;
727 PAT_High : Node_Id;
728 Btyp : Entity_Id;
729 Lit : Node_Id;
731 procedure Install_PAT;
732 -- This procedure is called with Decl set to the declaration for the
733 -- packed array type. It creates the type and installs it as required.
735 procedure Set_PB_Type;
736 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
737 -- requirements (see documentation in the spec of this package).
739 -----------------
740 -- Install_PAT --
741 -----------------
743 procedure Install_PAT is
744 Pushed_Scope : Boolean := False;
746 begin
747 -- We do not want to put the declaration we have created in the tree
748 -- since it is often hard, and sometimes impossible to find a proper
749 -- place for it (the impossible case arises for a packed array type
750 -- with bounds depending on the discriminant, a declaration cannot
751 -- be put inside the record, and the reference to the discriminant
752 -- cannot be outside the record).
754 -- The solution is to analyze the declaration while temporarily
755 -- attached to the tree at an appropriate point, and then we install
756 -- the resulting type as an Itype in the packed array type field of
757 -- the original type, so that no explicit declaration is required.
759 -- Note: the packed type is created in the scope of its parent
760 -- type. There are at least some cases where the current scope
761 -- is deeper, and so when this is the case, we temporarily reset
762 -- the scope for the definition. This is clearly safe, since the
763 -- first use of the packed array type will be the implicit
764 -- reference from the corresponding unpacked type when it is
765 -- elaborated.
767 if Is_Itype (Typ) then
768 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
769 else
770 Set_Parent (Decl, Declaration_Node (Typ));
771 end if;
773 if Scope (Typ) /= Current_Scope then
774 New_Scope (Scope (Typ));
775 Pushed_Scope := True;
776 end if;
778 Set_Is_Itype (PAT, True);
779 Set_Packed_Array_Type (Typ, PAT);
780 Analyze (Decl, Suppress => All_Checks);
782 if Pushed_Scope then
783 Pop_Scope;
784 end if;
786 -- Set Esize and RM_Size to the actual size of the packed object
787 -- Do not reset RM_Size if already set, as happens in the case
788 -- of a modular type.
790 Set_Esize (PAT, PASize);
792 if Unknown_RM_Size (PAT) then
793 Set_RM_Size (PAT, PASize);
794 end if;
796 -- Set remaining fields of packed array type
798 Init_Alignment (PAT);
799 Set_Parent (PAT, Empty);
800 Set_Associated_Node_For_Itype (PAT, Typ);
801 Set_Is_Packed_Array_Type (PAT, True);
802 Set_Original_Array_Type (PAT, Typ);
804 -- We definitely do not want to delay freezing for packed array
805 -- types. This is of particular importance for the itypes that
806 -- are generated for record components depending on discriminants
807 -- where there is no place to put the freeze node.
809 Set_Has_Delayed_Freeze (PAT, False);
810 Set_Has_Delayed_Freeze (Etype (PAT), False);
812 -- If we did allocate a freeze node, then clear out the reference
813 -- since it is obsolete (should we delete the freeze node???)
815 Set_Freeze_Node (PAT, Empty);
816 Set_Freeze_Node (Etype (PAT), Empty);
817 end Install_PAT;
819 -----------------
820 -- Set_PB_Type --
821 -----------------
823 procedure Set_PB_Type is
824 begin
825 -- If the user has specified an explicit alignment for the
826 -- type or component, take it into account.
828 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
829 or else Alignment (Typ) = 1
830 or else Component_Alignment (Typ) = Calign_Storage_Unit
831 then
832 PB_Type := RTE (RE_Packed_Bytes1);
834 elsif Csize mod 4 /= 0
835 or else Alignment (Typ) = 2
836 then
837 PB_Type := RTE (RE_Packed_Bytes2);
839 else
840 PB_Type := RTE (RE_Packed_Bytes4);
841 end if;
842 end Set_PB_Type;
844 -- Start of processing for Create_Packed_Array_Type
846 begin
847 -- If we already have a packed array type, nothing to do
849 if Present (Packed_Array_Type (Typ)) then
850 return;
851 end if;
853 -- If our immediate ancestor subtype is constrained, and it already
854 -- has a packed array type, then just share the same type, since the
855 -- bounds must be the same. If the ancestor is not an array type but
856 -- a private type, as can happen with multiple instantiations, create
857 -- a new packed type, to avoid privacy issues.
859 if Ekind (Typ) = E_Array_Subtype then
860 Ancest := Ancestor_Subtype (Typ);
862 if Present (Ancest)
863 and then Is_Array_Type (Ancest)
864 and then Is_Constrained (Ancest)
865 and then Present (Packed_Array_Type (Ancest))
866 then
867 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
868 return;
869 end if;
870 end if;
872 -- We preset the result type size from the size of the original array
873 -- type, since this size clearly belongs to the packed array type. The
874 -- size of the conceptual unpacked type is always set to unknown.
876 PASize := Esize (Typ);
878 -- Case of an array where at least one index is of an enumeration
879 -- type with a non-standard representation, but the component size
880 -- is not appropriate for bit packing. This is the case where we
881 -- have Is_Packed set (we would never be in this unit otherwise),
882 -- but Is_Bit_Packed_Array is false.
884 -- Note that if the component size is appropriate for bit packing,
885 -- then the circuit for the computation of the subscript properly
886 -- deals with the non-standard enumeration type case by taking the
887 -- Pos anyway.
889 if not Is_Bit_Packed_Array (Typ) then
891 -- Here we build a declaration:
893 -- type tttP is array (index1, index2, ...) of component_type
895 -- where index1, index2, are the index types. These are the same
896 -- as the index types of the original array, except for the non-
897 -- standard representation enumeration type case, where we have
898 -- two subcases.
900 -- For the unconstrained array case, we use
902 -- Natural range <>
904 -- For the constrained case, we use
906 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
907 -- Enum_Type'Pos (Enum_Type'Last);
909 PAT :=
910 Make_Defining_Identifier (Loc,
911 Chars => New_External_Name (Chars (Typ), 'P'));
913 Set_Packed_Array_Type (Typ, PAT);
915 declare
916 Indexes : constant List_Id := New_List;
917 Indx : Node_Id;
918 Indx_Typ : Entity_Id;
919 Enum_Case : Boolean;
920 Typedef : Node_Id;
922 begin
923 Indx := First_Index (Typ);
925 while Present (Indx) loop
926 Indx_Typ := Etype (Indx);
928 Enum_Case := Is_Enumeration_Type (Indx_Typ)
929 and then Has_Non_Standard_Rep (Indx_Typ);
931 -- Unconstrained case
933 if not Is_Constrained (Typ) then
934 if Enum_Case then
935 Indx_Typ := Standard_Natural;
936 end if;
938 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
940 -- Constrained case
942 else
943 if not Enum_Case then
944 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
946 else
947 Append_To (Indexes,
948 Make_Subtype_Indication (Loc,
949 Subtype_Mark =>
950 New_Occurrence_Of (Standard_Natural, Loc),
951 Constraint =>
952 Make_Range_Constraint (Loc,
953 Range_Expression =>
954 Make_Range (Loc,
955 Low_Bound =>
956 Make_Attribute_Reference (Loc,
957 Prefix =>
958 New_Occurrence_Of (Indx_Typ, Loc),
959 Attribute_Name => Name_Pos,
960 Expressions => New_List (
961 Make_Attribute_Reference (Loc,
962 Prefix =>
963 New_Occurrence_Of (Indx_Typ, Loc),
964 Attribute_Name => Name_First))),
966 High_Bound =>
967 Make_Attribute_Reference (Loc,
968 Prefix =>
969 New_Occurrence_Of (Indx_Typ, Loc),
970 Attribute_Name => Name_Pos,
971 Expressions => New_List (
972 Make_Attribute_Reference (Loc,
973 Prefix =>
974 New_Occurrence_Of (Indx_Typ, Loc),
975 Attribute_Name => Name_Last)))))));
977 end if;
978 end if;
980 Next_Index (Indx);
981 end loop;
983 if not Is_Constrained (Typ) then
984 Typedef :=
985 Make_Unconstrained_Array_Definition (Loc,
986 Subtype_Marks => Indexes,
987 Component_Definition =>
988 Make_Component_Definition (Loc,
989 Aliased_Present => False,
990 Subtype_Indication =>
991 New_Occurrence_Of (Ctyp, Loc)));
993 else
994 Typedef :=
995 Make_Constrained_Array_Definition (Loc,
996 Discrete_Subtype_Definitions => Indexes,
997 Component_Definition =>
998 Make_Component_Definition (Loc,
999 Aliased_Present => False,
1000 Subtype_Indication =>
1001 New_Occurrence_Of (Ctyp, Loc)));
1002 end if;
1004 Decl :=
1005 Make_Full_Type_Declaration (Loc,
1006 Defining_Identifier => PAT,
1007 Type_Definition => Typedef);
1008 end;
1010 -- Set type as packed array type and install it
1012 Set_Is_Packed_Array_Type (PAT);
1013 Install_PAT;
1014 return;
1016 -- Case of bit-packing required for unconstrained array. We create
1017 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1019 elsif not Is_Constrained (Typ) then
1020 PAT :=
1021 Make_Defining_Identifier (Loc,
1022 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1024 Set_Packed_Array_Type (Typ, PAT);
1025 Set_PB_Type;
1027 Decl :=
1028 Make_Subtype_Declaration (Loc,
1029 Defining_Identifier => PAT,
1030 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1031 Install_PAT;
1032 return;
1034 -- Remaining code is for the case of bit-packing for constrained array
1036 -- The name of the packed array subtype is
1038 -- ttt___Xsss
1040 -- where sss is the component size in bits and ttt is the name of
1041 -- the parent packed type.
1043 else
1044 PAT :=
1045 Make_Defining_Identifier (Loc,
1046 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1048 Set_Packed_Array_Type (Typ, PAT);
1050 -- Build an expression for the length of the array in bits.
1051 -- This is the product of the length of each of the dimensions
1053 declare
1054 J : Nat := 1;
1056 begin
1057 Len_Expr := Empty; -- suppress junk warning
1059 loop
1060 Len_Dim :=
1061 Make_Attribute_Reference (Loc,
1062 Attribute_Name => Name_Length,
1063 Prefix => New_Occurrence_Of (Typ, Loc),
1064 Expressions => New_List (
1065 Make_Integer_Literal (Loc, J)));
1067 if J = 1 then
1068 Len_Expr := Len_Dim;
1070 else
1071 Len_Expr :=
1072 Make_Op_Multiply (Loc,
1073 Left_Opnd => Len_Expr,
1074 Right_Opnd => Len_Dim);
1075 end if;
1077 J := J + 1;
1078 exit when J > Number_Dimensions (Typ);
1079 end loop;
1080 end;
1082 -- Temporarily attach the length expression to the tree and analyze
1083 -- and resolve it, so that we can test its value. We assume that the
1084 -- total length fits in type Integer. This expression may involve
1085 -- discriminants, so we treat it as a default/per-object expression.
1087 Set_Parent (Len_Expr, Typ);
1088 Analyze_Per_Use_Expression (Len_Expr, Standard_Long_Long_Integer);
1090 -- Use a modular type if possible. We can do this if we have
1091 -- static bounds, and the length is small enough, and the length
1092 -- is not zero. We exclude the zero length case because the size
1093 -- of things is always at least one, and the zero length object
1094 -- would have an anomalous size.
1096 if Compile_Time_Known_Value (Len_Expr) then
1097 Len_Bits := Expr_Value (Len_Expr) * Csize;
1099 -- Check for size known to be too large
1101 if Len_Bits >
1102 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1103 then
1104 if System_Storage_Unit = 8 then
1105 Error_Msg_N
1106 ("packed array size cannot exceed " &
1107 "Integer''Last bytes", Typ);
1108 else
1109 Error_Msg_N
1110 ("packed array size cannot exceed " &
1111 "Integer''Last storage units", Typ);
1112 end if;
1114 -- Reset length to arbitrary not too high value to continue
1116 Len_Expr := Make_Integer_Literal (Loc, 65535);
1117 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1118 end if;
1120 -- We normally consider small enough to mean no larger than the
1121 -- value of System_Max_Binary_Modulus_Power, checking that in the
1122 -- case of values longer than word size, we have long shifts.
1124 if Len_Bits > 0
1125 and then
1126 (Len_Bits <= System_Word_Size
1127 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1128 and then Support_Long_Shifts_On_Target))
1130 -- Also test for alignment given. If an alignment is given which
1131 -- is smaller than the natural modular alignment, force the array
1132 -- of bytes representation to accommodate the alignment.
1134 and then
1135 (No (Alignment_Clause (Typ))
1136 or else
1137 Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1138 / System_Storage_Unit))
1139 then
1140 -- We can use the modular type, it has the form:
1142 -- subtype tttPn is btyp
1143 -- range 0 .. 2 ** ((Typ'Length (1)
1144 -- * ... * Typ'Length (n)) * Csize) - 1;
1146 -- The bounds are statically known, and btyp is one
1147 -- of the unsigned types, depending on the length. If the
1148 -- type is its first subtype, i.e. it is a user-defined
1149 -- type, no object of the type will be larger, and it is
1150 -- worthwhile to use a small unsigned type.
1152 if Len_Bits <= Standard_Short_Integer_Size
1153 and then First_Subtype (Typ) = Typ
1154 then
1155 Btyp := RTE (RE_Short_Unsigned);
1157 elsif Len_Bits <= Standard_Integer_Size then
1158 Btyp := RTE (RE_Unsigned);
1160 elsif Len_Bits <= Standard_Long_Integer_Size then
1161 Btyp := RTE (RE_Long_Unsigned);
1163 else
1164 Btyp := RTE (RE_Long_Long_Unsigned);
1165 end if;
1167 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1168 Set_Print_In_Hex (Lit);
1170 Decl :=
1171 Make_Subtype_Declaration (Loc,
1172 Defining_Identifier => PAT,
1173 Subtype_Indication =>
1174 Make_Subtype_Indication (Loc,
1175 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1177 Constraint =>
1178 Make_Range_Constraint (Loc,
1179 Range_Expression =>
1180 Make_Range (Loc,
1181 Low_Bound =>
1182 Make_Integer_Literal (Loc, 0),
1183 High_Bound => Lit))));
1185 if PASize = Uint_0 then
1186 PASize := Len_Bits;
1187 end if;
1189 Install_PAT;
1190 return;
1191 end if;
1192 end if;
1194 -- Could not use a modular type, for all other cases, we build
1195 -- a packed array subtype:
1197 -- subtype tttPn is
1198 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1200 -- Bits is the length of the array in bits
1202 Set_PB_Type;
1204 Bits_U1 :=
1205 Make_Op_Add (Loc,
1206 Left_Opnd =>
1207 Make_Op_Multiply (Loc,
1208 Left_Opnd =>
1209 Make_Integer_Literal (Loc, Csize),
1210 Right_Opnd => Len_Expr),
1212 Right_Opnd =>
1213 Make_Integer_Literal (Loc, 7));
1215 Set_Paren_Count (Bits_U1, 1);
1217 PAT_High :=
1218 Make_Op_Subtract (Loc,
1219 Left_Opnd =>
1220 Make_Op_Divide (Loc,
1221 Left_Opnd => Bits_U1,
1222 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1223 Right_Opnd => Make_Integer_Literal (Loc, 1));
1225 Decl :=
1226 Make_Subtype_Declaration (Loc,
1227 Defining_Identifier => PAT,
1228 Subtype_Indication =>
1229 Make_Subtype_Indication (Loc,
1230 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1231 Constraint =>
1232 Make_Index_Or_Discriminant_Constraint (Loc,
1233 Constraints => New_List (
1234 Make_Range (Loc,
1235 Low_Bound =>
1236 Make_Integer_Literal (Loc, 0),
1237 High_Bound =>
1238 Convert_To (Standard_Integer, PAT_High))))));
1240 Install_PAT;
1242 -- Currently the code in this unit requires that packed arrays
1243 -- represented by non-modular arrays of bytes be on a byte
1244 -- boundary for bit sizes handled by System.Pack_nn units.
1245 -- That's because these units assume the array being accessed
1246 -- starts on a byte boundary.
1248 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1249 Set_Must_Be_On_Byte_Boundary (Typ);
1250 end if;
1251 end if;
1252 end Create_Packed_Array_Type;
1254 -----------------------------------
1255 -- Expand_Bit_Packed_Element_Set --
1256 -----------------------------------
1258 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1259 Loc : constant Source_Ptr := Sloc (N);
1260 Lhs : constant Node_Id := Name (N);
1262 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1263 -- Used to preserve assignment OK status when assignment is rewritten
1265 Rhs : Node_Id := Expression (N);
1266 -- Initially Rhs is the right hand side value, it will be replaced
1267 -- later by an appropriate unchecked conversion for the assignment.
1269 Obj : Node_Id;
1270 Atyp : Entity_Id;
1271 PAT : Entity_Id;
1272 Ctyp : Entity_Id;
1273 Csiz : Int;
1274 Cmask : Uint;
1276 Shift : Node_Id;
1277 -- The expression for the shift value that is required
1279 Shift_Used : Boolean := False;
1280 -- Set True if Shift has been used in the generated code at least
1281 -- once, so that it must be duplicated if used again
1283 New_Lhs : Node_Id;
1284 New_Rhs : Node_Id;
1286 Rhs_Val_Known : Boolean;
1287 Rhs_Val : Uint;
1288 -- If the value of the right hand side as an integer constant is
1289 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1290 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1291 -- the Rhs_Val is undefined.
1293 function Get_Shift return Node_Id;
1294 -- Function used to get the value of Shift, making sure that it
1295 -- gets duplicated if the function is called more than once.
1297 ---------------
1298 -- Get_Shift --
1299 ---------------
1301 function Get_Shift return Node_Id is
1302 begin
1303 -- If we used the shift value already, then duplicate it. We
1304 -- set a temporary parent in case actions have to be inserted.
1306 if Shift_Used then
1307 Set_Parent (Shift, N);
1308 return Duplicate_Subexpr_No_Checks (Shift);
1310 -- If first time, use Shift unchanged, and set flag for first use
1312 else
1313 Shift_Used := True;
1314 return Shift;
1315 end if;
1316 end Get_Shift;
1318 -- Start of processing for Expand_Bit_Packed_Element_Set
1320 begin
1321 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1323 Obj := Relocate_Node (Prefix (Lhs));
1324 Convert_To_Actual_Subtype (Obj);
1325 Atyp := Etype (Obj);
1326 PAT := Packed_Array_Type (Atyp);
1327 Ctyp := Component_Type (Atyp);
1328 Csiz := UI_To_Int (Component_Size (Atyp));
1330 -- We convert the right hand side to the proper subtype to ensure
1331 -- that an appropriate range check is made (since the normal range
1332 -- check from assignment will be lost in the transformations). This
1333 -- conversion is analyzed immediately so that subsequent processing
1334 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1336 -- If the right-hand side is a string literal, create a temporary for
1337 -- it, constant-folding is not ready to wrap the bit representation
1338 -- of a string literal.
1340 if Nkind (Rhs) = N_String_Literal then
1341 declare
1342 Decl : Node_Id;
1343 begin
1344 Decl :=
1345 Make_Object_Declaration (Loc,
1346 Defining_Identifier =>
1347 Make_Defining_Identifier (Loc, New_Internal_Name ('T')),
1348 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1349 Expression => New_Copy_Tree (Rhs));
1351 Insert_Actions (N, New_List (Decl));
1352 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1353 end;
1354 end if;
1356 Rhs := Convert_To (Ctyp, Rhs);
1357 Set_Parent (Rhs, N);
1358 Analyze_And_Resolve (Rhs, Ctyp);
1360 -- Case of component size 1,2,4 or any component size for the modular
1361 -- case. These are the cases for which we can inline the code.
1363 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1364 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1365 then
1366 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1368 -- The statement to be generated is:
1370 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1372 -- where mask1 is obtained by shifting Cmask left Shift bits
1373 -- and then complementing the result.
1375 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1377 -- the "or ..." is omitted if rhs is constant and all 0 bits
1379 -- rhs is converted to the appropriate type
1381 -- The result is converted back to the array type, since
1382 -- otherwise we lose knowledge of the packed nature.
1384 -- Determine if right side is all 0 bits or all 1 bits
1386 if Compile_Time_Known_Value (Rhs) then
1387 Rhs_Val := Expr_Rep_Value (Rhs);
1388 Rhs_Val_Known := True;
1390 -- The following test catches the case of an unchecked conversion
1391 -- of an integer literal. This results from optimizing aggregates
1392 -- of packed types.
1394 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1395 and then Compile_Time_Known_Value (Expression (Rhs))
1396 then
1397 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1398 Rhs_Val_Known := True;
1400 else
1401 Rhs_Val := No_Uint;
1402 Rhs_Val_Known := False;
1403 end if;
1405 -- Some special checks for the case where the right hand value
1406 -- is known at compile time. Basically we have to take care of
1407 -- the implicit conversion to the subtype of the component object.
1409 if Rhs_Val_Known then
1411 -- If we have a biased component type then we must manually do
1412 -- the biasing, since we are taking responsibility in this case
1413 -- for constructing the exact bit pattern to be used.
1415 if Has_Biased_Representation (Ctyp) then
1416 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1417 end if;
1419 -- For a negative value, we manually convert the twos complement
1420 -- value to a corresponding unsigned value, so that the proper
1421 -- field width is maintained. If we did not do this, we would
1422 -- get too many leading sign bits later on.
1424 if Rhs_Val < 0 then
1425 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1426 end if;
1427 end if;
1429 New_Lhs := Duplicate_Subexpr (Obj, True);
1430 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1432 -- First we deal with the "and"
1434 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1435 declare
1436 Mask1 : Node_Id;
1437 Lit : Node_Id;
1439 begin
1440 if Compile_Time_Known_Value (Shift) then
1441 Mask1 :=
1442 Make_Integer_Literal (Loc,
1443 Modulus (Etype (Obj)) - 1 -
1444 (Cmask * (2 ** Expr_Value (Get_Shift))));
1445 Set_Print_In_Hex (Mask1);
1447 else
1448 Lit := Make_Integer_Literal (Loc, Cmask);
1449 Set_Print_In_Hex (Lit);
1450 Mask1 :=
1451 Make_Op_Not (Loc,
1452 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1453 end if;
1455 New_Rhs :=
1456 Make_Op_And (Loc,
1457 Left_Opnd => New_Rhs,
1458 Right_Opnd => Mask1);
1459 end;
1460 end if;
1462 -- Then deal with the "or"
1464 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1465 declare
1466 Or_Rhs : Node_Id;
1468 procedure Fixup_Rhs;
1469 -- Adjust Rhs by bias if biased representation for components
1470 -- or remove extraneous high order sign bits if signed.
1472 procedure Fixup_Rhs is
1473 Etyp : constant Entity_Id := Etype (Rhs);
1475 begin
1476 -- For biased case, do the required biasing by simply
1477 -- converting to the biased subtype (the conversion
1478 -- will generate the required bias).
1480 if Has_Biased_Representation (Ctyp) then
1481 Rhs := Convert_To (Ctyp, Rhs);
1483 -- For a signed integer type that is not biased, generate
1484 -- a conversion to unsigned to strip high order sign bits.
1486 elsif Is_Signed_Integer_Type (Ctyp) then
1487 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1488 end if;
1490 -- Set Etype, since it can be referenced before the
1491 -- node is completely analyzed.
1493 Set_Etype (Rhs, Etyp);
1495 -- We now need to do an unchecked conversion of the
1496 -- result to the target type, but it is important that
1497 -- this conversion be a right justified conversion and
1498 -- not a left justified conversion.
1500 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1502 end Fixup_Rhs;
1504 begin
1505 if Rhs_Val_Known
1506 and then Compile_Time_Known_Value (Get_Shift)
1507 then
1508 Or_Rhs :=
1509 Make_Integer_Literal (Loc,
1510 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1511 Set_Print_In_Hex (Or_Rhs);
1513 else
1514 -- We have to convert the right hand side to Etype (Obj).
1515 -- A special case case arises if what we have now is a Val
1516 -- attribute reference whose expression type is Etype (Obj).
1517 -- This happens for assignments of fields from the same
1518 -- array. In this case we get the required right hand side
1519 -- by simply removing the inner attribute reference.
1521 if Nkind (Rhs) = N_Attribute_Reference
1522 and then Attribute_Name (Rhs) = Name_Val
1523 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1524 then
1525 Rhs := Relocate_Node (First (Expressions (Rhs)));
1526 Fixup_Rhs;
1528 -- If the value of the right hand side is a known integer
1529 -- value, then just replace it by an untyped constant,
1530 -- which will be properly retyped when we analyze and
1531 -- resolve the expression.
1533 elsif Rhs_Val_Known then
1535 -- Note that Rhs_Val has already been normalized to
1536 -- be an unsigned value with the proper number of bits.
1538 Rhs :=
1539 Make_Integer_Literal (Loc, Rhs_Val);
1541 -- Otherwise we need an unchecked conversion
1543 else
1544 Fixup_Rhs;
1545 end if;
1547 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1548 end if;
1550 if Nkind (New_Rhs) = N_Op_And then
1551 Set_Paren_Count (New_Rhs, 1);
1552 end if;
1554 New_Rhs :=
1555 Make_Op_Or (Loc,
1556 Left_Opnd => New_Rhs,
1557 Right_Opnd => Or_Rhs);
1558 end;
1559 end if;
1561 -- Now do the rewrite
1563 Rewrite (N,
1564 Make_Assignment_Statement (Loc,
1565 Name => New_Lhs,
1566 Expression =>
1567 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1568 Set_Assignment_OK (Name (N), Ass_OK);
1570 -- All other component sizes for non-modular case
1572 else
1573 -- We generate
1575 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1577 -- where Subscr is the computed linear subscript
1579 declare
1580 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1581 Set_nn : Entity_Id;
1582 Subscr : Node_Id;
1583 Atyp : Entity_Id;
1585 begin
1586 if No (Bits_nn) then
1588 -- Error, most likely High_Integrity_Mode restriction
1590 return;
1591 end if;
1593 -- Acquire proper Set entity. We use the aligned or unaligned
1594 -- case as appropriate.
1596 if Known_Aligned_Enough (Obj, Csiz) then
1597 Set_nn := RTE (Set_Id (Csiz));
1598 else
1599 Set_nn := RTE (SetU_Id (Csiz));
1600 end if;
1602 -- Now generate the set reference
1604 Obj := Relocate_Node (Prefix (Lhs));
1605 Convert_To_Actual_Subtype (Obj);
1606 Atyp := Etype (Obj);
1607 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1609 -- Below we must make the assumption that Obj is
1610 -- at least byte aligned, since otherwise its address
1611 -- cannot be taken. The assumption holds since the
1612 -- only arrays that can be misaligned are small packed
1613 -- arrays which are implemented as a modular type, and
1614 -- that is not the case here.
1616 Rewrite (N,
1617 Make_Procedure_Call_Statement (Loc,
1618 Name => New_Occurrence_Of (Set_nn, Loc),
1619 Parameter_Associations => New_List (
1620 Make_Attribute_Reference (Loc,
1621 Attribute_Name => Name_Address,
1622 Prefix => Obj),
1623 Subscr,
1624 Unchecked_Convert_To (Bits_nn,
1625 Convert_To (Ctyp, Rhs)))));
1627 end;
1628 end if;
1630 Analyze (N, Suppress => All_Checks);
1631 end Expand_Bit_Packed_Element_Set;
1633 -------------------------------------
1634 -- Expand_Packed_Address_Reference --
1635 -------------------------------------
1637 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1638 Loc : constant Source_Ptr := Sloc (N);
1639 Ploc : Source_Ptr;
1640 Pref : Node_Id;
1641 Expr : Node_Id;
1642 Term : Node_Id;
1643 Atyp : Entity_Id;
1644 Subscr : Node_Id;
1646 begin
1647 Pref := Prefix (N);
1648 Expr := Empty;
1650 -- We build up an expression serially that has the form
1652 -- outer_object'Address
1653 -- + (linear-subscript * component_size for each array reference
1654 -- + field'Bit_Position for each record field
1655 -- + ...
1656 -- + ...) / Storage_Unit;
1658 -- Some additional conversions are required to deal with the addition
1659 -- operation, which is not normally visible to generated code.
1661 loop
1662 Ploc := Sloc (Pref);
1664 if Nkind (Pref) = N_Indexed_Component then
1665 Convert_To_Actual_Subtype (Prefix (Pref));
1666 Atyp := Etype (Prefix (Pref));
1667 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1669 Term :=
1670 Make_Op_Multiply (Ploc,
1671 Left_Opnd => Subscr,
1672 Right_Opnd =>
1673 Make_Attribute_Reference (Ploc,
1674 Prefix => New_Occurrence_Of (Atyp, Ploc),
1675 Attribute_Name => Name_Component_Size));
1677 elsif Nkind (Pref) = N_Selected_Component then
1678 Term :=
1679 Make_Attribute_Reference (Ploc,
1680 Prefix => Selector_Name (Pref),
1681 Attribute_Name => Name_Bit_Position);
1683 else
1684 exit;
1685 end if;
1687 Term := Convert_To (RTE (RE_Integer_Address), Term);
1689 if No (Expr) then
1690 Expr := Term;
1692 else
1693 Expr :=
1694 Make_Op_Add (Ploc,
1695 Left_Opnd => Expr,
1696 Right_Opnd => Term);
1697 end if;
1699 Pref := Prefix (Pref);
1700 end loop;
1702 Rewrite (N,
1703 Unchecked_Convert_To (RTE (RE_Address),
1704 Make_Op_Add (Loc,
1705 Left_Opnd =>
1706 Unchecked_Convert_To (RTE (RE_Integer_Address),
1707 Make_Attribute_Reference (Loc,
1708 Prefix => Pref,
1709 Attribute_Name => Name_Address)),
1711 Right_Opnd =>
1712 Make_Op_Divide (Loc,
1713 Left_Opnd => Expr,
1714 Right_Opnd =>
1715 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1717 Analyze_And_Resolve (N, RTE (RE_Address));
1718 end Expand_Packed_Address_Reference;
1720 ------------------------------------
1721 -- Expand_Packed_Boolean_Operator --
1722 ------------------------------------
1724 -- This routine expands "a op b" for the packed cases
1726 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1727 Loc : constant Source_Ptr := Sloc (N);
1728 Typ : constant Entity_Id := Etype (N);
1729 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1730 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1732 Ltyp : Entity_Id;
1733 Rtyp : Entity_Id;
1734 PAT : Entity_Id;
1736 begin
1737 Convert_To_Actual_Subtype (L);
1738 Convert_To_Actual_Subtype (R);
1740 Ensure_Defined (Etype (L), N);
1741 Ensure_Defined (Etype (R), N);
1743 Apply_Length_Check (R, Etype (L));
1745 Ltyp := Etype (L);
1746 Rtyp := Etype (R);
1748 -- First an odd and silly test. We explicitly check for the XOR
1749 -- case where the component type is True .. True, since this will
1750 -- raise constraint error. A special check is required since CE
1751 -- will not be required other wise (cf Expand_Packed_Not).
1753 -- No such check is required for AND and OR, since for both these
1754 -- cases False op False = False, and True op True = True.
1756 if Nkind (N) = N_Op_Xor then
1757 declare
1758 CT : constant Entity_Id := Component_Type (Rtyp);
1759 BT : constant Entity_Id := Base_Type (CT);
1761 begin
1762 Insert_Action (N,
1763 Make_Raise_Constraint_Error (Loc,
1764 Condition =>
1765 Make_Op_And (Loc,
1766 Left_Opnd =>
1767 Make_Op_Eq (Loc,
1768 Left_Opnd =>
1769 Make_Attribute_Reference (Loc,
1770 Prefix => New_Occurrence_Of (CT, Loc),
1771 Attribute_Name => Name_First),
1773 Right_Opnd =>
1774 Convert_To (BT,
1775 New_Occurrence_Of (Standard_True, Loc))),
1777 Right_Opnd =>
1778 Make_Op_Eq (Loc,
1779 Left_Opnd =>
1780 Make_Attribute_Reference (Loc,
1781 Prefix => New_Occurrence_Of (CT, Loc),
1782 Attribute_Name => Name_Last),
1784 Right_Opnd =>
1785 Convert_To (BT,
1786 New_Occurrence_Of (Standard_True, Loc)))),
1787 Reason => CE_Range_Check_Failed));
1788 end;
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 (Rtyp, P));
1827 end;
1829 -- For the array case, we insert the actions
1831 -- Result : Ltype;
1833 -- System.Bitops.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 :=
1853 Make_Defining_Identifier (Loc,
1854 Chars => New_Internal_Name ('T'));
1856 E_Id : RE_Id;
1858 begin
1859 if Nkind (N) = N_Op_And then
1860 E_Id := RE_Bit_And;
1862 elsif Nkind (N) = N_Op_Or then
1863 E_Id := RE_Bit_Or;
1865 else -- Nkind (N) = N_Op_Xor
1866 E_Id := RE_Bit_Xor;
1867 end if;
1869 Insert_Actions (N, New_List (
1871 Make_Object_Declaration (Loc,
1872 Defining_Identifier => Result_Ent,
1873 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1875 Make_Procedure_Call_Statement (Loc,
1876 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1877 Parameter_Associations => New_List (
1879 Make_Byte_Aligned_Attribute_Reference (Loc,
1880 Attribute_Name => Name_Address,
1881 Prefix => L),
1883 Make_Op_Multiply (Loc,
1884 Left_Opnd =>
1885 Make_Attribute_Reference (Loc,
1886 Prefix =>
1887 New_Occurrence_Of
1888 (Etype (First_Index (Ltyp)), Loc),
1889 Attribute_Name => Name_Range_Length),
1890 Right_Opnd =>
1891 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1893 Make_Byte_Aligned_Attribute_Reference (Loc,
1894 Attribute_Name => Name_Address,
1895 Prefix => R),
1897 Make_Op_Multiply (Loc,
1898 Left_Opnd =>
1899 Make_Attribute_Reference (Loc,
1900 Prefix =>
1901 New_Occurrence_Of
1902 (Etype (First_Index (Rtyp)), Loc),
1903 Attribute_Name => Name_Range_Length),
1904 Right_Opnd =>
1905 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1907 Make_Byte_Aligned_Attribute_Reference (Loc,
1908 Attribute_Name => Name_Address,
1909 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1911 Rewrite (N,
1912 New_Occurrence_Of (Result_Ent, Loc));
1913 end;
1914 end if;
1916 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1917 end Expand_Packed_Boolean_Operator;
1919 -------------------------------------
1920 -- Expand_Packed_Element_Reference --
1921 -------------------------------------
1923 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1924 Loc : constant Source_Ptr := Sloc (N);
1925 Obj : Node_Id;
1926 Atyp : Entity_Id;
1927 PAT : Entity_Id;
1928 Ctyp : Entity_Id;
1929 Csiz : Int;
1930 Shift : Node_Id;
1931 Cmask : Uint;
1932 Lit : Node_Id;
1933 Arg : Node_Id;
1935 begin
1936 -- If not bit packed, we have the enumeration case, which is easily
1937 -- dealt with (just adjust the subscripts of the indexed component)
1939 -- Note: this leaves the result as an indexed component, which is
1940 -- still a variable, so can be used in the assignment case, as is
1941 -- required in the enumeration case.
1943 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1944 Setup_Enumeration_Packed_Array_Reference (N);
1945 return;
1946 end if;
1948 -- Remaining processing is for the bit-packed case
1950 Obj := Relocate_Node (Prefix (N));
1951 Convert_To_Actual_Subtype (Obj);
1952 Atyp := Etype (Obj);
1953 PAT := Packed_Array_Type (Atyp);
1954 Ctyp := Component_Type (Atyp);
1955 Csiz := UI_To_Int (Component_Size (Atyp));
1957 -- Case of component size 1,2,4 or any component size for the modular
1958 -- case. These are the cases for which we can inline the code.
1960 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1961 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1962 then
1963 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1964 Lit := Make_Integer_Literal (Loc, Cmask);
1965 Set_Print_In_Hex (Lit);
1967 -- We generate a shift right to position the field, followed by a
1968 -- masking operation to extract the bit field, and we finally do an
1969 -- unchecked conversion to convert the result to the required target.
1971 -- Note that the unchecked conversion automatically deals with the
1972 -- bias if we are dealing with a biased representation. What will
1973 -- happen is that we temporarily generate the biased representation,
1974 -- but almost immediately that will be converted to the original
1975 -- unbiased component type, and the bias will disappear.
1977 Arg :=
1978 Make_Op_And (Loc,
1979 Left_Opnd => Make_Shift_Right (Obj, Shift),
1980 Right_Opnd => Lit);
1982 -- We neded to analyze this before we do the unchecked convert
1983 -- below, but we need it temporarily attached to the tree for
1984 -- this analysis (hence the temporary Set_Parent call).
1986 Set_Parent (Arg, Parent (N));
1987 Analyze_And_Resolve (Arg);
1989 Rewrite (N,
1990 RJ_Unchecked_Convert_To (Ctyp, Arg));
1992 -- All other component sizes for non-modular case
1994 else
1995 -- We generate
1997 -- Component_Type!(Get_nn (Arr'address, Subscr))
1999 -- where Subscr is the computed linear subscript
2001 declare
2002 Get_nn : Entity_Id;
2003 Subscr : Node_Id;
2005 begin
2006 -- Acquire proper Get entity. We use the aligned or unaligned
2007 -- case as appropriate.
2009 if Known_Aligned_Enough (Obj, Csiz) then
2010 Get_nn := RTE (Get_Id (Csiz));
2011 else
2012 Get_nn := RTE (GetU_Id (Csiz));
2013 end if;
2015 -- Now generate the get reference
2017 Compute_Linear_Subscript (Atyp, N, Subscr);
2019 -- Below we make the assumption that Obj is at least byte
2020 -- aligned, since otherwise its address cannot be taken.
2021 -- The assumption holds since the only arrays that can be
2022 -- misaligned are small packed arrays which are implemented
2023 -- as a modular type, and that is not the case here.
2025 Rewrite (N,
2026 Unchecked_Convert_To (Ctyp,
2027 Make_Function_Call (Loc,
2028 Name => New_Occurrence_Of (Get_nn, Loc),
2029 Parameter_Associations => New_List (
2030 Make_Attribute_Reference (Loc,
2031 Attribute_Name => Name_Address,
2032 Prefix => Obj),
2033 Subscr))));
2034 end;
2035 end if;
2037 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2039 end Expand_Packed_Element_Reference;
2041 ----------------------
2042 -- Expand_Packed_Eq --
2043 ----------------------
2045 -- Handles expansion of "=" on packed array types
2047 procedure Expand_Packed_Eq (N : Node_Id) is
2048 Loc : constant Source_Ptr := Sloc (N);
2049 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2050 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2052 LLexpr : Node_Id;
2053 RLexpr : Node_Id;
2055 Ltyp : Entity_Id;
2056 Rtyp : Entity_Id;
2057 PAT : Entity_Id;
2059 begin
2060 Convert_To_Actual_Subtype (L);
2061 Convert_To_Actual_Subtype (R);
2062 Ltyp := Underlying_Type (Etype (L));
2063 Rtyp := Underlying_Type (Etype (R));
2065 Convert_To_PAT_Type (L);
2066 Convert_To_PAT_Type (R);
2067 PAT := Etype (L);
2069 LLexpr :=
2070 Make_Op_Multiply (Loc,
2071 Left_Opnd =>
2072 Make_Attribute_Reference (Loc,
2073 Attribute_Name => Name_Length,
2074 Prefix => New_Occurrence_Of (Ltyp, Loc)),
2075 Right_Opnd =>
2076 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2078 RLexpr :=
2079 Make_Op_Multiply (Loc,
2080 Left_Opnd =>
2081 Make_Attribute_Reference (Loc,
2082 Attribute_Name => Name_Length,
2083 Prefix => New_Occurrence_Of (Rtyp, Loc)),
2084 Right_Opnd =>
2085 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2087 -- For the modular case, we transform the comparison to:
2089 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2091 -- where PAT is the packed array type. This works fine, since in the
2092 -- modular case we guarantee that the unused bits are always zeroes.
2093 -- We do have to compare the lengths because we could be comparing
2094 -- two different subtypes of the same base type.
2096 if Is_Modular_Integer_Type (PAT) then
2097 Rewrite (N,
2098 Make_And_Then (Loc,
2099 Left_Opnd =>
2100 Make_Op_Eq (Loc,
2101 Left_Opnd => LLexpr,
2102 Right_Opnd => RLexpr),
2104 Right_Opnd =>
2105 Make_Op_Eq (Loc,
2106 Left_Opnd => L,
2107 Right_Opnd => R)));
2109 -- For the non-modular case, we call a runtime routine
2111 -- System.Bit_Ops.Bit_Eq
2112 -- (L'Address, L_Length, R'Address, R_Length)
2114 -- where PAT is the packed array type, and the lengths are the lengths
2115 -- in bits of the original packed arrays. This routine takes care of
2116 -- not comparing the unused bits in the last byte.
2118 else
2119 Rewrite (N,
2120 Make_Function_Call (Loc,
2121 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2122 Parameter_Associations => New_List (
2123 Make_Byte_Aligned_Attribute_Reference (Loc,
2124 Attribute_Name => Name_Address,
2125 Prefix => L),
2127 LLexpr,
2129 Make_Byte_Aligned_Attribute_Reference (Loc,
2130 Attribute_Name => Name_Address,
2131 Prefix => R),
2133 RLexpr)));
2134 end if;
2136 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2137 end Expand_Packed_Eq;
2139 -----------------------
2140 -- Expand_Packed_Not --
2141 -----------------------
2143 -- Handles expansion of "not" on packed array types
2145 procedure Expand_Packed_Not (N : Node_Id) is
2146 Loc : constant Source_Ptr := Sloc (N);
2147 Typ : constant Entity_Id := Etype (N);
2148 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2150 Rtyp : Entity_Id;
2151 PAT : Entity_Id;
2152 Lit : Node_Id;
2154 begin
2155 Convert_To_Actual_Subtype (Opnd);
2156 Rtyp := Etype (Opnd);
2158 -- First an odd and silly test. We explicitly check for the case
2159 -- where the 'First of the component type is equal to the 'Last of
2160 -- this component type, and if this is the case, we make sure that
2161 -- constraint error is raised. The reason is that the NOT is bound
2162 -- to cause CE in this case, and we will not otherwise catch it.
2164 -- Believe it or not, this was reported as a bug. Note that nearly
2165 -- always, the test will evaluate statically to False, so the code
2166 -- will be statically removed, and no extra overhead caused.
2168 declare
2169 CT : constant Entity_Id := Component_Type (Rtyp);
2171 begin
2172 Insert_Action (N,
2173 Make_Raise_Constraint_Error (Loc,
2174 Condition =>
2175 Make_Op_Eq (Loc,
2176 Left_Opnd =>
2177 Make_Attribute_Reference (Loc,
2178 Prefix => New_Occurrence_Of (CT, Loc),
2179 Attribute_Name => Name_First),
2181 Right_Opnd =>
2182 Make_Attribute_Reference (Loc,
2183 Prefix => New_Occurrence_Of (CT, Loc),
2184 Attribute_Name => Name_Last)),
2185 Reason => CE_Range_Check_Failed));
2186 end;
2188 -- Now that that silliness is taken care of, get packed array type
2190 Convert_To_PAT_Type (Opnd);
2191 PAT := Etype (Opnd);
2193 -- For the case where the packed array type is a modular type,
2194 -- not A expands simply into:
2196 -- rtyp!(PAT!(A) xor mask)
2198 -- where PAT is the packed array type, and mask is a mask of all
2199 -- one bits of length equal to the size of this packed type and
2200 -- rtyp is the actual subtype of the operand
2202 Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2203 Set_Print_In_Hex (Lit);
2205 if not Is_Array_Type (PAT) then
2206 Rewrite (N,
2207 Unchecked_Convert_To (Rtyp,
2208 Make_Op_Xor (Loc,
2209 Left_Opnd => Opnd,
2210 Right_Opnd => Lit)));
2212 -- For the array case, we insert the actions
2214 -- Result : Typ;
2216 -- System.Bitops.Bit_Not
2217 -- (Opnd'Address,
2218 -- Typ'Length * Typ'Component_Size;
2219 -- Result'Address);
2221 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2222 -- argument is the length of the operand in bits. Then we replace
2223 -- the expression by a reference to Result.
2225 else
2226 declare
2227 Result_Ent : constant Entity_Id :=
2228 Make_Defining_Identifier (Loc,
2229 Chars => New_Internal_Name ('T'));
2231 begin
2232 Insert_Actions (N, New_List (
2234 Make_Object_Declaration (Loc,
2235 Defining_Identifier => Result_Ent,
2236 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2238 Make_Procedure_Call_Statement (Loc,
2239 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2240 Parameter_Associations => New_List (
2242 Make_Byte_Aligned_Attribute_Reference (Loc,
2243 Attribute_Name => Name_Address,
2244 Prefix => Opnd),
2246 Make_Op_Multiply (Loc,
2247 Left_Opnd =>
2248 Make_Attribute_Reference (Loc,
2249 Prefix =>
2250 New_Occurrence_Of
2251 (Etype (First_Index (Rtyp)), Loc),
2252 Attribute_Name => Name_Range_Length),
2253 Right_Opnd =>
2254 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2256 Make_Byte_Aligned_Attribute_Reference (Loc,
2257 Attribute_Name => Name_Address,
2258 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2260 Rewrite (N,
2261 New_Occurrence_Of (Result_Ent, Loc));
2262 end;
2263 end if;
2265 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2267 end Expand_Packed_Not;
2269 -------------------------------------
2270 -- Involves_Packed_Array_Reference --
2271 -------------------------------------
2273 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2274 begin
2275 if Nkind (N) = N_Indexed_Component
2276 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2277 then
2278 return True;
2280 elsif Nkind (N) = N_Selected_Component then
2281 return Involves_Packed_Array_Reference (Prefix (N));
2283 else
2284 return False;
2285 end if;
2286 end Involves_Packed_Array_Reference;
2288 --------------------------
2289 -- Known_Aligned_Enough --
2290 --------------------------
2292 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2293 Typ : constant Entity_Id := Etype (Obj);
2295 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2296 -- If the component is in a record that contains previous packed
2297 -- components, consider it unaligned because the back-end might
2298 -- choose to pack the rest of the record. Lead to less efficient code,
2299 -- but safer vis-a-vis of back-end choices.
2301 --------------------------------
2302 -- In_Partially_Packed_Record --
2303 --------------------------------
2305 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2306 Rec_Type : constant Entity_Id := Scope (Comp);
2307 Prev_Comp : Entity_Id;
2309 begin
2310 Prev_Comp := First_Entity (Rec_Type);
2311 while Present (Prev_Comp) loop
2312 if Is_Packed (Etype (Prev_Comp)) then
2313 return True;
2315 elsif Prev_Comp = Comp then
2316 return False;
2317 end if;
2319 Next_Entity (Prev_Comp);
2320 end loop;
2322 return False;
2323 end In_Partially_Packed_Record;
2325 -- Start of processing for Known_Aligned_Enough
2327 begin
2328 -- Odd bit sizes don't need alignment anyway
2330 if Csiz mod 2 = 1 then
2331 return True;
2333 -- If we have a specified alignment, see if it is sufficient, if not
2334 -- then we can't possibly be aligned enough in any case.
2336 elsif Known_Alignment (Etype (Obj)) then
2337 -- Alignment required is 4 if size is a multiple of 4, and
2338 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2340 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2341 return False;
2342 end if;
2343 end if;
2345 -- OK, alignment should be sufficient, if object is aligned
2347 -- If object is strictly aligned, then it is definitely aligned
2349 if Strict_Alignment (Typ) then
2350 return True;
2352 -- Case of subscripted array reference
2354 elsif Nkind (Obj) = N_Indexed_Component then
2356 -- If we have a pointer to an array, then this is definitely
2357 -- aligned, because pointers always point to aligned versions.
2359 if Is_Access_Type (Etype (Prefix (Obj))) then
2360 return True;
2362 -- Otherwise, go look at the prefix
2364 else
2365 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2366 end if;
2368 -- Case of record field
2370 elsif Nkind (Obj) = N_Selected_Component then
2372 -- What is significant here is whether the record type is packed
2374 if Is_Record_Type (Etype (Prefix (Obj)))
2375 and then Is_Packed (Etype (Prefix (Obj)))
2376 then
2377 return False;
2379 -- Or the component has a component clause which might cause
2380 -- the component to become unaligned (we can't tell if the
2381 -- backend is doing alignment computations).
2383 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2384 return False;
2386 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2387 return False;
2389 -- In all other cases, go look at prefix
2391 else
2392 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2393 end if;
2395 elsif Nkind (Obj) = N_Type_Conversion then
2396 return Known_Aligned_Enough (Expression (Obj), Csiz);
2398 -- For a formal parameter, it is safer to assume that it is not
2399 -- aligned, because the formal may be unconstrained while the actual
2400 -- is constrained. In this situation, a small constrained packed
2401 -- array, represented in modular form, may be unaligned.
2403 elsif Is_Entity_Name (Obj) then
2404 return not Is_Formal (Entity (Obj));
2405 else
2407 -- If none of the above, must be aligned
2408 return True;
2409 end if;
2410 end Known_Aligned_Enough;
2412 ---------------------
2413 -- Make_Shift_Left --
2414 ---------------------
2416 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2417 Nod : Node_Id;
2419 begin
2420 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2421 return N;
2422 else
2423 Nod :=
2424 Make_Op_Shift_Left (Sloc (N),
2425 Left_Opnd => N,
2426 Right_Opnd => S);
2427 Set_Shift_Count_OK (Nod, True);
2428 return Nod;
2429 end if;
2430 end Make_Shift_Left;
2432 ----------------------
2433 -- Make_Shift_Right --
2434 ----------------------
2436 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2437 Nod : Node_Id;
2439 begin
2440 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2441 return N;
2442 else
2443 Nod :=
2444 Make_Op_Shift_Right (Sloc (N),
2445 Left_Opnd => N,
2446 Right_Opnd => S);
2447 Set_Shift_Count_OK (Nod, True);
2448 return Nod;
2449 end if;
2450 end Make_Shift_Right;
2452 -----------------------------
2453 -- RJ_Unchecked_Convert_To --
2454 -----------------------------
2456 function RJ_Unchecked_Convert_To
2457 (Typ : Entity_Id;
2458 Expr : Node_Id) return Node_Id
2460 Source_Typ : constant Entity_Id := Etype (Expr);
2461 Target_Typ : constant Entity_Id := Typ;
2463 Src : Node_Id := Expr;
2465 Source_Siz : Nat;
2466 Target_Siz : Nat;
2468 begin
2469 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2470 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2472 -- First step, if the source type is not a discrete type, then we
2473 -- first convert to a modular type of the source length, since
2474 -- otherwise, on a big-endian machine, we get left-justification.
2475 -- We do it for little-endian machines as well, because there might
2476 -- be junk bits that are not cleared if the type is not numeric.
2478 if Source_Siz /= Target_Siz
2479 and then not Is_Discrete_Type (Source_Typ)
2480 then
2481 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2482 end if;
2484 -- In the big endian case, if the lengths of the two types differ,
2485 -- then we must worry about possible left justification in the
2486 -- conversion, and avoiding that is what this is all about.
2488 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2490 -- Next step. If the target is not a discrete type, then we first
2491 -- convert to a modular type of the target length, since
2492 -- otherwise, on a big-endian machine, we get left-justification.
2494 if not Is_Discrete_Type (Target_Typ) then
2495 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2496 end if;
2497 end if;
2499 -- And now we can do the final conversion to the target type
2501 return Unchecked_Convert_To (Target_Typ, Src);
2502 end RJ_Unchecked_Convert_To;
2504 ----------------------------------------------
2505 -- Setup_Enumeration_Packed_Array_Reference --
2506 ----------------------------------------------
2508 -- All we have to do here is to find the subscripts that correspond
2509 -- to the index positions that have non-standard enumeration types
2510 -- and insert a Pos attribute to get the proper subscript value.
2512 -- Finally the prefix must be uncheck converted to the corresponding
2513 -- packed array type.
2515 -- Note that the component type is unchanged, so we do not need to
2516 -- fiddle with the types (Gigi always automatically takes the packed
2517 -- array type if it is set, as it will be in this case).
2519 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2520 Pfx : constant Node_Id := Prefix (N);
2521 Typ : constant Entity_Id := Etype (N);
2522 Exprs : constant List_Id := Expressions (N);
2523 Expr : Node_Id;
2525 begin
2526 -- If the array is unconstrained, then we replace the array
2527 -- reference with its actual subtype. This actual subtype will
2528 -- have a packed array type with appropriate bounds.
2530 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2531 Convert_To_Actual_Subtype (Pfx);
2532 end if;
2534 Expr := First (Exprs);
2535 while Present (Expr) loop
2536 declare
2537 Loc : constant Source_Ptr := Sloc (Expr);
2538 Expr_Typ : constant Entity_Id := Etype (Expr);
2540 begin
2541 if Is_Enumeration_Type (Expr_Typ)
2542 and then Has_Non_Standard_Rep (Expr_Typ)
2543 then
2544 Rewrite (Expr,
2545 Make_Attribute_Reference (Loc,
2546 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2547 Attribute_Name => Name_Pos,
2548 Expressions => New_List (Relocate_Node (Expr))));
2549 Analyze_And_Resolve (Expr, Standard_Natural);
2550 end if;
2551 end;
2553 Next (Expr);
2554 end loop;
2556 Rewrite (N,
2557 Make_Indexed_Component (Sloc (N),
2558 Prefix =>
2559 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2560 Expressions => Exprs));
2562 Analyze_And_Resolve (N, Typ);
2564 end Setup_Enumeration_Packed_Array_Reference;
2566 -----------------------------------------
2567 -- Setup_Inline_Packed_Array_Reference --
2568 -----------------------------------------
2570 procedure Setup_Inline_Packed_Array_Reference
2571 (N : Node_Id;
2572 Atyp : Entity_Id;
2573 Obj : in out Node_Id;
2574 Cmask : out Uint;
2575 Shift : out Node_Id)
2577 Loc : constant Source_Ptr := Sloc (N);
2578 PAT : Entity_Id;
2579 Otyp : Entity_Id;
2580 Csiz : Uint;
2581 Osiz : Uint;
2583 begin
2584 Csiz := Component_Size (Atyp);
2586 Convert_To_PAT_Type (Obj);
2587 PAT := Etype (Obj);
2589 Cmask := 2 ** Csiz - 1;
2591 if Is_Array_Type (PAT) then
2592 Otyp := Component_Type (PAT);
2593 Osiz := Component_Size (PAT);
2595 else
2596 Otyp := PAT;
2598 -- In the case where the PAT is a modular type, we want the actual
2599 -- size in bits of the modular value we use. This is neither the
2600 -- Object_Size nor the Value_Size, either of which may have been
2601 -- reset to strange values, but rather the minimum size. Note that
2602 -- since this is a modular type with full range, the issue of
2603 -- biased representation does not arise.
2605 Osiz := UI_From_Int (Minimum_Size (Otyp));
2606 end if;
2608 Compute_Linear_Subscript (Atyp, N, Shift);
2610 -- If the component size is not 1, then the subscript must be
2611 -- multiplied by the component size to get the shift count.
2613 if Csiz /= 1 then
2614 Shift :=
2615 Make_Op_Multiply (Loc,
2616 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2617 Right_Opnd => Shift);
2618 end if;
2620 -- If we have the array case, then this shift count must be broken
2621 -- down into a byte subscript, and a shift within the byte.
2623 if Is_Array_Type (PAT) then
2625 declare
2626 New_Shift : Node_Id;
2628 begin
2629 -- We must analyze shift, since we will duplicate it
2631 Set_Parent (Shift, N);
2632 Analyze_And_Resolve
2633 (Shift, Standard_Integer, Suppress => All_Checks);
2635 -- The shift count within the word is
2636 -- shift mod Osiz
2638 New_Shift :=
2639 Make_Op_Mod (Loc,
2640 Left_Opnd => Duplicate_Subexpr (Shift),
2641 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2643 -- The subscript to be used on the PAT array is
2644 -- shift / Osiz
2646 Obj :=
2647 Make_Indexed_Component (Loc,
2648 Prefix => Obj,
2649 Expressions => New_List (
2650 Make_Op_Divide (Loc,
2651 Left_Opnd => Duplicate_Subexpr (Shift),
2652 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2654 Shift := New_Shift;
2655 end;
2657 -- For the modular integer case, the object to be manipulated is
2658 -- the entire array, so Obj is unchanged. Note that we will reset
2659 -- its type to PAT before returning to the caller.
2661 else
2662 null;
2663 end if;
2665 -- The one remaining step is to modify the shift count for the
2666 -- big-endian case. Consider the following example in a byte:
2668 -- xxxxxxxx bits of byte
2669 -- vvvvvvvv bits of value
2670 -- 33221100 little-endian numbering
2671 -- 00112233 big-endian numbering
2673 -- Here we have the case of 2-bit fields
2675 -- For the little-endian case, we already have the proper shift
2676 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2678 -- For the big endian case, we have to adjust the shift count,
2679 -- computing it as (N - F) - shift, where N is the number of bits
2680 -- in an element of the array used to implement the packed array,
2681 -- F is the number of bits in a source level array element, and
2682 -- shift is the count so far computed.
2684 if Bytes_Big_Endian then
2685 Shift :=
2686 Make_Op_Subtract (Loc,
2687 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2688 Right_Opnd => Shift);
2689 end if;
2691 Set_Parent (Shift, N);
2692 Set_Parent (Obj, N);
2693 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2694 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2696 -- Make sure final type of object is the appropriate packed type
2698 Set_Etype (Obj, Otyp);
2700 end Setup_Inline_Packed_Array_Reference;
2702 end Exp_Pakd;