-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.mli
80 lines (76 loc) · 3.21 KB
/
generate.mli
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
(** A few used registers (not a complete list) *)
type register =
| RAX
| RBX
| RCX | CL
| RDX
| RDI
| RSI
| RSP
| RBP
| R08
| R09
| R10
| R11
| R12
| RIP
(** Any way to refer to an assembler variable / value *)
type location =
| Stack of int (** a variable on the stack *)
| Const of int (** a constant value *)
| Globl of string (** a global variable *)
| FnPtr of string (** a function tag *)
| Hexdc of string (** a hexadecimal constant *)
| Regst of register (** a register *)
| Deref of register (** a register read as an address *)
| Index of register * register (** an array access *)
(** A single assembler instruction.
* This is _not_ meant to be a full assembler, but a subset of the available instructions
* useful for our purposes.
*)
type instruction =
| RET (** return *)
| QTO (** convert qword RAX to oword RDX:RAX *)
| LTQ (** convert dword EAX to qword RAX *)
| NOP
| SYS (** syscall *)
| CAL of string (** a function call *)
| FUN of string (** a function declaration *)
| INC of location (** increment *)
| NOT of location (** unary bitwise not *)
| NEG of location (** unary negation *)
| DEC of location (** decrement *)
| DIV of location (** signed division *)
| MUL of location (** signed multiplication *)
| PSH of location (** push to the stack *)
| POP of location (** pop from the stack *)
| TAG of string * string (** declare function.label *)
| JMP of string * string (** jump to function.label *)
| JLE of string * string (** jump to function.label if less or equal *)
| JLT of string * string (** jump to function.label if less *)
| JGE of string * string (** jump to function.label if greater or equal *)
| JGT of string * string (** jump to function.label if greater *)
| JEQ of string * string (** jump to function.label if equal *)
| JNE of string * string (** jump to function.label if not equal *)
| MOV of location * location (** move qword *)
| LEA of location * location (** load address *)
| SUB of location * location (** subtraction *)
| ADD of location * location (** addition *)
| XOR of location * location (** binary bitwise excl. or *)
| SHL of location * location (** left arithmetic shift *)
| SHR of location * location (** right arithmetic shift *)
| AND of location * location (** binary bitwise and *)
| IOR of location * location (** binary bitwise incl. or *)
| CMP of location * location (** compare *)
| TST of location * location (** calc flags for binary bitwise and, used for cmp to zero *)
(** a list of global declarations and assembler instructions *)
type program = {
int: string -> int -> unit; (** declare a global integer variable *)
quad: string -> string -> unit; (** a global integer variable initialized with a string descriptor *)
str: string -> string; (** declare a (possibly new) global string *)
exc: string -> string; (** declare a (possibly new) exception *)
asm: instruction -> string -> unit; (** add a new instruction *)
gen: (out_channel * bool) -> unit; (** dump formatted assembler *)
}
(** blank program *)
val make_prog: unit -> program