1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
42 struct segtabnode
*left
;
43 struct segtabnode
*right
;
45 * counts of how many are left or right, for use in reorganising
58 * functions used by write_output() to manipulate associations
59 * between segment numbers and locations (which are built up on a per
60 * module basis, but we only need one module at a time...)
62 * implementation: we build a binary tree.
65 void init_seglocations(segtab
* root
)
70 static void descend_tree_add(struct segtabnode
**node
,
71 int localseg
, int destseg
, int32_t offset
)
76 *node
= nasm_malloc(sizeof(**node
));
78 fprintf(stderr
, "segment table: out of memory\n");
81 (*node
)->localseg
= localseg
;
82 (*node
)->offset
= offset
;
84 (*node
)->leftcount
= 0;
85 (*node
)->right
= NULL
;
86 (*node
)->rightcount
= 0;
87 (*node
)->destseg
= destseg
;
91 if (localseg
< (*node
)->localseg
) {
93 descend_tree_add(&(*node
)->left
, localseg
, destseg
, offset
);
95 if ((*node
)->leftcount
> (*node
)->rightcount
+ 2) {
98 n
->left
= (*node
)->right
;
99 n
->leftcount
= (*node
)->rightcount
;
101 (*node
)->rightcount
= n
->leftcount
+ n
->rightcount
+ 1;
104 (*node
)->rightcount
++;
105 descend_tree_add(&(*node
)->right
, localseg
, destseg
, offset
);
107 if ((*node
)->rightcount
> (*node
)->leftcount
+ 2) {
110 n
->right
= (*node
)->left
;
111 n
->rightcount
= (*node
)->leftcount
;
113 (*node
)->leftcount
= n
->leftcount
+ n
->rightcount
+ 1;
118 void add_seglocation(segtab
* root
, int localseg
, int destseg
, int32_t offset
)
120 descend_tree_add((struct segtabnode
**)root
, localseg
, destseg
,
124 int get_seglocation(segtab
* root
, int localseg
, int *destseg
,
127 struct segtabnode
*n
= (struct segtabnode
*)*root
;
129 while (n
&& n
->localseg
!= localseg
) {
130 if (localseg
< n
->localseg
)
136 *destseg
= n
->destseg
;
143 static void freenode(struct segtabnode
*n
)
152 void done_seglocations(segtab
* root
)
159 void printnode(int i
, struct segtabnode
*n
)
163 printnode(i
+ 1, n
->left
);
164 printf("%*s%d %d %ld\n", i
, "", n
->localseg
, n
->destseg
, n
->offset
);
165 printnode(i
+ 1, n
->right
);