Compiling OCaml to the TI-84 CE Calculator

https://news.ycombinator.com/rss Hits: 7
Summary

In this post, I’ll explain how I compiled an OCaml program to run on a TI-84+ CE calculator. 🐪 Background OCaml is a somewhat niche functional language that I’ve enjoyed learning over the last couple of years. I’ve also been a calculator enthusiast since high school, working on projects like PineappleCAS. At the time, the calculator toolchain only supported C and (e)z80 assembly. Now, talented folks have set up a toolchain that leverages LLVM so you can program in C, C++, Rust, Zig, and others. OCaml is notably missing from this list. Let’s fix that! Compiling OCaml There are native OCaml compilers for x86, ARM, and PowerPC. There is also a bytecode compiler, though it’s largely undocumented (see interp.c). All of these backends need a runtime that includes a garbage collector and native functions to do useful things like print to stdout and read files. Unfortunately, these artifacts tend to be large due to the OCaml compiler’s lack of dead code elimination. I really like the idea of compiling an entire OCaml program into a single highly portable ANSI C file that we could compile with the calculator toolchain (or any C toolchain!). This idea isn’t new: ocamlcc compiles OCaml bytecode to C. OMicroB compiles OCaml to small devices by optimizing the OCaml bytecode and using a lightweight virtual machine. For my approach, I wanted something that is: Highly portable Interacts well with the OCaml build system Small – The generated code and runtime should fit in the 256k RAM on the TI-84+ CE Note that “efficient” and “practical” are missing from this list :) Enter Js_of_ocaml. Js_of_ocaml powers nearly all of the OCaml website frontends that are written in OCaml. (Yes, that’s a real thing!). First, the OCaml is compiled to bytecode, then Js_of_ocaml optimizes that bytecode and lifts that bytecode into javascript. The high level idea is as follows: we’ll write a new backend for Js_of_ocaml to emit C instead of javascript. Then we’ll just need to add a couple native function...

First seen: 2025-05-20 13:11

Last seen: 2025-05-20 19:13