aboutsummaryrefslogtreecommitdiff
path: root/sim/v850/gencode.c
blob: 37f41b8b980acb9e029fb843689ba9f98efd0200 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "v850_sim.h"

static void write_header PARAMS ((void));
static void write_opcodes PARAMS ((void));
static void write_template PARAMS ((void));

int
main (argc, argv)
     int argc;
     char *argv[];
{
  if ((argc > 1) && (strcmp (argv[1],"-h") == 0))
    write_header();
  else if ((argc > 1) && (strcmp (argv[1],"-t") == 0))
    write_template ();
  else
    write_opcodes();
  return 0;
}


static void
write_header ()
{
  struct v850_opcode *opcode;

  for (opcode = (struct v850_opcode *)v850_opcodes; opcode->name; opcode++)
    printf("void OP_%X PARAMS ((void));\t\t/* %s */\n",opcode->opcode, opcode->name);
}


/* write_template creates a file all required functions, ready */
/* to be filled out */

static void
write_template ()
{
  struct v850_opcode *opcode;
  int i,j;

  printf ("#include \"v850_sim.h\"\n");
  printf ("#include \"simops.h\"\n");

  for (opcode = (struct v850_opcode *)v850_opcodes; opcode->name; opcode++)
    {
      printf("/* %s */\nvoid\nOP_%X ()\n{\n",opcode->name,opcode->opcode);
	  
      /* count operands */
      j = 0;
      for (i = 0; i < 6; i++)
	{
	  int flags = v850_operands[opcode->operands[i]].flags;

	  if (flags & (V850_OPERAND_REG | V850_OPERAND_SRG | V850_OPERAND_CC))
	    j++;
	}
      switch (j)
	{
	case 0:
	  printf ("printf(\"   %s\\n\");\n",opcode->name);
	  break;
	case 1:
	  printf ("printf(\"   %s\\t%%x\\n\",OP[0]);\n",opcode->name);
	  break;
	case 2:
	  printf ("printf(\"   %s\\t%%x,%%x\\n\",OP[0],OP[1]);\n",opcode->name);
	  break;
	case 3:
	  printf ("printf(\"   %s\\t%%x,%%x,%%x\\n\",OP[0],OP[1],OP[2]);\n",opcode->name);
	  break;
	default:
	  fprintf (stderr,"Too many operands: %d\n",j);
	}
      printf ("}\n\n");
    }
}


long Opcodes[512];
static int curop=0;

check_opcodes( long op)
{
  int i;

  for (i=0;i<curop;i++)
    if (Opcodes[i] == op)
      fprintf(stderr,"DUPLICATE OPCODES: %x\n",op);
}


static void
write_opcodes ()
{
  struct v850_opcode *opcode;
  int i, j;
  
  /* write out opcode table */
  printf ("#include \"v850_sim.h\"\n");
  printf ("#include \"simops.h\"\n\n");
  printf ("struct simops Simops[] = {\n");
  
  for (opcode = (struct v850_opcode *)v850_opcodes; opcode->name; opcode++)
    {
      printf ("  { %ld,%ld,OP_%X,",
	      opcode->opcode, opcode->mask, opcode->opcode);
      
      /* REMOVE ME */
      check_opcodes (opcode->opcode);
      Opcodes[curop++] = opcode->opcode;

      /* count operands */
      j = 0;
      for (i = 0; i < 6; i++)
	{
	  int flags = v850_operands[opcode->operands[i]].flags;

	  if (flags & (V850_OPERAND_REG | V850_OPERAND_SRG | V850_OPERAND_CC))
	    j++;
	}
      printf ("%d,{",j);
	  
      j = 0;
      for (i = 0; i < 6; i++)
	{
	  int flags = v850_operands[opcode->operands[i]].flags;
	  int shift = v850_operands[opcode->operands[i]].shift;

	  if (flags & (V850_OPERAND_REG | V850_OPERAND_SRG | V850_OPERAND_CC))
	    {
	      if (j)
		printf (", ");
#if 0
	      if ((flags & OPERAND_REG) && (opcode->format == LONG_L))
		shift += 15;
#endif
	      printf ("%d,%d,%d",shift,v850_operands[opcode->operands[i]].bits,flags);
	      j = 1;
	    }
	}
      printf ("}},\n");
    }
  printf ("{ 0,0,NULL,0,{ }},\n};\n");
}