10 return sub { $_[0]; };
16 return sub { $_[0] >= (2**($bits-1)) ? $_[0]-(2**$bits) : $_[0]; };
19 use constant OPCODE_E => [qw[
21 MUL_F MUL_V MUL_FV MUL_VF
25 EQ_F EQ_V EQ_S EQ_E EQ_FNC
26 NE_F NE_V NE_S NE_E NE_FNC
28 LOAD_F LOAD_V LOAD_S LOAD_ENT LOAD_FLD LOAD_FNC
30 STORE_F STORE_V STORE_S STORE_ENT STORE_FLD STORE_FNC
31 STOREP_F STOREP_V STOREP_S STOREP_ENT STOREP_FLD STOREP_FNC
33 NOT_F NOT_V NOT_S NOT_ENT NOT_FNC
35 CALL0 CALL1 CALL2 CALL3 CALL4 CALL5 CALL6 CALL7 CALL8
41 use constant ETYPE_E => [qw[
51 use constant DEF_SAVEGLOBAL => 32768;
55 return 3 if $type eq 'vector';
64 return { a => 'inglobalvec', b => 'ipoffset', isjump => 'b', isconditional => 1 };
68 return { a => 'inglobal', b => 'ipoffset', isjump => 'b', isconditional => 1 };
72 return { a => 'ipoffset', isjump => 'a', isconditional => 0 };
74 if($op =~ /^ADD_V$|^SUB_V$/)
76 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobalvec' };
78 if($op =~ /^MUL_V$|^EQ_V$|^NE_V$/)
80 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobal' };
84 return { a => 'inglobal', b => 'inglobalvec', c => 'outglobalvec' };
88 return { a => 'inglobalvec', b => 'inglobal', c => 'outglobalvec' };
92 return { a => 'inglobal', b => 'inglobal', c => 'outglobalvec' };
96 return { a => 'inglobalvec', c => 'outglobal' };
100 return { a => 'inglobal', c => 'outglobal' };
102 if($op eq 'STOREP_V')
104 return { a => 'inglobalvec', b => 'inglobal' };
108 return { a => 'inglobalvec', b => 'outglobalvec' };
110 if($op =~ /^STOREP_/)
112 return { a => 'inglobal', b => 'inglobal' };
116 return { a => 'inglobal', b => 'outglobal' };
120 return { a => 'inglobalfunc', iscall => 1 };
122 if($op =~ /^DONE$|^RETURN$/)
124 return { a => 'inglobalvec', isreturn => 1 };
128 return { a => 'inglobal', b => 'inglobalfunc' };
130 return { a => 'inglobal', b => 'inglobal', c => 'outglobal' };
133 use constant TYPES => {
134 int => ['V', 4, signed 32],
135 ushort => ['v', 2, id],
136 short => ['v', 2, signed 16],
137 opcode => ['v', 2, sub { OPCODE_E->[$_[0]] or do { warn "Invalid opcode: $_[0]"; "INVALID#$_[0]"; }; }],
138 float => ['f', 4, id],
139 uchar8 => ['a8', 8, sub { [unpack 'C8', $_[0]] }],
140 global => ['i', 4, sub { { int => $_[0], float => unpack "f", pack "L", $_[0] }; }],
141 deftype => ['v', 2, sub { { type => ETYPE_E->[$_[0] & ~DEF_SAVEGLOBAL], save => !!($_[0] & DEF_SAVEGLOBAL) }; }],
144 use constant DPROGRAMS_T => [
147 [int => 'ofs_statements'],
148 [int => 'numstatements'],
149 [int => 'ofs_globaldefs'],
150 [int => 'numglobaldefs'],
151 [int => 'ofs_fielddefs'],
152 [int => 'numfielddefs'],
153 [int => 'ofs_functions'],
154 [int => 'numfunctions'],
155 [int => 'ofs_strings'],
156 [int => 'numstrings'],
157 [int => 'ofs_globals'],
158 [int => 'numglobals'],
159 [int => 'entityfields']
162 use constant DSTATEMENT_T => [
169 use constant DDEF_T => [
175 use constant DGLOBAL_T => [
179 use constant DFUNCTION_T => [
180 [int => 'first_statement'],
181 [int => 'parm_start'],
187 [uchar8 => 'parm_size'],
192 my ($fh, $start, $len) = @_;
195 $len == read $fh, my $buf, $len
196 or die "short read from $start length $len (malformed progs header)";
200 sub parse_section($$$$$)
202 my ($fh, $struct, $start, $len, $cnt) = @_;
205 $itemlen += TYPES->{$_->[0]}->[1]
207 my $packspec = join '', map { TYPES->{$_->[0]}->[0]; } @$struct;
208 my @packnames = map { $_->[1]; } @$struct;
210 $len = $cnt * $itemlen
211 if not defined $len and defined $cnt;
212 $cnt = int($len / $itemlen)
213 if not defined $cnt and defined $len;
214 die "Invalid length specification"
215 unless defined $len and defined $cnt and $len == $cnt * $itemlen;
216 die "Invalid length specification in scalar context"
217 unless wantarray or $cnt == 1;
223 $itemlen == read $fh, my $buf, $itemlen
224 or die "short read from $start length $cnt * $itemlen $(malformed progs header)";
226 @h{@packnames} = unpack $packspec, $buf;
227 $h{$_->[1]} = TYPES->{$_->[0]}->[2]->($h{$_->[1]})
237 sub nfa_default_state_checker()
242 my ($ip, $state) = @_;
243 return $seen{"$ip $state"}++;
249 my ($progs, $ip, $state, $copy_handler, $state_checker, $instruction_handler) = @_;
251 my $statements = $progs->{statements};
256 no warnings 'recursion';
258 my ($ip, $state) = @_;
264 if $state_checker->($ip, $state);
266 my $s = $statements->[$ip];
267 my $c = checkop $s->{op};
269 if(($ret = $instruction_handler->($ip, $state, $s, $c)))
282 my $funcid = $progs->{globals}[$func]{v}{int};
284 if $progs->{error_func}{$funcid};
289 if($c->{isconditional})
293 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
301 $nfa->($ip+1, $copy_handler->($state));
302 $ip += $s->{$c->{isjump}};
307 $ip += $s->{$c->{isjump}};
319 $nfa->($ip, $copy_handler->($state));
324 my ($progs, $g) = @_;
329 elsif($g->{int} > 0 && $g->{int} < 8388608)
331 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
333 return str($progs->{getstring}->($g->{int}));
337 return $g->{int} . "i";
346 use constant PRE_MARK_STATEMENT => "";
347 use constant POST_MARK_STATEMENT => "";
348 use constant PRE_MARK_OPERAND => "*** ";
349 use constant POST_MARK_OPERAND => " ***";
351 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
352 use constant OPERAND_FORMAT => "%s";
353 use constant OPERAND_SEPARATOR => ", ";
354 use constant INSTRUCTION_SEPARATOR => "\n";
359 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
363 sub disassemble_function($$;$)
365 my ($progs, $func, $highlight) = @_;
367 print "$func->{debugname}:\n";
369 if($func->{first_statement} < 0) # builtin
371 printf INSTRUCTION_FORMAT, '', '', '.BUILTIN';
372 printf OPERAND_FORMAT, -$func->{first_statement};
373 print INSTRUCTION_SEPARATOR;
377 my $initializer = sub
380 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
385 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
386 printf OPERAND_FORMAT, "$func->{parm_start}";
387 print INSTRUCTION_SEPARATOR;
389 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
390 printf OPERAND_FORMAT, "$func->{locals}";
391 print INSTRUCTION_SEPARATOR;
393 my %override_locals = ();
394 my $p = $func->{parm_start};
395 for(0..($func->{numparms}-1))
397 $override_locals{$p} //= "argv_$_";
398 for my $comp(0..($func->{parm_size}[$_]-1))
400 $override_locals{$p} //= "argv_$_\[$comp]";
403 printf INSTRUCTION_FORMAT, '', '', '.ARG';
404 printf OPERAND_FORMAT, "argv_$_";
405 print OPERAND_SEPARATOR;
406 printf OPERAND_FORMAT, $func->{parm_size}[$_];
407 print INSTRUCTION_SEPARATOR;
409 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
412 if exists $override_locals{$_};
413 $override_locals{$_} = "local_$_";
415 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
416 printf OPERAND_FORMAT, "local_$_";
418 print INSTRUCTION_SEPARATOR;
424 return $override_locals{$ofs}
425 if exists $override_locals{$ofs};
426 my $def = $progs->{globaldef_byoffset}->($ofs);
427 return $def->{debugname};
432 my ($ip, $type, $operand) = @_;
433 if($type eq 'inglobal')
435 my $name = $getname->($operand);
436 printf OPERAND_FORMAT, "$name";
438 elsif($type eq 'outglobal')
440 my $name = $getname->($operand);
441 printf OPERAND_FORMAT, "&$name";
443 elsif($type eq 'inglobalvec')
445 my $name = $getname->($operand);
446 printf OPERAND_FORMAT, "$name\[\]";
448 elsif($type eq 'outglobalvec')
450 my $name = $getname->($operand);
451 printf OPERAND_FORMAT, "&$name\[\]";
453 elsif($type eq 'inglobalfunc')
455 my $name = $getname->($operand);
456 printf OPERAND_FORMAT, "$name()";
458 elsif($type eq 'ipoffset')
460 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
464 die "unknown type: $type";
468 my $statements = $func->{statements};
469 my $come_from = $func->{come_from};
472 for my $ip(sort { $a <=> $b } keys %$statements)
474 if($ip == $func->{first_statement})
476 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
477 print INSTRUCTION_SEPARATOR;
479 if(defined $ipprev && $ip != $ipprev + 1)
481 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
482 printf OPERAND_FORMAT, $ip - $ipprev - 1;
483 print INSTRUCTION_SEPARATOR;
485 if(my $cf = $come_from->{$ip})
487 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
489 for(sort { $a <=> $b } keys %$cf)
491 print OPERAND_SEPARATOR
493 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
495 print INSTRUCTION_SEPARATOR;
498 my $op = $progs->{statements}[$ip]{op};
499 my $ipt = $progs->{statements}[$ip];
500 my $opprop = checkop $op;
502 if($highlight and $highlight->{$ip})
504 for(values %{$highlight->{$ip}})
508 print PRE_MARK_STATEMENT;
509 printf INSTRUCTION_FORMAT, '', '<!>', '.WARN';
510 printf OPERAND_FORMAT, "$_ (in $func->{debugname})";
511 print INSTRUCTION_SEPARATOR;
516 print PRE_MARK_STATEMENT
517 if $highlight and $highlight->{$ip};
519 my $showip = $opprop->{isjump};
520 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? '<!>' : '', $op;
526 if not defined $opprop->{$o};
527 print OPERAND_SEPARATOR
529 print PRE_MARK_OPERAND
530 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
531 $operand->($ip, $opprop->{$o}, $ipt->{$o});
532 print POST_MARK_OPERAND
533 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
536 print POST_MARK_STATEMENT
537 if $highlight and $highlight->{$ip};
539 print INSTRUCTION_SEPARATOR;
543 sub find_uninitialized_locals($$)
545 my ($progs, $func) = @_;
548 if $func->{first_statement} < 0; # builtin
550 print STDERR "Checking $func->{debugname}...\n";
552 my $p = $func->{parm_start};
553 for(0..($func->{numparms}-1))
555 $p += $func->{parm_size}[$_];
558 use constant WATCHME_R => 1;
559 use constant WATCHME_W => 2;
560 use constant WATCHME_X => 4;
561 use constant WATCHME_T => 8;
562 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
564 for(keys %{$progs->{temps}})
567 if exists $watchme{$_};
568 if($progs->{temps}{$_})
571 $watchme{$_} = WATCHME_T | WATCHME_X
576 $watchme{$_} = WATCHME_X
580 $watchme{$_} |= WATCHME_R
581 for keys %{$func->{globals_read}};
582 $watchme{$_} |= WATCHME_W
583 for keys %{$func->{globals_written}};
585 my %write_places = ();
586 for my $ofs(keys %{$func->{globals_written}})
589 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
590 for my $ip(keys %{$func->{globals_written}{$ofs}})
592 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
594 push @{$write_places{$ip}{$op}}, $ofs;
602 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
606 if not keys %watchme;
611 flags => $watchme{$_},
612 valid => [0, undef, undef]
616 # mark parameters as initialized
617 for($func->{parm_start} .. ($p-1))
619 $watchme{$_}{valid} = [1, undef, undef]
620 if defined $watchme{$_};
625 run_nfa $progs, $func->{first_statement}, \%watchme,
628 return { map { $_ => { %{$h->{$_}} } } keys %$h };
631 my ($ip, $state) = @_;
633 my $s = $ip_seen{$ip};
636 # if $state is stronger or equal to $s, return 1
640 if($state->{$_}{valid}[0] < $s->{$_})
642 # The current state is LESS valid than the previously run one. We NEED to run this.
643 # The saved state can safely become the intersection [citation needed].
646 $s->{$_} = $state->{$_}{valid}[0]
647 if $state->{$_}{valid}[0] < $s->{$_};
652 # if we get here, $state is stronger or equal. No need to try it.
657 # Never seen this IP yet.
658 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
663 my ($ip, $state, $s, $c) = @_;
666 # QCVM BUG: RETURN always takes vector, there is no float equivalent
667 my $return_hack = $c->{isreturn} // 0;
671 # COMPILER BUG of QCC: params are always copied using STORE_V
672 if($s->{b} >= 4 && $s->{b} < 28) # parameter range
682 unless defined $type;
692 if not exists $state->{$ofs};
693 my $valid = $state->{$ofs}{valid};
696 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
697 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
699 ++$warned{$ip}{$_}{"Use of uninitialized value"};
702 elsif($valid->[0] < 0)
704 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
705 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
707 ++$warned{$ip}{$_}{"Use of temporary across CALL"};
713 if(defined $valid->[1])
715 delete $write_places{$valid->[1]}{$valid->[2]};
722 $state->{$ofs}{valid} = [1, $ip, $_]
723 if exists $state->{$ofs};
726 if($type eq 'inglobal' || $type eq 'inglobalfunc')
730 elsif($type eq 'inglobalvec')
736 elsif($type eq 'outglobal')
740 elsif($type eq 'outglobalvec')
746 elsif($type eq 'ipoffset')
748 ++$warned{$ip}{$_}{"Endless loop"}
750 ++$warned{$ip}{$_}{"No-operation jump"}
756 # builtin calls may clobber stuff
758 my $funcid = $progs->{globals}[$func]{v}{int};
759 my $funcobj = $progs->{functions}[$funcid];
760 if(!$funcobj || $funcobj->{first_statement} >= 0)
765 if($_->{flags} & WATCHME_T)
767 $_->{valid} = [-1, undef, undef];
776 for my $ip(keys %write_places)
778 for my $operand(keys %{$write_places{$ip}})
781 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
784 my %writeplace_seen = ();
785 run_nfa $progs, $ip+1, \%left,
792 my ($ip, $state) = @_;
793 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
797 my ($ip, $state, $s, $c) = @_;
802 unless defined $type;
805 if($type eq 'inglobal' || $type eq 'inglobalfunc')
810 return -1; # exit TOTALLY
813 elsif($type eq 'inglobalvec')
815 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
818 return -1; # exit TOTALLY
821 elsif($type eq 'outglobal')
823 delete $state->{$ofs};
827 elsif($type eq 'outglobalvec')
829 delete $state->{$ofs};
830 delete $state->{$ofs+1};
831 delete $state->{$ofs+2};
841 ++$warned{$ip}{$operand}{"Value is never used"};
846 disassemble_function($progs, $func, \%warned)
850 use constant DEFAULTGLOBALS => [
884 if($ofs < @{(DEFAULTGLOBALS)})
886 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
888 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
891 sub detect_constants($)
894 use constant GLOBALFLAG_R => 1; # read
895 use constant GLOBALFLAG_W => 2; # written
896 use constant GLOBALFLAG_S => 4; # saved
897 use constant GLOBALFLAG_I => 8; # initialized
898 use constant GLOBALFLAG_N => 16; # named
899 use constant GLOBALFLAG_Q => 32; # unique to function
900 use constant GLOBALFLAG_U => 64; # unused
901 use constant GLOBALFLAG_P => 128; # possibly parameter passing
902 use constant GLOBALFLAG_D => 256; # has a def
903 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x (@{$progs->{globals}} + 2);
905 for(@{$progs->{functions}})
907 for(keys %{$_->{globals_used}})
909 if($globalflags[$_] & GLOBALFLAG_U)
911 $globalflags[$_] &= ~GLOBALFLAG_U;
913 elsif($globalflags[$_] & GLOBALFLAG_Q)
915 $globalflags[$_] &= ~GLOBALFLAG_Q;
918 $globalflags[$_] |= GLOBALFLAG_R
919 for keys %{$_->{globals_read}};
920 $globalflags[$_] |= GLOBALFLAG_W
921 for keys %{$_->{globals_written}};
923 if $_->{first_statement} < 0;
924 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
926 my $s = $progs->{statements}[$ip];
927 if($s->{op} eq 'STORE_V')
929 $globalflags[$s->{a}] |= GLOBALFLAG_P
930 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
931 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
932 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
933 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
934 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
936 elsif($s->{op} =~ /^STORE_/)
938 $globalflags[$s->{a}] |= GLOBALFLAG_P
939 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
948 # parameter passing globals are only ever used in STORE_ instructions
949 for my $s(@{$progs->{statements}})
952 if $s->{op} =~ /^STORE_/;
954 my $c = checkop $s->{op};
960 unless defined $type;
963 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
965 $globalflags[$ofs] &= ~GLOBALFLAG_P;
967 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
969 $globalflags[$ofs] &= ~GLOBALFLAG_P;
970 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
971 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
976 my %offsets_saved = ();
977 for(@{$progs->{globaldefs}})
979 my $type = $_->{type};
980 my $name = $progs->{getstring}->($_->{s_name});
982 if $name eq 'IMMEDIATE' or $name =~ /^\./;
983 $_->{debugname} = $name
985 $globalflags[$_->{ofs}] |= GLOBALFLAG_D;
988 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
990 if(defined $_->{debugname})
992 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
997 for(@{$progs->{globaldefs}})
999 my $type = $_->{type};
1000 for my $i(1..(typesize($type->{type})-1))
1003 if(!($globalflags[$_->{ofs}+$i] & GLOBALFLAG_D))
1005 print "Missing globaldef for a component@{[defined $_->{debugname} ? ' of ' . $_->{debugname} : '']} at $_->{ofs}+$i\n";
1011 ofs => $_->{ofs} + $i,
1012 debugname => defined $_->{debugname} ? $_->{debugname} . "[$i]" : undef
1015 # "saved" and "named" states hit adjacent globals too
1016 $globalflags[$_->{ofs}+$i] |= $globalflags[$_->{ofs}] & (GLOBALFLAG_S | GLOBALFLAG_N | GLOBALFLAG_D);
1019 push @{$progs->{globaldefs}}, @extradefs;
1021 my %offsets_initialized = ();
1022 for(0..(@{$progs->{globals}}-1))
1024 if($progs->{globals}[$_]{v}{int})
1026 $globalflags[$_] |= GLOBALFLAG_I;
1030 my @globaltypes = (undef) x @{$progs->{globals}};
1033 for(0..(@{$progs->{globals}}-1))
1036 if $_ < @{(DEFAULTGLOBALS)};
1037 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
1039 $globaltypes[$_] = "unused";
1041 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
1044 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1046 $globaltypes[$_] = "read_only";
1048 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1050 $globaltypes[$_] = "const";
1054 $globaltypes[$_] = "read_only";
1057 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1059 $globaltypes[$_] = "write_only";
1063 # now we know it is rw
1064 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1066 $globaltypes[$_] = "global";
1068 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1070 if($globalflags[$_] & GLOBALFLAG_P)
1072 $globaltypes[$_] = "OFS_PARM";
1074 elsif($globalflags[$_] & GLOBALFLAG_Q)
1076 $globaltypes[$_] = "uniquetemp";
1081 $globaltypes[$_] = "temp";
1085 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1087 $globaltypes[$_] = "not_saved";
1091 $globaltypes[$_] = "global";
1095 $progs->{temps} = \%istemp;
1098 my @globaldefs = (undef) x @{$progs->{globals}};
1099 for(@{$progs->{globaldefs}})
1101 $globaldefs[$_->{ofs}] //= $_
1102 if defined $_->{debugname};
1104 for(@{$progs->{globaldefs}})
1106 $globaldefs[$_->{ofs}] //= $_;
1108 for(0..(@{$progs->{globals}}-1))
1110 $globaldefs[$_] //= {
1117 for(0..(@{(DEFAULTGLOBALS)}-1))
1119 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1120 $globaltypes[$_] = 'defglobal';
1122 my %globaldefs_namecount = ();
1125 $_->{globaltype} = $globaltypes[$_->{ofs}];
1126 if(defined $_->{debugname})
1128 # already has debugname
1130 elsif($_->{globaltype} eq 'const')
1132 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1136 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1138 ++$globaldefs_namecount{$_->{debugname}};
1143 if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
1144 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1145 $_->{debugname} .= "\@$_->{ofs}";
1147 $progs->{globaldef_byoffset} = sub
1150 my $def = $globaldefs[$ofs];
1161 print STDERR "Parsing header...\n";
1162 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1164 print STDERR "Parsing strings...\n";
1165 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1168 my ($startpos) = @_;
1169 my $endpos = index $p{strings}, "\0", $startpos;
1170 return substr $p{strings}, $startpos, $endpos - $startpos;
1173 print STDERR "Parsing globals...\n";
1174 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1176 print STDERR "Parsing globaldefs...\n";
1177 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1179 print STDERR "Range checking globaldefs...\n";
1180 for(0 .. (@{$p{globaldefs}}-1))
1182 my $g = $p{globaldefs}[$_];
1183 die "Out of range name in globaldef $_"
1184 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1185 my $name = $p{getstring}->($g->{s_name});
1186 die "Out of range ofs $g->{ofs} in globaldef $_ (name: \"$name\")"
1187 if $g->{ofs} >= $p{globals};
1190 print STDERR "Parsing fielddefs...\n";
1191 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1193 print STDERR "Range checking fielddefs...\n";
1194 for(0 .. (@{$p{fielddefs}}-1))
1196 my $g = $p{fielddefs}[$_];
1197 die "Out of range name in fielddef $_"
1198 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1199 my $name = $p{getstring}->($g->{s_name});
1200 die "Out of range ofs $g->{ofs} in fielddef $_ (name: \"$name\")"
1201 if $g->{ofs} >= $p{header}{entityfields};
1204 print STDERR "Parsing statements...\n";
1205 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1207 print STDERR "Parsing functions...\n";
1208 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1210 print STDERR "Range checking functions...\n";
1211 for(0 .. (@{$p{functions}} - 1))
1213 my $f = $p{functions}[$_];
1214 die "Out of range name in function $_"
1215 if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
1216 my $name = $p{getstring}->($f->{s_name});
1217 die "Out of range file in function $_"
1218 if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
1219 my $file = $p{getstring}->($f->{s_file});
1220 die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1221 if $f->{first_statement} >= @{$p{statements}};
1222 die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1223 if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
1224 die "Out of range locals in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1225 if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} >= @{$p{globals}};
1226 if($f->{first_statement} >= 0)
1228 die "Out of range numparms $f->{numparms} in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1229 if $f->{numparms} < 0 || $f->{numparms} > 8;
1231 for(0..($f->{numparms}-1))
1233 die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1234 unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
1235 $totalparms += $f->{parm_size}[$_];
1237 die "Out of range parms in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1238 if $f->{locals} < 0 || $f->{parm_start} + $totalparms >= @{$p{globals}};
1242 print STDERR "Range checking statements...\n";
1243 for my $ip(0 .. (@{$p{statements}}-1))
1245 my $s = $p{statements}[$ip];
1246 my $c = checkop $s->{op};
1250 my $type = $c->{$_};
1252 unless defined $type;
1254 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1257 die "Out of range global offset in statement $ip - cannot continue"
1258 if $s->{$_} >= @{$p{globals}};
1260 elsif($type eq 'inglobalvec')
1265 die "Out of range global offset in statement $ip - cannot continue"
1266 if $s->{$_} >= @{$p{globals}};
1267 print "Potentially out of range global offset in statement $ip - may crash engines"
1268 if $s->{$_} >= @{$p{globals}}-2;
1272 die "Out of range global offset in statement $ip - cannot continue"
1273 if $s->{$_} >= @{$p{globals}}-2;
1276 elsif($type eq 'outglobal')
1279 die "Out of range global offset in statement $ip - cannot continue"
1280 if $s->{$_} >= @{$p{globals}};
1282 elsif($type eq 'outglobalvec')
1285 die "Out of range global offset in statement $ip - cannot continue"
1286 if $s->{$_} >= @{$p{globals}}-2;
1288 elsif($type eq 'ipoffset')
1290 die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
1291 if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
1296 print STDERR "Looking for error()...\n";
1297 $p{error_func} = {};
1298 for(@{$p{globaldefs}})
1301 if $p{getstring}($_->{s_name}) ne 'error';
1302 my $v = $p{globals}[$_->{ofs}]{v}{int};
1304 if $v <= 0 || $v >= @{$p{functions}};
1305 my $first = $p{functions}[$v]{first_statement};
1308 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1309 $p{error_func}{$_->{ofs}} = 1;
1312 print STDERR "Scanning functions...\n";
1313 for(@{$p{functions}})
1315 my $file = $p{getstring}->($_->{s_file});
1316 my $name = $p{getstring}->($_->{s_name});
1317 $name = "$file:$name"
1319 $_->{debugname} = $name;
1322 if $_->{first_statement} < 0;
1324 my %statements = ();
1327 my %globals_read = ();
1328 my %globals_written = ();
1329 my %globals_used = ();
1331 if($_->{first_statement} >= 0)
1333 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1336 my ($ip, $state, $s, $c) = @_;
1339 if(my $j = $c->{isjump})
1341 my $t = $ip + $s->{$j};
1342 $come_from{$t}{$ip} = $c->{isconditional};
1343 $go_to{$ip}{$t} = $c->{isconditional};
1346 for my $o(qw(a b c))
1355 $globals_read{$ofs}{$ip}{$o} = 1;
1356 $globals_used{$ofs} = 1;
1361 $globals_written{$ofs}{$ip}{$o} = 1;
1362 $globals_used{$ofs} = 1;
1365 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1369 elsif($type eq 'inglobalvec')
1375 elsif($type eq 'outglobal')
1379 elsif($type eq 'outglobalvec')
1391 $_->{statements} = \%statements;
1392 $_->{come_from} = \%come_from;
1393 $_->{go_to} = \%go_to;
1394 $_->{globals_read} = \%globals_read;
1395 $_->{globals_written} = \%globals_written;
1396 $_->{globals_used} = \%globals_used;
1398 # using this info, we could now identify basic blocks
1401 print STDERR "Detecting constants and temps, and naming...\n";
1402 detect_constants \%p;
1411 # what do we want to do?
1412 my $checkfunc = \&find_uninitialized_locals;
1413 if($ENV{DISASSEMBLE})
1415 $checkfunc = \&disassemble_function;
1417 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1419 $checkfunc->(\%p, $_);
1423 open my $fh, '<', $ARGV[0];