Home | History | Annotate | Line # | Download | only in d10v
gencode.c revision 1.6
      1 #include "config.h"
      2 #include <stdio.h>
      3 #include <ctype.h>
      4 #include <limits.h>
      5 #include <string.h>
      6 #include "ansidecl.h"
      7 #include "opcode/d10v.h"
      8 
      9 static void write_header (void);
     10 static void write_opcodes (void);
     11 static void write_template (void);
     12 
     13 int
     14 main (int argc, char *argv[])
     15 {
     16   if ((argc > 1) && (strcmp (argv[1],"-h") == 0))
     17     write_header();
     18   else if ((argc > 1) && (strcmp (argv[1],"-t") == 0))
     19     write_template ();
     20   else
     21     write_opcodes();
     22   return 0;
     23 }
     24 
     25 
     26 static void
     27 write_header (void)
     28 {
     29   struct d10v_opcode *opcode;
     30 
     31   for (opcode = (struct d10v_opcode *)d10v_opcodes; opcode->name; opcode++)
     32     if (opcode->format != OPCODE_FAKE)
     33       printf ("void OP_%lX (SIM_DESC, SIM_CPU *);\t\t/* %s */\n", opcode->opcode, opcode->name);
     34 }
     35 
     36 
     37 /* write_template creates a file all required functions, ready */
     38 /* to be filled out */
     39 
     40 static void
     41 write_template (void)
     42 {
     43   struct d10v_opcode *opcode;
     44   int i,j;
     45 
     46   printf ("#include \"sim-main.h\"\n");
     47   printf ("#include \"simops.h\"\n");
     48 
     49   for (opcode = (struct d10v_opcode *)d10v_opcodes; opcode->name; opcode++)
     50     {
     51       if (opcode->format != OPCODE_FAKE)
     52 	{
     53 	  printf("/* %s */\nvoid\nOP_%lX ()\n{\n", opcode->name, opcode->opcode);
     54 
     55 	  /* count operands */
     56 	  j = 0;
     57 	  for (i=0;i<6;i++)
     58 	    {
     59 	      int flags = d10v_operands[opcode->operands[i]].flags;
     60 	      if ((flags & OPERAND_REG) || (flags & OPERAND_NUM) || (flags & OPERAND_ADDR))
     61 		j++;
     62 	    }
     63 	  switch (j)
     64 	    {
     65 	    case 0:
     66 	      printf ("printf(\"   %s\\n\");\n",opcode->name);
     67 	      break;
     68 	    case 1:
     69 	      printf ("printf(\"   %s\\t%%x\\n\",OP[0]);\n",opcode->name);
     70 	      break;
     71 	    case 2:
     72 	      printf ("printf(\"   %s\\t%%x,%%x\\n\",OP[0],OP[1]);\n",opcode->name);
     73 	      break;
     74 	    case 3:
     75 	      printf ("printf(\"   %s\\t%%x,%%x,%%x\\n\",OP[0],OP[1],OP[2]);\n",opcode->name);
     76 	      break;
     77 	    default:
     78 	      fprintf (stderr,"Too many operands: %d\n",j);
     79 	    }
     80 	  printf ("}\n\n");
     81 	}
     82     }
     83 }
     84 
     85 
     86 long Opcodes[512];
     87 static int curop=0;
     88 
     89 static void
     90 check_opcodes( long op)
     91 {
     92   int i;
     93 
     94   for (i=0;i<curop;i++)
     95     if (Opcodes[i] == op)
     96       fprintf(stderr,"DUPLICATE OPCODES: %lx\n", op);
     97 }
     98 
     99 static void
    100 write_opcodes (void)
    101 {
    102   struct d10v_opcode *opcode;
    103   int i, j;
    104 
    105   /* write out opcode table */
    106   printf ("#include \"sim-main.h\"\n");
    107   printf ("#include \"simops.h\"\n\n");
    108   printf ("struct simops Simops[] = {\n");
    109 
    110   for (opcode = (struct d10v_opcode *)d10v_opcodes; opcode->name; opcode++)
    111     {
    112       if (opcode->format != OPCODE_FAKE)
    113 	{
    114 	  printf ("  { %ld,%d,%ld,%d,%d,%d,%d,OP_%lX,", opcode->opcode,
    115 		  (opcode->format & LONG_OPCODE) ? 1 : 0, opcode->mask, opcode->format,
    116 		  opcode->cycles, opcode->unit, opcode->exec_type, opcode->opcode);
    117 
    118 	  /* REMOVE ME */
    119 	  check_opcodes (opcode->opcode);
    120 	  Opcodes[curop++] = opcode->opcode;
    121 
    122 	  j = 0;
    123 	  for (i=0;i<6;i++)
    124 	    {
    125 	      int flags = d10v_operands[opcode->operands[i]].flags;
    126 	      if ((flags & OPERAND_REG) || (flags & OPERAND_NUM) || (flags & OPERAND_ADDR))
    127 		j++;
    128 	    }
    129 	  printf ("%d,",j);
    130 
    131 	  j = 0;
    132 	  for (i=0;i<6;i++)
    133 	    {
    134 	      int flags = d10v_operands[opcode->operands[i]].flags;
    135 	      int shift = d10v_operands[opcode->operands[i]].shift;
    136 	      if ((flags & OPERAND_REG) || (flags & OPERAND_NUM)|| (flags & OPERAND_ADDR))
    137 		{
    138 		  if (j == 0)
    139 		    printf ("{");
    140 		  else
    141 		    printf (", ");
    142 		  if ((flags & OPERAND_REG) && (opcode->format == LONG_L))
    143 		    shift += 15;
    144 		  printf ("%d,%d,%d",shift,d10v_operands[opcode->operands[i]].bits,flags);
    145 		  j = 1;
    146 		}
    147 	    }
    148 	  if (j)
    149 	    printf ("}");
    150 	  printf ("},\n");
    151 	}
    152     }
    153   printf ("{ 0,0,0,0,0,0,0,(void (*)())0,0,{0,0,0}},\n};\n");
    154 }
    155