Module move_binary_format::file_format
source · Expand description
Binary format for transactions and modules.
This module provides a simple Rust abstraction over the binary format. That is the format of modules stored on chain or the format of the code section of a transaction.
file_format_common.rs
provides the constant values for entities in the binary format.
(The binary format is evolving so please come back here in time to check evolutions.)
Overall the binary format is structured in a number of sections:
- Header: this must start at offset 0 in the binary. It contains a blob that starts every Diem binary, followed by the version of the VM used to compile the code, and last is the number of tables present in this binary.
- Table Specification: it’s a number of tuple of the form
(table type, starting_offset, byte_count)
. The number of entries is specified in the header (last entry in header). There can only be a single entry per table type. Thestarting offset
is from the beginning of the binary. Tables must cover the entire size of the binary blob and cannot overlap. - Table Content: the serialized form of the specific entries in the table. Those roughly map to the structs defined in this module. Entries in each table must be unique.
We have two formats: one for modules here represented by CompiledModule
, another
for transaction scripts which is CompiledScript
. Building those tables and passing them
to the serializer (serializer.rs
) generates a binary of the form described. Vectors in
those structs translate to tables and table specifications.
Structs
- A set of
Ability
s - Index into the
AddressIdentifier
table. - A
CodeUnit
is the body of a function. It has the function header and the instruction stream. - A
CompiledModule
defines the structure of a module which is the unit of published code. - Contains the main function to execute and its dependencies.
- A
Constant
is a serialized value along with its type. That type will be deserialized by the loader/evauluator - Index into the
ConstantPool
table. - A
FieldDefinition
is the definition of a field: its name and the field type. - A field access info (owner type and offset)
- Index into the
FieldHandle
table. - A complete or partial instantiation of a field (or the type of it).
- Index into the
FieldInstantiation
table. - A
FunctionDefinition
is the implementation of a function. It defines the prototype of the function and the function body. - Index into the
FunctionDefinition
table. - A
FunctionHandle
is a reference to a function. It is composed by aModuleHandle
and the name and signature of that function within the module. - Index into the
FunctionHandle
table. - A complete or partial instantiation of a function
- Index into the
FunctionInstantiation
table. - A
FunctionSignature
in internally used to create a unique representation of the overall signature as need. Consider deprecated… - Index into the
Identifier
table. - A
ModuleHandle
is a reference to a MOVE module. It is composed by anaddress
and aname
. - Index into the
ModuleHandle
table. - A
Signature
is the list of locals used by a function. - Index into the
Signature
table. - An iterator to help traverse the
SignatureToken
in a non-recursive fashion to avoid overflowing the stack. - Alternative preorder traversal iterator for SignatureToken that also returns the depth at each node.
- A complete or partial instantiation of a generic struct
- Index into the
StructInstantiation
table. - A
StructDefinition
is a type definition. It either indicates it is native or defines all the user-specified fields declared on the type. - Index into the
StructDefinition
table. - A
StructHandle
is a reference to a user defined type. It is composed by aModuleHandle
and the name of the type within that module. - Index into the
StructHandle
table. - A type parameter used in the declaration of a struct.
- A type definition.
SignatureToken
allows the definition of the set of known types and their composition.
Enums
- An
Ability
classifies what operations are permitted for a given type Bytecode
is a VM instruction of variable size. The type of the bytecode (opcode) defines the size of the bytecode.- A
SignatureToken
is a type declaration for a location. StructFieldInformation
indicates whether a struct is native or has user-specified fieldsVisibility
restricts the accessibility of the associated entity.
Constants
- Index 0 into the LocalsSignaturePool, which is guaranteed to be an empty list. Used to represent function/struct instantiation with no type arguments – effectively non-generic functions and structs.
Functions
- Create the following module which is convenient in tests: // module
{ // struct Bar { x: u64 } // // foo() { // } // } - Return the simplest module that will pass the bounds checker
- Return a simple script that contains only a return in the main()
Type Definitions
- The pool of address identifiers (addresses used in ModuleHandles/ModuleIds). Does not include runtime values. Those are placed in the
ConstantPool
- Index into the code stream for a jump. The offset is relative to the beginning of the instruction stream.
- The pool of
Constant
values - The pool of identifiers.
- Index of a local variable in a function.
- Max number of fields in a
StructDefinition
. - The pool of
Signature
instances. Every function definition must define the set of locals used and their types. - Generic index into one of the tables in the binary format.
- Type parameters are encoded as indices. This index can also be used to lookup the kind of a type parameter in the
FunctionHandle
andStructHandle
. - The pool of
TypeSignature
instances. Those are system and user types used and their composition (e.g. &U64).