1 (* Auto-generate ARM Neon intrinsics tests.
2 Copyright (C) 2006-2014 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 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"
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
=
64 List.fold_left
(fun arg_number
-> fun (flags
, ty
) ->
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
;
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
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
89 Printf.fprintf chan
"%s%s out_%s;\n" spaces return_ty return_ty
);
92 (* The intrinsic does not return a value. *)
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
))
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
106 if List.mem Const flags
then
107 match const_valuator
with
109 if List.mem Pointer flags
then
110 Printf.fprintf chan
"0"
112 Printf.fprintf chan
"1"
113 | Some f
-> Printf.fprintf chan
"%s" (string_of_int
(f arg_number
))
115 Printf.fprintf chan
"arg%d_%s" arg_number ty
117 let rec print_args arg_number tys
=
120 | [ty
] -> print_arg chan arg_number ty
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";
134 List.iter
(fun regexp
->
136 "/* { dg-final { scan-assembler \"%s\" } } */\n" regexp
)
141 Printf.fprintf chan
"/* { dg-final { cleanup-saved-temps } } */\n"
143 (* Check a list of C types to determine which ones are pointers and which
145 let check_types tys
=
148 let len = String.length ty
in
149 if len > 2 && String.get ty
(len - 2) = ' '
150 && String.get ty
(len - 1) = '
*'
151 then ([Pointer
], String.sub ty
0 (len - 2))
154 List.map
(fun (flags
, ty
) ->
155 if String.length ty
> 6 && String.sub ty
0 6 = "const "
156 then (Const
:: flags
, String.sub ty
6 ((String.length ty
) - 6))
157 else (flags
, ty
)) tys'
159 (* Work out what the effective target should be. *)
160 let effective_target features
=
162 match List.find
(fun feature
->
163 match feature
with Requires_feature _
-> true
164 | Requires_arch _
-> true
165 | Requires_FP_bit
1 -> true
168 Requires_feature
"FMA" -> "arm_neonv2"
169 | Requires_feature
"CRYPTO" -> "arm_crypto"
170 | Requires_arch
8 -> "arm_v8_neon"
171 | Requires_FP_bit
1 -> "arm_neon_fp16"
173 with Not_found
-> "arm_neon"
175 (* Work out what the testcase optimization level should be, default to -O0. *)
176 let compile_test_optim features
=
178 match List.find
(fun feature
->
179 match feature
with Compiler_optim _
-> true
182 Compiler_optim opt
-> opt
184 with Not_found
-> "-O0"
186 (* Given an intrinsic shape, produce a regexp that will match
187 the right-hand sides of instructions generated by an intrinsic of
189 let rec analyze_shape shape
=
190 let rec n_things n thing
=
193 | n
-> thing
:: (n_things (n
- 1) thing
)
195 let rec analyze_shape_elt elt
=
197 Dreg
-> "\\[dD\\]\\[0-9\\]+"
198 | Qreg
-> "\\[qQ\\]\\[0-9\\]+"
199 | Corereg
-> "\\[rR\\]\\[0-9\\]+"
200 | Immed
-> "#\\[0-9\\]+"
201 | VecArray
(1, elt
) ->
202 let elt_regexp = analyze_shape_elt elt
in
203 "((\\\\\\{" ^
elt_regexp ^
"\\\\\\})|(" ^
elt_regexp ^
"))"
204 | VecArray
(n
, elt
) ->
205 let elt_regexp = analyze_shape_elt elt
in
206 let alt1 = elt_regexp ^
"-" ^
elt_regexp in
207 let alt2 = commas
(fun x
-> x
) (n_things n
elt_regexp) "" in
208 "\\\\\\{((" ^
alt1 ^
")|(" ^
alt2 ^
"))\\\\\\}"
209 | (PtrTo elt
| CstPtrTo elt
) ->
210 "\\\\\\[" ^
(analyze_shape_elt elt
) ^
"\\(:\\[0-9\\]+\\)?\\\\\\]"
211 | Element_of_dreg
-> (analyze_shape_elt Dreg
) ^
"\\\\\\[\\[0-9\\]+\\\\\\]"
212 | Element_of_qreg
-> (analyze_shape_elt Qreg
) ^
"\\\\\\[\\[0-9\\]+\\\\\\]"
213 | All_elements_of_dreg
-> (analyze_shape_elt Dreg
) ^
"\\\\\\[\\\\\\]"
214 | Alternatives
(elts
) -> "(" ^
(String.concat
"|" (List.map
analyze_shape_elt elts
)) ^
")"
217 All
(n
, elt
) -> commas
analyze_shape_elt (n_things n elt
) ""
218 | Long
-> (analyze_shape_elt Qreg
) ^
", " ^
(analyze_shape_elt Dreg
) ^
219 ", " ^
(analyze_shape_elt Dreg
)
220 | Long_noreg elt
-> (analyze_shape_elt elt
) ^
", " ^
(analyze_shape_elt elt
)
221 | Wide
-> (analyze_shape_elt Qreg
) ^
", " ^
(analyze_shape_elt Qreg
) ^
222 ", " ^
(analyze_shape_elt Dreg
)
223 | Wide_noreg elt
-> analyze_shape (Long_noreg elt
)
224 | Narrow
-> (analyze_shape_elt Dreg
) ^
", " ^
(analyze_shape_elt Qreg
) ^
225 ", " ^
(analyze_shape_elt Qreg
)
226 | Use_operands elts
-> commas
analyze_shape_elt (Array.to_list elts
) ""
228 analyze_shape (Use_operands
[| Dreg
; Dreg
; Element_of_dreg
|])
230 analyze_shape (Use_operands
[| Qreg
; Qreg
; Element_of_dreg
|])
231 | By_scalar _
-> assert false
233 analyze_shape (Use_operands
[| Qreg
; Dreg
; Element_of_dreg
|])
235 analyze_shape (Use_operands
[| Qreg
; Dreg
; Element_of_dreg
|])
237 let elt_regexp = analyze_shape_elt elt
in
238 elt_regexp ^
", " ^
elt_regexp
239 | Unary_scalar _
-> "FIXME Unary_scalar"
240 | Binary_imm elt
-> analyze_shape (Use_operands
[| elt
; elt
; Immed
|])
241 | Narrow_imm
-> analyze_shape (Use_operands
[| Dreg
; Qreg
; Immed
|])
242 | Long_imm
-> analyze_shape (Use_operands
[| Qreg
; Dreg
; Immed
|])
244 (* Generate tests for one intrinsic. *)
245 let test_intrinsic dir opcode features shape name munge elt_ty
=
246 (* Open the test source file. *)
247 let test_name = name ^
(string_of_elt elt_ty
) in
248 let chan = open_test_file dir
test_name in
249 (* Work out what argument and return types the intrinsic has. *)
250 let c_arity, new_elt_ty
= munge shape elt_ty
in
251 let c_types = check_types (strings_of_arity
c_arity) in
252 (* Extract any constant valuator (a function specifying what constant
253 values are to be written into the intrinsic call) from the features
257 match (List.find
(fun feature
-> match feature
with
258 Const_valuator _
-> true
259 | _
-> false) features
) with
260 Const_valuator f
-> Some f
262 with Not_found
-> None
264 (* Work out what instruction name(s) to expect. *)
265 let insns = get_insn_names features name
in
266 let no_suffix = (new_elt_ty
= NoElts
) in
268 if no_suffix then insns
269 else List.map
(fun insn
->
270 let suffix = string_of_elt_dots new_elt_ty
in
271 insn ^
"\\." ^
suffix) insns
273 (* Construct a regexp to match against the expected instruction name(s). *)
279 let rec calc_regexp insns cur_regexp
=
282 | [insn
] -> cur_regexp ^
"(" ^ insn ^
"))"
283 | insn
::insns -> calc_regexp insns (cur_regexp ^
"(" ^ insn ^
")|")
284 in calc_regexp insns "("
286 (* Construct regexps to match against the instructions that this
287 intrinsic expands to. Watch out for any writeback character and
288 comments after the instruction. *)
289 let regexps = List.map
(fun regexp
-> insn_regexp ^
"\\[ \t\\]+" ^ regexp ^
290 "!?\\(\\[ \t\\]+@\\[a-zA-Z0-9 \\]+\\)?\\n")
291 (analyze_all_shapes features shape
analyze_shape)
293 let effective_target = effective_target features
in
294 let compile_test_optim = compile_test_optim features
296 (* Emit file and function prologues. *)
297 emit_prologue chan test_name effective_target compile_test_optim;
299 if (compare
compile_test_optim "-O0") <> 0 then
300 (* Emit variable declarations. *)
301 emit_variables chan c_types features
"";
303 Printf.fprintf
chan "void test_%s (void)\n{\n" test_name;
305 if compare
compile_test_optim "-O0" = 0 then
306 (* Emit variable declarations. *)
307 emit_variables chan c_types features
" ";
309 Printf.fprintf
chan "\n";
310 (* Emit the call to the intrinsic. *)
311 emit_call chan const_valuator c_types name elt_ty
;
312 (* Emit the function epilogue and the DejaGNU scan-assembler directives. *)
313 emit_epilogue chan features
regexps;
314 (* Close the test file. *)
317 (* Generate tests for one element of the "ops" table. *)
318 let test_intrinsic_group dir
(opcode
, features
, shape
, name
, munge
, types
) =
319 List.iter
(test_intrinsic dir opcode features shape name munge
) types
321 (* Program entry point. *)
323 let directory = if Array.length
Sys.argv
<> 1 then Sys.argv
.(1) else "." in
324 List.iter
(test_intrinsic_group directory) (reinterp
@ reinterpq
@ ops
)