Reworked test files for better error reporting
[nasm/perl-rewrite.git] / perl / lib / Nasm / Regs.pm
blobb2cb9fb3efebd5883a7057dbc3aceab51b6daf6c
1 =head1 NAME
3 Nasm::Regs
5 =head1 DESCRIPTION
7 =head1 Subroutines
9 =cut
11 package Nasm::Regs;
12 use strict;
13 use warnings;
15 require Nasm::Regs::Register;
16 require Nasm::Regs::Format;
18 =head2 new
20 Creates Nasm::Regs object
22 =cut
23 sub new{
24 my($class,$filename) = @_;
25 my $self = bless {}, $class;
27 if( @_ > 1 and $filename ){
28 $self->ProcessFile($filename);
31 return $self;
35 =head2 ProcessFile
37 Loads the file
39 =cut
40 sub ProcessFile{
41 my($self,$filename) = @_;
42 my $file;
44 if( $filename eq '-'){
45 $file = *{STDIN}{IO};
46 }else{
47 open $file, '<', $filename or die;
49 $self->{filename} = $filename;
51 my %current =( line_number => -1 );
53 while( my $line = <$file> ){
54 $current{line_number}++;
55 chomp $line;
56 $line =~ s/\s*(?:\#.*)?$//;
57 next unless length $line;
59 $current{line} = $line;
60 $self->_ProcessLine(\%current);
63 close $file;
64 return $self;
69 sub _ProcessLine{
70 my($self,$current) = @_;
72 unless( $current->{line} =~ /^\s*(\S+)\s*(\S+)\s*(\S+)\s*([0-9]+)$/ ){
73 die;
76 my ($register,$a_class,$d_classes,$x86regno) = ($1,$2,$3,$4);
78 my @reg_list;
80 if ( $register =~ m{
82 (.*?) (\d+) - (\d+) (.*)
84 }x) {
85 for my $i ($2..$3){
86 push @reg_list, $1. $i . $4
88 } else {
90 @reg_list = $register;
93 # force numeric conversion
94 $x86regno += 0;
96 my @d_classes = split ',', $d_classes;
97 #my %d_is_class;
99 for my $register(@reg_list){
100 my $reg_obj = Nasm::Regs::Register->new($register,$a_class,[@d_classes],$x86regno);
101 $self->{register}{$register} = $reg_obj;
103 for my $class (@d_classes){
104 $self->{disassembler}{$class}[$x86regno] = $reg_obj
107 $x86regno++;
114 =head2 names
116 Returns a sorted list of RegisterNames
118 =cut
119 sub names{
120 my($self) = @_;
121 # deprecated
122 return $self->RegisterNames;
125 =head2 RegisterNames
127 Returns a sorted list of RegisterNames
129 =cut
130 sub RegisterNames{
131 my($self) = @_;
132 my @keys = sort grep { length $_ } keys %{$self->{register}};
134 return @keys if wantarray;
135 return \@keys;
140 =head2 Register( $register_name )
142 Returns a Register object
144 =cut
145 sub Register{
146 my($self,$register_name) = @_;
147 return $self->{register}{$register_name};
150 =head2 DisassemblerClasses
152 Returns a sorted list of DisassemblerClasses
154 =cut
155 sub DisassemblerClasses{
156 my($self) = @_;
157 my @return = sort keys %{$self->{disassembler}};
159 return @return if wantarray;
160 return \@return;
163 =head2 DisassemblerClass( $class )
167 =cut
168 sub DisassemblerClass{
169 my($self,$class) = @_;
170 my $return = $self->{disassembler}{$class};
171 return @$return if wantarray;
172 return $return;
176 =head2 format( $format_type )
178 Returns regs info in a given format
180 =cut
181 sub format{
182 my($self,$fmt) = @_;
183 require Nasm::Regs::Format;
184 Nasm::Regs::Format::format($self,$fmt);
188 =head2 filename
190 Returns the name of the file the data was loaded from
192 =cut
193 sub filename{
194 my($self) = @_;
195 return $self->{filename};