10a248bf714f18b1cdcda5d7b811303c232d6e885Daniel Dunbar(* RUN: rm -rf %t.builddir
20a248bf714f18b1cdcda5d7b811303c232d6e885Daniel Dunbar * RUN: mkdir -p %t.builddir
30a248bf714f18b1cdcda5d7b811303c232d6e885Daniel Dunbar * RUN: cp %s %t.builddir
40a248bf714f18b1cdcda5d7b811303c232d6e885Daniel Dunbar * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa llvm_bitwriter.cmxa %t.builddir/vmcore.ml -o %t
52a7d731828ab87bba42f7ecac8db509646cd2cddBob Wilson * RUN: %t %t.bc
6ed094c32ff2683c07d8eec558ee9defa4f0c00f6Reid Kleckner * RUN: llvm-dis < %t.bc > %t.ll
7ed094c32ff2683c07d8eec558ee9defa4f0c00f6Reid Kleckner * RUN: FileCheck %s < %t.ll
89f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner * Do a second pass for things that shouldn't be anywhere.
9ed094c32ff2683c07d8eec558ee9defa4f0c00f6Reid Kleckner * RUN: FileCheck -check-prefix=CHECK-NOWHERE %s < %t.ll
10874f021bc16aaf4619fd818da150f1b63e408c80Daniel Dunbar * XFAIL: vg_leak
118ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen *)
128ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
13b405bbe6648d24b0cb5b03dc43c3997ac4f50a9bErick Tryzelaar(* Note: It takes several seconds for ocamlopt to link an executable with
148ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen         libLLVMCore.a, so it's better to write a big test than a bunch of
158ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen         little ones. *)
168ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
178ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenopen Llvm
188ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenopen Llvm_bitwriter
198ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
208ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
21344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen(* Tiny unit test framework - really just to help find which line is busted *)
228ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenlet exit_status = ref 0
234468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksenlet suite_name = ref ""
244468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksenlet group_name = ref ""
258ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenlet case_num = ref 0
264468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksenlet print_checkpoints = false
275371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaarlet context = global_context ()
28b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaarlet i1_type = Llvm.i1_type context
29b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaarlet i8_type = Llvm.i8_type context
30b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaarlet i16_type = Llvm.i16_type context
31b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaarlet i32_type = Llvm.i32_type context
32b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaarlet i64_type = Llvm.i64_type context
33b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaarlet void_type = Llvm.void_type context
34b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaarlet float_type = Llvm.float_type context
35b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaarlet double_type = Llvm.double_type context
36b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaarlet fp128_type = Llvm.fp128_type context
378ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
388ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenlet group name =
394468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  group_name := !suite_name ^ "/" ^ name;
408ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  case_num := 0;
414468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  if print_checkpoints then
424468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen    prerr_endline ("  " ^ name ^ "...")
438ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
448ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenlet insist cond =
458ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  incr case_num;
464468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  if not cond then
474468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen    exit_status := 10;
484468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  match print_checkpoints, cond with
494468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  | false, true -> ()
504468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  | false, false ->
514468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen      prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num))
524468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  | true, true ->
534468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen      prerr_endline ("    " ^ (string_of_int !case_num))
544468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  | true, false ->
554468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen      prerr_endline ("    " ^ (string_of_int !case_num) ^ " FAIL")
568ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
578ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenlet suite name f =
584468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  suite_name := name;
594468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen  if print_checkpoints then
604468440a2a92fecd57f002b1b9c0683d2b9c4aeaGordon Henriksen    prerr_endline (name ^ ":");
618ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  f ()
628ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
638ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
648ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen(*===-- Fixture -----------------------------------------------------------===*)
658ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
668ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenlet filename = Sys.argv.(1)
675371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaarlet m = create_module context filename
688ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
698ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
70ba0c7cd012b064e2d8009480f9ae6f7d75e00e13Peter Zotov(*===-- Conversion --------------------------------------------------------===*)
71ba0c7cd012b064e2d8009480f9ae6f7d75e00e13Peter Zotov
72ba0c7cd012b064e2d8009480f9ae6f7d75e00e13Peter Zotovlet test_conversion () =
73ba0c7cd012b064e2d8009480f9ae6f7d75e00e13Peter Zotov  insist ("i32" = (string_of_lltype i32_type));
74ba0c7cd012b064e2d8009480f9ae6f7d75e00e13Peter Zotov  let c = const_int i32_type 42 in
75ba0c7cd012b064e2d8009480f9ae6f7d75e00e13Peter Zotov  insist ("i32 42" = (string_of_llvalue c))
76ba0c7cd012b064e2d8009480f9ae6f7d75e00e13Peter Zotov
77ba0c7cd012b064e2d8009480f9ae6f7d75e00e13Peter Zotov
78a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen(*===-- Target ------------------------------------------------------------===*)
79a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen
80a353ffa7e556bfd2864474911174da691117f691Gordon Henriksenlet test_target () =
81a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen  begin group "triple";
82a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen    let trip = "i686-apple-darwin8" in
83a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen    set_target_triple trip m;
84a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen    insist (trip = target_triple m)
85a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen  end;
86a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen  
87a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen  begin group "layout";
8836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    let layout = "e" in
89a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen    set_data_layout layout m;
90a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen    insist (layout = data_layout m)
91a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen  end
9236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  (* CHECK: target datalayout = "e"
939f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: target triple = "i686-apple-darwin8"
949f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   *)
95a353ffa7e556bfd2864474911174da691117f691Gordon Henriksen
968ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
97344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen(*===-- Constants ---------------------------------------------------------===*)
98344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen
99344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksenlet test_constants () =
1009f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_int{{.*}}i32{{.*}}-1
101344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
102344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "int";
103e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let c = const_int i32_type (-1) in
1046e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_int" c m);
105344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  insist (i32_type = type_of c);
106344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  insist (is_constant c);
107344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen
1089f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_sext_int{{.*}}i64{{.*}}-1
109344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
110344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "sext int";
111e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let c = const_int i64_type (-1) in
1126e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_sext_int" c m);
113344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  insist (i64_type = type_of c);
114344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen
1159f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_zext_int64{{.*}}i64{{.*}}4294967295
116344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
117344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "zext int64";
118e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in
1196e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_zext_int64" c m);
120344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  insist (i64_type = type_of c);
121344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen
1229f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_int_string{{.*}}i32{{.*}}-1
12345d6ac2cc13f7881687c2d7f03f9b9892fd85e6eErick Tryzelaar   *)
12445d6ac2cc13f7881687c2d7f03f9b9892fd85e6eErick Tryzelaar  group "int string";
12545d6ac2cc13f7881687c2d7f03f9b9892fd85e6eErick Tryzelaar  let c = const_int_of_string i32_type "-1" 10 in
1266e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_int_string" c m);
12745d6ac2cc13f7881687c2d7f03f9b9892fd85e6eErick Tryzelaar  insist (i32_type = type_of c);
12845d6ac2cc13f7881687c2d7f03f9b9892fd85e6eErick Tryzelaar
1299f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: @const_string = global {{.*}}c"cruel\00world"
130344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
131344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "string";
132b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaar  let c = const_string context "cruel\000world" in
1336e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_string" c m);
134957f9fe1cce0957bcde4a1093da83e17aaec6764Gordon Henriksen  insist ((array_type i8_type 11) = type_of c);
135344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen
1369f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_stringz{{.*}}"hi\00again\00"
137344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
138e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  group "stringz";
139b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaar  let c = const_stringz context "hi\000again" in
1406e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_stringz" c m);
141957f9fe1cce0957bcde4a1093da83e17aaec6764Gordon Henriksen  insist ((array_type i8_type 9) = type_of c);
142344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen
1439f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_single{{.*}}2.75
1449f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_double{{.*}}3.1459
1459f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_double_string{{.*}}1.25
146e62a8a353c3b21b551c00b9025800d3352e5349eGordon Henriksen   *)
147e62a8a353c3b21b551c00b9025800d3352e5349eGordon Henriksen  begin group "real";
148e62a8a353c3b21b551c00b9025800d3352e5349eGordon Henriksen    let cs = const_float float_type 2.75 in
1496e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar    ignore (define_global "const_single" cs m);
150e62a8a353c3b21b551c00b9025800d3352e5349eGordon Henriksen    insist (float_type = type_of cs);
151e62a8a353c3b21b551c00b9025800d3352e5349eGordon Henriksen    
152e62a8a353c3b21b551c00b9025800d3352e5349eGordon Henriksen    let cd = const_float double_type 3.1459 in
1536e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar    ignore (define_global "const_double" cd m);
15445d6ac2cc13f7881687c2d7f03f9b9892fd85e6eErick Tryzelaar    insist (double_type = type_of cd);
15545d6ac2cc13f7881687c2d7f03f9b9892fd85e6eErick Tryzelaar
15645d6ac2cc13f7881687c2d7f03f9b9892fd85e6eErick Tryzelaar    let cd = const_float_of_string double_type "1.25" in
1576e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar    ignore (define_global "const_double_string" cd m);
158e62a8a353c3b21b551c00b9025800d3352e5349eGordon Henriksen    insist (double_type = type_of cd)
159e62a8a353c3b21b551c00b9025800d3352e5349eGordon Henriksen  end;
160344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  
161e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let one = const_int i16_type 1 in
162e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let two = const_int i16_type 2 in
163e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let three = const_int i32_type 3 in
164e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let four = const_int i32_type 4 in
165344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  
1669f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_array{{.*}}[i32 3, i32 4]
167344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
168344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "array";
169e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let c = const_array i32_type [| three; four |] in
1706e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_array" c m);
171957f9fe1cce0957bcde4a1093da83e17aaec6764Gordon Henriksen  insist ((array_type i32_type 2) = (type_of c));
172344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  
1739f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_vector{{.*}}<i16 1, i16 2{{.*}}>
174344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
175344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "vector";
176e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let c = const_vector [| one; two; one; two;
177e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen                          one; two; one; two |] in
1786e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_vector" c m);
179957f9fe1cce0957bcde4a1093da83e17aaec6764Gordon Henriksen  insist ((vector_type i16_type 8) = (type_of c));
1805371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar
1819f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4
182344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
183344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "structure";
1845371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  let c = const_struct context [| one; two; three; four |] in
1856e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_structure" c m);
1865371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
187344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen        = (type_of c));
1881940dd10dd6b79e3332f384756c268b85d0ad400Erick Tryzelaar
1899f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_null{{.*}}zeroinit
190344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
191344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "null";
1925371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type;
1935371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar                                                    double_type |]) in
1946e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_null" c m);
195344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  
1969f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_all_ones{{.*}}-1
197344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
198344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "all ones";
199e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let c = const_all_ones i64_type in
2006e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_all_ones" c m);
2015b0a7741ad262315d6365250a2da2edb8ba37e69Erick Tryzelaar
2025b0a7741ad262315d6365250a2da2edb8ba37e69Erick Tryzelaar  group "pointer null"; begin
2039f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner    (* CHECK: const_pointer_null = global i64* null
2045b0a7741ad262315d6365250a2da2edb8ba37e69Erick Tryzelaar     *)
2055b0a7741ad262315d6365250a2da2edb8ba37e69Erick Tryzelaar    let c = const_pointer_null (pointer_type i64_type) in
2065b0a7741ad262315d6365250a2da2edb8ba37e69Erick Tryzelaar    ignore (define_global "const_pointer_null" c m);
2075b0a7741ad262315d6365250a2da2edb8ba37e69Erick Tryzelaar  end;
208344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  
2099f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_undef{{.*}}undef
210344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen   *)
211344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "undef";
212e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let c = undef i1_type in
2136e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_undef" c m);
214344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  insist (i1_type = type_of c);
2154647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  insist (is_undef c);
2164647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  
2174647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  group "constant arithmetic";
2189f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: @const_neg = global i64 sub
2199f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_nsw_neg = global i64 sub nsw
2209f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_nuw_neg = global i64 sub nuw
2219f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_fneg = global double fsub
2229f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_not = global i64 xor
2239f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_add = global i64 add
2249f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_nsw_add = global i64 add nsw
2259f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_nuw_add = global i64 add nuw
2269f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_fadd = global double fadd
2279f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_sub = global i64 sub
2289f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_nsw_sub = global i64 sub nsw
2299f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_nuw_sub = global i64 sub nuw
2309f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_fsub = global double fsub
2319f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_mul = global i64 mul
2329f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_nsw_mul = global i64 mul nsw
2339f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_nuw_mul = global i64 mul nuw
2349f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_fmul = global double fmul
2359f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_udiv = global i64 udiv
2369f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_sdiv = global i64 sdiv
2379f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_exact_sdiv = global i64 sdiv exact
2389f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_fdiv = global double fdiv
2399f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_urem = global i64 urem
2409f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_srem = global i64 srem
2419f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_frem = global double frem
2429f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_and = global i64 and
2439f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_or = global i64 or
2449f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_xor = global i64 xor
2459f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_icmp = global i1 icmp sle
2469f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: @const_fcmp = global i1 fcmp ole
2474647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen   *)
248957f9fe1cce0957bcde4a1093da83e17aaec6764Gordon Henriksen  let void_ptr = pointer_type i8_type in
249e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let five = const_int i64_type 5 in
2504647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  let ffive = const_uitofp five double_type in
251e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in
2524647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  let foldbomb = const_ptrtoint foldbomb_gv i64_type in
2534647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  let ffoldbomb = const_uitofp foldbomb double_type in
2546e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_neg" (const_neg foldbomb) m);
25519f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
25619f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
25719f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_fneg" (const_fneg ffoldbomb) m);
2586e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_not" (const_not foldbomb) m);
2596e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_add" (const_add foldbomb five) m);
26019f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
26119f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m);
26219f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m);
2636e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_sub" (const_sub foldbomb five) m);
26419f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m);
26519f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m);
26619f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m);
2676e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_mul" (const_mul foldbomb five) m);
26819f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m);
26919f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m);
27019f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m);
2716e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_udiv" (const_udiv foldbomb five) m);
2726e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m);
27319f047f5f6c44d1cd5681f9314b3ff5e7bae122dErick Tryzelaar  ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m);
2746e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m);
2756e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_urem" (const_urem foldbomb five) m);
2766e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_srem" (const_srem foldbomb five) m);
2776e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m);
2786e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_and" (const_and foldbomb five) m);
2796e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_or" (const_or foldbomb five) m);
2806e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_xor" (const_xor foldbomb five) m);
2816e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m);
2826e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
2834647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  
2844647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  group "constant casts";
2859f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_trunc{{.*}}trunc
2869f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_sext{{.*}}sext
2879f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_zext{{.*}}zext
2889f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_fptrunc{{.*}}fptrunc
2899f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_fpext{{.*}}fpext
2909f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_uitofp{{.*}}uitofp
2919f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_sitofp{{.*}}sitofp
2929f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_fptoui{{.*}}fptoui
2939f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_fptosi{{.*}}fptosi
2949f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_ptrtoint{{.*}}ptrtoint
2959f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_inttoptr{{.*}}inttoptr
2969f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_bitcast{{.*}}bitcast
297c8ac229cc8349685117f68bc6f1da04f98015cd6Peter Zotov   * CHECK: const_intcast{{.*}}zext
2984647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen   *)
299b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaar  let i128_type = integer_type context 128 in
3006e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five)
3014647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen                                               i8_type) m);
3026e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_sext" (const_sext foldbomb i128_type) m);
3036e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_zext" (const_zext foldbomb i128_type) m);
3046e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m);
3056e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m);
3066e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m);
3076e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m);
3086e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m);
3096e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m);
3106e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_ptrtoint" (const_ptrtoint 
311957f9fe1cce0957bcde4a1093da83e17aaec6764Gordon Henriksen    (const_gep (const_null (pointer_type i8_type))
312e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen               [| const_int i32_type 1 |])
3134647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen    i32_type) m);
3146e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five)
3154647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen                                                  void_ptr) m);
3166e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m);
317c8ac229cc8349685117f68bc6f1da04f98015cd6Peter Zotov  ignore (define_global "const_intcast"
318c8ac229cc8349685117f68bc6f1da04f98015cd6Peter Zotov          (const_intcast foldbomb i128_type ~is_signed:false) m);
3194647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  
3204647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen  group "misc constants";
3219f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null
3229f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_gep{{.*}}getelementptr
3239f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_select{{.*}}select
3249f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_extractelement{{.*}}extractelement
3259f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_insertelement{{.*}}insertelement
3269f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner   * CHECK: const_shufflevector = global <4 x i32> <i32 0, i32 1, i32 1, i32 0>
3274647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen   *)
3286e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m);
3296e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m);
3306e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_select" (const_select
331404a1942e43ca967700cc2608eb97b863add2677Gordon Henriksen    (const_icmp Icmp.Sle foldbomb five)
332e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen    (const_int i8_type (-1))
333e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen    (const_int i8_type 0)) m);
334e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let zero = const_int i32_type 0 in
335e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let one  = const_int i32_type 1 in
3366e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_extractelement" (const_extractelement
337e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen    (const_vector [| zero; one; zero; one |])
3384647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen    (const_trunc foldbomb i32_type)) m);
3396e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_insertelement" (const_insertelement
340e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen    (const_vector [| zero; one; zero; one |])
3414647569fe7706e76135a08ca0e5f90a447ccc5b4Gordon Henriksen    zero (const_trunc foldbomb i32_type)) m);
3426e2ee569c57338c193083f511cdc2c85622da20aErick Tryzelaar  ignore (define_global "const_shufflevector" (const_shufflevector
343e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen    (const_vector [| zero; one |])
344e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen    (const_vector [| one; zero |])
345e82e7700ea37a38e2e059c95420caec8dbc172c4Erick Tryzelaar    (const_vector [| const_int i32_type 0; const_int i32_type 1;
346e82e7700ea37a38e2e059c95420caec8dbc172c4Erick Tryzelaar                     const_int i32_type 2; const_int i32_type 3 |])) m);
347ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar
348ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar  group "asm"; begin
349ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar    let ft = function_type void_type [| i32_type; i32_type; i32_type |] in
350ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar    ignore (const_inline_asm
351ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar      ft
352ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar      ""
353ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar      "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}"
354ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar      true
355ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar      false)
3562c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin  end;
3572c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin
3582c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin  group "recursive struct"; begin
3592c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin      let nsty = named_struct_type context "rec" in
3602c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin      let pty = pointer_type nsty in
3612c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin      struct_set_body nsty [| i32_type; pty |] false;
3622c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin      let elts = [| const_int i32_type 4; const_pointer_null pty |] in
3632c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin      let grec_init = const_named_struct nsty elts in
3642c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin      ignore (define_global "grec" grec_init m);
3652c4ae181c4c3d93684bb5926cd73c6b3a8370c42Torok Edwin      ignore (string_of_lltype nsty);
366ce89b4e66f7ec82b2b621c94b072f0b22827a3cdErick Tryzelaar  end
367344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen
368344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen
3698ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen(*===-- Global Values -----------------------------------------------------===*)
3708ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
3718ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenlet test_global_values () =
3728ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  let (++) x f = f x; x in
373e3b989d4a4ba47f77d5d38c35ff17e9673d9f87bGordon Henriksen  let zero32 = const_null i32_type in
3748ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
3759f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: GVal01
3768ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen   *)
3778ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  group "naming";
3788ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  let g = define_global "TEMPORARY" zero32 m in
3798ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  insist ("TEMPORARY" = value_name g);
3808ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  set_value_name "GVal01" g;
3818ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  insist ("GVal01" = value_name g);
3828ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
3839f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: GVal02{{.*}}linkonce
3848ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen   *)
3858ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  group "linkage";
3868ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  let g = define_global "GVal02" zero32 m ++
387404a1942e43ca967700cc2608eb97b863add2677Gordon Henriksen          set_linkage Linkage.Link_once in
388404a1942e43ca967700cc2608eb97b863add2677Gordon Henriksen  insist (Linkage.Link_once = linkage g);
3898ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
3909f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: GVal03{{.*}}Hanalei
3918ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen   *)
3928ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  group "section";
3938ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  let g = define_global "GVal03" zero32 m ++
3948ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen          set_section "Hanalei" in
3958ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  insist ("Hanalei" = section g);
3968ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  
3979f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: GVal04{{.*}}hidden
3988ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen   *)
3998ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  group "visibility";
4008ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  let g = define_global "GVal04" zero32 m ++
401404a1942e43ca967700cc2608eb97b863add2677Gordon Henriksen          set_visibility Visibility.Hidden in
402404a1942e43ca967700cc2608eb97b863add2677Gordon Henriksen  insist (Visibility.Hidden = visibility g);
4038ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  
4049f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: GVal05{{.*}}align 128
4058ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen   *)
4068ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  group "alignment";
4078ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  let g = define_global "GVal05" zero32 m ++
4088ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen          set_alignment 128 in
4098ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  insist (128 = alignment g)
4108ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
4118ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
4128ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen(*===-- Global Variables --------------------------------------------------===*)
4138ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
4148ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksenlet test_global_variables () =
4158ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  let (++) x f = f x; x in
41636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  let forty_two32 = const_int i32_type 42 in
4178ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
4188e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar  group "declarations"; begin
419150c95eab549e1d764eab7ed05b17fe09663fc6ePeter Zotov    (* CHECK: @GVar01 = external global i32
420150c95eab549e1d764eab7ed05b17fe09663fc6ePeter Zotov     * CHECK: @QGVar01 = external addrspace(3) global i32
4218e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar     *)
4228e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (None == lookup_global "GVar01" m);
4238e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    let g = declare_global i32_type "GVar01" m in
4248e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (is_declaration g);
4258e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (pointer_type float_type ==
4268e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar              type_of (declare_global float_type "GVar01" m));
4278e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (g == declare_global i32_type "GVar01" m);
4288e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (match lookup_global "GVar01" m with Some x -> x = g
4298e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar                                              | None -> false);
4308e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar
4318e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (None == lookup_global "QGVar01" m);
4328e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    let g = declare_qualified_global i32_type "QGVar01" 3 m in
4338e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (is_declaration g);
4348e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (qualified_pointer_type float_type 3 ==
4358e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar              type_of (declare_qualified_global float_type "QGVar01" 3 m));
4368e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (g == declare_qualified_global i32_type "QGVar01" 3 m);
4378e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (match lookup_global "QGVar01" m with Some x -> x = g
4388e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar                                              | None -> false);
4398e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar  end;
4408ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  
4418e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar  group "definitions"; begin
442150c95eab549e1d764eab7ed05b17fe09663fc6ePeter Zotov    (* CHECK: @GVar02 = global i32 42
443150c95eab549e1d764eab7ed05b17fe09663fc6ePeter Zotov     * CHECK: @GVar03 = global i32 42
444150c95eab549e1d764eab7ed05b17fe09663fc6ePeter Zotov     * CHECK: @QGVar02 = addrspace(3) global i32 42
445150c95eab549e1d764eab7ed05b17fe09663fc6ePeter Zotov     * CHECK: @QGVar03 = addrspace(3) global i32 42
4468e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar     *)
44736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    let g = define_global "GVar02" forty_two32 m in
4488e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    let g2 = declare_global i32_type "GVar03" m ++
44936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines           set_initializer forty_two32 in
4508e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (not (is_declaration g));
4518e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (not (is_declaration g2));
4528e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist ((global_initializer g) == (global_initializer g2));
4538e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar
45436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    let g = define_qualified_global "QGVar02" forty_two32 3 m in
4558e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
45636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines           set_initializer forty_two32 in
4578e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (not (is_declaration g));
4588e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist (not (is_declaration g2));
4598e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar    insist ((global_initializer g) == (global_initializer g2));
4608e130b1d0d53346e05d0a1161a52c6d62de163c4Erick Tryzelaar  end;
4618ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
4629f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: GVar04{{.*}}thread_local
4638ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen   *)
4648ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  group "threadlocal";
46536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  let g = define_global "GVar04" forty_two32 m ++
4668ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen          set_thread_local true in
4678ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen  insist (is_thread_local g);
4688ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
4698a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov  (* CHECK: GVar05{{.*}}thread_local(initialexec)
4708a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov   *)
4718a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov  group "threadlocal_mode";
47236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  let g = define_global "GVar05" forty_two32 m ++
4738a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov          set_thread_local_mode ThreadLocalMode.InitialExec in
4748a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov  insist ((thread_local_mode g) = ThreadLocalMode.InitialExec);
4758a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov
4768a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov  (* CHECK: GVar06{{.*}}externally_initialized
4778a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov   *)
4788a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov  group "externally_initialized";
47936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  let g = define_global "GVar06" forty_two32 m ++
4808a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov          set_externally_initialized true in
4818a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov  insist (is_externally_initialized g);
4828a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov
4838a3bdd6a3fa8312809a432579a5685d431ca8410Peter Zotov  (* CHECK-NOWHERE-NOT: GVar07
4848ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen   *)
485344be5fbecec9908bab611eafeae0549ba3be6d7Gordon Henriksen  group "delete";
48636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  let g = define_global "GVar07" forty_two32 m in
487c84c16be9b29b4f805b92bfc2d93e2dfaa952f8fGordon Henriksen  delete_global g;
488c84c16be9b29b4f805b92bfc2d93e2dfaa952f8fGordon Henriksen
4899f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: ConstGlobalVar{{.*}}constant
490c84c16be9b29b4f805b92bfc2d93e2dfaa952f8fGordon Henriksen   *)
491c84c16be9b29b4f805b92bfc2d93e2dfaa952f8fGordon Henriksen  group "constant";
49236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  let g = define_global "ConstGlobalVar" forty_two32 m in
493c84c16be9b29b4f805b92bfc2d93e2dfaa952f8fGordon Henriksen  insist (not (is_global_constant g));
494c84c16be9b29b4f805b92bfc2d93e2dfaa952f8fGordon Henriksen  set_global_constant true g;
4954733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  insist (is_global_constant g);
4964733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  
4974733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  begin group "iteration";
4985371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar    let m = create_module context "temp" in
4994733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
5004733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_end m = global_begin m);
5014733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_start m = global_end m);
5024733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
5034733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let g1 = declare_global i32_type "One" m in
5044733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let g2 = declare_global i32_type "Two" m in
5054733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
5064733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (Before g1 = global_begin m);
5074733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (Before g2 = global_succ g1);
5084733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_end m = global_succ g2);
5094733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
5104733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (After g2 = global_end m);
5114733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (After g1 = global_pred g2);
5124733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_start m = global_pred g1);
5134733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
5144733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let lf s x = s ^ "->" ^ value_name x in
5154733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist ("->One->Two" = fold_left_globals lf "" m);
5164733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
5174733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let rf x s = value_name x ^ "<-" ^ s in
5184733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist ("One<-Two<-" = fold_right_globals rf m "");
5194733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
5204733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    dispose_module m
5214733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  end
5228ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
5239f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner(* String globals built below are emitted here.
5249f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner * CHECK: build_global_string{{.*}}stringval
5259f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner *)
5269f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner
527b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar
528705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar(*===-- Uses --------------------------------------------------------------===*)
529705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar
530705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaarlet test_uses () =
531705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let ty = function_type i32_type [| i32_type; i32_type |] in
532705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let fn = define_function "use_function" ty m in
533705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let b = builder_at_end context (entry_block fn) in
534705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar
535705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let p1 = param fn 0 in
536705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let p2 = param fn 1 in
537705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let v1 = build_add p1 p2 "v1" b in
538705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let v2 = build_add p1 v1 "v2" b in
539705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let _ = build_add v1 v2 "v3" b in
540705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar
541705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let lf s u = value_name (user u) ^ "->" ^ s in
542705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  insist ("v2->v3->" = fold_left_uses lf "" v1);
543705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let rf u s = value_name (user u) ^ "<-" ^ s in
544705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  insist ("v3<-v2<-" = fold_right_uses rf v1 "");
545705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar
546705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let lf s u = value_name (used_value u) ^ "->" ^ s in
547705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  insist ("v1->v1->" = fold_left_uses lf "" v1);
548705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar
549705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  let rf u s = value_name (used_value u) ^ "<-" ^ s in
550705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  insist ("v1<-v1<-" = fold_right_uses rf v1 "");
551705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar
552705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar  ignore (build_unreachable b)
553705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar
554705443ffd3f67018c1ec387014262566502a9ee3Erick Tryzelaar
555b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar(*===-- Users -------------------------------------------------------------===*)
556b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar
557b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaarlet test_users () =
558b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar  let ty = function_type i32_type [| i32_type; i32_type |] in
559b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar  let fn = define_function "user_function" ty m in
560b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar  let b = builder_at_end context (entry_block fn) in
561b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar
562b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar  let p1 = param fn 0 in
563b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar  let p2 = param fn 1 in
564f7af931930dfab74b8a72b195e53df34b4ea2480Erick Tryzelaar  let a3 = build_alloca i32_type "user_alloca" b in
565f7af931930dfab74b8a72b195e53df34b4ea2480Erick Tryzelaar  let p3 = build_load a3 "user_load" b in
566b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar  let i = build_add p1 p2 "sum" b in
567b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar
568f7af931930dfab74b8a72b195e53df34b4ea2480Erick Tryzelaar  insist ((num_operands i) = 2);
569b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar  insist ((operand i 0) = p1);
570b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar  insist ((operand i 1) = p2);
571b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar
572f7af931930dfab74b8a72b195e53df34b4ea2480Erick Tryzelaar  set_operand i 1 p3;
573f7af931930dfab74b8a72b195e53df34b4ea2480Erick Tryzelaar  insist ((operand i 1) != p2);
574f7af931930dfab74b8a72b195e53df34b4ea2480Erick Tryzelaar  insist ((operand i 1) = p3);
575f7af931930dfab74b8a72b195e53df34b4ea2480Erick Tryzelaar
576b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar  ignore (build_unreachable b)
577b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar
578b65c5f2ec347db8342cb7e42d072b0088b74fa73Erick Tryzelaar
5796ba5c56cf5b1609ec4af45f4901118865a8ed9b6Erick Tryzelaar(*===-- Aliases -----------------------------------------------------------===*)
5806ba5c56cf5b1609ec4af45f4901118865a8ed9b6Erick Tryzelaar
5816ba5c56cf5b1609ec4af45f4901118865a8ed9b6Erick Tryzelaarlet test_aliases () =
5829f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: @alias = alias i32* @aliasee
5836ba5c56cf5b1609ec4af45f4901118865a8ed9b6Erick Tryzelaar   *)
58436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  let forty_two32 = const_int i32_type 42 in
58536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  let v = define_global "aliasee" forty_two32 m in
5866ba5c56cf5b1609ec4af45f4901118865a8ed9b6Erick Tryzelaar  ignore (add_alias m (pointer_type i32_type) v "alias")
5876ba5c56cf5b1609ec4af45f4901118865a8ed9b6Erick Tryzelaar
5888ef426baa36639458f6777309db25c1768dc9c8aGordon Henriksen
58946abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen(*===-- Functions ---------------------------------------------------------===*)
59046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen
59146abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksenlet test_functions () =
592957f9fe1cce0957bcde4a1093da83e17aaec6764Gordon Henriksen  let ty = function_type i32_type [| i32_type; i64_type |] in
5936d6203dff3560a2cc3ac8ec620ac3b105b0c7cc7Gordon Henriksen  let ty2 = function_type i8_type [| i8_type; i64_type |] in
59446abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
5959f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: declare i32 @Fn1(i32, i64)
59646abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen   *)
597dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen  begin group "declare";
598dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    insist (None = lookup_function "Fn1" m);
599dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    let fn = declare_function "Fn1" ty m in
600dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    insist (pointer_type ty = type_of fn);
601dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    insist (is_declaration fn);
602dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    insist (0 = Array.length (basic_blocks fn));
603dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
604dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    insist (fn == declare_function "Fn1" ty m);
605dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    insist (None <> lookup_function "Fn1" m);
606dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    insist (match lookup_function "Fn1" m with Some x -> x = fn
607dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen                                             | None -> false);
608dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen    insist (m == global_parent fn)
609dc1ce7bdc6e32e7a4c4a110caa32834730183c1bGordon Henriksen  end;
61046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
6119f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK-NOWHERE-NOT: Fn2
61246abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen   *)
61346abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  group "delete";
61446abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let fn = declare_function "Fn2" ty m in
61546abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  delete_function fn;
61646abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
6179f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: define{{.*}}Fn3
61846abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen   *)
61946abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  group "define";
62046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let fn = define_function "Fn3" ty m in
62146abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (not (is_declaration fn));
62246abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (1 = Array.length (basic_blocks fn));
6235371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  ignore (build_unreachable (builder_at_end context (entry_block fn)));
62446abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
6259f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2
62646abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen   *)
62746abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  group "params";
62846abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let fn = define_function "Fn4" ty m in
62946abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let params = params fn in
63046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (2 = Array.length params);
63146abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (params.(0) = param fn 0);
63246abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (params.(1) = param fn 1);
63346abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (i32_type = type_of params.(0));
63446abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (i64_type = type_of params.(1));
63546abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  set_value_name "Param1" params.(0);
63646abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  set_value_name "Param2" params.(1);
6375371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  ignore (build_unreachable (builder_at_end context (entry_block fn)));
63846abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
6399f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: fastcc{{.*}}Fn5
64046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen   *)
64146abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  group "callconv";
64246abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let fn = define_function "Fn5" ty m in
6431475142b93a618da72ec471e5ad7c1510c2feb37Gordon Henriksen  insist (CallConv.c = function_call_conv fn);
6441475142b93a618da72ec471e5ad7c1510c2feb37Gordon Henriksen  set_function_call_conv CallConv.fast fn;
6451475142b93a618da72ec471e5ad7c1510c2feb37Gordon Henriksen  insist (CallConv.fast = function_call_conv fn);
6465371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  ignore (build_unreachable (builder_at_end context (entry_block fn)));
64780a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen  
6485eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  begin group "gc";
6499f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner    (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack
65080a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen     *)
65180a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen    let fn = define_function "Fn6" ty m in
6525eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    insist (None = gc fn);
6535eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    set_gc (Some "ocaml") fn;
6545eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    insist (Some "ocaml" = gc fn);
6555eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    set_gc None fn;
6565eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    insist (None = gc fn);
6575eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    set_gc (Some "shadowstack") fn;
6585371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar    ignore (build_unreachable (builder_at_end context (entry_block fn)));
6594733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  end;
6604733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  
6614733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  begin group "iteration";
6625371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar    let m = create_module context "temp" in
6634733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6644733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_end m = function_begin m);
6654733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_start m = function_end m);
6664733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6674733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let f1 = define_function "One" ty m in
6684733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let f2 = define_function "Two" ty m in
6694733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6704733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (Before f1 = function_begin m);
6714733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (Before f2 = function_succ f1);
6724733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_end m = function_succ f2);
6734733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6744733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (After f2 = function_end m);
6754733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (After f1 = function_pred f2);
6764733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_start m = function_pred f1);
6774733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6784733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let lf s x = s ^ "->" ^ value_name x in
6794733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist ("->One->Two" = fold_left_functions lf "" m);
6804733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6814733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let rf x s = value_name x ^ "<-" ^ s in
6824733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist ("One<-Two<-" = fold_right_functions rf m "");
6834733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6844733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    dispose_module m
6854733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  end
6864733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen
6874733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen
6884733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen(*===-- Params ------------------------------------------------------------===*)
6894733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen
6904733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksenlet test_params () =
6914733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  begin group "iteration";
6925371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar    let m = create_module context "temp" in
6934733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6944733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let vf = define_function "void" (function_type void_type [| |]) m in
6954733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6964733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_end vf = param_begin vf);
6974733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_start vf = param_end vf);
6984733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
6994733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let ty = function_type void_type [| i32_type; i32_type |] in
7004733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let f = define_function "f" ty m in
7014733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let p1 = param f 0 in
7024733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let p2 = param f 1 in
7034733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    set_value_name "One" p1;
7044733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    set_value_name "Two" p2;
705e149e9960ba0726f4b084763f7ef95afa12d9a88Duncan Sands    add_param_attr p1 Attribute.Sext;
706e149e9960ba0726f4b084763f7ef95afa12d9a88Duncan Sands    add_param_attr p2 Attribute.Noalias;
707e149e9960ba0726f4b084763f7ef95afa12d9a88Duncan Sands    remove_param_attr p2 Attribute.Noalias;
708e149e9960ba0726f4b084763f7ef95afa12d9a88Duncan Sands    add_function_attr f Attribute.Nounwind;
709e149e9960ba0726f4b084763f7ef95afa12d9a88Duncan Sands    add_function_attr f Attribute.Noreturn;
710e149e9960ba0726f4b084763f7ef95afa12d9a88Duncan Sands    remove_function_attr f Attribute.Noreturn;
711e149e9960ba0726f4b084763f7ef95afa12d9a88Duncan Sands
7124733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (Before p1 = param_begin f);
7134733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (Before p2 = param_succ p1);
7144733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_end f = param_succ p2);
7154733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
7164733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (After p2 = param_end f);
7174733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (After p1 = param_pred p2);
7184733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_start f = param_pred p1);
7194733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
7204733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let lf s x = s ^ "->" ^ value_name x in
7214733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist ("->One->Two" = fold_left_params lf "" f);
7224733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
7234733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let rf x s = value_name x ^ "<-" ^ s in
7244733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist ("One<-Two<-" = fold_right_params rf f "");
7254733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
7264733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    dispose_module m
72780a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen  end
72846abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen
72946abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen
73046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen(*===-- Basic Blocks ------------------------------------------------------===*)
73146abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen
73246abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksenlet test_basic_blocks () =
733957f9fe1cce0957bcde4a1093da83e17aaec6764Gordon Henriksen  let ty = function_type void_type [| |] in
73446abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
7359f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: Bb1
73646abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen   *)
73746abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  group "entry";
73846abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let fn = declare_function "X" ty m in
739b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaar  let bb = append_block context "Bb1" fn in
74046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (bb = entry_block fn);
7415371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  ignore (build_unreachable (builder_at_end context bb));
74246abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
7439f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK-NOWHERE-NOT: Bb2
74446abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen   *)
74546abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  group "delete";
74646abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let fn = declare_function "X2" ty m in
747b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaar  let bb = append_block context "Bb2" fn in
74846abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  delete_block bb;
74946abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
75046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  group "insert";
75146abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let fn = declare_function "X3" ty m in
752b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaar  let bbb = append_block context "b" fn in
753b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaar  let bba = insert_block context "a" bbb in
75446abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist ([| bba; bbb |] = basic_blocks fn);
7555371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  ignore (build_unreachable (builder_at_end context bba));
7565371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  ignore (build_unreachable (builder_at_end context bbb));
75746abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
7589f23fe0ea51d781c1af2eb069a5f03c262243374Reid Kleckner  (* CHECK: Bb3
75946abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen   *)
76046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  group "name/value";
76146abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let fn = define_function "X4" ty m in
76246abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let bb = entry_block fn in
7635371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  ignore (build_unreachable (builder_at_end context bb));
76446abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let bbv = value_of_block bb in
76546abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  set_value_name "Bb3" bbv;
76646abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist ("Bb3" = value_name bbv);
76746abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  
76846abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  group "casts";
76946abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let fn = define_function "X5" ty m in
77046abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  let bb = entry_block fn in
7715371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar  ignore (build_unreachable (builder_at_end context bb));
77246abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (bb = block_of_value (value_of_block bb));
77346abf91f7378fb7bb118d66fe6d69b5d3af1e9d5Gordon Henriksen  insist (value_is_block (value_of_block bb));
7744733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  insist (not (value_is_block (const_null i32_type)));
7754733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  
7764733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen  begin group "iteration";
7775371aa2a1c9a4eeecffdb9ab7b2175732e49475bErick Tryzelaar    let m = create_module context "temp" in
7784733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let f = declare_function "Temp" (function_type i32_type [| |]) m in
7794733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
7804733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_end f = block_begin f);
7814733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_start f = block_end f);
7824733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
783b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaar    let b1 = append_block context "One" f in
784b02b87882788e57e180c6b903a37ced0db1ce828Erick Tryzelaar    let b2 = append_block context "Two" f in
7854733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
7864733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (Before b1 = block_begin f);
7874733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (Before b2 = block_succ b1);
7884733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_end f = block_succ b2);
7894733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
7904733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (After b2 = block_end f);
7914733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (After b1 = block_pred b2);
7924733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    insist (At_start f = block_pred b1);
7934733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    
7944733be38930ae81716bba9ae75a8281bcb180634Gordon Henriksen    let lf s x = s ^ "->" ^ value_name (value_of_block x) in
795