| 1 | package Language::Zcode::Parser::TXD; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base qw(Language::Zcode::Parser::Generic); | ||
| 5 | |||
| 6 | =head1 Language::Zcode::Parser::TXD | ||
| 7 | |||
| 8 | Z-code parser that uses pure Perl to find where the Z subroutines | ||
| 9 | start and end. | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | # Command to use when running txd. In theory, I could get a path for this | ||
| 14 | use constant TXD_COMMAND => "txd"; | ||
| 15 | # -d dump hex of opcodes and data | ||
| 16 | # -a will give Inform assembly style: | ||
| 17 | # lower case opcodes, no commas between arguments, | ||
| 18 | # "local1" instead of L01, label/~label instead of [TRUE|FALSE] label, | ||
| 19 | # ^ instead of literal newline -- this last one lets me use while (<>) | ||
| 20 | # -n print addresses ("a82") instead of labels ("?l0001") for branch ops | ||
| 21 | # -g doesn't write unneeded lines like 'Action routine for "verbos"' | ||
| 22 | use constant TXD_OPTIONS => "-dang -w 0"; | ||
| 23 | |||
| 24 | |||
| 25 | =head2 find_subs (filename) | ||
| 26 | |||
| 27 | Run txd on the given file to create bare (unparsed) LZ::Parser::Routine | ||
| 28 | objects. (Really to get the address where each subroutine starts.) | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | sub find_subs { | ||
| 33 | # TODO sub takes a path for txd? | ||
| 34 | my ($self, $infile) = @_; | ||
| 35 | my $txd_cmd = TXD_COMMAND . " " . TXD_OPTIONS; | ||
| 36 | my $TXD = new IO::File "$txd_cmd $infile |" or die "txd error: $!\n"; | ||
| 37 | # Read in pre-code header information | ||
| 38 | $_ = <$TXD> until $_ && /^\[Start of code at/; <$TXD>; | ||
| 39 | |||
| 40 | my @Subs = (); | ||
| 41 | my ($rtn); #NOT in while loop; we only know to end a sub as we read the next | ||
| 42 | while (<$TXD>) { | ||
| 43 | if (/^(Main )?routine ([\da-f]+), (\d+) locals?/i) { | ||
| 44 | my $addr = hex $2; # my $num_locals = $3; | ||
| 45 | # Previous sub, if any, (and any padding 0's) must have ended by now | ||
| 46 | $rtn->end($addr -1) if @Subs; | ||
| 47 | |||
| 48 | # Now create the new sub, and read all of its commands | ||
| 49 | $rtn = new Language::Zcode::Parser::Routine ($addr); | ||
| 50 | push @Subs, $rtn; | ||
| 51 | $self->get_txd_commands($rtn, $TXD); | ||
| 52 | |||
| 53 | } elsif (/^\[End of code at ([0-9a-f]+)/) { | ||
| 54 | $rtn->end(hex($1)-1); | ||
| 55 | last; # done reading routines | ||
| 56 | |||
| 57 | } elsif (/^orphan code fragment/) { | ||
| 58 | # Keep putting this code into previous named routine. | ||
| 59 | # IRL, I think this code can NEVER be reached ('jump sp' | ||
| 60 | # is not AFAIK legal) so we could ignore it. | ||
| 61 | $self->get_txd_commands($rtn, $TXD); | ||
| 62 | |||
| 63 | } else { | ||
| 64 | die "Unknown line outside sub: $_"; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | close($TXD); | ||
| 68 | |||
| 69 | return @Subs; | ||
| 70 | } | ||
| 71 | |||
| 72 | =head2 get_txd_commands (Routine, filehandle) | ||
| 73 | |||
| 74 | Read the commands in a Z-code sub from the given filehandle (which is a pipe | ||
| 75 | from txd) and add them to the given LZ::Parser::Routine. | ||
| 76 | |||
| 77 | OR read commands in an orphan code fragment and add them to the routine's | ||
| 78 | already existing commands. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub get_txd_commands { | ||
| 83 | my ($self, $rtn, $fh) = @_; | ||
| 84 | my @commands; | ||
| 85 | <$fh>; # blank line always proceeds list of commands | ||
| 86 | while (<$fh>) { | ||
| 87 | last if /^$/; # blank line means done with subroutine or orphan fragment | ||
| 88 | |||
| 89 | # txd -d sometimes runs over into two lines | ||
| 90 | chomp, $_ .= <$fh> if /^ *([0-9a-f]+): *(( [a-f\d]{2})+) $/; | ||
| 91 | |||
| 92 | if (/^ *([0-9a-f]+): *(( \.+| [a-f\d]{2})+) +(\w+)\s+(.*)$/) { | ||
| 93 | push @commands, { | ||
| 94 | # (I use base 10 for jump/call and labels) | ||
| 95 | addr=>hex($1), hexbytes => $2, opcode => $4, args => $5 | ||
| 96 | }; | ||
| 97 | } else { | ||
| 98 | die "Unhandled line in sub: '$_'"; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | $rtn->txd_commands($rtn->txd_commands, @commands); | ||
| 103 | # As of this moment, last command we read is the last in the sub | ||
| 104 | $rtn->last_command_address(($rtn->txd_commands)[-1]->{addr}); | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; |