blob: b1a703126cd1c83f09f407eefe8369a7af80aa62 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovf064c1c2009-12-21 19:31:45 +03002 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07003 * 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.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * 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.
Cyrill Gorcunovf064c1c2009-12-21 19:31:45 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * 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
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
H. Peter Anvin6768eb72002-04-30 20:52:26 +000041#include <stdlib.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000042#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000043#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000044
H. Peter Anvin8d024e72007-09-19 21:41:02 -070045#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046#include "sync.h"
47
Cyrill Gorcunovfaaad6e2009-12-22 23:33:40 +030048#define SYNC_MAX_SHIFT 31
49#define SYNC_MAX_SIZE (1U << SYNC_MAX_SHIFT)
50
51/* initial # of sync points (*must* be power of two)*/
52#define SYNC_INITIAL_CHUNK (1U << 12)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000054/*
55 * This lot manages the current set of sync points by means of a
56 * heap (priority queue) structure.
57 */
58
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059static struct Sync {
Keith Kaniosb7a89542007-04-12 02:40:54 +000060 uint32_t pos;
61 uint32_t length;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062} *synx;
Cyrill Gorcunovf064c1c2009-12-21 19:31:45 +030063
Cyrill Gorcunovfaaad6e2009-12-22 23:33:40 +030064static uint32_t max_synx, nsynx;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000065
Cyrill Gorcunov2ec4ad82009-07-11 18:37:08 +040066static inline void swap_sync(uint32_t dst, uint32_t src)
67{
68 struct Sync t = synx[dst];
69 synx[dst] = synx[src];
70 synx[src] = t;
71}
72
H. Peter Anvine2c80182005-01-15 22:15:51 +000073void init_sync(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000074{
Cyrill Gorcunovfaaad6e2009-12-22 23:33:40 +030075 max_synx = SYNC_INITIAL_CHUNK;
76 synx = nasm_malloc((max_synx + 1) * sizeof(*synx));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000077 nsynx = 0;
78}
79
Keith Kaniosb7a89542007-04-12 02:40:54 +000080void add_sync(uint32_t pos, uint32_t length)
H. Peter Anvineba20a72002-04-30 20:53:55 +000081{
Cyrill Gorcunovfaaad6e2009-12-22 23:33:40 +030082 uint32_t i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000083
H. Peter Anvin8d024e72007-09-19 21:41:02 -070084 if (nsynx >= max_synx) {
Cyrill Gorcunovfaaad6e2009-12-22 23:33:40 +030085 if (max_synx >= SYNC_MAX_SIZE) /* too many sync points! */
86 return;
87 max_synx = (max_synx << 1);
Cyrill Gorcunovf064c1c2009-12-21 19:31:45 +030088 synx = nasm_realloc(synx, (max_synx + 1) * sizeof(*synx));
H. Peter Anvin8d024e72007-09-19 21:41:02 -070089 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000090
91 nsynx++;
92 synx[nsynx].pos = pos;
93 synx[nsynx].length = length;
94
95 for (i = nsynx; i > 1; i /= 2) {
Cyrill Gorcunov2ec4ad82009-07-11 18:37:08 +040096 if (synx[i / 2].pos > synx[i].pos)
97 swap_sync(i / 2, i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000098 }
99}
100
Keith Kaniosb7a89542007-04-12 02:40:54 +0000101uint32_t next_sync(uint32_t position, uint32_t *length)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000102{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000103 while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
Cyrill Gorcunovfaaad6e2009-12-22 23:33:40 +0300104 uint32_t i, j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105
Cyrill Gorcunov2ec4ad82009-07-11 18:37:08 +0400106 swap_sync(nsynx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107 nsynx--;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000108
109 i = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000110 while (i * 2 <= nsynx) {
111 j = i * 2;
112 if (synx[j].pos < synx[i].pos &&
113 (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) {
Cyrill Gorcunov2ec4ad82009-07-11 18:37:08 +0400114 swap_sync(j, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000115 i = j;
116 } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) {
Cyrill Gorcunov2ec4ad82009-07-11 18:37:08 +0400117 swap_sync(j + 1, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000118 i = j + 1;
119 } else
120 break;
121 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000122 }
123
124 if (nsynx > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000125 if (length)
126 *length = synx[1].length;
127 return synx[1].pos;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000129 if (length)
130 *length = 0L;
H. Peter Anvin49da4682007-11-20 13:22:58 -0800131 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000132 }
133}