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
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
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:
25 Or from your favourite OS's friendly packaging system. Tested with version
26 3.09.2, though other versions will probably work too.
30 ocamlc -o neon-testgen neon.cmo neon-testgen.ml
33 cd /path/to/gcc/testsuite/gcc.target/arm/neon
39 type c_type_flags = Pointer | Const
41 (* Open a test source file. *)
42 let open_test_file dir name =
44 open_out (dir ^ "/" ^ name ^ ".c")
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"
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 =
65 List.fold_left (fun arg_number -> fun (flags, ty) ->
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;
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
83 ((if List.mem Fixed_vector_reg features then
84 Printf.fprintf chan " register %s out_%s asm (\"d18\");\n"
86 else if List.mem Fixed_core_reg features then
87 Printf.fprintf chan " register %s out_%s asm (\"r0\");\n"
90 Printf.fprintf chan " %s out_%s;\n" return_ty return_ty);
93 (* The intrinsic does not return a value. *)
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))
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
107 if List.mem Const flags then
108 match const_valuator with
110 if List.mem Pointer flags then
111 Printf.fprintf chan "0"
113 Printf.fprintf chan "1"
114 | Some f -> Printf.fprintf chan "%s" (string_of_int (f arg_number))
116 Printf.fprintf chan "arg%d_%s" arg_number ty
118 let rec print_args arg_number tys =
121 | [ty] -> print_arg chan arg_number ty
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";
135 List.iter (fun regexp ->
137 "/* { dg-final { scan-assembler \"%s\" } } */\n" regexp)
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
146 let check_types tys =
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))
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
168 Requires_feature "FMA" -> "arm_neonv2"
169 | Requires_arch 8 -> "arm_v8_neon"
171 with Not_found -> "arm_neon"
173 (* Given an intrinsic shape, produce a regexp that will match
174 the right-hand sides of instructions generated by an intrinsic of
176 let rec analyze_shape shape =
177 let rec n_things n thing =
180 | n -> thing :: (n_things (n - 1) thing)
182 let rec analyze_shape_elt elt =
184 Dreg -> "\\[dD\\]\\[0-9\\]+"
185 | Qreg -> "\\[qQ\\]\\[0-9\\]+"
186 | Corereg -> "\\[rR\\]\\[0-9\\]+"
187 | Immed -> "#\\[0-9\\]+"
188 | VecArray (1, elt) ->
189 let elt_regexp = analyze_shape_elt elt in
190 "((\\\\\\{" ^ elt_regexp ^ "\\\\\\})|(" ^ elt_regexp ^ "))"
191 | VecArray (n, elt) ->
192 let elt_regexp = analyze_shape_elt elt in
193 let alt1 = elt_regexp ^ "-" ^ elt_regexp in
194 let alt2 = commas (fun x -> x) (n_things n elt_regexp) "" in
195 "\\\\\\{((" ^ alt1 ^ ")|(" ^ alt2 ^ "))\\\\\\}"
196 | (PtrTo elt | CstPtrTo elt) ->
197 "\\\\\\[" ^ (analyze_shape_elt elt) ^ "\\(:\\[0-9\\]+\\)?\\\\\\]"
198 | Element_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
199 | Element_of_qreg -> (analyze_shape_elt Qreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
200 | All_elements_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\\\\\]"
201 | Alternatives (elts) -> "(" ^ (String.concat "|" (List.map analyze_shape_elt elts)) ^ ")"
204 All (n, elt) -> commas analyze_shape_elt (n_things n elt) ""
205 | Long -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Dreg) ^
206 ", " ^ (analyze_shape_elt Dreg)
207 | Long_noreg elt -> (analyze_shape_elt elt) ^ ", " ^ (analyze_shape_elt elt)
208 | Wide -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
209 ", " ^ (analyze_shape_elt Dreg)
210 | Wide_noreg elt -> analyze_shape (Long_noreg elt)
211 | Narrow -> (analyze_shape_elt Dreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
212 ", " ^ (analyze_shape_elt Qreg)
213 | Use_operands elts -> commas analyze_shape_elt (Array.to_list elts) ""
215 analyze_shape (Use_operands [| Dreg; Dreg; Element_of_dreg |])
217 analyze_shape (Use_operands [| Qreg; Qreg; Element_of_dreg |])
218 | By_scalar _ -> assert false
220 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
222 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
224 let elt_regexp = analyze_shape_elt elt in
225 elt_regexp ^ ", " ^ elt_regexp
226 | Unary_scalar _ -> "FIXME Unary_scalar"
227 | Binary_imm elt -> analyze_shape (Use_operands [| elt; elt; Immed |])
228 | Narrow_imm -> analyze_shape (Use_operands [| Dreg; Qreg; Immed |])
229 | Long_imm -> analyze_shape (Use_operands [| Qreg; Dreg; Immed |])
231 (* Generate tests for one intrinsic. *)
232 let test_intrinsic dir opcode features shape name munge elt_ty =
233 (* Open the test source file. *)
234 let test_name = name ^ (string_of_elt elt_ty) in
235 let chan = open_test_file dir test_name in
236 (* Work out what argument and return types the intrinsic has. *)
237 let c_arity, new_elt_ty = munge shape elt_ty in
238 let c_types = check_types (strings_of_arity c_arity) in
239 (* Extract any constant valuator (a function specifying what constant
240 values are to be written into the intrinsic call) from the features
244 match (List.find (fun feature -> match feature with
245 Const_valuator _ -> true
246 | _ -> false) features) with
247 Const_valuator f -> Some f
249 with Not_found -> None
251 (* Work out what instruction name(s) to expect. *)
252 let insns = get_insn_names features name in
253 let no_suffix = (new_elt_ty = NoElts) in
255 if no_suffix then insns
256 else List.map (fun insn ->
257 let suffix = string_of_elt_dots new_elt_ty in
258 insn ^ "\\." ^ suffix) insns
260 (* Construct a regexp to match against the expected instruction name(s). *)
266 let rec calc_regexp insns cur_regexp =
269 | [insn] -> cur_regexp ^ "(" ^ insn ^ "))"
270 | insn::insns -> calc_regexp insns (cur_regexp ^ "(" ^ insn ^ ")|")
271 in calc_regexp insns "("
273 (* Construct regexps to match against the instructions that this
274 intrinsic expands to. Watch out for any writeback character and
275 comments after the instruction. *)
276 let regexps = List.map (fun regexp -> insn_regexp ^ "\\[ \t\\]+" ^ regexp ^
277 "!?\\(\\[ \t\\]+@\\[a-zA-Z0-9 \\]+\\)?\\n")
278 (analyze_all_shapes features shape analyze_shape)
280 let effective_target = effective_target features
282 (* Emit file and function prologues. *)
283 emit_prologue chan test_name effective_target;
284 (* Emit local variable declarations. *)
285 emit_automatics chan c_types features;
286 Printf.fprintf chan "\n";
287 (* Emit the call to the intrinsic. *)
288 emit_call chan const_valuator c_types name elt_ty;
289 (* Emit the function epilogue and the DejaGNU scan-assembler directives. *)
290 emit_epilogue chan features regexps;
291 (* Close the test file. *)
294 (* Generate tests for one element of the "ops" table. *)
295 let test_intrinsic_group dir (opcode, features, shape, name, munge, types) =
296 List.iter (test_intrinsic dir opcode features shape name munge) types
298 (* Program entry point. *)
300 let directory = if Array.length Sys.argv <> 1 then Sys.argv.(1) else "." in
301 List.iter (test_intrinsic_group directory) (reinterp @ ops)