segtab.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2014 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * 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.
  17. *
  18. * 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. #include "rdfutils.h"
  34. #include "segtab.h"
  35. struct segtabnode {
  36. int localseg;
  37. int destseg;
  38. int32_t offset;
  39. struct segtabnode *left;
  40. struct segtabnode *right;
  41. /*
  42. * counts of how many are left or right, for use in reorganising
  43. * the tree
  44. */
  45. int leftcount;
  46. int rightcount;
  47. };
  48. /*
  49. * init_seglocations()
  50. * add_seglocation()
  51. * get_seglocation()
  52. * done_seglocation()
  53. *
  54. * functions used by write_output() to manipulate associations
  55. * between segment numbers and locations (which are built up on a per
  56. * module basis, but we only need one module at a time...)
  57. *
  58. * implementation: we build a binary tree.
  59. */
  60. void init_seglocations(segtab * root)
  61. {
  62. *root = NULL;
  63. }
  64. static void descend_tree_add(struct segtabnode **node,
  65. int localseg, int destseg, int32_t offset)
  66. {
  67. struct segtabnode *n;
  68. if (*node == NULL) {
  69. *node = nasm_malloc(sizeof(**node));
  70. if (!*node) {
  71. fprintf(stderr, "segment table: out of memory\n");
  72. exit(1);
  73. }
  74. (*node)->localseg = localseg;
  75. (*node)->offset = offset;
  76. (*node)->left = NULL;
  77. (*node)->leftcount = 0;
  78. (*node)->right = NULL;
  79. (*node)->rightcount = 0;
  80. (*node)->destseg = destseg;
  81. return;
  82. }
  83. if (localseg < (*node)->localseg) {
  84. (*node)->leftcount++;
  85. descend_tree_add(&(*node)->left, localseg, destseg, offset);
  86. if ((*node)->leftcount > (*node)->rightcount + 2) {
  87. n = *node;
  88. *node = n->left;
  89. n->left = (*node)->right;
  90. n->leftcount = (*node)->rightcount;
  91. (*node)->right = n;
  92. (*node)->rightcount = n->leftcount + n->rightcount + 1;
  93. }
  94. } else {
  95. (*node)->rightcount++;
  96. descend_tree_add(&(*node)->right, localseg, destseg, offset);
  97. if ((*node)->rightcount > (*node)->leftcount + 2) {
  98. n = *node;
  99. *node = n->right;
  100. n->right = (*node)->left;
  101. n->rightcount = (*node)->leftcount;
  102. (*node)->left = n;
  103. (*node)->leftcount = n->leftcount + n->rightcount + 1;
  104. }
  105. }
  106. }
  107. void add_seglocation(segtab * root, int localseg, int destseg, int32_t offset)
  108. {
  109. descend_tree_add((struct segtabnode **)root, localseg, destseg,
  110. offset);
  111. }
  112. int get_seglocation(segtab * root, int localseg, int *destseg,
  113. int32_t *offset)
  114. {
  115. struct segtabnode *n = (struct segtabnode *)*root;
  116. while (n && n->localseg != localseg) {
  117. if (localseg < n->localseg)
  118. n = n->left;
  119. else
  120. n = n->right;
  121. }
  122. if (n) {
  123. *destseg = n->destseg;
  124. *offset = n->offset;
  125. return 1;
  126. } else
  127. return 0;
  128. }
  129. static void freenode(struct segtabnode *n)
  130. {
  131. if (!n)
  132. return;
  133. freenode(n->left);
  134. freenode(n->right);
  135. nasm_free(n);
  136. }
  137. void done_seglocations(segtab * root)
  138. {
  139. freenode(*root);
  140. *root = NULL;
  141. }
  142. #if 0
  143. void printnode(int i, struct segtabnode *n)
  144. {
  145. if (!n)
  146. return;
  147. printnode(i + 1, n->left);
  148. printf("%*s%d %d %ld\n", i, "", n->localseg, n->destseg, n->offset);
  149. printnode(i + 1, n->right);
  150. }
  151. void printtable()
  152. {
  153. printnode(0, root);
  154. }
  155. #endif