Skip to content

Comment on Writing Linux Modules in Ada (2016)parent

Comments

Representation clauses are by far the biggest feature for embedded programming:

https://learn.adacore.com/courses/advanced-ada/parts/data_ty...

http://www.ada-auth.org/standards/22rm/html/RM-13-1.html

Wouldn't it be nice in C to be able to define how a struct is laid out in the machine representation? In Ada, you can and it is part of the standard, so it is portable:

https://learn.adacore.com/courses/advanced-ada/parts/data_ty...

It's difficult for someone with 0 knowledge of the language to really understand representation clauses. It does seem to be similar to enum values in C++?

    // Ada
    for Day use (Mon => 2#00000001#,
                 Tue => 2#00000010#,
                 Wed => 2#00000100#,
                 Thu => 2#00001000#,
                 Fri => 2#00010000#,
                 Sat => 2#00100000#,
                 Sun => 2#01000000#);
    // C++
    enum Day {
        Mon = 0b00000001,
        Tue = 0b00000010,
        Wed = 0b00000100,
        Thu = 0b00001000,
        Fri = 0b00010000,
        Sat = 0b00100000,
        Sun = 0b01000000,
    };
As for the record representation, my understanding is that it is equivalent to having a normalized __attribute__((__packed__)), where smart compiler padding is disabled and you can arbitrarily decide the memory layout of your struct?

Representation clause is just language defined way of how struct (Record in Ada) Enum or Array are actually laid out in memory. So, for example, you can define a struct that represents a register, overlay that register address and use it like this:

  procedure Enable_USB_Clock is
  begin
     Registers.PMC_Periph.PMC_SCER.USBCLK := 1;
  end Enable_USB_Clock;
Example taken from here: https://learn.adacore.com/courses/Ada_For_The_Embedded_C_Dev...
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.