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

  1. /* Generate code from machine description to extract operands from insn as rtl.
  2.    Copyright (C) 1987 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. /* Number instruction patterns handled, starting at 0 for first one.  */
  35.  
  36. int insn_code_number;
  37.  
  38. /* Number the occurrences of MATCH_DUP in each instruction,
  39.    starting at 0 for the first occurrence.  */
  40.  
  41. int dup_count;
  42.  
  43. /* Record which operand numbers have been seen in the current pattern.
  44.    This table is made longer as needed.  */
  45.  
  46. char *operand_seen;
  47.  
  48. /* Current allocated length of operand_seen.  */
  49.  
  50. int operand_seen_length;
  51.  
  52. /* While tree-walking an instruction pattern, we keep a chain
  53.    of these `struct link's to record how to get down to the
  54.    current position.  In each one, POS is the operand number,
  55.    and if the operand is a vector VEC is the element number.
  56.    VEC is -1 if the operand is not a vector.  */
  57.  
  58. struct link
  59. {
  60.   struct link *next;
  61.   int pos;
  62.   int vecelt;
  63. };
  64.  
  65. void walk_rtx ();
  66. void print_path ();
  67. void fatal ();
  68. void fancy_abort ();
  69.  
  70. void
  71. gen_insn (insn)
  72.      rtx insn;
  73. {
  74.   register int i;
  75.  
  76.   dup_count = 0;
  77.  
  78.   /* No operands seen so far in this pattern.  */
  79.   bzero (operand_seen, operand_seen_length);
  80.  
  81.   /* Output the function name and argument declaration.  */
  82.   /* It would be cleaner to make `void' the return type
  83.      but 4.2 vax compiler doesn't accept that in the array
  84.      that these functions are supposed to go in.  */
  85.   printf ("VOID\nextract_%d (insn)\n     rtx insn;\n", insn_code_number);
  86.   printf ("{\n");
  87.  
  88.   /* Walk the insn's pattern, remembering at all times the path
  89.      down to the walking point.  */
  90.  
  91.   if (XVECLEN (insn, 1) == 1)
  92.     walk_rtx (XVECEXP (insn, 1, 0), 0);
  93.   else
  94.     for (i = XVECLEN (insn, 1) - 1; i >= 0; i--)
  95.       {
  96.     struct link link;
  97.     link.next = 0;
  98.     link.pos = 0;
  99.     link.vecelt = i;
  100.     walk_rtx (XVECEXP (insn, 1, i), &link);
  101.       }
  102.  
  103.   /* If the operand numbers used in the pattern are not consecutive,
  104.      don't leave an operand uninitialized.  */
  105.   for (i = operand_seen_length - 1; i >= 0; i--)
  106.     if (operand_seen[i])
  107.       break;
  108.   for (; i >= 0; i--)
  109.     if (!operand_seen[i])
  110.       {
  111.     printf ("  recog_operand[%d] = const0_rtx;\n", i);
  112.     printf ("  recog_operand_loc[%d] = &junk;\n", i);
  113.       }
  114.  
  115.   printf ("}\n\n");
  116. }
  117.  
  118. /* Like gen_insn but handles `define_peephole'.  */
  119.  
  120. void
  121. gen_peephole (peep)
  122.      rtx peep;
  123. {
  124.   /* Output the function name and argument declaration.  */
  125.   printf ("VOID\nextract_%d (insn)\n     rtx insn;\n", insn_code_number);
  126.   printf ("{\n");
  127.   /* The vector in the insn says how many operands it has.
  128.      And all it contains are operands.  In fact, the vector was
  129.      created just for the sake of this function.  */
  130.   printf ("\
  131.   bcopy (&XVECEXP (insn, 0, 0), recog_operand,\
  132.          sizeof (rtx) * XVECLEN (insn, 0));\n");
  133.   printf ("}\n\n");
  134. }
  135.  
  136. /* Record that we have seen an operand with number OPNO in this pattern.  */
  137.  
  138. void
  139. mark_operand_seen (opno)
  140.      int opno;
  141. {
  142.   if (opno >= operand_seen_length)
  143.     {
  144.       operand_seen_length *= 2;
  145.       operand_seen = (char *) xrealloc (operand_seen_length);
  146.     }
  147.  
  148.   operand_seen[opno] = 1;
  149. }
  150.  
  151. void
  152. walk_rtx (x, path)
  153.      rtx x;
  154.      struct link *path;
  155. {
  156.   register RTX_CODE code;
  157.   register int i;
  158.   register int len;
  159.   register char *fmt;
  160.   struct link link;
  161.  
  162.   if (x == 0)
  163.     return;
  164.  
  165.   code = GET_CODE (x);
  166.  
  167.   switch (code)
  168.     {
  169.     case PC:
  170.     case CC0:
  171.     case CONST_INT:
  172.     case SYMBOL_REF:
  173.       return;
  174.  
  175.     case MATCH_OPERAND:
  176.     case MATCH_SCRATCH:
  177.       mark_operand_seen (XINT (x, 0));
  178.       printf ("  recog_operand[%d] = *(recog_operand_loc[%d]\n    = &",
  179.           XINT (x, 0), XINT (x, 0));
  180.       print_path (path);
  181.       printf (");\n");
  182.       break;
  183.  
  184.     case MATCH_DUP:
  185.     case MATCH_OP_DUP:
  186.       printf ("  recog_dup_loc[%d] = &", dup_count);
  187.       print_path (path);
  188.       printf (";\n");
  189.       printf ("  recog_dup_num[%d] = %d;\n", dup_count, XINT (x, 0));
  190.       dup_count++;
  191.       break;
  192.  
  193.     case MATCH_OPERATOR:
  194.       mark_operand_seen (XINT (x, 0));
  195.       printf ("  recog_operand[%d] = *(recog_operand_loc[%d]\n    = &",
  196.           XINT (x, 0), XINT (x, 0));
  197.       print_path (path);
  198.       printf (");\n");
  199.       link.next = path;
  200.       link.vecelt = -1;
  201.       for (i = XVECLEN (x, 2) - 1; i >= 0; i--)
  202.     {
  203.       link.pos = i;
  204.       walk_rtx (XVECEXP (x, 2, i), &link);
  205.     }
  206.       return;
  207.  
  208.     case MATCH_PARALLEL:
  209.       mark_operand_seen (XINT (x, 0));
  210.       printf ("  recog_operand[%d] = *(recog_operand_loc[%d]\n    = &",
  211.           XINT (x, 0), XINT (x, 0));
  212.       print_path (path);
  213.       printf (");\n");
  214.       link.next = path;
  215.       link.pos = 0;
  216.       for (i = XVECLEN (x, 2) - 1; i >= 0; i--)
  217.     {
  218.       link.vecelt = i;
  219.       walk_rtx (XVECEXP (x, 2, i), &link);
  220.     }
  221.       return;
  222.  
  223.     case ADDRESS:
  224.       walk_rtx (XEXP (x, 0), path);
  225.       return;
  226.     }
  227.  
  228.   link.next = path;
  229.   link.vecelt = -1;
  230.   fmt = GET_RTX_FORMAT (code);
  231.   len = GET_RTX_LENGTH (code);
  232.   for (i = 0; i < len; i++)
  233.     {
  234.       link.pos = i;
  235.       if (fmt[i] == 'e' || fmt[i] == 'u')
  236.     {
  237.       walk_rtx (XEXP (x, i), &link);
  238.     }
  239.       else if (fmt[i] == 'E')
  240.     {
  241.       int j;
  242.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  243.         {
  244.           link.vecelt = j;
  245.           walk_rtx (XVECEXP (x, i, j), &link);
  246.         }
  247.     }
  248.     }
  249. }
  250.  
  251. /* Given a PATH, representing a path down the instruction's
  252.    pattern from the root to a certain point, output code to
  253.    evaluate to the rtx at that point.  */
  254.  
  255. void
  256. print_path (path)
  257.      struct link *path;
  258. {
  259.   if (path == 0)
  260.     printf ("insn");
  261.   else if (path->vecelt >= 0)
  262.     {
  263.       printf ("XVECEXP (");
  264.       print_path (path->next);
  265.       printf (", %d, %d)", path->pos, path->vecelt);
  266.     }
  267.   else
  268.     {
  269.       printf ("XEXP (");
  270.       print_path (path->next);
  271.       printf (", %d)", path->pos);
  272.     }
  273. }
  274.  
  275. int
  276. xmalloc (size)
  277. {
  278.   register int val = malloc (size);
  279.  
  280.   if (val == 0)
  281.     fatal ("virtual memory exhausted");
  282.   return val;
  283. }
  284.  
  285. int
  286. xrealloc (ptr, size)
  287.      char *ptr;
  288.      int size;
  289. {
  290.   int result = realloc (ptr, size);
  291.   if (!result)
  292.     fatal ("virtual memory exhausted");
  293.   return result;
  294. }
  295.  
  296. void
  297. fatal (s, a1, a2)
  298.      char *s;
  299. {
  300.   fprintf (stderr, "genextract: ");
  301.   fprintf (stderr, s, a1, a2);
  302.   fprintf (stderr, "\n");
  303.   exit (FATAL_EXIT_CODE);
  304. }
  305.  
  306. /* More 'friendly' abort that prints the line and file.
  307.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  308.  
  309. void
  310. fancy_abort ()
  311. {
  312.   fatal ("Internal gcc abort.");
  313. }
  314.  
  315. int
  316. main (argc, argv)
  317.      int argc;
  318.      char **argv;
  319. {
  320.   rtx desc;
  321.   FILE *infile;
  322.   extern rtx read_rtx ();
  323.   register int c, i;
  324.  
  325.   obstack_init (rtl_obstack);
  326.  
  327.   if (argc <= 1)
  328.     fatal ("No input file name.");
  329.  
  330.   infile = fopen (argv[1], "r");
  331.   if (infile == 0)
  332.     {
  333.       perror (argv[1]);
  334.       exit (FATAL_EXIT_CODE);
  335.     }
  336.  
  337.   init_rtl ();
  338.  
  339.   /* Assign sequential codes to all entries in the machine description
  340.      in parallel with the tables in insn-output.c.  */
  341.  
  342.   insn_code_number = 0;
  343.  
  344.   operand_seen_length = 40;
  345.   operand_seen = (char *) xmalloc (40);
  346.  
  347.   printf ("/* Generated automatically by the program `genextract'\n\
  348. from the machine description file `md'.  */\n\n");
  349.  
  350.   printf ("#include \"config.h\"\n");
  351.   printf ("#include \"rtl.h\"\n\n");
  352.  
  353.   /* This variable exists only so it can be the "location"
  354.      of any missing operand whose numbers are skipped by a given pattern.  */
  355.   printf ("static rtx junk;\n");
  356.   printf ("extern rtx recog_operand[];\n");
  357.   printf ("extern rtx *recog_operand_loc[];\n");
  358.   printf ("extern rtx *recog_dup_loc[];\n");
  359.   printf ("extern char recog_dup_num[];\n\n");
  360.  
  361.   /* The extractor functions really should return `void';
  362.      but old C compilers don't seem to be able to handle the array
  363.      definition if `void' is used.  So use `int' in non-ANSI C compilers.  */
  364.  
  365.   printf ("#ifdef __STDC__\n#define VOID void\n#else\n#define VOID int\n#endif\n\n");
  366.  
  367.   /* Read the machine description.  */
  368.  
  369.   while (1)
  370.     {
  371.       c = read_skip_spaces (infile);
  372.       if (c == EOF)
  373.     break;
  374.       ungetc (c, infile);
  375.  
  376.       desc = read_rtx (infile);
  377.       if (GET_CODE (desc) == DEFINE_INSN)
  378.     {
  379.       gen_insn (desc);
  380.       ++insn_code_number;
  381.     }
  382.       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  383.     {
  384.       gen_peephole (desc);
  385.       ++insn_code_number;
  386.     }
  387.       if (GET_CODE (desc) == DEFINE_EXPAND || GET_CODE (desc) == DEFINE_SPLIT)
  388.     {
  389.       printf ("VOID extract_%d () {}\n\n", insn_code_number);
  390.       ++insn_code_number;
  391.     }
  392.     }
  393.  
  394.   printf ("VOID (*insn_extract_fn[]) () =\n{ ");
  395.   for (i = 0; i < insn_code_number; i++)
  396.     {
  397.       if (i % 4 != 0)
  398.     printf (", ");
  399.       else if (i != 0)
  400.     printf (",\n  ");
  401.       printf ("extract_%d", i);
  402.     }
  403.   printf ("\n};\n\n");
  404.  
  405.   printf ("void fatal_insn_not_found ();\n\n");
  406.   printf ("void\ninsn_extract (insn)\n");
  407.   printf ("     rtx insn;\n");
  408.   printf ("{\n  if (INSN_CODE (insn) == -1) fatal_insn_not_found (insn);\n");
  409.   printf ("  (*insn_extract_fn[INSN_CODE (insn)]) (PATTERN (insn));\n}\n");
  410.  
  411.   fflush (stdout);
  412.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  413. }
  414.