genconditions.c 7.08 KB
Newer Older
1
/* Process machine description and calculate constant conditions.
2 3
   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007
   Free Software Foundation, Inc.
4

5
   This file is part of GCC.
6

7
   GCC 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 3, or (at your option)
10 11
   any later version.

12
   GCC is distributed in the hope that it will be useful,
13 14 15 16 17
   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.

   You should have received a copy of the GNU General Public License
18 19
   along with GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */
20 21 22 23 24 25 26 27 28

/* In a machine description, all of the insn patterns - define_insn,
   define_expand, define_split, define_peephole, define_peephole2 -
   contain an optional C expression which makes the final decision
   about whether or not this pattern is usable.  That expression may
   turn out to be always false when the compiler is built.  If it is,
   most of the programs that generate code from the machine
   description can simply ignore the entire pattern.  */

29
#include "bconfig.h"
30
#include "system.h"
31 32
#include "coretypes.h"
#include "tm.h"
33 34 35 36 37
#include "rtl.h"
#include "errors.h"
#include "hashtab.h"
#include "gensupport.h"

38
/* so we can include except.h in the generated file.  */
39 40
static int saw_eh_return;

41 42 43
static void write_header	(void);
static void write_conditions	(void);
static int write_one_condition	(void **, void *);
44 45 46 47

/* Generate the header for insn-conditions.c.  */

static void
48
write_header (void)
49 50 51 52 53
{
  puts ("\
/* Generated automatically by the program `genconditions' from the target\n\
   machine description file.  */\n\
\n\
54
#include \"bconfig.h\"\n\
55 56
#include \"system.h\"\n\
\n\
57 58 59 60 61
/* It is necessary, but not entirely safe, to include the headers below\n\
   in a generator program.  As a defensive measure, don't do so when the\n\
   table isn't going to have anything in it.  */\n\
#if GCC_VERSION >= 3001\n\
\n\
62 63 64 65 66 67
/* Do not allow checking to confuse the issue.  */\n\
#undef ENABLE_CHECKING\n\
#undef ENABLE_TREE_CHECKING\n\
#undef ENABLE_RTL_CHECKING\n\
#undef ENABLE_RTL_FLAG_CHECKING\n\
#undef ENABLE_GC_CHECKING\n\
68 69
#undef ENABLE_GC_ALWAYS_COLLECT\n\
\n\
70 71
#include \"coretypes.h\"\n\
#include \"tm.h\"\n\
72
#include \"insn-constants.h\"\n\
73 74
#include \"rtl.h\"\n\
#include \"tm_p.h\"\n\
75 76
#include \"function.h\"\n\
\n\
77 78 79
/* Fake - insn-config.h doesn't exist yet.  */\n\
#define MAX_RECOG_OPERANDS 10\n\
#define MAX_DUP_OPERANDS 10\n\
80 81
#define MAX_INSNS_PER_SPLIT 5\n\
\n\
82 83 84 85 86 87 88 89
#include \"regs.h\"\n\
#include \"recog.h\"\n\
#include \"real.h\"\n\
#include \"output.h\"\n\
#include \"flags.h\"\n\
#include \"hard-reg-set.h\"\n\
#include \"resource.h\"\n\
#include \"toplev.h\"\n\
90 91
#include \"reload.h\"\n\
#include \"tm-constrs.h\"\n");
92 93 94 95 96 97 98 99 100

  if (saw_eh_return)
    puts ("#define HAVE_eh_return 1");
  puts ("#include \"except.h\"\n");

  puts ("\
/* Dummy external declarations.  */\n\
extern rtx insn;\n\
extern rtx ins1;\n\
101 102 103
extern rtx operands[];\n\
\n\
#endif /* gcc >= 3.0.1 */\n");
104 105 106 107
}

/* Write out one entry in the conditions table, using the data pointed
   to by SLOT.  Each entry looks like this:
108 109 110 111 112

   { "! optimize_size && ! TARGET_READ_MODIFY_WRITE",
     __builtin_constant_p (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
     ? (int) (! optimize_size && ! TARGET_READ_MODIFY_WRITE)
     : -1) },  */
113 114

static int
115
write_one_condition (void **slot, void * ARG_UNUSED (dummy))
116 117 118 119
{
  const struct c_test *test = * (const struct c_test **) slot;
  const char *p;

120
  print_rtx_ptr_loc (test->expr);
121 122 123
  fputs ("  { \"", stdout);
  for (p = test->expr; *p; p++)
    {
124 125 126 127 128 129 130 131
      switch (*p)
	{
	case '\n': fputs ("\\n\\", stdout); break;
	case '\\':
	case '\"': putchar ('\\'); break;
	default: break;
	}
      putchar (*p);
132 133
    }

134
  fputs ("\",\n    __builtin_constant_p ", stdout);
135
  print_c_condition (test->expr);
136
  fputs ("\n    ? (int) ", stdout);
137
  print_c_condition (test->expr);
138
  fputs ("\n    : -1 },\n", stdout);
139 140 141 142 143 144
  return 1;
}

/* Write out the complete conditions table, its size, and a flag
   indicating that gensupport.c can now do insn elision.  */
static void
145
write_conditions (void)
146 147
{
  puts ("\
148 149 150 151 152 153
/* Structure definition duplicated from gensupport.h rather than\n\
   drag in that file and its dependencies.  */\n\
struct c_test\n\
{\n\
  const char *expr;\n\
  int value;\n\
154 155
};\n\
\n\
156 157
/* This table lists each condition found in the machine description.\n\
   Each condition is mapped to its truth value (0 or 1), or -1 if that\n\
158 159
   cannot be calculated at compile time.\n\
   If we don't have __builtin_constant_p, or it's not acceptable in array\n\
160 161 162
   initializers, fall back to assuming that all conditions potentially\n\
   vary at run time.  It works in 3.0.1 and later; 3.0 only when not\n\
   optimizing.  */\n\
163
\n\
164 165
#if GCC_VERSION >= 3001\n\
static const struct c_test insn_conditions[] = {\n");
166

167
  traverse_c_tests (write_one_condition, 0);
168

169
  puts ("\n};\n#endif /* gcc >= 3.0.1 */\n");
170
}
171

172 173 174 175 176 177 178
/* Emit code which will convert the C-format table to a
   (define_conditions) form, which the MD reader can understand.
   The result will be added to the set of files scanned by
   'downstream' generators.  */
static void
write_writer (void)
{
179 180 181 182 183 184
  puts ("int\n"
	"main(void)\n"
	"{\n"
	"  unsigned int i;\n"
        "  const char *p;\n"
        "  puts (\"(define_conditions [\");\n"
185
	"#if GCC_VERSION >= 3001\n"
186 187 188 189 190 191 192 193 194 195 196 197 198 199
	"  for (i = 0; i < ARRAY_SIZE (insn_conditions); i++)\n"
	"    {\n"
	"      printf (\"  (%d \\\"\", insn_conditions[i].value);\n"
	"      for (p = insn_conditions[i].expr; *p; p++)\n"
	"        {\n"
	"          switch (*p)\n"
	"	     {\n"
	"	     case '\\\\':\n"
	"	     case '\\\"': putchar ('\\\\'); break;\n"
	"	     default: break;\n"
	"	     }\n"
	"          putchar (*p);\n"
	"        }\n"
        "      puts (\"\\\")\");\n"
200
        "    }\n"
201
	"#endif /* gcc >= 3.0.1 */\n"
202
	"  puts (\"])\");\n"
203 204 205
        "  fflush (stdout);\n"
        "return ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;\n"
	"}");
206 207 208
}

int
209
main (int argc, char **argv)
210 211 212 213 214 215 216
{
  rtx desc;
  int pattern_lineno; /* not used */
  int code;

  progname = "genconditions";

217
  if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    return (FATAL_EXIT_CODE);

  /* Read the machine description.  */
  while (1)
    {
      desc = read_md_rtx (&pattern_lineno, &code);
      if (desc == NULL)
	break;

      /* N.B. define_insn_and_split, define_cond_exec are handled
	 entirely within read_md_rtx; we never see them.  */
      switch (GET_CODE (desc))
	{
	default:
	  break;

	case DEFINE_INSN:
	case DEFINE_EXPAND:
236
	  add_c_test (XSTR (desc, 2), -1);
237 238 239 240 241 242 243 244 245
	  /* except.h needs to know whether there is an eh_return
	     pattern in the machine description.  */
	  if (!strcmp (XSTR (desc, 0), "eh_return"))
	    saw_eh_return = 1;
	  break;

	case DEFINE_SPLIT:
	case DEFINE_PEEPHOLE:
	case DEFINE_PEEPHOLE2:
246
	  add_c_test (XSTR (desc, 1), -1);
247 248 249 250 251 252
	  break;
	}
    }

  write_header ();
  write_conditions ();
253
  write_writer ();
254 255 256 257

  fflush (stdout);
  return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
}