]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/progs-analyzer.pl
better analyze variable types
[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                 $watchme{$_} = WATCHME_T | WATCHME_X
539                         if not exists $watchme{$_};
540         }
541
542         $watchme{$_} |= WATCHME_R
543                 for keys %{$func->{globals_read}};
544         $watchme{$_} |= WATCHME_W
545                 for keys %{$func->{globals_written}};
546
547         my %write_places = ();
548         for my $ofs(keys %{$func->{globals_written}})
549         {
550                 next
551                         unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
552                 for my $ip(keys %{$func->{globals_written}{$ofs}})
553                 {
554                         for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
555                         {
556                                 push @{$write_places{$ip}{$op}}, $ofs;
557                         }
558                 }
559         }
560
561         for(keys %watchme)
562         {
563                 delete $watchme{$_}
564                         if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
565         }
566
567         return
568                 if not keys %watchme;
569
570         for(keys %watchme)
571         {
572                 $watchme{$_} = {
573                         flags => $watchme{$_},
574                         valid => [0, undef, undef]
575                 };
576         }
577
578         # mark parameters as initialized
579         for($func->{parm_start} .. ($p-1))
580         {
581                 $watchme{$_}{valid} = [1, undef, undef]
582                         if defined $watchme{$_};
583         }
584         # an initial run of STORE instruction is for receiving extra parameters
585         # (beyond 8). Only possible if the function is declared as having 8 params.
586         # Extra parameters behave otherwise like temps, but are initialized at
587         # startup.
588         for($func->{first_statement} .. (@{$progs->{statements}}-1))
589         {
590                 my $s = $progs->{statements}[$_];
591                 if($s->{op} eq 'STORE_V')
592                 {
593                         $watchme{$s->{a}}{valid} = [1, undef, undef]
594                                 if defined $watchme{$s->{a}};
595                         $watchme{$s->{a}+1}{valid} = [1, undef, undef]
596                                 if defined $watchme{$s->{a}+1};
597                         $watchme{$s->{a}+2}{valid} = [1, undef, undef]
598                                 if defined $watchme{$s->{a}+2};
599                 }
600                 elsif($s->{op} =~ /^STORE_/)
601                 {
602                         $watchme{$s->{a}}{valid} = [1, undef, undef]
603                                 if defined $watchme{$s->{a}};
604                 }
605                 else
606                 {
607                         last;
608                 }
609         }
610
611         my %warned = ();
612         my %ip_seen = ();
613         run_nfa $progs, $func->{first_statement}, \%watchme,
614                 sub {
615                         my ($h) = @_;
616                         return { map { $_ => { %{$h->{$_}} } } keys %$h };
617                 },
618                 sub {
619                         my ($ip, $state) = @_;
620
621                         my $s = $ip_seen{$ip};
622                         if($s)
623                         {
624                                 # if $state is stronger or equal to $s, return 1
625
626                                 for(keys %$state)
627                                 {
628                                         if($state->{$_}{valid}[0] < $s->{$_})
629                                         {
630                                                 # The current state is LESS valid than the previously run one. We NEED to run this.
631                                                 # The saved state can safely become the intersection [citation needed].
632                                                 for(keys %$state)
633                                                 {
634                                                         $s->{$_} = $state->{$_}{valid}[0]
635                                                                 if $state->{$_}{valid}[0] < $s->{$_};
636                                                 }
637                                                 return 0;
638                                         }
639                                 }
640                                 # if we get here, $state is stronger or equal. No need to try it.
641                                 return 1;
642                         }
643                         else
644                         {
645                                 # Never seen this IP yet.
646                                 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
647                                 return 0;
648                         }
649                 },
650                 sub {
651                         my ($ip, $state, $s, $c) = @_;
652                         my $op = $s->{op};
653
654                         my $return_hack = $c->{isreturn} // 0;
655
656                         for(qw(a b c))
657                         {
658                                 my $type = $c->{$_};
659                                 next
660                                         unless defined $type;
661
662                                 my $ofs = $s->{$_};
663
664                                 my $read = sub
665                                 {
666                                         my ($ofs) = @_;
667                                         ++$return_hack
668                                                 if $return_hack;
669                                         return
670                                                 if not exists $state->{$ofs};
671                                         my $valid = $state->{$ofs}{valid};
672                                         if($valid->[0] == 0)
673                                         {
674                                                 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
675                                                 {
676                                                         print "; Use of uninitialized value $ofs in $func->{debugname} at $ip.$_\n";
677                                                         ++$warned{$ip}{$_};
678                                                 }
679                                         }
680                                         elsif($valid->[0] < 0)
681                                         {
682                                                 if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b')) # fteqcc logicops cause this
683                                                 {
684                                                         print "; Use of temporary $ofs across CALL in $func->{debugname} at $ip.$_\n";
685                                                         ++$warned{$ip}{$_};
686                                                 }
687                                         }
688                                         else
689                                         {
690                                                 # it's VALID
691                                                 if(defined $valid->[1])
692                                                 {
693                                                         delete $write_places{$valid->[1]}{$valid->[2]};
694                                                 }
695                                         }
696                                 };
697                                 my $write = sub
698                                 {
699                                         my ($ofs) = @_;
700                                         $state->{$ofs}{valid} = [1, $ip, $_]
701                                                 if exists $state->{$ofs};
702                                 };
703
704                                 if($type eq 'inglobal' || $type eq 'inglobalfunc')
705                                 {
706                                         $read->($ofs);
707                                 }
708                                 elsif($type eq 'inglobalvec')
709                                 {
710                                         $read->($ofs);
711                                         $read->($ofs+1);
712                                         $read->($ofs+2);
713                                 }
714                                 elsif($type eq 'outglobal')
715                                 {
716                                         $write->($ofs);
717                                 }
718                                 elsif($type eq 'outglobalvec')
719                                 {
720                                         $write->($ofs);
721                                         $write->($ofs+1);
722                                         $write->($ofs+2);
723                                 }
724                         }
725                         if($c->{iscall})
726                         {
727                                 # builtin calls may clobber stuff
728                                 my $func = $s->{a};
729                                 my $funcid = $progs->{globals}[$func]{v}{int};
730                                 my $funcobj = $progs->{functions}[$funcid];
731                                 if(!$funcobj || $funcobj->{first_statement} >= 0)
732                                 {
733                                         # invalidate temps
734                                         for(values %$state)
735                                         {
736                                                 if($_->{flags} & WATCHME_T)
737                                                 {
738                                                         $_->{valid} = [-1, undef, undef];
739                                                 }
740                                         }
741                                 }
742                         }
743
744                         return 0;
745                 };
746
747         for my $ip(keys %write_places)
748         {
749                 for my $operand(keys %{$write_places{$ip}})
750                 {
751                         # TODO verify it
752                         my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
753                         my $isread = 0;
754
755                         my %writeplace_seen = ();
756                         run_nfa $progs, $ip+1, \%left,
757                                 sub
758                                 {
759                                         return { %{$_[0]} };
760                                 },
761                                 sub
762                                 {
763                                         my ($ip, $state) = @_;
764                                         return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
765                                 },
766                                 sub
767                                 {
768                                         my ($ip, $state, $s, $c) = @_;
769                                         for(qw(a b c))
770                                         {
771                                                 my $type = $c->{$_};
772                                                 next
773                                                         unless defined $type;
774
775                                                 my $ofs = $s->{$_};
776                                                 if($type eq 'inglobal' || $type eq 'inglobalfunc')
777                                                 {
778                                                         if($state->{$ofs})
779                                                         {
780                                                                 $isread = 1;
781                                                                 return -1; # exit TOTALLY
782                                                         }
783                                                 }
784                                                 elsif($type eq 'inglobalvec')
785                                                 {
786                                                         if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
787                                                         {
788                                                                 $isread = 1;
789                                                                 return -1; # exit TOTALLY
790                                                         }
791                                                 }
792                                                 elsif($type eq 'outglobal')
793                                                 {
794                                                         delete $state->{$ofs};
795                                                         return 1
796                                                                 if !%$state;
797                                                 }
798                                                 elsif($type eq 'outglobalvec')
799                                                 {
800                                                         delete $state->{$ofs};
801                                                         delete $state->{$ofs+1};
802                                                         delete $state->{$ofs+2};
803                                                         return 1
804                                                                 if !%$state;
805                                                 }
806                                         }
807                                         return 0;
808                                 };
809
810                         if(!$isread)
811                         {
812                                 print "; Value is never used in $func->{debugname} at $ip.$operand\n";
813                                 ++$warned{$ip}{$operand};
814                         }
815                 }
816         }
817         
818         disassemble_function($progs, $func, \%warned)
819                 if keys %warned;
820 }
821
822 use constant DEFAULTGLOBALS => [
823         "OFS_NULL",
824         "OFS_RETURN",
825         "OFS_RETURN[1]",
826         "OFS_RETURN[2]",
827         "OFS_PARM0",
828         "OFS_PARM0[1]",
829         "OFS_PARM0[2]",
830         "OFS_PARM1",
831         "OFS_PARM1[1]",
832         "OFS_PARM1[2]",
833         "OFS_PARM2",
834         "OFS_PARM2[1]",
835         "OFS_PARM2[2]",
836         "OFS_PARM3",
837         "OFS_PARM3[1]",
838         "OFS_PARM3[2]",
839         "OFS_PARM4",
840         "OFS_PARM4[1]",
841         "OFS_PARM4[2]",
842         "OFS_PARM5",
843         "OFS_PARM5[1]",
844         "OFS_PARM5[2]",
845         "OFS_PARM6",
846         "OFS_PARM6[1]",
847         "OFS_PARM6[2]",
848         "OFS_PARM7",
849         "OFS_PARM7[1]",
850         "OFS_PARM7[2]"
851 ];
852
853 sub defaultglobal($)
854 {
855         my ($ofs) = @_;
856         if($ofs < @{(DEFAULTGLOBALS)})
857         {
858                 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
859         }
860         return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
861 }
862
863 sub detect_constants($)
864 {
865         my ($progs) = @_;
866         use constant GLOBALFLAG_R => 1; # read
867         use constant GLOBALFLAG_W => 2; # written
868         use constant GLOBALFLAG_S => 4; # saved
869         use constant GLOBALFLAG_I => 8; # initialized
870         use constant GLOBALFLAG_N => 16; # named
871         use constant GLOBALFLAG_Q => 32; # unique to function
872         use constant GLOBALFLAG_U => 64; # unused
873         my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x @{$progs->{globals}};
874
875         for(@{$progs->{functions}})
876         {
877                 for(keys %{$_->{globals_used}})
878                 {
879                         if($globalflags[$_] & GLOBALFLAG_Q)
880                         {
881                                 $globalflags[$_] &= ~GLOBALFLAG_Q;
882                         }
883                         else
884                         {
885                                 $globalflags[$_] &= ~GLOBALFLAG_U;
886                         }
887                 }
888                 $globalflags[$_] |= GLOBALFLAG_R
889                         for keys %{$_->{globals_read}};
890                 $globalflags[$_] |= GLOBALFLAG_W
891                         for keys %{$_->{globals_written}};
892         }
893
894         my %offsets_saved = ();
895         for(@{$progs->{globaldefs}})
896         {
897                 my $type = $_->{type};
898                 my $name = $progs->{getstring}->($_->{s_name});
899                 if($type->{save})
900                 {
901                         for my $i(0..(typesize($_->{type}{type})-1))
902                         {
903                                 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
904                         }
905                 }
906                 if($name ne "")
907                 {
908                         for my $i(0..(typesize($_->{type}{type})-1))
909                         {
910                                 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
911                         }
912                 }
913         }
914         my %offsets_initialized = ();
915         for(0..(@{$progs->{globals}}-1))
916         {
917                 if($progs->{globals}[$_]{v}{int})
918                 {
919                         $globalflags[$_] |= GLOBALFLAG_I;
920                 }
921         }
922
923         my @globaltypes = (undef) x @{$progs->{globals}};
924
925         my %istemp = ();
926         for(0..(@{$progs->{globals}}-1))
927         {
928                 next
929                         if $_ < @{(DEFAULTGLOBALS)};
930                 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
931                 {
932                         $globaltypes[$_] = "unused";
933                 }
934                 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
935                 {
936                         # so it is ro
937                         if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
938                         {
939                                 $globaltypes[$_] = "read_only";
940                         }
941                         elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
942                         {
943                                 $globaltypes[$_] = "const";
944                         }
945                         else
946                         {
947                                 $globaltypes[$_] = "read_only";
948                         }
949                 }
950                 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
951                 {
952                         $globaltypes[$_] = "write_only";
953                 }
954                 else
955                 {
956                         # now we know it is rw
957                         if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
958                         {
959                                 $globaltypes[$_] = "global";
960                         }
961                         elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
962                         {
963                                 $globaltypes[$_] = "temp";
964                                 ++$istemp{$_};
965                         }
966                         elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
967                         {
968                                 $globaltypes[$_] = "not_saved";
969                         }
970                         else
971                         {
972                                 $globaltypes[$_] = "global";
973                         }
974                 }
975         }
976         $progs->{temps} = \%istemp;
977
978         # globaldefs
979         my @globaldefs = (undef) x @{$progs->{globaldefs}};
980         for(@{$progs->{globaldefs}})
981         {
982                 my $s = $progs->{getstring}->($_->{s_name});
983                 $_->{debugname} //= "\$" . "$s"
984                         if length $s;
985         }
986         for(@{$progs->{globaldefs}})
987         {
988                 $globaldefs[$_->{ofs}] //= $_
989                         if defined $_->{debugname};
990         }
991         for(@{$progs->{globaldefs}})
992         {
993                 $globaldefs[$_->{ofs}] //= $_;
994         }
995         for(0..(@{$progs->{globals}}-1))
996         {
997                 $globaldefs[$_] //= {
998                         ofs => $_,
999                         s_name => undef,
1000                         debugname => undef
1001                 };
1002         }
1003         for(0..(@{(DEFAULTGLOBALS)}-1))
1004         {
1005                 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1006                 $globaltypes[$_] = 'defglobal';
1007         }
1008         my %globaldefs_namecount = ();
1009         for(@globaldefs)
1010         {
1011                 $_->{globaltype} = $globaltypes[$_->{ofs}];
1012                 if(defined $_->{debugname})
1013                 {
1014                         # already has debugname
1015                 }
1016                 elsif($_->{globaltype} eq 'const')
1017                 {
1018                         $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1019                 }
1020                 else
1021                 {
1022                         $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1023                 }
1024                 ++$globaldefs_namecount{$_->{debugname}};
1025         }
1026         for(@globaldefs)
1027         {
1028                 next
1029                         if $globaldefs_namecount{$_->{debugname}} <= 1;
1030                 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1031                 $_->{debugname} .= "\@$_->{ofs}";
1032         }
1033         $progs->{globaldef_byoffset} = sub
1034         {
1035                 my ($ofs) = @_;
1036                 my $def = $globaldefs[$ofs];
1037                 return $def;
1038         };
1039 }
1040
1041 sub parse_progs($)
1042 {
1043         my ($fh) = @_;
1044
1045         my %p = ();
1046
1047         print STDERR "Parsing header...\n";
1048         $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1049         
1050         print STDERR "Parsing strings...\n";
1051         $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1052         $p{getstring} = sub
1053         {
1054                 my ($startpos) = @_;
1055                 my $endpos = index $p{strings}, "\0", $startpos;
1056                 return substr $p{strings}, $startpos, $endpos - $startpos;
1057         };
1058
1059         print STDERR "Parsing statements...\n";
1060         $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1061
1062         print STDERR "Fixing statements...\n";
1063         for my $s(@{$p{statements}})
1064         {
1065                 my $c = checkop $s->{op};
1066
1067                 for(qw(a b c))
1068                 {
1069                         my $type = $c->{$_};
1070                         next
1071                                 unless defined $type;
1072
1073                         if($type eq 'inglobal' || $type eq 'inglobalfunc')
1074                         {
1075                                 $s->{$_} &= 0xFFFF;
1076                         }
1077                         elsif($type eq 'inglobalvec')
1078                         {
1079                                 $s->{$_} &= 0xFFFF;
1080                         }
1081                         elsif($type eq 'outglobal')
1082                         {
1083                                 $s->{$_} &= 0xFFFF;
1084                         }
1085                         elsif($type eq 'outglobalvec')
1086                         {
1087                                 $s->{$_} &= 0xFFFF;
1088                         }
1089                 }
1090
1091         }
1092
1093         print STDERR "Parsing globaldefs...\n";
1094         $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1095
1096         print STDERR "Parsing fielddefs...\n";
1097         $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1098
1099         print STDERR "Parsing globals...\n";
1100         $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1101
1102         print STDERR "Parsing functions...\n";
1103         $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1104
1105         print STDERR "Looking for error()...\n";
1106         $p{error_func} = {};
1107         for(@{$p{globaldefs}})
1108         {
1109                 next
1110                         if $p{getstring}($_->{s_name}) ne 'error';
1111                 my $v = $p{globals}[$_->{ofs}]{v}{int};
1112                 next
1113                         if $v <= 0 || $v >= @{$p{functions}};
1114                 my $first = $p{functions}[$v]{first_statement};
1115                 next
1116                         if $first >= 0;
1117                 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1118                 $p{error_func}{$_->{ofs}} = 1;
1119         }
1120
1121         print STDERR "Scanning functions...\n";
1122         for(@{$p{functions}})
1123         {
1124                 my $file = $p{getstring}->($_->{s_file});
1125                 my $name = $p{getstring}->($_->{s_name});
1126                 $name = "$file:$name"
1127                         if length $file;
1128                 $_->{debugname} = $name;
1129
1130                 next
1131                         if $_->{first_statement} < 0;
1132
1133                 my %statements = ();
1134                 my %come_from = ();
1135                 my %go_to = ();
1136                 my %globals_read = ();
1137                 my %globals_written = ();
1138                 my %globals_used = ();
1139
1140                 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1141                         sub
1142                         {
1143                                 my ($ip, $state, $s, $c) = @_;
1144                                 ++$statements{$ip};
1145
1146                                 if(my $j = $c->{isjump})
1147                                 {
1148                                         my $t = $ip + $s->{$j};
1149                                         $come_from{$t}{$ip} = $c->{isconditional};
1150                                         $go_to{$ip}{$t} = $c->{isconditional};
1151                                 }
1152
1153                                 for my $o(qw(a b c))
1154                                 {
1155                                         my $type = $c->{$o}
1156                                                 or next;
1157                                         my $ofs = $s->{$o};
1158
1159                                         my $read = sub
1160                                         {
1161                                                 my ($ofs) = @_;
1162                                                 $globals_read{$ofs}{$ip}{$o} = 1;
1163                                                 $globals_used{$ofs} = 1;
1164                                         };
1165                                         my $write = sub
1166                                         {
1167                                                 my ($ofs) = @_;
1168                                                 $globals_written{$ofs}{$ip}{$o} = 1;
1169                                                 $globals_used{$ofs} = 1;
1170                                         };
1171
1172                                         if($type eq 'inglobal' || $type eq 'inglobalfunc')
1173                                         {
1174                                                 $read->($ofs);
1175                                         }
1176                                         elsif($type eq 'inglobalvec')
1177                                         {
1178                                                 $read->($ofs);
1179                                                 $read->($ofs+1);
1180                                                 $read->($ofs+2);
1181                                         }
1182                                         elsif($type eq 'outglobal')
1183                                         {
1184                                                 $write->($ofs);
1185                                         }
1186                                         elsif($type eq 'outglobalvec')
1187                                         {
1188                                                 $write->($ofs);
1189                                                 $write->($ofs+1);
1190                                                 $write->($ofs+2);
1191                                         }
1192                                 }
1193
1194                                 return 0;
1195                         };
1196
1197                 $_->{statements} = \%statements;
1198                 $_->{come_from} = \%come_from;
1199                 $_->{go_to} = \%go_to;
1200                 $_->{globals_read} = \%globals_read;
1201                 $_->{globals_written} = \%globals_written;
1202                 $_->{globals_used} = \%globals_used;
1203
1204                 # using this info, we could now identify basic blocks
1205         }
1206
1207         print STDERR "Detecting constants and temps, and naming...\n";
1208         detect_constants \%p;
1209
1210         # what do we want to do?
1211         my $checkfunc = \&find_uninitialized_locals;
1212         #my $checkfunc = \&disassemble_function;
1213         for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
1214         {
1215                 $checkfunc->(\%p, $_);
1216         }
1217 }
1218
1219 open my $fh, '<', $ARGV[0];
1220 parse_progs $fh;