Home | History | Annotate | Download | only in libtommath
      1 #!/usr/bin/perl 
      2 #
      3 # Walk through source, add labels and make classes
      4 #
      5 #use strict;
      6 
      7 my %deplist;
      8 
      9 #open class file and write preamble 
     10 open(CLASS, ">tommath_class.h") or die "Couldn't open tommath_class.h for writing\n";
     11 print CLASS "#if !(defined(LTM1) && defined(LTM2) && defined(LTM3))\n#if defined(LTM2)\n#define LTM3\n#endif\n#if defined(LTM1)\n#define LTM2\n#endif\n#define LTM1\n\n#if defined(LTM_ALL)\n";
     12 
     13 foreach my $filename (glob "bn*.c") {
     14    my $define = $filename;
     15 
     16 print "Processing $filename\n";
     17 
     18    # convert filename to upper case so we can use it as a define 
     19    $define =~ tr/[a-z]/[A-Z]/;
     20    $define =~ tr/\./_/;
     21    print CLASS "#define $define\n";
     22 
     23    # now copy text and apply #ifdef as required 
     24    my $apply = 0;
     25    open(SRC, "<$filename");
     26    open(OUT, ">tmp");
     27 
     28    # first line will be the #ifdef
     29    my $line = <SRC>;
     30    if ($line =~ /include/) {
     31       print OUT $line;
     32    } else {
     33       print OUT "#include <tommath.h>\n#ifdef $define\n$line";
     34       $apply = 1;
     35    }
     36    while (<SRC>) {
     37       if (!($_ =~ /tommath\.h/)) {
     38          print OUT $_;
     39       }
     40    }
     41    if ($apply == 1) {
     42       print OUT "#endif\n";
     43    }
     44    close SRC;
     45    close OUT;
     46 
     47    unlink($filename);
     48    rename("tmp", $filename);
     49 }
     50 print CLASS "#endif\n\n";
     51 
     52 # now do classes 
     53 
     54 foreach my $filename (glob "bn*.c") {
     55    open(SRC, "<$filename") or die "Can't open source file!\n"; 
     56 
     57    # convert filename to upper case so we can use it as a define 
     58    $filename =~ tr/[a-z]/[A-Z]/;
     59    $filename =~ tr/\./_/;
     60 
     61    print CLASS "#if defined($filename)\n";
     62    my $list = $filename;
     63 
     64    # scan for mp_* and make classes
     65    while (<SRC>) {
     66       my $line = $_;
     67       while ($line =~ m/(fast_)*(s_)*mp\_[a-z_0-9]*/) {
     68           $line = $';
     69           # now $& is the match, we want to skip over LTM keywords like
     70           # mp_int, mp_word, mp_digit
     71           if (!($& eq "mp_digit") && !($& eq "mp_word") && !($& eq "mp_int")) {
     72              my $a = $&;
     73              $a =~ tr/[a-z]/[A-Z]/;
     74              $a = "BN_" . $a . "_C";
     75              if (!($list =~ /$a/)) {
     76                 print CLASS "   #define $a\n";
     77              }
     78              $list = $list . "," . $a;
     79           }
     80       }
     81    }
     82    @deplist{$filename} = $list;
     83 
     84    print CLASS "#endif\n\n";
     85    close SRC;
     86 }
     87 
     88 print CLASS "#ifdef LTM3\n#define LTM_LAST\n#endif\n#include <tommath_superclass.h>\n#include <tommath_class.h>\n#else\n#define LTM_LAST\n#endif\n";
     89 close CLASS;
     90 
     91 #now let's make a cool call graph... 
     92 
     93 open(OUT,">callgraph.txt");
     94 $indent = 0;
     95 foreach (keys %deplist) {
     96    $list = "";
     97    draw_func(@deplist{$_});
     98    print OUT "\n\n";
     99 }
    100 close(OUT);
    101 
    102 sub draw_func()
    103 {
    104    my @funcs = split(",", $_[0]);
    105    if ($list =~ /@funcs[0]/) {
    106       return;
    107    } else {
    108       $list = $list . @funcs[0];
    109    }
    110    if ($indent == 0) { }
    111    elsif ($indent >= 1) { print OUT "|   " x ($indent - 1) . "+--->"; }
    112    print OUT @funcs[0] . "\n";   
    113    shift @funcs;
    114       my $temp = $list;
    115    foreach my $i (@funcs) {
    116       ++$indent;
    117       draw_func(@deplist{$i});
    118       --$indent;
    119    }
    120       $list = $temp;
    121 }
    122 
    123 
    124