home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / sim / z8k / writecode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-18  |  37.3 KB  |  1,971 lines

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