Ada: Difference between revisions

Jump to navigation Jump to search
593 bytes added ,  4 years ago
[unchecked revision][unchecked revision]
Line 41:
=== Representation clauses ===
 
Ada allows for explicitly defining the in-memory representation of scalar and compound types. The following example demonstrates the definition of ana enumeratedrecord type and( aequivalent recordto typestructures in C ), as well as theirits associated representation in memory.
 
<source lang="ada">
----------------------------------------------------------------------------
-- The format of the System Table Descriptor pointer used by the processor
-- to load descriptor tables like the GDT and IDT.
----------------------------------------------------------------------------
type System_Table_Descriptor is
record
Size : Unsigned_16;
Offset : System.Address;
end record
with Size => 48;
for System_Table_Descriptor use
record
Size at 0 range 0 .. 15;
Offset at 0 range 16 .. 47;
end record;
</source>
 
The <tt>Size</tt> ''aspect specifier'' instructs the compiler that the <tt>System_Table_Descriptor</tt> type must be 48 bits in size. The ''record representation clause'' instructs the compiler as to the required layout of this record type in memory. This example specifies that the <tt>Size</tt> member should occupy bits 0 to 15, and the <tt>Offset</tt> member should occupy bits 16 to 47. This feature is analogous to C's bit-fields.
 
The following example demonstrates defining the in-memory representation of an enumerated type.
 
<source lang="ada">
Line 62 ⟶ 84:
Ring_3 => 3
);
 
----------------------------------------------------------------------------
-- The format of the System Table Descriptor pointer used by the processor
-- to load descriptor tables like the GDT and IDT.
----------------------------------------------------------------------------
type System_Table_Descriptor is
record
Size : Unsigned_16;
Offset : System.Address;
end record
with Size => 48;
for System_Table_Descriptor use
record
Size at 0 range 0 .. 15;
Offset at 0 range 16 .. 47;
end record;
</source>
 
The <tt>Size</tt> ''aspect specifier'' instructs the compiler that the <tt>System_Table_DescriptorDescriptor_Privilege_Level</tt> type must be 482 bits in size. The ''record representation clause'' instructs the compiler as to the required layoutrepresentation of thiseach possible value of the recordenumerated type in memory. ThisIn this example specifiesthe thatvalue theof <tt>SizeRing_0</tt> memberwill shouldbe occupyrepresented bitsby 0a tovalue 15, and theof <tt>Offset0x0</tt> memberin shouldmemory, occupythe bitsvalue 16of to<tt>Ring_1</tt> 47.will Thisbe featurerepresented isby analogous<tt>0x1</tt>, toand C'sso bit-fieldson.
 
==See Also==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu