13 return sub { $_[0] >= (2**($bits-1)) ? $_[0]-(2**$bits) : $_[0]; };
16 use constant OPCODE_E => [qw[
18 MUL_F MUL_V MUL_FV MUL_VF
22 EQ_F EQ_V EQ_S EQ_E EQ_FNC
23 NE_F NE_V NE_S NE_E NE_FNC
25 LOAD_F LOAD_V LOAD_S LOAD_ENT LOAD_FLD LOAD_FNC
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
30 NOT_F NOT_V NOT_S NOT_ENT NOT_FNC
32 CALL0 CALL1 CALL2 CALL3 CALL4 CALL5 CALL6 CALL7 CALL8
38 use constant ETYPE_E => [qw[
48 use constant DEF_SAVEGLOBAL => 32768;
52 return 3 if $type eq 'vector';
61 return { a => 'inglobalvec', b => 'ipoffset', isjump => 'b', isconditional => 1 };
65 return { a => 'inglobal', b => 'ipoffset', isjump => 'b', isconditional => 1 };
69 return { a => 'ipoffset', isjump => 'a', isconditional => 0 };
71 if($op =~ /^ADD_V$|^SUB_V$/)
73 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobalvec' };
75 if($op =~ /^MUL_V$|^EQ_V$|^NE_V$/)
77 return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobal' };
81 return { a => 'inglobal', b => 'inglobalvec', c => 'outglobalvec' };
85 return { a => 'inglobalvec', b => 'inglobal', c => 'outglobalvec' };
89 return { a => 'inglobal', b => 'inglobal', c => 'outglobalvec' };
93 return { a => 'inglobalvec', c => 'outglobal' };
97 return { a => 'inglobal', c => 'outglobal' };
101 return { a => 'inglobalvec', b => 'inglobal' };
105 return { a => 'inglobalvec', b => 'outglobalvec' };
107 if($op =~ /^STOREP_/)
109 return { a => 'inglobal', b => 'inglobal' };
113 return { a => 'inglobal', b => 'outglobal' };
117 return { a => 'inglobalfunc', iscall => 1 };
119 if($op =~ /^DONE$|^RETURN$/)
121 return { a => 'inglobalvec', isreturn => 1 };
123 return { a => 'inglobal', b => 'inglobal', c => 'outglobal' };
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) }; }],
137 use constant DPROGRAMS_T => [
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']
155 use constant DSTATEMENT_T => [
162 use constant DDEF_T => [
168 use constant DGLOBAL_T => [
172 use constant DFUNCTION_T => [
173 [int => 'first_statement'],
174 [int => 'parm_start'],
180 [uchar8 => 'parm_size'],
185 my ($fh, $start, $len) = @_;
188 $len == read $fh, my $buf, $len
193 sub parse_section($$$$$)
195 my ($fh, $struct, $start, $len, $cnt) = @_;
198 $itemlen += TYPES->{$_->[0]}->[1]
200 my $packspec = join '', map { TYPES->{$_->[0]}->[0]; } @$struct;
201 my @packnames = map { $_->[1]; } @$struct;
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;
216 $itemlen == read $fh, my $buf, $itemlen
219 @h{@packnames} = unpack $packspec, $buf;
220 $h{$_->[1]} = TYPES->{$_->[0]}->[2]->($h{$_->[1]})
230 sub nfa_default_state_checker()
235 my ($ip, $state) = @_;
236 return $seen{"$ip $state"}++;
242 my ($progs, $ip, $state, $copy_handler, $state_checker, $instruction_handler) = @_;
244 my $statements = $progs->{statements};
249 no warnings 'recursion';
251 my ($ip, $state) = @_;
257 if $state_checker->($ip, $state);
259 my $s = $statements->[$ip];
260 my $c = checkop $s->{op};
262 if(($ret = $instruction_handler->($ip, $state, $s, $c)))
275 my $funcid = $progs->{globals}[$func]{v}{int};
277 if $progs->{error_func}{$funcid};
282 if($c->{isconditional})
286 if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
294 $nfa->($ip+1, $copy_handler->($state));
295 $ip += $s->{$c->{isjump}};
300 $ip += $s->{$c->{isjump}};
312 $nfa->($ip, $copy_handler->($state));
317 my ($progs, $g) = @_;
322 elsif($g->{int} > 0 && $g->{int} < 8388608)
324 if($g->{int} < length $progs->{strings} && $g->{int} > 0)
326 return str($progs->{getstring}->($g->{int}));
330 return $g->{int} . "i";
339 use constant PRE_MARK_STATEMENT => "";
340 use constant POST_MARK_STATEMENT => "";
341 use constant PRE_MARK_OPERAND => "*** ";
342 use constant POST_MARK_OPERAND => " ***";
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";
352 $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
356 sub disassemble_function($$;$)
358 my ($progs, $func, $highlight) = @_;
360 print "$func->{debugname}:\n";
362 my $initializer = sub
365 my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
370 printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
371 printf OPERAND_FORMAT, "$func->{parm_start}";
372 print INSTRUCTION_SEPARATOR;
374 printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
375 printf OPERAND_FORMAT, "$func->{locals}";
376 print INSTRUCTION_SEPARATOR;
378 my %override_locals = ();
379 my $p = $func->{parm_start};
380 for(0..($func->{numparms}-1))
382 $override_locals{$p} //= "argv_$_";
383 for my $comp(0..($func->{parm_size}[$_]-1))
385 $override_locals{$p} //= "argv_$_\[$comp]";
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;
394 for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
397 if exists $override_locals{$_};
398 $override_locals{$_} = "local_$_";
400 printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
401 printf OPERAND_FORMAT, "local_$_";
403 print INSTRUCTION_SEPARATOR;
409 return $override_locals{$ofs}
410 if exists $override_locals{$ofs};
411 my $def = $progs->{globaldef_byoffset}->($ofs);
412 return $def->{debugname};
417 my ($ip, $type, $operand) = @_;
418 if($type eq 'inglobal')
420 my $name = $getname->($operand);
421 printf OPERAND_FORMAT, "$name";
423 elsif($type eq 'outglobal')
425 my $name = $getname->($operand);
426 printf OPERAND_FORMAT, "&$name";
428 elsif($type eq 'inglobalvec')
430 my $name = $getname->($operand);
431 printf OPERAND_FORMAT, "$name\[\]";
433 elsif($type eq 'outglobalvec')
435 my $name = $getname->($operand);
436 printf OPERAND_FORMAT, "&$name\[\]";
438 elsif($type eq 'inglobalfunc')
440 my $name = $getname->($operand);
441 printf OPERAND_FORMAT, "$name()";
443 elsif($type eq 'ipoffset')
445 printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
449 die "unknown type: $type";
453 my $statements = $func->{statements};
454 my $come_from = $func->{come_from};
457 for my $ip(sort { $a <=> $b } keys %$statements)
459 if($ip == $func->{first_statement})
461 printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
462 print INSTRUCTION_SEPARATOR;
464 if(defined $ipprev && $ip != $ipprev + 1)
466 printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
467 printf OPERAND_FORMAT, $ip - $ipprev - 1;
468 print INSTRUCTION_SEPARATOR;
470 if(my $cf = $come_from->{$ip})
472 printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
474 for(sort { $a <=> $b } keys %$cf)
476 print OPERAND_SEPARATOR
478 printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
480 print INSTRUCTION_SEPARATOR;
483 my $op = $progs->{statements}[$ip]{op};
484 my $ipt = $progs->{statements}[$ip];
485 my $opprop = checkop $op;
487 if($highlight and $highlight->{$ip})
489 for(values %{$highlight->{$ip}})
493 print PRE_MARK_STATEMENT;
494 printf INSTRUCTION_FORMAT, '', '', '.WARN';
495 printf OPERAND_FORMAT, "$_ (in $func->{debugname})";
496 print INSTRUCTION_SEPARATOR;
501 print PRE_MARK_STATEMENT
502 if $highlight and $highlight->{$ip};
504 my $showip = $opprop->{isjump};
505 printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? "<!>" : "", $op;
511 if not defined $opprop->{$o};
512 print OPERAND_SEPARATOR
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};
521 print POST_MARK_STATEMENT
522 if $highlight and $highlight->{$ip};
524 print INSTRUCTION_SEPARATOR;
528 sub find_uninitialized_locals($$)
530 my ($progs, $func) = @_;
534 if $func->{first_statement} < 0; # builtin
536 print STDERR "Checking $func->{debugname}...\n";
538 my $p = $func->{parm_start};
539 for(0..($func->{numparms}-1))
541 $p += $func->{parm_size}[$_];
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));
550 for(keys %{$progs->{temps}})
553 if exists $watchme{$_};
554 if($progs->{temps}{$_})
557 $watchme{$_} = WATCHME_T | WATCHME_X
562 $watchme{$_} = WATCHME_X
566 $watchme{$_} |= WATCHME_R
567 for keys %{$func->{globals_read}};
568 $watchme{$_} |= WATCHME_W
569 for keys %{$func->{globals_written}};
571 my %write_places = ();
572 for my $ofs(keys %{$func->{globals_written}})
575 unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
576 for my $ip(keys %{$func->{globals_written}{$ofs}})
578 for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
580 push @{$write_places{$ip}{$op}}, $ofs;
588 if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
592 if not keys %watchme;
597 flags => $watchme{$_},
598 valid => [0, undef, undef]
602 # mark parameters as initialized
603 for($func->{parm_start} .. ($p-1))
605 $watchme{$_}{valid} = [1, undef, undef]
606 if defined $watchme{$_};
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
612 for($func->{first_statement} .. (@{$progs->{statements}}-1))
614 my $s = $progs->{statements}[$_];
615 if($s->{op} eq 'STORE_V')
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};
624 elsif($s->{op} =~ /^STORE_/)
626 $watchme{$s->{a}}{valid} = [1, undef, undef]
627 if defined $watchme{$s->{a}};
637 run_nfa $progs, $func->{first_statement}, \%watchme,
640 return { map { $_ => { %{$h->{$_}} } } keys %$h };
643 my ($ip, $state) = @_;
645 my $s = $ip_seen{$ip};
648 # if $state is stronger or equal to $s, return 1
652 if($state->{$_}{valid}[0] < $s->{$_})
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].
658 $s->{$_} = $state->{$_}{valid}[0]
659 if $state->{$_}{valid}[0] < $s->{$_};
664 # if we get here, $state is stronger or equal. No need to try it.
669 # Never seen this IP yet.
670 $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
675 my ($ip, $state, $s, $c) = @_;
678 # QCVM BUG: RETURN always takes vector, there is no float equivalent
679 my $return_hack = $c->{isreturn} // 0;
683 # COMPILER BUG of QCC: params are always copied using STORE_V
684 if($s->{b} >= 4 && $s->{b} < 28) # parameter range
694 unless defined $type;
704 if not exists $state->{$ofs};
705 my $valid = $state->{$ofs}{valid};
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'))
711 push @{$warned{$ip}{$_}}, "Use of uninitialized value";
714 elsif($valid->[0] < 0)
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'))
719 push @{$warned{$ip}{$_}}, "Use of temporary across CALL";
725 if(defined $valid->[1])
727 delete $write_places{$valid->[1]}{$valid->[2]};
734 $state->{$ofs}{valid} = [1, $ip, $_]
735 if exists $state->{$ofs};
738 if($type eq 'inglobal' || $type eq 'inglobalfunc')
742 elsif($type eq 'inglobalvec')
748 elsif($type eq 'outglobal')
752 elsif($type eq 'outglobalvec')
761 # builtin calls may clobber stuff
763 my $funcid = $progs->{globals}[$func]{v}{int};
764 my $funcobj = $progs->{functions}[$funcid];
765 if(!$funcobj || $funcobj->{first_statement} >= 0)
770 if($_->{flags} & WATCHME_T)
772 $_->{valid} = [-1, undef, undef];
781 for my $ip(keys %write_places)
783 for my $operand(keys %{$write_places{$ip}})
786 my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
789 my %writeplace_seen = ();
790 run_nfa $progs, $ip+1, \%left,
797 my ($ip, $state) = @_;
798 return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
802 my ($ip, $state, $s, $c) = @_;
807 unless defined $type;
810 if($type eq 'inglobal' || $type eq 'inglobalfunc')
815 return -1; # exit TOTALLY
818 elsif($type eq 'inglobalvec')
820 if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
823 return -1; # exit TOTALLY
826 elsif($type eq 'outglobal')
828 delete $state->{$ofs};
832 elsif($type eq 'outglobalvec')
834 delete $state->{$ofs};
835 delete $state->{$ofs+1};
836 delete $state->{$ofs+2};
846 push @{$warned{$ip}{$operand}}, "Value is never used";
851 disassemble_function($progs, $func, \%warned)
855 use constant DEFAULTGLOBALS => [
889 if($ofs < @{(DEFAULTGLOBALS)})
891 return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
893 return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
896 sub detect_constants($)
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}};
908 for(@{$progs->{functions}})
910 for(keys %{$_->{globals_used}})
912 if($globalflags[$_] & GLOBALFLAG_U)
914 $globalflags[$_] &= ~GLOBALFLAG_U;
916 elsif($globalflags[$_] & GLOBALFLAG_Q)
918 $globalflags[$_] &= ~GLOBALFLAG_Q;
921 $globalflags[$_] |= GLOBALFLAG_R
922 for keys %{$_->{globals_read}};
923 $globalflags[$_] |= GLOBALFLAG_W
924 for keys %{$_->{globals_written}};
927 my %offsets_saved = ();
928 for(@{$progs->{globaldefs}})
930 my $type = $_->{type};
931 my $name = $progs->{getstring}->($_->{s_name});
934 for my $i(0..(typesize($_->{type}{type})-1))
936 $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
941 for my $i(0..(typesize($_->{type}{type})-1))
943 $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
947 my %offsets_initialized = ();
948 for(0..(@{$progs->{globals}}-1))
950 if($progs->{globals}[$_]{v}{int})
952 $globalflags[$_] |= GLOBALFLAG_I;
956 my @globaltypes = (undef) x @{$progs->{globals}};
959 for(0..(@{$progs->{globals}}-1))
962 if $_ < @{(DEFAULTGLOBALS)};
963 if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
965 $globaltypes[$_] = "unused";
967 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
970 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
972 $globaltypes[$_] = "read_only";
974 elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
976 $globaltypes[$_] = "const";
980 $globaltypes[$_] = "read_only";
983 elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
985 $globaltypes[$_] = "write_only";
989 # now we know it is rw
990 if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
992 $globaltypes[$_] = "global";
994 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I | GLOBALFLAG_Q)) == GLOBALFLAG_Q)
996 $globaltypes[$_] = "uniquetemp";
999 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I | GLOBALFLAG_Q)) == 0)
1001 $globaltypes[$_] = "temp";
1004 elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
1006 $globaltypes[$_] = "not_saved";
1010 $globaltypes[$_] = "global";
1014 $progs->{temps} = \%istemp;
1017 my @globaldefs = (undef) x @{$progs->{globaldefs}};
1018 for(@{$progs->{globaldefs}})
1020 my $s = $progs->{getstring}->($_->{s_name});
1021 $_->{debugname} //= "\$" . "$s"
1024 for(@{$progs->{globaldefs}})
1026 $globaldefs[$_->{ofs}] //= $_
1027 if defined $_->{debugname};
1029 for(@{$progs->{globaldefs}})
1031 $globaldefs[$_->{ofs}] //= $_;
1033 for(0..(@{$progs->{globals}}-1))
1035 $globaldefs[$_] //= {
1041 for(0..(@{(DEFAULTGLOBALS)}-1))
1043 $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
1044 $globaltypes[$_] = 'defglobal';
1046 my %globaldefs_namecount = ();
1049 $_->{globaltype} = $globaltypes[$_->{ofs}];
1050 if(defined $_->{debugname})
1052 # already has debugname
1054 elsif($_->{globaltype} eq 'const')
1056 $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
1060 $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
1062 ++$globaldefs_namecount{$_->{debugname}};
1067 if $globaldefs_namecount{$_->{debugname}} <= 1;
1068 #print "Not unique: $_->{debugname} at $_->{ofs}\n";
1069 $_->{debugname} .= "\@$_->{ofs}";
1071 $progs->{globaldef_byoffset} = sub
1074 my $def = $globaldefs[$ofs];
1085 print STDERR "Parsing header...\n";
1086 $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
1088 print STDERR "Parsing strings...\n";
1089 $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
1092 my ($startpos) = @_;
1093 my $endpos = index $p{strings}, "\0", $startpos;
1094 return substr $p{strings}, $startpos, $endpos - $startpos;
1097 print STDERR "Parsing statements...\n";
1098 $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
1100 print STDERR "Fixing statements...\n";
1101 for my $s(@{$p{statements}})
1103 my $c = checkop $s->{op};
1107 my $type = $c->{$_};
1109 unless defined $type;
1111 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1115 elsif($type eq 'inglobalvec')
1119 elsif($type eq 'outglobal')
1123 elsif($type eq 'outglobalvec')
1131 print STDERR "Parsing globaldefs...\n";
1132 $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
1134 print STDERR "Parsing fielddefs...\n";
1135 $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
1137 print STDERR "Parsing globals...\n";
1138 $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
1140 print STDERR "Parsing functions...\n";
1141 $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
1143 print STDERR "Looking for error()...\n";
1144 $p{error_func} = {};
1145 for(@{$p{globaldefs}})
1148 if $p{getstring}($_->{s_name}) ne 'error';
1149 my $v = $p{globals}[$_->{ofs}]{v}{int};
1151 if $v <= 0 || $v >= @{$p{functions}};
1152 my $first = $p{functions}[$v]{first_statement};
1155 print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
1156 $p{error_func}{$_->{ofs}} = 1;
1159 print STDERR "Scanning functions...\n";
1160 for(@{$p{functions}})
1162 my $file = $p{getstring}->($_->{s_file});
1163 my $name = $p{getstring}->($_->{s_name});
1164 $name = "$file:$name"
1166 $_->{debugname} = $name;
1169 if $_->{first_statement} < 0;
1171 my %statements = ();
1174 my %globals_read = ();
1175 my %globals_written = ();
1176 my %globals_used = ();
1178 run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
1181 my ($ip, $state, $s, $c) = @_;
1184 if(my $j = $c->{isjump})
1186 my $t = $ip + $s->{$j};
1187 $come_from{$t}{$ip} = $c->{isconditional};
1188 $go_to{$ip}{$t} = $c->{isconditional};
1191 for my $o(qw(a b c))
1200 $globals_read{$ofs}{$ip}{$o} = 1;
1201 $globals_used{$ofs} = 1;
1206 $globals_written{$ofs}{$ip}{$o} = 1;
1207 $globals_used{$ofs} = 1;
1210 if($type eq 'inglobal' || $type eq 'inglobalfunc')
1214 elsif($type eq 'inglobalvec')
1220 elsif($type eq 'outglobal')
1224 elsif($type eq 'outglobalvec')
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;
1242 # using this info, we could now identify basic blocks
1245 print STDERR "Detecting constants and temps, and naming...\n";
1246 detect_constants \%p;
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}})
1253 $checkfunc->(\%p, $_);
1257 open my $fh, '<', $ARGV[0];