✨
This commit is contained in:
@ -156,7 +156,7 @@ let dynalloc (size_reg : Mips.reg,
|
||||
; Mips.ADD (HP, HP, tmp_reg)
|
||||
; Mips.SW (size_reg, place, 0) ]
|
||||
|
||||
code1 @ code2 @ code3
|
||||
[Mips.COMMENT "dynalloc"] @ code1 @ code2 @ code3
|
||||
|
||||
(* Pushing arguments on the stack: *)
|
||||
(* For each register 'r' in 'rs', copy them to registers from
|
||||
@ -540,7 +540,8 @@ let rec compileExp (e : TypedExp)
|
||||
; Mips.J loop_beg
|
||||
; Mips.LABEL loop_end
|
||||
]
|
||||
arr_code
|
||||
[Mips.COMMENT "map"]
|
||||
@ arr_code
|
||||
@ get_size
|
||||
@ dynalloc (size_reg, place, ret_type)
|
||||
@ init_regs
|
||||
|
@ -33,21 +33,21 @@ let rec copyConstPropFoldExp (vtable : VarTable)
|
||||
| Some (ConstProp x) -> Constant (x, pos)
|
||||
| Some (VarProp x) -> Var (x, pos)
|
||||
| _ -> Var (name, pos)
|
||||
| Index (name, e, 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 e' = copyConstPropFoldExp vtable e
|
||||
let e2' = copyConstPropFoldExp vtable e2
|
||||
match name' with
|
||||
| Some (VarProp x) -> Index (x, e', t, pos)
|
||||
| _ -> Index (name, e', t, pos)
|
||||
| Some (VarProp x) -> Index (x, e2', t, pos)
|
||||
| _ -> Index (name, e2', t, pos)
|
||||
|
||||
| Let (Dec (name, e, decpos), body, pos) ->
|
||||
let e' = copyConstPropFoldExp vtable e
|
||||
match e' with
|
||||
| Var (name', pos') ->
|
||||
| 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
|
||||
@ -57,7 +57,7 @@ let rec copyConstPropFoldExp (vtable : VarTable)
|
||||
let vtable' = SymTab.bind name (VarProp name') vtable
|
||||
let body' = copyConstPropFoldExp vtable' body
|
||||
Let (Dec (name, e', decpos), body', pos)
|
||||
| Constant (constval, pos') ->
|
||||
| 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
|
||||
@ -67,7 +67,7 @@ let rec copyConstPropFoldExp (vtable : VarTable)
|
||||
let vtable' = SymTab.bind name (ConstProp constval) vtable
|
||||
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) ->
|
||||
(* TODO project task 3:
|
||||
Hint: this has the structure
|
||||
`let y = (let x = e1 in e2) in e3`
|
||||
@ -79,8 +79,10 @@ let rec copyConstPropFoldExp (vtable : VarTable)
|
||||
restructured, semantically-equivalent expression:
|
||||
`let x = e1 in let y = e2 in e3`
|
||||
*)
|
||||
let let' = Let (Dec (name, body', decpos), body, pos)
|
||||
copyConstPropFoldExp vtable (Let (Dec (name', e'', decpos'), let', pos'))
|
||||
let e2' = copyConstPropFoldExp vtable e2
|
||||
let body2' = copyConstPropFoldExp vtable body2
|
||||
let body' = copyConstPropFoldExp vtable body
|
||||
Let (Dec (name, Let (Dec (name2, e2', decpos2), body2', pos2), decpos), body', pos)
|
||||
| _ -> (* Fallthrough - for everything else, do nothing *)
|
||||
let body' = copyConstPropFoldExp vtable body
|
||||
Let (Dec (name, e', decpos), body', pos)
|
||||
@ -92,11 +94,33 @@ let rec copyConstPropFoldExp (vtable : VarTable)
|
||||
1 * x = ?
|
||||
x * 0 = ?
|
||||
*)
|
||||
Times (e1, e2, pos)
|
||||
let e1' = copyConstPropFoldExp vtable e1
|
||||
let e2' = copyConstPropFoldExp vtable e2
|
||||
match (e1', e2') with
|
||||
| (Constant (IntVal x, _), Constant (IntVal y, _)) ->
|
||||
Constant (IntVal (x * y), pos)
|
||||
| (Constant (IntVal 0, _), _) ->
|
||||
Constant (IntVal 0, pos)
|
||||
| (_, Constant (IntVal 0, _)) ->
|
||||
Constant (IntVal 0, pos)
|
||||
| (Constant (IntVal 1, _), _) ->
|
||||
e2'
|
||||
| (_, Constant (IntVal 1, _)) ->
|
||||
e1'
|
||||
| _ -> Times (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 *)
|
||||
And (e1, e2, pos)
|
||||
let e1' = copyConstPropFoldExp vtable e1
|
||||
let e2' = copyConstPropFoldExp vtable e2
|
||||
match (e1', e2') with
|
||||
| (Constant (BoolVal a, _), Constant (BoolVal b, _)) ->
|
||||
Constant (BoolVal (a && b), pos)
|
||||
| (Constant (BoolVal false, _), _) ->
|
||||
Constant (BoolVal false, pos)
|
||||
| (_, Constant (BoolVal false, _)) ->
|
||||
Constant (BoolVal false, pos)
|
||||
| _ -> And (e1', e2', pos)
|
||||
| Constant (x,pos) -> Constant (x,pos)
|
||||
| StringLit (x,pos) -> StringLit (x,pos)
|
||||
| ArrayLit (es, t, pos) ->
|
||||
|
@ -149,7 +149,9 @@ let parseFastoFile (filename : string) : AbSyn.UntypedProg =
|
||||
let compile (filename : string) optimiser : Unit =
|
||||
let pgm = parseFastoFile filename
|
||||
let pgm_decorated = TypeChecker.checkProg pgm
|
||||
//printfn "%A" pgm_decorated
|
||||
let pgm_optimised = optimiser pgm_decorated
|
||||
//printfn "%A" pgm_optimised
|
||||
let mips_code = CodeGen.compile pgm_optimised
|
||||
let mips_code_text = Mips.ppMipsProg mips_code
|
||||
saveFile (filename + ".asm") mips_code_text
|
||||
|
Reference in New Issue
Block a user