blob: 562c59d67a0e87df9f4826625e06fb38dcc77dd2 [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
9#include <stdio.h>
H. Peter Anvin6768eb72002-04-30 20:52:26 +000010#include <stdlib.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000011#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000012#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000013
H. Peter Anvin8d024e72007-09-19 21:41:02 -070014#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000015#include "sync.h"
16
H. Peter Anvin8d024e72007-09-19 21:41:02 -070017#define SYNC_MAX 4096 /* max # of sync points (initial) */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000018
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000019/*
20 * This lot manages the current set of sync points by means of a
21 * heap (priority queue) structure.
22 */
23
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000024static struct Sync {
Keith Kaniosb7a89542007-04-12 02:40:54 +000025 uint32_t pos;
26 uint32_t length;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000027} *synx;
H. Peter Anvin8d024e72007-09-19 21:41:02 -070028static int max_synx, nsynx;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000029
H. Peter Anvine2c80182005-01-15 22:15:51 +000030void init_sync(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000031{
H. Peter Anvin8d024e72007-09-19 21:41:02 -070032 max_synx = SYNC_MAX-1;
33 synx = nasm_malloc(SYNC_MAX * sizeof(*synx));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000034 nsynx = 0;
35}
36
Keith Kaniosb7a89542007-04-12 02:40:54 +000037void add_sync(uint32_t pos, uint32_t length)
H. Peter Anvineba20a72002-04-30 20:53:55 +000038{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000039 int i;
40
H. Peter Anvin8d024e72007-09-19 21:41:02 -070041 if (nsynx >= max_synx) {
42 max_synx = (max_synx << 1)+1;
43 synx = nasm_realloc(synx, (max_synx+1) * sizeof(*synx));
44 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000045
46 nsynx++;
47 synx[nsynx].pos = pos;
48 synx[nsynx].length = length;
49
50 for (i = nsynx; i > 1; i /= 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000051 if (synx[i / 2].pos > synx[i].pos) {
52 struct Sync t;
53 t = synx[i / 2]; /* structure copy */
54 synx[i / 2] = synx[i]; /* structure copy again */
55 synx[i] = t; /* another structure copy */
56 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057 }
58}
59
Keith Kaniosb7a89542007-04-12 02:40:54 +000060uint32_t next_sync(uint32_t position, uint32_t *length)
H. Peter Anvineba20a72002-04-30 20:53:55 +000061{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062 while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000063 int i, j;
64 struct Sync t;
65 t = synx[nsynx]; /* structure copy */
66 synx[nsynx] = synx[1]; /* structure copy */
67 synx[1] = t; /* ditto */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000068
H. Peter Anvine2c80182005-01-15 22:15:51 +000069 nsynx--;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070
71 i = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +000072 while (i * 2 <= nsynx) {
73 j = i * 2;
74 if (synx[j].pos < synx[i].pos &&
75 (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) {
76 t = synx[j]; /* structure copy */
77 synx[j] = synx[i]; /* lots of these... */
78 synx[i] = t; /* ...aren't there? */
79 i = j;
80 } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) {
81 t = synx[j + 1]; /* structure copy */
82 synx[j + 1] = synx[i]; /* structure <yawn> copy */
83 synx[i] = t; /* structure copy <zzzz....> */
84 i = j + 1;
85 } else
86 break;
87 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000088 }
89
90 if (nsynx > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000091 if (length)
92 *length = synx[1].length;
93 return synx[1].pos;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000094 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +000095 if (length)
96 *length = 0L;
H. Peter Anvinfc918882007-08-20 20:10:04 +000097 return UINT32_MAX;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000098 }
99}