Vyper Documentation

repository·master·Indexed 26 days ago

https://github.com/vyperlang/vyper

A contract-oriented, pythonic programming language for writing smart contracts focused on security, readability, and simplicity. The documentation covers the vyper CLI for compiling .vy files to bytecode and ABI, the vyper.ast module for AST generation and traversal, the vyper.compiler for managing compilation phases (AST, IR, Assembly, and Bytecode), and the vyper.semantics package for syntax verification and type checking. It also details VyperIR grammar, the vyper-ir tool, and the Venom SSA intermediate representation compiler.

Tokens
46K
Snippets
149
Records
304
Agent score
90%

What's inside Vyper

  1. Overview of Venom compiler passes

    master

    The Venom compiler uses a pluggable architecture of passes to transform and optimize IR. The primary categories are:

    • Transformation passes: Modify the IR structure (e.g., Normalization, Dataflow Transformation).
    • Analysis/augmentation passes: Generate supplementary information (e.g., Control Flow Graph (CFG) calculation, Data Flow Graph (DFG) calculation, Liveness analysis).
    • Optimization passes: Improve code efficiency (e.g., Dead code elimination).

    Critical Pass Requirements:

    • Normalization: Essential for code emission. The code emitter requires the IR to be in a 'normalized' form where basic blocks do not have multiple inputs and outputs, ensuring a well-defined stack layout.
    • Execution Order: Some passes depend on others. For example, the code emitter requires a normalization pass, which in turn requires the IR to be augmented with code flow information.
  2. Overview of Vyper language features

    master

    Vyper is a Pythonic smart contract language that compiles to Ethereum Virtual Machine (EVM) bytecode. It is designed with a focus on security, auditability, and simplicity. Key safety features include:

    • Safety by default: Bounds and overflow checking on array accesses and arithmetic, and strong typing with explicit type conversions.
    • Predictable execution: Decidable gas consumption, bounded loops (compile-time maximum iterations), and no recursion.
    • Clean code reuse: Uses module imports instead of class inheritance, explicit extcall and staticcall keywords for external interactions, and support for pure functions.
    • Decimal Fixed Point: Uses decimal fixed point numbers to ensure literals like 0.1 have exact representations, avoiding binary floating-point precision errors.
  3. Understand the Venom IR structure

    master

    Venom is an SSA-based (Static Single Assignment) intermediate representation for Vyper, designed for the stack-based EVM.

    Hierarchy: IRContext $\rightarrow$ IRFunction(s) $\rightarrow$ IRBasicBlock(s) $\rightarrow$ IRInstruction(s)

    Key Characteristics:

    • Variables: Prefixed with % and are immutable after assignment (SSA).
    • Basic Blocks: Non-branching sequences of instructions terminated by specific jump or return instructions (jmp, jnz, djmp, ret, return, stop, or exit).
    • Normalized Form: A constraint where no basic block possesses both multiple predecessors AND multiple successors.
  4. Understand VyperIR Grammar and Valency

    master

    VyperIR is a high-level assembly representation. Expressions in VyperIR have a "valency" of either 1 or 0:

    • Valency 1: The expression returns a stack item.
    • Valency 0: The expression does not return a stack item.

    The grammar follows an (s_expr) structure including INT_LITERAL, IDENTIFIER, EVM_OPCODE, PSEUDO_OPCODE, with, set, seq, if, and repeat.

  5. Understand Deep Verification in Vyper

    master

    Deep verification is the process of reducing the 'verification gap'—the distance between a formally proven property and the actual code executing on-chain. It aims to bridge multiple layers of the stack: high-level security properties, source semantics, compiler correctness, and EVM execution semantics.

    Key concepts include:

    • Verification depth: How far a proof carries through the stack (from source semantics down to the EVM model).
    • Verification breadth: The scope of behavior covered (functions, properties, and environment assumptions).

    Currently, Vyper supports source-level verification, and work is ongoing to complete the compiler verification layer to connect source semantics to deployed bytecode.

  6. Understand the Experimental Direct-to-Venom Pipeline

    master

    Vyper features an experimental code generation pipeline that bypasses the legacy IR. The flow is:

    1. AST: The Abstract Syntax Tree.
    2. vyper/codegen_venom/: Translates the annotated Vyper AST directly into Venom SSA IR.
    3. Venom IR: The intermediate representation.
    4. Optimization: Optimization passes are run on the Venom IR.
    5. Assembly: venom/venom_to_assembly.py emits EVM assembly.
    6. Bytecode: The assembler in vyper/evm/assembler/ produces the final bytecode.
  7. Understand the Vyper compilation pipeline

    master

    The default production pipeline follows these stages:

    1. Parsing: vyper/ast/ (Pre-parser $\rightarrow$ Python AST $\rightarrow$ Vyper AST).
    2. Semantics: vyper/semantics/ (Type checking, validation, and AST annotation).
    3. Codegen: vyper/codegen/ (AST $\rightarrow$ s-expr IR).
    4. IR/EVM: vyper/ir/ and vyper/evm/ (s-expr IR $\rightarrow$ assembly $\rightarrow$ bytecode).

    An experimental Venom path is available via the --experimental-codegen flag, which uses vyper/codegen_venom/ (AST $\rightarrow$ Venom SSA IR) and vyper/venom/ (Venom IR optimization $\rightarrow$ assembly).

  8. Understand the Vyper compilation pipeline

    master

    The Vyper compilation process follows this transformation sequence:

    Source $\rightarrow$ pre_parser $\rightarrow$ Python AST $\rightarrow$ Vyper AST $\rightarrow$ semantic analysis $\rightarrow$ annotated AST.

    • AST Phase: Handled by vyper/ast/. The parse_to_ast() function is the main entry point for converting source to the Vyper AST.
    • Semantic Analysis Phase: Handled by vyper/semantics/.
  9. Understand the Default Production Pipeline (Legacy IR)

    master

    The current default production pipeline uses a tree-shaped s-expression IR. The flow is:

    1. AST: The Abstract Syntax Tree.
    2. vyper/codegen/: Generates the s-expression IR (IRnode).
    3. vyper/ir/compile_ir.py: Compiles the IR.
    4. Assembly: Generates EVM assembly.
    5. Bytecode: Produces the final bytecode.

    Key Difference: Unlike the Venom pipeline which uses SSA basic blocks, the legacy IR uses tree-shaped s-expressions.

  10. Understand the Vyper compilation phases

    master

    The Vyper compilation process follows these stages:

    1. AST Generation: Source code is parsed into an abstract syntax tree via vyper.ast.
    2. IR Generation: Contextualized AST nodes are converted into Intermediate Representation (IR) nodes via vyper.codegen.module.
    3. Assembly Generation: IR nodes are converted into assembly instructions via vyper.compile_ir.
    4. Bytecode Generation: Assembly instructions are converted into EVM bytecode via vyper.compile_ir.

    Note that phases 3 and 4 can produce two distinct types of bytecode:

    • Deployment bytecode: Used for deploying the contract onto the blockchain.
    • Runtime bytecode: The actual on-chain code resulting from deployment.
  11. Understand Vyper test organization

    master

    The test suite is organized into unit and functional categories:

    • tests/unit/: Isolated component tests (e.g., ast, semantics, compiler/venom, cli).
    • tests/functional/: End-to-end compilation and execution tests (e.g., builtins, codegen, grammar, syntax, venom).
    • tests/conftest.py: Contains shared pytest fixtures.
    • tests/evm_backends/: Contains pluggable EVM execution backends.
    • tests/venom_utils.py: Contains helpers for Venom tests.
  12. Understand the purpose of vyper.semantics

    master
    The vyper.semantics package is responsible for performing syntax verification, type checking, and semantics analysis of a Vyper Abstract Syntax Tree (AST). It ensures that the contract code follows Vyper's rules regarding types, scopes, and operations.