1 (* RUN: %ocamlc llvm.cma llvm_bitwriter.cma %s -o %t
3 * RUN: llvm-dis < %t.bc > %t.ll
6 (* Note: It takes several seconds for ocamlc to link an executable with
7 libLLVMCore.a, so it's better to write a big test than a bunch of
14 (* Tiny unit test framework - really just to help find which line is busted *)
15 let exit_status = ref 0
20 prerr_endline (" " ^ name ^ "...")
24 let msg = if cond then " pass " else begin
28 prerr_endline (msg ^ (string_of_int !case_num))
31 prerr_endline (name ^ ":");
35 (*===-- Fixture -----------------------------------------------------------===*)
37 let filename = Sys.argv.(1)
38 let m = create_module filename
41 (*===-- Types -------------------------------------------------------------===*)
44 (* RUN: grep {Ty01.*void} < %t.ll
47 insist (add_type_name "Ty01" void_type m);
48 insist (Void_type == classify_type void_type);
50 (* RUN: grep {Ty02.*i1} < %t.ll
53 insist (add_type_name "Ty02" i1_type m);
54 insist (Integer_type == classify_type i1_type);
56 (* RUN: grep {Ty03.*i32} < %t.ll
59 insist (add_type_name "Ty03" i32_type m);
61 (* RUN: grep {Ty04.*i42} < %t.ll
64 let ty = make_integer_type 42 in
65 insist (add_type_name "Ty04" ty m);
67 (* RUN: grep {Ty05.*float} < %t.ll
70 insist (add_type_name "Ty05" float_type m);
71 insist (Float_type == classify_type float_type);
73 (* RUN: grep {Ty06.*double} < %t.ll
76 insist (add_type_name "Ty06" double_type m);
77 insist (Double_type == classify_type double_type);
79 (* RUN: grep {Ty07.*i32.*i1, double} < %t.ll
82 let ty = make_function_type i32_type [| i1_type; double_type |] false in
83 insist (add_type_name "Ty07" ty m);
84 insist (Function_type = classify_type ty);
85 insist (not (is_var_arg ty));
86 insist (i32_type == return_type ty);
87 insist (double_type == (param_types ty).(1));
89 (* RUN: grep {Ty08.*\.\.\.} < %t.ll
92 let ty = make_function_type void_type [| i32_type |] true in
93 insist (add_type_name "Ty08" ty m);
94 insist (is_var_arg ty);
96 (* RUN: grep {Ty09.*\\\[7 x i8\\\]} < %t.ll
99 let ty = make_array_type i8_type 7 in
100 insist (add_type_name "Ty09" ty m);
101 insist (7 = array_length ty);
102 insist (i8_type == element_type ty);
103 insist (Array_type == classify_type ty);
105 (* RUN: grep {Ty10.*float\*} < %t.ll
108 let ty = make_pointer_type float_type in
109 insist (add_type_name "Ty10" ty m);
110 insist (float_type == element_type ty);
111 insist (Pointer_type == classify_type ty);
113 (* RUN: grep {Ty11.*\<4 x i16\>} < %t.ll
116 let ty = make_vector_type i16_type 4 in
117 insist (add_type_name "Ty11" ty m);
118 insist (i16_type == element_type ty);
119 insist (4 = vector_size ty);
121 (* RUN: grep {Ty12.*opaque} < %t.ll
124 let ty = make_opaque_type () in
125 insist (add_type_name "Ty12" ty m);
127 insist (ty <> make_opaque_type ())
130 (*===-- Constants ---------------------------------------------------------===*)
132 let test_constants () =
133 (* RUN: grep {Const01.*i32.*-1} < %t.ll
136 let c = make_int_constant i32_type (-1) true in
137 ignore (define_global "Const01" c m);
138 insist (i32_type = type_of c);
139 insist (is_constant c);
141 (* RUN: grep {Const02.*i64.*-1} < %t.ll
144 let c = make_int_constant i64_type (-1) true in
145 ignore (define_global "Const02" c m);
146 insist (i64_type = type_of c);
148 (* RUN: grep {Const03.*i64.*4294967295} < %t.ll
151 let c = make_int64_constant i64_type (Int64.of_string "4294967295") false in
152 ignore (define_global "Const03" c m);
153 insist (i64_type = type_of c);
155 (* RUN: grep {Const04.*"cruel\\\\00world"} < %t.ll
158 let c = make_string_constant "cruel\x00world" false in
159 ignore (define_global "Const04" c m);
160 insist ((make_array_type i8_type 11) = type_of c);
162 (* RUN: grep {Const05.*"hi\\\\00again\\\\00"} < %t.ll
164 group "string w/ null";
165 let c = make_string_constant "hi\x00again" true in
166 ignore (define_global "Const05" c m);
167 insist ((make_array_type i8_type 9) = type_of c);
169 (* RUN: grep {Const06.*3.1459} < %t.ll
172 let c = make_real_constant double_type 3.1459 in
173 ignore (define_global "Const06" c m);
174 insist (double_type = type_of c);
176 let one = make_int_constant i16_type 1 true in
177 let two = make_int_constant i16_type 2 true in
178 let three = make_int_constant i32_type 3 true in
179 let four = make_int_constant i32_type 4 true in
181 (* RUN: grep {Const07.*\\\[ i32 3, i32 4 \\\]} < %t.ll
184 let c = make_array_constant i32_type [| three; four |] in
185 ignore (define_global "Const07" c m);
186 insist ((make_array_type i32_type 2) = (type_of c));
188 (* RUN: grep {Const08.*< i16 1, i16 2.* >} < %t.ll
191 let c = make_vector_constant [| one; two; one; two;
192 one; two; one; two |] in
193 ignore (define_global "Const08" c m);
194 insist ((make_vector_type i16_type 8) = (type_of c));
196 (* RUN: grep {Const09.*\{ i16, i16, i32, i32 \} \{} < %t.ll
199 let c = make_struct_constant [| one; two; three; four |] false in
200 ignore (define_global "Const09" c m);
201 insist ((make_struct_type [| i16_type; i16_type; i32_type; i32_type |] false)
204 (* RUN: grep {Const10.*zeroinit} < %t.ll
207 let c = make_null (make_struct_type [| i1_type; i8_type;
208 i64_type; double_type |] true) in
209 ignore (define_global "Const10" c m);
211 (* RUN: grep {Const11.*-1} < %t.ll
214 let c = make_all_ones i64_type in
215 ignore (define_global "Const11" c m);
217 (* RUN: grep {Const12.*undef} < %t.ll
220 let c = make_undef i1_type in
221 ignore (define_global "Const12" c m);
222 insist (i1_type = type_of c);
226 (*===-- Global Values -----------------------------------------------------===*)
228 let test_global_values () =
229 let (++) x f = f x; x in
230 let zero32 = make_null i32_type in
232 (* RUN: grep {GVal01} < %t.ll
235 let g = define_global "TEMPORARY" zero32 m in
236 insist ("TEMPORARY" = value_name g);
237 set_value_name "GVal01" g;
238 insist ("GVal01" = value_name g);
240 (* RUN: grep {GVal02.*linkonce} < %t.ll
243 let g = define_global "GVal02" zero32 m ++
244 set_linkage Link_once_linkage in
245 insist (Link_once_linkage = linkage g);
247 (* RUN: grep {GVal03.*Hanalei} < %t.ll
250 let g = define_global "GVal03" zero32 m ++
251 set_section "Hanalei" in
252 insist ("Hanalei" = section g);
254 (* RUN: grep {GVal04.*hidden} < %t.ll
257 let g = define_global "GVal04" zero32 m ++
258 set_visibility Hidden_visibility in
259 insist (Hidden_visibility = visibility g);
261 (* RUN: grep {GVal05.*align 128} < %t.ll
264 let g = define_global "GVal05" zero32 m ++
266 insist (128 = alignment g)
269 (*===-- Global Variables --------------------------------------------------===*)
271 let test_global_variables () =
272 let (++) x f = f x; x in
273 let fourty_two32 = make_int_constant i32_type 42 false in
275 (* RUN: grep {GVar01.*external} < %t.ll
277 group "declarations";
278 let g = declare_global i32_type "GVar01" m in
279 insist (is_declaration g);
281 (* RUN: grep {GVar02.*42} < %t.ll
282 * RUN: grep {GVar03.*42} < %t.ll
285 let g = define_global "GVar02" fourty_two32 m in
286 let g2 = declare_global i32_type "GVar03" m ++
287 set_initializer fourty_two32 in
288 insist (not (is_declaration g));
289 insist (not (is_declaration g2));
290 insist ((global_initializer g) == (global_initializer g2));
292 (* RUN: grep {GVar04.*thread_local} < %t.ll
295 let g = define_global "GVar04" fourty_two32 m ++
296 set_thread_local true in
297 insist (is_thread_local g);
299 (* RUN: grep -v {GVar05} < %t.ll
302 let g = define_global "GVar05" fourty_two32 m in
306 (*===-- Writer ------------------------------------------------------------===*)
310 insist (write_bitcode_file m filename);
315 (*===-- Driver ------------------------------------------------------------===*)
318 suite "types" test_types;
319 suite "constants" test_constants;
320 suite "global values" test_global_values;
321 suite "global variables" test_global_variables;
322 suite "writer" test_writer;