annotate lab1/kgen.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* lab1/kgen.ml *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 (* Copyright (c) 2017 J. M. Spivey *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 open Tree
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 open Keiko
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 let optflag = ref false
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 (* |gen_expr| -- generate code for an expression *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 let rec gen_expr =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 function
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 Constant x ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 CONST x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 | Variable x ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 SEQ [LINE x.x_line; LDGW x.x_lab]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 | Monop (w, e1) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 SEQ [gen_expr e1; MONOP w]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 | Binop (w, e1, e2) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 SEQ [gen_expr e1; gen_expr e2; BINOP w]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 (* |gen_cond| -- generate code for short-circuit condition *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 let rec gen_cond e tlab flab =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 (* Jump to |tlab| if |e| is true and |flab| if it is false *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 match e with
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 Constant x ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 if x <> 0 then JUMP tlab else JUMP flab
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 | Binop ((Eq|Neq|Lt|Gt|Leq|Geq) as w, e1, e2) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 SEQ [gen_expr e1; gen_expr e2; JUMPC (w, tlab); JUMP flab]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 | Monop (Not, e1) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 gen_cond e1 flab tlab
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 | Binop (And, e1, e2) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 let lab1 = label () in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 SEQ [gen_cond e1 lab1 flab; LABEL lab1; gen_cond e2 tlab flab]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 | Binop (Or, e1, e2) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 let lab1 = label () in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 SEQ [gen_cond e1 tlab lab1; LABEL lab1; gen_cond e2 tlab flab]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 | _ ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 SEQ [gen_expr e; CONST 0; JUMPC (Neq, tlab); JUMP flab]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 (* |gen_stmt| -- generate code for a statement *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 let rec gen_stmt s =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 match s with
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 Skip -> NOP
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 | Seq stmts -> SEQ (List.map gen_stmt stmts)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 | Assign (v, e) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 SEQ [LINE v.x_line; gen_expr e; STGW v.x_lab]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 | Print e ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 SEQ [gen_expr e; CONST 0; GLOBAL "lib.print"; PCALL 1]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 | Newline ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 SEQ [CONST 0; GLOBAL "lib.newline"; PCALL 0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 | IfStmt (test, thenpt, elsept) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 let lab1 = label () and lab2 = label () and lab3 = label () in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 SEQ [gen_cond test lab1 lab2;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 LABEL lab1; gen_stmt thenpt; JUMP lab3;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 LABEL lab2; gen_stmt elsept; LABEL lab3]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 | WhileStmt (test, body) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 let lab1 = label () and lab2 = label () and lab3 = label () in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 SEQ [JUMP lab2; LABEL lab1; gen_stmt body;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 LABEL lab2; gen_cond test lab1 lab3; LABEL lab3]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 (* |translate| -- generate code for the whole program *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 let translate (Program ss) =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 let code = gen_stmt ss in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 Keiko.output (if !optflag then Peepopt.optimise code else code)