📄 test-gen.c
字号:
#ifndef TEST_GEN_C#define TEST_GEN_C 1/* Copyright (C) 2000, 2003 Free Software Foundation Contributed by Alexandre Oliva <aoliva@cygnus.com> This file is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* This is a source file with infra-structure to test generators for assemblers and disassemblers. The strategy to generate testcases is as follows. We'll output to two streams: one will get the assembly source, and the other will get regexps that match the expected binary patterns. To generate each instruction, the functions of a func[] are called, each with the corresponding func_arg. Each function should set members of insn_data, to decide what it's going to output to the assembly source, the corresponding output for the disassembler tester, and the bits to be set in the instruction word. The strings to be output must have been allocated with strdup() or malloc(), so that they can be freed. A function may also modify insn_size. More details in test-gen.c Because this would have generated too many tests, we have chosen to define ``random'' sequences of numbers/registers, and simply generate each instruction a couple of times, which should get us enough coverage. In general, test generators should be compiled/run as follows: % gcc test.c -o test % ./test > test.s 2 > test.d Please note that this file contains a couple of GCC-isms, such as macro varargs (also available in C99, but with a difference syntax) and labeled elements in initializers (so that insn definitions are simpler and safer). It is assumed that the test generator #includes this file after defining any of the preprocessor macros documented below. The test generator is supposed to define instructions, at least one group of instructions, optionally, a sequence of groups. It should also define a main() function that outputs the initial lines of the assembler input and of the test control file, that also contains the disassembler output. The main() funcion may optionally set skip_list too, before calling output_groups() or output_insns(). *//* Define to 1 to avoid repeating instructions and to use a simpler register/constant generation mechanism. This makes it much easier to verify that the generated bit patterns are correct. */#ifndef SIMPLIFY_OUTPUT#define SIMPLIFY_OUTPUT 0#endif/* Define to 0 to avoid generating disassembler tests. */#ifndef DISASSEMBLER_TEST#define DISASSEMBLER_TEST 1#endif/* Define to the number of times to repeat the generation of each insn. It's best to use prime numbers, to improve randomization. */#ifndef INSN_REPEAT#define INSN_REPEAT 5#endif/* Define in order to get randomization_counter printed, as a comment, in the disassembler output, after each insn is emitted. */#ifndef OUTPUT_RANDOMIZATION_COUNTER#define OUTPUT_RANDOMIZATION_COUNTER 0#endif/* Other configuration macros are DEFINED_WORD and DEFINED_FUNC_ARG, see below. */#include <stdio.h>#include <string.h>#include <stdlib.h>/* It is expected that the main program defines the type `word' before includeing this. */#ifndef DEFINED_WORDtypedef unsigned long long word;#endif/* This struct is used as the output area for each function. It should store in as_in a pointer to the string to be output to the assembler; in dis_out, the string to be expected in return from the disassembler, and in bits the bits of the instruction word that are enabled by the assembly fragment. */typedef struct{ char * as_in; char * dis_out; word bits;} insn_data;#ifndef DEFINED_FUNC_ARG/* This is the struct that feeds information to each function. You're free to extend it, by `typedef'ing it before including this file, and defining DEFINED_FUNC_ARG. You may even reorder the fields, but do not remove any of the existing fields. */typedef struct{ int i1; int i2; int i3; void * p1; void * p2; word w;} func_arg;#endif/* This is the struct whose arrays define insns. Each func in the array will be called, in sequence, being given a pointer to the associated arg and a pointer to a zero-initialized output area, that it may fill in. */typedef struct{ int (* func) (func_arg *, insn_data *); func_arg arg;} func;/* Use this to group insns under a name. */typedef struct{ const char * name; func ** insns;} group_t;/* This is the size of each instruction. Use `insn_size_bits' instead of `insn_bits' in an insn defition to modify it. */int insn_size = 4;/* The offset of the next insn, as expected in the disassembler output. */int current_offset = 0;/* The offset and name of the last label to be emitted. */int last_label_offset = 0;const char * last_label_name = 0;/* This variable may be initialized in main() to `argv+1', if `argc>1', so that tests are emitted only for instructions that match exactly one of the given command-line arguments. If it is NULL, tests for all instructions are emitted. It must be a NULL-terminated array of pointers to strings (just like `argv+1'). */char ** skip_list = 0;/* This is a counter used to walk the various arrays of ``random'' operand generation. In simplified output mode, it is zeroed after each insn, otherwise it just keeps growing. */unsigned randomization_counter = 0;/* Use `define_insn' to create an array of funcs to define an insn, then `insn' to refer to that insn when defining an insn group. */#define define_insn(insname, funcs...) \ func i_ ## insname[] = { funcs, { 0 } }#define insn(insname) (i_ ## insname)/* Use these to output a comma followed by an optional space, a single space, a plus sign, left and right square brackets and parentheses, all of them properly quoted. */#define comma literal_q (", ", ", ?")#define space literal (" ")#define tab literal ("\t")#define plus literal_q ("+", "\\+")#define lsqbkt literal_q ("[", "\\[")#define rsqbkt literal_q ("]", "\\]")#define lparen literal_q ("(", "\\(")#define rparen literal_q (")", "\\)")/* Use this as a placeholder when you define a macro that expects an argument, but you don't have anything to output there. */intnothing (func_arg *arg, insn_data *data)#define nothing { nothing }{ return 0;}/* This is to be used in the argument list of define_insn, causing a string to be copied into both the assembly and the expected disassembler output. It is assumed not to modify the binary encoding of the insn. */intliteral (func_arg *arg, insn_data *data)#define literal(s) { literal, { p1: (s) } }{ data->as_in = data->dis_out = strdup ((char *) arg->p1); return 0;}/* The characters `[', `]', `\\' and `^' must be quoted in the disassembler-output matcher. If a literal string contains any of these characters, use literal_q instead of literal, and specify the unquoted version (for as input) as the first argument, and the quoted version (for expected disassembler output) as the second one. */intliteral_q (func_arg *arg, insn_data *data)#define literal_q(s,q) { literal_q, { p1: (s), p2: (q) } }{ data->as_in = strdup ((char *) arg->p1); data->dis_out = strdup ((char *) arg->p2); return 0;}/* Given an insn name, check whether it should be skipped or not, depending on skip_list. Return non-zero if the insn is to be skipped. */intskip_insn (char *name){ char **test; if (! skip_list) return 0; for (test = skip_list; * test; ++ test) if (strcmp (name, * test) == 0) return 0; return 1;}/* Use this to emit the actual insn name, with its opcode, in architectures with fixed-length instructions. */intinsn_bits (func_arg *arg, insn_data *data)#define insn_bits(name,bits) \ { insn_bits, { p1: # name, w: bits } }{ if (skip_insn ((char *) arg->p1)) return 1; data->as_in = data->dis_out = strdup ((char *) arg->p1); data->bits = arg->w; return 0;}/* Use this to emit the insn name and its opcode in architectures without a variable instruction length. */ intinsn_size_bits (func_arg *arg, insn_data *data)#define insn_size_bits(name,size,bits) \ { insn_size_bits, { p1: # name, i1: size, w: bits } }{ if (skip_insn ((char *) arg->p1)) return 1; data->as_in = data->dis_out = strdup ((char *) arg->p1); data->bits = arg->w; insn_size = arg->i1; return 0;}/* Use this to advance the random generator by one, in case it is generating repetitive patterns. It is usually good to arrange that each insn consumes a prime number of ``random'' numbers, or, at least, that it does not consume an exact power of two ``random'' numbers. */inttick_random (func_arg *arg, insn_data *data)#define tick_random { tick_random }{ ++ randomization_counter; return 0;}/* Select the next ``random'' number from the array V of size S, and advance the counter. */#define get_bits_from_size(V,S) \ ((V)[randomization_counter ++ % (S)])/* Utility macros. `_get_bits_var', used in some macros below, assume the names of the arrays used to define the ``random'' orders start with `random_order_'. */#define _get_bits_var(N) (random_order_ ## N)#define _get_bits_size(V) (sizeof (V) / sizeof * (V))/* Use this within a `func_arg' to select one of the arrays below (or any other array that starts with random_order_N. */#define mk_get_bits(N) \ p2: _get_bits_var (N), i3: _get_bits_size (_get_bits_var (N))/* Simplified versions of get_bits_from_size for when you have access to the array, so that its size can be implicitly calculated. */#define get_bits_from(V) get_bits_from_size ((V),_get_bits_size ((V)))#define get_bits(N) get_bits_from (_get_bits_var (N))/* Use `2u' to generate 2-bit unsigned values. Good for selecting registers randomly from a set of 4 registers. */unsigned random_order_2u[] = { /* This sequence was generated by hand so that no digit appers more than once in any horizontal or vertical line. */ 0, 1, 3, 2, 2, 0, 1, 3, 1, 3, 2, 0, 3, 2, 0, 1 };/* Use `3u' to generate 3-bit unsigned values. Good for selecting registers randomly from a set of 8 registers. */unsigned random_order_3u[] = { /* This sequence was generated by: f(k) = 3k mod 8 except that the middle pairs were swapped. */ 0, 6, 3, 1, 4, 2, 7, 5, /* This sequence was generated by: f(k) = 5k mod 8 except that the middle pairs were swapped. */ 0, 2, 5, 7, 4, 6, 1, 3, };/* Use `4u' to generate 4-bit unsigned values. Good for selecting registers randomly from a set of 16 registers. */unsigned random_order_4u[] = { /* This sequence was generated by: f(k) = 5k mod 16 except that the middle pairs were swapped. */ 0, 5, 15, 10, 9, 4, 14, 3, 8, 13, 7, 2, 1, 12, 6, 11, /* This sequence was generated by: f(k) = 7k mod 16 except that the middle pairs were swapped. */ 0, 7, 5, 14, 3, 12, 10, 1, 8, 15, 13, 6, 11, 4, 2, 9, };/* Use `5u' to generate 5-bit unsigned values. Good for selecting registers randomly from a set of 32 registers. */unsigned random_order_5u[] = { /* This sequence was generated by: f(k) = (13k) mod 32 except that the middle pairs were swapped. */ 0, 26, 13, 7, 20, 14, 1, 27, 8, 2, 21, 15, 28, 22, 9, 3, 16, 10, 29, 23, 4, 30, 17, 11, 24, 18, 5, 31, 12, 6, 25, 19 };/* Use `7s' to generate 7-bit signed values. Good for selecting ``interesting'' constants from -64 to +63. */int random_order_7s[] = { /* Sequence generated by hand, to explore limit values and a few intermediate values selected by chance. Keep the number of intermediate values low, to ensure that the limit values are generated often enough. */ 0, -1, -64, 63, -32, 32, 24, -20, 9, -27, -31, 33, 40, -2, -5, 1 };/* Use `8s' to generate 8-bit signed values. Good for selecting
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -