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)))
274 if($c->{isconditional})
278 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
286 $nfa->($ip+1, $copy_handler->($state));
287 $ip += $s->{$c->{isjump}};
292 $ip += $s->{$c->{isjump}};
304 $nfa->($ip, $copy_handler->($state));
309 my ($progs, $g) = @_;
314 elsif($g->{int} > 0 && $g->{int} < 16777216)
316 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
318 return str($progs->{getstring}->($g->{int}));
322 return $g->{int} . "i";
331 use constant PRE_MARK_STATEMENT => "";
332 use constant POST_MARK_STATEMENT => "";
333 use constant PRE_MARK_OPERAND => "*** ";
334 use constant POST_MARK_OPERAND => " ***";
336 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
337 use constant OPERAND_FORMAT => "%s";
338 use constant OPERAND_SEPARATOR => ", ";
339 use constant INSTRUCTION_SEPARATOR => "\n";
344 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
348 sub disassemble_function($$;$)
350 my ($progs, $func, $highlight) = @_;
352 print "$func->{debugname}:\n";
354 my $initializer = sub
357 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
362 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
363 printf OPERAND_FORMAT, "$func->{parm_start}";
364 print INSTRUCTION_SEPARATOR;
366 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
367 printf OPERAND_FORMAT, "$func->{locals}";
368 print INSTRUCTION_SEPARATOR;
370 my %override_locals = ();
371 my $p = $func->{parm_start};
372 for(0..($func->{numparms}-1))
374 $override_locals{$p} //= "argv_$_";
375 for my $comp(0..($func->{parm_size}[$_]-1))
377 $override_locals{$p} //= "argv_$_\[$comp]";
380 printf INSTRUCTION_FORMAT, '', '', '.ARG';
381 printf OPERAND_FORMAT, "argv_$_";
382 print OPERAND_SEPARATOR;
383 printf OPERAND_FORMAT, $func->{parm_size}[$_];
384 print INSTRUCTION_SEPARATOR;
386 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
389 if exists $override_locals{$_};
390 $override_locals{$_} = "local_$_";
392 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
393 printf OPERAND_FORMAT, "local_$_";
395 print INSTRUCTION_SEPARATOR;
402 return $override_locals{$ofs}
403 if exists $override_locals{$ofs};
404 my $def = $progs->{globaldef_byoffset}->($ofs);
405 return $def->{debugname};
410 my ($ip, $type, $operand) = @_;
411 if($type eq 'inglobal')
413 my $name = $getname->($operand);
414 printf OPERAND_FORMAT, "$name";
416 elsif($type eq 'outglobal')
418 my $name = $getname->($operand);
419 printf OPERAND_FORMAT, "&$name";
421 elsif($type eq 'inglobalvec')
423 my $name = $getname->($operand);
424 printf OPERAND_FORMAT, "$name\[\]";
426 elsif($type eq 'outglobalvec')
428 my $name = $getname->($operand);
429 printf OPERAND_FORMAT, "&$name\[\]";
431 elsif($type eq 'inglobalfunc')
433 my $name = $getname->($operand);
434 printf OPERAND_FORMAT, "$name()";
436 elsif($type eq 'ipoffset')
438 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
442 die "unknown type: $type";
448 run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
451 my ($ip, $state, $s, $c) = @_;
454 if(my $j = $c->{isjump})
456 my $t = $ip + $s->{$j};
457 $come_from{$t}{$ip} = $c->{isconditional};
464 for my $ip(sort { $a <=> $b } keys %statements)
466 if($ip == $func->{first_statement})
468 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
469 print INSTRUCTION_SEPARATOR;
471 if(defined $ipprev && $ip != $ipprev + 1)
473 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
474 printf OPERAND_FORMAT, $ip - $ipprev - 1;
475 print INSTRUCTION_SEPARATOR;
477 if(my $cf = $come_from{$ip})
479 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
481 for(sort { $a <=> $b } keys %$cf)
483 print OPERAND_SEPARATOR
485 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
487 print INSTRUCTION_SEPARATOR;
490 my $op = $progs->{statements}[$ip]{op};
491 my $ipt = $progs->{statements}[$ip];
492 my $opprop = checkop $op;
494 print PRE_MARK_STATEMENT
495 if $highlight and $highlight->{$ip};
497 my $showip = $opprop->{isjump};
498 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? "<!>" : "", $op;
504 if not defined $opprop->{$o};
505 print OPERAND_SEPARATOR
507 print PRE_MARK_OPERAND
508 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
509 $operand->($ip, $opprop->{$o}, $ipt->{$o});
510 print POST_MARK_OPERAND
511 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
514 print POST_MARK_STATEMENT
515 if $highlight and $highlight->{$ip};
517 print INSTRUCTION_SEPARATOR;
521 sub find_uninitialized_locals($$)
523 my ($progs, $func) = @_;
527 if $func->{first_statement} < 0; # builtin
529 print STDERR "Checking $func->{debugname}...\n";
531 my $p = $func->{parm_start};
532 for(0..($func->{numparms}-1))
534 $p += $func->{parm_size}[$_];
537 use constant WATCHME_R => 1;
538 use constant WATCHME_W => 2;
539 use constant WATCHME_X => 4;
540 use constant WATCHME_T => 8;
541 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
543 for(keys %{$progs->{temps}})
545 $watchme{$_} = WATCHME_T | WATCHME_X
546 if not exists $watchme{$_};
549 my %write_places = ();
550 run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
553 my ($ip, $state, $s, $c) = @_;
558 unless defined $type;
561 if($type eq 'inglobal' || $type eq 'inglobalfunc')
563 $watchme{$ofs} |= WATCHME_R;
565 elsif($type eq 'inglobalvec')
567 $watchme{$ofs} |= WATCHME_R;
568 $watchme{$ofs+1} |= WATCHME_R;
569 $watchme{$ofs+2} |= WATCHME_R;
571 elsif($type eq 'outglobal')
573 $watchme{$ofs} |= WATCHME_W;
574 $write_places{$ip}{$_} = [$ofs]
575 if $watchme{$ofs} & WATCHME_X;
577 elsif($type eq 'outglobalvec')
579 $watchme{$ofs} |= WATCHME_W;
580 $watchme{$ofs+1} |= WATCHME_W;
581 $watchme{$ofs+2} |= WATCHME_W;
582 my @l = grep { $watchme{$_} & WATCHME_X } $ofs .. ($ofs+2);
583 $write_places{$ip}{$_} = \@l
594 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
598 if not keys %watchme;
603 flags => $watchme{$_},
604 valid => [0, undef, undef]
608 # mark parameters as initialized
609 for($func->{parm_start} .. ($p-1))
611 $watchme{$_}{valid} = [1, undef, undef]
612 if defined $watchme{$_};
614 # an initial run of STORE instruction is for receiving extra parameters
615 # (beyond 8). Only possible if the function is declared as having 8 params.
616 # Extra parameters behave otherwise like temps, but are initialized at
618 for($func->{first_statement} .. (@{$progs->{statements}}-1))
620 my $s = $progs->{statements}[$_];
621 if($s->{op} eq 'STORE_V')
623 $watchme{$s->{a}}{valid} = [1, undef, undef]
624 if defined $watchme{$s->{a}};
625 $watchme{$s->{a}+1}{valid} = [1, undef, undef]
626 if defined $watchme{$s->{a}+1};
627 $watchme{$s->{a}+2}{valid} = [1, undef, undef]
628 if defined $watchme{$s->{a}+2};
630 elsif($s->{op} =~ /^STORE_/)
632 $watchme{$s->{a}}{valid} = [1, undef, undef]
633 if defined $watchme{$s->{a}};
643 run_nfa $progs, $func->{first_statement}, \%watchme,
646 return { map { $_ => { %{$h->{$_}} } } keys %$h };
649 my ($ip, $state) = @_;
651 my $s = $ip_seen{$ip};
654 # if $state is stronger or equal to $s, return 1
658 if($state->{$_}{valid}[0] < $s->{$_})
660 # The current state is LESS valid than the previously run one. We NEED to run this.
661 # The saved state can safely become the intersection [citation needed].
664 $s->{$_} = $state->{$_}{valid}[0]
665 if $state->{$_}{valid}[0] < $s->{$_};
670 # if we get here, $state is stronger or equal. No need to try it.
675 # Never seen this IP yet.
676 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
681 my ($ip, $state, $s, $c) = @_;
684 my $return_hack = $c->{isreturn} // 0;
690 unless defined $type;
700 if not exists $state->{$ofs};
701 my $valid = $state->{$ofs}{valid};
704 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
706 print "; Use of uninitialized value $ofs in $func->{debugname} at $ip.$_\n";
710 elsif($valid->[0] < 0)
712 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
714 print "; Use of temporary $ofs across CALL in $func->{debugname} at $ip.$_\n";
721 if(defined $valid->[1])
723 delete $write_places{$valid->[1]}{$valid->[2]};
730 $state->{$ofs}{valid} = [1, $ip, $_]
731 if exists $state->{$ofs};
734 if($type eq 'inglobal' || $type eq 'inglobalfunc')
738 elsif($type eq 'inglobalvec')
744 elsif($type eq 'outglobal')
748 elsif($type eq 'outglobalvec')
757 # builtin calls may clobber stuff
759 my $funcid = $progs->{globals}[$func]{v}{int};
760 my $funcobj = $progs->{functions}[$funcid];
761 if(!$funcobj || $funcobj->{first_statement} >= 0)
766 if($_->{flags} & WATCHME_T)
768 $_->{valid} = [-1, undef, undef];
774 my $def = $progs->{globaldef_byoffset}->($func);
776 if $def->{debugname} eq 'error';
783 for my $ip(keys %write_places)
785 for my $operand(keys %{$write_places{$ip}})
788 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
791 my %writeplace_seen = ();
792 run_nfa $progs, $ip+1, \%left,
799 my ($ip, $state) = @_;
800 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
804 my ($ip, $state, $s, $c) = @_;
809 unless defined $type;
812 if($type eq 'inglobal' || $type eq 'inglobalfunc')
817 return -1; # exit TOTALLY
820 elsif($type eq 'inglobalvec')
822 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
825 return -1; # exit TOTALLY
828 elsif($type eq 'outglobal')
830 delete $state->{$ofs};
834 elsif($type eq 'outglobalvec')
836 delete $state->{$ofs};
837 delete $state->{$ofs+1};
838 delete $state->{$ofs+2};
848 print "; Value is never used in $func->{debugname} at $ip.$operand\n";
849 ++$warned{$ip}{$operand};
854 disassemble_function($progs, $func, \%warned)
858 use constant DEFAULTGLOBALS => [
892 if($ofs < @{(DEFAULTGLOBALS)})
894 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
896 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
905 print STDERR "Parsing header...\n";
906 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
908 print STDERR "Parsing strings...\n";
909 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
913 my $endpos = index $p{strings}, "\0", $startpos;
914 return substr $p{strings}, $startpos, $endpos - $startpos;
917 print STDERR "Parsing statements...\n";
918 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
920 print STDERR "Parsing globaldefs...\n";
921 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
923 print STDERR "Parsing fielddefs...\n";
924 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
926 print STDERR "Parsing globals...\n";
927 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
929 print STDERR "Parsing functions...\n";
930 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
932 print STDERR "Detecting temps...\n";
933 my %offsets_saved = ();
934 for(@{$p{globaldefs}})
936 my $type = $_->{type};
937 my $name = $p{getstring}->($_->{s_name});
939 unless $type->{save} or $name ne "";
940 for my $i(0..(typesize($_->{type}{type})-1))
942 ++$offsets_saved{$_->{ofs}+$i};
945 my %offsets_initialized = ();
946 for(0..(@{$p{globals}}-1))
948 if($p{globals}[$_]{v}{int})
950 ++$offsets_initialized{$_};
955 for(0..(@{$p{globals}}-1))
958 if $_ < @{(DEFAULTGLOBALS)};
960 if !$offsets_saved{$_} and $offsets_initialized{$_};
962 if !$offsets_saved{$_} and !$offsets_initialized{$_};
964 $p{temps} = \%istemp;
965 $p{consts} = \%isconst;
967 print STDERR "Naming...\n";
971 for(@{$p{globaldefs}})
973 my $s = $p{getstring}->($_->{s_name});
974 $_->{debugname} //= "_$s"
977 for(@{$p{globaldefs}})
979 $globaldefs[$_->{ofs}] //= $_
980 if defined $_->{debugname};
982 for(@{$p{globaldefs}})
984 $globaldefs[$_->{ofs}] //= $_;
986 for(0..(@{$p{globals}}-1))
988 $globaldefs[$_] //= {
997 if(!defined $_->{debugname})
999 if($istemp{$_->{ofs}})
1001 $_->{debugname} = "temp_$_->{ofs}";
1003 elsif($isconst{$_->{ofs}})
1005 $_->{debugname} = "(" . get_constant(\%p, $p{globals}[$_->{ofs}]{v}) . ")";
1009 $_->{debugname} = "global_$_->{ofs}";
1012 ++$globaldefs{$_->{debugname}};
1017 if $globaldefs{$_->{debugname}} <= 1;
1018 print "Not unique: $_->{debugname} at $_->{ofs}\n";
1019 $_->{debugname} .= "\@$_->{ofs}";
1021 $p{globaldef_byoffset} = sub
1025 if($ofs >= 0 && $ofs < @{(DEFAULTGLOBALS)})
1027 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
1029 my $def = $globaldefs[$ofs];
1035 for(@{$p{functions}})
1037 my $file = $p{getstring}->($_->{s_file});
1038 my $name = $p{getstring}->($_->{s_name});
1039 $name = "$file:$name"
1041 $_->{debugname} = $name;
1042 $functions{$_->{first_statement}} = $_;
1044 $p{function_byoffset} = sub
1047 return $functions{$ofs};
1050 # what do we want to do?
1051 my $checkfunc = \&find_uninitialized_locals;
1052 #my $checkfunc = \&disassemble_function;
1053 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1055 $checkfunc->(\%p, $_);
1059 open my $fh, '<', $ARGV[0];