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)))
299 my $funcid = $progs->{globals}[$func]{v}{int};
301 if $progs->{builtins}{error}{$funcid};
306 if($c->{isconditional})
310 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
318 $nfa->($ip+1, $copy_handler->($state));
319 $ip += $s->{$c->{isjump}};
324 $ip += $s->{$c->{isjump}};
336 $nfa->($ip, $copy_handler->($state));
339 sub get_constant($$$)
341 my ($progs, $g, $type) = @_;
343 if (!defined $type) {
346 if $g->{int} > 0 && $g->{int} < 8388608;
348 if $g->{int} > 0 && $g->{int} < length $progs->{strings};
351 return str($progs->{getstring}->($g->{int}))
352 if $type eq 'string';
355 return "'$g->{float} _ _'"
356 if $type eq 'vector';
357 return "entity $g->{int}"
358 if $type eq 'entity';
359 return ".$progs->{entityfieldnames}[$g->{int}]"
360 if $type eq 'field' and defined $progs->{entityfieldnames}[$g->{int}];
364 return "$type($g->{int})";
367 use constant PRE_MARK_STATEMENT => "";
368 use constant POST_MARK_STATEMENT => "";
369 use constant PRE_MARK_OPERAND => "*** ";
370 use constant POST_MARK_OPERAND => " ***";
372 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
373 use constant OPERAND_FORMAT => "%s";
374 use constant OPERAND_SEPARATOR => ", ";
375 use constant INSTRUCTION_SEPARATOR => "\n";
380 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
385 my ($progs, $func, $ip) = @_;
386 my $s = $func->{debugname};
388 my $column = $progs->{cno}[$ip]{v};
389 $s =~ s/:/:$column:/;
392 my $line = $progs->{lno}[$ip]{v};
398 sub disassemble_function($$;$)
400 my ($progs, $func, $highlight) = @_;
402 print "$func->{debugname}:\n";
404 if($func->{first_statement} < 0) # builtin
406 printf INSTRUCTION_FORMAT, '', '', '.BUILTIN';
407 printf OPERAND_FORMAT, -$func->{first_statement};
408 print INSTRUCTION_SEPARATOR;
412 my $initializer = sub
415 # TODO: Can we know its type?
416 my $g = get_constant($progs, $progs->{globals}[$ofs]{v}, undef);
421 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
422 printf OPERAND_FORMAT, "$func->{parm_start}";
423 print INSTRUCTION_SEPARATOR;
425 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
426 printf OPERAND_FORMAT, "$func->{locals}";
427 print INSTRUCTION_SEPARATOR;
429 my %override_locals = ();
430 my $p = $func->{parm_start};
431 for(0..($func->{numparms}-1))
433 $override_locals{$p} //= "argv_$_";
434 for my $comp(0..($func->{parm_size}[$_]-1))
436 $override_locals{$p} //= "argv_$_\[$comp]";
439 printf INSTRUCTION_FORMAT, '', '', '.ARG';
440 printf OPERAND_FORMAT, "argv_$_";
441 print OPERAND_SEPARATOR;
442 printf OPERAND_FORMAT, $func->{parm_size}[$_];
443 print INSTRUCTION_SEPARATOR;
445 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
448 if exists $override_locals{$_};
449 $override_locals{$_} = "local_$_";
451 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
452 printf OPERAND_FORMAT, "local_$_";
454 print INSTRUCTION_SEPARATOR;
460 return $override_locals{$ofs}
461 if exists $override_locals{$ofs};
462 my $def = $progs->{globaldef_byoffset}->($ofs);
463 return $def->{debugname};
468 my ($ip, $type, $operand) = @_;
469 if($type eq 'inglobal')
471 my $name = $getname->($operand);
472 printf OPERAND_FORMAT, "$name";
474 elsif($type eq 'outglobal')
476 my $name = $getname->($operand);
477 printf OPERAND_FORMAT, "&$name";
479 elsif($type eq 'inglobalvec')
481 my $name = $getname->($operand);
482 printf OPERAND_FORMAT, "$name\[\]";
484 elsif($type eq 'outglobalvec')
486 my $name = $getname->($operand);
487 printf OPERAND_FORMAT, "&$name\[\]";
489 elsif($type eq 'inglobalfunc')
491 my $name = $getname->($operand);
492 printf OPERAND_FORMAT, "$name()";
494 elsif($type eq 'ipoffset')
496 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
500 die "unknown type: $type";
504 my $statements = $func->{statements};
505 my $come_from = $func->{come_from};
508 for my $ip(sort { $a <=> $b } keys %$statements)
510 if($ip == $func->{first_statement})
512 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
513 print INSTRUCTION_SEPARATOR;
515 if(defined $ipprev && $ip != $ipprev + 1)
517 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
518 printf OPERAND_FORMAT, $ip - $ipprev - 1;
519 print INSTRUCTION_SEPARATOR;
521 if(my $cf = $come_from->{$ip})
523 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
525 for(sort { $a <=> $b } keys %$cf)
527 print OPERAND_SEPARATOR
529 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
531 print INSTRUCTION_SEPARATOR;
534 my $op = $progs->{statements}[$ip]{op};
535 my $ipt = $progs->{statements}[$ip];
536 my $opprop = checkop $op;
538 if($highlight and $highlight->{$ip})
540 for(values %{$highlight->{$ip}})
544 print PRE_MARK_STATEMENT;
545 printf INSTRUCTION_FORMAT, '', '<!>', '.WARN';
546 my $pos = debugpos $progs, $func, $ip;
547 printf OPERAND_FORMAT, "$_ (in $pos)";
548 print INSTRUCTION_SEPARATOR;
553 print PRE_MARK_STATEMENT
554 if $highlight and $highlight->{$ip};
556 my $showip = $opprop->{isjump};
557 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? '<!>' : '', $op;
563 if not defined $opprop->{$o};
564 print OPERAND_SEPARATOR
566 print PRE_MARK_OPERAND
567 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
568 $operand->($ip, $opprop->{$o}, $ipt->{$o});
569 print POST_MARK_OPERAND
570 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
573 print POST_MARK_STATEMENT
574 if $highlight and $highlight->{$ip};
576 print INSTRUCTION_SEPARATOR;
580 sub find_uninitialized_locals($$)
582 my ($progs, $func) = @_;
585 if $func->{first_statement} < 0; # builtin
587 print STDERR "Checking $func->{debugname}...\n";
589 my $p = $func->{parm_start};
590 for(0..($func->{numparms}-1))
592 $p += $func->{parm_size}[$_];
595 use constant WATCHME_R => 1;
596 use constant WATCHME_W => 2;
597 use constant WATCHME_X => 4;
598 use constant WATCHME_T => 8;
599 my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
601 for(keys %{$progs->{temps}})
604 if exists $watchme{$_};
605 if($progs->{temps}{$_})
608 $watchme{$_} = WATCHME_T | WATCHME_X
613 $watchme{$_} = WATCHME_X
617 $watchme{$_} |= WATCHME_R
618 for keys %{$func->{globals_read}};
619 $watchme{$_} |= WATCHME_W
620 for keys %{$func->{globals_written}};
622 my %write_places = ();
623 for my $ofs(keys %{$func->{globals_written}})
626 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
627 for my $ip(keys %{$func->{globals_written}{$ofs}})
629 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
631 push @{$write_places{$ip}{$op}}, $ofs;
639 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
643 if not keys %watchme;
648 flags => $watchme{$_},
649 valid => [0, undef, undef]
653 # mark parameters as initialized
654 for($func->{parm_start} .. ($p-1))
656 $watchme{$_}{valid} = [1, undef, undef]
657 if defined $watchme{$_};
662 run_nfa $progs, $func->{first_statement}, \%watchme,
665 return { map { $_ => { %{$h->{$_}} } } keys %$h };
668 my ($ip, $state) = @_;
670 my $s = $ip_seen{$ip};
673 # if $state is stronger or equal to $s, return 1
677 if($state->{$_}{valid}[0] < $s->{$_})
679 # The current state is LESS valid than the previously run one. We NEED to run this.
680 # The saved state can safely become the intersection [citation needed].
683 $s->{$_} = $state->{$_}{valid}[0]
684 if $state->{$_}{valid}[0] < $s->{$_};
689 # if we get here, $state is stronger or equal. No need to try it.
694 # Never seen this IP yet.
695 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
700 my ($ip, $state, $s, $c) = @_;
703 # QCVM BUG: RETURN always takes vector, there is no float equivalent
704 my $return_hack = $c->{isreturn} // 0;
708 # COMPILER BUG of QCC: params are always copied using STORE_V
709 if($s->{b} >= 4 && $s->{b} < 28) # parameter range
717 ++$warned{$ip}{''}{"Invalid opcode"};
723 unless defined $type;
733 if not exists $state->{$ofs};
734 my $valid = $state->{$ofs}{valid};
737 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
738 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
740 ++$warned{$ip}{$_}{"Use of uninitialized value"};
743 elsif($valid->[0] < 0)
745 # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
746 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
748 ++$warned{$ip}{$_}{"Use of temporary across CALL"};
754 if(defined $valid->[1])
756 delete $write_places{$valid->[1]}{$valid->[2]};
763 $state->{$ofs}{valid} = [1, $ip, $_]
764 if exists $state->{$ofs};
767 if($type eq 'inglobal' || $type eq 'inglobalfunc')
771 elsif($type eq 'inglobalvec')
777 elsif($type eq 'outglobal')
781 elsif($type eq 'outglobalvec')
787 elsif($type eq 'ipoffset')
789 ++$warned{$ip}{$_}{"Endless loop"}
791 ++$warned{$ip}{$_}{"No-operation jump"}
797 # builtin calls may clobber stuff
799 my $funcid = $progs->{globals}[$func]{v}{int};
800 my $funcobj = $progs->{functions}[$funcid];
801 if(!$funcobj || $funcobj->{first_statement} >= 0)
806 if($_->{flags} & WATCHME_T)
808 $_->{valid} = [-1, undef, undef];
817 for my $ip(keys %write_places)
819 for my $operand(keys %{$write_places{$ip}})
822 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
825 my %writeplace_seen = ();
826 run_nfa $progs, $ip+1, \%left,
833 my ($ip, $state) = @_;
834 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
838 my ($ip, $state, $s, $c) = @_;
843 unless defined $type;
846 if($type eq 'inglobal' || $type eq 'inglobalfunc')
851 return -1; # exit TOTALLY
854 elsif($type eq 'inglobalvec')
856 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
859 return -1; # exit TOTALLY
862 elsif($type eq 'outglobal')
864 delete $state->{$ofs};
868 elsif($type eq 'outglobalvec')
870 delete $state->{$ofs};
871 delete $state->{$ofs+1};
872 delete $state->{$ofs+2};
882 ++$warned{$ip}{$operand}{"Value is never used"};
887 disassemble_function($progs, $func, \%warned)
891 use constant DEFAULTGLOBALS => [
925 if($ofs < @{(DEFAULTGLOBALS)})
927 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
929 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
932 sub detect_constants($)
935 use constant GLOBALFLAG_R => 1; # read
936 use constant GLOBALFLAG_W => 2; # written
937 use constant GLOBALFLAG_S => 4; # saved
938 use constant GLOBALFLAG_I => 8; # initialized
939 use constant GLOBALFLAG_N => 16; # named
940 use constant GLOBALFLAG_Q => 32; # unique to function
941 use constant GLOBALFLAG_U => 64; # unused
942 use constant GLOBALFLAG_P => 128; # possibly parameter passing
943 use constant GLOBALFLAG_D => 256; # has a def
944 my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x (@{$progs->{globals}} + 2);
946 for(@{$progs->{functions}})
948 for(keys %{$_->{globals_used}})
950 if($globalflags[$_] & GLOBALFLAG_U)
952 $globalflags[$_] &= ~GLOBALFLAG_U;
954 elsif($globalflags[$_] & GLOBALFLAG_Q)
956 $globalflags[$_] &= ~GLOBALFLAG_Q;
959 $globalflags[$_] |= GLOBALFLAG_R
960 for keys %{$_->{globals_read}};
961 $globalflags[$_] |= GLOBALFLAG_W
962 for keys %{$_->{globals_written}};
964 if $_->{first_statement} < 0;
965 for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
967 my $s = $progs->{statements}[$ip];
968 if($s->{op} eq 'STORE_V')
970 $globalflags[$s->{a}] |= GLOBALFLAG_P
971 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
972 $globalflags[$s->{a}+1] |= GLOBALFLAG_P
973 if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
974 $globalflags[$s->{a}+2] |= GLOBALFLAG_P
975 if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
977 elsif($s->{op} =~ /^STORE_/)
979 $globalflags[$s->{a}] |= GLOBALFLAG_P
980 if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
989 # parameter passing globals are only ever used in STORE_ instructions
990 for my $s(@{$progs->{statements}})
993 if $s->{op} =~ /^STORE_/;
995 my $c = checkop $s->{op};
1001 unless defined $type;
1004 if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
1006 $globalflags[$ofs] &= ~GLOBALFLAG_P;
1008 if($type eq 'inglobalvec' || $type eq 'outglobalvec')
1010 $globalflags[$ofs] &= ~GLOBALFLAG_P;
1011 $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
1012 $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
1017 my %offsets_saved = ();
1018 for(@{$progs->{globaldefs}})
1020 my $type = $_->{type};
1021 my $name = $progs->{getstring}->($_->{s_name});
1023 if $name eq 'IMMEDIATE'; # for fteqcc I had: or $name =~ /^\./;
1024 $_->{debugname} = $name
1026 $globalflags[$_->{ofs}] |= GLOBALFLAG_D;
1029 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
1031 if(defined $_->{debugname})
1033 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
1038 for(@{$progs->{globaldefs}})
1040 my $type = $_->{type};
1041 for my $i(1..(typesize($type->{type})-1))
1044 if(!($globalflags[$_->{ofs}+$i] & GLOBALFLAG_D))
1046 print "Missing globaldef for a component@{[defined $_->{debugname} ? ' of ' . $_->{debugname} : '']} at $_->{ofs}+$i\n";
1052 ofs => $_->{ofs} + $i,
1053 debugname => defined $_->{debugname} ? $_->{debugname} . "[$i]" : undef
1056 # "saved" and "named" states hit adjacent globals too
1057 $globalflags[$_->{ofs}+$i] |= $globalflags[$_->{ofs}] & (GLOBALFLAG_S | GLOBALFLAG_N | GLOBALFLAG_D);
1060 push @{$progs->{globaldefs}}, @extradefs;
1062 my %offsets_initialized = ();
1063 for(0..(@{$progs->{globals}}-1))
1065 if($progs->{globals}[$_]{v}{int})
1067 $globalflags[$_] |= GLOBALFLAG_I;
1071 my @globaltypes = (undef) x @{$progs->{globals}};
1074 for(0..(@{$progs->{globals}}-1))
1077 if $_ < @{(DEFAULTGLOBALS)};
1078 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
1080 $globaltypes[$_] = "unused";
1082 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
1085 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1087 $globaltypes[$_] = "read_only";
1089 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
1091 $globaltypes[$_] = "const";
1095 $globaltypes[$_] = "read_only";
1098 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
1100 $globaltypes[$_] = "write_only";
1104 # now we know it is rw
1105 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
1107 $globaltypes[$_] = "global";
1109 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
1111 if($globalflags[$_] & GLOBALFLAG_P)
1113 $globaltypes[$_] = "OFS_PARM";
1115 elsif($globalflags[$_] & GLOBALFLAG_Q)
1117 $globaltypes[$_] = "uniquetemp";
1122 $globaltypes[$_] = "temp";
1126 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1128 $globaltypes[$_] = "not_saved";
1132 $globaltypes[$_] = "global";
1136 $progs->{temps} = \%istemp;
1139 my @globaldefs = (undef) x @{$progs->{globals}};
1140 for(@{$progs->{globaldefs}})
1142 $globaldefs[$_->{ofs}] //= $_
1143 if defined $_->{debugname};
1145 for(@{$progs->{globaldefs}})
1147 $globaldefs[$_->{ofs}] //= $_;
1149 for(0..(@{$progs->{globals}}-1))
1151 $globaldefs[$_] //= {
1158 for(0..(@{(DEFAULTGLOBALS)}-1))
1160 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1161 $globaltypes[$_] = 'defglobal';
1163 my %globaldefs_namecount = ();
1166 $_->{globaltype} = $globaltypes[$_->{ofs}];
1167 if(defined $_->{debugname})
1169 # already has debugname
1171 elsif($_->{globaltype} eq 'const')
1173 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v}, $_->{type}{type});
1177 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1179 ++$globaldefs_namecount{$_->{debugname}};
1184 if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
1185 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1186 $_->{debugname} .= "\@$_->{ofs}";
1188 $progs->{globaldef_byoffset} = sub
1191 my $def = $globaldefs[$ofs];
1198 my ($fh, $lnofh) = @_;
1202 print STDERR "Parsing header...\n";
1203 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1205 if (defined $lnofh) {
1206 print STDERR "Parsing LNO...\n";
1207 my $lnoheader = parse_section $lnofh, LNOHEADER_T, 0, undef, 1;
1210 if $lnoheader->{lnotype} != unpack 'V', 'LNOF';
1212 if $lnoheader->{version} != 1;
1213 die "Not same count of globaldefs"
1214 if $lnoheader->{numglobaldefs} != $p{header}{numglobaldefs};
1215 die "Not same count of globals"
1216 if $lnoheader->{numglobals} != $p{header}{numglobals};
1217 die "Not same count of fielddefs"
1218 if $lnoheader->{numfielddefs} != $p{header}{numfielddefs};
1219 die "Not same count of statements"
1220 if $lnoheader->{numstatements} != $p{header}{numstatements};
1221 $p{lno} = [parse_section $lnofh, LNO_T, 24, undef, $lnoheader->{numstatements}];
1223 $p{lno} = [parse_section $lnofh, LNO_T, 24, undef, $lnoheader->{numstatements} * 2];
1224 $p{cno} = [splice $p{lno}, $lnoheader->{numstatements}];
1225 print STDERR "Cool, this LNO even has column number info!\n";
1227 } or warn "Skipping LNO: $@";
1230 print STDERR "Parsing strings...\n";
1231 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1234 my ($startpos) = @_;
1235 my $endpos = index $p{strings}, "\0", $startpos;
1236 return substr $p{strings}, $startpos, $endpos - $startpos;
1239 print STDERR "Parsing globals...\n";
1240 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1242 print STDERR "Parsing globaldefs...\n";
1243 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1245 print STDERR "Range checking globaldefs...\n";
1246 for(0 .. (@{$p{globaldefs}}-1))
1248 my $g = $p{globaldefs}[$_];
1249 die "Out of range name in globaldef $_"
1250 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1251 my $name = $p{getstring}->($g->{s_name});
1252 die "Out of range ofs $g->{ofs} in globaldef $_ (name: \"$name\")"
1253 if $g->{ofs} >= $p{globals};
1256 print STDERR "Parsing fielddefs...\n";
1257 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1259 print STDERR "Range checking fielddefs...\n";
1260 for(0 .. (@{$p{fielddefs}}-1))
1262 my $g = $p{fielddefs}[$_];
1263 die "Out of range name in fielddef $_"
1264 if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
1265 my $name = $p{getstring}->($g->{s_name});
1266 die "Out of range ofs $g->{ofs} in fielddef $_ (name: \"$name\")"
1267 if $g->{ofs} >= $p{header}{entityfields};
1268 #warn "Duplicate fielddef for ofs $g->{ofs} in fielddef $_ (name: \"$name\")"
1269 # if exists $p{entityfieldnames}[$g->{ofs}];
1270 $p{entityfieldnames}[$g->{ofs}] = $name;
1273 print STDERR "Parsing statements...\n";
1274 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1276 print STDERR "Parsing functions...\n";
1277 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1279 print STDERR "Range checking functions...\n";
1280 for(0 .. (@{$p{functions}} - 1))
1282 my $f = $p{functions}[$_];
1283 die "Out of range name in function $_"
1284 if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
1285 my $name = $p{getstring}->($f->{s_name});
1286 die "Out of range file in function $_"
1287 if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
1288 my $file = $p{getstring}->($f->{s_file});
1289 die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1290 if $f->{first_statement} >= @{$p{statements}};
1291 if($f->{first_statement} >= 0)
1293 die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1294 if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
1295 die "Out of range locals in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1296 if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} > @{$p{globals}};
1297 die "Out of range numparms $f->{numparms} in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1298 if $f->{numparms} < 0 || $f->{numparms} > 8;
1300 for(0..($f->{numparms}-1))
1302 die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1303 unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
1304 $totalparms += $f->{parm_size}[$_];
1306 die "Out of range parms in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1307 if $f->{parm_start} + $totalparms > @{$p{globals}};
1308 die "More parms than locals in function $_ (name: \"$name\", file: \"$file\", first statement: $f->{first_statement})"
1309 if $totalparms > $f->{locals};
1313 print STDERR "Range checking statements...\n";
1314 for my $ip(0 .. (@{$p{statements}}-1))
1316 my $s = $p{statements}[$ip];
1317 my $c = checkop $s->{op};
1321 my $type = $c->{$_};
1323 unless defined $type;
1325 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1328 die "Out of range global offset in statement $ip - cannot continue"
1329 if $s->{$_} >= @{$p{globals}};
1331 elsif($type eq 'inglobalvec')
1336 die "Out of range global offset in statement $ip - cannot continue"
1337 if $s->{$_} >= @{$p{globals}};
1338 print "Potentially out of range global offset in statement $ip - may crash engines"
1339 if $s->{$_} >= @{$p{globals}}-2;
1343 die "Out of range global offset in statement $ip - cannot continue"
1344 if $s->{$_} >= @{$p{globals}}-2;
1347 elsif($type eq 'outglobal')
1350 die "Out of range global offset in statement $ip - cannot continue"
1351 if $s->{$_} >= @{$p{globals}};
1353 elsif($type eq 'outglobalvec')
1356 die "Out of range global offset in statement $ip - cannot continue"
1357 if $s->{$_} >= @{$p{globals}}-2;
1359 elsif($type eq 'ipoffset')
1361 die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
1362 if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
1367 print STDERR "Looking for error(), setmodel(), setsize()...\n";
1368 $p{builtins} = { error => {}, setmodel => {}, setsize => {} };
1369 for(@{$p{globaldefs}})
1371 my $name = $p{getstring}($_->{s_name});
1373 if not exists $p{builtins}{$name};
1374 my $v = $p{globals}[$_->{ofs}]{v}{int};
1376 if $v <= 0 || $v >= @{$p{functions}};
1377 my $first = $p{functions}[$v]{first_statement};
1380 print STDERR "Detected $name() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1381 $p{builtins}{$name}{$_->{ofs}} = 1;
1384 print STDERR "Scanning functions...\n";
1385 for(@{$p{functions}})
1387 my $file = $p{getstring}->($_->{s_file});
1388 my $name = $p{getstring}->($_->{s_name});
1389 $name = "$file:$name"
1391 $_->{debugname} = $name;
1394 if $_->{first_statement} < 0;
1396 my %statements = ();
1399 my %globals_read = ();
1400 my %globals_written = ();
1401 my %globals_used = ();
1403 if($_->{first_statement} >= 0)
1405 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1408 my ($ip, $state, $s, $c) = @_;
1411 if(my $j = $c->{isjump})
1413 my $t = $ip + $s->{$j};
1414 $come_from{$t}{$ip} = $c->{isconditional};
1415 $go_to{$ip}{$t} = $c->{isconditional};
1418 for my $o(qw(a b c))
1427 $globals_read{$ofs}{$ip}{$o} = 1;
1428 $globals_used{$ofs} = 1;
1433 $globals_written{$ofs}{$ip}{$o} = 1;
1434 $globals_used{$ofs} = 1;
1437 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1441 elsif($type eq 'inglobalvec')
1447 elsif($type eq 'outglobal')
1451 elsif($type eq 'outglobalvec')
1463 $_->{statements} = \%statements;
1464 $_->{come_from} = \%come_from;
1465 $_->{go_to} = \%go_to;
1466 $_->{globals_read} = \%globals_read;
1467 $_->{globals_written} = \%globals_written;
1468 $_->{globals_used} = \%globals_used;
1470 # using this info, we could now identify basic blocks
1473 print STDERR "Detecting constants and temps, and naming...\n";
1474 detect_constants \%p;
1483 # what do we want to do?
1484 my $checkfunc = \&find_uninitialized_locals;
1485 if($ENV{DISASSEMBLE})
1487 $checkfunc = \&disassemble_function;
1489 for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1491 $checkfunc->(\%p, $_);
1495 for my $progs (@ARGV) {
1496 my $lno = "$progs.lno";
1497 $lno =~ s/\.dat\.lno$/.lno/;
1499 open my $fh, '<', $progs
1500 or die "$progs: $!";
1502 open my $lnofh, '<', $lno
1505 parse_progs $fh, $lnofh;