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 my $initializer = sub
366 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
371 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
372 printf OPERAND_FORMAT, "$func->{parm_start}";
373 print INSTRUCTION_SEPARATOR;
375 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
376 printf OPERAND_FORMAT, "$func->{locals}";
377 print INSTRUCTION_SEPARATOR;
379 my %override_locals = ();
380 my $p = $func->{parm_start};
381 for(0..($func->{numparms}-1))
383 $override_locals{$p} //= "argv_$_";
384 for my $comp(0..($func->{parm_size}[$_]-1))
386 $override_locals{$p} //= "argv_$_\[$comp]";
389 printf INSTRUCTION_FORMAT, '', '', '.ARG';
390 printf OPERAND_FORMAT, "argv_$_";
391 print OPERAND_SEPARATOR;
392 printf OPERAND_FORMAT, $func->{parm_size}[$_];
393 print INSTRUCTION_SEPARATOR;
395 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
398 if exists $override_locals{$_};
399 $override_locals{$_} = "local_$_";
401 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
402 printf OPERAND_FORMAT, "local_$_";
404 print INSTRUCTION_SEPARATOR;
410 return $override_locals{$ofs}
411 if exists $override_locals{$ofs};
412 my $def = $progs->{globaldef_byoffset}->($ofs);
413 return $def->{debugname};
418 my ($ip, $type, $operand) = @_;
419 if($type eq 'inglobal')
421 my $name = $getname->($operand);
422 printf OPERAND_FORMAT, "$name";
424 elsif($type eq 'outglobal')
426 my $name = $getname->($operand);
427 printf OPERAND_FORMAT, "&$name";
429 elsif($type eq 'inglobalvec')
431 my $name = $getname->($operand);
432 printf OPERAND_FORMAT, "$name\[\]";
434 elsif($type eq 'outglobalvec')
436 my $name = $getname->($operand);
437 printf OPERAND_FORMAT, "&$name\[\]";
439 elsif($type eq 'inglobalfunc')
441 my $name = $getname->($operand);
442 printf OPERAND_FORMAT, "$name()";
444 elsif($type eq 'ipoffset')
446 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
450 die "unknown type: $type";
454 my $statements = $func->{statements};
455 my $come_from = $func->{come_from};
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 if($highlight and $highlight->{$ip})
490 for(values %{$highlight->{$ip}})
494 print PRE_MARK_STATEMENT;
495 printf INSTRUCTION_FORMAT, '', '<!>', '.WARN';
496 printf OPERAND_FORMAT, "$_ (in $func->{debugname})";
497 print INSTRUCTION_SEPARATOR;
502 print PRE_MARK_STATEMENT
503 if $highlight and $highlight->{$ip};
505 my $showip = $opprop->{isjump};
506 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? '<!>' : '', $op;
512 if not defined $opprop->{$o};
513 print OPERAND_SEPARATOR
515 print PRE_MARK_OPERAND
516 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
517 $operand->($ip, $opprop->{$o}, $ipt->{$o});
518 print POST_MARK_OPERAND
519 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
522 print POST_MARK_STATEMENT
523 if $highlight and $highlight->{$ip};
525 print INSTRUCTION_SEPARATOR;
529 sub find_uninitialized_locals($$)
531 my ($progs, $func) = @_;
535 if $func->{first_statement} < 0; # builtin
537 print STDERR "Checking $func->{debugname}...\n";
539 my $p = $func->{parm_start};
540 for(0..($func->{numparms}-1))
542 $p += $func->{parm_size}[$_];
545 use constant WATCHME_R => 1;
546 use constant WATCHME_W => 2;
547 use constant WATCHME_X => 4;
548 use constant WATCHME_T => 8;
549 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
551 for(keys %{$progs->{temps}})
554 if exists $watchme{$_};
555 if($progs->{temps}{$_})
558 $watchme{$_} = WATCHME_T | WATCHME_X
563 $watchme{$_} = WATCHME_X
567 $watchme{$_} |= WATCHME_R
568 for keys %{$func->{globals_read}};
569 $watchme{$_} |= WATCHME_W
570 for keys %{$func->{globals_written}};
572 my %write_places = ();
573 for my $ofs(keys %{$func->{globals_written}})
576 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
577 for my $ip(keys %{$func->{globals_written}{$ofs}})
579 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
581 push @{$write_places{$ip}{$op}}, $ofs;
589 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
593 if not keys %watchme;
598 flags => $watchme{$_},
599 valid => [0, undef, undef]
603 # mark parameters as initialized
604 for($func->{parm_start} .. ($p-1))
606 $watchme{$_}{valid} = [1, undef, undef]
607 if defined $watchme{$_};
612 run_nfa $progs, $func->{first_statement}, \%watchme,
615 return { map { $_ => { %{$h->{$_}} } } keys %$h };
618 my ($ip, $state) = @_;
620 my $s = $ip_seen{$ip};
623 # if $state is stronger or equal to $s, return 1
627 if($state->{$_}{valid}[0] < $s->{$_})
629 # The current state is LESS valid than the previously run one. We NEED to run this.
630 # The saved state can safely become the intersection [citation needed].
633 $s->{$_} = $state->{$_}{valid}[0]
634 if $state->{$_}{valid}[0] < $s->{$_};
639 # if we get here, $state is stronger or equal. No need to try it.
644 # Never seen this IP yet.
645 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
650 my ($ip, $state, $s, $c) = @_;
653 # QCVM BUG: RETURN always takes vector, there is no float equivalent
654 my $return_hack = $c->{isreturn} // 0;
658 # COMPILER BUG of QCC: params are always copied using STORE_V
659 if($s->{b} >= 4 && $s->{b} < 28) # parameter range
669 unless defined $type;
679 if not exists $state->{$ofs};
680 my $valid = $state->{$ofs}{valid};
683 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
684 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
686 push @{$warned{$ip}{$_}}, "Use of uninitialized value";
689 elsif($valid->[0] < 0)
691 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
692 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
694 push @{$warned{$ip}{$_}}, "Use of temporary across CALL";
700 if(defined $valid->[1])
702 delete $write_places{$valid->[1]}{$valid->[2]};
709 $state->{$ofs}{valid} = [1, $ip, $_]
710 if exists $state->{$ofs};
713 if($type eq 'inglobal' || $type eq 'inglobalfunc')
717 elsif($type eq 'inglobalvec')
723 elsif($type eq 'outglobal')
727 elsif($type eq 'outglobalvec')
736 # builtin calls may clobber stuff
738 my $funcid = $progs->{globals}[$func]{v}{int};
739 my $funcobj = $progs->{functions}[$funcid];
740 if(!$funcobj || $funcobj->{first_statement} >= 0)
745 if($_->{flags} & WATCHME_T)
747 $_->{valid} = [-1, undef, undef];
756 for my $ip(keys %write_places)
758 for my $operand(keys %{$write_places{$ip}})
761 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
764 my %writeplace_seen = ();
765 run_nfa $progs, $ip+1, \%left,
772 my ($ip, $state) = @_;
773 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
777 my ($ip, $state, $s, $c) = @_;
782 unless defined $type;
785 if($type eq 'inglobal' || $type eq 'inglobalfunc')
790 return -1; # exit TOTALLY
793 elsif($type eq 'inglobalvec')
795 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
798 return -1; # exit TOTALLY
801 elsif($type eq 'outglobal')
803 delete $state->{$ofs};
807 elsif($type eq 'outglobalvec')
809 delete $state->{$ofs};
810 delete $state->{$ofs+1};
811 delete $state->{$ofs+2};
821 push @{$warned{$ip}{$operand}}, "Value is never used";
826 disassemble_function($progs, $func, \%warned)
830 use constant DEFAULTGLOBALS => [
864 if($ofs < @{(DEFAULTGLOBALS)})
866 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
868 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
871 sub detect_constants($)
874 use constant GLOBALFLAG_R => 1; # read
875 use constant GLOBALFLAG_W => 2; # written
876 use constant GLOBALFLAG_S => 4; # saved
877 use constant GLOBALFLAG_I => 8; # initialized
878 use constant GLOBALFLAG_N => 16; # named
879 use constant GLOBALFLAG_Q => 32; # unique to function
880 use constant GLOBALFLAG_U => 64; # unused
881 use constant GLOBALFLAG_P => 128; # possibly parameter passing
882 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x @{$progs->{globals}};
884 for(@{$progs->{functions}})
886 for(keys %{$_->{globals_used}})
888 if($globalflags[$_] & GLOBALFLAG_U)
890 $globalflags[$_] &= ~GLOBALFLAG_U;
892 elsif($globalflags[$_] & GLOBALFLAG_Q)
894 $globalflags[$_] &= ~GLOBALFLAG_Q;
897 $globalflags[$_] |= GLOBALFLAG_R
898 for keys %{$_->{globals_read}};
899 $globalflags[$_] |= GLOBALFLAG_W
900 for keys %{$_->{globals_written}};
902 if $_->{first_statement} < 0;
903 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
905 my $s = $progs->{statements}[$ip];
906 if($s->{op} eq 'STORE_V')
908 $globalflags[$s->{a}] |= GLOBALFLAG_P
909 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
910 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
911 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
912 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
913 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
915 elsif($s->{op} =~ /^STORE_/)
917 $globalflags[$s->{a}] |= GLOBALFLAG_P
918 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
927 # parameter passing globals are only ever used in STORE_ instructions
928 for my $s(@{$progs->{statements}})
931 if $s->{op} =~ /^STORE_/;
933 my $c = checkop $s->{op};
939 unless defined $type;
942 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
944 $globalflags[$ofs] &= ~GLOBALFLAG_P;
946 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
948 $globalflags[$ofs] &= ~GLOBALFLAG_P;
949 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
950 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
955 my %offsets_saved = ();
956 for(@{$progs->{globaldefs}})
958 my $type = $_->{type};
959 my $name = $progs->{getstring}->($_->{s_name});
961 if $name eq 'IMMEDIATE' or $name =~ /^\./;
962 $_->{debugname} = $name
966 for my $i(0..(typesize($_->{type}{type})-1))
968 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
973 for my $i(0..(typesize($_->{type}{type})-1))
975 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
979 my %offsets_initialized = ();
980 for(0..(@{$progs->{globals}}-1))
982 if($progs->{globals}[$_]{v}{int})
984 $globalflags[$_] |= GLOBALFLAG_I;
988 my @globaltypes = (undef) x @{$progs->{globals}};
991 for(0..(@{$progs->{globals}}-1))
994 if $_ < @{(DEFAULTGLOBALS)};
995 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
997 $globaltypes[$_] = "unused";
999 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
1002 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1004 $globaltypes[$_] = "read_only";
1006 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1008 $globaltypes[$_] = "const";
1012 $globaltypes[$_] = "read_only";
1015 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1017 $globaltypes[$_] = "write_only";
1021 # now we know it is rw
1022 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1024 $globaltypes[$_] = "global";
1026 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1028 if($globalflags[$_] & GLOBALFLAG_P)
1030 $globaltypes[$_] = "OFS_PARM";
1032 elsif($globalflags[$_] & GLOBALFLAG_Q)
1034 $globaltypes[$_] = "uniquetemp";
1039 $globaltypes[$_] = "temp";
1043 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1045 $globaltypes[$_] = "not_saved";
1049 $globaltypes[$_] = "global";
1053 $progs->{temps} = \%istemp;
1056 my @globaldefs = (undef) x @{$progs->{globaldefs}};
1057 for(@{$progs->{globaldefs}})
1059 $globaldefs[$_->{ofs}] //= $_
1060 if defined $_->{debugname};
1062 for(@{$progs->{globaldefs}})
1064 $globaldefs[$_->{ofs}] //= $_;
1066 for(0..(@{$progs->{globals}}-1))
1068 $globaldefs[$_] //= {
1074 for(0..(@{(DEFAULTGLOBALS)}-1))
1076 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1077 $globaltypes[$_] = 'defglobal';
1079 my %globaldefs_namecount = ();
1082 $_->{globaltype} = $globaltypes[$_->{ofs}];
1083 if(defined $_->{debugname})
1085 # already has debugname
1087 elsif($_->{globaltype} eq 'const')
1089 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1093 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1095 ++$globaldefs_namecount{$_->{debugname}};
1100 if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
1101 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1102 $_->{debugname} .= "\@$_->{ofs}";
1104 $progs->{globaldef_byoffset} = sub
1107 my $def = $globaldefs[$ofs];
1118 print STDERR "Parsing header...\n";
1119 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1121 print STDERR "Parsing strings...\n";
1122 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1125 my ($startpos) = @_;
1126 my $endpos = index $p{strings}, "\0", $startpos;
1127 return substr $p{strings}, $startpos, $endpos - $startpos;
1130 print STDERR "Parsing globals...\n";
1131 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1133 print STDERR "Parsing globaldefs...\n";
1134 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1136 print STDERR "Range checking globaldefs...\n";
1137 for(0 .. (@{$p{globaldefs}}-1))
1139 my $g = $p{globaldefs}[$_];
1140 die "Out of range name in globaldef $_"
1141 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1142 my $name = $p{getstring}->($g->{s_name});
1143 die "Out of range ofs in globaldef $_ (name: \"$name\")"
1144 if $g->{ofs} >= $p{globals};
1147 print STDERR "Parsing fielddefs...\n";
1148 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1150 print STDERR "Range checking fielddefs...\n";
1151 for(0 .. (@{$p{fielddefs}}-1))
1153 my $g = $p{fielddefs}[$_];
1154 die "Out of range name in fielddef $_"
1155 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1156 my $name = $p{getstring}->($g->{s_name});
1157 die "Out of range ofs in globaldef $_ (name: \"$name\")"
1158 if $g->{ofs} >= $p{header}{entityfields};
1161 print STDERR "Parsing statements...\n";
1162 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1164 print STDERR "Parsing functions...\n";
1165 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1167 print STDERR "Range checking functions...\n";
1168 for(0 .. (@{$p{functions}} - 1))
1170 my $f = $p{functions}[$_];
1171 die "Out of range name in function $_"
1172 if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
1173 my $name = $p{getstring}->($f->{s_name});
1174 die "Out of range file in function $_"
1175 if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
1176 my $file = $p{getstring}->($f->{s_file});
1177 die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\")"
1178 if $f->{first_statement} >= @{$p{statements}};
1179 die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\")"
1180 if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
1181 die "Out of range locals in function $_ (name: \"$name\", file: \"$file\")"
1182 if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} >= @{$p{globals}};
1183 die "Out of range numparms in function $_ (name: \"$name\", file: \"$file\")"
1184 if $f->{numparms} < 0 || $f->{numparms} > 8;
1186 for(0..($f->{numparms}-1))
1188 die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\")"
1189 unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
1190 $totalparms += $f->{parm_size}[$_];
1192 die "Out of range parms in function $_ (name: \"$name\", file: \"$file\")"
1193 if $f->{locals} < 0 || $f->{parm_start} + $totalparms >= @{$p{globals}};
1196 print STDERR "Range checking statements...\n";
1197 for my $ip(0 .. (@{$p{statements}}-1))
1199 my $s = $p{statements}[$ip];
1200 my $c = checkop $s->{op};
1204 my $type = $c->{$_};
1206 unless defined $type;
1208 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1211 die "Out of range global offset in statement $ip - cannot continue"
1212 if $s->{$_} >= @{$p{globals}};
1214 elsif($type eq 'inglobalvec')
1217 die "Out of range global offset in statement $ip - cannot continue"
1218 if $s->{$_} >= @{$p{globals}}-2;
1220 elsif($type eq 'outglobal')
1223 die "Out of range global offset in statement $ip - cannot continue"
1224 if $s->{$_} >= @{$p{globals}};
1226 elsif($type eq 'outglobalvec')
1229 die "Out of range global offset in statement $ip - cannot continue"
1230 if $s->{$_} >= @{$p{globals}}-2;
1232 elsif($type eq 'ipoffset')
1234 die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
1235 if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
1240 print STDERR "Looking for error()...\n";
1241 $p{error_func} = {};
1242 for(@{$p{globaldefs}})
1245 if $p{getstring}($_->{s_name}) ne 'error';
1246 my $v = $p{globals}[$_->{ofs}]{v}{int};
1248 if $v <= 0 || $v >= @{$p{functions}};
1249 my $first = $p{functions}[$v]{first_statement};
1252 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1253 $p{error_func}{$_->{ofs}} = 1;
1256 print STDERR "Scanning functions...\n";
1257 for(@{$p{functions}})
1259 my $file = $p{getstring}->($_->{s_file});
1260 my $name = $p{getstring}->($_->{s_name});
1261 $name = "$file:$name"
1263 $_->{debugname} = $name;
1266 if $_->{first_statement} < 0;
1268 my %statements = ();
1271 my %globals_read = ();
1272 my %globals_written = ();
1273 my %globals_used = ();
1275 if($_->{first_statement} >= 0)
1277 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1280 my ($ip, $state, $s, $c) = @_;
1283 if(my $j = $c->{isjump})
1285 my $t = $ip + $s->{$j};
1286 $come_from{$t}{$ip} = $c->{isconditional};
1287 $go_to{$ip}{$t} = $c->{isconditional};
1290 for my $o(qw(a b c))
1299 $globals_read{$ofs}{$ip}{$o} = 1;
1300 $globals_used{$ofs} = 1;
1305 $globals_written{$ofs}{$ip}{$o} = 1;
1306 $globals_used{$ofs} = 1;
1309 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1313 elsif($type eq 'inglobalvec')
1319 elsif($type eq 'outglobal')
1323 elsif($type eq 'outglobalvec')
1335 $_->{statements} = \%statements;
1336 $_->{come_from} = \%come_from;
1337 $_->{go_to} = \%go_to;
1338 $_->{globals_read} = \%globals_read;
1339 $_->{globals_written} = \%globals_written;
1340 $_->{globals_used} = \%globals_used;
1342 # using this info, we could now identify basic blocks
1345 print STDERR "Detecting constants and temps, and naming...\n";
1346 detect_constants \%p;
1348 # what do we want to do?
1349 my $checkfunc = \&find_uninitialized_locals;
1350 if($ENV{DISASSEMBLE})
1352 $checkfunc = \&disassemble_function;
1354 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1356 $checkfunc->(\%p, $_);
1360 open my $fh, '<', $ARGV[0];