-
Notifications
You must be signed in to change notification settings - Fork 1
/
icode.ml
118 lines (104 loc) · 2.59 KB
/
icode.ml
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
open Ast
open Symboltable
(* TODO support differentiated data sizes *)
(* let int_sz = 4
let ptr_sz = 4
let char_sz = 1
let bool_sz = 1 *)
let data_sz = 4
let main_method_name = "_$DecafMain"
let mangle_name classname name is_constructor =
if is_constructor then
classname ^ "$$init$"
else
classname ^ "$" ^ name
let vtable_name classname =
"_V$" ^ classname
type icRegister =
Eax
| Ebx
| Ecx
| Edx
| Esi
| Edi
| Ebp
| Esp
| InvalidReg
type icClassRecord =
{ name: string
; super: icClassRecord option
; mutable size: int
; field_offset_table: int symbol_table
; method_offset_table: int symbol_table
(* Holds which classes actually declare inherited methods *)
; method_inherited_table: string symbol_table
}
type icBinOp =
Move
| Add
| Sub
| Mult
| Div
| And
| Or
| Mod
| Xor
| Lea
| Greater
| Less
| Equals
| Geq
| Leq
| Neq
type icUnOp =
Pos
| Neg
| Not
type icLiteral =
IntLiteral of int
| CharLiteral of char
| BoolLiteral of bool
| NullLiteral
type icLoc =
| LiteralVal of icLiteral
(* offset, NOT id*)
| LocalVal of int
| IdVal of string
(* TODO verbatimval isn't necessary if you make all IdVals
intended to be verbatim by turning ids into offsets before code generation *)
(* Do not even look up string - just write it verbatim*)
| VerbatimVal of string
| RegisterVal of icRegister
(* | ArrayLocationVal of icLoc * astType * icLoc *)
(* | FieldLocationVal of icLoc * int *)
type icStatement =
| BinStatement of icBinOp * icLoc * icLoc
| UnStatement of icUnOp * icLoc
(* destination, array, index *)
| ArrayStatement of icLoc * icLoc * icLoc
(* destination, type, index args *)
| NewArrayStatement of icLoc * astType * icLoc list
(* destination, class template, constructor args *)
| NewObjStatement of icLoc * icClassRecord * icLoc list
(* destination, callee, vtable offset, args *)
| MethodCallStatement of icLoc * icLoc * int * icLoc list
(* destination, name, args *)
| StaticMethodCallStatement of icLoc * icLoc * icLoc list
(* destination, object, field offset *)
| FieldAccessStatement of icLoc * icLoc * int
| IfStatement of int symbol_table * icLoc * icStatement list *
icStatement list
| WhileStatement of int symbol_table * icLoc * icStatement list
| ReturnStatement of icLoc option
| ContinueStatement
| BreakStatement
(* args *)
| SuperStatement of icLoc list
type icMethodFrame =
{ name: string
; c: icClassRecord
; mutable size: int
; local_offset_table: int symbol_table
; mutable statements: icStatement list
; static: bool
}