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