snoeathurc.h

This commit is contained in:
NikolajDanger
2022-05-18 17:29:24 +02:00
parent f0fda0bcd7
commit 5f7c450809
20 changed files with 6298 additions and 2039 deletions

View File

@ -194,8 +194,10 @@ let rec compileExp (e : TypedExp)
[ Mips.LUI (place, n / 65536)
; Mips.ORI (place, place, n % 65536) ]
| Constant (BoolVal p, _) ->
(* TODO project task 1: represent `true`/`false` values as `1`/`0` *)
failwith "Unimplemented code generation of boolean constants"
match p with
| true -> [Mips.LI (place, 1)]
| false -> [Mips.LI (place, 0)]
| Constant (CharVal c, pos) -> [ Mips.LI (place, int c) ]
(* Create/return a label here, collect all string literals of the program
@ -263,17 +265,30 @@ let rec compileExp (e : TypedExp)
version, but remember to come back and clean it up later.
`Not` and `Negate` are simpler; you can use `Mips.XORI` for `Not`
*)
| Times (_, _, _) ->
failwith "Unimplemented code generation of multiplication"
| Times (e1, e2, pos) ->
let t1 = newReg "times_L"
let t2 = newReg "times_R"
let code1 = compileExp e1 vtable t1
let code2 = compileExp e2 vtable t2
code1 @ code2 @ [Mips.MUL (place,t1,t2)]
| Divide (_, _, _) ->
failwith "Unimplemented code generation of division"
| Divide (e1, e2, pos) ->
let t1 = newReg "divide_L"
let t2 = newReg "divide_R"
let code1 = compileExp e1 vtable t1
let code2 = compileExp e2 vtable t2
code1 @ code2 @ [Mips.DIV (place,t1,t2)]
| Not (_, _) ->
failwith "Unimplemented code generation of not"
| Not (e, pos) ->
let t = newReg "not_R"
let code = compileExp e vtable t
code @ [Mips.XORI (place,t,0)]
| Negate (_, _) ->
failwith "Unimplemented code generation of negate"
| Negate (e, pos) ->
let t = newReg "negate_R"
let R0 = Mips.RN 0
let code = compileExp e vtable t
code @ [Mips.SUB (place,R0,t)]
| Let (dec, e1, pos) ->
let (code1, vtable1) = compileDec dec vtable
@ -389,11 +404,19 @@ let rec compileExp (e : TypedExp)
in `e1 || e2` if the execution of `e1` will evaluate to `true` then
the code of `e2` must not be executed. Similarly for `And` (&&).
*)
| And (_, _, _) ->
failwith "Unimplemented code generation of &&"
| And (e1, e2, pos) ->
let R0 = Mips.RS "0"
let label = newLab "false"
let code1 = compileExp e1 vtable place
let code2 = compileExp e2 vtable place
code1 @ [Mips.BEQ (place, R0, label)] @ code2 @ [Mips.LABEL label]
| Or (_, _, _) ->
failwith "Unimplemented code generation of ||"
| Or (e1, e2, pos) ->
let R0 = Mips.RS "0"
let label = newLab "true"
let code1 = compileExp e1 vtable place
let code2 = compileExp e2 vtable place
code1 @ [Mips.BNE (place, R0, label)] @ code2 @ [Mips.LABEL label]
(* Indexing:
1. generate code to compute the index