blob: ef274a887d0b6057050be1e3c973f881b60c5057 (
plain)
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
|
(* RUN: %ocamlc llvm.cma llvm_bitwriter.cma %s -o %t
* RUN: ./%t %t.bc
* RUN: llvm-dis < %t.bc > %t.ll
*)
(* Note: It takes several seconds for ocamlc to link an executable with
libLLVMCore.a, so it's better to write a big test than a bunch of
little ones. *)
open Llvm
open Llvm_bitwriter
(* Tiny unit test framework - really just to help find which line is busted *)
let exit_status = ref 0
let case_num = ref 0
let group name =
case_num := 0;
prerr_endline (" " ^ name ^ "...")
let insist cond =
incr case_num;
let msg = if cond then " pass " else begin
exit_status := 10;
" FAIL "
end in
prerr_endline (msg ^ (string_of_int !case_num))
let suite name f =
prerr_endline (name ^ ":");
f ()
(*===-- Fixture -----------------------------------------------------------===*)
let filename = Sys.argv.(1)
let m = create_module filename
(*===-- Types -------------------------------------------------------------===*)
let test_types () =
(* RUN: grep {Ty01.*void} < %t.ll
*)
group "void";
insist (add_type_name "Ty01" void_type m);
insist (Void_type == classify_type void_type);
(* RUN: grep {Ty02.*i1} < %t.ll
*)
group "i1";
insist (add_type_name "Ty02" i1_type m);
insist (Integer_type == classify_type i1_type);
(* RUN: grep {Ty03.*i32} < %t.ll
*)
group "i32";
insist (add_type_name "Ty03" i32_type m);
(* RUN: grep {Ty04.*i42} < %t.ll
*)
group "i42";
let ty = make_integer_type 42 in
insist (add_type_name "Ty04" ty m);
(* RUN: grep {Ty05.*float} < %t.ll
*)
group "float";
insist (add_type_name "Ty05" float_type m);
insist (Float_type == classify_type float_type);
(* RUN: grep {Ty06.*double} < %t.ll
*)
group "double";
insist (add_type_name "Ty06" double_type m);
insist (Double_type == classify_type double_type);
(* RUN: grep {Ty07.*i32.*i1, double} < %t.ll
*)
group "function";
let ty = make_function_type i32_type [| i1_type; double_type |] false in
insist (add_type_name "Ty07" ty m);
insist (Function_type = classify_type ty);
insist (not (is_var_arg ty));
insist (i32_type == return_type ty);
insist (double_type == (param_types ty).(1));
(* RUN: grep {Ty08.*\.\.\.} < %t.ll
*)
group "vararg";
let ty = make_function_type void_type [| i32_type |] true in
insist (add_type_name "Ty08" ty m);
insist (is_var_arg ty);
(* RUN: grep {Ty09.*\\\[7 x i8\\\]} < %t.ll
*)
group "array";
let ty = make_array_type i8_type 7 in
insist (add_type_name "Ty09" ty m);
insist (7 = array_length ty);
insist (i8_type == element_type ty);
insist (Array_type == classify_type ty);
(* RUN: grep {Ty10.*float\*} < %t.ll
*)
group "pointer";
let ty = make_pointer_type float_type in
insist (add_type_name "Ty10" ty m);
insist (float_type == element_type ty);
insist (Pointer_type == classify_type ty);
(* RUN: grep {Ty11.*\<4 x i16\>} < %t.ll
*)
group "vector";
let ty = make_vector_type i16_type 4 in
insist (add_type_name "Ty11" ty m);
insist (i16_type == element_type ty);
insist (4 = vector_size ty);
(* RUN: grep {Ty12.*opaque} < %t.ll
*)
group "opaque";
let ty = make_opaque_type () in
insist (add_type_name "Ty12" ty m);
insist (ty == ty);
insist (ty <> make_opaque_type ())
(*===-- Constants ---------------------------------------------------------===*)
let test_constants () =
(* RUN: grep {Const01.*i32.*-1} < %t.ll
*)
group "int";
let c = make_int_constant i32_type (-1) true in
ignore (define_global "Const01" c m);
insist (i32_type = type_of c);
insist (is_constant c);
(* RUN: grep {Const02.*i64.*-1} < %t.ll
*)
group "sext int";
let c = make_int_constant i64_type (-1) true in
ignore (define_global "Const02" c m);
insist (i64_type = type_of c);
(* RUN: grep {Const03.*i64.*4294967295} < %t.ll
*)
group "zext int64";
let c = make_int64_constant i64_type (Int64.of_string "4294967295") false in
ignore (define_global "Const03" c m);
insist (i64_type = type_of c);
(* RUN: grep {Const04.*"cruel\\\\00world"} < %t.ll
*)
group "string";
let c = make_string_constant "cruel\x00world" false in
ignore (define_global "Const04" c m);
insist ((make_array_type i8_type 11) = type_of c);
(* RUN: grep {Const05.*"hi\\\\00again\\\\00"} < %t.ll
*)
group "string w/ null";
let c = make_string_constant "hi\x00again" true in
ignore (define_global "Const05" c m);
insist ((make_array_type i8_type 9) = type_of c);
(* RUN: grep {Const06.*3.1459} < %t.ll
*)
group "real";
let c = make_real_constant double_type 3.1459 in
ignore (define_global "Const06" c m);
insist (double_type = type_of c);
let one = make_int_constant i16_type 1 true in
let two = make_int_constant i16_type 2 true in
let three = make_int_constant i32_type 3 true in
let four = make_int_constant i32_type 4 true in
(* RUN: grep {Const07.*\\\[ i32 3, i32 4 \\\]} < %t.ll
*)
group "array";
let c = make_array_constant i32_type [| three; four |] in
ignore (define_global "Const07" c m);
insist ((make_array_type i32_type 2) = (type_of c));
(* RUN: grep {Const08.*< i16 1, i16 2.* >} < %t.ll
*)
group "vector";
let c = make_vector_constant [| one; two; one; two;
one; two; one; two |] in
ignore (define_global "Const08" c m);
insist ((make_vector_type i16_type 8) = (type_of c));
(* RUN: grep {Const09.*\{ i16, i16, i32, i32 \} \{} < %t.ll
*)
group "structure";
let c = make_struct_constant [| one; two; three; four |] false in
ignore (define_global "Const09" c m);
insist ((make_struct_type [| i16_type; i16_type; i32_type; i32_type |] false)
= (type_of c));
(* RUN: grep {Const10.*zeroinit} < %t.ll
*)
group "null";
let c = make_null (make_struct_type [| i1_type; i8_type;
i64_type; double_type |] true) in
ignore (define_global "Const10" c m);
(* RUN: grep {Const11.*-1} < %t.ll
*)
group "all ones";
let c = make_all_ones i64_type in
ignore (define_global "Const11" c m);
(* RUN: grep {Const12.*undef} < %t.ll
*)
group "undef";
let c = make_undef i1_type in
ignore (define_global "Const12" c m);
insist (i1_type = type_of c);
insist (is_undef c)
(*===-- Global Values -----------------------------------------------------===*)
let test_global_values () =
let (++) x f = f x; x in
let zero32 = make_null i32_type in
(* RUN: grep {GVal01} < %t.ll
*)
group "naming";
let g = define_global "TEMPORARY" zero32 m in
insist ("TEMPORARY" = value_name g);
set_value_name "GVal01" g;
insist ("GVal01" = value_name g);
(* RUN: grep {GVal02.*linkonce} < %t.ll
*)
group "linkage";
let g = define_global "GVal02" zero32 m ++
set_linkage Link_once_linkage in
insist (Link_once_linkage = linkage g);
(* RUN: grep {GVal03.*Hanalei} < %t.ll
*)
group "section";
let g = define_global "GVal03" zero32 m ++
set_section "Hanalei" in
insist ("Hanalei" = section g);
(* RUN: grep {GVal04.*hidden} < %t.ll
*)
group "visibility";
let g = define_global "GVal04" zero32 m ++
set_visibility Hidden_visibility in
insist (Hidden_visibility = visibility g);
(* RUN: grep {GVal05.*align 128} < %t.ll
*)
group "alignment";
let g = define_global "GVal05" zero32 m ++
set_alignment 128 in
insist (128 = alignment g)
(*===-- Global Variables --------------------------------------------------===*)
let test_global_variables () =
let (++) x f = f x; x in
let fourty_two32 = make_int_constant i32_type 42 false in
(* RUN: grep {GVar01.*external} < %t.ll
*)
group "declarations";
let g = declare_global i32_type "GVar01" m in
insist (is_declaration g);
(* RUN: grep {GVar02.*42} < %t.ll
* RUN: grep {GVar03.*42} < %t.ll
*)
group "definitions";
let g = define_global "GVar02" fourty_two32 m in
let g2 = declare_global i32_type "GVar03" m ++
set_initializer fourty_two32 in
insist (not (is_declaration g));
insist (not (is_declaration g2));
insist ((global_initializer g) == (global_initializer g2));
(* RUN: grep {GVar04.*thread_local} < %t.ll
*)
group "threadlocal";
let g = define_global "GVar04" fourty_two32 m ++
set_thread_local true in
insist (is_thread_local g);
(* RUN: grep -v {GVar05} < %t.ll
*)
group "delete";
let g = define_global "GVar05" fourty_two32 m in
delete_global g
(*===-- Writer ------------------------------------------------------------===*)
let test_writer () =
group "writer";
insist (write_bitcode_file m filename);
dispose_module m
(*===-- Driver ------------------------------------------------------------===*)
let _ =
suite "types" test_types;
suite "constants" test_constants;
suite "global values" test_global_values;
suite "global variables" test_global_variables;
suite "writer" test_writer;
exit !exit_status
|