Commit | Line | Data |
---|---|---|
c906108c SS |
1 | |
2 | /* generate instructions for Z8KSIM | |
3 | Copyright (C) 1992, 1993 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Z8KSIM | |
6 | ||
7 | Z8KSIM is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | Z8KSIM is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with Z8KZIM; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | /* This program generates the code which emulates each of the z8k | |
22 | instructions | |
23 | ||
24 | code goes into three files, tc-gen1.h, tc-gen2.h and tc-gen3.h. | |
25 | which file being made depends upon the options | |
26 | ||
27 | -1 tc-gen1.h contains the fully expanded code for some selected | |
28 | opcodes, (those in the quick.c list) | |
29 | ||
30 | -2 tc-gen2.h contains a list of pointers to functions, one for each | |
31 | opcode. It points to functions in tc-gen3.h and tc-gen1.h | |
32 | depending upon quick.c | |
33 | ||
34 | -3 tc-gen3.h contains all the opcodes in unexpanded form. | |
35 | ||
36 | -b3 tc-genb3.h same as -3 but for long pointers | |
37 | ||
38 | -m regenerates list.c, which is an inverted list of opcodes to | |
39 | pointers into the z8k dissassemble opcode table, it's just there | |
40 | to makes things faster. | |
41 | */ | |
42 | ||
43 | /* steve chamberlain | |
44 | sac@cygnus.com */ | |
45 | ||
46 | #include "config.h" | |
47 | ||
48 | #include <ansidecl.h> | |
49 | #include <stdio.h> | |
50 | #ifdef HAVE_STDLIB_H | |
51 | #include <stdlib.h> | |
52 | #endif | |
53 | #ifdef HAVE_STRING_H | |
54 | #include <string.h> | |
55 | #else | |
56 | #ifdef HAVE_STRINGS_H | |
57 | #include <strings.h> | |
58 | #endif | |
59 | #endif | |
60 | ||
61 | #define NICENAMES | |
62 | ||
63 | #define DEFINE_TABLE | |
64 | #include "../opcodes/z8k-opc.h" | |
65 | ||
66 | #define NOPS 500 | |
67 | ||
68 | #define DIRTY_HACK 0 /* Enable if your gcc can't cope with huge tables */ | |
69 | extern short z8k_inv_list[]; | |
70 | struct opcode_value | |
71 | { | |
72 | int n; | |
73 | struct opcode_value *next; | |
74 | }; | |
75 | ||
76 | #define NICENAMES | |
77 | int BIG; | |
78 | ||
79 | static char *reg_names[] = | |
80 | {"bad", "src", "dst", "aux_a", "aux_b", "aux_r", "aux_x"}; | |
81 | ||
82 | #define IS_DST(x) ((x & 0xf) == 2) | |
83 | #define IS_SRC(x) ((x & 0xf)==1) | |
84 | #define SIZE_ADDRESS (BIG ? 8 : 4) /* number of nibbles in a ptr*/ | |
85 | ||
86 | static int file; | |
87 | static int makelist; | |
88 | ||
89 | static int nibs = 0; | |
90 | ||
91 | static char *current_size; | |
92 | static char *current_name; | |
93 | static char current_word0[40]; | |
94 | static char current_byte0[40]; | |
95 | static char current_byte1[40]; | |
96 | static int indent; | |
97 | static char *p; | |
98 | static char *d; | |
99 | ||
100 | struct opcode_value *list[NOPS]; | |
101 | ||
102 | static opcode_entry_type * | |
103 | lookup_inst (what) | |
104 | int what; | |
105 | { | |
106 | if (makelist) | |
107 | { | |
108 | ||
109 | int nibl_index; | |
110 | int nibl_matched; | |
111 | unsigned short instr_nibl; | |
112 | unsigned short tabl_datum, datum_class, datum_value; | |
113 | char instr_nibbles[8]; | |
114 | ||
115 | opcode_entry_type *ptr = z8k_table; | |
116 | ||
117 | nibl_matched = 0; | |
118 | ||
119 | instr_nibbles[3] = (what >> 0) & 0xf; | |
120 | instr_nibbles[2] = (what >> 4) & 0xf; | |
121 | instr_nibbles[1] = (what >> 8) & 0xf; | |
122 | instr_nibbles[0] = (what >> 12) & 0xf; | |
123 | ||
124 | while (ptr->name) | |
125 | { | |
126 | nibl_matched = 1; | |
127 | for (nibl_index = 0; nibl_index < 4 && nibl_matched; nibl_index++) | |
128 | { | |
129 | instr_nibl = instr_nibbles[nibl_index]; | |
130 | ||
131 | tabl_datum = ptr->byte_info[nibl_index]; | |
132 | datum_class = tabl_datum & CLASS_MASK; | |
133 | datum_value = ~CLASS_MASK & tabl_datum; | |
134 | ||
135 | switch (datum_class) | |
136 | { | |
137 | case CLASS_BIT_1OR2: | |
138 | if (datum_value != (instr_nibl & ~0x2)) | |
139 | nibl_matched = 0; | |
140 | break; | |
141 | ||
d1b0a5b4 NC |
142 | case CLASS_IGNORE: |
143 | break; | |
c906108c SS |
144 | case CLASS_BIT: |
145 | if (datum_value != instr_nibl) | |
146 | nibl_matched = 0; | |
147 | break; | |
148 | case CLASS_00II: | |
149 | if (!((~instr_nibl) & 0x4)) | |
150 | nibl_matched = 0; | |
151 | break; | |
152 | case CLASS_01II: | |
153 | if (!(instr_nibl & 0x4)) | |
154 | nibl_matched = 0; | |
155 | break; | |
156 | case CLASS_0CCC: | |
157 | if (!((~instr_nibl) & 0x8)) | |
158 | nibl_matched = 0; | |
159 | break; | |
160 | case CLASS_1CCC: | |
161 | if (!(instr_nibl & 0x8)) | |
162 | nibl_matched = 0; | |
163 | break; | |
164 | case CLASS_0DISP7: | |
165 | if (!((~instr_nibl) & 0x8)) | |
166 | nibl_matched = 0; | |
167 | nibl_index += 1; | |
168 | break; | |
169 | case CLASS_1DISP7: | |
170 | if (!(instr_nibl & 0x8)) | |
171 | nibl_matched = 0; | |
172 | nibl_index += 1; | |
173 | break; | |
174 | case CLASS_REGN0: | |
175 | if (instr_nibl == 0) | |
176 | nibl_matched = 0; | |
177 | break; | |
178 | default: | |
179 | break; | |
180 | } | |
181 | } | |
182 | if (nibl_matched) | |
183 | { | |
184 | return ptr; | |
185 | } | |
186 | ptr++; | |
187 | } | |
188 | return 0; | |
189 | } | |
190 | else | |
191 | { | |
192 | ||
193 | if (z8k_inv_list[what] < 0) | |
194 | return 0; | |
195 | return z8k_table + z8k_inv_list[what]; | |
196 | } | |
197 | } | |
198 | ||
199 | static char * | |
200 | insn_4 (n) | |
201 | int n; | |
202 | { | |
203 | switch (n) | |
204 | { | |
205 | case 1: | |
206 | return "((iwords_0>>8) & 0xf)"; | |
207 | case 2: | |
208 | return "((ibytes_1 >> 4) & 0xf)"; | |
209 | case 3: | |
210 | return "((ibytes_1) & 0xf)"; | |
211 | case 4: | |
212 | return "((ibytes_2>>4) & 0xf)"; | |
213 | case 5: | |
214 | return "((ibytes_2) & 0xf)"; | |
215 | case 6: | |
216 | return "((ibytes_3 >> 4) & 0xf)"; | |
217 | case 7: | |
218 | return "((ibytes_3) & 0xf)"; | |
219 | default: | |
220 | return "****"; | |
221 | } | |
222 | } | |
223 | ||
224 | char * | |
225 | ptr_mode () | |
226 | { | |
227 | if (BIG) | |
228 | { | |
229 | return "ptr_long"; | |
230 | } | |
231 | return "word"; | |
232 | } | |
233 | ||
234 | static | |
235 | char * | |
236 | ptr_size () | |
237 | { | |
238 | if (BIG) | |
239 | return "4"; | |
240 | return "2"; | |
241 | } | |
242 | ||
243 | static char * | |
244 | reg_n (x) | |
245 | unsigned int x; | |
246 | { | |
247 | return reg_names[x & 0xf]; | |
248 | } | |
249 | ||
250 | char * | |
251 | stack_ptr () | |
252 | { | |
253 | return BIG ? "14" : "15"; | |
254 | } | |
255 | ||
256 | char * | |
257 | mem () | |
258 | { | |
259 | #if 0 | |
260 | return BIG ? "segmem" : "unsegmem"; | |
261 | #else | |
262 | return "mem"; | |
263 | #endif | |
264 | } | |
265 | ||
266 | int | |
267 | match (a) | |
268 | char *a; | |
269 | { | |
270 | if (strncmp (p, a, strlen (a)) == 0) | |
271 | { | |
272 | p += strlen (a); | |
273 | return 1; | |
274 | } | |
275 | return 0; | |
276 | } | |
277 | ||
278 | static | |
279 | void | |
280 | sub (y) | |
281 | char *y; | |
282 | { | |
283 | sprintf (d, "%s", y); | |
284 | d += strlen (d); | |
285 | } | |
286 | ||
287 | static char * | |
288 | insn_16 (n) | |
289 | int n; | |
290 | { | |
291 | switch (n) | |
292 | { | |
293 | case 0: | |
294 | return "(iwords_0)"; | |
295 | case 4: | |
296 | return "(iwords_1)"; | |
297 | case 8: | |
298 | return "(iwords_2)"; | |
299 | case 12: | |
300 | return "(iwords_3)"; | |
301 | default: | |
302 | return "****"; | |
303 | } | |
304 | } | |
305 | ||
306 | static | |
307 | char * | |
308 | insn_32 (n) | |
309 | int n; | |
310 | { | |
311 | switch (n) | |
312 | { | |
313 | case 0: | |
314 | return "((iwords_0<<16) | (iwords_1))"; | |
315 | case 4: | |
316 | return "((iwords_1<<16) | (iwords_2))"; | |
317 | case 8: | |
318 | return "((iwords_2<<16) | (iwords_3))"; | |
319 | default: | |
320 | return "****"; | |
321 | } | |
322 | } | |
323 | ||
324 | static char * | |
325 | size_name (x) | |
326 | int x; | |
327 | { | |
328 | switch (x) | |
329 | { | |
330 | case 8: | |
331 | return "byte"; | |
332 | case 16: | |
333 | return "word"; | |
334 | case 32: | |
335 | return "long"; | |
336 | case 64: | |
337 | return "quad"; | |
338 | } | |
339 | return "!!"; | |
340 | } | |
341 | ||
342 | /*VARARGS*/ | |
343 | void | |
344 | emit (string, a1, a2, a3, a4, a5) | |
345 | char *string; | |
346 | char* a1; | |
347 | char* a2; | |
348 | char* a3; | |
349 | char* a4; | |
350 | char* a5; | |
351 | { | |
352 | int indent_inc = 0; | |
353 | int indent_dec = 0; | |
354 | int i; | |
355 | char buffer[1000]; | |
356 | ||
357 | d = buffer; | |
358 | p = string; | |
359 | ||
360 | while (*p) | |
361 | { | |
362 | if (match ("<fop>")) | |
363 | { | |
364 | if (BIG) | |
365 | { | |
366 | sub ("bfop"); | |
367 | } | |
368 | else | |
369 | { | |
370 | sub ("sfop"); | |
371 | } | |
372 | } | |
373 | else if (match ("<iptr>")) | |
374 | { | |
375 | if (BIG) | |
376 | { | |
377 | switch (nibs) | |
378 | { | |
379 | case 4: | |
380 | sub ("(((iwords_1 << 8) | (iwords_2)) & 0x7fffff)"); | |
381 | break; | |
382 | default: | |
383 | sub ("fail(context,124)"); | |
384 | break; | |
385 | } | |
386 | } | |
387 | else | |
388 | { | |
389 | switch (nibs) | |
390 | { | |
391 | case 4: | |
392 | sub ("iwords_1"); | |
393 | break; | |
394 | default: | |
395 | sub ("fail(context,123)"); | |
396 | break; | |
397 | } | |
398 | } | |
399 | } | |
400 | else if (match ("<name>")) | |
401 | { | |
402 | sub (current_name); | |
403 | } | |
404 | else if (match ("<size>")) | |
405 | { | |
406 | sub (current_size); | |
407 | } | |
408 | else if (match ("<insn_4>")) | |
409 | { | |
410 | sub (insn_4 (nibs)); | |
411 | } | |
412 | else if (match ("<insn_16>")) | |
413 | { | |
414 | sub (insn_16 (nibs)); | |
415 | } | |
416 | else if (match ("<insn_32>")) | |
417 | { | |
418 | sub (insn_32 (nibs)); | |
419 | } | |
420 | else if (match ("iwords_0")) | |
421 | { | |
422 | sub (current_word0); | |
423 | } | |
424 | else if (match ("ibytes_0")) | |
425 | { | |
426 | sub (current_byte0); | |
427 | } | |
428 | else if (match ("<ibytes_1>")) | |
429 | { | |
430 | sub (current_byte1); | |
431 | } | |
432 | else if (match ("<next_size>")) | |
433 | { | |
434 | if (strcmp (current_size, "word") == 0) | |
435 | sub ("long"); | |
436 | if (strcmp (current_size, "byte") == 0) | |
437 | sub ("word"); | |
438 | else if (strcmp (current_size, "long") == 0) | |
439 | sub ("quad"); | |
440 | else | |
441 | abort (); | |
442 | } | |
443 | else if (match ("<addr_type>")) | |
444 | { | |
445 | if (BIG) | |
446 | sub ("unsigned long"); | |
447 | else | |
448 | sub ("unsigned short"); | |
449 | } | |
450 | ||
451 | else if (match ("<c_size>")) | |
452 | { | |
453 | if (strcmp (current_size, "word") == 0) | |
454 | sub ("short"); | |
455 | else if (strcmp (current_size, "byte") == 0) | |
456 | sub ("char"); | |
457 | else if (strcmp (current_size, "long") == 0) | |
458 | sub ("long"); | |
459 | else | |
460 | abort (); | |
461 | } | |
462 | ||
463 | else if (match ("<pc>")) | |
464 | { | |
465 | sub ("pc"); | |
466 | } | |
467 | else if (match ("<mem>")) | |
468 | { | |
469 | sub (mem ()); | |
470 | } | |
471 | else if (match ("<sp>")) | |
472 | { | |
473 | sub (stack_ptr ()); | |
474 | } | |
475 | else if (match ("<ptr_size>")) | |
476 | { | |
477 | sub (ptr_size ()); | |
478 | } | |
479 | else if (match ("<ptr_mode>")) | |
480 | { | |
481 | sub (ptr_mode ()); | |
482 | } | |
483 | else if (match ("<insn_8>")) | |
484 | { | |
485 | switch (nibs) | |
486 | { | |
487 | case 2: | |
488 | sub ("(iwords_0&0xff)"); | |
489 | break; | |
490 | case 4: | |
491 | sub ("(iwords_1>>8)"); | |
492 | break; | |
493 | case 6: | |
494 | sub ("(iwords_1&0xff)"); | |
495 | break; | |
496 | case 8: | |
497 | sub ("(iwords_2>>8)"); | |
498 | break; | |
499 | case 12: | |
500 | sub ("(/* WHO */iwords_3&0xff)"); | |
501 | break; | |
502 | default: | |
503 | abort (); | |
504 | } | |
505 | } | |
506 | else | |
507 | { | |
508 | if (*p == '{') | |
509 | indent_inc++; | |
510 | if (*p == '}') | |
511 | indent_dec++; | |
512 | *d++ = *p; | |
513 | p++; | |
514 | } | |
515 | } | |
516 | *d++ = 0; | |
517 | indent -= indent_dec; | |
518 | for (i = 0; i < indent; i++) | |
519 | printf ("\t"); | |
520 | indent += indent_inc; | |
521 | printf (buffer, a1, a2, a3, a4, a5); | |
522 | ||
523 | } | |
524 | ||
525 | /* fetch the lvalues of the operands */ | |
526 | void | |
527 | info_args (p) | |
528 | opcode_entry_type *p; | |
529 | { | |
530 | unsigned int *s; | |
531 | ||
532 | int done_one_imm8 = 0; | |
533 | ||
534 | /* int done_read = 4;*/ | |
535 | s = p->byte_info; | |
536 | nibs = 0; | |
537 | while (*s) | |
538 | { | |
539 | switch (*s & CLASS_MASK) | |
540 | { | |
541 | case CLASS_BIT_1OR2: | |
542 | emit ("register unsigned int imm_src=(<insn_4>& 2)?2:1;\n"); | |
543 | break; | |
d1b0a5b4 | 544 | case CLASS_IGNORE: |
c906108c SS |
545 | case CLASS_BIT: |
546 | /* Just ignore these, we've already decoded this bit */ | |
547 | nibs++; | |
548 | break; | |
549 | case CLASS_REGN0: | |
550 | case CLASS_REG: | |
551 | /* this nibble tells us which register to use as an arg, | |
552 | if we've already gobbled the nibble we know what to use */ | |
553 | { | |
554 | int regname = *s & 0xf; | |
555 | ||
556 | emit ("register unsigned int reg_%s=<insn_4>;\n", | |
557 | reg_names[regname]); | |
558 | ||
559 | nibs++; | |
560 | } | |
561 | break; | |
562 | case CLASS_ADDRESS: | |
563 | emit ("register unsigned base_%s=<iptr>;\n", reg_n (*s)); | |
564 | ||
565 | nibs += SIZE_ADDRESS; | |
566 | ||
567 | break; | |
568 | case CLASS_01II: | |
569 | case CLASS_00II: | |
570 | emit ("register unsigned int imm_src=<insn_4>&0x2;\n"); | |
571 | nibs++; | |
572 | break; | |
573 | case CLASS_FLAGS: | |
574 | emit ("register unsigned int imm_src=<insn_4>;\n"); | |
575 | nibs++; | |
576 | break; | |
577 | case CLASS_IMM: | |
578 | /* Work out the size of the think to fetch */ | |
579 | ||
580 | { | |
581 | switch (*s & ~CLASS_MASK) | |
582 | { | |
583 | case ARG_IMM16: | |
584 | emit ("register unsigned imm_src=<insn_16>;\n"); | |
585 | nibs += 4; | |
586 | break; | |
587 | case ARG_IMM32: | |
588 | emit ("register unsigned int imm_src= %s;\n", insn_32 (nibs)); | |
589 | nibs += 8; | |
590 | break; | |
591 | case ARG_IMM4: | |
592 | emit ("register unsigned int imm_src=<insn_4>;\n"); | |
593 | nibs++; | |
594 | break; | |
d1b0a5b4 NC |
595 | case ARG_NIM4: |
596 | emit ("register unsigned int imm_src = - <insn_4>;\n"); | |
597 | nibs++; | |
598 | break; | |
c906108c SS |
599 | case ARG_IMM2: |
600 | emit ("register unsigned int imm_src=<insn_4> & 0x2;\n"); | |
601 | nibs++; | |
602 | break; | |
603 | ||
604 | case ARG_IMM4M1: | |
605 | emit ("register unsigned int imm_src=(<insn_4> + 1);\n"); | |
606 | nibs++; | |
607 | break; | |
608 | case ARG_IMM_1: | |
609 | emit ("register unsigned int imm_src=1;\n"); | |
610 | break; | |
611 | case ARG_IMM_2: | |
612 | emit ("register unsigned int imm_src=2;\n"); | |
613 | break; | |
614 | case ARG_NIM8: | |
615 | emit ("register unsigned int imm_src=-<insn_8>;\n"); | |
616 | nibs += 2; | |
617 | break; | |
618 | case ARG_IMM8: | |
619 | if (!done_one_imm8) | |
620 | { | |
621 | emit ("register unsigned int imm_src=<insn_8>;\n"); | |
622 | nibs += 2; | |
623 | done_one_imm8 = 1; | |
624 | } | |
625 | break; | |
626 | default: | |
627 | emit ("register int fail%d=fail(context,1);\n", nibs); | |
628 | break; | |
629 | } | |
630 | break; | |
631 | ||
632 | case CLASS_DISP8: | |
633 | /* We can't use `(char)' since char might be unsigned. | |
634 | We can't use `(signed char)' because the compiler might be K&R. | |
635 | This seems safe, since it only assumes that bytes are 8 | |
636 | bits. */ | |
637 | emit ("register unsigned int oplval_dst=((ibytes_1 << (sizeof (int) * 8 - 8)) >> (sizeof (int) * 8 - 9)) + pc;\n"); | |
638 | #if 0 | |
639 | /* Original code: fails if characters are unsigned. */ | |
640 | emit ("register unsigned int oplval_dst=(((char)ibytes_1)<<1) + pc;\n"); | |
641 | #endif | |
642 | nibs += 2; | |
643 | break; | |
644 | case CLASS_CC: | |
645 | emit ("register unsigned int op_cc=<insn_4>;\n"); | |
646 | nibs++; | |
647 | break; | |
648 | default: | |
649 | emit ("register int FAIL%d=fail(context,2);\n", nibs); | |
650 | break; | |
651 | } | |
652 | ; | |
653 | /* work out how to fetch the immediate value */ | |
654 | } | |
655 | ||
656 | s++; | |
657 | } | |
658 | } | |
659 | ||
660 | void | |
661 | info_special (p, getdst, nostore, before, nosrc) | |
662 | opcode_entry_type *p; | |
663 | int *getdst; | |
664 | int *nostore; | |
665 | int *before; | |
666 | int *nosrc; | |
667 | { | |
668 | switch (p->opcode) | |
669 | { | |
670 | case OPC_exts: | |
671 | case OPC_extsb: | |
672 | case OPC_extsl: | |
673 | *nostore = 1; | |
674 | *nosrc = 1; | |
675 | break; | |
676 | case OPC_ldm: | |
677 | *nostore = 1; | |
678 | *nosrc = 1; | |
679 | break; | |
680 | case OPC_negb: | |
681 | case OPC_neg: | |
682 | case OPC_sla: | |
683 | case OPC_slab: | |
684 | case OPC_slal: | |
685 | case OPC_sda: | |
686 | case OPC_sdab: | |
687 | case OPC_sdal: | |
688 | case OPC_com: | |
689 | case OPC_comb: | |
690 | case OPC_adc: | |
691 | case OPC_sbc: | |
692 | case OPC_nop: | |
693 | case OPC_adcb: | |
694 | case OPC_add: | |
695 | case OPC_addb: | |
696 | case OPC_addl: | |
697 | case OPC_inc: | |
698 | case OPC_sub: | |
699 | case OPC_subb: | |
700 | case OPC_subl: | |
701 | case OPC_and: | |
702 | case OPC_andb: | |
703 | case OPC_xorb: | |
704 | case OPC_xor: | |
705 | break; | |
706 | ||
707 | case OPC_mult: | |
708 | case OPC_multl: | |
709 | case OPC_div: | |
710 | case OPC_divl: | |
711 | ||
712 | *nostore = 1; | |
713 | break; | |
714 | ||
715 | case OPC_testb: | |
716 | case OPC_test: | |
717 | case OPC_testl: | |
718 | case OPC_cp: | |
719 | case OPC_cpb: | |
720 | case OPC_cpl: | |
721 | case OPC_bit: | |
722 | *nostore = 1; | |
723 | *before = 0; | |
724 | break; | |
725 | ||
726 | case OPC_bpt: | |
727 | case OPC_jr: | |
728 | case OPC_jp: | |
729 | case OPC_ret: | |
730 | case OPC_call: | |
731 | case OPC_tcc: | |
732 | *nosrc = 1; | |
733 | *nostore = 1; | |
734 | *before = 1; | |
735 | break; | |
736 | case OPC_sc: | |
737 | *nostore = 1; | |
738 | *before = 0; | |
739 | break; | |
740 | case OPC_clrb: | |
741 | case OPC_clr: | |
742 | *before = 1; | |
743 | *nosrc = 1; | |
744 | break; | |
745 | case OPC_ldi: | |
746 | case OPC_ldib: | |
747 | case OPC_lddb: | |
748 | case OPC_ldd: | |
749 | ||
750 | *before = 1; | |
751 | *nostore = 1; | |
752 | *nosrc = 1; | |
753 | break; | |
754 | case OPC_ldk: | |
755 | case OPC_ld: | |
756 | case OPC_ldb: | |
757 | case OPC_ldl: | |
758 | *before = 1; | |
759 | *getdst = 0; | |
760 | break; | |
761 | case OPC_push: | |
762 | case OPC_pushl: | |
763 | case OPC_pop: | |
764 | case OPC_popl: | |
765 | *before = 1; | |
766 | *getdst = 0; | |
767 | break; | |
768 | case OPC_lda: | |
769 | *nosrc = 1; | |
770 | break; | |
771 | } | |
772 | } | |
773 | ||
774 | /* calculate the lvalues required for the opcode */ | |
775 | void | |
776 | info_lvals (p) | |
777 | opcode_entry_type *p; | |
778 | { | |
779 | /* emit code to work out lvalues, if any */ | |
780 | unsigned int *i = p->arg_info; | |
781 | ||
782 | while (*i) | |
783 | { | |
784 | current_name = reg_n (*i); | |
785 | current_size = size_name (p->type); | |
786 | switch (*i & CLASS_MASK) | |
787 | { | |
788 | case CLASS_X: | |
789 | /* address(reg) */ | |
790 | emit ("register <addr_type> oplval_<name>= ((base_<name> + (short)get_word_reg(context,reg_<name>)) & 0xffff) + (base_<name> & ~0xffff);\n"); | |
791 | break; | |
792 | case CLASS_IR: | |
793 | /* Indirect register */ | |
794 | emit ("register int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>);\n"); | |
795 | break; | |
796 | case CLASS_DA: | |
797 | emit ("register int oplval_<name>=base_<name>;\n"); | |
798 | break; | |
799 | case CLASS_IMM: | |
800 | case CLASS_REG_WORD: | |
801 | case CLASS_REG_LONG: | |
802 | case CLASS_REG_BYTE: | |
803 | case CLASS_PR: | |
804 | break; | |
805 | case CLASS_BA: | |
806 | emit ("register int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>) + (short)(imm_src);\n"); | |
807 | break; | |
808 | case CLASS_BX: | |
809 | emit ("register int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>)\n"); | |
810 | emit (" + get_word_reg(context,reg_aux_x);\n"); | |
811 | break; | |
812 | } | |
813 | i++; | |
814 | } | |
815 | } | |
816 | ||
817 | /* emit code to fetch the args from calculated lvalues */ | |
818 | int allregs; | |
819 | void | |
820 | info_fetch (p, getdst) | |
821 | opcode_entry_type *p; | |
822 | int getdst; | |
823 | { | |
824 | unsigned int *i = p->arg_info; | |
825 | int had_src = 0; | |
826 | ||
827 | allregs = 1; | |
828 | while (*i) | |
829 | { | |
830 | ||
831 | current_name = reg_n (*i); | |
832 | current_size = size_name (p->type); | |
833 | switch (*i & CLASS_MASK) | |
834 | { | |
835 | case CLASS_X: | |
836 | case CLASS_IR: | |
837 | case CLASS_BA: | |
838 | case CLASS_BX: | |
839 | case CLASS_DA: | |
840 | if (!getdst && IS_DST (*i)) | |
841 | break; | |
842 | emit ("register int op_<name>= get_<size>_<mem>_da(context,oplval_<name>);\n"); | |
843 | allregs = 0; | |
844 | break; | |
845 | case CLASS_IMM: | |
846 | if (!had_src) | |
847 | { | |
848 | if (p->opcode == OPC_out || | |
849 | p->opcode == OPC_outb || | |
850 | p->opcode == OPC_sout || | |
851 | p->opcode == OPC_soutb) | |
852 | { | |
853 | /* The imm is a dest here */ | |
854 | emit ("register int op_dst = imm_src;\n"); | |
855 | } | |
856 | else | |
857 | { | |
858 | emit ("register int op_src = imm_src;\n"); | |
859 | } | |
860 | } | |
861 | break; | |
862 | case CLASS_REG_QUAD: | |
863 | if (!getdst && IS_DST (*i)) | |
864 | break; | |
865 | had_src |= IS_SRC (*i); | |
866 | emit ("UDItype op_<name> ;\n"); | |
867 | ||
868 | break; | |
869 | case CLASS_REG_WORD: | |
870 | if (!getdst && IS_DST (*i)) | |
871 | break; | |
872 | had_src |= IS_SRC (*i); | |
873 | emit ("register int op_<name> = get_word_reg(context,reg_<name>);\n"); | |
874 | break; | |
875 | ||
876 | case CLASS_REG_LONG: | |
877 | if (!getdst && IS_DST (*i)) | |
878 | break; | |
879 | had_src |= IS_SRC (*i); | |
880 | emit ("register int op_<name> = get_long_reg(context,reg_<name>);\n"); | |
881 | break; | |
882 | case CLASS_REG_BYTE: | |
883 | if (!getdst && IS_DST (*i)) | |
884 | break; | |
885 | had_src |= IS_SRC (*i); | |
886 | emit ("register int op_<name> = get_byte_reg(context,reg_<name>);\n"); | |
887 | break; | |
888 | } | |
889 | i++; | |
890 | } | |
891 | } | |
892 | ||
893 | static void | |
894 | normal_flags (p, s, neg) | |
895 | opcode_entry_type *p; | |
896 | char *s; | |
897 | { | |
898 | emit (" %s;\n", s); | |
899 | emit ("NORMAL_FLAGS(context,%d, tmp, op_dst, op_src,%d); \n", p->type,neg); | |
900 | } | |
901 | ||
902 | static void | |
903 | test_normal_flags (p, s, opt) | |
904 | opcode_entry_type *p; | |
905 | char *s; | |
906 | int opt; | |
907 | { | |
908 | emit (" %s;\n", s); | |
909 | if (0 && opt) | |
910 | { | |
911 | emit ("context->broken_flags = TST_FLAGS;\n"); | |
912 | emit ("context->size = %d;\n", p->type); | |
913 | } | |
914 | else | |
915 | { | |
916 | emit ("TEST_NORMAL_FLAGS(context,%d, tmp); \n", p->type); | |
917 | } | |
918 | ||
919 | } | |
920 | ||
921 | static void | |
922 | optimize_normal_flags (p, s,neg) | |
923 | opcode_entry_type *p; | |
924 | char *s; | |
925 | { | |
926 | emit (" %s;\n", s); | |
927 | #if 0 | |
928 | emit ("context->broken_flags = CMP_FLAGS;\n"); | |
929 | #else | |
930 | emit ("NORMAL_FLAGS(context,%d, tmp, op_dst, op_src,%d); \n", p->type, neg); | |
931 | #endif | |
932 | } | |
933 | ||
934 | static | |
935 | void | |
936 | jp (p) | |
937 | opcode_entry_type *p; | |
938 | { | |
939 | ||
940 | emit ("if(op_cc == 8 || COND(context,op_cc)) pc = oplval_dst;\n"); | |
941 | } | |
942 | ||
943 | static void | |
944 | jr (p) | |
945 | opcode_entry_type *p; | |
946 | { | |
947 | emit ("if(op_cc == 8 || COND(context,op_cc)) pc = oplval_dst;\n"); | |
948 | } | |
949 | ||
950 | static void | |
951 | ret (p) | |
952 | opcode_entry_type *p; | |
953 | { | |
954 | emit ("if(op_cc == 8 || COND(context,op_cc))\n{\n"); | |
955 | emit ("pc = get_<ptr_mode>_<mem>_ir(context,<sp>);\n"); | |
956 | emit ("put_<ptr_mode>_reg(context,<sp>, get_<ptr_mode>_reg(context,<sp>) + <ptr_size>);\n"); | |
957 | emit ("};\n"); | |
958 | } | |
959 | ||
960 | static void | |
961 | call (p) | |
962 | opcode_entry_type *p; | |
963 | { | |
964 | emit ("put_<ptr_mode>_reg(context,<sp>,tmp = get_<ptr_mode>_reg(context,<sp>) - <ptr_size>);\n"); | |
965 | emit ("put_<ptr_mode>_<mem>_da(context,tmp, pc);\n"); | |
966 | emit ("pc = oplval_dst;\n"); | |
967 | } | |
968 | ||
969 | static void | |
970 | push (p) | |
971 | opcode_entry_type *p; | |
972 | { | |
973 | emit ("tmp = op_src;\n"); | |
974 | emit ("oplval_dst -= %d;\n", p->type / 8); | |
975 | emit ("put_<ptr_mode>_reg(context,reg_dst, oplval_dst);\n"); | |
976 | } | |
977 | ||
978 | static void | |
979 | pop (p) | |
980 | opcode_entry_type *p; | |
981 | { | |
982 | emit ("tmp = op_src;\n"); | |
983 | emit ("put_<ptr_mode>_reg(context,reg_src, oplval_src + %d);\n", p->type / 8); | |
984 | } | |
985 | ||
986 | static void | |
987 | ld (p) | |
988 | opcode_entry_type *p; | |
989 | { | |
990 | emit ("tmp = op_src;\n"); | |
991 | } | |
992 | ||
993 | static void | |
994 | sc () | |
995 | { | |
996 | emit ("support_call(context,imm_src);\n"); | |
997 | } | |
998 | ||
999 | static void | |
1000 | bpt () | |
1001 | { | |
1002 | emit ("pc -=2; \n"); | |
1003 | emit ("context->exception = SIM_BREAKPOINT;\n"); | |
1004 | } | |
1005 | ||
1006 | static void | |
1007 | ldi (p, size, inc) | |
1008 | opcode_entry_type *p; | |
1009 | int size; | |
1010 | int inc; | |
1011 | { | |
1012 | int dinc = (size / 8) * inc; | |
1013 | ||
1014 | current_size = size_name (size); | |
1015 | emit ("{ \n"); | |
1016 | emit ("int type = %s;\n", insn_4 (7)); | |
1017 | emit ("int rs = get_<ptr_mode>_reg(context,reg_src);\n"); | |
1018 | emit ("int rd = get_<ptr_mode>_reg(context,reg_dst);\n"); | |
1019 | emit ("int rr = get_word_reg(context,reg_aux_r);\n"); | |
1020 | emit ("do {\n"); | |
1021 | emit ("put_<size>_<mem>_da(context,rd, get_<size>_<mem>_da(context,rs));\n"); | |
1022 | emit ("rd += %d;\n", dinc); | |
1023 | emit ("rs += %d;\n", dinc); | |
1024 | emit ("rr --;\n"); | |
1025 | emit ("context->cycles += 9;\n"); | |
1026 | emit ("} while (!type && rr != 0 && context->exception <= 1);\n"); | |
1027 | emit ("if (context->exception>1) pc -=4;\n"); | |
1028 | emit ("put_<ptr_mode>_reg(context,reg_src, rs);\n"); | |
1029 | emit ("put_<ptr_mode>_reg(context,reg_dst, rd);\n"); | |
1030 | emit ("put_word_reg(context,reg_aux_r, rr);\n"); | |
1031 | emit ("}\n"); | |
1032 | ||
1033 | } | |
1034 | ||
1035 | static void | |
1036 | shift (p, arith) | |
1037 | opcode_entry_type *p; | |
1038 | int arith; | |
1039 | { | |
1040 | ||
1041 | /* We can't use `(char)' since char might be unsigned. | |
1042 | We can't use `(signed char)' because the compiler might be K&R. | |
1043 | This seems safe, since it only assumes that bytes are 8 bits. */ | |
1044 | emit ("op_src = (op_src << (sizeof (int) * 8 - 8)) >> (sizeof (int) * 8 - 8);\n"); | |
1045 | #if 0 | |
1046 | /* Original code: fails if characters are unsigned. */ | |
1047 | emit ("op_src = (char)op_src;\n"); | |
1048 | #endif | |
1049 | emit ("if (op_src < 0) \n"); | |
1050 | emit ("{\n"); | |
1051 | emit ("op_src = -op_src;\n"); | |
1052 | emit ("op_dst = (%s <c_size>)op_dst;\n", arith ? "" : "unsigned"); | |
1053 | emit ("tmp = (%s op_dst) >> op_src;\n", arith ? "" : "(unsigned)"); | |
1054 | emit ("context->carry = op_dst >> (op_src-1);\n", p->type); | |
1055 | emit ("}\n"); | |
1056 | emit ("else\n"); | |
1057 | emit ("{\n"); | |
1058 | emit ("tmp = op_dst << op_src;\n"); | |
1059 | emit ("context->carry = op_dst >> (%d - op_src);\n", p->type); | |
1060 | emit ("}\n"); | |
1061 | emit ("context->zero = (<c_size>)tmp == 0;\n"); | |
1062 | emit ("context->sign = (int)((<c_size>)tmp) < 0;\n"); | |
1063 | emit ("context->overflow = ((int)tmp < 0) != ((int)op_dst < 0);\n"); | |
1064 | emit ("context->cycles += 3*op_src;\n"); | |
1065 | emit ("context->broken_flags = 0;\n"); | |
1066 | ||
1067 | } | |
1068 | ||
1069 | static void | |
1070 | rotate (p, through_carry, size, left) | |
1071 | opcode_entry_type *p; | |
1072 | int through_carry; | |
1073 | int size; | |
1074 | int left; | |
1075 | { | |
1076 | ||
1077 | if (!left) | |
1078 | { | |
1079 | emit ("while (op_src--) {\n"); | |
1080 | emit ("int rotbit;\n"); | |
1081 | emit ("rotbit = op_dst & 1;\n"); | |
1082 | emit ("op_dst = ((unsigned)op_dst) >> 1;\n"); | |
1083 | ||
1084 | if (through_carry) | |
1085 | { | |
1086 | emit ("op_dst |= context->carry << %d;\n", size - 1); | |
1087 | } | |
1088 | else | |
1089 | { | |
1090 | emit ("op_dst |= rotbit << %d;\n", size - 1); | |
1091 | } | |
1092 | emit ("context->carry = rotbit;\n"); | |
1093 | emit ("}\n"); | |
1094 | } | |
1095 | else | |
1096 | { | |
1097 | emit ("while (op_src--) {\n"); | |
1098 | emit ("int rotbit;\n"); | |
1099 | ||
1100 | emit ("rotbit = (op_dst >> (%d))&1;\n", size - 1); | |
1101 | emit ("op_dst <<=1;\n"); | |
1102 | if (through_carry) | |
1103 | { | |
1104 | emit ("if (context->carry) op_dst |=1;\n"); | |
1105 | } | |
1106 | else | |
1107 | { | |
1108 | emit ("if (rotbit) op_dst |= 1;\n"); | |
1109 | } | |
1110 | emit ("context->carry = rotbit;\n"); | |
1111 | emit ("}\n"); | |
1112 | } | |
1113 | emit ("tmp = (<c_size>)op_dst;\n"); | |
1114 | emit ("context->zero = tmp == 0;\n"); | |
1115 | emit ("context->sign = (int)tmp < 0;\n"); | |
1116 | emit ("context->overflow = ((int)tmp < 0) != ((int)op_dst < 0);\n"); | |
1117 | emit ("context->cycles += 3*op_src;\n"); | |
1118 | emit ("context->broken_flags = 0;\n"); | |
1119 | ||
1120 | } | |
1121 | ||
1122 | static void | |
1123 | adiv (p) | |
1124 | opcode_entry_type *p; | |
1125 | { | |
1126 | emit ("if (op_src==0)\n"); | |
1127 | emit ("{\n"); | |
1128 | emit ("context->exception = SIM_DIV_ZERO;\n"); | |
1129 | emit ("}\n"); | |
1130 | emit ("else\n"); | |
1131 | emit ("{\n"); | |
1132 | ||
1133 | if (p->type == 32) | |
1134 | { | |
1135 | emit ("op_dst.low = (int)get_long_reg(context,reg_dst+2);\n"); | |
1136 | emit ("op_dst.high = (int)get_long_reg(context,reg_dst+0);\n"); | |
1137 | #ifdef __GNUC__ | |
1138 | emit ("tmp = (((long long)op_dst.high << 32) + (op_dst.low)) / (int)op_src;\n"); | |
1139 | #else | |
1140 | emit ("tmp = (long)op_dst.low / (int)op_src;\n"); | |
1141 | #endif | |
1142 | emit ("put_long_reg(context,reg_dst+2, tmp);\n"); | |
1143 | #ifdef __GNUC__ | |
1144 | emit ("put_long_reg(context,reg_dst, (((long long)op_dst.high << 32) + (op_dst.low)) %% (int)op_src);\n"); | |
1145 | #else | |
1146 | emit ("put_long_reg(context,reg_dst, (int)op_dst.low %% (int)op_src);\n"); | |
1147 | #endif | |
1148 | ||
1149 | emit ("context->zero = op_src == 0 || (op_dst.low==0 && op_dst.high==0);\n"); | |
1150 | } | |
1151 | else | |
1152 | { | |
1153 | emit ("tmp = (long)op_dst / (short)op_src;\n"); | |
1154 | emit ("put_word_reg(context,reg_dst+1, tmp);\n"); | |
1155 | emit ("put_word_reg(context,reg_dst, (long) op_dst %% (short)op_src);\n"); | |
1156 | emit ("context->zero = op_src == 0 || op_dst==0;\n"); | |
1157 | } | |
1158 | ||
1159 | emit ("context->sign = (int)tmp < 0;\n"); | |
1160 | emit ("context->overflow =(tmp & 0x%x) != 0;\n", | |
1161 | ~((1 << (p->type)) - 1)); | |
1162 | emit ("context->carry = (tmp & 0x%x) != 0;\n", | |
1163 | ~(1 << (p->type))); | |
1164 | ||
1165 | emit ("}\n"); | |
1166 | } | |
1167 | ||
1168 | static void | |
1169 | dobit (p) | |
1170 | opcode_entry_type *p; | |
1171 | { | |
1172 | emit("context->zero = (op_dst & (1<<op_src))==0;\n"); | |
1173 | emit("context->broken_flags = 0;\n"); | |
1174 | } | |
1175 | static void | |
1176 | doset (p, v) | |
1177 | opcode_entry_type*p; | |
1178 | int v; | |
1179 | { | |
1180 | if (v) | |
1181 | emit (" tmp = op_dst | (1<< op_src);\n"); | |
1182 | else | |
1183 | emit (" tmp = op_dst & ~(1<< op_src);\n"); | |
1184 | } | |
1185 | ||
1186 | static void | |
1187 | mult (p) | |
1188 | opcode_entry_type *p; | |
1189 | { | |
1190 | ||
1191 | if (p->type == 32) | |
1192 | { | |
1193 | emit ("op_dst.low = get_long_reg(context,reg_dst+2);\n"); | |
1194 | emit ("tmp = op_dst.low * op_src;\n"); | |
1195 | emit ("put_long_reg(context,reg_dst+2, tmp);\n"); | |
1196 | emit ("put_long_reg(context,reg_dst, 0);\n"); | |
1197 | } | |
1198 | else | |
1199 | { | |
1200 | emit ("op_dst = get_word_reg(context,reg_dst+1);\n"); | |
1201 | emit ("tmp = op_dst * op_src;\n"); | |
1202 | emit ("put_long_reg(context,reg_dst, tmp);\n"); | |
1203 | } | |
1204 | ||
1205 | emit ("context->sign = (int)tmp < 0;\n"); | |
1206 | emit ("context->overflow =0;\n"); | |
1207 | emit ("context->carry = (tmp & 0x%x) != 0;\n", ~((1 << (p->type)) - 1)); | |
1208 | emit ("context->zero = tmp == 0;\n"); | |
1209 | ||
1210 | } | |
1211 | ||
1212 | static void | |
1213 | exts (p) | |
1214 | opcode_entry_type *p; | |
1215 | { | |
1216 | /* Fetch the ls part of the src */ | |
1217 | current_size = size_name (p->type * 2); | |
1218 | ||
1219 | if (p->type == 32) | |
1220 | { | |
1221 | emit ("tmp= get_long_reg(context,reg_dst+2);\n"); | |
1222 | emit ("if (tmp & (1<<%d)) {\n", p->type - 1); | |
1223 | emit ("put_long_reg(context,reg_dst, 0xffffffff);\n"); | |
1224 | emit ("}\n"); | |
1225 | emit ("else\n"); | |
1226 | emit ("{\n"); | |
1227 | emit ("put_long_reg(context,reg_dst, 0);\n"); | |
1228 | emit ("}\n"); | |
1229 | } | |
1230 | else | |
1231 | { | |
1232 | emit ("tmp= get_<size>_reg(context,reg_dst);\n"); | |
1233 | emit ("if (tmp & (1<<%d)) {\n", p->type - 1); | |
1234 | emit ("tmp |= 0x%x;\n", ~((1 << p->type) - 1)); | |
1235 | emit ("}\n"); | |
1236 | emit ("else\n"); | |
1237 | emit ("{\n"); | |
1238 | ||
1239 | emit ("tmp &= 0x%x;\n", ((1 << p->type) - 1)); | |
1240 | emit ("}\n"); | |
1241 | emit ("put_<size>_reg(context,reg_dst, tmp);\n"); | |
1242 | } | |
1243 | } | |
1244 | doflag(on) | |
1245 | int on; | |
1246 | { | |
1247 | /* Load up the flags */ | |
1248 | emit(" COND (context, 0x0b);\n"); | |
1249 | ||
1250 | if (on) | |
1251 | emit ("{ int on =1;\n "); | |
1252 | else | |
1253 | emit ("{ int on =0;\n "); | |
1254 | ||
1255 | emit ("if (imm_src & 1)\n"); | |
1256 | emit ("PSW_OVERFLOW = on;\n"); | |
1257 | ||
1258 | emit ("if (imm_src & 2)\n"); | |
1259 | emit ("PSW_SIGN = on;\n"); | |
1260 | ||
1261 | emit ("if (imm_src & 4)\n"); | |
1262 | emit ("PSW_ZERO = on;\n"); | |
1263 | ||
1264 | emit ("if (imm_src & 8)\n"); | |
1265 | emit ("PSW_CARRY = on;\n"); | |
1266 | emit("}\n"); | |
1267 | ||
1268 | ||
1269 | } | |
1270 | /* emit code to perform operation */ | |
1271 | void | |
1272 | info_docode (p) | |
1273 | opcode_entry_type *p; | |
1274 | { | |
1275 | switch (p->opcode) | |
1276 | { | |
1277 | case OPC_clr: | |
1278 | case OPC_clrb: | |
1279 | emit ("tmp = 0;\n"); | |
1280 | break; | |
1281 | case OPC_ex: | |
1282 | case OPC_exb: | |
1283 | ||
1284 | emit ("tmp = op_src; \n"); | |
1285 | if (allregs) | |
1286 | { | |
1287 | emit ("put_<size>_reg(context,reg_src, op_dst);\n"); | |
1288 | } | |
1289 | else | |
1290 | { | |
1291 | emit ("put_<size>_mem_da(context, oplval_src, op_dst);\n"); | |
1292 | } | |
1293 | break; | |
1294 | case OPC_adc: | |
1295 | case OPC_adcb: | |
1296 | normal_flags (p, "op_src += COND(context,7);tmp = op_dst + op_src ;",0); | |
1297 | break; | |
1298 | case OPC_sbc: | |
1299 | normal_flags (p, "op_src += COND(context,7);tmp = op_dst - op_src ;",1); | |
1300 | break; | |
1301 | case OPC_nop: | |
1302 | break; | |
1303 | case OPC_com: | |
1304 | case OPC_comb: | |
1305 | test_normal_flags (p, "tmp = ~ op_dst", 1); | |
1306 | break; | |
1307 | case OPC_and: | |
1308 | case OPC_andb: | |
1309 | test_normal_flags (p, "tmp = op_dst & op_src", 1); | |
1310 | break; | |
1311 | case OPC_xor: | |
1312 | case OPC_xorb: | |
1313 | test_normal_flags (p, "tmp = op_dst ^ op_src", 1); | |
1314 | break; | |
1315 | case OPC_or: | |
1316 | case OPC_orb: | |
1317 | test_normal_flags (p, "tmp = op_dst | op_src", 1); | |
1318 | break; | |
1319 | case OPC_sla: | |
1320 | case OPC_slab: | |
1321 | case OPC_slal: | |
1322 | case OPC_sda: | |
1323 | case OPC_sdab: | |
1324 | case OPC_sdal: | |
1325 | shift (p, 1); | |
1326 | break; | |
1327 | ||
1328 | case OPC_sll: | |
1329 | case OPC_sllb: | |
1330 | case OPC_slll: | |
1331 | case OPC_sdl: | |
1332 | case OPC_sdlb: | |
1333 | case OPC_sdll: | |
1334 | shift (p, 0); | |
1335 | break; | |
1336 | case OPC_rl: | |
1337 | rotate (p, 0, 16, 1); | |
1338 | break; | |
1339 | case OPC_rlb: | |
1340 | rotate (p, 0, 8, 1); | |
1341 | break; | |
1342 | case OPC_rr: | |
1343 | rotate (p, 0, 16, 0); | |
1344 | break; | |
1345 | case OPC_rrb: | |
1346 | rotate (p, 0, 8, 0); | |
1347 | break; | |
1348 | case OPC_rrc: | |
1349 | rotate (p, 1, 16, 0); | |
1350 | break; | |
1351 | case OPC_rrcb: | |
1352 | rotate (p, 1, 8, 0); | |
1353 | break; | |
1354 | case OPC_rlc: | |
1355 | rotate (p, 1, 16, 1); | |
1356 | break; | |
1357 | case OPC_rlcb: | |
1358 | rotate (p, 1, 8, 1); | |
1359 | break; | |
1360 | ||
1361 | case OPC_extsb: | |
1362 | case OPC_exts: | |
1363 | case OPC_extsl: | |
1364 | exts (p); | |
1365 | break; | |
1366 | case OPC_add: | |
1367 | case OPC_addb: | |
1368 | case OPC_addl: | |
1369 | case OPC_inc: | |
1370 | case OPC_incb: | |
1371 | optimize_normal_flags (p, "tmp = op_dst + op_src",0); | |
1372 | break; | |
1373 | case OPC_testb: | |
1374 | case OPC_test: | |
1375 | case OPC_testl: | |
1376 | test_normal_flags (p, "tmp = op_dst", 0); | |
1377 | break; | |
1378 | case OPC_cp: | |
1379 | case OPC_cpb: | |
1380 | case OPC_cpl: | |
1381 | normal_flags (p, "tmp = op_dst - op_src",1); | |
1382 | break; | |
1383 | case OPC_negb: | |
1384 | case OPC_neg: | |
1385 | emit ("{\n"); | |
1386 | emit ("int op_src = -op_dst;\n"); | |
1387 | emit ("op_dst = 0;\n"); | |
1388 | optimize_normal_flags (p, "tmp = op_dst + op_src;\n",1); | |
1389 | emit ("}"); | |
1390 | break; | |
1391 | ||
1392 | case OPC_sub: | |
1393 | case OPC_subb: | |
1394 | case OPC_subl: | |
1395 | case OPC_dec: | |
1396 | case OPC_decb: | |
1397 | optimize_normal_flags (p, "tmp = op_dst - op_src",1); | |
1398 | break; | |
1399 | case OPC_bpt: | |
1400 | bpt (); | |
1401 | break; | |
1402 | case OPC_jr: | |
1403 | jr (p); | |
1404 | break; | |
1405 | case OPC_sc: | |
1406 | sc (); | |
1407 | break; | |
1408 | case OPC_jp: | |
1409 | jp (p); | |
1410 | break; | |
1411 | case OPC_ret: | |
1412 | ret (p); | |
1413 | break; | |
1414 | case OPC_call: | |
1415 | call (p); | |
1416 | break; | |
1417 | case OPC_tcc: | |
1418 | case OPC_tccb: | |
1419 | emit ("if(op_cc == 8 || COND(context,op_cc)) put_word_reg(context,reg_dst, 1);\n"); | |
1420 | break; | |
1421 | case OPC_lda: | |
1422 | emit ("tmp = oplval_src; \n"); | |
1423 | /*(((oplval_src) & 0xff0000) << 8) | (oplval_src & 0xffff); \n");*/ | |
1424 | break; | |
1425 | case OPC_ldk: | |
1426 | case OPC_ld: | |
1427 | ||
1428 | case OPC_ldb: | |
1429 | case OPC_ldl: | |
1430 | ld (p); | |
1431 | break; | |
1432 | case OPC_ldib: | |
1433 | ldi (p, 8, 1); | |
1434 | break; | |
1435 | case OPC_ldi: | |
1436 | ldi (p, 16, 1); | |
1437 | break; | |
1438 | ||
1439 | case OPC_lddb: | |
1440 | ldi (p, 8, -1); | |
1441 | break; | |
1442 | case OPC_ldd: | |
1443 | ldi (p, 16, -1); | |
1444 | break; | |
1445 | ||
1446 | case OPC_push: | |
1447 | case OPC_pushl: | |
1448 | push (p); | |
1449 | break; | |
1450 | ||
1451 | case OPC_div: | |
1452 | case OPC_divl: | |
1453 | adiv (p); | |
1454 | break; | |
1455 | case OPC_mult: | |
1456 | case OPC_multl: | |
1457 | mult (p); | |
1458 | break; | |
1459 | case OPC_pop: | |
1460 | case OPC_popl: | |
1461 | pop (p); | |
1462 | break; | |
1463 | case OPC_set: | |
1464 | doset (p,1); | |
1465 | break; | |
1466 | case OPC_res: | |
1467 | doset (p,0); | |
1468 | break; | |
1469 | case OPC_bit: | |
1470 | dobit(p); | |
1471 | break; | |
1472 | case OPC_resflg: | |
1473 | doflag(0); | |
1474 | break; | |
1475 | case OPC_setflg: | |
1476 | doflag(1); | |
1477 | break; | |
1478 | default: | |
1479 | ||
1480 | emit ("tmp = fail(context,%d);\n", p->opcode); | |
1481 | break; | |
1482 | } | |
1483 | } | |
1484 | ||
1485 | /* emit code to store result in calculated lvalue */ | |
1486 | ||
1487 | void | |
1488 | info_store (p) | |
1489 | opcode_entry_type *p; | |
1490 | { | |
1491 | unsigned int *i = p->arg_info; | |
1492 | ||
1493 | while (*i) | |
1494 | { | |
1495 | current_name = reg_n (*i); | |
1496 | current_size = size_name (p->type); | |
1497 | ||
1498 | if (IS_DST (*i)) | |
1499 | { | |
1500 | switch (*i & CLASS_MASK) | |
1501 | { | |
1502 | case CLASS_PR: | |
1503 | emit ("put_<ptr_mode>_reg(context,reg_<name>, tmp);\n"); | |
1504 | break; | |
1505 | case CLASS_REG_LONG: | |
1506 | case CLASS_REG_WORD: | |
1507 | case CLASS_REG_BYTE: | |
1508 | ||
1509 | emit ("put_<size>_reg(context,reg_<name>,tmp);\n"); | |
1510 | break; | |
1511 | case CLASS_X: | |
1512 | case CLASS_IR: | |
1513 | case CLASS_DA: | |
1514 | case CLASS_BX: | |
1515 | case CLASS_BA: | |
1516 | ||
1517 | emit ("put_<size>_<mem>_da(context,oplval_<name>, tmp);\n"); | |
1518 | break; | |
1519 | case CLASS_IMM: | |
1520 | break; | |
1521 | default: | |
1522 | emit ("abort(); "); | |
1523 | break; | |
1524 | } | |
1525 | ||
1526 | } | |
1527 | i++; | |
1528 | } | |
1529 | } | |
1530 | ||
1531 | static | |
1532 | void | |
1533 | mangle (p, shortcut, value) | |
1534 | opcode_entry_type *p; | |
1535 | int shortcut; | |
1536 | int value; | |
1537 | { | |
1538 | int nostore = 0; | |
1539 | int extra; | |
1540 | int getdst = 1; | |
1541 | int before = 0; | |
1542 | int nosrc = 0; | |
1543 | ||
1544 | emit ("/\052 %s \052/\n", p->nicename); | |
1545 | if (shortcut) | |
1546 | { | |
1547 | emit ("int <fop>_%04x(context,pc)\n", value); | |
1548 | } | |
1549 | else | |
1550 | { | |
1551 | emit ("int <fop>_%d(context,pc,iwords0)\n", p->idx); | |
1552 | emit ("int iwords0;\n"); | |
1553 | } | |
1554 | emit ("sim_state_type *context;\n"); | |
1555 | emit ("int pc;\n"); | |
1556 | emit ("{\n"); | |
1557 | emit ("register unsigned int tmp;\n"); | |
1558 | if (shortcut) | |
1559 | { | |
1560 | emit ("register unsigned int iwords0 = 0x%x;\n", value); | |
1561 | } | |
1562 | ||
1563 | /* work out how much bigger this opcode could be because it's large | |
1564 | model */ | |
1565 | if (BIG) | |
1566 | { | |
1567 | int i; | |
1568 | ||
1569 | extra = 0; | |
1570 | for (i = 0; i < 4; i++) | |
1571 | { | |
1572 | if ((p->arg_info[i] & CLASS_MASK) == CLASS_DA | |
1573 | || (p->arg_info[i] & CLASS_MASK) == CLASS_X) | |
1574 | extra += 2; | |
1575 | } | |
1576 | } | |
1577 | else | |
1578 | { | |
1579 | extra = 0; | |
1580 | } | |
1581 | printf (" /* Length %d */ \n", p->length + extra); | |
1582 | switch (p->length + extra) | |
1583 | { | |
1584 | case 2: | |
1585 | emit ("pc += 2\n;"); | |
1586 | break; | |
1587 | case 4: | |
1588 | emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n"); | |
1589 | emit ("pc += 4;\n"); | |
1590 | break; | |
1591 | case 6: | |
1592 | ||
1593 | emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n"); | |
1594 | emit ("register unsigned int iwords2 = get_word_mem_da(context,pc+4);\n"); | |
1595 | emit ("pc += 6;\n"); | |
1596 | break; | |
1597 | case 8: | |
1598 | emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n"); | |
1599 | emit ("register unsigned int iwords2 = get_word_mem_da(context,pc+4);\n"); | |
1600 | emit ("register unsigned int iwords3 = get_word_mem_da(context,pc+6);\n"); | |
1601 | emit ("pc += 8;\n"); | |
1602 | break; | |
1603 | default: | |
1604 | break; | |
1605 | ||
1606 | } | |
1607 | emit ("context->cycles += %d;\n", p->cycles); | |
1608 | ||
1609 | emit ("{\n"); | |
1610 | info_args (p); | |
1611 | info_special (p, &getdst, &nostore, &before, &nosrc); | |
1612 | ||
1613 | info_lvals (p); | |
1614 | if (!nosrc) | |
1615 | { | |
1616 | info_fetch (p, getdst); | |
1617 | } | |
1618 | ||
1619 | if (before) | |
1620 | { | |
1621 | info_docode (p); | |
1622 | } | |
1623 | else | |
1624 | { | |
1625 | info_docode (p); | |
1626 | } | |
1627 | if (!nostore) | |
1628 | info_store (p); | |
1629 | emit ("}\n"); | |
1630 | emit ("return pc;\n"); | |
1631 | emit ("}\n"); | |
1632 | } | |
1633 | ||
1634 | void | |
1635 | static | |
1636 | one_instruction (i) | |
1637 | int i; | |
1638 | { | |
1639 | /* find the table entry */ | |
1640 | opcode_entry_type *p = z8k_table + i; | |
1641 | ||
1642 | if (!p) | |
1643 | return; | |
1644 | mangle (p, 0, 0); | |
1645 | } | |
1646 | ||
1647 | void | |
1648 | add_to_list (ptr, value) | |
1649 | struct opcode_value **ptr; | |
1650 | int value; | |
1651 | { | |
1652 | struct opcode_value *prev; | |
1653 | ||
1654 | prev = *ptr; | |
1655 | *ptr = (struct opcode_value *) malloc (sizeof (struct opcode_value)); | |
1656 | ||
1657 | (*ptr)->n = value; | |
1658 | (*ptr)->next = prev; | |
1659 | } | |
1660 | ||
1661 | void | |
1662 | build_list (i) | |
1663 | int i; | |
1664 | { | |
1665 | opcode_entry_type *p = lookup_inst (i); | |
1666 | ||
1667 | if (!p) | |
1668 | return; | |
1669 | add_to_list (&list[p->idx], i); | |
1670 | } | |
1671 | ||
1672 | int | |
1673 | main (ac, av) | |
1674 | int ac; | |
1675 | char **av; | |
1676 | { | |
1677 | int i; | |
1678 | int needcomma = 0; | |
1679 | ||
1680 | makelist = 0; | |
1681 | ||
1682 | for (i = 1; i < ac; i++) | |
1683 | { | |
1684 | if (strcmp (av[i], "-m") == 0) | |
1685 | makelist = 1; | |
1686 | if (strcmp (av[i], "-1") == 0) | |
1687 | file = 1; | |
1688 | if (strcmp (av[i], "-2") == 0) | |
1689 | file = 2; | |
1690 | if (strcmp (av[i], "-3") == 0) | |
1691 | file = 3; | |
1692 | if (strcmp (av[i], "-b3") == 0) | |
1693 | { | |
1694 | file = 3; | |
1695 | BIG = 1; | |
1696 | } | |
1697 | ||
1698 | } | |
1699 | if (makelist) | |
1700 | { | |
1701 | ||
1702 | int i; | |
1703 | needcomma = 0; | |
1704 | printf ("short int z8k_inv_list[] = {\n"); | |
1705 | ||
1706 | for (i = 0; i < 1 << 16; i++) | |
1707 | { | |
1708 | opcode_entry_type *p = lookup_inst (i); | |
1709 | ||
1710 | if(needcomma) | |
1711 | printf(","); | |
1712 | if ((i & 0xf) == 0) | |
1713 | printf ("\n"); | |
1714 | ||
1715 | #if 0 | |
1716 | printf ("\n /*%04x %s */", i, p ? p->nicename : ""); | |
1717 | #endif | |
1718 | ||
1719 | if (!p) | |
1720 | { | |
1721 | printf ("-1"); | |
1722 | } | |
1723 | else | |
1724 | { | |
1725 | printf ("%d", p->idx); | |
1726 | } | |
1727 | ||
1728 | if ((i & 0x3f) == 0 && DIRTY_HACK) | |
1729 | { | |
1730 | printf ("\n#ifdef __GNUC__\n"); | |
1731 | printf ("};\n"); | |
1732 | printf("short int int_list%d[] = {\n", i); | |
1733 | printf ("#else\n"); | |
1734 | printf (",\n"); | |
1735 | printf ("#endif\n"); | |
1736 | needcomma = 0; | |
1737 | } | |
1738 | else | |
1739 | needcomma = 1; | |
1740 | ||
1741 | } | |
1742 | printf ("};\n"); | |
1743 | return 1; | |
1744 | } | |
1745 | ||
1746 | /* First work out which opcodes use which bit patterns, | |
1747 | build a list of all matching bit pattens */ | |
1748 | for (i = 0; i < 1 << 16; i++) | |
1749 | { | |
1750 | build_list (i); | |
1751 | } | |
1752 | #if DUMP_LIST | |
1753 | for (i = 0; i < NOPS; i++) | |
1754 | { | |
1755 | struct opcode_value *p; | |
1756 | ||
1757 | printf ("%d,", i); | |
1758 | p = list[i]; | |
1759 | while (p) | |
1760 | { | |
1761 | printf (" %04x,", p->n); | |
1762 | p = p->next; | |
1763 | } | |
1764 | printf ("-1\n"); | |
1765 | } | |
1766 | ||
1767 | #endif | |
1768 | ||
1769 | if (file == 1) | |
1770 | { | |
1771 | extern int quick[]; | |
1772 | ||
1773 | /* Do the shortcuts */ | |
1774 | printf (" /* SHORTCUTS */\n"); | |
1775 | for (i = 0; quick[i]; i++) | |
1776 | { | |
1777 | int t = quick[i]; | |
1778 | ||
1779 | mangle (z8k_table + z8k_inv_list[t], | |
1780 | 1, | |
1781 | t); | |
1782 | } | |
1783 | } | |
1784 | if (file == 3) | |
1785 | { | |
1786 | printf (" /* NOT -SHORTCUTS */\n"); | |
1787 | for (i = 0; i < NOPS; i++) | |
1788 | { | |
1789 | if (list[i]) | |
1790 | { | |
1791 | one_instruction (i); | |
1792 | } | |
1793 | else | |
1794 | { | |
1795 | emit ("int <fop>_%d(context,pc)\n", i); | |
1796 | printf ("sim_state_type *context;\n"); | |
1797 | printf ("int pc;\n"); | |
1798 | emit ("{ <fop>_bad1();return pc; }\n"); | |
1799 | } | |
1800 | } | |
1801 | emit ("int <fop>_bad() ;\n"); | |
1802 | ||
1803 | /* Write the jump table */ | |
1804 | emit ("int (*(<fop>_table[]))() = {"); | |
1805 | needcomma = 0; | |
1806 | for (i = 0; i < NOPS; i++) | |
1807 | { | |
1808 | if (needcomma) | |
1809 | printf (","); | |
1810 | emit ("<fop>_%d\n", i); | |
1811 | needcomma = 1; | |
1812 | if ((i & 0x3f) == 0 && DIRTY_HACK) | |
1813 | { | |
1814 | printf ("#ifdef __GNUC__\n"); | |
1815 | printf ("};\n"); | |
1816 | emit ("int (*(<fop>_table%d[]))() = {\n", i); | |
1817 | printf ("#else\n"); | |
1818 | printf (",\n"); | |
1819 | printf ("#endif\n"); | |
1820 | needcomma = 0; | |
1821 | } | |
1822 | } | |
1823 | emit ("};\n"); | |
1824 | } | |
1825 | ||
1826 | if (file == 2) | |
1827 | { | |
1828 | extern int quick[]; | |
1829 | /* Static - since it's too be to be automatic on the apollo */ | |
1830 | static int big[64 * 1024]; | |
1831 | ||
1832 | for (i = 0; i < 64 * 1024; i++) | |
1833 | big[i] = 0; | |
1834 | ||
1835 | for (i = 0; quick[i]; i++) | |
1836 | { | |
1837 | #if 0 | |
1838 | ||
1839 | printf ("extern int <fop>_%04x();\n", quick[i]); | |
1840 | #endif | |
1841 | ||
1842 | big[quick[i]] = 1; | |
1843 | } | |
1844 | ||
1845 | for (i = 0; i < NOPS; i++) | |
1846 | { | |
1847 | #if 0 | |
1848 | printf ("extern int fop_%d();\n", i); | |
1849 | #endif | |
1850 | } | |
1851 | #if 0 | |
1852 | printf ("extern int fop_bad();\n"); | |
1853 | #endif | |
1854 | printf ("struct op_info op_info_table[] = {\n"); | |
1855 | for (i = 0; i < 1 << 16; i++) | |
1856 | { | |
1857 | int inv = z8k_inv_list[i]; | |
1858 | opcode_entry_type *p = z8k_table + inv; | |
1859 | ||
1860 | if (needcomma) | |
1861 | printf (","); | |
1862 | #if 0 | |
1863 | if (big[i]) | |
1864 | { | |
1865 | printf ("<fop>_%04x", i); | |
1866 | } | |
1867 | else | |
1868 | #endif | |
1869 | if (inv >= 0) | |
1870 | { | |
1871 | printf ("%d", inv); | |
1872 | } | |
1873 | else | |
1874 | printf ("400"); | |
1875 | if (inv >= 0) | |
1876 | { | |
1877 | printf (" /* %04x %s */\n", i, p->nicename); | |
1878 | } | |
1879 | else | |
1880 | { | |
1881 | printf ("\n"); | |
1882 | } | |
1883 | needcomma = 1; | |
1884 | if ((i & 0x3f) == 0 && DIRTY_HACK) | |
1885 | { | |
1886 | printf ("#ifdef __GNUC__\n"); | |
1887 | printf ("}; \n"); | |
1888 | printf ("struct op_info op_info_table%d[] = {\n", i); | |
1889 | printf ("#else\n"); | |
1890 | printf (",\n"); | |
1891 | ||
1892 | printf ("#endif\n"); | |
1893 | needcomma = 0; | |
1894 | } | |
1895 | } | |
1896 | printf ("};\n"); | |
1897 | ||
1898 | } | |
1899 | return 0; | |
1900 | } | |
1901 | ||
1902 | char * | |
1903 | insn_ptr (n) | |
1904 | int n; | |
1905 | { | |
1906 | if (BIG) | |
1907 | { | |
1908 | abort (); | |
1909 | } | |
1910 | ||
1911 | switch (n) | |
1912 | { | |
1913 | case 4: | |
1914 | return "iwords_1"; | |
1915 | default: | |
1916 | return "fail(context,123)"; | |
1917 | } | |
1918 | } | |
1919 | ||
1920 | /* work out if the opcode only wants lvalues */ | |
1921 | int | |
1922 | lvalue (p) | |
1923 | opcode_entry_type *p; | |
1924 | { | |
1925 | switch (p->opcode) | |
1926 | { | |
1927 | case OPC_lda: | |
1928 | return 1; | |
1929 | case OPC_call: | |
1930 | case OPC_jp: | |
1931 | return 1; | |
1932 | default: | |
1933 | return 0; | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | int | |
1938 | info_len_in_words (o) | |
1939 | opcode_entry_type *o; | |
1940 | { | |
1941 | unsigned int *p = o->byte_info; | |
1942 | int nibs = 0; | |
1943 | ||
1944 | while (*p) | |
1945 | { | |
1946 | switch (*p & CLASS_MASK) | |
1947 | { | |
d1b0a5b4 | 1948 | case CLASS_IGNORE: |
c906108c SS |
1949 | case CLASS_BIT: |
1950 | case CLASS_REGN0: | |
1951 | case CLASS_REG: | |
1952 | case CLASS_01II: | |
1953 | case CLASS_00II: | |
1954 | nibs++; | |
1955 | break; | |
1956 | case CLASS_ADDRESS: | |
1957 | nibs += SIZE_ADDRESS; | |
1958 | break; | |
1959 | case CLASS_IMM: | |
1960 | switch (*p & ~CLASS_MASK) | |
1961 | { | |
1962 | case ARG_IMM16: | |
1963 | nibs += 4; | |
1964 | break; | |
1965 | case ARG_IMM32: | |
1966 | nibs += 8; | |
1967 | break; | |
1968 | case ARG_IMM2: | |
1969 | case ARG_IMM4: | |
d1b0a5b4 | 1970 | case ARG_NIM4: |
c906108c SS |
1971 | case ARG_IMM4M1: |
1972 | case ARG_IMM_1: | |
1973 | case ARG_IMM_2: | |
1974 | case ARG_IMMNMINUS1: | |
1975 | nibs++; | |
1976 | break; | |
1977 | case ARG_NIM8: | |
1978 | ||
1979 | case ARG_IMM8: | |
1980 | nibs += 2; | |
1981 | break; | |
1982 | default: | |
1983 | abort (); | |
1984 | } | |
1985 | break; | |
1986 | case CLASS_DISP: | |
1987 | switch (*p & ~CLASS_MASK) | |
1988 | { | |
1989 | case ARG_DISP16: | |
1990 | nibs += 4; | |
1991 | break; | |
1992 | case ARG_DISP12: | |
1993 | nibs += 3; | |
1994 | break; | |
1995 | case ARG_DISP8: | |
1996 | nibs += 2; | |
1997 | break; | |
1998 | default: | |
1999 | abort (); | |
2000 | } | |
2001 | break; | |
2002 | case CLASS_0DISP7: | |
2003 | case CLASS_1DISP7: | |
2004 | case CLASS_DISP8: | |
2005 | nibs += 2; | |
2006 | break; | |
2007 | case CLASS_BIT_1OR2: | |
2008 | case CLASS_0CCC: | |
2009 | case CLASS_1CCC: | |
2010 | case CLASS_CC: | |
2011 | nibs++; | |
2012 | break; | |
2013 | default: | |
2014 | emit ("don't know %x\n", *p); | |
2015 | } | |
2016 | p++; | |
2017 | } | |
2018 | ||
2019 | return nibs / 4; /* return umber of words */ | |
2020 | } |