NASM 2.03rc7
[nasm/avx512.git] / sync.c
blobeda062d138079151c5d3e66fd396a532a3fb4db7
1 /* sync.c the Netwide Disassembler synchronisation processing module
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
7 */
9 #include "compiler.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <limits.h>
14 #include <inttypes.h>
16 #include "nasmlib.h"
17 #include "sync.h"
19 #define SYNC_MAX 4096 /* max # of sync points (initial) */
22 * This lot manages the current set of sync points by means of a
23 * heap (priority queue) structure.
26 static struct Sync {
27 uint32_t pos;
28 uint32_t length;
29 } *synx;
30 static int max_synx, nsynx;
32 void init_sync(void)
34 max_synx = SYNC_MAX-1;
35 synx = nasm_malloc(SYNC_MAX * sizeof(*synx));
36 nsynx = 0;
39 void add_sync(uint32_t pos, uint32_t length)
41 int i;
43 if (nsynx >= max_synx) {
44 max_synx = (max_synx << 1)+1;
45 synx = nasm_realloc(synx, (max_synx+1) * sizeof(*synx));
48 nsynx++;
49 synx[nsynx].pos = pos;
50 synx[nsynx].length = length;
52 for (i = nsynx; i > 1; i /= 2) {
53 if (synx[i / 2].pos > synx[i].pos) {
54 struct Sync t;
55 t = synx[i / 2]; /* structure copy */
56 synx[i / 2] = synx[i]; /* structure copy again */
57 synx[i] = t; /* another structure copy */
62 uint32_t next_sync(uint32_t position, uint32_t *length)
64 while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
65 int i, j;
66 struct Sync t;
67 t = synx[nsynx]; /* structure copy */
68 synx[nsynx] = synx[1]; /* structure copy */
69 synx[1] = t; /* ditto */
71 nsynx--;
73 i = 1;
74 while (i * 2 <= nsynx) {
75 j = i * 2;
76 if (synx[j].pos < synx[i].pos &&
77 (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) {
78 t = synx[j]; /* structure copy */
79 synx[j] = synx[i]; /* lots of these... */
80 synx[i] = t; /* ...aren't there? */
81 i = j;
82 } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) {
83 t = synx[j + 1]; /* structure copy */
84 synx[j + 1] = synx[i]; /* structure <yawn> copy */
85 synx[i] = t; /* structure copy <zzzz....> */
86 i = j + 1;
87 } else
88 break;
92 if (nsynx > 0) {
93 if (length)
94 *length = synx[1].length;
95 return synx[1].pos;
96 } else {
97 if (length)
98 *length = 0L;
99 return 0;