This module has three new .sml
files: asmutil.sml
, knrename.sml
, and codegen.sml
.
Code you will look at and understand (and edit one line of)
asmutil.sml
This file provides a kind of a compatibility layer used to build assembly-language instructions. The benefits are primarily notational:
Instead of dealing with value constructors, you have curried functions.
The interface distinguishes “instruction that sets a register” from “instruction that has a side effect.” Moreover, it checks that each primitive operator is used with the correct instruction-building function.
For instructions that set a destination register, functions
setreg
andsetregLit
split out the destination register into a distinct parameter, which makes it easier to build instructions in the code generator.There is also a function
newlabel
that creates a fresh label, which can be used withgoto
,ifgoto
, anddeflabel
.
You’ll want to familiarize yourself with the interface before you write the code generator.
All the functions are already implemented except
copyreg
. To implementcopyreg
, you need to know what opcode your SVM uses for a register-register move. Once you know that, you can code it usingsetreg
.
Code you will write or edit
knrename.sml
In this file you will define a “renamer” that can change the representation of a name in an expression in K-normal form. The change we are interested in is to change string names (from a disk file) into register names (for code generation). That function on names is implemented for you in the lexical analyzer for assembly language. What remains is for you to lift a transformation on names into a transformation on expressions. That’s the role of function
mapx
, which you will implement. Becausemapx
is polymorphic, the implementation is going to be hard to get wrong.codegen.sml
This file is a template for your code generator, which converts K-normal form to assembly code. Because the code generator manipulates a lot of lists, the file also contains some simple functions for implementing lists in the style of John Hughes (1986). This style enables two lists to be appended in constant time by using the function-composition operator
o
.primitives.sml
The list of primitives that the UFT “understands.” You’ve used this interface during disambiguation (module 5). In module 6, you are likely to need to extend the list in the primitive-implementation step for the module.