| 1 |
|
| 2 |
import sys, re, os |
| 3 |
|
| 4 |
SCRIPT_DIRECTORY = os.path.dirname(os.path.realpath(__file__)) |
| 5 |
GENERATED_DIRECTORY = os.path.join(SCRIPT_DIRECTORY, 'generated') |
| 6 |
EXTRACTION_DIRECTORY = os.path.realpath(os.path.join(SCRIPT_DIRECTORY, '..', '..', '..', 'ExtractionOCaml')) |
| 7 |
BITWIDTHS = ('32', '64') |
| 8 |
MKINDS = (('unsaturated_solinas', 'UnsaturatedSolinas', (' 0%nat', ' 1%nat')), ('word_by_word_montgomery', 'WordByWordMontgomery', ('',))) |
| 9 |
VKINDS = ('GallinaAxOf', 'GallinaAxComputedOf', 'PipelineOf', 'GallinaDefOf') |
| 10 |
|
| 11 |
def get_input(fname): |
| 12 |
if fname == '-': |
| 13 |
return sys.stdin.readlines() |
| 14 |
else: |
| 15 |
with open(fname, 'r') as f: |
| 16 |
return f.readlines() |
| 17 |
|
| 18 |
def filter_lines(lines): |
| 19 |
for line in lines: |
| 20 |
line = re.sub('#.*', '', line).strip(' \n').replace(' ', '') |
| 21 |
if line != '': |
| 22 |
yield line |
| 23 |
|
| 24 |
def write_if_different(fname, contents): |
| 25 |
if os.path.exists(fname): |
| 26 |
with open(fname, 'r') as f: |
| 27 |
old_contents = f.read() |
| 28 |
if old_contents == contents: return |
| 29 |
with open(fname, 'w') as f: |
| 30 |
f.write(contents) |
| 31 |
|
| 32 |
def prime_to_fname(prime, bitwidth, descr, ext=''): |
| 33 |
return 'p%s__x%s__%s%s' % (prime.replace('^', '').replace('-', 'm').replace('+', 'p').replace('*', 't'), bitwidth, descr.replace('%nat', '').replace(' ', '_'), ext) |
| 34 |
|
| 35 |
def write_files(prime): |
| 36 |
if not os.path.exists(GENERATED_DIRECTORY): os.mkdir(GENERATED_DIRECTORY) |
| 37 |
for bitwidth in BITWIDTHS: |
| 38 |
for mdescr, mkind, extra_args in MKINDS: |
| 39 |
for extra_arg in extra_args: |
| 40 |
for vkind in VKINDS: |
| 41 |
write_if_different(os.path.join(GENERATED_DIRECTORY, prime_to_fname(prime, bitwidth, mdescr + '_' + vkind + extra_arg, ext='.v')), |
| 42 |
r'''Require Import Crypto.Rewriter.PerfTesting.Core. |
| 43 |
Global Set Printing Width 1000000. |
| 44 |
Goal True. |
| 45 |
%s.perf%s "%s" %s%s. |
| 46 |
exact I. |
| 47 |
Defined. |
| 48 |
''' % (mkind, vkind, prime, bitwidth, extra_arg)) |
| 49 |
write_if_different(os.path.join(GENERATED_DIRECTORY, prime_to_fname(prime, bitwidth, mdescr + '_extracted' + extra_arg, ext='.sh')), |
| 50 |
r'''#!/usr/bin/env bash |
| 51 |
%s "%s" %s%s |
| 52 |
''' % (os.path.join(EXTRACTION_DIRECTORY, 'perf_' + mdescr), prime, bitwidth, extra_arg.replace('%nat', ''))) |
| 53 |
|
| 54 |
def write_makefile(primes): |
| 55 |
if not os.path.exists(GENERATED_DIRECTORY): os.mkdir(GENERATED_DIRECTORY) |
| 56 |
vos = ' \\\n\t'.join('src/Rewriter/PerfTesting/Specific/generated/%s' % prime_to_fname(prime, bitwidth, mdescr + '_' + vkind + extra_arg, ext='.vo') |
| 57 |
for prime in primes |
| 58 |
for bitwidth in BITWIDTHS |
| 59 |
for mdescr, mkind, extra_args in MKINDS |
| 60 |
for extra_arg in extra_args |
| 61 |
for vkind in VKINDS) |
| 62 |
shs = ' \\\n\t'.join('src/Rewriter/PerfTesting/Specific/generated/%s' % prime_to_fname(prime, bitwidth, mdescr + '_extracted' + extra_arg, ext='.sh') |
| 63 |
for prime in primes |
| 64 |
for bitwidth in BITWIDTHS |
| 65 |
for mdescr, mkind, extra_args in MKINDS |
| 66 |
for extra_arg in extra_args) |
| 67 |
|
| 68 |
|
| 69 |
with open(os.path.join(GENERATED_DIRECTORY, 'primes.mk'), 'w') as f: |
| 70 |
f.write(r'''PERF_PRIME_VOS := %s |
| 71 |
PERF_PRIME_SHS := %s |
| 72 |
''' % (vos, shs)) |
| 73 |
|
| 74 |
if __name__ == '__main__': |
| 75 |
primes = tuple(filter_lines(get_input(sys.argv[1]))) |
| 76 |
for prime in primes: write_files(prime) |
| 77 |
write_makefile(primes) |