2015-07-02 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / config / arm / neon-testgen.ml
blob3164ab74e245bde4f268eaadee6498784849186a
1 (* Auto-generate ARM Neon intrinsics tests.
2 Copyright (C) 2006-2015 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 compile_test_optim =
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 %s\" } */\n" compile_test_optim;
56 Printf.fprintf chan "/* { dg-add-options %s } */\n" effective_target;
57 Printf.fprintf chan "\n#include \"arm_neon.h\"\n\n"
59 (* Emit declarations of variables that are going to be passed
60 to an intrinsic, together with one to take a returned value if needed. *)
61 let emit_variables chan c_types features spaces =
62 let emit () =
63 ignore (
64 List.fold_left (fun arg_number -> fun (flags, ty) ->
65 let pointer_bit =
66 if List.mem Pointer flags then "*" else ""
68 (* Const arguments to builtins are directly
69 written in as constants. *)
70 if not (List.mem Const flags) then
71 Printf.fprintf chan "%s%s %sarg%d_%s;\n"
72 spaces ty pointer_bit arg_number ty;
73 arg_number + 1)
74 0 (List.tl c_types))
76 match c_types with
77 (_, return_ty) :: tys ->
78 if return_ty <> "void" then begin
79 (* The intrinsic returns a value. We need to do explict register
80 allocation for vget_low tests or they fail because of copy
81 elimination. *)
82 ((if List.mem Fixed_vector_reg features then
83 Printf.fprintf chan "%sregister %s out_%s asm (\"d18\");\n"
84 spaces return_ty return_ty
85 else if List.mem Fixed_core_reg features then
86 Printf.fprintf chan "%sregister %s out_%s asm (\"r0\");\n"
87 spaces return_ty return_ty
88 else
89 Printf.fprintf chan "%s%s out_%s;\n" spaces return_ty return_ty);
90 emit ())
91 end else
92 (* The intrinsic does not return a value. *)
93 emit ()
94 | _ -> assert false
96 (* Emit code to call an intrinsic. *)
97 let emit_call chan const_valuator c_types name elt_ty =
98 (if snd (List.hd c_types) <> "void" then
99 Printf.fprintf chan " out_%s = " (snd (List.hd c_types))
100 else
101 Printf.fprintf chan " ");
102 Printf.fprintf chan "%s_%s (" (intrinsic_name name) (string_of_elt elt_ty);
103 let print_arg chan arg_number (flags, ty) =
104 (* If the argument is of const type, then directly write in the
105 constant now. *)
106 if List.mem Const flags then
107 match const_valuator with
108 None ->
109 if List.mem Pointer flags then
110 Printf.fprintf chan "0"
111 else
112 Printf.fprintf chan "1"
113 | Some f -> Printf.fprintf chan "%s" (string_of_int (f arg_number))
114 else
115 Printf.fprintf chan "arg%d_%s" arg_number ty
117 let rec print_args arg_number tys =
118 match tys with
119 [] -> ()
120 | [ty] -> print_arg chan arg_number ty
121 | ty::tys ->
122 print_arg chan arg_number ty;
123 Printf.fprintf chan ", ";
124 print_args (arg_number + 1) tys
126 print_args 0 (List.tl c_types);
127 Printf.fprintf chan ");\n"
129 (* Emit epilogue code to a test source file. *)
130 let emit_epilogue chan features regexps =
131 let no_op = List.exists (fun feature -> feature = No_op) features in
132 Printf.fprintf chan "}\n\n";
133 (if not no_op then
134 List.iter (fun regexp ->
135 Printf.fprintf chan
136 "/* { dg-final { scan-assembler \"%s\" } } */\n" regexp)
137 regexps
138 else
142 (* Check a list of C types to determine which ones are pointers and which
143 ones are const. *)
144 let check_types tys =
145 let tys' =
146 List.map (fun ty ->
147 let len = String.length ty in
148 if len > 2 && String.get ty (len - 2) = ' '
149 && String.get ty (len - 1) = '*'
150 then ([Pointer], String.sub ty 0 (len - 2))
151 else ([], ty)) tys
153 List.map (fun (flags, ty) ->
154 if String.length ty > 6 && String.sub ty 0 6 = "const "
155 then (Const :: flags, String.sub ty 6 ((String.length ty) - 6))
156 else (flags, ty)) tys'
158 (* Work out what the effective target should be. *)
159 let effective_target features =
161 match List.find (fun feature ->
162 match feature with Requires_feature _ -> true
163 | Requires_arch _ -> true
164 | Requires_FP_bit 1 -> true
165 | _ -> false)
166 features with
167 Requires_feature "FMA" -> "arm_neonv2"
168 | Requires_feature "CRYPTO" -> "arm_crypto"
169 | Requires_arch 8 -> "arm_v8_neon"
170 | Requires_FP_bit 1 -> "arm_neon_fp16"
171 | _ -> assert false
172 with Not_found -> "arm_neon"
174 (* Work out what the testcase optimization level should be, default to -O0. *)
175 let compile_test_optim features =
177 match List.find (fun feature ->
178 match feature with Compiler_optim _ -> true
179 | _ -> false)
180 features with
181 Compiler_optim opt -> opt
182 | _ -> assert false
183 with Not_found -> "-O0"
185 (* Given an intrinsic shape, produce a regexp that will match
186 the right-hand sides of instructions generated by an intrinsic of
187 that shape. *)
188 let rec analyze_shape shape =
189 let rec n_things n thing =
190 match n with
191 0 -> []
192 | n -> thing :: (n_things (n - 1) thing)
194 let rec analyze_shape_elt elt =
195 match elt with
196 Dreg -> "\\[dD\\]\\[0-9\\]+"
197 | Qreg -> "\\[qQ\\]\\[0-9\\]+"
198 | Corereg -> "\\[rR\\]\\[0-9\\]+"
199 | Immed -> "#\\[0-9\\]+"
200 | VecArray (1, elt) ->
201 let elt_regexp = analyze_shape_elt elt in
202 "((\\\\\\{" ^ elt_regexp ^ "\\\\\\})|(" ^ elt_regexp ^ "))"
203 | VecArray (n, elt) ->
204 let elt_regexp = analyze_shape_elt elt in
205 let alt1 = elt_regexp ^ "-" ^ elt_regexp in
206 let alt2 = commas (fun x -> x) (n_things n elt_regexp) "" in
207 "\\\\\\{((" ^ alt1 ^ ")|(" ^ alt2 ^ "))\\\\\\}"
208 | (PtrTo elt | CstPtrTo elt) ->
209 "\\\\\\[" ^ (analyze_shape_elt elt) ^ "\\(:\\[0-9\\]+\\)?\\\\\\]"
210 | Element_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
211 | Element_of_qreg -> (analyze_shape_elt Qreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
212 | All_elements_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\\\\\]"
213 | Alternatives (elts) -> "(" ^ (String.concat "|" (List.map analyze_shape_elt elts)) ^ ")"
215 match shape with
216 All (n, elt) -> commas analyze_shape_elt (n_things n elt) ""
217 | Long -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Dreg) ^
218 ", " ^ (analyze_shape_elt Dreg)
219 | Long_noreg elt -> (analyze_shape_elt elt) ^ ", " ^ (analyze_shape_elt elt)
220 | Wide -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
221 ", " ^ (analyze_shape_elt Dreg)
222 | Wide_noreg elt -> analyze_shape (Long_noreg elt)
223 | Narrow -> (analyze_shape_elt Dreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
224 ", " ^ (analyze_shape_elt Qreg)
225 | Use_operands elts -> commas analyze_shape_elt (Array.to_list elts) ""
226 | By_scalar Dreg ->
227 analyze_shape (Use_operands [| Dreg; Dreg; Element_of_dreg |])
228 | By_scalar Qreg ->
229 analyze_shape (Use_operands [| Qreg; Qreg; Element_of_dreg |])
230 | By_scalar _ -> assert false
231 | Wide_lane ->
232 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
233 | Wide_scalar ->
234 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
235 | Pair_result elt ->
236 let elt_regexp = analyze_shape_elt elt in
237 elt_regexp ^ ", " ^ elt_regexp
238 | Unary_scalar _ -> "FIXME Unary_scalar"
239 | Binary_imm elt -> analyze_shape (Use_operands [| elt; elt; Immed |])
240 | Narrow_imm -> analyze_shape (Use_operands [| Dreg; Qreg; Immed |])
241 | Long_imm -> analyze_shape (Use_operands [| Qreg; Dreg; Immed |])
243 (* Generate tests for one intrinsic. *)
244 let test_intrinsic dir opcode features shape name munge elt_ty =
245 (* Open the test source file. *)
246 let test_name = name ^ (string_of_elt elt_ty) in
247 let chan = open_test_file dir test_name in
248 (* Work out what argument and return types the intrinsic has. *)
249 let c_arity, new_elt_ty = munge shape elt_ty in
250 let c_types = check_types (strings_of_arity c_arity) in
251 (* Extract any constant valuator (a function specifying what constant
252 values are to be written into the intrinsic call) from the features
253 list. *)
254 let const_valuator =
256 match (List.find (fun feature -> match feature with
257 Const_valuator _ -> true
258 | _ -> false) features) with
259 Const_valuator f -> Some f
260 | _ -> assert false
261 with Not_found -> None
263 (* Work out what instruction name(s) to expect. *)
264 let insns = get_insn_names features name in
265 let no_suffix = (new_elt_ty = NoElts) in
266 let insns =
267 if no_suffix then insns
268 else List.map (fun insn ->
269 let suffix = string_of_elt_dots new_elt_ty in
270 insn ^ "\\." ^ suffix) insns
272 (* Construct a regexp to match against the expected instruction name(s). *)
273 let insn_regexp =
274 match insns with
275 [] -> assert false
276 | [insn] -> insn
277 | _ ->
278 let rec calc_regexp insns cur_regexp =
279 match insns with
280 [] -> cur_regexp
281 | [insn] -> cur_regexp ^ "(" ^ insn ^ "))"
282 | insn::insns -> calc_regexp insns (cur_regexp ^ "(" ^ insn ^ ")|")
283 in calc_regexp insns "("
285 (* Construct regexps to match against the instructions that this
286 intrinsic expands to. Watch out for any writeback character and
287 comments after the instruction. *)
288 let regexps = List.map (fun regexp -> insn_regexp ^ "\\[ \t\\]+" ^ regexp ^
289 "!?\\(\\[ \t\\]+@\\[a-zA-Z0-9 \\]+\\)?\\n")
290 (analyze_all_shapes features shape analyze_shape)
292 let effective_target = effective_target features in
293 let compile_test_optim = compile_test_optim features
295 (* Emit file and function prologues. *)
296 emit_prologue chan test_name effective_target compile_test_optim;
298 if (compare compile_test_optim "-O0") <> 0 then
299 (* Emit variable declarations. *)
300 emit_variables chan c_types features "";
302 Printf.fprintf chan "void test_%s (void)\n{\n" test_name;
304 if compare compile_test_optim "-O0" = 0 then
305 (* Emit variable declarations. *)
306 emit_variables chan c_types features " ";
308 Printf.fprintf chan "\n";
309 (* Emit the call to the intrinsic. *)
310 emit_call chan const_valuator c_types name elt_ty;
311 (* Emit the function epilogue and the DejaGNU scan-assembler directives. *)
312 emit_epilogue chan features regexps;
313 (* Close the test file. *)
314 close_out chan
316 (* Generate tests for one element of the "ops" table. *)
317 let test_intrinsic_group dir (opcode, features, shape, name, munge, types) =
318 List.iter (test_intrinsic dir opcode features shape name munge) types
320 (* Program entry point. *)
321 let _ =
322 let directory = if Array.length Sys.argv <> 1 then Sys.argv.(1) else "." in
323 List.iter (test_intrinsic_group directory) (reinterp @ reinterpq @ ops)