PR other/53317
[official-gcc.git] / gcc / config / arm / neon-testgen.ml
blob543318bfcc6161b814000be507f9a79a7cd9cdbd
1 (* Auto-generate ARM Neon intrinsics tests.
2 Copyright (C) 2006-2013 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_arch 8 -> "arm_v8_neon"
171 | Requires_FP_bit 1 -> "arm_neon_fp16"
172 | _ -> assert false
173 with Not_found -> "arm_neon"
175 (* Given an intrinsic shape, produce a regexp that will match
176 the right-hand sides of instructions generated by an intrinsic of
177 that shape. *)
178 let rec analyze_shape shape =
179 let rec n_things n thing =
180 match n with
181 0 -> []
182 | n -> thing :: (n_things (n - 1) thing)
184 let rec analyze_shape_elt elt =
185 match elt with
186 Dreg -> "\\[dD\\]\\[0-9\\]+"
187 | Qreg -> "\\[qQ\\]\\[0-9\\]+"
188 | Corereg -> "\\[rR\\]\\[0-9\\]+"
189 | Immed -> "#\\[0-9\\]+"
190 | VecArray (1, elt) ->
191 let elt_regexp = analyze_shape_elt elt in
192 "((\\\\\\{" ^ elt_regexp ^ "\\\\\\})|(" ^ elt_regexp ^ "))"
193 | VecArray (n, elt) ->
194 let elt_regexp = analyze_shape_elt elt in
195 let alt1 = elt_regexp ^ "-" ^ elt_regexp in
196 let alt2 = commas (fun x -> x) (n_things n elt_regexp) "" in
197 "\\\\\\{((" ^ alt1 ^ ")|(" ^ alt2 ^ "))\\\\\\}"
198 | (PtrTo elt | CstPtrTo elt) ->
199 "\\\\\\[" ^ (analyze_shape_elt elt) ^ "\\(:\\[0-9\\]+\\)?\\\\\\]"
200 | Element_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
201 | Element_of_qreg -> (analyze_shape_elt Qreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
202 | All_elements_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\\\\\]"
203 | Alternatives (elts) -> "(" ^ (String.concat "|" (List.map analyze_shape_elt elts)) ^ ")"
205 match shape with
206 All (n, elt) -> commas analyze_shape_elt (n_things n elt) ""
207 | Long -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Dreg) ^
208 ", " ^ (analyze_shape_elt Dreg)
209 | Long_noreg elt -> (analyze_shape_elt elt) ^ ", " ^ (analyze_shape_elt elt)
210 | Wide -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
211 ", " ^ (analyze_shape_elt Dreg)
212 | Wide_noreg elt -> analyze_shape (Long_noreg elt)
213 | Narrow -> (analyze_shape_elt Dreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
214 ", " ^ (analyze_shape_elt Qreg)
215 | Use_operands elts -> commas analyze_shape_elt (Array.to_list elts) ""
216 | By_scalar Dreg ->
217 analyze_shape (Use_operands [| Dreg; Dreg; Element_of_dreg |])
218 | By_scalar Qreg ->
219 analyze_shape (Use_operands [| Qreg; Qreg; Element_of_dreg |])
220 | By_scalar _ -> assert false
221 | Wide_lane ->
222 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
223 | Wide_scalar ->
224 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
225 | Pair_result elt ->
226 let elt_regexp = analyze_shape_elt elt in
227 elt_regexp ^ ", " ^ elt_regexp
228 | Unary_scalar _ -> "FIXME Unary_scalar"
229 | Binary_imm elt -> analyze_shape (Use_operands [| elt; elt; Immed |])
230 | Narrow_imm -> analyze_shape (Use_operands [| Dreg; Qreg; Immed |])
231 | Long_imm -> analyze_shape (Use_operands [| Qreg; Dreg; Immed |])
233 (* Generate tests for one intrinsic. *)
234 let test_intrinsic dir opcode features shape name munge elt_ty =
235 (* Open the test source file. *)
236 let test_name = name ^ (string_of_elt elt_ty) in
237 let chan = open_test_file dir test_name in
238 (* Work out what argument and return types the intrinsic has. *)
239 let c_arity, new_elt_ty = munge shape elt_ty in
240 let c_types = check_types (strings_of_arity c_arity) in
241 (* Extract any constant valuator (a function specifying what constant
242 values are to be written into the intrinsic call) from the features
243 list. *)
244 let const_valuator =
246 match (List.find (fun feature -> match feature with
247 Const_valuator _ -> true
248 | _ -> false) features) with
249 Const_valuator f -> Some f
250 | _ -> assert false
251 with Not_found -> None
253 (* Work out what instruction name(s) to expect. *)
254 let insns = get_insn_names features name in
255 let no_suffix = (new_elt_ty = NoElts) in
256 let insns =
257 if no_suffix then insns
258 else List.map (fun insn ->
259 let suffix = string_of_elt_dots new_elt_ty in
260 insn ^ "\\." ^ suffix) insns
262 (* Construct a regexp to match against the expected instruction name(s). *)
263 let insn_regexp =
264 match insns with
265 [] -> assert false
266 | [insn] -> insn
267 | _ ->
268 let rec calc_regexp insns cur_regexp =
269 match insns with
270 [] -> cur_regexp
271 | [insn] -> cur_regexp ^ "(" ^ insn ^ "))"
272 | insn::insns -> calc_regexp insns (cur_regexp ^ "(" ^ insn ^ ")|")
273 in calc_regexp insns "("
275 (* Construct regexps to match against the instructions that this
276 intrinsic expands to. Watch out for any writeback character and
277 comments after the instruction. *)
278 let regexps = List.map (fun regexp -> insn_regexp ^ "\\[ \t\\]+" ^ regexp ^
279 "!?\\(\\[ \t\\]+@\\[a-zA-Z0-9 \\]+\\)?\\n")
280 (analyze_all_shapes features shape analyze_shape)
282 let effective_target = effective_target features
284 (* Emit file and function prologues. *)
285 emit_prologue chan test_name effective_target;
286 (* Emit local variable declarations. *)
287 emit_automatics chan c_types features;
288 Printf.fprintf chan "\n";
289 (* Emit the call to the intrinsic. *)
290 emit_call chan const_valuator c_types name elt_ty;
291 (* Emit the function epilogue and the DejaGNU scan-assembler directives. *)
292 emit_epilogue chan features regexps;
293 (* Close the test file. *)
294 close_out chan
296 (* Generate tests for one element of the "ops" table. *)
297 let test_intrinsic_group dir (opcode, features, shape, name, munge, types) =
298 List.iter (test_intrinsic dir opcode features shape name munge) types
300 (* Program entry point. *)
301 let _ =
302 let directory = if Array.length Sys.argv <> 1 then Sys.argv.(1) else "." in
303 List.iter (test_intrinsic_group directory) (reinterp @ ops)