blob: b60d2a4a269484759ff46e245cb7d880f7b34215 [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* sync.c the Netwide Disassembler synchronisation processing module
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
7 */
8
H. Peter Anvinfe501952007-10-02 21:53:51 -07009#include "compiler.h"
10
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000011#include <stdio.h>
H. Peter Anvin6768eb72002-04-30 20:52:26 +000012#include <stdlib.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000013#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000014#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000015
H. Peter Anvin8d024e72007-09-19 21:41:02 -070016#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000017#include "sync.h"
18
H. Peter Anvin8d024e72007-09-19 21:41:02 -070019#define SYNC_MAX 4096 /* max # of sync points (initial) */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000020
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000021/*
22 * This lot manages the current set of sync points by means of a
23 * heap (priority queue) structure.
24 */
25
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000026static struct Sync {
Keith Kaniosb7a89542007-04-12 02:40:54 +000027 uint32_t pos;
28 uint32_t length;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000029} *synx;
H. Peter Anvin8d024e72007-09-19 21:41:02 -070030static int max_synx, nsynx;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000031
H. Peter Anvine2c80182005-01-15 22:15:51 +000032void init_sync(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000033{
H. Peter Anvin8d024e72007-09-19 21:41:02 -070034 max_synx = SYNC_MAX-1;
35 synx = nasm_malloc(SYNC_MAX * sizeof(*synx));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 nsynx = 0;
37}
38
Keith Kaniosb7a89542007-04-12 02:40:54 +000039void add_sync(uint32_t pos, uint32_t length)
H. Peter Anvineba20a72002-04-30 20:53:55 +000040{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000041 int i;
42
H. Peter Anvin8d024e72007-09-19 21:41:02 -070043 if (nsynx >= max_synx) {
44 max_synx = (max_synx << 1)+1;
45 synx = nasm_realloc(synx, (max_synx+1) * sizeof(*synx));
46 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047
48 nsynx++;
49 synx[nsynx].pos = pos;
50 synx[nsynx].length = length;
51
52 for (i = nsynx; i > 1; i /= 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000053 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 */
58 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059 }
60}
61
Keith Kaniosb7a89542007-04-12 02:40:54 +000062uint32_t next_sync(uint32_t position, uint32_t *length)
H. Peter Anvineba20a72002-04-30 20:53:55 +000063{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064 while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000065 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 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070
H. Peter Anvine2c80182005-01-15 22:15:51 +000071 nsynx--;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000072
73 i = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +000074 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;
89 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000090 }
91
92 if (nsynx > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000093 if (length)
94 *length = synx[1].length;
95 return synx[1].pos;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000096 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +000097 if (length)
98 *length = 0L;
H. Peter Anvinfc918882007-08-20 20:10:04 +000099 return UINT32_MAX;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100 }
101}