comparison gcc/config/arm/neon-testgen.ml @ 0:a06113de4d67

first commit
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2009 14:47:48 +0900
parents
children 77e2b8dfacca
comparison
equal deleted inserted replaced
-1:000000000000 0:a06113de4d67
1 (* Auto-generate ARM Neon intrinsics tests.
2 Copyright (C) 2006, 2007 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 =
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 arm_neon_ok } */\n";
54 Printf.fprintf chan
55 "/* { dg-options \"-save-temps -O0 -mfpu=neon -mfloat-abi=softfp\" } */\n";
56 Printf.fprintf chan "\n#include \"arm_neon.h\"\n\n";
57 Printf.fprintf chan "void test_%s (void)\n{\n" test_name
58
59 (* Emit declarations of local variables that are going to be passed
60 to an intrinsic, together with one to take a returned value if needed. *)
61 let emit_automatics chan c_types =
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 ""
67 in
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 %sarg%d_%s;\n"
72 ty pointer_bit arg_number ty;
73 arg_number + 1)
74 0 (List.tl c_types))
75 in
76 match c_types with
77 (_, return_ty) :: tys ->
78 if return_ty <> "void" then
79 (* The intrinsic returns a value. *)
80 (Printf.fprintf chan " %s out_%s;\n" return_ty return_ty;
81 emit ())
82 else
83 (* The intrinsic does not return a value. *)
84 emit ()
85 | _ -> assert false
86
87 (* Emit code to call an intrinsic. *)
88 let emit_call chan const_valuator c_types name elt_ty =
89 (if snd (List.hd c_types) <> "void" then
90 Printf.fprintf chan " out_%s = " (snd (List.hd c_types))
91 else
92 Printf.fprintf chan " ");
93 Printf.fprintf chan "%s_%s (" (intrinsic_name name) (string_of_elt elt_ty);
94 let print_arg chan arg_number (flags, ty) =
95 (* If the argument is of const type, then directly write in the
96 constant now. *)
97 if List.mem Const flags then
98 match const_valuator with
99 None ->
100 if List.mem Pointer flags then
101 Printf.fprintf chan "0"
102 else
103 Printf.fprintf chan "1"
104 | Some f -> Printf.fprintf chan "%s" (string_of_int (f arg_number))
105 else
106 Printf.fprintf chan "arg%d_%s" arg_number ty
107 in
108 let rec print_args arg_number tys =
109 match tys with
110 [] -> ()
111 | [ty] -> print_arg chan arg_number ty
112 | ty::tys ->
113 print_arg chan arg_number ty;
114 Printf.fprintf chan ", ";
115 print_args (arg_number + 1) tys
116 in
117 print_args 0 (List.tl c_types);
118 Printf.fprintf chan ");\n"
119
120 (* Emit epilogue code to a test source file. *)
121 let emit_epilogue chan features regexps =
122 let no_op = List.exists (fun feature -> feature = No_op) features in
123 Printf.fprintf chan "}\n\n";
124 (if not no_op then
125 List.iter (fun regexp ->
126 Printf.fprintf chan
127 "/* { dg-final { scan-assembler \"%s\" } } */\n" regexp)
128 regexps
129 else
130 ()
131 );
132 Printf.fprintf chan "/* { dg-final { cleanup-saved-temps } } */\n"
133
134 (* Check a list of C types to determine which ones are pointers and which
135 ones are const. *)
136 let check_types tys =
137 let tys' =
138 List.map (fun ty ->
139 let len = String.length ty in
140 if len > 2 && String.get ty (len - 2) = ' '
141 && String.get ty (len - 1) = '*'
142 then ([Pointer], String.sub ty 0 (len - 2))
143 else ([], ty)) tys
144 in
145 List.map (fun (flags, ty) ->
146 if String.length ty > 6 && String.sub ty 0 6 = "const "
147 then (Const :: flags, String.sub ty 6 ((String.length ty) - 6))
148 else (flags, ty)) tys'
149
150 (* Given an intrinsic shape, produce a regexp that will match
151 the right-hand sides of instructions generated by an intrinsic of
152 that shape. *)
153 let rec analyze_shape shape =
154 let rec n_things n thing =
155 match n with
156 0 -> []
157 | n -> thing :: (n_things (n - 1) thing)
158 in
159 let rec analyze_shape_elt elt =
160 match elt with
161 Dreg -> "\\[dD\\]\\[0-9\\]+"
162 | Qreg -> "\\[qQ\\]\\[0-9\\]+"
163 | Corereg -> "\\[rR\\]\\[0-9\\]+"
164 | Immed -> "#\\[0-9\\]+"
165 | VecArray (1, elt) ->
166 let elt_regexp = analyze_shape_elt elt in
167 "((\\\\\\{" ^ elt_regexp ^ "\\\\\\})|(" ^ elt_regexp ^ "))"
168 | VecArray (n, elt) ->
169 let elt_regexp = analyze_shape_elt elt in
170 let alt1 = elt_regexp ^ "-" ^ elt_regexp in
171 let alt2 = commas (fun x -> x) (n_things n elt_regexp) "" in
172 "\\\\\\{((" ^ alt1 ^ ")|(" ^ alt2 ^ "))\\\\\\}"
173 | (PtrTo elt | CstPtrTo elt) ->
174 "\\\\\\[" ^ (analyze_shape_elt elt) ^ "\\\\\\]"
175 | Element_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
176 | Element_of_qreg -> (analyze_shape_elt Qreg) ^ "\\\\\\[\\[0-9\\]+\\\\\\]"
177 | All_elements_of_dreg -> (analyze_shape_elt Dreg) ^ "\\\\\\[\\\\\\]"
178 in
179 match shape with
180 All (n, elt) -> commas analyze_shape_elt (n_things n elt) ""
181 | Long -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Dreg) ^
182 ", " ^ (analyze_shape_elt Dreg)
183 | Long_noreg elt -> (analyze_shape_elt elt) ^ ", " ^ (analyze_shape_elt elt)
184 | Wide -> (analyze_shape_elt Qreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
185 ", " ^ (analyze_shape_elt Dreg)
186 | Wide_noreg elt -> analyze_shape (Long_noreg elt)
187 | Narrow -> (analyze_shape_elt Dreg) ^ ", " ^ (analyze_shape_elt Qreg) ^
188 ", " ^ (analyze_shape_elt Qreg)
189 | Use_operands elts -> commas analyze_shape_elt (Array.to_list elts) ""
190 | By_scalar Dreg ->
191 analyze_shape (Use_operands [| Dreg; Dreg; Element_of_dreg |])
192 | By_scalar Qreg ->
193 analyze_shape (Use_operands [| Qreg; Qreg; Element_of_dreg |])
194 | By_scalar _ -> assert false
195 | Wide_lane ->
196 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
197 | Wide_scalar ->
198 analyze_shape (Use_operands [| Qreg; Dreg; Element_of_dreg |])
199 | Pair_result elt ->
200 let elt_regexp = analyze_shape_elt elt in
201 elt_regexp ^ ", " ^ elt_regexp
202 | Unary_scalar _ -> "FIXME Unary_scalar"
203 | Binary_imm elt -> analyze_shape (Use_operands [| elt; elt; Immed |])
204 | Narrow_imm -> analyze_shape (Use_operands [| Dreg; Qreg; Immed |])
205 | Long_imm -> analyze_shape (Use_operands [| Qreg; Dreg; Immed |])
206
207 (* Generate tests for one intrinsic. *)
208 let test_intrinsic dir opcode features shape name munge elt_ty =
209 (* Open the test source file. *)
210 let test_name = name ^ (string_of_elt elt_ty) in
211 let chan = open_test_file dir test_name in
212 (* Work out what argument and return types the intrinsic has. *)
213 let c_arity, new_elt_ty = munge shape elt_ty in
214 let c_types = check_types (strings_of_arity c_arity) in
215 (* Extract any constant valuator (a function specifying what constant
216 values are to be written into the intrinsic call) from the features
217 list. *)
218 let const_valuator =
219 try
220 match (List.find (fun feature -> match feature with
221 Const_valuator _ -> true
222 | _ -> false) features) with
223 Const_valuator f -> Some f
224 | _ -> assert false
225 with Not_found -> None
226 in
227 (* Work out what instruction name(s) to expect. *)
228 let insns = get_insn_names features name in
229 let no_suffix = (new_elt_ty = NoElts) in
230 let insns =
231 if no_suffix then insns
232 else List.map (fun insn ->
233 let suffix = string_of_elt_dots new_elt_ty in
234 insn ^ "\\." ^ suffix) insns
235 in
236 (* Construct a regexp to match against the expected instruction name(s). *)
237 let insn_regexp =
238 match insns with
239 [] -> assert false
240 | [insn] -> insn
241 | _ ->
242 let rec calc_regexp insns cur_regexp =
243 match insns with
244 [] -> cur_regexp
245 | [insn] -> cur_regexp ^ "(" ^ insn ^ "))"
246 | insn::insns -> calc_regexp insns (cur_regexp ^ "(" ^ insn ^ ")|")
247 in calc_regexp insns "("
248 in
249 (* Construct regexps to match against the instructions that this
250 intrinsic expands to. Watch out for any writeback character and
251 comments after the instruction. *)
252 let regexps = List.map (fun regexp -> insn_regexp ^ "\\[ \t\\]+" ^ regexp ^
253 "!?\\(\\[ \t\\]+@\\[a-zA-Z0-9 \\]+\\)?\\n")
254 (analyze_all_shapes features shape analyze_shape)
255 in
256 (* Emit file and function prologues. *)
257 emit_prologue chan test_name;
258 (* Emit local variable declarations. *)
259 emit_automatics chan c_types;
260 Printf.fprintf chan "\n";
261 (* Emit the call to the intrinsic. *)
262 emit_call chan const_valuator c_types name elt_ty;
263 (* Emit the function epilogue and the DejaGNU scan-assembler directives. *)
264 emit_epilogue chan features regexps;
265 (* Close the test file. *)
266 close_out chan
267
268 (* Generate tests for one element of the "ops" table. *)
269 let test_intrinsic_group dir (opcode, features, shape, name, munge, types) =
270 List.iter (test_intrinsic dir opcode features shape name munge) types
271
272 (* Program entry point. *)
273 let _ =
274 let directory = if Array.length Sys.argv <> 1 then Sys.argv.(1) else "." in
275 List.iter (test_intrinsic_group directory) (reinterp @ ops)
276