Download e-book for kindle: Compiling functional languages by Leroy X.

pushl $i popl %eax addl 0(%esp), %eax • By efficient interpretation. The interpreter is typically written in C as shown on the next slide, and is about one order of magnitude faster than term-level interpretation. 35 A typical abstract machine interpreter value interpret(int * start_code) { register int * c = start_code; register value * s = bottom_of_stack; register environment e; while(1) switch case case case case case { (*c++) { CONST: ADD: ACCESS: CLOSURE: APPLY: case RETURN: case STOP: *s++ = *c++; break; s[-2] = s[-2] + s[-1]; s--; break; *s++ = Lookup(e, *c++); break; *s++ = MakeClosure(*c++, e); break; arg = *--sp; clos = *--sp; *sp++ = (value) c; *sp++ = (value) e; c = Code(clos); e = AddEnv(arg, Environment(clos)); break; res = *--sp; e = (environment) *--sp; c = (int *) *--sp; *sp++ = res; break; return *--sp; } } } 36 An abstract machine for call-by-name: Krivine’s machine As before, three components in this machine: code c, environment e, stack s.

Main applications: • Optimize function calls in functional languages. ) • Optimize method dispatch in object-oriented languages. (Similar problems to that of closures. ) • Eliminate run-time type tests in dynamically-typed languages such as Scheme. g. ) • More applications later. . 62 Variants of 0-CFA More precise analyses: • Polyvariant analyses (n-CFA, polymorphic splitting, . . ): distinguish between different call sites of the same function. • Finer approximation of values (Heintze’s set-based analysis): capture the shapes of data structures using grammars.

If a reduces infinitely, the machine started in state S performs an infinite number of transitions. 50 Part 3 Optimized compilation of functions: flow analysis 51 Compilation to optimized machine code Step 1: Replace functions by closures; make explicit the construction, passing, and accessing of the environments. e. closed functions). ) Step 2: Optimize and generate machine code from the intermediate language. • Write your own code generator using conventional compiler technology (OCaml, SML/NJ) • Or use a C compiler (GHC, Bigloo).

Download PDF sample

Compiling functional languages by Leroy X.

by Steven

Rated 4.43 of 5 – based on 35 votes