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 if($highlight and $highlight->{$ip})
489 for(values %{$highlight->{$ip}})
493 print PRE_MARK_STATEMENT;
494 printf INSTRUCTION_FORMAT, '', '<!>', '.WARN';
495 printf OPERAND_FORMAT, "$_ (in $func->{debugname})";
496 print INSTRUCTION_SEPARATOR;
501 print PRE_MARK_STATEMENT
502 if $highlight and $highlight->{$ip};
504 my $showip = $opprop->{isjump};
505 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? '<!>' : '', $op;
511 if not defined $opprop->{$o};
512 print OPERAND_SEPARATOR
514 print PRE_MARK_OPERAND
515 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
516 $operand->($ip, $opprop->{$o}, $ipt->{$o});
517 print POST_MARK_OPERAND
518 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
521 print POST_MARK_STATEMENT
522 if $highlight and $highlight->{$ip};
524 print INSTRUCTION_SEPARATOR;
528 sub find_uninitialized_locals($$)
530 my ($progs, $func) = @_;
534 if $func->{first_statement} < 0; # builtin
536 print STDERR "Checking $func->{debugname}...\n";
538 my $p = $func->{parm_start};
539 for(0..($func->{numparms}-1))
541 $p += $func->{parm_size}[$_];
544 use constant WATCHME_R => 1;
545 use constant WATCHME_W => 2;
546 use constant WATCHME_X => 4;
547 use constant WATCHME_T => 8;
548 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
550 for(keys %{$progs->{temps}})
553 if exists $watchme{$_};
554 if($progs->{temps}{$_})
557 $watchme{$_} = WATCHME_T | WATCHME_X
562 $watchme{$_} = WATCHME_X
566 $watchme{$_} |= WATCHME_R
567 for keys %{$func->{globals_read}};
568 $watchme{$_} |= WATCHME_W
569 for keys %{$func->{globals_written}};
571 my %write_places = ();
572 for my $ofs(keys %{$func->{globals_written}})
575 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
576 for my $ip(keys %{$func->{globals_written}{$ofs}})
578 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
580 push @{$write_places{$ip}{$op}}, $ofs;
588 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
592 if not keys %watchme;
597 flags => $watchme{$_},
598 valid => [0, undef, undef]
602 # mark parameters as initialized
603 for($func->{parm_start} .. ($p-1))
605 $watchme{$_}{valid} = [1, undef, undef]
606 if defined $watchme{$_};
611 run_nfa $progs, $func->{first_statement}, \%watchme,
614 return { map { $_ => { %{$h->{$_}} } } keys %$h };
617 my ($ip, $state) = @_;
619 my $s = $ip_seen{$ip};
622 # if $state is stronger or equal to $s, return 1
626 if($state->{$_}{valid}[0] < $s->{$_})
628 # The current state is LESS valid than the previously run one. We NEED to run this.
629 # The saved state can safely become the intersection [citation needed].
632 $s->{$_} = $state->{$_}{valid}[0]
633 if $state->{$_}{valid}[0] < $s->{$_};
638 # if we get here, $state is stronger or equal. No need to try it.
643 # Never seen this IP yet.
644 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
649 my ($ip, $state, $s, $c) = @_;
652 # QCVM BUG: RETURN always takes vector, there is no float equivalent
653 my $return_hack = $c->{isreturn} // 0;
657 # COMPILER BUG of QCC: params are always copied using STORE_V
658 if($s->{b} >= 4 && $s->{b} < 28) # parameter range
668 unless defined $type;
678 if not exists $state->{$ofs};
679 my $valid = $state->{$ofs}{valid};
682 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
683 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
685 push @{$warned{$ip}{$_}}, "Use of uninitialized value";
688 elsif($valid->[0] < 0)
690 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
691 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
693 push @{$warned{$ip}{$_}}, "Use of temporary across CALL";
699 if(defined $valid->[1])
701 delete $write_places{$valid->[1]}{$valid->[2]};
708 $state->{$ofs}{valid} = [1, $ip, $_]
709 if exists $state->{$ofs};
712 if($type eq 'inglobal' || $type eq 'inglobalfunc')
716 elsif($type eq 'inglobalvec')
722 elsif($type eq 'outglobal')
726 elsif($type eq 'outglobalvec')
735 # builtin calls may clobber stuff
737 my $funcid = $progs->{globals}[$func]{v}{int};
738 my $funcobj = $progs->{functions}[$funcid];
739 if(!$funcobj || $funcobj->{first_statement} >= 0)
744 if($_->{flags} & WATCHME_T)
746 $_->{valid} = [-1, undef, undef];
755 for my $ip(keys %write_places)
757 for my $operand(keys %{$write_places{$ip}})
760 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
763 my %writeplace_seen = ();
764 run_nfa $progs, $ip+1, \%left,
771 my ($ip, $state) = @_;
772 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
776 my ($ip, $state, $s, $c) = @_;
781 unless defined $type;
784 if($type eq 'inglobal' || $type eq 'inglobalfunc')
789 return -1; # exit TOTALLY
792 elsif($type eq 'inglobalvec')
794 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
797 return -1; # exit TOTALLY
800 elsif($type eq 'outglobal')
802 delete $state->{$ofs};
806 elsif($type eq 'outglobalvec')
808 delete $state->{$ofs};
809 delete $state->{$ofs+1};
810 delete $state->{$ofs+2};
820 push @{$warned{$ip}{$operand}}, "Value is never used";
825 disassemble_function($progs, $func, \%warned)
829 use constant DEFAULTGLOBALS => [
863 if($ofs < @{(DEFAULTGLOBALS)})
865 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
867 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
870 sub detect_constants($)
873 use constant GLOBALFLAG_R => 1; # read
874 use constant GLOBALFLAG_W => 2; # written
875 use constant GLOBALFLAG_S => 4; # saved
876 use constant GLOBALFLAG_I => 8; # initialized
877 use constant GLOBALFLAG_N => 16; # named
878 use constant GLOBALFLAG_Q => 32; # unique to function
879 use constant GLOBALFLAG_U => 64; # unused
880 use constant GLOBALFLAG_P => 128; # possibly parameter passing
881 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x @{$progs->{globals}};
883 for(@{$progs->{functions}})
885 for(keys %{$_->{globals_used}})
887 if($globalflags[$_] & GLOBALFLAG_U)
889 $globalflags[$_] &= ~GLOBALFLAG_U;
891 elsif($globalflags[$_] & GLOBALFLAG_Q)
893 $globalflags[$_] &= ~GLOBALFLAG_Q;
896 $globalflags[$_] |= GLOBALFLAG_R
897 for keys %{$_->{globals_read}};
898 $globalflags[$_] |= GLOBALFLAG_W
899 for keys %{$_->{globals_written}};
900 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
902 my $s = $progs->{statements}[$ip];
903 if($s->{op} eq 'STORE_V')
905 $globalflags[$s->{a}] |= GLOBALFLAG_P
906 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
907 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
908 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
909 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
910 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
912 elsif($s->{op} =~ /^STORE_/)
914 $globalflags[$s->{a}] |= GLOBALFLAG_P
915 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
924 # parameter passing globals are only ever used in STORE_ instructions
925 for my $s(@{$progs->{statements}})
928 if $s->{op} =~ /^STORE_/;
930 my $c = checkop $s->{op};
936 unless defined $type;
939 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
941 $globalflags[$ofs] &= ~GLOBALFLAG_P;
943 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
945 $globalflags[$ofs] &= ~GLOBALFLAG_P;
946 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
947 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
952 my %offsets_saved = ();
953 for(@{$progs->{globaldefs}})
955 my $type = $_->{type};
956 my $name = $progs->{getstring}->($_->{s_name});
959 for my $i(0..(typesize($_->{type}{type})-1))
961 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
966 for my $i(0..(typesize($_->{type}{type})-1))
968 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
972 my %offsets_initialized = ();
973 for(0..(@{$progs->{globals}}-1))
975 if($progs->{globals}[$_]{v}{int})
977 $globalflags[$_] |= GLOBALFLAG_I;
981 my @globaltypes = (undef) x @{$progs->{globals}};
984 for(0..(@{$progs->{globals}}-1))
987 if $_ < @{(DEFAULTGLOBALS)};
988 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
990 $globaltypes[$_] = "unused";
992 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
995 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
997 $globaltypes[$_] = "read_only";
999 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1001 $globaltypes[$_] = "const";
1005 $globaltypes[$_] = "read_only";
1008 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1010 $globaltypes[$_] = "write_only";
1014 # now we know it is rw
1015 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1017 $globaltypes[$_] = "global";
1019 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1021 if($globalflags[$_] & GLOBALFLAG_P)
1023 $globaltypes[$_] = "OFS_PARM";
1025 elsif($globalflags[$_] & GLOBALFLAG_Q)
1027 $globaltypes[$_] = "uniquetemp";
1032 $globaltypes[$_] = "temp";
1036 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1038 $globaltypes[$_] = "not_saved";
1042 $globaltypes[$_] = "global";
1046 $progs->{temps} = \%istemp;
1049 my @globaldefs = (undef) x @{$progs->{globaldefs}};
1050 for(@{$progs->{globaldefs}})
1052 my $s = $progs->{getstring}->($_->{s_name});
1053 $_->{debugname} //= "\$" . "$s"
1056 for(@{$progs->{globaldefs}})
1058 $globaldefs[$_->{ofs}] //= $_
1059 if defined $_->{debugname};
1061 for(@{$progs->{globaldefs}})
1063 $globaldefs[$_->{ofs}] //= $_;
1065 for(0..(@{$progs->{globals}}-1))
1067 $globaldefs[$_] //= {
1073 for(0..(@{(DEFAULTGLOBALS)}-1))
1075 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1076 $globaltypes[$_] = 'defglobal';
1078 my %globaldefs_namecount = ();
1081 $_->{globaltype} = $globaltypes[$_->{ofs}];
1082 if(defined $_->{debugname})
1084 # already has debugname
1086 elsif($_->{globaltype} eq 'const')
1088 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1092 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1094 ++$globaldefs_namecount{$_->{debugname}};
1099 if $globaldefs_namecount{$_->{debugname}} <= 1;
1100 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1101 $_->{debugname} .= "\@$_->{ofs}";
1103 $progs->{globaldef_byoffset} = sub
1106 my $def = $globaldefs[$ofs];
1117 print STDERR "Parsing header...\n";
1118 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1120 print STDERR "Parsing strings...\n";
1121 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1124 my ($startpos) = @_;
1125 my $endpos = index $p{strings}, "\0", $startpos;
1126 return substr $p{strings}, $startpos, $endpos - $startpos;
1129 print STDERR "Parsing statements...\n";
1130 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1132 print STDERR "Fixing statements...\n";
1133 for my $s(@{$p{statements}})
1135 my $c = checkop $s->{op};
1139 my $type = $c->{$_};
1141 unless defined $type;
1143 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1147 elsif($type eq 'inglobalvec')
1151 elsif($type eq 'outglobal')
1155 elsif($type eq 'outglobalvec')
1162 print STDERR "Parsing globaldefs...\n";
1163 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1165 print STDERR "Parsing fielddefs...\n";
1166 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1168 print STDERR "Parsing globals...\n";
1169 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1171 print STDERR "Parsing functions...\n";
1172 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1174 print STDERR "Looking for error()...\n";
1175 $p{error_func} = {};
1176 for(@{$p{globaldefs}})
1179 if $p{getstring}($_->{s_name}) ne 'error';
1180 my $v = $p{globals}[$_->{ofs}]{v}{int};
1182 if $v <= 0 || $v >= @{$p{functions}};
1183 my $first = $p{functions}[$v]{first_statement};
1186 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1187 $p{error_func}{$_->{ofs}} = 1;
1190 print STDERR "Scanning functions...\n";
1191 for(@{$p{functions}})
1193 my $file = $p{getstring}->($_->{s_file});
1194 my $name = $p{getstring}->($_->{s_name});
1195 $name = "$file:$name"
1197 $_->{debugname} = $name;
1200 if $_->{first_statement} < 0;
1202 my %statements = ();
1205 my %globals_read = ();
1206 my %globals_written = ();
1207 my %globals_used = ();
1209 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1212 my ($ip, $state, $s, $c) = @_;
1215 if(my $j = $c->{isjump})
1217 my $t = $ip + $s->{$j};
1218 $come_from{$t}{$ip} = $c->{isconditional};
1219 $go_to{$ip}{$t} = $c->{isconditional};
1222 for my $o(qw(a b c))
1231 $globals_read{$ofs}{$ip}{$o} = 1;
1232 $globals_used{$ofs} = 1;
1237 $globals_written{$ofs}{$ip}{$o} = 1;
1238 $globals_used{$ofs} = 1;
1241 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1245 elsif($type eq 'inglobalvec')
1251 elsif($type eq 'outglobal')
1255 elsif($type eq 'outglobalvec')
1266 $_->{statements} = \%statements;
1267 $_->{come_from} = \%come_from;
1268 $_->{go_to} = \%go_to;
1269 $_->{globals_read} = \%globals_read;
1270 $_->{globals_written} = \%globals_written;
1271 $_->{globals_used} = \%globals_used;
1273 # using this info, we could now identify basic blocks
1276 print STDERR "Detecting constants and temps, and naming...\n";
1277 detect_constants \%p;
1279 # what do we want to do?
1280 my $checkfunc = \&find_uninitialized_locals;
1281 #my $checkfunc = \&disassemble_function;
1282 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1284 $checkfunc->(\%p, $_);
1288 open my $fh, '<', $ARGV[0];