forked from mit-plv/fiat-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-function.sh
executable file
·92 lines (84 loc) · 2.21 KB
/
extract-function.sh
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
#!/bin/sh
set -eu
case "$#" in
0)
funcname=f
;;
1)
funcname="$1"
;;
*)
exit 111
;;
esac
cat <<EOF
#include <stdint.h>
#include <stdbool.h>
#include <x86intrin.h>
#include "liblow.h"
#include "$funcname.h"
typedef unsigned int uint128_t __attribute__((mode(TI)));
#if (defined(__GNUC__) || defined(__GNUG__)) && !(defined(__clang__)||defined(__INTEL_COMPILER))
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81294
#define _subborrow_u32 __builtin_ia32_sbb_u32
#define _subborrow_u64 __builtin_ia32_sbb_u64
#endif
#undef force_inline
#define force_inline __attribute__((always_inline))
EOF
lines=0
show=false
brace='{ '
close_brace='}'
function_open_brace=''
if [ ! -z "${FIAT_CRYPTO_EXTRACT_FUNCTION_IS_ASM}" ]; then
function_open_brace=' {'
brace=''
close_brace=''
fi
if [ -z "${BITWIDTH}" ]; then
BITWIDTH=64
fi
while IFS= read -r line; do
case "$line" in
*"λ '"*)
echo -n "void force_inline $funcname("
echo -n "uint${BITWIDTH}_t* out"
echo "$line" | grep -owP -- '\w+\d+' | \
while IFS= read -r arg; do
echo -n ", uint${BITWIDTH}_t $arg"
done
echo -n ')'
echo "${function_open_brace}"
show=true
;;
*"Return "*|*"return "*)
i=0
echo "$line" | \
sed 's:return::g' | sed 's:Return::g' | tr -d '(' | tr -d ')' | tr , '\n' | sed 's/^\s\+//g' | \
( while IFS= read -r ret; do
echo "out[$i] = $ret;"
i=$((i+1))
done;
seq 2 "$lines" | while IFS= read -r _; do
echo -n "${close_brace}"
done
echo "}"
echo "// caller: uint${BITWIDTH}_t out[$i];" )
show=false
break
;;
*)
case "$show" in
true)
lines=$((lines+1))
line="$(printf "%s" "$line" | \
sed s':^\([^,]*\),\(\s*\)\([^ ]*\) \([^ ]*\)\(.*\)\(mulx.*\))\([; ]*\)$: \3 \4;\2\1\5_\6, \&\4)\7:' | \
sed s':^\([^,]*\) \([^, ]*\)\(\s*\),\(.*\)\(addcarryx.*\))\([; ]*\)$:\1 \2\3;\4_\5, \&\2)\6:' | \
sed s':^\([^,]*\) \([^, ]*\)\(\s*\),\(.*\)\(subborrow.*\))\([; ]*\)$:\1 \2\3;\4_\5, \&\2)\6:')"
printf "%s%s\n" "${brace}" "${line}"
;;
esac
;;
esac
done