1 /* listing.c - maintain assembly listings
2 Copyright (C) 1991-2024 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 /* Contributed by Steve Chamberlain <sac@cygnus.com>
23 A listing page looks like:
25 LISTING_HEADER sourcefilename pagenumber
28 linenumber address data source
29 linenumber address data source
30 linenumber address data source
31 linenumber address data source
33 If not overridden, the listing commands are:
36 Put "stuff" onto the title line
38 Put stuff onto the subtitle line
40 If these commands come within 10 lines of the top of the page, they
41 will affect the page they are on, as well as any subsequent page
46 Increment the enable listing counter
48 Decrement the enable listing counter
51 Set the paper size to X wide and Y high. Setting a psize Y of
52 zero will suppress form feeds except where demanded by .eject
54 If the counter goes below zero, listing is suppressed.
56 Listings are a maintained by read calling various listing_<foo>
57 functions. What happens most is that the macro NO_LISTING is not
58 defined (from the Makefile), then the macro LISTING_NEWLINE expands
59 into a call to listing_newline. The call is done from read.c, every
60 time it sees a newline, and -l is on the command line.
62 The function listing_newline remembers the frag associated with the
63 newline, and creates a new frag - note that this is wasteful, but not
64 a big deal, since listing slows things down a lot anyway. The
65 function also remembers when the filename changes.
67 When all the input has finished, and gas has had a chance to settle
68 down, the listing is output. This is done by running down the list of
69 frag/source file records, and opening the files as needed and printing
70 out the bytes and chars associated with them.
72 The only things which the architecture can change about the listing
73 are defined in these macros:
75 LISTING_HEADER The name of the architecture
76 LISTING_WORD_SIZE The make of the number of bytes in a word, this determines
77 the clumping of the output data. eg a value of
78 2 makes words look like 1234 5678, whilst 1
79 would make the same value look like 12 34 56
81 LISTING_LHS_WIDTH Number of words of above size for the lhs
83 LISTING_LHS_WIDTH_SECOND Number of words for the data on the lhs
86 LISTING_LHS_CONT_LINES Max number of lines to use up for a continuation
87 LISTING_RHS_WIDTH Number of chars from the input file to print
91 #include "filenames.h"
92 #include "safe-ctype.h"
93 #include "input-file.h"
101 #ifndef LISTING_HEADER
102 #define LISTING_HEADER "GAS LISTING"
104 #ifndef LISTING_WORD_SIZE
105 #define LISTING_WORD_SIZE 4
107 #ifndef LISTING_LHS_WIDTH
108 #define LISTING_LHS_WIDTH ((LISTING_WORD_SIZE) > 4 ? 1 : 4 / (LISTING_WORD_SIZE))
110 #ifndef LISTING_LHS_WIDTH_SECOND
111 #define LISTING_LHS_WIDTH_SECOND LISTING_LHS_WIDTH
113 #ifndef LISTING_RHS_WIDTH
114 #define LISTING_RHS_WIDTH 100
116 #ifndef LISTING_LHS_CONT_LINES
117 #define LISTING_LHS_CONT_LINES 4
119 #define MAX_DATELEN 30
121 /* This structure remembers which .s were used. */
122 typedef struct file_info_struct
124 struct file_info_struct
* next
;
127 unsigned int linenum
;
146 struct list_message
*next
;
149 /* This structure remembers which line from which file goes into which
151 struct list_info_struct
153 /* Frag which this line of source is nearest to. */
156 /* The actual line in the source file. */
159 /* Pointer to the file info struct for the file which this line
161 file_info_type
*file
;
163 /* The expanded text of any macro that may have been executing. */
167 struct list_info_struct
*next
;
169 /* Pointer to the file info struct for the high level language
170 source line that belongs here. */
171 file_info_type
*hll_file
;
173 /* High level language source line. */
174 unsigned int hll_line
;
176 /* Pointers to linked list of messages associated with this line. */
177 struct list_message
*messages
, *last_message
;
179 enum edict_enum edict
;
182 /* Nonzero if this line is to be omitted because it contains
183 debugging information. This can become a flags field if we come
184 up with more information to store here. */
188 typedef struct list_info_struct list_info_type
;
190 int listing_lhs_width
= LISTING_LHS_WIDTH
;
191 int listing_lhs_width_second
= LISTING_LHS_WIDTH_SECOND
;
192 int listing_lhs_cont_lines
= LISTING_LHS_CONT_LINES
;
193 int listing_rhs_width
= LISTING_RHS_WIDTH
;
195 struct list_info_struct
* listing_tail
;
197 static file_info_type
* file_info_head
;
198 static file_info_type
* last_open_file_info
;
199 static FILE * last_open_file
;
200 static struct list_info_struct
* head
;
201 static int paper_width
= 200;
202 static int paper_height
= 60;
206 /* File to output listings to. */
207 static FILE *list_file
;
209 /* This static array is used to keep the text of data to be printed
210 before the start of the line. */
213 (((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width \
214 + ((((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second) \
215 * listing_lhs_cont_lines) \
218 static char *data_buffer
;
221 static void listing_message (const char *, const char *);
222 static file_info_type
*file_info (const char *);
223 static void new_frag (void);
224 static void listing_page (list_info_type
*);
225 static unsigned int calc_hex (list_info_type
*);
226 static void print_lines (list_info_type
*, unsigned int, const char *,
228 static void list_symbol_table (void);
229 static int debugging_pseudo (list_info_type
*, const char *);
230 static void listing_listing (char *);
233 listing_message (const char *name
, const char *message
)
235 if (listing_tail
!= (list_info_type
*) NULL
)
237 char *n
= concat (name
, message
, (char *) NULL
);
238 struct list_message
*lm
= XNEW (struct list_message
);
242 if (listing_tail
->last_message
)
243 listing_tail
->last_message
->next
= lm
;
245 listing_tail
->messages
= lm
;
246 listing_tail
->last_message
= lm
;
251 listing_warning (const char *message
)
253 listing_message (_("Warning: "), message
);
257 listing_error (const char *message
)
259 listing_message (_("Error: "), message
);
262 static file_info_type
*
263 file_info (const char *file_name
)
265 /* Find an entry with this file name. */
266 file_info_type
*p
= file_info_head
;
268 while (p
!= (file_info_type
*) NULL
)
270 if (filename_cmp (p
->filename
, file_name
) == 0)
275 /* Make new entry. */
276 p
= XNEW (file_info_type
);
277 p
->next
= file_info_head
;
279 p
->filename
= xstrdup (file_name
);
290 frag_wane (frag_now
);
295 listing_newline (char *ps
)
299 static unsigned int last_line
= 0xffff;
300 static const char *last_file
= NULL
;
301 list_info_type
*new_i
= NULL
;
306 if (now_seg
== absolute_section
)
310 /* In ELF, anything in a section beginning with .debug or .line is
311 considered to be debugging information. This includes the
312 statement which switches us into the debugging section, which we
313 can only set after we are already in the debugging section. */
314 if ((listing
& LISTING_NODEBUG
) != 0
315 && listing_tail
!= NULL
316 && ! listing_tail
->debugging
)
320 segname
= segment_name (now_seg
);
321 if (startswith (segname
, ".debug")
322 || startswith (segname
, ".line"))
323 listing_tail
->debugging
= 1;
327 /* PR 21977 - use the physical file name not the logical one unless high
328 level source files are being included in the listing. */
329 if (listing
& LISTING_HLL
)
330 file
= as_where (&line
);
332 file
= as_where_physical (&line
);
336 if (line
== last_line
337 && !(last_file
&& file
&& filename_cmp (file
, last_file
)))
340 new_i
= XNEW (list_info_type
);
342 /* Detect if we are reading from stdin by examining the file
343 name returned by as_where().
345 [FIXME: We rely upon the name in the strcmp below being the
346 same as the one used by input_scrub_new_file(), if that is
347 not true, then this code will fail].
349 If we are reading from stdin, then we need to save each input
350 line here (assuming of course that we actually have a line of
351 input to read), so that it can be displayed in the listing
352 that is produced at the end of the assembly. */
353 if (strcmp (file
, _("{standard input}")) == 0
354 && input_line_pointer
!= NULL
)
356 char *copy
, *src
, *dest
;
361 for (copy
= input_line_pointer
;
363 || is_end_of_line
[(unsigned char) *copy
] != 1);
368 else if (*copy
== '\\')
370 else if (*copy
== '"')
371 seen_quote
= !seen_quote
;
374 len
= copy
- input_line_pointer
+ 1;
376 copy
= XNEWVEC (char, len
);
378 src
= input_line_pointer
;
383 unsigned char c
= *src
++;
385 /* Omit control characters in the listing. */
392 new_i
->line_contents
= copy
;
395 new_i
->line_contents
= NULL
;
399 new_i
= XNEW (list_info_type
);
400 new_i
->line_contents
= ps
;
409 listing_tail
->next
= new_i
;
413 listing_tail
= new_i
;
415 new_i
->frag
= frag_now
;
417 new_i
->file
= file_info (file
);
418 new_i
->next
= (list_info_type
*) NULL
;
419 new_i
->messages
= NULL
;
420 new_i
->last_message
= NULL
;
421 new_i
->edict
= EDICT_NONE
;
422 new_i
->hll_file
= (file_info_type
*) NULL
;
424 new_i
->debugging
= 0;
429 /* In ELF, anything in a section beginning with .debug or .line is
430 considered to be debugging information. */
431 if ((listing
& LISTING_NODEBUG
) != 0)
435 segname
= segment_name (now_seg
);
436 if (startswith (segname
, ".debug")
437 || startswith (segname
, ".line"))
438 new_i
->debugging
= 1;
443 /* Attach all current frags to the previous line instead of the
444 current line. This is called by the MIPS backend when it discovers
445 that it needs to add some NOP instructions; the added NOP
446 instructions should go with the instruction that has the delay, not
447 with the new instruction. */
450 listing_prev_line (void)
455 if (head
== (list_info_type
*) NULL
456 || head
== listing_tail
)
461 for (l
= head
; l
->next
!= listing_tail
; l
= l
->next
)
464 for (f
= frchain_now
->frch_root
; f
!= (fragS
*) NULL
; f
= f
->fr_next
)
465 if (f
->line
== listing_tail
)
468 listing_tail
->frag
= frag_now
;
472 /* This function returns the next source line from the file supplied,
473 truncated to size. It appends a fake line to the end of each input
474 file to make using the returned buffer simpler. */
477 buffer_line (file_info_type
*file
, char *line
, unsigned int size
)
479 unsigned int count
= 0;
483 /* If we couldn't open the file, return an empty line. */
487 /* Check the cache and see if we last used this file. */
488 if (!last_open_file_info
|| file
!= last_open_file_info
)
492 last_open_file_info
->pos
= ftell (last_open_file
);
493 fclose (last_open_file
);
496 /* Open the file in the binary mode so that ftell above can
497 return a reliable value that we can feed to fseek below. */
498 last_open_file_info
= file
;
499 last_open_file
= fopen (file
->filename
, FOPEN_RB
);
500 if (last_open_file
== NULL
)
506 /* Seek to where we were last time this file was open. */
508 fseek (last_open_file
, file
->pos
, SEEK_SET
);
511 c
= fgetc (last_open_file
);
513 while (c
!= EOF
&& c
!= '\n' && c
!= '\r')
517 c
= fgetc (last_open_file
);
520 /* If '\r' is followed by '\n', swallow that. Likewise, if '\n'
521 is followed by '\r', swallow that as well. */
522 if (c
== '\r' || c
== '\n')
524 int next
= fgetc (last_open_file
);
526 if ((c
== '\r' && next
!= '\n')
527 || (c
== '\n' && next
!= '\r'))
528 ungetc (next
, last_open_file
);
534 if (count
+ 3 < size
)
547 /* This function rewinds the requested file back to the line requested,
548 reads it in again into the buffer provided and then restores the file
549 back to its original location. */
552 rebuffer_line (file_info_type
* file
,
553 unsigned int linenum
,
557 unsigned int count
= 0;
558 unsigned int current_line
;
566 if (file
== NULL
|| buffer
== NULL
|| size
<= 1 || file
->linenum
<= linenum
)
569 /* Check the cache and see if we last used this file. */
570 if (last_open_file_info
== NULL
|| file
!= last_open_file_info
)
574 last_open_file_info
->pos
= ftell (last_open_file
);
575 fclose (last_open_file
);
578 /* Open the file in the binary mode so that ftell above can
579 return a reliable value that we can feed to fseek below. */
580 last_open_file_info
= file
;
581 last_open_file
= fopen (file
->filename
, FOPEN_RB
);
582 if (last_open_file
== NULL
)
588 /* Seek to where we were last time this file was open. */
590 fseek (last_open_file
, file
->pos
, SEEK_SET
);
593 /* Remember where we are in the current file. */
594 pos2
= pos
= ftell (last_open_file
);
597 current_line
= file
->linenum
;
599 /* Leave room for the nul at the end of the buffer. */
603 /* Increment the current line count by one.
604 This is to allow for the fact that we are searching for the
605 start of a previous line, but we do this by detecting end-of-line
606 character(s) not start-of-line characters. */
609 while (pos2
> 0 && ! found
)
613 /* Move backwards through the file, looking for earlier lines. */
614 pos2
= (long) size
> pos2
? 0 : pos2
- size
;
615 fseek (last_open_file
, pos2
, SEEK_SET
);
617 /* Our caller has kindly provided us with a buffer, so we use it. */
618 if (fread (buffer
, 1, size
, last_open_file
) != size
)
620 as_warn (_("unable to rebuffer file: %s\n"), file
->filename
);
624 for (ptr
= buffer
+ size
; ptr
>= buffer
; -- ptr
)
630 if (current_line
== linenum
)
632 /* We have found the start of the line we seek. */
635 /* FIXME: We could skip the read-in-the-line code
636 below if we know that we already have the whole
637 line in the buffer. */
639 /* Advance pos2 to the newline character we have just located. */
640 pos2
+= (ptr
- buffer
);
642 /* Skip the newline and, if present, the carriage return. */
643 if (ptr
+ 1 == buffer
+ size
)
646 if (fgetc (last_open_file
) == '\r')
650 pos2
+= (ptr
[1] == '\r' ? 2 : 1);
652 /* Move the file pointer to this location. */
653 fseek (last_open_file
, pos2
, SEEK_SET
);
660 /* Read in the line. */
661 c
= fgetc (last_open_file
);
663 while (c
!= EOF
&& c
!= '\n' && c
!= '\r')
669 c
= fgetc (last_open_file
);
672 /* If '\r' is followed by '\n', swallow that. Likewise, if '\n'
673 is followed by '\r', swallow that as well. */
674 if (c
== '\r' || c
== '\n')
676 int next
= fgetc (last_open_file
);
678 if ((c
== '\r' && next
!= '\n')
679 || (c
== '\n' && next
!= '\r'))
680 ungetc (next
, last_open_file
);
683 /* Terminate the line. */
686 /* Reset the file position. */
687 fseek (last_open_file
, pos
, SEEK_SET
);
690 static const char *fn
;
691 static unsigned int eject
; /* Eject pending. */
692 static unsigned int page
; /* Current page number. */
693 static const char *title
; /* Current title. */
694 static const char *subtitle
; /* Current subtitle. */
695 static unsigned int on_page
; /* Number of lines printed on current page. */
698 listing_page (list_info_type
*list
)
700 /* Grope around, see if we can see a title or subtitle edict coming up
701 soon. (we look down 10 lines of the page and see if it's there) */
702 if ((eject
|| (on_page
>= (unsigned int) paper_height
))
703 && paper_height
!= 0)
707 int had_subtitle
= 0;
711 while (c
!= 0 && list
)
713 if (list
->edict
== EDICT_SBTTL
&& !had_subtitle
)
716 subtitle
= list
->edict_arg
;
718 if (list
->edict
== EDICT_TITLE
&& !had_title
)
721 title
= list
->edict_arg
;
729 fprintf (list_file
, "\f");
732 fprintf (list_file
, "%s %s \t\t\tpage %d\n", LISTING_HEADER
, fn
, page
);
733 fprintf (list_file
, "%s\n", title
);
734 fprintf (list_file
, "%s\n", subtitle
);
740 /* Print a line into the list_file. Update the line count
741 and if necessary start a new page. */
744 emit_line (list_info_type
* list
, const char * format
, ...)
748 va_start (args
, format
);
750 vfprintf (list_file
, format
, args
);
758 calc_hex (list_info_type
*list
)
760 int data_buffer_size
;
761 list_info_type
*first
= list
;
762 unsigned int address
= ~(unsigned int) 0;
765 unsigned int octet_in_frag
;
767 /* Find first frag which says it belongs to this line. */
769 while (frag
&& frag
->line
!= list
)
770 frag
= frag
->fr_next
;
774 data_buffer_size
= 0;
776 /* Dump all the frags which belong to this line. */
777 while (frag_ptr
!= (fragS
*) NULL
&& frag_ptr
->line
== first
)
779 /* Print as many bytes from the fixed part as is sensible. */
781 while (octet_in_frag
< frag_ptr
->fr_fix
782 && data_buffer_size
< MAX_BYTES
- 3)
784 if (address
== ~(unsigned int) 0)
785 address
= frag_ptr
->fr_address
/ OCTETS_PER_BYTE
;
787 sprintf (data_buffer
+ data_buffer_size
,
789 (frag_ptr
->fr_literal
[octet_in_frag
]) & 0xff);
790 data_buffer_size
+= 2;
793 if (frag_ptr
->fr_type
== rs_fill
)
795 unsigned int var_rep_max
= octet_in_frag
;
796 unsigned int var_rep_idx
= octet_in_frag
;
798 /* Print as many bytes from the variable part as is sensible. */
799 while ((octet_in_frag
800 < frag_ptr
->fr_fix
+ frag_ptr
->fr_var
* frag_ptr
->fr_offset
)
801 && data_buffer_size
< MAX_BYTES
- 3)
803 if (address
== ~(unsigned int) 0)
804 address
= frag_ptr
->fr_address
/ OCTETS_PER_BYTE
;
806 sprintf (data_buffer
+ data_buffer_size
,
808 (frag_ptr
->fr_literal
[var_rep_idx
]) & 0xff);
809 data_buffer_size
+= 2;
814 if (var_rep_idx
>= frag_ptr
->fr_fix
+ frag_ptr
->fr_var
)
815 var_rep_idx
= var_rep_max
;
818 else if (frag_ptr
->fr_type
== rs_fill_nop
&& frag_ptr
->fr_opcode
)
820 gas_assert (!octet_in_frag
);
822 /* Print as many bytes from fr_opcode as is sensible. */
823 while (octet_in_frag
< (unsigned int) frag_ptr
->fr_offset
824 && data_buffer_size
< MAX_BYTES
- 3)
826 if (address
== ~(unsigned int) 0)
827 address
= frag_ptr
->fr_address
/ OCTETS_PER_BYTE
;
829 sprintf (data_buffer
+ data_buffer_size
,
831 frag_ptr
->fr_opcode
[octet_in_frag
] & 0xff);
832 data_buffer_size
+= 2;
837 free (frag_ptr
->fr_opcode
);
838 frag_ptr
->fr_opcode
= NULL
;
841 frag_ptr
= frag_ptr
->fr_next
;
843 data_buffer
[data_buffer_size
] = '\0';
848 print_lines (list_info_type
*list
, unsigned int lineno
,
849 const char *string
, unsigned int address
)
854 unsigned int octet_in_word
= 0;
855 char *src
= data_buffer
;
857 struct list_message
*msg
;
859 /* Print the stuff on the first line. */
861 nchars
= (LISTING_WORD_SIZE
* 2 + 1) * listing_lhs_width
;
863 /* Print the hex for the first line. */
864 if (address
== ~(unsigned int) 0)
866 fprintf (list_file
, "% 4d ", lineno
);
867 for (idx
= 0; idx
< nchars
; idx
++)
868 fprintf (list_file
, " ");
870 emit_line (NULL
, "\t%s\n", string
? string
: "");
875 fprintf (list_file
, "% 4d ???? ", lineno
);
877 fprintf (list_file
, "% 4d %04x ", lineno
, address
);
879 /* And the data to go along with it. */
882 while (src
[cur
] && idx
< nchars
)
886 fprintf (list_file
, "%c%c", src
[offset
], src
[offset
+ 1]);
890 if (octet_in_word
== LISTING_WORD_SIZE
)
892 fprintf (list_file
, " ");
900 for (; idx
< nchars
; idx
++)
901 fprintf (list_file
, " ");
903 emit_line (list
, "\t%s\n", string
? string
: "");
905 for (msg
= list
->messages
; msg
; msg
= msg
->next
)
906 emit_line (list
, "**** %s\n", msg
->message
);
909 lines
< (unsigned int) listing_lhs_cont_lines
913 nchars
= ((LISTING_WORD_SIZE
* 2) + 1) * listing_lhs_width_second
- 1;
916 /* Print any more lines of data, but more compactly. */
917 fprintf (list_file
, "% 4d ", lineno
);
919 while (src
[cur
] && idx
< nchars
)
923 fprintf (list_file
, "%c%c", src
[offset
], src
[offset
+ 1]);
928 if (octet_in_word
== LISTING_WORD_SIZE
)
930 fprintf (list_file
, " ");
936 emit_line (list
, "\n");
941 list_symbol_table (void)
943 extern symbolS
*symbol_rootP
;
950 for (ptr
= symbol_rootP
; ptr
!= (symbolS
*) NULL
; ptr
= symbol_next (ptr
))
952 if (SEG_NORMAL (S_GET_SEGMENT (ptr
))
953 || S_GET_SEGMENT (ptr
) == absolute_section
)
955 /* Don't report section symbols. They are not interesting. */
956 if (symbol_section_p (ptr
))
959 if (S_GET_NAME (ptr
))
962 valueT val
= S_GET_VALUE (ptr
);
964 bfd_sprintf_vma (stdoutput
, buf
, val
);
967 fprintf (list_file
, "DEFINED SYMBOLS\n");
972 if (symbol_get_frag (ptr
) && symbol_get_frag (ptr
)->line
)
974 fprintf (list_file
, "%20s:%-5d %s:%s %s\n",
975 symbol_get_frag (ptr
)->line
->file
->filename
,
976 symbol_get_frag (ptr
)->line
->line
,
977 segment_name (S_GET_SEGMENT (ptr
)),
978 buf
, S_GET_NAME (ptr
));
982 fprintf (list_file
, "%33s:%s %s\n",
983 segment_name (S_GET_SEGMENT (ptr
)),
984 buf
, S_GET_NAME (ptr
));
995 fprintf (list_file
, "NO DEFINED SYMBOLS\n");
998 emit_line (NULL
, "\n");
1002 for (ptr
= symbol_rootP
; ptr
!= (symbolS
*) NULL
; ptr
= symbol_next (ptr
))
1004 if (S_GET_NAME (ptr
) && strlen (S_GET_NAME (ptr
)) != 0)
1006 if (S_GET_SEGMENT (ptr
) == undefined_section
)
1012 emit_line (NULL
, "UNDEFINED SYMBOLS\n");
1015 emit_line (NULL
, "%s\n", S_GET_NAME (ptr
));
1021 emit_line (NULL
, "NO UNDEFINED SYMBOLS\n");
1024 typedef struct cached_line
1026 file_info_type
* file
;
1028 char buffer
[LISTING_RHS_WIDTH
];
1032 print_source (file_info_type
* current_file
,
1033 list_info_type
* list
,
1036 #define NUM_CACHE_LINES 3
1037 static cached_line cached_lines
[NUM_CACHE_LINES
];
1038 static int next_free_line
= 0;
1039 cached_line
* cache
= NULL
;
1041 if (current_file
->linenum
> list
->hll_line
1042 && list
->hll_line
> 0)
1044 /* This can happen with modern optimizing compilers. The source
1045 lines from the high level language input program are split up
1046 and interleaved, meaning the line number we want to display
1047 (list->hll_line) can have already been displayed. We have
1050 a. Do nothing, since we have already displayed the source
1051 line. This was the old behaviour.
1053 b. Display the particular line requested again, but only
1054 that line. This is the new behaviour.
1056 c. Display the particular line requested again and reset
1057 the current_file->line_num value so that we redisplay
1058 all the following lines as well the next time we
1059 encounter a larger line number. */
1062 /* Check the cache, maybe we already have the line saved. */
1063 for (i
= 0; i
< NUM_CACHE_LINES
; i
++)
1064 if (cached_lines
[i
].file
== current_file
1065 && cached_lines
[i
].line
== list
->hll_line
)
1067 cache
= cached_lines
+ i
;
1071 if (i
== NUM_CACHE_LINES
)
1073 cache
= cached_lines
+ next_free_line
;
1075 if (next_free_line
== NUM_CACHE_LINES
)
1078 cache
->file
= current_file
;
1079 cache
->line
= list
->hll_line
;
1080 cache
->buffer
[0] = 0;
1081 rebuffer_line (current_file
, cache
->line
, cache
->buffer
, width
);
1084 emit_line (list
, "%4u:%-13s **** %s\n",
1085 cache
->line
, cache
->file
->filename
, cache
->buffer
);
1089 if (!current_file
->at_end
)
1091 int num_lines_shown
= 0;
1093 while (current_file
->linenum
< list
->hll_line
1094 && !current_file
->at_end
)
1098 cache
= cached_lines
+ next_free_line
;
1099 cache
->file
= current_file
;
1100 cache
->line
= current_file
->linenum
+ 1;
1101 cache
->buffer
[0] = 0;
1102 p
= buffer_line (current_file
, cache
->buffer
, width
);
1104 /* Cache optimization: If printing a group of lines
1105 cache the first and last lines in the group. */
1106 if (num_lines_shown
== 0)
1109 if (next_free_line
== NUM_CACHE_LINES
)
1113 emit_line (list
, "%4u:%-13s **** %s\n",
1114 cache
->line
, cache
->file
->filename
, p
);
1120 /* Sometimes the user doesn't want to be bothered by the debugging
1121 records inserted by the compiler, see if the line is suspicious. */
1124 debugging_pseudo (list_info_type
*list
, const char *line
)
1127 static int in_debug
;
1131 if (list
->debugging
)
1139 was_debug
= in_debug
;
1143 while (ISSPACE (*line
))
1149 /* The ELF compiler sometimes emits blank lines after switching
1150 out of a debugging section. If the next line drops us back
1151 into debugging information, then don't print the blank line.
1152 This is a hack for a particular compiler behaviour, not a
1156 && list
->next
!= NULL
1157 && list
->next
->debugging
)
1169 if (startswith (line
, "def"))
1171 if (startswith (line
, "val"))
1173 if (startswith (line
, "scl"))
1175 if (startswith (line
, "line"))
1177 if (startswith (line
, "endef"))
1179 if (startswith (line
, "ln"))
1181 if (startswith (line
, "type"))
1183 if (startswith (line
, "size"))
1185 if (startswith (line
, "dim"))
1187 if (startswith (line
, "tag"))
1189 if (startswith (line
, "stabs"))
1191 if (startswith (line
, "stabn"))
1198 listing_listing (char *name ATTRIBUTE_UNUSED
)
1200 list_info_type
*list
= head
;
1201 file_info_type
*current_hll_file
= (file_info_type
*) NULL
;
1204 int show_listing
= 1;
1207 buffer
= XNEWVEC (char, listing_rhs_width
);
1208 data_buffer
= XNEWVEC (char, MAX_BYTES
);
1214 unsigned int list_line
;
1216 width
= listing_rhs_width
> paper_width
? paper_width
:
1219 list_line
= list
->line
;
1220 switch (list
->edict
)
1223 /* Skip all lines up to the current. */
1229 case EDICT_NOLIST_NEXT
:
1230 if (show_listing
== 0)
1238 title
= list
->edict_arg
;
1241 subtitle
= list
->edict_arg
;
1247 if (show_listing
<= 0)
1249 while (list
->file
->linenum
< list_line
1250 && !list
->file
->at_end
)
1251 p
= buffer_line (list
->file
, buffer
, width
);
1254 if (list
->edict
== EDICT_LIST
1255 || (list
->edict
== EDICT_NOLIST_NEXT
&& show_listing
== 0))
1257 /* Enable listing for the single line that caused the enable. */
1262 if (show_listing
> 0)
1264 /* Scan down the list and print all the stuff which can be done
1265 with this line (or lines). */
1267 current_hll_file
= list
->hll_file
;
1269 if (current_hll_file
&& list
->hll_line
&& (listing
& LISTING_HLL
))
1270 print_source (current_hll_file
, list
, width
);
1272 if (!list
->line_contents
|| list
->file
->linenum
)
1274 while (list
->file
->linenum
< list_line
1275 && !list
->file
->at_end
)
1277 unsigned int address
;
1279 p
= buffer_line (list
->file
, buffer
, width
);
1281 if (list
->file
->linenum
< list_line
)
1282 address
= ~(unsigned int) 0;
1284 address
= calc_hex (list
);
1286 if (!((listing
& LISTING_NODEBUG
)
1287 && debugging_pseudo (list
, p
)))
1288 print_lines (list
, list
->file
->linenum
, p
, address
);
1292 if (list
->line_contents
)
1294 if (!((listing
& LISTING_NODEBUG
)
1295 && debugging_pseudo (list
, list
->line_contents
)))
1296 print_lines (list
, list
->line
, list
->line_contents
,
1299 free (list
->line_contents
);
1300 list
->line_contents
= NULL
;
1303 if (list
->edict
== EDICT_EJECT
)
1307 if (list
->edict
== EDICT_NOLIST_NEXT
&& show_listing
== 1)
1318 /* Print time stamp in ISO format: yyyy-mm-ddThh:mm:ss.ss+/-zzzz. */
1321 print_timestamp (void)
1323 const time_t now
= time (NULL
);
1324 struct tm
* timestamp
;
1325 char stampstr
[MAX_DATELEN
];
1327 /* Any portable way to obtain subsecond values??? */
1328 timestamp
= localtime (&now
);
1329 strftime (stampstr
, MAX_DATELEN
, "%Y-%m-%dT%H:%M:%S.000%z", timestamp
);
1330 fprintf (list_file
, _("\n time stamp \t: %s\n\n"), stampstr
);
1334 print_single_option (char * opt
, int *pos
)
1336 int opt_len
= strlen (opt
);
1338 if ((*pos
+ opt_len
) < paper_width
)
1340 fprintf (list_file
, _("%s "), opt
);
1341 *pos
= *pos
+ opt_len
;
1345 fprintf (list_file
, _("\n\t%s "), opt
);
1350 /* Print options passed to as. */
1353 print_options (char ** argv
)
1355 const char *field_name
= _("\n options passed\t: ");
1356 int pos
= strlen (field_name
);
1359 fputs (field_name
, list_file
);
1360 for (p
= &argv
[1]; *p
!= NULL
; p
++)
1364 if (strcmp (*p
, "-o") == 0)
1370 if (strcmp (*p
, "-v") == 0)
1373 print_single_option (*p
, &pos
);
1377 /* Print a first section with basic info like file names, as version,
1378 options passed, target, and timestamp.
1379 The format of this section is as follows:
1383 fieldname TAB ':' fieldcontents
1384 { TAB fieldcontents-cont } */
1387 listing_general_info (char ** argv
)
1389 /* Print the stuff on the first line. */
1391 listing_page (NULL
);
1394 _(" GNU assembler version %s (%s)\n\t using BFD version %s."),
1395 VERSION
, TARGET_ALIAS
, BFD_VERSION_STRING
);
1396 print_options (argv
);
1397 fprintf (list_file
, _("\n input file \t: %s"), fn
);
1398 fprintf (list_file
, _("\n output file \t: %s"), out_file_name
);
1399 fprintf (list_file
, _("\n target \t: %s"), TARGET_CANONICAL
);
1404 listing_print (char *name
, char **argv
)
1418 list_file
= fopen (name
, FOPEN_WT
);
1419 if (list_file
!= NULL
)
1423 as_warn (_("can't open %s: %s"), name
, xstrerror (errno
));
1429 if (listing
& LISTING_NOFORM
)
1432 if (listing
& LISTING_GENERAL
)
1433 listing_general_info (argv
);
1435 if (listing
& LISTING_LISTING
)
1436 listing_listing (name
);
1438 if (listing
& LISTING_SYMBOLS
)
1439 list_symbol_table ();
1443 if (fclose (list_file
) == EOF
)
1444 as_warn (_("can't close %s: %s"), name
, xstrerror (errno
));
1448 fclose (last_open_file
);
1452 listing_file (const char *name
)
1458 listing_eject (int ignore ATTRIBUTE_UNUSED
)
1461 listing_tail
->edict
= EDICT_EJECT
;
1464 /* Turn listing on or off. An argument of 0 means to turn off
1465 listing. An argument of 1 means to turn on listing. An argument
1466 of 2 means to turn off listing, but as of the next line; that is,
1467 the current line should be listed, but the next line should not. */
1470 listing_list (int on
)
1477 if (listing_tail
->edict
== EDICT_LIST
)
1478 listing_tail
->edict
= EDICT_NONE
;
1480 listing_tail
->edict
= EDICT_NOLIST
;
1483 if (listing_tail
->edict
== EDICT_NOLIST
1484 || listing_tail
->edict
== EDICT_NOLIST_NEXT
)
1485 listing_tail
->edict
= EDICT_NONE
;
1487 listing_tail
->edict
= EDICT_LIST
;
1490 listing_tail
->edict
= EDICT_NOLIST_NEXT
;
1499 listing_psize (int width_only
)
1503 paper_height
= get_absolute_expression ();
1505 if (paper_height
< 0 || paper_height
> 1000)
1508 as_warn (_("strange paper height, set to no form"));
1511 if (*input_line_pointer
!= ',')
1513 demand_empty_rest_of_line ();
1517 ++input_line_pointer
;
1523 (void) expression_and_evaluate (& exp
);
1525 if (exp
.X_op
== O_constant
)
1527 offsetT new_width
= exp
.X_add_number
;
1530 paper_width
= new_width
;
1532 as_bad (_("new paper width is too small"));
1534 else if (exp
.X_op
!= O_absent
)
1535 as_bad (_("bad or irreducible expression for paper width"));
1537 as_bad (_("missing expression for paper width"));
1540 demand_empty_rest_of_line ();
1544 listing_nopage (int ignore ATTRIBUTE_UNUSED
)
1550 listing_title (int depth
)
1555 unsigned int length
;
1558 if (*input_line_pointer
!= '\"')
1563 ++input_line_pointer
;
1566 start
= input_line_pointer
;
1568 while (*input_line_pointer
)
1571 ? *input_line_pointer
== '\"'
1572 : is_end_of_line
[(unsigned char) *input_line_pointer
])
1576 length
= input_line_pointer
- start
;
1577 ttl
= xmemdup0 (start
, length
);
1578 listing_tail
->edict
= depth
? EDICT_SBTTL
: EDICT_TITLE
;
1579 listing_tail
->edict_arg
= ttl
;
1582 input_line_pointer
++;
1583 demand_empty_rest_of_line ();
1586 else if (*input_line_pointer
== '\n')
1588 as_bad (_("new line in title"));
1589 demand_empty_rest_of_line ();
1594 input_line_pointer
++;
1600 listing_source_line (unsigned int line
)
1605 listing_tail
->hll_line
= line
;
1611 listing_source_file (const char *file
)
1614 listing_tail
->hll_file
= file_info (file
);
1619 /* Dummy functions for when compiled without listing enabled. */
1622 listing_list (int on
)
1628 listing_eject (int ignore
)
1634 listing_psize (int ignore
)
1640 listing_nopage (int ignore
)
1646 listing_title (int depth
)
1652 listing_file (const char *name
)
1657 listing_newline (char *name
)
1662 listing_source_line (unsigned int n
)
1667 listing_source_file (const char *n
)