1 /* listing.c - mainting assembly listings
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 Contributed by Steve Chamberlain
27 A listing page looks like:
29 LISTING_HEADER sourcefilename pagenumber
32 linenumber address data source
33 linenumber address data source
34 linenumber address data source
35 linenumber address data source
37 If not overridden, the listing commands are:
40 Put "stuff" onto the title line
42 Put stuff onto the subtitle line
44 If these commands come within 10 lines of the top of the page, they
45 will affect the page they are on, as well as any subsequent page
50 Increment the enable listing counter
52 Decrement the enable listing counter
55 Set the paper size to X wide and Y high. Setting a psize Y of
56 zero will suppress form feeds except where demanded by .eject
58 If the counter goes below zero, listing is suppressed.
61 Listings are a maintained by read calling various listing_<foo>
62 functions. What happens most is that the macro NO_LISTING is not
63 defined (from the Makefile), then the macro LISTING_NEWLINE expands
64 into a call to listing_newline. The call is done from read.c, every
65 time it sees a newline, and -l is on the command line.
67 The function listing_newline remembers the frag associated with the
68 newline, and creates a new frag - note that this is wasteful, but not
69 a big deal, since listing slows things down a lot anyway. The
70 function also rememebers when the filename changes.
72 When all the input has finished, and gas has had a chance to settle
73 down, the listing is output. This is done by running down the list of
74 frag/source file records, and opening the files as needed and printing
75 out the bytes and chars associated with them.
77 The only things which the architecture can change about the listing
78 are defined in these macros:
80 LISTING_HEADER The name of the architecture
81 LISTING_WORD_SIZE The make of the number of bytes in a word, this determines
82 the clumping of the output data. eg a value of
83 2 makes words look like 1234 5678, whilst 1
84 would make the same value look like 12 34 56
86 LISTING_LHS_WIDTH Number of words of above size for the lhs
88 LISTING_LHS_WIDTH_SECOND Number of words for the data on the lhs
91 LISTING_LHS_CONT_LINES Max number of lines to use up for a continutation
92 LISTING_RHS_WIDTH Number of chars from the input file to print
100 #include "input-file.h"
105 #ifndef LISTING_HEADER
106 #define LISTING_HEADER "GAS LISTING"
108 #ifndef LISTING_WORD_SIZE
109 #define LISTING_WORD_SIZE 4
111 #ifndef LISTING_LHS_WIDTH
112 #define LISTING_LHS_WIDTH 1
114 #ifndef LISTING_LHS_WIDTH_SECOND
115 #define LISTING_LHS_WIDTH_SECOND 1
117 #ifndef LISTING_RHS_WIDTH
118 #define LISTING_RHS_WIDTH 100
120 #ifndef LISTING_LHS_CONT_LINES
121 #define LISTING_LHS_CONT_LINES 4
124 /* This structure remembers which .s were used */
125 typedef struct file_info_struct
127 struct file_info_struct * next;
130 unsigned int linenum;
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. */
149 char * line_contents;
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 */
158 unsigned int hll_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;
184 int listing_lhs_width = LISTING_LHS_WIDTH;
185 int listing_lhs_width_second = LISTING_LHS_WIDTH_SECOND;
186 int listing_lhs_cont_lines = LISTING_LHS_CONT_LINES;
187 int listing_rhs_width = LISTING_RHS_WIDTH;
189 struct list_info_struct * listing_tail;
191 static file_info_type * file_info_head;
192 static file_info_type * last_open_file_info;
193 static FILE * last_open_file;
194 static struct list_info_struct * head;
195 static int paper_width = 200;
196 static int paper_height = 60;
200 /* File to output listings to. */
201 static FILE * list_file;
203 /* This static array is used to keep the text of data to be printed
204 before the start of the line. */
207 (((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width \
208 + ((((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second) \
209 * listing_lhs_cont_lines) \
212 static char * data_buffer;
215 static void listing_message PARAMS ((const char *name, const char *message));
216 static file_info_type * file_info PARAMS ((const char *file_name));
217 static void new_frag PARAMS ((void));
218 static char * buffer_line PARAMS ((file_info_type *file,
219 char *line, unsigned int size));
220 static void listing_page PARAMS ((list_info_type *list));
221 static unsigned int calc_hex PARAMS ((list_info_type *list));
222 static void print_lines PARAMS ((list_info_type *, unsigned int,
223 char *, unsigned int));
224 static void list_symbol_table PARAMS ((void));
225 static void print_source PARAMS ((file_info_type *current_file,
226 list_info_type *list,
228 unsigned int width));
229 static int debugging_pseudo PARAMS ((list_info_type *, const char *));
230 static void listing_listing PARAMS ((char *name));
234 listing_message (name, message)
238 unsigned int l = strlen (name) + strlen (message) + 1;
239 char *n = (char *) xmalloc (l);
242 if (listing_tail != (list_info_type *) NULL)
244 listing_tail->message = n;
249 listing_warning (message)
252 listing_message (_("Warning:"), message);
256 listing_error (message)
259 listing_message (_("Error:"), message);
262 static file_info_type *
263 file_info (file_name)
264 const char *file_name;
266 /* Find an entry with this file name */
267 file_info_type *p = file_info_head;
269 while (p != (file_info_type *) NULL)
271 if (strcmp (p->filename, file_name) == 0)
278 p = (file_info_type *) xmalloc (sizeof (file_info_type));
279 p->next = file_info_head;
281 p->filename = xmalloc ((unsigned long) strlen (file_name) + 1);
282 strcpy (p->filename, file_name);
295 frag_wane (frag_now);
306 static unsigned int last_line = 0xffff;
307 static char *last_file = NULL;
308 list_info_type *new = NULL;
313 if (now_seg == absolute_section)
317 /* In ELF, anything in a section beginning with .debug or .line is
318 considered to be debugging information. This includes the
319 statement which switches us into the debugging section, which we
320 can only set after we are already in the debugging section. */
321 if ((listing & LISTING_NODEBUG) != 0
322 && listing_tail != NULL
323 && ! listing_tail->debugging)
327 segname = segment_name (now_seg);
328 if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0
329 || strncmp (segname, ".line", sizeof ".line" - 1) == 0)
330 listing_tail->debugging = 1;
334 as_where (&file, &line);
337 if (line == last_line && !(last_file && file && strcmp (file, last_file)))
340 new = (list_info_type *) xmalloc (sizeof (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 line
350 here (assuming of course that we actually have a line of input to read),
351 so that it can be displayed in the listing that is produced at the end
353 if (strcmp (file, _("{standard input}")) == 0
354 && input_line_pointer != NULL)
360 for (copy = input_line_pointer - 1;
361 * copy && (seen_quote || (! is_end_of_line [* copy]));
363 if (* copy == '"' && copy[-1] != '\')
364 seen_quote = ! seen_quote;
366 len = (copy - input_line_pointer) + 2;
368 copy = xmalloc (len);
372 char * src = input_line_pointer - 1;
379 /* Omit control characters in the listing. */
380 if (isascii (c) && ! iscntrl (c))
387 new->line_contents = copy;
390 new->line_contents = NULL;
394 new = (list_info_type *) xmalloc (sizeof (list_info_type));
395 new->line_contents = ps;
404 listing_tail->next = new;
410 new->frag = frag_now;
412 new->file = file_info (file);
413 new->next = (list_info_type *) NULL;
414 new->message = (char *) NULL;
415 new->edict = EDICT_NONE;
416 new->hll_file = (file_info_type *) NULL;
423 /* In ELF, anything in a section beginning with .debug or .line is
424 considered to be debugging information. */
425 if ((listing & LISTING_NODEBUG) != 0)
429 segname = segment_name (now_seg);
430 if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0
431 || strncmp (segname, ".line", sizeof ".line" - 1) == 0)
437 /* Attach all current frags to the previous line instead of the
438 current line. This is called by the MIPS backend when it discovers
439 that it needs to add some NOP instructions; the added NOP
440 instructions should go with the instruction that has the delay, not
441 with the new instruction. */
449 if (head == (list_info_type *) NULL
450 || head == listing_tail)
455 for (l = head; l->next != listing_tail; l = l->next)
458 for (f = frchain_now->frch_root; f != (fragS *) NULL; f = f->fr_next)
459 if (f->line == listing_tail)
462 listing_tail->frag = frag_now;
467 This function returns the next source line from the file supplied,
468 truncated to size. It appends a fake line to the end of each input
473 buffer_line (file, line, size)
474 file_info_type * file;
478 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 last_open_file_info = file;
497 last_open_file = fopen (file->filename, "r");
498 if (last_open_file == NULL)
504 /* Seek to where we were last time this file was open. */
506 fseek(last_open_file, file->pos, SEEK_SET);
509 c = fgetc (last_open_file);
511 size -= 1; /* leave room for null */
513 while (c != EOF && c != '\n')
519 c = fgetc (last_open_file);
535 static const char *fn;
537 static unsigned int eject; /* Eject pending */
538 static unsigned int page; /* Current page number */
539 static char *title; /* current title */
540 static char *subtitle; /* current subtitle */
541 static unsigned int on_page; /* number of lines printed on current page */
546 list_info_type *list;
548 /* Grope around, see if we can see a title or subtitle edict coming up
549 soon (we look down 10 lines of the page and see if it's there)*/
550 if ((eject || (on_page >= (unsigned int) paper_height)) && paper_height != 0)
554 int had_subtitle = 0;
558 while (c != 0 && list)
560 if (list->edict == EDICT_SBTTL && !had_subtitle)
563 subtitle = list->edict_arg;
565 if (list->edict == EDICT_TITLE && !had_title)
568 title = list->edict_arg;
577 fprintf (list_file, "\f");
580 fprintf (list_file, "%s %s \t\t\tpage %d\n", LISTING_HEADER, fn, page);
581 fprintf (list_file, "%s\n", title);
582 fprintf (list_file, "%s\n", subtitle);
591 list_info_type * list;
593 int data_buffer_size;
594 list_info_type *first = list;
595 unsigned int address = ~ (unsigned int) 0;
598 unsigned int byte_in_frag;
600 /* Find first frag which says it belongs to this line */
602 while (frag && frag->line != list)
603 frag = frag->fr_next;
607 data_buffer_size = 0;
609 /* Dump all the frags which belong to this line */
610 while (frag_ptr != (fragS *) NULL && frag_ptr->line == first)
612 /* Print as many bytes from the fixed part as is sensible */
614 while ((offsetT) byte_in_frag < frag_ptr->fr_fix
615 && data_buffer_size < MAX_BYTES - 3)
617 if (address == ~ (unsigned int) 0)
619 address = frag_ptr->fr_address;
622 sprintf (data_buffer + data_buffer_size,
624 (frag_ptr->fr_literal[byte_in_frag]) & 0xff);
625 data_buffer_size += 2;
629 unsigned int var_rep_max = byte_in_frag;
630 unsigned int var_rep_idx = byte_in_frag;
632 /* Print as many bytes from the variable part as is sensible */
633 while (((offsetT) byte_in_frag
634 < frag_ptr->fr_fix + frag_ptr->fr_var * frag_ptr->fr_offset)
635 && data_buffer_size < MAX_BYTES - 3)
637 if (address == ~ (unsigned int) 0)
639 address = frag_ptr->fr_address;
641 sprintf (data_buffer + data_buffer_size,
643 (frag_ptr->fr_literal[var_rep_idx]) & 0xff);
645 data_buffer[data_buffer_size++] = '*';
646 data_buffer[data_buffer_size++] = '*';
648 data_buffer_size += 2;
653 if ((offsetT) var_rep_idx >= frag_ptr->fr_fix + frag_ptr->fr_var)
654 var_rep_idx = var_rep_max;
658 frag_ptr = frag_ptr->fr_next;
660 data_buffer[data_buffer_size] = '\0';
670 print_lines (list, lineno, string, address)
671 list_info_type *list;
674 unsigned int address;
679 unsigned int byte_in_word = 0;
680 char *src = data_buffer;
682 /* Print the stuff on the first line */
684 nchars = (LISTING_WORD_SIZE * 2 + 1) * listing_lhs_width;
686 /* Print the hex for the first line */
687 if (address == ~ (unsigned int) 0)
689 fprintf (list_file, "% 4d ", lineno);
690 for (idx = 0; idx < nchars; idx++)
691 fprintf (list_file, " ");
693 fprintf (list_file, "\t%s\n", string ? string : "");
703 fprintf (list_file, "% 4d ???? ", lineno);
705 fprintf (list_file, "% 4d %04x ", lineno, address);
707 /* And the data to go along with it */
710 while (*src && idx < nchars)
712 fprintf (list_file, "%c%c", src[0], src[1]);
716 if (byte_in_word == LISTING_WORD_SIZE)
718 fprintf (list_file, " ");
726 for (; idx < nchars; idx++)
727 fprintf (list_file, " ");
729 fprintf (list_file, "\t%s\n", string ? string : "");
735 fprintf (list_file, "**** %s\n", list->message);
741 lines < (unsigned int) listing_lhs_cont_lines
745 nchars = ((LISTING_WORD_SIZE * 2) + 1)
746 * listing_lhs_width_second - 1;
749 /* Print any more lines of data, but more compactly */
750 fprintf (list_file, "% 4d ", lineno);
752 while (*src && idx < nchars)
754 fprintf (list_file, "%c%c", src[0], src[1]);
759 if (byte_in_word == LISTING_WORD_SIZE)
761 fprintf (list_file, " ");
767 fprintf (list_file, "\n");
777 extern symbolS *symbol_rootP;
784 for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
786 if (SEG_NORMAL (S_GET_SEGMENT (ptr))
787 || S_GET_SEGMENT (ptr) == absolute_section)
790 /* Don't report section symbols. They are not interesting. */
791 if (ptr->bsym->flags & BSF_SECTION_SYM)
794 if (S_GET_NAME (ptr))
796 char buf[30], fmt[8];
797 valueT val = S_GET_VALUE (ptr);
799 /* @@ Note that this is dependent on the compilation options,
800 not solely on the target characteristics. */
801 if (sizeof (val) == 4 && sizeof (int) == 4)
802 sprintf (buf, "%08lx", (unsigned long) val);
803 else if (sizeof (val) <= sizeof (unsigned long))
805 sprintf (fmt, "%%0%lulx",
806 (unsigned long) (sizeof (val) * 2));
807 sprintf (buf, fmt, (unsigned long) val);
810 else if (sizeof (val) > 4)
811 sprintf_vma (buf, val);
818 fprintf (list_file, "DEFINED SYMBOLS\n");
823 if (ptr->sy_frag && ptr->sy_frag->line)
825 fprintf (list_file, "%20s:%-5d %s:%s %s\n",
826 ptr->sy_frag->line->file->filename,
827 ptr->sy_frag->line->line,
828 segment_name (S_GET_SEGMENT (ptr)),
829 buf, S_GET_NAME (ptr));
833 fprintf (list_file, "%33s:%s %s\n",
834 segment_name (S_GET_SEGMENT (ptr)),
835 buf, S_GET_NAME (ptr));
846 fprintf (list_file, "NO DEFINED SYMBOLS\n");
849 fprintf (list_file, "\n");
855 for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
857 if (S_GET_NAME (ptr) && strlen (S_GET_NAME (ptr)) != 0)
859 if (S_GET_SEGMENT (ptr) == undefined_section)
864 fprintf (list_file, "UNDEFINED SYMBOLS\n");
868 fprintf (list_file, "%s\n", S_GET_NAME (ptr));
876 fprintf (list_file, "NO UNDEFINED SYMBOLS\n");
883 print_source (current_file, list, buffer, width)
884 file_info_type *current_file;
885 list_info_type *list;
889 if (!current_file->at_end)
891 while (current_file->linenum < list->hll_line
892 && !current_file->at_end)
894 char *p = buffer_line (current_file, buffer, width);
895 fprintf (list_file, "%4u:%-13s **** %s\n", current_file->linenum,
896 current_file->filename, p);
903 /* Sometimes the user doesn't want to be bothered by the debugging
904 records inserted by the compiler, see if the line is suspicious. */
907 debugging_pseudo (list, line)
908 list_info_type *list;
920 was_debug = in_debug;
923 while (isspace ((unsigned char) *line))
929 /* The ELF compiler sometimes emits blank lines after switching
930 out of a debugging section. If the next line drops us back
931 into debugging information, then don't print the blank line.
932 This is a hack for a particular compiler behaviour, not a
936 && list->next != NULL
937 && list->next->debugging)
949 if (strncmp (line, "def", 3) == 0)
951 if (strncmp (line, "val", 3) == 0)
953 if (strncmp (line, "scl", 3) == 0)
955 if (strncmp (line, "line", 4) == 0)
957 if (strncmp (line, "endef", 5) == 0)
959 if (strncmp (line, "ln", 2) == 0)
961 if (strncmp (line, "type", 4) == 0)
963 if (strncmp (line, "size", 4) == 0)
965 if (strncmp (line, "dim", 3) == 0)
967 if (strncmp (line, "tag", 3) == 0)
970 if (strncmp (line, "stabs", 5) == 0)
972 if (strncmp (line, "stabn", 5) == 0)
979 listing_listing (name)
982 list_info_type *list = head;
983 file_info_type *current_hll_file = (file_info_type *) NULL;
987 int show_listing = 1;
990 buffer = xmalloc (listing_rhs_width);
991 data_buffer = xmalloc (MAX_BYTES);
995 while (list != (list_info_type *) NULL && 0)
998 list->frag = list->next->frag;
1010 width = listing_rhs_width > paper_width ? paper_width :
1013 list_line = list->line;
1014 switch (list->edict)
1017 /* Skip all lines up to the current. */
1023 case EDICT_NOLIST_NEXT:
1030 title = list->edict_arg;
1033 subtitle = list->edict_arg;
1039 if (show_listing <= 0)
1041 while (list->file->linenum < list_line
1042 && !list->file->at_end)
1043 p = buffer_line (list->file, buffer, width);
1046 if (list->edict == EDICT_LIST)
1048 /* Enable listing for the single line that caused the enable. */
1053 if (show_listing > 0)
1055 /* Scan down the list and print all the stuff which can be done
1056 with this line (or lines). */
1061 current_hll_file = list->hll_file;
1064 if (current_hll_file && list->hll_line && (listing & LISTING_HLL))
1066 print_source (current_hll_file, list, buffer, width);
1069 if (list->line_contents)
1071 if (!((listing & LISTING_NODEBUG)
1072 && debugging_pseudo (list, list->line_contents)))
1075 list->file->linenum == 0 ? list->line : list->file->linenum,
1076 list->line_contents, calc_hex (list));
1078 free (list->line_contents);
1079 list->line_contents = NULL;
1083 while (list->file->linenum < list_line
1084 && !list->file->at_end)
1086 unsigned int address;
1088 p = buffer_line (list->file, buffer, width);
1090 if (list->file->linenum < list_line)
1091 address = ~ (unsigned int) 0;
1093 address = calc_hex (list);
1095 if (!((listing & LISTING_NODEBUG)
1096 && debugging_pseudo (list, p)))
1097 print_lines (list, list->file->linenum, p, address);
1101 if (list->edict == EDICT_EJECT)
1107 if (list->edict == EDICT_NOLIST_NEXT)
1119 listing_print (name)
1134 list_file = fopen (name, "w");
1135 if (list_file != NULL)
1139 as_perror (_("can't open list file: %s"), name);
1145 if (listing & LISTING_NOFORM)
1150 if (listing & LISTING_LISTING)
1152 listing_listing (name);
1155 if (listing & LISTING_SYMBOLS)
1157 list_symbol_table ();
1162 if (fclose (list_file) == EOF)
1163 as_perror (_("error closing list file: %s"), name);
1168 fclose (last_open_file);
1181 listing_eject (ignore)
1185 listing_tail->edict = EDICT_EJECT;
1189 listing_flags (ignore)
1192 while ((*input_line_pointer++) && (*input_line_pointer != '\n'))
1193 input_line_pointer++;
1197 /* Turn listing on or off. An argument of 0 means to turn off
1198 listing. An argument of 1 means to turn on listing. An argument
1199 of 2 means to turn off listing, but as of the next line; that is,
1200 the current line should be listed, but the next line should not. */
1211 if (listing_tail->edict == EDICT_LIST)
1212 listing_tail->edict = EDICT_NONE;
1214 listing_tail->edict = EDICT_NOLIST;
1217 if (listing_tail->edict == EDICT_NOLIST
1218 || listing_tail->edict == EDICT_NOLIST_NEXT)
1219 listing_tail->edict = EDICT_NONE;
1221 listing_tail->edict = EDICT_LIST;
1224 listing_tail->edict = EDICT_NOLIST_NEXT;
1234 listing_psize (width_only)
1239 paper_height = get_absolute_expression ();
1241 if (paper_height < 0 || paper_height > 1000)
1244 as_warn (_("strange paper height, set to no form"));
1247 if (*input_line_pointer != ',')
1249 demand_empty_rest_of_line ();
1253 ++input_line_pointer;
1256 paper_width = get_absolute_expression ();
1258 demand_empty_rest_of_line ();
1262 listing_nopage (ignore)
1269 listing_title (depth)
1275 unsigned int length;
1278 if (*input_line_pointer != '\"')
1283 ++input_line_pointer;
1286 start = input_line_pointer;
1288 while (*input_line_pointer)
1291 ? *input_line_pointer == '\"'
1292 : is_end_of_line[(unsigned char) *input_line_pointer])
1296 length = input_line_pointer - start;
1297 ttl = xmalloc (length + 1);
1298 memcpy (ttl, start, length);
1300 listing_tail->edict = depth ? EDICT_SBTTL : EDICT_TITLE;
1301 listing_tail->edict_arg = ttl;
1304 input_line_pointer++;
1305 demand_empty_rest_of_line ();
1308 else if (*input_line_pointer == '\n')
1310 as_bad (_("New line in title"));
1311 demand_empty_rest_of_line ();
1316 input_line_pointer++;
1324 listing_source_line (line)
1330 listing_tail->hll_line = line;
1336 listing_source_file (file)
1340 listing_tail->hll_file = file_info (file);
1348 /* Dummy functions for when compiled without listing enabled */
1351 listing_flags (ignore)
1365 listing_eject (ignore)
1372 listing_psize (ignore)
1379 listing_nopage (ignore)
1386 listing_title (depth)
1400 listing_newline (name)
1407 listing_source_line (n)
1413 listing_source_file (n)