Overview of Code for Module 10

Your new work is going to be concentrated in four files: closure-convert.sml (closure conversion), knormalize.sml (K-normalization of closures), codegen.sml (code generation for closures), and vmrun.c (new instructions that support closures). But a new language feature is what the software engineers are pleased to call a “cross-cutting concern,” and you’ll change other files as well.

New things

New files you will write or edit

clscheme.sml

This file defines Closed Scheme, which is the target language of closure conversion. Closed Scheme is just First-Order Scheme extended with CLOSURE, CAPTURED, and LETREC constructs. When you receive the file, it will have everything you need except a couple of cases of embedding of Closed Scheme into vScheme. You’ll write these cases in step (@embedcases).

closure-convert.sml

This is where you’ll turn lambdas into closures. I provide a little scaffolding, but the bulk of the file is yours.

New files that are useful

set.sml

This file defines a set abstraction that you can use to compute sets of free variables. You won’t need to touch any of the code.

New file that is interesting for understanding or for depth points

mutability.sml

This file contains a “mutability detector” which rejects any program that mutates a captured variable. The detector is very conservative and may reject programs that should be OK.

More interesting, this file also contains a placeholder for another translation pass, which migrates captured, mutable variables onto the heap. This pass can be implemented for depth points. Such a pass is needed to do some of the examples from my book chapter, like the resettable counters or the random-number generator.

New, boring file that can be ignored completely

foclutil.sml

This file contains an embedding of First-Order Scheme into Closed Scheme. The embedding enables the UFT driver to materialize Closed Scheme from a file by first materializing First-Order Scheme and then embedding it. You can ignore this file completely. Why are you even reading this?

Old things

Interesting UFT files you will modify

knf.sml

You’ll add the CLOSURE, CAPTURED, and LETREC forms to K-normal form.

knormalize.sml

You’ll K-normalize the new forms.

embedkn.sml

You’ll embed the new forms into vScheme.

codegen.sml

You’ll generate code for the new forms.

Uninteresting UFT files you will modify

uft.sml

You’ll add cases to the driver’s translate function, so you can translate higher-order input languages.

asmparse.sml

You’ll unparse the new instructions to suitable assembly code.

asmutil.sml

You’ll define utilities that help generate the new instructions.

Familiar SVM files you will modify

opcodes.h

You’ll add opcodes for instructions that allocate a closure, assign to a slot, and read from a slot.

instructions.c

You’ll add table entries for those same instructions, including an unparsing template for readable assembly-language syntax.

vmrun.c

You’ll implement the new instructions, and you’ll touch up your call and tailcall instructions so they can call closures.