File: itbl-test.c

package info (click to toggle)
binutils 2.31.1-11
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 307,644 kB
  • sloc: ansic: 1,161,122; asm: 638,494; cpp: 128,815; exp: 68,557; makefile: 55,816; sh: 22,360; yacc: 14,238; lisp: 13,272; perl: 2,111; ada: 1,681; lex: 1,652; pascal: 1,446; cs: 879; sed: 195; python: 154; xml: 95; awk: 25
file content (128 lines) | stat: -rw-r--r-- 3,467 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* itbl-test.c

   Copyright (C) 1997-2018 Free Software Foundation, Inc.

   This file is part of GAS, the GNU Assembler.

   GAS 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 3, or (at your option)
   any later version.

   GAS 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 GAS; see the file COPYING.  If not, write to the Free
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */

/* Stand-alone test for instruction specification table support.
   Run using "itbl-test <itbl> <asm.s>"
   where <itbl> is the name of the instruction table,
   and <asm.s> is the name of the assembler fie. */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "itbl-ops.h"

static int test_reg (e_processor processor, e_type type, char *name,
		     unsigned long val);

int 
main (int argc, char **argv)
{
  unsigned int insn;
  FILE *fas;
  int aline = 0;
  char s[81], *name;

  if (argc < 3)
    {
      printf ("usage: %s itbl asm.s\n", argv[0]);
      exit (0);
    }
  if (itbl_parse (argv[1]) != 0)
    {
      printf ("failed to parse itbl\n");
      exit (0);
    }

  fas = fopen (argv[2], "r");
  if (fas == 0)
    {
      printf ("failed to open asm file %s\n", argv[2]);
      exit (0);
    }
  while (fgets (s, 80, fas))
    {
      char *p;
      aline++;

      if (p = strchr (s, ';'), p)	/* strip comments */
	*p = 0;
      if (p = strchr (s, '#'), p)	/* strip comments */
	*p = 0;
      p = s + strlen (s) - 1;
      while (p >= s && (*p == ' ' || *p == '\t' || *p == '\n'))	/* strip trailing spaces */
	p--;
      *(p + 1) = 0;
      p = s;
      while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))	/* strip leading spaces */
	p++;
      if (!*p)
	continue;

      name = itbl_get_field (&p);
      insn = itbl_assemble (name, p);
      if (insn == 0)
	printf ("line %d: Invalid instruction (%s)\n", aline, s);
      else
	{
	  char buf[128];
	  printf ("line %d: insn(%s) = 0x%x)\n", aline, s, insn);
	  if (!itbl_disassemble (buf, insn))
	    printf ("line %d: Can't disassemble instruction "
		    "(0x%x)\n", aline, insn);
	  else
	    printf ("line %d: disasm(0x%x) = %s)\n", aline, insn, buf);
	}
    }

  test_reg (1, e_dreg, "d1", 1);
  test_reg (3, e_creg, "c2", 22);
  test_reg (3, e_dreg, "d3", 3);

  fclose (fas);
  return 0;
}

static int 
test_reg (e_processor processor, e_type type, char *name,
	  unsigned long val)
{
  char *n;
  unsigned long v;

  n = itbl_get_name (processor, type, val);
  if (!n || strcmp (n, name))
    printf ("Error - reg name not found for proessor=%d, type=%d, val=%d\n",
	    processor, type, val);
  else
    printf ("name=%s found for processor=%d, type=%d, val=%d\n",
	    n, processor, type, val);

  /* We require that names be unique among processors and types. */
  if (! itbl_get_reg_val (name, &v)
      || v != val)
    printf ("Error - reg val not found for processor=%d, type=%d, name=%s\n",
	    processor, type, name);
  else
    printf ("val=0x%x found for processor=%d, type=%d, name=%s\n",
	    v, processor, type, name);
  return 0;
}