-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_generator.ml
More file actions
464 lines (425 loc) · 19.4 KB
/
Copy pathcpp_generator.ml
File metadata and controls
464 lines (425 loc) · 19.4 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
type 'a ptr
module Testing = struct
external hook_fail_functions: unit -> unit = "caml_hook_fail"
end
module Int8 = struct
type t
external _zero : unit -> t = "caml_zero_int8"
let zero = _zero ()
external _one: unit -> t = "caml_one_int8"
let one = _one ()
external _max_int: unit -> t = "caml_max_int_int8"
external _min_int: unit -> t = "caml_min_int_int8"
let max_int = _max_int ()
let min_int = _min_int ()
external add: t -> t -> t = "caml_add_int8"
external sub: t -> t -> t = "caml_sub_int8"
external mul: t -> t -> t = "caml_mul_int8"
external div: t -> t -> t = "caml_div_int8"
external rem: t -> t -> t = "caml_rem_int8"
external equal: t -> t -> bool = "caml_equal_int8"
external min: t -> t -> t = "caml_min_int8"
external max: t -> t -> t = "caml_max_int8"
external of_int: int -> t = "caml_int_to_int8"
external of_int32: int32 -> t = "caml_int32_to_int8"
external of_int64: int64 -> t = "caml_int64_to_int8"
external of_int64_opt: int64 -> t option = "caml_int64_to_int8_opt"
external to_int32: t -> int32 = "caml_int8_to_int32"
(* Doesn't do bound checking *)
external to_int: t -> int = "caml_int8_to_int"
external to_float: t -> float = "caml_int8_to_float"
external of_nativeint: nativeint -> t = "caml_int8_to_nativeint"
external to_nativeint: t -> nativeint = "caml_nativeint_to_int8"
external to_nativeint_opt: t -> nativeint = "caml_nativeint_to_int8_opt"
let succ a = add a one
let pred a = sub a one
external seeded_hash_param :
int -> int -> int -> t -> int = "caml_hash" [@@noalloc]
let seeded_hash seed x = seeded_hash_param 10 100 seed x
external of_string : string -> t = "caml_string_to_int8"
external of_stringopt : string -> t option = "caml_string_to_int8_opt"
external format_int8: string -> t -> string = "caml_int8_to_string"
external _formatter: unit -> string = "caml_int8_formatter"
let formatter = "%" ^ _formatter ()
let to_string x = format_int8 formatter x
external logand: t -> t -> t = "caml_int8_logand"
external logor: t -> t -> t = "caml_int8_logor"
external logxor: t -> t -> t = "caml_int8_logxor"
external lognot: t -> t = "caml_int8_lognot"
external shift_left: t -> t -> t = "caml_int8_shift_left"
external shift_right: t -> t -> t = "caml_int8_shift_right"
end
module Int16 = struct
type t
external _zero : unit -> t = "caml_zero_int16"
let zero = _zero ()
external _one: unit -> t = "caml_one_int16"
let one = _one ()
external _max_int: unit -> t = "caml_max_int_int16"
external _min_int: unit -> t = "caml_min_int_int16"
let max_int = _max_int ()
let min_int = _min_int ()
external add: t -> t -> t = "caml_add_int16"
external sub: t -> t -> t = "caml_sub_int16"
external mul: t -> t -> t = "caml_mul_int16"
external div: t -> t -> t = "caml_div_int16"
external rem: t -> t -> t = "caml_rem_int16"
external equal: t -> t -> bool = "caml_equal_int16"
external min: t -> t -> t = "caml_min_int16"
external max: t -> t -> t = "caml_max_int16"
external of_int: int -> t = "caml_int_to_int16"
external of_int32: int32 -> t = "caml_int32_to_int16"
external of_int64: int64 -> t = "caml_int64_to_int16"
external of_int64_opt: int64 -> t option = "caml_int64_to_int16_opt"
external to_int32: t -> int32 = "caml_int16_to_int32"
(* Doesn't do bound checking *)
external to_int: t -> int = "caml_int16_to_int"
external to_float: t -> float = "caml_int16_to_float"
external of_nativeint: nativeint -> t = "caml_int16_to_nativeint"
external to_nativeint: t -> nativeint = "caml_nativeint_to_int16"
external to_nativeint_opt: t -> nativeint = "caml_nativeint_to_int16_opt"
let succ a = add a one
let pred a = sub a one
external seeded_hash_param :
int -> int -> int -> t -> int = "caml_hash" [@@noalloc]
let seeded_hash seed x = seeded_hash_param 10 100 seed x
external of_string : string -> t = "caml_string_to_int16"
external of_stringopt : string -> t option = "caml_string_to_int16_opt"
external format_int16: string -> t -> string = "caml_int16_to_string"
external _formatter: unit -> string = "caml_int16_formatter"
let formatter = "%" ^ _formatter ()
let to_string x = format_int16 formatter x
external logand: t -> t -> t = "caml_int16_logand"
external logor: t -> t -> t = "caml_int16_logor"
external logxor: t -> t -> t = "caml_int16_logxor"
external lognot: t -> t = "caml_int16_lognot"
external shift_left: t -> t -> t = "caml_int16_shift_left"
external shift_right: t -> t -> t = "caml_int16_shift_right"
end
module UInt8 = struct
type t
external _zero : unit -> t = "caml_zero_uint8"
let zero = _zero ()
external _one: unit -> t = "caml_one_uint8"
let one = _one ()
external _max_int: unit -> t = "caml_max_int_uint8"
let max_int = _max_int ()
let min_int: t = zero
external add: t -> t -> t = "caml_add_uint8"
external sub: t -> t -> t = "caml_sub_uint8"
external mul: t -> t -> t = "caml_mul_uint8"
external div: t -> t -> t = "caml_div_uint8"
external rem: t -> t -> t = "caml_rem_uint8"
external equal: t -> t -> bool = "caml_equal_uint8"
external min: t -> t -> t = "caml_min_uint8"
external max: t -> t -> t = "caml_max_uint8"
external of_int: int -> t = "caml_int_to_uint8"
external of_int32: int32 -> t = "caml_int32_to_uint8"
external of_int64: int64 -> t = "caml_int64_to_uint8"
external of_int64_opt: int64 -> t option = "caml_int64_to_uint8_opt"
external to_int32: t -> int32 = "caml_uint8_to_int32"
(* Doesn't do bound checking *)
external to_int: t -> int = "caml_uint8_to_int"
external to_float: t -> float = "caml_uint8_to_float"
external of_nativeint: nativeint -> t = "caml_uint8_to_nativeint"
external to_nativeint: t -> nativeint = "caml_nativeint_to_uint8"
external to_nativeint_opt: t -> nativeint = "caml_nativeint_to_uint8_opt"
let succ a = add a one
let pred a = sub a one
external seeded_hash_param :
int -> int -> int -> t -> int = "caml_hash" [@@noalloc]
let seeded_hash seed x = seeded_hash_param 10 100 seed x
external of_string : string -> t = "caml_string_to_uint8"
external of_stringopt : string -> t option = "caml_string_to_uint8_opt"
external format_uint8: string -> t -> string = "caml_uint32_to_string"
external _formatter: unit -> string = "caml_uint8_formatter"
let formatter = "%" ^ _formatter ()
let to_string x = format_uint8 formatter x
external logand: t -> t -> t = "caml_uint8_logand"
external logor: t -> t -> t = "caml_uint8_logor"
external logxor: t -> t -> t = "caml_uint8_logxor"
external lognot: t -> t = "caml_uint8_lognot"
external shift_left: t -> t -> t = "caml_uint8_shift_left"
external shift_right: t -> t -> t = "caml_uint8_shift_right"
end
module UInt16 = struct
type t
external _zero : unit -> t = "caml_zero_uint16"
let zero = _zero ()
external _one: unit -> t = "caml_one_uint16"
let one = _one ()
external _max_int: unit -> t = "caml_max_int_uint16"
let max_int = _max_int ()
let min_int: t = zero
external add: t -> t -> t = "caml_add_uint16"
external sub: t -> t -> t = "caml_sub_uint16"
external mul: t -> t -> t = "caml_mul_uint16"
external div: t -> t -> t = "caml_div_uint16"
external rem: t -> t -> t = "caml_rem_uint16"
external equal: t -> t -> bool = "caml_equal_uint16"
external min: t -> t -> t = "caml_min_uint16"
external max: t -> t -> t = "caml_max_uint16"
external of_int: int -> t = "caml_int_to_uint16"
external of_int32: int32 -> t = "caml_int32_to_uint16"
external of_int64: int64 -> t = "caml_int64_to_uint16"
external of_int64_opt: int64 -> t option = "caml_int64_to_uint16_opt"
external to_int32: t -> int32 = "caml_uint16_to_int32"
(* Doesn't do bound checking *)
external to_int: t -> int = "caml_uint16_to_int"
external to_float: t -> float = "caml_uint16_to_float"
external of_nativeint: nativeint -> t = "caml_uint16_to_nativeint"
external to_nativeint: t -> nativeint = "caml_nativeint_to_uint16"
external to_nativeint_opt: t -> nativeint = "caml_nativeint_to_uint16_opt"
let succ a = add a one
let pred a = sub a one
external seeded_hash_param :
int -> int -> int -> t -> int = "caml_hash" [@@noalloc]
let seeded_hash seed x = seeded_hash_param 10 100 seed x
external of_string : string -> t = "caml_string_to_uint16"
external of_stringopt : string -> t option = "caml_string_to_uint16_opt"
external format_uint16: string -> t -> string = "caml_uint32_to_string"
external _formatter: unit -> string = "caml_uint16_formatter"
let formatter = "%" ^ _formatter ()
let to_string x = format_uint16 formatter x
external logand: t -> t -> t = "caml_uint16_logand"
external logor: t -> t -> t = "caml_uint16_logor"
external logxor: t -> t -> t = "caml_uint16_logxor"
external lognot: t -> t = "caml_uint16_lognot"
external shift_left: t -> t -> t = "caml_uint16_shift_left"
external shift_right: t -> t -> t = "caml_uint16_shift_right"
end
module UInt32 = struct
type t
external _zero : unit -> t = "caml_zero_uint32"
let zero = _zero ()
external _one: unit -> t = "caml_one_uint32"
let one = _one ()
external _max_int: unit -> t = "caml_max_int_uint32"
let max_int = _max_int ()
let min_int: t = zero
external add: t -> t -> t = "caml_add_uint32"
external sub: t -> t -> t = "caml_sub_uint32"
external mul: t -> t -> t = "caml_mul_uint32"
external div: t -> t -> t = "caml_div_uint32"
external rem: t -> t -> t = "caml_rem_uint32"
external equal: t -> t -> bool = "caml_equal_uint32"
external min: t -> t -> t = "caml_min_uint32"
external max: t -> t -> t = "caml_max_uint32"
external of_int: int -> t = "caml_int_to_uint32"
external of_int32: int32 -> t = "caml_int32_to_uint32"
external of_int64: int64 -> t = "caml_int64_to_uint32"
external of_int64_opt: int64 -> t option = "caml_int64_to_uint32_opt"
external to_int32: t -> int32 = "caml_uint32_to_int32"
(* Doesn't do bound checking *)
external to_int: t -> int = "caml_uint32_to_int"
external to_float: t -> float = "caml_uint32_to_float"
external of_nativeint: nativeint -> t = "caml_uint32_to_nativeint"
external to_nativeint: t -> nativeint = "caml_nativeint_to_uint32"
external to_nativeint_opt: t -> nativeint = "caml_nativeint_to_uint32_opt"
let succ a = add a one
let pred a = sub a one
external seeded_hash_param :
int -> int -> int -> t -> int = "caml_hash" [@@noalloc]
let seeded_hash seed x = seeded_hash_param 10 100 seed x
external of_string : string -> t = "caml_string_to_uint32"
external of_stringopt : string -> t option = "caml_string_to_uint32_opt"
external format_uint32: string -> t -> string = "caml_uint32_to_string"
external _formatter: unit -> string = "caml_uint32_formatter"
let formatter = "%" ^ _formatter ()
let to_string x = format_uint32 formatter x
external logand: t -> t -> t = "caml_uint32_logand"
external logor: t -> t -> t = "caml_uint32_logor"
external logxor: t -> t -> t = "caml_uint32_logxor"
external lognot: t -> t = "caml_uint32_lognot"
external shift_left: t -> t -> t = "caml_uint32_shift_left"
external shift_right: t -> t -> t = "caml_uint32_shift_right"
end
module UInt64 = struct
type t
external _zero : unit -> t = "caml_zero_uint64"
let zero = _zero ()
external _one: unit -> t = "caml_one_uint64"
let one = _one ()
external _max_int: unit -> t = "caml_max_int_uint64"
let max_int = _max_int ()
let min_int = zero
external add: t -> t -> t = "caml_add_uint64"
external sub: t -> t -> t = "caml_sub_uint64"
external mul: t -> t -> t = "caml_mul_uint64"
external div: t -> t -> t = "caml_div_uint64"
external rem: t -> t -> t = "caml_rem_uint64"
external equal: t -> t -> bool = "caml_equal_uint64"
external min: t -> t -> t = "caml_min_uint64"
external max: t -> t -> t = "caml_max_uint64"
external of_int: int -> t = "caml_int_to_uint64"
external of_int32: int32 -> t = "caml_int32_to_uint64"
external of_int64: int64 -> t = "caml_int64_to_uint64"
external to_int64: t -> int64 = "caml_uint64_to_int64"
external to_int: t -> int = "caml_uint64_to_int"
external to_float: t -> float = "caml_uint64_to_float"
external of_nativeint: nativeint -> t = "caml_nativeint_to_uint64"
external to_nativeint: t -> nativeint = "caml_uint64_to_nativeint"
let succ a = add a one
let pred a = sub a one
external seeded_hash_param :
int -> int -> int -> t -> int = "caml_hash" [@@noalloc]
let seeded_hash seed x = seeded_hash_param 10 100 seed x
external of_string : string -> t = "caml_string_to_uint64"
external of_stringopt : string -> t option = "caml_string_to_uint64_opt"
external _formatter: unit -> string = "caml_uint64_formatter"
let formatter = "%" ^ (_formatter ())
external format_uint64: string -> t -> string = "caml_uint64_to_string"
let to_string x = format_uint64 formatter x
external logand: t -> t -> t = "caml_uint64_logand"
external logor: t -> t -> t = "caml_uint64_logor"
external logxor: t -> t -> t = "caml_uint64_logxor"
external lognot: t -> t = "caml_uint64_lognot"
external shift_left: t -> t -> t = "caml_uint64_shift_left"
external shift_right: t -> t -> t = "caml_uint64_shift_right"
end
type 'a primitive =
| Bool : bool primitive
| Int : int primitive
| Int16 : int32 primitive
| UInt16 : int32 primitive
| Int32 : int32 primitive
| UInt32 : int32 primitive
| Int64 : int64 primitive
| UInt64 : int64 primitive
| Float : float primitive
| Double : float primitive
| Char : char primitive
| Ptr : 'b primitive
let sizeof: type a. a primitive -> Nativeint.t = function
| Bool -> 1n
| Int -> 4n
| Int16 -> 2n
| UInt16 -> 2n
| Int32 -> 4n
| UInt32 -> 4n
| UInt64 -> 8n
| Int64 -> 8n
| Float -> 4n
| Double -> 8n
| Char -> 1n
| Ptr -> Nativeint.of_int Nativeint.size
module CInteract = struct
external ptr_alloc: size: nativeint -> 'a ptr = "caml_ptr_alloc"
external ptr_free: ptr:'a ptr -> unit = "caml_ptr_free"
external is_null: ptr: 'a ptr -> bool = "caml_ptr_is_null"
external ptr_add: ptr:'a ptr -> offset:nativeint -> 'a ptr = "caml_ptr_add"
external ptr_sub: ptr:'a ptr -> offset:nativeint -> 'a ptr = "caml_ptr_sub"
let alloc (p: 'a primitive) : ('a ptr) = ptr_alloc ~size:(sizeof p)
external poke_bool: ptr: bool ptr -> value: bool -> unit = "caml_poke_bool"
external poke_i8: ptr: Int8.t ptr -> value: Int8.t -> unit = "caml_poke_i8"
external poke_i16: ptr: Int16.t ptr -> value: Int16.t -> unit = "caml_poke_i16"
external poke_i32: ptr: Int32.t ptr -> value: Int32.t -> unit = "caml_poke_i32"
(* let poke_int: ptr: int ptr -> value: int -> unit = poke_i32 *)
external poke_i64: ptr: Int64.t ptr -> value: Int64.t -> unit = "caml_poke_i64"
external poke_u8: ptr: UInt8.t ptr -> value: UInt8.t -> unit = "caml_poke_u8"
external poke_u16: ptr: UInt16.t ptr -> value: UInt16.t -> unit = "caml_poke_u16"
external poke_u32: ptr: UInt32.t ptr -> value: UInt32.t -> unit = "caml_poke_u32"
(* let poke_uint: ptr: UInt32.t ptr -> value: UInt32.t -> unit = poke_u32 *)
external poke_u64: ptr: UInt64.t ptr -> value: UInt64.t -> unit = "caml_poke_u64"
external poke_n: ptr: 't ptr -> value: 't -> size: Nativeint.t -> unit = "caml_poke_n"
external peek_bool: ptr: bool ptr -> bool = "caml_peek_bool"
external peek_i8: ptr: Int8.t ptr -> Int8.t = "caml_peek_i8"
external peek_i16: ptr: Int16.t ptr -> Int16.t = "caml_peek_i16"
external peek_i32: ptr: Int32.t ptr -> Int32.t = "caml_peek_i32"
(* let peek_int: ptr: int ptr -> value: int -> unit = peek_i32 *)
external peek_i64: ptr: Int64.t ptr -> Int64.t = "caml_peek_i64"
external peek_u8: ptr: UInt8.t ptr -> UInt8.t = "caml_peek_u8"
external peek_u16: ptr: UInt16.t ptr -> UInt16.t = "caml_peek_u16"
external peek_u32: ptr: UInt32.t ptr -> UInt32.t = "caml_peek_u32"
(* let peek_uint: ptr: UInt32.t ptr -> value: UInt32.t -> unit = peek_u32 *)
external peek_u64: ptr: UInt64.t ptr -> UInt64.t = "caml_peek_u64"
external peek_n: ptr: 't ptr -> size: nativeint -> 't = "caml_peek_n"
(* external peek_n_unboxed: ptr: 't ptr -> size: nativeint -> 't = "caml_peek_n_unboxed" *)
external cast: 'a -> 'b = "%identity"
(* external peek_int16: int32 ptr -> int32 = "caml_peek_ptr_int16"
external peek_uint16: int32 ptr -> int32 = "caml_peek_ptr_uint16"
external peek_int32: int32 ptr -> int32 = "caml_peek_ptr_int32"
external peek_uint32: int32 ptr -> int32 = "caml_peek_ptr_uint32"
external peek_int: int ptr -> int = "caml_peek_ptr_int32"
external peek_int64: int64 ptr -> int64 = "caml_peek_ptr_int64"
external peek_uint64: int64 ptr -> int64 = "caml_peek_ptr_uint64"
external peek_bool: bool ptr -> bool = "caml_peek_ptr_bool"
external peek_float: float ptr -> float= "caml_peek_ptr_float"
external peek_double: float ptr -> float= "caml_peek_ptr_double" *)
(* let peek : 'a ptr -> 'b = function
| Ptr (Bool addr) -> 1n
| Ptr (Int addr) -> 4n
| Ptr (Int16 addr) -> 2n
| Ptr (UInt16 addr) -> 2n
| Ptr (Int32 addr) -> 4n
| Ptr (UInt32 addr) -> 4n
| Ptr (UInt64 addr) -> 8n
| Ptr (Int64 addr) -> 8n
| Ptr (Float addr) -> 4n
| Ptr (Double addr) -> 8n
| Ptr (Char addr) -> 1n
| Ptr _ -> failwith "Not implemented" *)
end
module CArray = struct
open CInteract
type 't carray = {
length: int;
size_of_element: int;
ptr: 't ptr
}
let word_size = Sys.word_size / 8
(* external memset = ptr: 't tpr *)
let alloc_array len initValue =
let size = sizeof initValue in
let pVal = ptr_alloc ~size:size in
{
length = len;
size_of_element = Nativeint.to_int size;
ptr = pVal
}
let for_each callback carray =
let {length; size_of_element; ptr} = carray in
let rec for_each_help offset =
let currentPtr = ptr_add ~ptr:ptr ~offset:(Nativeint.of_int offset) in
callback currentPtr;
let nextOffset = offset + size_of_element in
if nextOffset >= (length * size_of_element)
then ()
else for_each_help (offset + size_of_element)
in
if length > 0 then
for_each_help 0
else ()
let for_each_ref callback carray =
let {length; size_of_element; ptr} = carray in
let rec for_each_help offset =
let currentPtr = ptr_add ~ptr:ptr ~offset:(Nativeint.of_int offset) in
let currentVal = peek_n ~ptr:currentPtr ~size:(Nativeint.of_int size_of_element) in
let currentValRef = ref currentVal in
callback currentValRef;
poke_n ~ptr:currentPtr ~value:!currentValRef ~size:(Nativeint.of_int size_of_element);
let nextOffset = offset + size_of_element in
if nextOffset >= (length * size_of_element )
then ()
else for_each_help nextOffset
in
if length > 0 then
for_each_help 0
else ()
(* let map (callback: 'a -> 'b) (carray: 'a array) =
let {length; size_of_element; ptr} = carray in
let rec for_each_help offset =
let currentPtr = ptr_add ~ptr:ptr ~offset:(Nativeint.of_int offset) in
let currentVal = peek_n ~ptr:currentPtr ~size:(Nativeint.of_int size_of_element) in
callback currentVal;
poke_n ~ptr:currentPtr ~value:!currentValRef ~size:(Nativeint.of_int size_of_element);
let nextOffset = offset + size_of_element in
if nextOffset >= (length * size_of_element)
then ()
else for_each_help nextOffset
in
if length > 0 then
for_each_help 0
else () *)
(* let fill array = *)
end