Class Bap_c_size.base

The base class for computing sizes and aligments of C data types.

The algorithm is implemented as a class to allow a particular implementation to fine tune the calculation.

The model argument defines the default sizes for integral data types. If no suitable model is available for your architecture then use the closest model and override the specific methods to fine-tune the data model of your target.

The entry methods are bits and aligment.

Example

For example, let's compute the size of the

      struct foo {
         char v1;
         int  v2;
         char v3;
      };

Using the LP64 data model, in which integers are 32 bit long and char is 8 bit. The size of the structure is 12 bytes, due to the 3 bytes of padding before v2 and six bytes of trailing padding.

# let size = new C.Size.base `LP64;;
# size#bits C.Type.(structure "foo" [
    "v1", basic `char;
    "v2", basic `uint;
    "v3", basic `char
  ]);;
- : C.Size.bits option = Some 96
method bits : Bap_c_type.t -> bits option

returns a size of the data type representation in bits.

For incomplete types returns None. The size is always a multitude of the data type alignment and includes the paddings necessary for preserving the alignment restrictions.

  • since 2.5.0 the size is a multitude of the alignment.
method alignment : Bap_c_type.t -> Bap.Std.size

alignment t the alignment of data type t.

The alignment of

  • void or an incomplete type is 8;
  • a scalar is sizeof(t);
  • an array is the alignment its element;
  • a function pointer is sizeof the pointer;
  • a structure or a union is the largest of the element's alignments.
method padding : Bap_c_type.t -> bits -> Bap.Std.size option

DEPRECATED. Use the padding function if you need to compute padding.

  • deprecated since [2021-05] this method is ignored

array spec if array spec is complete, i.e., the number of elements is known, then returns a product of the array size and the array's element type in bits, otherwise returns None

if spec is complete then returns a size in bits of the biggest element, including the padding between fields, but excludeing the trailing padding.

if spec is complete then returns a total size of the structure, including the padding between fields, but excluding the trailing padding.

method integer : Bap_c_type.integer -> Bap.Std.size

the size of intergral types.

method pointer : Bap.Std.addr_size

the size of a pointer.

method enum : (string * int64 option) list -> Bap.Std.size

the size of the enumeration.

method real : Bap_c_type.real -> [ `r32 | `r64 | `r128 ]

the size of a real floating-point data type.

method complex : Bap_c_type.complex -> Bap.Std.size

the size of a complex floating-point data type.

method floating : Bap_c_type.floating -> Bap.Std.size

the size of a floating-point data type.

method basic : Bap_c_type.basic -> Bap.Std.size

the size of a basic data type.

method scalar : Bap_c_type.scalar -> Bap.Std.size

the size of a scalar data type.