comparison lab2/kgen.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* lab2/kgen.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Dict
5 open Tree
6 open Keiko
7 open Print
8
9 let optflag = ref false
10
11 (* |line_number| -- find line number of variable reference *)
12 let rec line_number e =
13 match e.e_guts with
14 Variable x -> x.x_line
15 | Sub (a, e) -> line_number a
16 | _ -> 999
17
18 (* |gen_expr| -- generate code for an expression *)
19 let rec gen_expr e =
20 match e.e_guts with
21 Variable _ | Sub _ ->
22 SEQ [gen_addr e; LOADW]
23 | Constant (n, t) ->
24 CONST n
25 | Monop (w, e1) ->
26 SEQ [gen_expr e1; MONOP w]
27 | Binop (w, e1, e2) ->
28 SEQ [gen_expr e1; gen_expr e2; BINOP w]
29
30 (* |gen_addr| -- generate code to push address of a variable *)
31 and gen_addr v =
32 match v.e_guts with
33 Variable x ->
34 let d = get_def x in
35 SEQ [LINE x.x_line; GLOBAL d.d_lab]
36 | _ ->
37 failwith "gen_addr"
38
39 (* |gen_cond| -- generate code for short-circuit condition *)
40 let rec gen_cond e tlab flab =
41 (* Jump to |tlab| if |e| is true and |flab| if it is false *)
42 match e.e_guts with
43 Constant (x, t) ->
44 if x <> 0 then JUMP tlab else JUMP flab
45 | Binop ((Eq|Neq|Lt|Gt|Leq|Geq) as w, e1, e2) ->
46 SEQ [gen_expr e1; gen_expr e2;
47 JUMPC (w, tlab); JUMP flab]
48 | Monop (Not, e1) ->
49 gen_cond e1 flab tlab
50 | Binop (And, e1, e2) ->
51 let lab1 = label () in
52 SEQ [gen_cond e1 lab1 flab; LABEL lab1; gen_cond e2 tlab flab]
53 | Binop (Or, e1, e2) ->
54 let lab1 = label () in
55 SEQ [gen_cond e1 tlab lab1; LABEL lab1; gen_cond e2 tlab flab]
56 | _ ->
57 SEQ [gen_expr e; CONST 0; JUMPC (Neq, tlab); JUMP flab]
58
59 (* |gen_stmt| -- generate code for a statement *)
60 let rec gen_stmt =
61 function
62 Skip -> NOP
63 | Seq stmts -> SEQ (List.map gen_stmt stmts)
64 | Assign (v, e) ->
65 SEQ [LINE (line_number v); gen_expr e; gen_addr v; STOREW]
66 | Print e ->
67 SEQ [gen_expr e; CONST 0; GLOBAL "lib.print"; PCALL 1]
68 | Newline ->
69 SEQ [CONST 0; GLOBAL "lib.newline"; PCALL 0]
70 | IfStmt (test, thenpt, elsept) ->
71 let lab1 = label () and lab2 = label () and lab3 = label () in
72 SEQ [gen_cond test lab1 lab2;
73 LABEL lab1; gen_stmt thenpt; JUMP lab3;
74 LABEL lab2; gen_stmt elsept; LABEL lab3]
75 | WhileStmt (test, body) ->
76 let lab1 = label () and lab2 = label () and lab3 = label () in
77 SEQ [JUMP lab2; LABEL lab1; gen_stmt body;
78 LABEL lab2; gen_cond test lab1 lab3; LABEL lab3]
79
80 let gen_decl (Decl (xs, t)) =
81 List.iter (fun x ->
82 let d = get_def x in
83 let s = 4 in
84 printf "GLOVAR $ $\n" [fStr d.d_lab; fNum s]) xs
85
86 (* |translate| -- generate code for the whole program *)
87 let translate (Program (ds, ss)) =
88 let code = gen_stmt ss in
89 printf "PROC MAIN 0 0 0\n" [];
90 Keiko.output (if !optflag then Peepopt.optimise code else code);
91 printf "RETURN\n" [];
92 printf "END\n\n" [];
93 List.iter gen_decl ds