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