blob: 37bd221a673f5f9526df5e8c280538942ed421cb [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>
12
13#include "sync.h"
14
H. Peter Anvine2c80182005-01-15 22:15:51 +000015#define SYNC_MAX 4096 /* max # of sync points */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000016
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000017/*
18 * This lot manages the current set of sync points by means of a
19 * heap (priority queue) structure.
20 */
21
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000022static struct Sync {
23 unsigned long pos;
24 unsigned long length;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000025} *synx;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000026static int nsynx;
27
H. Peter Anvine2c80182005-01-15 22:15:51 +000028void init_sync(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000029{
H. Peter Anvin6768eb72002-04-30 20:52:26 +000030 /*
31 * I'd like to allocate an array of size SYNC_MAX, then write
32 * `synx--' which would allow numbering the array from one
33 * instead of zero without wasting memory. Sadly I don't trust
34 * this to work in 16-bit Large model, so it's staying the way
35 * it is. Btw, we don't care about freeing this array, since it
36 * has to last for the duration of the program and will then be
37 * auto-freed on exit. And I'm lazy ;-)
38 *
39 * Speaking of 16-bit Large model, that's also the reason I'm
40 * not declaring this array statically - by doing it
41 * dynamically I avoid problems with the total size of DGROUP
42 * in Borland C.
43 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000044 synx = malloc((SYNC_MAX + 1) * sizeof(*synx));
H. Peter Anvin6768eb72002-04-30 20:52:26 +000045 if (!synx) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000046 fprintf(stderr, "ndisasm: not enough memory for sync array\n");
47 exit(1);
H. Peter Anvin6768eb72002-04-30 20:52:26 +000048 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000049 nsynx = 0;
50}
51
H. Peter Anvine2c80182005-01-15 22:15:51 +000052void add_sync(unsigned long pos, unsigned long length)
H. Peter Anvineba20a72002-04-30 20:53:55 +000053{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000054 int i;
55
56 if (nsynx == SYNC_MAX)
H. Peter Anvine2c80182005-01-15 22:15:51 +000057 return; /* can't do anything - overflow */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058
59 nsynx++;
60 synx[nsynx].pos = pos;
61 synx[nsynx].length = length;
62
63 for (i = nsynx; i > 1; i /= 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000064 if (synx[i / 2].pos > synx[i].pos) {
65 struct Sync t;
66 t = synx[i / 2]; /* structure copy */
67 synx[i / 2] = synx[i]; /* structure copy again */
68 synx[i] = t; /* another structure copy */
69 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070 }
71}
72
H. Peter Anvine2c80182005-01-15 22:15:51 +000073unsigned long next_sync(unsigned long position, unsigned long *length)
H. Peter Anvineba20a72002-04-30 20:53:55 +000074{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000075 while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000076 int i, j;
77 struct Sync t;
78 t = synx[nsynx]; /* structure copy */
79 synx[nsynx] = synx[1]; /* structure copy */
80 synx[1] = t; /* ditto */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000081
H. Peter Anvine2c80182005-01-15 22:15:51 +000082 nsynx--;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000083
84 i = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +000085 while (i * 2 <= nsynx) {
86 j = i * 2;
87 if (synx[j].pos < synx[i].pos &&
88 (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) {
89 t = synx[j]; /* structure copy */
90 synx[j] = synx[i]; /* lots of these... */
91 synx[i] = t; /* ...aren't there? */
92 i = j;
93 } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) {
94 t = synx[j + 1]; /* structure copy */
95 synx[j + 1] = synx[i]; /* structure <yawn> copy */
96 synx[i] = t; /* structure copy <zzzz....> */
97 i = j + 1;
98 } else
99 break;
100 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000101 }
102
103 if (nsynx > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000104 if (length)
105 *length = synx[1].length;
106 return synx[1].pos;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000107 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108 if (length)
109 *length = 0L;
110 return ULONG_MAX;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000111 }
112}