aboutsummaryrefslogtreecommitdiff
path: root/extract-function.sh
blob: b99862405819f9ba20c3183fce71d90b96e45b13 (plain)
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
#!/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
while IFS= read -r line; do
  case "$line" in
    *"λ '"*)
      echo -n "void force_inline $funcname("
      echo -n "uint64_t* out"
      echo "$line" | grep -owP -- '\w+\d+' | \
        while IFS= read -r arg; do
          echo -n ", uint64_t $arg"
        done
        echo ')'
      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 "}"
          done
          echo "}"
          echo "// caller: uint64_t out[$i];" )
      show=false
      break
      ;;
    *)
      case "$show" in
        true)
          lines=$((lines+1))
          line="$(echo "$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:')"
          echo "{ $line"
          ;;
      esac
      ;;
  esac
done