13 return sub { $_[0] >= (2**($bits-1)) ? $_[0]-(2**$bits) : $_[0]; };
16 use constant OPCODE_E => [qw[
18 MUL_F MUL_V MUL_FV MUL_VF
22 EQ_F EQ_V EQ_S EQ_E EQ_FNC
23 NE_F NE_V NE_S NE_E NE_FNC
25 LOAD_F LOAD_V LOAD_S LOAD_ENT LOAD_FLD LOAD_FNC
27 STORE_F STORE_V STORE_S STORE_ENT STORE_FLD STORE_FNC
28 STOREP_F STOREP_V STOREP_S STOREP_ENT STOREP_FLD STOREP_FNC
30 NOT_F NOT_V NOT_S NOT_ENT NOT_FNC
32 CALL0 CALL1 CALL2 CALL3 CALL4 CALL5 CALL6 CALL7 CALL8
38 use constant ETYPE_E => [qw[
48 use constant DEF_SAVEGLOBAL => 32768;
52 return 3 if $type eq 'vector';
61 return { a => 'inglobalvec', b => 'ipoffset', isjump => 'b', isconditional => 1 };
65 return { a => 'inglobal', b => 'ipoffset', isjump => 'b', isconditional => 1 };
69 return { a => 'ipoffset', isjump => 'a', isconditional => 0 };
71 if($op =~ /^ADD_V$|^SUB_V$/)
73 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobalvec' };
75 if($op =~ /^MUL_V$|^EQ_V$|^NE_V$/)
77 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobal' };
81 return { a => 'inglobal', b => 'inglobalvec', c => 'outglobalvec' };
85 return { a => 'inglobalvec', b => 'inglobal', c => 'outglobalvec' };
89 return { a => 'inglobal', b => 'inglobal', c => 'outglobalvec' };
93 return { a => 'inglobalvec', c => 'outglobal' };
97 return { a => 'inglobal', c => 'outglobal' };
101 return { a => 'inglobalvec', b => 'inglobal' };
105 return { a => 'inglobalvec', b => 'outglobalvec' };
107 if($op =~ /^STOREP_/)
109 return { a => 'inglobal', b => 'inglobal' };
113 return { a => 'inglobal', b => 'outglobal' };
117 return { a => 'inglobalfunc', iscall => 1 };
119 if($op =~ /^DONE$|^RETURN$/)
121 return { a => 'inglobalvec', isreturn => 1 };
123 return { a => 'inglobal', b => 'inglobal', c => 'outglobal' };
126 use constant TYPES => {
127 int => ['V', 4, signed 32],
128 ushort => ['v', 2, id],
129 short => ['v', 2, signed 16],
130 opcode => ['v', 2, sub { OPCODE_E->[$_[0]] or do { warn "Invalid opcode: $_[0]"; "INVALID#$_[0]"; }; }],
131 float => ['f', 4, id],
132 uchar8 => ['a8', 8, sub { [unpack 'C8', $_[0]] }],
133 global => ['i', 4, sub { { int => $_[0], float => unpack "f", pack "L", $_[0] }; }],
134 deftype => ['v', 2, sub { { type => ETYPE_E->[$_[0] & ~DEF_SAVEGLOBAL], save => !!($_[0] & DEF_SAVEGLOBAL) }; }],
137 use constant DPROGRAMS_T => [
140 [int => 'ofs_statements'],
141 [int => 'numstatements'],
142 [int => 'ofs_globaldefs'],
143 [int => 'numglobaldefs'],
144 [int => 'ofs_fielddefs'],
145 [int => 'numfielddefs'],
146 [int => 'ofs_functions'],
147 [int => 'numfunctions'],
148 [int => 'ofs_strings'],
149 [int => 'numstrings'],
150 [int => 'ofs_globals'],
151 [int => 'numglobals'],
152 [int => 'entityfields']
155 use constant DSTATEMENT_T => [
162 use constant DDEF_T => [
168 use constant DGLOBAL_T => [
172 use constant DFUNCTION_T => [
173 [int => 'first_statement'],
174 [int => 'parm_start'],
180 [uchar8 => 'parm_size'],
185 my ($fh, $start, $len) = @_;
188 $len == read $fh, my $buf, $len
193 sub parse_section($$$$$)
195 my ($fh, $struct, $start, $len, $cnt) = @_;
198 $itemlen += TYPES->{$_->[0]}->[1]
200 my $packspec = join '', map { TYPES->{$_->[0]}->[0]; } @$struct;
201 my @packnames = map { $_->[1]; } @$struct;
203 $len = $cnt * $itemlen
204 if not defined $len and defined $cnt;
205 $cnt = int($len / $itemlen)
206 if not defined $cnt and defined $len;
207 die "Invalid length specification"
208 unless defined $len and defined $cnt and $len == $cnt * $itemlen;
209 die "Invalid length specification in scalar context"
210 unless wantarray or $cnt == 1;
216 $itemlen == read $fh, my $buf, $itemlen
219 @h{@packnames} = unpack $packspec, $buf;
220 $h{$_->[1]} = TYPES->{$_->[0]}->[2]->($h{$_->[1]})
230 sub nfa_default_state_checker()
235 my ($ip, $state) = @_;
236 return $seen{"$ip $state"}++;
242 my ($progs, $ip, $state, $copy_handler, $state_checker, $instruction_handler) = @_;
244 my $statements = $progs->{statements};
249 no warnings 'recursion';
251 my ($ip, $state) = @_;
257 if $state_checker->($ip, $state);
259 my $s = $statements->[$ip];
260 my $c = checkop $s->{op};
262 if(($ret = $instruction_handler->($ip, $state, $s, $c)))
275 my $funcid = $progs->{globals}[$func]{v}{int};
276 my $funcobj = $progs->{functions}[$funcid];
277 if($funcobj && $funcobj->{first_statement} < 0) # builtin
279 my $def = $progs->{globaldef_byoffset}->($func);
281 if $def->{debugname} eq '_error';
287 if($c->{isconditional})
291 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
299 $nfa->($ip+1, $copy_handler->($state));
300 $ip += $s->{$c->{isjump}};
305 $ip += $s->{$c->{isjump}};
317 $nfa->($ip, $copy_handler->($state));
322 my ($progs, $g) = @_;
327 elsif($g->{int} > 0 && $g->{int} < 16777216)
329 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
331 return str($progs->{getstring}->($g->{int}));
335 return $g->{int} . "i";
344 use constant PRE_MARK_STATEMENT => "";
345 use constant POST_MARK_STATEMENT => "";
346 use constant PRE_MARK_OPERAND => "*** ";
347 use constant POST_MARK_OPERAND => " ***";
349 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
350 use constant OPERAND_FORMAT => "%s";
351 use constant OPERAND_SEPARATOR => ", ";
352 use constant INSTRUCTION_SEPARATOR => "\n";
357 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
361 sub disassemble_function($$;$)
363 my ($progs, $func, $highlight) = @_;
365 print "$func->{debugname}:\n";
367 my $initializer = sub
370 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
375 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
376 printf OPERAND_FORMAT, "$func->{parm_start}";
377 print INSTRUCTION_SEPARATOR;
379 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
380 printf OPERAND_FORMAT, "$func->{locals}";
381 print INSTRUCTION_SEPARATOR;
383 my %override_locals = ();
384 my $p = $func->{parm_start};
385 for(0..($func->{numparms}-1))
387 $override_locals{$p} //= "argv_$_";
388 for my $comp(0..($func->{parm_size}[$_]-1))
390 $override_locals{$p} //= "argv_$_\[$comp]";
393 printf INSTRUCTION_FORMAT, '', '', '.ARG';
394 printf OPERAND_FORMAT, "argv_$_";
395 print OPERAND_SEPARATOR;
396 printf OPERAND_FORMAT, $func->{parm_size}[$_];
397 print INSTRUCTION_SEPARATOR;
399 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
402 if exists $override_locals{$_};
403 $override_locals{$_} = "local_$_";
405 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
406 printf OPERAND_FORMAT, "local_$_";
408 print INSTRUCTION_SEPARATOR;
415 return $override_locals{$ofs}
416 if exists $override_locals{$ofs};
417 my $def = $progs->{globaldef_byoffset}->($ofs);
418 return $def->{debugname};
423 my ($ip, $type, $operand) = @_;
424 if($type eq 'inglobal')
426 my $name = $getname->($operand);
427 printf OPERAND_FORMAT, "$name";
429 elsif($type eq 'outglobal')
431 my $name = $getname->($operand);
432 printf OPERAND_FORMAT, "&$name";
434 elsif($type eq 'inglobalvec')
436 my $name = $getname->($operand);
437 printf OPERAND_FORMAT, "$name\[\]";
439 elsif($type eq 'outglobalvec')
441 my $name = $getname->($operand);
442 printf OPERAND_FORMAT, "&$name\[\]";
444 elsif($type eq 'inglobalfunc')
446 my $name = $getname->($operand);
447 printf OPERAND_FORMAT, "$name()";
449 elsif($type eq 'ipoffset')
451 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
455 die "unknown type: $type";
461 run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
464 my ($ip, $state, $s, $c) = @_;
467 if(my $j = $c->{isjump})
469 my $t = $ip + $s->{$j};
470 $come_from{$t}{$ip} = $c->{isconditional};
477 for my $ip(sort { $a <=> $b } keys %statements)
479 if($ip == $func->{first_statement})
481 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
482 print INSTRUCTION_SEPARATOR;
484 if(defined $ipprev && $ip != $ipprev + 1)
486 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
487 printf OPERAND_FORMAT, $ip - $ipprev - 1;
488 print INSTRUCTION_SEPARATOR;
490 if(my $cf = $come_from{$ip})
492 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
494 for(sort { $a <=> $b } keys %$cf)
496 print OPERAND_SEPARATOR
498 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
500 print INSTRUCTION_SEPARATOR;
503 my $op = $progs->{statements}[$ip]{op};
504 my $ipt = $progs->{statements}[$ip];
505 my $opprop = checkop $op;
507 print PRE_MARK_STATEMENT
508 if $highlight and $highlight->{$ip};
510 my $showip = $opprop->{isjump};
511 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? "<!>" : "", $op;
517 if not defined $opprop->{$o};
518 print OPERAND_SEPARATOR
520 print PRE_MARK_OPERAND
521 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
522 $operand->($ip, $opprop->{$o}, $ipt->{$o});
523 print POST_MARK_OPERAND
524 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
527 print POST_MARK_STATEMENT
528 if $highlight and $highlight->{$ip};
530 print INSTRUCTION_SEPARATOR;
534 sub find_uninitialized_locals($$)
536 my ($progs, $func) = @_;
540 if $func->{first_statement} < 0; # builtin
542 print STDERR "Checking $func->{debugname}...\n";
544 my $p = $func->{parm_start};
545 for(0..($func->{numparms}-1))
547 $p += $func->{parm_size}[$_];
550 use constant WATCHME_R => 1;
551 use constant WATCHME_W => 2;
552 use constant WATCHME_X => 4;
553 use constant WATCHME_T => 8;
554 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
556 for(keys %{$progs->{temps}})
558 $watchme{$_} = WATCHME_T | WATCHME_X
559 if not exists $watchme{$_};
562 my %write_places = ();
563 run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
566 my ($ip, $state, $s, $c) = @_;
571 unless defined $type;
574 if($type eq 'inglobal' || $type eq 'inglobalfunc')
576 $watchme{$ofs} |= WATCHME_R;
578 elsif($type eq 'inglobalvec')
580 $watchme{$ofs} |= WATCHME_R;
581 $watchme{$ofs+1} |= WATCHME_R;
582 $watchme{$ofs+2} |= WATCHME_R;
584 elsif($type eq 'outglobal')
586 $watchme{$ofs} |= WATCHME_W;
587 $write_places{$ip}{$_} = [$ofs]
588 if $watchme{$ofs} & WATCHME_X;
590 elsif($type eq 'outglobalvec')
592 $watchme{$ofs} |= WATCHME_W;
593 $watchme{$ofs+1} |= WATCHME_W;
594 $watchme{$ofs+2} |= WATCHME_W;
595 my @l = grep { $watchme{$_} & WATCHME_X } $ofs .. ($ofs+2);
596 $write_places{$ip}{$_} = \@l
607 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
611 if not keys %watchme;
616 flags => $watchme{$_},
617 valid => [0, undef, undef]
621 # mark parameters as initialized
622 for($func->{parm_start} .. ($p-1))
624 $watchme{$_}{valid} = [1, undef, undef]
625 if defined $watchme{$_};
627 # an initial run of STORE instruction is for receiving extra parameters
628 # (beyond 8). Only possible if the function is declared as having 8 params.
629 # Extra parameters behave otherwise like temps, but are initialized at
631 for($func->{first_statement} .. (@{$progs->{statements}}-1))
633 my $s = $progs->{statements}[$_];
634 if($s->{op} eq 'STORE_V')
636 $watchme{$s->{a}}{valid} = [1, undef, undef]
637 if defined $watchme{$s->{a}};
638 $watchme{$s->{a}+1}{valid} = [1, undef, undef]
639 if defined $watchme{$s->{a}+1};
640 $watchme{$s->{a}+2}{valid} = [1, undef, undef]
641 if defined $watchme{$s->{a}+2};
643 elsif($s->{op} =~ /^STORE_/)
645 $watchme{$s->{a}}{valid} = [1, undef, undef]
646 if defined $watchme{$s->{a}};
656 run_nfa $progs, $func->{first_statement}, \%watchme,
659 return { map { $_ => { %{$h->{$_}} } } keys %$h };
662 my ($ip, $state) = @_;
664 my $s = $ip_seen{$ip};
667 # if $state is stronger or equal to $s, return 1
671 if($state->{$_}{valid}[0] < $s->{$_})
673 # The current state is LESS valid than the previously run one. We NEED to run this.
674 # The saved state can safely become the intersection [citation needed].
677 $s->{$_} = $state->{$_}{valid}[0]
678 if $state->{$_}{valid}[0] < $s->{$_};
683 # if we get here, $state is stronger or equal. No need to try it.
688 # Never seen this IP yet.
689 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
694 my ($ip, $state, $s, $c) = @_;
697 my $return_hack = $c->{isreturn} // 0;
703 unless defined $type;
713 if not exists $state->{$ofs};
714 my $valid = $state->{$ofs}{valid};
717 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
719 print "; Use of uninitialized value $ofs in $func->{debugname} at $ip.$_\n";
723 elsif($valid->[0] < 0)
725 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
727 print "; Use of temporary $ofs across CALL in $func->{debugname} at $ip.$_\n";
734 if(defined $valid->[1])
736 delete $write_places{$valid->[1]}{$valid->[2]};
743 $state->{$ofs}{valid} = [1, $ip, $_]
744 if exists $state->{$ofs};
747 if($type eq 'inglobal' || $type eq 'inglobalfunc')
751 elsif($type eq 'inglobalvec')
757 elsif($type eq 'outglobal')
761 elsif($type eq 'outglobalvec')
770 # builtin calls may clobber stuff
772 my $funcid = $progs->{globals}[$func]{v}{int};
773 my $funcobj = $progs->{functions}[$funcid];
774 if(!$funcobj || $funcobj->{first_statement} >= 0)
779 if($_->{flags} & WATCHME_T)
781 $_->{valid} = [-1, undef, undef];
790 for my $ip(keys %write_places)
792 for my $operand(keys %{$write_places{$ip}})
795 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
798 my %writeplace_seen = ();
799 run_nfa $progs, $ip+1, \%left,
806 my ($ip, $state) = @_;
807 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
811 my ($ip, $state, $s, $c) = @_;
816 unless defined $type;
819 if($type eq 'inglobal' || $type eq 'inglobalfunc')
824 return -1; # exit TOTALLY
827 elsif($type eq 'inglobalvec')
829 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
832 return -1; # exit TOTALLY
835 elsif($type eq 'outglobal')
837 delete $state->{$ofs};
841 elsif($type eq 'outglobalvec')
843 delete $state->{$ofs};
844 delete $state->{$ofs+1};
845 delete $state->{$ofs+2};
855 print "; Value is never used in $func->{debugname} at $ip.$operand\n";
856 ++$warned{$ip}{$operand};
861 disassemble_function($progs, $func, \%warned)
865 use constant DEFAULTGLOBALS => [
899 if($ofs < @{(DEFAULTGLOBALS)})
901 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
903 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
912 print STDERR "Parsing header...\n";
913 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
915 print STDERR "Parsing strings...\n";
916 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
920 my $endpos = index $p{strings}, "\0", $startpos;
921 return substr $p{strings}, $startpos, $endpos - $startpos;
924 print STDERR "Parsing statements...\n";
925 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
927 print STDERR "Parsing globaldefs...\n";
928 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
930 print STDERR "Parsing fielddefs...\n";
931 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
933 print STDERR "Parsing globals...\n";
934 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
936 print STDERR "Parsing functions...\n";
937 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
939 print STDERR "Detecting temps...\n";
940 my %offsets_saved = ();
941 for(@{$p{globaldefs}})
943 my $type = $_->{type};
944 my $name = $p{getstring}->($_->{s_name});
946 unless $type->{save} or $name ne "";
947 for my $i(0..(typesize($_->{type}{type})-1))
949 ++$offsets_saved{$_->{ofs}+$i};
952 my %offsets_initialized = ();
953 for(0..(@{$p{globals}}-1))
955 if($p{globals}[$_]{v}{int})
957 ++$offsets_initialized{$_};
962 for(0..(@{$p{globals}}-1))
965 if $_ < @{(DEFAULTGLOBALS)};
967 if !$offsets_saved{$_} and $offsets_initialized{$_};
969 if !$offsets_saved{$_} and !$offsets_initialized{$_};
971 $p{temps} = \%istemp;
972 $p{consts} = \%isconst;
974 print STDERR "Naming...\n";
978 for(@{$p{globaldefs}})
980 my $s = $p{getstring}->($_->{s_name});
981 $_->{debugname} //= "_$s"
984 for(@{$p{globaldefs}})
986 $globaldefs[$_->{ofs}] //= $_
987 if defined $_->{debugname};
989 for(@{$p{globaldefs}})
991 $globaldefs[$_->{ofs}] //= $_;
993 for(0..(@{$p{globals}}-1))
995 $globaldefs[$_] //= {
1001 my %globaldefs = ();
1004 if(!defined $_->{debugname})
1006 if($istemp{$_->{ofs}})
1008 $_->{debugname} = "temp_$_->{ofs}";
1010 elsif($isconst{$_->{ofs}})
1012 $_->{debugname} = "(" . get_constant(\%p, $p{globals}[$_->{ofs}]{v}) . ")";
1016 $_->{debugname} = "global_$_->{ofs}";
1019 ++$globaldefs{$_->{debugname}};
1024 if $globaldefs{$_->{debugname}} <= 1;
1025 print "Not unique: $_->{debugname} at $_->{ofs}\n";
1026 $_->{debugname} .= "\@$_->{ofs}";
1028 $p{globaldef_byoffset} = sub
1032 if($ofs >= 0 && $ofs < @{(DEFAULTGLOBALS)})
1034 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
1036 my $def = $globaldefs[$ofs];
1042 for(@{$p{functions}})
1044 my $file = $p{getstring}->($_->{s_file});
1045 my $name = $p{getstring}->($_->{s_name});
1046 $name = "$file:$name"
1048 $_->{debugname} = $name;
1049 $functions{$_->{first_statement}} = $_;
1051 $p{function_byoffset} = sub
1054 return $functions{$ofs};
1057 # what do we want to do?
1058 my $checkfunc = \&find_uninitialized_locals;
1059 #my $checkfunc = \&disassemble_function;
1060 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1062 $checkfunc->(\%p, $_);
1066 open my $fh, '<', $ARGV[0];