-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest21.sml
194 lines (173 loc) · 3.64 KB
/
test21.sml
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
structure Loc = struct type t = string * int end
type label = string
type aggregate_id = int
structure Type =
struct
datatype t =
Bool
| Byte
| Short
| Int
| Long
| Double
| Function
| Pointer of t
| Aggregate of Aggregate.t
| Array of t * int
end
structure Builtin =
struct
type t = {name: string, param_types: Type.t list, return_type: Type.t option}
end
structure Aggregate =
struct
type t =
{ id: aggregate_id
, name: string
, loc: Loc.t
, elements: (string * Type.t) list ref
}
end
structure Use =
struct
datatype t =
T of {value: Value.t ref, user: Value.t, prev: t ref, next: t ref}
end
structure Value =
struct
type id = int
datatype value =
Instr of Instruction.t
| Lit of Literal.t
| Argument of Argument.t
datatype t = T of {id: id, value: value ref, uses: Use.t option ref}
end
structure Literal =
struct
datatype t =
Bool of bool
| Byte of int
| Int of int
| Long of int
| Double of real
| Global of Global.t
| NullPointer of Type.t
| Function of Function.t
| ArrayString of string
| ArrayVtable of int * Use.t list
| AggregateClosure of Type.t * Use.t
end
structure Argument =
struct datatype t = T of {func: Function.t, type_: Type.t, decl_loc: Loc.t} end
structure Instruction =
struct
structure Phi = struct type t = {args: Use.t list ref} end
structure GetPointer =
struct
datatype 'a offset = PointerIndex of 'a | FieldIndex of int
type value_offset = Value.t offset
type use_offset = Use.t offset
datatype t =
T of
{pointer: Use.t, pointer_offset: Use.t option, offsets: use_offset list}
end
structure Call =
struct
datatype t =
T of {func: func, args: Use.t list, has_return: bool}
and func =
Value of Use.t
| MirBuiltin of Builtin.t
end
datatype comparison = Eq | Neq | Lt | LtEq | Gt | GtEq
datatype unary_operation = Neg | Not
datatype binary_operation =
Add
| Sub
| Mul
| Div
| Rem
| And
| Or
| Xor
| Shl
| Shr
| Shrl
datatype instr =
Phi of Phi.t
| Call of Call.t
| StackAlloc of Type.t
| Load of Use.t
| Store of Use.t * Use.t
| GetPointer of GetPointer.t
| Unary of unary_operation * Use.t
| Binary of binary_operation * Use.t * Use.t
| Cmp of comparison * Use.t * Use.t
| Cast of Use.t
| Trunc of Use.t
| SExt of Use.t
| ZExt of Use.t
| IntToFloat of Use.t
| FloatToInt of Use.t
| Ret of Use.t option
| Continue of Block.t
| Branch of {test: Use.t, continue: Block.t, jump: Block.t}
| Unreachable
| Mov of Use.t
and t =
T of
{ type_: Type.t ref
, instr: instr ref
, block: Block.t ref
, prev: Value.t ref
, next: Value.t ref
}
end
structure Program =
struct
datatype t =
T of
{ globals: Global.t list ref
, funcs: Function.t list ref
, types: Aggregate.t list ref
, main_func: Function.t ref
}
end
structure Block =
struct
type id = int
datatype t =
T of
{ id: id
, func: Function.t
, instructions: instructions option ref
, prev_blocks: Block.t list ref
}
and instructions =
Instructions of {first: Value.t ref, last: Value.t ref}
end
structure Global =
struct
datatype t =
T of
{ name: label
, loc: Loc.t
, type_: Type.t
, init_val: Use.t option ref
, is_constant: bool
, value: Value.t ref
}
end
structure Function =
struct
datatype t =
T of
{ name: label
, loc: Loc.t ref
, params: Value.t list ref
, return_type: Type.t option ref
, start_block: Block.t ref
, blocks: Block.t list ref
, value: Value.t ref
}
end