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\000world" 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\000again" true in
166 prerr_string "====> ";
167 prerr_int (array_length (type_of c));
168 prerr_endline " <====";
169 ignore (define_global "Const05" c m);
170 insist ((make_array_type i8_type 9) = type_of c);
172 (* RUN: grep {Const06.*3.1459} < %t.ll
175 let c = make_real_constant double_type 3.1459 in
176 ignore (define_global "Const06" c m);
177 insist (double_type = type_of c);
179 let one = make_int_constant i16_type 1 true in
180 let two = make_int_constant i16_type 2 true in
181 let three = make_int_constant i32_type 3 true in
182 let four = make_int_constant i32_type 4 true in
184 (* RUN: grep {Const07.*\\\[ i32 3, i32 4 \\\]} < %t.ll
187 let c = make_array_constant i32_type [| three; four |] in
188 ignore (define_global "Const07" c m);
189 insist ((make_array_type i32_type 2) = (type_of c));
191 (* RUN: grep {Const08.*< i16 1, i16 2.* >} < %t.ll
194 let c = make_vector_constant [| one; two; one; two;
195 one; two; one; two |] in
196 ignore (define_global "Const08" c m);
197 insist ((make_vector_type i16_type 8) = (type_of c));
199 (* RUN: grep {Const09.*\{ i16, i16, i32, i32 \} \{} < %t.ll
202 let c = make_struct_constant [| one; two; three; four |] false in
203 ignore (define_global "Const09" c m);
204 insist ((make_struct_type [| i16_type; i16_type; i32_type; i32_type |] false)
207 (* RUN: grep {Const10.*zeroinit} < %t.ll
210 let c = make_null (make_struct_type [| i1_type; i8_type;
211 i64_type; double_type |] true) in
212 ignore (define_global "Const10" c m);
214 (* RUN: grep {Const11.*-1} < %t.ll
217 let c = make_all_ones i64_type in
218 ignore (define_global "Const11" c m);
220 (* RUN: grep {Const12.*undef} < %t.ll
223 let c = make_undef i1_type in
224 ignore (define_global "Const12" c m);
225 insist (i1_type = type_of c);
229 (*===-- Global Values -----------------------------------------------------===*)
231 let test_global_values () =
232 let (++) x f = f x; x in
233 let zero32 = make_null i32_type in
235 (* RUN: grep {GVal01} < %t.ll
238 let g = define_global "TEMPORARY" zero32 m in
239 insist ("TEMPORARY" = value_name g);
240 set_value_name "GVal01" g;
241 insist ("GVal01" = value_name g);
243 (* RUN: grep {GVal02.*linkonce} < %t.ll
246 let g = define_global "GVal02" zero32 m ++
247 set_linkage Link_once_linkage in
248 insist (Link_once_linkage = linkage g);
250 (* RUN: grep {GVal03.*Hanalei} < %t.ll
253 let g = define_global "GVal03" zero32 m ++
254 set_section "Hanalei" in
255 insist ("Hanalei" = section g);
257 (* RUN: grep {GVal04.*hidden} < %t.ll
260 let g = define_global "GVal04" zero32 m ++
261 set_visibility Hidden_visibility in
262 insist (Hidden_visibility = visibility g);
264 (* RUN: grep {GVal05.*align 128} < %t.ll
267 let g = define_global "GVal05" zero32 m ++
269 insist (128 = alignment g)
272 (*===-- Global Variables --------------------------------------------------===*)
274 let test_global_variables () =
275 let (++) x f = f x; x in
276 let fourty_two32 = make_int_constant i32_type 42 false in
278 (* RUN: grep {GVar01.*external} < %t.ll
280 group "declarations";
281 let g = declare_global i32_type "GVar01" m in
282 insist (is_declaration g);
284 (* RUN: grep {GVar02.*42} < %t.ll
285 * RUN: grep {GVar03.*42} < %t.ll
288 let g = define_global "GVar02" fourty_two32 m in
289 let g2 = declare_global i32_type "GVar03" m ++
290 set_initializer fourty_two32 in
291 insist (not (is_declaration g));
292 insist (not (is_declaration g2));
293 insist ((global_initializer g) == (global_initializer g2));
295 (* RUN: grep {GVar04.*thread_local} < %t.ll
298 let g = define_global "GVar04" fourty_two32 m ++
299 set_thread_local true in
300 insist (is_thread_local g);
302 (* RUN: grep -v {GVar05} < %t.ll
305 let g = define_global "GVar05" fourty_two32 m in
309 (*===-- Writer ------------------------------------------------------------===*)
313 insist (write_bitcode_file m filename);
318 (*===-- Driver ------------------------------------------------------------===*)
321 suite "types" test_types;
322 suite "constants" test_constants;
323 suite "global values" test_global_values;
324 suite "global variables" test_global_variables;
325 suite "writer" test_writer;