+
+/* A subroutine of expand_asm_operands. Check that all operands have
+ the same number of alternatives. Return true if so. */
+
+static bool
+check_operand_nalternatives (outputs, inputs)
+ tree outputs, inputs;
+{
+ if (outputs || inputs)
+ {
+ tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
+ int nalternatives
+ = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
+ tree next = inputs;
+
+ if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
+ {
+ error ("too many alternatives in `asm'");
+ return false;
+ }
+
+ tmp = outputs;
+ while (tmp)
+ {
+ const char *constraint
+ = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
+
+ if (n_occurrences (',', constraint) != nalternatives)
+ {
+ error ("operand constraints for `asm' differ in number of alternatives");
+ return false;
+ }
+
+ if (TREE_CHAIN (tmp))
+ tmp = TREE_CHAIN (tmp);
+ else
+ tmp = next, next = 0;
+ }
+ }
+
+ return true;
+}
+
+/* A subroutine of expand_asm_operands. Check that all operand names
+ are unique. Return true if so. We rely on the fact that these names
+ are identifiers, and so have been canonicalized by get_identifier,
+ so all we need are pointer comparisons. */
+
+static bool
+check_unique_operand_names (outputs, inputs)
+ tree outputs, inputs;
+{
+ tree i, j;
+
+ for (i = outputs; i ; i = TREE_CHAIN (i))
+ {
+ tree i_name = TREE_PURPOSE (TREE_PURPOSE (i));
+ if (! i_name)
+ continue;
+
+ for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
+ if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
+ goto failure;
+ }
+
+ for (i = inputs; i ; i = TREE_CHAIN (i))
+ {
+ tree i_name = TREE_PURPOSE (TREE_PURPOSE (i));
+ if (! i_name)
+ continue;
+
+ for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
+ if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
+ goto failure;
+ for (j = outputs; j ; j = TREE_CHAIN (j))
+ if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
+ goto failure;
+ }
+
+ return true;
+
+ failure:
+ error ("duplicate asm operand name '%s'",
+ TREE_STRING_POINTER (TREE_PURPOSE (TREE_PURPOSE (i))));
+ return false;
+}
+
+/* A subroutine of expand_asm_operands. Resolve the names of the operands
+ in *POUTPUTS and *PINPUTS to numbers, and replace the name expansions in
+ STRING and in the constraints to those numbers. */
+
+static tree
+resolve_operand_names (string, outputs, inputs, pconstraints)
+ tree string;
+ tree outputs, inputs;
+ const char **pconstraints;
+{
+ char *buffer = xstrdup (TREE_STRING_POINTER (string));
+ char *p;
+ tree t;
+
+ /* Assume that we will not need extra space to perform the substitution.
+ This because we get to remove '[' and ']', which means we cannot have
+ a problem until we have more than 999 operands. */
+
+ p = buffer;
+ while ((p = strchr (p, '%')) != NULL)
+ {
+ if (p[1] == '[')
+ p += 1;
+ else if (ISALPHA (p[1]) && p[2] == '[')
+ p += 2;
+ else
+ {
+ p += 1;
+ continue;
+ }
+
+ p = resolve_operand_name_1 (p, outputs, inputs);
+ }
+
+ string = build_string (strlen (buffer), buffer);
+ free (buffer);
+
+ /* Collect output constraints here because it's convenient.
+ There should be no named operands here; this is verified
+ in expand_asm_operand. */
+ for (t = outputs; t ; t = TREE_CHAIN (t), pconstraints++)
+ *pconstraints = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
+
+ /* Substitute [<name>] in input constraint strings. */
+ for (t = inputs; t ; t = TREE_CHAIN (t), pconstraints++)
+ {
+ const char *c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
+ if (strchr (c, '[') == NULL)
+ *pconstraints = c;
+ else
+ {
+ p = buffer = xstrdup (c);
+ while ((p = strchr (p, '[')) != NULL)
+ p = resolve_operand_name_1 (p, outputs, inputs);
+
+ *pconstraints = ggc_alloc_string (buffer, -1);
+ free (buffer);
+ }
+ }
+
+ return string;
+}
+
+/* A subroutine of resolve_operand_names. P points to the '[' for a
+ potential named operand of the form [<name>]. In place, replace
+ the name and brackets with a number. Return a pointer to the
+ balance of the string after substitution. */
+
+static char *
+resolve_operand_name_1 (p, outputs, inputs)
+ char *p;
+ tree outputs, inputs;
+{
+ char *q;
+ int op;
+ tree t;
+ size_t len;
+
+ /* Collect the operand name. */
+ q = strchr (p, ']');
+ if (!q)
+ {
+ error ("missing close brace for named operand");
+ return strchr (p, '\0');
+ }
+ len = q - p - 1;
+
+ /* Resolve the name to a number. */
+ for (op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
+ {
+ tree name = TREE_PURPOSE (TREE_PURPOSE (t));
+ if (name)
+ {
+ const char *c = TREE_STRING_POINTER (name);
+ if (strncmp (c, p + 1, len) == 0 && c[len] == '\0')
+ goto found;
+ }
+ }
+ for (t = inputs; t ; t = TREE_CHAIN (t), op++)
+ {
+ tree name = TREE_PURPOSE (TREE_PURPOSE (t));
+ if (name)
+ {
+ const char *c = TREE_STRING_POINTER (name);
+ if (strncmp (c, p + 1, len) == 0 && c[len] == '\0')
+ goto found;
+ }
+ }
+
+ *q = '\0';
+ error ("undefined named operand '%s'", p + 1);
+ op = 0;
+ found:
+
+ /* Replace the name with the number. Unfortunately, not all libraries
+ get the return value of sprintf correct, so search for the end of the
+ generated string by hand. */
+ sprintf (p, "%d", op);
+ p = strchr (p, '\0');
+
+ /* Verify the no extra buffer space assumption. */
+ if (p > q)
+ abort ();
+
+ /* Shift the rest of the buffer down to fill the gap. */
+ memmove (p, q + 1, strlen (q + 1) + 1);
+
+ return p;
+}