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