project

“While CPS’ed programs make control-flow explicit, the resulting output is inefficient, and still far removed from the format necessary to enable efficient code generation. For this project, you will simplify CPS programs. While there are many optimizations possible, we will concentrate on particularly important one:
Continuations that are not exported via a Non-Tail call, and which are instantiated
at most once via a Goto, can be inlined at the point of call.”

Anyone else done with Project 3? If not and you want to meet, give me a call: (214) 404-8096. Until then I’ll post some thoughts on the project.
Allthe work is in cps.sml’s cps function. Basically, you’re just connecting work you recursively did with the current computation (whether it’s a binop, apply, tuple, etc)

The cps function has 3 args: ce, ko, and kl. Now you may be wondering “Well when the heck do I use ko and kl??” That’s easy:

ko : this will be NONE when there is nothing left to do after computing ce

kl : this is ALWAYS appended to, and NEVER removed from. You will have to append every continuation created to it

Suggested ordering for implementing the body of cps:

LetRec : Simple let fun ... without applications.

App : Get Tail and NonTail working. This should be the first time you’ll create a continuation. So you’ll need: Label, Id, RetVal, Ret. make sure you prepend the cont you created before returning!

BinOp, UnOp : The tough part’s done (assuming you tested app thoroughly) so these should two should be a breeze. Notes: Take advantage of buildSimpleCont. You can’t test if you are evaluating binops correctly until you have ref‘s done

Fn

Tuple : Here, I used List.foldr 3 times Notes: Take advantage of buildSimpleCont. You can’t test if you are evaluating tuples in the right order until you can create side-effects

Ith, SetIth : These are needed for refs

Retest Binops and Tuples. Create a test and run it in SML/NJ and compare. Note: Assignments in MiniML don’t return unit.

If

Constructor

Error

Once you decide to try out test-map.mml I’d suggest unsetting the noisy flag (cps-interp.sml line 382). Otherwise you’ll be sitting around for hours waiting for the compiler to print.

Here’s some random stuff I found useful after discussing with Phil on “how to start writing ML.”

1. use eval_be to handle binary operations and eval_ue for unary operations.

2. use conv() to handle A.Const_e and A.DataCon_e

3. patMatch() basically handle two cases: “Let val …” and “case … of …” both of these requires pattern matching (that’s why it is there).

4. Before you handling A.Let_e(A.Val_d(…)) in patMatch, it is a good idea to study how A.Case_e() works in patMatch. That should give you some hint on how to proceed.

5. Don’t worry about the A.Id_t(“pat”) or A.Id_t(“patType”) that you found in interp.sml. They are not used at all.

6. http://www.standardml.org/Basis/list-pair.html contains useful function to use when handling A.Tuple_p and A.Record_p in pat_compile. Specifically you need to walk down the tuple/records and perform matching one by one. ListPair is different from List where ListPair walk down two lists simultaneously (your root and p) whereas List do the same thing on one list.

A lot of time with SML, at least initially, will probably be spent fighting with the compiler. Specifically the cryptic compiler errors that are spit out. For those that are not fearful of IDE’s here’s a nifty plugin for eclipse that parses your SML and puts thosse infamous squigglys under the BAD parts of code. It works (hehe) and has been tested but I offer little support for the plugin. Attached is the zip file that you extract unto your eclipse/plugins dir.

The plugin creates a SML perspective and does all the nifty syntax highlighting for your sml (and sig) files. If you have smlnj installed, you can also optionally run the program from withing eclipse, or use MLton to typecheck every time you save (useful, but possibly time consuming). If there is interest, I’ll post how to do all of those.

Unlike the java editor however, it has no debugging/breakpoint support. After all, it’s only written by two people (but eclipse hackers are welcome to contribute!)

If anyone wants to check their project, pasting the code into smlnj is the best way, but if you’re on a linux machine you can run my compiled binary. It has a debug option that will print out the expression tree on every iteration of eval (type “:d” into the interpreter to toggle)