Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

889 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PudgeWithMoML

PudgeWithMoML is a MiniML → RISC-V compiler written in OCaml. It implements type inference, closures, partial application and a small runtime with garbage collection.

source.ml
   └─▶ parse ─▶ infer ─▶ α-rename ─▶ ANF ─▶ closure conversion ─▶ λ-lift ─▶ RISC-V

Features

  • let, recursion, lambdas, tuples, lists and pattern matching
  • Hindley–Milner-style type inference and polymorphism
  • first-class functions, closures and arbitrary arity
  • a RISC-V runtime with partial application and copying GC
  • expect and cram tests for individual compiler stages
  • property-based parser tests with QCheck

Architecture

The implementation is split into explicit compiler stages:

  • lib/frontend/ — AST, Angstrom parser, type inference and typed trees
  • lib/middle_end/ — alpha conversion, ANF, closure conversion and lambda lifting
  • lib/riscv/ — RISC-V machine model and assembly generation
  • lib/runtime/ — closure application, primitive functions and copying garbage collector
  • bin/compiler.ml — command-line driver connecting all stages
  • test/ — parser, type inference, ANF, code generation, runtime and QCheck tests

The frontend parses an ML-like source program, checks it and constructs the top-level type environment. The middle end gives every binding a unique name, converts expressions to ANF, eliminates free variables through closure conversion and lifts nested functions to the top level. The backend then emits RV64 assembly, which is linked with the C and assembly runtime.

Inline expect tests cover individual transformations and printers. Dune cram tests exercise the command-line compiler and generated programs, while QCheck verifies parser/pretty-printer round trips on generated ASTs.

Build and run

You will need OCaml/opam, Dune, the RISC-V GNU toolchain and qemu-riscv64.

cd PudgeWithMoML
opam install . --deps-only --with-test

make test
make compile input=bin/tests/fact
qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 main.exe

Expected output:

24

Inspect intermediate stages

dune exec compiler -- -fromfile bin/tests/fact -dparsetree
dune exec compiler -- -fromfile bin/tests/fact -dtypes
dune exec compiler -- -fromfile bin/tests/fact -anf
dune exec compiler -- -fromfile bin/tests/fact -cc

Built by Gleb Nasretdinov and Ilhom Kombaev. Licensed under LGPL-3.0-or-later.

Releases

Packages

Contributors

Languages