Annotation of GNUtools/cc/listing, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.