Program Compilation - Algorand Specifications

Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Algorand Specifications

A TEAL program is compiled using the POST /v2/teal/compile endpoint of algod node (go-algorand reference implementation).

See the algod node API non-normative section for further details.

The node begins by decoding the TEAL source code and converting it into AVM bytecode using the internal assemble function.

Important

IMPLEMENTATION:

Assembler reference implementation.

The following diagram outlines the steps involved in TEAL assembly:

Loop

Next line

Check: version and non-empty program

Read TEAL program

Get lexical tokens from line

Check Statement

Settle Version and prepare PseudoOps

Check Labels

Assemble instruction

Optimize intcblock and bytecblock

Resolve labels

Return Assembled Stream

The assembly process begins with two initial checks:

For a complete list of all available opcodes by versions, refer to the TEAL normative section

If no version is declared, the assembler uses a placeholder (assemblerNoVersion) that is later replaced with the default compiler version or one specified by a #pragma directive.

Then the assembler excludes empty strings (as they are not valid in TEAL).

Next, the assembler reads the program line by line and performs the following steps:

Once all statements are parsed, the assembler optimizes constant blocks to reduce the program size:

Label targets are resolved into relative byte offsets (2-bytes), pointing from the end of the current instruction to the target.

After assembling the program, the resulting bytecode buffer is hashed. The algod API response includes both the assembled bytecode and its hash, completing the compilation process.