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