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