1 /* listing.c - mainting assembly listings
2 Copyright (C) 1991, 1992 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
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 Contributed by Steve Chamberlain
25 A listing page looks like:
27 LISTING_HEADER sourcefilename pagenumber
30 linenumber address data source
31 linenumber address data source
32 linenumber address data source
33 linenumber address data source
35 If not overridden, the listing commands are:
38 Put "stuff" onto the title line
40 Put stuff onto the subtitle line
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
48 Increment the enable listing counter
50 Decrement the enable listing counter
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
56 If the counter goes below zero, listing is suppressed.
59 Listings are a maintained by read calling various listing_<foo>
60 functions. What happens most is that the macro NO_LISTING is not
61 defined (from the Makefile), then the macro LISTING_NEWLINE expands
62 into a call to listing_newline. The call is done from read.c, every
63 time it sees a newline, and -l is on the command line.
65 The function listing_newline remembers the frag associated with the
66 newline, and creates a new frag - note that this is wasteful, but not
67 a big deal, since listing slows things down a lot anyway. The
68 function also rememebers when the filename changes.
70 When all the input has finished, and gas has had a chance to settle
71 down, the listing is output. This is done by running down the list of
72 frag/source file records, and opening the files as needed and printing
73 out the bytes and chars associated with them.
75 The only things which the architecture can change about the listing
76 are defined in these macros:
78 LISTING_HEADER The name of the architecture
79 LISTING_WORD_SIZE The make of the number of bytes in a word, this determines
80 the clumping of the output data. eg a value of
81 2 makes words look like 1234 5678, whilst 1
82 would make the same value look like 12 34 56
84 LISTING_LHS_WIDTH Number of words of above size for the lhs
86 LISTING_LHS_WIDTH_SECOND Number of words for the data on the lhs
89 LISTING_LHS_CONT_LINES Max number of lines to use up for a continutation
90 LISTING_RHS_WIDTH Number of chars from the input file to print
98 #include "input-file.h"
102 #ifndef LISTING_HEADER
103 #define LISTING_HEADER "GAS LISTING"
105 #ifndef LISTING_WORD_SIZE
106 #define LISTING_WORD_SIZE 4
108 #ifndef LISTING_LHS_WIDTH
109 #define LISTING_LHS_WIDTH 1
111 #ifndef LISTING_LHS_WIDTH_SECOND
112 #define LISTING_LHS_WIDTH_SECOND 1
114 #ifndef LISTING_RHS_WIDTH
115 #define LISTING_RHS_WIDTH 100
117 #ifndef LISTING_LHS_CONT_LINES
118 #define LISTING_LHS_CONT_LINES 4
124 /* This structure remembers which .s were used */
125 typedef struct file_info_struct
130 struct file_info_struct *next;
137 /* this structure rememebrs which line from which file goes into which
139 typedef struct list_info_struct
141 /* Frag which this line of source is nearest to */
143 /* The actual line in the source file */
145 /* Pointer to the file info struct for the file which this line
147 file_info_type *file;
150 struct list_info_struct *next;
153 /* Pointer to the file info struct for the high level language
154 source line that belongs here */
155 file_info_type *hll_file;
157 /* High level language source line */
161 /* Pointer to any error message associated with this line */
180 static struct list_info_struct *head;
181 struct list_info_struct *listing_tail;
183 extern fragS *frag_now;
186 static int paper_width = 200;
187 static int paper_height = 60;
189 /* File to output listings to. */
190 static FILE *list_file;
192 /* this static array is used to keep the text of data to be printed
193 before the start of the line.
194 It is stored so we can give a bit more info on the next line. To much, and large
195 initialized arrays will use up lots of paper.
198 static char data_buffer[100];
199 static unsigned int data_buffer_size;
203 static void listing_message PARAMS ((const char *name, const char *message));
204 static file_info_type *file_info PARAMS ((const char *file_name));
205 static void new_frag PARAMS ((void));
206 static char *buffer_line PARAMS ((file_info_type *file,
207 char *line, unsigned int size));
208 static void listing_page PARAMS ((list_info_type *list));
209 static unsigned int calc_hex PARAMS ((list_info_type *list));
210 static void print_lines PARAMS ((list_info_type *list,
212 unsigned int address));
213 static void list_symbol_table PARAMS ((void));
214 static void print_source PARAMS ((file_info_type *current_file,
215 list_info_type *list,
217 unsigned int width));
218 static int debugging_pseudo PARAMS ((char *line));
219 static void listing_listing PARAMS ((char *name));
223 listing_message (name, message)
227 unsigned int l = strlen (name) + strlen (message) + 1;
228 char *n = (char *) xmalloc (l);
231 if (listing_tail != (list_info_type *) NULL)
233 listing_tail->message = n;
238 listing_warning (message)
241 listing_message ("Warning:", message);
245 listing_error (message)
248 listing_message ("Error:", message);
254 static file_info_type *file_info_head;
256 static file_info_type *
257 file_info (file_name)
258 const char *file_name;
260 /* Find an entry with this file name */
261 file_info_type *p = file_info_head;
263 while (p != (file_info_type *) NULL)
265 if (strcmp (p->filename, file_name) == 0)
272 p = (file_info_type *) xmalloc (sizeof (file_info_type));
273 p->next = file_info_head;
275 p->filename = xmalloc ((unsigned long) strlen (file_name) + 1);
276 strcpy (p->filename, file_name);
280 p->file = fopen (p->filename, "r");
292 frag_wane (frag_now);
303 static unsigned int last_line = 0xffff;
304 static char *last_file = NULL;
307 as_where (&file, &line);
308 if (line != last_line || (last_file && file && strcmp(file, last_file)))
314 new = (list_info_type *) xmalloc (sizeof (list_info_type));
315 new->frag = frag_now;
317 new->file = file_info (file);
321 listing_tail->next = new;
328 new->next = (list_info_type *) NULL;
329 new->message = (char *) NULL;
330 new->edict = EDICT_NONE;
331 new->hll_file = (file_info_type *) NULL;
337 /* Attach all current frags to the previous line instead of the
338 current line. This is called by the MIPS backend when it discovers
339 that it needs to add some NOP instructions; the added NOP
340 instructions should go with the instruction that has the delay, not
341 with the new instruction. */
349 if (head == (list_info_type *) NULL
350 || head == listing_tail)
355 for (l = head; l->next != listing_tail; l = l->next)
358 for (f = frchain_now->frch_root; f != (fragS *) NULL; f = f->fr_next)
359 if (f->line == listing_tail)
362 listing_tail->frag = frag_now;
367 This function returns the next source line from the file supplied,
368 truncated to size. It appends a fake line to the end of each input
373 buffer_line (file, line, size)
374 file_info_type * file;
378 unsigned int count = 0;
383 /* If we couldn't open the file, return an empty line */
384 if (file->file == (FILE *) NULL || file->at_end)
389 if (file->linenum == 0)
392 c = fgetc (file->file);
394 size -= 1; /* leave room for null */
396 while (c != EOF && c != '\n')
402 c = fgetc (file->file);
418 static const char *fn;
420 static unsigned int eject; /* Eject pending */
421 static unsigned int page; /* Current page number */
422 static char *title; /* current title */
423 static char *subtitle; /* current subtitle */
424 static unsigned int on_page; /* number of lines printed on current page */
429 list_info_type *list;
431 /* Grope around, see if we can see a title or subtitle edict coming up
432 soon (we look down 10 lines of the page and see if it's there)*/
433 if ((eject || (on_page >= paper_height)) && paper_height != 0)
437 int had_subtitle = 0;
441 while (c != 0 && list)
443 if (list->edict == EDICT_SBTTL && !had_subtitle)
446 subtitle = list->edict_arg;
448 if (list->edict == EDICT_TITLE && !had_title)
451 title = list->edict_arg;
460 fprintf (list_file, "\f");
463 fprintf (list_file, "%s %s \t\t\tpage %d\n", LISTING_HEADER, fn, page);
464 fprintf (list_file, "%s\n", title);
465 fprintf (list_file, "%s\n", subtitle);
474 list_info_type * list;
476 list_info_type *first = list;
477 unsigned int address = (unsigned int) ~0;
482 unsigned int byte_in_frag;
485 /* Find first frag which says it belongs to this line */
487 while (frag && frag->line != list)
488 frag = frag->fr_next;
492 data_buffer_size = 0;
494 /* Dump all the frags which belong to this line */
495 while (frag_ptr != (fragS *) NULL && frag_ptr->line == first)
497 /* Print as many bytes from the fixed part as is sensible */
499 while (byte_in_frag < frag_ptr->fr_fix && data_buffer_size < sizeof (data_buffer) - 10)
503 address = frag_ptr->fr_address;
506 sprintf (data_buffer + data_buffer_size,
508 (frag_ptr->fr_literal[byte_in_frag]) & 0xff);
509 data_buffer_size += 2;
513 unsigned int var_rep_max = byte_in_frag;
514 unsigned int var_rep_idx = byte_in_frag;
516 /* Print as many bytes from the variable part as is sensible */
517 while (byte_in_frag < frag_ptr->fr_var * frag_ptr->fr_offset
518 && data_buffer_size < sizeof (data_buffer) - 10)
522 address = frag_ptr->fr_address;
524 sprintf (data_buffer + data_buffer_size,
526 (frag_ptr->fr_literal[var_rep_idx]) & 0xff);
528 data_buffer[data_buffer_size++] = '*';
529 data_buffer[data_buffer_size++] = '*';
531 data_buffer_size += 2;
536 if (var_rep_idx >= frag_ptr->fr_var)
537 var_rep_idx = var_rep_max;
541 frag_ptr = frag_ptr->fr_next;
543 data_buffer[data_buffer_size++] = 0;
553 print_lines (list, string, address)
554 list_info_type *list;
556 unsigned int address;
561 unsigned int byte_in_word = 0;
562 char *src = data_buffer;
564 /* Print the stuff on the first line */
566 nchars = (LISTING_WORD_SIZE * 2 + 1) * LISTING_LHS_WIDTH;
567 /* Print the hex for the first line */
570 fprintf (list_file, "% 4d ", list->line);
571 for (idx = 0; idx < nchars; idx++)
572 fprintf (list_file, " ");
574 fprintf (list_file, "\t%s\n", string ? string : "");
583 fprintf (list_file, "% 4d ???? ", list->line);
587 fprintf (list_file, "% 4d %04x ", list->line, address);
590 /* And the data to go along with it */
593 while (*src && idx < nchars)
595 fprintf (list_file, "%c%c", src[0], src[1]);
598 if (byte_in_word == LISTING_WORD_SIZE)
600 fprintf (list_file, " ");
607 for (; idx < nchars; idx++)
608 fprintf (list_file, " ");
610 fprintf (list_file, "\t%s\n", string ? string : "");
615 fprintf (list_file, "**** %s\n", list->message);
621 lines < LISTING_LHS_CONT_LINES
625 nchars = ((LISTING_WORD_SIZE * 2) + 1) * LISTING_LHS_WIDTH_SECOND - 1;
627 /* Print any more lines of data, but more compactly */
628 fprintf (list_file, "% 4d ", list->line);
630 while (*src && idx < nchars)
632 fprintf (list_file, "%c%c", src[0], src[1]);
636 if (byte_in_word == LISTING_WORD_SIZE)
638 fprintf (list_file, " ");
644 fprintf (list_file, "\n");
658 extern symbolS *symbol_rootP;
665 for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
667 if (ptr->sy_frag->line)
669 if (S_GET_NAME (ptr))
671 char buf[30], fmt[8];
672 valueT val = S_GET_VALUE (ptr);
674 /* @@ Note that this is dependent on the compilation options,
675 not solely on the target characteristics. */
676 if (sizeof (val) == 4 && sizeof (int) == 4)
677 sprintf (buf, "%08lx", (unsigned long) val);
678 else if (sizeof (val) <= sizeof (unsigned long))
680 sprintf (fmt, "%%0%lulx",
681 (unsigned long) (sizeof (val) * 2));
682 sprintf (buf, fmt, (unsigned long) val);
685 else if (sizeof (val) > 4)
688 sprintf_vma (buf1, val);
689 strcpy (buf, "00000000");
690 strcpy (buf + 8 - strlen (buf1), buf1);
698 fprintf (list_file, "DEFINED SYMBOLS\n");
703 fprintf (list_file, "%20s:%-5d %s:%s %s\n",
704 ptr->sy_frag->line->file->filename,
705 ptr->sy_frag->line->line,
706 segment_name (S_GET_SEGMENT (ptr)),
707 buf, S_GET_NAME (ptr));
717 fprintf (list_file, "NO DEFINED SYMBOLS\n");
720 fprintf (list_file, "\n");
726 for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
728 if (S_GET_NAME (ptr) && strlen (S_GET_NAME (ptr)) != 0)
730 if (ptr->sy_frag->line == 0
732 && !S_IS_REGISTER (ptr)
734 && S_GET_SEGMENT (ptr) != reg_section)
739 fprintf (list_file, "UNDEFINED SYMBOLS\n");
743 fprintf (list_file, "%s\n", S_GET_NAME (ptr));
751 fprintf (list_file, "NO UNDEFINED SYMBOLS\n");
758 print_source (current_file, list, buffer, width)
759 file_info_type *current_file;
760 list_info_type *list;
764 if (current_file->file)
766 while (current_file->linenum < list->hll_line
767 && !current_file->at_end)
769 char *p = buffer_line (current_file, buffer, width);
770 fprintf (list_file, "%4d:%-13s **** %s\n", current_file->linenum,
771 current_file->filename, p);
778 /* Sometimes the user doesn't want to be bothered by the debugging
779 records inserted by the compiler, see if the line is suspicious */
782 debugging_pseudo (line)
785 while (isspace (*line))
793 if (strncmp (line, "def", 3) == 0)
795 if (strncmp (line, "val", 3) == 0)
797 if (strncmp (line, "scl", 3) == 0)
799 if (strncmp (line, "line", 4) == 0)
801 if (strncmp (line, "endef", 5) == 0)
803 if (strncmp (line, "ln", 2) == 0)
805 if (strncmp (line, "type", 4) == 0)
807 if (strncmp (line, "size", 4) == 0)
809 if (strncmp (line, "dim", 3) == 0)
811 if (strncmp (line, "tag", 3) == 0)
814 if (strncmp (line, "stabs", 5) == 0)
816 if (strncmp (line, "stabn", 5) == 0)
824 listing_listing (name)
827 list_info_type *list = head;
828 file_info_type *current_hll_file = (file_info_type *) NULL;
832 int show_listing = 1;
835 buffer = xmalloc (LISTING_RHS_WIDTH);
839 while (list != (list_info_type *) NULL && 0)
842 list->frag = list->next->frag;
852 width = LISTING_RHS_WIDTH > paper_width ? paper_width :
868 title = list->edict_arg;
871 subtitle = list->edict_arg;
877 if (show_listing > 0)
879 /* Scan down the list and print all the stuff which can be done
880 with this line (or lines). */
885 current_hll_file = list->hll_file;
888 if (current_hll_file && list->hll_line && listing & LISTING_HLL)
890 print_source (current_hll_file, list, buffer, width);
893 while (list->file->file
894 && list->file->linenum < list->line
895 && !list->file->at_end)
897 p = buffer_line (list->file, buffer, width);
899 if (!((listing & LISTING_NODEBUG) && debugging_pseudo (p)))
901 print_lines (list, p, calc_hex (list));
905 if (list->edict == EDICT_EJECT)
912 while (list->file->file
913 && list->file->linenum < list->line
914 && !list->file->at_end)
915 p = buffer_line (list->file, buffer, width);
934 list_file = fopen (name, "w");
935 if (list_file == NULL)
937 as_perror ("can't open list file: %s", name);
942 if (listing & LISTING_NOFORM)
947 if (listing & LISTING_LISTING)
949 listing_listing (name);
952 if (listing & LISTING_SYMBOLS)
954 list_symbol_table ();
967 listing_eject (ignore)
970 listing_tail->edict = EDICT_EJECT;
974 listing_flags (ignore)
977 while ((*input_line_pointer++) && (*input_line_pointer != '\n'))
978 input_line_pointer++;
986 listing_tail->edict = on ? EDICT_LIST : EDICT_NOLIST;
991 listing_psize (width_only)
996 paper_height = get_absolute_expression ();
998 if (paper_height < 0 || paper_height > 1000)
1001 as_warn ("strange paper height, set to no form");
1004 if (*input_line_pointer != ',')
1006 demand_empty_rest_of_line ();
1010 ++input_line_pointer;
1013 paper_width = get_absolute_expression ();
1015 demand_empty_rest_of_line ();
1019 listing_title (depth)
1024 unsigned int length;
1027 if (*input_line_pointer == '\"')
1029 input_line_pointer++;
1030 start = input_line_pointer;
1032 while (*input_line_pointer)
1034 if (*input_line_pointer == '\"')
1036 length = input_line_pointer - start;
1037 ttl = xmalloc (length + 1);
1038 memcpy (ttl, start, length);
1040 listing_tail->edict = depth ? EDICT_SBTTL : EDICT_TITLE;
1041 listing_tail->edict_arg = ttl;
1042 input_line_pointer++;
1043 demand_empty_rest_of_line ();
1046 else if (*input_line_pointer == '\n')
1048 as_bad ("New line in title");
1049 demand_empty_rest_of_line ();
1054 input_line_pointer++;
1060 as_bad ("expecting title in quotes");
1067 listing_source_line (line)
1071 listing_tail->hll_line = line;
1077 listing_source_file (file)
1081 listing_tail->hll_file = file_info (file);
1089 /* Dummy functions for when compiled without listing enabled */
1092 listing_flags (ignore)
1106 listing_eject (ignore)
1113 listing_psize (ignore)
1120 listing_title (depth)
1134 listing_newline (name)
1141 listing_source_line (n)
1147 listing_source_file (n)