xref: /freebsd-src/cddl/contrib/opensolaris/tools/ctf/cvt/merge.c (revision 75a813087a5bba4504efe6695411715f6352a8a8)
11673e404SJohn Birrell /*
21673e404SJohn Birrell  * CDDL HEADER START
31673e404SJohn Birrell  *
41673e404SJohn Birrell  * The contents of this file are subject to the terms of the
51673e404SJohn Birrell  * Common Development and Distribution License (the "License").
61673e404SJohn Birrell  * You may not use this file except in compliance with the License.
71673e404SJohn Birrell  *
81673e404SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91673e404SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
101673e404SJohn Birrell  * See the License for the specific language governing permissions
111673e404SJohn Birrell  * and limitations under the License.
121673e404SJohn Birrell  *
131673e404SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
141673e404SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151673e404SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
161673e404SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
171673e404SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
181673e404SJohn Birrell  *
191673e404SJohn Birrell  * CDDL HEADER END
201673e404SJohn Birrell  */
211673e404SJohn Birrell /*
221673e404SJohn Birrell  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
231673e404SJohn Birrell  * Use is subject to license terms.
241673e404SJohn Birrell  */
251673e404SJohn Birrell 
261673e404SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
271673e404SJohn Birrell 
281673e404SJohn Birrell /*
291673e404SJohn Birrell  * This file contains routines that merge one tdata_t tree, called the child,
301673e404SJohn Birrell  * into another, called the parent.  Note that these names are used mainly for
311673e404SJohn Birrell  * convenience and to represent the direction of the merge.  They are not meant
321673e404SJohn Birrell  * to imply any relationship between the tdata_t graphs prior to the merge.
331673e404SJohn Birrell  *
341673e404SJohn Birrell  * tdata_t structures contain two main elements - a hash of iidesc_t nodes, and
351673e404SJohn Birrell  * a directed graph of tdesc_t nodes, pointed to by the iidesc_t nodes.  Simply
361673e404SJohn Birrell  * put, we merge the tdesc_t graphs, followed by the iidesc_t nodes, and then we
371673e404SJohn Birrell  * clean up loose ends.
381673e404SJohn Birrell  *
391673e404SJohn Birrell  * The algorithm is as follows:
401673e404SJohn Birrell  *
411673e404SJohn Birrell  * 1. Mapping iidesc_t nodes
421673e404SJohn Birrell  *
431673e404SJohn Birrell  * For each child iidesc_t node, we first try to map its tdesc_t subgraph
441673e404SJohn Birrell  * against the tdesc_t graph in the parent.  For each node in the child subgraph
451673e404SJohn Birrell  * that exists in the parent, a mapping between the two (between their type IDs)
461673e404SJohn Birrell  * is established.  For the child nodes that cannot be mapped onto existing
471673e404SJohn Birrell  * parent nodes, a mapping is established between the child node ID and a
481673e404SJohn Birrell  * newly-allocated ID that the node will use when it is re-created in the
491673e404SJohn Birrell  * parent.  These unmappable nodes are added to the md_tdtba (tdesc_t To Be
501673e404SJohn Birrell  * Added) hash, which tracks nodes that need to be created in the parent.
511673e404SJohn Birrell  *
521673e404SJohn Birrell  * If all of the nodes in the subgraph for an iidesc_t in the child can be
531673e404SJohn Birrell  * mapped to existing nodes in the parent, then we can try to map the child
541673e404SJohn Birrell  * iidesc_t onto an iidesc_t in the parent.  If we cannot find an equivalent
551673e404SJohn Birrell  * iidesc_t, or if we were not able to completely map the tdesc_t subgraph(s),
561673e404SJohn Birrell  * then we add this iidesc_t to the md_iitba (iidesc_t To Be Added) list.  This
571673e404SJohn Birrell  * list tracks iidesc_t nodes that are to be created in the parent.
581673e404SJohn Birrell  *
591673e404SJohn Birrell  * While visiting the tdesc_t nodes, we may discover a forward declaration (a
601673e404SJohn Birrell  * FORWARD tdesc_t) in the parent that is resolved in the child.  That is, there
611673e404SJohn Birrell  * may be a structure or union definition in the child with the same name as the
621673e404SJohn Birrell  * forward declaration in the parent.  If we find such a node, we record an
631673e404SJohn Birrell  * association in the md_fdida (Forward => Definition ID Association) list
641673e404SJohn Birrell  * between the parent ID of the forward declaration and the ID that the
651673e404SJohn Birrell  * definition will use when re-created in the parent.
661673e404SJohn Birrell  *
671673e404SJohn Birrell  * 2. Creating new tdesc_t nodes (the md_tdtba hash)
681673e404SJohn Birrell  *
691673e404SJohn Birrell  * We have now attempted to map all tdesc_t nodes from the child into the
701673e404SJohn Birrell  * parent, and have, in md_tdtba, a hash of all tdesc_t nodes that need to be
711673e404SJohn Birrell  * created (or, as we so wittily call it, conjured) in the parent.  We iterate
721673e404SJohn Birrell  * through this hash, creating the indicated tdesc_t nodes.  For a given tdesc_t
731673e404SJohn Birrell  * node, conjuring requires two steps - the copying of the common tdesc_t data
741673e404SJohn Birrell  * (name, type, etc) from the child node, and the creation of links from the
751673e404SJohn Birrell  * newly-created node to the parent equivalents of other tdesc_t nodes pointed
761673e404SJohn Birrell  * to by node being conjured.  Note that in some cases, the targets of these
771673e404SJohn Birrell  * links will be on the md_tdtba hash themselves, and may not have been created
781673e404SJohn Birrell  * yet.  As such, we can't establish the links from these new nodes into the
791673e404SJohn Birrell  * parent graph.  We therefore conjure them with links to nodes in the *child*
801673e404SJohn Birrell  * graph, and add pointers to the links to be created to the md_tdtbr (tdesc_t
811673e404SJohn Birrell  * To Be Remapped) hash.  For example, a POINTER tdesc_t that could not be
821673e404SJohn Birrell  * resolved would have its &tdesc_t->t_tdesc added to md_tdtbr.
831673e404SJohn Birrell  *
841673e404SJohn Birrell  * 3. Creating new iidesc_t nodes (the md_iitba list)
851673e404SJohn Birrell  *
861673e404SJohn Birrell  * When we have completed step 2, all tdesc_t nodes have been created (or
871673e404SJohn Birrell  * already existed) in the parent.  Some of them may have incorrect links (the
881673e404SJohn Birrell  * members of the md_tdtbr list), but they've all been created.  As such, we can
891673e404SJohn Birrell  * create all of the iidesc_t nodes, as we can attach the tdesc_t subgraph
901673e404SJohn Birrell  * pointers correctly.  We create each node, and attach the pointers to the
911673e404SJohn Birrell  * appropriate parts of the parent tdesc_t graph.
921673e404SJohn Birrell  *
931673e404SJohn Birrell  * 4. Resolving newly-created tdesc_t node links (the md_tdtbr list)
941673e404SJohn Birrell  *
951673e404SJohn Birrell  * As in step 3, we rely on the fact that all of the tdesc_t nodes have been
961673e404SJohn Birrell  * created.  Each entry in the md_tdtbr list is a pointer to where a link into
971673e404SJohn Birrell  * the parent will be established.  As saved in the md_tdtbr list, these
981673e404SJohn Birrell  * pointers point into the child tdesc_t subgraph.  We can thus get the target
991673e404SJohn Birrell  * type ID from the child, look at the ID mapping to determine the desired link
1001673e404SJohn Birrell  * target, and redirect the link accordingly.
1011673e404SJohn Birrell  *
1021673e404SJohn Birrell  * 5. Parent => child forward declaration resolution
1031673e404SJohn Birrell  *
1041673e404SJohn Birrell  * If entries were made in the md_fdida list in step 1, we have forward
1051673e404SJohn Birrell  * declarations in the parent that need to be resolved to their definitions
1061673e404SJohn Birrell  * re-created in step 2 from the child.  Using the md_fdida list, we can locate
1071673e404SJohn Birrell  * the definition for the forward declaration, and we can redirect all inbound
1081673e404SJohn Birrell  * edges to the forward declaration node to the actual definition.
1091673e404SJohn Birrell  *
1101673e404SJohn Birrell  * A pox on the house of anyone who changes the algorithm without updating
1111673e404SJohn Birrell  * this comment.
1121673e404SJohn Birrell  */
1131673e404SJohn Birrell 
1141673e404SJohn Birrell #include <stdio.h>
1151673e404SJohn Birrell #include <strings.h>
1161673e404SJohn Birrell #include <assert.h>
1171673e404SJohn Birrell #include <pthread.h>
1181673e404SJohn Birrell 
1191673e404SJohn Birrell #include "ctf_headers.h"
1201673e404SJohn Birrell #include "ctftools.h"
1211673e404SJohn Birrell #include "list.h"
1221673e404SJohn Birrell #include "alist.h"
1231673e404SJohn Birrell #include "memory.h"
1241673e404SJohn Birrell #include "traverse.h"
1251673e404SJohn Birrell 
1261673e404SJohn Birrell typedef struct equiv_data equiv_data_t;
1271673e404SJohn Birrell typedef struct merge_cb_data merge_cb_data_t;
1281673e404SJohn Birrell 
1291673e404SJohn Birrell /*
1301673e404SJohn Birrell  * There are two traversals in this file, for equivalency and for tdesc_t
1311673e404SJohn Birrell  * re-creation, that do not fit into the tdtraverse() framework.  We have our
1321673e404SJohn Birrell  * own traversal mechanism and ops vector here for those two cases.
1331673e404SJohn Birrell  */
1341673e404SJohn Birrell typedef struct tdesc_ops {
1354cc75139SJohn Birrell 	const char *name;
1361673e404SJohn Birrell 	int (*equiv)(tdesc_t *, tdesc_t *, equiv_data_t *);
1371673e404SJohn Birrell 	tdesc_t *(*conjure)(tdesc_t *, int, merge_cb_data_t *);
1381673e404SJohn Birrell } tdesc_ops_t;
1391673e404SJohn Birrell extern tdesc_ops_t tdesc_ops[];
1401673e404SJohn Birrell 
1411673e404SJohn Birrell /*
1421673e404SJohn Birrell  * The workhorse structure of tdata_t merging.  Holds all lists of nodes to be
1431673e404SJohn Birrell  * processed during various phases of the merge algorithm.
1441673e404SJohn Birrell  */
1451673e404SJohn Birrell struct merge_cb_data {
1461673e404SJohn Birrell 	tdata_t *md_parent;
1471673e404SJohn Birrell 	tdata_t *md_tgt;
1481673e404SJohn Birrell 	alist_t *md_ta;		/* Type Association */
1491673e404SJohn Birrell 	alist_t *md_fdida;	/* Forward -> Definition ID Association */
1501673e404SJohn Birrell 	list_t	**md_iitba;	/* iidesc_t nodes To Be Added to the parent */
1511673e404SJohn Birrell 	hash_t	*md_tdtba;	/* tdesc_t nodes To Be Added to the parent */
1521673e404SJohn Birrell 	list_t	**md_tdtbr;	/* tdesc_t nodes To Be Remapped */
1531673e404SJohn Birrell 	int md_flags;
1541673e404SJohn Birrell }; /* merge_cb_data_t */
1551673e404SJohn Birrell 
1561673e404SJohn Birrell /*
1571673e404SJohn Birrell  * When we first create a tdata_t from stabs data, we will have duplicate nodes.
1581673e404SJohn Birrell  * Normal merges, however, assume that the child tdata_t is already self-unique,
1591673e404SJohn Birrell  * and for speed reasons do not attempt to self-uniquify.  If this flag is set,
1601673e404SJohn Birrell  * the merge algorithm will self-uniquify by avoiding the insertion of
1611673e404SJohn Birrell  * duplicates in the md_tdtdba list.
1621673e404SJohn Birrell  */
1631673e404SJohn Birrell #define	MCD_F_SELFUNIQUIFY	0x1
1641673e404SJohn Birrell 
1651673e404SJohn Birrell /*
1661673e404SJohn Birrell  * When we merge the CTF data for the modules, we don't want it to contain any
1671673e404SJohn Birrell  * data that can be found in the reference module (usually genunix).  If this
1681673e404SJohn Birrell  * flag is set, we're doing a merge between the fully merged tdata_t for this
1691673e404SJohn Birrell  * module and the tdata_t for the reference module, with the data unique to this
1701673e404SJohn Birrell  * module ending up in a third tdata_t.  It is this third tdata_t that will end
1711673e404SJohn Birrell  * up in the .SUNW_ctf section for the module.
1721673e404SJohn Birrell  */
1731673e404SJohn Birrell #define	MCD_F_REFMERGE	0x2
1741673e404SJohn Birrell 
1751673e404SJohn Birrell /*
1761673e404SJohn Birrell  * Mapping of child type IDs to parent type IDs
1771673e404SJohn Birrell  */
1781673e404SJohn Birrell 
1791673e404SJohn Birrell static void
add_mapping(alist_t * ta,tid_t srcid,tid_t tgtid)1801673e404SJohn Birrell add_mapping(alist_t *ta, tid_t srcid, tid_t tgtid)
1811673e404SJohn Birrell {
1824cc75139SJohn Birrell 	debug(3, "Adding mapping %u <%x> => %u <%x>\n", srcid, srcid, tgtid, tgtid);
1831673e404SJohn Birrell 
1844cc75139SJohn Birrell 	assert(!alist_find(ta, (void *)(uintptr_t)srcid, NULL));
1851673e404SJohn Birrell 	assert(srcid != 0 && tgtid != 0);
1861673e404SJohn Birrell 
1874cc75139SJohn Birrell 	alist_add(ta, (void *)(uintptr_t)srcid, (void *)(uintptr_t)tgtid);
1881673e404SJohn Birrell }
1891673e404SJohn Birrell 
1901673e404SJohn Birrell static tid_t
get_mapping(alist_t * ta,int srcid)1911673e404SJohn Birrell get_mapping(alist_t *ta, int srcid)
1921673e404SJohn Birrell {
1934cc75139SJohn Birrell 	void *ltgtid;
1941673e404SJohn Birrell 
1954cc75139SJohn Birrell 	if (alist_find(ta, (void *)(uintptr_t)srcid, (void **)&ltgtid))
1964cc75139SJohn Birrell 		return ((uintptr_t)ltgtid);
1971673e404SJohn Birrell 	else
1981673e404SJohn Birrell 		return (0);
1991673e404SJohn Birrell }
2001673e404SJohn Birrell 
2011673e404SJohn Birrell /*
2021673e404SJohn Birrell  * Determining equivalence of tdesc_t subgraphs
2031673e404SJohn Birrell  */
2041673e404SJohn Birrell 
2051673e404SJohn Birrell struct equiv_data {
2061673e404SJohn Birrell 	alist_t *ed_ta;
2071673e404SJohn Birrell 	tdesc_t *ed_node;
2081673e404SJohn Birrell 	tdesc_t *ed_tgt;
2091673e404SJohn Birrell 
2101673e404SJohn Birrell 	int ed_clear_mark;
2111673e404SJohn Birrell 	int ed_cur_mark;
2121673e404SJohn Birrell 	int ed_selfuniquify;
2131673e404SJohn Birrell }; /* equiv_data_t */
2141673e404SJohn Birrell 
2151673e404SJohn Birrell static int equiv_node(tdesc_t *, tdesc_t *, equiv_data_t *);
2161673e404SJohn Birrell 
2171673e404SJohn Birrell /*ARGSUSED2*/
2181673e404SJohn Birrell static int
equiv_intrinsic(tdesc_t * stdp,tdesc_t * ttdp,equiv_data_t * ed __unused)2194cc75139SJohn Birrell equiv_intrinsic(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed __unused)
2201673e404SJohn Birrell {
2211673e404SJohn Birrell 	intr_t *si = stdp->t_intr;
2221673e404SJohn Birrell 	intr_t *ti = ttdp->t_intr;
2231673e404SJohn Birrell 
2241673e404SJohn Birrell 	if (si->intr_type != ti->intr_type ||
2251673e404SJohn Birrell 	    si->intr_signed != ti->intr_signed ||
2261673e404SJohn Birrell 	    si->intr_offset != ti->intr_offset ||
2271673e404SJohn Birrell 	    si->intr_nbits != ti->intr_nbits)
2281673e404SJohn Birrell 		return (0);
2291673e404SJohn Birrell 
2301673e404SJohn Birrell 	if (si->intr_type == INTR_INT &&
2311673e404SJohn Birrell 	    si->intr_iformat != ti->intr_iformat)
2321673e404SJohn Birrell 		return (0);
2331673e404SJohn Birrell 	else if (si->intr_type == INTR_REAL &&
2341673e404SJohn Birrell 	    si->intr_fformat != ti->intr_fformat)
2351673e404SJohn Birrell 		return (0);
2361673e404SJohn Birrell 
2371673e404SJohn Birrell 	return (1);
2381673e404SJohn Birrell }
2391673e404SJohn Birrell 
2401673e404SJohn Birrell static int
equiv_plain(tdesc_t * stdp,tdesc_t * ttdp,equiv_data_t * ed)2411673e404SJohn Birrell equiv_plain(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
2421673e404SJohn Birrell {
2431673e404SJohn Birrell 	return (equiv_node(stdp->t_tdesc, ttdp->t_tdesc, ed));
2441673e404SJohn Birrell }
2451673e404SJohn Birrell 
2461673e404SJohn Birrell static int
equiv_function(tdesc_t * stdp,tdesc_t * ttdp,equiv_data_t * ed)2471673e404SJohn Birrell equiv_function(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
2481673e404SJohn Birrell {
2491673e404SJohn Birrell 	fndef_t *fn1 = stdp->t_fndef, *fn2 = ttdp->t_fndef;
2501673e404SJohn Birrell 	int i;
2511673e404SJohn Birrell 
2521673e404SJohn Birrell 	if (fn1->fn_nargs != fn2->fn_nargs ||
2531673e404SJohn Birrell 	    fn1->fn_vargs != fn2->fn_vargs)
2541673e404SJohn Birrell 		return (0);
2551673e404SJohn Birrell 
2561673e404SJohn Birrell 	if (!equiv_node(fn1->fn_ret, fn2->fn_ret, ed))
2571673e404SJohn Birrell 		return (0);
2581673e404SJohn Birrell 
2594cc75139SJohn Birrell 	for (i = 0; i < (int) fn1->fn_nargs; i++) {
2601673e404SJohn Birrell 		if (!equiv_node(fn1->fn_args[i], fn2->fn_args[i], ed))
2611673e404SJohn Birrell 			return (0);
2621673e404SJohn Birrell 	}
2631673e404SJohn Birrell 
2641673e404SJohn Birrell 	return (1);
2651673e404SJohn Birrell }
2661673e404SJohn Birrell 
2671673e404SJohn Birrell static int
equiv_array(tdesc_t * stdp,tdesc_t * ttdp,equiv_data_t * ed)2681673e404SJohn Birrell equiv_array(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
2691673e404SJohn Birrell {
2701673e404SJohn Birrell 	ardef_t *ar1 = stdp->t_ardef, *ar2 = ttdp->t_ardef;
2711673e404SJohn Birrell 
2721673e404SJohn Birrell 	if (!equiv_node(ar1->ad_contents, ar2->ad_contents, ed) ||
2731673e404SJohn Birrell 	    !equiv_node(ar1->ad_idxtype, ar2->ad_idxtype, ed))
2741673e404SJohn Birrell 		return (0);
2751673e404SJohn Birrell 
2761673e404SJohn Birrell 	if (ar1->ad_nelems != ar2->ad_nelems)
2771673e404SJohn Birrell 		return (0);
2781673e404SJohn Birrell 
2791673e404SJohn Birrell 	return (1);
2801673e404SJohn Birrell }
2811673e404SJohn Birrell 
2821673e404SJohn Birrell static int
equiv_su(tdesc_t * stdp,tdesc_t * ttdp,equiv_data_t * ed)2831673e404SJohn Birrell equiv_su(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
2841673e404SJohn Birrell {
2851673e404SJohn Birrell 	mlist_t *ml1 = stdp->t_members, *ml2 = ttdp->t_members;
2861673e404SJohn Birrell 
2871673e404SJohn Birrell 	while (ml1 && ml2) {
2881673e404SJohn Birrell 		if (ml1->ml_offset != ml2->ml_offset ||
289980b4d53SMark Johnston 		    strcmp(ml1->ml_name, ml2->ml_name) != 0 ||
290980b4d53SMark Johnston 		    ml1->ml_size != ml2->ml_size ||
2911673e404SJohn Birrell 		    !equiv_node(ml1->ml_type, ml2->ml_type, ed))
2921673e404SJohn Birrell 			return (0);
2931673e404SJohn Birrell 
2941673e404SJohn Birrell 		ml1 = ml1->ml_next;
2951673e404SJohn Birrell 		ml2 = ml2->ml_next;
2961673e404SJohn Birrell 	}
2971673e404SJohn Birrell 
2981673e404SJohn Birrell 	if (ml1 || ml2)
2991673e404SJohn Birrell 		return (0);
3001673e404SJohn Birrell 
3011673e404SJohn Birrell 	return (1);
3021673e404SJohn Birrell }
3031673e404SJohn Birrell 
3041673e404SJohn Birrell /*ARGSUSED2*/
3051673e404SJohn Birrell static int
equiv_enum(tdesc_t * stdp,tdesc_t * ttdp,equiv_data_t * ed __unused)3064cc75139SJohn Birrell equiv_enum(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed __unused)
3071673e404SJohn Birrell {
3081673e404SJohn Birrell 	elist_t *el1 = stdp->t_emem;
3091673e404SJohn Birrell 	elist_t *el2 = ttdp->t_emem;
3101673e404SJohn Birrell 
3111673e404SJohn Birrell 	while (el1 && el2) {
3121673e404SJohn Birrell 		if (el1->el_number != el2->el_number ||
3131673e404SJohn Birrell 		    strcmp(el1->el_name, el2->el_name) != 0)
3141673e404SJohn Birrell 			return (0);
3151673e404SJohn Birrell 
3161673e404SJohn Birrell 		el1 = el1->el_next;
3171673e404SJohn Birrell 		el2 = el2->el_next;
3181673e404SJohn Birrell 	}
3191673e404SJohn Birrell 
3201673e404SJohn Birrell 	if (el1 || el2)
3211673e404SJohn Birrell 		return (0);
3221673e404SJohn Birrell 
3231673e404SJohn Birrell 	return (1);
3241673e404SJohn Birrell }
3251673e404SJohn Birrell 
3261673e404SJohn Birrell /*ARGSUSED*/
3271673e404SJohn Birrell static int
equiv_assert(tdesc_t * stdp __unused,tdesc_t * ttdp __unused,equiv_data_t * ed __unused)3284cc75139SJohn Birrell equiv_assert(tdesc_t *stdp __unused, tdesc_t *ttdp __unused, equiv_data_t *ed __unused)
3291673e404SJohn Birrell {
3301673e404SJohn Birrell 	/* foul, evil, and very bad - this is a "shouldn't happen" */
3311673e404SJohn Birrell 	assert(1 == 0);
3321673e404SJohn Birrell 
3331673e404SJohn Birrell 	return (0);
3341673e404SJohn Birrell }
3351673e404SJohn Birrell 
3361673e404SJohn Birrell static int
fwd_equiv(tdesc_t * ctdp,tdesc_t * mtdp)3371673e404SJohn Birrell fwd_equiv(tdesc_t *ctdp, tdesc_t *mtdp)
3381673e404SJohn Birrell {
3391673e404SJohn Birrell 	tdesc_t *defn = (ctdp->t_type == FORWARD ? mtdp : ctdp);
3401673e404SJohn Birrell 
341*eb9da1adSMark Johnston 	return (defn->t_type == STRUCT || defn->t_type == UNION ||
342*eb9da1adSMark Johnston 	    defn->t_type == ENUM);
3431673e404SJohn Birrell }
3441673e404SJohn Birrell 
3451673e404SJohn Birrell static int
equiv_node(tdesc_t * ctdp,tdesc_t * mtdp,equiv_data_t * ed)3461673e404SJohn Birrell equiv_node(tdesc_t *ctdp, tdesc_t *mtdp, equiv_data_t *ed)
3471673e404SJohn Birrell {
3484cc75139SJohn Birrell 	int (*equiv)(tdesc_t *, tdesc_t *, equiv_data_t *);
3491673e404SJohn Birrell 	int mapping;
3501673e404SJohn Birrell 
351bd81e07dSMark Johnston 	if (ctdp->t_emark > ed->ed_clear_mark &&
3521673e404SJohn Birrell 	    mtdp->t_emark > ed->ed_clear_mark)
3531673e404SJohn Birrell 		return (ctdp->t_emark == mtdp->t_emark);
3541673e404SJohn Birrell 
3551673e404SJohn Birrell 	/*
3561673e404SJohn Birrell 	 * In normal (non-self-uniquify) mode, we don't want to do equivalency
3571673e404SJohn Birrell 	 * checking on a subgraph that has already been checked.  If a mapping
3581673e404SJohn Birrell 	 * has already been established for a given child node, we can simply
3591673e404SJohn Birrell 	 * compare the mapping for the child node with the ID of the parent
3601673e404SJohn Birrell 	 * node.  If we are in self-uniquify mode, then we're comparing two
3611673e404SJohn Birrell 	 * subgraphs within the child graph, and thus need to ignore any
3621673e404SJohn Birrell 	 * type mappings that have been created, as they are only valid into the
3631673e404SJohn Birrell 	 * parent.
3641673e404SJohn Birrell 	 */
3651673e404SJohn Birrell 	if ((mapping = get_mapping(ed->ed_ta, ctdp->t_id)) > 0 &&
3661673e404SJohn Birrell 	    mapping == mtdp->t_id && !ed->ed_selfuniquify)
3671673e404SJohn Birrell 		return (1);
3681673e404SJohn Birrell 
3691673e404SJohn Birrell 	if (!streq(ctdp->t_name, mtdp->t_name))
3701673e404SJohn Birrell 		return (0);
3711673e404SJohn Birrell 
3721673e404SJohn Birrell 	if (ctdp->t_type != mtdp->t_type) {
3731673e404SJohn Birrell 		if (ctdp->t_type == FORWARD || mtdp->t_type == FORWARD)
3741673e404SJohn Birrell 			return (fwd_equiv(ctdp, mtdp));
3751673e404SJohn Birrell 		else
3761673e404SJohn Birrell 			return (0);
3771673e404SJohn Birrell 	}
3781673e404SJohn Birrell 
3791673e404SJohn Birrell 	ctdp->t_emark = ed->ed_cur_mark;
3801673e404SJohn Birrell 	mtdp->t_emark = ed->ed_cur_mark;
3811673e404SJohn Birrell 	ed->ed_cur_mark++;
3821673e404SJohn Birrell 
3831673e404SJohn Birrell 	if ((equiv = tdesc_ops[ctdp->t_type].equiv) != NULL)
3841673e404SJohn Birrell 		return (equiv(ctdp, mtdp, ed));
3851673e404SJohn Birrell 
3861673e404SJohn Birrell 	return (1);
3871673e404SJohn Birrell }
3881673e404SJohn Birrell 
3891673e404SJohn Birrell /*
3901673e404SJohn Birrell  * We perform an equivalency check on two subgraphs by traversing through them
3911673e404SJohn Birrell  * in lockstep.  If a given node is equivalent in both the parent and the child,
3921673e404SJohn Birrell  * we mark it in both subgraphs, using the t_emark field, with a monotonically
3931673e404SJohn Birrell  * increasing number.  If, in the course of the traversal, we reach a node that
3941673e404SJohn Birrell  * we have visited and numbered during this equivalency check, we have a cycle.
3951673e404SJohn Birrell  * If the previously-visited nodes don't have the same emark, then the edges
3961673e404SJohn Birrell  * that brought us to these nodes are not equivalent, and so the check ends.
3971673e404SJohn Birrell  * If the emarks are the same, the edges are equivalent.  We then backtrack and
3981673e404SJohn Birrell  * continue the traversal.  If we have exhausted all edges in the subgraph, and
3991673e404SJohn Birrell  * have not found any inequivalent nodes, then the subgraphs are equivalent.
4001673e404SJohn Birrell  */
4011673e404SJohn Birrell static int
equiv_cb(void * bucket,void * arg)4021673e404SJohn Birrell equiv_cb(void *bucket, void *arg)
4031673e404SJohn Birrell {
4041673e404SJohn Birrell 	equiv_data_t *ed = arg;
4051673e404SJohn Birrell 	tdesc_t *mtdp = bucket;
4061673e404SJohn Birrell 	tdesc_t *ctdp = ed->ed_node;
4071673e404SJohn Birrell 
4081673e404SJohn Birrell 	ed->ed_clear_mark = ed->ed_cur_mark + 1;
4091673e404SJohn Birrell 	ed->ed_cur_mark = ed->ed_clear_mark + 1;
4101673e404SJohn Birrell 
4111673e404SJohn Birrell 	if (equiv_node(ctdp, mtdp, ed)) {
4124cc75139SJohn Birrell 		debug(3, "equiv_node matched %d <%x> %d <%x>\n",
4134cc75139SJohn Birrell 		    ctdp->t_id, ctdp->t_id, mtdp->t_id, mtdp->t_id);
4141673e404SJohn Birrell 		ed->ed_tgt = mtdp;
4151673e404SJohn Birrell 		/* matched.  stop looking */
4161673e404SJohn Birrell 		return (-1);
4171673e404SJohn Birrell 	}
4181673e404SJohn Birrell 
4191673e404SJohn Birrell 	return (0);
4201673e404SJohn Birrell }
4211673e404SJohn Birrell 
4221673e404SJohn Birrell /*ARGSUSED1*/
4231673e404SJohn Birrell static int
map_td_tree_pre(tdesc_t * ctdp,tdesc_t ** ctdpp __unused,void * private)4244cc75139SJohn Birrell map_td_tree_pre(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private)
4251673e404SJohn Birrell {
4261673e404SJohn Birrell 	merge_cb_data_t *mcd = private;
4271673e404SJohn Birrell 
4281673e404SJohn Birrell 	if (get_mapping(mcd->md_ta, ctdp->t_id) > 0)
4291673e404SJohn Birrell 		return (0);
4301673e404SJohn Birrell 
4311673e404SJohn Birrell 	return (1);
4321673e404SJohn Birrell }
4331673e404SJohn Birrell 
4341673e404SJohn Birrell /*ARGSUSED1*/
4351673e404SJohn Birrell static int
map_td_tree_post(tdesc_t * ctdp,tdesc_t ** ctdpp __unused,void * private)4364cc75139SJohn Birrell map_td_tree_post(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private)
4371673e404SJohn Birrell {
4381673e404SJohn Birrell 	merge_cb_data_t *mcd = private;
4391673e404SJohn Birrell 	equiv_data_t ed;
4401673e404SJohn Birrell 
4411673e404SJohn Birrell 	ed.ed_ta = mcd->md_ta;
4421673e404SJohn Birrell 	ed.ed_clear_mark = mcd->md_parent->td_curemark;
4431673e404SJohn Birrell 	ed.ed_cur_mark = mcd->md_parent->td_curemark + 1;
4441673e404SJohn Birrell 	ed.ed_node = ctdp;
4451673e404SJohn Birrell 	ed.ed_selfuniquify = 0;
4461673e404SJohn Birrell 
4474cc75139SJohn Birrell 	debug(3, "map_td_tree_post on %d <%x> %s\n", ctdp->t_id, ctdp->t_id,tdesc_name(ctdp));
4481673e404SJohn Birrell 
4491673e404SJohn Birrell 	if (hash_find_iter(mcd->md_parent->td_layouthash, ctdp,
4501673e404SJohn Birrell 	    equiv_cb, &ed) < 0) {
4511673e404SJohn Birrell 		/* We found an equivalent node */
4521673e404SJohn Birrell 		if (ed.ed_tgt->t_type == FORWARD && ctdp->t_type != FORWARD) {
4531673e404SJohn Birrell 			int id = mcd->md_tgt->td_nextid++;
4541673e404SJohn Birrell 
4554cc75139SJohn Birrell 			debug(3, "Creating new defn type %d <%x>\n", id, id);
4561673e404SJohn Birrell 			add_mapping(mcd->md_ta, ctdp->t_id, id);
4571673e404SJohn Birrell 			alist_add(mcd->md_fdida, (void *)(ulong_t)ed.ed_tgt,
4581673e404SJohn Birrell 			    (void *)(ulong_t)id);
4591673e404SJohn Birrell 			hash_add(mcd->md_tdtba, ctdp);
4601673e404SJohn Birrell 		} else
4611673e404SJohn Birrell 			add_mapping(mcd->md_ta, ctdp->t_id, ed.ed_tgt->t_id);
4621673e404SJohn Birrell 
4631673e404SJohn Birrell 	} else if (debug_level > 1 && hash_iter(mcd->md_parent->td_idhash,
4641673e404SJohn Birrell 	    equiv_cb, &ed) < 0) {
4651673e404SJohn Birrell 		/*
4661673e404SJohn Birrell 		 * We didn't find an equivalent node by looking through the
4671673e404SJohn Birrell 		 * layout hash, but we somehow found it by performing an
4681673e404SJohn Birrell 		 * exhaustive search through the entire graph.  This usually
4691673e404SJohn Birrell 		 * means that the "name" hash function is broken.
4701673e404SJohn Birrell 		 */
4711673e404SJohn Birrell 		aborterr("Second pass for %d (%s) == %d\n", ctdp->t_id,
4721673e404SJohn Birrell 		    tdesc_name(ctdp), ed.ed_tgt->t_id);
4731673e404SJohn Birrell 	} else {
4741673e404SJohn Birrell 		int id = mcd->md_tgt->td_nextid++;
4751673e404SJohn Birrell 
4764cc75139SJohn Birrell 		debug(3, "Creating new type %d <%x>\n", id, id);
4771673e404SJohn Birrell 		add_mapping(mcd->md_ta, ctdp->t_id, id);
4781673e404SJohn Birrell 		hash_add(mcd->md_tdtba, ctdp);
4791673e404SJohn Birrell 	}
4801673e404SJohn Birrell 
4811673e404SJohn Birrell 	mcd->md_parent->td_curemark = ed.ed_cur_mark + 1;
4821673e404SJohn Birrell 
4831673e404SJohn Birrell 	return (1);
4841673e404SJohn Birrell }
4851673e404SJohn Birrell 
4861673e404SJohn Birrell /*ARGSUSED1*/
4871673e404SJohn Birrell static int
map_td_tree_self_post(tdesc_t * ctdp,tdesc_t ** ctdpp __unused,void * private)4884cc75139SJohn Birrell map_td_tree_self_post(tdesc_t *ctdp, tdesc_t **ctdpp __unused, void *private)
4891673e404SJohn Birrell {
4901673e404SJohn Birrell 	merge_cb_data_t *mcd = private;
4911673e404SJohn Birrell 	equiv_data_t ed;
4921673e404SJohn Birrell 
4931673e404SJohn Birrell 	ed.ed_ta = mcd->md_ta;
4941673e404SJohn Birrell 	ed.ed_clear_mark = mcd->md_parent->td_curemark;
4951673e404SJohn Birrell 	ed.ed_cur_mark = mcd->md_parent->td_curemark + 1;
4961673e404SJohn Birrell 	ed.ed_node = ctdp;
4971673e404SJohn Birrell 	ed.ed_selfuniquify = 1;
4981673e404SJohn Birrell 	ed.ed_tgt = NULL;
4991673e404SJohn Birrell 
5001673e404SJohn Birrell 	if (hash_find_iter(mcd->md_tdtba, ctdp, equiv_cb, &ed) < 0) {
5014cc75139SJohn Birrell 		debug(3, "Self check found %d <%x> in %d <%x>\n", ctdp->t_id,
5024cc75139SJohn Birrell 		    ctdp->t_id, ed.ed_tgt->t_id, ed.ed_tgt->t_id);
5031673e404SJohn Birrell 		add_mapping(mcd->md_ta, ctdp->t_id,
5041673e404SJohn Birrell 		    get_mapping(mcd->md_ta, ed.ed_tgt->t_id));
5051673e404SJohn Birrell 	} else if (debug_level > 1 && hash_iter(mcd->md_tdtba,
5061673e404SJohn Birrell 	    equiv_cb, &ed) < 0) {
5071673e404SJohn Birrell 		/*
5081673e404SJohn Birrell 		 * We didn't find an equivalent node using the quick way (going
5091673e404SJohn Birrell 		 * through the hash normally), but we did find it by iterating
5101673e404SJohn Birrell 		 * through the entire hash.  This usually means that the hash
5111673e404SJohn Birrell 		 * function is broken.
5121673e404SJohn Birrell 		 */
5134cc75139SJohn Birrell 		aborterr("Self-unique second pass for %d <%x> (%s) == %d <%x>\n",
5144cc75139SJohn Birrell 		    ctdp->t_id, ctdp->t_id, tdesc_name(ctdp), ed.ed_tgt->t_id,
5154cc75139SJohn Birrell 		    ed.ed_tgt->t_id);
5161673e404SJohn Birrell 	} else {
5171673e404SJohn Birrell 		int id = mcd->md_tgt->td_nextid++;
5181673e404SJohn Birrell 
5194cc75139SJohn Birrell 		debug(3, "Creating new type %d <%x>\n", id, id);
5201673e404SJohn Birrell 		add_mapping(mcd->md_ta, ctdp->t_id, id);
5211673e404SJohn Birrell 		hash_add(mcd->md_tdtba, ctdp);
5221673e404SJohn Birrell 	}
5231673e404SJohn Birrell 
5241673e404SJohn Birrell 	mcd->md_parent->td_curemark = ed.ed_cur_mark + 1;
5251673e404SJohn Birrell 
5261673e404SJohn Birrell 	return (1);
5271673e404SJohn Birrell }
5281673e404SJohn Birrell 
5291673e404SJohn Birrell static tdtrav_cb_f map_pre[] = {
5301673e404SJohn Birrell 	NULL,
5311673e404SJohn Birrell 	map_td_tree_pre,	/* intrinsic */
5321673e404SJohn Birrell 	map_td_tree_pre,	/* pointer */
5331673e404SJohn Birrell 	map_td_tree_pre,	/* array */
5341673e404SJohn Birrell 	map_td_tree_pre,	/* function */
5351673e404SJohn Birrell 	map_td_tree_pre,	/* struct */
5361673e404SJohn Birrell 	map_td_tree_pre,	/* union */
5371673e404SJohn Birrell 	map_td_tree_pre,	/* enum */
5381673e404SJohn Birrell 	map_td_tree_pre,	/* forward */
5391673e404SJohn Birrell 	map_td_tree_pre,	/* typedef */
5401673e404SJohn Birrell 	tdtrav_assert,		/* typedef_unres */
5411673e404SJohn Birrell 	map_td_tree_pre,	/* volatile */
5421673e404SJohn Birrell 	map_td_tree_pre,	/* const */
5431673e404SJohn Birrell 	map_td_tree_pre		/* restrict */
5441673e404SJohn Birrell };
5451673e404SJohn Birrell 
5461673e404SJohn Birrell static tdtrav_cb_f map_post[] = {
5471673e404SJohn Birrell 	NULL,
5481673e404SJohn Birrell 	map_td_tree_post,	/* intrinsic */
5491673e404SJohn Birrell 	map_td_tree_post,	/* pointer */
5501673e404SJohn Birrell 	map_td_tree_post,	/* array */
5511673e404SJohn Birrell 	map_td_tree_post,	/* function */
5521673e404SJohn Birrell 	map_td_tree_post,	/* struct */
5531673e404SJohn Birrell 	map_td_tree_post,	/* union */
5541673e404SJohn Birrell 	map_td_tree_post,	/* enum */
5551673e404SJohn Birrell 	map_td_tree_post,	/* forward */
5561673e404SJohn Birrell 	map_td_tree_post,	/* typedef */
5571673e404SJohn Birrell 	tdtrav_assert,		/* typedef_unres */
5581673e404SJohn Birrell 	map_td_tree_post,	/* volatile */
5591673e404SJohn Birrell 	map_td_tree_post,	/* const */
5601673e404SJohn Birrell 	map_td_tree_post	/* restrict */
5611673e404SJohn Birrell };
5621673e404SJohn Birrell 
5631673e404SJohn Birrell static tdtrav_cb_f map_self_post[] = {
5641673e404SJohn Birrell 	NULL,
5651673e404SJohn Birrell 	map_td_tree_self_post,	/* intrinsic */
5661673e404SJohn Birrell 	map_td_tree_self_post,	/* pointer */
5671673e404SJohn Birrell 	map_td_tree_self_post,	/* array */
5681673e404SJohn Birrell 	map_td_tree_self_post,	/* function */
5691673e404SJohn Birrell 	map_td_tree_self_post,	/* struct */
5701673e404SJohn Birrell 	map_td_tree_self_post,	/* union */
5711673e404SJohn Birrell 	map_td_tree_self_post,	/* enum */
5721673e404SJohn Birrell 	map_td_tree_self_post,	/* forward */
5731673e404SJohn Birrell 	map_td_tree_self_post,	/* typedef */
5741673e404SJohn Birrell 	tdtrav_assert,		/* typedef_unres */
5751673e404SJohn Birrell 	map_td_tree_self_post,	/* volatile */
5761673e404SJohn Birrell 	map_td_tree_self_post,	/* const */
5771673e404SJohn Birrell 	map_td_tree_self_post	/* restrict */
5781673e404SJohn Birrell };
5791673e404SJohn Birrell 
5801673e404SJohn Birrell /*
5811673e404SJohn Birrell  * Determining equivalence of iidesc_t nodes
5821673e404SJohn Birrell  */
5831673e404SJohn Birrell 
5841673e404SJohn Birrell typedef struct iifind_data {
5851673e404SJohn Birrell 	iidesc_t *iif_template;
5861673e404SJohn Birrell 	alist_t *iif_ta;
5871673e404SJohn Birrell 	int iif_newidx;
5881673e404SJohn Birrell 	int iif_refmerge;
5891673e404SJohn Birrell } iifind_data_t;
5901673e404SJohn Birrell 
5911673e404SJohn Birrell /*
5921673e404SJohn Birrell  * Check to see if this iidesc_t (node) - the current one on the list we're
5931673e404SJohn Birrell  * iterating through - matches the target one (iif->iif_template).  Return -1
5941673e404SJohn Birrell  * if it matches, to stop the iteration.
5951673e404SJohn Birrell  */
5961673e404SJohn Birrell static int
iidesc_match(void * data,void * arg)5971673e404SJohn Birrell iidesc_match(void *data, void *arg)
5981673e404SJohn Birrell {
5991673e404SJohn Birrell 	iidesc_t *node = data;
6001673e404SJohn Birrell 	iifind_data_t *iif = arg;
6011673e404SJohn Birrell 	int i;
6021673e404SJohn Birrell 
6031673e404SJohn Birrell 	if (node->ii_type != iif->iif_template->ii_type ||
6041673e404SJohn Birrell 	    !streq(node->ii_name, iif->iif_template->ii_name) ||
6051673e404SJohn Birrell 	    node->ii_dtype->t_id != iif->iif_newidx)
6061673e404SJohn Birrell 		return (0);
6071673e404SJohn Birrell 
6081673e404SJohn Birrell 	if ((node->ii_type == II_SVAR || node->ii_type == II_SFUN) &&
6091673e404SJohn Birrell 	    !streq(node->ii_owner, iif->iif_template->ii_owner))
6101673e404SJohn Birrell 		return (0);
6111673e404SJohn Birrell 
6121673e404SJohn Birrell 	if (node->ii_nargs != iif->iif_template->ii_nargs)
6131673e404SJohn Birrell 		return (0);
6141673e404SJohn Birrell 
6151673e404SJohn Birrell 	for (i = 0; i < node->ii_nargs; i++) {
6161673e404SJohn Birrell 		if (get_mapping(iif->iif_ta,
6171673e404SJohn Birrell 		    iif->iif_template->ii_args[i]->t_id) !=
6181673e404SJohn Birrell 		    node->ii_args[i]->t_id)
6191673e404SJohn Birrell 			return (0);
6201673e404SJohn Birrell 	}
6211673e404SJohn Birrell 
6221673e404SJohn Birrell 	if (iif->iif_refmerge) {
6231673e404SJohn Birrell 		switch (iif->iif_template->ii_type) {
6241673e404SJohn Birrell 		case II_GFUN:
6251673e404SJohn Birrell 		case II_SFUN:
6261673e404SJohn Birrell 		case II_GVAR:
6271673e404SJohn Birrell 		case II_SVAR:
6281673e404SJohn Birrell 			debug(3, "suppressing duping of %d %s from %s\n",
6291673e404SJohn Birrell 			    iif->iif_template->ii_type,
6301673e404SJohn Birrell 			    iif->iif_template->ii_name,
6311673e404SJohn Birrell 			    (iif->iif_template->ii_owner ?
6321673e404SJohn Birrell 			    iif->iif_template->ii_owner : "NULL"));
6331673e404SJohn Birrell 			return (0);
6341673e404SJohn Birrell 		case II_NOT:
6351673e404SJohn Birrell 		case II_PSYM:
6361673e404SJohn Birrell 		case II_SOU:
6371673e404SJohn Birrell 		case II_TYPE:
6381673e404SJohn Birrell 			break;
6391673e404SJohn Birrell 		}
6401673e404SJohn Birrell 	}
6411673e404SJohn Birrell 
6421673e404SJohn Birrell 	return (-1);
6431673e404SJohn Birrell }
6441673e404SJohn Birrell 
6451673e404SJohn Birrell static int
merge_type_cb(void * data,void * arg)6461673e404SJohn Birrell merge_type_cb(void *data, void *arg)
6471673e404SJohn Birrell {
6481673e404SJohn Birrell 	iidesc_t *sii = data;
6491673e404SJohn Birrell 	merge_cb_data_t *mcd = arg;
6501673e404SJohn Birrell 	iifind_data_t iif;
6511673e404SJohn Birrell 	tdtrav_cb_f *post;
6521673e404SJohn Birrell 
6531673e404SJohn Birrell 	post = (mcd->md_flags & MCD_F_SELFUNIQUIFY ? map_self_post : map_post);
6541673e404SJohn Birrell 
6551673e404SJohn Birrell 	/* Map the tdesc nodes */
6561673e404SJohn Birrell 	(void) iitraverse(sii, &mcd->md_parent->td_curvgen, NULL, map_pre, post,
6571673e404SJohn Birrell 	    mcd);
6581673e404SJohn Birrell 
6591673e404SJohn Birrell 	/* Map the iidesc nodes */
6601673e404SJohn Birrell 	iif.iif_template = sii;
6611673e404SJohn Birrell 	iif.iif_ta = mcd->md_ta;
6621673e404SJohn Birrell 	iif.iif_newidx = get_mapping(mcd->md_ta, sii->ii_dtype->t_id);
6631673e404SJohn Birrell 	iif.iif_refmerge = (mcd->md_flags & MCD_F_REFMERGE);
6641673e404SJohn Birrell 
6651673e404SJohn Birrell 	if (hash_match(mcd->md_parent->td_iihash, sii, iidesc_match,
6661673e404SJohn Birrell 	    &iif) == 1)
6671673e404SJohn Birrell 		/* successfully mapped */
6681673e404SJohn Birrell 		return (1);
6691673e404SJohn Birrell 
6701673e404SJohn Birrell 	debug(3, "tba %s (%d)\n", (sii->ii_name ? sii->ii_name : "(anon)"),
6711673e404SJohn Birrell 	    sii->ii_type);
6721673e404SJohn Birrell 
6731673e404SJohn Birrell 	list_add(mcd->md_iitba, sii);
6741673e404SJohn Birrell 
6751673e404SJohn Birrell 	return (0);
6761673e404SJohn Birrell }
6771673e404SJohn Birrell 
6781673e404SJohn Birrell static int
remap_node(tdesc_t ** tgtp,tdesc_t * oldtgt,int selftid,tdesc_t * newself,merge_cb_data_t * mcd)6791673e404SJohn Birrell remap_node(tdesc_t **tgtp, tdesc_t *oldtgt, int selftid, tdesc_t *newself,
6801673e404SJohn Birrell     merge_cb_data_t *mcd)
6811673e404SJohn Birrell {
6821673e404SJohn Birrell 	tdesc_t *tgt = NULL;
6831673e404SJohn Birrell 	tdesc_t template;
6841673e404SJohn Birrell 	int oldid = oldtgt->t_id;
6851673e404SJohn Birrell 
6861673e404SJohn Birrell 	if (oldid == selftid) {
6871673e404SJohn Birrell 		*tgtp = newself;
6881673e404SJohn Birrell 		return (1);
6891673e404SJohn Birrell 	}
6901673e404SJohn Birrell 
6911673e404SJohn Birrell 	if ((template.t_id = get_mapping(mcd->md_ta, oldid)) == 0)
6924cc75139SJohn Birrell 		aborterr("failed to get mapping for tid %d <%x>\n", oldid, oldid);
6931673e404SJohn Birrell 
6941673e404SJohn Birrell 	if (!hash_find(mcd->md_parent->td_idhash, (void *)&template,
6951673e404SJohn Birrell 	    (void *)&tgt) && (!(mcd->md_flags & MCD_F_REFMERGE) ||
6961673e404SJohn Birrell 	    !hash_find(mcd->md_tgt->td_idhash, (void *)&template,
6971673e404SJohn Birrell 	    (void *)&tgt))) {
6984cc75139SJohn Birrell 		debug(3, "Remap couldn't find %d <%x> (from %d <%x>)\n", template.t_id,
6994cc75139SJohn Birrell 		    template.t_id, oldid, oldid);
7001673e404SJohn Birrell 		*tgtp = oldtgt;
7011673e404SJohn Birrell 		list_add(mcd->md_tdtbr, tgtp);
7021673e404SJohn Birrell 		return (0);
7031673e404SJohn Birrell 	}
7041673e404SJohn Birrell 
7051673e404SJohn Birrell 	*tgtp = tgt;
7061673e404SJohn Birrell 	return (1);
7071673e404SJohn Birrell }
7081673e404SJohn Birrell 
7091673e404SJohn Birrell static tdesc_t *
conjure_template(tdesc_t * old,int newselfid)7101673e404SJohn Birrell conjure_template(tdesc_t *old, int newselfid)
7111673e404SJohn Birrell {
7121673e404SJohn Birrell 	tdesc_t *new = xcalloc(sizeof (tdesc_t));
7131673e404SJohn Birrell 
7141673e404SJohn Birrell 	new->t_name = old->t_name ? xstrdup(old->t_name) : NULL;
7151673e404SJohn Birrell 	new->t_type = old->t_type;
7161673e404SJohn Birrell 	new->t_size = old->t_size;
7171673e404SJohn Birrell 	new->t_id = newselfid;
7181673e404SJohn Birrell 	new->t_flags = old->t_flags;
7191673e404SJohn Birrell 
7201673e404SJohn Birrell 	return (new);
7211673e404SJohn Birrell }
7221673e404SJohn Birrell 
7231673e404SJohn Birrell /*ARGSUSED2*/
7241673e404SJohn Birrell static tdesc_t *
conjure_intrinsic(tdesc_t * old,int newselfid,merge_cb_data_t * mcd __unused)7254cc75139SJohn Birrell conjure_intrinsic(tdesc_t *old, int newselfid, merge_cb_data_t *mcd __unused)
7261673e404SJohn Birrell {
7271673e404SJohn Birrell 	tdesc_t *new = conjure_template(old, newselfid);
7281673e404SJohn Birrell 
7291673e404SJohn Birrell 	new->t_intr = xmalloc(sizeof (intr_t));
7301673e404SJohn Birrell 	bcopy(old->t_intr, new->t_intr, sizeof (intr_t));
7311673e404SJohn Birrell 
7321673e404SJohn Birrell 	return (new);
7331673e404SJohn Birrell }
7341673e404SJohn Birrell 
7351673e404SJohn Birrell static tdesc_t *
conjure_plain(tdesc_t * old,int newselfid,merge_cb_data_t * mcd)7361673e404SJohn Birrell conjure_plain(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
7371673e404SJohn Birrell {
7381673e404SJohn Birrell 	tdesc_t *new = conjure_template(old, newselfid);
7391673e404SJohn Birrell 
7401673e404SJohn Birrell 	(void) remap_node(&new->t_tdesc, old->t_tdesc, old->t_id, new, mcd);
7411673e404SJohn Birrell 
7421673e404SJohn Birrell 	return (new);
7431673e404SJohn Birrell }
7441673e404SJohn Birrell 
7451673e404SJohn Birrell static tdesc_t *
conjure_function(tdesc_t * old,int newselfid,merge_cb_data_t * mcd)7461673e404SJohn Birrell conjure_function(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
7471673e404SJohn Birrell {
7481673e404SJohn Birrell 	tdesc_t *new = conjure_template(old, newselfid);
7491673e404SJohn Birrell 	fndef_t *nfn = xmalloc(sizeof (fndef_t));
7501673e404SJohn Birrell 	fndef_t *ofn = old->t_fndef;
7511673e404SJohn Birrell 	int i;
7521673e404SJohn Birrell 
7531673e404SJohn Birrell 	(void) remap_node(&nfn->fn_ret, ofn->fn_ret, old->t_id, new, mcd);
7541673e404SJohn Birrell 
7551673e404SJohn Birrell 	nfn->fn_nargs = ofn->fn_nargs;
7561673e404SJohn Birrell 	nfn->fn_vargs = ofn->fn_vargs;
7571673e404SJohn Birrell 
7581673e404SJohn Birrell 	if (nfn->fn_nargs > 0)
7591673e404SJohn Birrell 		nfn->fn_args = xcalloc(sizeof (tdesc_t *) * ofn->fn_nargs);
7601673e404SJohn Birrell 
7614cc75139SJohn Birrell 	for (i = 0; i < (int) ofn->fn_nargs; i++) {
7621673e404SJohn Birrell 		(void) remap_node(&nfn->fn_args[i], ofn->fn_args[i], old->t_id,
7631673e404SJohn Birrell 		    new, mcd);
7641673e404SJohn Birrell 	}
7651673e404SJohn Birrell 
7661673e404SJohn Birrell 	new->t_fndef = nfn;
7671673e404SJohn Birrell 
7681673e404SJohn Birrell 	return (new);
7691673e404SJohn Birrell }
7701673e404SJohn Birrell 
7711673e404SJohn Birrell static tdesc_t *
conjure_array(tdesc_t * old,int newselfid,merge_cb_data_t * mcd)7721673e404SJohn Birrell conjure_array(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
7731673e404SJohn Birrell {
7741673e404SJohn Birrell 	tdesc_t *new = conjure_template(old, newselfid);
7751673e404SJohn Birrell 	ardef_t *nar = xmalloc(sizeof (ardef_t));
7761673e404SJohn Birrell 	ardef_t *oar = old->t_ardef;
7771673e404SJohn Birrell 
7781673e404SJohn Birrell 	(void) remap_node(&nar->ad_contents, oar->ad_contents, old->t_id, new,
7791673e404SJohn Birrell 	    mcd);
7801673e404SJohn Birrell 	(void) remap_node(&nar->ad_idxtype, oar->ad_idxtype, old->t_id, new,
7811673e404SJohn Birrell 	    mcd);
7821673e404SJohn Birrell 
7831673e404SJohn Birrell 	nar->ad_nelems = oar->ad_nelems;
7841673e404SJohn Birrell 
7851673e404SJohn Birrell 	new->t_ardef = nar;
7861673e404SJohn Birrell 
7871673e404SJohn Birrell 	return (new);
7881673e404SJohn Birrell }
7891673e404SJohn Birrell 
7901673e404SJohn Birrell static tdesc_t *
conjure_su(tdesc_t * old,int newselfid,merge_cb_data_t * mcd)7911673e404SJohn Birrell conjure_su(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
7921673e404SJohn Birrell {
7931673e404SJohn Birrell 	tdesc_t *new = conjure_template(old, newselfid);
7941673e404SJohn Birrell 	mlist_t *omem, **nmemp;
7951673e404SJohn Birrell 
7961673e404SJohn Birrell 	for (omem = old->t_members, nmemp = &new->t_members;
7971673e404SJohn Birrell 	    omem; omem = omem->ml_next, nmemp = &((*nmemp)->ml_next)) {
7981673e404SJohn Birrell 		*nmemp = xmalloc(sizeof (mlist_t));
7991673e404SJohn Birrell 		(*nmemp)->ml_offset = omem->ml_offset;
8001673e404SJohn Birrell 		(*nmemp)->ml_size = omem->ml_size;
8014cc75139SJohn Birrell 		(*nmemp)->ml_name = xstrdup(omem->ml_name ? omem->ml_name : "empty omem->ml_name");
8021673e404SJohn Birrell 		(void) remap_node(&((*nmemp)->ml_type), omem->ml_type,
8031673e404SJohn Birrell 		    old->t_id, new, mcd);
8041673e404SJohn Birrell 	}
8051673e404SJohn Birrell 	*nmemp = NULL;
8061673e404SJohn Birrell 
8071673e404SJohn Birrell 	return (new);
8081673e404SJohn Birrell }
8091673e404SJohn Birrell 
8101673e404SJohn Birrell /*ARGSUSED2*/
8111673e404SJohn Birrell static tdesc_t *
conjure_enum(tdesc_t * old,int newselfid,merge_cb_data_t * mcd __unused)8124cc75139SJohn Birrell conjure_enum(tdesc_t *old, int newselfid, merge_cb_data_t *mcd __unused)
8131673e404SJohn Birrell {
8141673e404SJohn Birrell 	tdesc_t *new = conjure_template(old, newselfid);
8151673e404SJohn Birrell 	elist_t *oel, **nelp;
8161673e404SJohn Birrell 
8171673e404SJohn Birrell 	for (oel = old->t_emem, nelp = &new->t_emem;
8181673e404SJohn Birrell 	    oel; oel = oel->el_next, nelp = &((*nelp)->el_next)) {
8191673e404SJohn Birrell 		*nelp = xmalloc(sizeof (elist_t));
8201673e404SJohn Birrell 		(*nelp)->el_name = xstrdup(oel->el_name);
8211673e404SJohn Birrell 		(*nelp)->el_number = oel->el_number;
8221673e404SJohn Birrell 	}
8231673e404SJohn Birrell 	*nelp = NULL;
8241673e404SJohn Birrell 
8251673e404SJohn Birrell 	return (new);
8261673e404SJohn Birrell }
8271673e404SJohn Birrell 
8281673e404SJohn Birrell /*ARGSUSED2*/
8291673e404SJohn Birrell static tdesc_t *
conjure_forward(tdesc_t * old,int newselfid,merge_cb_data_t * mcd)8301673e404SJohn Birrell conjure_forward(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
8311673e404SJohn Birrell {
8321673e404SJohn Birrell 	tdesc_t *new = conjure_template(old, newselfid);
8331673e404SJohn Birrell 
8341673e404SJohn Birrell 	list_add(&mcd->md_tgt->td_fwdlist, new);
8351673e404SJohn Birrell 
8361673e404SJohn Birrell 	return (new);
8371673e404SJohn Birrell }
8381673e404SJohn Birrell 
8391673e404SJohn Birrell /*ARGSUSED*/
8401673e404SJohn Birrell static tdesc_t *
conjure_assert(tdesc_t * old __unused,int newselfid __unused,merge_cb_data_t * mcd __unused)8414cc75139SJohn Birrell conjure_assert(tdesc_t *old __unused, int newselfid __unused, merge_cb_data_t *mcd __unused)
8421673e404SJohn Birrell {
8431673e404SJohn Birrell 	assert(1 == 0);
8441673e404SJohn Birrell 	return (NULL);
8451673e404SJohn Birrell }
8461673e404SJohn Birrell 
8471673e404SJohn Birrell static iidesc_t *
conjure_iidesc(iidesc_t * old,merge_cb_data_t * mcd)8481673e404SJohn Birrell conjure_iidesc(iidesc_t *old, merge_cb_data_t *mcd)
8491673e404SJohn Birrell {
8501673e404SJohn Birrell 	iidesc_t *new = iidesc_dup(old);
8511673e404SJohn Birrell 	int i;
8521673e404SJohn Birrell 
8531673e404SJohn Birrell 	(void) remap_node(&new->ii_dtype, old->ii_dtype, -1, NULL, mcd);
8541673e404SJohn Birrell 	for (i = 0; i < new->ii_nargs; i++) {
8551673e404SJohn Birrell 		(void) remap_node(&new->ii_args[i], old->ii_args[i], -1, NULL,
8561673e404SJohn Birrell 		    mcd);
8571673e404SJohn Birrell 	}
8581673e404SJohn Birrell 
8591673e404SJohn Birrell 	return (new);
8601673e404SJohn Birrell }
8611673e404SJohn Birrell 
8621673e404SJohn Birrell static int
fwd_redir(tdesc_t * fwd,tdesc_t ** fwdp,void * private)8631673e404SJohn Birrell fwd_redir(tdesc_t *fwd, tdesc_t **fwdp, void *private)
8641673e404SJohn Birrell {
8651673e404SJohn Birrell 	alist_t *map = private;
8664cc75139SJohn Birrell 	void *defn;
8671673e404SJohn Birrell 
8681673e404SJohn Birrell 	if (!alist_find(map, (void *)fwd, (void **)&defn))
8691673e404SJohn Birrell 		return (0);
8701673e404SJohn Birrell 
8711673e404SJohn Birrell 	debug(3, "Redirecting an edge to %s\n", tdesc_name(defn));
8721673e404SJohn Birrell 
8731673e404SJohn Birrell 	*fwdp = defn;
8741673e404SJohn Birrell 
8751673e404SJohn Birrell 	return (1);
8761673e404SJohn Birrell }
8771673e404SJohn Birrell 
8781673e404SJohn Birrell static tdtrav_cb_f fwd_redir_cbs[] = {
8791673e404SJohn Birrell 	NULL,
8801673e404SJohn Birrell 	NULL,			/* intrinsic */
8811673e404SJohn Birrell 	NULL,			/* pointer */
8821673e404SJohn Birrell 	NULL,			/* array */
8831673e404SJohn Birrell 	NULL,			/* function */
8841673e404SJohn Birrell 	NULL,			/* struct */
8851673e404SJohn Birrell 	NULL,			/* union */
8861673e404SJohn Birrell 	NULL,			/* enum */
8871673e404SJohn Birrell 	fwd_redir,		/* forward */
8881673e404SJohn Birrell 	NULL,			/* typedef */
8891673e404SJohn Birrell 	tdtrav_assert,		/* typedef_unres */
8901673e404SJohn Birrell 	NULL,			/* volatile */
8911673e404SJohn Birrell 	NULL,			/* const */
8921673e404SJohn Birrell 	NULL			/* restrict */
8931673e404SJohn Birrell };
8941673e404SJohn Birrell 
8951673e404SJohn Birrell typedef struct redir_mstr_data {
8961673e404SJohn Birrell 	tdata_t *rmd_tgt;
8971673e404SJohn Birrell 	alist_t *rmd_map;
8981673e404SJohn Birrell } redir_mstr_data_t;
8991673e404SJohn Birrell 
9001673e404SJohn Birrell static int
redir_mstr_fwd_cb(void * name,void * value,void * arg)9011673e404SJohn Birrell redir_mstr_fwd_cb(void *name, void *value, void *arg)
9021673e404SJohn Birrell {
9031673e404SJohn Birrell 	tdesc_t *fwd = name;
9044cc75139SJohn Birrell 	int defnid = (uintptr_t)value;
9051673e404SJohn Birrell 	redir_mstr_data_t *rmd = arg;
9061673e404SJohn Birrell 	tdesc_t template;
9071673e404SJohn Birrell 	tdesc_t *defn;
9081673e404SJohn Birrell 
9091673e404SJohn Birrell 	template.t_id = defnid;
9101673e404SJohn Birrell 
9111673e404SJohn Birrell 	if (!hash_find(rmd->rmd_tgt->td_idhash, (void *)&template,
9121673e404SJohn Birrell 	    (void *)&defn)) {
9131673e404SJohn Birrell 		aborterr("Couldn't unforward %d (%s)\n", defnid,
9141673e404SJohn Birrell 		    tdesc_name(defn));
9151673e404SJohn Birrell 	}
9161673e404SJohn Birrell 
9171673e404SJohn Birrell 	debug(3, "Forward map: resolved %d to %s\n", defnid, tdesc_name(defn));
9181673e404SJohn Birrell 
9191673e404SJohn Birrell 	alist_add(rmd->rmd_map, (void *)fwd, (void *)defn);
9201673e404SJohn Birrell 
9211673e404SJohn Birrell 	return (1);
9221673e404SJohn Birrell }
9231673e404SJohn Birrell 
9241673e404SJohn Birrell static void
redir_mstr_fwds(merge_cb_data_t * mcd)9251673e404SJohn Birrell redir_mstr_fwds(merge_cb_data_t *mcd)
9261673e404SJohn Birrell {
9271673e404SJohn Birrell 	redir_mstr_data_t rmd;
9281673e404SJohn Birrell 	alist_t *map = alist_new(NULL, NULL);
9291673e404SJohn Birrell 
9301673e404SJohn Birrell 	rmd.rmd_tgt = mcd->md_tgt;
9311673e404SJohn Birrell 	rmd.rmd_map = map;
9321673e404SJohn Birrell 
9331673e404SJohn Birrell 	if (alist_iter(mcd->md_fdida, redir_mstr_fwd_cb, &rmd)) {
9341673e404SJohn Birrell 		(void) iitraverse_hash(mcd->md_tgt->td_iihash,
9351673e404SJohn Birrell 		    &mcd->md_tgt->td_curvgen, fwd_redir_cbs, NULL, NULL, map);
9361673e404SJohn Birrell 	}
9371673e404SJohn Birrell 
9381673e404SJohn Birrell 	alist_free(map);
9391673e404SJohn Birrell }
9401673e404SJohn Birrell 
9411673e404SJohn Birrell static int
add_iitba_cb(void * data,void * private)9421673e404SJohn Birrell add_iitba_cb(void *data, void *private)
9431673e404SJohn Birrell {
9441673e404SJohn Birrell 	merge_cb_data_t *mcd = private;
9451673e404SJohn Birrell 	iidesc_t *tba = data;
9461673e404SJohn Birrell 	iidesc_t *new;
9471673e404SJohn Birrell 	iifind_data_t iif;
9481673e404SJohn Birrell 	int newidx;
9491673e404SJohn Birrell 
9501673e404SJohn Birrell 	newidx = get_mapping(mcd->md_ta, tba->ii_dtype->t_id);
9511673e404SJohn Birrell 	assert(newidx != -1);
9521673e404SJohn Birrell 
9531673e404SJohn Birrell 	(void) list_remove(mcd->md_iitba, data, NULL, NULL);
9541673e404SJohn Birrell 
9551673e404SJohn Birrell 	iif.iif_template = tba;
9561673e404SJohn Birrell 	iif.iif_ta = mcd->md_ta;
9571673e404SJohn Birrell 	iif.iif_newidx = newidx;
9581673e404SJohn Birrell 	iif.iif_refmerge = (mcd->md_flags & MCD_F_REFMERGE);
9591673e404SJohn Birrell 
9601673e404SJohn Birrell 	if (hash_match(mcd->md_parent->td_iihash, tba, iidesc_match,
9611673e404SJohn Birrell 	    &iif) == 1) {
9621673e404SJohn Birrell 		debug(3, "iidesc_t %s already exists\n",
9631673e404SJohn Birrell 		    (tba->ii_name ? tba->ii_name : "(anon)"));
9641673e404SJohn Birrell 		return (1);
9651673e404SJohn Birrell 	}
9661673e404SJohn Birrell 
9671673e404SJohn Birrell 	new = conjure_iidesc(tba, mcd);
9681673e404SJohn Birrell 	hash_add(mcd->md_tgt->td_iihash, new);
9691673e404SJohn Birrell 
9701673e404SJohn Birrell 	return (1);
9711673e404SJohn Birrell }
9721673e404SJohn Birrell 
9731673e404SJohn Birrell static int
add_tdesc(tdesc_t * oldtdp,int newid,merge_cb_data_t * mcd)9741673e404SJohn Birrell add_tdesc(tdesc_t *oldtdp, int newid, merge_cb_data_t *mcd)
9751673e404SJohn Birrell {
9761673e404SJohn Birrell 	tdesc_t *newtdp;
9771673e404SJohn Birrell 	tdesc_t template;
9781673e404SJohn Birrell 
9791673e404SJohn Birrell 	template.t_id = newid;
9801673e404SJohn Birrell 	assert(hash_find(mcd->md_parent->td_idhash,
9811673e404SJohn Birrell 	    (void *)&template, NULL) == 0);
9821673e404SJohn Birrell 
9834cc75139SJohn Birrell 	debug(3, "trying to conjure %d %s (%d, <%x>) as %d, <%x>\n",
9844cc75139SJohn Birrell 	    oldtdp->t_type, tdesc_name(oldtdp), oldtdp->t_id,
9854cc75139SJohn Birrell 	    oldtdp->t_id, newid, newid);
9861673e404SJohn Birrell 
9871673e404SJohn Birrell 	if ((newtdp = tdesc_ops[oldtdp->t_type].conjure(oldtdp, newid,
9881673e404SJohn Birrell 	    mcd)) == NULL)
9891673e404SJohn Birrell 		/* couldn't map everything */
9901673e404SJohn Birrell 		return (0);
9911673e404SJohn Birrell 
9921673e404SJohn Birrell 	debug(3, "succeeded\n");
9931673e404SJohn Birrell 
9941673e404SJohn Birrell 	hash_add(mcd->md_tgt->td_idhash, newtdp);
9951673e404SJohn Birrell 	hash_add(mcd->md_tgt->td_layouthash, newtdp);
9961673e404SJohn Birrell 
9971673e404SJohn Birrell 	return (1);
9981673e404SJohn Birrell }
9991673e404SJohn Birrell 
10001673e404SJohn Birrell static int
add_tdtba_cb(void * data,void * arg)10011673e404SJohn Birrell add_tdtba_cb(void *data, void *arg)
10021673e404SJohn Birrell {
10031673e404SJohn Birrell 	tdesc_t *tdp = data;
10041673e404SJohn Birrell 	merge_cb_data_t *mcd = arg;
10051673e404SJohn Birrell 	int newid;
10061673e404SJohn Birrell 	int rc;
10071673e404SJohn Birrell 
10081673e404SJohn Birrell 	newid = get_mapping(mcd->md_ta, tdp->t_id);
10091673e404SJohn Birrell 	assert(newid != -1);
10101673e404SJohn Birrell 
10111673e404SJohn Birrell 	if ((rc = add_tdesc(tdp, newid, mcd)))
10121673e404SJohn Birrell 		hash_remove(mcd->md_tdtba, (void *)tdp);
10131673e404SJohn Birrell 
10141673e404SJohn Birrell 	return (rc);
10151673e404SJohn Birrell }
10161673e404SJohn Birrell 
10171673e404SJohn Birrell static int
add_tdtbr_cb(void * data,void * arg)10181673e404SJohn Birrell add_tdtbr_cb(void *data, void *arg)
10191673e404SJohn Birrell {
10201673e404SJohn Birrell 	tdesc_t **tdpp = data;
10211673e404SJohn Birrell 	merge_cb_data_t *mcd = arg;
10221673e404SJohn Birrell 
10231673e404SJohn Birrell 	debug(3, "Remapping %s (%d)\n", tdesc_name(*tdpp), (*tdpp)->t_id);
10241673e404SJohn Birrell 
10251673e404SJohn Birrell 	if (!remap_node(tdpp, *tdpp, -1, NULL, mcd))
10261673e404SJohn Birrell 		return (0);
10271673e404SJohn Birrell 
10281673e404SJohn Birrell 	(void) list_remove(mcd->md_tdtbr, (void *)tdpp, NULL, NULL);
10291673e404SJohn Birrell 	return (1);
10301673e404SJohn Birrell }
10311673e404SJohn Birrell 
10321673e404SJohn Birrell static void
merge_types(hash_t * src,merge_cb_data_t * mcd)10331673e404SJohn Birrell merge_types(hash_t *src, merge_cb_data_t *mcd)
10341673e404SJohn Birrell {
10351673e404SJohn Birrell 	list_t *iitba = NULL;
10361673e404SJohn Birrell 	list_t *tdtbr = NULL;
10371673e404SJohn Birrell 	int iirc, tdrc;
10381673e404SJohn Birrell 
10391673e404SJohn Birrell 	mcd->md_iitba = &iitba;
10401673e404SJohn Birrell 	mcd->md_tdtba = hash_new(TDATA_LAYOUT_HASH_SIZE, tdesc_layouthash,
10411673e404SJohn Birrell 	    tdesc_layoutcmp);
10421673e404SJohn Birrell 	mcd->md_tdtbr = &tdtbr;
10431673e404SJohn Birrell 
10441673e404SJohn Birrell 	(void) hash_iter(src, merge_type_cb, mcd);
10451673e404SJohn Birrell 
10464cc75139SJohn Birrell 	tdrc = hash_iter(mcd->md_tdtba, add_tdtba_cb, mcd);
10471673e404SJohn Birrell 	debug(3, "add_tdtba_cb added %d items\n", tdrc);
10481673e404SJohn Birrell 
10494cc75139SJohn Birrell 	iirc = list_iter(*mcd->md_iitba, add_iitba_cb, mcd);
10501673e404SJohn Birrell 	debug(3, "add_iitba_cb added %d items\n", iirc);
10511673e404SJohn Birrell 
10521673e404SJohn Birrell 	assert(list_count(*mcd->md_iitba) == 0 &&
10531673e404SJohn Birrell 	    hash_count(mcd->md_tdtba) == 0);
10541673e404SJohn Birrell 
10554cc75139SJohn Birrell 	tdrc = list_iter(*mcd->md_tdtbr, add_tdtbr_cb, mcd);
10561673e404SJohn Birrell 	debug(3, "add_tdtbr_cb added %d items\n", tdrc);
10571673e404SJohn Birrell 
10581673e404SJohn Birrell 	if (list_count(*mcd->md_tdtbr) != 0)
10591673e404SJohn Birrell 		aborterr("Couldn't remap all nodes\n");
10601673e404SJohn Birrell 
10611673e404SJohn Birrell 	/*
10621673e404SJohn Birrell 	 * We now have an alist of master forwards and the ids of the new master
10631673e404SJohn Birrell 	 * definitions for those forwards in mcd->md_fdida.  By this point,
10641673e404SJohn Birrell 	 * we're guaranteed that all of the master definitions referenced in
10651673e404SJohn Birrell 	 * fdida have been added to the master tree.  We now traverse through
10661673e404SJohn Birrell 	 * the master tree, redirecting all edges inbound to forwards that have
10671673e404SJohn Birrell 	 * definitions to those definitions.
10681673e404SJohn Birrell 	 */
10691673e404SJohn Birrell 	if (mcd->md_parent == mcd->md_tgt) {
10701673e404SJohn Birrell 		redir_mstr_fwds(mcd);
10711673e404SJohn Birrell 	}
10721673e404SJohn Birrell }
10731673e404SJohn Birrell 
10741673e404SJohn Birrell void
merge_into_master(tdata_t * cur,tdata_t * mstr,tdata_t * tgt,int selfuniquify)10751673e404SJohn Birrell merge_into_master(tdata_t *cur, tdata_t *mstr, tdata_t *tgt, int selfuniquify)
10761673e404SJohn Birrell {
10771673e404SJohn Birrell 	merge_cb_data_t mcd;
10781673e404SJohn Birrell 
10791673e404SJohn Birrell 	cur->td_ref++;
10801673e404SJohn Birrell 	mstr->td_ref++;
10811673e404SJohn Birrell 	if (tgt)
10821673e404SJohn Birrell 		tgt->td_ref++;
10831673e404SJohn Birrell 
10841673e404SJohn Birrell 	assert(cur->td_ref == 1 && mstr->td_ref == 1 &&
10851673e404SJohn Birrell 	    (tgt == NULL || tgt->td_ref == 1));
10861673e404SJohn Birrell 
10871673e404SJohn Birrell 	mcd.md_parent = mstr;
10881673e404SJohn Birrell 	mcd.md_tgt = (tgt ? tgt : mstr);
10891673e404SJohn Birrell 	mcd.md_ta = alist_new(NULL, NULL);
10901673e404SJohn Birrell 	mcd.md_fdida = alist_new(NULL, NULL);
10911673e404SJohn Birrell 	mcd.md_flags = 0;
10921673e404SJohn Birrell 
10931673e404SJohn Birrell 	if (selfuniquify)
10941673e404SJohn Birrell 		mcd.md_flags |= MCD_F_SELFUNIQUIFY;
10951673e404SJohn Birrell 	if (tgt)
10961673e404SJohn Birrell 		mcd.md_flags |= MCD_F_REFMERGE;
10971673e404SJohn Birrell 
10981673e404SJohn Birrell 	mstr->td_curvgen = MAX(mstr->td_curvgen, cur->td_curvgen);
10991673e404SJohn Birrell 	mstr->td_curemark = MAX(mstr->td_curemark, cur->td_curemark);
11001673e404SJohn Birrell 
11011673e404SJohn Birrell 	merge_types(cur->td_iihash, &mcd);
11021673e404SJohn Birrell 
11031673e404SJohn Birrell 	if (debug_level >= 3) {
11041673e404SJohn Birrell 		debug(3, "Type association stats\n");
11051673e404SJohn Birrell 		alist_stats(mcd.md_ta, 0);
11061673e404SJohn Birrell 		debug(3, "Layout hash stats\n");
11071673e404SJohn Birrell 		hash_stats(mcd.md_tgt->td_layouthash, 1);
11081673e404SJohn Birrell 	}
11091673e404SJohn Birrell 
11101673e404SJohn Birrell 	alist_free(mcd.md_fdida);
11111673e404SJohn Birrell 	alist_free(mcd.md_ta);
11121673e404SJohn Birrell 
11131673e404SJohn Birrell 	cur->td_ref--;
11141673e404SJohn Birrell 	mstr->td_ref--;
11151673e404SJohn Birrell 	if (tgt)
11161673e404SJohn Birrell 		tgt->td_ref--;
11171673e404SJohn Birrell }
11181673e404SJohn Birrell 
11191673e404SJohn Birrell tdesc_ops_t tdesc_ops[] = {
11201673e404SJohn Birrell 	{ "ERROR! BAD tdesc TYPE", NULL, NULL },
11211673e404SJohn Birrell 	{ "intrinsic",		equiv_intrinsic,	conjure_intrinsic },
11221673e404SJohn Birrell 	{ "pointer", 		equiv_plain,		conjure_plain },
11231673e404SJohn Birrell 	{ "array", 		equiv_array,		conjure_array },
11241673e404SJohn Birrell 	{ "function", 		equiv_function,		conjure_function },
11251673e404SJohn Birrell 	{ "struct",		equiv_su,		conjure_su },
11261673e404SJohn Birrell 	{ "union",		equiv_su,		conjure_su },
11271673e404SJohn Birrell 	{ "enum",		equiv_enum,		conjure_enum },
11281673e404SJohn Birrell 	{ "forward",		NULL,			conjure_forward },
11291673e404SJohn Birrell 	{ "typedef",		equiv_plain,		conjure_plain },
11301673e404SJohn Birrell 	{ "typedef_unres",	equiv_assert,		conjure_assert },
11311673e404SJohn Birrell 	{ "volatile",		equiv_plain,		conjure_plain },
11321673e404SJohn Birrell 	{ "const", 		equiv_plain,		conjure_plain },
11331673e404SJohn Birrell 	{ "restrict",		equiv_plain,		conjure_plain }
11341673e404SJohn Birrell };
1135