-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmach.ml
363 lines (339 loc) · 14.3 KB
/
mach.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Representation of machine code by sequences of pseudoinstructions *)
type trap_stack =
| Uncaught
| Generic_trap of trap_stack
| Specific_trap of Cmm.trywith_shared_label * trap_stack
type integer_comparison =
Isigned of Cmm.integer_comparison
| Iunsigned of Cmm.integer_comparison
type integer_operation =
Iadd | Isub | Imul | Imulh of { signed: bool } | Idiv | Imod
| Iand | Ior | Ixor | Ilsl | Ilsr | Iasr
| Iclz of { arg_is_non_zero: bool; }
| Ictz of { arg_is_non_zero: bool; }
| Ipopcnt
| Icomp of integer_comparison
| Icheckbound
type float_comparison = Cmm.float_comparison
type mutable_flag = Immutable | Mutable
type test =
Itruetest
| Ifalsetest
| Iinttest of integer_comparison
| Iinttest_imm of integer_comparison * int
| Ifloattest of float_comparison
| Ioddtest
| Ieventest
type operation =
Imove
| Ispill
| Ireload
| Iconst_int of nativeint
| Iconst_float of int64
| Iconst_symbol of string
| Icall_ind
| Icall_imm of { func : string; }
| Itailcall_ind
| Itailcall_imm of { func : string; }
| Iextcall of { func : string;
ty_res : Cmm.machtype; ty_args : Cmm.exttype list;
alloc : bool; returns : bool; }
| Istackoffset of int
| Iload of Cmm.memory_chunk * Arch.addressing_mode * mutable_flag
| Istore of Cmm.memory_chunk * Arch.addressing_mode * bool
| Ialloc of { bytes : int; dbginfo : Debuginfo.alloc_dbginfo;
mode : Lambda.alloc_mode }
| Iintop of integer_operation
| Iintop_imm of integer_operation * int
| Iintop_atomic of { op : Cmm.atomic_op; size : Cmm.atomic_bitwidth;
addr : Arch.addressing_mode }
| Icompf of float_comparison
| Inegf | Iabsf | Iaddf | Isubf | Imulf | Idivf
| Icsel of test
| Ifloatofint | Iintoffloat
| Ivalueofint | Iintofvalue
| Iopaque
| Ispecific of Arch.specific_operation
| Ipoll of { return_label: Cmm.label option }
| Iname_for_debugger of { ident : Backend_var.t; which_parameter : int option;
provenance : unit option; is_assignment : bool; }
| Iprobe of { name: string; handler_code_sym: string; }
| Iprobe_is_enabled of { name: string }
| Ibeginregion | Iendregion
type instruction =
{ desc: instruction_desc;
next: instruction;
arg: Reg.t array;
res: Reg.t array;
dbg: Debuginfo.t;
mutable live: Reg.Set.t;
mutable available_before: Reg_availability_set.t;
mutable available_across: Reg_availability_set.t option;
}
and instruction_desc =
Iend
| Iop of operation
| Ireturn of Cmm.trap_action list
| Iifthenelse of test * instruction * instruction
| Iswitch of int array * instruction array
| Icatch of Cmm.rec_flag * trap_stack * (int * trap_stack * instruction) list * instruction
| Iexit of int * Cmm.trap_action list
| Itrywith of instruction * Cmm.trywith_kind * (trap_stack * instruction)
| Iraise of Lambda.raise_kind
type fundecl =
{ fun_name: string;
fun_args: Reg.t array;
fun_body: instruction;
fun_codegen_options : Cmm.codegen_option list;
fun_dbg : Debuginfo.t;
fun_poll: Lambda.poll_attribute;
fun_num_stack_slots: int array;
fun_contains_calls: bool;
}
let rec dummy_instr =
{ desc = Iend;
next = dummy_instr;
arg = [||];
res = [||];
dbg = Debuginfo.none;
live = Reg.Set.empty;
available_before = Reg_availability_set.Ok Reg_with_debug_info.Set.empty;
available_across = None;
}
let end_instr () =
{ desc = Iend;
next = dummy_instr;
arg = [||];
res = [||];
dbg = Debuginfo.none;
live = Reg.Set.empty;
available_before = Reg_availability_set.Ok Reg_with_debug_info.Set.empty;
available_across = None;
}
let instr_cons d a r n =
{ desc = d; next = n; arg = a; res = r;
dbg = Debuginfo.none; live = Reg.Set.empty;
available_before = Reg_availability_set.Ok Reg_with_debug_info.Set.empty;
available_across = None;
}
let instr_cons_debug d a r dbg n =
{ desc = d; next = n; arg = a; res = r; dbg = dbg; live = Reg.Set.empty;
available_before = Reg_availability_set.Ok Reg_with_debug_info.Set.empty;
available_across = None;
}
let rec instr_iter f i =
match i.desc with
Iend -> ()
| _ ->
f i;
match i.desc with
Iend -> ()
| Ireturn _ | Iop Itailcall_ind | Iop(Itailcall_imm _) -> ()
| Iifthenelse(_tst, ifso, ifnot) ->
instr_iter f ifso; instr_iter f ifnot; instr_iter f i.next
| Iswitch(_index, cases) ->
for i = 0 to Array.length cases - 1 do
instr_iter f cases.(i)
done;
instr_iter f i.next
| Icatch(_, _ts, handlers, body) ->
instr_iter f body;
List.iter (fun (_n, _ts, handler) -> instr_iter f handler) handlers;
instr_iter f i.next
| Iexit _ -> ()
| Itrywith(body, _kind, (_ts, handler)) ->
instr_iter f body; instr_iter f handler; instr_iter f i.next
| Iraise _ -> ()
| Iop (Imove | Ispill | Ireload
| Iconst_int _ | Iconst_float _ | Iconst_symbol _
| Icall_ind | Icall_imm _ | Iextcall _ | Istackoffset _
| Iload _ | Istore _ | Ialloc _
| Iintop _ | Iintop_imm _ | Iintop_atomic _
| Inegf | Iabsf | Iaddf | Isubf | Imulf | Idivf
| Icompf _
| Icsel _
| Ifloatofint | Iintoffloat | Ivalueofint | Iintofvalue
| Ispecific _ | Iname_for_debugger _ | Iprobe _ | Iprobe_is_enabled _
| Iopaque
| Ibeginregion | Iendregion | Ipoll _) ->
instr_iter f i.next
let operation_is_pure = function
| Icall_ind | Icall_imm _ | Itailcall_ind | Itailcall_imm _
| Iextcall _ | Istackoffset _ | Istore _ | Ialloc _ | Ipoll _
| Iintop(Icheckbound) | Iintop_imm(Icheckbound, _) | Iopaque
(* Conservative to ensure valueofint/intofvalue are not eliminated before emit. *)
| Ivalueofint | Iintofvalue | Iintop_atomic _ -> false
| Ibeginregion | Iendregion -> false
| Iprobe _ -> false
| Iprobe_is_enabled _-> true
| Ispecific sop -> Arch.operation_is_pure sop
| Iintop_imm((Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Ipopcnt | Iclz _|Ictz _|Icomp _), _)
| Iintop(Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Ipopcnt | Iclz _|Ictz _|Icomp _)
| Imove | Ispill | Ireload | Inegf | Iabsf | Iaddf | Isubf | Imulf | Idivf
| Icompf _
| Icsel _
| Ifloatofint | Iintoffloat
| Iconst_int _ | Iconst_float _ | Iconst_symbol _
| Iload (_, _, _) | Iname_for_debugger _
-> true
let operation_can_raise op =
match op with
| Icall_ind | Icall_imm _ | Iextcall _
| Iintop (Icheckbound) | Iintop_imm (Icheckbound, _)
| Iprobe _ -> true
| Ispecific sop -> Arch.operation_can_raise sop
| Iintop_imm((Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Ipopcnt | Iclz _|Ictz _|Icomp _), _)
| Iintop(Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Ipopcnt | Iclz _|Ictz _|Icomp _)
| Iintop_atomic _
| Imove | Ispill | Ireload | Inegf | Iabsf | Iaddf | Isubf | Imulf | Idivf
| Icompf _
| Icsel _
| Ifloatofint | Iintoffloat | Ivalueofint | Iintofvalue
| Iconst_int _ | Iconst_float _ | Iconst_symbol _
| Istackoffset _ | Istore _ | Iload (_, _, _) | Iname_for_debugger _
| Itailcall_imm _ | Itailcall_ind
| Iopaque | Ibeginregion | Iendregion
| Iprobe_is_enabled _ | Ialloc _ | Ipoll _
-> false
let free_conts_for_handlers fundecl =
let module S = Numbers.Int.Set in
let module M = Numbers.Int.Map in
let acc = ref M.empty in
let rec free_conts i =
match i.desc with
| Iend -> S.empty
| desc ->
let next_conts = free_conts i.next in
match desc with
| Iend -> assert false
| Iop _ -> next_conts
| Ireturn _ -> next_conts
| Iifthenelse (_, then_, else_) ->
S.union next_conts (S.union (free_conts then_) (free_conts else_))
| Iswitch (_, cases) ->
Array.fold_left (fun conts instr -> S.union conts (free_conts instr))
next_conts cases
| Icatch (_rec_flag, _ts, handlers, body) ->
let conts = S.union next_conts (free_conts body) in
let conts =
List.fold_left (fun conts (nfail, ts, i) ->
let rec add_exn_conts conts = function
| Uncaught -> conts
| Generic_trap ts -> add_exn_conts conts ts
| Specific_trap (nfail, ts) -> add_exn_conts (S.add nfail conts) ts
in
let free = add_exn_conts (free_conts i) ts in
acc := M.add nfail free !acc;
S.union conts free)
conts handlers
in
List.fold_left (fun conts (nfail, _ts, _i) ->
S.remove nfail conts)
conts handlers
| Iexit (nfail, _) -> S.add nfail next_conts
| Itrywith (body, kind, (_ts, handler)) ->
let conts =
S.union next_conts (S.union (free_conts body) (free_conts handler))
in
begin match kind with
| Regular -> conts
| Delayed nfail -> S.remove nfail conts
end
| Iraise _ -> next_conts
in
let free = free_conts fundecl.fun_body in
assert(S.is_empty free);
!acc
let rec equal_trap_stack ts1 ts2 =
match ts1, ts2 with
| Uncaught, Uncaught -> true
| Generic_trap ts1, Generic_trap ts2 -> equal_trap_stack ts1 ts2
| Specific_trap (lbl1, ts1), Specific_trap (lbl2, ts2) ->
Int.equal lbl1 lbl2 && equal_trap_stack ts1 ts2
| Uncaught, (Generic_trap _ | Specific_trap _)
| Generic_trap _, (Uncaught | Specific_trap _)
| Specific_trap _, (Uncaught | Generic_trap _) -> false
let equal_integer_comparison left right =
match left, right with
| Isigned left, Isigned right -> Cmm.equal_integer_comparison left right
| Iunsigned left, Iunsigned right -> Cmm.equal_integer_comparison left right
| Isigned _, Iunsigned _
| Iunsigned _, Isigned _ ->
false
let equal_integer_operation left right =
match left, right with
| Iadd, Iadd -> true
| Isub, Isub -> true
| Imul, Imul -> true
| Imulh { signed = left }, Imulh { signed = right } ->
Bool.equal left right
| Idiv, Idiv -> true
| Imod, Imod -> true
| Iand, Iand -> true
| Ior, Ior -> true
| Ixor, Ixor -> true
| Ilsl, Ilsl -> true
| Ilsr, Ilsr -> true
| Iasr, Iasr -> true
| Iclz { arg_is_non_zero = left_arg_is_non_zero; },
Iclz { arg_is_non_zero = right_arg_is_non_zero; } ->
Bool.equal left_arg_is_non_zero right_arg_is_non_zero
| Ictz { arg_is_non_zero = left_arg_is_non_zero; },
Ictz { arg_is_non_zero = right_arg_is_non_zero; } ->
Bool.equal left_arg_is_non_zero right_arg_is_non_zero
| Ipopcnt, Ipopcnt -> true
| Icomp left, Icomp right -> equal_integer_comparison left right
| Icheckbound, Icheckbound -> true
| Iadd, (Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Isub, (Iadd | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Imul, (Iadd | Isub | Imulh _ | Idiv | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Imulh _, (Iadd | Isub | Imul | Idiv | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Idiv, (Iadd | Isub | Imul | Imulh _ | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Imod, (Iadd | Isub | Imul | Imulh _ | Idiv | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Iand, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Ior, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Ixor, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Ilsl, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Ilsr, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Iasr, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iclz _ | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Iclz _, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Ictz _ | Ipopcnt | Icomp _ | Icheckbound)
| Ictz _, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Iclz _ | Ipopcnt | Icomp _ | Icheckbound)
| Ipopcnt, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Iclz _ | Ictz _ | Icomp _ | Icheckbound)
| Icomp _, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icheckbound)
| Icheckbound, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior
| Ixor | Ilsl | Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _) ->
false