comparison ppc/keiko.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 (* ppc/keiko.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Print
5
6 (* |symbol| -- global symbols *)
7 type symbol = string
8
9 type codelab = int
10
11 let nolab = -1
12
13 (* |lab| -- last used code label *)
14 let lab = ref 0
15
16 (* |label| -- allocate a code label *)
17 let label () = incr lab; !lab
18
19 (* |fLab| -- format a code label for printf *)
20 let fLab n = fMeta "L$" [fNum n]
21
22 let nosym = "*nosym*"
23
24 let gensym () = sprintf "g$" [fNum (label ())]
25
26 (* |op| -- type of picoPascal operators *)
27 type op = Plus | Minus | Times | Div | Mod | Eq
28 | Uminus | Lt | Gt | Leq | Geq | Neq | And | Or | Not
29 | Lsl | Lsr | Asr | BitAnd | BitOr | BitNot
30
31 (* |code| -- type of intermediate instructions *)
32 type code =
33 CONST of int (* Constant (value) *)
34 | GLOBAL of symbol (* Constant (symbol) *)
35 | LOCAL of int (* Local address (offset) *)
36 | LOAD of int (* Load (size) *)
37 | STORE of int (* Store (size) *)
38 | FIXCOPY (* Copy multiple values (size) *)
39 | PCALL of int * int (* Call procedure (nparams, rsize) *)
40 | RETURN of int (* Procedure return (rsize) *)
41 | MONOP of op (* Perform unary operation (op) *)
42 | BINOP of op (* Perform binary operation (op) *)
43 | OFFSET (* Add address and offset *)
44 | BOUND of int (* Array bound check (line) *)
45 | NCHECK of int (* Null pointer check (line) *)
46 | ERETURN of int (* Failure to return (line) *)
47 | LABEL of codelab (* Set code label *)
48 | JUMP of codelab (* Unconditional branch (dest) *)
49 | JUMPC of op * codelab (* Conditional branch (cond, dest) *)
50 | JCASE of codelab list (* Jump table *)
51 | LINE of int (* Line number *)
52
53 | LDL of int * int (* LDL (n, s) = LOCAL n / LOAD s *)
54 | STL of int * int (* STL (n, s) = LOCAL n / STORE s *)
55 | LDG of symbol * int (* LDG (x, s) = GLOBAL x / LOAD s *)
56 | STG of symbol * int (* STG (x, s) = GLOBAL x / STORE s *)
57 | LDNW of int (* LDNW n = CONST n / OFFSET / LOAD 4 *)
58 | STNW of int (* STNW n = CONST n / OFFSET / STORE 4 *)
59 | LDI of int (* LDI s = CONST s / TIMES / OFFSET / LOAD s *)
60 | STI of int (* STI s = CONST s / TIMES / OFFSET / STORE s *)
61 | JUMPCZ of op * codelab (* Conditional branch with zero (cond, dest) *)
62
63 | SEQ of code list (* Sequence of other instructions *)
64 | NOP (* Null operation *)
65
66 let mark_line n ys =
67 if n = 0 then ys else
68 match ys with
69 [] | LINE _ :: _ -> ys
70 | _ -> LINE n :: ys
71
72 let canon x =
73 let rec accum x ys =
74 match x with
75 SEQ xs -> List.fold_right accum xs ys
76 | NOP -> ys
77 | LINE n -> mark_line n ys
78 | _ -> x :: ys in
79 SEQ (accum x [])
80
81 let op_name =
82 function
83 Plus -> "PLUS" | Minus -> "MINUS" | Times -> "TIMES"
84 | Div -> "DIV" | Mod -> "MOD" | Eq -> "EQ"
85 | Uminus -> "UMINUS" | Lt -> "LT" | Gt -> "GT"
86 | Leq -> "LEQ" | Geq -> "GEQ" | Neq -> "NEQ"
87 | And -> "AND" | Or -> "OR" | Not -> "NOT"
88 | Lsl -> "LSL" | Lsr -> "LSR" | Asr -> "ASR"
89 | BitAnd -> "BITAND" | BitOr -> "BITOR" | BitNot -> "BITNOT"
90
91 let fOp w = fStr (op_name w)
92
93 let fType =
94 function 1 -> fStr "C" | 4 -> fStr "W" | s -> fMeta "*$*" [fNum s]
95
96 let fType1 =
97 function 0 -> fStr "" | 1 -> fStr "W" | s -> fMeta "*$*" [fNum s]
98
99 let fInst =
100 function
101 CONST x -> fMeta "CONST $" [fNum x]
102 | GLOBAL a -> fMeta "GLOBAL $" [fStr a]
103 | LOCAL n -> fMeta "LOCAL $" [fNum n]
104 | LOAD s -> fMeta "LOAD$" [fType s]
105 | STORE s -> fMeta "STORE$" [fType s]
106 | FIXCOPY -> fStr "FIXCOPY"
107 | PCALL (n, s) -> fMeta "PCALL$ $" [fType1 s; fNum n]
108 | RETURN s -> fMeta "RETURN$" [fType1 s]
109 | MONOP w -> fStr (op_name w)
110 | BINOP w -> fStr (op_name w)
111 | OFFSET -> fStr "OFFSET"
112 | BOUND n -> fMeta "BOUND $" [fNum n]
113 | NCHECK n -> fMeta "NCHECK $" [fNum n]
114 | ERETURN n -> fMeta "ERROR E_RETURN $" [fNum n]
115 | LABEL l -> fMeta "LABEL $" [fLab l]
116 | JUMP l -> fMeta "JUMP $" [fLab l]
117 | JUMPC (w, l) -> fMeta "J$ $" [fStr (op_name w); fLab l]
118 | JCASE labs -> fMeta "JCASE $" [fNum (List.length labs)]
119 | LINE n -> fMeta "LINE $" [fNum n]
120
121 | LDL (n, s) -> fMeta "LDL$ $" [fType s; fNum n]
122 | STL (n, s) -> fMeta "STL$ $" [fType s; fNum n]
123 | LDG (x, s) -> fMeta "LDG$ $" [fType s; fStr x]
124 | STG (x, s) -> fMeta "STG$ $" [fType s; fStr x]
125 | LDNW n -> fMeta "LDNW $" [fNum n]
126 | STNW n -> fMeta "STNW $" [fNum n]
127 | LDI s -> fMeta "LDI$" [fType s]
128 | STI s -> fMeta "STI$" [fType s]
129 | JUMPCZ (w, lab) -> fMeta "J$Z $" [fStr (op_name w); fLab lab]
130
131 | SEQ _ -> fStr "SEQ ..."
132 | NOP -> fStr "NOP"
133
134 (* |output| -- output code sequence *)
135 let output code =
136 let line = ref 0 in
137 let rec out =
138 function
139 SEQ xs -> List.iter out xs
140 | NOP -> ()
141 | LINE n ->
142 if !line <> n then begin
143 printf "! $\n" [fStr (Source.get_line n)];
144 line := n
145 end
146 | JCASE labs ->
147 printf "$\n" [fInst (JCASE labs)];
148 List.iter (fun lab -> printf "CASEL $\n" [fLab lab]) labs
149 | x -> printf "$\n" [fInst x] in
150 out code
151
152
153 let int_of_bool b = if b then 1 else 0
154
155 (* |do_monop| -- evaluate unary operators *)
156 let do_monop w x =
157 match w with
158 Uminus -> - x
159 | Not -> if x <> 0 then 0 else 1
160 | BitNot -> lnot x
161 | _ -> failwith "do_monop"
162
163 (* |do_binop| -- evaluate binary operators *)
164 let do_binop w x y =
165 match w with
166 Plus -> x + y
167 | Minus -> x - y
168 | Times -> x * y
169 | Div -> x / y
170 | Mod -> x mod y
171 | Eq -> int_of_bool (x = y)
172 | Lt -> int_of_bool (x < y)
173 | Gt -> int_of_bool (x > y)
174 | Leq -> int_of_bool (x <= y)
175 | Geq -> int_of_bool (x >= y)
176 | Neq -> int_of_bool (x <> y)
177 | And -> if x <> 0 then y else 0
178 | Or -> if x <> 0 then 1 else y
179 | Lsl -> x lsl y
180 | Lsr -> x lsr y
181 | Asr -> x asr y
182 | BitAnd -> x land y
183 | BitOr -> x lor y
184 | _ -> failwith (sprintf "do_binop $" [fOp w])
185
186 (* |negate| -- negation of a comparison *)
187 let negate =
188 function Eq -> Neq | Neq -> Eq | Lt -> Geq
189 | Leq -> Gt | Gt -> Leq | Geq -> Lt
190 | _ -> failwith "negate"