Meta tags:
Headings (most frequently used words):
terra, generative, programming, embedding, and, interoperability, simplicity, multi, stage, operators, compiling, language, conditional, compilation, namespaces, templating, specialization, class, systems,
Text of the page (most frequently used words):
terra (125), the (104), lua (63), end (59), and (58), function (48), code (45), can (37), for (34), return (34), int (32), that (31), data (31), you (30), ptr (27), #language (25), this (23), then (22), use (22), local (21), are (20), functions (17), example (16), from (16), with (15), programming (14), level (14), generate (13), languages (13), class (12), using (12), low (12), stmt (12), terralib (11), such (11), add (11), get (10), var (10), elseif (10), quote (10), meta (10), methods (9), inside (9), compile (9), other (9), array (9), compiled (9), into (9), struct (8), libraries (8), table (8), here (8), type (8), arrayt (8), first (8), values (8), myfunctions (8), will (8), program (8), body (8), loop (8), target (8), hello (8), our (7), file (7), built (7), self (7), each (7), since (7), when (7), call (7), implemented (7), input (7), performance (7), escape (7), more (6), possible (6), allows (6), types (6), else (6), print (6), just (6), includec (6), uname (6), programs (6), features (6), your (6), stmts (6), high (6), system (5), implementation (5), templating (5), systems (5), simple (5), returns (5), add3 (5), printf (5), error (5), conditional (5), compilation (5), symbol (5), character (5), operator (5), http (5), design (5), jit (5), implement (4), while (4), implements (4), square (4), but (4), not (4), write (4), specific (4), equivalent (4), statically (4), typed (4), syntax (4), makearray (4), create (4), generates (4), version (4), stdio (4), they (4), need (4), constructs (4), run (4), out (4), result (4), combination (4), also (4), existing (4), api (4), embedded (4), make (4), beginning (4), before (4), insert (4), goto (4), statements (4), getchar (4), tape (4), variables (4), add4 (4), output (4), org (4), waitatend (4), programmed (4), like (3), similar (3), lib (3), interface (3), draw (3), define (3), any (3), instance (3), emit (3), double (3), mymath (3), different (3), object (3), unlike (3), runtime (3), new (3), init (3), set (3), template (3), fact (3), stored (3), tables (3), called (3), well (3), normally (3), namespaces (3), name (3), value (3), what (3), reportos (3), linux (3), unknown (3), dynamic (3), means (3), argc (3), argv (3), plain (3), its (3), application (3), easy (3), directly (3), reference (3), generative (3), domain (3), jumpstack (3), labels (3), label (3), after (3), printpretty (3), int32 (3), extract0 (3), prints (3), over (3), string (3), operators (3), compiler (3), arbitrary (3), printtwice (3), expressions (3), evaluated (3), choosesecond (3), sin5 (3), multi (3), stage (3), wikipedia (3), wiki (3), getting (3), alone (3), powerful (3), control (3), llvm (3), making (3), software (3), rax (3), information (2), has (2), java (2), javalike (2), pldi (2), paper (2), extends (2), require (2), drawable (2), shown (2), does (2), provide (2), inheritance (2), polymorphism (2), instead (2), written (2), power (2), makepown (2), pow3 (2), versions (2), similarly (2), holds (2), sugar (2), contains (2), pointer (2), list (2), size (2), intarray (2), doublearray (2), simply (2), which (2), hold (2), multiple (2), objects (2), already (2), behavior (2), keyword (2), organize (2), calling (2), time (2), import (2), osx (2), conditionally (2), preprocessor (2), construct (2), most (2), statement (2), ifdef (2), many (2), include (2), main (2), state (2), initialize (2), exit (2), pre (2), furthermore (2), backwards (2), compatible (2), makes (2), files (2), loadfile (2), between (2), top (2), even (2), projects (2), these (2), auto (2), newlist (2), sub (2), represent (2), labelname (2), putchar (2), corresponding (2), below (2), compilers (2), turing (2), machine (2), take (2), addtwo (2), expression (2), spliced (2), escapes (2), have (2), variable (2), defined (2), used (2), case (2), entities (2), math (2), sin (2), publications (2), started (2), designed (2), save (2), stand (2), serves (2), better (2), core (2), scripting (2), library (2), flow (2), void (2), templates (2), float (2), floatarray (2), composed (2), printhello (2), 0x60e6010 (2), movabs (2), eax (2), appropriate, available, one, google, golike, length, shape, mechanism, normal, user, might, fill, pow, nesting, pow2, specialization, invocation, classes, there, mechanisms, declarations, associates, method, stdlib, some, cast, malloc, sizeof, usearrays, functionality, creating, defining, where, takes, std, vector, pairs, seek, asprintf, gets, size_t, uint64, iterate, through, want, seen, imported, declaration, specifically, deal, problem, way, resolve, placing, another, namespace, figure, running, popen, read, darwin, right, accomplished, directives, custom, build, decide, how, full, things, preprocessors, cannot, external, differently, discover, operating, instantiate, depending, few, examples, simplicity, char, lua_state, lual_newstate, lual_openlibs, terra_init, terra_dofile, finally, applications, linking, against, very, initializes, runs, specified, argument, interpreter, libterra, both, pure, preexisting, treat, load, combined, header, pass, luajit, vice, versa, access, details, foreign, fuction, don, exist, vacuum, useful, primarily, integrate, keeping, project, established, embedding, interoperability, tuners, describes, orion, image, processing, kernels, process, porting, mesh, based, pde, liszt, stack, keep, track, ending, globally, unique, retrieve, match, remove, assert, back, edge, looping, bf_t_46_1, 256, adds3, loops, shifts, now, responsible, generating, given, clear, initially, two, suited, writing, minimal, emulates, maximum, skeleton, sets, compiling, print4, rather, than, backtick, quotation, concrete, integer, references, symbols, false, number, true, issymbol, refers, refer, treated, were, escaped, default, foo, bar, example4, add43, prettified, representation, sin50, 95892427466314, constant, verified, printing, once, splice, passed, returned, programmatically, hygienic_macro, x_macro, template_metaprogramming, www, rice, edu, taha, msp, benchmarksgame, alioth, debian, about, see, depth, look, guide, was, independently, final, doesn, executable, addition, ensuring, clean, separation, lets, replacement, metaprogramming, macros, nicer, properties, hygiene, exists, keeps, enabling, only, always, abstraction, gives, predictable, same, backend, apple, uses, performs, translations, benchmark, game, perform, within, speed, their, equivalents, clang, frontend, includes, support, simd, operations, non, temporal, writes, prefetches, configure, controllable, fannhakunen, nbody, extensions, techniques, all, dsls, ecosystem, dsl, tune, dynamically, embed, building, done, determines, _win32, iswindows, endif, become, typedef, fall, behaviors, interleaved, evaluation, depend, generation, gave, trend, replaced, real, comes, realization, really, calls, surrounding, mix, processor, definitions, complete, been, produce, optimized, horrible, practice, eigen, manual, memory, management, try, others, browser, via, replit, introduces, rawstring, nil, introspected, disas, assembly, address, push, 0x60e6011, rdi, 102129664, 0x60e601b, 140735712154681, 0x60e6025, 0x60e6027, xor, 0x60e6029, pop, rdx, 0x60e602a, ret, executables, shared, link, them, saveobj, helloterra, community, source, downloads, home, counterpart,
Text of the page (random words):
bedded jit compiler for building languages we use techniques from multi stage programming 2 to make it possible to meta program terra using lua terra expressions types and functions are all first class lua values making it possible to generate arbitrary programs at runtime this allows you to compile domain specific languages dsls written in lua into high performance terra code furthermore since terra is built on the lua ecosystem it is easy to embed terra lua programs in other software as a library this design allows you to add a jit compiler into your existing software you can use it to add a jit compiled dsl to your application or to auto tune high performance code dynamically a scripting language with high performance extensions while the performance of lua and other dynamic languages is always getting better a low level of abstraction gives you predictable control of performance when you need it terra programs use the same llvm backend that apple uses for its c compilers this means that terra code performs similarly to equivalent c code for instance our translations of the nbody and fannhakunen programs from the programming language benchmark game 1 perform within 5 of the speed of their c equivalents when compiled with clang llvm s c frontend terra also includes built in support for simd operations and other low level features like non temporal writes and prefetches you can use lua to organize and configure your application and then call into terra code when you need controllable performance a stand alone low level language terra was designed so that it can run independently from lua in fact if your final program doesn t need lua you can save terra code into a o file or executable in addition to ensuring a clean separation between high and low level code this design lets you use terra as a stand alone low level language in this use case lua serves as a powerful meta programming language here it serves as a replacement for c template metaprogramming 3 or c preprocessor x macros 4 with better syntax and nicer properties such as hygiene 5 since terra exists only as code embedded in a lua meta program features that are normally built into low level languages can be implemented as lua libraries this design keeps the core of terra simple while enabling powerful behavior such as conditional compilation namespaces templating and even class systems implemented as libraries for more information about using terra see the getting started guide and api reference our publications provide a more in depth look at its design 1 http benchmarksgame alioth debian org 2 http www cs rice edu taha msp 3 http en wikipedia org wiki template_metaprogramming 4 http en wikipedia org wiki x_macro 5 http en wikipedia org wiki hygienic_macro generative programming terra entities such as functions types variables and expressions are first class lua values they can be stored in lua variables and passed to or returned from lua functions using constructs from multi stage programming 2 you can write lua code to programmatically generate arbitrary terra code multi stage operators inside terra code you can use an escape operator to splice the result of a lua expression into the terra code local a 5 terra sin5 return math sin a end an escape is evaluated when a terra function is compiled and the result is spliced into the terra code in this example this means that math sin 5 will be evaluated once and the code that implements the terra function will return a constant this can be verified by printing out the compiled version of the sin5 function output a prettified representation of what this function does sin5 printpretty output sin50 terra double return 0 95892427466314 end escapes can also return other terra entities such as a function add4 terra a int return a 4 end terra example return add4 3 7 end in this case terra will insert a call to the terra function stored in the add4 variable example printpretty output example4 terra int32 return extract0 add43 3 end in fact any name used in terra code such as add4 or foo bar is treated as if it were escaped by default inside an escape you can refer to variables defined in terra a function to be called inside an escape function choosesecond a b prints false a is not a number print a 1 prints true a is a terra symbol print terralib issymbol a return b end terra example input int var a input var b input 1 create an escape that refers to a and b return choosesecond a b returns the value of b end example 1 returns 2 since escapes are evaluated before a terra function is compiled variables a and b will not have concrete integer values inside the escape instead inside the lua code a and b are terra symbols that represent references to terra values since choosesecond returns the symbol b the example function will return the value of terra variable b when called the quotation operator a backtick allows you to generate terra statements and expression in lua they can then be spliced into terra code using the escape operator function addtwo a b return a b end terra example input int var a input var b input 1 return addtwo a b end example 1 returns 3 to generate statements rather than expressions you can use the quote operator local printtwice quote c printf hello n c printf hello n end terra print4 printtwice printtwice end compiling a language with these two operators you can use lua to generate arbitrary terra code at compile time this makes the combination of lua terra well suited for writing compilers for high performance domain specific languages for instance we can implement a compiler for bf a minimal language that emulates a turing machine the lua function compile will take a string of bf code and a maximum tape size n it then generates a terra function that implements the bf code here is a skeleton that sets up the bf program local function compile code n local function body data ptr implementation of body end return terra an array to hold the tape var data int n clear the tape initially for i 0 n do data i 0 end var ptr 0 generate the code for the body body data ptr end end the function body is responsible for generating body of the bf program given the code string local function body data ptr the list of terra statements that make up the bf program local stmts terralib newlist loop over each character in the bf code for i 1 code do local c code sub i i local stmt generate the corresponding terra statement for each bf operator if c then stmt quote ptr ptr 1 end elseif c then stmt quote ptr ptr 1 end elseif c then stmt quote data ptr data ptr 1 end elseif c then stmt quote data ptr data ptr 1 end elseif c then stmt quote c putchar data ptr end elseif c then stmt quote data ptr c getchar end elseif c then error implemented below elseif c then error implemented below else error unknown character c end stmts insert stmt end return stmts end it loops over the code string and generates the corresponding terra code for each character of bf e g shifts the tape over by 1 and is implemented by the terra code ptr ptr 1 we can now compile a bf function add3 compile the result add3 is a terra function that adds3 to an input character and then prints it out add3 printpretty bf_t_46_1 terra var data int32 256 var ptr int32 0 data ptr extract0 getchar data ptr data ptr 1 data ptr data ptr 1 data ptr data ptr 1 extract0 putchar data ptr end we can also use goto statements goto labelname and labels labelname to implement bf s looping construct local function body data ptr local stmts terralib newlist add a stack to keep track of the beginning of each loop local jumpstack for i 1 code do local c code sub i i local stmt if elseif c then generate labels to represent the beginning and ending of the loop the symbol function generates a globally unique name for the label local target before symbol after symbol table insert jumpstack target stmt quote label for beginning of the loop target before if data ptr 0 then goto target after exit the loop end end elseif c then retrieve the labels that match this loop local target table remove jumpstack assert target stmt quote goto target before loop back edge target after label for end of the loop end else error unknown character c end stmts insert stmt end return stmts end we are using these generative programming constructs to implement domain specific languages and auto tuners our pldi paper describes our implementation of orion a language for image processing kernels and we are in the process of porting the liszt language for mesh based pde s to terra embedding and interoperability programming languages don t exist in a vacuum and the generative programming features of terra can be useful even in projects that are primarily implemented in other programming languages we make it possible to integrate terra with other projects so you can use it to generate low level code while keeping most of your project in a well established language first we make it possible to pass values between lua and terra our implementation is built on top of luajit s foreign fuction interface you can call terra functions directly from lua or vice versa and access terra objects directly from lua more details in the api reference furthermore lua terra is backwards compatible with both pure lua and c which makes it easy to use preexisting code in lua terra you can use require or loadfile and it will treat the file as a lua program use terralib loadfile to load a combined lua terra file you can use terralib includec to import c functions from already existing header files finally lua terra can also be embedded in pre existing applications by linking the application against libterra a and using terra s c api the interface is very similar to that of the lua interpreter a simple example initializes terra and then runs code from the file specified in each argument include stdio h include terra h int main int argc char argv lua_state l lual_newstate create a plain lua state lual_openlibs l initialize its libraries initialize the terra state in lua terra_init l for int i 1 i argc i run the terra code in each file if terra_dofile l argv i exit 1 return 0 simplicity the combination of a simple low level language with a simple dynamic programming language means that many built in features of statically typed low level languages can be implemented as libraries in the dynamic language here are just a few examples conditional compilation normally conditional compilation is accomplished using preprocessor directives e g ifdef or custom build systems using lua terra we can write lua code to decide how to construct a terra function since lua is a full programming language it can do things that most preprocessors cannot such as call external programs in this example we conditionally compile a terra function differently on osx and linux by first calling uname to discover the operating system and then using an if statement to instantiate a different version of the terra function depending on the result run uname to figure out what os we are running local uname io popen uname r read a local c terralib includec stdio h if uname darwin n then terra reportos c printf this is osx n end elseif uname linux n then terra reportos c printf this is linux n end else error os unknown end conditionally compiled to the right version for this os reportos namespaces statically typed languages normally need constructs that specifically deal with the problem of namespaces e g c s namespace keyword or java s import constructs for terra we just use lua s first class tables as way to organize functions when you use any name such as myfunctions add inside a terra function the terra will resolve it at compile time to the terra value it holds here is an example of placing a terra function inside a lua table and then calling it from another terra function local myfunctions terra functions are first class lua values they can be stored in lua tables terra myfunctions add a int b int int return a b end and called from the tables as well terra myfunctions add3 a int return myfunctions add a 3 end the declaration of myfunctions add is just syntax sugar for myfunctions add terra a int b int int return a b end print myfunctions add3 4 in fact you ve already seen this behavior when we imported c functions c terralib includec stdio h the function includec just returns a lua table c that contains the c functions since c is a lua table you can iterate through it if you want for k v in pairs c do print k v end seek terra function asprintf terra function gets terra function size_t uint64 templating since terra types and functions are first class values you can get functionality similar to a c template by simply creating a terra type and defining a terra function inside of a lua function here is an example where we define the lua function makearray t which takes a terra type t and generates an array object that can hold multiple t objects i e a simple version of c s std vector c terralib includec stdlib h function makearray t create a new struct type that contains a pointer to a list of t s and a size n local struct arrayt t is a pointer to t data t n int add some methods to the type terra arrayt init n int the syntax t is a cast the c equivalent is t self data t c malloc sizeof t n self n n end terra arrayt get i int return self data i end terra arrayt set i int v t self data i v end return the type as a return arrayt end intarray makearray int doublearray makearray double terra usearrays var ia intarray var da doublearray ia init 1 da init 1 ia set 0 3 da set 0 4 5 return ia get 0 da get 0 end as shown in this example terra allows you to define methods on struct types unlike other statically typed languages with classes there are no built in mechanisms for inheritance or runtime polymorphism methods declarations are just a syntax sugar that associates table of lua methods with each type here the get method is equivalent to arrayt methods get terra self t i int return self data i end the object arrayt methods is a lua table that holds the methods for type arrayt similarly an invocation such as ia get 0 is equivalent to t methods get ia 0 specialization by nesting a terra function inside a lua function you can compile different versions of a function here we generate different versions of the power function e g pow2 or pow3 generate a power function for a specific n e g n 3 function makepown n local function emit a n if n 0 then return 1 else return a emit a n 1 end end return terra a double return emit a n end end use it to fill in a table of functions local mymath for n 1 10 do mymath pow n makepown n end print mymath pow3 2 8 class systems as shown in the templating example terra allows you to define methods on struct types but does not provide any built in mechanism for inheritance or polymorphism instead normal class systems can be written as libraries for instance a user might write j terralib require lib javalike drawable j interface draw struct square lengt...
|