Back to index

cell-binutils  2.17cvs20070401
itbl-test.c
Go to the documentation of this file.
00001 
00002 
00003 /* itbl-test.c
00004 
00005    Copyright (C) 1997  Free Software Foundation, Inc.
00006 
00007    This file is part of GAS, the GNU Assembler.
00008 
00009    GAS is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2, or (at your option)
00012    any later version.
00013 
00014    GAS is distributed in the hope that it will be useful, 
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with GAS; see the file COPYING.  If not, write to the Free
00021    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00022    02110-1301, USA.  */
00023 
00024 /* Stand-alone test for instruction specification table support.
00025    Run using "itbl-test <itbl> <asm.s>"
00026    where <itbl> is the name of the instruction table,
00027    and <asm.s> is the name of the assembler fie. */
00028 
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include "itbl-ops.h"
00034 
00035 static int test_reg (e_processor processor, e_type type, char *name,
00036                    unsigned long val);
00037 
00038 int 
00039 main (int argc, char **argv)
00040 {
00041   unsigned int insn;
00042   FILE *fas;
00043   int aline = 0;
00044   char s[81], *name;
00045 
00046   if (argc < 3)
00047     {
00048       printf ("usage: %s itbl asm.s\n", argv[0]);
00049       exit (0);
00050     }
00051   if (itbl_parse (argv[1]) != 0)
00052     {
00053       printf ("failed to parse itbl\n");
00054       exit (0);
00055     }
00056 
00057   fas = fopen (argv[2], "r");
00058   if (fas == 0)
00059     {
00060       printf ("failed to open asm file %s\n", argv[2]);
00061       exit (0);
00062     }
00063   while (fgets (s, 80, fas))
00064     {
00065       char *p;
00066       aline++;
00067 
00068       if (p = strchr (s, ';'), p)  /* strip comments */
00069        *p = 0;
00070       if (p = strchr (s, '#'), p)  /* strip comments */
00071        *p = 0;
00072       p = s + strlen (s) - 1;
00073       while (p >= s && (*p == ' ' || *p == '\t' || *p == '\n'))       /* strip trailing spaces */
00074        p--;
00075       *(p + 1) = 0;
00076       p = s;
00077       while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))    /* strip leading spaces */
00078        p++;
00079       if (!*p)
00080        continue;
00081 
00082       name = itbl_get_field (&p);
00083       insn = itbl_assemble (name, p);
00084       if (insn == 0)
00085        printf ("line %d: Invalid instruction (%s)\n", aline, s);
00086       else
00087        {
00088          char buf[128];
00089          printf ("line %d: insn(%s) = 0x%x)\n", aline, s, insn);
00090          if (!itbl_disassemble (buf, insn))
00091            printf ("line %d: Can't disassemble instruction "
00092                   "(0x%x)\n", aline, insn);
00093          else
00094            printf ("line %d: disasm(0x%x) = %s)\n", aline, insn, buf);
00095        }
00096     }
00097 
00098   test_reg (1, e_dreg, "d1", 1);
00099   test_reg (3, e_creg, "c2", 22);
00100   test_reg (3, e_dreg, "d3", 3);
00101 
00102   return 0;
00103 }
00104 
00105 static int 
00106 test_reg (e_processor processor, e_type type, char *name,
00107          unsigned long val)
00108 {
00109   char *n;
00110   unsigned long v;
00111 
00112   n = itbl_get_name (processor, type, val);
00113   if (!n || strcmp (n, name))
00114     printf ("Error - reg name not found for proessor=%d, type=%d, val=%d\n",
00115            processor, type, val);
00116   else
00117     printf ("name=%s found for processor=%d, type=%d, val=%d\n",
00118            n, processor, type, val);
00119 
00120   /* We require that names be unique amoung processors and types. */
00121   if (! itbl_get_reg_val (name, &v)
00122       || v != val)
00123     printf ("Error - reg val not found for processor=%d, type=%d, name=%s\n",
00124            processor, type, name);
00125   else
00126     printf ("val=0x%x found for processor=%d, type=%d, name=%s\n",
00127            v, processor, type, name);
00128   return 0;
00129 }