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 };
126 return { a => 'inglobal', b => 'inglobalfunc' };
128 return { a => 'inglobal', b => 'inglobal', c => 'outglobal' };
131 use constant TYPES => {
132 int => ['V', 4, signed 32],
133 ushort => ['v', 2, id],
134 short => ['v', 2, signed 16],
135 opcode => ['v', 2, sub { OPCODE_E->[$_[0]] or do { warn "Invalid opcode: $_[0]"; "INVALID#$_[0]"; }; }],
136 float => ['f', 4, id],
137 uchar8 => ['a8', 8, sub { [unpack 'C8', $_[0]] }],
138 global => ['i', 4, sub { { int => $_[0], float => unpack "f", pack "L", $_[0] }; }],
139 deftype => ['v', 2, sub { { type => ETYPE_E->[$_[0] & ~DEF_SAVEGLOBAL], save => !!($_[0] & DEF_SAVEGLOBAL) }; }],
142 use constant DPROGRAMS_T => [
145 [int => 'ofs_statements'],
146 [int => 'numstatements'],
147 [int => 'ofs_globaldefs'],
148 [int => 'numglobaldefs'],
149 [int => 'ofs_fielddefs'],
150 [int => 'numfielddefs'],
151 [int => 'ofs_functions'],
152 [int => 'numfunctions'],
153 [int => 'ofs_strings'],
154 [int => 'numstrings'],
155 [int => 'ofs_globals'],
156 [int => 'numglobals'],
157 [int => 'entityfields']
160 use constant DSTATEMENT_T => [
167 use constant DDEF_T => [
173 use constant DGLOBAL_T => [
177 use constant DFUNCTION_T => [
178 [int => 'first_statement'],
179 [int => 'parm_start'],
185 [uchar8 => 'parm_size'],
190 my ($fh, $start, $len) = @_;
193 $len == read $fh, my $buf, $len
194 or die "short read from $start length $len (malformed progs header)";
198 sub parse_section($$$$$)
200 my ($fh, $struct, $start, $len, $cnt) = @_;
203 $itemlen += TYPES->{$_->[0]}->[1]
205 my $packspec = join '', map { TYPES->{$_->[0]}->[0]; } @$struct;
206 my @packnames = map { $_->[1]; } @$struct;
208 $len = $cnt * $itemlen
209 if not defined $len and defined $cnt;
210 $cnt = int($len / $itemlen)
211 if not defined $cnt and defined $len;
212 die "Invalid length specification"
213 unless defined $len and defined $cnt and $len == $cnt * $itemlen;
214 die "Invalid length specification in scalar context"
215 unless wantarray or $cnt == 1;
221 $itemlen == read $fh, my $buf, $itemlen
222 or die "short read from $start length $cnt * $itemlen $(malformed progs header)";
224 @h{@packnames} = unpack $packspec, $buf;
225 $h{$_->[1]} = TYPES->{$_->[0]}->[2]->($h{$_->[1]})
235 sub nfa_default_state_checker()
240 my ($ip, $state) = @_;
241 return $seen{"$ip $state"}++;
247 my ($progs, $ip, $state, $copy_handler, $state_checker, $instruction_handler) = @_;
249 my $statements = $progs->{statements};
254 no warnings 'recursion';
256 my ($ip, $state) = @_;
262 if $state_checker->($ip, $state);
264 my $s = $statements->[$ip];
265 my $c = checkop $s->{op};
267 if(($ret = $instruction_handler->($ip, $state, $s, $c)))
280 my $funcid = $progs->{globals}[$func]{v}{int};
282 if $progs->{error_func}{$funcid};
287 if($c->{isconditional})
291 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
299 $nfa->($ip+1, $copy_handler->($state));
300 $ip += $s->{$c->{isjump}};
305 $ip += $s->{$c->{isjump}};
317 $nfa->($ip, $copy_handler->($state));
322 my ($progs, $g) = @_;
327 elsif($g->{int} > 0 && $g->{int} < 8388608)
329 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
331 return str($progs->{getstring}->($g->{int}));
335 return $g->{int} . "i";
344 use constant PRE_MARK_STATEMENT => "";
345 use constant POST_MARK_STATEMENT => "";
346 use constant PRE_MARK_OPERAND => "*** ";
347 use constant POST_MARK_OPERAND => " ***";
349 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
350 use constant OPERAND_FORMAT => "%s";
351 use constant OPERAND_SEPARATOR => ", ";
352 use constant INSTRUCTION_SEPARATOR => "\n";
357 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
361 sub disassemble_function($$;$)
363 my ($progs, $func, $highlight) = @_;
365 print "$func->{debugname}:\n";
367 if($func->{first_statement} < 0) # builtin
369 printf INSTRUCTION_FORMAT, '', '', '.BUILTIN';
370 printf OPERAND_FORMAT, -$func->{first_statement};
371 print INSTRUCTION_SEPARATOR;
375 my $initializer = sub
378 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
383 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
384 printf OPERAND_FORMAT, "$func->{parm_start}";
385 print INSTRUCTION_SEPARATOR;
387 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
388 printf OPERAND_FORMAT, "$func->{locals}";
389 print INSTRUCTION_SEPARATOR;
391 my %override_locals = ();
392 my $p = $func->{parm_start};
393 for(0..($func->{numparms}-1))
395 $override_locals{$p} //= "argv_$_";
396 for my $comp(0..($func->{parm_size}[$_]-1))
398 $override_locals{$p} //= "argv_$_\[$comp]";
401 printf INSTRUCTION_FORMAT, '', '', '.ARG';
402 printf OPERAND_FORMAT, "argv_$_";
403 print OPERAND_SEPARATOR;
404 printf OPERAND_FORMAT, $func->{parm_size}[$_];
405 print INSTRUCTION_SEPARATOR;
407 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
410 if exists $override_locals{$_};
411 $override_locals{$_} = "local_$_";
413 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
414 printf OPERAND_FORMAT, "local_$_";
416 print INSTRUCTION_SEPARATOR;
422 return $override_locals{$ofs}
423 if exists $override_locals{$ofs};
424 my $def = $progs->{globaldef_byoffset}->($ofs);
425 return $def->{debugname};
430 my ($ip, $type, $operand) = @_;
431 if($type eq 'inglobal')
433 my $name = $getname->($operand);
434 printf OPERAND_FORMAT, "$name";
436 elsif($type eq 'outglobal')
438 my $name = $getname->($operand);
439 printf OPERAND_FORMAT, "&$name";
441 elsif($type eq 'inglobalvec')
443 my $name = $getname->($operand);
444 printf OPERAND_FORMAT, "$name\[\]";
446 elsif($type eq 'outglobalvec')
448 my $name = $getname->($operand);
449 printf OPERAND_FORMAT, "&$name\[\]";
451 elsif($type eq 'inglobalfunc')
453 my $name = $getname->($operand);
454 printf OPERAND_FORMAT, "$name()";
456 elsif($type eq 'ipoffset')
458 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
462 die "unknown type: $type";
466 my $statements = $func->{statements};
467 my $come_from = $func->{come_from};
470 for my $ip(sort { $a <=> $b } keys %$statements)
472 if($ip == $func->{first_statement})
474 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
475 print INSTRUCTION_SEPARATOR;
477 if(defined $ipprev && $ip != $ipprev + 1)
479 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
480 printf OPERAND_FORMAT, $ip - $ipprev - 1;
481 print INSTRUCTION_SEPARATOR;
483 if(my $cf = $come_from->{$ip})
485 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
487 for(sort { $a <=> $b } keys %$cf)
489 print OPERAND_SEPARATOR
491 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
493 print INSTRUCTION_SEPARATOR;
496 my $op = $progs->{statements}[$ip]{op};
497 my $ipt = $progs->{statements}[$ip];
498 my $opprop = checkop $op;
500 if($highlight and $highlight->{$ip})
502 for(values %{$highlight->{$ip}})
506 print PRE_MARK_STATEMENT;
507 printf INSTRUCTION_FORMAT, '', '<!>', '.WARN';
508 printf OPERAND_FORMAT, "$_ (in $func->{debugname})";
509 print INSTRUCTION_SEPARATOR;
514 print PRE_MARK_STATEMENT
515 if $highlight and $highlight->{$ip};
517 my $showip = $opprop->{isjump};
518 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? '<!>' : '', $op;
524 if not defined $opprop->{$o};
525 print OPERAND_SEPARATOR
527 print PRE_MARK_OPERAND
528 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
529 $operand->($ip, $opprop->{$o}, $ipt->{$o});
530 print POST_MARK_OPERAND
531 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
534 print POST_MARK_STATEMENT
535 if $highlight and $highlight->{$ip};
537 print INSTRUCTION_SEPARATOR;
541 sub find_uninitialized_locals($$)
543 my ($progs, $func) = @_;
546 if $func->{first_statement} < 0; # builtin
548 print STDERR "Checking $func->{debugname}...\n";
550 my $p = $func->{parm_start};
551 for(0..($func->{numparms}-1))
553 $p += $func->{parm_size}[$_];
556 use constant WATCHME_R => 1;
557 use constant WATCHME_W => 2;
558 use constant WATCHME_X => 4;
559 use constant WATCHME_T => 8;
560 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
562 for(keys %{$progs->{temps}})
565 if exists $watchme{$_};
566 if($progs->{temps}{$_})
569 $watchme{$_} = WATCHME_T | WATCHME_X
574 $watchme{$_} = WATCHME_X
578 $watchme{$_} |= WATCHME_R
579 for keys %{$func->{globals_read}};
580 $watchme{$_} |= WATCHME_W
581 for keys %{$func->{globals_written}};
583 my %write_places = ();
584 for my $ofs(keys %{$func->{globals_written}})
587 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
588 for my $ip(keys %{$func->{globals_written}{$ofs}})
590 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
592 push @{$write_places{$ip}{$op}}, $ofs;
600 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
604 if not keys %watchme;
609 flags => $watchme{$_},
610 valid => [0, undef, undef]
614 # mark parameters as initialized
615 for($func->{parm_start} .. ($p-1))
617 $watchme{$_}{valid} = [1, undef, undef]
618 if defined $watchme{$_};
623 run_nfa $progs, $func->{first_statement}, \%watchme,
626 return { map { $_ => { %{$h->{$_}} } } keys %$h };
629 my ($ip, $state) = @_;
631 my $s = $ip_seen{$ip};
634 # if $state is stronger or equal to $s, return 1
638 if($state->{$_}{valid}[0] < $s->{$_})
640 # The current state is LESS valid than the previously run one. We NEED to run this.
641 # The saved state can safely become the intersection [citation needed].
644 $s->{$_} = $state->{$_}{valid}[0]
645 if $state->{$_}{valid}[0] < $s->{$_};
650 # if we get here, $state is stronger or equal. No need to try it.
655 # Never seen this IP yet.
656 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
661 my ($ip, $state, $s, $c) = @_;
664 # QCVM BUG: RETURN always takes vector, there is no float equivalent
665 my $return_hack = $c->{isreturn} // 0;
669 # COMPILER BUG of QCC: params are always copied using STORE_V
670 if($s->{b} >= 4 && $s->{b} < 28) # parameter range
680 unless defined $type;
690 if not exists $state->{$ofs};
691 my $valid = $state->{$ofs}{valid};
694 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
695 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
697 push @{$warned{$ip}{$_}}, "Use of uninitialized value";
700 elsif($valid->[0] < 0)
702 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
703 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
705 push @{$warned{$ip}{$_}}, "Use of temporary across CALL";
711 if(defined $valid->[1])
713 delete $write_places{$valid->[1]}{$valid->[2]};
720 $state->{$ofs}{valid} = [1, $ip, $_]
721 if exists $state->{$ofs};
724 if($type eq 'inglobal' || $type eq 'inglobalfunc')
728 elsif($type eq 'inglobalvec')
734 elsif($type eq 'outglobal')
738 elsif($type eq 'outglobalvec')
747 # builtin calls may clobber stuff
749 my $funcid = $progs->{globals}[$func]{v}{int};
750 my $funcobj = $progs->{functions}[$funcid];
751 if(!$funcobj || $funcobj->{first_statement} >= 0)
756 if($_->{flags} & WATCHME_T)
758 $_->{valid} = [-1, undef, undef];
767 for my $ip(keys %write_places)
769 for my $operand(keys %{$write_places{$ip}})
772 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
775 my %writeplace_seen = ();
776 run_nfa $progs, $ip+1, \%left,
783 my ($ip, $state) = @_;
784 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
788 my ($ip, $state, $s, $c) = @_;
793 unless defined $type;
796 if($type eq 'inglobal' || $type eq 'inglobalfunc')
801 return -1; # exit TOTALLY
804 elsif($type eq 'inglobalvec')
806 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
809 return -1; # exit TOTALLY
812 elsif($type eq 'outglobal')
814 delete $state->{$ofs};
818 elsif($type eq 'outglobalvec')
820 delete $state->{$ofs};
821 delete $state->{$ofs+1};
822 delete $state->{$ofs+2};
832 push @{$warned{$ip}{$operand}}, "Value is never used";
837 disassemble_function($progs, $func, \%warned)
841 use constant DEFAULTGLOBALS => [
875 if($ofs < @{(DEFAULTGLOBALS)})
877 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
879 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
882 sub detect_constants($)
885 use constant GLOBALFLAG_R => 1; # read
886 use constant GLOBALFLAG_W => 2; # written
887 use constant GLOBALFLAG_S => 4; # saved
888 use constant GLOBALFLAG_I => 8; # initialized
889 use constant GLOBALFLAG_N => 16; # named
890 use constant GLOBALFLAG_Q => 32; # unique to function
891 use constant GLOBALFLAG_U => 64; # unused
892 use constant GLOBALFLAG_P => 128; # possibly parameter passing
893 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x @{$progs->{globals}};
895 for(@{$progs->{functions}})
897 for(keys %{$_->{globals_used}})
899 if($globalflags[$_] & GLOBALFLAG_U)
901 $globalflags[$_] &= ~GLOBALFLAG_U;
903 elsif($globalflags[$_] & GLOBALFLAG_Q)
905 $globalflags[$_] &= ~GLOBALFLAG_Q;
908 $globalflags[$_] |= GLOBALFLAG_R
909 for keys %{$_->{globals_read}};
910 $globalflags[$_] |= GLOBALFLAG_W
911 for keys %{$_->{globals_written}};
913 if $_->{first_statement} < 0;
914 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
916 my $s = $progs->{statements}[$ip];
917 if($s->{op} eq 'STORE_V')
919 $globalflags[$s->{a}] |= GLOBALFLAG_P
920 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
921 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
922 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
923 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
924 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
926 elsif($s->{op} =~ /^STORE_/)
928 $globalflags[$s->{a}] |= GLOBALFLAG_P
929 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
938 # parameter passing globals are only ever used in STORE_ instructions
939 for my $s(@{$progs->{statements}})
942 if $s->{op} =~ /^STORE_/;
944 my $c = checkop $s->{op};
950 unless defined $type;
953 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
955 $globalflags[$ofs] &= ~GLOBALFLAG_P;
957 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
959 $globalflags[$ofs] &= ~GLOBALFLAG_P;
960 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
961 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
966 my %offsets_saved = ();
967 for(@{$progs->{globaldefs}})
969 my $type = $_->{type};
970 my $name = $progs->{getstring}->($_->{s_name});
972 if $name eq 'IMMEDIATE' or $name =~ /^\./;
973 $_->{debugname} = $name
977 for my $i(0..(typesize($_->{type}{type})-1))
979 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
984 for my $i(0..(typesize($_->{type}{type})-1))
986 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
990 my %offsets_initialized = ();
991 for(0..(@{$progs->{globals}}-1))
993 if($progs->{globals}[$_]{v}{int})
995 $globalflags[$_] |= GLOBALFLAG_I;
999 my @globaltypes = (undef) x @{$progs->{globals}};
1002 for(0..(@{$progs->{globals}}-1))
1005 if $_ < @{(DEFAULTGLOBALS)};
1006 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
1008 $globaltypes[$_] = "unused";
1010 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
1013 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1015 $globaltypes[$_] = "read_only";
1017 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1019 $globaltypes[$_] = "const";
1023 $globaltypes[$_] = "read_only";
1026 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1028 $globaltypes[$_] = "write_only";
1032 # now we know it is rw
1033 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1035 $globaltypes[$_] = "global";
1037 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1039 if($globalflags[$_] & GLOBALFLAG_P)
1041 $globaltypes[$_] = "OFS_PARM";
1043 elsif($globalflags[$_] & GLOBALFLAG_Q)
1045 $globaltypes[$_] = "uniquetemp";
1050 $globaltypes[$_] = "temp";
1054 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1056 $globaltypes[$_] = "not_saved";
1060 $globaltypes[$_] = "global";
1064 $progs->{temps} = \%istemp;
1067 my @globaldefs = (undef) x @{$progs->{globals}};
1068 for(@{$progs->{globaldefs}})
1070 $globaldefs[$_->{ofs}] //= $_
1071 if defined $_->{debugname};
1073 for(@{$progs->{globaldefs}})
1075 $globaldefs[$_->{ofs}] //= $_;
1077 for(0..(@{$progs->{globals}}-1))
1079 $globaldefs[$_] //= {
1085 for(0..(@{(DEFAULTGLOBALS)}-1))
1087 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1088 $globaltypes[$_] = 'defglobal';
1090 my %globaldefs_namecount = ();
1093 $_->{globaltype} = $globaltypes[$_->{ofs}];
1094 if(defined $_->{debugname})
1096 # already has debugname
1098 elsif($_->{globaltype} eq 'const')
1100 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1104 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1106 ++$globaldefs_namecount{$_->{debugname}};
1111 if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
1112 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1113 $_->{debugname} .= "\@$_->{ofs}";
1115 $progs->{globaldef_byoffset} = sub
1118 my $def = $globaldefs[$ofs];
1129 print STDERR "Parsing header...\n";
1130 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1132 print STDERR "Parsing strings...\n";
1133 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1136 my ($startpos) = @_;
1137 my $endpos = index $p{strings}, "\0", $startpos;
1138 return substr $p{strings}, $startpos, $endpos - $startpos;
1141 print STDERR "Parsing globals...\n";
1142 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1144 print STDERR "Parsing globaldefs...\n";
1145 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1147 print STDERR "Range checking globaldefs...\n";
1148 for(0 .. (@{$p{globaldefs}}-1))
1150 my $g = $p{globaldefs}[$_];
1151 die "Out of range name in globaldef $_"
1152 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1153 my $name = $p{getstring}->($g->{s_name});
1154 die "Out of range ofs in globaldef $_ (name: \"$name\")"
1155 if $g->{ofs} >= $p{globals};
1158 print STDERR "Parsing fielddefs...\n";
1159 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1161 print STDERR "Range checking fielddefs...\n";
1162 for(0 .. (@{$p{fielddefs}}-1))
1164 my $g = $p{fielddefs}[$_];
1165 die "Out of range name in fielddef $_"
1166 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1167 my $name = $p{getstring}->($g->{s_name});
1168 die "Out of range ofs in globaldef $_ (name: \"$name\")"
1169 if $g->{ofs} >= $p{header}{entityfields};
1172 print STDERR "Parsing statements...\n";
1173 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1175 print STDERR "Parsing functions...\n";
1176 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1178 print STDERR "Range checking functions...\n";
1179 for(0 .. (@{$p{functions}} - 1))
1181 my $f = $p{functions}[$_];
1182 die "Out of range name in function $_"
1183 if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
1184 my $name = $p{getstring}->($f->{s_name});
1185 die "Out of range file in function $_"
1186 if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
1187 my $file = $p{getstring}->($f->{s_file});
1188 die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\")"
1189 if $f->{first_statement} >= @{$p{statements}};
1190 die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\")"
1191 if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
1192 die "Out of range locals in function $_ (name: \"$name\", file: \"$file\")"
1193 if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} >= @{$p{globals}};
1194 die "Out of range numparms in function $_ (name: \"$name\", file: \"$file\")"
1195 if $f->{numparms} < 0 || $f->{numparms} > 8;
1197 for(0..($f->{numparms}-1))
1199 die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\")"
1200 unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
1201 $totalparms += $f->{parm_size}[$_];
1203 die "Out of range parms in function $_ (name: \"$name\", file: \"$file\")"
1204 if $f->{locals} < 0 || $f->{parm_start} + $totalparms >= @{$p{globals}};
1207 print STDERR "Range checking statements...\n";
1208 for my $ip(0 .. (@{$p{statements}}-1))
1210 my $s = $p{statements}[$ip];
1211 my $c = checkop $s->{op};
1215 my $type = $c->{$_};
1217 unless defined $type;
1219 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1222 die "Out of range global offset in statement $ip - cannot continue"
1223 if $s->{$_} >= @{$p{globals}};
1225 elsif($type eq 'inglobalvec')
1228 die "Out of range global offset in statement $ip - cannot continue"
1229 if $s->{$_} >= @{$p{globals}}-2;
1231 elsif($type eq 'outglobal')
1234 die "Out of range global offset in statement $ip - cannot continue"
1235 if $s->{$_} >= @{$p{globals}};
1237 elsif($type eq 'outglobalvec')
1240 die "Out of range global offset in statement $ip - cannot continue"
1241 if $s->{$_} >= @{$p{globals}}-2;
1243 elsif($type eq 'ipoffset')
1245 die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
1246 if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
1251 print STDERR "Looking for error()...\n";
1252 $p{error_func} = {};
1253 for(@{$p{globaldefs}})
1256 if $p{getstring}($_->{s_name}) ne 'error';
1257 my $v = $p{globals}[$_->{ofs}]{v}{int};
1259 if $v <= 0 || $v >= @{$p{functions}};
1260 my $first = $p{functions}[$v]{first_statement};
1263 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1264 $p{error_func}{$_->{ofs}} = 1;
1267 print STDERR "Scanning functions...\n";
1268 for(@{$p{functions}})
1270 my $file = $p{getstring}->($_->{s_file});
1271 my $name = $p{getstring}->($_->{s_name});
1272 $name = "$file:$name"
1274 $_->{debugname} = $name;
1277 if $_->{first_statement} < 0;
1279 my %statements = ();
1282 my %globals_read = ();
1283 my %globals_written = ();
1284 my %globals_used = ();
1286 if($_->{first_statement} >= 0)
1288 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1291 my ($ip, $state, $s, $c) = @_;
1294 if(my $j = $c->{isjump})
1296 my $t = $ip + $s->{$j};
1297 $come_from{$t}{$ip} = $c->{isconditional};
1298 $go_to{$ip}{$t} = $c->{isconditional};
1301 for my $o(qw(a b c))
1310 $globals_read{$ofs}{$ip}{$o} = 1;
1311 $globals_used{$ofs} = 1;
1316 $globals_written{$ofs}{$ip}{$o} = 1;
1317 $globals_used{$ofs} = 1;
1320 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1324 elsif($type eq 'inglobalvec')
1330 elsif($type eq 'outglobal')
1334 elsif($type eq 'outglobalvec')
1346 $_->{statements} = \%statements;
1347 $_->{come_from} = \%come_from;
1348 $_->{go_to} = \%go_to;
1349 $_->{globals_read} = \%globals_read;
1350 $_->{globals_written} = \%globals_written;
1351 $_->{globals_used} = \%globals_used;
1353 # using this info, we could now identify basic blocks
1356 print STDERR "Detecting constants and temps, and naming...\n";
1357 detect_constants \%p;
1366 # what do we want to do?
1367 my $checkfunc = \&find_uninitialized_locals;
1368 if($ENV{DISASSEMBLE})
1370 $checkfunc = \&disassemble_function;
1372 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1374 $checkfunc->(\%p, $_);
1378 open my $fh, '<', $ARGV[0];