/
llvm_ocaml.c
2760 lines (2307 loc) · 90.5 KB
/
llvm_ocaml.c
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*===-- llvm_ocaml.c - LLVM OCaml Glue --------------------------*- C++ -*-===*\
|* *|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
|* Exceptions. *|
|* See https://llvm.org/LICENSE.txt for license information. *|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
|* *|
|*===----------------------------------------------------------------------===*|
|* *|
|* This file glues LLVM's OCaml interface to its C interface. These functions *|
|* are by and large transparent wrappers to the corresponding C functions. *|
|* *|
|* Note that these functions intentionally take liberties with the CAMLparamX *|
|* macros, since most of the parameters are not GC heap objects. *|
|* *|
\*===----------------------------------------------------------------------===*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "llvm-c/Core.h"
#include "llvm-c/Support.h"
#include "llvm/Config/llvm-config.h"
#include "caml/memory.h"
#include "caml/fail.h"
#include "caml/callback.h"
#include "llvm_ocaml.h"
#if OCAML_VERSION < 41200
value caml_alloc_some(value v) {
CAMLparam1(v);
value Some = caml_alloc_small(1, 0);
Field(Some, 0) = v;
CAMLreturn(Some);
}
#endif
value caml_alloc_tuple_uninit(mlsize_t wosize) {
if (wosize <= Max_young_wosize) {
return caml_alloc_small(wosize, 0);
} else {
return caml_alloc_shr(wosize, 0);
}
}
value to_val(void *ptr) {
assert((((value)ptr) & 1) == 0 &&
"OCaml bindings assume LLVM objects are at least 2-byte aligned");
return ((value)ptr) | 1;
}
void *from_val(value v) {
assert(Is_long(v) && "OCaml values representing LLVM objects should have the "
"low bit set so that the OCaml GC "
"treats them as tagged integers");
return (void *)(v ^ 1);
}
value llvm_string_of_message(char *Message) {
value String = caml_copy_string(Message);
LLVMDisposeMessage(Message);
return String;
}
value ptr_to_option(void *Ptr) {
if (!Ptr)
return Val_none;
return caml_alloc_some(to_val(Ptr));
}
value cstr_to_string(const char *Str, mlsize_t Len) {
if (!Str)
return caml_alloc_string(0);
value String = caml_alloc_string(Len);
memcpy((char *)String_val(String), Str, Len);
return String;
}
value cstr_to_string_option(const char *CStr, mlsize_t Len) {
if (!CStr)
return Val_none;
value String = caml_alloc_string(Len);
memcpy((char *)String_val(String), CStr, Len);
return caml_alloc_some(String);
}
void llvm_raise(value Prototype, char *Message) {
caml_raise_with_arg(Prototype, llvm_string_of_message(Message));
}
static value llvm_fatal_error_handler;
static void llvm_fatal_error_trampoline(const char *Reason) {
caml_callback(llvm_fatal_error_handler, caml_copy_string(Reason));
}
value llvm_install_fatal_error_handler(value Handler) {
LLVMInstallFatalErrorHandler(llvm_fatal_error_trampoline);
llvm_fatal_error_handler = Handler;
caml_register_global_root(&llvm_fatal_error_handler);
return Val_unit;
}
value llvm_reset_fatal_error_handler(value Unit) {
caml_remove_global_root(&llvm_fatal_error_handler);
LLVMResetFatalErrorHandler();
return Val_unit;
}
value llvm_enable_pretty_stacktrace(value Unit) {
LLVMEnablePrettyStackTrace();
return Val_unit;
}
value llvm_parse_command_line_options(value Overview, value Args) {
const char *COverview;
if (Overview == Val_int(0)) {
COverview = NULL;
} else {
COverview = String_val(Field(Overview, 0));
}
LLVMParseCommandLineOptions(Wosize_val(Args),
(const char *const *)Op_val(Args), COverview);
return Val_unit;
}
void *from_val_array(value Elements) {
mlsize_t Length = Wosize_val(Elements);
void **Temp = malloc(sizeof(void *) * Length);
if (Temp == NULL)
caml_raise_out_of_memory();
for (unsigned I = 0; I < Length; ++I) {
Temp[I] = from_val(Field(Elements, I));
}
return Temp;
}
static value alloc_variant(int tag, value Value) {
value Iter = caml_alloc_small(1, tag);
Field(Iter, 0) = Value;
return Iter;
}
/* Macro to convert the C first/next/last/prev idiom to the Ocaml llpos/
llrev_pos idiom. */
#define DEFINE_ITERATORS(camlname, cname, pty_val, cty, cty_val, pfun) \
/* llmodule -> ('a, 'b) llpos */ \
value llvm_##camlname##_begin(value Mom) { \
cty First = LLVMGetFirst##cname(pty_val(Mom)); \
if (First) \
return alloc_variant(1, to_val(First)); \
return alloc_variant(0, Mom); \
} \
\
/* llvalue -> ('a, 'b) llpos */ \
value llvm_##camlname##_succ(value Kid) { \
cty Next = LLVMGetNext##cname(cty_val(Kid)); \
if (Next) \
return alloc_variant(1, to_val(Next)); \
return alloc_variant(0, to_val(pfun(cty_val(Kid)))); \
} \
\
/* llmodule -> ('a, 'b) llrev_pos */ \
value llvm_##camlname##_end(value Mom) { \
cty Last = LLVMGetLast##cname(pty_val(Mom)); \
if (Last) \
return alloc_variant(1, to_val(Last)); \
return alloc_variant(0, Mom); \
} \
\
/* llvalue -> ('a, 'b) llrev_pos */ \
value llvm_##camlname##_pred(value Kid) { \
cty Prev = LLVMGetPrevious##cname(cty_val(Kid)); \
if (Prev) \
return alloc_variant(1, to_val(Prev)); \
return alloc_variant(0, to_val(pfun(cty_val(Kid)))); \
}
/*===-- Context error handling --------------------------------------------===*/
void llvm_diagnostic_handler_trampoline(LLVMDiagnosticInfoRef DI,
void *DiagnosticContext) {
caml_callback(*((value *)DiagnosticContext), to_val(DI));
}
/* Diagnostic.t -> string */
value llvm_get_diagnostic_description(value Diagnostic) {
return llvm_string_of_message(
LLVMGetDiagInfoDescription(DiagnosticInfo_val(Diagnostic)));
}
/* Diagnostic.t -> DiagnosticSeverity.t */
value llvm_get_diagnostic_severity(value Diagnostic) {
return Val_int(LLVMGetDiagInfoSeverity(DiagnosticInfo_val(Diagnostic)));
}
static void llvm_remove_diagnostic_handler(value C) {
CAMLparam1(C);
LLVMContextRef context = Context_val(C);
if (LLVMContextGetDiagnosticHandler(context) ==
llvm_diagnostic_handler_trampoline) {
value *Handler = (value *)LLVMContextGetDiagnosticContext(context);
caml_remove_global_root(Handler);
free(Handler);
}
CAMLreturn0;
}
/* llcontext -> (Diagnostic.t -> unit) option -> unit */
value llvm_set_diagnostic_handler(value C, value Handler) {
CAMLparam2(C, Handler);
LLVMContextRef context = Context_val(C);
llvm_remove_diagnostic_handler(C);
if (Handler == Val_none) {
LLVMContextSetDiagnosticHandler(context, NULL, NULL);
} else {
value *DiagnosticContext = malloc(sizeof(value));
if (DiagnosticContext == NULL)
caml_raise_out_of_memory();
caml_register_global_root(DiagnosticContext);
*DiagnosticContext = Field(Handler, 0);
LLVMContextSetDiagnosticHandler(context, llvm_diagnostic_handler_trampoline,
DiagnosticContext);
}
CAMLreturn(Val_unit);
}
/*===-- Contexts ----------------------------------------------------------===*/
/* unit -> llcontext */
value llvm_create_context(value Unit) { return to_val(LLVMContextCreate()); }
/* llcontext -> unit */
value llvm_dispose_context(value C) {
llvm_remove_diagnostic_handler(C);
LLVMContextDispose(Context_val(C));
return Val_unit;
}
/* unit -> llcontext */
value llvm_global_context(value Unit) { return to_val(LLVMGetGlobalContext()); }
/* llcontext -> string -> int */
value llvm_mdkind_id(value C, value Name) {
unsigned MDKindID = LLVMGetMDKindIDInContext(Context_val(C), String_val(Name),
caml_string_length(Name));
return Val_int(MDKindID);
}
/*===-- Attributes --------------------------------------------------------===*/
/* string -> llattrkind */
value llvm_enum_attr_kind(value Name) {
unsigned Kind = LLVMGetEnumAttributeKindForName(String_val(Name),
caml_string_length(Name));
if (Kind == 0)
caml_raise_with_arg(*caml_named_value("Llvm.UnknownAttribute"), Name);
return Val_int(Kind);
}
/* llcontext -> int -> int64 -> llattribute */
value llvm_create_enum_attr_by_kind(value C, value Kind, value Value) {
return to_val(
LLVMCreateEnumAttribute(Context_val(C), Int_val(Kind), Int64_val(Value)));
}
/* llattribute -> bool */
value llvm_is_enum_attr(value A) {
return Val_int(LLVMIsEnumAttribute(Attribute_val(A)));
}
/* llattribute -> llattrkind */
value llvm_get_enum_attr_kind(value A) {
return Val_int(LLVMGetEnumAttributeKind(Attribute_val(A)));
}
/* llattribute -> int64 */
value llvm_get_enum_attr_value(value A) {
return caml_copy_int64(LLVMGetEnumAttributeValue(Attribute_val(A)));
}
/* llcontext -> kind:string -> name:string -> llattribute */
value llvm_create_string_attr(value C, value Kind, value Value) {
return to_val(LLVMCreateStringAttribute(
Context_val(C), String_val(Kind), caml_string_length(Kind),
String_val(Value), caml_string_length(Value)));
}
/* llattribute -> bool */
value llvm_is_string_attr(value A) {
return Val_int(LLVMIsStringAttribute(Attribute_val(A)));
}
/* llattribute -> string */
value llvm_get_string_attr_kind(value A) {
unsigned Length;
const char *String = LLVMGetStringAttributeKind(Attribute_val(A), &Length);
return cstr_to_string(String, Length);
}
/* llattribute -> string */
value llvm_get_string_attr_value(value A) {
unsigned Length;
const char *String = LLVMGetStringAttributeValue(Attribute_val(A), &Length);
return cstr_to_string(String, Length);
}
/*===-- Modules -----------------------------------------------------------===*/
/* llcontext -> string -> llmodule */
value llvm_create_module(value C, value ModuleID) {
return to_val(
LLVMModuleCreateWithNameInContext(String_val(ModuleID), Context_val(C)));
}
/* llmodule -> unit */
value llvm_dispose_module(value M) {
LLVMDisposeModule(Module_val(M));
return Val_unit;
}
/* llmodule -> string */
value llvm_target_triple(value M) {
return caml_copy_string(LLVMGetTarget(Module_val(M)));
}
/* string -> llmodule -> unit */
value llvm_set_target_triple(value Trip, value M) {
LLVMSetTarget(Module_val(M), String_val(Trip));
return Val_unit;
}
/* llmodule -> string */
value llvm_data_layout(value M) {
return caml_copy_string(LLVMGetDataLayout(Module_val(M)));
}
/* string -> llmodule -> unit */
value llvm_set_data_layout(value Layout, value M) {
LLVMSetDataLayout(Module_val(M), String_val(Layout));
return Val_unit;
}
/* llmodule -> unit */
value llvm_dump_module(value M) {
LLVMDumpModule(Module_val(M));
return Val_unit;
}
/* string -> llmodule -> unit */
value llvm_print_module(value Filename, value M) {
char *Message;
if (LLVMPrintModuleToFile(Module_val(M), String_val(Filename), &Message))
llvm_raise(*caml_named_value("Llvm.IoError"), Message);
return Val_unit;
}
/* llmodule -> string */
value llvm_string_of_llmodule(value M) {
char *ModuleCStr = LLVMPrintModuleToString(Module_val(M));
value ModuleStr = caml_copy_string(ModuleCStr);
LLVMDisposeMessage(ModuleCStr);
return ModuleStr;
}
/* llmodule -> llcontext */
value llvm_get_module_context(value M) {
LLVMContextRef C = LLVMGetModuleContext(Module_val(M));
return to_val(C);
}
/* llmodule -> string */
value llvm_get_module_identifier(value M) {
size_t Len;
const char *Name = LLVMGetModuleIdentifier(Module_val(M), &Len);
return cstr_to_string(Name, (mlsize_t)Len);
}
/* llmodule -> string -> unit */
value llvm_set_module_identifier(value M, value Id) {
LLVMSetModuleIdentifier(Module_val(M), String_val(Id),
caml_string_length(Id));
return Val_unit;
}
/* llmodule -> string -> unit */
value llvm_set_module_inline_asm(value M, value Asm) {
LLVMSetModuleInlineAsm(Module_val(M), String_val(Asm));
return Val_unit;
}
/* llmodule -> string -> llmetadata option */
value llvm_get_module_flag(value M, value Key) {
return ptr_to_option(LLVMGetModuleFlag(Module_val(M), String_val(Key),
caml_string_length(Key)));
}
/* llmodule -> ModuleFlagBehavior.t -> string -> llmetadata -> unit */
value llvm_add_module_flag(value M, value Behaviour, value Key, value Val) {
LLVMAddModuleFlag(Module_val(M), Int_val(Behaviour), String_val(Key),
caml_string_length(Key), Metadata_val(Val));
return Val_unit;
}
/*===-- Types -------------------------------------------------------------===*/
/* lltype -> TypeKind.t */
value llvm_classify_type(value Ty) {
return Val_int(LLVMGetTypeKind(Type_val(Ty)));
}
/* lltype -> bool */
value llvm_type_is_sized(value Ty) {
return Val_bool(LLVMTypeIsSized(Type_val(Ty)));
}
/* lltype -> llcontext */
value llvm_type_context(value Ty) {
return to_val(LLVMGetTypeContext(Type_val(Ty)));
}
/* lltype -> unit */
value llvm_dump_type(value Val) {
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVMDumpType(Type_val(Val));
#else
caml_raise_with_arg(*caml_named_value("Llvm.FeatureDisabled"),
caml_copy_string("dump"));
#endif
return Val_unit;
}
/* lltype -> string */
value llvm_string_of_lltype(value M) {
char *TypeCStr = LLVMPrintTypeToString(Type_val(M));
value TypeStr = caml_copy_string(TypeCStr);
LLVMDisposeMessage(TypeCStr);
return TypeStr;
}
/*--... Operations on integer types ........................................--*/
/* llcontext -> lltype */
value llvm_i1_type(value Context) {
return to_val(LLVMInt1TypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_i8_type(value Context) {
return to_val(LLVMInt8TypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_i16_type(value Context) {
return to_val(LLVMInt16TypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_i32_type(value Context) {
return to_val(LLVMInt32TypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_i64_type(value Context) {
return to_val(LLVMInt64TypeInContext(Context_val(Context)));
}
/* llcontext -> int -> lltype */
value llvm_integer_type(value Context, value Width) {
return to_val(LLVMIntTypeInContext(Context_val(Context), Int_val(Width)));
}
/* lltype -> int */
value llvm_integer_bitwidth(value IntegerTy) {
return Val_int(LLVMGetIntTypeWidth(Type_val(IntegerTy)));
}
/*--... Operations on real types ...........................................--*/
/* llcontext -> lltype */
value llvm_float_type(value Context) {
return to_val(LLVMFloatTypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_double_type(value Context) {
return to_val(LLVMDoubleTypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_x86fp80_type(value Context) {
return to_val(LLVMX86FP80TypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_fp128_type(value Context) {
return to_val(LLVMFP128TypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_ppc_fp128_type(value Context) {
return to_val(LLVMPPCFP128TypeInContext(Context_val(Context)));
}
/*--... Operations on function types .......................................--*/
/* lltype -> lltype array -> lltype */
value llvm_function_type(value RetTy, value ParamTys) {
mlsize_t len = Wosize_val(ParamTys);
LLVMTypeRef *Temp = from_val_array(ParamTys);
LLVMTypeRef Type = LLVMFunctionType(Type_val(RetTy), Temp, len, 0);
free(Temp);
return to_val(Type);
}
/* lltype -> lltype array -> lltype */
value llvm_var_arg_function_type(value RetTy, value ParamTys) {
mlsize_t len = Wosize_val(ParamTys);
LLVMTypeRef *Temp = from_val_array(ParamTys);
LLVMTypeRef Type = LLVMFunctionType(Type_val(RetTy), Temp, len, 1);
free(Temp);
return to_val(Type);
}
/* lltype -> bool */
value llvm_is_var_arg(value FunTy) {
return Val_bool(LLVMIsFunctionVarArg(Type_val(FunTy)));
}
/* lltype -> lltype */
value llvm_return_type(value FunTy) {
LLVMTypeRef Type = LLVMGetReturnType(Type_val(FunTy));
return to_val(Type);
}
/* lltype -> lltype array */
value llvm_param_types(value FunTy) {
unsigned Length = LLVMCountParamTypes(Type_val(FunTy));
value Tys = caml_alloc_tuple_uninit(Length);
LLVMGetParamTypes(Type_val(FunTy), (LLVMTypeRef *)Op_val(Tys));
for (unsigned I = 0; I < Length; ++I) {
Field(Tys, I) = to_val((LLVMTypeRef)Field(Tys, I));
}
return Tys;
}
/*--... Operations on struct types .........................................--*/
/* llcontext -> lltype array -> lltype */
value llvm_struct_type(value C, value ElementTypes) {
mlsize_t Length = Wosize_val(ElementTypes);
LLVMTypeRef *Temp = from_val_array(ElementTypes);
LLVMTypeRef Type = LLVMStructTypeInContext(Context_val(C), Temp, Length, 0);
free(Temp);
return to_val(Type);
}
/* llcontext -> lltype array -> lltype */
value llvm_packed_struct_type(value C, value ElementTypes) {
mlsize_t Length = Wosize_val(ElementTypes);
LLVMTypeRef *Temp = from_val_array(ElementTypes);
LLVMTypeRef Type = LLVMStructTypeInContext(Context_val(C), Temp, Length, 1);
free(Temp);
return to_val(Type);
}
/* llcontext -> string -> lltype */
value llvm_named_struct_type(value C, value Name) {
return to_val(LLVMStructCreateNamed(Context_val(C), String_val(Name)));
}
/* lltype -> lltype array -> bool -> unit */
value llvm_struct_set_body(value Ty, value ElementTypes, value Packed) {
mlsize_t Length = Wosize_val(ElementTypes);
LLVMTypeRef *Temp = from_val_array(ElementTypes);
LLVMStructSetBody(Type_val(Ty), Temp, Length, Bool_val(Packed));
return Val_unit;
}
/* lltype -> string option */
value llvm_struct_name(value Ty) {
const char *CStr = LLVMGetStructName(Type_val(Ty));
size_t Len;
if (!CStr)
return Val_none;
Len = strlen(CStr);
return cstr_to_string_option(CStr, Len);
}
/* lltype -> lltype array */
value llvm_struct_element_types(value StructTy) {
unsigned Length = LLVMCountStructElementTypes(Type_val(StructTy));
value Tys = caml_alloc_tuple_uninit(Length);
LLVMGetStructElementTypes(Type_val(StructTy), (LLVMTypeRef *)Op_val(Tys));
for (unsigned I = 0; I < Length; ++I) {
Field(Tys, I) = to_val((LLVMTypeRef)Field(Tys, I));
}
return Tys;
}
/* lltype -> bool */
value llvm_is_packed(value StructTy) {
return Val_bool(LLVMIsPackedStruct(Type_val(StructTy)));
}
/* lltype -> bool */
value llvm_is_opaque(value StructTy) {
return Val_bool(LLVMIsOpaqueStruct(Type_val(StructTy)));
}
/* lltype -> bool */
value llvm_is_literal(value StructTy) {
return Val_bool(LLVMIsLiteralStruct(Type_val(StructTy)));
}
/*--... Operations on array, pointer, and vector types .....................--*/
/* lltype -> lltype array */
value llvm_subtypes(value Ty) {
unsigned Length = LLVMGetNumContainedTypes(Type_val(Ty));
value Arr = caml_alloc_tuple_uninit(Length);
LLVMGetSubtypes(Type_val(Ty), (LLVMTypeRef *)Op_val(Arr));
for (unsigned I = 0; I < Length; ++I) {
Field(Arr, I) = to_val((LLVMTypeRef)Field(Arr, I));
}
return Arr;
}
/* lltype -> int -> lltype */
value llvm_array_type(value ElementTy, value Count) {
return to_val(LLVMArrayType(Type_val(ElementTy), Int_val(Count)));
}
/* llcontext -> lltype */
value llvm_pointer_type(value C) {
LLVMTypeRef Type = LLVMPointerTypeInContext(Context_val(C), 0);
return to_val(Type);
}
/* llcontext -> int -> lltype */
value llvm_qualified_pointer_type(value C, value AddressSpace) {
LLVMTypeRef Type =
LLVMPointerTypeInContext(Context_val(C), Int_val(AddressSpace));
return to_val(Type);
}
/* lltype -> int -> lltype */
value llvm_vector_type(value ElementTy, value Count) {
return to_val(LLVMVectorType(Type_val(ElementTy), Int_val(Count)));
}
/* lltype -> lltype */
value llvm_get_element_type(value Ty) {
return to_val(LLVMGetElementType(Type_val(Ty)));
}
/* lltype -> int */
value llvm_array_length(value ArrayTy) {
return Val_int(LLVMGetArrayLength2(Type_val(ArrayTy)));
}
/* lltype -> int */
value llvm_address_space(value PtrTy) {
return Val_int(LLVMGetPointerAddressSpace(Type_val(PtrTy)));
}
/* lltype -> int */
value llvm_vector_size(value VectorTy) {
return Val_int(LLVMGetVectorSize(Type_val(VectorTy)));
}
/*--... Operations on other types ..........................................--*/
/* llcontext -> lltype */
value llvm_void_type(value Context) {
return to_val(LLVMVoidTypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_label_type(value Context) {
return to_val(LLVMLabelTypeInContext(Context_val(Context)));
}
/* llcontext -> lltype */
value llvm_x86_mmx_type(value Context) {
return to_val(LLVMX86MMXTypeInContext(Context_val(Context)));
}
/* llmodule -> string -> lltype option */
value llvm_type_by_name(value M, value Name) {
return ptr_to_option(LLVMGetTypeByName(Module_val(M), String_val(Name)));
}
/*===-- VALUES ------------------------------------------------------------===*/
/* llvalue -> lltype */
value llvm_type_of(value Val) { return to_val(LLVMTypeOf(Value_val(Val))); }
/* keep in sync with ValueKind.t */
enum ValueKind {
NullValue = 0,
Argument,
BasicBlock,
InlineAsm,
MDNode,
MDString,
BlockAddress,
ConstantAggregateZero,
ConstantArray,
ConstantDataArray,
ConstantDataVector,
ConstantExpr,
ConstantFP,
ConstantInt,
ConstantPointerNull,
ConstantStruct,
ConstantVector,
Function,
GlobalAlias,
GlobalIFunc,
GlobalVariable,
UndefValue,
PoisonValue,
Instruction
};
/* llvalue -> ValueKind.t */
#define DEFINE_CASE(Val, Kind) \
do { \
if (LLVMIsA##Kind(Val)) \
return Val_int(Kind); \
} while (0)
value llvm_classify_value(value V) {
LLVMValueRef Val = Value_val(V);
if (!Val)
return Val_int(NullValue);
if (LLVMIsAConstant(Val)) {
DEFINE_CASE(Val, BlockAddress);
DEFINE_CASE(Val, ConstantAggregateZero);
DEFINE_CASE(Val, ConstantArray);
DEFINE_CASE(Val, ConstantDataArray);
DEFINE_CASE(Val, ConstantDataVector);
DEFINE_CASE(Val, ConstantExpr);
DEFINE_CASE(Val, ConstantFP);
DEFINE_CASE(Val, ConstantInt);
DEFINE_CASE(Val, ConstantPointerNull);
DEFINE_CASE(Val, ConstantStruct);
DEFINE_CASE(Val, ConstantVector);
}
if (LLVMIsAInstruction(Val)) {
value result = caml_alloc_small(1, 0);
Field(result, 0) = Val_int(LLVMGetInstructionOpcode(Val));
return result;
}
if (LLVMIsAGlobalValue(Val)) {
DEFINE_CASE(Val, Function);
DEFINE_CASE(Val, GlobalAlias);
DEFINE_CASE(Val, GlobalIFunc);
DEFINE_CASE(Val, GlobalVariable);
}
DEFINE_CASE(Val, Argument);
DEFINE_CASE(Val, BasicBlock);
DEFINE_CASE(Val, InlineAsm);
DEFINE_CASE(Val, MDNode);
DEFINE_CASE(Val, MDString);
DEFINE_CASE(Val, UndefValue);
DEFINE_CASE(Val, PoisonValue);
caml_failwith("Unknown Value class");
}
/* llvalue -> string */
value llvm_value_name(value Val) {
return caml_copy_string(LLVMGetValueName(Value_val(Val)));
}
/* string -> llvalue -> unit */
value llvm_set_value_name(value Name, value Val) {
LLVMSetValueName(Value_val(Val), String_val(Name));
return Val_unit;
}
/* llvalue -> unit */
value llvm_dump_value(value Val) {
LLVMDumpValue(Value_val(Val));
return Val_unit;
}
/* llvalue -> string */
value llvm_string_of_llvalue(value M) {
char *ValueCStr = LLVMPrintValueToString(Value_val(M));
value ValueStr = caml_copy_string(ValueCStr);
LLVMDisposeMessage(ValueCStr);
return ValueStr;
}
/* lldbgrecord -> string */
value llvm_string_of_lldbgrecord(value Record) {
char *ValueCStr = LLVMPrintDbgRecordToString(DbgRecord_val(Record));
value ValueStr = caml_copy_string(ValueCStr);
LLVMDisposeMessage(ValueCStr);
return ValueStr;
}
/* llvalue -> llvalue -> unit */
value llvm_replace_all_uses_with(value OldVal, value NewVal) {
LLVMReplaceAllUsesWith(Value_val(OldVal), Value_val(NewVal));
return Val_unit;
}
/*--... Operations on users ................................................--*/
/* llvalue -> int -> llvalue */
value llvm_operand(value V, value I) {
return to_val(LLVMGetOperand(Value_val(V), Int_val(I)));
}
/* llvalue -> int -> lluse */
value llvm_operand_use(value V, value I) {
return to_val(LLVMGetOperandUse(Value_val(V), Int_val(I)));
}
/* llvalue -> int -> llvalue -> unit */
value llvm_set_operand(value U, value I, value V) {
LLVMSetOperand(Value_val(U), Int_val(I), Value_val(V));
return Val_unit;
}
/* llvalue -> int */
value llvm_num_operands(value V) {
return Val_int(LLVMGetNumOperands(Value_val(V)));
}
/* llvalue -> int array */
value llvm_indices(value Instr) {
unsigned Length = LLVMGetNumIndices(Value_val(Instr));
const unsigned *Indices = LLVMGetIndices(Value_val(Instr));
value Array = caml_alloc_tuple_uninit(Length);
for (unsigned I = 0; I < Length; ++I) {
Field(Array, I) = Val_int(Indices[I]);
}
return Array;
}
/*--... Operations on constants of (mostly) any type .......................--*/
/* llvalue -> bool */
value llvm_is_constant(value Val) {
return Val_bool(LLVMIsConstant(Value_val(Val)));
}
/* lltype -> llvalue */
value llvm_const_null(value Ty) {
LLVMValueRef Value = LLVMConstNull(Type_val(Ty));
return to_val(Value);
}
/* lltype -> llvalue */
value llvm_const_all_ones(value Ty) {
LLVMValueRef Value = LLVMConstAllOnes(Type_val(Ty));
return to_val(Value);
}
/* lltype -> llvalue */
value llvm_const_pointer_null(value Ty) {
LLVMValueRef Value = LLVMConstPointerNull(Type_val(Ty));
return to_val(Value);
}
/* lltype -> llvalue */
value llvm_get_undef(value Ty) {
LLVMValueRef Value = LLVMGetUndef(Type_val(Ty));
return to_val(Value);
}
/* lltype -> llvalue */
value llvm_get_poison(value Ty) {
LLVMValueRef Value = LLVMGetPoison(Type_val(Ty));
return to_val(Value);
}
/* llvalue -> bool */
value llvm_is_null(value Val) { return Val_bool(LLVMIsNull(Value_val(Val))); }
/* llvalue -> bool */
value llvm_is_undef(value Val) { return Val_bool(LLVMIsUndef(Value_val(Val))); }
/* llvalue -> bool */
value llvm_is_poison(value Val) {
return Val_bool(LLVMIsPoison(Value_val(Val)));
}
/* llvalue -> Opcode.t */
value llvm_constexpr_get_opcode(value Val) {
return LLVMIsAConstantExpr(Value_val(Val))
? Val_int(LLVMGetConstOpcode(Value_val(Val)))
: Val_int(0);
}
/*--... Operations on instructions .........................................--*/
/* llvalue -> bool */
value llvm_has_metadata(value Val) {
return Val_bool(LLVMHasMetadata(Value_val(Val)));
}
/* llvalue -> int -> llvalue option */
value llvm_metadata(value Val, value MDKindID) {
return ptr_to_option(LLVMGetMetadata(Value_val(Val), Int_val(MDKindID)));
}
/* llvalue -> int -> llvalue -> unit */
value llvm_set_metadata(value Val, value MDKindID, value MD) {
LLVMSetMetadata(Value_val(Val), Int_val(MDKindID), Value_val(MD));
return Val_unit;
}
/* llvalue -> int -> unit */
value llvm_clear_metadata(value Val, value MDKindID) {
LLVMSetMetadata(Value_val(Val), Int_val(MDKindID), NULL);
return Val_unit;
}
/*--... Operations on metadata .............................................--*/
/* llcontext -> string -> llvalue */
value llvm_mdstring(value C, value S) {
return to_val(LLVMMDStringInContext(Context_val(C), String_val(S),
caml_string_length(S)));
}
/* llcontext -> llvalue array -> llvalue */
value llvm_mdnode(value C, value ElementVals) {
mlsize_t Length = Wosize_val(ElementVals);
LLVMValueRef *Temp = from_val_array(ElementVals);
LLVMValueRef Value = LLVMMDNodeInContext(Context_val(C), Temp, Length);
free(Temp);
return to_val(Value);
}
/* llcontext -> llvalue */
value llvm_mdnull(value C) { return to_val(NULL); }
/* llvalue -> string option */
value llvm_get_mdstring(value V) {
unsigned Len;
const char *CStr = LLVMGetMDString(Value_val(V), &Len);
return cstr_to_string_option(CStr, Len);
}
/* llvalue -> llvalue array */
value llvm_get_mdnode_operands(value Value) {
LLVMValueRef V = Value_val(Value);
unsigned Length = LLVMGetMDNodeNumOperands(V);
value Operands = caml_alloc_tuple_uninit(Length);
LLVMGetMDNodeOperands(V, (LLVMValueRef *)Op_val(Operands));
for (unsigned I = 0; I < Length; ++I) {
Field(Operands, I) = to_val((LLVMValueRef)Field(Operands, I));
}
return Operands;
}
/* llmodule -> string -> llvalue array */
value llvm_get_namedmd(value M, value Name) {
CAMLparam1(Name);
unsigned Length =
LLVMGetNamedMetadataNumOperands(Module_val(M), String_val(Name));
value Nodes = caml_alloc_tuple_uninit(Length);
LLVMGetNamedMetadataOperands(Module_val(M), String_val(Name),
(LLVMValueRef *)Op_val(Nodes));
for (unsigned I = 0; I < Length; ++I) {
Field(Nodes, I) = to_val((LLVMValueRef)Field(Nodes, I));
}
CAMLreturn(Nodes);
}
/* llmodule -> string -> llvalue -> unit */
value llvm_append_namedmd(value M, value Name, value Val) {
LLVMAddNamedMetadataOperand(Module_val(M), String_val(Name), Value_val(Val));
return Val_unit;
}
/* llvalue -> llmetadata */
value llvm_value_as_metadata(value Val) {
return to_val(LLVMValueAsMetadata(Value_val(Val)));
}
/* llcontext -> llmetadata -> llvalue */
value llvm_metadata_as_value(value C, value MD) {
return to_val(LLVMMetadataAsValue(Context_val(C), Metadata_val(MD)));
}
/*--... Operations on scalar constants .....................................--*/