#!/usr/bin/perl -w # Gather stats from zoltraak code. use strict; if( $#ARGV < 0 && -t STDIN ) { die "$0 {input.c} [...]\n"; } my $files_processed = 0; my $current_file = ""; my $header_lines = undef; my $header_bytes = undef; my $data_lines = undef; my $data_bytes = undef; my $data_tokens = undef; my $repeat_instructions = undef; my $output_bytes = undef; my $inside_header = undef; my $parsed_bits = undef; my $bit_index = undef; # Report stats across filename changes. sub flush_stats() { # Output CSV header on first file. if( defined($output_bytes) ) { if( $files_processed++ == 0 ) { print "file,header_lines,header_bytes,", "data_lines,data_bytes,data_tokens,repeat_instructions,", "output_bytes,expansion_ratio\n"; } my $ratio = "0"; if( $output_bytes > 0 ) { $ratio = sprintf '%.4f', ($header_bytes + $data_bytes) / $output_bytes; } print "$current_file,$header_lines,$header_bytes,", "$data_lines,$data_bytes,$data_tokens,$repeat_instructions,", "$output_bytes,$ratio\n"; } $header_lines = $header_bytes = 0; $data_lines = $data_bytes = $data_tokens = $repeat_instructions = 0; $output_bytes = 0; $inside_header = $parsed_bits = $bit_index = 0; } while( my $line = <> ) { if( $current_file ne $ARGV ) { flush_stats(); $current_file = $ARGV; } if( $inside_header ) { # Update header stats. $header_lines++; $header_bytes += length($line); if( index($line, "#endif") >= 0 ) { # Exit header block. $inside_header = 0; $bit_index = $parsed_bits = 0; } } else { if( index($line, "#ifndef") >= 0 ) { # Enter header block and update header stats. $inside_header = 1; $header_lines++; $header_bytes += length($line); } else { # Update data stats. $data_lines++; $data_bytes += length($line); # Update parser state. if( $line =~ /^\s*z/ ) { $data_tokens++; $parsed_bits |= 1 << ($bit_index++); if( $bit_index == 9 ) { $output_bytes++; $bit_index = $parsed_bits = 0; } elsif( $bit_index == 18 ) { $repeat_instructions++; $output_bytes += ($parsed_bits & 0xff) + 3; $bit_index = $parsed_bits = 0; } } else { $bit_index++; if( $bit_index >= 18 ) { die "$current_file contains incomplete instruction\n"; } } } } } flush_stats();