1 (* Auto-generate ARM Neon intrinsics tests.
2 Copyright (C) 2006, 2007 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
=
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";
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
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
=
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 %sarg%d_%s;\n"
72 ty
pointer_bit arg_number ty
;
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
;
83 (* The intrinsic does not return a value. *)
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
))
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
97 if List.mem Const flags
then
98 match const_valuator
with
100 if List.mem Pointer flags
then
101 Printf.fprintf chan
"0"
103 Printf.fprintf chan
"1"
104 | Some f
-> Printf.fprintf chan
"%s" (string_of_int
(f arg_number
))
106 Printf.fprintf chan
"arg%d_%s" arg_number ty
108 let rec print_args arg_number tys
=
111 | [ty
] -> print_arg chan arg_number ty
113 print_arg chan arg_number ty
;
114 Printf.fprintf chan
", ";
115 print_args (arg_number
+ 1) tys
117 print_args 0 (List.tl c_types
);
118 Printf.fprintf chan
");\n"
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";
125 List.iter
(fun regexp
->
127 "/* { dg-final { scan-assembler \"%s\" } } */\n" regexp
)
132 Printf.fprintf chan
"/* { dg-final { cleanup-saved-temps } } */\n"
134 (* Check a list of C types to determine which ones are pointers and which
136 let check_types tys
=
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))
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'
150 (* Given an intrinsic shape, produce a regexp that will match
151 the right-hand sides of instructions generated by an intrinsic of
153 let rec analyze_shape shape
=
154 let rec n_things n thing
=
157 | n
-> thing
:: (n_things (n
- 1) thing
)
159 let rec analyze_shape_elt elt
=
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
) ^
"\\\\\\[\\\\\\]"
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
) ""
191 analyze_shape (Use_operands
[| Dreg
; Dreg
; Element_of_dreg
|])
193 analyze_shape (Use_operands
[| Qreg
; Qreg
; Element_of_dreg
|])
194 | By_scalar _
-> assert false
196 analyze_shape (Use_operands
[| Qreg
; Dreg
; Element_of_dreg
|])
198 analyze_shape (Use_operands
[| Qreg
; Dreg
; Element_of_dreg
|])
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
|])
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
220 match (List.find
(fun feature
-> match feature
with
221 Const_valuator _
-> true
222 | _
-> false) features
) with
223 Const_valuator f
-> Some f
225 with Not_found
-> None
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
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
236 (* Construct a regexp to match against the expected instruction name(s). *)
242 let rec calc_regexp insns cur_regexp
=
245 | [insn
] -> cur_regexp ^
"(" ^ insn ^
"))"
246 | insn
::insns -> calc_regexp insns (cur_regexp ^
"(" ^ insn ^
")|")
247 in calc_regexp insns "("
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)
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. *)
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
272 (* Program entry point. *)
274 let directory = if Array.length
Sys.argv
<> 1 then Sys.argv
.(1) else "." in
275 List.iter
(test_intrinsic_group directory) (reinterp
@ ops
)