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