H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 1 | /* 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 Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 11 | #include <limits.h> |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 12 | #include <inttypes.h> |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 13 | |
| 14 | #include "sync.h" |
| 15 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 16 | #define SYNC_MAX 4096 /* max # of sync points */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 17 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 18 | /* |
| 19 | * This lot manages the current set of sync points by means of a |
| 20 | * heap (priority queue) structure. |
| 21 | */ |
| 22 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 23 | static struct Sync { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 24 | uint32_t pos; |
| 25 | uint32_t length; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 26 | } *synx; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 27 | static int nsynx; |
| 28 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 29 | void init_sync(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 30 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 31 | /* |
| 32 | * I'd like to allocate an array of size SYNC_MAX, then write |
| 33 | * `synx--' which would allow numbering the array from one |
| 34 | * instead of zero without wasting memory. Sadly I don't trust |
| 35 | * this to work in 16-bit Large model, so it's staying the way |
| 36 | * it is. Btw, we don't care about freeing this array, since it |
| 37 | * has to last for the duration of the program and will then be |
| 38 | * auto-freed on exit. And I'm lazy ;-) |
| 39 | * |
| 40 | * Speaking of 16-bit Large model, that's also the reason I'm |
| 41 | * not declaring this array statically - by doing it |
| 42 | * dynamically I avoid problems with the total size of DGROUP |
| 43 | * in Borland C. |
| 44 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 45 | synx = malloc((SYNC_MAX + 1) * sizeof(*synx)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 46 | if (!synx) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 47 | fprintf(stderr, "ndisasm: not enough memory for sync array\n"); |
| 48 | exit(1); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 49 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 50 | nsynx = 0; |
| 51 | } |
| 52 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 53 | void add_sync(uint32_t pos, uint32_t length) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 54 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 55 | int i; |
| 56 | |
| 57 | if (nsynx == SYNC_MAX) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 58 | return; /* can't do anything - overflow */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 59 | |
| 60 | nsynx++; |
| 61 | synx[nsynx].pos = pos; |
| 62 | synx[nsynx].length = length; |
| 63 | |
| 64 | for (i = nsynx; i > 1; i /= 2) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 65 | if (synx[i / 2].pos > synx[i].pos) { |
| 66 | struct Sync t; |
| 67 | t = synx[i / 2]; /* structure copy */ |
| 68 | synx[i / 2] = synx[i]; /* structure copy again */ |
| 69 | synx[i] = t; /* another structure copy */ |
| 70 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 74 | uint32_t next_sync(uint32_t position, uint32_t *length) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 75 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 76 | while (nsynx > 0 && synx[1].pos + synx[1].length <= position) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 77 | int i, j; |
| 78 | struct Sync t; |
| 79 | t = synx[nsynx]; /* structure copy */ |
| 80 | synx[nsynx] = synx[1]; /* structure copy */ |
| 81 | synx[1] = t; /* ditto */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 82 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 83 | nsynx--; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 84 | |
| 85 | i = 1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 86 | while (i * 2 <= nsynx) { |
| 87 | j = i * 2; |
| 88 | if (synx[j].pos < synx[i].pos && |
| 89 | (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) { |
| 90 | t = synx[j]; /* structure copy */ |
| 91 | synx[j] = synx[i]; /* lots of these... */ |
| 92 | synx[i] = t; /* ...aren't there? */ |
| 93 | i = j; |
| 94 | } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) { |
| 95 | t = synx[j + 1]; /* structure copy */ |
| 96 | synx[j + 1] = synx[i]; /* structure <yawn> copy */ |
| 97 | synx[i] = t; /* structure copy <zzzz....> */ |
| 98 | i = j + 1; |
| 99 | } else |
| 100 | break; |
| 101 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (nsynx > 0) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 105 | if (length) |
| 106 | *length = synx[1].length; |
| 107 | return synx[1].pos; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 108 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 109 | if (length) |
| 110 | *length = 0L; |
H. Peter Anvin | fc91888 | 2007-08-20 20:10:04 +0000 | [diff] [blame] | 111 | return UINT32_MAX; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 112 | } |
| 113 | } |