# Help wanted with compilation error

**URL:** <https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178>\
**Category:** General\
**Tags:** embedded, ada\
**Created:** [September 7, 2024, 7:17pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178 "2024-09-07T19:17:45Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [September 7, 2024, 7:17pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/1 "2024-09-07T19:17:45Z")

</div>

I can’t see what I’m doing wrong here at line 41. Constant aggregates are best, but … is it something to do with array aggregates?

This will, I hope, be code for a RISC-V core in an RP2350 chip.

```ada
-*- mode: compilation; default-directory: "~/Developer/FreeRTOS-Ada/rp2350/adainclude/" -*-
Compilation started at Sat Sep 7 20:10:13

gnatmake problem.adb -gnatl
gcc -c -gnatl problem.adb

GNAT 14.2.0
Copyright 1992-2024, Free Software Foundation, Inc.

Compiling: problem.adb
Source file time stamp: 2024-09-07 19:09:06
Compiled at: 2024-09-07 20:10:13

     1. with System.Machine_Code;
     2.
     3. procedure Problem is
     4.
     5. package RP2350 is
     6. type Bit is mod 2**1
     7. with Size => 1;
     8. type UInt5 is mod 2**5
     9. with Size => 5;
    10. type UInt11 is mod 2**11
    11. with Size => 11;
    12. end RP2350;
    13. use RP2350;
    14.
    15. type Interrupt_ID is range 0 .. 45;
    16.
    17. procedure Enable_Interrupt (ID : Interrupt_ID);
    18.
    19. -- This type is subtyped for MEIEA, MEIPA, MEIFA.
    20.
    21. type Bit_Window_Content is array (0 .. 15) of Boolean
    22. with Pack, Size => 16;
    23. type CSR_Bit_Data is record
    24. INDEX : RP2350.UInt5 := 0;
    25. Reserved : RP2350.UInt11 := 0;
    26. WINDOW : Bit_Window_Content := (others => False);
    27. end record
    28. with Size => 32;
    29.
    30. for CSR_Bit_Data use record
    31. INDEX at 0 range 0 .. 4;
    32. Reserved at 0 range 5 .. 15;
    33. WINDOW at 0 range 16 .. 31;
    34. end record;
    35.
    36. subtype MEIEA_Data is CSR_Bit_Data;
    37.
    38. procedure Enable_Interrupt (ID : Interrupt_ID)
    39. is
    40. ID_In_Window : constant Integer := Integer (ID mod 16);
    41. Window : constant Bit_Window_Content := (ID_In_Window => True,
                                                       |
        >>> error: dynamic or empty choice in aggregate must be the only choice

    42. others => False);
    43. Data : constant MEIEA_Data := (INDEX => UInt5 (ID / 16),
    44. WINDOW => Window,
    45. others => <>);
    46. begin
    47. System.Machine_Code.Asm
    48. ("csrrs meinext, %0",
    49. Inputs => MEIEA_Data'Asm_Input
    50. ("r", Data),
    51. Volatile => True);
    52. end Enable_Interrupt;
    53.
    54. begin
    55. null;
    56. end Problem;

 56 lines: 1 error
gnatmake: "problem.adb" compilation error

Compilation exited abnormally with code 4 at Sat Sep 7 20:10:13

```

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [September 7, 2024, 7:41pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/2 "2024-09-07T19:41:17Z")

</div>

I think it’s choking on the fact that Integer can be outside `0..15`, even though you have that taken care of with the **mod 16**.

Could you quickly add/alter so that:

```ada
   type Nybble is range 0..15 with Size => 4;
   type Bit_Window_Content is array ( Nybble ) of Boolean
        with Pack, Size => 16;
--…
procedure Enable_Interrupt (ID : Interrupt_ID) is
   ID_In_Window : constant Nybble := Nybble (ID mod 16);
   Window : constant Bit_Window_Content := (ID_In_Window => True, others => False);
--…

```

---

<div class="post-metadata">

**Author:** ![godunko](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/godunko/32/22_2.png) [@godunko](https://forum.ada-lang.io/u/godunko)\
**Post date:** [September 7, 2024, 7:58pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/3 "2024-09-07T19:58:48Z")

</div>

Value of the `ID_In_Window` doesn’t known at compilation time.

---

<div class="post-metadata">

**Author:** ![DrPi](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/drpi/32/225_2.png) [@DrPi](https://forum.ada-lang.io/u/DrPi)\
**Post date:** [September 7, 2024, 8:26pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/4 "2024-09-07T20:26:34Z")

</div>

Have you tried this ?

```ada
Window : constant Bit_Window_Content := Bit_Window_Content'(ID_In_Window => True, others => False);

```

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [September 7, 2024, 8:35pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/5 "2024-09-07T20:35:05Z")

</div>

> [@godunko](#):
>
> Value of the `ID_In_Window` doesn’t known at compilation time.

That shouldn’t matter.

```ada
Declare
   Text : Constant String:= Ada.Text_IO.Get_Line;
Begin
   --...
End;

```

This is perfectly valid… so it shouldn’t be that there is runtime/unknown indefiniteness, in and of itself.

> [@DrPi](#):
>
> ```ada
> Window : constant Bit_Window_Content := Bit_Window_Content'(ID_In_Window => True, others => False);
> 
> ```

Hm, qualification is a good idea; try:

```ada
procedure Enable_Interrupt (ID : Interrupt_ID) is
   ID_In_Window : Nybble renames Nybble(ID mod 16);
   Window : Bit_Window_Content renames 
        Bit_Window_Content'(ID_In_Window => True, others => False);

```

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [September 7, 2024, 8:57pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/6 "2024-09-07T20:57:06Z")

</div>

Many suggestions, still puzzling!

```ada
    40. ID_In_Window : constant Integer := Integer (ID mod 16);
    41. Window : constant Bit_Window_Content
    42. := Bit_Window_Content'(ID_In_Window => True,
                                       |
        >>> error: dynamic or empty choice in aggregate must be the only choice

    43. others => False);

```

I tried removing the `constant` … no joy.

This goes against the grain, but compiles OK, so I’ll leave determining whether there’s an error to be reported to another day! Thanks for all the suggestions.

```ada
      Data : MEIEA_Data;
   begin
      Data.INDEX := RP2350.UInt5 (ID / 16);
      Data.WINDOW (Integer (ID mod 16)) := True;

```

Of course, given the very short scope of this procedure, the generated code may well be just the same as would have been produced by the first try had it worked.

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [September 7, 2024, 9:11pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/7 "2024-09-07T21:11:55Z")

</div>

I think the way this hardware works (it’s RaspberryPi’s Xh3irq interrupt controller extension in the RISC-V half of the RP2350) needs a little bit more explanation by way of comments in the code.

Oh, and the CSR should have been `meiea`. I suppose you have to expect assembler mnemonics to be obscure.

---

<div class="post-metadata">

**Author:** ![kevlar700](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/kevlar700/32/1118_2.png) [@kevlar700](https://forum.ada-lang.io/u/kevlar700)\
**Post date:** [September 7, 2024, 9:50pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/8 "2024-09-07T21:50:23Z")

</div>

You might try moving it after begin and add a declare block for Data. It would have to lose the constant property though.

---

<div class="post-metadata">

**Author:** ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)\
**Post date:** [September 7, 2024, 11:51pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/9 "2024-09-07T23:51:55Z")

</div>

Will this work as a work-around?:

```Ada
Window : constant Bit_Window_Content := 
        (for I in 0 .. 15 => (if I = ID_In_Window then True else False));

```

I tried it in jdoodle and got it to compile on GNAT 13.2.

I don’t know if it is a bug or intended. I looked at section 4.3.3 of the RM (ref below) but coudln’t make a solid decision on if your code is allowed or not.

jdoodle example: [Online Compiler and Editor/IDE for Java, C, C++, PHP, Python, Ruby, Perl - Code and Run Online](https://www.jdoodle.com/ia/1gfD)

RM Section: [Array Aggregates](http://www.ada-auth.org/standards/22rm/html/RM-4-3-3.html)

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [September 8, 2024, 4:18am UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/10 "2024-09-08T04:18:54Z")

</div>

> [@jere](#):
>
> ```ada
> Window : constant Bit_Window_Content := 
> (for I in 0 .. 15 => I = ID_In_Window);
> 
> ```

I fixed that for you.

---

<div class="post-metadata">

**Author:** ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)\
**Post date:** [September 8, 2024, 4:41am UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/11 "2024-09-08T04:41:55Z")

</div>

I think I prefer the more wordy way in this case. Helps me see everything better with how the words are colored.

---

<div class="post-metadata">

**Author:** ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)\
**Post date:** [September 8, 2024, 10:33am UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/12 "2024-09-08T10:33:22Z")

</div>

This is required by the language; see [ARM 4.3.3(17/3)](http://www.ada-auth.org/standards/aarm12_w_tc1/html/AA-4-3-3.html):

> The discrete\_choice\_list of an array\_component\_association is allowed to have a discrete\_choice that is a nonstatic choice\_expression or that is a subtype\_indication or range that defines a nonstatic or null range, only if it is the single discrete\_choice of its discrete\_choice\_list, and there is only one array\_component\_association in the array\_aggregate.

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [September 8, 2024, 11:49am UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/13 "2024-09-08T11:49:29Z")

</div>

Well, thanks for that, but I don’t see what I’m doing wrong. This seems an extraordinary limitation.

---

<div class="post-metadata">

**Author:** ![kevlar700](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/kevlar700/32/1118_2.png) [@kevlar700](https://forum.ada-lang.io/u/kevlar700)\
**Post date:** [September 8, 2024, 1:36pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/14 "2024-09-08T13:36:13Z")

</div>

Isn’t it that you have to drop the others? I wonder if a default\_component\_value would work. If that’s the case then I think I have thought it strange too.

---

<div class="post-metadata">

**Author:** ![sttaft](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/sttaft/32/12_2.png) [@sttaft](https://forum.ada-lang.io/u/sttaft)\
**Post date:** [September 8, 2024, 3:23pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/15 "2024-09-08T15:23:14Z")

</div>

@JC001 is correct. if you have a dynamic choice in an array aggregate, it must be the only component association in the aggregate. On the other hand, array aggregates can now be specified using “`for X in ... =>`” and this gives you a lot of flexibility. Several folks suggested this, the simplest being the one from @OneWingedShark (slightly adjusted here):

```ada
  Window : constant Bit_Window_Content :=
    (for I in Bit_Window_Content'Range => I = ID_In_Window)

```

I agree the rule given in RM 4.3.3(17) seems kind of arbitrary, but it is one of those rules that has been there forever, and no one has bothered to wonder whether it could be relaxed. In fact, the Ada 83 rule was slightly more limiting, and so the current rule (which dates from Ada 95) was a slight relaxation on that one. Feel free to make a suggestion on the [ARG GitHub issues list](https://github.com/Ada-Rapporteur-Group/User-Community-Input/issues) that this rule be relaxed further …

---

<div class="post-metadata">

**Author:** ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)\
**Post date:** [September 9, 2024, 10:46am UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/16 "2024-09-09T10:46:21Z")

</div>

> [@simonjwright](#):
>
> I don’t see what I’m doing wrong.

What you’re doing wrong is violating a language rule. Whether the rule is necessary or could be relaxed is a different question.

For a one-dimensional array, I see two options:

```ada
procedure Non_Static_Agg is
   type I16 is range 0 .. 15;
   type List is array (I16) of Boolean;

   procedure Try (Index : in I16);

   procedure Try (Index : in I16) is
      A : constant List := (List'First .. Index - 1 => False) & True & (Index + 1 .. List'Last => False);

      B : List := (others => False);
   begin -- Try
      B (Index) := True;
   end Try;
begin -- Non_Static_Agg
   null;
end Non_Static_Agg;

```

The rule may be related to the rule for discriminant values in record aggregates. Many people are confused by

```ada
type Disc is range 1 .. 4;

type Rec (D : Disc) is record
   case D is
   when 1 .. 2 =>
      A : Integer;
   when 3 .. 4 =>
      B : Float;
   end case;
end record;
...
function New_Rec (D : in Disc) return Rec is
begin
   case D is
   when 1 .. 2 =>
      return (D => D, A => -3);
...

```

being illegal. The value for the discriminant must be static. Instead, one can use

```ada
   when 1 .. 2 =>
      declare
         R : Rec (D => D);
      begin
         R.A := -3;

         return R;
      end;

```

(an extended return can also be used). This is similar to the use of B in the array example.

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [September 14, 2024, 1:12pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/17 "2024-09-14T13:12:54Z")

</div>

> [@simonjwright](#):
>
> I can’t see what I’m doing wrong here

I see I managed it in [2016](https://stackoverflow.com/questions/36143022/record-aggregate-with-dynamic-choice).

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [September 14, 2024, 3:43pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/18 "2024-09-14T15:43:25Z")

</div>

> [@simonjwright](#):
>
> I see I managed it in [2016](https://stackoverflow.com/questions/36143022/record-aggregate-with-dynamic-choice).

If you still have that odious “they declare each register with its own array-type” then use a `generic`.

```ada
-- Assuming: Type Bit is new Boolean with Size => 1;
Generic
   Type Index is (<>);
   Type Bit_Vector is array (Index range <>) of Bit;
Function Value( Input : Index ) return Bit_Vector;

Function Value( Input : Index ) return Bit_Vector is
Begin
   Return Result : Bit_Vector:= (Index'Range => False) do
      Result(Input) := True;
   End return;
End Value;

```

If you’re having to deal with anonymous types (e.g. ` A : Array (1..8) of Bit;`) the easiest way might be overlays+generics.

If it’s just something as cumbersome as `Type AX_Type is...` followed by the variables, then I would use generics+overloading to provide a usable/consistent interface:

```ada
-- Gven:
Bit_Width : Constant 32;
Type Generic_Register of Array(1..Bit_Width) of Bit;
Type Register_AX is new Generic_Register;
Type Register_BX is new Generic_Register;

AX : Register_AX;
BX : Register_BX;

-- Use something like:

Type Bit_Index is range 1..Bit_Width; -- Mirroring the register's.

Generic
   Type Register_Type is Array(1..Bit_Width) of Bit;
   R : in out Register;
Procedure Set_Bit( X : Bit_Index );
Procedure Set_Bit( X : Bit_Index ) is
   Actual_Index : Constant := R'First + (X-Bit_Index'First);
Begin
   R( Actual_Index ):= True;
End Set_Bit;
--...same for unset;

```

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [September 14, 2024, 8:58pm UTC](https://forum.ada-lang.io/t/help-wanted-with-compilation-error/1178/19 "2024-09-14T20:58:24Z")

</div>

I ended up declaring a blank (but preset) data object, then filling in the blanks.

```ada
      Data : MEIEA_Data;
   begin
      Data.INDEX := UInt5 (ID / 16);
      Data.WINDOW (Integer (ID mod 16)) := True;

```

There’s a lot to be said for simple.

Thanks to everyone for all the suggestions.
