xref: /onnv-gate/usr/src/tools/ctf/cvt/merge.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This file contains routines that merge one tdata_t tree, called the child,
31*0Sstevel@tonic-gate  * into another, called the parent.  Note that these names are used mainly for
32*0Sstevel@tonic-gate  * convenience and to represent the direction of the merge.  They are not meant
33*0Sstevel@tonic-gate  * to imply any relationship between the tdata_t graphs prior to the merge.
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * tdata_t structures contain two main elements - a hash of iidesc_t nodes, and
36*0Sstevel@tonic-gate  * a directed graph of tdesc_t nodes, pointed to by the iidesc_t nodes.  Simply
37*0Sstevel@tonic-gate  * put, we merge the tdesc_t graphs, followed by the iidesc_t nodes, and then we
38*0Sstevel@tonic-gate  * clean up loose ends.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * The algorithm is as follows:
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * 1. Mapping iidesc_t nodes
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * For each child iidesc_t node, we first try to map its tdesc_t subgraph
45*0Sstevel@tonic-gate  * against the tdesc_t graph in the parent.  For each node in the child subgraph
46*0Sstevel@tonic-gate  * that exists in the parent, a mapping between the two (between their type IDs)
47*0Sstevel@tonic-gate  * is established.  For the child nodes that cannot be mapped onto existing
48*0Sstevel@tonic-gate  * parent nodes, a mapping is established between the child node ID and a
49*0Sstevel@tonic-gate  * newly-allocated ID that the node will use when it is re-created in the
50*0Sstevel@tonic-gate  * parent.  These unmappable nodes are added to the md_tdtba (tdesc_t To Be
51*0Sstevel@tonic-gate  * Added) hash, which tracks nodes that need to be created in the parent.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * If all of the nodes in the subgraph for an iidesc_t in the child can be
54*0Sstevel@tonic-gate  * mapped to existing nodes in the parent, then we can try to map the child
55*0Sstevel@tonic-gate  * iidesc_t onto an iidesc_t in the parent.  If we cannot find an equivalent
56*0Sstevel@tonic-gate  * iidesc_t, or if we were not able to completely map the tdesc_t subgraph(s),
57*0Sstevel@tonic-gate  * then we add this iidesc_t to the md_iitba (iidesc_t To Be Added) list.  This
58*0Sstevel@tonic-gate  * list tracks iidesc_t nodes that are to be created in the parent.
59*0Sstevel@tonic-gate  *
60*0Sstevel@tonic-gate  * While visiting the tdesc_t nodes, we may discover a forward declaration (a
61*0Sstevel@tonic-gate  * FORWARD tdesc_t) in the parent that is resolved in the child.  That is, there
62*0Sstevel@tonic-gate  * may be a structure or union definition in the child with the same name as the
63*0Sstevel@tonic-gate  * forward declaration in the parent.  If we find such a node, we record an
64*0Sstevel@tonic-gate  * association in the md_fdida (Forward => Definition ID Association) list
65*0Sstevel@tonic-gate  * between the parent ID of the forward declaration and the ID that the
66*0Sstevel@tonic-gate  * definition will use when re-created in the parent.
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  * 2. Creating new tdesc_t nodes (the md_tdtba hash)
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * We have now attempted to map all tdesc_t nodes from the child into the
71*0Sstevel@tonic-gate  * parent, and have, in md_tdtba, a hash of all tdesc_t nodes that need to be
72*0Sstevel@tonic-gate  * created (or, as we so wittily call it, conjured) in the parent.  We iterate
73*0Sstevel@tonic-gate  * through this hash, creating the indicated tdesc_t nodes.  For a given tdesc_t
74*0Sstevel@tonic-gate  * node, conjuring requires two steps - the copying of the common tdesc_t data
75*0Sstevel@tonic-gate  * (name, type, etc) from the child node, and the creation of links from the
76*0Sstevel@tonic-gate  * newly-created node to the parent equivalents of other tdesc_t nodes pointed
77*0Sstevel@tonic-gate  * to by node being conjured.  Note that in some cases, the targets of these
78*0Sstevel@tonic-gate  * links will be on the md_tdtba hash themselves, and may not have been created
79*0Sstevel@tonic-gate  * yet.  As such, we can't establish the links from these new nodes into the
80*0Sstevel@tonic-gate  * parent graph.  We therefore conjure them with links to nodes in the *child*
81*0Sstevel@tonic-gate  * graph, and add pointers to the links to be created to the md_tdtbr (tdesc_t
82*0Sstevel@tonic-gate  * To Be Remapped) hash.  For example, a POINTER tdesc_t that could not be
83*0Sstevel@tonic-gate  * resolved would have its &tdesc_t->t_tdesc added to md_tdtbr.
84*0Sstevel@tonic-gate  *
85*0Sstevel@tonic-gate  * 3. Creating new iidesc_t nodes (the md_iitba list)
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  * When we have completed step 2, all tdesc_t nodes have been created (or
88*0Sstevel@tonic-gate  * already existed) in the parent.  Some of them may have incorrect links (the
89*0Sstevel@tonic-gate  * members of the md_tdtbr list), but they've all been created.  As such, we can
90*0Sstevel@tonic-gate  * create all of the iidesc_t nodes, as we can attach the tdesc_t subgraph
91*0Sstevel@tonic-gate  * pointers correctly.  We create each node, and attach the pointers to the
92*0Sstevel@tonic-gate  * appropriate parts of the parent tdesc_t graph.
93*0Sstevel@tonic-gate  *
94*0Sstevel@tonic-gate  * 4. Resolving newly-created tdesc_t node links (the md_tdtbr list)
95*0Sstevel@tonic-gate  *
96*0Sstevel@tonic-gate  * As in step 3, we rely on the fact that all of the tdesc_t nodes have been
97*0Sstevel@tonic-gate  * created.  Each entry in the md_tdtbr list is a pointer to where a link into
98*0Sstevel@tonic-gate  * the parent will be established.  As saved in the md_tdtbr list, these
99*0Sstevel@tonic-gate  * pointers point into the child tdesc_t subgraph.  We can thus get the target
100*0Sstevel@tonic-gate  * type ID from the child, look at the ID mapping to determine the desired link
101*0Sstevel@tonic-gate  * target, and redirect the link accordingly.
102*0Sstevel@tonic-gate  *
103*0Sstevel@tonic-gate  * 5. Parent => child forward declaration resolution
104*0Sstevel@tonic-gate  *
105*0Sstevel@tonic-gate  * If entries were made in the md_fdida list in step 1, we have forward
106*0Sstevel@tonic-gate  * declarations in the parent that need to be resolved to their definitions
107*0Sstevel@tonic-gate  * re-created in step 2 from the child.  Using the md_fdida list, we can locate
108*0Sstevel@tonic-gate  * the definition for the forward declaration, and we can redirect all inbound
109*0Sstevel@tonic-gate  * edges to the forward declaration node to the actual definition.
110*0Sstevel@tonic-gate  *
111*0Sstevel@tonic-gate  * A pox on the house of anyone who changes the algorithm without updating
112*0Sstevel@tonic-gate  * this comment.
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate #include <stdio.h>
116*0Sstevel@tonic-gate #include <strings.h>
117*0Sstevel@tonic-gate #include <assert.h>
118*0Sstevel@tonic-gate #include <pthread.h>
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate #include "ctf_headers.h"
121*0Sstevel@tonic-gate #include "ctftools.h"
122*0Sstevel@tonic-gate #include "list.h"
123*0Sstevel@tonic-gate #include "alist.h"
124*0Sstevel@tonic-gate #include "memory.h"
125*0Sstevel@tonic-gate #include "traverse.h"
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate typedef struct equiv_data equiv_data_t;
128*0Sstevel@tonic-gate typedef struct merge_cb_data merge_cb_data_t;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate /*
131*0Sstevel@tonic-gate  * There are two traversals in this file, for equivalency and for tdesc_t
132*0Sstevel@tonic-gate  * re-creation, that do not fit into the tdtraverse() framework.  We have our
133*0Sstevel@tonic-gate  * own traversal mechanism and ops vector here for those two cases.
134*0Sstevel@tonic-gate  */
135*0Sstevel@tonic-gate typedef struct tdesc_ops {
136*0Sstevel@tonic-gate 	char *name;
137*0Sstevel@tonic-gate 	int (*equiv)(tdesc_t *, tdesc_t *, equiv_data_t *);
138*0Sstevel@tonic-gate 	tdesc_t *(*conjure)(tdesc_t *, int, merge_cb_data_t *);
139*0Sstevel@tonic-gate } tdesc_ops_t;
140*0Sstevel@tonic-gate extern tdesc_ops_t tdesc_ops[];
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate /*
143*0Sstevel@tonic-gate  * The workhorse structure of tdata_t merging.  Holds all lists of nodes to be
144*0Sstevel@tonic-gate  * processed during various phases of the merge algorithm.
145*0Sstevel@tonic-gate  */
146*0Sstevel@tonic-gate struct merge_cb_data {
147*0Sstevel@tonic-gate 	tdata_t *md_parent;
148*0Sstevel@tonic-gate 	tdata_t *md_tgt;
149*0Sstevel@tonic-gate 	alist_t *md_ta;		/* Type Association */
150*0Sstevel@tonic-gate 	alist_t *md_fdida;	/* Forward -> Definition ID Association */
151*0Sstevel@tonic-gate 	list_t	**md_iitba;	/* iidesc_t nodes To Be Added to the parent */
152*0Sstevel@tonic-gate 	hash_t	*md_tdtba;	/* tdesc_t nodes To Be Added to the parent */
153*0Sstevel@tonic-gate 	list_t	**md_tdtbr;	/* tdesc_t nodes To Be Remapped */
154*0Sstevel@tonic-gate 	int md_flags;
155*0Sstevel@tonic-gate }; /* merge_cb_data_t */
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * When we first create a tdata_t from stabs data, we will have duplicate nodes.
159*0Sstevel@tonic-gate  * Normal merges, however, assume that the child tdata_t is already self-unique,
160*0Sstevel@tonic-gate  * and for speed reasons do not attempt to self-uniquify.  If this flag is set,
161*0Sstevel@tonic-gate  * the merge algorithm will self-uniquify by avoiding the insertion of
162*0Sstevel@tonic-gate  * duplicates in the md_tdtdba list.
163*0Sstevel@tonic-gate  */
164*0Sstevel@tonic-gate #define	MCD_F_SELFUNIQUIFY	0x1
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate /*
167*0Sstevel@tonic-gate  * When we merge the CTF data for the modules, we don't want it to contain any
168*0Sstevel@tonic-gate  * data that can be found in the reference module (usually genunix).  If this
169*0Sstevel@tonic-gate  * flag is set, we're doing a merge between the fully merged tdata_t for this
170*0Sstevel@tonic-gate  * module and the tdata_t for the reference module, with the data unique to this
171*0Sstevel@tonic-gate  * module ending up in a third tdata_t.  It is this third tdata_t that will end
172*0Sstevel@tonic-gate  * up in the .SUNW_ctf section for the module.
173*0Sstevel@tonic-gate  */
174*0Sstevel@tonic-gate #define	MCD_F_REFMERGE	0x2
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate /*
177*0Sstevel@tonic-gate  * Mapping of child type IDs to parent type IDs
178*0Sstevel@tonic-gate  */
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate static void
181*0Sstevel@tonic-gate add_mapping(alist_t *ta, tid_t srcid, tid_t tgtid)
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate 	debug(3, "Adding mapping %u => %u\n", srcid, tgtid);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	assert(!alist_find(ta, (void *)srcid, NULL));
186*0Sstevel@tonic-gate 	assert(srcid != 0 && tgtid != 0);
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	alist_add(ta, (void *)srcid, (void *)tgtid);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate static tid_t
192*0Sstevel@tonic-gate get_mapping(alist_t *ta, int srcid)
193*0Sstevel@tonic-gate {
194*0Sstevel@tonic-gate 	long ltgtid;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	if (alist_find(ta, (void *)srcid, (void **)&ltgtid))
197*0Sstevel@tonic-gate 		return ((int)ltgtid);
198*0Sstevel@tonic-gate 	else
199*0Sstevel@tonic-gate 		return (0);
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate /*
203*0Sstevel@tonic-gate  * Determining equivalence of tdesc_t subgraphs
204*0Sstevel@tonic-gate  */
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate struct equiv_data {
207*0Sstevel@tonic-gate 	alist_t *ed_ta;
208*0Sstevel@tonic-gate 	tdesc_t *ed_node;
209*0Sstevel@tonic-gate 	tdesc_t *ed_tgt;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	int ed_clear_mark;
212*0Sstevel@tonic-gate 	int ed_cur_mark;
213*0Sstevel@tonic-gate 	int ed_selfuniquify;
214*0Sstevel@tonic-gate }; /* equiv_data_t */
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate static int equiv_node(tdesc_t *, tdesc_t *, equiv_data_t *);
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate /*ARGSUSED2*/
219*0Sstevel@tonic-gate static int
220*0Sstevel@tonic-gate equiv_intrinsic(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
221*0Sstevel@tonic-gate {
222*0Sstevel@tonic-gate 	intr_t *si = stdp->t_intr;
223*0Sstevel@tonic-gate 	intr_t *ti = ttdp->t_intr;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	if (si->intr_type != ti->intr_type ||
226*0Sstevel@tonic-gate 	    si->intr_signed != ti->intr_signed ||
227*0Sstevel@tonic-gate 	    si->intr_offset != ti->intr_offset ||
228*0Sstevel@tonic-gate 	    si->intr_nbits != ti->intr_nbits)
229*0Sstevel@tonic-gate 		return (0);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	if (si->intr_type == INTR_INT &&
232*0Sstevel@tonic-gate 	    si->intr_iformat != ti->intr_iformat)
233*0Sstevel@tonic-gate 		return (0);
234*0Sstevel@tonic-gate 	else if (si->intr_type == INTR_REAL &&
235*0Sstevel@tonic-gate 	    si->intr_fformat != ti->intr_fformat)
236*0Sstevel@tonic-gate 		return (0);
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	return (1);
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate static int
242*0Sstevel@tonic-gate equiv_plain(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
243*0Sstevel@tonic-gate {
244*0Sstevel@tonic-gate 	return (equiv_node(stdp->t_tdesc, ttdp->t_tdesc, ed));
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate static int
248*0Sstevel@tonic-gate equiv_function(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate 	fndef_t *fn1 = stdp->t_fndef, *fn2 = ttdp->t_fndef;
251*0Sstevel@tonic-gate 	int i;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	if (fn1->fn_nargs != fn2->fn_nargs ||
254*0Sstevel@tonic-gate 	    fn1->fn_vargs != fn2->fn_vargs)
255*0Sstevel@tonic-gate 		return (0);
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	if (!equiv_node(fn1->fn_ret, fn2->fn_ret, ed))
258*0Sstevel@tonic-gate 		return (0);
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	for (i = 0; i < fn1->fn_nargs; i++) {
261*0Sstevel@tonic-gate 		if (!equiv_node(fn1->fn_args[i], fn2->fn_args[i], ed))
262*0Sstevel@tonic-gate 			return (0);
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	return (1);
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate static int
269*0Sstevel@tonic-gate equiv_array(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
270*0Sstevel@tonic-gate {
271*0Sstevel@tonic-gate 	ardef_t *ar1 = stdp->t_ardef, *ar2 = ttdp->t_ardef;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	if (!equiv_node(ar1->ad_contents, ar2->ad_contents, ed) ||
274*0Sstevel@tonic-gate 	    !equiv_node(ar1->ad_idxtype, ar2->ad_idxtype, ed))
275*0Sstevel@tonic-gate 		return (0);
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	if (ar1->ad_nelems != ar2->ad_nelems)
278*0Sstevel@tonic-gate 		return (0);
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	return (1);
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate static int
284*0Sstevel@tonic-gate equiv_su(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
285*0Sstevel@tonic-gate {
286*0Sstevel@tonic-gate 	mlist_t *ml1 = stdp->t_members, *ml2 = ttdp->t_members;
287*0Sstevel@tonic-gate 	mlist_t *olm1 = NULL;
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	while (ml1 && ml2) {
290*0Sstevel@tonic-gate 		if (ml1->ml_offset != ml2->ml_offset ||
291*0Sstevel@tonic-gate 		    strcmp(ml1->ml_name, ml2->ml_name) != 0)
292*0Sstevel@tonic-gate 			return (0);
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 		/*
295*0Sstevel@tonic-gate 		 * Don't do the recursive equivalency checking more than
296*0Sstevel@tonic-gate 		 * we have to.
297*0Sstevel@tonic-gate 		 */
298*0Sstevel@tonic-gate 		if (olm1 == NULL || olm1->ml_type->t_id != ml1->ml_type->t_id) {
299*0Sstevel@tonic-gate 			if (ml1->ml_size != ml2->ml_size ||
300*0Sstevel@tonic-gate 			    !equiv_node(ml1->ml_type, ml2->ml_type, ed))
301*0Sstevel@tonic-gate 				return (0);
302*0Sstevel@tonic-gate 		}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 		olm1 = ml1;
305*0Sstevel@tonic-gate 		ml1 = ml1->ml_next;
306*0Sstevel@tonic-gate 		ml2 = ml2->ml_next;
307*0Sstevel@tonic-gate 	}
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	if (ml1 || ml2)
310*0Sstevel@tonic-gate 		return (0);
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	return (1);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate /*ARGSUSED2*/
316*0Sstevel@tonic-gate static int
317*0Sstevel@tonic-gate equiv_enum(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate 	elist_t *el1 = stdp->t_emem;
320*0Sstevel@tonic-gate 	elist_t *el2 = ttdp->t_emem;
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	while (el1 && el2) {
323*0Sstevel@tonic-gate 		if (el1->el_number != el2->el_number ||
324*0Sstevel@tonic-gate 		    strcmp(el1->el_name, el2->el_name) != 0)
325*0Sstevel@tonic-gate 			return (0);
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 		el1 = el1->el_next;
328*0Sstevel@tonic-gate 		el2 = el2->el_next;
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	if (el1 || el2)
332*0Sstevel@tonic-gate 		return (0);
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	return (1);
335*0Sstevel@tonic-gate }
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate /*ARGSUSED*/
338*0Sstevel@tonic-gate static int
339*0Sstevel@tonic-gate equiv_assert(tdesc_t *stdp, tdesc_t *ttdp, equiv_data_t *ed)
340*0Sstevel@tonic-gate {
341*0Sstevel@tonic-gate 	/* foul, evil, and very bad - this is a "shouldn't happen" */
342*0Sstevel@tonic-gate 	assert(1 == 0);
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	return (0);
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate static int
348*0Sstevel@tonic-gate fwd_equiv(tdesc_t *ctdp, tdesc_t *mtdp)
349*0Sstevel@tonic-gate {
350*0Sstevel@tonic-gate 	tdesc_t *defn = (ctdp->t_type == FORWARD ? mtdp : ctdp);
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	return (defn->t_type == STRUCT || defn->t_type == UNION);
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate static int
356*0Sstevel@tonic-gate equiv_node(tdesc_t *ctdp, tdesc_t *mtdp, equiv_data_t *ed)
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate 	int (*equiv)();
359*0Sstevel@tonic-gate 	int mapping;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	if (ctdp->t_emark > ed->ed_clear_mark ||
362*0Sstevel@tonic-gate 	    mtdp->t_emark > ed->ed_clear_mark)
363*0Sstevel@tonic-gate 		return (ctdp->t_emark == mtdp->t_emark);
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	/*
366*0Sstevel@tonic-gate 	 * In normal (non-self-uniquify) mode, we don't want to do equivalency
367*0Sstevel@tonic-gate 	 * checking on a subgraph that has already been checked.  If a mapping
368*0Sstevel@tonic-gate 	 * has already been established for a given child node, we can simply
369*0Sstevel@tonic-gate 	 * compare the mapping for the child node with the ID of the parent
370*0Sstevel@tonic-gate 	 * node.  If we are in self-uniquify mode, then we're comparing two
371*0Sstevel@tonic-gate 	 * subgraphs within the child graph, and thus need to ignore any
372*0Sstevel@tonic-gate 	 * type mappings that have been created, as they are only valid into the
373*0Sstevel@tonic-gate 	 * parent.
374*0Sstevel@tonic-gate 	 */
375*0Sstevel@tonic-gate 	if ((mapping = get_mapping(ed->ed_ta, ctdp->t_id)) > 0 &&
376*0Sstevel@tonic-gate 	    mapping == mtdp->t_id && !ed->ed_selfuniquify)
377*0Sstevel@tonic-gate 		return (1);
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	if (!streq(ctdp->t_name, mtdp->t_name))
380*0Sstevel@tonic-gate 		return (0);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	if (ctdp->t_type != mtdp->t_type) {
383*0Sstevel@tonic-gate 		if (ctdp->t_type == FORWARD || mtdp->t_type == FORWARD)
384*0Sstevel@tonic-gate 			return (fwd_equiv(ctdp, mtdp));
385*0Sstevel@tonic-gate 		else
386*0Sstevel@tonic-gate 			return (0);
387*0Sstevel@tonic-gate 	}
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	ctdp->t_emark = ed->ed_cur_mark;
390*0Sstevel@tonic-gate 	mtdp->t_emark = ed->ed_cur_mark;
391*0Sstevel@tonic-gate 	ed->ed_cur_mark++;
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	if ((equiv = tdesc_ops[ctdp->t_type].equiv) != NULL)
394*0Sstevel@tonic-gate 		return (equiv(ctdp, mtdp, ed));
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	return (1);
397*0Sstevel@tonic-gate }
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate /*
400*0Sstevel@tonic-gate  * We perform an equivalency check on two subgraphs by traversing through them
401*0Sstevel@tonic-gate  * in lockstep.  If a given node is equivalent in both the parent and the child,
402*0Sstevel@tonic-gate  * we mark it in both subgraphs, using the t_emark field, with a monotonically
403*0Sstevel@tonic-gate  * increasing number.  If, in the course of the traversal, we reach a node that
404*0Sstevel@tonic-gate  * we have visited and numbered during this equivalency check, we have a cycle.
405*0Sstevel@tonic-gate  * If the previously-visited nodes don't have the same emark, then the edges
406*0Sstevel@tonic-gate  * that brought us to these nodes are not equivalent, and so the check ends.
407*0Sstevel@tonic-gate  * If the emarks are the same, the edges are equivalent.  We then backtrack and
408*0Sstevel@tonic-gate  * continue the traversal.  If we have exhausted all edges in the subgraph, and
409*0Sstevel@tonic-gate  * have not found any inequivalent nodes, then the subgraphs are equivalent.
410*0Sstevel@tonic-gate  */
411*0Sstevel@tonic-gate static int
412*0Sstevel@tonic-gate equiv_cb(void *bucket, void *arg)
413*0Sstevel@tonic-gate {
414*0Sstevel@tonic-gate 	equiv_data_t *ed = arg;
415*0Sstevel@tonic-gate 	tdesc_t *mtdp = bucket;
416*0Sstevel@tonic-gate 	tdesc_t *ctdp = ed->ed_node;
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 	ed->ed_clear_mark = ed->ed_cur_mark + 1;
419*0Sstevel@tonic-gate 	ed->ed_cur_mark = ed->ed_clear_mark + 1;
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	if (equiv_node(ctdp, mtdp, ed)) {
422*0Sstevel@tonic-gate 		debug(3, "equiv_node matched %d %d\n", ctdp->t_id, mtdp->t_id);
423*0Sstevel@tonic-gate 		ed->ed_tgt = mtdp;
424*0Sstevel@tonic-gate 		/* matched.  stop looking */
425*0Sstevel@tonic-gate 		return (-1);
426*0Sstevel@tonic-gate 	}
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 	return (0);
429*0Sstevel@tonic-gate }
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate /*ARGSUSED1*/
432*0Sstevel@tonic-gate static int
433*0Sstevel@tonic-gate map_td_tree_pre(tdesc_t *ctdp, tdesc_t **ctdpp, void *private)
434*0Sstevel@tonic-gate {
435*0Sstevel@tonic-gate 	merge_cb_data_t *mcd = private;
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	if (get_mapping(mcd->md_ta, ctdp->t_id) > 0)
438*0Sstevel@tonic-gate 		return (0);
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	return (1);
441*0Sstevel@tonic-gate }
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate /*ARGSUSED1*/
444*0Sstevel@tonic-gate static int
445*0Sstevel@tonic-gate map_td_tree_post(tdesc_t *ctdp, tdesc_t **ctdpp, void *private)
446*0Sstevel@tonic-gate {
447*0Sstevel@tonic-gate 	merge_cb_data_t *mcd = private;
448*0Sstevel@tonic-gate 	equiv_data_t ed;
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	ed.ed_ta = mcd->md_ta;
451*0Sstevel@tonic-gate 	ed.ed_clear_mark = mcd->md_parent->td_curemark;
452*0Sstevel@tonic-gate 	ed.ed_cur_mark = mcd->md_parent->td_curemark + 1;
453*0Sstevel@tonic-gate 	ed.ed_node = ctdp;
454*0Sstevel@tonic-gate 	ed.ed_selfuniquify = 0;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	debug(3, "map_td_tree_post on %d %s\n", ctdp->t_id,
457*0Sstevel@tonic-gate 	    ctdp->t_name == NULL ? "(anon)" : ctdp->t_name);
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	if (hash_find_iter(mcd->md_parent->td_layouthash, ctdp,
460*0Sstevel@tonic-gate 	    equiv_cb, &ed) < 0) {
461*0Sstevel@tonic-gate 		/* We found an equivalent node */
462*0Sstevel@tonic-gate 		if (ed.ed_tgt->t_type == FORWARD && ctdp->t_type != FORWARD) {
463*0Sstevel@tonic-gate 			int id = mcd->md_tgt->td_nextid++;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 			debug(3, "Creating new defn type %d\n", id);
466*0Sstevel@tonic-gate 			add_mapping(mcd->md_ta, ctdp->t_id, id);
467*0Sstevel@tonic-gate 			alist_add(mcd->md_fdida, (void *)(ulong_t)ed.ed_tgt,
468*0Sstevel@tonic-gate 			    (void *)(ulong_t)id);
469*0Sstevel@tonic-gate 			hash_add(mcd->md_tdtba, ctdp);
470*0Sstevel@tonic-gate 		} else
471*0Sstevel@tonic-gate 			add_mapping(mcd->md_ta, ctdp->t_id, ed.ed_tgt->t_id);
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	} else if (debug_level > 1 && hash_iter(mcd->md_parent->td_idhash,
474*0Sstevel@tonic-gate 	    equiv_cb, &ed) < 0) {
475*0Sstevel@tonic-gate 		/*
476*0Sstevel@tonic-gate 		 * We didn't find an equivalent node by looking through the
477*0Sstevel@tonic-gate 		 * layout hash, but we somehow found it by performing an
478*0Sstevel@tonic-gate 		 * exhaustive search through the entire graph.  This usually
479*0Sstevel@tonic-gate 		 * means that the "name" hash function is broken.
480*0Sstevel@tonic-gate 		 */
481*0Sstevel@tonic-gate 		terminate("Second pass for %d (%s) == %d\n", ctdp->t_id,
482*0Sstevel@tonic-gate 		    (ctdp->t_name ? ctdp->t_name : "(anon)"), ed.ed_tgt->t_id);
483*0Sstevel@tonic-gate 	} else {
484*0Sstevel@tonic-gate 		int id = mcd->md_tgt->td_nextid++;
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 		debug(3, "Creating new type %d\n", id);
487*0Sstevel@tonic-gate 		add_mapping(mcd->md_ta, ctdp->t_id, id);
488*0Sstevel@tonic-gate 		hash_add(mcd->md_tdtba, ctdp);
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	mcd->md_parent->td_curemark = ed.ed_cur_mark + 1;
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	return (1);
494*0Sstevel@tonic-gate }
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate /*ARGSUSED1*/
497*0Sstevel@tonic-gate static int
498*0Sstevel@tonic-gate map_td_tree_self_post(tdesc_t *ctdp, tdesc_t **ctdpp, void *private)
499*0Sstevel@tonic-gate {
500*0Sstevel@tonic-gate 	merge_cb_data_t *mcd = private;
501*0Sstevel@tonic-gate 	equiv_data_t ed;
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 	ed.ed_ta = mcd->md_ta;
504*0Sstevel@tonic-gate 	ed.ed_clear_mark = mcd->md_parent->td_curemark;
505*0Sstevel@tonic-gate 	ed.ed_cur_mark = mcd->md_parent->td_curemark + 1;
506*0Sstevel@tonic-gate 	ed.ed_node = ctdp;
507*0Sstevel@tonic-gate 	ed.ed_selfuniquify = 1;
508*0Sstevel@tonic-gate 	ed.ed_tgt = NULL;
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	if (hash_find_iter(mcd->md_tdtba, ctdp, equiv_cb, &ed) < 0) {
511*0Sstevel@tonic-gate 		debug(3, "Self check found %d in %d\n", ctdp->t_id,
512*0Sstevel@tonic-gate 		    ed.ed_tgt->t_id);
513*0Sstevel@tonic-gate 		add_mapping(mcd->md_ta, ctdp->t_id,
514*0Sstevel@tonic-gate 		    get_mapping(mcd->md_ta, ed.ed_tgt->t_id));
515*0Sstevel@tonic-gate 	} else if (debug_level > 1 && hash_iter(mcd->md_tdtba,
516*0Sstevel@tonic-gate 	    equiv_cb, &ed) < 0) {
517*0Sstevel@tonic-gate 		/*
518*0Sstevel@tonic-gate 		 * We didn't find an equivalent node using the quick way (going
519*0Sstevel@tonic-gate 		 * through the hash normally), but we did find it by iterating
520*0Sstevel@tonic-gate 		 * through the entire hash.  This usually means that the hash
521*0Sstevel@tonic-gate 		 * function is broken.
522*0Sstevel@tonic-gate 		 */
523*0Sstevel@tonic-gate 		terminate("Self-unique second pass for %d (%s) == %d\n",
524*0Sstevel@tonic-gate 		    ctdp->t_id, (ctdp->t_name ? ctdp->t_name : "(anon)"),
525*0Sstevel@tonic-gate 		    ed.ed_tgt->t_id);
526*0Sstevel@tonic-gate 	} else {
527*0Sstevel@tonic-gate 		int id = mcd->md_tgt->td_nextid++;
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 		debug(3, "Creating new type %d\n", id);
530*0Sstevel@tonic-gate 		add_mapping(mcd->md_ta, ctdp->t_id, id);
531*0Sstevel@tonic-gate 		hash_add(mcd->md_tdtba, ctdp);
532*0Sstevel@tonic-gate 	}
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	mcd->md_parent->td_curemark = ed.ed_cur_mark + 1;
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 	return (1);
537*0Sstevel@tonic-gate }
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate static tdtrav_cb_f map_pre[] = {
540*0Sstevel@tonic-gate 	NULL,
541*0Sstevel@tonic-gate 	map_td_tree_pre,	/* intrinsic */
542*0Sstevel@tonic-gate 	map_td_tree_pre,	/* pointer */
543*0Sstevel@tonic-gate 	map_td_tree_pre,	/* array */
544*0Sstevel@tonic-gate 	map_td_tree_pre,	/* function */
545*0Sstevel@tonic-gate 	map_td_tree_pre,	/* struct */
546*0Sstevel@tonic-gate 	map_td_tree_pre,	/* union */
547*0Sstevel@tonic-gate 	map_td_tree_pre,	/* enum */
548*0Sstevel@tonic-gate 	map_td_tree_pre,	/* forward */
549*0Sstevel@tonic-gate 	map_td_tree_pre,	/* typedef */
550*0Sstevel@tonic-gate 	tdtrav_assert,		/* typedef_unres */
551*0Sstevel@tonic-gate 	map_td_tree_pre,	/* volatile */
552*0Sstevel@tonic-gate 	map_td_tree_pre,	/* const */
553*0Sstevel@tonic-gate 	map_td_tree_pre		/* restrict */
554*0Sstevel@tonic-gate };
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate static tdtrav_cb_f map_post[] = {
557*0Sstevel@tonic-gate 	NULL,
558*0Sstevel@tonic-gate 	map_td_tree_post,	/* intrinsic */
559*0Sstevel@tonic-gate 	map_td_tree_post,	/* pointer */
560*0Sstevel@tonic-gate 	map_td_tree_post,	/* array */
561*0Sstevel@tonic-gate 	map_td_tree_post,	/* function */
562*0Sstevel@tonic-gate 	map_td_tree_post,	/* struct */
563*0Sstevel@tonic-gate 	map_td_tree_post,	/* union */
564*0Sstevel@tonic-gate 	map_td_tree_post,	/* enum */
565*0Sstevel@tonic-gate 	map_td_tree_post,	/* forward */
566*0Sstevel@tonic-gate 	map_td_tree_post,	/* typedef */
567*0Sstevel@tonic-gate 	tdtrav_assert,		/* typedef_unres */
568*0Sstevel@tonic-gate 	map_td_tree_post,	/* volatile */
569*0Sstevel@tonic-gate 	map_td_tree_post,	/* const */
570*0Sstevel@tonic-gate 	map_td_tree_post	/* restrict */
571*0Sstevel@tonic-gate };
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate static tdtrav_cb_f map_self_post[] = {
574*0Sstevel@tonic-gate 	NULL,
575*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* intrinsic */
576*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* pointer */
577*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* array */
578*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* function */
579*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* struct */
580*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* union */
581*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* enum */
582*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* forward */
583*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* typedef */
584*0Sstevel@tonic-gate 	tdtrav_assert,		/* typedef_unres */
585*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* volatile */
586*0Sstevel@tonic-gate 	map_td_tree_self_post,	/* const */
587*0Sstevel@tonic-gate 	map_td_tree_self_post	/* restrict */
588*0Sstevel@tonic-gate };
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate /*
591*0Sstevel@tonic-gate  * Determining equivalence of iidesc_t nodes
592*0Sstevel@tonic-gate  */
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate typedef struct iifind_data {
595*0Sstevel@tonic-gate 	iidesc_t *iif_template;
596*0Sstevel@tonic-gate 	alist_t *iif_ta;
597*0Sstevel@tonic-gate 	int iif_newidx;
598*0Sstevel@tonic-gate 	int iif_refmerge;
599*0Sstevel@tonic-gate } iifind_data_t;
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate /*
602*0Sstevel@tonic-gate  * Check to see if this iidesc_t (node) - the current one on the list we're
603*0Sstevel@tonic-gate  * iterating through - matches the target one (iif->iif_template).  Return -1
604*0Sstevel@tonic-gate  * if it matches, to stop the iteration.
605*0Sstevel@tonic-gate  */
606*0Sstevel@tonic-gate static int
607*0Sstevel@tonic-gate iidesc_match(void *data, void *arg)
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate 	iidesc_t *node = data;
610*0Sstevel@tonic-gate 	iifind_data_t *iif = arg;
611*0Sstevel@tonic-gate 	int i;
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	if (node->ii_type != iif->iif_template->ii_type ||
614*0Sstevel@tonic-gate 	    !streq(node->ii_name, iif->iif_template->ii_name) ||
615*0Sstevel@tonic-gate 	    node->ii_dtype->t_id != iif->iif_newidx)
616*0Sstevel@tonic-gate 		return (0);
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	if ((node->ii_type == II_SVAR || node->ii_type == II_SFUN) &&
619*0Sstevel@tonic-gate 	    !streq(node->ii_owner, iif->iif_template->ii_owner))
620*0Sstevel@tonic-gate 		return (0);
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	if (node->ii_nargs != iif->iif_template->ii_nargs)
623*0Sstevel@tonic-gate 		return (0);
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 	for (i = 0; i < node->ii_nargs; i++) {
626*0Sstevel@tonic-gate 		if (get_mapping(iif->iif_ta,
627*0Sstevel@tonic-gate 		    iif->iif_template->ii_args[i]->t_id) !=
628*0Sstevel@tonic-gate 		    node->ii_args[i]->t_id)
629*0Sstevel@tonic-gate 			return (0);
630*0Sstevel@tonic-gate 	}
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	if (iif->iif_refmerge) {
633*0Sstevel@tonic-gate 		switch (iif->iif_template->ii_type) {
634*0Sstevel@tonic-gate 		case II_GFUN:
635*0Sstevel@tonic-gate 		case II_SFUN:
636*0Sstevel@tonic-gate 		case II_GVAR:
637*0Sstevel@tonic-gate 		case II_SVAR:
638*0Sstevel@tonic-gate 			debug(3, "suppressing duping of %d %s from %s\n",
639*0Sstevel@tonic-gate 			    iif->iif_template->ii_type,
640*0Sstevel@tonic-gate 			    iif->iif_template->ii_name,
641*0Sstevel@tonic-gate 			    (iif->iif_template->ii_owner ?
642*0Sstevel@tonic-gate 			    iif->iif_template->ii_owner : "NULL"));
643*0Sstevel@tonic-gate 			return (0);
644*0Sstevel@tonic-gate 		case II_NOT:
645*0Sstevel@tonic-gate 		case II_PSYM:
646*0Sstevel@tonic-gate 		case II_SOU:
647*0Sstevel@tonic-gate 		case II_TYPE:
648*0Sstevel@tonic-gate 			break;
649*0Sstevel@tonic-gate 		}
650*0Sstevel@tonic-gate 	}
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	return (-1);
653*0Sstevel@tonic-gate }
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate static int
656*0Sstevel@tonic-gate merge_type_cb(void *data, void *arg)
657*0Sstevel@tonic-gate {
658*0Sstevel@tonic-gate 	iidesc_t *sii = data;
659*0Sstevel@tonic-gate 	merge_cb_data_t *mcd = arg;
660*0Sstevel@tonic-gate 	iifind_data_t iif;
661*0Sstevel@tonic-gate 	tdtrav_cb_f *post;
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	post = (mcd->md_flags & MCD_F_SELFUNIQUIFY ? map_self_post : map_post);
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 	/* Map the tdesc nodes */
666*0Sstevel@tonic-gate 	(void) iitraverse(sii, &mcd->md_parent->td_curvgen, NULL, map_pre, post,
667*0Sstevel@tonic-gate 	    mcd);
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	/* Map the iidesc nodes */
670*0Sstevel@tonic-gate 	iif.iif_template = sii;
671*0Sstevel@tonic-gate 	iif.iif_ta = mcd->md_ta;
672*0Sstevel@tonic-gate 	iif.iif_newidx = get_mapping(mcd->md_ta, sii->ii_dtype->t_id);
673*0Sstevel@tonic-gate 	iif.iif_refmerge = (mcd->md_flags & MCD_F_REFMERGE);
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 	if (hash_match(mcd->md_parent->td_iihash, sii, iidesc_match,
676*0Sstevel@tonic-gate 	    &iif) == 1)
677*0Sstevel@tonic-gate 		/* successfully mapped */
678*0Sstevel@tonic-gate 		return (1);
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	debug(3, "tba %s (%d)\n", (sii->ii_name ? sii->ii_name : "(anon)"),
681*0Sstevel@tonic-gate 	    sii->ii_type);
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 	list_add(mcd->md_iitba, sii);
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 	return (0);
686*0Sstevel@tonic-gate }
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate static int
689*0Sstevel@tonic-gate remap_node(tdesc_t **tgtp, tdesc_t *oldtgt, int selftid, tdesc_t *newself,
690*0Sstevel@tonic-gate     merge_cb_data_t *mcd)
691*0Sstevel@tonic-gate {
692*0Sstevel@tonic-gate 	tdesc_t *tgt = NULL;
693*0Sstevel@tonic-gate 	tdesc_t template;
694*0Sstevel@tonic-gate 	int oldid = oldtgt->t_id;
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate 	if (oldid == selftid) {
697*0Sstevel@tonic-gate 		*tgtp = newself;
698*0Sstevel@tonic-gate 		return (1);
699*0Sstevel@tonic-gate 	}
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	if ((template.t_id = get_mapping(mcd->md_ta, oldid)) == 0)
702*0Sstevel@tonic-gate 		terminate("failed to get mapping for tid %d\n", oldid);
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	if (!hash_find(mcd->md_parent->td_idhash, (void *)&template,
705*0Sstevel@tonic-gate 	    (void *)&tgt) && (!(mcd->md_flags & MCD_F_REFMERGE) ||
706*0Sstevel@tonic-gate 	    !hash_find(mcd->md_tgt->td_idhash, (void *)&template,
707*0Sstevel@tonic-gate 	    (void *)&tgt))) {
708*0Sstevel@tonic-gate 		debug(3, "Remap couldn't find %d (from %d)\n", template.t_id,
709*0Sstevel@tonic-gate 		    oldid);
710*0Sstevel@tonic-gate 		*tgtp = oldtgt;
711*0Sstevel@tonic-gate 		list_add(mcd->md_tdtbr, tgtp);
712*0Sstevel@tonic-gate 		return (0);
713*0Sstevel@tonic-gate 	}
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 	*tgtp = tgt;
716*0Sstevel@tonic-gate 	return (1);
717*0Sstevel@tonic-gate }
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate static tdesc_t *
720*0Sstevel@tonic-gate conjure_template(tdesc_t *old, int newselfid)
721*0Sstevel@tonic-gate {
722*0Sstevel@tonic-gate 	tdesc_t *new = xcalloc(sizeof (tdesc_t));
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate 	new->t_name = old->t_name ? xstrdup(old->t_name) : NULL;
725*0Sstevel@tonic-gate 	new->t_type = old->t_type;
726*0Sstevel@tonic-gate 	new->t_size = old->t_size;
727*0Sstevel@tonic-gate 	new->t_id = newselfid;
728*0Sstevel@tonic-gate 	new->t_flags = old->t_flags;
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	return (new);
731*0Sstevel@tonic-gate }
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate /*ARGSUSED2*/
734*0Sstevel@tonic-gate static tdesc_t *
735*0Sstevel@tonic-gate conjure_intrinsic(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
736*0Sstevel@tonic-gate {
737*0Sstevel@tonic-gate 	tdesc_t *new = conjure_template(old, newselfid);
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate 	new->t_intr = xmalloc(sizeof (intr_t));
740*0Sstevel@tonic-gate 	bcopy(old->t_intr, new->t_intr, sizeof (intr_t));
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	return (new);
743*0Sstevel@tonic-gate }
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate static tdesc_t *
746*0Sstevel@tonic-gate conjure_plain(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
747*0Sstevel@tonic-gate {
748*0Sstevel@tonic-gate 	tdesc_t *new = conjure_template(old, newselfid);
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 	(void) remap_node(&new->t_tdesc, old->t_tdesc, old->t_id, new, mcd);
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 	return (new);
753*0Sstevel@tonic-gate }
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate static tdesc_t *
756*0Sstevel@tonic-gate conjure_function(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
757*0Sstevel@tonic-gate {
758*0Sstevel@tonic-gate 	tdesc_t *new = conjure_template(old, newselfid);
759*0Sstevel@tonic-gate 	fndef_t *nfn = xmalloc(sizeof (fndef_t));
760*0Sstevel@tonic-gate 	fndef_t *ofn = old->t_fndef;
761*0Sstevel@tonic-gate 	int i;
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate 	(void) remap_node(&nfn->fn_ret, ofn->fn_ret, old->t_id, new, mcd);
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate 	nfn->fn_nargs = ofn->fn_nargs;
766*0Sstevel@tonic-gate 	nfn->fn_vargs = ofn->fn_vargs;
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	if (nfn->fn_nargs > 0)
769*0Sstevel@tonic-gate 		nfn->fn_args = xcalloc(sizeof (tdesc_t *) * ofn->fn_nargs);
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	for (i = 0; i < ofn->fn_nargs; i++) {
772*0Sstevel@tonic-gate 		(void) remap_node(&nfn->fn_args[i], ofn->fn_args[i], old->t_id,
773*0Sstevel@tonic-gate 		    new, mcd);
774*0Sstevel@tonic-gate 	}
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate 	new->t_fndef = nfn;
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 	return (new);
779*0Sstevel@tonic-gate }
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate static tdesc_t *
782*0Sstevel@tonic-gate conjure_array(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
783*0Sstevel@tonic-gate {
784*0Sstevel@tonic-gate 	tdesc_t *new = conjure_template(old, newselfid);
785*0Sstevel@tonic-gate 	ardef_t *nar = xmalloc(sizeof (ardef_t));
786*0Sstevel@tonic-gate 	ardef_t *oar = old->t_ardef;
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	(void) remap_node(&nar->ad_contents, oar->ad_contents, old->t_id, new,
789*0Sstevel@tonic-gate 	    mcd);
790*0Sstevel@tonic-gate 	(void) remap_node(&nar->ad_idxtype, oar->ad_idxtype, old->t_id, new,
791*0Sstevel@tonic-gate 	    mcd);
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	nar->ad_nelems = oar->ad_nelems;
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate 	new->t_ardef = nar;
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	return (new);
798*0Sstevel@tonic-gate }
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate static tdesc_t *
801*0Sstevel@tonic-gate conjure_su(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
802*0Sstevel@tonic-gate {
803*0Sstevel@tonic-gate 	tdesc_t *new = conjure_template(old, newselfid);
804*0Sstevel@tonic-gate 	mlist_t *omem, **nmemp;
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	for (omem = old->t_members, nmemp = &new->t_members;
807*0Sstevel@tonic-gate 	    omem; omem = omem->ml_next, nmemp = &((*nmemp)->ml_next)) {
808*0Sstevel@tonic-gate 		*nmemp = xmalloc(sizeof (mlist_t));
809*0Sstevel@tonic-gate 		(*nmemp)->ml_offset = omem->ml_offset;
810*0Sstevel@tonic-gate 		(*nmemp)->ml_size = omem->ml_size;
811*0Sstevel@tonic-gate 		(*nmemp)->ml_name = xstrdup(omem->ml_name);
812*0Sstevel@tonic-gate 		(void) remap_node(&((*nmemp)->ml_type), omem->ml_type,
813*0Sstevel@tonic-gate 		    old->t_id, new, mcd);
814*0Sstevel@tonic-gate 	}
815*0Sstevel@tonic-gate 	*nmemp = NULL;
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 	return (new);
818*0Sstevel@tonic-gate }
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate /*ARGSUSED2*/
821*0Sstevel@tonic-gate static tdesc_t *
822*0Sstevel@tonic-gate conjure_enum(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
823*0Sstevel@tonic-gate {
824*0Sstevel@tonic-gate 	tdesc_t *new = conjure_template(old, newselfid);
825*0Sstevel@tonic-gate 	elist_t *oel, **nelp;
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 	for (oel = old->t_emem, nelp = &new->t_emem;
828*0Sstevel@tonic-gate 	    oel; oel = oel->el_next, nelp = &((*nelp)->el_next)) {
829*0Sstevel@tonic-gate 		*nelp = xmalloc(sizeof (elist_t));
830*0Sstevel@tonic-gate 		(*nelp)->el_name = xstrdup(oel->el_name);
831*0Sstevel@tonic-gate 		(*nelp)->el_number = oel->el_number;
832*0Sstevel@tonic-gate 	}
833*0Sstevel@tonic-gate 	*nelp = NULL;
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 	return (new);
836*0Sstevel@tonic-gate }
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate /*ARGSUSED2*/
839*0Sstevel@tonic-gate static tdesc_t *
840*0Sstevel@tonic-gate conjure_forward(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
841*0Sstevel@tonic-gate {
842*0Sstevel@tonic-gate 	tdesc_t *new = conjure_template(old, newselfid);
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 	list_add(&mcd->md_tgt->td_fwdlist, new);
845*0Sstevel@tonic-gate 
846*0Sstevel@tonic-gate 	return (new);
847*0Sstevel@tonic-gate }
848*0Sstevel@tonic-gate 
849*0Sstevel@tonic-gate /*ARGSUSED*/
850*0Sstevel@tonic-gate static tdesc_t *
851*0Sstevel@tonic-gate conjure_assert(tdesc_t *old, int newselfid, merge_cb_data_t *mcd)
852*0Sstevel@tonic-gate {
853*0Sstevel@tonic-gate 	assert(1 == 0);
854*0Sstevel@tonic-gate 	return (NULL);
855*0Sstevel@tonic-gate }
856*0Sstevel@tonic-gate 
857*0Sstevel@tonic-gate static iidesc_t *
858*0Sstevel@tonic-gate conjure_iidesc(iidesc_t *old, merge_cb_data_t *mcd)
859*0Sstevel@tonic-gate {
860*0Sstevel@tonic-gate 	iidesc_t *new = iidesc_dup(old);
861*0Sstevel@tonic-gate 	int i;
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 	(void) remap_node(&new->ii_dtype, old->ii_dtype, -1, NULL, mcd);
864*0Sstevel@tonic-gate 	for (i = 0; i < new->ii_nargs; i++) {
865*0Sstevel@tonic-gate 		(void) remap_node(&new->ii_args[i], old->ii_args[i], -1, NULL,
866*0Sstevel@tonic-gate 		    mcd);
867*0Sstevel@tonic-gate 	}
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 	return (new);
870*0Sstevel@tonic-gate }
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate static int
873*0Sstevel@tonic-gate fwd_redir(tdesc_t *fwd, tdesc_t **fwdp, void *private)
874*0Sstevel@tonic-gate {
875*0Sstevel@tonic-gate 	alist_t *map = private;
876*0Sstevel@tonic-gate 	tdesc_t *defn;
877*0Sstevel@tonic-gate 
878*0Sstevel@tonic-gate 	if (!alist_find(map, (void *)fwd, (void **)&defn))
879*0Sstevel@tonic-gate 		return (0);
880*0Sstevel@tonic-gate 
881*0Sstevel@tonic-gate 	debug(3, "Redirecting an edge to %s\n",
882*0Sstevel@tonic-gate 	    (defn->t_name ? defn->t_name : "(anon)"));
883*0Sstevel@tonic-gate 
884*0Sstevel@tonic-gate 	*fwdp = defn;
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate 	return (1);
887*0Sstevel@tonic-gate }
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate static tdtrav_cb_f fwd_redir_cbs[] = {
890*0Sstevel@tonic-gate 	NULL,
891*0Sstevel@tonic-gate 	NULL,			/* intrinsic */
892*0Sstevel@tonic-gate 	NULL,			/* pointer */
893*0Sstevel@tonic-gate 	NULL,			/* array */
894*0Sstevel@tonic-gate 	NULL,			/* function */
895*0Sstevel@tonic-gate 	NULL,			/* struct */
896*0Sstevel@tonic-gate 	NULL,			/* union */
897*0Sstevel@tonic-gate 	NULL,			/* enum */
898*0Sstevel@tonic-gate 	fwd_redir,		/* forward */
899*0Sstevel@tonic-gate 	NULL,			/* typedef */
900*0Sstevel@tonic-gate 	tdtrav_assert,		/* typedef_unres */
901*0Sstevel@tonic-gate 	NULL,			/* volatile */
902*0Sstevel@tonic-gate 	NULL,			/* const */
903*0Sstevel@tonic-gate 	NULL			/* restrict */
904*0Sstevel@tonic-gate };
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate typedef struct redir_mstr_data {
907*0Sstevel@tonic-gate 	tdata_t *rmd_tgt;
908*0Sstevel@tonic-gate 	alist_t *rmd_map;
909*0Sstevel@tonic-gate } redir_mstr_data_t;
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate static int
912*0Sstevel@tonic-gate redir_mstr_fwd_cb(void *name, void *value, void *arg)
913*0Sstevel@tonic-gate {
914*0Sstevel@tonic-gate 	tdesc_t *fwd = name;
915*0Sstevel@tonic-gate 	int defnid = (int)value;
916*0Sstevel@tonic-gate 	redir_mstr_data_t *rmd = arg;
917*0Sstevel@tonic-gate 	tdesc_t template;
918*0Sstevel@tonic-gate 	tdesc_t *defn;
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 	template.t_id = defnid;
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate 	if (!hash_find(rmd->rmd_tgt->td_idhash, (void *)&template,
923*0Sstevel@tonic-gate 	    (void *)&defn)) {
924*0Sstevel@tonic-gate 		terminate("Couldn't unforward %d (%s)\n", defnid,
925*0Sstevel@tonic-gate 		    (defn->t_name ? defn->t_name : "(anon)"));
926*0Sstevel@tonic-gate 	}
927*0Sstevel@tonic-gate 
928*0Sstevel@tonic-gate 	debug(3, "Forward map: resolved %d to %s\n",
929*0Sstevel@tonic-gate 	    defnid, (defn->t_name ? defn->t_name : "(anon)"));
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate 	alist_add(rmd->rmd_map, (void *)fwd, (void *)defn);
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate 	return (1);
934*0Sstevel@tonic-gate }
935*0Sstevel@tonic-gate 
936*0Sstevel@tonic-gate static void
937*0Sstevel@tonic-gate redir_mstr_fwds(merge_cb_data_t *mcd)
938*0Sstevel@tonic-gate {
939*0Sstevel@tonic-gate 	redir_mstr_data_t rmd;
940*0Sstevel@tonic-gate 	alist_t *map = alist_new(NULL, NULL);
941*0Sstevel@tonic-gate 
942*0Sstevel@tonic-gate 	rmd.rmd_tgt = mcd->md_tgt;
943*0Sstevel@tonic-gate 	rmd.rmd_map = map;
944*0Sstevel@tonic-gate 
945*0Sstevel@tonic-gate 	if (alist_iter(mcd->md_fdida, redir_mstr_fwd_cb, &rmd)) {
946*0Sstevel@tonic-gate 		(void) iitraverse_hash(mcd->md_tgt->td_iihash,
947*0Sstevel@tonic-gate 		    &mcd->md_tgt->td_curvgen, fwd_redir_cbs, NULL, NULL, map);
948*0Sstevel@tonic-gate 	}
949*0Sstevel@tonic-gate 
950*0Sstevel@tonic-gate 	alist_free(map);
951*0Sstevel@tonic-gate }
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate static int
954*0Sstevel@tonic-gate add_iitba_cb(void *data, void *private)
955*0Sstevel@tonic-gate {
956*0Sstevel@tonic-gate 	merge_cb_data_t *mcd = private;
957*0Sstevel@tonic-gate 	iidesc_t *tba = data;
958*0Sstevel@tonic-gate 	iidesc_t *new;
959*0Sstevel@tonic-gate 	iifind_data_t iif;
960*0Sstevel@tonic-gate 	int newidx;
961*0Sstevel@tonic-gate 
962*0Sstevel@tonic-gate 	newidx = get_mapping(mcd->md_ta, tba->ii_dtype->t_id);
963*0Sstevel@tonic-gate 	assert(newidx != -1);
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate 	(void) list_remove(mcd->md_iitba, data, NULL, NULL);
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate 	iif.iif_template = tba;
968*0Sstevel@tonic-gate 	iif.iif_ta = mcd->md_ta;
969*0Sstevel@tonic-gate 	iif.iif_newidx = newidx;
970*0Sstevel@tonic-gate 	iif.iif_refmerge = (mcd->md_flags & MCD_F_REFMERGE);
971*0Sstevel@tonic-gate 
972*0Sstevel@tonic-gate 	if (hash_match(mcd->md_parent->td_iihash, tba, iidesc_match,
973*0Sstevel@tonic-gate 	    &iif) == 1) {
974*0Sstevel@tonic-gate 		debug(3, "iidesc_t %s already exists\n",
975*0Sstevel@tonic-gate 		    (tba->ii_name ? tba->ii_name : "(anon)"));
976*0Sstevel@tonic-gate 		return (1);
977*0Sstevel@tonic-gate 	}
978*0Sstevel@tonic-gate 
979*0Sstevel@tonic-gate 	new = conjure_iidesc(tba, mcd);
980*0Sstevel@tonic-gate 	hash_add(mcd->md_tgt->td_iihash, new);
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 	return (1);
983*0Sstevel@tonic-gate }
984*0Sstevel@tonic-gate 
985*0Sstevel@tonic-gate static int
986*0Sstevel@tonic-gate add_tdesc(tdesc_t *oldtdp, int newid, merge_cb_data_t *mcd)
987*0Sstevel@tonic-gate {
988*0Sstevel@tonic-gate 	tdesc_t *newtdp;
989*0Sstevel@tonic-gate 	tdesc_t template;
990*0Sstevel@tonic-gate 
991*0Sstevel@tonic-gate 	template.t_id = newid;
992*0Sstevel@tonic-gate 	assert(hash_find(mcd->md_parent->td_idhash,
993*0Sstevel@tonic-gate 	    (void *)&template, NULL) == 0);
994*0Sstevel@tonic-gate 
995*0Sstevel@tonic-gate 	debug(3, "trying to conjure %d %s (%d) as %d\n",
996*0Sstevel@tonic-gate 	    oldtdp->t_type, (oldtdp->t_name ? oldtdp->t_name : "(anon)"),
997*0Sstevel@tonic-gate 	    oldtdp->t_id, newid);
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 	if ((newtdp = tdesc_ops[oldtdp->t_type].conjure(oldtdp, newid,
1000*0Sstevel@tonic-gate 	    mcd)) == NULL)
1001*0Sstevel@tonic-gate 		/* couldn't map everything */
1002*0Sstevel@tonic-gate 		return (0);
1003*0Sstevel@tonic-gate 
1004*0Sstevel@tonic-gate 	debug(3, "succeeded\n");
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 	hash_add(mcd->md_tgt->td_idhash, newtdp);
1007*0Sstevel@tonic-gate 	hash_add(mcd->md_tgt->td_layouthash, newtdp);
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 	return (1);
1010*0Sstevel@tonic-gate }
1011*0Sstevel@tonic-gate 
1012*0Sstevel@tonic-gate static int
1013*0Sstevel@tonic-gate add_tdtba_cb(void *data, void *arg)
1014*0Sstevel@tonic-gate {
1015*0Sstevel@tonic-gate 	tdesc_t *tdp = data;
1016*0Sstevel@tonic-gate 	merge_cb_data_t *mcd = arg;
1017*0Sstevel@tonic-gate 	int newid;
1018*0Sstevel@tonic-gate 	int rc;
1019*0Sstevel@tonic-gate 
1020*0Sstevel@tonic-gate 	newid = get_mapping(mcd->md_ta, tdp->t_id);
1021*0Sstevel@tonic-gate 	assert(newid != -1);
1022*0Sstevel@tonic-gate 
1023*0Sstevel@tonic-gate 	if ((rc = add_tdesc(tdp, newid, mcd)))
1024*0Sstevel@tonic-gate 		hash_remove(mcd->md_tdtba, (void *)tdp);
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 	return (rc);
1027*0Sstevel@tonic-gate }
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate static int
1030*0Sstevel@tonic-gate add_tdtbr_cb(void *data, void *arg)
1031*0Sstevel@tonic-gate {
1032*0Sstevel@tonic-gate 	tdesc_t **tdpp = data;
1033*0Sstevel@tonic-gate 	merge_cb_data_t *mcd = arg;
1034*0Sstevel@tonic-gate 
1035*0Sstevel@tonic-gate 	debug(3, "Remapping %s (%d)\n",
1036*0Sstevel@tonic-gate 	    ((*tdpp)->t_name ? (*tdpp)->t_name : "(anon)"), (*tdpp)->t_id);
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate 	if (!remap_node(tdpp, *tdpp, -1, NULL, mcd))
1039*0Sstevel@tonic-gate 		return (0);
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate 	(void) list_remove(mcd->md_tdtbr, (void *)tdpp, NULL, NULL);
1042*0Sstevel@tonic-gate 	return (1);
1043*0Sstevel@tonic-gate }
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate static void
1046*0Sstevel@tonic-gate merge_types(hash_t *src, merge_cb_data_t *mcd)
1047*0Sstevel@tonic-gate {
1048*0Sstevel@tonic-gate 	list_t *iitba = NULL;
1049*0Sstevel@tonic-gate 	list_t *tdtbr = NULL;
1050*0Sstevel@tonic-gate 	int iirc, tdrc;
1051*0Sstevel@tonic-gate 
1052*0Sstevel@tonic-gate 	mcd->md_iitba = &iitba;
1053*0Sstevel@tonic-gate 	mcd->md_tdtba = hash_new(TDATA_LAYOUT_HASH_SIZE, tdesc_layouthash,
1054*0Sstevel@tonic-gate 	    tdesc_layoutcmp);
1055*0Sstevel@tonic-gate 	mcd->md_tdtbr = &tdtbr;
1056*0Sstevel@tonic-gate 
1057*0Sstevel@tonic-gate 	(void) hash_iter(src, merge_type_cb, mcd);
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 	tdrc = hash_iter(mcd->md_tdtba, add_tdtba_cb, (void *)mcd);
1060*0Sstevel@tonic-gate 	debug(3, "add_tdtba_cb added %d items\n", tdrc);
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate 	iirc = list_iter(*mcd->md_iitba, add_iitba_cb, (void *)mcd);
1063*0Sstevel@tonic-gate 	debug(3, "add_iitba_cb added %d items\n", iirc);
1064*0Sstevel@tonic-gate 
1065*0Sstevel@tonic-gate 	assert(list_count(*mcd->md_iitba) == 0 &&
1066*0Sstevel@tonic-gate 	    hash_count(mcd->md_tdtba) == 0);
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate 	tdrc = list_iter(*mcd->md_tdtbr, add_tdtbr_cb, (void *)mcd);
1069*0Sstevel@tonic-gate 	debug(3, "add_tdtbr_cb added %d items\n", tdrc);
1070*0Sstevel@tonic-gate 
1071*0Sstevel@tonic-gate 	if (list_count(*mcd->md_tdtbr) != 0)
1072*0Sstevel@tonic-gate 		terminate("Couldn't remap all nodes\n");
1073*0Sstevel@tonic-gate 
1074*0Sstevel@tonic-gate 	/*
1075*0Sstevel@tonic-gate 	 * We now have an alist of master forwards and the ids of the new master
1076*0Sstevel@tonic-gate 	 * definitions for those forwards in mcd->md_fdida.  By this point,
1077*0Sstevel@tonic-gate 	 * we're guaranteed that all of the master definitions referenced in
1078*0Sstevel@tonic-gate 	 * fdida have been added to the master tree.  We now traverse through
1079*0Sstevel@tonic-gate 	 * the master tree, redirecting all edges inbound to forwards that have
1080*0Sstevel@tonic-gate 	 * definitions to those definitions.
1081*0Sstevel@tonic-gate 	 */
1082*0Sstevel@tonic-gate 	if (mcd->md_parent == mcd->md_tgt) {
1083*0Sstevel@tonic-gate 		redir_mstr_fwds(mcd);
1084*0Sstevel@tonic-gate 	}
1085*0Sstevel@tonic-gate }
1086*0Sstevel@tonic-gate 
1087*0Sstevel@tonic-gate void
1088*0Sstevel@tonic-gate merge_into_master(tdata_t *cur, tdata_t *mstr, tdata_t *tgt, int selfuniquify)
1089*0Sstevel@tonic-gate {
1090*0Sstevel@tonic-gate 	merge_cb_data_t mcd;
1091*0Sstevel@tonic-gate 
1092*0Sstevel@tonic-gate 	cur->td_ref++;
1093*0Sstevel@tonic-gate 	mstr->td_ref++;
1094*0Sstevel@tonic-gate 	if (tgt)
1095*0Sstevel@tonic-gate 		tgt->td_ref++;
1096*0Sstevel@tonic-gate 
1097*0Sstevel@tonic-gate 	assert(cur->td_ref == 1 && mstr->td_ref == 1 &&
1098*0Sstevel@tonic-gate 	    (tgt == NULL || tgt->td_ref == 1));
1099*0Sstevel@tonic-gate 
1100*0Sstevel@tonic-gate 	mcd.md_parent = mstr;
1101*0Sstevel@tonic-gate 	mcd.md_tgt = (tgt ? tgt : mstr);
1102*0Sstevel@tonic-gate 	mcd.md_ta = alist_new(NULL, NULL);
1103*0Sstevel@tonic-gate 	mcd.md_fdida = alist_new(NULL, NULL);
1104*0Sstevel@tonic-gate 	mcd.md_flags = 0;
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 	if (selfuniquify)
1107*0Sstevel@tonic-gate 		mcd.md_flags |= MCD_F_SELFUNIQUIFY;
1108*0Sstevel@tonic-gate 	if (tgt)
1109*0Sstevel@tonic-gate 		mcd.md_flags |= MCD_F_REFMERGE;
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate 	mstr->td_curvgen = MAX(mstr->td_curvgen, cur->td_curvgen);
1112*0Sstevel@tonic-gate 	mstr->td_curemark = MAX(mstr->td_curemark, cur->td_curemark);
1113*0Sstevel@tonic-gate 
1114*0Sstevel@tonic-gate 	merge_types(cur->td_iihash, &mcd);
1115*0Sstevel@tonic-gate 
1116*0Sstevel@tonic-gate 	if (debug_level >= 3) {
1117*0Sstevel@tonic-gate 		debug(3, "Type association stats\n");
1118*0Sstevel@tonic-gate 		alist_stats(mcd.md_ta, 0);
1119*0Sstevel@tonic-gate 		debug(3, "Layout hash stats\n");
1120*0Sstevel@tonic-gate 		hash_stats(mcd.md_tgt->td_layouthash, 1);
1121*0Sstevel@tonic-gate 	}
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate 	alist_free(mcd.md_fdida);
1124*0Sstevel@tonic-gate 	alist_free(mcd.md_ta);
1125*0Sstevel@tonic-gate 
1126*0Sstevel@tonic-gate 	cur->td_ref--;
1127*0Sstevel@tonic-gate 	mstr->td_ref--;
1128*0Sstevel@tonic-gate 	if (tgt)
1129*0Sstevel@tonic-gate 		tgt->td_ref--;
1130*0Sstevel@tonic-gate }
1131*0Sstevel@tonic-gate 
1132*0Sstevel@tonic-gate tdesc_ops_t tdesc_ops[] = {
1133*0Sstevel@tonic-gate 	{ "ERROR! BAD tdesc TYPE", NULL, NULL },
1134*0Sstevel@tonic-gate 	{ "intrinsic",		equiv_intrinsic,	conjure_intrinsic },
1135*0Sstevel@tonic-gate 	{ "pointer", 		equiv_plain,		conjure_plain },
1136*0Sstevel@tonic-gate 	{ "array", 		equiv_array,		conjure_array },
1137*0Sstevel@tonic-gate 	{ "function", 		equiv_function,		conjure_function },
1138*0Sstevel@tonic-gate 	{ "struct",		equiv_su,		conjure_su },
1139*0Sstevel@tonic-gate 	{ "union",		equiv_su,		conjure_su },
1140*0Sstevel@tonic-gate 	{ "enum",		equiv_enum,		conjure_enum },
1141*0Sstevel@tonic-gate 	{ "forward",		NULL,			conjure_forward },
1142*0Sstevel@tonic-gate 	{ "typedef",		equiv_plain,		conjure_plain },
1143*0Sstevel@tonic-gate 	{ "typedef_unres",	equiv_assert,		conjure_assert },
1144*0Sstevel@tonic-gate 	{ "volatile",		equiv_plain,		conjure_plain },
1145*0Sstevel@tonic-gate 	{ "const", 		equiv_plain,		conjure_plain },
1146*0Sstevel@tonic-gate 	{ "restrict",		equiv_plain,		conjure_plain }
1147*0Sstevel@tonic-gate };
1148