]>
Commit | Line | Data |
---|---|---|
436d9e46 | 1 | /* listing.c - maintain assembly listings |
61b96bb4 | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
83f10cb2 | 3 | 2001, 2002, 2003, 2005, 2006, 2007, 2008 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | ||
5a1964ec | 6 | This file is part of GAS, the GNU Assembler. |
252b5132 | 7 | |
5a1964ec NC |
8 | GAS is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
ec2655a6 | 10 | the Free Software Foundation; either version 3, or (at your option) |
5a1964ec | 11 | any later version. |
252b5132 | 12 | |
5a1964ec NC |
13 | GAS is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
252b5132 | 17 | |
5a1964ec NC |
18 | You should have received a copy of the GNU General Public License |
19 | along with GAS; see the file COPYING. If not, write to the Free | |
4b4da160 NC |
20 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
21 | 02110-1301, USA. */ | |
252b5132 | 22 | |
5a1964ec | 23 | /* Contributed by Steve Chamberlain <[email protected]> |
252b5132 RH |
24 | |
25 | A listing page looks like: | |
26 | ||
27 | LISTING_HEADER sourcefilename pagenumber | |
28 | TITLE LINE | |
29 | SUBTITLE LINE | |
30 | linenumber address data source | |
31 | linenumber address data source | |
32 | linenumber address data source | |
33 | linenumber address data source | |
34 | ||
35 | If not overridden, the listing commands are: | |
36 | ||
37 | .title "stuff" | |
38 | Put "stuff" onto the title line | |
39 | .sbttl "stuff" | |
40 | Put stuff onto the subtitle line | |
41 | ||
42 | If these commands come within 10 lines of the top of the page, they | |
43 | will affect the page they are on, as well as any subsequent page | |
44 | ||
45 | .eject | |
46 | Thow a page | |
47 | .list | |
48 | Increment the enable listing counter | |
49 | .nolist | |
50 | Decrement the enable listing counter | |
51 | ||
52 | .psize Y[,X] | |
53 | Set the paper size to X wide and Y high. Setting a psize Y of | |
54 | zero will suppress form feeds except where demanded by .eject | |
55 | ||
56 | If the counter goes below zero, listing is suppressed. | |
57 | ||
252b5132 RH |
58 | Listings are a maintained by read calling various listing_<foo> |
59 | functions. What happens most is that the macro NO_LISTING is not | |
60 | defined (from the Makefile), then the macro LISTING_NEWLINE expands | |
61 | into a call to listing_newline. The call is done from read.c, every | |
62 | time it sees a newline, and -l is on the command line. | |
63 | ||
64 | The function listing_newline remembers the frag associated with the | |
65 | newline, and creates a new frag - note that this is wasteful, but not | |
66 | a big deal, since listing slows things down a lot anyway. The | |
47eebc20 | 67 | function also remembers when the filename changes. |
252b5132 RH |
68 | |
69 | When all the input has finished, and gas has had a chance to settle | |
70 | down, the listing is output. This is done by running down the list of | |
71 | frag/source file records, and opening the files as needed and printing | |
72 | out the bytes and chars associated with them. | |
73 | ||
74 | The only things which the architecture can change about the listing | |
75 | are defined in these macros: | |
76 | ||
77 | LISTING_HEADER The name of the architecture | |
78 | LISTING_WORD_SIZE The make of the number of bytes in a word, this determines | |
79 | the clumping of the output data. eg a value of | |
80 | 2 makes words look like 1234 5678, whilst 1 | |
81 | would make the same value look like 12 34 56 | |
82 | 78 | |
83 | LISTING_LHS_WIDTH Number of words of above size for the lhs | |
84 | ||
85 | LISTING_LHS_WIDTH_SECOND Number of words for the data on the lhs | |
86 | for the second line | |
87 | ||
47eebc20 | 88 | LISTING_LHS_CONT_LINES Max number of lines to use up for a continuation |
252b5132 | 89 | LISTING_RHS_WIDTH Number of chars from the input file to print |
5a1964ec | 90 | on a line. */ |
252b5132 | 91 | |
252b5132 | 92 | #include "as.h" |
29589b0c | 93 | #include "obstack.h" |
3882b010 | 94 | #include "safe-ctype.h" |
252b5132 RH |
95 | #include "input-file.h" |
96 | #include "subsegs.h" | |
83f10cb2 NC |
97 | #include "bfdver.h" |
98 | #include <time.h> | |
752d5da4 | 99 | #include <stdarg.h> |
252b5132 RH |
100 | |
101 | #ifndef NO_LISTING | |
102 | ||
103 | #ifndef LISTING_HEADER | |
104 | #define LISTING_HEADER "GAS LISTING" | |
105 | #endif | |
106 | #ifndef LISTING_WORD_SIZE | |
107 | #define LISTING_WORD_SIZE 4 | |
108 | #endif | |
109 | #ifndef LISTING_LHS_WIDTH | |
224de7a5 | 110 | #define LISTING_LHS_WIDTH ((LISTING_WORD_SIZE) > 4 ? 1 : 4 / (LISTING_WORD_SIZE)) |
252b5132 RH |
111 | #endif |
112 | #ifndef LISTING_LHS_WIDTH_SECOND | |
224de7a5 | 113 | #define LISTING_LHS_WIDTH_SECOND LISTING_LHS_WIDTH |
252b5132 RH |
114 | #endif |
115 | #ifndef LISTING_RHS_WIDTH | |
116 | #define LISTING_RHS_WIDTH 100 | |
117 | #endif | |
118 | #ifndef LISTING_LHS_CONT_LINES | |
119 | #define LISTING_LHS_CONT_LINES 4 | |
120 | #endif | |
83f10cb2 | 121 | #define MAX_DATELEN 30 |
252b5132 | 122 | |
cf39a089 | 123 | /* This structure remembers which .s were used. */ |
5a1964ec NC |
124 | typedef struct file_info_struct |
125 | { | |
252b5132 RH |
126 | struct file_info_struct * next; |
127 | char * filename; | |
128 | long pos; | |
129 | unsigned int linenum; | |
130 | int at_end; | |
ef99799a | 131 | } file_info_type; |
252b5132 | 132 | |
47eebc20 | 133 | /* This structure remembers which line from which file goes into which |
cf39a089 | 134 | frag. */ |
5a1964ec NC |
135 | struct list_info_struct |
136 | { | |
cf39a089 KH |
137 | /* Frag which this line of source is nearest to. */ |
138 | fragS *frag; | |
252b5132 | 139 | |
cf39a089 | 140 | /* The actual line in the source file. */ |
252b5132 | 141 | unsigned int line; |
5a1964ec | 142 | |
252b5132 | 143 | /* Pointer to the file info struct for the file which this line |
cf39a089 KH |
144 | belongs to. */ |
145 | file_info_type *file; | |
252b5132 RH |
146 | |
147 | /* The expanded text of any macro that may have been executing. */ | |
cf39a089 | 148 | char *line_contents; |
252b5132 | 149 | |
cf39a089 KH |
150 | /* Next in list. */ |
151 | struct list_info_struct *next; | |
252b5132 RH |
152 | |
153 | /* Pointer to the file info struct for the high level language | |
cf39a089 KH |
154 | source line that belongs here. */ |
155 | file_info_type *hll_file; | |
5a1964ec | 156 | |
cf39a089 | 157 | /* High level language source line. */ |
252b5132 RH |
158 | unsigned int hll_line; |
159 | ||
cf39a089 KH |
160 | /* Pointer to any error message associated with this line. */ |
161 | char *message; | |
252b5132 | 162 | |
5a1964ec NC |
163 | enum |
164 | { | |
165 | EDICT_NONE, | |
166 | EDICT_SBTTL, | |
167 | EDICT_TITLE, | |
168 | EDICT_NOLIST, | |
169 | EDICT_LIST, | |
170 | EDICT_NOLIST_NEXT, | |
171 | EDICT_EJECT | |
172 | } edict; | |
cf39a089 | 173 | char *edict_arg; |
252b5132 RH |
174 | |
175 | /* Nonzero if this line is to be omitted because it contains | |
176 | debugging information. This can become a flags field if we come | |
177 | up with more information to store here. */ | |
178 | int debugging; | |
179 | }; | |
180 | ||
181 | typedef struct list_info_struct list_info_type; | |
182 | ||
252b5132 RH |
183 | int listing_lhs_width = LISTING_LHS_WIDTH; |
184 | int listing_lhs_width_second = LISTING_LHS_WIDTH_SECOND; | |
185 | int listing_lhs_cont_lines = LISTING_LHS_CONT_LINES; | |
186 | int listing_rhs_width = LISTING_RHS_WIDTH; | |
187 | ||
188 | struct list_info_struct * listing_tail; | |
189 | ||
190 | static file_info_type * file_info_head; | |
191 | static file_info_type * last_open_file_info; | |
192 | static FILE * last_open_file; | |
193 | static struct list_info_struct * head; | |
194 | static int paper_width = 200; | |
195 | static int paper_height = 60; | |
196 | ||
197 | extern int listing; | |
198 | ||
199 | /* File to output listings to. */ | |
ef99799a | 200 | static FILE *list_file; |
252b5132 RH |
201 | |
202 | /* This static array is used to keep the text of data to be printed | |
203 | before the start of the line. */ | |
204 | ||
205 | #define MAX_BYTES \ | |
206 | (((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width \ | |
207 | + ((((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second) \ | |
208 | * listing_lhs_cont_lines) \ | |
209 | + 20) | |
210 | ||
cf39a089 | 211 | static char *data_buffer; |
252b5132 RH |
212 | |
213 | /* Prototypes. */ | |
5a1964ec NC |
214 | static void listing_message (const char *, const char *); |
215 | static file_info_type *file_info (const char *); | |
254d758c | 216 | static void new_frag (void); |
5a1964ec NC |
217 | static void listing_page (list_info_type *); |
218 | static unsigned int calc_hex (list_info_type *); | |
219 | static void print_lines (list_info_type *, unsigned int, char *, unsigned int); | |
254d758c | 220 | static void list_symbol_table (void); |
254d758c | 221 | static int debugging_pseudo (list_info_type *, const char *); |
5a1964ec | 222 | static void listing_listing (char *); |
252b5132 | 223 | |
252b5132 | 224 | static void |
254d758c | 225 | listing_message (const char *name, const char *message) |
252b5132 | 226 | { |
252b5132 RH |
227 | if (listing_tail != (list_info_type *) NULL) |
228 | { | |
a735d1cd L |
229 | unsigned int l = strlen (name) + strlen (message) + 1; |
230 | char *n = (char *) xmalloc (l); | |
231 | strcpy (n, name); | |
232 | strcat (n, message); | |
252b5132 RH |
233 | listing_tail->message = n; |
234 | } | |
235 | } | |
236 | ||
237 | void | |
254d758c | 238 | listing_warning (const char *message) |
252b5132 RH |
239 | { |
240 | listing_message (_("Warning:"), message); | |
241 | } | |
242 | ||
243 | void | |
254d758c | 244 | listing_error (const char *message) |
252b5132 RH |
245 | { |
246 | listing_message (_("Error:"), message); | |
247 | } | |
248 | ||
249 | static file_info_type * | |
254d758c | 250 | file_info (const char *file_name) |
252b5132 | 251 | { |
cf39a089 | 252 | /* Find an entry with this file name. */ |
252b5132 RH |
253 | file_info_type *p = file_info_head; |
254 | ||
255 | while (p != (file_info_type *) NULL) | |
256 | { | |
257 | if (strcmp (p->filename, file_name) == 0) | |
258 | return p; | |
259 | p = p->next; | |
260 | } | |
261 | ||
cf39a089 | 262 | /* Make new entry. */ |
5a1964ec | 263 | p = xmalloc (sizeof (file_info_type)); |
252b5132 RH |
264 | p->next = file_info_head; |
265 | file_info_head = p; | |
a2c36061 | 266 | p->filename = xstrdup (file_name); |
252b5132 RH |
267 | p->pos = 0; |
268 | p->linenum = 0; | |
269 | p->at_end = 0; | |
270 | ||
271 | return p; | |
272 | } | |
273 | ||
252b5132 | 274 | static void |
254d758c | 275 | new_frag (void) |
252b5132 | 276 | { |
252b5132 RH |
277 | frag_wane (frag_now); |
278 | frag_new (0); | |
252b5132 RH |
279 | } |
280 | ||
281 | void | |
254d758c | 282 | listing_newline (char *ps) |
252b5132 RH |
283 | { |
284 | char *file; | |
285 | unsigned int line; | |
286 | static unsigned int last_line = 0xffff; | |
287 | static char *last_file = NULL; | |
288 | list_info_type *new = NULL; | |
289 | ||
290 | if (listing == 0) | |
291 | return; | |
292 | ||
293 | if (now_seg == absolute_section) | |
294 | return; | |
295 | ||
296 | #ifdef OBJ_ELF | |
297 | /* In ELF, anything in a section beginning with .debug or .line is | |
298 | considered to be debugging information. This includes the | |
299 | statement which switches us into the debugging section, which we | |
300 | can only set after we are already in the debugging section. */ | |
301 | if ((listing & LISTING_NODEBUG) != 0 | |
302 | && listing_tail != NULL | |
303 | && ! listing_tail->debugging) | |
304 | { | |
305 | const char *segname; | |
306 | ||
307 | segname = segment_name (now_seg); | |
308 | if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0 | |
309 | || strncmp (segname, ".line", sizeof ".line" - 1) == 0) | |
310 | listing_tail->debugging = 1; | |
311 | } | |
312 | #endif | |
313 | ||
314 | as_where (&file, &line); | |
315 | if (ps == NULL) | |
316 | { | |
cf39a089 KH |
317 | if (line == last_line |
318 | && !(last_file && file && strcmp (file, last_file))) | |
252b5132 RH |
319 | return; |
320 | ||
321 | new = (list_info_type *) xmalloc (sizeof (list_info_type)); | |
322 | ||
323 | /* Detect if we are reading from stdin by examining the file | |
324 | name returned by as_where(). | |
325 | ||
326 | [FIXME: We rely upon the name in the strcmp below being the | |
327 | same as the one used by input_scrub_new_file(), if that is | |
328 | not true, then this code will fail]. | |
329 | ||
cf39a089 KH |
330 | If we are reading from stdin, then we need to save each input |
331 | line here (assuming of course that we actually have a line of | |
332 | input to read), so that it can be displayed in the listing | |
333 | that is produced at the end of the assembly. */ | |
252b5132 RH |
334 | if (strcmp (file, _("{standard input}")) == 0 |
335 | && input_line_pointer != NULL) | |
336 | { | |
cf39a089 | 337 | char *copy; |
252b5132 RH |
338 | int len; |
339 | int seen_quote = 0; | |
d67ffd56 | 340 | int seen_slash = 0; |
252b5132 RH |
341 | |
342 | for (copy = input_line_pointer - 1; | |
cf39a089 | 343 | *copy && (seen_quote |
d67ffd56 | 344 | || is_end_of_line [(unsigned char) *copy] != 1); |
cf39a089 | 345 | copy++) |
d67ffd56 L |
346 | { |
347 | if (*copy == '\\') | |
348 | seen_slash = ! seen_slash; | |
349 | else if (*copy == '"' && seen_slash) | |
350 | seen_quote = ! seen_quote; | |
351 | } | |
252b5132 RH |
352 | |
353 | len = (copy - input_line_pointer) + 2; | |
354 | ||
355 | copy = xmalloc (len); | |
356 | ||
357 | if (copy != NULL) | |
358 | { | |
cf39a089 KH |
359 | char *src = input_line_pointer - 1; |
360 | char *dest = copy; | |
361 | ||
252b5132 RH |
362 | while (--len) |
363 | { | |
cf39a089 | 364 | unsigned char c = *src++; |
252b5132 RH |
365 | |
366 | /* Omit control characters in the listing. */ | |
3882b010 | 367 | if (!ISCNTRL (c)) |
cf39a089 | 368 | *dest++ = c; |
252b5132 | 369 | } |
cf39a089 | 370 | |
252b5132 RH |
371 | *dest = 0; |
372 | } | |
cf39a089 | 373 | |
252b5132 RH |
374 | new->line_contents = copy; |
375 | } | |
376 | else | |
377 | new->line_contents = NULL; | |
378 | } | |
379 | else | |
380 | { | |
5a1964ec | 381 | new = xmalloc (sizeof (list_info_type)); |
252b5132 RH |
382 | new->line_contents = ps; |
383 | } | |
384 | ||
385 | last_line = line; | |
386 | last_file = file; | |
cf39a089 | 387 | |
252b5132 RH |
388 | new_frag (); |
389 | ||
390 | if (listing_tail) | |
391 | listing_tail->next = new; | |
392 | else | |
393 | head = new; | |
cf39a089 | 394 | |
252b5132 RH |
395 | listing_tail = new; |
396 | ||
397 | new->frag = frag_now; | |
398 | new->line = line; | |
399 | new->file = file_info (file); | |
400 | new->next = (list_info_type *) NULL; | |
401 | new->message = (char *) NULL; | |
402 | new->edict = EDICT_NONE; | |
403 | new->hll_file = (file_info_type *) NULL; | |
404 | new->hll_line = 0; | |
405 | new->debugging = 0; | |
cf39a089 | 406 | |
252b5132 RH |
407 | new_frag (); |
408 | ||
409 | #ifdef OBJ_ELF | |
410 | /* In ELF, anything in a section beginning with .debug or .line is | |
411 | considered to be debugging information. */ | |
412 | if ((listing & LISTING_NODEBUG) != 0) | |
413 | { | |
414 | const char *segname; | |
415 | ||
416 | segname = segment_name (now_seg); | |
417 | if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0 | |
418 | || strncmp (segname, ".line", sizeof ".line" - 1) == 0) | |
419 | new->debugging = 1; | |
420 | } | |
421 | #endif | |
422 | } | |
423 | ||
424 | /* Attach all current frags to the previous line instead of the | |
425 | current line. This is called by the MIPS backend when it discovers | |
426 | that it needs to add some NOP instructions; the added NOP | |
427 | instructions should go with the instruction that has the delay, not | |
428 | with the new instruction. */ | |
429 | ||
430 | void | |
254d758c | 431 | listing_prev_line (void) |
252b5132 RH |
432 | { |
433 | list_info_type *l; | |
434 | fragS *f; | |
435 | ||
436 | if (head == (list_info_type *) NULL | |
437 | || head == listing_tail) | |
438 | return; | |
439 | ||
440 | new_frag (); | |
441 | ||
442 | for (l = head; l->next != listing_tail; l = l->next) | |
443 | ; | |
444 | ||
445 | for (f = frchain_now->frch_root; f != (fragS *) NULL; f = f->fr_next) | |
446 | if (f->line == listing_tail) | |
447 | f->line = l; | |
448 | ||
449 | listing_tail->frag = frag_now; | |
450 | new_frag (); | |
451 | } | |
452 | ||
cf39a089 KH |
453 | /* This function returns the next source line from the file supplied, |
454 | truncated to size. It appends a fake line to the end of each input | |
752d5da4 | 455 | file to make using the returned buffer simpler. */ |
252b5132 RH |
456 | |
457 | static char * | |
254d758c | 458 | buffer_line (file_info_type *file, char *line, unsigned int size) |
252b5132 RH |
459 | { |
460 | unsigned int count = 0; | |
461 | int c; | |
252b5132 RH |
462 | char *p = line; |
463 | ||
cf39a089 | 464 | /* If we couldn't open the file, return an empty line. */ |
252b5132 RH |
465 | if (file->at_end) |
466 | return ""; | |
467 | ||
468 | /* Check the cache and see if we last used this file. */ | |
469 | if (!last_open_file_info || file != last_open_file_info) | |
470 | { | |
471 | if (last_open_file) | |
472 | { | |
473 | last_open_file_info->pos = ftell (last_open_file); | |
474 | fclose (last_open_file); | |
475 | } | |
476 | ||
7e66d8ac KH |
477 | /* Open the file in the binary mode so that ftell above can |
478 | return a reliable value that we can feed to fseek below. */ | |
252b5132 | 479 | last_open_file_info = file; |
7e66d8ac | 480 | last_open_file = fopen (file->filename, FOPEN_RB); |
252b5132 RH |
481 | if (last_open_file == NULL) |
482 | { | |
483 | file->at_end = 1; | |
484 | return ""; | |
485 | } | |
cf39a089 | 486 | |
252b5132 RH |
487 | /* Seek to where we were last time this file was open. */ |
488 | if (file->pos) | |
cf39a089 | 489 | fseek (last_open_file, file->pos, SEEK_SET); |
252b5132 RH |
490 | } |
491 | ||
cf39a089 KH |
492 | /* Leave room for null. */ |
493 | size -= 1; | |
252b5132 | 494 | |
752d5da4 NC |
495 | c = fgetc (last_open_file); |
496 | ||
7e66d8ac | 497 | while (c != EOF && c != '\n' && c != '\r') |
252b5132 RH |
498 | { |
499 | if (count < size) | |
500 | *p++ = c; | |
501 | count++; | |
502 | ||
503 | c = fgetc (last_open_file); | |
252b5132 | 504 | } |
7e66d8ac KH |
505 | |
506 | /* If '\r' is followed by '\n', swallow that. Likewise, if '\n' | |
507 | is followed by '\r', swallow that as well. */ | |
508 | if (c == '\r' || c == '\n') | |
509 | { | |
510 | int next = fgetc (last_open_file); | |
752d5da4 | 511 | |
7e66d8ac KH |
512 | if ((c == '\r' && next != '\n') |
513 | || (c == '\n' && next != '\r')) | |
514 | ungetc (next, last_open_file); | |
515 | } | |
516 | ||
252b5132 RH |
517 | if (c == EOF) |
518 | { | |
519 | file->at_end = 1; | |
97735a42 AM |
520 | if (count + 2 < size) |
521 | { | |
522 | *p++ = '.'; | |
523 | *p++ = '.'; | |
524 | *p++ = '.'; | |
525 | } | |
252b5132 RH |
526 | } |
527 | file->linenum++; | |
528 | *p++ = 0; | |
529 | return line; | |
530 | } | |
531 | ||
752d5da4 NC |
532 | |
533 | /* This function rewinds the requested file back to the line requested, | |
534 | reads it in again into the buffer provided and then restores the file | |
535 | back to its original location. */ | |
536 | ||
537 | static char * | |
538 | rebuffer_line (file_info_type * file, | |
539 | unsigned int linenum, | |
540 | char * buffer, | |
541 | unsigned int size) | |
542 | { | |
543 | unsigned int count = 0; | |
544 | unsigned int current_line = 1; | |
545 | char * p = buffer; | |
546 | long pos; | |
547 | int c; | |
548 | ||
549 | /* Sanity checks. */ | |
550 | if (file == NULL || buffer == NULL || size == 0 || file->linenum <= linenum) | |
551 | return ""; | |
552 | ||
553 | /* Check the cache and see if we last used this file. */ | |
554 | if (last_open_file_info == NULL || file != last_open_file_info) | |
555 | { | |
556 | if (last_open_file) | |
557 | { | |
558 | last_open_file_info->pos = ftell (last_open_file); | |
559 | fclose (last_open_file); | |
560 | } | |
561 | ||
562 | /* Open the file in the binary mode so that ftell above can | |
563 | return a reliable value that we can feed to fseek below. */ | |
564 | last_open_file_info = file; | |
565 | last_open_file = fopen (file->filename, FOPEN_RB); | |
566 | if (last_open_file == NULL) | |
567 | { | |
568 | file->at_end = 1; | |
569 | return ""; | |
570 | } | |
571 | ||
572 | /* Seek to where we were last time this file was open. */ | |
573 | if (file->pos) | |
574 | fseek (last_open_file, file->pos, SEEK_SET); | |
575 | } | |
576 | ||
577 | /* Remember where we are in the current file. */ | |
578 | pos = ftell (last_open_file); | |
579 | ||
580 | /* Go back to the beginning. */ | |
581 | fseek (last_open_file, 0, SEEK_SET); | |
582 | ||
583 | /* Skip lines prior to the one we are interested in. */ | |
584 | while (current_line < linenum) | |
585 | { | |
586 | /* fgets only stops on newlines and has a size limit, | |
587 | so we read one character at a time instead. */ | |
588 | do | |
589 | { | |
590 | c = fgetc (last_open_file); | |
591 | } | |
592 | while (c != EOF && c != '\n' && c != '\r'); | |
593 | ||
594 | ++ current_line; | |
595 | ||
596 | if (c == '\r' || c == '\n') | |
597 | { | |
598 | int next = fgetc (last_open_file); | |
599 | ||
600 | /* If '\r' is followed by '\n', swallow that. Likewise, if '\n' | |
601 | is followed by '\r', swallow that as well. */ | |
602 | if ((c == '\r' && next != '\n') | |
603 | || (c == '\n' && next != '\r')) | |
604 | ungetc (next, last_open_file); | |
605 | } | |
606 | } | |
607 | ||
608 | /* Leave room for the nul at the end of the buffer. */ | |
609 | size -= 1; | |
610 | ||
611 | /* Read in the line. */ | |
612 | c = fgetc (last_open_file); | |
613 | ||
614 | while (c != EOF && c != '\n' && c != '\r') | |
615 | { | |
616 | if (count < size) | |
617 | *p++ = c; | |
618 | count++; | |
619 | ||
620 | c = fgetc (last_open_file); | |
621 | } | |
622 | ||
623 | /* If '\r' is followed by '\n', swallow that. Likewise, if '\n' | |
624 | is followed by '\r', swallow that as well. */ | |
625 | if (c == '\r' || c == '\n') | |
626 | { | |
627 | int next = fgetc (last_open_file); | |
628 | ||
629 | if ((c == '\r' && next != '\n') | |
630 | || (c == '\n' && next != '\r')) | |
631 | ungetc (next, last_open_file); | |
632 | } | |
633 | ||
634 | /* Terminate the line. */ | |
635 | *p++ = 0; | |
636 | ||
637 | /* Reset the file position. */ | |
638 | fseek (last_open_file, pos, SEEK_SET); | |
639 | ||
640 | return buffer; | |
641 | } | |
642 | ||
252b5132 RH |
643 | static const char *fn; |
644 | ||
645 | static unsigned int eject; /* Eject pending */ | |
646 | static unsigned int page; /* Current page number */ | |
cf39a089 KH |
647 | static char *title; /* Current title */ |
648 | static char *subtitle; /* Current subtitle */ | |
649 | static unsigned int on_page; /* Number of lines printed on current page */ | |
252b5132 RH |
650 | |
651 | static void | |
254d758c | 652 | listing_page (list_info_type *list) |
252b5132 RH |
653 | { |
654 | /* Grope around, see if we can see a title or subtitle edict coming up | |
cf39a089 KH |
655 | soon. (we look down 10 lines of the page and see if it's there) */ |
656 | if ((eject || (on_page >= (unsigned int) paper_height)) | |
657 | && paper_height != 0) | |
252b5132 RH |
658 | { |
659 | unsigned int c = 10; | |
660 | int had_title = 0; | |
661 | int had_subtitle = 0; | |
662 | ||
663 | page++; | |
664 | ||
665 | while (c != 0 && list) | |
666 | { | |
667 | if (list->edict == EDICT_SBTTL && !had_subtitle) | |
668 | { | |
669 | had_subtitle = 1; | |
670 | subtitle = list->edict_arg; | |
671 | } | |
672 | if (list->edict == EDICT_TITLE && !had_title) | |
673 | { | |
674 | had_title = 1; | |
675 | title = list->edict_arg; | |
676 | } | |
677 | list = list->next; | |
678 | c--; | |
679 | } | |
680 | ||
252b5132 RH |
681 | if (page > 1) |
682 | { | |
683 | fprintf (list_file, "\f"); | |
684 | } | |
685 | ||
686 | fprintf (list_file, "%s %s \t\t\tpage %d\n", LISTING_HEADER, fn, page); | |
687 | fprintf (list_file, "%s\n", title); | |
688 | fprintf (list_file, "%s\n", subtitle); | |
689 | on_page = 3; | |
690 | eject = 0; | |
691 | } | |
692 | } | |
693 | ||
752d5da4 NC |
694 | /* Print a line into the list_file. Update the line count |
695 | and if necessary start a new page. */ | |
696 | ||
697 | static void | |
698 | emit_line (list_info_type * list, const char * format, ...) | |
699 | { | |
700 | va_list args; | |
701 | ||
702 | va_start (args, format); | |
703 | ||
704 | vfprintf (list_file, format, args); | |
705 | on_page++; | |
706 | listing_page (list); | |
707 | ||
708 | va_end (args); | |
709 | } | |
710 | ||
252b5132 | 711 | static unsigned int |
254d758c | 712 | calc_hex (list_info_type *list) |
252b5132 RH |
713 | { |
714 | int data_buffer_size; | |
715 | list_info_type *first = list; | |
cf39a089 | 716 | unsigned int address = ~(unsigned int) 0; |
252b5132 RH |
717 | fragS *frag; |
718 | fragS *frag_ptr; | |
bea9907b | 719 | unsigned int octet_in_frag; |
252b5132 | 720 | |
cf39a089 | 721 | /* Find first frag which says it belongs to this line. */ |
252b5132 RH |
722 | frag = list->frag; |
723 | while (frag && frag->line != list) | |
724 | frag = frag->fr_next; | |
725 | ||
726 | frag_ptr = frag; | |
727 | ||
728 | data_buffer_size = 0; | |
729 | ||
cf39a089 | 730 | /* Dump all the frags which belong to this line. */ |
252b5132 RH |
731 | while (frag_ptr != (fragS *) NULL && frag_ptr->line == first) |
732 | { | |
cf39a089 | 733 | /* Print as many bytes from the fixed part as is sensible. */ |
bea9907b TW |
734 | octet_in_frag = 0; |
735 | while ((offsetT) octet_in_frag < frag_ptr->fr_fix | |
252b5132 RH |
736 | && data_buffer_size < MAX_BYTES - 3) |
737 | { | |
cf39a089 | 738 | if (address == ~(unsigned int) 0) |
5a1964ec | 739 | address = frag_ptr->fr_address / OCTETS_PER_BYTE; |
252b5132 RH |
740 | |
741 | sprintf (data_buffer + data_buffer_size, | |
742 | "%02X", | |
bea9907b | 743 | (frag_ptr->fr_literal[octet_in_frag]) & 0xff); |
252b5132 | 744 | data_buffer_size += 2; |
bea9907b | 745 | octet_in_frag++; |
252b5132 | 746 | } |
411863a4 KH |
747 | if (frag_ptr->fr_type == rs_fill) |
748 | { | |
749 | unsigned int var_rep_max = octet_in_frag; | |
750 | unsigned int var_rep_idx = octet_in_frag; | |
751 | ||
752 | /* Print as many bytes from the variable part as is sensible. */ | |
753 | while (((offsetT) octet_in_frag | |
754 | < (frag_ptr->fr_fix + frag_ptr->fr_var * frag_ptr->fr_offset)) | |
755 | && data_buffer_size < MAX_BYTES - 3) | |
756 | { | |
757 | if (address == ~(unsigned int) 0) | |
5a1964ec NC |
758 | address = frag_ptr->fr_address / OCTETS_PER_BYTE; |
759 | ||
411863a4 KH |
760 | sprintf (data_buffer + data_buffer_size, |
761 | "%02X", | |
762 | (frag_ptr->fr_literal[var_rep_idx]) & 0xff); | |
411863a4 | 763 | data_buffer_size += 2; |
252b5132 | 764 | |
411863a4 KH |
765 | var_rep_idx++; |
766 | octet_in_frag++; | |
252b5132 | 767 | |
411863a4 KH |
768 | if ((offsetT) var_rep_idx >= frag_ptr->fr_fix + frag_ptr->fr_var) |
769 | var_rep_idx = var_rep_max; | |
770 | } | |
771 | } | |
252b5132 RH |
772 | |
773 | frag_ptr = frag_ptr->fr_next; | |
774 | } | |
775 | data_buffer[data_buffer_size] = '\0'; | |
776 | return address; | |
777 | } | |
778 | ||
252b5132 | 779 | static void |
254d758c KH |
780 | print_lines (list_info_type *list, unsigned int lineno, |
781 | char *string, unsigned int address) | |
252b5132 RH |
782 | { |
783 | unsigned int idx; | |
784 | unsigned int nchars; | |
785 | unsigned int lines; | |
bea9907b | 786 | unsigned int octet_in_word = 0; |
252b5132 | 787 | char *src = data_buffer; |
bea9907b | 788 | int cur; |
252b5132 | 789 | |
cf39a089 | 790 | /* Print the stuff on the first line. */ |
252b5132 RH |
791 | listing_page (list); |
792 | nchars = (LISTING_WORD_SIZE * 2 + 1) * listing_lhs_width; | |
cf39a089 KH |
793 | |
794 | /* Print the hex for the first line. */ | |
795 | if (address == ~(unsigned int) 0) | |
252b5132 RH |
796 | { |
797 | fprintf (list_file, "% 4d ", lineno); | |
798 | for (idx = 0; idx < nchars; idx++) | |
799 | fprintf (list_file, " "); | |
800 | ||
752d5da4 | 801 | emit_line (NULL, "\t%s\n", string ? string : ""); |
252b5132 RH |
802 | return; |
803 | } | |
804 | ||
805 | if (had_errors ()) | |
806 | fprintf (list_file, "% 4d ???? ", lineno); | |
807 | else | |
808 | fprintf (list_file, "% 4d %04x ", lineno, address); | |
809 | ||
cf39a089 | 810 | /* And the data to go along with it. */ |
252b5132 | 811 | idx = 0; |
bea9907b TW |
812 | cur = 0; |
813 | while (src[cur] && idx < nchars) | |
252b5132 | 814 | { |
bea9907b | 815 | int offset; |
bea9907b | 816 | offset = cur; |
cf39a089 | 817 | fprintf (list_file, "%c%c", src[offset], src[offset + 1]); |
bea9907b TW |
818 | cur += 2; |
819 | octet_in_word++; | |
cf39a089 | 820 | |
bea9907b | 821 | if (octet_in_word == LISTING_WORD_SIZE) |
252b5132 RH |
822 | { |
823 | fprintf (list_file, " "); | |
824 | idx++; | |
bea9907b | 825 | octet_in_word = 0; |
252b5132 | 826 | } |
cf39a089 | 827 | |
252b5132 RH |
828 | idx += 2; |
829 | } | |
cf39a089 | 830 | |
252b5132 RH |
831 | for (; idx < nchars; idx++) |
832 | fprintf (list_file, " "); | |
cf39a089 | 833 | |
752d5da4 | 834 | emit_line (list, "\t%s\n", string ? string : ""); |
cf39a089 | 835 | |
252b5132 | 836 | if (list->message) |
752d5da4 | 837 | emit_line (list, "**** %s\n", list->message); |
cf39a089 | 838 | |
252b5132 RH |
839 | for (lines = 0; |
840 | lines < (unsigned int) listing_lhs_cont_lines | |
bea9907b | 841 | && src[cur]; |
cf39a089 | 842 | lines++) |
252b5132 | 843 | { |
cf39a089 | 844 | nchars = ((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second - 1; |
252b5132 | 845 | idx = 0; |
cf39a089 KH |
846 | |
847 | /* Print any more lines of data, but more compactly. */ | |
252b5132 | 848 | fprintf (list_file, "% 4d ", lineno); |
cf39a089 | 849 | |
bea9907b | 850 | while (src[cur] && idx < nchars) |
252b5132 | 851 | { |
cf39a089 KH |
852 | int offset; |
853 | offset = cur; | |
854 | fprintf (list_file, "%c%c", src[offset], src[offset + 1]); | |
bea9907b | 855 | cur += 2; |
252b5132 | 856 | idx += 2; |
bea9907b | 857 | octet_in_word++; |
cf39a089 | 858 | |
bea9907b | 859 | if (octet_in_word == LISTING_WORD_SIZE) |
252b5132 RH |
860 | { |
861 | fprintf (list_file, " "); | |
862 | idx++; | |
bea9907b | 863 | octet_in_word = 0; |
252b5132 RH |
864 | } |
865 | } | |
cf39a089 | 866 | |
752d5da4 | 867 | emit_line (list, "\n"); |
252b5132 RH |
868 | } |
869 | } | |
870 | ||
252b5132 | 871 | static void |
254d758c | 872 | list_symbol_table (void) |
252b5132 RH |
873 | { |
874 | extern symbolS *symbol_rootP; | |
875 | int got_some = 0; | |
876 | ||
877 | symbolS *ptr; | |
878 | eject = 1; | |
752d5da4 | 879 | listing_page (NULL); |
252b5132 RH |
880 | |
881 | for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr)) | |
882 | { | |
883 | if (SEG_NORMAL (S_GET_SEGMENT (ptr)) | |
884 | || S_GET_SEGMENT (ptr) == absolute_section) | |
885 | { | |
252b5132 | 886 | /* Don't report section symbols. They are not interesting. */ |
49309057 | 887 | if (symbol_section_p (ptr)) |
252b5132 | 888 | continue; |
7be1c489 | 889 | |
252b5132 RH |
890 | if (S_GET_NAME (ptr)) |
891 | { | |
892 | char buf[30], fmt[8]; | |
893 | valueT val = S_GET_VALUE (ptr); | |
894 | ||
895 | /* @@ Note that this is dependent on the compilation options, | |
896 | not solely on the target characteristics. */ | |
897 | if (sizeof (val) == 4 && sizeof (int) == 4) | |
898 | sprintf (buf, "%08lx", (unsigned long) val); | |
899 | else if (sizeof (val) <= sizeof (unsigned long)) | |
900 | { | |
901 | sprintf (fmt, "%%0%lulx", | |
902 | (unsigned long) (sizeof (val) * 2)); | |
903 | sprintf (buf, fmt, (unsigned long) val); | |
904 | } | |
905 | #if defined (BFD64) | |
906 | else if (sizeof (val) > 4) | |
907 | sprintf_vma (buf, val); | |
908 | #endif | |
909 | else | |
910 | abort (); | |
911 | ||
912 | if (!got_some) | |
913 | { | |
914 | fprintf (list_file, "DEFINED SYMBOLS\n"); | |
915 | on_page++; | |
916 | got_some = 1; | |
917 | } | |
918 | ||
49309057 | 919 | if (symbol_get_frag (ptr) && symbol_get_frag (ptr)->line) |
252b5132 RH |
920 | { |
921 | fprintf (list_file, "%20s:%-5d %s:%s %s\n", | |
49309057 ILT |
922 | symbol_get_frag (ptr)->line->file->filename, |
923 | symbol_get_frag (ptr)->line->line, | |
252b5132 RH |
924 | segment_name (S_GET_SEGMENT (ptr)), |
925 | buf, S_GET_NAME (ptr)); | |
926 | } | |
927 | else | |
928 | { | |
929 | fprintf (list_file, "%33s:%s %s\n", | |
930 | segment_name (S_GET_SEGMENT (ptr)), | |
931 | buf, S_GET_NAME (ptr)); | |
932 | } | |
933 | ||
cf39a089 | 934 | on_page++; |
752d5da4 | 935 | listing_page (NULL); |
252b5132 RH |
936 | } |
937 | } | |
938 | ||
939 | } | |
940 | if (!got_some) | |
941 | { | |
942 | fprintf (list_file, "NO DEFINED SYMBOLS\n"); | |
943 | on_page++; | |
944 | } | |
752d5da4 | 945 | emit_line (NULL, "\n"); |
252b5132 RH |
946 | |
947 | got_some = 0; | |
948 | ||
949 | for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr)) | |
950 | { | |
951 | if (S_GET_NAME (ptr) && strlen (S_GET_NAME (ptr)) != 0) | |
952 | { | |
953 | if (S_GET_SEGMENT (ptr) == undefined_section) | |
954 | { | |
955 | if (!got_some) | |
956 | { | |
957 | got_some = 1; | |
752d5da4 NC |
958 | |
959 | emit_line (NULL, "UNDEFINED SYMBOLS\n"); | |
252b5132 | 960 | } |
752d5da4 NC |
961 | |
962 | emit_line (NULL, "%s\n", S_GET_NAME (ptr)); | |
252b5132 RH |
963 | } |
964 | } | |
965 | } | |
752d5da4 | 966 | |
252b5132 | 967 | if (!got_some) |
752d5da4 | 968 | emit_line (NULL, "NO UNDEFINED SYMBOLS\n"); |
252b5132 RH |
969 | } |
970 | ||
752d5da4 NC |
971 | typedef struct cached_line |
972 | { | |
973 | file_info_type * file; | |
974 | unsigned int line; | |
975 | char buffer [LISTING_RHS_WIDTH]; | |
976 | } cached_line; | |
977 | ||
252b5132 | 978 | static void |
752d5da4 NC |
979 | print_source (file_info_type * current_file, |
980 | list_info_type * list, | |
981 | unsigned int width) | |
252b5132 | 982 | { |
752d5da4 NC |
983 | #define NUM_CACHE_LINES 3 |
984 | static cached_line cached_lines[NUM_CACHE_LINES]; | |
985 | static int next_free_line = 0; | |
3726e6c5 | 986 | cached_line * cache = NULL; |
752d5da4 NC |
987 | |
988 | if (current_file->linenum > list->hll_line | |
989 | && list->hll_line > 0) | |
990 | { | |
991 | /* This can happen with modern optimizing compilers. The source | |
992 | lines from the high level language input program are split up | |
993 | and interleaved, meaning the line number we want to display | |
994 | (list->hll_line) can have already been displayed. We have | |
995 | three choices: | |
996 | ||
997 | a. Do nothing, since we have already displayed the source | |
998 | line. This was the old behaviour. | |
999 | ||
1000 | b. Display the particular line requested again, but only | |
1001 | that line. This is the new behaviour. | |
1002 | ||
1003 | c. Display the particular line requested again and reset | |
1004 | the current_file->line_num value so that we redisplay | |
1005 | all the following lines as well the next time we | |
1006 | encounter a larger line number. */ | |
1007 | int i; | |
1008 | ||
1009 | /* Check the cache, maybe we already have the line saved. */ | |
1010 | for (i = 0; i < NUM_CACHE_LINES; i++) | |
1011 | if (cached_lines[i].file == current_file | |
1012 | && cached_lines[i].line == list->hll_line) | |
1013 | { | |
1014 | cache = cached_lines + i; | |
1015 | break; | |
1016 | } | |
1017 | ||
1018 | if (i == NUM_CACHE_LINES) | |
1019 | { | |
1020 | cache = cached_lines + next_free_line; | |
1021 | next_free_line ++; | |
1022 | if (next_free_line == NUM_CACHE_LINES) | |
1023 | next_free_line = 0; | |
1024 | ||
1025 | cache->file = current_file; | |
1026 | cache->line = list->hll_line; | |
1027 | cache->buffer[0] = 0; | |
1028 | rebuffer_line (current_file, cache->line, cache->buffer, width); | |
1029 | } | |
1030 | ||
1031 | emit_line (list, "%4u:%-13s **** %s\n", | |
1032 | cache->line, cache->file->filename, cache->buffer); | |
1033 | return; | |
1034 | } | |
1035 | ||
252b5132 RH |
1036 | if (!current_file->at_end) |
1037 | { | |
752d5da4 NC |
1038 | int num_lines_shown = 0; |
1039 | ||
252b5132 RH |
1040 | while (current_file->linenum < list->hll_line |
1041 | && !current_file->at_end) | |
1042 | { | |
752d5da4 NC |
1043 | cached_line * cache = cached_lines + next_free_line; |
1044 | char *p; | |
1045 | ||
1046 | cache->file = current_file; | |
1047 | cache->line = current_file->linenum; | |
1048 | cache->buffer[0] = 0; | |
1049 | p = buffer_line (current_file, cache->buffer, width); | |
1050 | ||
1051 | /* Cache optimization: If printing a group of lines | |
1052 | cache the first and last lines in the group. */ | |
1053 | if (num_lines_shown == 0) | |
1054 | { | |
1055 | next_free_line ++; | |
1056 | if (next_free_line == NUM_CACHE_LINES) | |
1057 | next_free_line = 0; | |
1058 | } | |
5a1964ec | 1059 | |
752d5da4 NC |
1060 | emit_line (list, "%4u:%-13s **** %s\n", |
1061 | cache->line, cache->file->filename, p); | |
1062 | num_lines_shown ++; | |
252b5132 RH |
1063 | } |
1064 | } | |
1065 | } | |
1066 | ||
1067 | /* Sometimes the user doesn't want to be bothered by the debugging | |
1068 | records inserted by the compiler, see if the line is suspicious. */ | |
1069 | ||
1070 | static int | |
254d758c | 1071 | debugging_pseudo (list_info_type *list, const char *line) |
252b5132 RH |
1072 | { |
1073 | static int in_debug; | |
1074 | int was_debug; | |
1075 | ||
1076 | if (list->debugging) | |
1077 | { | |
1078 | in_debug = 1; | |
1079 | return 1; | |
1080 | } | |
1081 | ||
1082 | was_debug = in_debug; | |
1083 | in_debug = 0; | |
1084 | ||
3882b010 | 1085 | while (ISSPACE (*line)) |
252b5132 RH |
1086 | line++; |
1087 | ||
1088 | if (*line != '.') | |
1089 | { | |
1090 | #ifdef OBJ_ELF | |
1091 | /* The ELF compiler sometimes emits blank lines after switching | |
1092 | out of a debugging section. If the next line drops us back | |
1093 | into debugging information, then don't print the blank line. | |
1094 | This is a hack for a particular compiler behaviour, not a | |
1095 | general case. */ | |
1096 | if (was_debug | |
1097 | && *line == '\0' | |
1098 | && list->next != NULL | |
1099 | && list->next->debugging) | |
1100 | { | |
1101 | in_debug = 1; | |
1102 | return 1; | |
1103 | } | |
1104 | #endif | |
1105 | ||
1106 | return 0; | |
1107 | } | |
1108 | ||
1109 | line++; | |
1110 | ||
1111 | if (strncmp (line, "def", 3) == 0) | |
1112 | return 1; | |
1113 | if (strncmp (line, "val", 3) == 0) | |
1114 | return 1; | |
1115 | if (strncmp (line, "scl", 3) == 0) | |
1116 | return 1; | |
1117 | if (strncmp (line, "line", 4) == 0) | |
1118 | return 1; | |
1119 | if (strncmp (line, "endef", 5) == 0) | |
1120 | return 1; | |
1121 | if (strncmp (line, "ln", 2) == 0) | |
1122 | return 1; | |
1123 | if (strncmp (line, "type", 4) == 0) | |
1124 | return 1; | |
1125 | if (strncmp (line, "size", 4) == 0) | |
1126 | return 1; | |
1127 | if (strncmp (line, "dim", 3) == 0) | |
1128 | return 1; | |
1129 | if (strncmp (line, "tag", 3) == 0) | |
1130 | return 1; | |
252b5132 RH |
1131 | if (strncmp (line, "stabs", 5) == 0) |
1132 | return 1; | |
1133 | if (strncmp (line, "stabn", 5) == 0) | |
1134 | return 1; | |
1135 | ||
1136 | return 0; | |
1137 | } | |
1138 | ||
1139 | static void | |
254d758c | 1140 | listing_listing (char *name ATTRIBUTE_UNUSED) |
252b5132 RH |
1141 | { |
1142 | list_info_type *list = head; | |
1143 | file_info_type *current_hll_file = (file_info_type *) NULL; | |
1144 | char *message; | |
1145 | char *buffer; | |
1146 | char *p; | |
1147 | int show_listing = 1; | |
1148 | unsigned int width; | |
1149 | ||
1150 | buffer = xmalloc (listing_rhs_width); | |
1151 | data_buffer = xmalloc (MAX_BYTES); | |
1152 | eject = 1; | |
252b5132 RH |
1153 | list = head->next; |
1154 | ||
252b5132 RH |
1155 | while (list) |
1156 | { | |
ab9da554 | 1157 | unsigned int list_line; |
252b5132 RH |
1158 | |
1159 | width = listing_rhs_width > paper_width ? paper_width : | |
1160 | listing_rhs_width; | |
1161 | ||
1162 | list_line = list->line; | |
1163 | switch (list->edict) | |
1164 | { | |
1165 | case EDICT_LIST: | |
1166 | /* Skip all lines up to the current. */ | |
1167 | list_line--; | |
1168 | break; | |
1169 | case EDICT_NOLIST: | |
1170 | show_listing--; | |
1171 | break; | |
1172 | case EDICT_NOLIST_NEXT: | |
61b96bb4 AM |
1173 | if (show_listing == 0) |
1174 | list_line--; | |
252b5132 RH |
1175 | break; |
1176 | case EDICT_EJECT: | |
1177 | break; | |
1178 | case EDICT_NONE: | |
1179 | break; | |
1180 | case EDICT_TITLE: | |
1181 | title = list->edict_arg; | |
1182 | break; | |
1183 | case EDICT_SBTTL: | |
1184 | subtitle = list->edict_arg; | |
1185 | break; | |
1186 | default: | |
1187 | abort (); | |
1188 | } | |
1189 | ||
1190 | if (show_listing <= 0) | |
1191 | { | |
1192 | while (list->file->linenum < list_line | |
1193 | && !list->file->at_end) | |
1194 | p = buffer_line (list->file, buffer, width); | |
1195 | } | |
1196 | ||
61b96bb4 AM |
1197 | if (list->edict == EDICT_LIST |
1198 | || (list->edict == EDICT_NOLIST_NEXT && show_listing == 0)) | |
252b5132 RH |
1199 | { |
1200 | /* Enable listing for the single line that caused the enable. */ | |
1201 | list_line++; | |
1202 | show_listing++; | |
1203 | } | |
1204 | ||
1205 | if (show_listing > 0) | |
1206 | { | |
1207 | /* Scan down the list and print all the stuff which can be done | |
1208 | with this line (or lines). */ | |
1209 | message = 0; | |
1210 | ||
1211 | if (list->hll_file) | |
5a1964ec | 1212 | current_hll_file = list->hll_file; |
252b5132 RH |
1213 | |
1214 | if (current_hll_file && list->hll_line && (listing & LISTING_HLL)) | |
752d5da4 | 1215 | print_source (current_hll_file, list, width); |
252b5132 RH |
1216 | |
1217 | if (list->line_contents) | |
1218 | { | |
1219 | if (!((listing & LISTING_NODEBUG) | |
1220 | && debugging_pseudo (list, list->line_contents))) | |
5a1964ec NC |
1221 | print_lines (list, |
1222 | list->file->linenum == 0 ? list->line : list->file->linenum, | |
1223 | list->line_contents, calc_hex (list)); | |
1224 | ||
252b5132 RH |
1225 | free (list->line_contents); |
1226 | list->line_contents = NULL; | |
1227 | } | |
1228 | else | |
1229 | { | |
1230 | while (list->file->linenum < list_line | |
1231 | && !list->file->at_end) | |
1232 | { | |
1233 | unsigned int address; | |
1234 | ||
1235 | p = buffer_line (list->file, buffer, width); | |
1236 | ||
1237 | if (list->file->linenum < list_line) | |
cf39a089 | 1238 | address = ~(unsigned int) 0; |
252b5132 RH |
1239 | else |
1240 | address = calc_hex (list); | |
1241 | ||
1242 | if (!((listing & LISTING_NODEBUG) | |
1243 | && debugging_pseudo (list, p))) | |
1244 | print_lines (list, list->file->linenum, p, address); | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | if (list->edict == EDICT_EJECT) | |
5a1964ec | 1249 | eject = 1; |
252b5132 RH |
1250 | } |
1251 | ||
61b96bb4 | 1252 | if (list->edict == EDICT_NOLIST_NEXT && show_listing == 1) |
252b5132 RH |
1253 | --show_listing; |
1254 | ||
1255 | list = list->next; | |
1256 | } | |
1257 | ||
1258 | free (buffer); | |
1259 | free (data_buffer); | |
1260 | data_buffer = NULL; | |
1261 | } | |
1262 | ||
83f10cb2 NC |
1263 | /* Print time stamp in ISO format: yyyy-mm-ddThh:mm:ss.ss+/-zzzz. */ |
1264 | ||
1265 | static void | |
1266 | print_timestamp (void) | |
1267 | { | |
1268 | const time_t now = time (NULL); | |
d5a35a55 | 1269 | struct tm * timestamp; |
83f10cb2 NC |
1270 | char stampstr[MAX_DATELEN]; |
1271 | ||
1272 | /* Any portable way to obtain subsecond values??? */ | |
d5a35a55 NC |
1273 | timestamp = localtime (&now); |
1274 | strftime (stampstr, MAX_DATELEN, "%Y-%m-%dT%H:%M:%S.000%z", timestamp); | |
83f10cb2 NC |
1275 | fprintf (list_file, _("\n time stamp \t: %s\n\n"), stampstr); |
1276 | } | |
1277 | ||
1278 | static void | |
1279 | print_single_option (char * opt, int *pos) | |
1280 | { | |
1281 | int opt_len = strlen (opt); | |
1282 | ||
1283 | if ((*pos + opt_len) < paper_width) | |
1284 | { | |
1285 | fprintf (list_file, _("%s "), opt); | |
1286 | *pos = *pos + opt_len; | |
1287 | } | |
1288 | else | |
1289 | { | |
1290 | fprintf (list_file, _("\n\t%s "), opt); | |
1291 | *pos = opt_len; | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | /* Print options passed to as. */ | |
1296 | ||
1297 | static void | |
1298 | print_options (char ** argv) | |
1299 | { | |
1300 | const char *field_name = _("\n options passed\t: "); | |
1301 | int pos = strlen (field_name); | |
1302 | char **p; | |
1303 | ||
ead47374 | 1304 | fputs (field_name, list_file); |
83f10cb2 NC |
1305 | for (p = &argv[1]; *p != NULL; p++) |
1306 | if (**p == '-') | |
1307 | { | |
1308 | /* Ignore these. */ | |
1309 | if (strcmp (*p, "-o") == 0) | |
1310 | { | |
1311 | if (p[1] != NULL) | |
1312 | p++; | |
1313 | continue; | |
1314 | } | |
1315 | if (strcmp (*p, "-v") == 0) | |
1316 | continue; | |
1317 | ||
1318 | print_single_option (*p, &pos); | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | /* Print a first section with basic info like file names, as version, | |
1323 | options passed, target, and timestamp. | |
1324 | The format of this section is as follows: | |
1325 | ||
1326 | AS VERSION | |
1327 | ||
1328 | fieldname TAB ':' fieldcontents | |
1329 | { TAB fieldcontents-cont } */ | |
1330 | ||
1331 | static void | |
1332 | listing_general_info (char ** argv) | |
1333 | { | |
1334 | /* Print the stuff on the first line. */ | |
1335 | eject = 1; | |
752d5da4 | 1336 | listing_page (NULL); |
83f10cb2 NC |
1337 | |
1338 | fprintf (list_file, | |
1339 | _(" GNU assembler version %s (%s)\n\t using BFD version %s."), | |
1340 | VERSION, TARGET_ALIAS, BFD_VERSION_STRING); | |
1341 | print_options (argv); | |
1342 | fprintf (list_file, _("\n input file \t: %s"), fn); | |
1343 | fprintf (list_file, _("\n output file \t: %s"), out_file_name); | |
1344 | fprintf (list_file, _("\n target \t: %s"), TARGET_CANONICAL); | |
1345 | print_timestamp (); | |
1346 | } | |
1347 | ||
252b5132 | 1348 | void |
83f10cb2 | 1349 | listing_print (char *name, char **argv) |
252b5132 RH |
1350 | { |
1351 | int using_stdout; | |
cf39a089 | 1352 | |
252b5132 RH |
1353 | title = ""; |
1354 | subtitle = ""; | |
1355 | ||
1356 | if (name == NULL) | |
1357 | { | |
1358 | list_file = stdout; | |
1359 | using_stdout = 1; | |
1360 | } | |
1361 | else | |
1362 | { | |
f740e790 | 1363 | list_file = fopen (name, FOPEN_WT); |
252b5132 RH |
1364 | if (list_file != NULL) |
1365 | using_stdout = 0; | |
1366 | else | |
1367 | { | |
885afe7b | 1368 | as_warn (_("can't open %s: %s"), name, xstrerror (errno)); |
252b5132 RH |
1369 | list_file = stdout; |
1370 | using_stdout = 1; | |
1371 | } | |
1372 | } | |
1373 | ||
1374 | if (listing & LISTING_NOFORM) | |
5a1964ec | 1375 | paper_height = 0; |
252b5132 | 1376 | |
83f10cb2 NC |
1377 | if (listing & LISTING_GENERAL) |
1378 | listing_general_info (argv); | |
1379 | ||
252b5132 | 1380 | if (listing & LISTING_LISTING) |
5a1964ec | 1381 | listing_listing (name); |
252b5132 RH |
1382 | |
1383 | if (listing & LISTING_SYMBOLS) | |
5a1964ec | 1384 | list_symbol_table (); |
252b5132 RH |
1385 | |
1386 | if (! using_stdout) | |
1387 | { | |
1388 | if (fclose (list_file) == EOF) | |
885afe7b | 1389 | as_warn (_("can't close %s: %s"), name, xstrerror (errno)); |
252b5132 RH |
1390 | } |
1391 | ||
1392 | if (last_open_file) | |
5a1964ec | 1393 | fclose (last_open_file); |
252b5132 RH |
1394 | } |
1395 | ||
252b5132 | 1396 | void |
254d758c | 1397 | listing_file (const char *name) |
252b5132 RH |
1398 | { |
1399 | fn = name; | |
1400 | } | |
1401 | ||
1402 | void | |
254d758c | 1403 | listing_eject (int ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
1404 | { |
1405 | if (listing) | |
1406 | listing_tail->edict = EDICT_EJECT; | |
1407 | } | |
1408 | ||
1409 | void | |
254d758c | 1410 | listing_flags (int ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
1411 | { |
1412 | while ((*input_line_pointer++) && (*input_line_pointer != '\n')) | |
1413 | input_line_pointer++; | |
1414 | ||
1415 | } | |
1416 | ||
1417 | /* Turn listing on or off. An argument of 0 means to turn off | |
1418 | listing. An argument of 1 means to turn on listing. An argument | |
1419 | of 2 means to turn off listing, but as of the next line; that is, | |
1420 | the current line should be listed, but the next line should not. */ | |
1421 | ||
1422 | void | |
254d758c | 1423 | listing_list (int on) |
252b5132 RH |
1424 | { |
1425 | if (listing) | |
1426 | { | |
1427 | switch (on) | |
1428 | { | |
1429 | case 0: | |
1430 | if (listing_tail->edict == EDICT_LIST) | |
1431 | listing_tail->edict = EDICT_NONE; | |
1432 | else | |
1433 | listing_tail->edict = EDICT_NOLIST; | |
1434 | break; | |
1435 | case 1: | |
1436 | if (listing_tail->edict == EDICT_NOLIST | |
1437 | || listing_tail->edict == EDICT_NOLIST_NEXT) | |
1438 | listing_tail->edict = EDICT_NONE; | |
1439 | else | |
1440 | listing_tail->edict = EDICT_LIST; | |
1441 | break; | |
1442 | case 2: | |
1443 | listing_tail->edict = EDICT_NOLIST_NEXT; | |
1444 | break; | |
1445 | default: | |
1446 | abort (); | |
1447 | } | |
1448 | } | |
1449 | } | |
1450 | ||
252b5132 | 1451 | void |
254d758c | 1452 | listing_psize (int width_only) |
252b5132 RH |
1453 | { |
1454 | if (! width_only) | |
1455 | { | |
1456 | paper_height = get_absolute_expression (); | |
1457 | ||
1458 | if (paper_height < 0 || paper_height > 1000) | |
1459 | { | |
1460 | paper_height = 0; | |
1461 | as_warn (_("strange paper height, set to no form")); | |
1462 | } | |
1463 | ||
1464 | if (*input_line_pointer != ',') | |
1465 | { | |
1466 | demand_empty_rest_of_line (); | |
1467 | return; | |
1468 | } | |
1469 | ||
1470 | ++input_line_pointer; | |
1471 | } | |
1472 | ||
1473 | paper_width = get_absolute_expression (); | |
1474 | ||
1475 | demand_empty_rest_of_line (); | |
1476 | } | |
1477 | ||
1478 | void | |
254d758c | 1479 | listing_nopage (int ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
1480 | { |
1481 | paper_height = 0; | |
1482 | } | |
1483 | ||
1484 | void | |
254d758c | 1485 | listing_title (int depth) |
252b5132 RH |
1486 | { |
1487 | int quoted; | |
1488 | char *start; | |
1489 | char *ttl; | |
1490 | unsigned int length; | |
1491 | ||
1492 | SKIP_WHITESPACE (); | |
1493 | if (*input_line_pointer != '\"') | |
1494 | quoted = 0; | |
1495 | else | |
1496 | { | |
1497 | quoted = 1; | |
1498 | ++input_line_pointer; | |
1499 | } | |
1500 | ||
1501 | start = input_line_pointer; | |
1502 | ||
1503 | while (*input_line_pointer) | |
1504 | { | |
1505 | if (quoted | |
1506 | ? *input_line_pointer == '\"' | |
1507 | : is_end_of_line[(unsigned char) *input_line_pointer]) | |
1508 | { | |
1509 | if (listing) | |
1510 | { | |
1511 | length = input_line_pointer - start; | |
1512 | ttl = xmalloc (length + 1); | |
1513 | memcpy (ttl, start, length); | |
1514 | ttl[length] = 0; | |
1515 | listing_tail->edict = depth ? EDICT_SBTTL : EDICT_TITLE; | |
1516 | listing_tail->edict_arg = ttl; | |
1517 | } | |
1518 | if (quoted) | |
1519 | input_line_pointer++; | |
1520 | demand_empty_rest_of_line (); | |
1521 | return; | |
1522 | } | |
1523 | else if (*input_line_pointer == '\n') | |
1524 | { | |
0e389e77 | 1525 | as_bad (_("new line in title")); |
252b5132 RH |
1526 | demand_empty_rest_of_line (); |
1527 | return; | |
1528 | } | |
1529 | else | |
1530 | { | |
1531 | input_line_pointer++; | |
1532 | } | |
1533 | } | |
1534 | } | |
1535 | ||
252b5132 | 1536 | void |
254d758c | 1537 | listing_source_line (unsigned int line) |
252b5132 RH |
1538 | { |
1539 | if (listing) | |
1540 | { | |
1541 | new_frag (); | |
1542 | listing_tail->hll_line = line; | |
1543 | new_frag (); | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | void | |
254d758c | 1548 | listing_source_file (const char *file) |
252b5132 RH |
1549 | { |
1550 | if (listing) | |
1551 | listing_tail->hll_file = file_info (file); | |
1552 | } | |
1553 | ||
252b5132 RH |
1554 | #else |
1555 | ||
cf39a089 | 1556 | /* Dummy functions for when compiled without listing enabled. */ |
252b5132 RH |
1557 | |
1558 | void | |
254d758c | 1559 | listing_flags (int ignore) |
252b5132 RH |
1560 | { |
1561 | s_ignore (0); | |
1562 | } | |
1563 | ||
cf39a089 | 1564 | void |
254d758c | 1565 | listing_list (int on) |
252b5132 RH |
1566 | { |
1567 | s_ignore (0); | |
1568 | } | |
1569 | ||
cf39a089 | 1570 | void |
254d758c | 1571 | listing_eject (int ignore) |
252b5132 RH |
1572 | { |
1573 | s_ignore (0); | |
1574 | } | |
1575 | ||
cf39a089 | 1576 | void |
254d758c | 1577 | listing_psize (int ignore) |
252b5132 RH |
1578 | { |
1579 | s_ignore (0); | |
1580 | } | |
1581 | ||
1582 | void | |
254d758c | 1583 | listing_nopage (int ignore) |
252b5132 RH |
1584 | { |
1585 | s_ignore (0); | |
1586 | } | |
1587 | ||
cf39a089 | 1588 | void |
254d758c | 1589 | listing_title (int depth) |
252b5132 RH |
1590 | { |
1591 | s_ignore (0); | |
1592 | } | |
1593 | ||
1594 | void | |
254d758c | 1595 | listing_file (const char *name) |
252b5132 | 1596 | { |
252b5132 RH |
1597 | } |
1598 | ||
cf39a089 | 1599 | void |
254d758c | 1600 | listing_newline (char *name) |
252b5132 | 1601 | { |
252b5132 RH |
1602 | } |
1603 | ||
cf39a089 | 1604 | void |
254d758c | 1605 | listing_source_line (unsigned int n) |
252b5132 | 1606 | { |
252b5132 | 1607 | } |
cf39a089 KH |
1608 | |
1609 | void | |
254d758c | 1610 | listing_source_file (const char *n) |
252b5132 | 1611 | { |
252b5132 RH |
1612 | } |
1613 | ||
1614 | #endif |