14 return sub { $_[0] >= (2**($bits-1)) ? $_[0]-(2**$bits) : $_[0]; };
17 use constant OPCODE_E => [qw[
19 MUL_F MUL_V MUL_FV MUL_VF
23 EQ_F EQ_V EQ_S EQ_E EQ_FNC
24 NE_F NE_V NE_S NE_E NE_FNC
26 LOAD_F LOAD_V LOAD_S LOAD_ENT LOAD_FLD LOAD_FNC
28 STORE_F STORE_V STORE_S STORE_ENT STORE_FLD STORE_FNC
29 STOREP_F STOREP_V STOREP_S STOREP_ENT STOREP_FLD STOREP_FNC
31 NOT_F NOT_V NOT_S NOT_ENT NOT_FNC
33 CALL0 CALL1 CALL2 CALL3 CALL4 CALL5 CALL6 CALL7 CALL8
39 use constant ETYPE_E => [qw[
49 use constant DEF_SAVEGLOBAL => 32768;
53 return 3 if $type eq 'vector';
62 return { a => 'inglobalvec', b => 'ipoffset', isjump => 'b', isconditional => 1 };
66 return { a => 'inglobal', b => 'ipoffset', isjump => 'b', isconditional => 1 };
70 return { a => 'ipoffset', isjump => 'a', isconditional => 0 };
72 if($op =~ /^ADD_V$|^SUB_V$/)
74 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobalvec' };
76 if($op =~ /^MUL_V$|^EQ_V$|^NE_V$/)
78 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobal' };
82 return { a => 'inglobal', b => 'inglobalvec', c => 'outglobalvec' };
86 return { a => 'inglobalvec', b => 'inglobal', c => 'outglobalvec' };
90 return { a => 'inglobal', b => 'inglobal', c => 'outglobalvec' };
94 return { a => 'inglobalvec', c => 'outglobal' };
98 return { a => 'inglobal', c => 'outglobal' };
100 if($op eq 'STOREP_V')
102 return { a => 'inglobalvec', b => 'inglobal' };
106 return { a => 'inglobalvec', b => 'outglobalvec' };
108 if($op =~ /^STOREP_/)
110 return { a => 'inglobal', b => 'inglobal' };
114 return { a => 'inglobal', b => 'outglobal' };
118 return { a => 'inglobalfunc', iscall => 1 };
120 if($op =~ /^DONE$|^RETURN$/)
122 return { a => 'inglobalvec', isreturn => 1 };
124 return { a => 'inglobal', b => 'inglobal', c => 'outglobal' };
127 use constant TYPES => {
128 int => ['V', 4, signed 32],
129 ushort => ['v', 2, id],
130 short => ['v', 2, signed 16],
131 opcode => ['v', 2, sub { OPCODE_E->[$_[0]] or do { warn "Invalid opcode: $_[0]"; "INVALID#$_[0]"; }; }],
132 float => ['f', 4, id],
133 uchar8 => ['a8', 8, sub { [unpack 'C8', $_[0]] }],
134 global => ['i', 4, sub { { int => $_[0], float => unpack "f", pack "L", $_[0] }; }],
135 deftype => ['v', 2, sub { { type => ETYPE_E->[$_[0] & ~DEF_SAVEGLOBAL], save => !!($_[0] & DEF_SAVEGLOBAL) }; }],
138 use constant DPROGRAMS_T => [
141 [int => 'ofs_statements'],
142 [int => 'numstatements'],
143 [int => 'ofs_globaldefs'],
144 [int => 'numglobaldefs'],
145 [int => 'ofs_fielddefs'],
146 [int => 'numfielddefs'],
147 [int => 'ofs_functions'],
148 [int => 'numfunctions'],
149 [int => 'ofs_strings'],
150 [int => 'numstrings'],
151 [int => 'ofs_globals'],
152 [int => 'numglobals'],
153 [int => 'entityfields']
156 use constant DSTATEMENT_T => [
163 use constant DDEF_T => [
169 use constant DGLOBAL_T => [
173 use constant DFUNCTION_T => [
174 [int => 'first_statement'],
175 [int => 'parm_start'],
181 [uchar8 => 'parm_size'],
186 my ($fh, $start, $len) = @_;
189 $len == read $fh, my $buf, $len
190 or die "short read from $start length $len (malformed progs header)";
194 sub parse_section($$$$$)
196 my ($fh, $struct, $start, $len, $cnt) = @_;
199 $itemlen += TYPES->{$_->[0]}->[1]
201 my $packspec = join '', map { TYPES->{$_->[0]}->[0]; } @$struct;
202 my @packnames = map { $_->[1]; } @$struct;
204 $len = $cnt * $itemlen
205 if not defined $len and defined $cnt;
206 $cnt = int($len / $itemlen)
207 if not defined $cnt and defined $len;
208 die "Invalid length specification"
209 unless defined $len and defined $cnt and $len == $cnt * $itemlen;
210 die "Invalid length specification in scalar context"
211 unless wantarray or $cnt == 1;
217 $itemlen == read $fh, my $buf, $itemlen
218 or die "short read from $start length $cnt * $itemlen $(malformed progs header)";
220 @h{@packnames} = unpack $packspec, $buf;
221 $h{$_->[1]} = TYPES->{$_->[0]}->[2]->($h{$_->[1]})
231 sub nfa_default_state_checker()
236 my ($ip, $state) = @_;
237 return $seen{"$ip $state"}++;
243 my ($progs, $ip, $state, $copy_handler, $state_checker, $instruction_handler) = @_;
245 my $statements = $progs->{statements};
250 no warnings 'recursion';
252 my ($ip, $state) = @_;
258 if $state_checker->($ip, $state);
260 my $s = $statements->[$ip];
261 my $c = checkop $s->{op};
263 if(($ret = $instruction_handler->($ip, $state, $s, $c)))
276 my $funcid = $progs->{globals}[$func]{v}{int};
278 if $progs->{error_func}{$funcid};
283 if($c->{isconditional})
287 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
295 $nfa->($ip+1, $copy_handler->($state));
296 $ip += $s->{$c->{isjump}};
301 $ip += $s->{$c->{isjump}};
313 $nfa->($ip, $copy_handler->($state));
318 my ($progs, $g) = @_;
323 elsif($g->{int} > 0 && $g->{int} < 8388608)
325 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
327 return str($progs->{getstring}->($g->{int}));
331 return $g->{int} . "i";
340 use constant PRE_MARK_STATEMENT => "";
341 use constant POST_MARK_STATEMENT => "";
342 use constant PRE_MARK_OPERAND => "*** ";
343 use constant POST_MARK_OPERAND => " ***";
345 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
346 use constant OPERAND_FORMAT => "%s";
347 use constant OPERAND_SEPARATOR => ", ";
348 use constant INSTRUCTION_SEPARATOR => "\n";
353 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
357 sub disassemble_function($$;$)
359 my ($progs, $func, $highlight) = @_;
361 print "$func->{debugname}:\n";
363 if($func->{first_statement} < 0) # builtin
365 printf INSTRUCTION_FORMAT, '', '', '.BUILTIN';
366 printf OPERAND_FORMAT, -$func->{first_statement};
367 print INSTRUCTION_SEPARATOR;
371 my $initializer = sub
374 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
379 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
380 printf OPERAND_FORMAT, "$func->{parm_start}";
381 print INSTRUCTION_SEPARATOR;
383 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
384 printf OPERAND_FORMAT, "$func->{locals}";
385 print INSTRUCTION_SEPARATOR;
387 my %override_locals = ();
388 my $p = $func->{parm_start};
389 for(0..($func->{numparms}-1))
391 $override_locals{$p} //= "argv_$_";
392 for my $comp(0..($func->{parm_size}[$_]-1))
394 $override_locals{$p} //= "argv_$_\[$comp]";
397 printf INSTRUCTION_FORMAT, '', '', '.ARG';
398 printf OPERAND_FORMAT, "argv_$_";
399 print OPERAND_SEPARATOR;
400 printf OPERAND_FORMAT, $func->{parm_size}[$_];
401 print INSTRUCTION_SEPARATOR;
403 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
406 if exists $override_locals{$_};
407 $override_locals{$_} = "local_$_";
409 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
410 printf OPERAND_FORMAT, "local_$_";
412 print INSTRUCTION_SEPARATOR;
418 return $override_locals{$ofs}
419 if exists $override_locals{$ofs};
420 my $def = $progs->{globaldef_byoffset}->($ofs);
421 return $def->{debugname};
426 my ($ip, $type, $operand) = @_;
427 if($type eq 'inglobal')
429 my $name = $getname->($operand);
430 printf OPERAND_FORMAT, "$name";
432 elsif($type eq 'outglobal')
434 my $name = $getname->($operand);
435 printf OPERAND_FORMAT, "&$name";
437 elsif($type eq 'inglobalvec')
439 my $name = $getname->($operand);
440 printf OPERAND_FORMAT, "$name\[\]";
442 elsif($type eq 'outglobalvec')
444 my $name = $getname->($operand);
445 printf OPERAND_FORMAT, "&$name\[\]";
447 elsif($type eq 'inglobalfunc')
449 my $name = $getname->($operand);
450 printf OPERAND_FORMAT, "$name()";
452 elsif($type eq 'ipoffset')
454 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
458 die "unknown type: $type";
462 my $statements = $func->{statements};
463 my $come_from = $func->{come_from};
466 for my $ip(sort { $a <=> $b } keys %$statements)
468 if($ip == $func->{first_statement})
470 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
471 print INSTRUCTION_SEPARATOR;
473 if(defined $ipprev && $ip != $ipprev + 1)
475 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
476 printf OPERAND_FORMAT, $ip - $ipprev - 1;
477 print INSTRUCTION_SEPARATOR;
479 if(my $cf = $come_from->{$ip})
481 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
483 for(sort { $a <=> $b } keys %$cf)
485 print OPERAND_SEPARATOR
487 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
489 print INSTRUCTION_SEPARATOR;
492 my $op = $progs->{statements}[$ip]{op};
493 my $ipt = $progs->{statements}[$ip];
494 my $opprop = checkop $op;
496 if($highlight and $highlight->{$ip})
498 for(values %{$highlight->{$ip}})
502 print PRE_MARK_STATEMENT;
503 printf INSTRUCTION_FORMAT, '', '<!>', '.WARN';
504 printf OPERAND_FORMAT, "$_ (in $func->{debugname})";
505 print INSTRUCTION_SEPARATOR;
510 print PRE_MARK_STATEMENT
511 if $highlight and $highlight->{$ip};
513 my $showip = $opprop->{isjump};
514 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? '<!>' : '', $op;
520 if not defined $opprop->{$o};
521 print OPERAND_SEPARATOR
523 print PRE_MARK_OPERAND
524 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
525 $operand->($ip, $opprop->{$o}, $ipt->{$o});
526 print POST_MARK_OPERAND
527 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
530 print POST_MARK_STATEMENT
531 if $highlight and $highlight->{$ip};
533 print INSTRUCTION_SEPARATOR;
537 sub find_uninitialized_locals($$)
539 my ($progs, $func) = @_;
542 if $func->{first_statement} < 0; # builtin
544 print STDERR "Checking $func->{debugname}...\n";
546 my $p = $func->{parm_start};
547 for(0..($func->{numparms}-1))
549 $p += $func->{parm_size}[$_];
552 use constant WATCHME_R => 1;
553 use constant WATCHME_W => 2;
554 use constant WATCHME_X => 4;
555 use constant WATCHME_T => 8;
556 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
558 for(keys %{$progs->{temps}})
561 if exists $watchme{$_};
562 if($progs->{temps}{$_})
565 $watchme{$_} = WATCHME_T | WATCHME_X
570 $watchme{$_} = WATCHME_X
574 $watchme{$_} |= WATCHME_R
575 for keys %{$func->{globals_read}};
576 $watchme{$_} |= WATCHME_W
577 for keys %{$func->{globals_written}};
579 my %write_places = ();
580 for my $ofs(keys %{$func->{globals_written}})
583 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
584 for my $ip(keys %{$func->{globals_written}{$ofs}})
586 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
588 push @{$write_places{$ip}{$op}}, $ofs;
596 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
600 if not keys %watchme;
605 flags => $watchme{$_},
606 valid => [0, undef, undef]
610 # mark parameters as initialized
611 for($func->{parm_start} .. ($p-1))
613 $watchme{$_}{valid} = [1, undef, undef]
614 if defined $watchme{$_};
619 run_nfa $progs, $func->{first_statement}, \%watchme,
622 return { map { $_ => { %{$h->{$_}} } } keys %$h };
625 my ($ip, $state) = @_;
627 my $s = $ip_seen{$ip};
630 # if $state is stronger or equal to $s, return 1
634 if($state->{$_}{valid}[0] < $s->{$_})
636 # The current state is LESS valid than the previously run one. We NEED to run this.
637 # The saved state can safely become the intersection [citation needed].
640 $s->{$_} = $state->{$_}{valid}[0]
641 if $state->{$_}{valid}[0] < $s->{$_};
646 # if we get here, $state is stronger or equal. No need to try it.
651 # Never seen this IP yet.
652 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
657 my ($ip, $state, $s, $c) = @_;
660 # QCVM BUG: RETURN always takes vector, there is no float equivalent
661 my $return_hack = $c->{isreturn} // 0;
665 # COMPILER BUG of QCC: params are always copied using STORE_V
666 if($s->{b} >= 4 && $s->{b} < 28) # parameter range
676 unless defined $type;
686 if not exists $state->{$ofs};
687 my $valid = $state->{$ofs}{valid};
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 uninitialized value";
696 elsif($valid->[0] < 0)
698 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
699 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
701 push @{$warned{$ip}{$_}}, "Use of temporary across CALL";
707 if(defined $valid->[1])
709 delete $write_places{$valid->[1]}{$valid->[2]};
716 $state->{$ofs}{valid} = [1, $ip, $_]
717 if exists $state->{$ofs};
720 if($type eq 'inglobal' || $type eq 'inglobalfunc')
724 elsif($type eq 'inglobalvec')
730 elsif($type eq 'outglobal')
734 elsif($type eq 'outglobalvec')
743 # builtin calls may clobber stuff
745 my $funcid = $progs->{globals}[$func]{v}{int};
746 my $funcobj = $progs->{functions}[$funcid];
747 if(!$funcobj || $funcobj->{first_statement} >= 0)
752 if($_->{flags} & WATCHME_T)
754 $_->{valid} = [-1, undef, undef];
763 for my $ip(keys %write_places)
765 for my $operand(keys %{$write_places{$ip}})
768 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
771 my %writeplace_seen = ();
772 run_nfa $progs, $ip+1, \%left,
779 my ($ip, $state) = @_;
780 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
784 my ($ip, $state, $s, $c) = @_;
789 unless defined $type;
792 if($type eq 'inglobal' || $type eq 'inglobalfunc')
797 return -1; # exit TOTALLY
800 elsif($type eq 'inglobalvec')
802 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
805 return -1; # exit TOTALLY
808 elsif($type eq 'outglobal')
810 delete $state->{$ofs};
814 elsif($type eq 'outglobalvec')
816 delete $state->{$ofs};
817 delete $state->{$ofs+1};
818 delete $state->{$ofs+2};
828 push @{$warned{$ip}{$operand}}, "Value is never used";
833 disassemble_function($progs, $func, \%warned)
837 use constant DEFAULTGLOBALS => [
871 if($ofs < @{(DEFAULTGLOBALS)})
873 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
875 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
878 sub detect_constants($)
881 use constant GLOBALFLAG_R => 1; # read
882 use constant GLOBALFLAG_W => 2; # written
883 use constant GLOBALFLAG_S => 4; # saved
884 use constant GLOBALFLAG_I => 8; # initialized
885 use constant GLOBALFLAG_N => 16; # named
886 use constant GLOBALFLAG_Q => 32; # unique to function
887 use constant GLOBALFLAG_U => 64; # unused
888 use constant GLOBALFLAG_P => 128; # possibly parameter passing
889 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x @{$progs->{globals}};
891 for(@{$progs->{functions}})
893 for(keys %{$_->{globals_used}})
895 if($globalflags[$_] & GLOBALFLAG_U)
897 $globalflags[$_] &= ~GLOBALFLAG_U;
899 elsif($globalflags[$_] & GLOBALFLAG_Q)
901 $globalflags[$_] &= ~GLOBALFLAG_Q;
904 $globalflags[$_] |= GLOBALFLAG_R
905 for keys %{$_->{globals_read}};
906 $globalflags[$_] |= GLOBALFLAG_W
907 for keys %{$_->{globals_written}};
909 if $_->{first_statement} < 0;
910 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
912 my $s = $progs->{statements}[$ip];
913 if($s->{op} eq 'STORE_V')
915 $globalflags[$s->{a}] |= GLOBALFLAG_P
916 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
917 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
918 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
919 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
920 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
922 elsif($s->{op} =~ /^STORE_/)
924 $globalflags[$s->{a}] |= GLOBALFLAG_P
925 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
934 # parameter passing globals are only ever used in STORE_ instructions
935 for my $s(@{$progs->{statements}})
938 if $s->{op} =~ /^STORE_/;
940 my $c = checkop $s->{op};
946 unless defined $type;
949 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
951 $globalflags[$ofs] &= ~GLOBALFLAG_P;
953 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
955 $globalflags[$ofs] &= ~GLOBALFLAG_P;
956 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
957 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
962 my %offsets_saved = ();
963 for(@{$progs->{globaldefs}})
965 my $type = $_->{type};
966 my $name = $progs->{getstring}->($_->{s_name});
968 if $name eq 'IMMEDIATE' or $name =~ /^\./;
969 $_->{debugname} = $name
973 for my $i(0..(typesize($_->{type}{type})-1))
975 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
980 for my $i(0..(typesize($_->{type}{type})-1))
982 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
986 my %offsets_initialized = ();
987 for(0..(@{$progs->{globals}}-1))
989 if($progs->{globals}[$_]{v}{int})
991 $globalflags[$_] |= GLOBALFLAG_I;
995 my @globaltypes = (undef) x @{$progs->{globals}};
998 for(0..(@{$progs->{globals}}-1))
1001 if $_ < @{(DEFAULTGLOBALS)};
1002 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
1004 $globaltypes[$_] = "unused";
1006 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
1009 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1011 $globaltypes[$_] = "read_only";
1013 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1015 $globaltypes[$_] = "const";
1019 $globaltypes[$_] = "read_only";
1022 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1024 $globaltypes[$_] = "write_only";
1028 # now we know it is rw
1029 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1031 $globaltypes[$_] = "global";
1033 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1035 if($globalflags[$_] & GLOBALFLAG_P)
1037 $globaltypes[$_] = "OFS_PARM";
1039 elsif($globalflags[$_] & GLOBALFLAG_Q)
1041 $globaltypes[$_] = "uniquetemp";
1046 $globaltypes[$_] = "temp";
1050 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1052 $globaltypes[$_] = "not_saved";
1056 $globaltypes[$_] = "global";
1060 $progs->{temps} = \%istemp;
1063 my @globaldefs = (undef) x @{$progs->{globals}};
1064 for(@{$progs->{globaldefs}})
1066 $globaldefs[$_->{ofs}] //= $_
1067 if defined $_->{debugname};
1069 for(@{$progs->{globaldefs}})
1071 $globaldefs[$_->{ofs}] //= $_;
1073 for(0..(@{$progs->{globals}}-1))
1075 $globaldefs[$_] //= {
1081 for(0..(@{(DEFAULTGLOBALS)}-1))
1083 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1084 $globaltypes[$_] = 'defglobal';
1086 my %globaldefs_namecount = ();
1089 $_->{globaltype} = $globaltypes[$_->{ofs}];
1090 if(defined $_->{debugname})
1092 # already has debugname
1094 elsif($_->{globaltype} eq 'const')
1096 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1100 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1102 ++$globaldefs_namecount{$_->{debugname}};
1107 if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
1108 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1109 $_->{debugname} .= "\@$_->{ofs}";
1111 $progs->{globaldef_byoffset} = sub
1114 my $def = $globaldefs[$ofs];
1125 print STDERR "Parsing header...\n";
1126 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1128 print STDERR "Parsing strings...\n";
1129 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1132 my ($startpos) = @_;
1133 my $endpos = index $p{strings}, "\0", $startpos;
1134 return substr $p{strings}, $startpos, $endpos - $startpos;
1137 print STDERR "Parsing globals...\n";
1138 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1140 print STDERR "Parsing globaldefs...\n";
1141 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1143 print STDERR "Range checking globaldefs...\n";
1144 for(0 .. (@{$p{globaldefs}}-1))
1146 my $g = $p{globaldefs}[$_];
1147 die "Out of range name in globaldef $_"
1148 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1149 my $name = $p{getstring}->($g->{s_name});
1150 die "Out of range ofs in globaldef $_ (name: \"$name\")"
1151 if $g->{ofs} >= $p{globals};
1154 print STDERR "Parsing fielddefs...\n";
1155 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1157 print STDERR "Range checking fielddefs...\n";
1158 for(0 .. (@{$p{fielddefs}}-1))
1160 my $g = $p{fielddefs}[$_];
1161 die "Out of range name in fielddef $_"
1162 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1163 my $name = $p{getstring}->($g->{s_name});
1164 die "Out of range ofs in globaldef $_ (name: \"$name\")"
1165 if $g->{ofs} >= $p{header}{entityfields};
1168 print STDERR "Parsing statements...\n";
1169 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
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 "Range checking functions...\n";
1175 for(0 .. (@{$p{functions}} - 1))
1177 my $f = $p{functions}[$_];
1178 die "Out of range name in function $_"
1179 if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
1180 my $name = $p{getstring}->($f->{s_name});
1181 die "Out of range file in function $_"
1182 if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
1183 my $file = $p{getstring}->($f->{s_file});
1184 die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\")"
1185 if $f->{first_statement} >= @{$p{statements}};
1186 die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\")"
1187 if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
1188 die "Out of range locals in function $_ (name: \"$name\", file: \"$file\")"
1189 if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} >= @{$p{globals}};
1190 die "Out of range numparms in function $_ (name: \"$name\", file: \"$file\")"
1191 if $f->{numparms} < 0 || $f->{numparms} > 8;
1193 for(0..($f->{numparms}-1))
1195 die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\")"
1196 unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
1197 $totalparms += $f->{parm_size}[$_];
1199 die "Out of range parms in function $_ (name: \"$name\", file: \"$file\")"
1200 if $f->{locals} < 0 || $f->{parm_start} + $totalparms >= @{$p{globals}};
1203 print STDERR "Range checking statements...\n";
1204 for my $ip(0 .. (@{$p{statements}}-1))
1206 my $s = $p{statements}[$ip];
1207 my $c = checkop $s->{op};
1211 my $type = $c->{$_};
1213 unless defined $type;
1215 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1218 die "Out of range global offset in statement $ip - cannot continue"
1219 if $s->{$_} >= @{$p{globals}};
1221 elsif($type eq 'inglobalvec')
1224 die "Out of range global offset in statement $ip - cannot continue"
1225 if $s->{$_} >= @{$p{globals}}-2;
1227 elsif($type eq 'outglobal')
1230 die "Out of range global offset in statement $ip - cannot continue"
1231 if $s->{$_} >= @{$p{globals}};
1233 elsif($type eq 'outglobalvec')
1236 die "Out of range global offset in statement $ip - cannot continue"
1237 if $s->{$_} >= @{$p{globals}}-2;
1239 elsif($type eq 'ipoffset')
1241 die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
1242 if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
1247 print STDERR "Looking for error()...\n";
1248 $p{error_func} = {};
1249 for(@{$p{globaldefs}})
1252 if $p{getstring}($_->{s_name}) ne 'error';
1253 my $v = $p{globals}[$_->{ofs}]{v}{int};
1255 if $v <= 0 || $v >= @{$p{functions}};
1256 my $first = $p{functions}[$v]{first_statement};
1259 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1260 $p{error_func}{$_->{ofs}} = 1;
1263 print STDERR "Scanning functions...\n";
1264 for(@{$p{functions}})
1266 my $file = $p{getstring}->($_->{s_file});
1267 my $name = $p{getstring}->($_->{s_name});
1268 $name = "$file:$name"
1270 $_->{debugname} = $name;
1273 if $_->{first_statement} < 0;
1275 my %statements = ();
1278 my %globals_read = ();
1279 my %globals_written = ();
1280 my %globals_used = ();
1282 if($_->{first_statement} >= 0)
1284 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1287 my ($ip, $state, $s, $c) = @_;
1290 if(my $j = $c->{isjump})
1292 my $t = $ip + $s->{$j};
1293 $come_from{$t}{$ip} = $c->{isconditional};
1294 $go_to{$ip}{$t} = $c->{isconditional};
1297 for my $o(qw(a b c))
1306 $globals_read{$ofs}{$ip}{$o} = 1;
1307 $globals_used{$ofs} = 1;
1312 $globals_written{$ofs}{$ip}{$o} = 1;
1313 $globals_used{$ofs} = 1;
1316 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1320 elsif($type eq 'inglobalvec')
1326 elsif($type eq 'outglobal')
1330 elsif($type eq 'outglobalvec')
1342 $_->{statements} = \%statements;
1343 $_->{come_from} = \%come_from;
1344 $_->{go_to} = \%go_to;
1345 $_->{globals_read} = \%globals_read;
1346 $_->{globals_written} = \%globals_written;
1347 $_->{globals_used} = \%globals_used;
1349 # using this info, we could now identify basic blocks
1352 print STDERR "Detecting constants and temps, and naming...\n";
1353 detect_constants \%p;
1362 # what do we want to do?
1363 my $checkfunc = \&find_uninitialized_locals;
1364 if($ENV{DISASSEMBLE})
1366 $checkfunc = \&disassemble_function;
1368 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1370 $checkfunc->(\%p, $_);
1374 open my $fh, '<', $ARGV[0];