Annotation of gcc/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: 
        !            33: # uncomment the line you need:
        !            34: # MYSYS=mc68020
        !            35: # MYSYS=mc68030
        !            36: # MYSYS=sparc
        !            37: # MYSYS=i386
        !            38: # MYSYS=`mach`  # this will work on Suns with SunOS > 4.0.0
        !            39: 
        !            40: exec gawk -vsys=$MYSYS '
        !            41: # commandline arguments:
        !            42: #  ARGV[0] = "gawk"
        !            43: #  ARGV[1] = processid
        !            44: #  ARGV[2] = filename
        !            45: #  ARGV[3] .. ARGV[ARGC-1] = compiler options
        !            46: BEGIN {
        !            47:   if (ARGC < 3) {
        !            48:     usage()
        !            49:     exit 1
        !            50:   }
        !            51: 
        !            52:   # Declaration of global variables
        !            53:   c_filename = ""
        !            54:   asm_filename = ""
        !            55:   cmdline = ""
        !            56:   asm_code = ""
        !            57:   c_code = ""
        !            58:   c_lineno = 0
        !            59:   oldlineno = 0
        !            60:   newlineno = 0
        !            61:   ignore_stabd = 0
        !            62:   num_of_fields = 0
        !            63: 
        !            64:   # check processor architecture and set sourcecode line_hint accordingly
        !            65:   if (sys == "sparc" || sys == "i386") {
        !            66:     line_hint = "^[ \t]*\.stabn.*"
        !            67:   }
        !            68:   else if (sys == "mc68020" || sys == "mc68030") {
        !            69:     line_hint = "^[ \t]*\.stabd.*"
        !            70:   }
        !            71:   else {
        !            72:     error("Processor type " sys " is not supported yet, sorry")
        !            73:   }
        !            74: 
        !            75:   parse_cmdline()
        !            76: 
        !            77:   printf("compiling %s to asm code\n", c_filename ) > "/dev/stderr"
        !            78: 
        !            79:   if (system(cmdline) != 0 ) {
        !            80:     error("Compilation of " c_filename " failed")
        !            81:   }
        !            82: 
        !            83:   printf("generating listing\n") > "/dev/stderr"
        !            84: 
        !            85: 
        !            86:   while ( getline asm_code < asm_filename > 0 ) {
        !            87:     if ( (ignore_stabd==0) && (asm_code ~ line_hint)) {
        !            88:       # source line hint found. Split the line into fields seperated by commas.
        !            89:       # num_of_fields is 4 for sparc, 3 for m68k
        !            90:       num_of_fields = split(asm_code, fields, ",")
        !            91:       newlineno = fields[3] + 0 # the line number we are looking for is field 3
        !            92: 
        !            93:       if (newlineno > oldlineno) {
        !            94:         while ( newlineno > c_lineno ) {
        !            95:          getline c_code < c_filename
        !            96:          c_lineno++
        !            97:          printf("%4d %s\n", c_lineno, c_code)
        !            98:        }
        !            99:        oldlineno = newlineno
        !           100:       }
        !           101:     }
        !           102:     else if ( asm_code ~ ".*Ltext[ \t]*$" ) {
        !           103:       # filename hint found
        !           104:       if ( match(asm_code, c_filename)) {
        !           105:         ignore_stabd = 0
        !           106:       }
        !           107:       else {
        !           108:         ignore_stabd = 1
        !           109:       }
        !           110:     }
        !           111:     printf("\t\t\t%s\n", asm_code)
        !           112:   }
        !           113: 
        !           114:   # general cleanup
        !           115:   system("/bin/rm " asm_filename)
        !           116: }
        !           117: 
        !           118: function usage() {
        !           119:     printf("usage: %s filename compiler-options\n", argv[0]) > "/dev/stderr"
        !           120: }
        !           121: 
        !           122: function error(s) {
        !           123:     printf("error: %s\n", s) > "/dev/stderr"
        !           124:     exit 1
        !           125: }
        !           126: 
        !           127: function parse_cmdline(    i) {
        !           128:   # construct filenames to use
        !           129:   asm_filename = "/tmp/lister" ARGV[1] ".s"
        !           130:   ARGV[1] = ""
        !           131:   c_filename = ARGV[2]
        !           132:   ARGV[2] = ""
        !           133: 
        !           134:   # construct commandline to use
        !           135:   if ( match(c_filename, ".C") || match(c_filename, ".cc") ) {
        !           136:     cmdline = "g++"
        !           137:   }
        !           138:   else if (match(c_filename, ".c")) {
        !           139:     cmdline = "gcc"
        !           140:   }
        !           141:   else {
        !           142:     error("unknown extension for file " c_filename)
        !           143:   }
        !           144: 
        !           145:   cmdline = cmdline " -g -S -o " asm_filename
        !           146: 
        !           147:   # now we append the compiler options specified by the user
        !           148:   for (i = 3; i < ARGC; i++) {
        !           149:     cmdline = cmdline " " ARGV[i]
        !           150:     ARGV[i] = "" # we do not want to treat it as an inputfile
        !           151:   }
        !           152: 
        !           153:   # last but not least: the name of the file to compile
        !           154:   cmdline = cmdline " " c_filename
        !           155: }
        !           156: 
        !           157: ' $$ $*

unix.superglobalmegacorp.com

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