#! /usr/bin/perl -w

use strict;

my $objdump = "avr-objdump";

die "usage: obj2enum <objectfile(s)>\n" unless $#ARGV >= 0;

while ($#ARGV >= 0) {
    my $ofile = shift @ARGV;

    # try to obtain stabs debugging information from each object file
    # in sequence
    if (open(P, "${objdump} -G $ofile|") == 0) {
	warn "Cannot objdump $ofile: $!\n";
	next;
    }

    # stabs entries belonging to an enum look like:
    # Symnum n_type n_othr n_desc n_value  n_strx String
    # 103    LSYM   0      0      00000000 4340   led_id_tag:T(16,5)=eLED_0:0,LED_1:1,LED_2:2,;
    while (<P>) {
	chomp;
	# only filter enum entries
	next unless m|T[(]\d+,\d+[)]=e(.*);|;
	# got one, now split off individual values
	foreach my $enumval (split(/,/, $1)) {
	    next unless $enumval =~ /^([A-Za-z_][A-Za-z_0-9]*):(\d)$/;
	    print "#define $1 $2\n";
	}
    }
    close(P);
}
