Initial revision
[binutils.git] / gas / listing.c
blobe2b173be7d68be23142b6fa0a1d726b60d4f2c62
1 /* listing.c - mainting assembly listings
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999
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)
10 any later version.
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
20 02111-1307, USA. */
23 Contributed by Steve Chamberlain
24 sac@cygnus.com
27 A listing page looks like:
29 LISTING_HEADER sourcefilename pagenumber
30 TITLE LINE
31 SUBTITLE LINE
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:
39 .title "stuff"
40 Put "stuff" onto the title line
41 .sbttl "stuff"
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
47 .eject
48 Thow a page
49 .list
50 Increment the enable listing counter
51 .nolist
52 Decrement the enable listing counter
54 .psize Y[,X]
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
89 for the second line
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
93 on a line
96 #include <ctype.h>
98 #include "as.h"
99 #include <obstack.h>
100 #include "input-file.h"
101 #include "subsegs.h"
103 #ifndef NO_LISTING
105 #ifndef LISTING_HEADER
106 #define LISTING_HEADER "GAS LISTING"
107 #endif
108 #ifndef LISTING_WORD_SIZE
109 #define LISTING_WORD_SIZE 4
110 #endif
111 #ifndef LISTING_LHS_WIDTH
112 #define LISTING_LHS_WIDTH 1
113 #endif
114 #ifndef LISTING_LHS_WIDTH_SECOND
115 #define LISTING_LHS_WIDTH_SECOND 1
116 #endif
117 #ifndef LISTING_RHS_WIDTH
118 #define LISTING_RHS_WIDTH 100
119 #endif
120 #ifndef LISTING_LHS_CONT_LINES
121 #define LISTING_LHS_CONT_LINES 4
122 #endif
124 /* This structure remembers which .s were used */
125 typedef struct file_info_struct
127 struct file_info_struct * next;
128 char * filename;
129 long pos;
130 unsigned int linenum;
131 int at_end;
133 file_info_type;
135 /* This structure rememebrs which line from which file goes into which
136 frag */
137 struct list_info_struct
139 /* Frag which this line of source is nearest to */
140 fragS * frag;
142 /* The actual line in the source file */
143 unsigned int line;
144 /* Pointer to the file info struct for the file which this line
145 belongs to */
146 file_info_type * file;
148 /* The expanded text of any macro that may have been executing. */
149 char * line_contents;
151 /* Next in list */
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 */
161 char * message;
163 enum
165 EDICT_NONE,
166 EDICT_SBTTL,
167 EDICT_TITLE,
168 EDICT_NOLIST,
169 EDICT_LIST,
170 EDICT_NOLIST_NEXT,
171 EDICT_EJECT
172 } edict;
173 char * edict_arg;
175 /* Nonzero if this line is to be omitted because it contains
176 debugging information. This can become a flags field if we come
177 up with more information to store here. */
178 int debugging;
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;
198 extern int listing;
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. */
206 #define MAX_BYTES \
207 (((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width \
208 + ((((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second) \
209 * listing_lhs_cont_lines) \
210 + 20)
212 static char * data_buffer;
214 /* Prototypes. */
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,
227 char *buffer,
228 unsigned int width));
229 static int debugging_pseudo PARAMS ((list_info_type *, const char *));
230 static void listing_listing PARAMS ((char *name));
233 static void
234 listing_message (name, message)
235 const char *name;
236 const char *message;
238 unsigned int l = strlen (name) + strlen (message) + 1;
239 char *n = (char *) xmalloc (l);
240 strcpy (n, name);
241 strcat (n, message);
242 if (listing_tail != (list_info_type *) NULL)
244 listing_tail->message = n;
248 void
249 listing_warning (message)
250 const char *message;
252 listing_message (_("Warning:"), message);
255 void
256 listing_error (message)
257 const char *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)
272 return p;
273 p = p->next;
276 /* Make new entry */
278 p = (file_info_type *) xmalloc (sizeof (file_info_type));
279 p->next = file_info_head;
280 file_info_head = p;
281 p->filename = xmalloc ((unsigned long) strlen (file_name) + 1);
282 strcpy (p->filename, file_name);
283 p->pos = 0;
284 p->linenum = 0;
285 p->at_end = 0;
287 return p;
291 static void
292 new_frag ()
295 frag_wane (frag_now);
296 frag_new (0);
300 void
301 listing_newline (ps)
302 char *ps;
304 char *file;
305 unsigned int line;
306 static unsigned int last_line = 0xffff;
307 static char *last_file = NULL;
308 list_info_type *new = NULL;
310 if (listing == 0)
311 return;
313 if (now_seg == absolute_section)
314 return;
316 #ifdef OBJ_ELF
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)
325 const char *segname;
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;
332 #endif
334 as_where (&file, &line);
335 if (ps == NULL)
337 if (line == last_line && !(last_file && file && strcmp (file, last_file)))
338 return;
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
352 of the assembly. */
353 if (strcmp (file, _("{standard input}")) == 0
354 && input_line_pointer != NULL)
356 char * copy;
357 int len;
358 int seen_quote = 0;
360 for (copy = input_line_pointer - 1;
361 * copy && (seen_quote
362 || (! is_end_of_line [(unsigned char) * copy]));
363 copy ++)
364 if (* copy == '"' && copy[-1] != '\\')
365 seen_quote = ! seen_quote;
367 len = (copy - input_line_pointer) + 2;
369 copy = xmalloc (len);
371 if (copy != NULL)
373 char * src = input_line_pointer - 1;
374 char * dest = copy;
376 while (--len)
378 char c = * src ++;
380 /* Omit control characters in the listing. */
381 if (isascii (c) && ! iscntrl (c))
382 * dest ++ = c;
385 *dest = 0;
388 new->line_contents = copy;
390 else
391 new->line_contents = NULL;
393 else
395 new = (list_info_type *) xmalloc (sizeof (list_info_type));
396 new->line_contents = ps;
399 last_line = line;
400 last_file = file;
402 new_frag ();
404 if (listing_tail)
405 listing_tail->next = new;
406 else
407 head = new;
409 listing_tail = new;
411 new->frag = frag_now;
412 new->line = line;
413 new->file = file_info (file);
414 new->next = (list_info_type *) NULL;
415 new->message = (char *) NULL;
416 new->edict = EDICT_NONE;
417 new->hll_file = (file_info_type *) NULL;
418 new->hll_line = 0;
419 new->debugging = 0;
421 new_frag ();
423 #ifdef OBJ_ELF
424 /* In ELF, anything in a section beginning with .debug or .line is
425 considered to be debugging information. */
426 if ((listing & LISTING_NODEBUG) != 0)
428 const char *segname;
430 segname = segment_name (now_seg);
431 if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0
432 || strncmp (segname, ".line", sizeof ".line" - 1) == 0)
433 new->debugging = 1;
435 #endif
438 /* Attach all current frags to the previous line instead of the
439 current line. This is called by the MIPS backend when it discovers
440 that it needs to add some NOP instructions; the added NOP
441 instructions should go with the instruction that has the delay, not
442 with the new instruction. */
444 void
445 listing_prev_line ()
447 list_info_type *l;
448 fragS *f;
450 if (head == (list_info_type *) NULL
451 || head == listing_tail)
452 return;
454 new_frag ();
456 for (l = head; l->next != listing_tail; l = l->next)
459 for (f = frchain_now->frch_root; f != (fragS *) NULL; f = f->fr_next)
460 if (f->line == listing_tail)
461 f->line = l;
463 listing_tail->frag = frag_now;
464 new_frag ();
468 This function returns the next source line from the file supplied,
469 truncated to size. It appends a fake line to the end of each input
470 file to make
473 static char *
474 buffer_line (file, line, size)
475 file_info_type * file;
476 char *line;
477 unsigned int size;
479 unsigned int count = 0;
480 int c;
482 char *p = line;
484 /* If we couldn't open the file, return an empty line */
485 if (file->at_end)
486 return "";
488 /* Check the cache and see if we last used this file. */
489 if (!last_open_file_info || file != last_open_file_info)
491 if (last_open_file)
493 last_open_file_info->pos = ftell (last_open_file);
494 fclose (last_open_file);
497 last_open_file_info = file;
498 last_open_file = fopen (file->filename, "r");
499 if (last_open_file == NULL)
501 file->at_end = 1;
502 return "";
505 /* Seek to where we were last time this file was open. */
506 if (file->pos)
507 fseek(last_open_file, file->pos, SEEK_SET);
510 c = fgetc (last_open_file);
512 size -= 1; /* leave room for null */
514 while (c != EOF && c != '\n')
516 if (count < size)
517 *p++ = c;
518 count++;
520 c = fgetc (last_open_file);
523 if (c == EOF)
525 file->at_end = 1;
526 *p++ = '.';
527 *p++ = '.';
528 *p++ = '.';
530 file->linenum++;
531 *p++ = 0;
532 return line;
536 static const char *fn;
538 static unsigned int eject; /* Eject pending */
539 static unsigned int page; /* Current page number */
540 static char *title; /* current title */
541 static char *subtitle; /* current subtitle */
542 static unsigned int on_page; /* number of lines printed on current page */
545 static void
546 listing_page (list)
547 list_info_type *list;
549 /* Grope around, see if we can see a title or subtitle edict coming up
550 soon (we look down 10 lines of the page and see if it's there)*/
551 if ((eject || (on_page >= (unsigned int) paper_height)) && paper_height != 0)
553 unsigned int c = 10;
554 int had_title = 0;
555 int had_subtitle = 0;
557 page++;
559 while (c != 0 && list)
561 if (list->edict == EDICT_SBTTL && !had_subtitle)
563 had_subtitle = 1;
564 subtitle = list->edict_arg;
566 if (list->edict == EDICT_TITLE && !had_title)
568 had_title = 1;
569 title = list->edict_arg;
571 list = list->next;
572 c--;
576 if (page > 1)
578 fprintf (list_file, "\f");
581 fprintf (list_file, "%s %s \t\t\tpage %d\n", LISTING_HEADER, fn, page);
582 fprintf (list_file, "%s\n", title);
583 fprintf (list_file, "%s\n", subtitle);
584 on_page = 3;
585 eject = 0;
590 static unsigned int
591 calc_hex (list)
592 list_info_type * list;
594 int data_buffer_size;
595 list_info_type *first = list;
596 unsigned int address = ~ (unsigned int) 0;
597 fragS *frag;
598 fragS *frag_ptr;
599 unsigned int byte_in_frag;
601 /* Find first frag which says it belongs to this line */
602 frag = list->frag;
603 while (frag && frag->line != list)
604 frag = frag->fr_next;
606 frag_ptr = frag;
608 data_buffer_size = 0;
610 /* Dump all the frags which belong to this line */
611 while (frag_ptr != (fragS *) NULL && frag_ptr->line == first)
613 /* Print as many bytes from the fixed part as is sensible */
614 byte_in_frag = 0;
615 while ((offsetT) byte_in_frag < frag_ptr->fr_fix
616 && data_buffer_size < MAX_BYTES - 3)
618 if (address == ~ (unsigned int) 0)
620 address = frag_ptr->fr_address;
623 sprintf (data_buffer + data_buffer_size,
624 "%02X",
625 (frag_ptr->fr_literal[byte_in_frag]) & 0xff);
626 data_buffer_size += 2;
627 byte_in_frag++;
630 unsigned int var_rep_max = byte_in_frag;
631 unsigned int var_rep_idx = byte_in_frag;
633 /* Print as many bytes from the variable part as is sensible */
634 while (((offsetT) byte_in_frag
635 < frag_ptr->fr_fix + frag_ptr->fr_var * frag_ptr->fr_offset)
636 && data_buffer_size < MAX_BYTES - 3)
638 if (address == ~ (unsigned int) 0)
640 address = frag_ptr->fr_address;
642 sprintf (data_buffer + data_buffer_size,
643 "%02X",
644 (frag_ptr->fr_literal[var_rep_idx]) & 0xff);
645 #if 0
646 data_buffer[data_buffer_size++] = '*';
647 data_buffer[data_buffer_size++] = '*';
648 #endif
649 data_buffer_size += 2;
651 var_rep_idx++;
652 byte_in_frag++;
654 if ((offsetT) var_rep_idx >= frag_ptr->fr_fix + frag_ptr->fr_var)
655 var_rep_idx = var_rep_max;
659 frag_ptr = frag_ptr->fr_next;
661 data_buffer[data_buffer_size] = '\0';
662 return address;
670 static void
671 print_lines (list, lineno, string, address)
672 list_info_type *list;
673 unsigned int lineno;
674 char *string;
675 unsigned int address;
677 unsigned int idx;
678 unsigned int nchars;
679 unsigned int lines;
680 unsigned int byte_in_word = 0;
681 char *src = data_buffer;
683 /* Print the stuff on the first line */
684 listing_page (list);
685 nchars = (LISTING_WORD_SIZE * 2 + 1) * listing_lhs_width;
687 /* Print the hex for the first line */
688 if (address == ~ (unsigned int) 0)
690 fprintf (list_file, "% 4d ", lineno);
691 for (idx = 0; idx < nchars; idx++)
692 fprintf (list_file, " ");
694 fprintf (list_file, "\t%s\n", string ? string : "");
696 on_page ++;
698 listing_page (0);
700 return;
703 if (had_errors ())
704 fprintf (list_file, "% 4d ???? ", lineno);
705 else
706 fprintf (list_file, "% 4d %04x ", lineno, address);
708 /* And the data to go along with it */
709 idx = 0;
711 while (*src && idx < nchars)
713 fprintf (list_file, "%c%c", src[0], src[1]);
714 src += 2;
715 byte_in_word++;
717 if (byte_in_word == LISTING_WORD_SIZE)
719 fprintf (list_file, " ");
720 idx++;
721 byte_in_word = 0;
724 idx += 2;
727 for (; idx < nchars; idx++)
728 fprintf (list_file, " ");
730 fprintf (list_file, "\t%s\n", string ? string : "");
731 on_page++;
732 listing_page (list);
734 if (list->message)
736 fprintf (list_file, "**** %s\n", list->message);
737 listing_page (list);
738 on_page++;
741 for (lines = 0;
742 lines < (unsigned int) listing_lhs_cont_lines
743 && *src;
744 lines ++)
746 nchars = ((LISTING_WORD_SIZE * 2) + 1)
747 * listing_lhs_width_second - 1;
748 idx = 0;
750 /* Print any more lines of data, but more compactly */
751 fprintf (list_file, "% 4d ", lineno);
753 while (*src && idx < nchars)
755 fprintf (list_file, "%c%c", src[0], src[1]);
756 src += 2;
757 idx += 2;
758 byte_in_word++;
760 if (byte_in_word == LISTING_WORD_SIZE)
762 fprintf (list_file, " ");
763 idx++;
764 byte_in_word = 0;
768 fprintf (list_file, "\n");
769 on_page ++;
770 listing_page (list);
775 static void
776 list_symbol_table ()
778 extern symbolS *symbol_rootP;
779 int got_some = 0;
781 symbolS *ptr;
782 eject = 1;
783 listing_page (0);
785 for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
787 if (SEG_NORMAL (S_GET_SEGMENT (ptr))
788 || S_GET_SEGMENT (ptr) == absolute_section)
790 #ifdef BFD_ASSEMBLER
791 /* Don't report section symbols. They are not interesting. */
792 if (ptr->bsym->flags & BSF_SECTION_SYM)
793 continue;
794 #endif
795 if (S_GET_NAME (ptr))
797 char buf[30], fmt[8];
798 valueT val = S_GET_VALUE (ptr);
800 /* @@ Note that this is dependent on the compilation options,
801 not solely on the target characteristics. */
802 if (sizeof (val) == 4 && sizeof (int) == 4)
803 sprintf (buf, "%08lx", (unsigned long) val);
804 else if (sizeof (val) <= sizeof (unsigned long))
806 sprintf (fmt, "%%0%lulx",
807 (unsigned long) (sizeof (val) * 2));
808 sprintf (buf, fmt, (unsigned long) val);
810 #if defined (BFD64)
811 else if (sizeof (val) > 4)
812 sprintf_vma (buf, val);
813 #endif
814 else
815 abort ();
817 if (!got_some)
819 fprintf (list_file, "DEFINED SYMBOLS\n");
820 on_page++;
821 got_some = 1;
824 if (ptr->sy_frag && ptr->sy_frag->line)
826 fprintf (list_file, "%20s:%-5d %s:%s %s\n",
827 ptr->sy_frag->line->file->filename,
828 ptr->sy_frag->line->line,
829 segment_name (S_GET_SEGMENT (ptr)),
830 buf, S_GET_NAME (ptr));
832 else
834 fprintf (list_file, "%33s:%s %s\n",
835 segment_name (S_GET_SEGMENT (ptr)),
836 buf, S_GET_NAME (ptr));
839 on_page ++;
840 listing_page (0);
845 if (!got_some)
847 fprintf (list_file, "NO DEFINED SYMBOLS\n");
848 on_page++;
850 fprintf (list_file, "\n");
851 on_page++;
852 listing_page (0);
854 got_some = 0;
856 for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
858 if (S_GET_NAME (ptr) && strlen (S_GET_NAME (ptr)) != 0)
860 if (S_GET_SEGMENT (ptr) == undefined_section)
862 if (!got_some)
864 got_some = 1;
865 fprintf (list_file, "UNDEFINED SYMBOLS\n");
866 on_page++;
867 listing_page (0);
869 fprintf (list_file, "%s\n", S_GET_NAME (ptr));
870 on_page++;
871 listing_page (0);
875 if (!got_some)
877 fprintf (list_file, "NO UNDEFINED SYMBOLS\n");
878 on_page++;
879 listing_page (0);
883 static void
884 print_source (current_file, list, buffer, width)
885 file_info_type *current_file;
886 list_info_type *list;
887 char *buffer;
888 unsigned int width;
890 if (!current_file->at_end)
892 while (current_file->linenum < list->hll_line
893 && !current_file->at_end)
895 char *p = buffer_line (current_file, buffer, width);
896 fprintf (list_file, "%4u:%-13s **** %s\n", current_file->linenum,
897 current_file->filename, p);
898 on_page++;
899 listing_page (list);
904 /* Sometimes the user doesn't want to be bothered by the debugging
905 records inserted by the compiler, see if the line is suspicious. */
907 static int
908 debugging_pseudo (list, line)
909 list_info_type *list;
910 const char *line;
912 static int in_debug;
913 int was_debug;
915 if (list->debugging)
917 in_debug = 1;
918 return 1;
921 was_debug = in_debug;
922 in_debug = 0;
924 while (isspace ((unsigned char) *line))
925 line++;
927 if (*line != '.')
929 #ifdef OBJ_ELF
930 /* The ELF compiler sometimes emits blank lines after switching
931 out of a debugging section. If the next line drops us back
932 into debugging information, then don't print the blank line.
933 This is a hack for a particular compiler behaviour, not a
934 general case. */
935 if (was_debug
936 && *line == '\0'
937 && list->next != NULL
938 && list->next->debugging)
940 in_debug = 1;
941 return 1;
943 #endif
945 return 0;
948 line++;
950 if (strncmp (line, "def", 3) == 0)
951 return 1;
952 if (strncmp (line, "val", 3) == 0)
953 return 1;
954 if (strncmp (line, "scl", 3) == 0)
955 return 1;
956 if (strncmp (line, "line", 4) == 0)
957 return 1;
958 if (strncmp (line, "endef", 5) == 0)
959 return 1;
960 if (strncmp (line, "ln", 2) == 0)
961 return 1;
962 if (strncmp (line, "type", 4) == 0)
963 return 1;
964 if (strncmp (line, "size", 4) == 0)
965 return 1;
966 if (strncmp (line, "dim", 3) == 0)
967 return 1;
968 if (strncmp (line, "tag", 3) == 0)
969 return 1;
971 if (strncmp (line, "stabs", 5) == 0)
972 return 1;
973 if (strncmp (line, "stabn", 5) == 0)
974 return 1;
976 return 0;
979 static void
980 listing_listing (name)
981 char *name;
983 list_info_type *list = head;
984 file_info_type *current_hll_file = (file_info_type *) NULL;
985 char *message;
986 char *buffer;
987 char *p;
988 int show_listing = 1;
989 unsigned int width;
991 buffer = xmalloc (listing_rhs_width);
992 data_buffer = xmalloc (MAX_BYTES);
993 eject = 1;
994 list = head;
996 while (list != (list_info_type *) NULL && 0)
998 if (list->next)
999 list->frag = list->next->frag;
1000 list = list->next;
1004 list = head->next;
1007 while (list)
1009 int list_line;
1011 width = listing_rhs_width > paper_width ? paper_width :
1012 listing_rhs_width;
1014 list_line = list->line;
1015 switch (list->edict)
1017 case EDICT_LIST:
1018 /* Skip all lines up to the current. */
1019 list_line--;
1020 break;
1021 case EDICT_NOLIST:
1022 show_listing--;
1023 break;
1024 case EDICT_NOLIST_NEXT:
1025 break;
1026 case EDICT_EJECT:
1027 break;
1028 case EDICT_NONE:
1029 break;
1030 case EDICT_TITLE:
1031 title = list->edict_arg;
1032 break;
1033 case EDICT_SBTTL:
1034 subtitle = list->edict_arg;
1035 break;
1036 default:
1037 abort ();
1040 if (show_listing <= 0)
1042 while (list->file->linenum < list_line
1043 && !list->file->at_end)
1044 p = buffer_line (list->file, buffer, width);
1047 if (list->edict == EDICT_LIST)
1049 /* Enable listing for the single line that caused the enable. */
1050 list_line++;
1051 show_listing++;
1054 if (show_listing > 0)
1056 /* Scan down the list and print all the stuff which can be done
1057 with this line (or lines). */
1058 message = 0;
1060 if (list->hll_file)
1062 current_hll_file = list->hll_file;
1065 if (current_hll_file && list->hll_line && (listing & LISTING_HLL))
1067 print_source (current_hll_file, list, buffer, width);
1070 if (list->line_contents)
1072 if (!((listing & LISTING_NODEBUG)
1073 && debugging_pseudo (list, list->line_contents)))
1075 print_lines (list,
1076 list->file->linenum == 0 ? list->line : list->file->linenum,
1077 list->line_contents, calc_hex (list));
1079 free (list->line_contents);
1080 list->line_contents = NULL;
1082 else
1084 while (list->file->linenum < list_line
1085 && !list->file->at_end)
1087 unsigned int address;
1089 p = buffer_line (list->file, buffer, width);
1091 if (list->file->linenum < list_line)
1092 address = ~ (unsigned int) 0;
1093 else
1094 address = calc_hex (list);
1096 if (!((listing & LISTING_NODEBUG)
1097 && debugging_pseudo (list, p)))
1098 print_lines (list, list->file->linenum, p, address);
1102 if (list->edict == EDICT_EJECT)
1104 eject = 1;
1108 if (list->edict == EDICT_NOLIST_NEXT)
1109 --show_listing;
1111 list = list->next;
1114 free (buffer);
1115 free (data_buffer);
1116 data_buffer = NULL;
1119 void
1120 listing_print (name)
1121 char *name;
1123 int using_stdout;
1125 title = "";
1126 subtitle = "";
1128 if (name == NULL)
1130 list_file = stdout;
1131 using_stdout = 1;
1133 else
1135 list_file = fopen (name, "w");
1136 if (list_file != NULL)
1137 using_stdout = 0;
1138 else
1140 as_perror (_("can't open list file: %s"), name);
1141 list_file = stdout;
1142 using_stdout = 1;
1146 if (listing & LISTING_NOFORM)
1148 paper_height = 0;
1151 if (listing & LISTING_LISTING)
1153 listing_listing (name);
1156 if (listing & LISTING_SYMBOLS)
1158 list_symbol_table ();
1161 if (! using_stdout)
1163 if (fclose (list_file) == EOF)
1164 as_perror (_("error closing list file: %s"), name);
1167 if (last_open_file)
1169 fclose (last_open_file);
1174 void
1175 listing_file (name)
1176 const char *name;
1178 fn = name;
1181 void
1182 listing_eject (ignore)
1183 int ignore;
1185 if (listing)
1186 listing_tail->edict = EDICT_EJECT;
1189 void
1190 listing_flags (ignore)
1191 int ignore;
1193 while ((*input_line_pointer++) && (*input_line_pointer != '\n'))
1194 input_line_pointer++;
1198 /* Turn listing on or off. An argument of 0 means to turn off
1199 listing. An argument of 1 means to turn on listing. An argument
1200 of 2 means to turn off listing, but as of the next line; that is,
1201 the current line should be listed, but the next line should not. */
1203 void
1204 listing_list (on)
1205 int on;
1207 if (listing)
1209 switch (on)
1211 case 0:
1212 if (listing_tail->edict == EDICT_LIST)
1213 listing_tail->edict = EDICT_NONE;
1214 else
1215 listing_tail->edict = EDICT_NOLIST;
1216 break;
1217 case 1:
1218 if (listing_tail->edict == EDICT_NOLIST
1219 || listing_tail->edict == EDICT_NOLIST_NEXT)
1220 listing_tail->edict = EDICT_NONE;
1221 else
1222 listing_tail->edict = EDICT_LIST;
1223 break;
1224 case 2:
1225 listing_tail->edict = EDICT_NOLIST_NEXT;
1226 break;
1227 default:
1228 abort ();
1234 void
1235 listing_psize (width_only)
1236 int width_only;
1238 if (! width_only)
1240 paper_height = get_absolute_expression ();
1242 if (paper_height < 0 || paper_height > 1000)
1244 paper_height = 0;
1245 as_warn (_("strange paper height, set to no form"));
1248 if (*input_line_pointer != ',')
1250 demand_empty_rest_of_line ();
1251 return;
1254 ++input_line_pointer;
1257 paper_width = get_absolute_expression ();
1259 demand_empty_rest_of_line ();
1262 void
1263 listing_nopage (ignore)
1264 int ignore;
1266 paper_height = 0;
1269 void
1270 listing_title (depth)
1271 int depth;
1273 int quoted;
1274 char *start;
1275 char *ttl;
1276 unsigned int length;
1278 SKIP_WHITESPACE ();
1279 if (*input_line_pointer != '\"')
1280 quoted = 0;
1281 else
1283 quoted = 1;
1284 ++input_line_pointer;
1287 start = input_line_pointer;
1289 while (*input_line_pointer)
1291 if (quoted
1292 ? *input_line_pointer == '\"'
1293 : is_end_of_line[(unsigned char) *input_line_pointer])
1295 if (listing)
1297 length = input_line_pointer - start;
1298 ttl = xmalloc (length + 1);
1299 memcpy (ttl, start, length);
1300 ttl[length] = 0;
1301 listing_tail->edict = depth ? EDICT_SBTTL : EDICT_TITLE;
1302 listing_tail->edict_arg = ttl;
1304 if (quoted)
1305 input_line_pointer++;
1306 demand_empty_rest_of_line ();
1307 return;
1309 else if (*input_line_pointer == '\n')
1311 as_bad (_("New line in title"));
1312 demand_empty_rest_of_line ();
1313 return;
1315 else
1317 input_line_pointer++;
1324 void
1325 listing_source_line (line)
1326 unsigned int line;
1328 if (listing)
1330 new_frag ();
1331 listing_tail->hll_line = line;
1332 new_frag ();
1336 void
1337 listing_source_file (file)
1338 const char *file;
1340 if (listing)
1341 listing_tail->hll_file = file_info (file);
1346 #else
1349 /* Dummy functions for when compiled without listing enabled */
1351 void
1352 listing_flags (ignore)
1353 int ignore;
1355 s_ignore (0);
1358 void
1359 listing_list (on)
1360 int on;
1362 s_ignore (0);
1365 void
1366 listing_eject (ignore)
1367 int ignore;
1369 s_ignore (0);
1372 void
1373 listing_psize (ignore)
1374 int ignore;
1376 s_ignore (0);
1379 void
1380 listing_nopage (ignore)
1381 int ignore;
1383 s_ignore (0);
1386 void
1387 listing_title (depth)
1388 int depth;
1390 s_ignore (0);
1393 void
1394 listing_file (name)
1395 const char *name;
1400 void
1401 listing_newline (name)
1402 char *name;
1407 void
1408 listing_source_line (n)
1409 unsigned int n;
1413 void
1414 listing_source_file (n)
1415 const char *n;
1420 #endif