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'],
194 use constant LNOHEADER_T => [
197 [int => 'numglobaldefs'],
198 [int => 'numglobals'],
199 [int => 'numfielddefs'],
200 [int => 'numstatements'],
203 use constant LNO_T => [
209 my ($fh, $start, $len) = @_;
212 $len == read $fh, my $buf, $len
213 or die "short read from $start length $len (malformed progs header)";
217 sub parse_section($$$$$)
219 my ($fh, $struct, $start, $len, $cnt) = @_;
222 $itemlen += TYPES->{$_->[0]}->[1]
224 my $packspec = join '', map { TYPES->{$_->[0]}->[0]; } @$struct;
225 my @packnames = map { $_->[1]; } @$struct;
227 $len = $cnt * $itemlen
228 if not defined $len and defined $cnt;
229 $cnt = int($len / $itemlen)
230 if not defined $cnt and defined $len;
231 die "Invalid length specification"
232 unless defined $len and defined $cnt and $len == $cnt * $itemlen;
233 die "Invalid length specification in scalar context"
234 unless wantarray or $cnt == 1;
240 $itemlen == read $fh, my $buf, $itemlen
241 or die "short read from $start length $cnt * $itemlen $(malformed progs header)";
243 @h{@packnames} = unpack $packspec, $buf;
244 $h{$_->[1]} = TYPES->{$_->[0]}->[2]->($h{$_->[1]})
254 sub nfa_default_state_checker()
259 my ($ip, $state) = @_;
260 return $seen{"$ip $state"}++;
266 my ($progs, $ip, $state, $copy_handler, $state_checker, $instruction_handler) = @_;
268 my $statements = $progs->{statements};
273 no warnings 'recursion';
275 my ($ip, $state) = @_;
281 if $state_checker->($ip, $state);
283 my $s = $statements->[$ip];
284 my $c = checkop $s->{op};
286 if(($ret = $instruction_handler->($ip, $state, $s, $c)))
300 if $progs->{builtins}{error}{$func};
305 if($c->{isconditional})
309 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
317 $nfa->($ip+1, $copy_handler->($state));
318 $ip += $s->{$c->{isjump}};
323 $ip += $s->{$c->{isjump}};
335 $nfa->($ip, $copy_handler->($state));
338 sub get_constant($$$)
340 my ($progs, $g, $type) = @_;
342 if (!defined $type) {
345 if $g->{int} > 0 && $g->{int} < 8388608;
347 if $g->{int} > 0 && $g->{int} < length $progs->{strings};
350 return str($progs->{getstring}->($g->{int}))
351 if $type eq 'string';
354 return "'$g->{float} _ _'"
355 if $type eq 'vector';
356 return "entity $g->{int}"
357 if $type eq 'entity';
358 return ".$progs->{entityfieldnames}[$g->{int}][0]"
359 if $type eq 'field' and defined $progs->{entityfieldnames}[$g->{int}][0];
363 return "$type($g->{int})";
366 use constant PRE_MARK_STATEMENT => "";
367 use constant POST_MARK_STATEMENT => "";
368 use constant PRE_MARK_OPERAND => "*** ";
369 use constant POST_MARK_OPERAND => " ***";
371 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
372 use constant OPERAND_FORMAT => "%s";
373 use constant OPERAND_SEPARATOR => ", ";
374 use constant INSTRUCTION_SEPARATOR => "\n";
379 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
384 my ($progs, $func, $ip) = @_;
385 my $s = $func->{debugname};
387 my $column = $progs->{cno}[$ip]{v};
388 $s =~ s/:/:$column:/;
391 my $line = $progs->{lno}[$ip]{v};
397 sub disassemble_function($$;$)
399 my ($progs, $func, $highlight) = @_;
401 print "$func->{debugname}:\n";
403 if($func->{first_statement} < 0) # builtin
405 printf INSTRUCTION_FORMAT, '', '', '.BUILTIN';
406 printf OPERAND_FORMAT, -$func->{first_statement};
407 print INSTRUCTION_SEPARATOR;
411 my $initializer = sub
414 # TODO: Can we know its type?
415 my $g = get_constant($progs, $progs->{globals}[$ofs]{v}, undef);
420 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
421 printf OPERAND_FORMAT, "$func->{parm_start}";
422 print INSTRUCTION_SEPARATOR;
424 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
425 printf OPERAND_FORMAT, "$func->{locals}";
426 print INSTRUCTION_SEPARATOR;
428 my %override_locals = ();
429 my $p = $func->{parm_start};
430 for(0..($func->{numparms}-1))
432 $override_locals{$p} //= "argv_$_";
433 for my $comp(0..($func->{parm_size}[$_]-1))
435 $override_locals{$p} //= "argv_$_\[$comp]";
438 printf INSTRUCTION_FORMAT, '', '', '.ARG';
439 printf OPERAND_FORMAT, "argv_$_";
440 print OPERAND_SEPARATOR;
441 printf OPERAND_FORMAT, $func->{parm_size}[$_];
442 print INSTRUCTION_SEPARATOR;
444 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
447 if exists $override_locals{$_};
448 $override_locals{$_} = "local_$_";
450 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
451 printf OPERAND_FORMAT, "local_$_";
453 print INSTRUCTION_SEPARATOR;
459 return $override_locals{$ofs}
460 if exists $override_locals{$ofs};
461 my $def = $progs->{globaldef_byoffset}->($ofs);
462 return $def->{debugname};
467 my ($ip, $type, $operand) = @_;
468 if($type eq 'inglobal')
470 my $name = $getname->($operand);
471 printf OPERAND_FORMAT, "$name";
473 elsif($type eq 'outglobal')
475 my $name = $getname->($operand);
476 printf OPERAND_FORMAT, "&$name";
478 elsif($type eq 'inglobalvec')
480 my $name = $getname->($operand);
481 printf OPERAND_FORMAT, "$name\[\]";
483 elsif($type eq 'outglobalvec')
485 my $name = $getname->($operand);
486 printf OPERAND_FORMAT, "&$name\[\]";
488 elsif($type eq 'inglobalfunc')
490 my $name = $getname->($operand);
491 printf OPERAND_FORMAT, "$name()";
493 elsif($type eq 'ipoffset')
495 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
499 die "unknown type: $type";
503 my $statements = $func->{statements};
504 my $come_from = $func->{come_from};
507 for my $ip(sort { $a <=> $b } keys %$statements)
509 if($ip == $func->{first_statement})
511 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
512 print INSTRUCTION_SEPARATOR;
514 if(defined $ipprev && $ip != $ipprev + 1)
516 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
517 printf OPERAND_FORMAT, $ip - $ipprev - 1;
518 print INSTRUCTION_SEPARATOR;
520 if(my $cf = $come_from->{$ip})
522 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
524 for(sort { $a <=> $b } keys %$cf)
526 print OPERAND_SEPARATOR
528 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
530 print INSTRUCTION_SEPARATOR;
533 my $op = $progs->{statements}[$ip]{op};
534 my $ipt = $progs->{statements}[$ip];
535 my $opprop = checkop $op;
537 if($highlight and $highlight->{$ip})
539 for(values %{$highlight->{$ip}})
543 print PRE_MARK_STATEMENT;
544 printf INSTRUCTION_FORMAT, '', '<!>', '.WARN';
545 my $pos = debugpos $progs, $func, $ip;
546 printf OPERAND_FORMAT, "$_ (in $pos)";
547 print INSTRUCTION_SEPARATOR;
552 print PRE_MARK_STATEMENT
553 if $highlight and $highlight->{$ip};
555 my $showip = $opprop->{isjump};
556 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? '<!>' : '', $op;
562 if not defined $opprop->{$o};
563 print OPERAND_SEPARATOR
565 print PRE_MARK_OPERAND
566 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
567 $operand->($ip, $opprop->{$o}, $ipt->{$o});
568 print POST_MARK_OPERAND
569 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
572 print POST_MARK_STATEMENT
573 if $highlight and $highlight->{$ip};
575 print INSTRUCTION_SEPARATOR;
579 sub find_uninitialized_locals($$)
581 my ($progs, $func) = @_;
584 if $func->{first_statement} < 0; # builtin
586 print STDERR "Checking $func->{debugname}...\n";
588 my $p = $func->{parm_start};
589 for(0..($func->{numparms}-1))
591 $p += $func->{parm_size}[$_];
594 use constant WATCHME_R => 1;
595 use constant WATCHME_W => 2;
596 use constant WATCHME_X => 4;
597 use constant WATCHME_T => 8;
598 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
600 for(keys %{$progs->{temps}})
603 if exists $watchme{$_};
604 if($progs->{temps}{$_})
607 $watchme{$_} = WATCHME_T | WATCHME_X
612 $watchme{$_} = WATCHME_X
616 $watchme{$_} |= WATCHME_R
617 for keys %{$func->{globals_read}};
618 $watchme{$_} |= WATCHME_W
619 for keys %{$func->{globals_written}};
621 my %write_places = ();
622 for my $ofs(keys %{$func->{globals_written}})
625 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
626 for my $ip(keys %{$func->{globals_written}{$ofs}})
628 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
630 push @{$write_places{$ip}{$op}}, $ofs;
638 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
642 if not keys %watchme;
647 flags => $watchme{$_},
648 valid => [0, undef, undef]
652 # mark parameters as initialized
653 for($func->{parm_start} .. ($p-1))
655 $watchme{$_}{valid} = [1, undef, undef]
656 if defined $watchme{$_};
661 run_nfa $progs, $func->{first_statement}, \%watchme,
664 return { map { $_ => { %{$h->{$_}} } } keys %$h };
667 my ($ip, $state) = @_;
669 my $s = $ip_seen{$ip};
672 # if $state is stronger or equal to $s, return 1
676 if($state->{$_}{valid}[0] < $s->{$_})
678 # The current state is LESS valid than the previously run one. We NEED to run this.
679 # The saved state can safely become the intersection [citation needed].
682 $s->{$_} = $state->{$_}{valid}[0]
683 if $state->{$_}{valid}[0] < $s->{$_};
688 # if we get here, $state is stronger or equal. No need to try it.
693 # Never seen this IP yet.
694 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
699 my ($ip, $state, $s, $c) = @_;
702 # QCVM BUG: RETURN always takes vector, there is no float equivalent
703 my $return_hack = $c->{isreturn} // 0;
707 # COMPILER BUG of QCC: params are always copied using STORE_V
708 if($s->{b} >= 4 && $s->{b} < 28) # parameter range
716 ++$warned{$ip}{''}{"Invalid opcode"};
722 unless defined $type;
732 if not exists $state->{$ofs};
733 my $valid = $state->{$ofs}{valid};
736 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
737 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
739 ++$warned{$ip}{$_}{"Use of uninitialized value"};
742 elsif($valid->[0] < 0)
744 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
745 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
747 ++$warned{$ip}{$_}{"Use of temporary across CALL"};
753 if(defined $valid->[1])
755 delete $write_places{$valid->[1]}{$valid->[2]};
762 $state->{$ofs}{valid} = [1, $ip, $_]
763 if exists $state->{$ofs};
766 if($type eq 'inglobal' || $type eq 'inglobalfunc')
770 elsif($type eq 'inglobalvec')
776 elsif($type eq 'outglobal')
780 elsif($type eq 'outglobalvec')
786 elsif($type eq 'ipoffset')
788 ++$warned{$ip}{$_}{"Endless loop"}
790 ++$warned{$ip}{$_}{"No-operation jump"}
796 # builtin calls may clobber stuff
798 my $funcid = $progs->{globals}[$func]{v}{int};
799 my $funcobj = $progs->{functions}[$funcid];
800 if(!$funcobj || $funcobj->{first_statement} >= 0)
805 if($_->{flags} & WATCHME_T)
807 $_->{valid} = [-1, undef, undef];
816 for my $ip(keys %write_places)
818 for my $operand(keys %{$write_places{$ip}})
821 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
824 my %writeplace_seen = ();
825 run_nfa $progs, $ip+1, \%left,
832 my ($ip, $state) = @_;
833 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
837 my ($ip, $state, $s, $c) = @_;
842 unless defined $type;
845 if($type eq 'inglobal' || $type eq 'inglobalfunc')
850 return -1; # exit TOTALLY
853 elsif($type eq 'inglobalvec')
855 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
858 return -1; # exit TOTALLY
861 elsif($type eq 'outglobal')
863 delete $state->{$ofs};
867 elsif($type eq 'outglobalvec')
869 delete $state->{$ofs};
870 delete $state->{$ofs+1};
871 delete $state->{$ofs+2};
881 ++$warned{$ip}{$operand}{"Value is never used"};
887 run_nfa $progs, $func->{first_statement}, do { my $state = -1; \$state; },
890 my $state = ${$_[0]};
895 my ($ip, $state) = @_;
896 return $solid_seen{"$ip $$state"}++;
900 my ($ip, $state, $s, $c) = @_;
902 if($s->{op} eq 'ADDRESS')
904 my $field_ptr_ofs = $s->{b};
905 my $def = $progs->{globaldef_byoffset}->($field_ptr_ofs);
907 if (($def->{globaltype} eq 'read_only' || $def->{globaltype} eq 'const') &&
908 grep { $_ eq 'solid' } @{$progs->{entityfieldnames}[$progs->{globals}[$field_ptr_ofs]{v}{int}]})
910 # Taking address of 'solid' for subsequent write!
911 # TODO check if this address is then actually used in STOREP.
918 # TODO check if the entity passed is actually the one on which solid was set.
920 if ($progs->{builtins}{setmodel}{$func} || $progs->{builtins}{setmodelindex}{$func} || $progs->{builtins}{setorigin}{$func} || $progs->{builtins}{setsize}{$func})
930 ++$warned{$$state}{''}{"Changing .solid without setmodel/setmodelindex/setorigin/setsize breaks area grid linking in Quake [write is here]"};
931 ++$warned{$ip}{''}{"Changing .solid without setmodel/setmodelindex/setorigin/setsize breaks area grid linking in Quake [return is here]"};
938 disassemble_function($progs, $func, \%warned)
942 use constant DEFAULTGLOBALS => [
976 if($ofs < @{(DEFAULTGLOBALS)})
978 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
980 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
983 sub detect_constants($)
986 use constant GLOBALFLAG_R => 1; # read
987 use constant GLOBALFLAG_W => 2; # written
988 use constant GLOBALFLAG_S => 4; # saved
989 use constant GLOBALFLAG_I => 8; # initialized
990 use constant GLOBALFLAG_N => 16; # named
991 use constant GLOBALFLAG_Q => 32; # unique to function
992 use constant GLOBALFLAG_U => 64; # unused
993 use constant GLOBALFLAG_P => 128; # possibly parameter passing
994 use constant GLOBALFLAG_D => 256; # has a def
995 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x (@{$progs->{globals}} + 2);
997 for(@{$progs->{functions}})
999 for(keys %{$_->{globals_used}})
1001 if($globalflags[$_] & GLOBALFLAG_U)
1003 $globalflags[$_] &= ~GLOBALFLAG_U;
1005 elsif($globalflags[$_] & GLOBALFLAG_Q)
1007 $globalflags[$_] &= ~GLOBALFLAG_Q;
1010 $globalflags[$_] |= GLOBALFLAG_R
1011 for keys %{$_->{globals_read}};
1012 $globalflags[$_] |= GLOBALFLAG_W
1013 for keys %{$_->{globals_written}};
1015 if $_->{first_statement} < 0;
1016 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
1018 my $s = $progs->{statements}[$ip];
1019 if($s->{op} eq 'STORE_V')
1021 $globalflags[$s->{a}] |= GLOBALFLAG_P
1022 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
1023 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
1024 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
1025 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
1026 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
1028 elsif($s->{op} =~ /^STORE_/)
1030 $globalflags[$s->{a}] |= GLOBALFLAG_P
1031 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
1040 # parameter passing globals are only ever used in STORE_ instructions
1041 for my $s(@{$progs->{statements}})
1044 if $s->{op} =~ /^STORE_/;
1046 my $c = checkop $s->{op};
1050 my $type = $c->{$_};
1052 unless defined $type;
1055 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
1057 $globalflags[$ofs] &= ~GLOBALFLAG_P;
1059 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
1061 $globalflags[$ofs] &= ~GLOBALFLAG_P;
1062 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
1063 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
1068 my %offsets_saved = ();
1069 for(@{$progs->{globaldefs}})
1071 my $type = $_->{type};
1072 my $name = $progs->{getstring}->($_->{s_name});
1074 if $name eq 'IMMEDIATE'; # for fteqcc I had: or $name =~ /^\./;
1075 $_->{debugname} = $name
1077 $globalflags[$_->{ofs}] |= GLOBALFLAG_D;
1080 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
1082 if(defined $_->{debugname})
1084 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
1089 for(@{$progs->{globaldefs}})
1091 my $type = $_->{type};
1092 for my $i(1..(typesize($type->{type})-1))
1095 if(!($globalflags[$_->{ofs}+$i] & GLOBALFLAG_D))
1097 print "Missing globaldef for a component@{[defined $_->{debugname} ? ' of ' . $_->{debugname} : '']} at $_->{ofs}+$i\n";
1103 ofs => $_->{ofs} + $i,
1104 debugname => defined $_->{debugname} ? $_->{debugname} . "[$i]" : undef
1107 # "saved" and "named" states hit adjacent globals too
1108 $globalflags[$_->{ofs}+$i] |= $globalflags[$_->{ofs}] & (GLOBALFLAG_S | GLOBALFLAG_N | GLOBALFLAG_D);
1111 push @{$progs->{globaldefs}}, @extradefs;
1113 my %offsets_initialized = ();
1114 for(0..(@{$progs->{globals}}-1))
1116 if($progs->{globals}[$_]{v}{int})
1118 $globalflags[$_] |= GLOBALFLAG_I;
1122 my @globaltypes = (undef) x @{$progs->{globals}};
1125 for(0..(@{$progs->{globals}}-1))
1128 if $_ < @{(DEFAULTGLOBALS)};
1129 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
1131 $globaltypes[$_] = "unused";
1133 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
1136 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1138 $globaltypes[$_] = "read_only";
1140 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1142 $globaltypes[$_] = "const";
1146 $globaltypes[$_] = "read_only";
1149 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1151 $globaltypes[$_] = "write_only";
1155 # now we know it is rw
1156 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1158 $globaltypes[$_] = "global";
1160 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1162 if($globalflags[$_] & GLOBALFLAG_P)
1164 $globaltypes[$_] = "OFS_PARM";
1166 elsif($globalflags[$_] & GLOBALFLAG_Q)
1168 $globaltypes[$_] = "uniquetemp";
1173 $globaltypes[$_] = "temp";
1177 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1179 $globaltypes[$_] = "not_saved";
1183 $globaltypes[$_] = "global";
1187 $progs->{temps} = \%istemp;
1190 my @globaldefs = (undef) x @{$progs->{globals}};
1191 for(@{$progs->{globaldefs}})
1193 $globaldefs[$_->{ofs}] //= $_
1194 if defined $_->{debugname};
1196 for(@{$progs->{globaldefs}})
1198 $globaldefs[$_->{ofs}] //= $_;
1200 for(0..(@{$progs->{globals}}-1))
1202 $globaldefs[$_] //= {
1209 for(0..(@{(DEFAULTGLOBALS)}-1))
1211 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1212 $globaltypes[$_] = 'defglobal';
1214 my %globaldefs_namecount = ();
1217 $_->{globaltype} = $globaltypes[$_->{ofs}];
1218 if(defined $_->{debugname})
1220 # already has debugname
1222 elsif($_->{globaltype} eq 'const')
1224 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v}, $_->{type}{type});
1228 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1230 ++$globaldefs_namecount{$_->{debugname}};
1235 if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
1236 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1237 $_->{debugname} .= "\@$_->{ofs}";
1239 $progs->{globaldef_byoffset} = sub
1242 my $def = $globaldefs[$ofs];
1249 my ($fh, $lnofh) = @_;
1253 print STDERR "Parsing header...\n";
1254 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1256 if (defined $lnofh) {
1257 print STDERR "Parsing LNO...\n";
1258 my $lnoheader = parse_section $lnofh, LNOHEADER_T, 0, undef, 1;
1261 if $lnoheader->{lnotype} != unpack 'V', 'LNOF';
1263 if $lnoheader->{version} != 1;
1264 die "Not same count of globaldefs"
1265 if $lnoheader->{numglobaldefs} != $p{header}{numglobaldefs};
1266 die "Not same count of globals"
1267 if $lnoheader->{numglobals} != $p{header}{numglobals};
1268 die "Not same count of fielddefs"
1269 if $lnoheader->{numfielddefs} != $p{header}{numfielddefs};
1270 die "Not same count of statements"
1271 if $lnoheader->{numstatements} != $p{header}{numstatements};
1272 $p{lno} = [parse_section $lnofh, LNO_T, 24, undef, $lnoheader->{numstatements}];
1274 $p{lno} = [parse_section $lnofh, LNO_T, 24, undef, $lnoheader->{numstatements} * 2];
1275 $p{cno} = [splice $p{lno}, $lnoheader->{numstatements}];
1276 print STDERR "Cool, this LNO even has column number info!\n";
1278 } or warn "Skipping LNO: $@";
1281 print STDERR "Parsing strings...\n";
1282 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1285 my ($startpos) = @_;
1286 my $endpos = index $p{strings}, "\0", $startpos;
1287 return substr $p{strings}, $startpos, $endpos - $startpos;
1290 print STDERR "Parsing globals...\n";
1291 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1293 print STDERR "Parsing globaldefs...\n";
1294 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1296 print STDERR "Range checking globaldefs...\n";
1297 for(0 .. (@{$p{globaldefs}}-1))
1299 my $g = $p{globaldefs}[$_];
1300 die "Out of range name in globaldef $_"
1301 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1302 my $name = $p{getstring}->($g->{s_name});
1303 die "Out of range ofs $g->{ofs} in globaldef $_ (name: \"$name\")"
1304 if $g->{ofs} >= $p{globals};
1307 print STDERR "Parsing fielddefs...\n";
1308 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1310 print STDERR "Range checking fielddefs...\n";
1311 for(0 .. (@{$p{fielddefs}}-1))
1313 my $g = $p{fielddefs}[$_];
1314 die "Out of range name in fielddef $_"
1315 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1316 my $name = $p{getstring}->($g->{s_name});
1317 die "Out of range ofs $g->{ofs} in fielddef $_ (name: \"$name\")"
1318 if $g->{ofs} >= $p{header}{entityfields};
1319 push @{$p{entityfieldnames}[$g->{ofs}]}, $name;
1322 print STDERR "Parsing statements...\n";
1323 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1325 print STDERR "Parsing functions...\n";
1326 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1328 print STDERR "Range checking functions...\n";
1329 for(0 .. (@{$p{functions}} - 1))
1331 my $f = $p{functions}[$_];
1332 die "Out of range name in function $_"
1333 if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
1334 my $name = $p{getstring}->($f->{s_name});
1335 die "Out of range file in function $_"
1336 if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
1337 my $file = $p{getstring}->($f->{s_file});
1338 die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1339 if $f->{first_statement} >= @{$p{statements}};
1340 if($f->{first_statement} >= 0)
1342 die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1343 if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
1344 die "Out of range locals in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1345 if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} > @{$p{globals}};
1346 die "Out of range numparms $f->{numparms} in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1347 if $f->{numparms} < 0 || $f->{numparms} > 8;
1349 for(0..($f->{numparms}-1))
1351 die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1352 unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
1353 $totalparms += $f->{parm_size}[$_];
1355 die "Out of range parms in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1356 if $f->{parm_start} + $totalparms > @{$p{globals}};
1357 die "More parms than locals in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1358 if $totalparms > $f->{locals};
1362 print STDERR "Range checking statements...\n";
1363 for my $ip(0 .. (@{$p{statements}}-1))
1365 my $s = $p{statements}[$ip];
1366 my $c = checkop $s->{op};
1370 my $type = $c->{$_};
1372 unless defined $type;
1374 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1377 die "Out of range global offset in statement $ip - cannot continue"
1378 if $s->{$_} >= @{$p{globals}};
1380 elsif($type eq 'inglobalvec')
1385 die "Out of range global offset in statement $ip - cannot continue"
1386 if $s->{$_} >= @{$p{globals}};
1387 print "Potentially out of range global offset in statement $ip - may crash engines"
1388 if $s->{$_} >= @{$p{globals}}-2;
1392 die "Out of range global offset in statement $ip - cannot continue"
1393 if $s->{$_} >= @{$p{globals}}-2;
1396 elsif($type eq 'outglobal')
1399 die "Out of range global offset in statement $ip - cannot continue"
1400 if $s->{$_} >= @{$p{globals}};
1402 elsif($type eq 'outglobalvec')
1405 die "Out of range global offset in statement $ip - cannot continue"
1406 if $s->{$_} >= @{$p{globals}}-2;
1408 elsif($type eq 'ipoffset')
1410 die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
1411 if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
1416 print STDERR "Looking for error(), setmodel(), setmodelindex(), setorigin(), setsize()...\n";
1417 $p{builtins} = { error => {}, setmodel => {}, setmodelindex => {}, setorigin => {}, setsize => {} };
1418 for(@{$p{globaldefs}})
1420 my $name = $p{getstring}($_->{s_name});
1422 if not exists $p{builtins}{$name};
1423 my $v = $p{globals}[$_->{ofs}]{v}{int};
1425 if $v <= 0 || $v >= @{$p{functions}};
1426 my $first = $p{functions}[$v]{first_statement};
1429 print STDERR "Detected $name() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1430 $p{builtins}{$name}{$_->{ofs}} = 1;
1433 print STDERR "Scanning functions...\n";
1434 for(@{$p{functions}})
1436 my $file = $p{getstring}->($_->{s_file});
1437 my $name = $p{getstring}->($_->{s_name});
1438 $name = "$file:$name"
1440 $_->{debugname} = $name;
1443 if $_->{first_statement} < 0;
1445 my %statements = ();
1448 my %globals_read = ();
1449 my %globals_written = ();
1450 my %globals_used = ();
1452 if($_->{first_statement} >= 0)
1454 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1457 my ($ip, $state, $s, $c) = @_;
1460 if(my $j = $c->{isjump})
1462 my $t = $ip + $s->{$j};
1463 $come_from{$t}{$ip} = $c->{isconditional};
1464 $go_to{$ip}{$t} = $c->{isconditional};
1467 for my $o(qw(a b c))
1476 $globals_read{$ofs}{$ip}{$o} = 1;
1477 $globals_used{$ofs} = 1;
1482 $globals_written{$ofs}{$ip}{$o} = 1;
1483 $globals_used{$ofs} = 1;
1486 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1490 elsif($type eq 'inglobalvec')
1496 elsif($type eq 'outglobal')
1500 elsif($type eq 'outglobalvec')
1512 $_->{statements} = \%statements;
1513 $_->{come_from} = \%come_from;
1514 $_->{go_to} = \%go_to;
1515 $_->{globals_read} = \%globals_read;
1516 $_->{globals_written} = \%globals_written;
1517 $_->{globals_used} = \%globals_used;
1519 # using this info, we could now identify basic blocks
1522 print STDERR "Detecting constants and temps, and naming...\n";
1523 detect_constants \%p;
1532 # what do we want to do?
1533 my $checkfunc = \&find_uninitialized_locals;
1534 if($ENV{DISASSEMBLE})
1536 $checkfunc = \&disassemble_function;
1538 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1540 $checkfunc->(\%p, $_);
1544 for my $progs (@ARGV) {
1545 my $lno = "$progs.lno";
1546 $lno =~ s/\.dat\.lno$/.lno/;
1548 open my $fh, '<', $progs
1549 or die "$progs: $!";
1551 open my $lnofh, '<', $lno
1554 parse_progs $fh, $lnofh;