]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/progs-analyzer.pl
detect temps used by only one function
[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 do { warn "Invalid opcode: $_[0]"; "INVALID#$_[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                 my $ret = 0;
253
254                 for(;;)
255                 {
256                         return $ret
257                                 if $state_checker->($ip, $state);
258
259                         my $s = $statements->[$ip];
260                         my $c = checkop $s->{op};
261
262                         if(($ret = $instruction_handler->($ip, $state, $s, $c)))
263                         {
264                                 # abort execution
265                                 last;
266                         }
267
268                         if($c->{isreturn})
269                         {
270                                 last;
271                         }
272                         elsif($c->{iscall})
273                         {
274                                 my $func = $s->{a};
275                                 my $funcid = $progs->{globals}[$func]{v}{int};
276                                 last
277                                         if $progs->{error_func}{$funcid};
278                                 $ip += 1;
279                         }
280                         elsif($c->{isjump})
281                         {
282                                 if($c->{isconditional})
283                                 {
284                                         if(rand 2)
285                                         {
286                                                 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
287                                                 {
288                                                         last;
289                                                 }
290                                                 $ip += 1;
291                                         }
292                                         else
293                                         {
294                                                 $nfa->($ip+1, $copy_handler->($state));
295                                                 $ip += $s->{$c->{isjump}};
296                                         }
297                                 }
298                                 else
299                                 {
300                                         $ip += $s->{$c->{isjump}};
301                                 }
302                         }
303                         else
304                         {
305                                 $ip += 1;
306                         }
307                 }
308
309                 return $ret;
310         };
311
312         $nfa->($ip, $copy_handler->($state));
313 }
314
315 sub get_constant($$)
316 {
317         my ($progs, $g) = @_;
318         if($g->{int} == 0)
319         {
320                 return 0;
321         }
322         elsif($g->{int} > 0 && $g->{int} < 8388608)
323         {
324                 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
325                 {
326                         return str($progs->{getstring}->($g->{int}));
327                 }
328                 else
329                 {
330                         return $g->{int} . "i";
331                 }
332         }
333         else
334         {
335                 return $g->{float};
336         }
337 }
338
339 use constant PRE_MARK_STATEMENT => "";
340 use constant POST_MARK_STATEMENT => "";
341 use constant PRE_MARK_OPERAND => "*** ";
342 use constant POST_MARK_OPERAND => " ***";
343
344 use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
345 use constant OPERAND_FORMAT => "%s";
346 use constant OPERAND_SEPARATOR => ", ";
347 use constant INSTRUCTION_SEPARATOR => "\n";
348
349 sub str($)
350 {
351         my ($str) = @_;
352         $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
353         return "\"$str\"";
354 }
355
356 sub disassemble_function($$;$)
357 {
358         my ($progs, $func, $highlight) = @_;
359
360         print "$func->{debugname}:\n";
361
362         my $initializer = sub
363         {
364                 my ($ofs) = @_;
365                 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
366                 print " = $g"
367                         if defined $g;
368         };
369
370         printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
371         printf OPERAND_FORMAT, "$func->{parm_start}";
372         print INSTRUCTION_SEPARATOR;
373
374         printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
375         printf OPERAND_FORMAT, "$func->{locals}";
376         print INSTRUCTION_SEPARATOR;
377
378         my %override_locals = ();
379         my $p = $func->{parm_start};
380         for(0..($func->{numparms}-1))
381         {
382                 $override_locals{$p} //= "argv_$_";
383                 for my $comp(0..($func->{parm_size}[$_]-1))
384                 {
385                         $override_locals{$p} //= "argv_$_\[$comp]";
386                         ++$p;
387                 }
388                 printf INSTRUCTION_FORMAT, '', '', '.ARG';
389                 printf OPERAND_FORMAT, "argv_$_";
390                 print OPERAND_SEPARATOR;
391                 printf OPERAND_FORMAT, $func->{parm_size}[$_];
392                 print INSTRUCTION_SEPARATOR;
393         }
394         for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
395         {
396                 next
397                         if exists $override_locals{$_};
398                 $override_locals{$_} = "local_$_";
399
400                 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
401                 printf OPERAND_FORMAT, "local_$_";
402                 $initializer->($_);
403                 print INSTRUCTION_SEPARATOR;
404         }
405
406         my $getname = sub
407         {
408                 my ($ofs) = @_;
409                 return $override_locals{$ofs}
410                         if exists $override_locals{$ofs};
411                 my $def = $progs->{globaldef_byoffset}->($ofs);
412                 return $def->{debugname};
413         };
414
415         my $operand = sub
416         {
417                 my ($ip, $type, $operand) = @_;
418                 if($type eq 'inglobal')
419                 {
420                         my $name = $getname->($operand);
421                         printf OPERAND_FORMAT, "$name";
422                 }
423                 elsif($type eq 'outglobal')
424                 {
425                         my $name = $getname->($operand);
426                         printf OPERAND_FORMAT, "&$name";
427                 }
428                 elsif($type eq 'inglobalvec')
429                 {
430                         my $name = $getname->($operand);
431                         printf OPERAND_FORMAT, "$name\[\]";
432                 }
433                 elsif($type eq 'outglobalvec')
434                 {
435                         my $name = $getname->($operand);
436                         printf OPERAND_FORMAT, "&$name\[\]";
437                 }
438                 elsif($type eq 'inglobalfunc')
439                 {
440                         my $name = $getname->($operand);
441                         printf OPERAND_FORMAT, "$name()";
442                 }
443                 elsif($type eq 'ipoffset')
444                 {
445                         printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
446                 }
447                 else
448                 {
449                         die "unknown type: $type";
450                 }
451         };
452
453         my $statements = $func->{statements};
454         my $come_from = $func->{come_from};
455
456         my $ipprev = undef;
457         for my $ip(sort { $a <=> $b } keys %$statements)
458         {
459                 if($ip == $func->{first_statement})
460                 {
461                         printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
462                         print INSTRUCTION_SEPARATOR;
463                 }
464                 if(defined $ipprev && $ip != $ipprev + 1)
465                 {
466                         printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
467                         printf OPERAND_FORMAT, $ip - $ipprev - 1;
468                         print INSTRUCTION_SEPARATOR;
469                 }
470                 if(my $cf = $come_from->{$ip})
471                 {
472                         printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
473                         my $cnt = 0;
474                         for(sort { $a <=> $b } keys %$cf)
475                         {
476                                 print OPERAND_SEPARATOR
477                                         if $cnt++;
478                                 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
479                         }
480                         print INSTRUCTION_SEPARATOR;
481                 }
482
483                 my $op = $progs->{statements}[$ip]{op};
484                 my $ipt = $progs->{statements}[$ip];
485                 my $opprop = checkop $op;
486
487                 print PRE_MARK_STATEMENT
488                         if $highlight and $highlight->{$ip};
489
490                 my $showip = $opprop->{isjump};
491                 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? "<!>" : "", $op;
492
493                 my $cnt = 0;
494                 for my $o(qw(a b c))
495                 {
496                         next
497                                 if not defined $opprop->{$o};
498                         print OPERAND_SEPARATOR
499                                 if $cnt++;
500                         print PRE_MARK_OPERAND
501                                 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
502                         $operand->($ip, $opprop->{$o}, $ipt->{$o});
503                         print POST_MARK_OPERAND
504                                 if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
505                 }
506
507                 print POST_MARK_STATEMENT
508                         if $highlight and $highlight->{$ip};
509
510                 print INSTRUCTION_SEPARATOR;
511         }
512 }
513
514 sub find_uninitialized_locals($$)
515 {
516         my ($progs, $func) = @_;
517
518
519         return
520                 if $func->{first_statement} < 0; # builtin
521
522         print STDERR "Checking $func->{debugname}...\n";
523
524         my $p = $func->{parm_start};
525         for(0..($func->{numparms}-1))
526         {
527                 $p += $func->{parm_size}[$_];
528         }
529
530         use constant WATCHME_R => 1;
531         use constant WATCHME_W => 2;
532         use constant WATCHME_X => 4;
533         use constant WATCHME_T => 8;
534         my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
535
536         for(keys %{$progs->{temps}})
537         {
538                 next
539                         if exists $watchme{$_};
540                 if($progs->{temps}{$_})
541                 {
542                         # shared temp
543                         $watchme{$_} = WATCHME_T | WATCHME_X
544                 }
545                 else
546                 {
547                         # unique temp
548                         $watchme{$_} = WATCHME_X
549                 }
550         }
551
552         $watchme{$_} |= WATCHME_R
553                 for keys %{$func->{globals_read}};
554         $watchme{$_} |= WATCHME_W
555                 for keys %{$func->{globals_written}};
556
557         my %write_places = ();
558         for my $ofs(keys %{$func->{globals_written}})
559         {
560                 next
561                         unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
562                 for my $ip(keys %{$func->{globals_written}{$ofs}})
563                 {
564                         for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
565                         {
566                                 push @{$write_places{$ip}{$op}}, $ofs;
567                         }
568                 }
569         }
570
571         for(keys %watchme)
572         {
573                 delete $watchme{$_}
574                         if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
575         }
576
577         return
578                 if not keys %watchme;
579
580         for(keys %watchme)
581         {
582                 $watchme{$_} = {
583                         flags => $watchme{$_},
584                         valid => [0, undef, undef]
585                 };
586         }
587
588         # mark parameters as initialized
589         for($func->{parm_start} .. ($p-1))
590         {
591                 $watchme{$_}{valid} = [1, undef, undef]
592                         if defined $watchme{$_};
593         }
594         # an initial run of STORE instruction is for receiving extra parameters
595         # (beyond 8). Only possible if the function is declared as having 8 params.
596         # Extra parameters behave otherwise like temps, but are initialized at
597         # startup.
598         for($func->{first_statement} .. (@{$progs->{statements}}-1))
599         {
600                 my $s = $progs->{statements}[$_];
601                 if($s->{op} eq 'STORE_V')
602                 {
603                         $watchme{$s->{a}}{valid} = [1, undef, undef]
604                                 if defined $watchme{$s->{a}};
605                         $watchme{$s->{a}+1}{valid} = [1, undef, undef]
606                                 if defined $watchme{$s->{a}+1};
607                         $watchme{$s->{a}+2}{valid} = [1, undef, undef]
608                                 if defined $watchme{$s->{a}+2};
609                 }
610                 elsif($s->{op} =~ /^STORE_/)
611                 {
612                         $watchme{$s->{a}}{valid} = [1, undef, undef]
613                                 if defined $watchme{$s->{a}};
614                 }
615                 else
616                 {
617                         last;
618                 }
619         }
620
621         my %warned = ();
622         my %ip_seen = ();
623         run_nfa $progs, $func->{first_statement}, \%watchme,
624                 sub {
625                         my ($h) = @_;
626                         return { map { $_ => { %{$h->{$_}} } } keys %$h };
627                 },
628                 sub {
629                         my ($ip, $state) = @_;
630
631                         my $s = $ip_seen{$ip};
632                         if($s)
633                         {
634                                 # if $state is stronger or equal to $s, return 1
635
636                                 for(keys %$state)
637                                 {
638                                         if($state->{$_}{valid}[0] < $s->{$_})
639                                         {
640                                                 # The current state is LESS valid than the previously run one. We NEED to run this.
641                                                 # The saved state can safely become the intersection [citation needed].
642                                                 for(keys %$state)
643                                                 {
644                                                         $s->{$_} = $state->{$_}{valid}[0]
645                                                                 if $state->{$_}{valid}[0] < $s->{$_};
646                                                 }
647                                                 return 0;
648                                         }
649                                 }
650                                 # if we get here, $state is stronger or equal. No need to try it.
651                                 return 1;
652                         }
653                         else
654                         {
655                                 # Never seen this IP yet.
656                                 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
657                                 return 0;
658                         }
659                 },
660                 sub {
661                         my ($ip, $state, $s, $c) = @_;
662                         my $op = $s->{op};
663
664                         my $return_hack = $c->{isreturn} // 0;
665
666                         for(qw(a b c))
667                         {
668                                 my $type = $c->{$_};
669                                 next
670                                         unless defined $type;
671
672                                 my $ofs = $s->{$_};
673
674                                 my $read = sub
675                                 {
676                                         my ($ofs) = @_;
677                                         ++$return_hack
678                                                 if $return_hack;
679                                         return
680                                                 if not exists $state->{$ofs};
681                                         my $valid = $state->{$ofs}{valid};
682                                         if($valid->[0] == 0)
683                                         {
684                                                 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
685                                                 {
686                                                         print "; Use of uninitialized value $ofs in $func->{debugname} at $ip.$_\n";
687                                                         ++$warned{$ip}{$_};
688                                                 }
689                                         }
690                                         elsif($valid->[0] < 0)
691                                         {
692                                                 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
693                                                 {
694                                                         print "; Use of temporary $ofs across CALL in $func->{debugname} at $ip.$_\n";
695                                                         ++$warned{$ip}{$_};
696                                                 }
697                                         }
698                                         else
699                                         {
700                                                 # it's VALID
701                                                 if(defined $valid->[1])
702                                                 {
703                                                         delete $write_places{$valid->[1]}{$valid->[2]};
704                                                 }
705                                         }
706                                 };
707                                 my $write = sub
708                                 {
709                                         my ($ofs) = @_;
710                                         $state->{$ofs}{valid} = [1, $ip, $_]
711                                                 if exists $state->{$ofs};
712                                 };
713
714                                 if($type eq 'inglobal' || $type eq 'inglobalfunc')
715                                 {
716                                         $read->($ofs);
717                                 }
718                                 elsif($type eq 'inglobalvec')
719                                 {
720                                         $read->($ofs);
721                                         $read->($ofs+1);
722                                         $read->($ofs+2);
723                                 }
724                                 elsif($type eq 'outglobal')
725                                 {
726                                         $write->($ofs);
727                                 }
728                                 elsif($type eq 'outglobalvec')
729                                 {
730                                         $write->($ofs);
731                                         $write->($ofs+1);
732                                         $write->($ofs+2);
733                                 }
734                         }
735                         if($c->{iscall})
736                         {
737                                 # builtin calls may clobber stuff
738                                 my $func = $s->{a};
739                                 my $funcid = $progs->{globals}[$func]{v}{int};
740                                 my $funcobj = $progs->{functions}[$funcid];
741                                 if(!$funcobj || $funcobj->{first_statement} >= 0)
742                                 {
743                                         # invalidate temps
744                                         for(values %$state)
745                                         {
746                                                 if($_->{flags} & WATCHME_T)
747                                                 {
748                                                         $_->{valid} = [-1, undef, undef];
749                                                 }
750                                         }
751                                 }
752                         }
753
754                         return 0;
755                 };
756
757         for my $ip(keys %write_places)
758         {
759                 for my $operand(keys %{$write_places{$ip}})
760                 {
761                         # TODO verify it
762                         my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
763                         my $isread = 0;
764
765                         my %writeplace_seen = ();
766                         run_nfa $progs, $ip+1, \%left,
767                                 sub
768                                 {
769                                         return { %{$_[0]} };
770                                 },
771                                 sub
772                                 {
773                                         my ($ip, $state) = @_;
774                                         return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
775                                 },
776                                 sub
777                                 {
778                                         my ($ip, $state, $s, $c) = @_;
779                                         for(qw(a b c))
780                                         {
781                                                 my $type = $c->{$_};
782                                                 next
783                                                         unless defined $type;
784
785                                                 my $ofs = $s->{$_};
786                                                 if($type eq 'inglobal' || $type eq 'inglobalfunc')
787                                                 {
788                                                         if($state->{$ofs})
789                                                         {
790                                                                 $isread = 1;
791                                                                 return -1; # exit TOTALLY
792                                                         }
793                                                 }
794                                                 elsif($type eq 'inglobalvec')
795                                                 {
796                                                         if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
797                                                         {
798                                                                 $isread = 1;
799                                                                 return -1; # exit TOTALLY
800                                                         }
801                                                 }
802                                                 elsif($type eq 'outglobal')
803                                                 {
804                                                         delete $state->{$ofs};
805                                                         return 1
806                                                                 if !%$state;
807                                                 }
808                                                 elsif($type eq 'outglobalvec')
809                                                 {
810                                                         delete $state->{$ofs};
811                                                         delete $state->{$ofs+1};
812                                                         delete $state->{$ofs+2};
813                                                         return 1
814                                                                 if !%$state;
815                                                 }
816                                         }
817                                         return 0;
818                                 };
819
820                         if(!$isread)
821                         {
822                                 print "; Value is never used in $func->{debugname} at $ip.$operand\n";
823                                 ++$warned{$ip}{$operand};
824                         }
825                 }
826         }
827         
828         disassemble_function($progs, $func, \%warned)
829                 if keys %warned;
830 }
831
832 use constant DEFAULTGLOBALS => [
833         "OFS_NULL",
834         "OFS_RETURN",
835         "OFS_RETURN[1]",
836         "OFS_RETURN[2]",
837         "OFS_PARM0",
838         "OFS_PARM0[1]",
839         "OFS_PARM0[2]",
840         "OFS_PARM1",
841         "OFS_PARM1[1]",
842         "OFS_PARM1[2]",
843         "OFS_PARM2",
844         "OFS_PARM2[1]",
845         "OFS_PARM2[2]",
846         "OFS_PARM3",
847         "OFS_PARM3[1]",
848         "OFS_PARM3[2]",
849         "OFS_PARM4",
850         "OFS_PARM4[1]",
851         "OFS_PARM4[2]",
852         "OFS_PARM5",
853         "OFS_PARM5[1]",
854         "OFS_PARM5[2]",
855         "OFS_PARM6",
856         "OFS_PARM6[1]",
857         "OFS_PARM6[2]",
858         "OFS_PARM7",
859         "OFS_PARM7[1]",
860         "OFS_PARM7[2]"
861 ];
862
863 sub defaultglobal($)
864 {
865         my ($ofs) = @_;
866         if($ofs < @{(DEFAULTGLOBALS)})
867         {
868                 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
869         }
870         return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
871 }
872
873 sub detect_constants($)
874 {
875         my ($progs) = @_;
876         use constant GLOBALFLAG_R => 1; # read
877         use constant GLOBALFLAG_W => 2; # written
878         use constant GLOBALFLAG_S => 4; # saved
879         use constant GLOBALFLAG_I => 8; # initialized
880         use constant GLOBALFLAG_N => 16; # named
881         use constant GLOBALFLAG_Q => 32; # unique to function
882         use constant GLOBALFLAG_U => 64; # unused
883         my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x @{$progs->{globals}};
884
885         for(@{$progs->{functions}})
886         {
887                 for(keys %{$_->{globals_used}})
888                 {
889                         if($globalflags[$_] & GLOBALFLAG_U)
890                         {
891                                 $globalflags[$_] &= ~GLOBALFLAG_U;
892                         }
893                         elsif($globalflags[$_] & GLOBALFLAG_Q)
894                         {
895                                 $globalflags[$_] &= ~GLOBALFLAG_Q;
896                         }
897                 }
898                 $globalflags[$_] |= GLOBALFLAG_R
899                         for keys %{$_->{globals_read}};
900                 $globalflags[$_] |= GLOBALFLAG_W
901                         for keys %{$_->{globals_written}};
902         }
903
904         my %offsets_saved = ();
905         for(@{$progs->{globaldefs}})
906         {
907                 my $type = $_->{type};
908                 my $name = $progs->{getstring}->($_->{s_name});
909                 if($type->{save})
910                 {
911                         for my $i(0..(typesize($_->{type}{type})-1))
912                         {
913                                 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
914                         }
915                 }
916                 if($name ne "")
917                 {
918                         for my $i(0..(typesize($_->{type}{type})-1))
919                         {
920                                 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
921                         }
922                 }
923         }
924         my %offsets_initialized = ();
925         for(0..(@{$progs->{globals}}-1))
926         {
927                 if($progs->{globals}[$_]{v}{int})
928                 {
929                         $globalflags[$_] |= GLOBALFLAG_I;
930                 }
931         }
932
933         my @globaltypes = (undef) x @{$progs->{globals}};
934
935         my %istemp = ();
936         for(0..(@{$progs->{globals}}-1))
937         {
938                 next
939                         if $_ < @{(DEFAULTGLOBALS)};
940                 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
941                 {
942                         $globaltypes[$_] = "unused";
943                 }
944                 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
945                 {
946                         # so it is ro
947                         if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
948                         {
949                                 $globaltypes[$_] = "read_only";
950                         }
951                         elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
952                         {
953                                 $globaltypes[$_] = "const";
954                         }
955                         else
956                         {
957                                 $globaltypes[$_] = "read_only";
958                         }
959                 }
960                 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
961                 {
962                         $globaltypes[$_] = "write_only";
963                 }
964                 else
965                 {
966                         # now we know it is rw
967                         if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
968                         {
969                                 $globaltypes[$_] = "global";
970                         }
971                         elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I | GLOBALFLAG_Q)) == GLOBALFLAG_Q)
972                         {
973                                 $globaltypes[$_] = "uniquetemp";
974                                 $istemp{$_} = 0;
975                         }
976                         elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I | GLOBALFLAG_Q)) == 0)
977                         {
978                                 $globaltypes[$_] = "temp";
979                                 $istemp{$_} = 1;
980                         }
981                         elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
982                         {
983                                 $globaltypes[$_] = "not_saved";
984                         }
985                         else
986                         {
987                                 $globaltypes[$_] = "global";
988                         }
989                 }
990         }
991         $progs->{temps} = \%istemp;
992
993         # globaldefs
994         my @globaldefs = (undef) x @{$progs->{globaldefs}};
995         for(@{$progs->{globaldefs}})
996         {
997                 my $s = $progs->{getstring}->($_->{s_name});
998                 $_->{debugname} //= "\$" . "$s"
999                         if length $s;
1000         }
1001         for(@{$progs->{globaldefs}})
1002         {
1003                 $globaldefs[$_->{ofs}] //= $_
1004                         if defined $_->{debugname};
1005         }
1006         for(@{$progs->{globaldefs}})
1007         {
1008                 $globaldefs[$_->{ofs}] //= $_;
1009         }
1010         for(0..(@{$progs->{globals}}-1))
1011         {
1012                 $globaldefs[$_] //= {
1013                         ofs => $_,
1014                         s_name => undef,
1015                         debugname => undef
1016                 };
1017         }
1018         for(0..(@{(DEFAULTGLOBALS)}-1))
1019         {
1020                 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1021                 $globaltypes[$_] = 'defglobal';
1022         }
1023         my %globaldefs_namecount = ();
1024         for(@globaldefs)
1025         {
1026                 $_->{globaltype} = $globaltypes[$_->{ofs}];
1027                 if(defined $_->{debugname})
1028                 {
1029                         # already has debugname
1030                 }
1031                 elsif($_->{globaltype} eq 'const')
1032                 {
1033                         $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1034                 }
1035                 else
1036                 {
1037                         $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1038                 }
1039                 ++$globaldefs_namecount{$_->{debugname}};
1040         }
1041         for(@globaldefs)
1042         {
1043                 next
1044                         if $globaldefs_namecount{$_->{debugname}} <= 1;
1045                 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1046                 $_->{debugname} .= "\@$_->{ofs}";
1047         }
1048         $progs->{globaldef_byoffset} = sub
1049         {
1050                 my ($ofs) = @_;
1051                 my $def = $globaldefs[$ofs];
1052                 return $def;
1053         };
1054 }
1055
1056 sub parse_progs($)
1057 {
1058         my ($fh) = @_;
1059
1060         my %p = ();
1061
1062         print STDERR "Parsing header...\n";
1063         $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1064         
1065         print STDERR "Parsing strings...\n";
1066         $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1067         $p{getstring} = sub
1068         {
1069                 my ($startpos) = @_;
1070                 my $endpos = index $p{strings}, "\0", $startpos;
1071                 return substr $p{strings}, $startpos, $endpos - $startpos;
1072         };
1073
1074         print STDERR "Parsing statements...\n";
1075         $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1076
1077         print STDERR "Fixing statements...\n";
1078         for my $s(@{$p{statements}})
1079         {
1080                 my $c = checkop $s->{op};
1081
1082                 for(qw(a b c))
1083                 {
1084                         my $type = $c->{$_};
1085                         next
1086                                 unless defined $type;
1087
1088                         if($type eq 'inglobal' || $type eq 'inglobalfunc')
1089                         {
1090                                 $s->{$_} &= 0xFFFF;
1091                         }
1092                         elsif($type eq 'inglobalvec')
1093                         {
1094                                 $s->{$_} &= 0xFFFF;
1095                         }
1096                         elsif($type eq 'outglobal')
1097                         {
1098                                 $s->{$_} &= 0xFFFF;
1099                         }
1100                         elsif($type eq 'outglobalvec')
1101                         {
1102                                 $s->{$_} &= 0xFFFF;
1103                         }
1104                 }
1105
1106         }
1107
1108         print STDERR "Parsing globaldefs...\n";
1109         $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1110
1111         print STDERR "Parsing fielddefs...\n";
1112         $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1113
1114         print STDERR "Parsing globals...\n";
1115         $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1116
1117         print STDERR "Parsing functions...\n";
1118         $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1119
1120         print STDERR "Looking for error()...\n";
1121         $p{error_func} = {};
1122         for(@{$p{globaldefs}})
1123         {
1124                 next
1125                         if $p{getstring}($_->{s_name}) ne 'error';
1126                 my $v = $p{globals}[$_->{ofs}]{v}{int};
1127                 next
1128                         if $v <= 0 || $v >= @{$p{functions}};
1129                 my $first = $p{functions}[$v]{first_statement};
1130                 next
1131                         if $first >= 0;
1132                 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1133                 $p{error_func}{$_->{ofs}} = 1;
1134         }
1135
1136         print STDERR "Scanning functions...\n";
1137         for(@{$p{functions}})
1138         {
1139                 my $file = $p{getstring}->($_->{s_file});
1140                 my $name = $p{getstring}->($_->{s_name});
1141                 $name = "$file:$name"
1142                         if length $file;
1143                 $_->{debugname} = $name;
1144
1145                 next
1146                         if $_->{first_statement} < 0;
1147
1148                 my %statements = ();
1149                 my %come_from = ();
1150                 my %go_to = ();
1151                 my %globals_read = ();
1152                 my %globals_written = ();
1153                 my %globals_used = ();
1154
1155                 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1156                         sub
1157                         {
1158                                 my ($ip, $state, $s, $c) = @_;
1159                                 ++$statements{$ip};
1160
1161                                 if(my $j = $c->{isjump})
1162                                 {
1163                                         my $t = $ip + $s->{$j};
1164                                         $come_from{$t}{$ip} = $c->{isconditional};
1165                                         $go_to{$ip}{$t} = $c->{isconditional};
1166                                 }
1167
1168                                 for my $o(qw(a b c))
1169                                 {
1170                                         my $type = $c->{$o}
1171                                                 or next;
1172                                         my $ofs = $s->{$o};
1173
1174                                         my $read = sub
1175                                         {
1176                                                 my ($ofs) = @_;
1177                                                 $globals_read{$ofs}{$ip}{$o} = 1;
1178                                                 $globals_used{$ofs} = 1;
1179                                         };
1180                                         my $write = sub
1181                                         {
1182                                                 my ($ofs) = @_;
1183                                                 $globals_written{$ofs}{$ip}{$o} = 1;
1184                                                 $globals_used{$ofs} = 1;
1185                                         };
1186
1187                                         if($type eq 'inglobal' || $type eq 'inglobalfunc')
1188                                         {
1189                                                 $read->($ofs);
1190                                         }
1191                                         elsif($type eq 'inglobalvec')
1192                                         {
1193                                                 $read->($ofs);
1194                                                 $read->($ofs+1);
1195                                                 $read->($ofs+2);
1196                                         }
1197                                         elsif($type eq 'outglobal')
1198                                         {
1199                                                 $write->($ofs);
1200                                         }
1201                                         elsif($type eq 'outglobalvec')
1202                                         {
1203                                                 $write->($ofs);
1204                                                 $write->($ofs+1);
1205                                                 $write->($ofs+2);
1206                                         }
1207                                 }
1208
1209                                 return 0;
1210                         };
1211
1212                 $_->{statements} = \%statements;
1213                 $_->{come_from} = \%come_from;
1214                 $_->{go_to} = \%go_to;
1215                 $_->{globals_read} = \%globals_read;
1216                 $_->{globals_written} = \%globals_written;
1217                 $_->{globals_used} = \%globals_used;
1218
1219                 # using this info, we could now identify basic blocks
1220         }
1221
1222         print STDERR "Detecting constants and temps, and naming...\n";
1223         detect_constants \%p;
1224
1225         # what do we want to do?
1226         my $checkfunc = \&find_uninitialized_locals;
1227         #my $checkfunc = \&disassemble_function;
1228         for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1229         {
1230                 $checkfunc->(\%p, $_);
1231         }
1232 }
1233
1234 open my $fh, '<', $ARGV[0];
1235 parse_progs $fh;