swamp-code-gen

Crates.ioswamp-code-gen
lib.rsswamp-code-gen
version0.2.27
created_at2025-03-29 15:45:22.718736+00
updated_at2025-09-22 09:24:20.708229+00
descriptiongenerates swamp-vm opcodes
homepage
repositoryhttps://github.com/swamp/swamp
max_upload_size
id1611410
size458,028
Peter Bjorklund (piot)

documentation

README

Swamp Code Generator

Translates the type-checked, resolved Abstract Semantic Graph (ASG) from swamp-semantic into executable bytecode (BinaryInstruction from swamp-vm-types) for the Swamp Virtual Machine (VM).

Overview

This crate represents the code generation phase of the Swamp compiler pipeline. It takes the high-level, semantically rich representation of the program (where types are known, names are resolved, etc.) and lowers it into a sequence of simple, low-level instructions that the Swamp VM can directly execute.

Key Responsibilities

  • Instruction Selection: Chooses the appropriate VM opcodes (swamp_vm_types::opcode::OpCode) for each operation in the semantic tree (e.g., selecting AddI32 for an integer addition).
  • Memory Management (Frame): Assigns stack frame memory addresses ( swamp_vm_types::FrameMemoryAddress) for local variables, function arguments, and temporary intermediate values.
  • Control Flow Generation: Translates high-level control flow structures (like if, match, loops) into sequences of conditional and unconditional jump instructions (Bz, Bnz, Jmp).
  • Function Call Handling: Generates the necessary instructions for function calls (Call, Enter, Ret), including setting up arguments and handling return values according to the VM's calling convention.
  • Constant Pool Interaction: Places literal values (integers, strings, etc.) into a constant pool (managed externally or internally) and generates instructions (LdConst, StringFromConstantSlice) to load them into registers or memory at runtime.
  • Lowering Complex Operations: Breaks down complex semantic operations (e.g., struct instantiation, list manipulation) into sequences of simpler VM instructions.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Copyright

Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp

Commit count: 802

cargo fmt