annotate lab4/optree.mli @ 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 (* lab4/keiko.mli *)
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 (* |op| -- type of picoPascal operators *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 type op = Plus | Minus | Times | Div | Mod | Eq
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 | Uminus | Lt | Gt | Leq | Geq | Neq
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 | And | Or | Not | Lsl | Lsr | Asr | BitAnd | BitOr | BitNot
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 val fOp : op -> Print.arg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 (* |symbol| -- global symbols *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 type symbol = string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 val nosym : symbol
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 val gensym : unit -> symbol
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 (* |codelab| -- type of code labels *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 type codelab
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 val nolab : codelab
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 (* |label| -- generate a code label *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 val label : unit -> codelab
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 val fLab : codelab -> Print.arg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 (* |inst| -- type of intermediate instructions *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 type inst =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 CONST of int (* Constant (value) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 | GLOBAL of symbol (* Global address (symbol) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 | LOCAL of int (* Local address (offset) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 | REGVAR of int (* Register (index) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 | LOADC (* Load char *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 | LOADW (* Load word *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 | STOREC (* Store char *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 | STOREW (* Store word *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 | ARG of int (* Pass argument (index) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 | SLINK (* Pass static link *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 | PCALL of int (* Call procedure (nparams) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 | RESULTW (* Procedure result *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 | MONOP of op (* Perform unary operation (op) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 | BINOP of op (* Perform binary operation (op) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 | OFFSET (* Add address and offset *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 | BOUND (* Array bound check *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 | NCHECK (* Null pointer check *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 | LABEL of codelab (* Set code label *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 | JUMP of codelab (* Unconditional branch (dest) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 | JUMPC of op * codelab (* Conditional branch (cond, dest) *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 | JCASE of codelab list * codelab (* Jump table *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 (* Extra instructions *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 | LINE of int (* Line number *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 | NOP
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 | SEQ
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 | AFTER (* Expression with side effect *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 | DEFTMP of int
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 | TEMP of int (* Temporary *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 (* |Inst| -- printf format for instructions *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 val fInst : inst -> Print.arg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 (* |do_monop| -- evaluate unary operation *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 val do_monop : op -> int -> int
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 (* |do_binop| -- evaluate binary operation *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 val do_binop : op -> int -> int -> int
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 (* |negate| -- find opposite for comparison op *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 val negate : op -> op
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 (* Operator trees *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 (* |optree| -- type of operator trees *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 type optree = Node of inst * optree list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 (* |canon| -- eliminate SEQ, NOP, AFTER nodes *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 val canon : optree -> optree list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 (* |flatten| -- move args before calls *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 val flatten : optree list -> optree list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 (* |print_optree| -- output operator tree on stdout with line breaking *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 val print_optree : string -> optree -> unit