-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmach.ml
394 lines (364 loc) · 14.8 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
(**************************************************************************)
(* *)
(* 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
| 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
type float_width = Cmm.float_width
type float_comparison = Cmm.float_comparison
type float_operation =
| Inegf | Iabsf | Iaddf | Isubf | Imulf | Idivf
| Icompf of float_comparison
type mutable_flag = Immutable | Mutable
let of_ast_mutable_flag
: Asttypes.mutable_flag -> mutable_flag
= function
| Immutable -> Immutable
| Mutable -> Mutable
let to_ast_mutable_flag
: mutable_flag -> Asttypes.mutable_flag
= function
| Immutable -> Immutable
| Mutable -> Mutable
type test =
Itruetest
| Ifalsetest
| Iinttest of integer_comparison
| Iinttest_imm of integer_comparison * int
| Ifloattest of float_width * float_comparison
| Ioddtest
| Ieventest
type operation =
Imove
| Ispill
| Ireload
| Iconst_int of nativeint
| Iconst_float32 of int32
| Iconst_float of int64
| Iconst_vec128 of Cmm.vec128_bits
| Iconst_symbol of Cmm.symbol
| Icall_ind
| Icall_imm of { func : Cmm.symbol; }
| Itailcall_ind
| Itailcall_imm of { func : Cmm.symbol; }
| Iextcall of { func : string;
ty_res : Cmm.machtype; ty_args : Cmm.exttype list;
alloc : bool; returns : bool;
stack_ofs : int; }
| Istackoffset of int
| Iload of { memory_chunk : Cmm.memory_chunk;
addressing_mode : Arch.addressing_mode;
mutability : Asttypes.mutable_flag;
is_atomic : bool }
| 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 }
| Ifloatop of float_width * float_operation
| Icsel of test
| Ivalueofint | Iintofvalue
| Ivectorcast of Cmm.vector_cast
| Iscalarcast of Cmm.scalar_cast
| 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 : Backend_var.Provenance.t option; is_assignment : bool;
regs : Reg.t array }
| Iprobe of { name: string; handler_code_sym: string; enabled_at_init: bool; }
| Iprobe_is_enabled of { name: string }
| Ibeginregion | Iendregion
| Idls_get
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 * bool) list * instruction
| Iexit of int * Cmm.trap_action list
| Itrywith of instruction * Cmm.trywith_shared_label
* (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_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, _is_cold) -> 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_float32 _ | Iconst_float _
| Iconst_symbol _ | Iconst_vec128 _
| Icall_ind | Icall_imm _ | Iextcall _ | Istackoffset _
| Iload _ | Istore _ | Ialloc _
| Iintop _ | Iintop_imm _ | Iintop_atomic _
| Ifloatop _
| Icsel _ | Iscalarcast _
| Ivalueofint | Iintofvalue | Ivectorcast _
| Ispecific _ | Iname_for_debugger _ | Iprobe _ | Iprobe_is_enabled _
| Iopaque
| Ibeginregion | Iendregion | Ipoll _ | Idls_get) ->
instr_iter f i.next
let operation_is_pure = function
| Icall_ind | Icall_imm _ | Itailcall_ind | Itailcall_imm _
| Iextcall _ | Istackoffset _ | Istore _ | Ialloc _ | Ipoll _
| Idls_get
| 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 | Ifloatop _
| Icsel _
| Ivectorcast _ | Iscalarcast _
| Iconst_int _ | Iconst_float _ | Iconst_float32 _
| Iconst_symbol _ | Iconst_vec128 _
| Iload _ -> true
| Iname_for_debugger _ -> false
let operation_can_raise op =
match op with
| Icall_ind | Icall_imm _ | Iextcall _
| 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 | Ifloatop _
| Icsel _ | Iscalarcast _
| Ivalueofint | Iintofvalue | Ivectorcast _
| Iconst_int _ | Iconst_float _ | Iconst_float32 _
| Iconst_symbol _ | Iconst_vec128 _
| Istackoffset _ | Istore _ | Iload _ | Iname_for_debugger _
| Itailcall_imm _ | Itailcall_ind
| Iopaque | Ibeginregion | Iendregion
| Iprobe_is_enabled _ | Ialloc _ | Ipoll _ | Idls_get
-> 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, _is_cold) ->
let rec add_exn_conts conts = function
| Uncaught -> conts
| 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, _is_cold) ->
S.remove nfail conts)
conts handlers
| Iexit (nfail, _) -> S.add nfail next_conts
| Itrywith (body, nfail, (_ts, handler)) ->
let conts =
S.union next_conts (S.union (free_conts body) (free_conts handler))
in
S.remove nfail conts
| 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
| Specific_trap (lbl1, ts1), Specific_trap (lbl2, ts2) ->
Int.equal lbl1 lbl2 && equal_trap_stack ts1 ts2
| Uncaught, Specific_trap _
| Specific_trap _, Uncaught -> 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
| Iadd, (Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Isub, (Iadd | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Imul, (Iadd | Isub | Imulh _ | Idiv | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Imulh _, (Iadd | Isub | Imul | Idiv | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Idiv, (Iadd | Isub | Imul | Imulh _ | Imod | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Imod, (Iadd | Isub | Imul | Imulh _ | Idiv | Iand | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Iand, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Ior | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Ior, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ixor | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Ixor, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ilsl
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Ilsl, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Ilsr, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Iasr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Iasr, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iclz _ | Ictz _ | Ipopcnt | Icomp _)
| Iclz _, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Ictz _ | Ipopcnt | Icomp _)
| Ictz _, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Iclz _ | Ipopcnt | Icomp _)
| Ipopcnt, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Iclz _ | Ictz _ | Icomp _)
| Icomp _, (Iadd | Isub | Imul | Imulh _ | Idiv | Imod | Iand | Ior | Ixor
| Ilsl | Ilsr | Iasr | Iclz _ | Ictz _ | Ipopcnt)
-> false
let equal_float_comparison = Cmm.equal_float_comparison
let equal_float_width = Cmm.equal_float_width
let equal_float_operation left right =
match left, right with
| Inegf, Inegf -> true
| Iabsf, Iabsf -> true
| Iaddf, Iaddf -> true
| Isubf, Isubf -> true
| Imulf, Imulf -> true
| Idivf, Idivf -> true
| Icompf left, Icompf right -> equal_float_comparison left right
| Inegf, (Iabsf | Iaddf | Isubf | Imulf | Idivf | Icompf _)
| Iabsf, (Inegf | Iaddf | Isubf | Imulf | Idivf | Icompf _)
| Iaddf, (Inegf | Iabsf | Isubf | Imulf | Idivf | Icompf _)
| Isubf, (Inegf | Iabsf | Iaddf | Imulf | Idivf | Icompf _)
| Imulf, (Inegf | Iabsf | Iaddf | Isubf | Idivf | Icompf _)
| Idivf, (Inegf | Iabsf | Iaddf | Isubf | Imulf | Icompf _)
| Icompf _, (Inegf | Iabsf | Iaddf | Isubf | Imulf | Idivf)
-> false