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 if($op =~ /^INVALID#/)
132 return { isinvalid => 1 };
134 return { a => 'inglobal', b => 'inglobal', c => 'outglobal' };
137 use constant TYPES => {
138 int => ['V', 4, signed 32],
139 ushort => ['v', 2, id],
140 short => ['v', 2, signed 16],
141 opcode => ['v', 2, sub { OPCODE_E->[$_[0]] or do { warn "Invalid opcode: $_[0]"; "INVALID#$_[0]"; }; }],
142 float => ['f', 4, id],
143 uchar8 => ['a8', 8, sub { [unpack 'C8', $_[0]] }],
144 global => ['i', 4, sub { { int => $_[0], float => unpack "f", pack "L", $_[0] }; }],
145 deftype => ['v', 2, sub { { type => ETYPE_E->[$_[0] & ~DEF_SAVEGLOBAL], save => !!($_[0] & DEF_SAVEGLOBAL) }; }],
148 use constant DPROGRAMS_T => [
151 [int => 'ofs_statements'],
152 [int => 'numstatements'],
153 [int => 'ofs_globaldefs'],
154 [int => 'numglobaldefs'],
155 [int => 'ofs_fielddefs'],
156 [int => 'numfielddefs'],
157 [int => 'ofs_functions'],
158 [int => 'numfunctions'],
159 [int => 'ofs_strings'],
160 [int => 'numstrings'],
161 [int => 'ofs_globals'],
162 [int => 'numglobals'],
163 [int => 'entityfields']
166 use constant DSTATEMENT_T => [
173 use constant DDEF_T => [
179 use constant DGLOBAL_T => [
183 use constant DFUNCTION_T => [
184 [int => 'first_statement'],
185 [int => 'parm_start'],
191 [uchar8 => 'parm_size'],
196 my ($fh, $start, $len) = @_;
199 $len == read $fh, my $buf, $len
200 or die "short read from $start length $len (malformed progs header)";
204 sub parse_section($$$$$)
206 my ($fh, $struct, $start, $len, $cnt) = @_;
209 $itemlen += TYPES->{$_->[0]}->[1]
211 my $packspec = join '', map { TYPES->{$_->[0]}->[0]; } @$struct;
212 my @packnames = map { $_->[1]; } @$struct;
214 $len = $cnt * $itemlen
215 if not defined $len and defined $cnt;
216 $cnt = int($len / $itemlen)
217 if not defined $cnt and defined $len;
218 die "Invalid length specification"
219 unless defined $len and defined $cnt and $len == $cnt * $itemlen;
220 die "Invalid length specification in scalar context"
221 unless wantarray or $cnt == 1;
227 $itemlen == read $fh, my $buf, $itemlen
228 or die "short read from $start length $cnt * $itemlen $(malformed progs header)";
230 @h{@packnames} = unpack $packspec, $buf;
231 $h{$_->[1]} = TYPES->{$_->[0]}->[2]->($h{$_->[1]})
241 sub nfa_default_state_checker()
246 my ($ip, $state) = @_;
247 return $seen{"$ip $state"}++;
253 my ($progs, $ip, $state, $copy_handler, $state_checker, $instruction_handler) = @_;
255 my $statements = $progs->{statements};
260 no warnings 'recursion';
262 my ($ip, $state) = @_;
268 if $state_checker->($ip, $state);
270 my $s = $statements->[$ip];
271 my $c = checkop $s->{op};
273 if(($ret = $instruction_handler->($ip, $state, $s, $c)))
286 my $funcid = $progs->{globals}[$func]{v}{int};
288 if $progs->{error_func}{$funcid};
293 if($c->{isconditional})
297 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
305 $nfa->($ip+1, $copy_handler->($state));
306 $ip += $s->{$c->{isjump}};
311 $ip += $s->{$c->{isjump}};
323 $nfa->($ip, $copy_handler->($state));
328 my ($progs, $g) = @_;
333 elsif($g->{int} > 0 && $g->{int} < 8388608)
335 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
337 return str($progs->{getstring}->($g->{int}));
341 return $g->{int} . "i";
350 use constant PRE_MARK_STATEMENT => "";
351 use constant POST_MARK_STATEMENT => "";
352 use constant PRE_MARK_OPERAND => "*** ";
353 use constant POST_MARK_OPERAND => " ***";
355 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
356 use constant OPERAND_FORMAT => "%s";
357 use constant OPERAND_SEPARATOR => ", ";
358 use constant INSTRUCTION_SEPARATOR => "\n";
363 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
367 sub disassemble_function($$;$)
369 my ($progs, $func, $highlight) = @_;
371 print "$func->{debugname}:\n";
373 if($func->{first_statement} < 0) # builtin
375 printf INSTRUCTION_FORMAT, '', '', '.BUILTIN';
376 printf OPERAND_FORMAT, -$func->{first_statement};
377 print INSTRUCTION_SEPARATOR;
381 my $initializer = sub
384 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
389 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
390 printf OPERAND_FORMAT, "$func->{parm_start}";
391 print INSTRUCTION_SEPARATOR;
393 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
394 printf OPERAND_FORMAT, "$func->{locals}";
395 print INSTRUCTION_SEPARATOR;
397 my %override_locals = ();
398 my $p = $func->{parm_start};
399 for(0..($func->{numparms}-1))
401 $override_locals{$p} //= "argv_$_";
402 for my $comp(0..($func->{parm_size}[$_]-1))
404 $override_locals{$p} //= "argv_$_\[$comp]";
407 printf INSTRUCTION_FORMAT, '', '', '.ARG';
408 printf OPERAND_FORMAT, "argv_$_";
409 print OPERAND_SEPARATOR;
410 printf OPERAND_FORMAT, $func->{parm_size}[$_];
411 print INSTRUCTION_SEPARATOR;
413 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
416 if exists $override_locals{$_};
417 $override_locals{$_} = "local_$_";
419 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
420 printf OPERAND_FORMAT, "local_$_";
422 print INSTRUCTION_SEPARATOR;
428 return $override_locals{$ofs}
429 if exists $override_locals{$ofs};
430 my $def = $progs->{globaldef_byoffset}->($ofs);
431 return $def->{debugname};
436 my ($ip, $type, $operand) = @_;
437 if($type eq 'inglobal')
439 my $name = $getname->($operand);
440 printf OPERAND_FORMAT, "$name";
442 elsif($type eq 'outglobal')
444 my $name = $getname->($operand);
445 printf OPERAND_FORMAT, "&$name";
447 elsif($type eq 'inglobalvec')
449 my $name = $getname->($operand);
450 printf OPERAND_FORMAT, "$name\[\]";
452 elsif($type eq 'outglobalvec')
454 my $name = $getname->($operand);
455 printf OPERAND_FORMAT, "&$name\[\]";
457 elsif($type eq 'inglobalfunc')
459 my $name = $getname->($operand);
460 printf OPERAND_FORMAT, "$name()";
462 elsif($type eq 'ipoffset')
464 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
468 die "unknown type: $type";
472 my $statements = $func->{statements};
473 my $come_from = $func->{come_from};
476 for my $ip(sort { $a <=> $b } keys %$statements)
478 if($ip == $func->{first_statement})
480 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
481 print INSTRUCTION_SEPARATOR;
483 if(defined $ipprev && $ip != $ipprev + 1)
485 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
486 printf OPERAND_FORMAT, $ip - $ipprev - 1;
487 print INSTRUCTION_SEPARATOR;
489 if(my $cf = $come_from->{$ip})
491 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
493 for(sort { $a <=> $b } keys %$cf)
495 print OPERAND_SEPARATOR
497 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
499 print INSTRUCTION_SEPARATOR;
502 my $op = $progs->{statements}[$ip]{op};
503 my $ipt = $progs->{statements}[$ip];
504 my $opprop = checkop $op;
506 if($highlight and $highlight->{$ip})
508 for(values %{$highlight->{$ip}})
512 print PRE_MARK_STATEMENT;
513 printf INSTRUCTION_FORMAT, '', '<!>', '.WARN';
514 printf OPERAND_FORMAT, "$_ (in $func->{debugname})";
515 print INSTRUCTION_SEPARATOR;
520 print PRE_MARK_STATEMENT
521 if $highlight and $highlight->{$ip};
523 my $showip = $opprop->{isjump};
524 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? '<!>' : '', $op;
530 if not defined $opprop->{$o};
531 print OPERAND_SEPARATOR
533 print PRE_MARK_OPERAND
534 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
535 $operand->($ip, $opprop->{$o}, $ipt->{$o});
536 print POST_MARK_OPERAND
537 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
540 print POST_MARK_STATEMENT
541 if $highlight and $highlight->{$ip};
543 print INSTRUCTION_SEPARATOR;
547 sub find_uninitialized_locals($$)
549 my ($progs, $func) = @_;
552 if $func->{first_statement} < 0; # builtin
554 print STDERR "Checking $func->{debugname}...\n";
556 my $p = $func->{parm_start};
557 for(0..($func->{numparms}-1))
559 $p += $func->{parm_size}[$_];
562 use constant WATCHME_R => 1;
563 use constant WATCHME_W => 2;
564 use constant WATCHME_X => 4;
565 use constant WATCHME_T => 8;
566 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
568 for(keys %{$progs->{temps}})
571 if exists $watchme{$_};
572 if($progs->{temps}{$_})
575 $watchme{$_} = WATCHME_T | WATCHME_X
580 $watchme{$_} = WATCHME_X
584 $watchme{$_} |= WATCHME_R
585 for keys %{$func->{globals_read}};
586 $watchme{$_} |= WATCHME_W
587 for keys %{$func->{globals_written}};
589 my %write_places = ();
590 for my $ofs(keys %{$func->{globals_written}})
593 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
594 for my $ip(keys %{$func->{globals_written}{$ofs}})
596 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
598 push @{$write_places{$ip}{$op}}, $ofs;
606 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
610 if not keys %watchme;
615 flags => $watchme{$_},
616 valid => [0, undef, undef]
620 # mark parameters as initialized
621 for($func->{parm_start} .. ($p-1))
623 $watchme{$_}{valid} = [1, undef, undef]
624 if defined $watchme{$_};
629 run_nfa $progs, $func->{first_statement}, \%watchme,
632 return { map { $_ => { %{$h->{$_}} } } keys %$h };
635 my ($ip, $state) = @_;
637 my $s = $ip_seen{$ip};
640 # if $state is stronger or equal to $s, return 1
644 if($state->{$_}{valid}[0] < $s->{$_})
646 # The current state is LESS valid than the previously run one. We NEED to run this.
647 # The saved state can safely become the intersection [citation needed].
650 $s->{$_} = $state->{$_}{valid}[0]
651 if $state->{$_}{valid}[0] < $s->{$_};
656 # if we get here, $state is stronger or equal. No need to try it.
661 # Never seen this IP yet.
662 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
667 my ($ip, $state, $s, $c) = @_;
670 # QCVM BUG: RETURN always takes vector, there is no float equivalent
671 my $return_hack = $c->{isreturn} // 0;
675 # COMPILER BUG of QCC: params are always copied using STORE_V
676 if($s->{b} >= 4 && $s->{b} < 28) # parameter range
684 ++$warned{$ip}{''}{"Invalid opcode"};
690 unless defined $type;
700 if not exists $state->{$ofs};
701 my $valid = $state->{$ofs}{valid};
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 uninitialized value"};
710 elsif($valid->[0] < 0)
712 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
713 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
715 ++$warned{$ip}{$_}{"Use of temporary across CALL"};
721 if(defined $valid->[1])
723 delete $write_places{$valid->[1]}{$valid->[2]};
730 $state->{$ofs}{valid} = [1, $ip, $_]
731 if exists $state->{$ofs};
734 if($type eq 'inglobal' || $type eq 'inglobalfunc')
738 elsif($type eq 'inglobalvec')
744 elsif($type eq 'outglobal')
748 elsif($type eq 'outglobalvec')
754 elsif($type eq 'ipoffset')
756 ++$warned{$ip}{$_}{"Endless loop"}
758 ++$warned{$ip}{$_}{"No-operation jump"}
764 # builtin calls may clobber stuff
766 my $funcid = $progs->{globals}[$func]{v}{int};
767 my $funcobj = $progs->{functions}[$funcid];
768 if(!$funcobj || $funcobj->{first_statement} >= 0)
773 if($_->{flags} & WATCHME_T)
775 $_->{valid} = [-1, undef, undef];
784 for my $ip(keys %write_places)
786 for my $operand(keys %{$write_places{$ip}})
789 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
792 my %writeplace_seen = ();
793 run_nfa $progs, $ip+1, \%left,
800 my ($ip, $state) = @_;
801 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
805 my ($ip, $state, $s, $c) = @_;
810 unless defined $type;
813 if($type eq 'inglobal' || $type eq 'inglobalfunc')
818 return -1; # exit TOTALLY
821 elsif($type eq 'inglobalvec')
823 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
826 return -1; # exit TOTALLY
829 elsif($type eq 'outglobal')
831 delete $state->{$ofs};
835 elsif($type eq 'outglobalvec')
837 delete $state->{$ofs};
838 delete $state->{$ofs+1};
839 delete $state->{$ofs+2};
849 ++$warned{$ip}{$operand}{"Value is never used"};
854 disassemble_function($progs, $func, \%warned)
858 use constant DEFAULTGLOBALS => [
892 if($ofs < @{(DEFAULTGLOBALS)})
894 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
896 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
899 sub detect_constants($)
902 use constant GLOBALFLAG_R => 1; # read
903 use constant GLOBALFLAG_W => 2; # written
904 use constant GLOBALFLAG_S => 4; # saved
905 use constant GLOBALFLAG_I => 8; # initialized
906 use constant GLOBALFLAG_N => 16; # named
907 use constant GLOBALFLAG_Q => 32; # unique to function
908 use constant GLOBALFLAG_U => 64; # unused
909 use constant GLOBALFLAG_P => 128; # possibly parameter passing
910 use constant GLOBALFLAG_D => 256; # has a def
911 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x (@{$progs->{globals}} + 2);
913 for(@{$progs->{functions}})
915 for(keys %{$_->{globals_used}})
917 if($globalflags[$_] & GLOBALFLAG_U)
919 $globalflags[$_] &= ~GLOBALFLAG_U;
921 elsif($globalflags[$_] & GLOBALFLAG_Q)
923 $globalflags[$_] &= ~GLOBALFLAG_Q;
926 $globalflags[$_] |= GLOBALFLAG_R
927 for keys %{$_->{globals_read}};
928 $globalflags[$_] |= GLOBALFLAG_W
929 for keys %{$_->{globals_written}};
931 if $_->{first_statement} < 0;
932 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
934 my $s = $progs->{statements}[$ip];
935 if($s->{op} eq 'STORE_V')
937 $globalflags[$s->{a}] |= GLOBALFLAG_P
938 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
939 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
940 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
941 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
942 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
944 elsif($s->{op} =~ /^STORE_/)
946 $globalflags[$s->{a}] |= GLOBALFLAG_P
947 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
956 # parameter passing globals are only ever used in STORE_ instructions
957 for my $s(@{$progs->{statements}})
960 if $s->{op} =~ /^STORE_/;
962 my $c = checkop $s->{op};
968 unless defined $type;
971 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
973 $globalflags[$ofs] &= ~GLOBALFLAG_P;
975 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
977 $globalflags[$ofs] &= ~GLOBALFLAG_P;
978 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
979 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
984 my %offsets_saved = ();
985 for(@{$progs->{globaldefs}})
987 my $type = $_->{type};
988 my $name = $progs->{getstring}->($_->{s_name});
990 if $name eq 'IMMEDIATE' or $name =~ /^\./;
991 $_->{debugname} = $name
993 $globalflags[$_->{ofs}] |= GLOBALFLAG_D;
996 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
998 if(defined $_->{debugname})
1000 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
1005 for(@{$progs->{globaldefs}})
1007 my $type = $_->{type};
1008 for my $i(1..(typesize($type->{type})-1))
1011 if(!($globalflags[$_->{ofs}+$i] & GLOBALFLAG_D))
1013 print "Missing globaldef for a component@{[defined $_->{debugname} ? ' of ' . $_->{debugname} : '']} at $_->{ofs}+$i\n";
1019 ofs => $_->{ofs} + $i,
1020 debugname => defined $_->{debugname} ? $_->{debugname} . "[$i]" : undef
1023 # "saved" and "named" states hit adjacent globals too
1024 $globalflags[$_->{ofs}+$i] |= $globalflags[$_->{ofs}] & (GLOBALFLAG_S | GLOBALFLAG_N | GLOBALFLAG_D);
1027 push @{$progs->{globaldefs}}, @extradefs;
1029 my %offsets_initialized = ();
1030 for(0..(@{$progs->{globals}}-1))
1032 if($progs->{globals}[$_]{v}{int})
1034 $globalflags[$_] |= GLOBALFLAG_I;
1038 my @globaltypes = (undef) x @{$progs->{globals}};
1041 for(0..(@{$progs->{globals}}-1))
1044 if $_ < @{(DEFAULTGLOBALS)};
1045 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
1047 $globaltypes[$_] = "unused";
1049 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
1052 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1054 $globaltypes[$_] = "read_only";
1056 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1058 $globaltypes[$_] = "const";
1062 $globaltypes[$_] = "read_only";
1065 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1067 $globaltypes[$_] = "write_only";
1071 # now we know it is rw
1072 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1074 $globaltypes[$_] = "global";
1076 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1078 if($globalflags[$_] & GLOBALFLAG_P)
1080 $globaltypes[$_] = "OFS_PARM";
1082 elsif($globalflags[$_] & GLOBALFLAG_Q)
1084 $globaltypes[$_] = "uniquetemp";
1089 $globaltypes[$_] = "temp";
1093 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1095 $globaltypes[$_] = "not_saved";
1099 $globaltypes[$_] = "global";
1103 $progs->{temps} = \%istemp;
1106 my @globaldefs = (undef) x @{$progs->{globals}};
1107 for(@{$progs->{globaldefs}})
1109 $globaldefs[$_->{ofs}] //= $_
1110 if defined $_->{debugname};
1112 for(@{$progs->{globaldefs}})
1114 $globaldefs[$_->{ofs}] //= $_;
1116 for(0..(@{$progs->{globals}}-1))
1118 $globaldefs[$_] //= {
1125 for(0..(@{(DEFAULTGLOBALS)}-1))
1127 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1128 $globaltypes[$_] = 'defglobal';
1130 my %globaldefs_namecount = ();
1133 $_->{globaltype} = $globaltypes[$_->{ofs}];
1134 if(defined $_->{debugname})
1136 # already has debugname
1138 elsif($_->{globaltype} eq 'const')
1140 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1144 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1146 ++$globaldefs_namecount{$_->{debugname}};
1151 if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
1152 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1153 $_->{debugname} .= "\@$_->{ofs}";
1155 $progs->{globaldef_byoffset} = sub
1158 my $def = $globaldefs[$ofs];
1169 print STDERR "Parsing header...\n";
1170 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1172 print STDERR "Parsing strings...\n";
1173 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1176 my ($startpos) = @_;
1177 my $endpos = index $p{strings}, "\0", $startpos;
1178 return substr $p{strings}, $startpos, $endpos - $startpos;
1181 print STDERR "Parsing globals...\n";
1182 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1184 print STDERR "Parsing globaldefs...\n";
1185 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1187 print STDERR "Range checking globaldefs...\n";
1188 for(0 .. (@{$p{globaldefs}}-1))
1190 my $g = $p{globaldefs}[$_];
1191 die "Out of range name in globaldef $_"
1192 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1193 my $name = $p{getstring}->($g->{s_name});
1194 die "Out of range ofs $g->{ofs} in globaldef $_ (name: \"$name\")"
1195 if $g->{ofs} >= $p{globals};
1198 print STDERR "Parsing fielddefs...\n";
1199 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1201 print STDERR "Range checking fielddefs...\n";
1202 for(0 .. (@{$p{fielddefs}}-1))
1204 my $g = $p{fielddefs}[$_];
1205 die "Out of range name in fielddef $_"
1206 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1207 my $name = $p{getstring}->($g->{s_name});
1208 die "Out of range ofs $g->{ofs} in fielddef $_ (name: \"$name\")"
1209 if $g->{ofs} >= $p{header}{entityfields};
1212 print STDERR "Parsing statements...\n";
1213 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1215 print STDERR "Parsing functions...\n";
1216 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1218 print STDERR "Range checking functions...\n";
1219 for(0 .. (@{$p{functions}} - 1))
1221 my $f = $p{functions}[$_];
1222 die "Out of range name in function $_"
1223 if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
1224 my $name = $p{getstring}->($f->{s_name});
1225 die "Out of range file in function $_"
1226 if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
1227 my $file = $p{getstring}->($f->{s_file});
1228 die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1229 if $f->{first_statement} >= @{$p{statements}};
1230 die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1231 if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
1232 die "Out of range locals in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1233 if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} >= @{$p{globals}};
1234 if($f->{first_statement} >= 0)
1236 die "Out of range numparms $f->{numparms} in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1237 if $f->{numparms} < 0 || $f->{numparms} > 8;
1239 for(0..($f->{numparms}-1))
1241 die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1242 unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
1243 $totalparms += $f->{parm_size}[$_];
1245 die "Out of range parms in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1246 if $f->{locals} < 0 || $f->{parm_start} + $totalparms >= @{$p{globals}};
1250 print STDERR "Range checking statements...\n";
1251 for my $ip(0 .. (@{$p{statements}}-1))
1253 my $s = $p{statements}[$ip];
1254 my $c = checkop $s->{op};
1258 my $type = $c->{$_};
1260 unless defined $type;
1262 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1265 die "Out of range global offset in statement $ip - cannot continue"
1266 if $s->{$_} >= @{$p{globals}};
1268 elsif($type eq 'inglobalvec')
1273 die "Out of range global offset in statement $ip - cannot continue"
1274 if $s->{$_} >= @{$p{globals}};
1275 print "Potentially out of range global offset in statement $ip - may crash engines"
1276 if $s->{$_} >= @{$p{globals}}-2;
1280 die "Out of range global offset in statement $ip - cannot continue"
1281 if $s->{$_} >= @{$p{globals}}-2;
1284 elsif($type eq 'outglobal')
1287 die "Out of range global offset in statement $ip - cannot continue"
1288 if $s->{$_} >= @{$p{globals}};
1290 elsif($type eq 'outglobalvec')
1293 die "Out of range global offset in statement $ip - cannot continue"
1294 if $s->{$_} >= @{$p{globals}}-2;
1296 elsif($type eq 'ipoffset')
1298 die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
1299 if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
1304 print STDERR "Looking for error()...\n";
1305 $p{error_func} = {};
1306 for(@{$p{globaldefs}})
1309 if $p{getstring}($_->{s_name}) ne 'error';
1310 my $v = $p{globals}[$_->{ofs}]{v}{int};
1312 if $v <= 0 || $v >= @{$p{functions}};
1313 my $first = $p{functions}[$v]{first_statement};
1316 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1317 $p{error_func}{$_->{ofs}} = 1;
1320 print STDERR "Scanning functions...\n";
1321 for(@{$p{functions}})
1323 my $file = $p{getstring}->($_->{s_file});
1324 my $name = $p{getstring}->($_->{s_name});
1325 $name = "$file:$name"
1327 $_->{debugname} = $name;
1330 if $_->{first_statement} < 0;
1332 my %statements = ();
1335 my %globals_read = ();
1336 my %globals_written = ();
1337 my %globals_used = ();
1339 if($_->{first_statement} >= 0)
1341 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1344 my ($ip, $state, $s, $c) = @_;
1347 if(my $j = $c->{isjump})
1349 my $t = $ip + $s->{$j};
1350 $come_from{$t}{$ip} = $c->{isconditional};
1351 $go_to{$ip}{$t} = $c->{isconditional};
1354 for my $o(qw(a b c))
1363 $globals_read{$ofs}{$ip}{$o} = 1;
1364 $globals_used{$ofs} = 1;
1369 $globals_written{$ofs}{$ip}{$o} = 1;
1370 $globals_used{$ofs} = 1;
1373 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1377 elsif($type eq 'inglobalvec')
1383 elsif($type eq 'outglobal')
1387 elsif($type eq 'outglobalvec')
1399 $_->{statements} = \%statements;
1400 $_->{come_from} = \%come_from;
1401 $_->{go_to} = \%go_to;
1402 $_->{globals_read} = \%globals_read;
1403 $_->{globals_written} = \%globals_written;
1404 $_->{globals_used} = \%globals_used;
1406 # using this info, we could now identify basic blocks
1409 print STDERR "Detecting constants and temps, and naming...\n";
1410 detect_constants \%p;
1419 # what do we want to do?
1420 my $checkfunc = \&find_uninitialized_locals;
1421 if($ENV{DISASSEMBLE})
1423 $checkfunc = \&disassemble_function;
1425 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1427 $checkfunc->(\%p, $_);
1431 open my $fh, '<', $ARGV[0];