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};
277 if $progs->{error_func}{$funcid};
282 if($c->{isconditional})
286 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
294 $nfa->($ip+1, $copy_handler->($state));
295 $ip += $s->{$c->{isjump}};
300 $ip += $s->{$c->{isjump}};
312 $nfa->($ip, $copy_handler->($state));
317 my ($progs, $g) = @_;
322 elsif($g->{int} > 0 && $g->{int} < 8388608)
324 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
326 return str($progs->{getstring}->($g->{int}));
330 return $g->{int} . "i";
339 use constant PRE_MARK_STATEMENT => "";
340 use constant POST_MARK_STATEMENT => "";
341 use constant PRE_MARK_OPERAND => "*** ";
342 use constant POST_MARK_OPERAND => " ***";
344 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
345 use constant OPERAND_FORMAT => "%s";
346 use constant OPERAND_SEPARATOR => ", ";
347 use constant INSTRUCTION_SEPARATOR => "\n";
352 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
356 sub disassemble_function($$;$)
358 my ($progs, $func, $highlight) = @_;
360 print "$func->{debugname}:\n";
362 my $initializer = sub
365 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
370 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
371 printf OPERAND_FORMAT, "$func->{parm_start}";
372 print INSTRUCTION_SEPARATOR;
374 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
375 printf OPERAND_FORMAT, "$func->{locals}";
376 print INSTRUCTION_SEPARATOR;
378 my %override_locals = ();
379 my $p = $func->{parm_start};
380 for(0..($func->{numparms}-1))
382 $override_locals{$p} //= "argv_$_";
383 for my $comp(0..($func->{parm_size}[$_]-1))
385 $override_locals{$p} //= "argv_$_\[$comp]";
388 printf INSTRUCTION_FORMAT, '', '', '.ARG';
389 printf OPERAND_FORMAT, "argv_$_";
390 print OPERAND_SEPARATOR;
391 printf OPERAND_FORMAT, $func->{parm_size}[$_];
392 print INSTRUCTION_SEPARATOR;
394 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
397 if exists $override_locals{$_};
398 $override_locals{$_} = "local_$_";
400 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
401 printf OPERAND_FORMAT, "local_$_";
403 print INSTRUCTION_SEPARATOR;
409 return $override_locals{$ofs}
410 if exists $override_locals{$ofs};
411 my $def = $progs->{globaldef_byoffset}->($ofs);
412 return $def->{debugname};
417 my ($ip, $type, $operand) = @_;
418 if($type eq 'inglobal')
420 my $name = $getname->($operand);
421 printf OPERAND_FORMAT, "$name";
423 elsif($type eq 'outglobal')
425 my $name = $getname->($operand);
426 printf OPERAND_FORMAT, "&$name";
428 elsif($type eq 'inglobalvec')
430 my $name = $getname->($operand);
431 printf OPERAND_FORMAT, "$name\[\]";
433 elsif($type eq 'outglobalvec')
435 my $name = $getname->($operand);
436 printf OPERAND_FORMAT, "&$name\[\]";
438 elsif($type eq 'inglobalfunc')
440 my $name = $getname->($operand);
441 printf OPERAND_FORMAT, "$name()";
443 elsif($type eq 'ipoffset')
445 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
449 die "unknown type: $type";
453 my $statements = $func->{statements};
454 my $come_from = $func->{come_from};
457 for my $ip(sort { $a <=> $b } keys %$statements)
459 if($ip == $func->{first_statement})
461 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
462 print INSTRUCTION_SEPARATOR;
464 if(defined $ipprev && $ip != $ipprev + 1)
466 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
467 printf OPERAND_FORMAT, $ip - $ipprev - 1;
468 print INSTRUCTION_SEPARATOR;
470 if(my $cf = $come_from->{$ip})
472 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
474 for(sort { $a <=> $b } keys %$cf)
476 print OPERAND_SEPARATOR
478 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
480 print INSTRUCTION_SEPARATOR;
483 my $op = $progs->{statements}[$ip]{op};
484 my $ipt = $progs->{statements}[$ip];
485 my $opprop = checkop $op;
487 print PRE_MARK_STATEMENT
488 if $highlight and $highlight->{$ip};
490 my $showip = $opprop->{isjump};
491 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? "<!>" : "", $op;
497 if not defined $opprop->{$o};
498 print OPERAND_SEPARATOR
500 print PRE_MARK_OPERAND
501 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
502 $operand->($ip, $opprop->{$o}, $ipt->{$o});
503 print POST_MARK_OPERAND
504 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
507 print POST_MARK_STATEMENT
508 if $highlight and $highlight->{$ip};
510 print INSTRUCTION_SEPARATOR;
514 sub find_uninitialized_locals($$)
516 my ($progs, $func) = @_;
520 if $func->{first_statement} < 0; # builtin
522 print STDERR "Checking $func->{debugname}...\n";
524 my $p = $func->{parm_start};
525 for(0..($func->{numparms}-1))
527 $p += $func->{parm_size}[$_];
530 use constant WATCHME_R => 1;
531 use constant WATCHME_W => 2;
532 use constant WATCHME_X => 4;
533 use constant WATCHME_T => 8;
534 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
536 for(keys %{$progs->{temps}})
538 $watchme{$_} = WATCHME_T | WATCHME_X
539 if not exists $watchme{$_};
542 $watchme{$_} |= WATCHME_R
543 for keys %{$func->{globals_read}};
544 $watchme{$_} |= WATCHME_W
545 for keys %{$func->{globals_written}};
547 my %write_places = ();
548 for my $ofs(keys %{$func->{globals_written}})
551 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
552 for my $ip(keys %{$func->{globals_written}{$ofs}})
554 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
556 push @{$write_places{$ip}{$op}}, $ofs;
564 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
568 if not keys %watchme;
573 flags => $watchme{$_},
574 valid => [0, undef, undef]
578 # mark parameters as initialized
579 for($func->{parm_start} .. ($p-1))
581 $watchme{$_}{valid} = [1, undef, undef]
582 if defined $watchme{$_};
584 # an initial run of STORE instruction is for receiving extra parameters
585 # (beyond 8). Only possible if the function is declared as having 8 params.
586 # Extra parameters behave otherwise like temps, but are initialized at
588 for($func->{first_statement} .. (@{$progs->{statements}}-1))
590 my $s = $progs->{statements}[$_];
591 if($s->{op} eq 'STORE_V')
593 $watchme{$s->{a}}{valid} = [1, undef, undef]
594 if defined $watchme{$s->{a}};
595 $watchme{$s->{a}+1}{valid} = [1, undef, undef]
596 if defined $watchme{$s->{a}+1};
597 $watchme{$s->{a}+2}{valid} = [1, undef, undef]
598 if defined $watchme{$s->{a}+2};
600 elsif($s->{op} =~ /^STORE_/)
602 $watchme{$s->{a}}{valid} = [1, undef, undef]
603 if defined $watchme{$s->{a}};
613 run_nfa $progs, $func->{first_statement}, \%watchme,
616 return { map { $_ => { %{$h->{$_}} } } keys %$h };
619 my ($ip, $state) = @_;
621 my $s = $ip_seen{$ip};
624 # if $state is stronger or equal to $s, return 1
628 if($state->{$_}{valid}[0] < $s->{$_})
630 # The current state is LESS valid than the previously run one. We NEED to run this.
631 # The saved state can safely become the intersection [citation needed].
634 $s->{$_} = $state->{$_}{valid}[0]
635 if $state->{$_}{valid}[0] < $s->{$_};
640 # if we get here, $state is stronger or equal. No need to try it.
645 # Never seen this IP yet.
646 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
651 my ($ip, $state, $s, $c) = @_;
654 my $return_hack = $c->{isreturn} // 0;
660 unless defined $type;
670 if not exists $state->{$ofs};
671 my $valid = $state->{$ofs}{valid};
674 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
676 print "; Use of uninitialized value $ofs in $func->{debugname} at $ip.$_\n";
680 elsif($valid->[0] < 0)
682 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
684 print "; Use of temporary $ofs across CALL in $func->{debugname} at $ip.$_\n";
691 if(defined $valid->[1])
693 delete $write_places{$valid->[1]}{$valid->[2]};
700 $state->{$ofs}{valid} = [1, $ip, $_]
701 if exists $state->{$ofs};
704 if($type eq 'inglobal' || $type eq 'inglobalfunc')
708 elsif($type eq 'inglobalvec')
714 elsif($type eq 'outglobal')
718 elsif($type eq 'outglobalvec')
727 # builtin calls may clobber stuff
729 my $funcid = $progs->{globals}[$func]{v}{int};
730 my $funcobj = $progs->{functions}[$funcid];
731 if(!$funcobj || $funcobj->{first_statement} >= 0)
736 if($_->{flags} & WATCHME_T)
738 $_->{valid} = [-1, undef, undef];
747 for my $ip(keys %write_places)
749 for my $operand(keys %{$write_places{$ip}})
752 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
755 my %writeplace_seen = ();
756 run_nfa $progs, $ip+1, \%left,
763 my ($ip, $state) = @_;
764 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
768 my ($ip, $state, $s, $c) = @_;
773 unless defined $type;
776 if($type eq 'inglobal' || $type eq 'inglobalfunc')
781 return -1; # exit TOTALLY
784 elsif($type eq 'inglobalvec')
786 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
789 return -1; # exit TOTALLY
792 elsif($type eq 'outglobal')
794 delete $state->{$ofs};
798 elsif($type eq 'outglobalvec')
800 delete $state->{$ofs};
801 delete $state->{$ofs+1};
802 delete $state->{$ofs+2};
812 print "; Value is never used in $func->{debugname} at $ip.$operand\n";
813 ++$warned{$ip}{$operand};
818 disassemble_function($progs, $func, \%warned)
822 use constant DEFAULTGLOBALS => [
856 if($ofs < @{(DEFAULTGLOBALS)})
858 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
860 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
863 sub detect_constants($)
866 use constant GLOBALFLAG_R => 1; # read
867 use constant GLOBALFLAG_W => 2; # written
868 use constant GLOBALFLAG_S => 4; # saved
869 use constant GLOBALFLAG_I => 8; # initialized
870 use constant GLOBALFLAG_N => 16; # named
871 use constant GLOBALFLAG_Q => 32; # unique to function
872 use constant GLOBALFLAG_U => 64; # unused
873 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x @{$progs->{globals}};
875 for(@{$progs->{functions}})
877 for(keys %{$_->{globals_used}})
879 if($globalflags[$_] & GLOBALFLAG_Q)
881 $globalflags[$_] &= ~GLOBALFLAG_Q;
885 $globalflags[$_] &= ~GLOBALFLAG_U;
888 $globalflags[$_] |= GLOBALFLAG_R
889 for keys %{$_->{globals_read}};
890 $globalflags[$_] |= GLOBALFLAG_W
891 for keys %{$_->{globals_written}};
894 my %offsets_saved = ();
895 for(@{$progs->{globaldefs}})
897 my $type = $_->{type};
898 my $name = $progs->{getstring}->($_->{s_name});
901 for my $i(0..(typesize($_->{type}{type})-1))
903 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
908 for my $i(0..(typesize($_->{type}{type})-1))
910 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
914 my %offsets_initialized = ();
915 for(0..(@{$progs->{globals}}-1))
917 if($progs->{globals}[$_]{v}{int})
919 $globalflags[$_] |= GLOBALFLAG_I;
923 my @globaltypes = (undef) x @{$progs->{globals}};
926 for(0..(@{$progs->{globals}}-1))
929 if $_ < @{(DEFAULTGLOBALS)};
930 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
932 $globaltypes[$_] = "unused";
934 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
937 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
939 $globaltypes[$_] = "read_only";
941 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
943 $globaltypes[$_] = "const";
947 $globaltypes[$_] = "read_only";
950 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
952 $globaltypes[$_] = "write_only";
956 # now we know it is rw
957 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
959 $globaltypes[$_] = "global";
961 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I | GLOBALFLAG_Q)) == GLOBALFLAG_Q)
963 $globaltypes[$_] = "uniquetemp";
965 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I | GLOBALFLAG_Q)) == 0)
967 $globaltypes[$_] = "temp";
970 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
972 $globaltypes[$_] = "not_saved";
976 $globaltypes[$_] = "global";
980 $progs->{temps} = \%istemp;
983 my @globaldefs = (undef) x @{$progs->{globaldefs}};
984 for(@{$progs->{globaldefs}})
986 my $s = $progs->{getstring}->($_->{s_name});
987 $_->{debugname} //= "\$" . "$s"
990 for(@{$progs->{globaldefs}})
992 $globaldefs[$_->{ofs}] //= $_
993 if defined $_->{debugname};
995 for(@{$progs->{globaldefs}})
997 $globaldefs[$_->{ofs}] //= $_;
999 for(0..(@{$progs->{globals}}-1))
1001 $globaldefs[$_] //= {
1007 for(0..(@{(DEFAULTGLOBALS)}-1))
1009 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1010 $globaltypes[$_] = 'defglobal';
1012 my %globaldefs_namecount = ();
1015 $_->{globaltype} = $globaltypes[$_->{ofs}];
1016 if(defined $_->{debugname})
1018 # already has debugname
1020 elsif($_->{globaltype} eq 'const')
1022 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1026 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1028 ++$globaldefs_namecount{$_->{debugname}};
1033 if $globaldefs_namecount{$_->{debugname}} <= 1;
1034 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1035 $_->{debugname} .= "\@$_->{ofs}";
1037 $progs->{globaldef_byoffset} = sub
1040 my $def = $globaldefs[$ofs];
1051 print STDERR "Parsing header...\n";
1052 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1054 print STDERR "Parsing strings...\n";
1055 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1058 my ($startpos) = @_;
1059 my $endpos = index $p{strings}, "\0", $startpos;
1060 return substr $p{strings}, $startpos, $endpos - $startpos;
1063 print STDERR "Parsing statements...\n";
1064 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1066 print STDERR "Fixing statements...\n";
1067 for my $s(@{$p{statements}})
1069 my $c = checkop $s->{op};
1073 my $type = $c->{$_};
1075 unless defined $type;
1077 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1081 elsif($type eq 'inglobalvec')
1085 elsif($type eq 'outglobal')
1089 elsif($type eq 'outglobalvec')
1097 print STDERR "Parsing globaldefs...\n";
1098 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1100 print STDERR "Parsing fielddefs...\n";
1101 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1103 print STDERR "Parsing globals...\n";
1104 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1106 print STDERR "Parsing functions...\n";
1107 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1109 print STDERR "Looking for error()...\n";
1110 $p{error_func} = {};
1111 for(@{$p{globaldefs}})
1114 if $p{getstring}($_->{s_name}) ne 'error';
1115 my $v = $p{globals}[$_->{ofs}]{v}{int};
1117 if $v <= 0 || $v >= @{$p{functions}};
1118 my $first = $p{functions}[$v]{first_statement};
1121 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1122 $p{error_func}{$_->{ofs}} = 1;
1125 print STDERR "Scanning functions...\n";
1126 for(@{$p{functions}})
1128 my $file = $p{getstring}->($_->{s_file});
1129 my $name = $p{getstring}->($_->{s_name});
1130 $name = "$file:$name"
1132 $_->{debugname} = $name;
1135 if $_->{first_statement} < 0;
1137 my %statements = ();
1140 my %globals_read = ();
1141 my %globals_written = ();
1142 my %globals_used = ();
1144 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1147 my ($ip, $state, $s, $c) = @_;
1150 if(my $j = $c->{isjump})
1152 my $t = $ip + $s->{$j};
1153 $come_from{$t}{$ip} = $c->{isconditional};
1154 $go_to{$ip}{$t} = $c->{isconditional};
1157 for my $o(qw(a b c))
1166 $globals_read{$ofs}{$ip}{$o} = 1;
1167 $globals_used{$ofs} = 1;
1172 $globals_written{$ofs}{$ip}{$o} = 1;
1173 $globals_used{$ofs} = 1;
1176 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1180 elsif($type eq 'inglobalvec')
1186 elsif($type eq 'outglobal')
1190 elsif($type eq 'outglobalvec')
1201 $_->{statements} = \%statements;
1202 $_->{come_from} = \%come_from;
1203 $_->{go_to} = \%go_to;
1204 $_->{globals_read} = \%globals_read;
1205 $_->{globals_written} = \%globals_written;
1206 $_->{globals_used} = \%globals_used;
1208 # using this info, we could now identify basic blocks
1211 print STDERR "Detecting constants and temps, and naming...\n";
1212 detect_constants \%p;
1214 # what do we want to do?
1215 my $checkfunc = \&find_uninitialized_locals;
1216 #my $checkfunc = \&disassemble_function;
1217 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1219 $checkfunc->(\%p, $_);
1223 open my $fh, '<', $ARGV[0];