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';
286 if($c->{isconditional})
290 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
298 $nfa->($ip+1, $copy_handler->($state));
299 $ip += $s->{$c->{isjump}};
304 $ip += $s->{$c->{isjump}};
316 $nfa->($ip, $copy_handler->($state));
321 my ($progs, $g) = @_;
326 elsif($g->{int} > 0 && $g->{int} < 16777216)
328 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
330 return str($progs->{getstring}->($g->{int}));
334 return $g->{int} . "i";
343 use constant PRE_MARK_STATEMENT => "";
344 use constant POST_MARK_STATEMENT => "";
345 use constant PRE_MARK_OPERAND => "*** ";
346 use constant POST_MARK_OPERAND => " ***";
348 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
349 use constant OPERAND_FORMAT => "%s";
350 use constant OPERAND_SEPARATOR => ", ";
351 use constant INSTRUCTION_SEPARATOR => "\n";
356 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
360 sub disassemble_function($$;$)
362 my ($progs, $func, $highlight) = @_;
364 print "$func->{debugname}:\n";
366 my $initializer = sub
369 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
374 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
375 printf OPERAND_FORMAT, "$func->{parm_start}";
376 print INSTRUCTION_SEPARATOR;
378 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
379 printf OPERAND_FORMAT, "$func->{locals}";
380 print INSTRUCTION_SEPARATOR;
382 my %override_locals = ();
383 my $p = $func->{parm_start};
384 for(0..($func->{numparms}-1))
386 $override_locals{$p} //= "argv_$_";
387 for my $comp(0..($func->{parm_size}[$_]-1))
389 $override_locals{$p} //= "argv_$_\[$comp]";
392 printf INSTRUCTION_FORMAT, '', '', '.ARG';
393 printf OPERAND_FORMAT, "argv_$_";
394 print OPERAND_SEPARATOR;
395 printf OPERAND_FORMAT, $func->{parm_size}[$_];
396 print INSTRUCTION_SEPARATOR;
398 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
401 if exists $override_locals{$_};
402 $override_locals{$_} = "local_$_";
404 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
405 printf OPERAND_FORMAT, "local_$_";
407 print INSTRUCTION_SEPARATOR;
414 return $override_locals{$ofs}
415 if exists $override_locals{$ofs};
416 my $def = $progs->{globaldef_byoffset}->($ofs);
417 return $def->{debugname};
422 my ($ip, $type, $operand) = @_;
423 if($type eq 'inglobal')
425 my $name = $getname->($operand);
426 printf OPERAND_FORMAT, "$name";
428 elsif($type eq 'outglobal')
430 my $name = $getname->($operand);
431 printf OPERAND_FORMAT, "&$name";
433 elsif($type eq 'inglobalvec')
435 my $name = $getname->($operand);
436 printf OPERAND_FORMAT, "$name\[\]";
438 elsif($type eq 'outglobalvec')
440 my $name = $getname->($operand);
441 printf OPERAND_FORMAT, "&$name\[\]";
443 elsif($type eq 'inglobalfunc')
445 my $name = $getname->($operand);
446 printf OPERAND_FORMAT, "$name()";
448 elsif($type eq 'ipoffset')
450 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
454 die "unknown type: $type";
460 run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
463 my ($ip, $state, $s, $c) = @_;
466 if(my $j = $c->{isjump})
468 my $t = $ip + $s->{$j};
469 $come_from{$t}{$ip} = $c->{isconditional};
476 for my $ip(sort { $a <=> $b } keys %statements)
478 if($ip == $func->{first_statement})
480 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
481 print INSTRUCTION_SEPARATOR;
483 if(defined $ipprev && $ip != $ipprev + 1)
485 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
486 printf OPERAND_FORMAT, $ip - $ipprev - 1;
487 print INSTRUCTION_SEPARATOR;
489 if(my $cf = $come_from{$ip})
491 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
493 for(sort { $a <=> $b } keys %$cf)
495 print OPERAND_SEPARATOR
497 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
499 print INSTRUCTION_SEPARATOR;
502 my $op = $progs->{statements}[$ip]{op};
503 my $ipt = $progs->{statements}[$ip];
504 my $opprop = checkop $op;
506 print PRE_MARK_STATEMENT
507 if $highlight and $highlight->{$ip};
509 my $showip = $opprop->{isjump};
510 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? "<!>" : "", $op;
516 if not defined $opprop->{$o};
517 print OPERAND_SEPARATOR
519 print PRE_MARK_OPERAND
520 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
521 $operand->($ip, $opprop->{$o}, $ipt->{$o});
522 print POST_MARK_OPERAND
523 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
526 print POST_MARK_STATEMENT
527 if $highlight and $highlight->{$ip};
529 print INSTRUCTION_SEPARATOR;
533 sub find_uninitialized_locals($$)
535 my ($progs, $func) = @_;
539 if $func->{first_statement} < 0; # builtin
541 print STDERR "Checking $func->{debugname}...\n";
543 my $p = $func->{parm_start};
544 for(0..($func->{numparms}-1))
546 $p += $func->{parm_size}[$_];
549 use constant WATCHME_R => 1;
550 use constant WATCHME_W => 2;
551 use constant WATCHME_X => 4;
552 use constant WATCHME_T => 8;
553 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
555 for(keys %{$progs->{temps}})
557 $watchme{$_} = WATCHME_T | WATCHME_X
558 if not exists $watchme{$_};
561 my %write_places = ();
562 run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
565 my ($ip, $state, $s, $c) = @_;
570 unless defined $type;
573 if($type eq 'inglobal' || $type eq 'inglobalfunc')
575 $watchme{$ofs} |= WATCHME_R;
577 elsif($type eq 'inglobalvec')
579 $watchme{$ofs} |= WATCHME_R;
580 $watchme{$ofs+1} |= WATCHME_R;
581 $watchme{$ofs+2} |= WATCHME_R;
583 elsif($type eq 'outglobal')
585 $watchme{$ofs} |= WATCHME_W;
586 $write_places{$ip}{$_} = [$ofs]
587 if $watchme{$ofs} & WATCHME_X;
589 elsif($type eq 'outglobalvec')
591 $watchme{$ofs} |= WATCHME_W;
592 $watchme{$ofs+1} |= WATCHME_W;
593 $watchme{$ofs+2} |= WATCHME_W;
594 my @l = grep { $watchme{$_} & WATCHME_X } $ofs .. ($ofs+2);
595 $write_places{$ip}{$_} = \@l
606 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
610 if not keys %watchme;
615 flags => $watchme{$_},
616 valid => [0, undef, undef]
620 # mark parameters as initialized
621 for($func->{parm_start} .. ($p-1))
623 $watchme{$_}{valid} = [1, undef, undef]
624 if defined $watchme{$_};
626 # an initial run of STORE instruction is for receiving extra parameters
627 # (beyond 8). Only possible if the function is declared as having 8 params.
628 # Extra parameters behave otherwise like temps, but are initialized at
630 for($func->{first_statement} .. (@{$progs->{statements}}-1))
632 my $s = $progs->{statements}[$_];
633 if($s->{op} eq 'STORE_V')
635 $watchme{$s->{a}}{valid} = [1, undef, undef]
636 if defined $watchme{$s->{a}};
637 $watchme{$s->{a}+1}{valid} = [1, undef, undef]
638 if defined $watchme{$s->{a}+1};
639 $watchme{$s->{a}+2}{valid} = [1, undef, undef]
640 if defined $watchme{$s->{a}+2};
642 elsif($s->{op} =~ /^STORE_/)
644 $watchme{$s->{a}}{valid} = [1, undef, undef]
645 if defined $watchme{$s->{a}};
655 run_nfa $progs, $func->{first_statement}, \%watchme,
658 return { map { $_ => { %{$h->{$_}} } } keys %$h };
661 my ($ip, $state) = @_;
663 my $s = $ip_seen{$ip};
666 # if $state is stronger or equal to $s, return 1
670 if($state->{$_}{valid}[0] < $s->{$_})
672 # The current state is LESS valid than the previously run one. We NEED to run this.
673 # The saved state can safely become the intersection [citation needed].
676 $s->{$_} = $state->{$_}{valid}[0]
677 if $state->{$_}{valid}[0] < $s->{$_};
682 # if we get here, $state is stronger or equal. No need to try it.
687 # Never seen this IP yet.
688 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
693 my ($ip, $state, $s, $c) = @_;
696 my $return_hack = $c->{isreturn} // 0;
702 unless defined $type;
712 if not exists $state->{$ofs};
713 my $valid = $state->{$ofs}{valid};
716 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
718 print "; Use of uninitialized value $ofs in $func->{debugname} at $ip.$_\n";
722 elsif($valid->[0] < 0)
724 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
726 print "; Use of temporary $ofs across CALL in $func->{debugname} at $ip.$_\n";
733 if(defined $valid->[1])
735 delete $write_places{$valid->[1]}{$valid->[2]};
742 $state->{$ofs}{valid} = [1, $ip, $_]
743 if exists $state->{$ofs};
746 if($type eq 'inglobal' || $type eq 'inglobalfunc')
750 elsif($type eq 'inglobalvec')
756 elsif($type eq 'outglobal')
760 elsif($type eq 'outglobalvec')
769 # builtin calls may clobber stuff
771 my $funcid = $progs->{globals}[$func]{v}{int};
772 my $funcobj = $progs->{functions}[$funcid];
773 if(!$funcobj || $funcobj->{first_statement} >= 0)
778 if($_->{flags} & WATCHME_T)
780 $_->{valid} = [-1, undef, undef];
789 for my $ip(keys %write_places)
791 for my $operand(keys %{$write_places{$ip}})
794 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
797 my %writeplace_seen = ();
798 run_nfa $progs, $ip+1, \%left,
805 my ($ip, $state) = @_;
806 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
810 my ($ip, $state, $s, $c) = @_;
815 unless defined $type;
818 if($type eq 'inglobal' || $type eq 'inglobalfunc')
823 return -1; # exit TOTALLY
826 elsif($type eq 'inglobalvec')
828 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
831 return -1; # exit TOTALLY
834 elsif($type eq 'outglobal')
836 delete $state->{$ofs};
840 elsif($type eq 'outglobalvec')
842 delete $state->{$ofs};
843 delete $state->{$ofs+1};
844 delete $state->{$ofs+2};
854 print "; Value is never used in $func->{debugname} at $ip.$operand\n";
855 ++$warned{$ip}{$operand};
860 disassemble_function($progs, $func, \%warned)
864 use constant DEFAULTGLOBALS => [
898 if($ofs < @{(DEFAULTGLOBALS)})
900 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
902 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
911 print STDERR "Parsing header...\n";
912 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
914 print STDERR "Parsing strings...\n";
915 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
919 my $endpos = index $p{strings}, "\0", $startpos;
920 return substr $p{strings}, $startpos, $endpos - $startpos;
923 print STDERR "Parsing statements...\n";
924 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
926 print STDERR "Parsing globaldefs...\n";
927 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
929 print STDERR "Parsing fielddefs...\n";
930 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
932 print STDERR "Parsing globals...\n";
933 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
935 print STDERR "Parsing functions...\n";
936 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
938 print STDERR "Detecting temps...\n";
939 my %offsets_saved = ();
940 for(@{$p{globaldefs}})
942 my $type = $_->{type};
943 my $name = $p{getstring}->($_->{s_name});
945 unless $type->{save} or $name ne "";
946 for my $i(0..(typesize($_->{type}{type})-1))
948 ++$offsets_saved{$_->{ofs}+$i};
951 my %offsets_initialized = ();
952 for(0..(@{$p{globals}}-1))
954 if($p{globals}[$_]{v}{int})
956 ++$offsets_initialized{$_};
961 for(0..(@{$p{globals}}-1))
964 if $_ < @{(DEFAULTGLOBALS)};
966 if !$offsets_saved{$_} and $offsets_initialized{$_};
968 if !$offsets_saved{$_} and !$offsets_initialized{$_};
970 $p{temps} = \%istemp;
971 $p{consts} = \%isconst;
973 print STDERR "Naming...\n";
977 for(@{$p{globaldefs}})
979 my $s = $p{getstring}->($_->{s_name});
980 $_->{debugname} //= "_$s"
983 for(@{$p{globaldefs}})
985 $globaldefs[$_->{ofs}] //= $_
986 if defined $_->{debugname};
988 for(@{$p{globaldefs}})
990 $globaldefs[$_->{ofs}] //= $_;
992 for(0..(@{$p{globals}}-1))
994 $globaldefs[$_] //= {
1000 my %globaldefs = ();
1003 if(!defined $_->{debugname})
1005 if($istemp{$_->{ofs}})
1007 $_->{debugname} = "temp_$_->{ofs}";
1009 elsif($isconst{$_->{ofs}})
1011 $_->{debugname} = "(" . get_constant(\%p, $p{globals}[$_->{ofs}]{v}) . ")";
1015 $_->{debugname} = "global_$_->{ofs}";
1018 ++$globaldefs{$_->{debugname}};
1023 if $globaldefs{$_->{debugname}} <= 1;
1024 print "Not unique: $_->{debugname} at $_->{ofs}\n";
1025 $_->{debugname} .= "\@$_->{ofs}";
1027 $p{globaldef_byoffset} = sub
1031 if($ofs >= 0 && $ofs < @{(DEFAULTGLOBALS)})
1033 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
1035 my $def = $globaldefs[$ofs];
1041 for(@{$p{functions}})
1043 my $file = $p{getstring}->($_->{s_file});
1044 my $name = $p{getstring}->($_->{s_name});
1045 $name = "$file:$name"
1047 $_->{debugname} = $name;
1048 $functions{$_->{first_statement}} = $_;
1050 $p{function_byoffset} = sub
1053 return $functions{$ofs};
1056 # what do we want to do?
1057 my $checkfunc = \&find_uninitialized_locals;
1058 #my $checkfunc = \&disassemble_function;
1059 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1061 $checkfunc->(\%p, $_);
1065 open my $fh, '<', $ARGV[0];