tools/elf4rom/libs/dwarf-20071209/dwarfdump/tag_tree.c
changeset 34 92d87f2e53c2
equal deleted inserted replaced
33:1af5c1be89f8 34:92d87f2e53c2
       
     1 /* 
       
     2   Copyright (C) 2000,2004,2005 Silicon Graphics, Inc.  All Rights Reserved.
       
     3 
       
     4   This program is free software; you can redistribute it and/or modify it
       
     5   under the terms of version 2 of the GNU General Public License as
       
     6   published by the Free Software Foundation.
       
     7 
       
     8   This program is distributed in the hope that it would be useful, but
       
     9   WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
       
    11 
       
    12   Further, this software is distributed without any warranty that it is
       
    13   free of the rightful claim of any third person regarding infringement
       
    14   or the like.  Any license provided herein, whether implied or
       
    15   otherwise, applies only to this software file.  Patent licenses, if
       
    16   any, provided herein do not apply to combinations of this program with
       
    17   other software, or any other product whatsoever.
       
    18 
       
    19   You should have received a copy of the GNU General Public License along
       
    20   with this program; if not, write the Free Software Foundation, Inc., 51
       
    21   Franklin Street - Fifth Floor, Boston MA 02110-1301, USA.
       
    22 
       
    23   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
       
    24   Mountain View, CA 94043, or:
       
    25 
       
    26   http://www.sgi.com
       
    27 
       
    28   For further information regarding this notice, see:
       
    29 
       
    30   http://oss.sgi.com/projects/GenInfo/NoticeExplan
       
    31 
       
    32 
       
    33 
       
    34 $Header: /plroot/cmplrs.src/v7.4.5m/.RCS/PL/dwarfdump/RCS/tag_tree.c,v 1.8 2005/12/01 17:34:59 davea Exp $ */
       
    35 #include <dwarf.h>
       
    36 #include <stdio.h>
       
    37 #include <stdlib.h>		/* For exit() declaration etc. */
       
    38 #include <errno.h>		/* For errno declaration. */
       
    39 
       
    40 
       
    41 /*  The following is the magic token used to
       
    42     distinguish real tags/attrs from group-delimiters.
       
    43     Blank lines have been eliminated by an awk script.
       
    44 */
       
    45 #define MAGIC_TOKEN_VALUE 0xffffffff
       
    46 
       
    47 /* Expected input format
       
    48 
       
    49 0xffffffff
       
    50 value of a tag
       
    51 value of a standard tag that may be a child ofthat tag
       
    52 ...
       
    53 0xffffffff
       
    54 value of a tag
       
    55 value of a standard tag that may be a child ofthat tag
       
    56 ...
       
    57 0xffffffff
       
    58 ...
       
    59 
       
    60 No blank lines or commentary allowed, no symbols, just numbers.
       
    61 
       
    62 
       
    63 */
       
    64 
       
    65 
       
    66 /* We don't need really long lines: the input file is simple. */
       
    67 #define MAX_LINE_SIZE 1000
       
    68 
       
    69 /* 1 more than the higest number in the DW_TAG defines. */
       
    70 #define TABLE_SIZE 0x41
       
    71 
       
    72 /* Enough entries to have a bit for each standard legal tag. */
       
    73 #define COLUMN_COUNT 2
       
    74 
       
    75 /* Bits per 'int' to mark legal attrs. */
       
    76 #define BITS_PER_WORD 32
       
    77 
       
    78 
       
    79 
       
    80 #define TABLE_SIZE 0x41
       
    81 
       
    82 static unsigned int
       
    83     tag_tree_combination_table[TABLE_SIZE][COLUMN_COUNT];
       
    84 static char *tag_name[] = {
       
    85     "0x00",
       
    86     "0x01 DW_TAG_array_type",
       
    87     "0x02 DW_TAG_class_type",
       
    88     "0x03 DW_TAG_entry_point",
       
    89     "0x04 DW_TAG_enumeration_type",
       
    90     "0x05 DW_TAG_formal_parameter",
       
    91     "0x06",
       
    92     "0x07",
       
    93     "0x08 DW_TAG_imported_declaration",
       
    94     "0x09",
       
    95     "0x0a DW_TAG_label",
       
    96     "0x0b DW_TAG_lexical_block",
       
    97     "0x0c",
       
    98     "0x0d DW_TAG_member",
       
    99     "0x0e",
       
   100     "0x0f DW_TAG_pointer_type",
       
   101     "0x10 DW_TAG_reference_type",
       
   102     "0x11 DW_TAG_compile_unit",
       
   103     "0x12 DW_TAG_string_type",
       
   104     "0x13 DW_TAG_structure_type",
       
   105     "0x14",
       
   106     "0x15 DW_TAG_subroutine_type",
       
   107     "0x16 DW_TAG_typedef",
       
   108     "0x17 DW_TAG_union_type",
       
   109     "0x18 DW_TAG_unspecified_parameters",
       
   110     "0x19 DW_TAG_variant",
       
   111     "0x1a DW_TAG_common_block",
       
   112     "0x1b DW_TAG_common_inclusion",
       
   113     "0x1c DW_TAG_inheritance",
       
   114     "0x1d DW_TAG_inlined_subroutine",
       
   115     "0x1e DW_TAG_module",
       
   116     "0x1f DW_TAG_ptr_to_member_type",
       
   117     "0x20 DW_TAG_set_type",
       
   118     "0x21 DW_TAG_subrange_type",
       
   119     "0x22 DW_TAG_with_stmt",
       
   120     "0x23 DW_TAG_access_declaration",
       
   121     "0x24 DW_TAG_base_type",
       
   122     "0x25 DW_TAG_catch_block",
       
   123     "0x26 DW_TAG_const_type",
       
   124     "0x27 DW_TAG_constant",
       
   125     "0x28 DW_TAG_enumerator",
       
   126     "0x29 DW_TAG_file_type",
       
   127     "0x2a DW_TAG_friend",
       
   128     "0x2b DW_TAG_namelist",
       
   129     "0x2c DW_TAG_namelist_item",
       
   130     "0x2d DW_TAG_packed_type",
       
   131     "0x2e DW_TAG_subprogram",
       
   132     "0x2f DW_TAG_template_type_parameter",
       
   133     "0x30 DW_TAG_template_value_parameter",
       
   134     "0x31 DW_TAG_thrown_type",
       
   135     "0x32 DW_TAG_try_block",
       
   136     "0x33 DW_TAG_variant_part",
       
   137     "0x34 DW_TAG_variable",
       
   138     "0x35 DW_TAG_volatile_type",
       
   139     "0x36 DW_TAG_dwarf_procedure",
       
   140     "0x37 DW_TAG_restrict_type",
       
   141     "0x38 DW_TAG_interface_type",
       
   142     "0x39 DW_TAG_namespace",
       
   143     "0x3a DW_TAG_imported_module",
       
   144     "0x3b DW_TAG_unspecified_type",
       
   145     "0x3c DW_TAG_partial_unit",
       
   146     "0x3d DW_TAG_imported_unit",
       
   147     "0x3e",			/* was DW_TAG_mutable_type, removed
       
   148 				   from DWARF3f. */
       
   149     "0x3f DW_TAG_condition",
       
   150     "0x40 DW_TAG_shared_type",
       
   151 };
       
   152 
       
   153 static int linecount = 0;
       
   154 static char line_in[MAX_LINE_SIZE];
       
   155 
       
   156 #define IS_EOF 1
       
   157 #define NOT_EOF 0
       
   158 
       
   159 #define MAX_LINE_SIZE 1000
       
   160 
       
   161 
       
   162 static void
       
   163 bad_line_input(char *msg)
       
   164 {
       
   165     fprintf(stderr,
       
   166 	    "tag_tree table build failed %s, line %d: \"%s\"  \n",
       
   167 	    msg, linecount, line_in);
       
   168     exit(1);
       
   169 
       
   170 }
       
   171 static void
       
   172 trim_newline(char *line, int max)
       
   173 {
       
   174     char *end = line + max - 1;
       
   175 
       
   176     for (; *line && (line < end); ++line) {
       
   177 	if (*line == '\n') {
       
   178 	    /* Found newline, drop it */
       
   179 	    *line = 0;
       
   180 	    return;
       
   181 	}
       
   182     }
       
   183 
       
   184     return;
       
   185 }
       
   186 
       
   187 
       
   188 /* Reads a value from the text table. 
       
   189    Exits  with non-zero status 
       
   190    if the table is erroneous in some way. 
       
   191 */
       
   192 static int
       
   193 read_value(unsigned int *outval)
       
   194 {
       
   195     char *res = 0;
       
   196     FILE *file = stdin;
       
   197     unsigned long lval;
       
   198     char *strout = 0;
       
   199 
       
   200     ++linecount;
       
   201     *outval = 0;
       
   202     res = fgets(line_in, sizeof(line_in), file);
       
   203     if (res == 0) {
       
   204 	if (ferror(file)) {
       
   205 	    fprintf(stderr,
       
   206 		    "tag_attr: Error reading table, %d lines read\n",
       
   207 		    linecount);
       
   208 	    exit(1);
       
   209 	}
       
   210 	if (feof(file)) {
       
   211 	    return IS_EOF;
       
   212 	}
       
   213 	/* impossible */
       
   214 	fprintf(stderr, "tag_attr: Impossible error reading table, "
       
   215 		"%d lines read\n", linecount);
       
   216 	exit(1);
       
   217     }
       
   218     trim_newline(line_in, sizeof(line_in));
       
   219     errno = 0;
       
   220     lval = strtoul(line_in, &strout, 0);
       
   221     if (strout == line_in) {
       
   222 	bad_line_input("bad number input!");
       
   223     }
       
   224     if (errno != 0) {
       
   225 	int myerr = errno;
       
   226 
       
   227 	fprintf(stderr, "tag_attr errno %d\n", myerr);
       
   228 	bad_line_input("invalid number on line");
       
   229     }
       
   230     *outval = (int) lval;
       
   231     return NOT_EOF;
       
   232 }
       
   233 
       
   234 
       
   235 int
       
   236 main()
       
   237 {
       
   238     int i;
       
   239     unsigned int num;
       
   240     int input_eof;
       
   241 
       
   242 
       
   243     input_eof = read_value(&num);	/* 0xffffffff */
       
   244     if (IS_EOF == input_eof) {
       
   245 	bad_line_input("Empty input file");
       
   246     }
       
   247     if (num != MAGIC_TOKEN_VALUE) {
       
   248 	bad_line_input("Expected 0xffffffff");
       
   249     }
       
   250 
       
   251     while (!feof(stdin)) {
       
   252 	unsigned int tag;
       
   253 
       
   254 	input_eof = read_value(&tag);
       
   255 	if (IS_EOF == input_eof) {
       
   256 	    /* Reached normal eof */
       
   257 	    break;
       
   258 	}
       
   259 	if (tag >= TABLE_SIZE) {
       
   260 	    bad_line_input("tag value exceeds table size");
       
   261 	}
       
   262 	input_eof = read_value(&num);
       
   263 	if (IS_EOF == input_eof) {
       
   264 	    bad_line_input("Not terminated correctly..");
       
   265 	}
       
   266 
       
   267 	while (num != 0xffffffff) {
       
   268 	    int idx = num / BITS_PER_WORD;
       
   269 	    int bit = num % BITS_PER_WORD;
       
   270 
       
   271 	    if (idx >= COLUMN_COUNT) {
       
   272 		bad_line_input("too many TAGs: table incomplete.");
       
   273 	    }
       
   274 
       
   275 	    tag_tree_combination_table[tag][idx] |= (1 << bit);
       
   276 	    input_eof = read_value(&num);
       
   277 	    if (IS_EOF == input_eof) {
       
   278 		bad_line_input("Not terminated correctly.");
       
   279 	    }
       
   280 	}
       
   281     }
       
   282     printf
       
   283 	("static unsigned int tag_tree_combination_table [ ][%d] = {\n",
       
   284 	 COLUMN_COUNT);
       
   285     for (i = 0; i < TABLE_SIZE; i++) {
       
   286 	printf("/* %-37s*/\n", tag_name[i]);
       
   287 	printf("    { %#.8x, %#.8x},\n",
       
   288 	       tag_tree_combination_table[i][0],
       
   289 	       tag_tree_combination_table[i][1]);
       
   290     }
       
   291     printf("};\n");
       
   292     return (0);
       
   293 }