1 /* listing.c - mainting assembly listings
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 1997 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 2, 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, 59 Temple Place - Suite 330, Boston, MA
22 Contributed by Steve Chamberlain
26 A listing page looks like:
28 LISTING_HEADER sourcefilename pagenumber
31 linenumber address data source
32 linenumber address data source
33 linenumber address data source
34 linenumber address data source
36 If not overridden, the listing commands are:
39 Put "stuff" onto the title line
41 Put stuff onto the subtitle line
43 If these commands come within 10 lines of the top of the page, they
44 will affect the page they are on, as well as any subsequent page
49 Increment the enable listing counter
51 Decrement the enable listing counter
54 Set the paper size to X wide and Y high. Setting a psize Y of
55 zero will suppress form feeds except where demanded by .eject
57 If the counter goes below zero, listing is suppressed.
60 Listings are a maintained by read calling various listing_<foo>
61 functions. What happens most is that the macro NO_LISTING is not
62 defined (from the Makefile), then the macro LISTING_NEWLINE expands
63 into a call to listing_newline. The call is done from read.c, every
64 time it sees a newline, and -l is on the command line.
66 The function listing_newline remembers the frag associated with the
67 newline, and creates a new frag - note that this is wasteful, but not
68 a big deal, since listing slows things down a lot anyway. The
69 function also rememebers when the filename changes.
71 When all the input has finished, and gas has had a chance to settle
72 down, the listing is output. This is done by running down the list of
73 frag/source file records, and opening the files as needed and printing
74 out the bytes and chars associated with them.
76 The only things which the architecture can change about the listing
77 are defined in these macros:
79 LISTING_HEADER The name of the architecture
80 LISTING_WORD_SIZE The make of the number of bytes in a word, this determines
81 the clumping of the output data. eg a value of
82 2 makes words look like 1234 5678, whilst 1
83 would make the same value look like 12 34 56
85 LISTING_LHS_WIDTH Number of words of above size for the lhs
87 LISTING_LHS_WIDTH_SECOND Number of words for the data on the lhs
90 LISTING_LHS_CONT_LINES Max number of lines to use up for a continutation
91 LISTING_RHS_WIDTH Number of chars from the input file to print
99 #include "input-file.h"
104 #ifndef LISTING_HEADER
105 #define LISTING_HEADER "GAS LISTING"
107 #ifndef LISTING_WORD_SIZE
108 #define LISTING_WORD_SIZE 4
110 #ifndef LISTING_LHS_WIDTH
111 #define LISTING_LHS_WIDTH 1
113 #ifndef LISTING_LHS_WIDTH_SECOND
114 #define LISTING_LHS_WIDTH_SECOND 1
116 #ifndef LISTING_RHS_WIDTH
117 #define LISTING_RHS_WIDTH 100
119 #ifndef LISTING_LHS_CONT_LINES
120 #define LISTING_LHS_CONT_LINES 4
123 /* This structure remembers which .s were used */
124 typedef struct file_info_struct
126 struct file_info_struct *next;
135 /* this structure rememebrs which line from which file goes into which
137 struct list_info_struct
139 /* Frag which this line of source is nearest to */
142 /* The actual line in the source file */
144 /* Pointer to the file info struct for the file which this line
146 file_info_type *file;
148 /* The expanded text of any macro that may have been executing. */
152 struct list_info_struct *next;
154 /* Pointer to the file info struct for the high level language
155 source line that belongs here */
156 file_info_type *hll_file;
157 /* High level language source line */
160 /* Pointer to any error message associated with this line */
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. */
181 typedef struct list_info_struct list_info_type;
183 static struct list_info_struct *head;
184 struct list_info_struct *listing_tail;
187 static int paper_width = 200;
188 static int paper_height = 60;
190 /* File to output listings to. */
191 static FILE *list_file;
193 /* This static array is used to keep the text of data to be printed
194 before the start of the line. */
197 (((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width \
198 + ((((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second) \
199 * listing_lhs_cont_lines) \
202 static char *data_buffer;
205 static void listing_message PARAMS ((const char *name, const char *message));
206 static file_info_type *file_info PARAMS ((const char *file_name));
207 static void new_frag PARAMS ((void));
208 static char *buffer_line PARAMS ((file_info_type *file,
209 char *line, unsigned int size));
210 static void listing_page PARAMS ((list_info_type *list));
211 static unsigned int calc_hex PARAMS ((list_info_type *list));
212 static void print_lines PARAMS ((list_info_type *, unsigned int,
213 char *, unsigned int));
214 static void list_symbol_table PARAMS ((void));
215 static void print_source PARAMS ((file_info_type *current_file,
216 list_info_type *list,
218 unsigned int width));
219 static int debugging_pseudo PARAMS ((list_info_type *, const char *));
220 static void listing_listing PARAMS ((char *name));
224 listing_message (name, message)
228 unsigned int l = strlen (name) + strlen (message) + 1;
229 char *n = (char *) xmalloc (l);
232 if (listing_tail != (list_info_type *) NULL)
234 listing_tail->message = n;
239 listing_warning (message)
242 listing_message ("Warning:", message);
246 listing_error (message)
249 listing_message ("Error:", message);
253 int listing_lhs_width = LISTING_LHS_WIDTH;
254 int listing_lhs_width_second = LISTING_LHS_WIDTH_SECOND;
255 int listing_lhs_cont_lines = LISTING_LHS_CONT_LINES;
256 int listing_rhs_width = LISTING_RHS_WIDTH;
259 static file_info_type *file_info_head;
260 static file_info_type *last_open_file_info;
261 static FILE *last_open_file;
263 static file_info_type *
264 file_info (file_name)
265 const char *file_name;
267 /* Find an entry with this file name */
268 file_info_type *p = file_info_head;
270 while (p != (file_info_type *) NULL)
272 if (strcmp (p->filename, file_name) == 0)
279 p = (file_info_type *) xmalloc (sizeof (file_info_type));
280 p->next = file_info_head;
282 p->filename = xmalloc ((unsigned long) strlen (file_name) + 1);
283 strcpy (p->filename, file_name);
296 frag_wane (frag_now);
307 static unsigned int last_line = 0xffff;
308 static char *last_file = NULL;
309 list_info_type *new = NULL;
314 if (now_seg == absolute_section)
318 /* In ELF, anything in a section beginning with .debug or .line is
319 considered to be debugging information. This includes the
320 statement which switches us into the debugging section, which we
321 can only set after we are already in the debugging section. */
322 if ((listing & LISTING_NODEBUG) != 0
323 && listing_tail != NULL
324 && ! listing_tail->debugging)
328 segname = segment_name (now_seg);
329 if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0
330 || strncmp (segname, ".line", sizeof ".line" - 1) == 0)
331 listing_tail->debugging = 1;
335 as_where (&file, &line);
338 if (line == last_line && !(last_file && file && strcmp(file, last_file)))
341 new = (list_info_type *) xmalloc (sizeof (list_info_type));
342 new->line_contents = NULL;
346 new = (list_info_type *) xmalloc (sizeof (list_info_type));
347 new->line_contents = ps;
356 listing_tail->next = new;
364 new->frag = frag_now;
366 new->file = file_info (file);
367 new->next = (list_info_type *) NULL;
368 new->message = (char *) NULL;
369 new->edict = EDICT_NONE;
370 new->hll_file = (file_info_type *) NULL;
376 /* In ELF, anything in a section beginning with .debug or .line is
377 considered to be debugging information. */
378 if ((listing & LISTING_NODEBUG) != 0)
382 segname = segment_name (now_seg);
383 if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0
384 || strncmp (segname, ".line", sizeof ".line" - 1) == 0)
390 /* Attach all current frags to the previous line instead of the
391 current line. This is called by the MIPS backend when it discovers
392 that it needs to add some NOP instructions; the added NOP
393 instructions should go with the instruction that has the delay, not
394 with the new instruction. */
402 if (head == (list_info_type *) NULL
403 || head == listing_tail)
408 for (l = head; l->next != listing_tail; l = l->next)
411 for (f = frchain_now->frch_root; f != (fragS *) NULL; f = f->fr_next)
412 if (f->line == listing_tail)
415 listing_tail->frag = frag_now;
420 This function returns the next source line from the file supplied,
421 truncated to size. It appends a fake line to the end of each input
426 buffer_line (file, line, size)
427 file_info_type * file;
431 unsigned int count = 0;
436 /* If we couldn't open the file, return an empty line */
440 /* Check the cache and see if we last used this file. */
441 if (!last_open_file_info || file != last_open_file_info)
445 last_open_file_info->pos = ftell (last_open_file);
446 fclose (last_open_file);
449 last_open_file_info = file;
450 last_open_file = fopen (file->filename, "r");
454 /* Seek to where we were last time this file was open. */
456 fseek(last_open_file, file->pos, SEEK_SET);
459 c = fgetc (last_open_file);
461 size -= 1; /* leave room for null */
463 while (c != EOF && c != '\n')
469 c = fgetc (last_open_file);
485 static const char *fn;
487 static unsigned int eject; /* Eject pending */
488 static unsigned int page; /* Current page number */
489 static char *title; /* current title */
490 static char *subtitle; /* current subtitle */
491 static unsigned int on_page; /* number of lines printed on current page */
496 list_info_type *list;
498 /* Grope around, see if we can see a title or subtitle edict coming up
499 soon (we look down 10 lines of the page and see if it's there)*/
500 if ((eject || (on_page >= paper_height)) && paper_height != 0)
504 int had_subtitle = 0;
508 while (c != 0 && list)
510 if (list->edict == EDICT_SBTTL && !had_subtitle)
513 subtitle = list->edict_arg;
515 if (list->edict == EDICT_TITLE && !had_title)
518 title = list->edict_arg;
527 fprintf (list_file, "\f");
530 fprintf (list_file, "%s %s \t\t\tpage %d\n", LISTING_HEADER, fn, page);
531 fprintf (list_file, "%s\n", title);
532 fprintf (list_file, "%s\n", subtitle);
541 list_info_type * list;
543 int data_buffer_size;
544 list_info_type *first = list;
545 unsigned int address = (unsigned int) ~0;
548 unsigned int byte_in_frag;
550 /* Find first frag which says it belongs to this line */
552 while (frag && frag->line != list)
553 frag = frag->fr_next;
557 data_buffer_size = 0;
559 /* Dump all the frags which belong to this line */
560 while (frag_ptr != (fragS *) NULL && frag_ptr->line == first)
562 /* Print as many bytes from the fixed part as is sensible */
564 while (byte_in_frag < frag_ptr->fr_fix
565 && data_buffer_size < MAX_BYTES - 3)
569 address = frag_ptr->fr_address;
572 sprintf (data_buffer + data_buffer_size,
574 (frag_ptr->fr_literal[byte_in_frag]) & 0xff);
575 data_buffer_size += 2;
579 unsigned int var_rep_max = byte_in_frag;
580 unsigned int var_rep_idx = byte_in_frag;
582 /* Print as many bytes from the variable part as is sensible */
584 < frag_ptr->fr_fix + frag_ptr->fr_var * frag_ptr->fr_offset)
585 && data_buffer_size < MAX_BYTES - 3)
589 address = frag_ptr->fr_address;
591 sprintf (data_buffer + data_buffer_size,
593 (frag_ptr->fr_literal[var_rep_idx]) & 0xff);
595 data_buffer[data_buffer_size++] = '*';
596 data_buffer[data_buffer_size++] = '*';
598 data_buffer_size += 2;
603 if (var_rep_idx >= frag_ptr->fr_fix + frag_ptr->fr_var)
604 var_rep_idx = var_rep_max;
608 frag_ptr = frag_ptr->fr_next;
610 data_buffer[data_buffer_size] = '\0';
620 print_lines (list, lineno, string, address)
621 list_info_type *list;
624 unsigned int address;
629 unsigned int byte_in_word = 0;
630 char *src = data_buffer;
632 /* Print the stuff on the first line */
634 nchars = (LISTING_WORD_SIZE * 2 + 1) * listing_lhs_width;
635 /* Print the hex for the first line */
638 fprintf (list_file, "% 4d ", lineno);
639 for (idx = 0; idx < nchars; idx++)
640 fprintf (list_file, " ");
642 fprintf (list_file, "\t%s\n", string ? string : "");
651 fprintf (list_file, "% 4d ???? ", lineno);
655 fprintf (list_file, "% 4d %04x ", lineno, address);
658 /* And the data to go along with it */
661 while (*src && idx < nchars)
663 fprintf (list_file, "%c%c", src[0], src[1]);
666 if (byte_in_word == LISTING_WORD_SIZE)
668 fprintf (list_file, " ");
675 for (; idx < nchars; idx++)
676 fprintf (list_file, " ");
678 fprintf (list_file, "\t%s\n", string ? string : "");
683 fprintf (list_file, "**** %s\n", list->message);
688 for (lines = 0; lines < listing_lhs_cont_lines && *src; lines++)
690 nchars = (((LISTING_WORD_SIZE * 2) + 1)
691 * listing_lhs_width_second - 1);
693 /* Print any more lines of data, but more compactly */
694 fprintf (list_file, "% 4d ", lineno);
696 while (*src && idx < nchars)
698 fprintf (list_file, "%c%c", src[0], src[1]);
702 if (byte_in_word == LISTING_WORD_SIZE)
704 fprintf (list_file, " ");
710 fprintf (list_file, "\n");
724 extern symbolS *symbol_rootP;
731 for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
733 if (ptr->sy_frag->line)
735 if (S_GET_NAME (ptr))
737 char buf[30], fmt[8];
738 valueT val = S_GET_VALUE (ptr);
740 /* @@ Note that this is dependent on the compilation options,
741 not solely on the target characteristics. */
742 if (sizeof (val) == 4 && sizeof (int) == 4)
743 sprintf (buf, "%08lx", (unsigned long) val);
744 else if (sizeof (val) <= sizeof (unsigned long))
746 sprintf (fmt, "%%0%lulx",
747 (unsigned long) (sizeof (val) * 2));
748 sprintf (buf, fmt, (unsigned long) val);
751 else if (sizeof (val) > 4)
752 sprintf_vma (buf, val);
759 fprintf (list_file, "DEFINED SYMBOLS\n");
764 fprintf (list_file, "%20s:%-5d %s:%s %s\n",
765 ptr->sy_frag->line->file->filename,
766 ptr->sy_frag->line->line,
767 segment_name (S_GET_SEGMENT (ptr)),
768 buf, S_GET_NAME (ptr));
778 fprintf (list_file, "NO DEFINED SYMBOLS\n");
781 fprintf (list_file, "\n");
787 for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
789 if (S_GET_NAME (ptr) && strlen (S_GET_NAME (ptr)) != 0)
791 if (ptr->sy_frag->line == 0
793 && !S_IS_REGISTER (ptr)
795 && S_GET_SEGMENT (ptr) != reg_section)
800 fprintf (list_file, "UNDEFINED SYMBOLS\n");
804 fprintf (list_file, "%s\n", S_GET_NAME (ptr));
812 fprintf (list_file, "NO UNDEFINED SYMBOLS\n");
819 print_source (current_file, list, buffer, width)
820 file_info_type *current_file;
821 list_info_type *list;
825 if (!current_file->at_end)
827 while (current_file->linenum < list->hll_line
828 && !current_file->at_end)
830 char *p = buffer_line (current_file, buffer, width);
831 fprintf (list_file, "%4d:%-13s **** %s\n", current_file->linenum,
832 current_file->filename, p);
839 /* Sometimes the user doesn't want to be bothered by the debugging
840 records inserted by the compiler, see if the line is suspicious. */
843 debugging_pseudo (list, line)
844 list_info_type *list;
856 was_debug = in_debug;
859 while (isspace (*line))
865 /* The ELF compiler sometimes emits blank lines after switching
866 out of a debugging section. If the next line drops us back
867 into debugging information, then don't print the blank line.
868 This is a hack for a particular compiler behaviour, not a
872 && list->next != NULL
873 && list->next->debugging)
885 if (strncmp (line, "def", 3) == 0)
887 if (strncmp (line, "val", 3) == 0)
889 if (strncmp (line, "scl", 3) == 0)
891 if (strncmp (line, "line", 4) == 0)
893 if (strncmp (line, "endef", 5) == 0)
895 if (strncmp (line, "ln", 2) == 0)
897 if (strncmp (line, "type", 4) == 0)
899 if (strncmp (line, "size", 4) == 0)
901 if (strncmp (line, "dim", 3) == 0)
903 if (strncmp (line, "tag", 3) == 0)
906 if (strncmp (line, "stabs", 5) == 0)
908 if (strncmp (line, "stabn", 5) == 0)
915 listing_listing (name)
918 list_info_type *list = head;
919 file_info_type *current_hll_file = (file_info_type *) NULL;
923 int show_listing = 1;
926 buffer = xmalloc (listing_rhs_width);
927 data_buffer = xmalloc (MAX_BYTES);
931 while (list != (list_info_type *) NULL && 0)
934 list->frag = list->next->frag;
944 width = listing_rhs_width > paper_width ? paper_width :
955 case EDICT_NOLIST_NEXT:
962 title = list->edict_arg;
965 subtitle = list->edict_arg;
971 if (show_listing > 0)
973 /* Scan down the list and print all the stuff which can be done
974 with this line (or lines). */
979 current_hll_file = list->hll_file;
982 if (current_hll_file && list->hll_line && (listing & LISTING_HLL))
984 print_source (current_hll_file, list, buffer, width);
987 if (list->line_contents)
989 if (!((listing & LISTING_NODEBUG)
990 && debugging_pseudo (list, list->line_contents)))
992 print_lines (list, list->file->linenum,
993 list->line_contents, calc_hex (list));
998 while (list->file->linenum < list->line
999 && !list->file->at_end)
1001 unsigned int address;
1003 p = buffer_line (list->file, buffer, width);
1005 if (list->file->linenum < list->line)
1006 address = ~ (unsigned int) 0;
1008 address = calc_hex (list);
1010 if (!((listing & LISTING_NODEBUG)
1011 && debugging_pseudo (list, p)))
1012 print_lines (list, list->file->linenum, p, address);
1016 if (list->edict == EDICT_EJECT)
1023 while (list->file->linenum < list->line
1024 && !list->file->at_end)
1025 p = buffer_line (list->file, buffer, width);
1028 if (list->edict == EDICT_NOLIST_NEXT)
1040 listing_print (name)
1056 list_file = fopen (name, "w");
1057 if (list_file != NULL)
1061 as_perror ("can't open list file: %s", name);
1067 if (listing & LISTING_NOFORM)
1072 if (listing & LISTING_LISTING)
1074 listing_listing (name);
1077 if (listing & LISTING_SYMBOLS)
1079 list_symbol_table ();
1084 if (fclose (list_file) == EOF)
1085 as_perror ("error closing list file: %s", name);
1090 fclose (last_open_file);
1103 listing_eject (ignore)
1107 listing_tail->edict = EDICT_EJECT;
1111 listing_flags (ignore)
1114 while ((*input_line_pointer++) && (*input_line_pointer != '\n'))
1115 input_line_pointer++;
1119 /* Turn listing on or off. An argument of 0 means to turn off
1120 listing. An argument of 1 means to turn on listing. An argument
1121 of 2 means to turn off listing, but as of the next line; that is,
1122 the current line should be listed, but the next line should not. */
1133 if (listing_tail->edict == EDICT_LIST)
1134 listing_tail->edict = EDICT_NONE;
1136 listing_tail->edict = EDICT_NOLIST;
1139 if (listing_tail->edict == EDICT_NOLIST
1140 || listing_tail->edict == EDICT_NOLIST_NEXT)
1141 listing_tail->edict = EDICT_NONE;
1143 listing_tail->edict = EDICT_LIST;
1146 listing_tail->edict = EDICT_NOLIST_NEXT;
1156 listing_psize (width_only)
1161 paper_height = get_absolute_expression ();
1163 if (paper_height < 0 || paper_height > 1000)
1166 as_warn ("strange paper height, set to no form");
1169 if (*input_line_pointer != ',')
1171 demand_empty_rest_of_line ();
1175 ++input_line_pointer;
1178 paper_width = get_absolute_expression ();
1180 demand_empty_rest_of_line ();
1184 listing_nopage (ignore)
1191 listing_title (depth)
1197 unsigned int length;
1200 if (*input_line_pointer != '\"')
1205 ++input_line_pointer;
1208 start = input_line_pointer;
1210 while (*input_line_pointer)
1213 ? *input_line_pointer == '\"'
1214 : is_end_of_line[(unsigned char) *input_line_pointer])
1218 length = input_line_pointer - start;
1219 ttl = xmalloc (length + 1);
1220 memcpy (ttl, start, length);
1222 listing_tail->edict = depth ? EDICT_SBTTL : EDICT_TITLE;
1223 listing_tail->edict_arg = ttl;
1226 input_line_pointer++;
1227 demand_empty_rest_of_line ();
1230 else if (*input_line_pointer == '\n')
1232 as_bad ("New line in title");
1233 demand_empty_rest_of_line ();
1238 input_line_pointer++;
1246 listing_source_line (line)
1252 listing_tail->hll_line = line;
1258 listing_source_file (file)
1262 listing_tail->hll_file = file_info (file);
1270 /* Dummy functions for when compiled without listing enabled */
1273 listing_flags (ignore)
1287 listing_eject (ignore)
1294 listing_psize (ignore)
1301 listing_nopage (ignore)
1308 listing_title (depth)
1322 listing_newline (name)
1329 listing_source_line (n)
1335 listing_source_file (n)