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 die "Invalid opcode: $_[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) = @_;
256 if $state_checker->($ip, $state);
258 my $s = $statements->[$ip];
259 my $c = checkop $s->{op};
261 if($instruction_handler->($ip, $state, $s, $c))
273 if($c->{isconditional})
277 $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state));
282 $nfa->($ip+1, $copy_handler->($state));
283 $ip += $s->{$c->{isjump}};
288 $ip += $s->{$c->{isjump}};
298 $nfa->($ip, $copy_handler->($state));
303 my ($progs, $g) = @_;
308 elsif($g->{int} > 0 && $g->{int} < 16777216)
310 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
312 return str($progs->{getstring}->($g->{int}));
316 return $g->{int} . "i";
325 use constant PRE_MARK_STATEMENT => "";
326 use constant POST_MARK_STATEMENT => "";
327 use constant PRE_MARK_OPERAND => "*** ";
328 use constant POST_MARK_OPERAND => " ***";
330 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
331 use constant OPERAND_FORMAT => "%s";
332 use constant OPERAND_SEPARATOR => ", ";
333 use constant INSTRUCTION_SEPARATOR => "\n";
338 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
342 sub disassemble_function($$;$)
344 my ($progs, $func, $highlight) = @_;
346 print "$func->{debugname}:\n";
348 my $initializer = sub
351 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
356 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
357 printf OPERAND_FORMAT, "$func->{parm_start}";
358 print INSTRUCTION_SEPARATOR;
360 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
361 printf OPERAND_FORMAT, "$func->{locals}";
362 print INSTRUCTION_SEPARATOR;
364 my %override_locals = ();
365 my $p = $func->{parm_start};
366 for(0..($func->{numparms}-1))
368 $override_locals{$p} //= "argv_$_";
369 for my $comp(0..($func->{parm_size}[$_]-1))
371 $override_locals{$p} //= "argv_$_\[$comp]";
374 printf INSTRUCTION_FORMAT, '', '', '.ARG';
375 printf OPERAND_FORMAT, "argv_$_";
376 print OPERAND_SEPARATOR;
377 printf OPERAND_FORMAT, $func->{parm_size}[$_];
378 print INSTRUCTION_SEPARATOR;
380 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
383 if exists $override_locals{$_};
384 $override_locals{$_} = "local_$_";
386 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
387 printf OPERAND_FORMAT, "local_$_";
389 print INSTRUCTION_SEPARATOR;
396 return $override_locals{$ofs}
397 if exists $override_locals{$ofs};
398 my $def = $progs->{globaldef_byoffset}->($ofs);
399 return $def->{debugname};
404 my ($ip, $type, $operand) = @_;
405 if($type eq 'inglobal')
407 my $name = $getname->($operand);
408 printf OPERAND_FORMAT, "$name";
410 elsif($type eq 'outglobal')
412 my $name = $getname->($operand);
413 printf OPERAND_FORMAT, "&$name";
415 elsif($type eq 'inglobalvec')
417 my $name = $getname->($operand);
418 printf OPERAND_FORMAT, "$name\[\]";
420 elsif($type eq 'outglobalvec')
422 my $name = $getname->($operand);
423 printf OPERAND_FORMAT, "&$name\[\]";
425 elsif($type eq 'inglobalfunc')
427 my $name = $getname->($operand);
428 printf OPERAND_FORMAT, "$name()";
430 elsif($type eq 'ipoffset')
432 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
436 die "unknown type: $type";
442 run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
445 my ($ip, $state, $s, $c) = @_;
448 if(my $j = $c->{isjump})
450 my $t = $ip + $s->{$j};
451 $come_from{$t}{$ip} = $c->{isconditional};
458 for my $ip(sort { $a <=> $b } keys %statements)
460 if($ip == $func->{first_statement})
462 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
463 print INSTRUCTION_SEPARATOR;
465 if(defined $ipprev && $ip != $ipprev + 1)
467 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
468 printf OPERAND_FORMAT, $ip - $ipprev - 1;
469 print INSTRUCTION_SEPARATOR;
471 if(my $cf = $come_from{$ip})
473 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
475 for(sort { $a <=> $b } keys %$cf)
477 print OPERAND_SEPARATOR
479 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
481 print INSTRUCTION_SEPARATOR;
484 my $op = $progs->{statements}[$ip]{op};
485 my $ipt = $progs->{statements}[$ip];
486 my $opprop = checkop $op;
488 print PRE_MARK_STATEMENT
489 if $highlight and $highlight->{$ip};
491 my $showip = $opprop->{isjump};
492 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? "<!>" : "", $op;
498 if not defined $opprop->{$o};
499 print OPERAND_SEPARATOR
501 print PRE_MARK_OPERAND
502 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
503 $operand->($ip, $opprop->{$o}, $ipt->{$o});
504 print POST_MARK_OPERAND
505 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
508 print POST_MARK_STATEMENT
509 if $highlight and $highlight->{$ip};
511 print INSTRUCTION_SEPARATOR;
515 sub find_uninitialized_locals($$)
517 my ($progs, $func) = @_;
520 # 21:04:25 divVerent | just wondering how I can best detect "temp value is never used"
521 # 21:04:33 divVerent | I know which vars are temps already
522 # 21:04:59 divVerent | basically, looks like for each write, I will not just have to track that the new value is valid
523 # 21:05:01 divVerent | but also its source
524 # 21:05:12 divVerent | on each read, I'll remember that this source statement's value has been used
525 # 21:05:21 divVerent | and will compare the list of sources in a step after "execution"
526 # 21:05:27 divVerent | to the list of total write statements to the temp
529 if $func->{first_statement} < 0; # builtin
531 print STDERR "Checking $func->{debugname}...\n";
533 my $p = $func->{parm_start};
534 for(0..($func->{numparms}-1))
536 $p += $func->{parm_size}[$_];
539 use constant WATCHME_R => 1;
540 use constant WATCHME_W => 2;
541 use constant WATCHME_X => 4;
542 use constant WATCHME_T => 8;
543 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
545 for(keys %{$progs->{temps}})
547 $watchme{$_} = WATCHME_T | WATCHME_X
548 if not exists $watchme{$_};
551 my %write_places = ();
552 run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
555 my ($ip, $state, $s, $c) = @_;
560 unless defined $type;
563 if($type eq 'inglobal' || $type eq 'inglobalfunc')
565 $watchme{$ofs} |= WATCHME_R;
567 elsif($type eq 'inglobalvec')
569 $watchme{$ofs} |= WATCHME_R;
570 $watchme{$ofs+1} |= WATCHME_R;
571 $watchme{$ofs+2} |= WATCHME_R;
573 elsif($type eq 'outglobal')
575 $watchme{$ofs} |= WATCHME_W;
576 $write_places{$ip}{$_} = $_
577 if $watchme{$ofs} & WATCHME_X;
579 elsif($type eq 'outglobalvec')
581 $watchme{$ofs} |= WATCHME_W;
582 $watchme{$ofs+1} |= WATCHME_W;
583 $watchme{$ofs+2} |= WATCHME_W;
584 $write_places{$ip}{$_} = 1
585 if ($watchme{$ofs} | $watchme{$ofs+1} | $watchme{$ofs+2}) & WATCHME_X;
595 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
599 if not keys %watchme;
604 flags => $watchme{$_},
605 valid => [0, undef, undef]
609 # mark parameters as initialized
610 for($func->{parm_start} .. ($p-1))
612 $watchme{$_}{valid} = [1, undef, undef]
613 if defined $watchme{$_};
615 # an initial run of STORE instruction is for receiving extra parameters
616 # (beyond 8). Only possible if the function is declared as having 8 params.
617 # Extra parameters behave otherwise like temps, but are initialized at
619 for($func->{first_statement} .. (@{$progs->{statements}}-1))
621 my $s = $progs->{statements}[$_];
622 if($s->{op} eq 'STORE_V')
624 $watchme{$s->{a}}{valid} = [1, undef, undef]
625 if defined $watchme{$s->{a}};
626 $watchme{$s->{a}+1}{valid} = [1, undef, undef]
627 if defined $watchme{$s->{a}+1};
628 $watchme{$s->{a}+2}{valid} = [1, undef, undef]
629 if defined $watchme{$s->{a}+2};
631 elsif($s->{op} =~ /^STORE_/)
633 $watchme{$s->{a}}{valid} = [1, undef, undef]
634 if defined $watchme{$s->{a}};
644 run_nfa $progs, $func->{first_statement}, \%watchme,
647 return { map { $_ => { %{$h->{$_}} } } keys %$h };
650 my ($ip, $state) = @_;
652 my $s = $ip_seen{$ip};
655 # if $state is stronger or equal to $s, return 1
657 # FIXME this is wrong now
658 # when merging states, we also must somehow merge initialization sources
659 # to become the union, EVEN for already analyzes future instructions!
660 # maybe can do this by abusing references
661 # and thereby adjusting the value after the fact
665 if($state->{$_}{valid}[0] < $s->{$_}[0])
667 # The current state is LESS valid than the previously run one. We NEED to run this.
668 # The saved state can safely become the intersection [citation needed].
671 $s->{$_} = $state->{$_}{valid}
672 if $state->{$_}{valid}[0] < $s->{$_}[0];
677 # if we get here, $state is stronger or equal. No need to try it.
682 # Never seen this IP yet.
683 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}); } keys %$state };
688 my ($ip, $state, $s, $c) = @_;
691 my $return_hack = $c->{isreturn} // 0;
697 unless defined $type;
707 if not exists $state->{$ofs};
708 my $valid = $state->{$ofs}{valid};
711 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
713 print "; Use of uninitialized value $ofs in $func->{debugname} at $ip.$_\n";
717 elsif($valid->[0] < 0)
719 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
721 print "; Use of temporary $ofs across CALL in $func->{debugname} at $ip.$_\n";
728 if(defined $valid->[1])
730 delete $write_places{$valid->[1]}{$valid->[2]};
737 $state->{$ofs}{valid} = [1, $ip, $_]
738 if exists $state->{$ofs};
741 if($type eq 'inglobal' || $type eq 'inglobalfunc')
745 elsif($type eq 'inglobalvec')
751 elsif($type eq 'outglobal')
755 elsif($type eq 'outglobalvec')
764 # builtin calls may clobber stuff
766 my $funcid = $progs->{globals}[$func]{v}{int};
767 my $funcobj = $progs->{functions}[$funcid];
768 if(!$funcobj || $funcobj->{first_statement} >= 0)
773 if($_->{flags} & WATCHME_T)
775 $_->{valid} = [-1, undef, undef];
781 my $def = $progs->{globaldef_byoffset}->($func);
783 if $def->{debugname} eq 'error';
790 # for my $ip(keys %write_places)
792 # for(keys %{$write_places{$ip}})
794 # print "; Value is never used in $func->{debugname} at $ip.$_\n";
795 # ++$warned{$ip}{$_};
799 disassemble_function($progs, $func, \%warned)
803 use constant DEFAULTGLOBALS => [
837 if($ofs < @{(DEFAULTGLOBALS)})
839 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
841 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
850 print STDERR "Parsing header...\n";
851 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
853 print STDERR "Parsing strings...\n";
854 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
858 my $endpos = index $p{strings}, "\0", $startpos;
859 return substr $p{strings}, $startpos, $endpos - $startpos;
862 print STDERR "Parsing statements...\n";
863 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
865 print STDERR "Parsing globaldefs...\n";
866 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
868 print STDERR "Parsing fielddefs...\n";
869 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
871 print STDERR "Parsing globals...\n";
872 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
874 print STDERR "Parsing functions...\n";
875 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
877 print STDERR "Detecting temps...\n";
878 my %offsets_saved = ();
879 for(@{$p{globaldefs}})
881 my $type = $_->{type};
882 my $name = $p{getstring}->($_->{s_name});
884 unless $type->{save} or $name ne "";
885 for my $i(0..(typesize($_->{type}{type})-1))
887 ++$offsets_saved{$_->{ofs}+$i};
890 my %offsets_initialized = ();
891 for(0..(@{$p{globals}}-1))
893 if($p{globals}[$_]{v}{int})
895 ++$offsets_initialized{$_};
900 for(0..(@{$p{globals}}-1))
903 if $_ < @{(DEFAULTGLOBALS)};
905 if !$offsets_saved{$_} and $offsets_initialized{$_};
907 if !$offsets_saved{$_} and !$offsets_initialized{$_};
909 $p{temps} = \%istemp;
910 $p{consts} = \%isconst;
912 print STDERR "Naming...\n";
916 for(@{$p{globaldefs}})
918 my $s = $p{getstring}->($_->{s_name});
919 $_->{debugname} //= "_$s"
922 for(@{$p{globaldefs}})
924 $globaldefs[$_->{ofs}] //= $_
925 if defined $_->{debugname};
927 for(@{$p{globaldefs}})
929 $globaldefs[$_->{ofs}] //= $_;
931 for(0..(@{$p{globals}}-1))
933 $globaldefs[$_] //= {
942 if(!defined $_->{debugname})
944 if($istemp{$_->{ofs}})
946 $_->{debugname} = "temp_$_->{ofs}";
948 elsif($isconst{$_->{ofs}})
950 $_->{debugname} = "(" . get_constant(\%p, $p{globals}[$_->{ofs}]{v}) . ")";
954 $_->{debugname} = "global_$_->{ofs}";
957 ++$globaldefs{$_->{debugname}};
962 if $globaldefs{$_->{debugname}} <= 1;
963 print "Not unique: $_->{debugname} at $_->{ofs}\n";
964 $_->{debugname} .= "\@$_->{ofs}";
966 $p{globaldef_byoffset} = sub
970 if($ofs >= 0 && $ofs < @{(DEFAULTGLOBALS)})
972 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
974 my $def = $globaldefs[$ofs];
980 for(@{$p{functions}})
982 my $file = $p{getstring}->($_->{s_file});
983 my $name = $p{getstring}->($_->{s_name});
984 $name = "$file:$name"
986 $_->{debugname} = $name;
987 $functions{$_->{first_statement}} = $_;
989 $p{function_byoffset} = sub
992 return $functions{$ofs};
995 # what do we want to do?
996 my $checkfunc = \&find_uninitialized_locals;
997 #my $checkfunc = \&disassemble_function;
998 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1000 $checkfunc->(\%p, $_);
1004 open my $fh, '<', $ARGV[0];