29.1 Runtime representation
Any type constructor that the SML# compiler manages has its “size” on memory, “tag” indicating whether or not GC traces, and “representation” indicating the range and semantics of bits. We refer to the triple of these as the runtime representation of a type constructor. The runtime representation of a type is determined by its outermost type constructor. The following are runtime representations of built-in types.
built-in type constructor | size (in bytes) | tag | representation |
---|---|---|---|
int, int32 | 4 | 0 | signed integer |
int8 | 1 | 0 | signed integer |
int16 | 2 | 0 | signed integer |
int64 | 8 | 0 | signed integer |
word, word32 | 4 | 0 | unsigned integer |
word8 | 1 | 0 | unsigned integer |
word16 | 2 | 0 | unsigned integer |
word64 | 8 | 0 | unsigned integer |
char | 1 | 0 | unspecified |
real, real64 | 8 | 0 | IEEE754 floating point number |
real32 | 4 | 0 | IEEE754 floating point number |
ptr | 4 or 8 | 0 | void* of C |
codeptr | 4 or 8 | 0 | function pointer of C |
unit | 4 | 0 | unique variant |
contag | 4 | 0 | unspecified |
boxed | 4 or 8 | 1 | unspecified |
array | 4 or 8 | 1 | non-null pointer |
vector | 4 or 8 | 1 | non-null pointer |
ref | 4 or 8 | 1 | non-null pointer |
string | 4 or 8 | 1 | non-null pointer |
exn | 4 or 8 | 1 | non-null pointer |
record type, tuple type | 4 or 8 | 1 | non-null pointer |
function type | 4 or 8 | 1 | non-null pointer |
The size of the type whose size is “4 or 8” depends on whether the target platform is 32-bit or 64-bit.
For any type constructors, their alignments are equal to their size. The alignments should be equal to C’s alignments, but current SML# compiler gives alignments constantly regardless of target platform information (this restriction will be refined in the future version of SML#).
The runtime representation of user-defined types defined by datatype declaration is calculated from the set of data constructors as follows:
-
•
If the type has only one data constructor without argument, the runtime representation is equal to the unit type.
-
•
If the type has two or more data constructor, each of which has no argument, the runtime representation is euqal to the contag type.
-
•
Otherwise, the runtime representation is equal to the boxed type.
When computing runtime represenatation, the alias type defined by type declaration are all expanded. The runtime representation of opaque types introduced by opaque signature constraints is equal to that of the implementation type of the opaque type. The runtime representation of the types declared as opaque types by an interface file is equal to that of the implementation type given by its opaque type declaration in the interface file.