2 # Generate a source code listing for C or C++ code with assembler code. The
3 # listing is always written to stdout.
4 # Author: Igor Metz <metz@iam.unibe.ch>
6 # Revision 1.4 94/08/26 13:58:27 coxs <coxs@dg-rtp.dg.com>
7 # lister now guesses how to should be configured. Added elf and coff support.
9 # Revision 1.3 89/12/18 13:58:27 metz
10 # lister must now be configured before it can be used. This is done in the
11 # /bin/sh part of the code.
14 # Revision 1.2 89/08/16 17:35:02 metz
15 # Support for SPARC added.
17 # Revision 1.1 89/08/16 16:49:22 metz
21 # Requires: gawk (may be it works also with nawk)
23 # usage: lister filename [compiler-options]
26 # compile the source with -g option to assembler code, then merge the
27 # generated assembler code with the source code. Compiler options
28 # can be supplied on the command line (for example -O)
30 # To install lister, assign one of the supported values to the variable MYSYS:
31 # mc68020 for Motorola 68020 (Sun-3, ..)
32 # mc68030 for Motorola 68030 (Sun-3, ..)
33 # sparc for SPARC (SUN-4, ..)
34 # i386 for i386 (Sun i386, ...)
35 # i386-linux for i386 (Linux, ...)
37 # Guess what kind of objects we are creating and thus what type of assembler
40 ex
/tmp
/$$.c
<<END >/dev/null
52 *COFF
*|
*BCS
*) MYSYS
=coff
;;
53 *mc68k
*|
*M68000
*) MYSYS
=mc68030
;;
54 *SPARC
*) MYSYS
=sparc
;;
60 # uncomment the line you need if the above guesses incorrectly:
66 # MYSYS=`mach` # this will work on Suns with SunOS > 4.0.0
71 if [ $# -gt 0 ] ; then
76 exec gawk
-v whoami
=$WHOAMI -vsys=$MYSYS -voptions="$*" '
77 # commandline arguments:
87 # Declaration of global variables
99 # check processor architecture and set sourcecode line_hint accordingly
100 if (sys == "sparc" || sys == "i386") {
101 line_hint = "^[ \t]*\.stabn.*"
103 line_delimiter = ",";
106 else if (sys == "mc68020" || sys == "mc68030" || sys == "i386-linux") {
107 line_hint = "^[ \t]*\.stabd.*"
109 line_delimiter = ",";
112 else if (sys == "elf") {
113 line_hint = "section.*\.line"
115 line_delimiter = "\t";
118 else if (sys == "coff") {
119 line_hint = "^[ \t]*ln"
121 line_delimiter = "\t";
124 error("Processor type " sys " is not supported yet, sorry")
129 printf("compiling %s to asm code\n", c_filename ) > "/dev/stderr"
131 if (system(cmdline) != 0 ) {
132 error("Compilation of " c_filename " failed")
135 printf("generating listing\n") > "/dev/stderr"
138 while ( getline asm_code < asm_filename > 0 ) {
139 if ( (ignore_stabd==0) && (asm_code ~ line_hint)) {
140 while ( sys == "elf" && (asm_code !~ "word" && asm_code !~ "byte") &&
141 getline asm_code < asm_filename > 0);
142 # source line hint found. Split the line into fields separated by commas.
143 # num_of_fields is 4 for sparc, 3 for m68k
144 num_of_fields = split(asm_code, fields, line_delimiter)
145 newlineno = fields[line_field] + line_offset;
147 if (newlineno > oldlineno) {
148 while ( newlineno > c_lineno && getline c_code < c_filename > 0) {
150 printf("%4d %s\n", c_lineno, c_code)
152 oldlineno = newlineno
155 else if ( asm_code ~ ".*Ltext[ \t]*$" ) {
156 # filename hint found
157 if ( match(asm_code, c_filename)) {
164 else if ( sys == "elf" && asm_code ~ "section.*\.debug" ) {
165 while ( asm_code !~ "^[ \t]*[.]*previous" &&
166 asm_code !~ "\.popsection" &&
167 getline asm_code < asm_filename > 0 );
168 if ( ! (getline asm_code < asm_filename > 0)) break;
170 else if ( sys == "coff" && asm_code ~ "^[ \t]*sdef" ) {
171 if ( asm_code ~ "\.bf" ) {
172 while ( asm_code !~ "^[ \t]*line" &&
173 getline asm_code < asm_filename > 0 ) {
174 num_of_fields = split(asm_code, fields, "\t")
175 line_offset = fields[line_field] - 1;
178 while ( asm_code !~ "^[ \t]*endef" &&
179 getline asm_code < asm_filename > 0 ) {
181 if ( ! (getline asm_code < asm_filename > 0)) break;
183 printf("\t\t\t%s\n", asm_code)
187 system("/bin/rm " asm_filename)
191 printf("usage: %s filename compiler-options\n", whoami) > "/dev/stderr"
195 printf("error: %s\n", s) > "/dev/stderr"
199 function parse_cmdline( i) {
200 # construct filenames to use
201 asm_filename = "/tmp/lister" ARGV[1] ".s"
206 # construct commandline to use
207 if ( match(c_filename, ".C") || match(c_filename, ".cc") ) {
210 else if (match(c_filename, ".c") || match(c_filename, ".i")) {
214 error("unknown extension for file " c_filename)
217 cmdline = cmdline " -g -S -o " asm_filename
219 # now we append the compiler options specified by the user
220 cmdline = cmdline " " options
222 # last but not least: the name of the file to compile
223 cmdline = cmdline " " c_filename