Rebase.
[official-gcc.git] / gcc / config / arm / neon-testgen.ml
blobdf429f59e2792cd71f64f34f1c9ab1e01c5fe394
1 (* Auto-generate ARM Neon intrinsics tests.
2 Copyright (C) 2006-2014 Free Software Foundation, Inc.
3 Contributed by CodeSourcery.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 This is an O'Caml program. The O'Caml compiler is available from:
23 http://caml.inria.fr/
25 Or from your favourite OS's friendly packaging system. Tested with version
26 3.09.2, though other versions will probably work too.
28 Compile with:
29 ocamlc -c neon.ml
30 ocamlc -o neon-testgen neon.cmo neon-testgen.ml
32 Run with:
33 cd /path/to/gcc/testsuite/gcc.target/arm/neon
34 /path/to/neon-testgen
37 open Neon
39 type c_type_flags = Pointer | Const
41 (* Open a test source file. *)
42 let open_test_file dir name =
43 try
44 open_out (dir ^ "/" ^ name ^ ".c")
45 with Sys_error str ->
46 failwith ("Could not create test source file " ^ name ^ ": " ^ str)
48 (* Emit prologue code to a test source file. *)
49 let emit_prologue chan test_name effective_target =
50 Printf.fprintf chan "/* Test the `%s' ARM Neon intrinsic. */\n" test_name;
51 Printf.fprintf chan "/* This file was autogenerated by neon-testgen. */\n\n";
52 Printf.fprintf chan "/* { dg-do assemble } */\n";
53 Printf.fprintf chan "/* { dg-require-effective-target %s_ok } */\n"
54 effective_target;
55 Printf.fprintf chan "/* { dg-options \"-save-temps -O0\" } */\n";
56 Printf.fprintf chan "/* { dg-add-options %s } */\n" effective_target;
57 Printf.fprintf chan "\n#include \"arm_neon.h\"\n\n";
58 Printf.fprintf chan "void test_%s (void)\n{\n" test_name
60 (* Emit declarations of local variables that are going to be passed
61 to an intrinsic, together with one to take a returned value if needed. *)
62 let emit_automatics chan c_types features =
63 let emit () =
64 ignore (
65 List.fold_left (fun arg_number -> fun (flags, ty) ->
66 let pointer_bit =
67 if List.mem Pointer flags then "*" else ""
69 (* Const arguments to builtins are directly
70 written in as constants. *)
71 if not (List.mem Const flags) then
72 Printf.fprintf chan " %s %sarg%d_%s;\n"
73 ty pointer_bit arg_number ty;
74 arg_number + 1)
75 0 (List.tl c_types))
77 match c_types with
78 (_, return_ty) :: tys ->
79 if return_ty <> "void" then begin
80 (* The intrinsic returns a value. We need to do explict register
81 allocation for vget_low tests or they fail because of copy
82 elimination. *)
83 ((if List.mem Fixed_vector_reg features then
84 Printf.fprintf chan " register %s out_%s asm (\"d18\");\n"
85 return_ty return_ty
86 else if List.mem Fixed_core_reg features then
87 Printf.fprintf chan " register %s out_%s asm (\"r0\");\n"
88 return_ty return_ty
89 else
90 Printf.fprintf chan " %s out_%s;\n" return_ty return_ty);
91 emit ())
92 end else
93 (* The intrinsic does not return a value. *)
94 emit ()
95 | _ -> assert false
97 (* Emit code to call an intrinsic. *)
98 let emit_call chan const_valuator c_types name elt_ty =
99 (if snd (List.hd c_types) <> "void" then
100 Printf.fprintf chan " out_%s = " (snd (List.hd c_types))
101 else
102 Printf.fprintf chan " ");
103 Printf.fprintf chan "%s_%s (" (intrinsic_name name) (string_of_elt elt_ty);
104 let print_arg chan arg_number (flags, ty) =
105 (* If the argument is of const type, then directly write in the
106 constant now. *)
107 if List.mem Const flags then
108 match const_valuator with
109 None ->
110 if List.mem Pointer flags then
111 Printf.fprintf chan "0"
112 else
113 Printf.fprintf chan "1"
114 | Some f -> Printf.fprintf chan "%s" (string_of_int (f arg_number))
115 else
116 Printf.fprintf chan "arg%d_%s" arg_number ty
118 let rec print_args arg_number tys =
119 match tys with
120 [] -> ()
121 | [ty] -> print_arg chan arg_number ty
122 | ty::tys ->
123 print_arg chan arg_number ty;
124 Printf.fprintf chan ", ";
125 print_args (arg_number + 1) tys
127 print_args 0 (List.tl c_types);
128 Printf.fprintf chan ");\n"
130 (* Emit epilogue code to a test source file. *)
131 let emit_epilogue chan features regexps =
132 let no_op = List.exists (fun feature -> feature = No_op) features in
133 Printf.fprintf chan "}\n\n";
134 (if not no_op then
135 List.iter (fun regexp ->
136 Printf.fprintf chan
137 "/* { dg-final { scan-assembler \"%s\" } } */\n" regexp)
138 regexps
139 else
142 Printf.fprintf chan "/* { dg-final { cleanup-saved-temps } } */\n"
144 (* Check a list of C types to determine which ones are pointers and which
145 ones are const. *)
146 let check_types tys =
147 let tys' =
148 List.map (fun ty ->
149 let len = String.length ty in
150 if len > 2 && String.get ty (len - 2) = ' '
151 && String.get ty (len - 1) = '*'
152 then ([Pointer], String.sub ty 0 (len - 2))
153 else ([], ty)) tys
155 List.map (fun (flags, ty) ->
156 if String.length ty > 6 && String.sub ty 0 6 = "const "
157 then (Const :: flags, String.sub ty 6 ((String.length ty) - 6))
158 else (flags, ty)) tys'
160 (* Work out what the effective target should be. *)
161 let effective_target features =
163 match List.find (fun feature ->
164 match feature with Requires_feature _ -> true
165 | Requires_arch _ -> true
166 | Requires_FP_bit 1 -> true
167 | _ -> false)
168 features with
169 Requires_feature "FMA" -> "arm_neonv2"
170 | Requires_feature "CRYPTO" -> "arm_crypto"
171 | Requires_arch 8 -> "arm_v8_neon"
172 | Requires_FP_bit 1 -> "arm_neon_fp16"
173 | _ -> assert false
174 with Not_found -> "arm_neon"
176 (* Given an intrinsic shape, produce a regexp that will match
177 the right-hand sides of instructions generated by an intrinsic of
178 that shape. *)
179 let rec analyze_shape shape =
180 let rec n_things n thing =
181 match n with
182 0 -> []
183 | n -> thing :: (n_things (n - 1) thing)
185 let rec analyze_shape_elt elt =
186 match elt with
187 Dreg -> "\\[dD\\]\\[0-9\\]+"
188 | Qreg -> "\\[qQ\\]\\[0-9\\]+"
189 | Corereg -> "\\[rR\\]\\[0-9\\]+"
190 | Immed -> "#\\[0-9\\]+"
191 | VecArray (1, elt) ->
192 let elt_regexp = analyze_shape_elt elt in
193 "((\\\\\\{" ^ elt_regexp ^ "\\\\\\})|(" ^ elt_regexp ^ "))"
194 | VecArray (n, elt) ->
195 let elt_regexp = analyze_shape_elt elt in
196 let alt1 = elt_regexp ^ "-" ^ elt_regexp in
197 let alt2 = commas (fun x -> x) (n_things n elt_regexp) "" in
198 "\\\\\\{((" ^ alt1 ^ ")|(" ^ alt2 ^ "))\\\\\\}"
199 | (PtrTo elt | CstPtrTo elt) ->
200 "\\\\\\[" ^ (analyze_shape_elt elt) ^ "\\(:\\[0-9\\]+\\)?\\\\\\]"
201 | Element_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
202 | Element_of_qreg -> (analyze_shape_elt Qreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
203 | All_elements_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\\\\\]"
204 | Alternatives (elts) -> "(" ^ (String.concat "|" (List.map analyze_shape_elt elts)) ^ ")"
206 match shape with
207 All (n, elt) -> commas analyze_shape_elt (n_things n elt) ""
208 | Long -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Dreg) ^
209 ", " ^ (analyze_shape_elt Dreg)
210 | Long_noreg elt -> (analyze_shape_elt elt) ^ ", " ^ (analyze_shape_elt elt)
211 | Wide -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
212 ", " ^ (analyze_shape_elt Dreg)
213 | Wide_noreg elt -> analyze_shape (Long_noreg elt)
214 | Narrow -> (analyze_shape_elt Dreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
215 ", " ^ (analyze_shape_elt Qreg)
216 | Use_operands elts -> commas analyze_shape_elt (Array.to_list elts) ""
217 | By_scalar Dreg ->
218 analyze_shape (Use_operands [| Dreg; Dreg; Element_of_dreg |])
219 | By_scalar Qreg ->
220 analyze_shape (Use_operands [| Qreg; Qreg; Element_of_dreg |])
221 | By_scalar _ -> assert false
222 | Wide_lane ->
223 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
224 | Wide_scalar ->
225 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
226 | Pair_result elt ->
227 let elt_regexp = analyze_shape_elt elt in
228 elt_regexp ^ ", " ^ elt_regexp
229 | Unary_scalar _ -> "FIXME Unary_scalar"
230 | Binary_imm elt -> analyze_shape (Use_operands [| elt; elt; Immed |])
231 | Narrow_imm -> analyze_shape (Use_operands [| Dreg; Qreg; Immed |])
232 | Long_imm -> analyze_shape (Use_operands [| Qreg; Dreg; Immed |])
234 (* Generate tests for one intrinsic. *)
235 let test_intrinsic dir opcode features shape name munge elt_ty =
236 (* Open the test source file. *)
237 let test_name = name ^ (string_of_elt elt_ty) in
238 let chan = open_test_file dir test_name in
239 (* Work out what argument and return types the intrinsic has. *)
240 let c_arity, new_elt_ty = munge shape elt_ty in
241 let c_types = check_types (strings_of_arity c_arity) in
242 (* Extract any constant valuator (a function specifying what constant
243 values are to be written into the intrinsic call) from the features
244 list. *)
245 let const_valuator =
247 match (List.find (fun feature -> match feature with
248 Const_valuator _ -> true
249 | _ -> false) features) with
250 Const_valuator f -> Some f
251 | _ -> assert false
252 with Not_found -> None
254 (* Work out what instruction name(s) to expect. *)
255 let insns = get_insn_names features name in
256 let no_suffix = (new_elt_ty = NoElts) in
257 let insns =
258 if no_suffix then insns
259 else List.map (fun insn ->
260 let suffix = string_of_elt_dots new_elt_ty in
261 insn ^ "\\." ^ suffix) insns
263 (* Construct a regexp to match against the expected instruction name(s). *)
264 let insn_regexp =
265 match insns with
266 [] -> assert false
267 | [insn] -> insn
268 | _ ->
269 let rec calc_regexp insns cur_regexp =
270 match insns with
271 [] -> cur_regexp
272 | [insn] -> cur_regexp ^ "(" ^ insn ^ "))"
273 | insn::insns -> calc_regexp insns (cur_regexp ^ "(" ^ insn ^ ")|")
274 in calc_regexp insns "("
276 (* Construct regexps to match against the instructions that this
277 intrinsic expands to. Watch out for any writeback character and
278 comments after the instruction. *)
279 let regexps = List.map (fun regexp -> insn_regexp ^ "\\[ \t\\]+" ^ regexp ^
280 "!?\\(\\[ \t\\]+@\\[a-zA-Z0-9 \\]+\\)?\\n")
281 (analyze_all_shapes features shape analyze_shape)
283 let effective_target = effective_target features
285 (* Emit file and function prologues. *)
286 emit_prologue chan test_name effective_target;
287 (* Emit local variable declarations. *)
288 emit_automatics chan c_types features;
289 Printf.fprintf chan "\n";
290 (* Emit the call to the intrinsic. *)
291 emit_call chan const_valuator c_types name elt_ty;
292 (* Emit the function epilogue and the DejaGNU scan-assembler directives. *)
293 emit_epilogue chan features regexps;
294 (* Close the test file. *)
295 close_out chan
297 (* Generate tests for one element of the "ops" table. *)
298 let test_intrinsic_group dir (opcode, features, shape, name, munge, types) =
299 List.iter (test_intrinsic dir opcode features shape name munge) types
301 (* Program entry point. *)
302 let _ =
303 let directory = if Array.length Sys.argv <> 1 then Sys.argv.(1) else "." in
304 List.iter (test_intrinsic_group directory) (reinterp @ reinterpq @ ops)