10554
|
1 /* ----------------------------------------------------------------------- *
|
|
2 *
|
|
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
|
|
4 * See the file AUTHORS included with the NASM distribution for
|
|
5 * the specific copyright holders.
|
|
6 *
|
|
7 * Redistribution and use in source and binary forms, with or without
|
|
8 * modification, are permitted provided that the following
|
|
9 * conditions are met:
|
|
10 *
|
|
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.
|
|
17 *
|
|
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.
|
|
31 *
|
|
32 * ----------------------------------------------------------------------- */
|
|
33
|
|
34 /*
|
|
35 * sync.c the Netwide Disassembler synchronisation processing module
|
|
36 */
|
|
37
|
|
38 #include "compiler.h"
|
|
39
|
|
40 #include <stdio.h>
|
|
41 #include <stdlib.h>
|
|
42 #include <limits.h>
|
|
43
|
|
44 #include "nasmlib.h"
|
|
45 #include "sync.h"
|
|
46
|
|
47 #define SYNC_MAX_SHIFT 31
|
|
48 #define SYNC_MAX_SIZE (1U << SYNC_MAX_SHIFT)
|
|
49
|
|
50 /* initial # of sync points (*must* be power of two)*/
|
|
51 #define SYNC_INITIAL_CHUNK (1U << 12)
|
|
52
|
|
53 /*
|
|
54 * This lot manages the current set of sync points by means of a
|
|
55 * heap (priority queue) structure.
|
|
56 */
|
|
57
|
|
58 static struct Sync {
|
|
59 uint64_t pos;
|
|
60 uint32_t length;
|
|
61 } *synx;
|
|
62
|
|
63 static uint32_t max_synx, nsynx;
|
|
64
|
|
65 static inline void swap_sync(uint32_t dst, uint32_t src)
|
|
66 {
|
|
67 struct Sync t = synx[dst];
|
|
68 synx[dst] = synx[src];
|
|
69 synx[src] = t;
|
|
70 }
|
|
71
|
|
72 void init_sync(void)
|
|
73 {
|
|
74 max_synx = SYNC_INITIAL_CHUNK;
|
|
75 synx = nasm_malloc((max_synx + 1) * sizeof(*synx));
|
|
76 nsynx = 0;
|
|
77 }
|
|
78
|
|
79 void add_sync(uint64_t pos, uint32_t length)
|
|
80 {
|
|
81 uint32_t i;
|
|
82
|
|
83 if (nsynx >= max_synx) {
|
|
84 if (max_synx >= SYNC_MAX_SIZE) /* too many sync points! */
|
|
85 return;
|
|
86 max_synx = (max_synx << 1);
|
|
87 synx = nasm_realloc(synx, (max_synx + 1) * sizeof(*synx));
|
|
88 }
|
|
89
|
|
90 nsynx++;
|
|
91 synx[nsynx].pos = pos;
|
|
92 synx[nsynx].length = length;
|
|
93
|
|
94 for (i = nsynx; i > 1; i /= 2) {
|
|
95 if (synx[i / 2].pos > synx[i].pos)
|
|
96 swap_sync(i / 2, i);
|
|
97 }
|
|
98 }
|
|
99
|
|
100 uint64_t next_sync(uint64_t position, uint32_t *length)
|
|
101 {
|
|
102 while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
|
|
103 uint32_t i, j;
|
|
104
|
|
105 swap_sync(nsynx, 1);
|
|
106 nsynx--;
|
|
107
|
|
108 i = 1;
|
|
109 while (i * 2 <= nsynx) {
|
|
110 j = i * 2;
|
|
111 if (synx[j].pos < synx[i].pos &&
|
|
112 (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) {
|
|
113 swap_sync(j, i);
|
|
114 i = j;
|
|
115 } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) {
|
|
116 swap_sync(j + 1, i);
|
|
117 i = j + 1;
|
|
118 } else
|
|
119 break;
|
|
120 }
|
|
121 }
|
|
122
|
|
123 if (nsynx > 0) {
|
|
124 if (length)
|
|
125 *length = synx[1].length;
|
|
126 return synx[1].pos;
|
|
127 } else {
|
|
128 if (length)
|
|
129 *length = 0L;
|
|
130 return 0;
|
|
131 }
|
|
132 }
|