-
Notifications
You must be signed in to change notification settings - Fork 4
/
c2v
executable file
·143 lines (126 loc) · 3 KB
/
c2v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
#
# Cryptol to acirc optimized by ABC
#
set -e
scriptdir=$(dirname $(realpath "${BASH_SOURCE[0]}"))
liberty_file=$scriptdir/../liberties/gc_cells.lib
opt_binary=""
ty="acirc"
usage () {
echo "C2V: Cryptol to acirc through Verilog"
echo "Usage: $0 [options] <cryptol-file> <function>"
echo " -b output binary netlist instead of acirc"
echo " -l LIBERTY use liberty file LIBERTY"
echo " -t TYPE type of circuit to produce from cxs [default=$ty]"
echo " -Y skip yosys step"
exit $1
}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-b)
opt_binary=1
shift
;;
-l)
liberty_file="$scriptdir/../$2"
shift
shift
;;
-h)
usage 0
;;
-t)
ty=$2
shift
shift
;;
-Y)
skip_yosys=1
shift
;;
*)
POSITIONAL+=("$1")
shift
esac
done
set -- "${POSITIONAL[@]}"
if [[ x$1 = x ]] || ([[ x$2 = x ]] && [[ ${1##*.} != 'aig' ]]); then
usage 1
fi
if ! [ -f cxs ]; then
echo "[c2a] ERROR: cxs not present! maybe you should run ./build_cxs.sh" >/dev/stderr
exit 1
fi
cxs=$(realpath cxs)
filename=$(realpath $1)
echo "filename = $filename"
if [[ ${1##*.} == 'aig' ]]; then
file=$(basename $1)
func=${file%.*}
else
func=$2
fi
dir=$(pwd)
tmp=$(mktemp -d)
cd $tmp
echo "temp dir: $tmp"
cat > sawcmds.saw <<- EOM
m <- cryptol_load "$filename";
f <- cryptol_extract m "$func";
write_aig "${func}.aig" f;
EOM
if [[ "$skip_yosys" ]]; then
cat > abccmds.abc <<- EOM
read "${func}.aig"
write "${func}.bench"
EOM
else
cat > abccmds.abc <<- EOM
read "${func}.aig"
write_verilog "${func}.in.v"
EOM
fi
cat > yosys.ys <<- EOM
read_verilog "${func}.in.v"
synth -run fine
techmap; opt -fast
dfflibmap -liberty ${liberty_file}
abc -liberty ${liberty_file}
write_verilog ${func}.out.v
EOM
extension="${filename##*.}"
if [[ $extension == 'cry' ]]; then
sizes=($(egrep "^$func\\s*:\\s*" $filename | perl -ne \
'if (/: \(\[(\d+)],\s*\[(\d+)\]\) ->/) { print "$1 $2"; } elsif (/: \[(\d+)\] ->/) { print "$1 0"; }' \
))
inputsize=${sizes[0]}
keysize=${sizes[1]}
fixed_input=""
for i in $(seq $inputsize $((inputsize + keysize - 1))); do
fixed_input+=" $i:0"
done
echo "running saw..."
saw sawcmds.saw
elif [[ $extension == 'aig' ]]; then
cp $filename .
fi
echo "running abc..."
abc -f abccmds.abc
if [[ ! "$skip_yosys" ]]; then
echo "running yosys..."
yosys yosys.ys
fi
if [ "$opt_binary" ]; then
cat $func.out.v > $dir/$func.c2v.netlist
else
if [[ ! "$skip_yosys" ]]; then
$scriptdir/resolve-netlist-aliases.pl $func.out.v > $func.netlist
echo "running cxs..."
$cxs read $func.netlist -i -o $dir/$func.c2v.$ty -k "$fixed_input"
else
$cxs read $func.bench -i -o $dir/$func.c2v.$ty -k "$fixed_input"
fi
fi