home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / genemit.c < prev    next >
C/C++ Source or Header  |  1991-06-03  |  19KB  |  740 lines

  1. /* Generate code from machine description to emit insns as rtl.
  2.    Copyright (C) 1987-1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "obstack.h"
  25.  
  26. struct obstack obstack;
  27. struct obstack *rtl_obstack = &obstack;
  28.  
  29. #define obstack_chunk_alloc xmalloc
  30. #define obstack_chunk_free free
  31. extern int xmalloc ();
  32. extern void free ();
  33.  
  34. void fatal ();
  35. void fancy_abort ();
  36.  
  37. int max_opno;
  38. int max_dup_opno;
  39. int register_constraints;
  40. int insn_code_number;
  41. int insn_index_number;
  42.  
  43. /* Data structure for recording the patterns of insns that have CLOBBERs.
  44.    We use this to output a function that adds these CLOBBERs to a 
  45.    previously-allocated PARALLEL expression.  */
  46.  
  47. struct clobber_pat
  48. {
  49.   int code_number;        /* Counts only insns.  */
  50.   rtx pattern;
  51.   int first_clobber;
  52.   struct clobber_pat *next;
  53. } *clobber_list;
  54.  
  55. #define max(a, b) ((a) > (b) ? (a) : (b))
  56.  
  57. void
  58. max_operand_1 (x)
  59.      rtx x;
  60. {
  61.   register RTX_CODE code;
  62.   register int i;
  63.   register int len;
  64.   register char *fmt;
  65.  
  66.   if (x == 0)
  67.     return;
  68.  
  69.   code = GET_CODE (x);
  70.  
  71.   if (code == MATCH_OPERAND && XSTR (x, 2) != 0 && *XSTR (x, 2) != '\0')
  72.     register_constraints = 1;
  73.   if (code == MATCH_SCRATCH && XSTR (x, 1) != 0 && *XSTR (x, 1) != '\0')
  74.     register_constraints = 1;
  75.   if (code == MATCH_OPERAND || code == MATCH_OPERATOR
  76.       || code == MATCH_PARALLEL)
  77.     max_opno = max (max_opno, XINT (x, 0));
  78.   if (code == MATCH_DUP || code == MATCH_OP_DUP)
  79.     max_dup_opno = max (max_dup_opno, XINT (x, 0));
  80.  
  81.   fmt = GET_RTX_FORMAT (code);
  82.   len = GET_RTX_LENGTH (code);
  83.   for (i = 0; i < len; i++)
  84.     {
  85.       if (fmt[i] == 'e' || fmt[i] == 'u')
  86.     max_operand_1 (XEXP (x, i));
  87.       else if (fmt[i] == 'E')
  88.     {
  89.       int j;
  90.       for (j = 0; j < XVECLEN (x, i); j++)
  91.         max_operand_1 (XVECEXP (x, i, j));
  92.     }
  93.     }
  94. }
  95.  
  96. int
  97. max_operand_vec (insn, arg)
  98.      rtx insn;
  99.      int arg;
  100. {
  101.   register int len = XVECLEN (insn, arg);
  102.   register int i;
  103.  
  104.   max_opno = -1;
  105.   max_dup_opno = -1;
  106.  
  107.   for (i = 0; i < len; i++)
  108.     max_operand_1 (XVECEXP (insn, arg, i));
  109.  
  110.   return max_opno + 1;
  111. }
  112.  
  113. void
  114. print_code (code)
  115.      RTX_CODE code;
  116. {
  117.   register char *p1;
  118.   for (p1 = GET_RTX_NAME (code); *p1; p1++)
  119.     {
  120.       if (*p1 >= 'a' && *p1 <= 'z')
  121.     putchar (*p1 + 'A' - 'a');
  122.       else
  123.     putchar (*p1);
  124.     }
  125. }
  126.  
  127. /* Print a C expression to construct an RTX just like X,
  128.    substituting any operand references appearing within.  */
  129.  
  130. void
  131. gen_exp (x)
  132.      rtx x;
  133. {
  134.   register RTX_CODE code;
  135.   register int i;
  136.   register int len;
  137.   register char *fmt;
  138.  
  139.   if (x == 0)
  140.     {
  141.       printf ("0");
  142.       return;
  143.     }
  144.  
  145.   code = GET_CODE (x);
  146.  
  147.   switch (code)
  148.     {
  149.     case MATCH_OPERAND:
  150.     case MATCH_DUP:
  151.       printf ("operand%d", XINT (x, 0));
  152.       return;
  153.  
  154.     case MATCH_OP_DUP:
  155.       printf ("gen_rtx (GET_CODE (operand%d), GET_MODE (operand%d)",
  156.           XINT (x, 0), XINT (x, 0));
  157.       for (i = 0; i < XVECLEN (x, 1); i++)
  158.     {
  159.       printf (",\n\t\t");
  160.       gen_exp (XVECEXP (x, 1, i));
  161.     }
  162.       printf (")");
  163.       return;
  164.  
  165.     case MATCH_OPERATOR:
  166.       printf ("gen_rtx (GET_CODE (operand%d)", XINT (x, 0));
  167.       printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
  168.       for (i = 0; i < XVECLEN (x, 2); i++)
  169.     {
  170.       printf (",\n\t\t");
  171.       gen_exp (XVECEXP (x, 2, i));
  172.     }
  173.       printf (")");
  174.       return;
  175.  
  176.     case MATCH_PARALLEL:
  177.       printf ("operand%d", XINT (x, 0));
  178.       return;
  179.  
  180.     case MATCH_SCRATCH:
  181.       printf ("gen_rtx (SCRATCH, %smode, 0)", GET_MODE_NAME (GET_MODE (x)));
  182.       return;
  183.  
  184.     case ADDRESS:
  185.       fatal ("ADDRESS expression code used in named instruction pattern");
  186.  
  187.     case PC:
  188.       printf ("pc_rtx");
  189.       return;
  190.  
  191.     case CC0:
  192.       printf ("cc0_rtx");
  193.       return;
  194.  
  195.     case CONST_INT:
  196.       if (INTVAL (x) == 0)
  197.     {
  198.       printf ("const0_rtx");
  199.       return;
  200.     }
  201.       if (INTVAL (x) == 1)
  202.     {
  203.       printf ("const1_rtx");
  204.       return;
  205.     }
  206.       if (INTVAL (x) == -1)
  207.     {
  208.       printf ("constm1_rtx");
  209.       return;
  210.     }
  211.       if (INTVAL (x) == STORE_FLAG_VALUE)
  212.     {
  213.       printf ("const_true_rtx");
  214.       return;
  215.     }
  216.     }
  217.  
  218.   printf ("gen_rtx (");
  219.   print_code (code);
  220.   printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
  221.  
  222.   fmt = GET_RTX_FORMAT (code);
  223.   len = GET_RTX_LENGTH (code);
  224.   for (i = 0; i < len; i++)
  225.     {
  226.       if (fmt[i] == '0')
  227.     break;
  228.       printf (", ");
  229.       if (fmt[i] == 'e' || fmt[i] == 'u')
  230.     gen_exp (XEXP (x, i));
  231.       else if (fmt[i] == 'i')
  232.     printf ("%d", XINT (x, i));
  233.       else if (fmt[i] == 's')
  234.     printf ("\"%s\"", XSTR (x, i));
  235.       else if (fmt[i] == 'E')
  236.     {
  237.       int j;
  238.       printf ("gen_rtvec (%d", XVECLEN (x, i));
  239.       for (j = 0; j < XVECLEN (x, i); j++)
  240.         {
  241.           printf (",\n\t\t");
  242.           gen_exp (XVECEXP (x, i, j));
  243.         }
  244.       printf (")");
  245.     }
  246.       else
  247.     abort ();
  248.     }
  249.   printf (")");
  250. }  
  251.  
  252. /* Generate the `gen_...' function for a DEFINE_INSN.  */
  253.  
  254. void
  255. gen_insn (insn)
  256.      rtx insn;
  257. {
  258.   int operands;
  259.   register int i;
  260.  
  261.   /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
  262.      registers or MATCH_SCRATCHes.  If so, store away the information for
  263.      later. */
  264.  
  265.   if (XVEC (insn, 1))
  266.     {
  267.       for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
  268.     if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER
  269.         || (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != REG
  270.         && GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH))
  271.       break;
  272.  
  273.       if (i != XVECLEN (insn, 1) - 1)
  274.     {
  275.       register struct clobber_pat *new
  276.         = (struct clobber_pat *) xmalloc (sizeof (struct clobber_pat));
  277.       
  278.       new->code_number = insn_code_number;
  279.       new->pattern = insn;
  280.       new->first_clobber = i + 1;
  281.       new->next = clobber_list;
  282.       clobber_list = new;
  283.     }
  284.     }
  285.  
  286.   /* Don't mention instructions whose names are the null string.
  287.      They are in the machine description just to be recognized.  */
  288.   if (strlen (XSTR (insn, 0)) == 0)
  289.     return;
  290.  
  291.   /* Find out how many operands this function has,
  292.      and also whether any of them have register constraints.  */
  293.   register_constraints = 0;
  294.   operands = max_operand_vec (insn, 1);
  295.   if (max_dup_opno >= operands)
  296.     fatal ("match_dup operand number has no match_operand");
  297.  
  298.   /* Output the function name and argument declarations.  */
  299.   printf ("rtx\ngen_%s (", XSTR (insn, 0));
  300.   for (i = 0; i < operands; i++)
  301.     printf (i ? ", operand%d" : "operand%d", i);
  302.   printf (")\n");
  303.   for (i = 0; i < operands; i++)
  304.     printf ("     rtx operand%d;\n", i);
  305.   printf ("{\n");
  306.  
  307.   /* Output code to construct and return the rtl for the instruction body */
  308.  
  309.   if (XVECLEN (insn, 1) == 1)
  310.     {
  311.       printf ("  return ");
  312.       gen_exp (XVECEXP (insn, 1, 0));
  313.       printf (";\n}\n\n");
  314.     }
  315.   else
  316.     {
  317.       printf ("  return gen_rtx (PARALLEL, VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
  318.       for (i = 0; i < XVECLEN (insn, 1); i++)
  319.     {
  320.       printf (",\n\t\t");
  321.       gen_exp (XVECEXP (insn, 1, i));
  322.     }
  323.       printf ("));\n}\n\n");
  324.     }
  325. }
  326.  
  327. /* Generate the `gen_...' function for a DEFINE_EXPAND.  */
  328.  
  329. void
  330. gen_expand (expand)
  331.      rtx expand;
  332. {
  333.   int operands;
  334.   register int i;
  335.  
  336.   if (strlen (XSTR (expand, 0)) == 0)
  337.     fatal ("define_expand lacks a name");
  338.   if (XVEC (expand, 1) == 0)
  339.     fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
  340.  
  341.   /* Find out how many operands this function has,
  342.      and also whether any of them have register constraints.  */
  343.   register_constraints = 0;
  344.  
  345.   operands = max_operand_vec (expand, 1);
  346.  
  347.   /* Output the function name and argument declarations.  */
  348.   printf ("rtx\ngen_%s (", XSTR (expand, 0));
  349.   for (i = 0; i < operands; i++)
  350.     printf (i ? ", operand%d" : "operand%d", i);
  351.   printf (")\n");
  352.   for (i = 0; i < operands; i++)
  353.     printf ("     rtx operand%d;\n", i);
  354.   printf ("{\n");
  355.  
  356.   /* If we don't have any C code to write, only one insn is being written,
  357.      and no MATCH_DUPs are present, we can just return the desired insn
  358.      like we do for a DEFINE_INSN.  This saves memory.  */
  359.   if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
  360.       && operands > max_dup_opno
  361.       && XVECLEN (expand, 1) == 1)
  362.     {
  363.       printf ("  return ");
  364.       gen_exp (XVECEXP (expand, 1, 0));
  365.       printf (";\n}\n\n");
  366.       return;
  367.     }
  368.  
  369.   /* For each operand referred to only with MATCH_DUPs,
  370.      make a local variable.  */
  371.   for (i = operands; i <= max_dup_opno; i++)
  372.     printf ("  rtx operand%d;\n", i);
  373.   if (operands > 0 || max_dup_opno >= 0)
  374.     printf ("  rtx operands[%d];\n", max (operands, max_dup_opno + 1));
  375.   printf ("  rtx _val;\n");
  376.   printf ("  start_sequence ();\n");
  377.  
  378.   /* The fourth operand of DEFINE_EXPAND is some code to be executed
  379.      before the actual construction.
  380.      This code expects to refer to `operands'
  381.      just as the output-code in a DEFINE_INSN does,
  382.      but here `operands' is an automatic array.
  383.      So copy the operand values there before executing it.  */
  384.   if (XSTR (expand, 3) && *XSTR (expand, 3))
  385.     {
  386.       /* Output code to copy the arguments into `operands'.  */
  387.       for (i = 0; i < operands; i++)
  388.     printf ("  operands[%d] = operand%d;\n", i, i);
  389.  
  390.       /* Output the special code to be executed before the sequence
  391.      is generated.  */
  392.       printf ("%s\n", XSTR (expand, 3));
  393.  
  394.       /* Output code to copy the arguments back out of `operands'
  395.      (unless we aren't going to use them at all).  */
  396.       if (XVEC (expand, 1) != 0)
  397.     {
  398.       for (i = 0; i < operands; i++)
  399.         printf ("  operand%d = operands[%d];\n", i, i);
  400.       for (; i <= max_dup_opno; i++)
  401.         printf ("  operand%d = operands[%d];\n", i, i);
  402.     }
  403.     }
  404.  
  405.   /* Output code to construct the rtl for the instruction bodies.
  406.      Use emit_insn to add them to the sequence being accumulated.
  407.      But don't do this if the user's code has set `no_more' nonzero.  */
  408.  
  409.   for (i = 0; i < XVECLEN (expand, 1); i++)
  410.     {
  411.       rtx next = XVECEXP (expand, 1, i);
  412.       if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
  413.       || (GET_CODE (next) == PARALLEL
  414.           && GET_CODE (XVECEXP (next, 0, 0)) == SET
  415.           && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
  416.       || GET_CODE (next) == RETURN)
  417.     printf ("  emit_jump_insn (");
  418.       else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
  419.            || GET_CODE (next) == CALL
  420.            || (GET_CODE (next) == PARALLEL
  421.            && GET_CODE (XVECEXP (next, 0, 0)) == SET
  422.            && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
  423.            || (GET_CODE (next) == PARALLEL
  424.            && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
  425.     printf ("  emit_call_insn (");
  426.       else if (GET_CODE (next) == CODE_LABEL)
  427.     printf ("  emit_label (");
  428.       else if (GET_CODE (next) == MATCH_OPERAND
  429.            || GET_CODE (next) == MATCH_OPERATOR
  430.            || GET_CODE (next) == MATCH_PARALLEL
  431.            || GET_CODE (next) == MATCH_OP_DUP
  432.            || GET_CODE (next) == MATCH_DUP
  433.            || GET_CODE (next) == PARALLEL)
  434.     printf ("  emit (");
  435.       else
  436.     printf ("  emit_insn (");
  437.       gen_exp (next);
  438.       printf (");\n");
  439.       if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
  440.       && GET_CODE (SET_SRC (next)) == LABEL_REF)
  441.     printf ("  emit_barrier ();");
  442.     }
  443.  
  444.   /* Call `gen_sequence' to make a SEQUENCE out of all the
  445.      insns emitted within this gen_... function.  */
  446.  
  447.   printf (" _done:\n");
  448.   printf ("  _val = gen_sequence ();\n");
  449.   printf ("  end_sequence ();\n");
  450.   printf ("  return _val;\n}\n\n");
  451. }
  452.  
  453. /* Like gen_expand, but generates a SEQUENCE.  */
  454. void
  455. gen_split (split)
  456.      rtx split;
  457. {
  458.   register int i;
  459.   int operands;
  460.  
  461.   if (XVEC (split, 0) == 0)
  462.     fatal ("define_split %d lacks a pattern", insn_index_number);
  463.   else if (XVEC (split, 2) == 0)
  464.     fatal ("define_split %d lacks a replacement pattern", insn_index_number);
  465.  
  466.   /* Find out how many operands this function has.  */
  467.  
  468.   max_operand_vec (split, 2);
  469.   operands = max (max_opno, max_dup_opno) + 1;
  470.  
  471.   /* Output the function name and argument declarations.  */
  472.   printf ("rtx\ngen_split_%d (operands)\n     rtx *operands;\n",
  473.       insn_code_number);
  474.   printf ("{\n");
  475.  
  476.   /* Declare all local variables.  */
  477.   for (i = 0; i < operands; i++)
  478.     printf ("  rtx operand%d;\n", i);
  479.   printf ("  rtx _val;\n");
  480.   printf ("  start_sequence ();\n");
  481.  
  482.   /* The fourth operand of DEFINE_SPLIT is some code to be executed
  483.      before the actual construction.  */
  484.  
  485.   if (XSTR (split, 3))
  486.     printf ("%s\n", XSTR (split, 3));
  487.  
  488.   /* Output code to copy the arguments back out of `operands'  */
  489.   for (i = 0; i < operands; i++)
  490.     printf ("  operand%d = operands[%d];\n", i, i);
  491.  
  492.   /* Output code to construct the rtl for the instruction bodies.
  493.      Use emit_insn to add them to the sequence being accumulated.
  494.      But don't do this if the user's code has set `no_more' nonzero.  */
  495.  
  496.   for (i = 0; i < XVECLEN (split, 2); i++)
  497.     {
  498.       rtx next = XVECEXP (split, 2, i);
  499.       if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
  500.       || (GET_CODE (next) == PARALLEL
  501.           && GET_CODE (XVECEXP (next, 0, 0)) == SET
  502.           && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
  503.       || GET_CODE (next) == RETURN)
  504.     printf ("  emit_jump_insn (");
  505.       else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
  506.            || GET_CODE (next) == CALL
  507.            || (GET_CODE (next) == PARALLEL
  508.            && GET_CODE (XVECEXP (next, 0, 0)) == SET
  509.            && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
  510.            || (GET_CODE (next) == PARALLEL
  511.            && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
  512.     printf ("  emit_call_insn (");
  513.       else if (GET_CODE (next) == CODE_LABEL)
  514.     printf ("  emit_label (");
  515.       else if (GET_CODE (next) == MATCH_OPERAND
  516.            || GET_CODE (next) == MATCH_OPERATOR
  517.            || GET_CODE (next) == MATCH_PARALLEL
  518.            || GET_CODE (next) == MATCH_OP_DUP
  519.            || GET_CODE (next) == MATCH_DUP
  520.            || GET_CODE (next) == PARALLEL)
  521.     printf ("  emit (");
  522.       else
  523.     printf ("  emit_insn (");
  524.       gen_exp (next);
  525.       printf (");\n");
  526.       if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
  527.       && GET_CODE (SET_SRC (next)) == LABEL_REF)
  528.     printf ("  emit_barrier ();");
  529.     }
  530.  
  531.   /* Call `gen_sequence' to make a SEQUENCE out of all the
  532.      insns emitted within this gen_... function.  */
  533.  
  534.   printf (" _done:\n");
  535.   printf ("  _val = gen_sequence ();\n");
  536.   printf ("  end_sequence ();\n");
  537.   printf ("  return _val;\n}\n\n");
  538. }
  539.  
  540. /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
  541.    size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
  542.    the end of the vector.  */
  543.  
  544. void
  545. output_add_clobbers ()
  546. {
  547.   struct clobber_pat *clobber;
  548.   int i;
  549.  
  550.   printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
  551.   printf ("     rtx pattern;\n     int insn_code_number;\n");
  552.   printf ("{\n");
  553.   printf ("  int i;\n\n");
  554.   printf ("  switch (insn_code_number)\n");
  555.   printf ("    {\n");
  556.  
  557.   for (clobber = clobber_list; clobber; clobber = clobber->next)
  558.     {
  559.       printf ("    case %d:\n", clobber->code_number);
  560.  
  561.       for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
  562.     {
  563.       printf ("      XVECEXP (pattern, 0, %d) = ", i);
  564.       gen_exp (XVECEXP (clobber->pattern, 1, i));
  565.       printf (";\n");
  566.     }
  567.  
  568.       printf ("      break;\n");
  569.     }
  570.  
  571.   printf ("    default:\n");
  572.   printf ("      abort ();\n");
  573.   printf ("    }\n");
  574.   printf ("}\n");
  575. }
  576.  
  577. /* Write a function, init_mov_optab, that is called to set up entries
  578.    in mov_optab for EXTRA_CC_MODES.  */
  579.  
  580. static void
  581. output_init_mov_optab ()
  582. {
  583. #ifdef EXTRA_CC_NAMES
  584.   static char *cc_names[] = { EXTRA_CC_NAMES };
  585.   char *p;
  586.   int i;
  587.  
  588.   printf ("\nvoid\ninit_mov_optab ()\n{\n");
  589.  
  590.   for (i = 0; i < sizeof cc_names / sizeof cc_names[0]; i++)
  591.     {
  592.       printf ("#ifdef HAVE_mov");
  593.       for (p = cc_names[i]; *p; p++)
  594.     printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
  595.       printf ("\n");
  596.       printf ("  if (HAVE_mov");
  597.       for (p = cc_names[i]; *p; p++)
  598.     printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
  599.       printf (")\n");
  600.       printf ("    mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
  601.           cc_names[i]);
  602.       for (p = cc_names[i]; *p; p++)
  603.     printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
  604.       printf (";\n#endif\n");
  605.     }
  606.  
  607.   printf ("}\n");
  608. #endif
  609. }
  610.  
  611. int
  612. xmalloc (size)
  613. {
  614.   register int val = malloc (size);
  615.  
  616.   if (val == 0)
  617.     fatal ("virtual memory exhausted");
  618.  
  619.   return val;
  620. }
  621.  
  622. int
  623. xrealloc (ptr, size)
  624.      char *ptr;
  625.      int size;
  626. {
  627.   int result = realloc (ptr, size);
  628.   if (!result)
  629.     fatal ("virtual memory exhausted");
  630.   return result;
  631. }
  632.  
  633. void
  634. fatal (s, a1, a2)
  635.      char *s;
  636. {
  637.   fprintf (stderr, "genemit: ");
  638.   fprintf (stderr, s, a1, a2);
  639.   fprintf (stderr, "\n");
  640.   exit (FATAL_EXIT_CODE);
  641. }
  642.  
  643. /* More 'friendly' abort that prints the line and file.
  644.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  645.  
  646. void
  647. fancy_abort ()
  648. {
  649.   fatal ("Internal gcc abort.");
  650. }
  651.  
  652. int
  653. main (argc, argv)
  654.      int argc;
  655.      char **argv;
  656. {
  657.   rtx desc;
  658.   FILE *infile;
  659.   extern rtx read_rtx ();
  660.   register int c;
  661.  
  662.   obstack_init (rtl_obstack);
  663.  
  664.   if (argc <= 1)
  665.     fatal ("No input file name.");
  666.  
  667.   infile = fopen (argv[1], "r");
  668.   if (infile == 0)
  669.     {
  670.       perror (argv[1]);
  671.       exit (FATAL_EXIT_CODE);
  672.     }
  673.  
  674.   init_rtl ();
  675.  
  676.   /* Assign sequential codes to all entries in the machine description
  677.      in parallel with the tables in insn-output.c.  */
  678.  
  679.   insn_code_number = 0;
  680.   insn_index_number = 0;
  681.  
  682.   printf ("/* Generated automatically by the program `genemit'\n\
  683. from the machine description file `md'.  */\n\n");
  684.  
  685.   printf ("#include \"config.h\"\n");
  686.   printf ("#include \"rtl.h\"\n");
  687.   printf ("#include \"expr.h\"\n");
  688.   printf ("#include \"real.h\"\n");
  689.   printf ("#include \"output.h\"\n");
  690.   printf ("#include \"insn-config.h\"\n\n");
  691.   printf ("#include \"insn-flags.h\"\n\n");
  692.   printf ("#include \"insn-codes.h\"\n\n");
  693.   printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
  694.   printf ("extern rtx recog_operand[];\n");
  695.   printf ("#define operands emit_operand\n\n");
  696.   printf ("#define FAIL do { end_sequence (); return 0;} while (0)\n\n");
  697.   printf ("#define DONE goto _done\n\n");
  698.  
  699.   /* Read the machine description.  */
  700.  
  701.   while (1)
  702.     {
  703.       c = read_skip_spaces (infile);
  704.       if (c == EOF)
  705.     break;
  706.       ungetc (c, infile);
  707.  
  708.       desc = read_rtx (infile);
  709.       if (GET_CODE (desc) == DEFINE_INSN)
  710.     {
  711.       gen_insn (desc);
  712.       ++insn_code_number;
  713.     }
  714.       if (GET_CODE (desc) == DEFINE_EXPAND)
  715.     {
  716.       gen_expand (desc);
  717.       ++insn_code_number;
  718.     }
  719.       if (GET_CODE (desc) == DEFINE_SPLIT)
  720.     {
  721.       gen_split (desc);
  722.       ++insn_code_number;
  723.     }
  724.       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  725.     {
  726.       ++insn_code_number;
  727.     }
  728.       ++insn_index_number;
  729.     }
  730.  
  731.   /* Write out the routine to add CLOBBERs to a pattern.  */
  732.   output_add_clobbers ();
  733.  
  734.   /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES.  */
  735.   output_init_mov_optab ();
  736.  
  737.   fflush (stdout);
  738.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  739. }
  740.