about summary refs log tree commit diff
path: root/users/wpcarro/scratch/compiler/register_vm.ml
blob: 0a573048e77edd82c6a7e2f73ff4a82c7e2a729a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(*
  Rewriting the Python implementation of the register VM in OCaml to see how
  how much imperative/mutative programming OCaml allows.

  Note: Some of this code is intentionally not written in a functional style
  because one of the goals was to see how similar this OCaml implementation
  could be to the Python implementation.

  Conclusion: It's pretty easy to switch between the two languages.

  Usage: Recommended compilation settings I hastily found online:
  $ ocamlopt -w +A-42-48 -warn-error +A-3-44 ./register_vm.ml && ./a.out

  Formatting:
  $ ocamlformat --inplace --enable-outside-detected-project ./register_vm.ml
 *)

open Vec

type reg = X | Y | Res
type binop = int -> int -> int

type ast =
  | Const of int
  | Add of ast * ast
  | Sub of ast * ast
  | Mul of ast * ast
  | Div of ast * ast

type opcode0 =
  | Op0AssignRegLit of reg * int
  | Op0AssignRegReg of reg * reg
  | Op0BinOp of binop * reg * reg * reg
  | Op0PushReg of reg
  | Op0PopAndSet of reg
  | Op0Null

type opcode1 =
  | Op1AssignRegLit of int * int
  | Op1AssignRegReg of int * int
  | Op1BinOp of (int -> int -> int) * int * int * int
  | Op1PushReg of int
  | Op1PopAndSet of int
  | Op1Null

type opcodes0 = opcode0 vec
type opcodes1 = opcode1 vec

let registers : int vec = Vec.make 8 0
let stack : int Stack.t = Stack.create ()
let reg_idx (r : reg) : int = match r with X -> 0 | Y -> 1 | Res -> 2

let reg_name (r : reg) : string =
  match r with X -> "x" | Y -> "y" | Res -> "res"

let print_opcodes0 (xs : opcodes0) : opcodes0 =
  let print_opcode x =
    match x with
    | Op0AssignRegLit (r, x) -> Printf.printf "%s <- %d\n" (reg_name r) x
    | Op0AssignRegReg (dst, src) ->
        Printf.printf "%s <- $%s\n" (reg_name dst) (reg_name src)
    | Op0PushReg src -> Printf.printf "push $%s\n" (reg_name src)
    | Op0PopAndSet dst -> Printf.printf "%s <- pop\n" (reg_name dst)
    | Op0BinOp (_, lhs, rhs, dst) ->
        Printf.printf "%s <- $%s ? $%s\n" (reg_name dst) (reg_name lhs)
          (reg_name rhs)
    | Op0Null -> ()
  in
  Vec.iter print_opcode xs;
  xs

let rec compile (ast : ast) : opcodes0 =
  let result : opcodes0 = Vec.create () in
  (match ast with
   | Const x -> Vec.append (Op0AssignRegLit (Res, x)) result;
   | Add (lhs, rhs) -> compile_bin_op ( + ) lhs rhs result
   | Sub (lhs, rhs) -> compile_bin_op ( - ) lhs rhs result
   | Mul (lhs, rhs) -> compile_bin_op ( * ) lhs rhs result
   | Div (lhs, rhs) -> compile_bin_op ( / ) lhs rhs result);
  result

and compile_bin_op (f : binop) (lhs : ast) (rhs : ast) (result : opcodes0) =
  lhs |> compile |> Vec.append_to result;
  Vec.append (Op0PushReg Res) result;
  rhs |> compile |> Vec.append_to result;
  Vec.append (Op0PopAndSet X) result;
  Vec.append (Op0AssignRegReg (Y, Res)) result;
  Vec.append (Op0BinOp (f, X, Y, Res)) result

let compile_registers (xs : opcodes0) : opcodes1 =
  let do_compile x =
    match x with
    | Op0AssignRegLit (dst, x) -> Op1AssignRegLit (reg_idx dst, x)
    | Op0AssignRegReg (dst, src) -> Op1AssignRegReg (reg_idx dst, reg_idx src)
    | Op0PushReg src -> Op1PushReg (reg_idx src)
    | Op0PopAndSet dst -> Op1PopAndSet (reg_idx dst)
    | Op0BinOp (f, lhs, rhs, dst) -> Op1BinOp (f, reg_idx lhs, reg_idx rhs, reg_idx dst)
    | Op0Null -> Op1Null
  in
  Vec.map do_compile xs

let eval (xs : opcodes1) : int =
  let ip = ref 0 in
  while !ip < Vec.length xs do
    match Vec.get_unsafe !ip xs with
    | Op1AssignRegLit (dst, x) ->
        Vec.set dst x registers;
        ip := !ip + 1
    | Op1AssignRegReg (dst, src) ->
        Vec.set dst (Vec.get_unsafe src registers) registers;
        ip := !ip + 1
    | Op1PushReg src ->
        Stack.push (Vec.get_unsafe src registers) stack;
        ip := !ip + 1
    | Op1PopAndSet dst ->
        Vec.set dst (Stack.pop stack) registers;
        ip := !ip + 1
    | Op1BinOp (f, lhs, rhs, dst) ->
        let lhs = Vec.get_unsafe lhs registers in
        let rhs = Vec.get_unsafe rhs registers in
        Vec.set dst (f lhs rhs) registers;
        ip := !ip + 1
    | Op1Null -> ip := !ip + 1
  done;
  Vec.get_unsafe (reg_idx Res) registers
;;

Add (Mul (Const 2, Div (Const 100, Const 2)), Const 5)
|> compile |> print_opcodes0 |> compile_registers |> eval |> print_int