✨
This commit is contained in:
@@ -22,22 +22,12 @@ let rec copyConstPropFoldExp (vtable : VarTable)
|
|||||||
(* Copy propagation is handled entirely in the following three
|
(* Copy propagation is handled entirely in the following three
|
||||||
cases for variables, array indexing, and let-bindings. *)
|
cases for variables, array indexing, and let-bindings. *)
|
||||||
| Var (name, pos) ->
|
| Var (name, pos) ->
|
||||||
(* TODO project task 3:
|
|
||||||
Should probably look in the symbol table to see if
|
|
||||||
a binding corresponding to the current variable `name`
|
|
||||||
exists and if so, it should replace the current expression
|
|
||||||
with the variable or constant to be propagated.
|
|
||||||
*)
|
|
||||||
let name' = SymTab.lookup name vtable
|
let name' = SymTab.lookup name vtable
|
||||||
match name' with
|
match name' with
|
||||||
| Some (ConstProp x) -> Constant (x, pos)
|
| Some (ConstProp x) -> Constant (x, pos)
|
||||||
| Some (VarProp x) -> Var (x, pos)
|
| Some (VarProp x) -> Var (x, pos)
|
||||||
| _ -> Var (name, pos)
|
| _ -> Var (name, pos)
|
||||||
| Index (name, e2, t, pos) ->
|
| Index (name, e2, t, pos) ->
|
||||||
(* TODO project task 3:
|
|
||||||
Should probably do the same as the `Var` case, for
|
|
||||||
the array name, and optimize the index expression `e` as well.
|
|
||||||
*)
|
|
||||||
let name' = SymTab.lookup name vtable
|
let name' = SymTab.lookup name vtable
|
||||||
let e2' = copyConstPropFoldExp vtable e2
|
let e2' = copyConstPropFoldExp vtable e2
|
||||||
match name' with
|
match name' with
|
||||||
@@ -48,37 +38,14 @@ let rec copyConstPropFoldExp (vtable : VarTable)
|
|||||||
let e' = copyConstPropFoldExp vtable e
|
let e' = copyConstPropFoldExp vtable e
|
||||||
match e' with
|
match e' with
|
||||||
| Var (name', _) ->
|
| Var (name', _) ->
|
||||||
(* TODO project task 3:
|
|
||||||
Hint: I have discovered a variable-copy statement `let x = a`.
|
|
||||||
I should probably record it in the `vtable` by
|
|
||||||
associating `x` with a variable-propagatee binding,
|
|
||||||
and optimize the `body` of the let.
|
|
||||||
*)
|
|
||||||
let vtable' = SymTab.bind name (VarProp name') vtable
|
let vtable' = SymTab.bind name (VarProp name') vtable
|
||||||
let body' = copyConstPropFoldExp vtable' body
|
let body' = copyConstPropFoldExp vtable' body
|
||||||
Let (Dec (name, e', decpos), body', pos)
|
Let (Dec (name, e', decpos), body', pos)
|
||||||
| Constant (constval, _) ->
|
| Constant (constval, _) ->
|
||||||
(* TODO project task 3:
|
|
||||||
Hint: I have discovered a constant-copy statement `let x = 5`.
|
|
||||||
I should probably record it in the `vtable` by
|
|
||||||
associating `x` with a constant-propagatee binding,
|
|
||||||
and optimize the `body` of the let.
|
|
||||||
*)
|
|
||||||
let vtable' = SymTab.bind name (ConstProp constval) vtable
|
let vtable' = SymTab.bind name (ConstProp constval) vtable
|
||||||
let body' = copyConstPropFoldExp vtable' body
|
let body' = copyConstPropFoldExp vtable' body
|
||||||
Let (Dec (name, e', decpos), body', pos)
|
Let (Dec (name, e', decpos), body', pos)
|
||||||
| Let (Dec (name2, e2, decpos2), body2, pos2) ->
|
| Let (Dec (name2, e2, decpos2), body2, pos2) ->
|
||||||
(* TODO project task 3:
|
|
||||||
Hint: this has the structure
|
|
||||||
`let y = (let x = e1 in e2) in e3`
|
|
||||||
Problem is, in this form, `e2` may simplify
|
|
||||||
to a variable or constant, but I will miss
|
|
||||||
identifying the resulting variable/constant-copy
|
|
||||||
statement on `y`.
|
|
||||||
A potential solution is to optimize directly the
|
|
||||||
restructured, semantically-equivalent expression:
|
|
||||||
`let x = e1 in let y = e2 in e3`
|
|
||||||
*)
|
|
||||||
let e2' = copyConstPropFoldExp vtable e2
|
let e2' = copyConstPropFoldExp vtable e2
|
||||||
let body2' = copyConstPropFoldExp vtable body2
|
let body2' = copyConstPropFoldExp vtable body2
|
||||||
let body' = copyConstPropFoldExp vtable body
|
let body' = copyConstPropFoldExp vtable body
|
||||||
@@ -88,12 +55,6 @@ let rec copyConstPropFoldExp (vtable : VarTable)
|
|||||||
Let (Dec (name, e', decpos), body', pos)
|
Let (Dec (name, e', decpos), body', pos)
|
||||||
|
|
||||||
| Times (e1, e2, pos) ->
|
| Times (e1, e2, pos) ->
|
||||||
(* TODO project task 3: implement as many safe algebraic
|
|
||||||
simplifications as you can think of. You may inspire
|
|
||||||
yourself from the case of `Plus`. For example:
|
|
||||||
1 * x = ?
|
|
||||||
x * 0 = ?
|
|
||||||
*)
|
|
||||||
let e1' = copyConstPropFoldExp vtable e1
|
let e1' = copyConstPropFoldExp vtable e1
|
||||||
let e2' = copyConstPropFoldExp vtable e2
|
let e2' = copyConstPropFoldExp vtable e2
|
||||||
match (e1', e2') with
|
match (e1', e2') with
|
||||||
@@ -109,8 +70,6 @@ let rec copyConstPropFoldExp (vtable : VarTable)
|
|||||||
e1'
|
e1'
|
||||||
| _ -> Times (e1, e2, pos)
|
| _ -> Times (e1, e2, pos)
|
||||||
| And (e1, e2, pos) ->
|
| And (e1, e2, pos) ->
|
||||||
(* TODO project task 3: see above. You may inspire yourself from
|
|
||||||
`Or` below, but that only scratches the surface of what's possible *)
|
|
||||||
let e1' = copyConstPropFoldExp vtable e1
|
let e1' = copyConstPropFoldExp vtable e1
|
||||||
let e2' = copyConstPropFoldExp vtable e2
|
let e2' = copyConstPropFoldExp vtable e2
|
||||||
match (e1', e2') with
|
match (e1', e2') with
|
||||||
|
|||||||
Reference in New Issue
Block a user