Terra: A low-level counterpart to Lua

A very interesting project developed by Zachary DeVito et al at Stanford University:

Terra is a new low-level system programming language that is designed to interoperate seamlessly with the Lua programming language:

-- This top-level code is plain Lua code.
print("Hello, Lua!")

-- Terra is backwards compatible with C
-- we'll use C's io library in our example.
C = terralib.includec("stdio.h")

-- The keyword 'terra' introduces
-- a new Terra function.
terra hello(argc : int, argv : &rawstring)
    -- Here we call a C function from Terra
    C.printf("Hello, Terra!\n")
    return 0

-- You can call Terra functions directly from Lua

-- Or, you can save them to disk as executables or .o
-- files and link them into existing programs
terralib.saveobj("helloterra",{ main = hello })

Like C, Terra is a simple, statically-typed, compiled language with manual memory management. But unlike C, it is designed from the beginning to interoperate with Lua. Terra functions are first-class Lua values created using the terra keyword. When needed they are JIT-compiled to machine code.

Seems as if the target use case is high-performance computing. The team has also released a related paper, titled Terra: A Multi-Stage Language for High-Performance Computing:

High-performance computing applications, such as auto-tuners and domain-specific languages, rely on generative programming techniques to achieve high performance and portability. However, these systems are often implemented in multiple disparate languages and perform code generation in a separate process from program execution, making certain optimizations difficult to engineer. We leverage a popular scripting language, Lua, to stage the execution of a novel low-level language, Terra. Users can implement optimizations in the high-level language, and use built-in constructs to generate and execute high-performance Terra code. To simplify meta-programming, Lua and Terra share the same lexical environment, but, to ensure performance, Terra code can execute independently of Lua’s runtime. We evaluate our design by reimplementing existing multi-language systems entirely in Terra. Our Terra-based auto-tuner for BLAS routines performs within 20% of ATLAS, and our DSL for stencil computations runs 2.3x faster than hand-written C.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


Terra sounds nifty! Tho... personally I'd like something with GC and nice advanced static type checking and inference. Wonder what else exists along such lines.


what about non-alpha/beta ware?

like ada :-)

since bitc is no longer.

or another favourite of mine that i never get a chance to use, ats.

the others seem a bit too much on the academic side yet still, unfortunately.

erlang has decent support for arbitrary binary data layouts, iiuc? can't f# understand low-level struct layouts / do ffi with C?

maybe it is still just a hard problem to get really fancy-theoretically :-) right?

p.s. shen on C some day via SECD.; idris sounds quite nifty for an ivory-tower thing.

p.p.s. ouch! care of bitc.


Better BLAS through meta-programming

It is not that difficult to generate BLAS that
matches the performance of ALTAS (and achieves more then 80% of the
peak performance) -- if one uses good tools. Please see Section 4.3 in
the following old paper and Figure 21.


MetaOCaml and Scala can also help in generating stencil


BTW, MetaOCaml code statically assures that all array references are
in-bounds. (The hahd-written code had a problem in this respect).