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 use constant GLOBALFLAG_D => 256; # has a def
901 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x (@{$progs->{globals}} + 2);
903 for(@{$progs->{functions}})
905 for(keys %{$_->{globals_used}})
907 if($globalflags[$_] & GLOBALFLAG_U)
909 $globalflags[$_] &= ~GLOBALFLAG_U;
911 elsif($globalflags[$_] & GLOBALFLAG_Q)
913 $globalflags[$_] &= ~GLOBALFLAG_Q;
916 $globalflags[$_] |= GLOBALFLAG_R
917 for keys %{$_->{globals_read}};
918 $globalflags[$_] |= GLOBALFLAG_W
919 for keys %{$_->{globals_written}};
921 if $_->{first_statement} < 0;
922 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
924 my $s = $progs->{statements}[$ip];
925 if($s->{op} eq 'STORE_V')
927 $globalflags[$s->{a}] |= GLOBALFLAG_P
928 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
929 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
930 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
931 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
932 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
934 elsif($s->{op} =~ /^STORE_/)
936 $globalflags[$s->{a}] |= GLOBALFLAG_P
937 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
946 # parameter passing globals are only ever used in STORE_ instructions
947 for my $s(@{$progs->{statements}})
950 if $s->{op} =~ /^STORE_/;
952 my $c = checkop $s->{op};
958 unless defined $type;
961 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
963 $globalflags[$ofs] &= ~GLOBALFLAG_P;
965 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
967 $globalflags[$ofs] &= ~GLOBALFLAG_P;
968 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
969 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
974 my %offsets_saved = ();
975 for(@{$progs->{globaldefs}})
977 my $type = $_->{type};
978 my $name = $progs->{getstring}->($_->{s_name});
980 if $name eq 'IMMEDIATE' or $name =~ /^\./;
981 $_->{debugname} = $name
983 $globalflags[$_->{ofs}] |= GLOBALFLAG_D;
986 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
988 if(defined $_->{debugname})
990 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
995 for(@{$progs->{globaldefs}})
997 my $type = $_->{type};
998 for my $i(1..(typesize($type->{type})-1))
1001 if(!($globalflags[$_->{ofs}+$i] & GLOBALFLAG_D))
1003 print "Missing globaldef for a component@{[defined $_->{debugname} ? ' of ' . $_->{debugname} : '']} at $_->{ofs}+$i\n";
1009 ofs => $_->{ofs} + $i,
1010 debugname => defined $_->{debugname} ? $_->{debugname} . "[$i]" : undef
1013 # "saved" and "named" states hit adjacent globals too
1014 $globalflags[$_->{ofs}+$i] |= $globalflags[$_->{ofs}] & (GLOBALFLAG_S | GLOBALFLAG_N | GLOBALFLAG_D);
1017 push @{$progs->{globaldefs}}, @extradefs;
1019 my %offsets_initialized = ();
1020 for(0..(@{$progs->{globals}}-1))
1022 if($progs->{globals}[$_]{v}{int})
1024 $globalflags[$_] |= GLOBALFLAG_I;
1028 my @globaltypes = (undef) x @{$progs->{globals}};
1031 for(0..(@{$progs->{globals}}-1))
1034 if $_ < @{(DEFAULTGLOBALS)};
1035 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
1037 $globaltypes[$_] = "unused";
1039 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
1042 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1044 $globaltypes[$_] = "read_only";
1046 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1048 $globaltypes[$_] = "const";
1052 $globaltypes[$_] = "read_only";
1055 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1057 $globaltypes[$_] = "write_only";
1061 # now we know it is rw
1062 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1064 $globaltypes[$_] = "global";
1066 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1068 if($globalflags[$_] & GLOBALFLAG_P)
1070 $globaltypes[$_] = "OFS_PARM";
1072 elsif($globalflags[$_] & GLOBALFLAG_Q)
1074 $globaltypes[$_] = "uniquetemp";
1079 $globaltypes[$_] = "temp";
1083 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1085 $globaltypes[$_] = "not_saved";
1089 $globaltypes[$_] = "global";
1093 $progs->{temps} = \%istemp;
1096 my @globaldefs = (undef) x @{$progs->{globals}};
1097 for(@{$progs->{globaldefs}})
1099 $globaldefs[$_->{ofs}] //= $_
1100 if defined $_->{debugname};
1102 for(@{$progs->{globaldefs}})
1104 $globaldefs[$_->{ofs}] //= $_;
1106 for(0..(@{$progs->{globals}}-1))
1108 $globaldefs[$_] //= {
1115 for(0..(@{(DEFAULTGLOBALS)}-1))
1117 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1118 $globaltypes[$_] = 'defglobal';
1120 my %globaldefs_namecount = ();
1123 $_->{globaltype} = $globaltypes[$_->{ofs}];
1124 if(defined $_->{debugname})
1126 # already has debugname
1128 elsif($_->{globaltype} eq 'const')
1130 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1134 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1136 ++$globaldefs_namecount{$_->{debugname}};
1141 if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
1142 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1143 $_->{debugname} .= "\@$_->{ofs}";
1145 $progs->{globaldef_byoffset} = sub
1148 my $def = $globaldefs[$ofs];
1159 print STDERR "Parsing header...\n";
1160 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1162 print STDERR "Parsing strings...\n";
1163 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1166 my ($startpos) = @_;
1167 my $endpos = index $p{strings}, "\0", $startpos;
1168 return substr $p{strings}, $startpos, $endpos - $startpos;
1171 print STDERR "Parsing globals...\n";
1172 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1174 print STDERR "Parsing globaldefs...\n";
1175 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1177 print STDERR "Range checking globaldefs...\n";
1178 for(0 .. (@{$p{globaldefs}}-1))
1180 my $g = $p{globaldefs}[$_];
1181 die "Out of range name in globaldef $_"
1182 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1183 my $name = $p{getstring}->($g->{s_name});
1184 die "Out of range ofs $g->{ofs} in globaldef $_ (name: \"$name\")"
1185 if $g->{ofs} >= $p{globals};
1188 print STDERR "Parsing fielddefs...\n";
1189 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1191 print STDERR "Range checking fielddefs...\n";
1192 for(0 .. (@{$p{fielddefs}}-1))
1194 my $g = $p{fielddefs}[$_];
1195 die "Out of range name in fielddef $_"
1196 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1197 my $name = $p{getstring}->($g->{s_name});
1198 die "Out of range ofs $g->{ofs} in fielddef $_ (name: \"$name\")"
1199 if $g->{ofs} >= $p{header}{entityfields};
1202 print STDERR "Parsing statements...\n";
1203 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1205 print STDERR "Parsing functions...\n";
1206 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1208 print STDERR "Range checking functions...\n";
1209 for(0 .. (@{$p{functions}} - 1))
1211 my $f = $p{functions}[$_];
1212 die "Out of range name in function $_"
1213 if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
1214 my $name = $p{getstring}->($f->{s_name});
1215 die "Out of range file in function $_"
1216 if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
1217 my $file = $p{getstring}->($f->{s_file});
1218 die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1219 if $f->{first_statement} >= @{$p{statements}};
1220 die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1221 if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
1222 die "Out of range locals in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1223 if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} >= @{$p{globals}};
1224 if($f->{first_statement} >= 0)
1226 die "Out of range numparms $f->{numparms} in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1227 if $f->{numparms} < 0 || $f->{numparms} > 8;
1229 for(0..($f->{numparms}-1))
1231 die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1232 unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
1233 $totalparms += $f->{parm_size}[$_];
1235 die "Out of range parms in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1236 if $f->{locals} < 0 || $f->{parm_start} + $totalparms >= @{$p{globals}};
1240 print STDERR "Range checking statements...\n";
1241 for my $ip(0 .. (@{$p{statements}}-1))
1243 my $s = $p{statements}[$ip];
1244 my $c = checkop $s->{op};
1248 my $type = $c->{$_};
1250 unless defined $type;
1252 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1255 die "Out of range global offset in statement $ip - cannot continue"
1256 if $s->{$_} >= @{$p{globals}};
1258 elsif($type eq 'inglobalvec')
1263 die "Out of range global offset in statement $ip - cannot continue"
1264 if $s->{$_} >= @{$p{globals}};
1265 print "Potentially out of range global offset in statement $ip - may crash engines"
1266 if $s->{$_} >= @{$p{globals}}-2;
1270 die "Out of range global offset in statement $ip - cannot continue"
1271 if $s->{$_} >= @{$p{globals}}-2;
1274 elsif($type eq 'outglobal')
1277 die "Out of range global offset in statement $ip - cannot continue"
1278 if $s->{$_} >= @{$p{globals}};
1280 elsif($type eq 'outglobalvec')
1283 die "Out of range global offset in statement $ip - cannot continue"
1284 if $s->{$_} >= @{$p{globals}}-2;
1286 elsif($type eq 'ipoffset')
1288 die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
1289 if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
1294 print STDERR "Looking for error()...\n";
1295 $p{error_func} = {};
1296 for(@{$p{globaldefs}})
1299 if $p{getstring}($_->{s_name}) ne 'error';
1300 my $v = $p{globals}[$_->{ofs}]{v}{int};
1302 if $v <= 0 || $v >= @{$p{functions}};
1303 my $first = $p{functions}[$v]{first_statement};
1306 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1307 $p{error_func}{$_->{ofs}} = 1;
1310 print STDERR "Scanning functions...\n";
1311 for(@{$p{functions}})
1313 my $file = $p{getstring}->($_->{s_file});
1314 my $name = $p{getstring}->($_->{s_name});
1315 $name = "$file:$name"
1317 $_->{debugname} = $name;
1320 if $_->{first_statement} < 0;
1322 my %statements = ();
1325 my %globals_read = ();
1326 my %globals_written = ();
1327 my %globals_used = ();
1329 if($_->{first_statement} >= 0)
1331 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1334 my ($ip, $state, $s, $c) = @_;
1337 if(my $j = $c->{isjump})
1339 my $t = $ip + $s->{$j};
1340 $come_from{$t}{$ip} = $c->{isconditional};
1341 $go_to{$ip}{$t} = $c->{isconditional};
1344 for my $o(qw(a b c))
1353 $globals_read{$ofs}{$ip}{$o} = 1;
1354 $globals_used{$ofs} = 1;
1359 $globals_written{$ofs}{$ip}{$o} = 1;
1360 $globals_used{$ofs} = 1;
1363 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1367 elsif($type eq 'inglobalvec')
1373 elsif($type eq 'outglobal')
1377 elsif($type eq 'outglobalvec')
1389 $_->{statements} = \%statements;
1390 $_->{come_from} = \%come_from;
1391 $_->{go_to} = \%go_to;
1392 $_->{globals_read} = \%globals_read;
1393 $_->{globals_written} = \%globals_written;
1394 $_->{globals_used} = \%globals_used;
1396 # using this info, we could now identify basic blocks
1399 print STDERR "Detecting constants and temps, and naming...\n";
1400 detect_constants \%p;
1409 # what do we want to do?
1410 my $checkfunc = \&find_uninitialized_locals;
1411 if($ENV{DISASSEMBLE})
1413 $checkfunc = \&disassemble_function;
1415 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1417 $checkfunc->(\%p, $_);
1421 open my $fh, '<', $ARGV[0];