linemap.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * YASM assembler virtual line mapping handling (for parse stage)
  3. *
  4. * Copyright (C) 2002-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "util.h"
  28. #include "coretype.h"
  29. #include "hamt.h"
  30. #include "errwarn.h"
  31. #include "linemap.h"
  32. typedef struct line_mapping {
  33. /* monotonically increasing virtual line */
  34. unsigned long line;
  35. /* related info */
  36. /* "original" source filename */
  37. /*@null@*/ /*@dependent@*/ const char *filename;
  38. /* "original" source base line number */
  39. unsigned long file_line;
  40. /* "original" source line number increment (for following lines) */
  41. unsigned long line_inc;
  42. } line_mapping;
  43. typedef struct line_source_info {
  44. /* first bytecode on line; NULL if no bytecodes on line */
  45. /*@null@*/ /*@dependent@*/ yasm_bytecode *bc;
  46. /* source code line */
  47. /*@owned@*/ char *source;
  48. } line_source_info;
  49. struct yasm_linemap {
  50. /* Shared storage for filenames */
  51. /*@only@*/ /*@null@*/ HAMT *filenames;
  52. /* Current virtual line number. */
  53. unsigned long current;
  54. /* Mappings from virtual to physical line numbers */
  55. struct line_mapping *map_vector;
  56. unsigned long map_size;
  57. unsigned long map_allocated;
  58. /* Bytecode and source line information */
  59. /*@only@*/ line_source_info *source_info;
  60. size_t source_info_size;
  61. };
  62. static void
  63. filename_delete_one(/*@only@*/ void *d)
  64. {
  65. yasm_xfree(d);
  66. }
  67. void
  68. yasm_linemap_set(yasm_linemap *linemap, const char *filename,
  69. unsigned long virtual_line, unsigned long file_line,
  70. unsigned long line_inc)
  71. {
  72. char *copy;
  73. unsigned long i;
  74. int replace = 0;
  75. line_mapping *mapping = NULL;
  76. if (virtual_line == 0) {
  77. virtual_line = linemap->current;
  78. }
  79. /* Replace all existing mappings that have line numbers >= this one. */
  80. for (i = linemap->map_size; i > 0; i--) {
  81. if (linemap->map_vector[i-1].line < virtual_line) {
  82. if (i < linemap->map_size) {
  83. mapping = &linemap->map_vector[i];
  84. linemap->map_size = i + 1;
  85. }
  86. break;
  87. }
  88. }
  89. if (mapping == NULL) {
  90. /* Create a new mapping in the map */
  91. if (linemap->map_size >= linemap->map_allocated) {
  92. /* allocate another size bins when full for 2x space */
  93. linemap->map_vector = yasm_xrealloc(linemap->map_vector,
  94. 2*linemap->map_allocated*sizeof(line_mapping));
  95. linemap->map_allocated *= 2;
  96. }
  97. mapping = &linemap->map_vector[linemap->map_size];
  98. linemap->map_size++;
  99. }
  100. /* Fill it */
  101. if (!filename) {
  102. if (linemap->map_size >= 2)
  103. mapping->filename =
  104. linemap->map_vector[linemap->map_size-2].filename;
  105. else
  106. filename = "unknown";
  107. }
  108. if (filename) {
  109. /* Copy the filename (via shared storage) */
  110. copy = yasm__xstrdup(filename);
  111. /*@-aliasunique@*/
  112. mapping->filename = HAMT_insert(linemap->filenames, copy, copy,
  113. &replace, filename_delete_one);
  114. /*@=aliasunique@*/
  115. }
  116. mapping->line = virtual_line;
  117. mapping->file_line = file_line;
  118. mapping->line_inc = line_inc;
  119. }
  120. unsigned long
  121. yasm_linemap_poke(yasm_linemap *linemap, const char *filename,
  122. unsigned long file_line)
  123. {
  124. unsigned long line;
  125. line_mapping *mapping;
  126. linemap->current++;
  127. yasm_linemap_set(linemap, filename, 0, file_line, 0);
  128. mapping = &linemap->map_vector[linemap->map_size-1];
  129. line = linemap->current;
  130. linemap->current++;
  131. yasm_linemap_set(linemap, mapping->filename, 0,
  132. mapping->file_line +
  133. mapping->line_inc*(linemap->current-2-mapping->line),
  134. mapping->line_inc);
  135. return line;
  136. }
  137. yasm_linemap *
  138. yasm_linemap_create(void)
  139. {
  140. size_t i;
  141. yasm_linemap *linemap = yasm_xmalloc(sizeof(yasm_linemap));
  142. linemap->filenames = HAMT_create(0, yasm_internal_error_);
  143. linemap->current = 1;
  144. /* initialize mapping vector */
  145. linemap->map_vector = yasm_xmalloc(8*sizeof(line_mapping));
  146. linemap->map_size = 0;
  147. linemap->map_allocated = 8;
  148. /* initialize source line information array */
  149. linemap->source_info_size = 2;
  150. linemap->source_info = yasm_xmalloc(linemap->source_info_size *
  151. sizeof(line_source_info));
  152. for (i=0; i<linemap->source_info_size; i++) {
  153. linemap->source_info[i].bc = NULL;
  154. linemap->source_info[i].source = NULL;
  155. }
  156. return linemap;
  157. }
  158. void
  159. yasm_linemap_destroy(yasm_linemap *linemap)
  160. {
  161. size_t i;
  162. for (i=0; i<linemap->source_info_size; i++) {
  163. if (linemap->source_info[i].source)
  164. yasm_xfree(linemap->source_info[i].source);
  165. }
  166. yasm_xfree(linemap->source_info);
  167. yasm_xfree(linemap->map_vector);
  168. if (linemap->filenames)
  169. HAMT_destroy(linemap->filenames, filename_delete_one);
  170. yasm_xfree(linemap);
  171. }
  172. unsigned long
  173. yasm_linemap_get_current(yasm_linemap *linemap)
  174. {
  175. return linemap->current;
  176. }
  177. void
  178. yasm_linemap_add_source(yasm_linemap *linemap, yasm_bytecode *bc,
  179. const char *source)
  180. {
  181. size_t i;
  182. while (linemap->current > linemap->source_info_size) {
  183. /* allocate another size bins when full for 2x space */
  184. linemap->source_info = yasm_xrealloc(linemap->source_info,
  185. 2*linemap->source_info_size*sizeof(line_source_info));
  186. for (i=linemap->source_info_size; i<linemap->source_info_size*2; i++) {
  187. linemap->source_info[i].bc = NULL;
  188. linemap->source_info[i].source = NULL;
  189. }
  190. linemap->source_info_size *= 2;
  191. }
  192. /* Delete existing info for that line (if any) */
  193. if (linemap->source_info[linemap->current-1].source)
  194. yasm_xfree(linemap->source_info[linemap->current-1].source);
  195. linemap->source_info[linemap->current-1].bc = bc;
  196. linemap->source_info[linemap->current-1].source = yasm__xstrdup(source);
  197. }
  198. unsigned long
  199. yasm_linemap_goto_next(yasm_linemap *linemap)
  200. {
  201. return ++(linemap->current);
  202. }
  203. void
  204. yasm_linemap_lookup(yasm_linemap *linemap, unsigned long line,
  205. const char **filename, unsigned long *file_line)
  206. {
  207. line_mapping *mapping;
  208. unsigned long vindex, step;
  209. assert(line <= linemap->current);
  210. /* Binary search through map to find highest line_index <= index */
  211. vindex = 0;
  212. /* start step as the greatest power of 2 <= size */
  213. step = 1;
  214. while (step*2<=linemap->map_size)
  215. step*=2;
  216. while (step>0) {
  217. if (vindex+step < linemap->map_size
  218. && linemap->map_vector[vindex+step].line <= line)
  219. vindex += step;
  220. step /= 2;
  221. }
  222. mapping = &linemap->map_vector[vindex];
  223. *filename = mapping->filename;
  224. *file_line = (line ? mapping->file_line + mapping->line_inc*(line-mapping->line) : 0);
  225. }
  226. int
  227. yasm_linemap_traverse_filenames(yasm_linemap *linemap, /*@null@*/ void *d,
  228. int (*func) (const char *filename, void *d))
  229. {
  230. return HAMT_traverse(linemap->filenames, d, (int (*) (void *, void *))func);
  231. }
  232. int
  233. yasm_linemap_get_source(yasm_linemap *linemap, unsigned long line,
  234. yasm_bytecode **bcp, const char **sourcep)
  235. {
  236. if (line > linemap->source_info_size) {
  237. *bcp = NULL;
  238. *sourcep = NULL;
  239. return 1;
  240. }
  241. *bcp = linemap->source_info[line-1].bc;
  242. *sourcep = linemap->source_info[line-1].source;
  243. return (!(*sourcep));
  244. }