]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/progs-analyzer.pl
better detect unused locals
[xonotic/xonotic.git] / misc / tools / progs-analyzer.pl
1 use strict;
2 use warnings;
3 use Digest::SHA;
4
5 sub id()
6 {
7         return sub { $_[0]; };
8 }
9
10 sub signed($)
11 {
12         my ($bits) = @_;
13         return sub { $_[0] >= (2**($bits-1)) ? $_[0]-(2**$bits) : $_[0]; };
14 }
15
16 use constant OPCODE_E => [qw[
17         DONE
18         MUL_F MUL_V MUL_FV MUL_VF
19         DIV_F
20         ADD_F ADD_V
21         SUB_F SUB_V
22         EQ_F EQ_V EQ_S EQ_E EQ_FNC
23         NE_F NE_V NE_S NE_E NE_FNC
24         LE GE LT GT
25         LOAD_F LOAD_V LOAD_S LOAD_ENT LOAD_FLD LOAD_FNC
26         ADDRESS
27         STORE_F STORE_V STORE_S STORE_ENT STORE_FLD STORE_FNC
28         STOREP_F STOREP_V STOREP_S STOREP_ENT STOREP_FLD STOREP_FNC
29         RETURN
30         NOT_F NOT_V NOT_S NOT_ENT NOT_FNC
31         IF IFNOT
32         CALL0 CALL1 CALL2 CALL3 CALL4 CALL5 CALL6 CALL7 CALL8
33         STATE
34         GOTO
35         AND OR
36         BITAND BITOR
37 ]];
38 use constant ETYPE_E => [qw[
39         void
40         string
41         float
42         vector
43         entity
44         field
45         function
46         pointer
47 ]];
48 use constant DEF_SAVEGLOBAL => 32768;
49 sub typesize($)
50 {
51         my ($type) = @_;
52         return 3 if $type eq 'vector';
53         return 1;
54 }
55
56 sub checkop($)
57 {
58         my ($op) = @_;
59         if($op =~ /^IF.*_V$/)
60         {
61                 return { a => 'inglobalvec', b => 'ipoffset', isjump => 'b', isconditional => 1 };
62         }
63         if($op =~ /^IF/)
64         {
65                 return { a => 'inglobal', b => 'ipoffset', isjump => 'b', isconditional => 1 };
66         }
67         if($op eq 'GOTO')
68         {
69                 return { a => 'ipoffset', isjump => 'a', isconditional => 0 };
70         }
71         if($op =~ /^ADD_V$|^SUB_V$/)
72         {
73                 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobalvec' };
74         }
75         if($op =~ /^MUL_V$|^EQ_V$|^NE_V$/)
76         {
77                 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobal' };
78         }
79         if($op eq 'MUL_FV')
80         {
81                 return { a => 'inglobal', b => 'inglobalvec', c => 'outglobalvec' };
82         }
83         if($op eq 'MUL_VF')
84         {
85                 return { a => 'inglobalvec', b => 'inglobal', c => 'outglobalvec' };
86         }
87         if($op eq 'LOAD_V')
88         {
89                 return { a => 'inglobal', b => 'inglobal', c => 'outglobalvec' };
90         }
91         if($op =~ /^NOT_V/)
92         {
93                 return { a => 'inglobalvec', c => 'outglobal' };
94         }
95         if($op =~ /^NOT_/)
96         {
97                 return { a => 'inglobal', c => 'outglobal' };
98         }
99         if($op eq 'STOREP_V')
100         {
101                 return { a => 'inglobalvec', b => 'inglobal' };
102         }
103         if($op eq 'STORE_V')
104         {
105                 return { a => 'inglobalvec', b => 'outglobalvec' };
106         }
107         if($op =~ /^STOREP_/)
108         {
109                 return { a => 'inglobal', b => 'inglobal' };
110         }
111         if($op =~ /^STORE_/)
112         {
113                 return { a => 'inglobal', b => 'outglobal' };
114         }
115         if($op =~ /^CALL/)
116         {
117                 return { a => 'inglobalfunc', iscall => 1 };
118         }
119         if($op =~ /^DONE$|^RETURN$/)
120         {
121                 return { a => 'inglobalvec', isreturn => 1 };
122         }
123         return { a => 'inglobal', b => 'inglobal', c => 'outglobal' };
124 }
125
126 use constant TYPES => {
127         int => ['V', 4, signed 32],
128         ushort => ['v', 2, id],
129         short => ['v', 2, signed 16],
130         opcode => ['v', 2, sub { OPCODE_E->[$_[0]] or die "Invalid opcode: $_[0]"; }],
131         float => ['f', 4, id],
132         uchar8 => ['a8', 8, sub { [unpack 'C8', $_[0]] }],
133         global => ['i', 4, sub { { int => $_[0], float => unpack "f", pack "L", $_[0] }; }],
134         deftype => ['v', 2, sub { { type => ETYPE_E->[$_[0] & ~DEF_SAVEGLOBAL], save => !!($_[0] & DEF_SAVEGLOBAL) }; }],
135 };
136
137 use constant DPROGRAMS_T => [
138         [int => 'version'],
139         [int => 'crc'],
140         [int => 'ofs_statements'],
141         [int => 'numstatements'],
142         [int => 'ofs_globaldefs'],
143         [int => 'numglobaldefs'],
144         [int => 'ofs_fielddefs'],
145         [int => 'numfielddefs'],
146         [int => 'ofs_functions'],
147         [int => 'numfunctions'],
148         [int => 'ofs_strings'],
149         [int => 'numstrings'],
150         [int => 'ofs_globals'],
151         [int => 'numglobals'],
152         [int => 'entityfields']
153 ];
154
155 use constant DSTATEMENT_T => [
156         [opcode => 'op'],
157         [short => 'a'],
158         [short => 'b'],
159         [short => 'c']
160 ];
161
162 use constant DDEF_T => [
163         [deftype => 'type'],
164         [ushort => 'ofs'],
165         [int => 's_name']
166 ];
167
168 use constant DGLOBAL_T => [
169         [global => 'v'],
170 ];
171
172 use constant DFUNCTION_T => [
173         [int => 'first_statement'],
174         [int => 'parm_start'],
175         [int => 'locals'],
176         [int => 'profile'],
177         [int => 's_name'],
178         [int => 's_file'],
179         [int => 'numparms'],
180         [uchar8 => 'parm_size'],
181 ];
182
183 sub get_section($$$)
184 {
185         my ($fh, $start, $len) = @_;
186         seek $fh, $start, 0
187                 or die "seek: $!";
188         $len == read $fh, my $buf, $len
189                 or die "short read";
190         return $buf;
191 }
192
193 sub parse_section($$$$$)
194 {
195         my ($fh, $struct, $start, $len, $cnt) = @_;
196
197         my $itemlen = 0;
198         $itemlen += TYPES->{$_->[0]}->[1]
199                 for @$struct;
200         my $packspec = join '', map { TYPES->{$_->[0]}->[0]; } @$struct;
201         my @packnames = map { $_->[1]; } @$struct;
202
203         $len = $cnt * $itemlen
204                 if not defined $len and defined $cnt;
205         $cnt = int($len / $itemlen)
206                 if not defined $cnt and defined $len;
207         die "Invalid length specification"
208                 unless defined $len and defined $cnt and $len == $cnt * $itemlen;
209         die "Invalid length specification in scalar context"
210                 unless wantarray or $cnt == 1;
211
212         seek $fh, $start, 0
213                 or die "seek: $!";
214         my @out = map
215         {
216                 $itemlen == read $fh, my $buf, $itemlen
217                         or die "short read";
218                 my %h = ();
219                 @h{@packnames} = unpack $packspec, $buf;
220                 $h{$_->[1]} = TYPES->{$_->[0]}->[2]->($h{$_->[1]})
221                         for @$struct;
222                 \%h;
223         }
224         0..($cnt-1);
225         return @out
226                 if wantarray;
227         return $out[0];
228 }
229
230 sub nfa_default_state_checker()
231 {
232         my %seen;
233         return sub
234         {
235                 my ($ip, $state) = @_;
236                 return $seen{"$ip $state"}++;
237         };
238 }
239
240 sub run_nfa($$$$$$)
241 {
242         my ($progs, $ip, $state, $copy_handler, $state_checker, $instruction_handler) = @_;
243
244         my $statements = $progs->{statements};
245
246         my $nfa;
247         $nfa = sub
248         {
249                 no warnings 'recursion';
250
251                 my ($ip, $state) = @_;
252
253                 for(;;)
254                 {
255                         return
256                                 if $state_checker->($ip, $state);
257
258                         my $s = $statements->[$ip];
259                         my $c = checkop $s->{op};
260
261                         if($instruction_handler->($ip, $state, $s, $c))
262                         {
263                                 # abort execution
264                                 last;
265                         }
266
267                         if($c->{isreturn})
268                         {
269                                 last;
270                         }
271                         elsif($c->{isjump})
272                         {
273                                 if($c->{isconditional})
274                                 {
275                                         if(rand 2)
276                                         {
277                                                 $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state));
278                                                 $ip += 1;
279                                         }
280                                         else
281                                         {
282                                                 $nfa->($ip+1, $copy_handler->($state));
283                                                 $ip += $s->{$c->{isjump}};
284                                         }
285                                 }
286                                 else
287                                 {
288                                         $ip += $s->{$c->{isjump}};
289                                 }
290                         }
291                         else
292                         {
293                                 $ip += 1;
294                         }
295                 }
296         };
297
298         $nfa->($ip, $copy_handler->($state));
299 }
300
301 sub get_constant($$)
302 {
303         my ($progs, $g) = @_;
304         if($g->{int} == 0)
305         {
306                 return undef;
307         }
308         elsif($g->{int} > 0 && $g->{int} < 16777216)
309         {
310                 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
311                 {
312                         return str($progs->{getstring}->($g->{int}));
313                 }
314                 else
315                 {
316                         return $g->{int} . "i";
317                 }
318         }
319         else
320         {
321                 return $g->{float};
322         }
323 }
324
325 use constant PRE_MARK_STATEMENT => "";
326 use constant POST_MARK_STATEMENT => "";
327 use constant PRE_MARK_OPERAND => "*** ";
328 use constant POST_MARK_OPERAND => " ***";
329
330 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
331 use constant OPERAND_FORMAT => "%s";
332 use constant OPERAND_SEPARATOR => ", ";
333 use constant INSTRUCTION_SEPARATOR => "\n";
334
335 sub str($)
336 {
337         my ($str) = @_;
338         $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
339         return "\"$str\"";
340 }
341
342 sub disassemble_function($$;$)
343 {
344         my ($progs, $func, $highlight) = @_;
345
346         print "$func->{debugname}:\n";
347
348         my $initializer = sub
349         {
350                 my ($ofs) = @_;
351                 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
352                 print " = $g"
353                         if defined $g;
354         };
355
356         printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
357         printf OPERAND_FORMAT, "$func->{parm_start}";
358         print INSTRUCTION_SEPARATOR;
359
360         printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
361         printf OPERAND_FORMAT, "$func->{locals}";
362         print INSTRUCTION_SEPARATOR;
363
364         my %override_locals = ();
365         my $p = $func->{parm_start};
366         for(0..($func->{numparms}-1))
367         {
368                 $override_locals{$p} //= "argv_$_";
369                 for my $comp(0..($func->{parm_size}[$_]-1))
370                 {
371                         $override_locals{$p} //= "argv_$_\[$comp]";
372                         ++$p;
373                 }
374                 printf INSTRUCTION_FORMAT, '', '', '.ARG';
375                 printf OPERAND_FORMAT, "argv_$_";
376                 print OPERAND_SEPARATOR;
377                 printf OPERAND_FORMAT, $func->{parm_size}[$_];
378                 print INSTRUCTION_SEPARATOR;
379         }
380         for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
381         {
382                 next
383                         if exists $override_locals{$_};
384                 $override_locals{$_} = "local_$_";
385
386                 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
387                 printf OPERAND_FORMAT, "local_$_";
388                 $initializer->($_);
389                 print INSTRUCTION_SEPARATOR;
390         }
391
392         my $getname = sub
393         {
394                 my ($ofs) = @_;
395                 $ofs &= 0xFFFF;
396                 return $override_locals{$ofs}
397                         if exists $override_locals{$ofs};
398                 my $def = $progs->{globaldef_byoffset}->($ofs);
399                 return $def->{debugname};
400         };
401
402         my $operand = sub
403         {
404                 my ($ip, $type, $operand) = @_;
405                 if($type eq 'inglobal')
406                 {
407                         my $name = $getname->($operand);
408                         printf OPERAND_FORMAT, "$name";
409                 }
410                 elsif($type eq 'outglobal')
411                 {
412                         my $name = $getname->($operand);
413                         printf OPERAND_FORMAT, "&$name";
414                 }
415                 elsif($type eq 'inglobalvec')
416                 {
417                         my $name = $getname->($operand);
418                         printf OPERAND_FORMAT, "$name\[\]";
419                 }
420                 elsif($type eq 'outglobalvec')
421                 {
422                         my $name = $getname->($operand);
423                         printf OPERAND_FORMAT, "&$name\[\]";
424                 }
425                 elsif($type eq 'inglobalfunc')
426                 {
427                         my $name = $getname->($operand);
428                         printf OPERAND_FORMAT, "$name()";
429                 }
430                 elsif($type eq 'ipoffset')
431                 {
432                         printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
433                 }
434                 else
435                 {
436                         die "unknown type: $type";
437                 }
438         };
439
440         my %statements = ();
441         my %come_from = ();
442         run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
443                 sub
444                 {
445                         my ($ip, $state, $s, $c) = @_;
446                         ++$statements{$ip};
447
448                         if(my $j = $c->{isjump})
449                         {
450                                 my $t = $ip + $s->{$j};
451                                 $come_from{$t}{$ip} = $c->{isconditional};
452                         }
453
454                         return 0;
455                 };
456
457         my $ipprev = undef;
458         for my $ip(sort { $a <=> $b } keys %statements)
459         {
460                 if($ip == $func->{first_statement})
461                 {
462                         printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
463                         print INSTRUCTION_SEPARATOR;
464                 }
465                 if(defined $ipprev && $ip != $ipprev + 1)
466                 {
467                         printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
468                         printf OPERAND_FORMAT, $ip - $ipprev - 1;
469                         print INSTRUCTION_SEPARATOR;
470                 }
471                 if(my $cf = $come_from{$ip})
472                 {
473                         printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
474                         my $cnt = 0;
475                         for(sort { $a <=> $b } keys %$cf)
476                         {
477                                 print OPERAND_SEPARATOR
478                                         if $cnt++;
479                                 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
480                         }
481                         print INSTRUCTION_SEPARATOR;
482                 }
483
484                 my $op = $progs->{statements}[$ip]{op};
485                 my $ipt = $progs->{statements}[$ip];
486                 my $opprop = checkop $op;
487
488                 print PRE_MARK_STATEMENT
489                         if $highlight and $highlight->{$ip};
490
491                 my $showip = $opprop->{isjump};
492                 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? "<!>" : "", $op;
493
494                 my $cnt = 0;
495                 for my $o(qw(a b c))
496                 {
497                         next
498                                 if not defined $opprop->{$o};
499                         print OPERAND_SEPARATOR
500                                 if $cnt++;
501                         print PRE_MARK_OPERAND
502                                 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
503                         $operand->($ip, $opprop->{$o}, $ipt->{$o});
504                         print POST_MARK_OPERAND
505                                 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
506                 }
507
508                 print POST_MARK_STATEMENT
509                         if $highlight and $highlight->{$ip};
510
511                 print INSTRUCTION_SEPARATOR;
512         }
513 }
514
515 sub find_uninitialized_locals($$)
516 {
517         my ($progs, $func) = @_;
518
519 #       TODO
520 #       21:04:25      divVerent | just wondering how I can best detect "temp value is never used"
521 #       21:04:33      divVerent | I know which vars are temps already
522 #       21:04:59      divVerent | basically, looks like for each write, I will not just have to track that the new value is valid
523 #       21:05:01      divVerent | but also its source
524 #       21:05:12      divVerent | on each read, I'll remember that this source statement's value has been used
525 #       21:05:21      divVerent | and will compare the list of sources in a step after "execution"
526 #       21:05:27      divVerent | to the list of total write statements to the temp
527
528         return
529                 if $func->{first_statement} < 0; # builtin
530
531         print STDERR "Checking $func->{debugname}...\n";
532
533         my $p = $func->{parm_start};
534         for(0..($func->{numparms}-1))
535         {
536                 $p += $func->{parm_size}[$_];
537         }
538
539         use constant WATCHME_R => 1;
540         use constant WATCHME_W => 2;
541         use constant WATCHME_X => 4;
542         use constant WATCHME_T => 8;
543         my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
544
545         for(keys %{$progs->{temps}})
546         {
547                 $watchme{$_} = WATCHME_T | WATCHME_X
548                         if not exists $watchme{$_};
549         }
550
551         my %write_places = ();
552         run_nfa $progs, $func->{first_statement}, "", id, nfa_default_state_checker,
553                 sub
554                 {
555                         my ($ip, $state, $s, $c) = @_;
556                         for(qw(a b c))
557                         {
558                                 my $type = $c->{$_};
559                                 next
560                                         unless defined $type;
561
562                                 my $ofs = $s->{$_};
563                                 if($type eq 'inglobal' || $type eq 'inglobalfunc')
564                                 {
565                                         $watchme{$ofs} |= WATCHME_R;
566                                 }
567                                 elsif($type eq 'inglobalvec')
568                                 {
569                                         $watchme{$ofs} |= WATCHME_R;
570                                         $watchme{$ofs+1} |= WATCHME_R;
571                                         $watchme{$ofs+2} |= WATCHME_R;
572                                 }
573                                 elsif($type eq 'outglobal')
574                                 {
575                                         $watchme{$ofs} |= WATCHME_W;
576                                         $write_places{$ip}{$_} = [$ofs]
577                                                 if $watchme{$ofs} & WATCHME_X;
578                                 }
579                                 elsif($type eq 'outglobalvec')
580                                 {
581                                         $watchme{$ofs} |= WATCHME_W;
582                                         $watchme{$ofs+1} |= WATCHME_W;
583                                         $watchme{$ofs+2} |= WATCHME_W;
584                                         my @l = grep { $watchme{$_} & WATCHME_X } $ofs .. ($ofs+2);
585                                         $write_places{$ip}{$_} = \@l
586                                                 if @l;
587                                 }
588                         }
589
590                         return 0;
591                 };
592
593         for(keys %watchme)
594         {
595                 delete $watchme{$_}
596                         if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
597         }
598
599         return
600                 if not keys %watchme;
601
602         for(keys %watchme)
603         {
604                 $watchme{$_} = {
605                         flags => $watchme{$_},
606                         valid => [0, undef, undef]
607                 };
608         }
609
610         # mark parameters as initialized
611         for($func->{parm_start} .. ($p-1))
612         {
613                 $watchme{$_}{valid} = [1, undef, undef]
614                         if defined $watchme{$_};
615         }
616         # an initial run of STORE instruction is for receiving extra parameters
617         # (beyond 8). Only possible if the function is declared as having 8 params.
618         # Extra parameters behave otherwise like temps, but are initialized at
619         # startup.
620         for($func->{first_statement} .. (@{$progs->{statements}}-1))
621         {
622                 my $s = $progs->{statements}[$_];
623                 if($s->{op} eq 'STORE_V')
624                 {
625                         $watchme{$s->{a}}{valid} = [1, undef, undef]
626                                 if defined $watchme{$s->{a}};
627                         $watchme{$s->{a}+1}{valid} = [1, undef, undef]
628                                 if defined $watchme{$s->{a}+1};
629                         $watchme{$s->{a}+2}{valid} = [1, undef, undef]
630                                 if defined $watchme{$s->{a}+2};
631                 }
632                 elsif($s->{op} =~ /^STORE_/)
633                 {
634                         $watchme{$s->{a}}{valid} = [1, undef, undef]
635                                 if defined $watchme{$s->{a}};
636                 }
637                 else
638                 {
639                         last;
640                 }
641         }
642
643         my %warned = ();
644         my %ip_seen = ();
645         run_nfa $progs, $func->{first_statement}, \%watchme,
646                 sub {
647                         my ($h) = @_;
648                         return { map { $_ => { %{$h->{$_}} } } keys %$h };
649                 },
650                 sub {
651                         my ($ip, $state) = @_;
652
653                         my $s = $ip_seen{$ip};
654                         if($s)
655                         {
656                                 # if $state is stronger or equal to $s, return 1
657
658                                 for(keys %$state)
659                                 {
660                                         if($state->{$_}{valid}[0] < $s->{$_}[0])
661                                         {
662                                                 # The current state is LESS valid than the previously run one. We NEED to run this.
663                                                 # The saved state can safely become the intersection [citation needed].
664                                                 for(keys %$state)
665                                                 {
666                                                         $s->{$_} = $state->{$_}{valid}
667                                                                 if $state->{$_}{valid}[0] < $s->{$_}[0];
668                                                 }
669                                                 return 0;
670                                         }
671                                 }
672                                 # if we get here, $state is stronger or equal. No need to try it.
673                                 return 1;
674                         }
675                         else
676                         {
677                                 # Never seen this IP yet.
678                                 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}); } keys %$state };
679                                 return 0;
680                         }
681                 },
682                 sub {
683                         my ($ip, $state, $s, $c) = @_;
684                         my $op = $s->{op};
685
686                         my $return_hack = $c->{isreturn} // 0;
687
688                         for(qw(a b c))
689                         {
690                                 my $type = $c->{$_};
691                                 next
692                                         unless defined $type;
693
694                                 my $ofs = $s->{$_};
695
696                                 my $read = sub
697                                 {
698                                         my ($ofs) = @_;
699                                         ++$return_hack
700                                                 if $return_hack;
701                                         return
702                                                 if not exists $state->{$ofs};
703                                         my $valid = $state->{$ofs}{valid};
704                                         if($valid->[0] == 0)
705                                         {
706                                                 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
707                                                 {
708                                                         print "; Use of uninitialized value $ofs in $func->{debugname} at $ip.$_\n";
709                                                         ++$warned{$ip}{$_};
710                                                 }
711                                         }
712                                         elsif($valid->[0] < 0)
713                                         {
714                                                 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
715                                                 {
716                                                         print "; Use of temporary $ofs across CALL in $func->{debugname} at $ip.$_\n";
717                                                         ++$warned{$ip}{$_};
718                                                 }
719                                         }
720                                         else
721                                         {
722                                                 # it's VALID
723                                                 if(defined $valid->[1])
724                                                 {
725                                                         delete $write_places{$valid->[1]}{$valid->[2]};
726                                                 }
727                                         }
728                                 };
729                                 my $write = sub
730                                 {
731                                         my ($ofs) = @_;
732                                         $state->{$ofs}{valid} = [1, $ip, $_]
733                                                 if exists $state->{$ofs};
734                                 };
735
736                                 if($type eq 'inglobal' || $type eq 'inglobalfunc')
737                                 {
738                                         $read->($ofs);
739                                 }
740                                 elsif($type eq 'inglobalvec')
741                                 {
742                                         $read->($ofs);
743                                         $read->($ofs+1);
744                                         $read->($ofs+2);
745                                 }
746                                 elsif($type eq 'outglobal')
747                                 {
748                                         $write->($ofs);
749                                 }
750                                 elsif($type eq 'outglobalvec')
751                                 {
752                                         $write->($ofs);
753                                         $write->($ofs+1);
754                                         $write->($ofs+2);
755                                 }
756                         }
757                         if($c->{iscall})
758                         {
759                                 # builtin calls may clobber stuff
760                                 my $func = $s->{a};
761                                 my $funcid = $progs->{globals}[$func]{v}{int};
762                                 my $funcobj = $progs->{functions}[$funcid];
763                                 if(!$funcobj || $funcobj->{first_statement} >= 0)
764                                 {
765                                         # invalidate temps
766                                         for(values %$state)
767                                         {
768                                                 if($_->{flags} & WATCHME_T)
769                                                 {
770                                                         $_->{valid} = [-1, undef, undef];
771                                                 }
772                                         }
773                                 }
774                                 else # builtin
775                                 {
776                                         my $def = $progs->{globaldef_byoffset}->($func);
777                                         return 1
778                                                 if $def->{debugname} eq 'error';
779                                 }
780                         }
781
782                         return 0;
783                 };
784
785         my %writeplace_seen = ();
786         for my $ip(keys %write_places)
787         {
788                 for my $operand(keys %{$write_places{$ip}})
789                 {
790                         # TODO verify it
791                         my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
792                         my $isread = 0;
793
794                         run_nfa $progs, $ip+1, \%left,
795                                 sub
796                                 {
797                                         return { %{$_[0]} };
798                                 },
799                                 sub
800                                 {
801                                         my ($ip, $state) = @_;
802                                         return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
803                                 },
804                                 sub
805                                 {
806                                         my ($ip, $state, $s, $c) = @_;
807                                         for(qw(a b c))
808                                         {
809                                                 my $type = $c->{$_};
810                                                 next
811                                                         unless defined $type;
812
813                                                 my $ofs = $s->{$_};
814                                                 if($type eq 'inglobal' || $type eq 'inglobalfunc')
815                                                 {
816                                                         if($state->{$ofs})
817                                                         {
818                                                                 $isread = 1;
819                                                                 return -1; # exit TOTALLY
820                                                         }
821                                                 }
822                                                 elsif($type eq 'inglobalvec')
823                                                 {
824                                                         if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
825                                                         {
826                                                                 $isread = 1;
827                                                                 return -1; # exit TOTALLY
828                                                         }
829                                                 }
830                                                 elsif($type eq 'outglobal')
831                                                 {
832                                                         delete $state->{$ofs};
833                                                         return 1
834                                                                 if !%$state;
835                                                 }
836                                                 elsif($type eq 'outglobalvec')
837                                                 {
838                                                         delete $state->{$ofs};
839                                                         delete $state->{$ofs+1};
840                                                         delete $state->{$ofs+2};
841                                                         return 1
842                                                                 if !%$state;
843                                                 }
844                                         }
845                                         return 0;
846                                 };
847
848                         if(!$isread)
849                         {
850                                 print "; Value is never used in $func->{debugname} at $ip.$operand\n";
851                                 ++$warned{$ip}{$operand};
852                         }
853                 }
854         }
855         
856         disassemble_function($progs, $func, \%warned)
857                 if keys %warned;
858 }
859
860 use constant DEFAULTGLOBALS => [
861         "OFS_NULL",
862         "OFS_RETURN",
863         "OFS_RETURN[1]",
864         "OFS_RETURN[2]",
865         "OFS_PARM0",
866         "OFS_PARM0[1]",
867         "OFS_PARM0[2]",
868         "OFS_PARM1",
869         "OFS_PARM1[1]",
870         "OFS_PARM1[2]",
871         "OFS_PARM2",
872         "OFS_PARM2[1]",
873         "OFS_PARM2[2]",
874         "OFS_PARM3",
875         "OFS_PARM3[1]",
876         "OFS_PARM3[2]",
877         "OFS_PARM4",
878         "OFS_PARM4[1]",
879         "OFS_PARM4[2]",
880         "OFS_PARM5",
881         "OFS_PARM5[1]",
882         "OFS_PARM5[2]",
883         "OFS_PARM6",
884         "OFS_PARM6[1]",
885         "OFS_PARM6[2]",
886         "OFS_PARM7",
887         "OFS_PARM7[1]",
888         "OFS_PARM7[2]"
889 ];
890
891 sub defaultglobal($)
892 {
893         my ($ofs) = @_;
894         if($ofs < @{(DEFAULTGLOBALS)})
895         {
896                 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
897         }
898         return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
899 }
900
901 sub parse_progs($)
902 {
903         my ($fh) = @_;
904
905         my %p = ();
906
907         print STDERR "Parsing header...\n";
908         $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
909         
910         print STDERR "Parsing strings...\n";
911         $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
912         $p{getstring} = sub
913         {
914                 my ($startpos) = @_;
915                 my $endpos = index $p{strings}, "\0", $startpos;
916                 return substr $p{strings}, $startpos, $endpos - $startpos;
917         };
918
919         print STDERR "Parsing statements...\n";
920         $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
921
922         print STDERR "Parsing globaldefs...\n";
923         $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
924
925         print STDERR "Parsing fielddefs...\n";
926         $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
927
928         print STDERR "Parsing globals...\n";
929         $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
930
931         print STDERR "Parsing functions...\n";
932         $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
933
934         print STDERR "Detecting temps...\n";
935         my %offsets_saved = ();
936         for(@{$p{globaldefs}})
937         {
938                 my $type = $_->{type};
939                 my $name = $p{getstring}->($_->{s_name});
940                 next
941                         unless $type->{save} or $name ne "";
942                 for my $i(0..(typesize($_->{type}{type})-1))
943                 {
944                         ++$offsets_saved{$_->{ofs}+$i};
945                 }
946         }
947         my %offsets_initialized = ();
948         for(0..(@{$p{globals}}-1))
949         {
950                 if($p{globals}[$_]{v}{int})
951                 {
952                         ++$offsets_initialized{$_};
953                 }
954         }
955         my %istemp = ();
956         my %isconst = ();
957         for(0..(@{$p{globals}}-1))
958         {
959                 next
960                         if $_ < @{(DEFAULTGLOBALS)};
961                 ++$isconst{$_}
962                         if !$offsets_saved{$_} and $offsets_initialized{$_};
963                 ++$istemp{$_}
964                         if !$offsets_saved{$_} and !$offsets_initialized{$_};
965         }
966         $p{temps} = \%istemp;
967         $p{consts} = \%isconst;
968
969         print STDERR "Naming...\n";
970
971         # globaldefs
972         my @globaldefs = ();
973         for(@{$p{globaldefs}})
974         {
975                 my $s = $p{getstring}->($_->{s_name});
976                 $_->{debugname} //= "_$s"
977                         if length $s;
978         }
979         for(@{$p{globaldefs}})
980         {
981                 $globaldefs[$_->{ofs}] //= $_
982                         if defined $_->{debugname};
983         }
984         for(@{$p{globaldefs}})
985         {
986                 $globaldefs[$_->{ofs}] //= $_;
987         }
988         for(0..(@{$p{globals}}-1))
989         {
990                 $globaldefs[$_] //= {
991                         ofs => $_,
992                         s_name => undef,
993                         debugname => undef
994                 };
995         }
996         my %globaldefs = ();
997         for(@globaldefs)
998         {
999                 if(!defined $_->{debugname})
1000                 {
1001                         if($istemp{$_->{ofs}})
1002                         {
1003                                 $_->{debugname} = "temp_$_->{ofs}";
1004                         }
1005                         elsif($isconst{$_->{ofs}})
1006                         {
1007                                 $_->{debugname} = "(" . get_constant(\%p, $p{globals}[$_->{ofs}]{v}) . ")";
1008                         }
1009                         else
1010                         {
1011                                 $_->{debugname} = "global_$_->{ofs}";
1012                         }
1013                 }
1014                 ++$globaldefs{$_->{debugname}};
1015         }
1016         for(@globaldefs)
1017         {
1018                 next
1019                         if $globaldefs{$_->{debugname}} <= 1;
1020                 print "Not unique: $_->{debugname} at $_->{ofs}\n";
1021                 $_->{debugname} .= "\@$_->{ofs}";
1022         }
1023         $p{globaldef_byoffset} = sub
1024         {
1025                 my ($ofs) = @_;
1026                 $ofs &= 0xFFFF;
1027                 if($ofs >= 0 && $ofs < @{(DEFAULTGLOBALS)})
1028                 {
1029                         return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
1030                 }
1031                 my $def = $globaldefs[$ofs];
1032                 return $def;
1033         };
1034
1035         # functions
1036         my %functions = ();
1037         for(@{$p{functions}})
1038         {
1039                 my $file = $p{getstring}->($_->{s_file});
1040                 my $name = $p{getstring}->($_->{s_name});
1041                 $name = "$file:$name"
1042                         if length $file;
1043                 $_->{debugname} = $name;
1044                 $functions{$_->{first_statement}} = $_;
1045         }
1046         $p{function_byoffset} = sub
1047         {
1048                 my ($ofs) = @_;
1049                 return $functions{$ofs};
1050         };
1051
1052         # what do we want to do?
1053         my $checkfunc = \&find_uninitialized_locals;
1054         #my $checkfunc = \&disassemble_function;
1055         for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1056         {
1057                 $checkfunc->(\%p, $_);
1058         }
1059 }
1060
1061 open my $fh, '<', $ARGV[0];
1062 parse_progs $fh;