Compiler & VM Architecture
Internal architecture of the Lexer, Parser, AST, Bytecode VM, and Native AOT backend.
NextViper Production Backend Architecture & Backend Evaluation
Executive Summary
As NextViper transitions from language specification and baseline execution into its ecosystem and high-performance backend phase, this document establishes the production compiler architecture and evaluates code generation backends (LLVM vs Cranelift).
NextViper is designed for high-performance computing, automation, AI, and data processing. To fulfill these design requirements without sacrificing developer ergonomics or safety, NextViper utilizes a multi-tier compilation pipeline with a typed Intermediate Representation (IR) lowering into machine code.
Production Compiler Pipeline
The complete compilation and execution pipeline is structured as follows:
NextViper Source (.nv)
↓
Lexer (Scanner / Tokenizer)
↓
Parser (Pratt Precedence & AST Construction)
↓
AST (Abstract Syntax Tree with SourceSpan)
↓
Type Checker (Static Inference, Type Propagation & Safety)
↓
NextViper Typed IR (Three-Address Code SSA-Style Control Flow Graph)
↓
IR Optimizer (Constant Folding, Dead-Code Elimination, Mem2Reg)
↓
Native Backend (LLVM Code Generation / Machine Binary Emitter)
↓
Standalone Native Machine Binary (ELF / Mach-O / PE)Stage Responsibilities
|>).let vs let mut), and verifies collection constraints.r0, r1, ...), and typed operations.Backend Evaluation: LLVM vs Cranelift
To select the production native code generator, LLVM and Cranelift were evaluated across six critical dimensions:
| Evaluation Dimension | LLVM | Cranelift | NextViper Priority | Winner |
|---|---|---|---|---|
| **1. Runtime Performance** | World-class global optimizations, polyhedral loop transformations, auto-vectorization (AVX-512, NEON, SVE), LTO. | Baseline code generation; lacks aggressive vectorization and polyhedral loop optimizers. | **Critical** (AI, numeric tensors, data workflows) | **LLVM** |
| **2. Compilation Speed** | Heavier compilation pipeline; moderate to slow compile times for deep optimization levels (`-O3`). | Extremely fast compilation designed for real-time JIT compilation (WebAssembly). | **Medium** (AOT builds prioritize peak speed) | **Cranelift** |
| **3. Cross-Platform Support** | Universal tier-1 support (x86_64, aarch64, ARM, Apple Silicon, Windows MSVC, RISC-V, WASM, embedded). | Solid x86_64 and aarch64; experimental Windows MSVC ABI, limited 32-bit/specialized platform support. | **High** (Linux, macOS, Windows, Docker) | **LLVM** |
| **4. Integration Complexity** | Native C++ API (`llvm::IRBuilder`, `llvm::Module`). Directly compatible with NextViper C++20 core without FFI. | Written in Rust. Requires C FFI boundary layer (`cranelift-c` or cdylib), requiring dual toolchains (Rust + C++). | **High** (Maintainability & toolchain simplicity) | **LLVM** |
| **5. Debugging & Tooling** | Industry-standard DWARF5, Windows CodeView/PDB, full GDB, LLDB, and Valgrind interoperability. | Basic DWARF support; limited Windows PDB support. | **High** (Enterprise debugging experience) | **LLVM** |
| **6. Future GPU / AI Support** | Direct integration with MLIR, NVPTX (CUDA), AMDGPU, SPIR-V, WebGPU, and tensor intrinsics (`llvm.matrix`). | No native GPU target backends (targets CPU machine code exclusively). | **Critical** (First-class AI/Tensor roadmap) | **LLVM** |
Architectural Decision & Justification
Selected Backend: **LLVM**
Primary Reasons for Selection:
tensor.matmul), and tabular dataset processing. LLVM's automatic vectorizer (SIMD), loop unrolling, and auto-broadcasting deliver the mathematical throughput essential for AI and data science workloads.llvm::IRBuilder, llvm::LLVMContext, llvm::Module), eliminating foreign function interface (FFI) overhead, marshaling bottlenecks, and complex dual-compiler build dependencies.nvptx64-nvidia-cuda, amdgcn-amd-amdhsa, and SPIR-V). This aligns with NextViper's roadmap for hardware acceleration.Role of Cranelift
Cranelift remains an attractive candidate for a future ultra-fast development JIT mode (e.g. nextviper dev or instant REPL execution), but LLVM is chosen as the primary production native AOT compiler backend.
Working Native Compilation Prototype
NextViper includes a fully functional, verified native compilation pipeline:
Sample Program (`test_sum.nv`):
let x = 10
let y = 20
print(x + y)Execution Pathways:
./bin/nextviper run test_sum.nv
# Output: 30 ./bin/nextviper build test_sum.nv -o test_sum_bin --native
./test_sum_bin
# Output: 30Equivalence Verification:
Automated test suite (tests/test_native_compiler.cpp) verifies that:
`.
`.

