Skip to content

Latest commit

 

History

History
158 lines (116 loc) · 5.07 KB

Compile.md

File metadata and controls

158 lines (116 loc) · 5.07 KB

Compile Mode Quickstart

This guide walks through compiling a FerroPhase project to a native target using the intended scheduler-based compiler design. See docs/Design.md, docs/Compiler.md, and docs/ConstEval.md for the architecture.

Prerequisites

  • Rust toolchain (stable) for building fp binaries
  • LLVM toolchain (llc + clang or compatible) available on PATH
  • fp CLI built from crates/fp-cli

Check availability:

$ fp --version
$ fp compile --help

Project Setup

A project is a directory containing a Magnet.toml package manifest and its src/ sources:

$ mkdir my_project && cd my_project

Project layout:

my_project/
├── src/
│   └── main.fp
├── FerroPhase.toml
└── README.md

Writing Code

Example src/main.fp demonstrating comptime generation, quoting, and a simple entry point:

const API_NAME: str = "FerroPhase";

const GREETING = quote item {
    fn message() -> str {
        "Hello from quote/splice!"
    }
};

const GENERATED: () = splice GREETING;

fn main() {
    println!("Welcome to {}", API_NAME);
    println!("{}", message());
}

Highlights:

  • API_NAME is answered by comptime work.
  • quote/splice demonstrate structured metaprogramming (see docs/Quoting.md). quote yields an AST token whose type is inferred by the compiler.
  • main is a standard entry point.

Inspecting Comptime Work

To inspect scheduler-produced AST and typed artefacts, run:

$ fp compile src/main.fp --package demo --emit ast --emit ast-typed --emit hir

Artifacts (paths depend on your configuration):

  • target/ast/src_main.ast - canonical AST state after applied answers
  • target/ast/src_main.ast-typed - typed AST annotations
  • target/hir/src_main.hir - HIR for requested lowered scopes

Building To Backends

The same compile command can emit native binaries, LLVM, Cranelift, and bytecode, or run through the interpreter. Select a backend with --backend; select a source printer with --target.

Common flags:

  • --opt-level {0|1|2|3} – Optimisation level (default: 2)
  • --debug – Include debug info
  • --emit {ast,ast-typed,hir,mir,lir} - Persist intermediates for inspection
  • --save-intermediates – Shortcut to emit all intermediates

For example:

$ fp compile src/main.fp --package demo --backend binary --output main
$ fp compile src/main.fp --package demo --backend bytecode --output main.fbc
$ fp compile src/main.fp --package demo --target rust --output main.rs

Inspecting Intermediates

$ fp compile src/main.fp --package demo --emit ast-typed --emit hir
$ cat target/ast/src_main.ast-typed
$ cat target/hir/src_main.hir
  • The typed AST shows annotations applied to canonical AST state.
  • HIR shows requested scopes after comptime answers needed by lowering were applied.

Inspecting Produced Artifacts

Use the inspect command to read compiled artifacts back out of their binary form:

$ fp inspect target/main.fbc
$ fp inspect target/main.fbc --export-text-bytecode target/main.ftbc
$ fp inspect target/main --hex --max-bytes 128
  • .fbc files are decoded as FerroPhase binary bytecode and summarised by entrypoint, constants, and functions.
  • --export-text-bytecode converts .fbc into the stable textual .ftbc representation for review and diffing.
  • Native/object binaries are summarised by file kind, architecture, sections, symbols, and code sections.
  • --hex prints a bounded raw byte preview for any inspected artifact.
  • Auto-detection is header-based across these families: FerroPhase binary bytecode, FerroPhase text bytecode, JVM class files, Unix archives, WebAssembly modules, and native/object containers (ELF, Mach-O, PE, COFF, XCOFF, dyld cache). Unknown files fall back to raw bytes.

Current scope:

  • FerroPhase can inspect and transcode its own bytecode representation.
  • Native executables can be inspected structurally, but there is not yet a reverse compiler path from machine code back into MIR/AST, so high-level decompilation is intentionally out of scope for now.

Release Artifacts

Production builds must emit the release artifacts defined in docs/ReleaseArtifacts.md. When --release is enabled, record:

  • Toolchain and build options (see docs/BuildOptions.md).
  • Output paths (target/ or --output overrides).
  • Backend-specific intermediates saved with --save-intermediates.

The release record is required for reproducibility audits.

Troubleshooting

  • Missing LLVM tools: fp compile reports if llc/clang are unavailable. Install via your package manager or set FP_LLVM_PATH.
  • Comptime errors: Review diagnostics by RequestId and original span.
  • Target mismatch: Ensure docs/Design.md consistency rules hold; quoted and spliced code should be applied to canonical AST before affected scopes are lowered or emitted.

Next Steps

  • Explore bytecode mode: fp compile src/main.fp --package demo --backend bytecode
  • Generate Rust output with fp compile --target rust
  • Extend the project with modules and custom targets; update FerroPhase.toml accordingly.