]> gcc.gnu.org Git - gcc.git/blame - gcc/genrecog.c
new
[gcc.git] / gcc / genrecog.c
CommitLineData
ec65fa66 1/* Generate code from machine description to recognize rtl as insns.
01a7aaea 2 Copyright (C) 1987, 88, 92, 93, 94, 95, 97, 98 Free Software Foundation, Inc.
ec65fa66
RK
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
a35311b0
RK
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
ec65fa66
RK
20
21
22/* This program is used to produce insn-recog.c, which contains
23 a function called `recog' plus its subroutines.
24 These functions contain a decision tree
25 that recognizes whether an rtx, the argument given to recog,
26 is a valid instruction.
27
28 recog returns -1 if the rtx is not valid.
29 If the rtx is valid, recog returns a nonnegative number
30 which is the insn code number for the pattern that matched.
31 This is the same as the order in the machine description of the
32 entry that matched. This number can be used as an index into various
e0689256 33 insn_* tables, such as insn_template, insn_outfun, and insn_n_operands
ec65fa66
RK
34 (found in insn-output.c).
35
36 The third argument to recog is an optional pointer to an int.
37 If present, recog will accept a pattern if it matches except for
38 missing CLOBBER expressions at the end. In that case, the value
39 pointed to by the optional pointer will be set to the number of
40 CLOBBERs that need to be added (it should be initialized to zero by
41 the caller). If it is set nonzero, the caller should allocate a
42 PARALLEL of the appropriate size, copy the initial entries, and call
43 add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
44
45 This program also generates the function `split_insns',
46 which returns 0 if the rtl could not be split, or
47 it returns the split rtl in a SEQUENCE. */
48
20f92396 49#include "hconfig.h"
0b93b64e 50#include "system.h"
ec65fa66
RK
51#include "rtl.h"
52#include "obstack.h"
53
54static struct obstack obstack;
55struct obstack *rtl_obstack = &obstack;
56
57#define obstack_chunk_alloc xmalloc
58#define obstack_chunk_free free
59
4db83042
MM
60/* Define this so we can link with print-rtl.o to get debug_rtx function. */
61char **insn_name_ptr = 0;
62
e0689256
RK
63/* Data structure for a listhead of decision trees. The alternatives
64 to a node are kept in a doublely-linked list so we can easily add nodes
65 to the proper place when merging. */
66
67struct decision_head { struct decision *first, *last; };
68
ec65fa66
RK
69/* Data structure for decision tree for recognizing
70 legitimate instructions. */
71
72struct decision
73{
e0689256
RK
74 int number; /* Node number, used for labels */
75 char *position; /* String denoting position in pattern */
76 RTX_CODE code; /* Code to test for or UNKNOWN to suppress */
77 char ignore_code; /* If non-zero, need not test code */
78 char ignore_mode; /* If non-zero, need not test mode */
79 int veclen; /* Length of vector, if nonzero */
80 enum machine_mode mode; /* Machine mode of node */
81 char enforce_mode; /* If non-zero, test `mode' */
82 char retest_code, retest_mode; /* See write_tree_1 */
83 int test_elt_zero_int; /* Nonzero if should test XINT (rtl, 0) */
84 int elt_zero_int; /* Required value for XINT (rtl, 0) */
85 int test_elt_one_int; /* Nonzero if should test XINT (rtl, 1) */
de6a431b 86 int elt_one_int; /* Required value for XINT (rtl, 1) */
3d678dca
RS
87 int test_elt_zero_wide; /* Nonzero if should test XWINT (rtl, 0) */
88 HOST_WIDE_INT elt_zero_wide; /* Required value for XWINT (rtl, 0) */
e0689256
RK
89 char *tests; /* If nonzero predicate to call */
90 int pred; /* `preds' index of predicate or -1 */
91 char *c_test; /* Additional test to perform */
92 struct decision_head success; /* Nodes to test on success */
93 int insn_code_number; /* Insn number matched, if success */
94 int num_clobbers_to_add; /* Number of CLOBBERs to be added to pattern */
95 struct decision *next; /* Node to test on failure */
96 struct decision *prev; /* Node whose failure tests us */
97 struct decision *afterward; /* Node to test on success, but failure of
98 successor nodes */
99 int opno; /* Operand number, if >= 0 */
100 int dupno; /* Number of operand to compare against */
101 int label_needed; /* Nonzero if label needed when writing tree */
102 int subroutine_number; /* Number of subroutine this node starts */
ec65fa66
RK
103};
104
105#define SUBROUTINE_THRESHOLD 50
106
107static int next_subroutine_number;
108
109/* We can write two types of subroutines: One for insn recognition and
110 one to split insns. This defines which type is being written. */
111
112enum routine_type {RECOG, SPLIT};
113
e0689256 114/* Next available node number for tree nodes. */
ec65fa66 115
e0689256 116static int next_number;
ec65fa66 117
e0689256 118/* Next number to use as an insn_code. */
ec65fa66 119
e0689256 120static int next_insn_code;
ec65fa66 121
e0689256 122/* Similar, but counts all expressions in the MD file; used for
0f41302f 123 error messages. */
ec65fa66 124
e0689256 125static int next_index;
ec65fa66 126
e0689256
RK
127/* Record the highest depth we ever have so we know how many variables to
128 allocate in each subroutine we make. */
ec65fa66 129
e0689256
RK
130static int max_depth;
131\f
132/* This table contains a list of the rtl codes that can possibly match a
133 predicate defined in recog.c. The function `not_both_true' uses it to
134 deduce that there are no expressions that can be matches by certain pairs
135 of tree nodes. Also, if a predicate can match only one code, we can
136 hardwire that code into the node testing the predicate. */
ec65fa66 137
e0689256
RK
138static struct pred_table
139{
140 char *name;
141 RTX_CODE codes[NUM_RTX_CODE];
142} preds[]
143 = {{"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
144 LABEL_REF, SUBREG, REG, MEM}},
145#ifdef PREDICATE_CODES
146 PREDICATE_CODES
147#endif
148 {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
149 LABEL_REF, SUBREG, REG, MEM, PLUS, MINUS, MULT}},
150 {"register_operand", {SUBREG, REG}},
151 {"scratch_operand", {SCRATCH, REG}},
152 {"immediate_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
153 LABEL_REF}},
154 {"const_int_operand", {CONST_INT}},
155 {"const_double_operand", {CONST_INT, CONST_DOUBLE}},
156 {"nonimmediate_operand", {SUBREG, REG, MEM}},
157 {"nonmemory_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
158 LABEL_REF, SUBREG, REG}},
159 {"push_operand", {MEM}},
160 {"memory_operand", {SUBREG, MEM}},
161 {"indirect_operand", {SUBREG, MEM}},
62e066e2 162 {"comparison_operator", {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU}},
e0689256
RK
163 {"mode_independent_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
164 LABEL_REF, SUBREG, REG, MEM}}};
165
166#define NUM_KNOWN_PREDS (sizeof preds / sizeof preds[0])
ec65fa66 167
7967c666
RK
168static struct decision_head make_insn_sequence PROTO((rtx, enum routine_type));
169static struct decision *add_to_sequence PROTO((rtx, struct decision_head *,
170 char *));
171static int not_both_true PROTO((struct decision *, struct decision *,
172 int));
173static int position_merit PROTO((struct decision *, enum machine_mode,
174 enum rtx_code));
175static struct decision_head merge_trees PROTO((struct decision_head,
176 struct decision_head));
177static int break_out_subroutines PROTO((struct decision_head,
178 enum routine_type, int));
179static void write_subroutine PROTO((struct decision *, enum routine_type));
180static void write_tree_1 PROTO((struct decision *, char *,
181 struct decision *, enum routine_type));
182static void print_code PROTO((enum rtx_code));
183static int same_codes PROTO((struct decision *, enum rtx_code));
184static void clear_codes PROTO((struct decision *));
185static int same_modes PROTO((struct decision *, enum machine_mode));
186static void clear_modes PROTO((struct decision *));
187static void write_tree PROTO((struct decision *, char *,
188 struct decision *, int,
189 enum routine_type));
190static void change_state PROTO((char *, char *, int));
191static char *copystr PROTO((char *));
192static void mybzero PROTO((char *, unsigned));
193static void mybcopy PROTO((char *, char *, unsigned));
320e7c40 194static void fatal PVPROTO((char *, ...)) ATTRIBUTE_PRINTF_1;
7967c666
RK
195char *xrealloc PROTO((char *, unsigned));
196char *xmalloc PROTO((unsigned));
197void fancy_abort PROTO((void));
ec65fa66 198\f
ec65fa66 199/* Construct and return a sequence of decisions
e0689256 200 that will recognize INSN.
ec65fa66 201
e0689256
RK
202 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */
203
204static struct decision_head
205make_insn_sequence (insn, type)
ec65fa66 206 rtx insn;
e0689256 207 enum routine_type type;
ec65fa66
RK
208{
209 rtx x;
e0689256 210 char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
ec65fa66 211 struct decision *last;
e0689256 212 struct decision_head head;
ec65fa66 213
e0689256
RK
214 if (XVECLEN (insn, type == RECOG) == 1)
215 x = XVECEXP (insn, type == RECOG, 0);
ec65fa66
RK
216 else
217 {
218 x = rtx_alloc (PARALLEL);
e0689256 219 XVEC (x, 0) = XVEC (insn, type == RECOG);
ec65fa66
RK
220 PUT_MODE (x, VOIDmode);
221 }
222
e0689256 223 last = add_to_sequence (x, &head, "");
ec65fa66
RK
224
225 if (c_test[0])
226 last->c_test = c_test;
227 last->insn_code_number = next_insn_code;
228 last->num_clobbers_to_add = 0;
229
e0689256
RK
230 /* If this is not a DEFINE_SPLIT and X is a PARALLEL, see if it ends with a
231 group of CLOBBERs of (hard) registers or MATCH_SCRATCHes. If so, set up
232 to recognize the pattern without these CLOBBERs. */
ec65fa66 233
e0689256 234 if (type == RECOG && GET_CODE (x) == PARALLEL)
ec65fa66
RK
235 {
236 int i;
237
238 for (i = XVECLEN (x, 0); i > 0; i--)
239 if (GET_CODE (XVECEXP (x, 0, i - 1)) != CLOBBER
240 || (GET_CODE (XEXP (XVECEXP (x, 0, i - 1), 0)) != REG
241 && GET_CODE (XEXP (XVECEXP (x, 0, i - 1), 0)) != MATCH_SCRATCH))
242 break;
243
244 if (i != XVECLEN (x, 0))
245 {
246 rtx new;
e0689256 247 struct decision_head clobber_head;
ec65fa66
RK
248
249 if (i == 1)
250 new = XVECEXP (x, 0, 0);
251 else
252 {
253 int j;
254
255 new = rtx_alloc (PARALLEL);
256 XVEC (new, 0) = rtvec_alloc (i);
257 for (j = i - 1; j >= 0; j--)
258 XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
259 }
260
e0689256 261 last = add_to_sequence (new, &clobber_head, "");
ec65fa66
RK
262
263 if (c_test[0])
264 last->c_test = c_test;
265 last->insn_code_number = next_insn_code;
266 last->num_clobbers_to_add = XVECLEN (x, 0) - i;
e0689256
RK
267
268 head = merge_trees (head, clobber_head);
ec65fa66
RK
269 }
270 }
271
272 next_insn_code++;
ec65fa66 273
e0689256
RK
274 if (type == SPLIT)
275 /* Define the subroutine we will call below and emit in genemit. */
276 printf ("extern rtx gen_split_%d ();\n", last->insn_code_number);
ec65fa66 277
e0689256
RK
278 return head;
279}
280\f
281/* Create a chain of nodes to verify that an rtl expression matches
282 PATTERN.
ec65fa66 283
e0689256
RK
284 LAST is a pointer to the listhead in the previous node in the chain (or
285 in the calling function, for the first node).
ec65fa66 286
e0689256 287 POSITION is the string representing the current position in the insn.
ec65fa66 288
e0689256 289 A pointer to the final node in the chain is returned. */
ec65fa66
RK
290
291static struct decision *
292add_to_sequence (pattern, last, position)
293 rtx pattern;
e0689256 294 struct decision_head *last;
ec65fa66
RK
295 char *position;
296{
297 register RTX_CODE code;
298 register struct decision *new
299 = (struct decision *) xmalloc (sizeof (struct decision));
300 struct decision *this;
301 char *newpos;
302 register char *fmt;
85066503 303 register size_t i;
e0689256 304 int depth = strlen (position);
ec65fa66
RK
305 int len;
306
e0689256
RK
307 if (depth > max_depth)
308 max_depth = depth;
309
ec65fa66
RK
310 new->number = next_number++;
311 new->position = copystr (position);
e0689256
RK
312 new->ignore_code = 0;
313 new->ignore_mode = 0;
314 new->enforce_mode = 1;
315 new->retest_code = new->retest_mode = 0;
316 new->veclen = 0;
ec65fa66
RK
317 new->test_elt_zero_int = 0;
318 new->test_elt_one_int = 0;
3d678dca 319 new->test_elt_zero_wide = 0;
ec65fa66
RK
320 new->elt_zero_int = 0;
321 new->elt_one_int = 0;
3d678dca 322 new->elt_zero_wide = 0;
e0689256
RK
323 new->tests = 0;
324 new->pred = -1;
325 new->c_test = 0;
326 new->success.first = new->success.last = 0;
327 new->insn_code_number = -1;
328 new->num_clobbers_to_add = 0;
329 new->next = 0;
330 new->prev = 0;
ec65fa66 331 new->afterward = 0;
e0689256
RK
332 new->opno = -1;
333 new->dupno = -1;
ec65fa66 334 new->label_needed = 0;
ec65fa66
RK
335 new->subroutine_number = 0;
336
337 this = new;
338
e0689256 339 last->first = last->last = new;
ec65fa66 340
ec65fa66
RK
341 newpos = (char *) alloca (depth + 2);
342 strcpy (newpos, position);
343 newpos[depth + 1] = 0;
344
345 restart:
346
ec65fa66
RK
347 new->mode = GET_MODE (pattern);
348 new->code = code = GET_CODE (pattern);
349
350 switch (code)
351 {
352 case MATCH_OPERAND:
ec65fa66 353 case MATCH_SCRATCH:
ec65fa66 354 case MATCH_OPERATOR:
ec65fa66 355 case MATCH_PARALLEL:
693e265f 356 case MATCH_INSN2:
ec65fa66 357 new->opno = XINT (pattern, 0);
e0689256
RK
358 new->code = (code == MATCH_PARALLEL ? PARALLEL : UNKNOWN);
359 new->enforce_mode = 0;
360
361 if (code == MATCH_SCRATCH)
362 new->tests = "scratch_operand";
363 else
364 new->tests = XSTR (pattern, 1);
365
ec65fa66
RK
366 if (*new->tests == 0)
367 new->tests = 0;
e0689256
RK
368
369 /* See if we know about this predicate and save its number. If we do,
370 and it only accepts one code, note that fact. The predicate
371 `const_int_operand' only tests for a CONST_INT, so if we do so we
372 can avoid calling it at all.
373
374 Finally, if we know that the predicate does not allow CONST_INT, we
375 know that the only way the predicate can match is if the modes match
9faa82d8 376 (here we use the kludge of relying on the fact that "address_operand"
e0689256
RK
377 accepts CONST_INT; otherwise, it would have to be a special case),
378 so we can test the mode (but we need not). This fact should
379 considerably simplify the generated code. */
380
381 if (new->tests)
9edd4689
RK
382 {
383 for (i = 0; i < NUM_KNOWN_PREDS; i++)
384 if (! strcmp (preds[i].name, new->tests))
385 {
386 int j;
387 int allows_const_int = 0;
e0689256 388
9edd4689 389 new->pred = i;
e0689256 390
9edd4689
RK
391 if (preds[i].codes[1] == 0 && new->code == UNKNOWN)
392 {
393 new->code = preds[i].codes[0];
394 if (! strcmp ("const_int_operand", new->tests))
395 new->tests = 0, new->pred = -1;
396 }
e0689256 397
9edd4689
RK
398 for (j = 0; j < NUM_RTX_CODE && preds[i].codes[j] != 0; j++)
399 if (preds[i].codes[j] == CONST_INT)
400 allows_const_int = 1;
e0689256 401
9edd4689
RK
402 if (! allows_const_int)
403 new->enforce_mode = new->ignore_mode= 1;
e0689256 404
9edd4689
RK
405 break;
406 }
407
408#ifdef PREDICATE_CODES
409 /* If the port has a list of the predicates it uses but omits
410 one, warn. */
411 if (i == NUM_KNOWN_PREDS)
412 fprintf (stderr, "Warning: `%s' not in PREDICATE_CODES\n",
413 new->tests);
414#endif
415 }
e0689256
RK
416
417 if (code == MATCH_OPERATOR || code == MATCH_PARALLEL)
ec65fa66 418 {
e0689256
RK
419 for (i = 0; i < XVECLEN (pattern, 2); i++)
420 {
421 newpos[depth] = i + (code == MATCH_OPERATOR ? '0': 'a');
422 new = add_to_sequence (XVECEXP (pattern, 2, i),
423 &new->success, newpos);
424 }
ec65fa66 425 }
e0689256 426
ec65fa66
RK
427 return new;
428
429 case MATCH_OP_DUP:
430 new->opno = XINT (pattern, 0);
431 new->dupno = XINT (pattern, 0);
432 new->code = UNKNOWN;
433 new->tests = 0;
434 for (i = 0; i < XVECLEN (pattern, 1); i++)
435 {
436 newpos[depth] = i + '0';
e0689256
RK
437 new = add_to_sequence (XVECEXP (pattern, 1, i),
438 &new->success, newpos);
ec65fa66 439 }
ec65fa66
RK
440 return new;
441
442 case MATCH_DUP:
f582c9d5 443 case MATCH_PAR_DUP:
ec65fa66
RK
444 new->dupno = XINT (pattern, 0);
445 new->code = UNKNOWN;
e0689256 446 new->enforce_mode = 0;
ec65fa66
RK
447 return new;
448
449 case ADDRESS:
450 pattern = XEXP (pattern, 0);
451 goto restart;
452
ec65fa66 453 case SET:
141dba98
JC
454 /* The operands of a SET must have the same mode unless one is VOIDmode. */
455 if (GET_MODE (SET_SRC (pattern)) != VOIDmode
456 && GET_MODE (SET_DEST (pattern)) != VOIDmode
457 && GET_MODE (SET_SRC (pattern)) != GET_MODE (SET_DEST (pattern))
458 /* The mode of an ADDRESS_OPERAND is the mode of the memory reference,
459 not the mode of the address. */
460 && ! (GET_CODE (SET_SRC (pattern)) == MATCH_OPERAND
461 && ! strcmp (XSTR (SET_SRC (pattern), 1), "address_operand")))
462 {
463 print_rtl (stderr, pattern);
464 fputc ('\n', stderr);
465 fatal ("mode mismatch in SET");
466 }
ec65fa66 467 newpos[depth] = '0';
e0689256
RK
468 new = add_to_sequence (SET_DEST (pattern), &new->success, newpos);
469 this->success.first->enforce_mode = 1;
ec65fa66 470 newpos[depth] = '1';
e0689256
RK
471 new = add_to_sequence (SET_SRC (pattern), &new->success, newpos);
472
473 /* If set are setting CC0 from anything other than a COMPARE, we
474 must enforce the mode so that we do not produce ambiguous insns. */
475 if (GET_CODE (SET_DEST (pattern)) == CC0
476 && GET_CODE (SET_SRC (pattern)) != COMPARE)
477 this->success.first->enforce_mode = 1;
ec65fa66
RK
478 return new;
479
e0689256
RK
480 case SIGN_EXTEND:
481 case ZERO_EXTEND:
ec65fa66
RK
482 case STRICT_LOW_PART:
483 newpos[depth] = '0';
e0689256
RK
484 new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
485 this->success.first->enforce_mode = 1;
ec65fa66
RK
486 return new;
487
488 case SUBREG:
489 this->test_elt_one_int = 1;
490 this->elt_one_int = XINT (pattern, 1);
491 newpos[depth] = '0';
e0689256
RK
492 new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
493 this->success.first->enforce_mode = 1;
ec65fa66
RK
494 return new;
495
496 case ZERO_EXTRACT:
497 case SIGN_EXTRACT:
498 newpos[depth] = '0';
e0689256
RK
499 new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
500 this->success.first->enforce_mode = 1;
ec65fa66 501 newpos[depth] = '1';
e0689256 502 new = add_to_sequence (XEXP (pattern, 1), &new->success, newpos);
ec65fa66 503 newpos[depth] = '2';
e0689256
RK
504 new = add_to_sequence (XEXP (pattern, 2), &new->success, newpos);
505 return new;
506
507 case EQ: case NE: case LE: case LT: case GE: case GT:
508 case LEU: case LTU: case GEU: case GTU:
509 /* If the first operand is (cc0), we don't have to do anything
510 special. */
511 if (GET_CODE (XEXP (pattern, 0)) == CC0)
512 break;
513
0f41302f 514 /* ... fall through ... */
e0689256
RK
515
516 case COMPARE:
517 /* Enforce the mode on the first operand to avoid ambiguous insns. */
518 newpos[depth] = '0';
519 new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
520 this->success.first->enforce_mode = 1;
521 newpos[depth] = '1';
522 new = add_to_sequence (XEXP (pattern, 1), &new->success, newpos);
ec65fa66 523 return new;
76d31c63
JL
524
525 default:
526 break;
ec65fa66
RK
527 }
528
529 fmt = GET_RTX_FORMAT (code);
530 len = GET_RTX_LENGTH (code);
531 for (i = 0; i < len; i++)
532 {
533 newpos[depth] = '0' + i;
534 if (fmt[i] == 'e' || fmt[i] == 'u')
e0689256 535 new = add_to_sequence (XEXP (pattern, i), &new->success, newpos);
ec65fa66
RK
536 else if (fmt[i] == 'i' && i == 0)
537 {
538 this->test_elt_zero_int = 1;
539 this->elt_zero_int = XINT (pattern, i);
540 }
541 else if (fmt[i] == 'i' && i == 1)
542 {
543 this->test_elt_one_int = 1;
544 this->elt_one_int = XINT (pattern, i);
545 }
3d678dca
RS
546 else if (fmt[i] == 'w' && i == 0)
547 {
548 this->test_elt_zero_wide = 1;
549 this->elt_zero_wide = XWINT (pattern, i);
550 }
ec65fa66
RK
551 else if (fmt[i] == 'E')
552 {
553 register int j;
554 /* We do not handle a vector appearing as other than
555 the first item, just because nothing uses them
556 and by handling only the special case
557 we can use one element in newpos for either
558 the item number of a subexpression
559 or the element number in a vector. */
560 if (i != 0)
561 abort ();
562 this->veclen = XVECLEN (pattern, i);
563 for (j = 0; j < XVECLEN (pattern, i); j++)
564 {
565 newpos[depth] = 'a' + j;
566 new = add_to_sequence (XVECEXP (pattern, i, j),
e0689256 567 &new->success, newpos);
ec65fa66
RK
568 }
569 }
570 else if (fmt[i] != '0')
571 abort ();
572 }
573 return new;
574}
e0689256
RK
575\f
576/* Return 1 if we can prove that there is no RTL that can match both
577 D1 and D2. Otherwise, return 0 (it may be that there is an RTL that
578 can match both or just that we couldn't prove there wasn't such an RTL).
ec65fa66 579
e0689256
RK
580 TOPLEVEL is non-zero if we are to only look at the top level and not
581 recursively descend. */
ec65fa66 582
e0689256
RK
583static int
584not_both_true (d1, d2, toplevel)
585 struct decision *d1, *d2;
586 int toplevel;
ec65fa66 587{
e0689256
RK
588 struct decision *p1, *p2;
589
590 /* If they are both to test modes and the modes are different, they aren't
0f41302f 591 both true. Similarly for codes, integer elements, and vector lengths. */
e0689256
RK
592
593 if ((d1->enforce_mode && d2->enforce_mode
594 && d1->mode != VOIDmode && d2->mode != VOIDmode && d1->mode != d2->mode)
595 || (d1->code != UNKNOWN && d2->code != UNKNOWN && d1->code != d2->code)
596 || (d1->test_elt_zero_int && d2->test_elt_zero_int
597 && d1->elt_zero_int != d2->elt_zero_int)
598 || (d1->test_elt_one_int && d2->test_elt_one_int
599 && d1->elt_one_int != d2->elt_one_int)
3d678dca
RS
600 || (d1->test_elt_zero_wide && d2->test_elt_zero_wide
601 && d1->elt_zero_wide != d2->elt_zero_wide)
e0689256
RK
602 || (d1->veclen && d2->veclen && d1->veclen != d2->veclen))
603 return 1;
604
605 /* If either is a wild-card MATCH_OPERAND without a predicate, it can match
606 absolutely anything, so we can't say that no intersection is possible.
607 This case is detected by having a zero TESTS field with a code of
608 UNKNOWN. */
609
610 if ((d1->tests == 0 && d1->code == UNKNOWN)
611 || (d2->tests == 0 && d2->code == UNKNOWN))
612 return 0;
ec65fa66 613
e0689256
RK
614 /* If either has a predicate that we know something about, set things up so
615 that D1 is the one that always has a known predicate. Then see if they
616 have any codes in common. */
ec65fa66 617
e0689256 618 if (d1->pred >= 0 || d2->pred >= 0)
ec65fa66 619 {
e0689256
RK
620 int i, j;
621
622 if (d2->pred >= 0)
623 p1 = d1, d1 = d2, d2 = p1;
624
625 /* If D2 tests an explicit code, see if it is in the list of valid codes
626 for D1's predicate. */
627 if (d2->code != UNKNOWN)
ec65fa66 628 {
a23b64d5 629 for (i = 0; i < NUM_RTX_CODE && preds[d1->pred].codes[i] != 0; i++)
e0689256
RK
630 if (preds[d1->pred].codes[i] == d2->code)
631 break;
632
633 if (preds[d1->pred].codes[i] == 0)
634 return 1;
635 }
636
637 /* Otherwise see if the predicates have any codes in common. */
638
639 else if (d2->pred >= 0)
640 {
a23b64d5 641 for (i = 0; i < NUM_RTX_CODE && preds[d1->pred].codes[i] != 0; i++)
e0689256
RK
642 {
643 for (j = 0; j < NUM_RTX_CODE; j++)
644 if (preds[d2->pred].codes[j] == 0
645 || preds[d2->pred].codes[j] == preds[d1->pred].codes[i])
646 break;
647
648 if (preds[d2->pred].codes[j] != 0)
649 break;
650 }
651
652 if (preds[d1->pred].codes[i] == 0)
653 return 1;
ec65fa66 654 }
ec65fa66 655 }
ec65fa66 656
e0689256
RK
657 /* If we got here, we can't prove that D1 and D2 cannot both be true.
658 If we are only to check the top level, return 0. Otherwise, see if
659 we can prove that all choices in both successors are mutually
660 exclusive. If either does not have any successors, we can't prove
661 they can't both be true. */
ec65fa66 662
e0689256
RK
663 if (toplevel || d1->success.first == 0 || d2->success.first == 0)
664 return 0;
665
666 for (p1 = d1->success.first; p1; p1 = p1->next)
667 for (p2 = d2->success.first; p2; p2 = p2->next)
668 if (! not_both_true (p1, p2, 0))
669 return 0;
670
671 return 1;
672}
673\f
674/* Assuming that we can reorder all the alternatives at a specific point in
675 the tree (see discussion in merge_trees), we would prefer an ordering of
676 nodes where groups of consecutive nodes test the same mode and, within each
677 mode, groups of nodes test the same code. With this order, we can
678 construct nested switch statements, the inner one to test the code and
679 the outer one to test the mode.
680
681 We would like to list nodes testing for specific codes before those
682 that test predicates to avoid unnecessary function calls. Similarly,
6dc42e49 683 tests for specific modes should precede nodes that allow any mode.
e0689256
RK
684
685 This function returns the merit (with 0 being the best) of inserting
686 a test involving the specified MODE and CODE after node P. If P is
687 zero, we are to determine the merit of inserting the test at the front
688 of the list. */
689
690static int
691position_merit (p, mode, code)
692 struct decision *p;
693 enum machine_mode mode;
7967c666 694 enum rtx_code code;
ec65fa66 695{
e0689256 696 enum machine_mode p_mode;
ec65fa66 697
e0689256
RK
698 /* The only time the front of the list is anything other than the worst
699 position is if we are testing a mode that isn't VOIDmode. */
700 if (p == 0)
701 return mode == VOIDmode ? 3 : 2;
ec65fa66 702
e0689256 703 p_mode = p->enforce_mode ? p->mode : VOIDmode;
ec65fa66 704
e0689256
RK
705 /* The best case is if the codes and modes both match. */
706 if (p_mode == mode && p->code== code)
707 return 0;
ec65fa66 708
e0689256
RK
709 /* If the codes don't match, the next best case is if the modes match.
710 In that case, the best position for this node depends on whether
711 we are testing for a specific code or not. If we are, the best place
712 is after some other test for an explicit code and our mode or after
713 the last test in the previous mode if every test in our mode is for
714 an unknown code.
715
716 If we are testing for UNKNOWN, then the next best case is at the end of
717 our mode. */
718
719 if ((code != UNKNOWN
720 && ((p_mode == mode && p->code != UNKNOWN)
721 || (p_mode != mode && p->next
722 && (p->next->enforce_mode ? p->next->mode : VOIDmode) == mode
723 && (p->next->code == UNKNOWN))))
724 || (code == UNKNOWN && p_mode == mode
725 && (p->next == 0
726 || (p->next->enforce_mode ? p->next->mode : VOIDmode) != mode)))
727 return 1;
728
729 /* The third best case occurs when nothing is testing MODE. If MODE
730 is not VOIDmode, then the third best case is after something of any
731 mode that is not VOIDmode. If we are testing VOIDmode, the third best
732 place is the end of the list. */
733
734 if (p_mode != mode
735 && ((mode != VOIDmode && p_mode != VOIDmode)
736 || (mode == VOIDmode && p->next == 0)))
737 return 2;
738
739 /* Otherwise, we have the worst case. */
740 return 3;
741}
742\f
743/* Merge two decision tree listheads OLDH and ADDH,
744 modifying OLDH destructively, and return the merged tree. */
745
746static struct decision_head
747merge_trees (oldh, addh)
748 register struct decision_head oldh, addh;
749{
750 struct decision *add, *next;
751
752 if (oldh.first == 0)
753 return addh;
754
755 if (addh.first == 0)
756 return oldh;
ec65fa66 757
e0689256
RK
758 /* If we are adding things at different positions, something is wrong. */
759 if (strcmp (oldh.first->position, addh.first->position))
760 abort ();
761
762 for (add = addh.first; add; add = next)
ec65fa66 763 {
e0689256
RK
764 enum machine_mode add_mode = add->enforce_mode ? add->mode : VOIDmode;
765 struct decision *best_position = 0;
766 int best_merit = 4;
767 struct decision *old;
768
769 next = add->next;
770
771 /* The semantics of pattern matching state that the tests are done in
772 the order given in the MD file so that if an insn matches two
773 patterns, the first one will be used. However, in practice, most,
774 if not all, patterns are unambiguous so that their order is
775 independent. In that case, we can merge identical tests and
776 group all similar modes and codes together.
777
778 Scan starting from the end of OLDH until we reach a point
779 where we reach the head of the list or where we pass a pattern
780 that could also be true if NEW is true. If we find an identical
781 pattern, we can merge them. Also, record the last node that tests
782 the same code and mode and the last one that tests just the same mode.
783
784 If we have no match, place NEW after the closest match we found. */
785
786 for (old = oldh.last; old; old = old->prev)
ec65fa66 787 {
e0689256
RK
788 int our_merit;
789
790 /* If we don't have anything to test except an additional test,
791 do not consider the two nodes equal. If we did, the test below
792 would cause an infinite recursion. */
793 if (old->tests == 0 && old->test_elt_zero_int == 0
794 && old->test_elt_one_int == 0 && old->veclen == 0
3d678dca 795 && old->test_elt_zero_wide == 0
e0689256
RK
796 && old->dupno == -1 && old->mode == VOIDmode
797 && old->code == UNKNOWN
798 && (old->c_test != 0 || add->c_test != 0))
799 ;
800
801 else if ((old->tests == add->tests
802 || (old->pred >= 0 && old->pred == add->pred)
803 || (old->tests && add->tests
804 && !strcmp (old->tests, add->tests)))
3d678dca
RS
805 && old->test_elt_zero_int == add->test_elt_zero_int
806 && old->elt_zero_int == add->elt_zero_int
807 && old->test_elt_one_int == add->test_elt_one_int
808 && old->elt_one_int == add->elt_one_int
809 && old->test_elt_zero_wide == add->test_elt_zero_wide
810 && old->elt_zero_wide == add->elt_zero_wide
811 && old->veclen == add->veclen
812 && old->dupno == add->dupno
813 && old->opno == add->opno
814 && old->code == add->code
815 && old->enforce_mode == add->enforce_mode
816 && old->mode == add->mode)
e0689256
RK
817 {
818 /* If the additional test is not the same, split both nodes
819 into nodes that just contain all things tested before the
820 additional test and nodes that contain the additional test
821 and actions when it is true. This optimization is important
822 because of the case where we have almost identical patterns
823 with different tests on target flags. */
824
825 if (old->c_test != add->c_test
826 && ! (old->c_test && add->c_test
827 && !strcmp (old->c_test, add->c_test)))
828 {
829 if (old->insn_code_number >= 0 || old->opno >= 0)
830 {
831 struct decision *split
832 = (struct decision *) xmalloc (sizeof (struct decision));
833
7967c666
RK
834 mybcopy ((char *) old, (char *) split,
835 sizeof (struct decision));
e0689256
RK
836
837 old->success.first = old->success.last = split;
838 old->c_test = 0;
839 old->opno = -1;
840 old->insn_code_number = -1;
841 old->num_clobbers_to_add = 0;
842
843 split->number = next_number++;
844 split->next = split->prev = 0;
845 split->mode = VOIDmode;
846 split->code = UNKNOWN;
847 split->veclen = 0;
848 split->test_elt_zero_int = 0;
849 split->test_elt_one_int = 0;
3d678dca 850 split->test_elt_zero_wide = 0;
e0689256
RK
851 split->tests = 0;
852 split->pred = -1;
4805ff59 853 split->dupno = -1;
e0689256
RK
854 }
855
856 if (add->insn_code_number >= 0 || add->opno >= 0)
857 {
858 struct decision *split
859 = (struct decision *) xmalloc (sizeof (struct decision));
860
7967c666
RK
861 mybcopy ((char *) add, (char *) split,
862 sizeof (struct decision));
e0689256
RK
863
864 add->success.first = add->success.last = split;
865 add->c_test = 0;
866 add->opno = -1;
867 add->insn_code_number = -1;
868 add->num_clobbers_to_add = 0;
869
870 split->number = next_number++;
871 split->next = split->prev = 0;
872 split->mode = VOIDmode;
873 split->code = UNKNOWN;
874 split->veclen = 0;
875 split->test_elt_zero_int = 0;
876 split->test_elt_one_int = 0;
3d678dca 877 split->test_elt_zero_wide = 0;
e0689256
RK
878 split->tests = 0;
879 split->pred = -1;
4805ff59 880 split->dupno = -1;
e0689256
RK
881 }
882 }
883
e0689256 884 if (old->insn_code_number >= 0 && add->insn_code_number >= 0)
de6a431b
RK
885 {
886 /* If one node is for a normal insn and the second is
887 for the base insn with clobbers stripped off, the
888 second node should be ignored. */
889
890 if (old->num_clobbers_to_add == 0
891 && add->num_clobbers_to_add > 0)
892 /* Nothing to do here. */
893 ;
894 else if (old->num_clobbers_to_add > 0
895 && add->num_clobbers_to_add == 0)
896 {
897 /* In this case, replace OLD with ADD. */
898 old->insn_code_number = add->insn_code_number;
899 old->num_clobbers_to_add = 0;
900 }
901 else
902 fatal ("Two actions at one point in tree");
903 }
904
e0689256
RK
905 if (old->insn_code_number == -1)
906 old->insn_code_number = add->insn_code_number;
de6a431b 907 old->success = merge_trees (old->success, add->success);
e0689256 908 add = 0;
ec65fa66 909 break;
e0689256
RK
910 }
911
912 /* Unless we have already found the best possible insert point,
913 see if this position is better. If so, record it. */
914
915 if (best_merit != 0
916 && ((our_merit = position_merit (old, add_mode, add->code))
917 < best_merit))
918 best_merit = our_merit, best_position = old;
919
920 if (! not_both_true (old, add, 0))
921 break;
ec65fa66 922 }
ec65fa66 923
e0689256
RK
924 /* If ADD was duplicate, we are done. */
925 if (add == 0)
926 continue;
ec65fa66 927
e0689256
RK
928 /* Otherwise, find the best place to insert ADD. Normally this is
929 BEST_POSITION. However, if we went all the way to the top of
930 the list, it might be better to insert at the top. */
ec65fa66 931
e0689256
RK
932 if (best_position == 0)
933 abort ();
ec65fa66 934
3d678dca
RS
935 if (old == 0
936 && position_merit (NULL_PTR, add_mode, add->code) < best_merit)
e0689256
RK
937 {
938 add->prev = 0;
939 add->next = oldh.first;
940 oldh.first->prev = add;
941 oldh.first = add;
942 }
ec65fa66 943
e0689256
RK
944 else
945 {
946 add->prev = best_position;
947 add->next = best_position->next;
948 best_position->next = add;
949 if (best_position == oldh.last)
950 oldh.last = add;
951 else
952 add->next->prev = add;
953 }
954 }
ec65fa66 955
e0689256 956 return oldh;
ec65fa66
RK
957}
958\f
e0689256
RK
959/* Count the number of subnodes of HEAD. If the number is high enough,
960 make the first node in HEAD start a separate subroutine in the C code
961 that is generated.
ec65fa66 962
e0689256
RK
963 TYPE gives the type of routine we are writing.
964
965 INITIAL is non-zero if this is the highest-level node. We never write
966 it out here. */
ec65fa66
RK
967
968static int
e0689256
RK
969break_out_subroutines (head, type, initial)
970 struct decision_head head;
ec65fa66 971 enum routine_type type;
e0689256 972 int initial;
ec65fa66
RK
973{
974 int size = 0;
87bd0490 975 struct decision *sub;
e0689256
RK
976
977 for (sub = head.first; sub; sub = sub->next)
978 size += 1 + break_out_subroutines (sub->success, type, 0);
979
980 if (size > SUBROUTINE_THRESHOLD && ! initial)
ec65fa66 981 {
e0689256
RK
982 head.first->subroutine_number = ++next_subroutine_number;
983 write_subroutine (head.first, type);
ec65fa66
RK
984 size = 1;
985 }
986 return size;
987}
e0689256
RK
988\f
989/* Write out a subroutine of type TYPE to do comparisons starting at node
990 TREE. */
ec65fa66
RK
991
992static void
993write_subroutine (tree, type)
994 struct decision *tree;
995 enum routine_type type;
996{
e0689256 997 int i;
ec65fa66
RK
998
999 if (type == SPLIT)
e0689256 1000 printf ("rtx\nsplit");
ec65fa66 1001 else
e0689256
RK
1002 printf ("int\nrecog");
1003
1004 if (tree != 0 && tree->subroutine_number > 0)
1005 printf ("_%d", tree->subroutine_number);
1006 else if (type == SPLIT)
1007 printf ("_insns");
1008
1009 printf (" (x0, insn");
1010 if (type == RECOG)
1011 printf (", pnum_clobbers");
1012
1013 printf (")\n");
cab634f2 1014 printf (" register rtx x0;\n rtx insn ATTRIBUTE_UNUSED;\n");
e0689256 1015 if (type == RECOG)
cab634f2 1016 printf (" int *pnum_clobbers ATTRIBUTE_UNUSED;\n");
ec65fa66
RK
1017
1018 printf ("{\n");
1019 printf (" register rtx *ro = &recog_operand[0];\n");
e0689256
RK
1020
1021 printf (" register rtx ");
1022 for (i = 1; i < max_depth; i++)
cab634f2 1023 printf ("x%d ATTRIBUTE_UNUSED, ", i);
e0689256 1024
cab634f2
KG
1025 printf ("x%d ATTRIBUTE_UNUSED;\n", max_depth);
1026 printf (" %s tem ATTRIBUTE_UNUSED;\n", type == SPLIT ? "rtx" : "int");
3d678dca 1027 write_tree (tree, "", NULL_PTR, 1, type);
ec65fa66
RK
1028 printf (" ret0: return %d;\n}\n\n", type == SPLIT ? 0 : -1);
1029}
1030\f
e0689256
RK
1031/* This table is used to indent the recog_* functions when we are inside
1032 conditions or switch statements. We only support small indentations
1033 and always indent at least two spaces. */
1034
1035static char *indents[]
1036 = {" ", " ", " ", " ", " ", " ", " ", " ",
1037 "\t", "\t ", "\t ", "\t ", "\t ", "\t ", "\t ",
1038 "\t\t", "\t\t ", "\t\t ", "\t\t ", "\t\t ", "\t\t "};
1039
1040/* Write out C code to perform the decisions in TREE for a subroutine of
1041 type TYPE. If all of the choices fail, branch to node AFTERWARD, if
1042 non-zero, otherwise return. PREVPOS is the position of the node that
1043 branched to this test.
1044
1045 When we merged all alternatives, we tried to set up a convenient order.
1046 Specifically, tests involving the same mode are all grouped together,
1047 followed by a group that does not contain a mode test. Within each group
1048 of the same mode, we also group tests with the same code, followed by a
1049 group that does not test a code.
1050
6dc42e49 1051 Occasionally, we cannot arbitrarily reorder the tests so that multiple
e0689256
RK
1052 sequence of groups as described above are present.
1053
1054 We generate two nested switch statements, the outer statement for
1055 testing modes, and the inner switch for testing RTX codes. It is
1056 not worth optimizing cases when only a small number of modes or
1057 codes is tested, since the compiler can do that when compiling the
1058 resulting function. We do check for when every test is the same mode
1059 or code. */
ec65fa66 1060
7967c666 1061static void
e0689256 1062write_tree_1 (tree, prevpos, afterward, type)
ec65fa66
RK
1063 struct decision *tree;
1064 char *prevpos;
e0689256 1065 struct decision *afterward;
ec65fa66
RK
1066 enum routine_type type;
1067{
1068 register struct decision *p, *p1;
e0689256
RK
1069 register int depth = tree ? strlen (tree->position) : 0;
1070 enum machine_mode switch_mode = VOIDmode;
1071 RTX_CODE switch_code = UNKNOWN;
1072 int uncond = 0;
ec65fa66
RK
1073 char modemap[NUM_MACHINE_MODES];
1074 char codemap[NUM_RTX_CODE];
e0689256
RK
1075 int indent = 2;
1076 int i;
1077
1078 /* One tricky area is what is the exact state when we branch to a
1079 node's label. There are two cases where we branch: when looking at
1080 successors to a node, or when a set of tests fails.
1081
1082 In the former case, we are always branching to the first node in a
1083 decision list and we want all required tests to be performed. We
1084 put the labels for such nodes in front of any switch or test statements.
1085 These branches are done without updating the position to that of the
1086 target node.
1087
1088 In the latter case, we are branching to a node that is not the first
1089 node in a decision list. We have already checked that it is possible
1090 for both the node we originally tested at this level and the node we
38e01259 1091 are branching to to both match some pattern. That means that they
e0689256
RK
1092 usually will be testing the same mode and code. So it is normally safe
1093 for such labels to be inside switch statements, since the tests done
1094 by virtue of arriving at that label will usually already have been
1095 done. The exception is a branch from a node that does not test a
1096 mode or code to one that does. In such cases, we set the `retest_mode'
1097 or `retest_code' flags. That will ensure that we start a new switch
1098 at that position and put the label before the switch.
1099
1100 The branches in the latter case must set the position to that of the
1101 target node. */
ec65fa66 1102
ec65fa66 1103
e0689256
RK
1104 printf ("\n");
1105 if (tree && tree->subroutine_number == 0)
1106 {
1107 printf (" L%d:\n", tree->number);
1108 tree->label_needed = 0;
1109 }
1110
1111 if (tree)
1112 {
1113 change_state (prevpos, tree->position, 2);
1114 prevpos = tree->position;
1115 }
1116
ec65fa66
RK
1117 for (p = tree; p; p = p->next)
1118 {
e0689256 1119 enum machine_mode mode = p->enforce_mode ? p->mode : VOIDmode;
cba998bf
RK
1120 int need_bracket;
1121 int wrote_bracket = 0;
e0689256
RK
1122 int inner_indent;
1123
1124 if (p->success.first == 0 && p->insn_code_number < 0)
1125 abort ();
1126
1127 /* Find the next alternative to p that might be true when p is true.
1128 Test that one next if p's successors fail. */
1129
1130 for (p1 = p->next; p1 && not_both_true (p, p1, 1); p1 = p1->next)
1131 ;
ec65fa66 1132 p->afterward = p1;
ec65fa66 1133
e0689256 1134 if (p1)
ec65fa66 1135 {
e0689256
RK
1136 if (mode == VOIDmode && p1->enforce_mode && p1->mode != VOIDmode)
1137 p1->retest_mode = 1;
1138 if (p->code == UNKNOWN && p1->code != UNKNOWN)
1139 p1->retest_code = 1;
1140 p1->label_needed = 1;
ec65fa66 1141 }
e0689256
RK
1142
1143 /* If we have a different code or mode than the last node and
1144 are in a switch on codes, we must either end the switch or
1145 go to another case. We must also end the switch if this
1146 node needs a label and to retest either the mode or code. */
1147
1148 if (switch_code != UNKNOWN
1149 && (switch_code != p->code || switch_mode != mode
1150 || (p->label_needed && (p->retest_mode || p->retest_code))))
ec65fa66 1151 {
e0689256
RK
1152 enum rtx_code code = p->code;
1153
1154 /* If P is testing a predicate that we know about and we haven't
1155 seen any of the codes that are valid for the predicate, we
1156 can write a series of "case" statement, one for each possible
1157 code. Since we are already in a switch, these redundant tests
0f41302f 1158 are very cheap and will reduce the number of predicate called. */
e0689256
RK
1159
1160 if (p->pred >= 0)
1161 {
a23b64d5 1162 for (i = 0; i < NUM_RTX_CODE && preds[p->pred].codes[i] != 0; i++)
e0689256
RK
1163 if (codemap[(int) preds[p->pred].codes[i]])
1164 break;
1165
1166 if (preds[p->pred].codes[i] == 0)
1167 code = MATCH_OPERAND;
1168 }
1169
1170 if (code == UNKNOWN || codemap[(int) code]
1171 || switch_mode != mode
1172 || (p->label_needed && (p->retest_mode || p->retest_code)))
1173 {
1174 printf ("%s}\n", indents[indent - 2]);
1175 switch_code = UNKNOWN;
1176 indent -= 4;
1177 }
1178 else
1179 {
1180 if (! uncond)
1181 printf ("%sbreak;\n", indents[indent]);
1182
1183 if (code == MATCH_OPERAND)
1184 {
a23b64d5 1185 for (i = 0; i < NUM_RTX_CODE && preds[p->pred].codes[i] != 0; i++)
e0689256
RK
1186 {
1187 printf ("%scase ", indents[indent - 2]);
1188 print_code (preds[p->pred].codes[i]);
1189 printf (":\n");
1190 codemap[(int) preds[p->pred].codes[i]] = 1;
1191 }
1192 }
1193 else
1194 {
1195 printf ("%scase ", indents[indent - 2]);
1196 print_code (code);
1197 printf (":\n");
1198 codemap[(int) p->code] = 1;
1199 }
1200
1201 switch_code = code;
1202 }
1203
1204 uncond = 0;
ec65fa66
RK
1205 }
1206
e0689256
RK
1207 /* If we were previously in a switch on modes and now have a different
1208 mode, end at least the case, and maybe end the switch if we are
1209 not testing a mode or testing a mode whose case we already saw. */
ec65fa66 1210
e0689256
RK
1211 if (switch_mode != VOIDmode
1212 && (switch_mode != mode || (p->label_needed && p->retest_mode)))
1213 {
1214 if (mode == VOIDmode || modemap[(int) mode]
1215 || (p->label_needed && p->retest_mode))
1216 {
1217 printf ("%s}\n", indents[indent - 2]);
1218 switch_mode = VOIDmode;
1219 indent -= 4;
1220 }
1221 else
1222 {
1223 if (! uncond)
1224 printf (" break;\n");
1225 printf (" case %smode:\n", GET_MODE_NAME (mode));
1226 switch_mode = mode;
1227 modemap[(int) mode] = 1;
1228 }
1229
1230 uncond = 0;
1231 }
1232
1233 /* If we are about to write dead code, something went wrong. */
1234 if (! p->label_needed && uncond)
ec65fa66
RK
1235 abort ();
1236
e0689256
RK
1237 /* If we need a label and we will want to retest the mode or code at
1238 that label, write the label now. We have already ensured that
1239 things will be valid for the test. */
1240
1241 if (p->label_needed && (p->retest_mode || p->retest_code))
1242 {
1243 printf ("%sL%d:\n", indents[indent - 2], p->number);
1244 p->label_needed = 0;
1245 }
1246
1247 uncond = 0;
ec65fa66 1248
e0689256
RK
1249 /* If we are not in any switches, see if we can shortcut things
1250 by checking for identical modes and codes. */
ec65fa66 1251
e0689256 1252 if (switch_mode == VOIDmode && switch_code == UNKNOWN)
ec65fa66
RK
1253 {
1254 /* If p and its alternatives all want the same mode,
1255 reject all others at once, first, then ignore the mode. */
e0689256
RK
1256
1257 if (mode != VOIDmode && p->next && same_modes (p, mode))
ec65fa66
RK
1258 {
1259 printf (" if (GET_MODE (x%d) != %smode)\n",
1260 depth, GET_MODE_NAME (p->mode));
1261 if (afterward)
1262 {
e0689256
RK
1263 printf (" {\n");
1264 change_state (p->position, afterward->position, 6);
1265 printf (" goto L%d;\n }\n", afterward->number);
ec65fa66
RK
1266 }
1267 else
1268 printf (" goto ret0;\n");
1269 clear_modes (p);
e0689256 1270 mode = VOIDmode;
ec65fa66
RK
1271 }
1272
1273 /* If p and its alternatives all want the same code,
1274 reject all others at once, first, then ignore the code. */
e0689256 1275
ec65fa66
RK
1276 if (p->code != UNKNOWN && p->next && same_codes (p, p->code))
1277 {
1278 printf (" if (GET_CODE (x%d) != ", depth);
1279 print_code (p->code);
1280 printf (")\n");
1281 if (afterward)
1282 {
e0689256
RK
1283 printf (" {\n");
1284 change_state (p->position, afterward->position, indent + 4);
1285 printf (" goto L%d;\n }\n", afterward->number);
ec65fa66
RK
1286 }
1287 else
1288 printf (" goto ret0;\n");
1289 clear_codes (p);
1290 }
1291 }
1292
e0689256
RK
1293 /* If we are not in a mode switch and we are testing for a specific
1294 mode, start a mode switch unless we have just one node or the next
1295 node is not testing a mode (we have already tested for the case of
1296 more than one mode, but all of the same mode). */
ec65fa66 1297
e0689256
RK
1298 if (switch_mode == VOIDmode && mode != VOIDmode && p->next != 0
1299 && p->next->enforce_mode && p->next->mode != VOIDmode)
1300 {
ec65fa66 1301 mybzero (modemap, sizeof modemap);
e0689256
RK
1302 printf ("%sswitch (GET_MODE (x%d))\n", indents[indent], depth);
1303 printf ("%s{\n", indents[indent + 2]);
1304 indent += 4;
76d31c63
JL
1305 printf ("%sdefault:\n%sbreak;\n", indents[indent - 2],
1306 indents[indent]);
e0689256
RK
1307 printf ("%scase %smode:\n", indents[indent - 2],
1308 GET_MODE_NAME (mode));
1309 modemap[(int) mode] = 1;
1310 switch_mode = mode;
ec65fa66
RK
1311 }
1312
e0689256
RK
1313 /* Similarly for testing codes. */
1314
1315 if (switch_code == UNKNOWN && p->code != UNKNOWN && ! p->ignore_code
1316 && p->next != 0 && p->next->code != UNKNOWN)
ec65fa66 1317 {
ec65fa66 1318 mybzero (codemap, sizeof codemap);
e0689256
RK
1319 printf ("%sswitch (GET_CODE (x%d))\n", indents[indent], depth);
1320 printf ("%s{\n", indents[indent + 2]);
1321 indent += 4;
76d31c63
JL
1322 printf ("%sdefault:\n%sbreak;\n", indents[indent - 2],
1323 indents[indent]);
e0689256
RK
1324 printf ("%scase ", indents[indent - 2]);
1325 print_code (p->code);
1326 printf (":\n");
1327 codemap[(int) p->code] = 1;
1328 switch_code = p->code;
ec65fa66
RK
1329 }
1330
e0689256 1331 /* Now that most mode and code tests have been done, we can write out
0f41302f 1332 a label for an inner node, if we haven't already. */
e0689256
RK
1333 if (p->label_needed)
1334 printf ("%sL%d:\n", indents[indent - 2], p->number);
1335
1336 inner_indent = indent;
1337
1338 /* The only way we can have to do a mode or code test here is if
1339 this node needs such a test but is the only node to be tested.
1340 In that case, we won't have started a switch. Note that this is
1341 the only way the switch and test modes can disagree. */
ec65fa66 1342
e0689256
RK
1343 if ((mode != switch_mode && ! p->ignore_mode)
1344 || (p->code != switch_code && p->code != UNKNOWN && ! p->ignore_code)
3d678dca
RS
1345 || p->test_elt_zero_int || p->test_elt_one_int
1346 || p->test_elt_zero_wide || p->veclen
e0689256 1347 || p->dupno >= 0 || p->tests || p->num_clobbers_to_add)
ec65fa66 1348 {
e0689256
RK
1349 printf ("%sif (", indents[indent]);
1350
1351 if (mode != switch_mode && ! p->ignore_mode)
1352 printf ("GET_MODE (x%d) == %smode && ",
1353 depth, GET_MODE_NAME (mode));
1354 if (p->code != switch_code && p->code != UNKNOWN && ! p->ignore_code)
ec65fa66
RK
1355 {
1356 printf ("GET_CODE (x%d) == ", depth);
1357 print_code (p->code);
e0689256 1358 printf (" && ");
ec65fa66 1359 }
e0689256
RK
1360
1361 if (p->test_elt_zero_int)
1362 printf ("XINT (x%d, 0) == %d && ", depth, p->elt_zero_int);
1363 if (p->test_elt_one_int)
1364 printf ("XINT (x%d, 1) == %d && ", depth, p->elt_one_int);
3d678dca 1365 if (p->test_elt_zero_wide)
b030d598
RK
1366 {
1367 /* Set offset to 1 iff the number might get propagated to
1368 unsigned long by ANSI C rules, else 0.
1369 Prospective hosts are required to have at least 32 bit
1370 ints, and integer constants in machine descriptions
1371 must fit in 32 bit, thus it suffices to check only
1372 for 1 << 31 . */
1373 HOST_WIDE_INT offset = p->elt_zero_wide == -2147483647 - 1;
76d31c63
JL
1374 printf ("XWINT (x%d, 0) == ", depth);
1375 printf (HOST_WIDE_INT_PRINT_DEC, p->elt_zero_wide + offset);
1376 printf ("%s && ", offset ? "-1" : "");
b030d598 1377 }
e0689256
RK
1378 if (p->veclen)
1379 printf ("XVECLEN (x%d, 0) == %d && ", depth, p->veclen);
1380 if (p->dupno >= 0)
1381 printf ("rtx_equal_p (x%d, ro[%d]) && ", depth, p->dupno);
1382 if (p->num_clobbers_to_add)
1383 printf ("pnum_clobbers != 0 && ");
1384 if (p->tests)
1385 printf ("%s (x%d, %smode)", p->tests, depth,
1386 GET_MODE_NAME (p->mode));
1387 else
1388 printf ("1");
1389
1390 printf (")\n");
1391 inner_indent += 2;
ec65fa66 1392 }
ec65fa66 1393 else
e0689256 1394 uncond = 1;
ec65fa66 1395
cba998bf
RK
1396 need_bracket = ! uncond;
1397
ec65fa66 1398 if (p->opno >= 0)
e0689256 1399 {
cba998bf
RK
1400 if (need_bracket)
1401 {
1402 printf ("%s{\n", indents[inner_indent]);
1403 inner_indent += 2;
1404 wrote_bracket = 1;
1405 need_bracket = 0;
1406 }
1407
1408 printf ("%sro[%d] = x%d;\n", indents[inner_indent], p->opno, depth);
e0689256 1409 }
ec65fa66
RK
1410
1411 if (p->c_test)
e0689256
RK
1412 {
1413 printf ("%sif (%s)\n", indents[inner_indent], p->c_test);
1414 inner_indent += 2;
1415 uncond = 0;
cba998bf 1416 need_bracket = 1;
e0689256 1417 }
ec65fa66
RK
1418
1419 if (p->insn_code_number >= 0)
1420 {
1421 if (type == SPLIT)
e0689256
RK
1422 printf ("%sreturn gen_split_%d (operands);\n",
1423 indents[inner_indent], p->insn_code_number);
ec65fa66
RK
1424 else
1425 {
1426 if (p->num_clobbers_to_add)
1427 {
cba998bf 1428 if (need_bracket)
e0689256
RK
1429 {
1430 printf ("%s{\n", indents[inner_indent]);
1431 inner_indent += 2;
1432 }
1433
1434 printf ("%s*pnum_clobbers = %d;\n",
1435 indents[inner_indent], p->num_clobbers_to_add);
1436 printf ("%sreturn %d;\n",
1437 indents[inner_indent], p->insn_code_number);
1438
cba998bf 1439 if (need_bracket)
e0689256
RK
1440 {
1441 inner_indent -= 2;
1442 printf ("%s}\n", indents[inner_indent]);
1443 }
ec65fa66
RK
1444 }
1445 else
e0689256
RK
1446 printf ("%sreturn %d;\n",
1447 indents[inner_indent], p->insn_code_number);
ec65fa66
RK
1448 }
1449 }
1450 else
e0689256
RK
1451 printf ("%sgoto L%d;\n", indents[inner_indent],
1452 p->success.first->number);
ec65fa66 1453
cba998bf 1454 if (wrote_bracket)
e0689256
RK
1455 printf ("%s}\n", indents[inner_indent - 2]);
1456 }
ec65fa66 1457
e0689256 1458 /* We have now tested all alternatives. End any switches we have open
cba998bf
RK
1459 and branch to the alternative node unless we know that we can't fall
1460 through to the branch. */
ec65fa66 1461
e0689256
RK
1462 if (switch_code != UNKNOWN)
1463 {
1464 printf ("%s}\n", indents[indent - 2]);
1465 indent -= 4;
cba998bf 1466 uncond = 0;
e0689256 1467 }
ec65fa66 1468
e0689256
RK
1469 if (switch_mode != VOIDmode)
1470 {
1471 printf ("%s}\n", indents[indent - 2]);
1472 indent -= 4;
cba998bf 1473 uncond = 0;
ec65fa66
RK
1474 }
1475
e0689256
RK
1476 if (indent != 2)
1477 abort ();
ec65fa66 1478
cba998bf
RK
1479 if (uncond)
1480 return;
1481
ec65fa66
RK
1482 if (afterward)
1483 {
e0689256
RK
1484 change_state (prevpos, afterward->position, 2);
1485 printf (" goto L%d;\n", afterward->number);
ec65fa66
RK
1486 }
1487 else
1488 printf (" goto ret0;\n");
ec65fa66
RK
1489}
1490
1491static void
1492print_code (code)
7967c666 1493 enum rtx_code code;
ec65fa66
RK
1494{
1495 register char *p1;
1496 for (p1 = GET_RTX_NAME (code); *p1; p1++)
1497 {
1498 if (*p1 >= 'a' && *p1 <= 'z')
1499 putchar (*p1 + 'A' - 'a');
1500 else
1501 putchar (*p1);
1502 }
1503}
1504
1505static int
1506same_codes (p, code)
1507 register struct decision *p;
7967c666 1508 register enum rtx_code code;
ec65fa66
RK
1509{
1510 for (; p; p = p->next)
1511 if (p->code != code)
1512 return 0;
1513
1514 return 1;
1515}
1516
1517static void
1518clear_codes (p)
1519 register struct decision *p;
1520{
1521 for (; p; p = p->next)
e0689256 1522 p->ignore_code = 1;
ec65fa66
RK
1523}
1524
1525static int
1526same_modes (p, mode)
1527 register struct decision *p;
1528 register enum machine_mode mode;
1529{
1530 for (; p; p = p->next)
cba998bf 1531 if ((p->enforce_mode ? p->mode : VOIDmode) != mode)
ec65fa66
RK
1532 return 0;
1533
1534 return 1;
1535}
1536
1537static void
1538clear_modes (p)
1539 register struct decision *p;
1540{
1541 for (; p; p = p->next)
e0689256 1542 p->enforce_mode = 0;
ec65fa66
RK
1543}
1544\f
e0689256
RK
1545/* Write out the decision tree starting at TREE for a subroutine of type TYPE.
1546
1547 PREVPOS is the position at the node that branched to this node.
1548
1549 INITIAL is nonzero if this is the first node we are writing in a subroutine.
1550
1551 If all nodes are false, branch to the node AFTERWARD. */
1552
1553static void
1554write_tree (tree, prevpos, afterward, initial, type)
1555 struct decision *tree;
1556 char *prevpos;
1557 struct decision *afterward;
1558 int initial;
1559 enum routine_type type;
1560{
1561 register struct decision *p;
1562 char *name_prefix = (type == SPLIT ? "split" : "recog");
1563 char *call_suffix = (type == SPLIT ? "" : ", pnum_clobbers");
1564
1565 if (! initial && tree->subroutine_number > 0)
1566 {
1567 printf (" L%d:\n", tree->number);
1568
1569 if (afterward)
1570 {
1571 printf (" tem = %s_%d (x0, insn%s);\n",
1572 name_prefix, tree->subroutine_number, call_suffix);
71bde1f3
RS
1573 if (type == SPLIT)
1574 printf (" if (tem != 0) return tem;\n");
1575 else
1576 printf (" if (tem >= 0) return tem;\n");
e0689256
RK
1577 change_state (tree->position, afterward->position, 2);
1578 printf (" goto L%d;\n", afterward->number);
1579 }
1580 else
1581 printf (" return %s_%d (x0, insn%s);\n",
1582 name_prefix, tree->subroutine_number, call_suffix);
1583 return;
1584 }
1585
1586 write_tree_1 (tree, prevpos, afterward, type);
1587
1588 for (p = tree; p; p = p->next)
1589 if (p->success.first)
1590 write_tree (p->success.first, p->position,
1591 p->afterward ? p->afterward : afterward, 0, type);
1592}
1593
1594\f
1595/* Assuming that the state of argument is denoted by OLDPOS, take whatever
1596 actions are necessary to move to NEWPOS.
1597
1598 INDENT says how many blanks to place at the front of lines. */
1599
ec65fa66 1600static void
e0689256 1601change_state (oldpos, newpos, indent)
ec65fa66
RK
1602 char *oldpos;
1603 char *newpos;
e0689256 1604 int indent;
ec65fa66
RK
1605{
1606 int odepth = strlen (oldpos);
1607 int depth = odepth;
1608 int ndepth = strlen (newpos);
1609
1610 /* Pop up as many levels as necessary. */
1611
1612 while (strncmp (oldpos, newpos, depth))
1613 --depth;
1614
1615 /* Go down to desired level. */
1616
1617 while (depth < ndepth)
1618 {
1619 if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
e0689256
RK
1620 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1621 indents[indent], depth + 1, depth, newpos[depth] - 'a');
ec65fa66 1622 else
e0689256
RK
1623 printf ("%sx%d = XEXP (x%d, %c);\n",
1624 indents[indent], depth + 1, depth, newpos[depth]);
ec65fa66
RK
1625 ++depth;
1626 }
1627}
1628\f
1629static char *
1630copystr (s1)
1631 char *s1;
1632{
1633 register char *tem;
1634
1635 if (s1 == 0)
1636 return 0;
1637
1638 tem = (char *) xmalloc (strlen (s1) + 1);
1639 strcpy (tem, s1);
1640
1641 return tem;
1642}
1643
1644static void
1645mybzero (b, length)
1646 register char *b;
1647 register unsigned length;
1648{
1649 while (length-- > 0)
1650 *b++ = 0;
1651}
1652
e0689256
RK
1653static void
1654mybcopy (in, out, length)
1655 register char *in, *out;
1656 register unsigned length;
1657{
1658 while (length-- > 0)
1659 *out++ = *in++;
1660}
1661
ec65fa66
RK
1662char *
1663xrealloc (ptr, size)
1664 char *ptr;
1665 unsigned size;
1666{
1667 char *result = (char *) realloc (ptr, size);
1668 if (!result)
1669 fatal ("virtual memory exhausted");
1670 return result;
1671}
1672
1673char *
1674xmalloc (size)
1675 unsigned size;
1676{
1677 register char *val = (char *) malloc (size);
1678
1679 if (val == 0)
1680 fatal ("virtual memory exhausted");
1681 return val;
1682}
1683
1684static void
320e7c40 1685fatal VPROTO ((char *format, ...))
ec65fa66 1686{
320e7c40
KG
1687#ifndef __STDC__
1688 char *format;
1689#endif
1690 va_list ap;
1691
1692 VA_START (ap, format);
1693
1694#ifndef __STDC__
1695 format = va_arg (ap, char *);
1696#endif
1697
ec65fa66 1698 fprintf (stderr, "genrecog: ");
320e7c40
KG
1699 vfprintf (stderr, format, ap);
1700 va_end (ap);
ec65fa66 1701 fprintf (stderr, "\n");
de6a431b 1702 fprintf (stderr, "after %d definitions\n", next_index);
ec65fa66
RK
1703 exit (FATAL_EXIT_CODE);
1704}
1705
1706/* More 'friendly' abort that prints the line and file.
1707 config.h can #define abort fancy_abort if you like that sort of thing. */
1708
1709void
1710fancy_abort ()
1711{
1712 fatal ("Internal gcc abort.");
1713}
1714\f
1715int
1716main (argc, argv)
1717 int argc;
1718 char **argv;
1719{
1720 rtx desc;
e0689256
RK
1721 struct decision_head recog_tree;
1722 struct decision_head split_tree;
ec65fa66 1723 FILE *infile;
ec65fa66
RK
1724 register int c;
1725
1726 obstack_init (rtl_obstack);
e0689256 1727 recog_tree.first = recog_tree.last = split_tree.first = split_tree.last = 0;
ec65fa66
RK
1728
1729 if (argc <= 1)
1730 fatal ("No input file name.");
1731
1732 infile = fopen (argv[1], "r");
1733 if (infile == 0)
1734 {
1735 perror (argv[1]);
1736 exit (FATAL_EXIT_CODE);
1737 }
1738
1739 init_rtl ();
1740 next_insn_code = 0;
1741 next_index = 0;
1742
1743 printf ("/* Generated automatically by the program `genrecog'\n\
1744from the machine description file `md'. */\n\n");
1745
1746 printf ("#include \"config.h\"\n");
729da3f5 1747 printf ("#include \"system.h\"\n");
ec65fa66
RK
1748 printf ("#include \"rtl.h\"\n");
1749 printf ("#include \"insn-config.h\"\n");
1750 printf ("#include \"recog.h\"\n");
1751 printf ("#include \"real.h\"\n");
1752 printf ("#include \"output.h\"\n");
1753 printf ("#include \"flags.h\"\n");
1754 printf ("\n");
1755
1756 /* Read the machine description. */
1757
1758 while (1)
1759 {
1760 c = read_skip_spaces (infile);
1761 if (c == EOF)
1762 break;
1763 ungetc (c, infile);
1764
1765 desc = read_rtx (infile);
1766 if (GET_CODE (desc) == DEFINE_INSN)
e0689256
RK
1767 recog_tree = merge_trees (recog_tree,
1768 make_insn_sequence (desc, RECOG));
ec65fa66 1769 else if (GET_CODE (desc) == DEFINE_SPLIT)
e0689256
RK
1770 split_tree = merge_trees (split_tree,
1771 make_insn_sequence (desc, SPLIT));
ec65fa66
RK
1772 if (GET_CODE (desc) == DEFINE_PEEPHOLE
1773 || GET_CODE (desc) == DEFINE_EXPAND)
1774 next_insn_code++;
1775 next_index++;
1776 }
1777
1778 printf ("\n\
1779/* `recog' contains a decision tree\n\
1780 that recognizes whether the rtx X0 is a valid instruction.\n\
1781\n\
1782 recog returns -1 if the rtx is not valid.\n\
1783 If the rtx is valid, recog returns a nonnegative number\n\
1784 which is the insn code number for the pattern that matched.\n");
1785 printf (" This is the same as the order in the machine description of\n\
01a7aaea 1786 the entry that matched. This number can be used as an index into various\n\
ec65fa66
RK
1787 insn_* tables, such as insn_templates, insn_outfun, and insn_n_operands\n\
1788 (found in insn-output.c).\n\n");
1789 printf (" The third argument to recog is an optional pointer to an int.\n\
1790 If present, recog will accept a pattern if it matches except for\n\
1791 missing CLOBBER expressions at the end. In that case, the value\n\
1792 pointed to by the optional pointer will be set to the number of\n\
1793 CLOBBERs that need to be added (it should be initialized to zero by\n\
1794 the caller). If it is set nonzero, the caller should allocate a\n\
1795 PARALLEL of the appropriate size, copy the initial entries, and call\n\
1796 add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.");
1797
e0689256 1798 if (split_tree.first)
ec65fa66
RK
1799 printf ("\n\n The function split_insns returns 0 if the rtl could not\n\
1800 be split or the split rtl in a SEQUENCE if it can be.");
1801
1802 printf ("*/\n\n");
1803
1804 printf ("rtx recog_operand[MAX_RECOG_OPERANDS];\n\n");
1805 printf ("rtx *recog_operand_loc[MAX_RECOG_OPERANDS];\n\n");
1806 printf ("rtx *recog_dup_loc[MAX_DUP_OPERANDS];\n\n");
1807 printf ("char recog_dup_num[MAX_DUP_OPERANDS];\n\n");
1808 printf ("#define operands recog_operand\n\n");
1809
1810 next_subroutine_number = 0;
e0689256
RK
1811 break_out_subroutines (recog_tree, RECOG, 1);
1812 write_subroutine (recog_tree.first, RECOG);
ec65fa66
RK
1813
1814 next_subroutine_number = 0;
e0689256
RK
1815 break_out_subroutines (split_tree, SPLIT, 1);
1816 write_subroutine (split_tree.first, SPLIT);
ec65fa66
RK
1817
1818 fflush (stdout);
1819 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
1820 /* NOTREACHED */
1821 return 0;
1822}
This page took 0.83476 seconds and 5 git commands to generate.