home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / gcc-2.3.3-src.lha / GNU / src / amiga / gcc-2.3.3 / listing < prev    next >
Text File  |  1994-02-06  |  4KB  |  158 lines

  1. #!/bin/sh -f
  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>
  5.  
  6. # Revision 1.3  89/12/18  13:58:27  metz
  7. # lister must now be configured before it can be used. This is done in the
  8. # /bin/sh part of the code.
  9. # Revision 1.2  89/08/16  17:35:02  metz
  10. # Support for SPARC added.
  11. # Revision 1.1  89/08/16  16:49:22  metz
  12. # Initial revision
  13.  
  14. # Requires: gawk (may be it works also with nawk)
  15.  
  16. # usage:  lister filename [compiler-options]
  17.  
  18. # Method:
  19. # compile the source with -g option to assembler code, then merge the
  20. # generated assembler code with the source code. Compiler options
  21. # can be supplied on the command line (for example -O)
  22.  
  23. # To install lister, assign one of the supported values to the variable MYSYS:
  24. # mc68020  for Motorola 68020 (Sun-3, ..)
  25. # mc68030  for Motorola 68030 (Sun-3, ..)
  26. # sparc    for SPARC (SUN-4, ..)
  27. # i386     for i386 (Sun i386, ...)
  28.  
  29. # uncomment the line you need:
  30. # MYSYS=mc68020
  31. # MYSYS=mc68030
  32. # MYSYS=sparc
  33. # MYSYS=i386
  34. # MYSYS=`mach`  # this will work on Suns with SunOS > 4.0.0
  35.  
  36. exec gawk -vsys=$MYSYS '
  37. # commandline arguments:
  38. #  ARGV[0] = "gawk"
  39. #  ARGV[1] = processid
  40. #  ARGV[2] = filename
  41. #  ARGV[3] .. ARGV[ARGC-1] = compiler options
  42. BEGIN {
  43.   if (ARGC < 3) {
  44.     usage()
  45.     exit 1
  46.   }
  47.  
  48.   # Declaration of global variables
  49.   c_filename = ""
  50.   asm_filename = ""
  51.   cmdline = ""
  52.   asm_code = ""
  53.   c_code = ""
  54.   c_lineno = 0
  55.   oldlineno = 0
  56.   newlineno = 0
  57.   ignore_stabd = 0
  58.   num_of_fields = 0
  59.  
  60.   # check processor architecture and set sourcecode line_hint accordingly
  61.   if (sys == "sparc" || sys == "i386") {
  62.     line_hint = "^[ \t]*\.stabn.*"
  63.   }
  64.   else if (sys == "mc68020" || sys == "mc68030") {
  65.     line_hint = "^[ \t]*\.stabd.*"
  66.   }
  67.   else {
  68.     error("Processor type " sys " is not supported yet, sorry")
  69.   }
  70.  
  71.   parse_cmdline()
  72.  
  73.   printf("compiling %s to asm code\n", c_filename ) > "/dev/stderr"
  74.  
  75.   if (system(cmdline) != 0 ) {
  76.     error("Compilation of " c_filename " failed")
  77.   }
  78.  
  79.   printf("generating listing\n") > "/dev/stderr"
  80.  
  81.  
  82.   while ( getline asm_code < asm_filename > 0 ) {
  83.     if ( (ignore_stabd==0) && (asm_code ~ line_hint)) {
  84.       # source line hint found. Split the line into fields separated by commas.
  85.       # num_of_fields is 4 for sparc, 3 for m68k
  86.       num_of_fields = split(asm_code, fields, ",")
  87.       newlineno = fields[3] + 0 # the line number we are looking for is field 3
  88.  
  89.       if (newlineno > oldlineno) {
  90.         while ( newlineno > c_lineno ) {
  91.       getline c_code < c_filename
  92.       c_lineno++
  93.       printf("%4d %s\n", c_lineno, c_code)
  94.     }
  95.     oldlineno = newlineno
  96.       }
  97.     }
  98.     else if ( asm_code ~ ".*Ltext[ \t]*$" ) {
  99.       # filename hint found
  100.       if ( match(asm_code, c_filename)) {
  101.         ignore_stabd = 0
  102.       }
  103.       else {
  104.         ignore_stabd = 1
  105.       }
  106.     }
  107.     printf("\t\t\t%s\n", asm_code)
  108.   }
  109.  
  110.   # general cleanup
  111.   system("/bin/rm " asm_filename)
  112. }
  113.  
  114. function usage() {
  115.     printf("usage: %s filename compiler-options\n", argv[0]) > "/dev/stderr"
  116. }
  117.  
  118. function error(s) {
  119.     printf("error: %s\n", s) > "/dev/stderr"
  120.     exit 1
  121. }
  122.  
  123. function parse_cmdline(    i) {
  124.   # construct filenames to use
  125.   asm_filename = "/tmp/lister" ARGV[1] ".s"
  126.   ARGV[1] = ""
  127.   c_filename = ARGV[2]
  128.   ARGV[2] = ""
  129.  
  130.   # construct commandline to use
  131.   if ( match(c_filename, ".C") || match(c_filename, ".cc") ) {
  132.     cmdline = "g++"
  133.   }
  134.   else if (match(c_filename, ".c")) {
  135.     cmdline = "gcc"
  136.   }
  137.   else {
  138.     error("unknown extension for file " c_filename)
  139.   }
  140.  
  141.   cmdline = cmdline " -g -S -o " asm_filename
  142.  
  143.   # now we append the compiler options specified by the user
  144.   for (i = 3; i < ARGC; i++) {
  145.     cmdline = cmdline " " ARGV[i]
  146.     ARGV[i] = "" # we do not want to treat it as an inputfile
  147.   }
  148.  
  149.   # last but not least: the name of the file to compile
  150.   cmdline = cmdline " " c_filename
  151. }
  152.  
  153. ' $$ $*
  154.