remove unused files
[platform/upstream/gcc48.git] / gcc / config / arm / neon-testgen.ml
1 (* Auto-generate ARM Neon intrinsics tests.
2    Copyright (C) 2006-2013 Free Software Foundation, Inc.
3    Contributed by CodeSourcery.
4
5    This file is part of GCC.
6
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.
11
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.
16
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/>.
20
21    This is an O'Caml program.  The O'Caml compiler is available from:
22
23      http://caml.inria.fr/
24
25    Or from your favourite OS's friendly packaging system. Tested with version
26    3.09.2, though other versions will probably work too.
27
28    Compile with:
29      ocamlc -c neon.ml
30      ocamlc -o neon-testgen neon.cmo neon-testgen.ml
31
32    Run with:
33      cd /path/to/gcc/testsuite/gcc.target/arm/neon
34      /path/to/neon-testgen
35 *)
36
37 open Neon
38
39 type c_type_flags = Pointer | Const
40
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)
47
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
59
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 ""
68                         in
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))
76   in
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
96
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
117   in
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
126   in
127     print_args 0 (List.tl c_types);
128     Printf.fprintf chan ");\n"
129
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
140        ()
141     );
142     Printf.fprintf chan "/* { dg-final { cleanup-saved-temps } } */\n"
143
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
154   in
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'
159
160 (* Work out what the effective target should be.  *)
161 let effective_target features =
162   try
163     match List.find (fun feature ->
164                        match feature with Requires_feature _ -> true
165                                         | Requires_arch _ -> true
166                                         | _ -> false)
167                      features with
168       Requires_feature "FMA" -> "arm_neonv2"
169     | Requires_arch 8 -> "arm_v8_neon"
170     | _ -> assert false
171   with Not_found -> "arm_neon"
172
173 (* Given an intrinsic shape, produce a regexp that will match
174    the right-hand sides of instructions generated by an intrinsic of
175    that shape.  *)
176 let rec analyze_shape shape =
177   let rec n_things n thing =
178     match n with
179       0 -> []
180     | n -> thing :: (n_things (n - 1) thing)
181   in
182   let rec analyze_shape_elt elt =
183     match elt with
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)) ^ ")"
202   in
203     match shape with
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) ""
214     | By_scalar Dreg ->
215         analyze_shape (Use_operands [| Dreg; Dreg; Element_of_dreg |])
216     | By_scalar Qreg ->
217         analyze_shape (Use_operands [| Qreg; Qreg; Element_of_dreg |])
218     | By_scalar _ -> assert false
219     | Wide_lane ->
220         analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
221     | Wide_scalar ->
222         analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
223     | Pair_result elt ->
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 |])
230
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
241      list.  *)
242   let const_valuator =
243     try
244       match (List.find (fun feature -> match feature with
245                                          Const_valuator _ -> true
246                                        | _ -> false) features) with
247         Const_valuator f -> Some f
248       | _ -> assert false
249     with Not_found -> None
250   in
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
254   let insns =
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
259   in
260   (* Construct a regexp to match against the expected instruction name(s).  *)
261   let insn_regexp =
262     match insns with
263       [] -> assert false
264     | [insn] -> insn
265     | _ ->
266       let rec calc_regexp insns cur_regexp =
267         match insns with
268           [] -> cur_regexp
269         | [insn] -> cur_regexp ^ "(" ^ insn ^ "))"
270         | insn::insns -> calc_regexp insns (cur_regexp ^ "(" ^ insn ^ ")|")
271       in calc_regexp insns "("
272   in
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)
279   in
280   let effective_target = effective_target features
281   in
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.  *)
292     close_out chan
293
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
297
298 (* Program entry point.  *)
299 let _ =
300   let directory = if Array.length Sys.argv <> 1 then Sys.argv.(1) else "." in
301     List.iter (test_intrinsic_group directory) (reinterp @ ops)
302