Terra
Terra is a low-level, statically typed systems programming language embedded inside Lua, letting developers use Lua as a compile-time meta-language to generate and specialize high-performance Terra code.
Definition
Terra is a low-level, statically typed systems programming language embedded inside Lua, letting developers use Lua as a compile-time meta-language to generate and specialize high-performance Terra code.
Overview
Terra was created by Zachary DeVito, James Bornholt, and colleagues at Stanford University and released in 2013 as a research project exploring the combination of a dynamically typed scripting language with a statically typed, LLVM-compiled systems language in a single source file. Rather than being a standalone language with its own toolchain, Terra is implemented as a library that runs inside a Lua interpreter (LuaJIT): Lua code executes at 'meta' time to construct, inspect, and specialize Terra functions, which are then just-in-time or ahead-of-time compiled to native machine code via LLVM. This design lets a programmer write ordinary imperative, C-like code for performance-critical inner loops while using Lua's full expressive power — closures, tables, string manipulation, macros — to generate variants of that code, build domain-specific languages, or perform autotuning without needing a separate build step or preprocessor. Because Terra functions are plain data structures manipulable from Lua, the same host program can programmatically construct different specialized kernels depending on hardware, problem size, or numerical precision, then compile and run them within the same process. Terra has no built-in garbage collector by default and gives programmers explicit control over memory layout, pointers, and structs, making it suitable for the same performance-sensitive domains as C, while still supporting features like operator overloading, templates via Lua-level metaprogramming, and easy interoperation with existing C libraries. Terra is best known as the implementation vehicle for research systems in high-performance computing and graphics, most notably the Legion runtime and its Regent programming language for distributed heterogeneous computing, and it has been used to build embedded DSLs for image processing and numerical kernels in academic settings. It remains primarily a research and specialist tool rather than a mainstream production language, valued for the flexibility its Lua/Terra split gives to compiler and DSL authors who need both metaprogramming power and native performance in one system.
Key Features
- Embedded inside Lua — Lua acts as the compile-time meta-language for generating Terra code
- Statically typed, low-level code compiled to native machine code via LLVM
- No mandatory garbage collector; explicit control over memory and pointers like C
- Terra functions are first-class Lua values that can be programmatically constructed and specialized
- Supports just-in-time and ahead-of-time (standalone executable) compilation
- Seamless interoperation with existing C libraries and headers
- Well suited to building domain-specific languages (DSLs) and autotuned numerical kernels
- Used as the implementation substrate for the Legion/Regent HPC runtime