genflags.c 6.73 KB
Newer Older
Tom Wood committed
1 2 3
/* Generate from machine description:
   - some flags HAVE_... saying which simple standard instructions are
   available for this machine.
4 5
   Copyright (C) 1987, 1991, 1995, 1998, 1999, 2000, 2003, 2004, 2007
   Free Software Foundation, Inc.
Tom Wood committed
6

7
This file is part of GCC.
Tom Wood committed
8

9 10
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
11
Software Foundation; either version 3, or (at your option) any later
12
version.
Tom Wood committed
13

14 15 16 17
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
Tom Wood committed
18 19

You should have received a copy of the GNU General Public License
20 21
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
Tom Wood committed
22 23


24
#include "bconfig.h"
25
#include "system.h"
26 27
#include "coretypes.h"
#include "tm.h"
Tom Wood committed
28 29
#include "rtl.h"
#include "obstack.h"
Zack Weinberg committed
30
#include "errors.h"
Clinton Popetz committed
31
#include "gensupport.h"
Tom Wood committed
32

33 34
/* Obstack to remember insns with.  */
static struct obstack obstack;
35 36 37 38

/* Max size of names encountered.  */
static int max_id_len;

39 40
/* Max operand encountered in a scan over some insn.  */
static int max_opno;
41

42 43 44 45 46
static void max_operand_1 (rtx);
static int num_operands (rtx);
static void gen_proto (rtx);
static void gen_macro (const char *, int, int);
static void gen_insn (rtx);
47

48
/* Count the number of match_operand's found.  */
Mike Stump committed
49

50
static void
51
max_operand_1 (rtx x)
52
{
53 54 55 56
  RTX_CODE code;
  int i;
  int len;
  const char *fmt;
57

58 59
  if (x == 0)
    return;
60

61
  code = GET_CODE (x);
62

63 64 65 66 67 68 69
  if (code == MATCH_OPERAND || code == MATCH_OPERATOR
      || code == MATCH_PARALLEL)
    max_opno = MAX (max_opno, XINT (x, 0));

  fmt = GET_RTX_FORMAT (code);
  len = GET_RTX_LENGTH (code);
  for (i = 0; i < len; i++)
70
    {
71 72 73
      if (fmt[i] == 'e' || fmt[i] == 'u')
	max_operand_1 (XEXP (x, i));
      else if (fmt[i] == 'E')
74
	{
75 76 77
	  int j;
	  for (j = 0; j < XVECLEN (x, i); j++)
	    max_operand_1 (XVECEXP (x, i, j));
78 79
	}
    }
80
}
81

82
static int
83
num_operands (rtx insn)
84
{
85 86
  int len = XVECLEN (insn, 1);
  int i;
87 88 89 90 91 92 93

  max_opno = -1;

  for (i = 0; i < len; i++)
    max_operand_1 (XVECEXP (insn, 1, i));

  return max_opno + 1;
94 95
}

96 97 98 99
/* Print out a wrapper macro for a function which corrects the number
   of arguments it takes.  Any missing arguments are assumed to be at
   the end.  */
static void
100
gen_macro (const char *name, int real, int expect)
101 102 103
{
  int i;

104 105
  gcc_assert (real <= expect);
  gcc_assert (real);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

  /* #define GEN_CALL(A, B, C, D) gen_call((A), (B)) */
  fputs ("#define GEN_", stdout);
  for (i = 0; name[i]; i++)
    putchar (TOUPPER (name[i]));

  putchar('(');
  for (i = 0; i < expect - 1; i++)
    printf ("%c, ", i + 'A');
  printf ("%c) gen_%s (", i + 'A', name);

  for (i = 0; i < real - 1; i++)
    printf ("(%c), ", i + 'A');
  printf ("(%c))\n", i + 'A');
}

122 123 124
/* Print out prototype information for a generator function.  If the
   insn pattern has been elided, print out a dummy generator that
   does nothing.  */
Mike Stump committed
125

126
static void
127
gen_proto (rtx insn)
128 129
{
  int num = num_operands (insn);
130
  int i;
131
  const char *name = XSTR (insn, 0);
132
  int truth = maybe_eval_c_test (XSTR (insn, 2));
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

  /* Many md files don't refer to the last two operands passed to the
     call patterns.  This means their generator functions will be two
     arguments too short.  Instead of changing every md file to touch
     those operands, we wrap the prototypes in macros that take the
     correct number of arguments.  */
  if (name[0] == 'c' || name[0] == 's')
    {
      if (!strcmp (name, "call")
	  || !strcmp (name, "call_pop")
	  || !strcmp (name, "sibcall")
	  || !strcmp (name, "sibcall_pop"))
	gen_macro (name, num, 4);
      else if (!strcmp (name, "call_value")
	       || !strcmp (name, "call_value_pop")
	       || !strcmp (name, "sibcall_value")
	       || !strcmp (name, "sibcall_value_pop"))
	gen_macro (name, num, 5);
    }

153
  if (truth != 0)
154
    printf ("extern rtx        gen_%-*s (", max_id_len, name);
155
  else
156
    printf ("static inline rtx gen_%-*s (", max_id_len, name);
157 158

  if (num == 0)
159
    fputs ("void", stdout);
160 161
  else
    {
162 163
      for (i = 1; i < num; i++)
	fputs ("rtx, ", stdout);
164

165
      fputs ("rtx", stdout);
166 167
    }

168
  puts (");");
169 170 171 172 173 174 175 176 177 178

  /* Some back ends want to take the address of generator functions,
     so we cannot simply use #define for these dummy definitions.  */
  if (truth == 0)
    {
      printf ("static inline rtx\ngen_%s", name);
      if (num > 0)
	{
	  putchar ('(');
	  for (i = 0; i < num-1; i++)
179 180
	    printf ("rtx ARG_UNUSED (%c), ", 'a' + i);
	  printf ("rtx ARG_UNUSED (%c))\n", 'a' + i);
181 182
	}
      else
183
	puts ("(void)");
184 185
      puts ("{\n  return 0;\n}");
    }
186 187 188

}

Tom Wood committed
189
static void
190
gen_insn (rtx insn)
Tom Wood committed
191
{
192 193
  const char *name = XSTR (insn, 0);
  const char *p;
194
  int len;
195
  int truth = maybe_eval_c_test (XSTR (insn, 2));
Tom Wood committed
196

197 198 199 200
  /* Don't mention instructions whose names are the null string
     or begin with '*'.  They are in the machine description just
     to be recognized.  */
  if (name[0] == 0 || name[0] == '*')
Tom Wood committed
201 202
    return;

203 204
  len = strlen (name);

205 206 207
  if (len > max_id_len)
    max_id_len = len;

208
  if (truth == 0)
209
    /* Emit nothing.  */;
210 211
  else if (truth == 1)
    printf ("#define HAVE_%s 1\n", name);
Tom Wood committed
212 213 214 215
  else
    {
      /* Write the macro definition, putting \'s at the end of each line,
	 if more than one.  */
216
      printf ("#define HAVE_%s (", name);
Tom Wood committed
217 218
      for (p = XSTR (insn, 2); *p; p++)
	{
219
	  if (IS_VSPACE (*p))
220
	    fputs (" \\\n", stdout);
Tom Wood committed
221
	  else
222
	    putchar (*p);
Tom Wood committed
223
	}
224
      fputs (")\n", stdout);
Tom Wood committed
225
    }
226

227
  obstack_grow (&obstack, &insn, sizeof (rtx));
Tom Wood committed
228 229 230
}

int
231
main (int argc, char **argv)
Tom Wood committed
232 233
{
  rtx desc;
234
  rtx dummy;
235
  rtx *insns;
236
  rtx *insn_ptr;
Tom Wood committed
237

Zack Weinberg committed
238
  progname = "genflags";
239
  obstack_init (&obstack);
Tom Wood committed
240

241 242 243 244
  /* We need to see all the possibilities.  Elided insns may have
     direct calls to their generators in C code.  */
  insn_elision = 0;

245
  if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
Clinton Popetz committed
246
    return (FATAL_EXIT_CODE);
247

248 249 250 251
  puts ("/* Generated automatically by the program `genflags'");
  puts ("   from the machine description file `md'.  */\n");
  puts ("#ifndef GCC_INSN_FLAGS_H");
  puts ("#define GCC_INSN_FLAGS_H\n");
Tom Wood committed
252 253 254 255 256

  /* Read the machine description.  */

  while (1)
    {
Clinton Popetz committed
257
      int line_no, insn_code_number = 0;
Tom Wood committed
258

Clinton Popetz committed
259 260 261
      desc = read_md_rtx (&line_no, &insn_code_number);
      if (desc == NULL)
	break;
Tom Wood committed
262 263 264 265
      if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
	gen_insn (desc);
    }

266
  /* Print out the prototypes now.  */
Mike Stump committed
267
  dummy = (rtx) 0;
268
  obstack_grow (&obstack, &dummy, sizeof (rtx));
269
  insns = XOBFINISH (&obstack, rtx *);
270

271
  for (insn_ptr = insns; *insn_ptr; insn_ptr++)
272 273
    gen_proto (*insn_ptr);

274 275 276 277 278 279
  puts("\n#endif /* GCC_INSN_FLAGS_H */");

  if (ferror (stdout) || fflush (stdout) || fclose (stdout))
    return FATAL_EXIT_CODE;

  return SUCCESS_EXIT_CODE;
Tom Wood committed
280
}