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