1 /* itbl-test.c
2 
3    Copyright (C) 1997-2021 Free Software Foundation, Inc.
4 
5    This file is part of GAS, the GNU Assembler.
6 
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11 
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21 
22 /* Stand-alone test for instruction specification table support.
23    Run using "itbl-test <itbl> <asm.s>"
24    where <itbl> is the name of the instruction table,
25    and <asm.s> is the name of the assembler fie. */
26 
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "itbl-ops.h"
32 
33 static int test_reg (e_processor processor, e_type type, char *name,
34 		     unsigned long val);
35 
36 int
main(int argc,char ** argv)37 main (int argc, char **argv)
38 {
39   unsigned int insn;
40   FILE *fas;
41   int aline = 0;
42   char s[81], *name;
43 
44   if (argc < 3)
45     {
46       printf ("usage: %s itbl asm.s\n", argv[0]);
47       exit (0);
48     }
49   if (itbl_parse (argv[1]) != 0)
50     {
51       printf ("failed to parse itbl\n");
52       exit (0);
53     }
54 
55   fas = fopen (argv[2], "r");
56   if (fas == 0)
57     {
58       printf ("failed to open asm file %s\n", argv[2]);
59       exit (0);
60     }
61   while (fgets (s, 80, fas))
62     {
63       char *p;
64       aline++;
65 
66       if (p = strchr (s, ';'), p)	/* strip comments */
67 	*p = 0;
68       if (p = strchr (s, '#'), p)	/* strip comments */
69 	*p = 0;
70       p = s + strlen (s) - 1;
71       while (p >= s && (*p == ' ' || *p == '\t' || *p == '\n'))	/* strip trailing spaces */
72 	p--;
73       *(p + 1) = 0;
74       p = s;
75       while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))	/* strip leading spaces */
76 	p++;
77       if (!*p)
78 	continue;
79 
80       name = itbl_get_field (&p);
81       insn = itbl_assemble (name, p);
82       if (insn == 0)
83 	printf ("line %d: Invalid instruction (%s)\n", aline, s);
84       else
85 	{
86 	  char buf[128];
87 	  printf ("line %d: insn(%s) = 0x%x)\n", aline, s, insn);
88 	  if (!itbl_disassemble (buf, insn))
89 	    printf ("line %d: Can't disassemble instruction "
90 		    "(0x%x)\n", aline, insn);
91 	  else
92 	    printf ("line %d: disasm(0x%x) = %s)\n", aline, insn, buf);
93 	}
94     }
95 
96   test_reg (1, e_dreg, "d1", 1);
97   test_reg (3, e_creg, "c2", 22);
98   test_reg (3, e_dreg, "d3", 3);
99 
100   fclose (fas);
101   return 0;
102 }
103 
104 static int
test_reg(e_processor processor,e_type type,char * name,unsigned long val)105 test_reg (e_processor processor, e_type type, char *name,
106 	  unsigned long val)
107 {
108   char *n;
109   unsigned long v;
110 
111   n = itbl_get_name (processor, type, val);
112   if (!n || strcmp (n, name))
113     printf ("Error - reg name not found for proessor=%d, type=%d, val=%d\n",
114 	    processor, type, val);
115   else
116     printf ("name=%s found for processor=%d, type=%d, val=%d\n",
117 	    n, processor, type, val);
118 
119   /* We require that names be unique among processors and types. */
120   if (! itbl_get_reg_val (name, &v)
121       || v != val)
122     printf ("Error - reg val not found for processor=%d, type=%d, name=%s\n",
123 	    processor, type, name);
124   else
125     printf ("val=0x%x found for processor=%d, type=%d, name=%s\n",
126 	    v, processor, type, name);
127   return 0;
128 }
129