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 ++$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 ++$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')
744 elsif($type eq 'ipoffset')
746 ++$warned{$ip}{$_}{"Endless loop"}
748 ++$warned{$ip}{$_}{"No-operation jump"}
754 # builtin calls may clobber stuff
756 my $funcid = $progs->{globals}[$func]{v}{int};
757 my $funcobj = $progs->{functions}[$funcid];
758 if(!$funcobj || $funcobj->{first_statement} >= 0)
763 if($_->{flags} & WATCHME_T)
765 $_->{valid} = [-1, undef, undef];
774 for my $ip(keys %write_places)
776 for my $operand(keys %{$write_places{$ip}})
779 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
782 my %writeplace_seen = ();
783 run_nfa $progs, $ip+1, \%left,
790 my ($ip, $state) = @_;
791 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
795 my ($ip, $state, $s, $c) = @_;
800 unless defined $type;
803 if($type eq 'inglobal' || $type eq 'inglobalfunc')
808 return -1; # exit TOTALLY
811 elsif($type eq 'inglobalvec')
813 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
816 return -1; # exit TOTALLY
819 elsif($type eq 'outglobal')
821 delete $state->{$ofs};
825 elsif($type eq 'outglobalvec')
827 delete $state->{$ofs};
828 delete $state->{$ofs+1};
829 delete $state->{$ofs+2};
839 ++$warned{$ip}{$operand}{"Value is never used"};
844 disassemble_function($progs, $func, \%warned)
848 use constant DEFAULTGLOBALS => [
882 if($ofs < @{(DEFAULTGLOBALS)})
884 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
886 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
889 sub detect_constants($)
892 use constant GLOBALFLAG_R => 1; # read
893 use constant GLOBALFLAG_W => 2; # written
894 use constant GLOBALFLAG_S => 4; # saved
895 use constant GLOBALFLAG_I => 8; # initialized
896 use constant GLOBALFLAG_N => 16; # named
897 use constant GLOBALFLAG_Q => 32; # unique to function
898 use constant GLOBALFLAG_U => 64; # unused
899 use constant GLOBALFLAG_P => 128; # possibly parameter passing
900 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x @{$progs->{globals}};
902 for(@{$progs->{functions}})
904 for(keys %{$_->{globals_used}})
906 if($globalflags[$_] & GLOBALFLAG_U)
908 $globalflags[$_] &= ~GLOBALFLAG_U;
910 elsif($globalflags[$_] & GLOBALFLAG_Q)
912 $globalflags[$_] &= ~GLOBALFLAG_Q;
915 $globalflags[$_] |= GLOBALFLAG_R
916 for keys %{$_->{globals_read}};
917 $globalflags[$_] |= GLOBALFLAG_W
918 for keys %{$_->{globals_written}};
920 if $_->{first_statement} < 0;
921 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
923 my $s = $progs->{statements}[$ip];
924 if($s->{op} eq 'STORE_V')
926 $globalflags[$s->{a}] |= GLOBALFLAG_P
927 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
928 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
929 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
930 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
931 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
933 elsif($s->{op} =~ /^STORE_/)
935 $globalflags[$s->{a}] |= GLOBALFLAG_P
936 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
945 # parameter passing globals are only ever used in STORE_ instructions
946 for my $s(@{$progs->{statements}})
949 if $s->{op} =~ /^STORE_/;
951 my $c = checkop $s->{op};
957 unless defined $type;
960 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
962 $globalflags[$ofs] &= ~GLOBALFLAG_P;
964 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
966 $globalflags[$ofs] &= ~GLOBALFLAG_P;
967 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
968 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
973 my %offsets_saved = ();
974 for(@{$progs->{globaldefs}})
976 my $type = $_->{type};
977 my $name = $progs->{getstring}->($_->{s_name});
979 if $name eq 'IMMEDIATE' or $name =~ /^\./;
980 $_->{debugname} = $name
984 for my $i(0..(typesize($_->{type}{type})-1))
986 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
991 for my $i(0..(typesize($_->{type}{type})-1))
993 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
997 my %offsets_initialized = ();
998 for(0..(@{$progs->{globals}}-1))
1000 if($progs->{globals}[$_]{v}{int})
1002 $globalflags[$_] |= GLOBALFLAG_I;
1006 my @globaltypes = (undef) x @{$progs->{globals}};
1009 for(0..(@{$progs->{globals}}-1))
1012 if $_ < @{(DEFAULTGLOBALS)};
1013 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
1015 $globaltypes[$_] = "unused";
1017 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
1020 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1022 $globaltypes[$_] = "read_only";
1024 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1026 $globaltypes[$_] = "const";
1030 $globaltypes[$_] = "read_only";
1033 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1035 $globaltypes[$_] = "write_only";
1039 # now we know it is rw
1040 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1042 $globaltypes[$_] = "global";
1044 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1046 if($globalflags[$_] & GLOBALFLAG_P)
1048 $globaltypes[$_] = "OFS_PARM";
1050 elsif($globalflags[$_] & GLOBALFLAG_Q)
1052 $globaltypes[$_] = "uniquetemp";
1057 $globaltypes[$_] = "temp";
1061 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1063 $globaltypes[$_] = "not_saved";
1067 $globaltypes[$_] = "global";
1071 $progs->{temps} = \%istemp;
1074 my @globaldefs = (undef) x @{$progs->{globals}};
1075 for(@{$progs->{globaldefs}})
1077 $globaldefs[$_->{ofs}] //= $_
1078 if defined $_->{debugname};
1080 for(@{$progs->{globaldefs}})
1082 $globaldefs[$_->{ofs}] //= $_;
1084 for(0..(@{$progs->{globals}}-1))
1086 $globaldefs[$_] //= {
1092 for(0..(@{(DEFAULTGLOBALS)}-1))
1094 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1095 $globaltypes[$_] = 'defglobal';
1097 my %globaldefs_namecount = ();
1100 $_->{globaltype} = $globaltypes[$_->{ofs}];
1101 if(defined $_->{debugname})
1103 # already has debugname
1105 elsif($_->{globaltype} eq 'const')
1107 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1111 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1113 ++$globaldefs_namecount{$_->{debugname}};
1118 if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
1119 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1120 $_->{debugname} .= "\@$_->{ofs}";
1122 $progs->{globaldef_byoffset} = sub
1125 my $def = $globaldefs[$ofs];
1136 print STDERR "Parsing header...\n";
1137 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1139 print STDERR "Parsing strings...\n";
1140 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1143 my ($startpos) = @_;
1144 my $endpos = index $p{strings}, "\0", $startpos;
1145 return substr $p{strings}, $startpos, $endpos - $startpos;
1148 print STDERR "Parsing globals...\n";
1149 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1151 print STDERR "Parsing globaldefs...\n";
1152 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1154 print STDERR "Range checking globaldefs...\n";
1155 for(0 .. (@{$p{globaldefs}}-1))
1157 my $g = $p{globaldefs}[$_];
1158 die "Out of range name in globaldef $_"
1159 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1160 my $name = $p{getstring}->($g->{s_name});
1161 die "Out of range ofs in globaldef $_ (name: \"$name\")"
1162 if $g->{ofs} >= $p{globals};
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 "Range checking fielddefs...\n";
1169 for(0 .. (@{$p{fielddefs}}-1))
1171 my $g = $p{fielddefs}[$_];
1172 die "Out of range name in fielddef $_"
1173 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1174 my $name = $p{getstring}->($g->{s_name});
1175 die "Out of range ofs in globaldef $_ (name: \"$name\")"
1176 if $g->{ofs} >= $p{header}{entityfields};
1179 print STDERR "Parsing statements...\n";
1180 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1182 print STDERR "Parsing functions...\n";
1183 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1185 print STDERR "Range checking functions...\n";
1186 for(0 .. (@{$p{functions}} - 1))
1188 my $f = $p{functions}[$_];
1189 die "Out of range name in function $_"
1190 if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
1191 my $name = $p{getstring}->($f->{s_name});
1192 die "Out of range file in function $_"
1193 if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
1194 my $file = $p{getstring}->($f->{s_file});
1195 die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1196 if $f->{first_statement} >= @{$p{statements}};
1197 die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1198 if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
1199 die "Out of range locals in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1200 if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} >= @{$p{globals}};
1201 if($f->{first_statement} >= 0)
1203 die "Out of range numparms $f->{numparms} in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1204 if $f->{numparms} < 0 || $f->{numparms} > 8;
1206 for(0..($f->{numparms}-1))
1208 die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1209 unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
1210 $totalparms += $f->{parm_size}[$_];
1212 die "Out of range parms in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1213 if $f->{locals} < 0 || $f->{parm_start} + $totalparms >= @{$p{globals}};
1217 print STDERR "Range checking statements...\n";
1218 for my $ip(0 .. (@{$p{statements}}-1))
1220 my $s = $p{statements}[$ip];
1221 my $c = checkop $s->{op};
1225 my $type = $c->{$_};
1227 unless defined $type;
1229 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1232 die "Out of range global offset in statement $ip - cannot continue"
1233 if $s->{$_} >= @{$p{globals}};
1235 elsif($type eq 'inglobalvec')
1238 die "Out of range global offset in statement $ip - cannot continue"
1239 if $s->{$_} >= @{$p{globals}}-2;
1241 elsif($type eq 'outglobal')
1244 die "Out of range global offset in statement $ip - cannot continue"
1245 if $s->{$_} >= @{$p{globals}};
1247 elsif($type eq 'outglobalvec')
1250 die "Out of range global offset in statement $ip - cannot continue"
1251 if $s->{$_} >= @{$p{globals}}-2;
1253 elsif($type eq 'ipoffset')
1255 die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
1256 if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
1261 print STDERR "Looking for error()...\n";
1262 $p{error_func} = {};
1263 for(@{$p{globaldefs}})
1266 if $p{getstring}($_->{s_name}) ne 'error';
1267 my $v = $p{globals}[$_->{ofs}]{v}{int};
1269 if $v <= 0 || $v >= @{$p{functions}};
1270 my $first = $p{functions}[$v]{first_statement};
1273 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1274 $p{error_func}{$_->{ofs}} = 1;
1277 print STDERR "Scanning functions...\n";
1278 for(@{$p{functions}})
1280 my $file = $p{getstring}->($_->{s_file});
1281 my $name = $p{getstring}->($_->{s_name});
1282 $name = "$file:$name"
1284 $_->{debugname} = $name;
1287 if $_->{first_statement} < 0;
1289 my %statements = ();
1292 my %globals_read = ();
1293 my %globals_written = ();
1294 my %globals_used = ();
1296 if($_->{first_statement} >= 0)
1298 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1301 my ($ip, $state, $s, $c) = @_;
1304 if(my $j = $c->{isjump})
1306 my $t = $ip + $s->{$j};
1307 $come_from{$t}{$ip} = $c->{isconditional};
1308 $go_to{$ip}{$t} = $c->{isconditional};
1311 for my $o(qw(a b c))
1320 $globals_read{$ofs}{$ip}{$o} = 1;
1321 $globals_used{$ofs} = 1;
1326 $globals_written{$ofs}{$ip}{$o} = 1;
1327 $globals_used{$ofs} = 1;
1330 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1334 elsif($type eq 'inglobalvec')
1340 elsif($type eq 'outglobal')
1344 elsif($type eq 'outglobalvec')
1356 $_->{statements} = \%statements;
1357 $_->{come_from} = \%come_from;
1358 $_->{go_to} = \%go_to;
1359 $_->{globals_read} = \%globals_read;
1360 $_->{globals_written} = \%globals_written;
1361 $_->{globals_used} = \%globals_used;
1363 # using this info, we could now identify basic blocks
1366 print STDERR "Detecting constants and temps, and naming...\n";
1367 detect_constants \%p;
1376 # what do we want to do?
1377 my $checkfunc = \&find_uninitialized_locals;
1378 if($ENV{DISASSEMBLE})
1380 $checkfunc = \&disassemble_function;
1382 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1384 $checkfunc->(\%p, $_);
1388 open my $fh, '<', $ARGV[0];