RISC-V: Support SiFive extensions: xsfvqmaccdod, xsfvqmaccqoq and xsfvfnrclipxfqf
[binutils-gdb.git] / sim / h8300 / writecode.c
blobea5c8ac340517e5a01830f23bc6d2c6914e948a8
1 /* Code generator for the Hitachi H8/300 architecture simulator.
3 Written by Steve Chamberlain of Cygnus Support.
4 sac@cygnus.com
6 This file is part of H8/300 sim
9 THIS SOFTWARE IS NOT COPYRIGHTED
11 Cygnus offers the following for use in the public domain. Cygnus
12 makes no warranty with regard to the software or it's performance
13 and the user accepts the software "AS IS" with all faults.
15 CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
16 THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 /* This program reads the H8/300 opcode table and writes out
22 a large switch statement to understand the opcodes (with ifs if
23 there is more than one opcode per case) and code to do the stuff */
25 #include <stdio.h>
27 #define DEFINE_TABLE
28 #define INSIM
29 #include"opcode/h8300.h"
31 #define MAXSAME 140
33 #define PTWO 256
34 static struct h8_opcode *h8_opcodes_sorted[PTWO][MAXSAME];
36 char *cs = "/*";
37 char *ce = "*/";
39 /* How to get at nibble n from the instruction */
40 char *nibs[] =
42 "foo",
43 "(b0&0xf)",
44 "((b1>>4)&0xf)",
45 "((b1)&0xf)",
46 "((pc[1]>>12)&0xf)",
47 "((pc[1]>>8)&0xf)",
48 "((pc[1]>>4)&0xf)",
49 "((pc[1])&0xf)",
50 0, 0};
52 /* how to get at the 3 bit immediate in the instruction */
53 char *imm3[] =
54 {"foo",
55 "foo",
56 "((b1>>4)&0x7)",
57 "foo",
58 "foo",
59 "foo",
60 "(pc[1]>>4)&0x7"};
62 /* How to get at a byte register from an index in the instruction at
63 nibble n */
64 char *breg[] =
65 {"foo",
66 "*(blow[b0])",
67 "*(bhigh[b1])",
68 "*(blow[b1])",
69 0, 0,
70 "*(bhigh[pc[1]>>8])"};
72 /* How to get at a word register from an index in the instruction at
73 nibble n */
75 char *wreg[] =
76 {"foo",
77 "*(wlow[b0])",
78 "*(whigh[b1])",
79 "*(wlow[b1])"};
81 #define sorted_key noperands
83 /* sort the opcode table into h8_opcodes_sorted[0..255] */
84 static void
85 init ()
87 unsigned int i;
88 struct h8_opcode *p;
90 for (p = h8_opcodes; p->name; p++)
92 int n1 = 0;
93 int n2 = 0;
94 int j;
95 #if 0
96 for (j = 0; p->data.nib[j] != E; j++)
98 if ((int) p->data.nib[j] == ABS16ORREL8SRC)
99 p->data.nib[j] = ABS16SRC;
100 if ((int) p->data.nib[j] == ABS16OR8SRC)
101 p->data.nib[j] = ABS16SRC;
102 if ((int) p->data.nib[j] == ABS16OR8DST)
103 p->data.nib[j] = ABS16DST;
105 #endif
107 if ((int) p->data.nib[0] < 16)
109 n1 = (int) p->data.nib[0];
111 else
112 n1 = 0;
113 if ((int) p->data.nib[1] < 16)
115 n2 = (int) p->data.nib[1];
117 else
118 n2 = 0;
119 for (i = 0; i < MAXSAME; i++)
121 int j = /* ((n3 >> 3) * 512) + ((n4 >> 3) * 256) + */ n1 * 16 + n2;
123 if (h8_opcodes_sorted[j][i] == (struct h8_opcode *) NULL)
125 h8_opcodes_sorted[j][i] = p;
126 p->sorted_key = j;
127 break;
131 if (i == MAXSAME)
132 abort ();
134 /* Just make sure there are an even number of nibbles in it, and
135 that the count is the same s the length */
136 for (i = 0; p->data.nib[i] != E; i++)
137 /*EMPTY*/ ;
138 if (i & 1)
139 abort ();
140 p->length = i / 2;
142 for (i = 0; i < PTWO; i++)
144 if (h8_opcodes_sorted[i][0])
145 p = h8_opcodes_sorted[i][0];
146 else
147 h8_opcodes_sorted[i][0] = p;
151 /* decode the lvalues, creating a pointer in real space to object -
152 remember if the thing has to be swapped out of where it is */
155 int swap[2];
157 lval (p)
158 struct h8_opcode *p;
160 int i;
162 for (i = 0; p->data.nib[i] != E; i++)
164 int x = p->data.nib[i];
165 int size;
166 int op;
167 op = (x & DST) ? 1 : 0;
169 switch (x & SIZE)
171 case L_32:
172 size = 32;
173 break;
174 case L_16:
175 size = 16;
176 break;
177 case L_8:
178 size = 8;
179 break;
180 default:
181 size = 1234;
184 if (x & REG)
186 printf ("ir%d = GET_LVAL_%d_REG(%d);\n", op, size, i);
188 else if (x & IMM)
190 printf ("/* Imm has no lvalue */\n");
199 void
200 decode (p, fetch, size)
201 struct h8_opcode *p;
202 int fetch;
203 int size;
205 if (fetch)
207 lval (p);
214 static void
215 esleep ()
217 printf ("saved_state.exception = SIGSTOP;\n");
220 static void
221 mov (p, s, sz)
222 struct h8_opcode *p;
223 char *s;
224 int sz;
226 printf ("dst = srca;\n");
229 static void
230 andc (p)
231 struct h8_opcode *p;
233 printf ("SET_CCR(GET_CCR() & srca);\n");
236 static void
237 addx (p)
238 struct h8_opcode *p;
240 printf ("dst = srca + srcb+ (c != 0);\n");
243 static void
244 subx (p)
245 struct h8_opcode *p;
247 printf ("dst = srcb - srca - (c != 0);\n");
250 static void
251 add (p, s, sz)
252 struct h8_opcode *p;
253 char *s;
254 int sz;
256 printf ("%s;\n", s);
259 static void
260 adds (p, s)
261 struct h8_opcode *p;
262 char *s;
264 printf ("%s;\n", s);
267 static void
268 bra (p, a)
269 struct h8_opcode *p;
270 char *a;
272 printf ("if (%s) npc += ((char )b1)>>1;\n", a);
275 static void
276 bsr (p, a)
277 struct h8_opcode *p;
278 char *a;
280 printf ("reg[7]-=2;\n");
281 printf ("tmp = reg[7];\n");
282 printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
283 printf ("npc += ((char)b1)>>1;\n");
286 static void
287 cmp (p, a, s)
288 struct h8_opcode *p;
289 char *a;
290 int s;
292 decode (p, 1, s);
293 printf ("srca = -srca;\n");
294 printf ("dst = srca + srcb;\n");
297 static
298 void
299 jsr (p, a, s)
300 struct h8_opcode *p;
301 char *a;
302 int s;
304 printf ("if (b1 == 0xc4) {\n");
305 printf ("printf(\"%%c\", reg[2]);\n");
306 printf ("}\n");
307 printf ("else {\n");
308 printf ("reg[7]-=2;\n");
309 printf ("tmp = reg[7];\n");
310 printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
311 printf ("npc = (lval>>1) + saved_state.mem;\n");
312 printf ("}");
315 static void
316 jmp (p, a, s)
317 struct h8_opcode *p;
318 char *a;
319 int s;
321 printf ("npc = (lval>>1) + saved_state.mem;\n");
324 static void
325 rts (p, a, s)
326 struct h8_opcode *p;
327 char *a;
328 int s;
330 printf ("tmp = reg[7];\n");
331 printf ("reg[7]+=2;\n");
332 printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
335 static void
336 rte (p, a, s)
337 struct h8_opcode *p;
338 char *a;
339 int s;
341 printf ("reg[7]+=2;\n");
342 printf ("tmp = reg[7];\n");
343 printf ("reg[7]+=2;\n");
344 printf ("SET_CCR(tmp);\n");
345 printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
348 static void
349 setf (p, a, s)
350 struct h8_opcode *p;
351 char *a;
352 int s;
354 printf ("tmp = GET_CCR();\n");
355 printf ("tmp %s= srca;\n", a);
358 static void
359 bpt (p, a, s)
360 struct h8_opcode *p;
361 char *a;
362 int s;
364 printf ("saved_state.exception = SIGTRAP;\n");
365 printf ("npc = pc;\n");
368 static void
369 log (p, a, s)
370 struct h8_opcode *p;
371 char *a;
372 int s;
374 printf ("dst = srcb %s srca;\n", a);
377 static void
378 ulog (p, a, s)
379 struct h8_opcode *p;
380 char *a;
381 int s;
383 printf ("dst = %s srcb ;\n", a);
386 static void
387 nop ()
391 static void
392 rotl ()
394 printf ("c = srcb & 0x80;\n");
395 printf ("dst = srcb << 1;\n");
396 printf ("if (c) dst|=1;\n");
399 static void
400 rotr ()
402 printf ("c = srcb & 1;\n");
403 printf ("dst = srcb >> 1;\n");
404 printf ("if (c) dst|=0x80;\n");
407 static void
408 rotxl ()
410 printf ("tmp = srcb & 0x80;\n");
411 printf ("dst = srcb << 1;\n");
412 printf ("if (c) dst|=1;\n");
413 printf ("c = tmp;\n");
416 static void
417 rotxr ()
419 printf ("tmp = srcb & 1;\n");
420 printf ("dst = srcb >> 1;\n");
421 printf ("if (c) dst|=0x80;\n");
422 printf ("c = tmp;\n");
425 static void
426 shal ()
428 printf ("c = srcb&0x80;\n");
429 printf ("dst = srcb << 1;\n");
432 static
433 void
434 shar ()
436 printf ("c = srcb&0x1;\n");
437 printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");
438 printf ("else dst = (srcb>>1) &~ 0x80;\n");
441 static
442 void
443 shll ()
445 printf ("c = srcb&0x80;\n");
446 printf ("dst = srcb << 1;\n");
449 static
450 void
451 shlr ()
453 printf ("c = srcb&0x1;\n");
454 printf ("dst = (srcb>>1) &~ 0x80;\n");
457 static
458 void
459 divxu ()
461 printf ("srca = %s;\n", breg[2]);
462 printf ("srcb = %s;\n", wreg[3]);
463 printf ("n = srca & 0x80;\n");
464 printf ("z = !srca;\n");
465 printf ("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n");
466 printf ("%s = (dst & 0xff) | (tmp << 8);\n", wreg[3]);
469 static
470 void
471 mulxu ()
473 printf ("srca = %s;\n", breg[2]);
474 printf ("srcb = %s;\n", wreg[3]);
476 printf ("dst = (srcb&0xff) * srca;\n");
477 printf ("%s = dst;\n", wreg[3]);
480 static
481 void
482 inc ()
484 printf ("dst = %s;\n", breg[3]);
485 printf ("v = (dst==0x7f);\n");
486 printf ("dst++;\n");
487 printf ("%s= dst;\n", breg[3]);
490 static
491 void
492 bit (p, a, s)
493 struct h8_opcode *p;
494 char *a;
495 int s;
497 printf ("%s\n", a);
500 static
501 void
502 dec ()
504 printf ("dst = %s;\n", breg[3]);
505 printf ("v = (dst==0x80);\n");
506 printf ("dst--;\n");
507 printf ("%s = dst;\n", breg[3]);
510 char saf[] = "goto setflags;";
511 char sf[] = "goto shiftflags;";
512 char af8[] = "goto aluflags8;";
513 char af16[] = "goto aluflags16;";
514 char lf[] = "goto logflags;";
515 char icf[] = "goto incflags;";
516 char mf8[] = "goto movflags8;";
517 char mf16[] = "goto movflags16;";
518 char nx[] = "goto next;";
520 struct
522 char *ftype;
523 int decode;
524 char *name;
525 void (*func) ();
526 char *arg;
527 int size;
531 table[] =
534 nx, 1, "bld", bit, "dst = srcb; c = (srcb>>srca)&1;", 8
538 nx, 1, "bild", bit, "dst = srcb; c = !((srcb>>srca)&1);", 8
542 nx, 1, "band", bit, "dst = srcb; c = C &&((srcb>>srca)&1);", 8
546 nx, 1, "biand", bit, "dst = srcb; c = C &&(!((srcb>>srca)&1));", 8
550 nx, 1, "bior", bit, "dst = srcb; c = C ||(!((srcb>>srca)&1));", 8
554 nx, 1, "bor", bit, "dst = srcb; c = C ||(((srcb>>srca)&1));", 8
558 nx, 1, "bixor", bit, "dst = srcb; c = C ^(!((srcb>>srca)&1));", 8
562 nx, 1, "bxor", bit, "dst = srcb; c = C ^(((srcb>>srca)&1));", 8
566 nx, 1, "bnot", bit, "dst = srcb ^ (1<<srca);", 8
570 nx, 1, "bclr", bit, "dst = srcb & ~(1<<srca);", 8
574 nx, 1, "bset", bit, "dst = srcb | (1<<srca);", 8
578 nx, 1, "bst", bit, "dst = (srcb & ~(1<<srca))| ((C)<<srca);", 8
582 nx, 1, "bist", bit, "dst = (srcb & ~(1<<srca))| ((!C)<<srca);", 8
586 nx, 1, "btst", bit, "dst = srcb; z = !((srcb>>srca)&1);", 8
590 icf, 0, "dec", dec, 0, 0
594 icf, 0, "inc", inc, 0, 0
598 saf, 1, "orc", setf, "|", 0
602 saf, 1, "xorc", setf, "^", 0
606 saf, 1, "andc", setf, "&", 0
610 nx, 1, "nop", nop, 0, 0
614 nx, 1, "bra", bra, "1", 0
618 nx, 1, "brn", bra, "0", 0
622 nx, 1, "bhi", bra, "(C||Z)==0", 0
626 nx, 1, "bls", bra, "(C||Z)==1", 0
630 nx, 1, "bcs", bra, "C==1", 0
634 nx, 1, "bcc", bra, "C==0", 0
638 nx, 1, "bpl", bra, "N==0", 0
642 nx, 1, "bmi", bra, "N==1", 0
646 nx, 1, "bvs", bra, "V==1", 0
650 nx, 1, "bvc", bra, "V==0", 0
654 nx, 1, "bge", bra, "(N^V)==0", 0
658 nx, 1, "bgt", bra, "(Z|(N^V))==0", 0
662 nx, 1, "blt", bra, "(N^V)==1", 0
666 nx, 1, "ble", bra, "(Z|(N^V))==1", 0
670 nx, 1, "beq", bra, "Z==1", 0
674 nx, 1, "bne", bra, "Z==0", 0
678 nx, 1, "bsr", bsr, "", 0
682 nx, 1, "jsr", jsr, 0, 0
686 nx, 1, "jmp", jmp, 0, 0
690 nx, 0, "rts", rts, 0, 0
694 nx, 0, "rte", rte, 0, 0
698 nx, 1, "andc", andc, 0, 0
702 sf, 1, "shal", shal, 0, 0
706 sf, 1, "shar", shar, 0, 0
710 sf, 1, "shll", shll, 0, 0
714 sf, 1, "shlr", shlr, 0, 0
718 sf, 1, "rotxl", rotxl, 0, 0
722 sf, 1, "rotxr", rotxr, 0, 0
726 sf, 1, "rotl", rotl, 0, 0
730 sf, 1, "rotr", rotr, 0, 0
734 lf, 1, "xor", log, "^", 0
738 lf, 1, "and", log, "&", 0
742 lf, 1, "or", log, "|", 0
746 lf, 1, "not", ulog, " ~", 0
750 lf, 1, "neg", ulog, " - ", 0
754 nx, 1, "adds", adds, "dst = srca + srcb", 0
758 nx, 1, "subs", adds, "srca = -srca; dst = srcb + srca", 0
762 af8, 1, "add.b", add, "dst = srca + srcb", 8
766 af16, 1, "add.w", add, "dst = srca + srcb", 16
770 af16, 1, "sub.w", add, "srca = -srca; dst = srcb + srca", 16
774 af8, 1, "sub.b", add, "srca = -srca; dst = srcb + srca", 8
778 af8, 1, "addx", addx, 0, 8
782 af8, 1, "subx", subx, 0, 8
786 af8, 0, "cmp.b", cmp, 0, 8
790 af16, 0, "cmp.w", cmp, 0, 16
794 nx, 1, "sleep", esleep, 0, 0
798 nx, 0, "bpt", bpt, 0, 8
802 nx, 0, "divxu", divxu, 0, 0
806 nx, 0, "mulxu", mulxu, 0, 0
810 mf8, 1, "mov.b", mov, 0, 8
814 mf8, 1, "movtpe", mov, 0, 8
818 mf8, 1, "movfpe", mov, 0, 8
822 mf16, 1, "mov.w", mov, 0, 16
830 static
831 void
832 edo (p)
833 struct h8_opcode *p;
835 int i;
837 printf ("%s %s %s\n", cs, p->name, ce);
839 for (i = 0; table[i].name; i++)
841 if (strcmp (table[i].name, p->name) == 0)
843 printf ("{\n");
844 if (table[i].decode)
845 decode (p, 1, table[i].size);
846 printf ("cycles += %d;\n", p->time);
847 printf ("npc = pc + %d;\n", p->length / 2);
848 table[i].func (p, table[i].arg, table[i].size);
849 if (table[i].decode)
850 decode (p, 0, table[i].size);
851 if (table[i].ftype)
852 printf (table[i].ftype);
853 else
854 printf ("goto next;\n");
855 printf ("}\n");
856 return;
859 printf ("%s not found %s\n", cs, ce);
860 printf ("saved_state.exception = SIGILL;\n");
861 printf ("break;\n");
864 static
866 owrite (i)
867 int i;
869 /* write if statements to select the right opcode */
870 struct h8_opcode **p;
871 int needand = 1;
873 p = h8_opcodes_sorted[i];
874 printf ("case 0x%03x:\n", i);
876 if (p[1] == 0)
878 /* See if the next few also match */
879 while (h8_opcodes_sorted[i + 1][0] == *p)
881 i++;
882 printf ("case 0x%03x:\n", i);
885 /* Don't need any if's this is the only one */
886 edo (*p);
888 else
890 while (*p)
892 /* start two nibbles in since we know we match in the first byte */
893 int c;
894 int nib = 2;
895 int byte = 1;
896 int mask1[5];
897 int mask0[5];
898 int nibshift = 4;
899 int any = 0;
901 for (c = 0; c < 5; c++)
903 mask1[c] = 0;
904 mask0[c] = 0;
906 printf ("%s %x%x", cs, (*p)->data.nib[0], (*p)->data.nib[1]);
907 while ((c = (*p)->data.nib[nib]) != E)
909 if (c & B30)
911 /* bit 3 must be zero */
912 mask0[byte] |= 0x8 << nibshift;
913 printf ("0");
914 any = 1;
916 else if (c & B31)
918 /* bit 3 must be one */
919 mask1[byte] |= 0x8 << nibshift;
920 printf ("8");
921 any = 1;
923 else if (c <= HexF)
925 mask0[byte] |= ((~c) & 0xf) << nibshift;
926 mask1[byte] |= (c & 0xf) << nibshift;
927 printf ("%x", c);
928 any = 1;
930 else
932 printf ("x");
934 nib++;
935 if (nibshift == 4)
937 nibshift = 0;
939 else
941 byte++;
942 nibshift = 4;
945 printf ("*/\n");
946 if (any)
948 printf ("if (");
949 needand = 0;
950 for (c = 1; c < byte; c++)
952 if (mask0[c] | mask1[c])
954 int sh;
956 if (needand)
957 printf ("\n&&");
958 if (c & 1)
959 sh = 0;
960 else
961 sh = 8;
962 if (c / 2 == 0 && sh == 0)
963 printf ("((b1&0x%x)==0x%x)", mask0[c] | mask1[c],
964 mask1[c]);
965 else
967 printf ("((pc[%d]&(0x%02x<<%d))==(0x%x<<%d))",
968 c / 2, mask0[c] | mask1[c], sh,
969 mask1[c], sh);
972 needand = 1;
975 printf (")\n");
977 edo (*p);
978 p++;
982 return i;
985 static
986 void
987 remove_dups ()
989 struct h8_opcode *s;
990 struct h8_opcode *d;
992 for (d = s = h8_opcodes; s->name; s++)
994 int doit = 1;
996 if (strcmp (s->name, "push") == 0)
997 doit = 0;
998 if (strcmp (s->name, "bhs") == 0)
999 doit = 0;
1000 if (strcmp (s->name, "blo") == 0)
1001 doit = 0;
1002 if (strcmp (s->name, "bt") == 0)
1003 doit = 0;
1004 if (strcmp (s->name, "bf") == 0)
1005 doit = 0;
1006 if (strcmp (s->name, "pop") == 0)
1007 doit = 0;
1008 if (doit)
1010 *d++ = *s;
1013 *d++ = *s++;
1017 main ()
1019 int i;
1021 remove_dups ();
1022 init ();
1024 printf ("%s do the operation %s\n", cs, ce);
1025 printf ("switch (b0) \n{\n");
1026 for (i = 0; i < PTWO; i++)
1028 i = owrite (i);
1030 printf ("}\n");
1032 return 0;